2 * QEMU live block migration
4 * Copyright IBM, Corp. 2009
7 * Liran Schour <lirans@il.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/cutils.h"
21 #include "qemu/queue.h"
23 #include "block/dirty-bitmap.h"
24 #include "migration/misc.h"
25 #include "migration.h"
26 #include "migration-stats.h"
27 #include "migration/register.h"
28 #include "qemu-file.h"
29 #include "migration/vmstate.h"
30 #include "sysemu/block-backend.h"
34 #define BLK_MIG_BLOCK_SIZE (1ULL << 20)
35 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLK_MIG_BLOCK_SIZE >> BDRV_SECTOR_BITS)
37 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
38 #define BLK_MIG_FLAG_EOS 0x02
39 #define BLK_MIG_FLAG_PROGRESS 0x04
40 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
42 #define MAX_IS_ALLOCATED_SEARCH (65536 * BDRV_SECTOR_SIZE)
44 #define MAX_IO_BUFFERS 512
45 #define MAX_PARALLEL_IO 16
47 typedef struct BlkMigDevState
{
48 /* Written during setup phase. Can be read without a lock. */
52 int64_t total_sectors
;
53 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
56 /* Only used by migration thread. Does not need a lock. */
61 /* Data in the aio_bitmap is protected by block migration lock.
62 * Allocation and free happen during setup and cleanup respectively.
64 unsigned long *aio_bitmap
;
66 /* Protected by block migration lock. */
67 int64_t completed_sectors
;
69 /* During migration this is protected by bdrv_dirty_bitmap_lock().
70 * Allocation and free happen during setup and cleanup respectively.
72 BdrvDirtyBitmap
*dirty_bitmap
;
75 typedef struct BlkMigBlock
{
76 /* Only used by migration thread. */
84 /* Protected by block migration lock. */
86 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
89 typedef struct BlkMigState
{
90 QSIMPLEQ_HEAD(, BlkMigDevState
) bmds_list
;
91 int64_t total_sector_sum
;
94 /* Protected by lock. */
95 QSIMPLEQ_HEAD(, BlkMigBlock
) blk_list
;
99 /* Only used by migration thread. Does not need a lock. */
104 /* Lock must be taken _inside_ the BQL. */
108 static BlkMigState block_mig_state
;
110 static void blk_mig_lock(void)
112 qemu_mutex_lock(&block_mig_state
.lock
);
115 static void blk_mig_unlock(void)
117 qemu_mutex_unlock(&block_mig_state
.lock
);
120 /* Must run outside of the BQL during the bulk phase,
121 * or the VM will stall.
124 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
127 uint64_t flags
= BLK_MIG_FLAG_DEVICE_BLOCK
;
129 if (block_mig_state
.zero_blocks
&&
130 buffer_is_zero(blk
->buf
, BLK_MIG_BLOCK_SIZE
)) {
131 flags
|= BLK_MIG_FLAG_ZERO_BLOCK
;
134 /* sector number and flags */
135 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
139 len
= strlen(blk
->bmds
->blk_name
);
140 qemu_put_byte(f
, len
);
141 qemu_put_buffer(f
, (uint8_t *) blk
->bmds
->blk_name
, len
);
143 /* if a block is zero we need to flush here since the network
144 * bandwidth is now a lot higher than the storage device bandwidth.
145 * thus if we queue zero blocks we slow down the migration */
146 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
151 qemu_put_buffer(f
, blk
->buf
, BLK_MIG_BLOCK_SIZE
);
154 int blk_mig_active(void)
156 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
159 int blk_mig_bulk_active(void)
161 return blk_mig_active() && !block_mig_state
.bulk_completed
;
164 uint64_t blk_mig_bytes_transferred(void)
166 BlkMigDevState
*bmds
;
170 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
171 sum
+= bmds
->completed_sectors
;
174 return sum
<< BDRV_SECTOR_BITS
;
177 uint64_t blk_mig_bytes_remaining(void)
179 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
182 uint64_t blk_mig_bytes_total(void)
184 BlkMigDevState
*bmds
;
187 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
188 sum
+= bmds
->total_sectors
;
190 return sum
<< BDRV_SECTOR_BITS
;
194 /* Called with migration lock held. */
196 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
198 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
200 if (sector
< bmds
->total_sectors
) {
201 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
202 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
208 /* Called with migration lock held. */
210 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
211 int nb_sectors
, int set
)
214 unsigned long val
, idx
, bit
;
216 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
217 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
219 for (; start
<= end
; start
++) {
220 idx
= start
/ (sizeof(unsigned long) * 8);
221 bit
= start
% (sizeof(unsigned long) * 8);
222 val
= bmds
->aio_bitmap
[idx
];
226 val
&= ~(1UL << bit
);
228 bmds
->aio_bitmap
[idx
] = val
;
232 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
236 bitmap_size
= bmds
->total_sectors
+ BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
237 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
239 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
242 /* Never hold migration lock when yielding to the main loop! */
244 static void blk_mig_read_cb(void *opaque
, int ret
)
246 BlkMigBlock
*blk
= opaque
;
251 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
252 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
254 block_mig_state
.submitted
--;
255 block_mig_state
.read_done
++;
256 assert(block_mig_state
.submitted
>= 0);
260 /* Called with no lock taken. */
262 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
264 int64_t total_sectors
= bmds
->total_sectors
;
265 int64_t cur_sector
= bmds
->cur_sector
;
266 BlockBackend
*bb
= bmds
->blk
;
271 if (bmds
->shared_base
) {
273 /* Skip unallocated sectors; intentionally treats failure or
274 * partial sector as an allocated sector */
275 while (cur_sector
< total_sectors
&&
276 !bdrv_is_allocated(blk_bs(bb
), cur_sector
* BDRV_SECTOR_SIZE
,
277 MAX_IS_ALLOCATED_SEARCH
, &count
)) {
278 if (count
< BDRV_SECTOR_SIZE
) {
281 cur_sector
+= count
>> BDRV_SECTOR_BITS
;
286 if (cur_sector
>= total_sectors
) {
287 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
291 bmds
->completed_sectors
= cur_sector
;
293 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
295 /* we are going to transfer a full block even if it is not allocated */
296 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
298 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
299 nr_sectors
= total_sectors
- cur_sector
;
302 blk
= g_new(BlkMigBlock
, 1);
303 blk
->buf
= g_malloc(BLK_MIG_BLOCK_SIZE
);
305 blk
->sector
= cur_sector
;
306 blk
->nr_sectors
= nr_sectors
;
308 qemu_iovec_init_buf(&blk
->qiov
, blk
->buf
, nr_sectors
* BDRV_SECTOR_SIZE
);
311 block_mig_state
.submitted
++;
315 * The migration thread does not have an AioContext. Lock the BQL so that
316 * I/O runs in the main loop AioContext (see
317 * qemu_get_current_aio_context()).
320 bdrv_reset_dirty_bitmap(bmds
->dirty_bitmap
, cur_sector
* BDRV_SECTOR_SIZE
,
321 nr_sectors
* BDRV_SECTOR_SIZE
);
322 blk
->aiocb
= blk_aio_preadv(bb
, cur_sector
* BDRV_SECTOR_SIZE
, &blk
->qiov
,
323 0, blk_mig_read_cb
, blk
);
326 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
327 return (bmds
->cur_sector
>= total_sectors
);
330 /* Called with the BQL taken. */
332 static int set_dirty_tracking(void)
334 BlkMigDevState
*bmds
;
337 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
338 bmds
->dirty_bitmap
= bdrv_create_dirty_bitmap(blk_bs(bmds
->blk
),
341 if (!bmds
->dirty_bitmap
) {
349 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
350 if (bmds
->dirty_bitmap
) {
351 bdrv_release_dirty_bitmap(bmds
->dirty_bitmap
);
357 /* Called with the BQL taken. */
359 static void unset_dirty_tracking(void)
361 BlkMigDevState
*bmds
;
363 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
364 if (bmds
->dirty_bitmap
) {
365 bdrv_release_dirty_bitmap(bmds
->dirty_bitmap
);
370 static int init_blk_migration(QEMUFile
*f
)
372 BlockDriverState
*bs
;
373 BlkMigDevState
*bmds
;
378 BlkMigDevState
*bmds
;
379 BlockDriverState
*bs
;
381 Error
*local_err
= NULL
;
384 GRAPH_RDLOCK_GUARD_MAINLOOP();
386 block_mig_state
.submitted
= 0;
387 block_mig_state
.read_done
= 0;
388 block_mig_state
.transferred
= 0;
389 block_mig_state
.total_sector_sum
= 0;
390 block_mig_state
.prev_progress
= -1;
391 block_mig_state
.bulk_completed
= 0;
392 block_mig_state
.zero_blocks
= migrate_zero_blocks();
394 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
397 bmds_bs
= g_malloc0(num_bs
* sizeof(*bmds_bs
));
399 for (i
= 0, bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
), i
++) {
400 if (bdrv_is_read_only(bs
)) {
404 sectors
= bdrv_nb_sectors(bs
);
410 bdrv_next_cleanup(&it
);
414 bmds
= g_new0(BlkMigDevState
, 1);
415 bmds
->blk
= blk_new(qemu_get_aio_context(),
416 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
417 bmds
->blk_name
= g_strdup(bdrv_get_device_name(bs
));
418 bmds
->bulk_completed
= 0;
419 bmds
->total_sectors
= sectors
;
420 bmds
->completed_sectors
= 0;
421 bmds
->shared_base
= migrate_block_incremental();
424 bmds_bs
[i
].bmds
= bmds
;
427 block_mig_state
.total_sector_sum
+= sectors
;
429 if (bmds
->shared_base
) {
430 trace_migration_block_init_shared(bdrv_get_device_name(bs
));
432 trace_migration_block_init_full(bdrv_get_device_name(bs
));
435 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
438 /* Can only insert new BDSes now because doing so while iterating block
439 * devices may end up in a deadlock (iterating the new BDSes, too). */
440 for (i
= 0; i
< num_bs
; i
++) {
441 bmds
= bmds_bs
[i
].bmds
;
445 ret
= blk_insert_bs(bmds
->blk
, bs
, &local_err
);
447 error_report_err(local_err
);
451 alloc_aio_bitmap(bmds
);
452 error_setg(&bmds
->blocker
, "block device is in use by migration");
453 bdrv_op_block_all(bs
, bmds
->blocker
);
463 /* Called with no lock taken. */
465 static int blk_mig_save_bulked_block(QEMUFile
*f
)
467 int64_t completed_sector_sum
= 0;
468 BlkMigDevState
*bmds
;
472 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
473 if (bmds
->bulk_completed
== 0) {
474 if (mig_save_device_bulk(f
, bmds
) == 1) {
475 /* completed bulk section for this device */
476 bmds
->bulk_completed
= 1;
478 completed_sector_sum
+= bmds
->completed_sectors
;
482 completed_sector_sum
+= bmds
->completed_sectors
;
486 if (block_mig_state
.total_sector_sum
!= 0) {
487 progress
= completed_sector_sum
* 100 /
488 block_mig_state
.total_sector_sum
;
492 if (progress
!= block_mig_state
.prev_progress
) {
493 block_mig_state
.prev_progress
= progress
;
494 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
495 | BLK_MIG_FLAG_PROGRESS
);
496 trace_migration_block_progression(progress
);
502 static void blk_mig_reset_dirty_cursor(void)
504 BlkMigDevState
*bmds
;
506 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
511 /* Called with the BQL taken. */
513 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
517 int64_t total_sectors
= bmds
->total_sectors
;
522 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
524 if (bmds_aio_inflight(bmds
, sector
)) {
526 blk_drain(bmds
->blk
);
530 bdrv_dirty_bitmap_lock(bmds
->dirty_bitmap
);
531 if (bdrv_dirty_bitmap_get_locked(bmds
->dirty_bitmap
,
532 sector
* BDRV_SECTOR_SIZE
)) {
533 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
534 nr_sectors
= total_sectors
- sector
;
536 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
538 bdrv_reset_dirty_bitmap_locked(bmds
->dirty_bitmap
,
539 sector
* BDRV_SECTOR_SIZE
,
540 nr_sectors
* BDRV_SECTOR_SIZE
);
541 bdrv_dirty_bitmap_unlock(bmds
->dirty_bitmap
);
543 blk
= g_new(BlkMigBlock
, 1);
544 blk
->buf
= g_malloc(BLK_MIG_BLOCK_SIZE
);
546 blk
->sector
= sector
;
547 blk
->nr_sectors
= nr_sectors
;
550 qemu_iovec_init_buf(&blk
->qiov
, blk
->buf
,
551 nr_sectors
* BDRV_SECTOR_SIZE
);
553 blk
->aiocb
= blk_aio_preadv(bmds
->blk
,
554 sector
* BDRV_SECTOR_SIZE
,
555 &blk
->qiov
, 0, blk_mig_read_cb
,
559 block_mig_state
.submitted
++;
560 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
563 ret
= blk_pread(bmds
->blk
, sector
* BDRV_SECTOR_SIZE
,
564 nr_sectors
* BDRV_SECTOR_SIZE
, blk
->buf
, 0);
574 sector
+= nr_sectors
;
575 bmds
->cur_dirty
= sector
;
579 bdrv_dirty_bitmap_unlock(bmds
->dirty_bitmap
);
580 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
581 bmds
->cur_dirty
= sector
;
584 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
587 trace_migration_block_save_device_dirty(sector
);
593 /* Called with the BQL taken.
596 * 0: too much data for max_downtime
597 * 1: few enough data for max_downtime
599 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
601 BlkMigDevState
*bmds
;
604 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
605 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
614 /* Called with no locks taken. */
616 static int flush_blks(QEMUFile
*f
)
621 trace_migration_block_flush_blks("Enter", block_mig_state
.submitted
,
622 block_mig_state
.read_done
,
623 block_mig_state
.transferred
);
626 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
627 if (migration_rate_exceeded(f
)) {
635 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
643 block_mig_state
.read_done
--;
644 block_mig_state
.transferred
++;
645 assert(block_mig_state
.read_done
>= 0);
649 trace_migration_block_flush_blks("Exit", block_mig_state
.submitted
,
650 block_mig_state
.read_done
,
651 block_mig_state
.transferred
);
655 /* Called with the BQL taken. */
657 static int64_t get_remaining_dirty(void)
659 BlkMigDevState
*bmds
;
662 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
663 bdrv_dirty_bitmap_lock(bmds
->dirty_bitmap
);
664 dirty
+= bdrv_get_dirty_count(bmds
->dirty_bitmap
);
665 bdrv_dirty_bitmap_unlock(bmds
->dirty_bitmap
);
673 /* Called with the BQL taken. */
674 static void block_migration_cleanup_bmds(void)
676 BlkMigDevState
*bmds
;
677 BlockDriverState
*bs
;
679 unset_dirty_tracking();
681 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
682 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
684 bs
= blk_bs(bmds
->blk
);
686 bdrv_op_unblock_all(bs
, bmds
->blocker
);
688 error_free(bmds
->blocker
);
689 blk_unref(bmds
->blk
);
690 g_free(bmds
->blk_name
);
691 g_free(bmds
->aio_bitmap
);
696 /* Called with the BQL taken. */
697 static void block_migration_cleanup(void *opaque
)
703 block_migration_cleanup_bmds();
706 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
707 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
714 static int block_save_setup(QEMUFile
*f
, void *opaque
)
718 trace_migration_block_save("setup", block_mig_state
.submitted
,
719 block_mig_state
.transferred
);
721 warn_report("block migration is deprecated;"
722 " use blockdev-mirror with NBD instead");
724 ret
= init_blk_migration(f
);
729 /* start track dirty blocks */
730 ret
= set_dirty_tracking();
736 blk_mig_reset_dirty_cursor();
737 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
742 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
745 uint64_t last_bytes
= qemu_file_transferred(f
);
747 trace_migration_block_save("iterate", block_mig_state
.submitted
,
748 block_mig_state
.transferred
);
755 blk_mig_reset_dirty_cursor();
757 /* control the rate of transfer */
759 while (block_mig_state
.read_done
* BLK_MIG_BLOCK_SIZE
<
760 migration_rate_get() &&
761 block_mig_state
.submitted
< MAX_PARALLEL_IO
&&
762 (block_mig_state
.submitted
+ block_mig_state
.read_done
) <
765 if (block_mig_state
.bulk_completed
== 0) {
766 /* first finish the bulk phase */
767 if (blk_mig_save_bulked_block(f
) == 0) {
768 /* finished saving bulk on all devices */
769 block_mig_state
.bulk_completed
= 1;
773 /* Always called with the BQL taken for
774 * simplicity, block_save_complete also calls it.
777 ret
= blk_mig_save_dirty_block(f
, 1);
785 /* no more dirty blocks */
796 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
797 uint64_t delta_bytes
= qemu_file_transferred(f
) - last_bytes
;
798 return (delta_bytes
> 0);
801 /* Called with the BQL taken. */
803 static int block_save_complete(QEMUFile
*f
, void *opaque
)
807 trace_migration_block_save("complete", block_mig_state
.submitted
,
808 block_mig_state
.transferred
);
815 blk_mig_reset_dirty_cursor();
817 /* we know for sure that save bulk is completed and
818 all async read completed */
820 assert(block_mig_state
.submitted
== 0);
824 ret
= blk_mig_save_dirty_block(f
, 0);
830 /* report completion */
831 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
833 trace_migration_block_save_complete();
835 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
837 /* Make sure that our BlockBackends are gone, so that the block driver
838 * nodes can be inactivated. */
839 block_migration_cleanup_bmds();
844 static void block_state_pending(void *opaque
, uint64_t *must_precopy
,
845 uint64_t *can_postcopy
)
847 /* Estimate pending number of bytes to send */
851 pending
= get_remaining_dirty();
855 pending
+= block_mig_state
.submitted
* BLK_MIG_BLOCK_SIZE
+
856 block_mig_state
.read_done
* BLK_MIG_BLOCK_SIZE
;
859 /* Report at least one block pending during bulk phase */
860 if (!pending
&& !block_mig_state
.bulk_completed
) {
861 pending
= BLK_MIG_BLOCK_SIZE
;
864 trace_migration_block_state_pending(pending
);
865 /* We don't do postcopy */
866 *must_precopy
+= pending
;
869 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
871 static int banner_printed
;
873 char device_name
[256];
875 BlockBackend
*blk
, *blk_prev
= NULL
;
876 Error
*local_err
= NULL
;
878 int64_t total_sectors
= 0;
882 int cluster_size
= BLK_MIG_BLOCK_SIZE
;
885 addr
= qemu_get_be64(f
);
887 flags
= addr
& (BDRV_SECTOR_SIZE
- 1);
888 addr
>>= BDRV_SECTOR_BITS
;
890 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
891 /* get device name */
892 len
= qemu_get_byte(f
);
893 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
894 device_name
[len
] = '\0';
896 blk
= blk_by_name(device_name
);
898 fprintf(stderr
, "Error unknown block device %s\n",
903 if (blk
!= blk_prev
) {
905 total_sectors
= blk_nb_sectors(blk
);
906 if (total_sectors
<= 0) {
907 error_report("Error getting length of block device %s",
912 blk_activate(blk
, &local_err
);
914 error_report_err(local_err
);
918 ret
= bdrv_get_info(blk_bs(blk
), &bdi
);
919 if (ret
== 0 && bdi
.cluster_size
> 0 &&
920 bdi
.cluster_size
<= BLK_MIG_BLOCK_SIZE
&&
921 BLK_MIG_BLOCK_SIZE
% bdi
.cluster_size
== 0) {
922 cluster_size
= bdi
.cluster_size
;
924 cluster_size
= BLK_MIG_BLOCK_SIZE
;
928 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
929 nr_sectors
= total_sectors
- addr
;
931 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
934 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
935 ret
= blk_pwrite_zeroes(blk
, addr
* BDRV_SECTOR_SIZE
,
936 nr_sectors
* BDRV_SECTOR_SIZE
,
943 buf
= g_malloc(BLK_MIG_BLOCK_SIZE
);
944 qemu_get_buffer(f
, buf
, BLK_MIG_BLOCK_SIZE
);
945 for (i
= 0; i
< BLK_MIG_BLOCK_SIZE
/ cluster_size
; i
++) {
946 cur_addr
= addr
* BDRV_SECTOR_SIZE
+ i
* cluster_size
;
947 cur_buf
= buf
+ i
* cluster_size
;
949 if ((!block_mig_state
.zero_blocks
||
950 cluster_size
< BLK_MIG_BLOCK_SIZE
) &&
951 buffer_is_zero(cur_buf
, cluster_size
)) {
952 ret
= blk_pwrite_zeroes(blk
, cur_addr
,
956 ret
= blk_pwrite(blk
, cur_addr
, cluster_size
, cur_buf
,
969 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
970 if (!banner_printed
) {
971 printf("Receiving block device images\n");
974 printf("Completed %d %%%c", (int)addr
,
975 (addr
== 100) ? '\n' : '\r');
977 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
978 fprintf(stderr
, "Unknown block migration flags: 0x%x\n", flags
);
981 ret
= qemu_file_get_error(f
);
985 } while (!(flags
& BLK_MIG_FLAG_EOS
));
990 static bool block_is_active(void *opaque
)
992 return migrate_block();
995 static SaveVMHandlers savevm_block_handlers
= {
996 .save_setup
= block_save_setup
,
997 .save_live_iterate
= block_save_iterate
,
998 .save_live_complete_precopy
= block_save_complete
,
999 .state_pending_exact
= block_state_pending
,
1000 .state_pending_estimate
= block_state_pending
,
1001 .load_state
= block_load
,
1002 .save_cleanup
= block_migration_cleanup
,
1003 .is_active
= block_is_active
,
1006 void blk_mig_init(void)
1008 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
1009 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
1010 qemu_mutex_init(&block_mig_state
.lock
);
1012 register_savevm_live("block", 0, 1, &savevm_block_handlers
,