Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 720 lines (589 sloc) 19.748 kb
c163b5c Block live migration
lirans@il.ibm.com authored
1 /*
2 * QEMU live block migration
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Liran Schour <lirans@il.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca @bonzini prepare for future GPLv2+ relicensing
bonzini authored
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
c163b5c Block live migration
lirans@il.ibm.com authored
14 */
15
16 #include "qemu-common.h"
17 #include "block_int.h"
18 #include "hw/hw.h"
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
19 #include "qemu-queue.h"
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
20 #include "qemu-timer.h"
c163b5c Block live migration
lirans@il.ibm.com authored
21 #include "block-migration.h"
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
22 #include "migration.h"
f48905d block-migration: add reference to target DriveInfo
Marcelo Tosatti authored
23 #include "blockdev.h"
c163b5c Block live migration
lirans@il.ibm.com authored
24 #include <assert.h>
25
6ea4430 @jan-kiszka block migration: Rework constants API
jan-kiszka authored
26 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
c163b5c Block live migration
lirans@il.ibm.com authored
27
28 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
29 #define BLK_MIG_FLAG_EOS 0x02
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
30 #define BLK_MIG_FLAG_PROGRESS 0x04
c163b5c Block live migration
lirans@il.ibm.com authored
31
32 #define MAX_IS_ALLOCATED_SEARCH 65536
33
34 //#define DEBUG_BLK_MIGRATION
35
36 #ifdef DEBUG_BLK_MIGRATION
d0f2c4c @moosotc Do not use dprintf
moosotc authored
37 #define DPRINTF(fmt, ...) \
c163b5c Block live migration
lirans@il.ibm.com authored
38 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
39 #else
d0f2c4c @moosotc Do not use dprintf
moosotc authored
40 #define DPRINTF(fmt, ...) \
c163b5c Block live migration
lirans@il.ibm.com authored
41 do { } while (0)
42 #endif
43
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
44 typedef struct BlkMigDevState {
45 BlockDriverState *bs;
46 int bulk_completed;
47 int shared_base;
48 int64_t cur_sector;
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
49 int64_t cur_dirty;
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
50 int64_t completed_sectors;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
51 int64_t total_sectors;
52 int64_t dirty;
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
53 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
54 unsigned long *aio_bitmap;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
55 } BlkMigDevState;
56
c163b5c Block live migration
lirans@il.ibm.com authored
57 typedef struct BlkMigBlock {
58 uint8_t *buf;
59 BlkMigDevState *bmds;
60 int64_t sector;
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
61 int nr_sectors;
c163b5c Block live migration
lirans@il.ibm.com authored
62 struct iovec iov;
63 QEMUIOVector qiov;
64 BlockDriverAIOCB *aiocb;
65 int ret;
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
66 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
c163b5c Block live migration
lirans@il.ibm.com authored
67 } BlkMigBlock;
68
69 typedef struct BlkMigState {
70 int blk_enable;
71 int shared_base;
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
72 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
73 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
c163b5c Block live migration
lirans@il.ibm.com authored
74 int submitted;
75 int read_done;
76 int transferred;
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
77 int64_t total_sector_sum;
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
78 int prev_progress;
e970ec0 Remove unused code
Liran Schour authored
79 int bulk_completed;
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
80 long double total_time;
ff5c52a @avishay-traeger Improve accuracy of block migration bandwidth calculation
avishay-traeger authored
81 long double prev_time_offset;
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
82 int reads;
c163b5c Block live migration
lirans@il.ibm.com authored
83 } BlkMigState;
84
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
85 static BlkMigState block_mig_state;
c163b5c Block live migration
lirans@il.ibm.com authored
86
13f0b67 @jan-kiszka block migration: Consolidate block transmission
jan-kiszka authored
87 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
88 {
89 int len;
90
91 /* sector number and flags */
92 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
93 | BLK_MIG_FLAG_DEVICE_BLOCK);
94
95 /* device name */
96 len = strlen(blk->bmds->bs->device_name);
97 qemu_put_byte(f, len);
98 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
99
100 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
101 }
102
25f2364 @jan-kiszka block migration: Report progress also via info migration
jan-kiszka authored
103 int blk_mig_active(void)
104 {
105 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
106 }
107
108 uint64_t blk_mig_bytes_transferred(void)
109 {
110 BlkMigDevState *bmds;
111 uint64_t sum = 0;
112
113 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
114 sum += bmds->completed_sectors;
115 }
116 return sum << BDRV_SECTOR_BITS;
117 }
118
119 uint64_t blk_mig_bytes_remaining(void)
120 {
121 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
122 }
123
124 uint64_t blk_mig_bytes_total(void)
125 {
126 BlkMigDevState *bmds;
127 uint64_t sum = 0;
128
129 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
130 sum += bmds->total_sectors;
131 }
132 return sum << BDRV_SECTOR_BITS;
133 }
134
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
135 static inline long double compute_read_bwidth(void)
136 {
137 assert(block_mig_state.total_time != 0);
155eb9a @avishay-traeger Fix integer overflow in block migration bandwidth calculation
avishay-traeger authored
138 return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
139 }
140
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
141 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
142 {
143 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
144
62155e2 block migration: do not submit multiple AIOs for same sector (v2)
Marcelo Tosatti authored
145 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
146 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
147 (1UL << (chunk % (sizeof(unsigned long) * 8))));
148 } else {
149 return 0;
150 }
151 }
152
153 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
154 int nb_sectors, int set)
155 {
156 int64_t start, end;
157 unsigned long val, idx, bit;
158
159 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
160 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
161
162 for (; start <= end; start++) {
163 idx = start / (sizeof(unsigned long) * 8);
164 bit = start % (sizeof(unsigned long) * 8);
165 val = bmds->aio_bitmap[idx];
166 if (set) {
62155e2 block migration: do not submit multiple AIOs for same sector (v2)
Marcelo Tosatti authored
167 val |= 1UL << bit;
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
168 } else {
62155e2 block migration: do not submit multiple AIOs for same sector (v2)
Marcelo Tosatti authored
169 val &= ~(1UL << bit);
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
170 }
171 bmds->aio_bitmap[idx] = val;
172 }
173 }
174
175 static void alloc_aio_bitmap(BlkMigDevState *bmds)
176 {
177 BlockDriverState *bs = bmds->bs;
178 int64_t bitmap_size;
179
180 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
181 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
182 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
183
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
184 bmds->aio_bitmap = g_malloc0(bitmap_size);
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
185 }
186
c163b5c Block live migration
lirans@il.ibm.com authored
187 static void blk_mig_read_cb(void *opaque, int ret)
188 {
ff5c52a @avishay-traeger Improve accuracy of block migration bandwidth calculation
avishay-traeger authored
189 long double curr_time = qemu_get_clock_ns(rt_clock);
c163b5c Block live migration
lirans@il.ibm.com authored
190 BlkMigBlock *blk = opaque;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
191
c163b5c Block live migration
lirans@il.ibm.com authored
192 blk->ret = ret;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
193
ff5c52a @avishay-traeger Improve accuracy of block migration bandwidth calculation
avishay-traeger authored
194 block_mig_state.reads++;
195 block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
196 block_mig_state.prev_time_offset = curr_time;
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
197
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
198 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
199 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
200
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
201 block_mig_state.submitted--;
202 block_mig_state.read_done++;
203 assert(block_mig_state.submitted >= 0);
c163b5c Block live migration
lirans@il.ibm.com authored
204 }
205
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
206 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
207 {
57cce12 @jan-kiszka block migration: Consolidate mig_read_device_bulk into mig_save_device_b...
jan-kiszka authored
208 int64_t total_sectors = bmds->total_sectors;
209 int64_t cur_sector = bmds->cur_sector;
210 BlockDriverState *bs = bmds->bs;
c163b5c Block live migration
lirans@il.ibm.com authored
211 BlkMigBlock *blk;
13f0b67 @jan-kiszka block migration: Consolidate block transmission
jan-kiszka authored
212 int nr_sectors;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
213
57cce12 @jan-kiszka block migration: Consolidate mig_read_device_bulk into mig_save_device_b...
jan-kiszka authored
214 if (bmds->shared_base) {
b1d1085 @jan-kiszka block migration: Clean up use of total_sectors
jan-kiszka authored
215 while (cur_sector < total_sectors &&
57cce12 @jan-kiszka block migration: Consolidate mig_read_device_bulk into mig_save_device_b...
jan-kiszka authored
216 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
217 &nr_sectors)) {
c163b5c Block live migration
lirans@il.ibm.com authored
218 cur_sector += nr_sectors;
219 }
220 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
221
222 if (cur_sector >= total_sectors) {
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
223 bmds->cur_sector = bmds->completed_sectors = total_sectors;
c163b5c Block live migration
lirans@il.ibm.com authored
224 return 1;
225 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
226
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
227 bmds->completed_sectors = cur_sector;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
228
57cce12 @jan-kiszka block migration: Consolidate mig_read_device_bulk into mig_save_device_b...
jan-kiszka authored
229 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
230
6ea4430 @jan-kiszka block migration: Rework constants API
jan-kiszka authored
231 /* we are going to transfer a full block even if it is not allocated */
232 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
c163b5c Block live migration
lirans@il.ibm.com authored
233
6ea4430 @jan-kiszka block migration: Rework constants API
jan-kiszka authored
234 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
57cce12 @jan-kiszka block migration: Consolidate mig_read_device_bulk into mig_save_device_b...
jan-kiszka authored
235 nr_sectors = total_sectors - cur_sector;
c163b5c Block live migration
lirans@il.ibm.com authored
236 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
237
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
238 blk = g_malloc(sizeof(BlkMigBlock));
239 blk->buf = g_malloc(BLOCK_SIZE);
13f0b67 @jan-kiszka block migration: Consolidate block transmission
jan-kiszka authored
240 blk->bmds = bmds;
241 blk->sector = cur_sector;
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
242 blk->nr_sectors = nr_sectors;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
243
e970ec0 Remove unused code
Liran Schour authored
244 blk->iov.iov_base = blk->buf;
245 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
246 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
247
ff5c52a @avishay-traeger Improve accuracy of block migration bandwidth calculation
avishay-traeger authored
248 if (block_mig_state.submitted == 0) {
249 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
250 }
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
251
e970ec0 Remove unused code
Liran Schour authored
252 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
253 nr_sectors, blk_mig_read_cb, blk);
254 block_mig_state.submitted++;
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
255
13f0b67 @jan-kiszka block migration: Consolidate block transmission
jan-kiszka authored
256 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
257 bmds->cur_sector = cur_sector + nr_sectors;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
258
13f0b67 @jan-kiszka block migration: Consolidate block transmission
jan-kiszka authored
259 return (bmds->cur_sector >= total_sectors);
c163b5c Block live migration
lirans@il.ibm.com authored
260 }
261
262 static void set_dirty_tracking(int enable)
263 {
264 BlkMigDevState *bmds;
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
265
266 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
267 bdrv_set_dirty_tracking(bmds->bs, enable);
c163b5c Block live migration
lirans@il.ibm.com authored
268 }
269 }
270
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
271 static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
c163b5c Block live migration
lirans@il.ibm.com authored
272 {
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
273 BlkMigDevState *bmds;
792773b @jan-kiszka block migration: Skip zero-sized disks
jan-kiszka authored
274 int64_t sectors;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
275
d246673 block migration: Fix test for read-only drive
Markus Armbruster authored
276 if (!bdrv_is_read_only(bs)) {
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
277 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
31f54f2 Block migration fail, ignore error from bdrv_getlength
Shahar Havivi authored
278 if (sectors <= 0) {
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
279 return;
280 }
281
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
282 bmds = g_malloc0(sizeof(BlkMigDevState));
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
283 bmds->bs = bs;
284 bmds->bulk_completed = 0;
285 bmds->total_sectors = sectors;
286 bmds->completed_sectors = 0;
287 bmds->shared_base = block_mig_state.shared_base;
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
288 alloc_aio_bitmap(bmds);
f48905d block-migration: add reference to target DriveInfo
Marcelo Tosatti authored
289 drive_get_ref(drive_get_by_blockdev(bs));
8591675 block: enable in_use flag
Marcelo Tosatti authored
290 bdrv_set_in_use(bs, 1);
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
291
292 block_mig_state.total_sector_sum += sectors;
293
294 if (bmds->shared_base) {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
295 DPRINTF("Start migration for %s with shared base image\n",
296 bs->device_name);
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
297 } else {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
298 DPRINTF("Start full migration for %s\n", bs->device_name);
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
299 }
300
301 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
302 }
303 }
304
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
305 static void init_blk_migration(QEMUFile *f)
b66460e block: Do not export bdrv_first
Stefan Hajnoczi authored
306 {
69d63a9 @jan-kiszka block migration: Initialize remaining BlkMigState fields
jan-kiszka authored
307 block_mig_state.submitted = 0;
308 block_mig_state.read_done = 0;
309 block_mig_state.transferred = 0;
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
310 block_mig_state.total_sector_sum = 0;
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
311 block_mig_state.prev_progress = -1;
e970ec0 Remove unused code
Liran Schour authored
312 block_mig_state.bulk_completed = 0;
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
313 block_mig_state.total_time = 0;
314 block_mig_state.reads = 0;
69d63a9 @jan-kiszka block migration: Initialize remaining BlkMigState fields
jan-kiszka authored
315
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
316 bdrv_iterate(init_blk_migration_it, NULL);
c163b5c Block live migration
lirans@il.ibm.com authored
317 }
318
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
319 static int blk_mig_save_bulked_block(QEMUFile *f)
c163b5c Block live migration
lirans@il.ibm.com authored
320 {
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
321 int64_t completed_sector_sum = 0;
c163b5c Block live migration
lirans@il.ibm.com authored
322 BlkMigDevState *bmds;
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
323 int progress;
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
324 int ret = 0;
c163b5c Block live migration
lirans@il.ibm.com authored
325
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
326 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
327 if (bmds->bulk_completed == 0) {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
328 if (mig_save_device_bulk(f, bmds) == 1) {
57cce12 @jan-kiszka block migration: Consolidate mig_read_device_bulk into mig_save_device_b...
jan-kiszka authored
329 /* completed bulk section for this device */
330 bmds->bulk_completed = 1;
c163b5c Block live migration
lirans@il.ibm.com authored
331 }
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
332 completed_sector_sum += bmds->completed_sectors;
333 ret = 1;
334 break;
335 } else {
336 completed_sector_sum += bmds->completed_sectors;
c163b5c Block live migration
lirans@il.ibm.com authored
337 }
338 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
339
8b6b2af @priteau Avoid divide by zero when there is no block device to migrate
priteau authored
340 if (block_mig_state.total_sector_sum != 0) {
341 progress = completed_sector_sum * 100 /
342 block_mig_state.total_sector_sum;
343 } else {
344 progress = 100;
345 }
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
346 if (progress != block_mig_state.prev_progress) {
347 block_mig_state.prev_progress = progress;
348 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
349 | BLK_MIG_FLAG_PROGRESS);
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
350 DPRINTF("Completed %d %%\r", progress);
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
351 }
352
353 return ret;
c163b5c Block live migration
lirans@il.ibm.com authored
354 }
355
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
356 static void blk_mig_reset_dirty_cursor(void)
c163b5c Block live migration
lirans@il.ibm.com authored
357 {
358 BlkMigDevState *bmds;
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
359
360 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
361 bmds->cur_dirty = 0;
362 }
363 }
364
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
365 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
366 int is_async)
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
367 {
368 BlkMigBlock *blk;
369 int64_t total_sectors = bmds->total_sectors;
c163b5c Block live migration
lirans@il.ibm.com authored
370 int64_t sector;
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
371 int nr_sectors;
dcd1d22 @juanquintela migration: change has_error to contain errno values
juanquintela authored
372 int ret = -EIO;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
373
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
374 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
62155e2 block migration: do not submit multiple AIOs for same sector (v2)
Marcelo Tosatti authored
375 if (bmds_aio_inflight(bmds, sector)) {
922453b block: convert qemu_aio_flush() calls to bdrv_drain_all()
Stefan Hajnoczi authored
376 bdrv_drain_all();
62155e2 block migration: do not submit multiple AIOs for same sector (v2)
Marcelo Tosatti authored
377 }
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
378 if (bdrv_get_dirty(bmds->bs, sector)) {
575a58d @jan-kiszka block migration: Avoid large stack buffer
jan-kiszka authored
379
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
380 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
381 nr_sectors = total_sectors - sector;
382 } else {
383 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
384 }
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
385 blk = g_malloc(sizeof(BlkMigBlock));
386 blk->buf = g_malloc(BLOCK_SIZE);
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
387 blk->bmds = bmds;
388 blk->sector = sector;
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
389 blk->nr_sectors = nr_sectors;
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
390
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
391 if (is_async) {
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
392 blk->iov.iov_base = blk->buf;
393 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
394 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
395
ff5c52a @avishay-traeger Improve accuracy of block migration bandwidth calculation
avishay-traeger authored
396 if (block_mig_state.submitted == 0) {
397 block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
398 }
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
399
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
400 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
401 nr_sectors, blk_mig_read_cb, blk);
402 block_mig_state.submitted++;
33656af block migration: do not submit multiple AIOs for same sector
Marcelo Tosatti authored
403 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
404 } else {
dcd1d22 @juanquintela migration: change has_error to contain errno values
juanquintela authored
405 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
406 if (ret < 0) {
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
407 goto error;
c163b5c Block live migration
lirans@il.ibm.com authored
408 }
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
409 blk_send(f, blk);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
410
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
411 g_free(blk->buf);
412 g_free(blk);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
413 }
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
414
415 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
416 break;
c163b5c Block live migration
lirans@il.ibm.com authored
417 }
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
418 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
419 bmds->cur_dirty = sector;
c163b5c Block live migration
lirans@il.ibm.com authored
420 }
575a58d @jan-kiszka block migration: Avoid large stack buffer
jan-kiszka authored
421
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
422 return (bmds->cur_dirty >= bmds->total_sectors);
423
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
424 error:
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
425 DPRINTF("Error reading sector %" PRId64 "\n", sector);
dcd1d22 @juanquintela migration: change has_error to contain errno values
juanquintela authored
426 qemu_file_set_error(f, ret);
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
427 g_free(blk->buf);
428 g_free(blk);
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
429 return 0;
430 }
431
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
432 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
433 {
434 BlkMigDevState *bmds;
435 int ret = 0;
436
437 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
438 if (mig_save_device_dirty(f, bmds, is_async) == 0) {
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
439 ret = 1;
440 break;
441 }
442 }
443
444 return ret;
c163b5c Block live migration
lirans@il.ibm.com authored
445 }
446
447 static void flush_blks(QEMUFile* f)
448 {
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
449 BlkMigBlock *blk;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
450
d0f2c4c @moosotc Do not use dprintf
moosotc authored
451 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
452 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
453 block_mig_state.transferred);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
454
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
455 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
456 if (qemu_file_rate_limit(f)) {
457 break;
458 }
4b64036 @jan-kiszka block migration: Add error handling/propagation
jan-kiszka authored
459 if (blk->ret < 0) {
dcd1d22 @juanquintela migration: change has_error to contain errno values
juanquintela authored
460 qemu_file_set_error(f, blk->ret);
4b64036 @jan-kiszka block migration: Add error handling/propagation
jan-kiszka authored
461 break;
462 }
13f0b67 @jan-kiszka block migration: Consolidate block transmission
jan-kiszka authored
463 blk_send(f, blk);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
464
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
465 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
466 g_free(blk->buf);
467 g_free(blk);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
468
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
469 block_mig_state.read_done--;
470 block_mig_state.transferred++;
471 assert(block_mig_state.read_done >= 0);
c163b5c Block live migration
lirans@il.ibm.com authored
472 }
473
d0f2c4c @moosotc Do not use dprintf
moosotc authored
474 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
475 block_mig_state.submitted, block_mig_state.read_done,
476 block_mig_state.transferred);
c163b5c Block live migration
lirans@il.ibm.com authored
477 }
478
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
479 static int64_t get_remaining_dirty(void)
480 {
481 BlkMigDevState *bmds;
482 int64_t dirty = 0;
483
484 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
485 dirty += bdrv_get_dirty_count(bmds->bs);
486 }
487
488 return dirty * BLOCK_SIZE;
489 }
490
c163b5c Block live migration
lirans@il.ibm.com authored
491 static int is_stage2_completed(void)
492 {
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
493 int64_t remaining_dirty;
494 long double bwidth;
495
496 if (block_mig_state.bulk_completed == 1) {
497
498 remaining_dirty = get_remaining_dirty();
bd0858b block migration: replace tabs by spaces.
Yoshiaki Tamura authored
499 if (remaining_dirty == 0) {
500 return 1;
501 }
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
502
bd0858b block migration: replace tabs by spaces.
Yoshiaki Tamura authored
503 bwidth = compute_read_bwidth();
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
504
bd0858b block migration: replace tabs by spaces.
Yoshiaki Tamura authored
505 if ((remaining_dirty / bwidth) <=
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
506 migrate_max_downtime()) {
4238e26 @stweil Fix some spelling bugs in documentation and comments
stweil authored
507 /* finish stage2 because we think that we can finish remaining work
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
508 below max_downtime */
509
510 return 1;
511 }
512 }
513
514 return 0;
c163b5c Block live migration
lirans@il.ibm.com authored
515 }
516
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
517 static void blk_mig_cleanup(void)
4ec7fcc @jan-kiszka live migration: Allow cleanup after cancellation or error
jan-kiszka authored
518 {
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
519 BlkMigDevState *bmds;
520 BlkMigBlock *blk;
4ec7fcc @jan-kiszka live migration: Allow cleanup after cancellation or error
jan-kiszka authored
521
8f794c5 block-migration: actually disable dirty tracking on cleanup
Marcelo Tosatti authored
522 set_dirty_tracking(0);
523
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
524 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
525 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
8591675 block: enable in_use flag
Marcelo Tosatti authored
526 bdrv_set_in_use(bmds->bs, 0);
f48905d block-migration: add reference to target DriveInfo
Marcelo Tosatti authored
527 drive_put_ref(drive_get_by_blockdev(bmds->bs));
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
528 g_free(bmds->aio_bitmap);
529 g_free(bmds);
4ec7fcc @jan-kiszka live migration: Allow cleanup after cancellation or error
jan-kiszka authored
530 }
531
82801d8 @jan-kiszka block migration: Report overall migration progress
jan-kiszka authored
532 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
533 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
534 g_free(blk->buf);
535 g_free(blk);
4ec7fcc @jan-kiszka live migration: Allow cleanup after cancellation or error
jan-kiszka authored
536 }
537 }
538
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
539 static int block_save_live(QEMUFile *f, int stage, void *opaque)
c163b5c Block live migration
lirans@il.ibm.com authored
540 {
2975725 @juanquintela migration: make *save_live return errors
juanquintela authored
541 int ret;
542
d0f2c4c @moosotc Do not use dprintf
moosotc authored
543 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
544 stage, block_mig_state.submitted, block_mig_state.transferred);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
545
4ec7fcc @jan-kiszka live migration: Allow cleanup after cancellation or error
jan-kiszka authored
546 if (stage < 0) {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
547 blk_mig_cleanup();
4ec7fcc @jan-kiszka live migration: Allow cleanup after cancellation or error
jan-kiszka authored
548 return 0;
549 }
550
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
551 if (block_mig_state.blk_enable != 1) {
c163b5c Block live migration
lirans@il.ibm.com authored
552 /* no need to migrate storage */
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
553 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
c163b5c Block live migration
lirans@il.ibm.com authored
554 return 1;
555 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
556
557 if (stage == 1) {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
558 init_blk_migration(f);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
559
c163b5c Block live migration
lirans@il.ibm.com authored
560 /* start track dirty blocks */
561 set_dirty_tracking(1);
562 }
563
564 flush_blks(f);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
565
2975725 @juanquintela migration: make *save_live return errors
juanquintela authored
566 ret = qemu_file_get_error(f);
567 if (ret) {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
568 blk_mig_cleanup();
2975725 @juanquintela migration: make *save_live return errors
juanquintela authored
569 return ret;
4b64036 @jan-kiszka block migration: Add error handling/propagation
jan-kiszka authored
570 }
571
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
572 blk_mig_reset_dirty_cursor();
573
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
574 if (stage == 2) {
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
575 /* control the rate of transfer */
576 while ((block_mig_state.submitted +
577 block_mig_state.read_done) * BLOCK_SIZE <
578 qemu_file_get_rate_limit(f)) {
579 if (block_mig_state.bulk_completed == 0) {
580 /* first finish the bulk phase */
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
581 if (blk_mig_save_bulked_block(f) == 0) {
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
582 /* finished saving bulk on all devices */
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
583 block_mig_state.bulk_completed = 1;
584 }
585 } else {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
586 if (blk_mig_save_dirty_block(f, 1) == 0) {
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
587 /* no more dirty blocks */
588 break;
589 }
590 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
591 }
592
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
593 flush_blks(f);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
594
2975725 @juanquintela migration: make *save_live return errors
juanquintela authored
595 ret = qemu_file_get_error(f);
596 if (ret) {
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
597 blk_mig_cleanup();
2975725 @juanquintela migration: make *save_live return errors
juanquintela authored
598 return ret;
d76cac7 Tranfer dirty blocks during iterative phase
Liran Schour authored
599 }
4b64036 @jan-kiszka block migration: Add error handling/propagation
jan-kiszka authored
600 }
601
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
602 if (stage == 3) {
889ae39 Try not to exceed max downtime on stage3
Liran Schour authored
603 /* we know for sure that save bulk is completed and
604 all async read completed */
605 assert(block_mig_state.submitted == 0);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
606
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
607 while (blk_mig_save_dirty_block(f, 0) != 0);
608 blk_mig_cleanup();
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
609
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
610 /* report completion */
611 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
612
2975725 @juanquintela migration: make *save_live return errors
juanquintela authored
613 ret = qemu_file_get_error(f);
614 if (ret) {
615 return ret;
4b64036 @jan-kiszka block migration: Add error handling/propagation
jan-kiszka authored
616 }
617
539de12 Purge migration of (almost) everything to do with monitors
Luiz Capitulino authored
618 DPRINTF("Block migration completed\n");
c163b5c Block live migration
lirans@il.ibm.com authored
619 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
620
621 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
622
c163b5c Block live migration
lirans@il.ibm.com authored
623 return ((stage == 2) && is_stage2_completed());
624 }
625
626 static int block_load(QEMUFile *f, void *opaque, int version_id)
627 {
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
628 static int banner_printed;
c163b5c Block live migration
lirans@il.ibm.com authored
629 int len, flags;
630 char device_name[256];
631 int64_t addr;
77358b5 @priteau Fix block migration when the device size is not a multiple of 1 MB
priteau authored
632 BlockDriverState *bs, *bs_prev = NULL;
c163b5c Block live migration
lirans@il.ibm.com authored
633 uint8_t *buf;
77358b5 @priteau Fix block migration when the device size is not a multiple of 1 MB
priteau authored
634 int64_t total_sectors = 0;
635 int nr_sectors;
42802d4 @juanquintela migration: use qemu_file_get_error() return value when possible
juanquintela authored
636 int ret;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
637
c163b5c Block live migration
lirans@il.ibm.com authored
638 do {
639 addr = qemu_get_be64(f);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
640
6ea4430 @jan-kiszka block migration: Rework constants API
jan-kiszka authored
641 flags = addr & ~BDRV_SECTOR_MASK;
642 addr >>= BDRV_SECTOR_BITS;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
643
644 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5c Block live migration
lirans@il.ibm.com authored
645 /* get device name */
646 len = qemu_get_byte(f);
647 qemu_get_buffer(f, (uint8_t *)device_name, len);
648 device_name[len] = '\0';
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
649
c163b5c Block live migration
lirans@il.ibm.com authored
650 bs = bdrv_find(device_name);
4b64036 @jan-kiszka block migration: Add error handling/propagation
jan-kiszka authored
651 if (!bs) {
652 fprintf(stderr, "Error unknown block device %s\n",
653 device_name);
654 return -EINVAL;
655 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
656
77358b5 @priteau Fix block migration when the device size is not a multiple of 1 MB
priteau authored
657 if (bs != bs_prev) {
658 bs_prev = bs;
659 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
660 if (total_sectors <= 0) {
6daf194 Strip trailing '\n' from error_report()'s first argument
Markus Armbruster authored
661 error_report("Error getting length of block device %s",
77358b5 @priteau Fix block migration when the device size is not a multiple of 1 MB
priteau authored
662 device_name);
663 return -EINVAL;
664 }
665 }
666
667 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
668 nr_sectors = total_sectors - addr;
669 } else {
670 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
671 }
672
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
673 buf = g_malloc(BLOCK_SIZE);
575a58d @jan-kiszka block migration: Avoid large stack buffer
jan-kiszka authored
674
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
675 qemu_get_buffer(f, buf, BLOCK_SIZE);
77358b5 @priteau Fix block migration when the device size is not a multiple of 1 MB
priteau authored
676 ret = bdrv_write(bs, addr, buf, nr_sectors);
575a58d @jan-kiszka block migration: Avoid large stack buffer
jan-kiszka authored
677
7267c09 Use glib memory allocation and free functions
Anthony Liguori authored
678 g_free(buf);
b02bea3 block migration: propagate return value when bdrv_write() returns < 0
Yoshiaki Tamura authored
679 if (ret < 0) {
680 return ret;
681 }
01e61e2 @jan-kiszka block migration: Add support for restore progress reporting
jan-kiszka authored
682 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
683 if (!banner_printed) {
684 printf("Receiving block device images\n");
685 banner_printed = 1;
686 }
687 printf("Completed %d %%%c", (int)addr,
688 (addr == 100) ? '\n' : '\r');
689 fflush(stdout);
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
690 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
4b64036 @jan-kiszka block migration: Add error handling/propagation
jan-kiszka authored
691 fprintf(stderr, "Unknown flags\n");
692 return -EINVAL;
693 }
42802d4 @juanquintela migration: use qemu_file_get_error() return value when possible
juanquintela authored
694 ret = qemu_file_get_error(f);
695 if (ret != 0) {
696 return ret;
c163b5c Block live migration
lirans@il.ibm.com authored
697 }
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
698 } while (!(flags & BLK_MIG_FLAG_EOS));
699
c163b5c Block live migration
lirans@il.ibm.com authored
700 return 0;
701 }
702
703 static void block_set_params(int blk_enable, int shared_base, void *opaque)
704 {
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
705 block_mig_state.blk_enable = blk_enable;
706 block_mig_state.shared_base = shared_base;
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
707
c163b5c Block live migration
lirans@il.ibm.com authored
708 /* shared base means that blk_enable = 1 */
d11ecd3 @jan-kiszka block migration: Avoid indirection of block_mig_state
jan-kiszka authored
709 block_mig_state.blk_enable |= shared_base;
c163b5c Block live migration
lirans@il.ibm.com authored
710 }
711
712 void blk_mig_init(void)
a55eb92 @jan-kiszka block migration: Fix coding style and whitespaces
jan-kiszka authored
713 {
5e5328b @jan-kiszka block migration: Switch device and block lists to QSIMPLEQ
jan-kiszka authored
714 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
715 QSIMPLEQ_INIT(&block_mig_state.blk_list);
716
0be71e3 @awilliam savevm: Add DeviceState param
awilliam authored
717 register_savevm_live(NULL, "block", 0, 1, block_set_params,
718 block_save_live, NULL, block_load, &block_mig_state);
c163b5c Block live migration
lirans@il.ibm.com authored
719 }
Something went wrong with that request. Please try again.