]> git.proxmox.com Git - qemu.git/blob - block-migration.c
b726c6c0025cbaa82e8ca676a5b2955d68d544d2
[qemu.git] / block-migration.c
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 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
14 */
15
16 #include "qemu-common.h"
17 #include "block/block_int.h"
18 #include "hw/hw.h"
19 #include "qemu/queue.h"
20 #include "qemu/timer.h"
21 #include "migration/block.h"
22 #include "migration/migration.h"
23 #include "sysemu/blockdev.h"
24 #include <assert.h>
25
26 #define BLOCK_SIZE (1 << 20)
27 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
28
29 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
30 #define BLK_MIG_FLAG_EOS 0x02
31 #define BLK_MIG_FLAG_PROGRESS 0x04
32
33 #define MAX_IS_ALLOCATED_SEARCH 65536
34
35 //#define DEBUG_BLK_MIGRATION
36
37 #ifdef DEBUG_BLK_MIGRATION
38 #define DPRINTF(fmt, ...) \
39 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
40 #else
41 #define DPRINTF(fmt, ...) \
42 do { } while (0)
43 #endif
44
45 typedef struct BlkMigDevState {
46 /* Written during setup phase. Can be read without a lock. */
47 BlockDriverState *bs;
48 int shared_base;
49 int64_t total_sectors;
50 QSIMPLEQ_ENTRY(BlkMigDevState) entry;
51
52 /* Only used by migration thread. Does not need a lock. */
53 int bulk_completed;
54 int64_t cur_sector;
55 int64_t cur_dirty;
56
57 /* Protected by block migration lock. */
58 unsigned long *aio_bitmap;
59 int64_t completed_sectors;
60 } BlkMigDevState;
61
62 typedef struct BlkMigBlock {
63 /* Only used by migration thread. */
64 uint8_t *buf;
65 BlkMigDevState *bmds;
66 int64_t sector;
67 int nr_sectors;
68 struct iovec iov;
69 QEMUIOVector qiov;
70 BlockDriverAIOCB *aiocb;
71
72 /* Protected by block migration lock. */
73 int ret;
74 QSIMPLEQ_ENTRY(BlkMigBlock) entry;
75 } BlkMigBlock;
76
77 typedef struct BlkMigState {
78 /* Written during setup phase. Can be read without a lock. */
79 int blk_enable;
80 int shared_base;
81 QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
82 int64_t total_sector_sum;
83
84 /* Protected by lock. */
85 QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
86 int submitted;
87 int read_done;
88
89 /* Only used by migration thread. Does not need a lock. */
90 int transferred;
91 int prev_progress;
92 int bulk_completed;
93
94 /* Lock must be taken _inside_ the iothread lock. */
95 QemuMutex lock;
96 } BlkMigState;
97
98 static BlkMigState block_mig_state;
99
100 static void blk_mig_lock(void)
101 {
102 qemu_mutex_lock(&block_mig_state.lock);
103 }
104
105 static void blk_mig_unlock(void)
106 {
107 qemu_mutex_unlock(&block_mig_state.lock);
108 }
109
110 static void blk_send(QEMUFile *f, BlkMigBlock * blk)
111 {
112 int len;
113
114 /* sector number and flags */
115 qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
116 | BLK_MIG_FLAG_DEVICE_BLOCK);
117
118 /* device name */
119 len = strlen(blk->bmds->bs->device_name);
120 qemu_put_byte(f, len);
121 qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
122
123 qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
124 }
125
126 int blk_mig_active(void)
127 {
128 return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
129 }
130
131 uint64_t blk_mig_bytes_transferred(void)
132 {
133 BlkMigDevState *bmds;
134 uint64_t sum = 0;
135
136 blk_mig_lock();
137 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
138 sum += bmds->completed_sectors;
139 }
140 blk_mig_unlock();
141 return sum << BDRV_SECTOR_BITS;
142 }
143
144 uint64_t blk_mig_bytes_remaining(void)
145 {
146 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
147 }
148
149 uint64_t blk_mig_bytes_total(void)
150 {
151 BlkMigDevState *bmds;
152 uint64_t sum = 0;
153
154 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
155 sum += bmds->total_sectors;
156 }
157 return sum << BDRV_SECTOR_BITS;
158 }
159
160
161 /* Called with migration lock held. */
162
163 static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
164 {
165 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
166
167 if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
168 return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
169 (1UL << (chunk % (sizeof(unsigned long) * 8))));
170 } else {
171 return 0;
172 }
173 }
174
175 /* Called with migration lock held. */
176
177 static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
178 int nb_sectors, int set)
179 {
180 int64_t start, end;
181 unsigned long val, idx, bit;
182
183 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
184 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
185
186 for (; start <= end; start++) {
187 idx = start / (sizeof(unsigned long) * 8);
188 bit = start % (sizeof(unsigned long) * 8);
189 val = bmds->aio_bitmap[idx];
190 if (set) {
191 val |= 1UL << bit;
192 } else {
193 val &= ~(1UL << bit);
194 }
195 bmds->aio_bitmap[idx] = val;
196 }
197 }
198
199 static void alloc_aio_bitmap(BlkMigDevState *bmds)
200 {
201 BlockDriverState *bs = bmds->bs;
202 int64_t bitmap_size;
203
204 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
205 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
206 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
207
208 bmds->aio_bitmap = g_malloc0(bitmap_size);
209 }
210
211 /* Never hold migration lock when yielding to the main loop! */
212
213 static void blk_mig_read_cb(void *opaque, int ret)
214 {
215 BlkMigBlock *blk = opaque;
216
217 blk_mig_lock();
218 blk->ret = ret;
219
220 QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
221 bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
222
223 block_mig_state.submitted--;
224 block_mig_state.read_done++;
225 assert(block_mig_state.submitted >= 0);
226 blk_mig_unlock();
227 }
228
229 static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
230 {
231 int64_t total_sectors = bmds->total_sectors;
232 int64_t cur_sector = bmds->cur_sector;
233 BlockDriverState *bs = bmds->bs;
234 BlkMigBlock *blk;
235 int nr_sectors;
236
237 if (bmds->shared_base) {
238 while (cur_sector < total_sectors &&
239 !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
240 &nr_sectors)) {
241 cur_sector += nr_sectors;
242 }
243 }
244
245 if (cur_sector >= total_sectors) {
246 bmds->cur_sector = bmds->completed_sectors = total_sectors;
247 return 1;
248 }
249
250 bmds->completed_sectors = cur_sector;
251
252 cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
253
254 /* we are going to transfer a full block even if it is not allocated */
255 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
256
257 if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
258 nr_sectors = total_sectors - cur_sector;
259 }
260
261 blk = g_malloc(sizeof(BlkMigBlock));
262 blk->buf = g_malloc(BLOCK_SIZE);
263 blk->bmds = bmds;
264 blk->sector = cur_sector;
265 blk->nr_sectors = nr_sectors;
266
267 blk->iov.iov_base = blk->buf;
268 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
269 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
270
271 blk_mig_lock();
272 block_mig_state.submitted++;
273 blk_mig_unlock();
274
275 blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
276 nr_sectors, blk_mig_read_cb, blk);
277
278 bdrv_reset_dirty(bs, cur_sector, nr_sectors);
279 bmds->cur_sector = cur_sector + nr_sectors;
280
281 return (bmds->cur_sector >= total_sectors);
282 }
283
284 static void set_dirty_tracking(int enable)
285 {
286 BlkMigDevState *bmds;
287
288 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
289 bdrv_set_dirty_tracking(bmds->bs, enable ? BLOCK_SIZE : 0);
290 }
291 }
292
293 static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
294 {
295 BlkMigDevState *bmds;
296 int64_t sectors;
297
298 if (!bdrv_is_read_only(bs)) {
299 sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
300 if (sectors <= 0) {
301 return;
302 }
303
304 bmds = g_malloc0(sizeof(BlkMigDevState));
305 bmds->bs = bs;
306 bmds->bulk_completed = 0;
307 bmds->total_sectors = sectors;
308 bmds->completed_sectors = 0;
309 bmds->shared_base = block_mig_state.shared_base;
310 alloc_aio_bitmap(bmds);
311 drive_get_ref(drive_get_by_blockdev(bs));
312 bdrv_set_in_use(bs, 1);
313
314 block_mig_state.total_sector_sum += sectors;
315
316 if (bmds->shared_base) {
317 DPRINTF("Start migration for %s with shared base image\n",
318 bs->device_name);
319 } else {
320 DPRINTF("Start full migration for %s\n", bs->device_name);
321 }
322
323 QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
324 }
325 }
326
327 static void init_blk_migration(QEMUFile *f)
328 {
329 block_mig_state.submitted = 0;
330 block_mig_state.read_done = 0;
331 block_mig_state.transferred = 0;
332 block_mig_state.total_sector_sum = 0;
333 block_mig_state.prev_progress = -1;
334 block_mig_state.bulk_completed = 0;
335
336 bdrv_iterate(init_blk_migration_it, NULL);
337 }
338
339 static int blk_mig_save_bulked_block(QEMUFile *f)
340 {
341 int64_t completed_sector_sum = 0;
342 BlkMigDevState *bmds;
343 int progress;
344 int ret = 0;
345
346 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
347 if (bmds->bulk_completed == 0) {
348 if (mig_save_device_bulk(f, bmds) == 1) {
349 /* completed bulk section for this device */
350 bmds->bulk_completed = 1;
351 }
352 completed_sector_sum += bmds->completed_sectors;
353 ret = 1;
354 break;
355 } else {
356 completed_sector_sum += bmds->completed_sectors;
357 }
358 }
359
360 if (block_mig_state.total_sector_sum != 0) {
361 progress = completed_sector_sum * 100 /
362 block_mig_state.total_sector_sum;
363 } else {
364 progress = 100;
365 }
366 if (progress != block_mig_state.prev_progress) {
367 block_mig_state.prev_progress = progress;
368 qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
369 | BLK_MIG_FLAG_PROGRESS);
370 DPRINTF("Completed %d %%\r", progress);
371 }
372
373 return ret;
374 }
375
376 static void blk_mig_reset_dirty_cursor(void)
377 {
378 BlkMigDevState *bmds;
379
380 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
381 bmds->cur_dirty = 0;
382 }
383 }
384
385 static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
386 int is_async)
387 {
388 BlkMigBlock *blk;
389 int64_t total_sectors = bmds->total_sectors;
390 int64_t sector;
391 int nr_sectors;
392 int ret = -EIO;
393
394 for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
395 blk_mig_lock();
396 if (bmds_aio_inflight(bmds, sector)) {
397 blk_mig_unlock();
398 bdrv_drain_all();
399 } else {
400 blk_mig_unlock();
401 }
402 if (bdrv_get_dirty(bmds->bs, sector)) {
403
404 if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
405 nr_sectors = total_sectors - sector;
406 } else {
407 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
408 }
409 blk = g_malloc(sizeof(BlkMigBlock));
410 blk->buf = g_malloc(BLOCK_SIZE);
411 blk->bmds = bmds;
412 blk->sector = sector;
413 blk->nr_sectors = nr_sectors;
414
415 if (is_async) {
416 blk->iov.iov_base = blk->buf;
417 blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
418 qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
419
420 blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
421 nr_sectors, blk_mig_read_cb, blk);
422
423 blk_mig_lock();
424 block_mig_state.submitted++;
425 bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
426 blk_mig_unlock();
427 } else {
428 ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
429 if (ret < 0) {
430 goto error;
431 }
432 blk_send(f, blk);
433
434 g_free(blk->buf);
435 g_free(blk);
436 }
437
438 bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
439 break;
440 }
441 sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
442 bmds->cur_dirty = sector;
443 }
444
445 return (bmds->cur_dirty >= bmds->total_sectors);
446
447 error:
448 DPRINTF("Error reading sector %" PRId64 "\n", sector);
449 g_free(blk->buf);
450 g_free(blk);
451 return ret;
452 }
453
454 /* return value:
455 * 0: too much data for max_downtime
456 * 1: few enough data for max_downtime
457 */
458 static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
459 {
460 BlkMigDevState *bmds;
461 int ret = 1;
462
463 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
464 ret = mig_save_device_dirty(f, bmds, is_async);
465 if (ret <= 0) {
466 break;
467 }
468 }
469
470 return ret;
471 }
472
473 static int flush_blks(QEMUFile *f)
474 {
475 BlkMigBlock *blk;
476 int ret = 0;
477
478 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
479 __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
480 block_mig_state.transferred);
481
482 blk_mig_lock();
483 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
484 if (qemu_file_rate_limit(f)) {
485 break;
486 }
487 if (blk->ret < 0) {
488 ret = blk->ret;
489 break;
490 }
491
492 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
493 blk_mig_unlock();
494 blk_send(f, blk);
495 blk_mig_lock();
496
497 g_free(blk->buf);
498 g_free(blk);
499
500 block_mig_state.read_done--;
501 block_mig_state.transferred++;
502 assert(block_mig_state.read_done >= 0);
503 }
504 blk_mig_unlock();
505
506 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
507 block_mig_state.submitted, block_mig_state.read_done,
508 block_mig_state.transferred);
509 return ret;
510 }
511
512 static int64_t get_remaining_dirty(void)
513 {
514 BlkMigDevState *bmds;
515 int64_t dirty = 0;
516
517 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
518 dirty += bdrv_get_dirty_count(bmds->bs);
519 }
520
521 return dirty << BDRV_SECTOR_BITS;
522 }
523
524 static void blk_mig_cleanup(void)
525 {
526 BlkMigDevState *bmds;
527 BlkMigBlock *blk;
528
529 bdrv_drain_all();
530
531 set_dirty_tracking(0);
532
533 blk_mig_lock();
534 while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
535 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
536 bdrv_set_in_use(bmds->bs, 0);
537 drive_put_ref(drive_get_by_blockdev(bmds->bs));
538 g_free(bmds->aio_bitmap);
539 g_free(bmds);
540 }
541
542 while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
543 QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
544 g_free(blk->buf);
545 g_free(blk);
546 }
547 blk_mig_unlock();
548 }
549
550 static void block_migration_cancel(void *opaque)
551 {
552 blk_mig_cleanup();
553 }
554
555 static int block_save_setup(QEMUFile *f, void *opaque)
556 {
557 int ret;
558
559 DPRINTF("Enter save live setup submitted %d transferred %d\n",
560 block_mig_state.submitted, block_mig_state.transferred);
561
562 init_blk_migration(f);
563
564 /* start track dirty blocks */
565 set_dirty_tracking(1);
566
567 ret = flush_blks(f);
568 blk_mig_reset_dirty_cursor();
569 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
570
571 return ret;
572 }
573
574 static int block_save_iterate(QEMUFile *f, void *opaque)
575 {
576 int ret;
577 int64_t last_ftell = qemu_ftell(f);
578
579 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
580 block_mig_state.submitted, block_mig_state.transferred);
581
582 ret = flush_blks(f);
583 if (ret) {
584 return ret;
585 }
586
587 blk_mig_reset_dirty_cursor();
588
589 /* control the rate of transfer */
590 blk_mig_lock();
591 while ((block_mig_state.submitted +
592 block_mig_state.read_done) * BLOCK_SIZE <
593 qemu_file_get_rate_limit(f)) {
594 blk_mig_unlock();
595 if (block_mig_state.bulk_completed == 0) {
596 /* first finish the bulk phase */
597 if (blk_mig_save_bulked_block(f) == 0) {
598 /* finished saving bulk on all devices */
599 block_mig_state.bulk_completed = 1;
600 }
601 ret = 0;
602 } else {
603 ret = blk_mig_save_dirty_block(f, 1);
604 }
605 if (ret < 0) {
606 return ret;
607 }
608 blk_mig_lock();
609 if (ret != 0) {
610 /* no more dirty blocks */
611 break;
612 }
613 }
614 blk_mig_unlock();
615
616 ret = flush_blks(f);
617 if (ret) {
618 return ret;
619 }
620
621 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
622 return qemu_ftell(f) - last_ftell;
623 }
624
625 static int block_save_complete(QEMUFile *f, void *opaque)
626 {
627 int ret;
628
629 DPRINTF("Enter save live complete submitted %d transferred %d\n",
630 block_mig_state.submitted, block_mig_state.transferred);
631
632 ret = flush_blks(f);
633 if (ret) {
634 return ret;
635 }
636
637 blk_mig_reset_dirty_cursor();
638
639 /* we know for sure that save bulk is completed and
640 all async read completed */
641 blk_mig_lock();
642 assert(block_mig_state.submitted == 0);
643 blk_mig_unlock();
644
645 do {
646 ret = blk_mig_save_dirty_block(f, 0);
647 if (ret < 0) {
648 return ret;
649 }
650 } while (ret == 0);
651
652 /* report completion */
653 qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
654
655 DPRINTF("Block migration completed\n");
656
657 qemu_put_be64(f, BLK_MIG_FLAG_EOS);
658
659 blk_mig_cleanup();
660 return 0;
661 }
662
663 static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
664 {
665 /* Estimate pending number of bytes to send */
666 uint64_t pending;
667
668 blk_mig_lock();
669 pending = get_remaining_dirty() +
670 block_mig_state.submitted * BLOCK_SIZE +
671 block_mig_state.read_done * BLOCK_SIZE;
672
673 /* Report at least one block pending during bulk phase */
674 if (pending == 0 && !block_mig_state.bulk_completed) {
675 pending = BLOCK_SIZE;
676 }
677 blk_mig_unlock();
678
679 DPRINTF("Enter save live pending %" PRIu64 "\n", pending);
680 return pending;
681 }
682
683 static int block_load(QEMUFile *f, void *opaque, int version_id)
684 {
685 static int banner_printed;
686 int len, flags;
687 char device_name[256];
688 int64_t addr;
689 BlockDriverState *bs, *bs_prev = NULL;
690 uint8_t *buf;
691 int64_t total_sectors = 0;
692 int nr_sectors;
693 int ret;
694
695 do {
696 addr = qemu_get_be64(f);
697
698 flags = addr & ~BDRV_SECTOR_MASK;
699 addr >>= BDRV_SECTOR_BITS;
700
701 if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
702 /* get device name */
703 len = qemu_get_byte(f);
704 qemu_get_buffer(f, (uint8_t *)device_name, len);
705 device_name[len] = '\0';
706
707 bs = bdrv_find(device_name);
708 if (!bs) {
709 fprintf(stderr, "Error unknown block device %s\n",
710 device_name);
711 return -EINVAL;
712 }
713
714 if (bs != bs_prev) {
715 bs_prev = bs;
716 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
717 if (total_sectors <= 0) {
718 error_report("Error getting length of block device %s",
719 device_name);
720 return -EINVAL;
721 }
722 }
723
724 if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
725 nr_sectors = total_sectors - addr;
726 } else {
727 nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
728 }
729
730 buf = g_malloc(BLOCK_SIZE);
731
732 qemu_get_buffer(f, buf, BLOCK_SIZE);
733 ret = bdrv_write(bs, addr, buf, nr_sectors);
734
735 g_free(buf);
736 if (ret < 0) {
737 return ret;
738 }
739 } else if (flags & BLK_MIG_FLAG_PROGRESS) {
740 if (!banner_printed) {
741 printf("Receiving block device images\n");
742 banner_printed = 1;
743 }
744 printf("Completed %d %%%c", (int)addr,
745 (addr == 100) ? '\n' : '\r');
746 fflush(stdout);
747 } else if (!(flags & BLK_MIG_FLAG_EOS)) {
748 fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
749 return -EINVAL;
750 }
751 ret = qemu_file_get_error(f);
752 if (ret != 0) {
753 return ret;
754 }
755 } while (!(flags & BLK_MIG_FLAG_EOS));
756
757 return 0;
758 }
759
760 static void block_set_params(const MigrationParams *params, void *opaque)
761 {
762 block_mig_state.blk_enable = params->blk;
763 block_mig_state.shared_base = params->shared;
764
765 /* shared base means that blk_enable = 1 */
766 block_mig_state.blk_enable |= params->shared;
767 }
768
769 static bool block_is_active(void *opaque)
770 {
771 return block_mig_state.blk_enable == 1;
772 }
773
774 SaveVMHandlers savevm_block_handlers = {
775 .set_params = block_set_params,
776 .save_live_setup = block_save_setup,
777 .save_live_iterate = block_save_iterate,
778 .save_live_complete = block_save_complete,
779 .save_live_pending = block_save_pending,
780 .load_state = block_load,
781 .cancel = block_migration_cancel,
782 .is_active = block_is_active,
783 };
784
785 void blk_mig_init(void)
786 {
787 QSIMPLEQ_INIT(&block_mig_state.bmds_list);
788 QSIMPLEQ_INIT(&block_mig_state.blk_list);
789 qemu_mutex_init(&block_mig_state.lock);
790
791 register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
792 &block_mig_state);
793 }