]> git.proxmox.com Git - qemu.git/blame - block-migration.c
cleanup block driver option handling in vl.c
[qemu.git] / block-migration.c
CommitLineData
c163b5ca 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 *
12 */
13
14#include "qemu-common.h"
15#include "block_int.h"
16#include "hw/hw.h"
5e5328be 17#include "qemu-queue.h"
889ae39c 18#include "qemu-timer.h"
7184049e 19#include "monitor.h"
c163b5ca 20#include "block-migration.h"
889ae39c 21#include "migration.h"
c163b5ca 22#include <assert.h>
c163b5ca 23
6ea44308 24#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
c163b5ca 25
26#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
27#define BLK_MIG_FLAG_EOS 0x02
01e61e2d 28#define BLK_MIG_FLAG_PROGRESS 0x04
c163b5ca 29
30#define MAX_IS_ALLOCATED_SEARCH 65536
c163b5ca 31
32//#define DEBUG_BLK_MIGRATION
33
34#ifdef DEBUG_BLK_MIGRATION
d0f2c4c6 35#define DPRINTF(fmt, ...) \
c163b5ca 36 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
37#else
d0f2c4c6 38#define DPRINTF(fmt, ...) \
c163b5ca 39 do { } while (0)
40#endif
41
a55eb92c
JK
42typedef struct BlkMigDevState {
43 BlockDriverState *bs;
44 int bulk_completed;
45 int shared_base;
a55eb92c 46 int64_t cur_sector;
d76cac7d 47 int64_t cur_dirty;
82801d8f 48 int64_t completed_sectors;
a55eb92c
JK
49 int64_t total_sectors;
50 int64_t dirty;
5e5328be 51 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
a55eb92c
JK
52} BlkMigDevState;
53
c163b5ca 54typedef struct BlkMigBlock {
55 uint8_t *buf;
56 BlkMigDevState *bmds;
57 int64_t sector;
58 struct iovec iov;
59 QEMUIOVector qiov;
60 BlockDriverAIOCB *aiocb;
61 int ret;
889ae39c 62 int64_t time;
5e5328be 63 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
c163b5ca 64} BlkMigBlock;
65
66typedef struct BlkMigState {
c163b5ca 67 int blk_enable;
68 int shared_base;
5e5328be
JK
69 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
70 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
c163b5ca 71 int submitted;
72 int read_done;
73 int transferred;
82801d8f 74 int64_t total_sector_sum;
01e61e2d 75 int prev_progress;
e970ec0b 76 int bulk_completed;
889ae39c
LS
77 long double total_time;
78 int reads;
c163b5ca 79} BlkMigState;
80
d11ecd3d 81static BlkMigState block_mig_state;
c163b5ca 82
13f0b67f
JK
83static void blk_send(QEMUFile *f, BlkMigBlock * blk)
84{
85 int len;
86
87 /* sector number and flags */
88 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
89 | BLK_MIG_FLAG_DEVICE_BLOCK);
90
91 /* device name */
92 len = strlen(blk->bmds->bs->device_name);
93 qemu_put_byte(f, len);
94 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
95
96 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
97}
98
25f23643
JK
99int blk_mig_active(void)
100{
101 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
102}
103
104uint64_t blk_mig_bytes_transferred(void)
105{
106 BlkMigDevState *bmds;
107 uint64_t sum = 0;
108
109 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
110 sum += bmds->completed_sectors;
111 }
112 return sum << BDRV_SECTOR_BITS;
113}
114
115uint64_t blk_mig_bytes_remaining(void)
116{
117 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
118}
119
120uint64_t blk_mig_bytes_total(void)
121{
122 BlkMigDevState *bmds;
123 uint64_t sum = 0;
124
125 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
126 sum += bmds->total_sectors;
127 }
128 return sum << BDRV_SECTOR_BITS;
129}
130
889ae39c
LS
131static inline void add_avg_read_time(int64_t time)
132{
133 block_mig_state.reads++;
134 block_mig_state.total_time += time;
135}
136
137static inline long double compute_read_bwidth(void)
138{
139 assert(block_mig_state.total_time != 0);
140 return (block_mig_state.reads * BLOCK_SIZE)/ block_mig_state.total_time;
141}
142
c163b5ca 143static void blk_mig_read_cb(void *opaque, int ret)
144{
145 BlkMigBlock *blk = opaque;
a55eb92c 146
c163b5ca 147 blk->ret = ret;
a55eb92c 148
889ae39c
LS
149 blk->time = qemu_get_clock_ns(rt_clock) - blk->time;
150
151 add_avg_read_time(blk->time);
152
5e5328be 153 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
a55eb92c 154
d11ecd3d
JK
155 block_mig_state.submitted--;
156 block_mig_state.read_done++;
157 assert(block_mig_state.submitted >= 0);
c163b5ca 158}
159
7184049e 160static int mig_save_device_bulk(Monitor *mon, QEMUFile *f,
e970ec0b 161 BlkMigDevState *bmds)
a55eb92c 162{
57cce12d
JK
163 int64_t total_sectors = bmds->total_sectors;
164 int64_t cur_sector = bmds->cur_sector;
165 BlockDriverState *bs = bmds->bs;
c163b5ca 166 BlkMigBlock *blk;
13f0b67f 167 int nr_sectors;
a55eb92c 168
57cce12d 169 if (bmds->shared_base) {
b1d10856 170 while (cur_sector < total_sectors &&
57cce12d
JK
171 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
172 &nr_sectors)) {
c163b5ca 173 cur_sector += nr_sectors;
174 }
175 }
a55eb92c
JK
176
177 if (cur_sector >= total_sectors) {
82801d8f 178 bmds->cur_sector = bmds->completed_sectors = total_sectors;
c163b5ca 179 return 1;
180 }
a55eb92c 181
82801d8f 182 bmds->completed_sectors = cur_sector;
a55eb92c 183
57cce12d
JK
184 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
185
6ea44308
JK
186 /* we are going to transfer a full block even if it is not allocated */
187 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
c163b5ca 188
6ea44308 189 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
57cce12d 190 nr_sectors = total_sectors - cur_sector;
c163b5ca 191 }
a55eb92c 192
13f0b67f
JK
193 blk = qemu_malloc(sizeof(BlkMigBlock));
194 blk->buf = qemu_malloc(BLOCK_SIZE);
195 blk->bmds = bmds;
196 blk->sector = cur_sector;
a55eb92c 197
e970ec0b
LS
198 blk->iov.iov_base = blk->buf;
199 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
200 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
a55eb92c 201
889ae39c
LS
202 blk->time = qemu_get_clock_ns(rt_clock);
203
e970ec0b
LS
204 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
205 nr_sectors, blk_mig_read_cb, blk);
206 if (!blk->aiocb) {
207 goto error;
c163b5ca 208 }
e970ec0b 209 block_mig_state.submitted++;
d76cac7d 210
13f0b67f
JK
211 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
212 bmds->cur_sector = cur_sector + nr_sectors;
a55eb92c 213
13f0b67f 214 return (bmds->cur_sector >= total_sectors);
4b640365
JK
215
216error:
7184049e 217 monitor_printf(mon, "Error reading sector %" PRId64 "\n", cur_sector);
4b640365
JK
218 qemu_file_set_error(f);
219 qemu_free(blk->buf);
220 qemu_free(blk);
221 return 0;
c163b5ca 222}
223
c163b5ca 224static void set_dirty_tracking(int enable)
225{
226 BlkMigDevState *bmds;
5e5328be
JK
227
228 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 229 bdrv_set_dirty_tracking(bmds->bs, enable);
c163b5ca 230 }
c163b5ca 231}
232
7184049e 233static void init_blk_migration(Monitor *mon, QEMUFile *f)
c163b5ca 234{
5e5328be 235 BlkMigDevState *bmds;
c163b5ca 236 BlockDriverState *bs;
792773b2 237 int64_t sectors;
a55eb92c 238
69d63a97
JK
239 block_mig_state.submitted = 0;
240 block_mig_state.read_done = 0;
241 block_mig_state.transferred = 0;
82801d8f 242 block_mig_state.total_sector_sum = 0;
01e61e2d 243 block_mig_state.prev_progress = -1;
e970ec0b 244 block_mig_state.bulk_completed = 0;
889ae39c
LS
245 block_mig_state.total_time = 0;
246 block_mig_state.reads = 0;
69d63a97 247
c163b5ca 248 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
a55eb92c 249 if (bs->type == BDRV_TYPE_HD) {
792773b2
JK
250 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
251 if (sectors == 0) {
252 continue;
253 }
254
c163b5ca 255 bmds = qemu_mallocz(sizeof(BlkMigDevState));
256 bmds->bs = bs;
257 bmds->bulk_completed = 0;
792773b2 258 bmds->total_sectors = sectors;
82801d8f 259 bmds->completed_sectors = 0;
d11ecd3d 260 bmds->shared_base = block_mig_state.shared_base;
a55eb92c 261
792773b2 262 block_mig_state.total_sector_sum += sectors;
82801d8f 263
a55eb92c 264 if (bmds->shared_base) {
7184049e
JK
265 monitor_printf(mon, "Start migration for %s with shared base "
266 "image\n",
267 bs->device_name);
c163b5ca 268 } else {
7184049e
JK
269 monitor_printf(mon, "Start full migration for %s\n",
270 bs->device_name);
c163b5ca 271 }
a55eb92c 272
5e5328be 273 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
c163b5ca 274 }
a55eb92c 275 }
c163b5ca 276}
277
e970ec0b 278static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f)
c163b5ca 279{
82801d8f 280 int64_t completed_sector_sum = 0;
c163b5ca 281 BlkMigDevState *bmds;
01e61e2d 282 int progress;
82801d8f 283 int ret = 0;
c163b5ca 284
5e5328be 285 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
a55eb92c 286 if (bmds->bulk_completed == 0) {
e970ec0b 287 if (mig_save_device_bulk(mon, f, bmds) == 1) {
57cce12d
JK
288 /* completed bulk section for this device */
289 bmds->bulk_completed = 1;
c163b5ca 290 }
82801d8f
JK
291 completed_sector_sum += bmds->completed_sectors;
292 ret = 1;
293 break;
294 } else {
295 completed_sector_sum += bmds->completed_sectors;
c163b5ca 296 }
297 }
a55eb92c 298
01e61e2d
JK
299 progress = completed_sector_sum * 100 / block_mig_state.total_sector_sum;
300 if (progress != block_mig_state.prev_progress) {
301 block_mig_state.prev_progress = progress;
302 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
303 | BLK_MIG_FLAG_PROGRESS);
304 monitor_printf(mon, "Completed %d %%\r", progress);
7184049e 305 monitor_flush(mon);
82801d8f
JK
306 }
307
308 return ret;
c163b5ca 309}
310
d76cac7d 311static void blk_mig_reset_dirty_cursor(void)
c163b5ca 312{
313 BlkMigDevState *bmds;
d76cac7d
LS
314
315 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
316 bmds->cur_dirty = 0;
317 }
318}
319
320static int mig_save_device_dirty(Monitor *mon, QEMUFile *f,
321 BlkMigDevState *bmds, int is_async)
322{
323 BlkMigBlock *blk;
324 int64_t total_sectors = bmds->total_sectors;
c163b5ca 325 int64_t sector;
d76cac7d 326 int nr_sectors;
a55eb92c 327
d76cac7d
LS
328 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
329 if (bdrv_get_dirty(bmds->bs, sector)) {
575a58d7 330
d76cac7d
LS
331 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
332 nr_sectors = total_sectors - sector;
333 } else {
334 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
335 }
336 blk = qemu_malloc(sizeof(BlkMigBlock));
337 blk->buf = qemu_malloc(BLOCK_SIZE);
338 blk->bmds = bmds;
339 blk->sector = sector;
340
889ae39c 341 if (is_async) {
d76cac7d
LS
342 blk->iov.iov_base = blk->buf;
343 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
344 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
345
889ae39c
LS
346 blk->time = qemu_get_clock_ns(rt_clock);
347
d76cac7d
LS
348 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
349 nr_sectors, blk_mig_read_cb, blk);
350 if (!blk->aiocb) {
351 goto error;
352 }
353 block_mig_state.submitted++;
354 } else {
355 if (bdrv_read(bmds->bs, sector, blk->buf,
356 nr_sectors) < 0) {
357 goto error;
c163b5ca 358 }
d76cac7d 359 blk_send(f, blk);
a55eb92c 360
d76cac7d
LS
361 qemu_free(blk->buf);
362 qemu_free(blk);
a55eb92c 363 }
d76cac7d
LS
364
365 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
366 break;
c163b5ca 367 }
d76cac7d
LS
368 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
369 bmds->cur_dirty = sector;
c163b5ca 370 }
575a58d7 371
d76cac7d
LS
372 return (bmds->cur_dirty >= bmds->total_sectors);
373
889ae39c 374error:
d76cac7d
LS
375 monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector);
376 qemu_file_set_error(f);
377 qemu_free(blk->buf);
378 qemu_free(blk);
379 return 0;
380}
381
382static int blk_mig_save_dirty_block(Monitor *mon, QEMUFile *f, int is_async)
383{
384 BlkMigDevState *bmds;
385 int ret = 0;
386
387 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
889ae39c 388 if (mig_save_device_dirty(mon, f, bmds, is_async) == 0) {
d76cac7d
LS
389 ret = 1;
390 break;
391 }
392 }
393
394 return ret;
c163b5ca 395}
396
397static void flush_blks(QEMUFile* f)
398{
5e5328be 399 BlkMigBlock *blk;
a55eb92c 400
d0f2c4c6 401 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
d11ecd3d
JK
402 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
403 block_mig_state.transferred);
a55eb92c 404
5e5328be
JK
405 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
406 if (qemu_file_rate_limit(f)) {
407 break;
408 }
4b640365
JK
409 if (blk->ret < 0) {
410 qemu_file_set_error(f);
411 break;
412 }
13f0b67f 413 blk_send(f, blk);
a55eb92c 414
5e5328be 415 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
c163b5ca 416 qemu_free(blk->buf);
417 qemu_free(blk);
a55eb92c 418
d11ecd3d
JK
419 block_mig_state.read_done--;
420 block_mig_state.transferred++;
421 assert(block_mig_state.read_done >= 0);
c163b5ca 422 }
c163b5ca 423
d0f2c4c6 424 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
d11ecd3d
JK
425 block_mig_state.submitted, block_mig_state.read_done,
426 block_mig_state.transferred);
c163b5ca 427}
428
889ae39c
LS
429static int64_t get_remaining_dirty(void)
430{
431 BlkMigDevState *bmds;
432 int64_t dirty = 0;
433
434 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
435 dirty += bdrv_get_dirty_count(bmds->bs);
436 }
437
438 return dirty * BLOCK_SIZE;
439}
440
c163b5ca 441static int is_stage2_completed(void)
442{
889ae39c
LS
443 int64_t remaining_dirty;
444 long double bwidth;
445
446 if (block_mig_state.bulk_completed == 1) {
447
448 remaining_dirty = get_remaining_dirty();
449 if (remaining_dirty == 0) {
450 return 1;
451 }
452
453 bwidth = compute_read_bwidth();
454
455 if ((remaining_dirty / bwidth) <=
456 migrate_max_downtime()) {
457 /* finish stage2 because we think that we can finish remaing work
458 below max_downtime */
459
460 return 1;
461 }
462 }
463
464 return 0;
c163b5ca 465}
466
7184049e 467static void blk_mig_cleanup(Monitor *mon)
4ec7fcc7 468{
82801d8f
JK
469 BlkMigDevState *bmds;
470 BlkMigBlock *blk;
4ec7fcc7 471
82801d8f
JK
472 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
473 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
4ec7fcc7
JK
474 qemu_free(bmds);
475 }
476
82801d8f
JK
477 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
478 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
4ec7fcc7
JK
479 qemu_free(blk->buf);
480 qemu_free(blk);
481 }
482
483 set_dirty_tracking(0);
484
7184049e 485 monitor_printf(mon, "\n");
4ec7fcc7
JK
486}
487
f327aa0c 488static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
c163b5ca 489{
d0f2c4c6 490 DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
d11ecd3d 491 stage, block_mig_state.submitted, block_mig_state.transferred);
a55eb92c 492
4ec7fcc7 493 if (stage < 0) {
7184049e 494 blk_mig_cleanup(mon);
4ec7fcc7
JK
495 return 0;
496 }
497
d11ecd3d 498 if (block_mig_state.blk_enable != 1) {
c163b5ca 499 /* no need to migrate storage */
a55eb92c 500 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
c163b5ca 501 return 1;
502 }
a55eb92c
JK
503
504 if (stage == 1) {
7184049e 505 init_blk_migration(mon, f);
a55eb92c 506
c163b5ca 507 /* start track dirty blocks */
508 set_dirty_tracking(1);
c163b5ca 509 }
510
511 flush_blks(f);
a55eb92c 512
4b640365 513 if (qemu_file_has_error(f)) {
7184049e 514 blk_mig_cleanup(mon);
4b640365
JK
515 return 0;
516 }
517
d76cac7d
LS
518 blk_mig_reset_dirty_cursor();
519
889ae39c 520 if (stage == 2) {
d76cac7d
LS
521 /* control the rate of transfer */
522 while ((block_mig_state.submitted +
523 block_mig_state.read_done) * BLOCK_SIZE <
524 qemu_file_get_rate_limit(f)) {
525 if (block_mig_state.bulk_completed == 0) {
526 /* first finish the bulk phase */
527 if (blk_mig_save_bulked_block(mon, f) == 0) {
889ae39c 528 /* finished saving bulk on all devices */
d76cac7d
LS
529 block_mig_state.bulk_completed = 1;
530 }
531 } else {
532 if (blk_mig_save_dirty_block(mon, f, 1) == 0) {
533 /* no more dirty blocks */
534 break;
535 }
536 }
a55eb92c 537 }
a55eb92c 538
d76cac7d 539 flush_blks(f);
a55eb92c 540
d76cac7d
LS
541 if (qemu_file_has_error(f)) {
542 blk_mig_cleanup(mon);
543 return 0;
544 }
4b640365
JK
545 }
546
a55eb92c 547 if (stage == 3) {
889ae39c
LS
548 /* we know for sure that save bulk is completed and
549 all async read completed */
550 assert(block_mig_state.submitted == 0);
a55eb92c 551
889ae39c 552 while (blk_mig_save_dirty_block(mon, f, 0) != 0);
7184049e 553 blk_mig_cleanup(mon);
a55eb92c 554
01e61e2d
JK
555 /* report completion */
556 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
557
4b640365
JK
558 if (qemu_file_has_error(f)) {
559 return 0;
560 }
561
7184049e 562 monitor_printf(mon, "Block migration completed\n");
c163b5ca 563 }
a55eb92c
JK
564
565 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
566
c163b5ca 567 return ((stage == 2) && is_stage2_completed());
568}
569
570static int block_load(QEMUFile *f, void *opaque, int version_id)
571{
01e61e2d 572 static int banner_printed;
c163b5ca 573 int len, flags;
574 char device_name[256];
575 int64_t addr;
576 BlockDriverState *bs;
577 uint8_t *buf;
a55eb92c 578
c163b5ca 579 do {
c163b5ca 580 addr = qemu_get_be64(f);
a55eb92c 581
6ea44308
JK
582 flags = addr & ~BDRV_SECTOR_MASK;
583 addr >>= BDRV_SECTOR_BITS;
a55eb92c
JK
584
585 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
c163b5ca 586 /* get device name */
587 len = qemu_get_byte(f);
c163b5ca 588 qemu_get_buffer(f, (uint8_t *)device_name, len);
589 device_name[len] = '\0';
a55eb92c 590
c163b5ca 591 bs = bdrv_find(device_name);
4b640365
JK
592 if (!bs) {
593 fprintf(stderr, "Error unknown block device %s\n",
594 device_name);
595 return -EINVAL;
596 }
a55eb92c 597
575a58d7
JK
598 buf = qemu_malloc(BLOCK_SIZE);
599
a55eb92c 600 qemu_get_buffer(f, buf, BLOCK_SIZE);
4b640365 601 bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
575a58d7
JK
602
603 qemu_free(buf);
01e61e2d
JK
604 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
605 if (!banner_printed) {
606 printf("Receiving block device images\n");
607 banner_printed = 1;
608 }
609 printf("Completed %d %%%c", (int)addr,
610 (addr == 100) ? '\n' : '\r');
611 fflush(stdout);
a55eb92c 612 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
4b640365
JK
613 fprintf(stderr, "Unknown flags\n");
614 return -EINVAL;
615 }
616 if (qemu_file_has_error(f)) {
617 return -EIO;
c163b5ca 618 }
a55eb92c
JK
619 } while (!(flags & BLK_MIG_FLAG_EOS));
620
c163b5ca 621 return 0;
622}
623
624static void block_set_params(int blk_enable, int shared_base, void *opaque)
625{
d11ecd3d
JK
626 block_mig_state.blk_enable = blk_enable;
627 block_mig_state.shared_base = shared_base;
a55eb92c 628
c163b5ca 629 /* shared base means that blk_enable = 1 */
d11ecd3d 630 block_mig_state.blk_enable |= shared_base;
c163b5ca 631}
632
c163b5ca 633void blk_mig_init(void)
a55eb92c 634{
5e5328be
JK
635 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
636 QSIMPLEQ_INIT(&block_mig_state.blk_list);
637
a55eb92c 638 register_savevm_live("block", 0, 1, block_set_params, block_save_live,
d11ecd3d 639 NULL, block_load, &block_mig_state);
c163b5ca 640}