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/register.h"
27 #include "qemu-file.h"
28 #include "migration/vmstate.h"
29 #include "sysemu/block-backend.h"
32 #define BLK_MIG_BLOCK_SIZE (1ULL << 20)
33 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLK_MIG_BLOCK_SIZE >> BDRV_SECTOR_BITS)
35 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
36 #define BLK_MIG_FLAG_EOS 0x02
37 #define BLK_MIG_FLAG_PROGRESS 0x04
38 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
40 #define MAX_IS_ALLOCATED_SEARCH (65536 * BDRV_SECTOR_SIZE)
42 #define MAX_IO_BUFFERS 512
43 #define MAX_PARALLEL_IO 16
45 typedef struct BlkMigDevState
{
46 /* Written during setup phase. Can be read without a lock. */
50 int64_t total_sectors
;
51 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
54 /* Only used by migration thread. Does not need a lock. */
59 /* Data in the aio_bitmap is protected by block migration lock.
60 * Allocation and free happen during setup and cleanup respectively.
62 unsigned long *aio_bitmap
;
64 /* Protected by block migration lock. */
65 int64_t completed_sectors
;
67 /* During migration this is protected by iothread lock / AioContext.
68 * Allocation and free happen during setup and cleanup respectively.
70 BdrvDirtyBitmap
*dirty_bitmap
;
73 typedef struct BlkMigBlock
{
74 /* Only used by migration thread. */
82 /* Protected by block migration lock. */
84 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
87 typedef struct BlkMigState
{
88 QSIMPLEQ_HEAD(, BlkMigDevState
) bmds_list
;
89 int64_t total_sector_sum
;
92 /* Protected by lock. */
93 QSIMPLEQ_HEAD(, BlkMigBlock
) blk_list
;
97 /* Only used by migration thread. Does not need a lock. */
102 /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
106 static BlkMigState block_mig_state
;
108 static void blk_mig_lock(void)
110 qemu_mutex_lock(&block_mig_state
.lock
);
113 static void blk_mig_unlock(void)
115 qemu_mutex_unlock(&block_mig_state
.lock
);
118 /* Must run outside of the iothread lock during the bulk phase,
119 * or the VM will stall.
122 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
125 uint64_t flags
= BLK_MIG_FLAG_DEVICE_BLOCK
;
127 if (block_mig_state
.zero_blocks
&&
128 buffer_is_zero(blk
->buf
, BLK_MIG_BLOCK_SIZE
)) {
129 flags
|= BLK_MIG_FLAG_ZERO_BLOCK
;
132 /* sector number and flags */
133 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
137 len
= strlen(blk
->bmds
->blk_name
);
138 qemu_put_byte(f
, len
);
139 qemu_put_buffer(f
, (uint8_t *) blk
->bmds
->blk_name
, len
);
141 /* if a block is zero we need to flush here since the network
142 * bandwidth is now a lot higher than the storage device bandwidth.
143 * thus if we queue zero blocks we slow down the migration */
144 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
149 qemu_put_buffer(f
, blk
->buf
, BLK_MIG_BLOCK_SIZE
);
152 int blk_mig_active(void)
154 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
157 int blk_mig_bulk_active(void)
159 return blk_mig_active() && !block_mig_state
.bulk_completed
;
162 uint64_t blk_mig_bytes_transferred(void)
164 BlkMigDevState
*bmds
;
168 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
169 sum
+= bmds
->completed_sectors
;
172 return sum
<< BDRV_SECTOR_BITS
;
175 uint64_t blk_mig_bytes_remaining(void)
177 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
180 uint64_t blk_mig_bytes_total(void)
182 BlkMigDevState
*bmds
;
185 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
186 sum
+= bmds
->total_sectors
;
188 return sum
<< BDRV_SECTOR_BITS
;
192 /* Called with migration lock held. */
194 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
196 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
198 if (sector
< blk_nb_sectors(bmds
->blk
)) {
199 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
200 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
206 /* Called with migration lock held. */
208 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
209 int nb_sectors
, int set
)
212 unsigned long val
, idx
, bit
;
214 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
215 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
217 for (; start
<= end
; start
++) {
218 idx
= start
/ (sizeof(unsigned long) * 8);
219 bit
= start
% (sizeof(unsigned long) * 8);
220 val
= bmds
->aio_bitmap
[idx
];
224 val
&= ~(1UL << bit
);
226 bmds
->aio_bitmap
[idx
] = val
;
230 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
232 BlockBackend
*bb
= bmds
->blk
;
235 bitmap_size
= blk_nb_sectors(bb
) + BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
236 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
238 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
241 /* Never hold migration lock when yielding to the main loop! */
243 static void blk_mig_read_cb(void *opaque
, int ret
)
245 BlkMigBlock
*blk
= opaque
;
250 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
251 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
253 block_mig_state
.submitted
--;
254 block_mig_state
.read_done
++;
255 assert(block_mig_state
.submitted
>= 0);
259 /* Called with no lock taken. */
261 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
263 int64_t total_sectors
= bmds
->total_sectors
;
264 int64_t cur_sector
= bmds
->cur_sector
;
265 BlockBackend
*bb
= bmds
->blk
;
270 if (bmds
->shared_base
) {
271 qemu_mutex_lock_iothread();
272 aio_context_acquire(blk_get_aio_context(bb
));
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
;
283 aio_context_release(blk_get_aio_context(bb
));
284 qemu_mutex_unlock_iothread();
287 if (cur_sector
>= total_sectors
) {
288 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
292 bmds
->completed_sectors
= cur_sector
;
294 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
296 /* we are going to transfer a full block even if it is not allocated */
297 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
299 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
300 nr_sectors
= total_sectors
- cur_sector
;
303 blk
= g_new(BlkMigBlock
, 1);
304 blk
->buf
= g_malloc(BLK_MIG_BLOCK_SIZE
);
306 blk
->sector
= cur_sector
;
307 blk
->nr_sectors
= nr_sectors
;
309 qemu_iovec_init_buf(&blk
->qiov
, blk
->buf
, nr_sectors
* BDRV_SECTOR_SIZE
);
312 block_mig_state
.submitted
++;
315 /* We do not know if bs is under the main thread (and thus does
316 * not acquire the AioContext when doing AIO) or rather under
317 * dataplane. Thus acquire both the iothread mutex and the
320 * This is ugly and will disappear when we make bdrv_* thread-safe,
321 * without the need to acquire the AioContext.
323 qemu_mutex_lock_iothread();
324 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
325 bdrv_reset_dirty_bitmap(bmds
->dirty_bitmap
, cur_sector
* BDRV_SECTOR_SIZE
,
326 nr_sectors
* BDRV_SECTOR_SIZE
);
327 blk
->aiocb
= blk_aio_preadv(bb
, cur_sector
* BDRV_SECTOR_SIZE
, &blk
->qiov
,
328 0, blk_mig_read_cb
, blk
);
329 aio_context_release(blk_get_aio_context(bmds
->blk
));
330 qemu_mutex_unlock_iothread();
332 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
333 return (bmds
->cur_sector
>= total_sectors
);
336 /* Called with iothread lock taken. */
338 static int set_dirty_tracking(void)
340 BlkMigDevState
*bmds
;
343 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
344 bmds
->dirty_bitmap
= bdrv_create_dirty_bitmap(blk_bs(bmds
->blk
),
347 if (!bmds
->dirty_bitmap
) {
355 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
356 if (bmds
->dirty_bitmap
) {
357 bdrv_release_dirty_bitmap(bmds
->dirty_bitmap
);
363 /* Called with iothread lock taken. */
365 static void unset_dirty_tracking(void)
367 BlkMigDevState
*bmds
;
369 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
370 bdrv_release_dirty_bitmap(bmds
->dirty_bitmap
);
374 static int init_blk_migration(QEMUFile
*f
)
376 BlockDriverState
*bs
;
377 BlkMigDevState
*bmds
;
382 BlkMigDevState
*bmds
;
383 BlockDriverState
*bs
;
385 Error
*local_err
= NULL
;
388 block_mig_state
.submitted
= 0;
389 block_mig_state
.read_done
= 0;
390 block_mig_state
.transferred
= 0;
391 block_mig_state
.total_sector_sum
= 0;
392 block_mig_state
.prev_progress
= -1;
393 block_mig_state
.bulk_completed
= 0;
394 block_mig_state
.zero_blocks
= migrate_zero_blocks();
396 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
399 bmds_bs
= g_malloc0(num_bs
* sizeof(*bmds_bs
));
401 for (i
= 0, bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
), i
++) {
402 if (bdrv_is_read_only(bs
)) {
406 sectors
= bdrv_nb_sectors(bs
);
409 bdrv_next_cleanup(&it
);
413 bmds
= g_new0(BlkMigDevState
, 1);
414 bmds
->blk
= blk_new(qemu_get_aio_context(),
415 BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
416 bmds
->blk_name
= g_strdup(bdrv_get_device_name(bs
));
417 bmds
->bulk_completed
= 0;
418 bmds
->total_sectors
= sectors
;
419 bmds
->completed_sectors
= 0;
420 bmds
->shared_base
= migrate_use_block_incremental();
423 bmds_bs
[i
].bmds
= bmds
;
426 block_mig_state
.total_sector_sum
+= sectors
;
428 if (bmds
->shared_base
) {
429 trace_migration_block_init_shared(bdrv_get_device_name(bs
));
431 trace_migration_block_init_full(bdrv_get_device_name(bs
));
434 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
437 /* Can only insert new BDSes now because doing so while iterating block
438 * devices may end up in a deadlock (iterating the new BDSes, too). */
439 for (i
= 0; i
< num_bs
; i
++) {
440 BlkMigDevState
*bmds
= bmds_bs
[i
].bmds
;
441 BlockDriverState
*bs
= bmds_bs
[i
].bs
;
444 ret
= blk_insert_bs(bmds
->blk
, bs
, &local_err
);
446 error_report_err(local_err
);
450 alloc_aio_bitmap(bmds
);
451 error_setg(&bmds
->blocker
, "block device is in use by migration");
452 bdrv_op_block_all(bs
, bmds
->blocker
);
462 /* Called with no lock taken. */
464 static int blk_mig_save_bulked_block(QEMUFile
*f
)
466 int64_t completed_sector_sum
= 0;
467 BlkMigDevState
*bmds
;
471 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
472 if (bmds
->bulk_completed
== 0) {
473 if (mig_save_device_bulk(f
, bmds
) == 1) {
474 /* completed bulk section for this device */
475 bmds
->bulk_completed
= 1;
477 completed_sector_sum
+= bmds
->completed_sectors
;
481 completed_sector_sum
+= bmds
->completed_sectors
;
485 if (block_mig_state
.total_sector_sum
!= 0) {
486 progress
= completed_sector_sum
* 100 /
487 block_mig_state
.total_sector_sum
;
491 if (progress
!= block_mig_state
.prev_progress
) {
492 block_mig_state
.prev_progress
= progress
;
493 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
494 | BLK_MIG_FLAG_PROGRESS
);
495 trace_migration_block_progression(progress
);
501 static void blk_mig_reset_dirty_cursor(void)
503 BlkMigDevState
*bmds
;
505 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
510 /* Called with iothread lock and AioContext taken. */
512 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
516 int64_t total_sectors
= bmds
->total_sectors
;
521 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
523 if (bmds_aio_inflight(bmds
, sector
)) {
525 blk_drain(bmds
->blk
);
529 bdrv_dirty_bitmap_lock(bmds
->dirty_bitmap
);
530 if (bdrv_dirty_bitmap_get_locked(bmds
->dirty_bitmap
,
531 sector
* BDRV_SECTOR_SIZE
)) {
532 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
533 nr_sectors
= total_sectors
- sector
;
535 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
537 bdrv_reset_dirty_bitmap_locked(bmds
->dirty_bitmap
,
538 sector
* BDRV_SECTOR_SIZE
,
539 nr_sectors
* BDRV_SECTOR_SIZE
);
540 bdrv_dirty_bitmap_unlock(bmds
->dirty_bitmap
);
542 blk
= g_new(BlkMigBlock
, 1);
543 blk
->buf
= g_malloc(BLK_MIG_BLOCK_SIZE
);
545 blk
->sector
= sector
;
546 blk
->nr_sectors
= nr_sectors
;
549 qemu_iovec_init_buf(&blk
->qiov
, blk
->buf
,
550 nr_sectors
* BDRV_SECTOR_SIZE
);
552 blk
->aiocb
= blk_aio_preadv(bmds
->blk
,
553 sector
* BDRV_SECTOR_SIZE
,
554 &blk
->qiov
, 0, blk_mig_read_cb
,
558 block_mig_state
.submitted
++;
559 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
562 ret
= blk_pread(bmds
->blk
, sector
* BDRV_SECTOR_SIZE
,
563 nr_sectors
* BDRV_SECTOR_SIZE
, blk
->buf
, 0);
573 sector
+= nr_sectors
;
574 bmds
->cur_dirty
= sector
;
578 bdrv_dirty_bitmap_unlock(bmds
->dirty_bitmap
);
579 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
580 bmds
->cur_dirty
= sector
;
583 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
586 trace_migration_block_save_device_dirty(sector
);
592 /* Called with iothread lock taken.
595 * 0: too much data for max_downtime
596 * 1: few enough data for max_downtime
598 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
600 BlkMigDevState
*bmds
;
603 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
604 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
605 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
606 aio_context_release(blk_get_aio_context(bmds
->blk
));
615 /* Called with no locks taken. */
617 static int flush_blks(QEMUFile
*f
)
622 trace_migration_block_flush_blks("Enter", block_mig_state
.submitted
,
623 block_mig_state
.read_done
,
624 block_mig_state
.transferred
);
627 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
628 if (qemu_file_rate_limit(f
)) {
636 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
644 block_mig_state
.read_done
--;
645 block_mig_state
.transferred
++;
646 assert(block_mig_state
.read_done
>= 0);
650 trace_migration_block_flush_blks("Exit", block_mig_state
.submitted
,
651 block_mig_state
.read_done
,
652 block_mig_state
.transferred
);
656 /* Called with iothread lock taken. */
658 static int64_t get_remaining_dirty(void)
660 BlkMigDevState
*bmds
;
663 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
664 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
665 dirty
+= bdrv_get_dirty_count(bmds
->dirty_bitmap
);
666 aio_context_release(blk_get_aio_context(bmds
->blk
));
674 /* Called with iothread lock taken. */
675 static void block_migration_cleanup_bmds(void)
677 BlkMigDevState
*bmds
;
680 unset_dirty_tracking();
682 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
683 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
684 bdrv_op_unblock_all(blk_bs(bmds
->blk
), bmds
->blocker
);
685 error_free(bmds
->blocker
);
687 /* Save ctx, because bmds->blk can disappear during blk_unref. */
688 ctx
= blk_get_aio_context(bmds
->blk
);
689 aio_context_acquire(ctx
);
690 blk_unref(bmds
->blk
);
691 aio_context_release(ctx
);
693 g_free(bmds
->blk_name
);
694 g_free(bmds
->aio_bitmap
);
699 /* Called with iothread lock taken. */
700 static void block_migration_cleanup(void *opaque
)
706 block_migration_cleanup_bmds();
709 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
710 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
717 static int block_save_setup(QEMUFile
*f
, void *opaque
)
721 trace_migration_block_save("setup", block_mig_state
.submitted
,
722 block_mig_state
.transferred
);
724 qemu_mutex_lock_iothread();
725 ret
= init_blk_migration(f
);
727 qemu_mutex_unlock_iothread();
731 /* start track dirty blocks */
732 ret
= set_dirty_tracking();
734 qemu_mutex_unlock_iothread();
741 blk_mig_reset_dirty_cursor();
742 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
747 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
750 int64_t last_bytes
= qemu_file_total_transferred(f
);
753 trace_migration_block_save("iterate", block_mig_state
.submitted
,
754 block_mig_state
.transferred
);
761 blk_mig_reset_dirty_cursor();
763 /* control the rate of transfer */
765 while (block_mig_state
.read_done
* BLK_MIG_BLOCK_SIZE
<
766 qemu_file_get_rate_limit(f
) &&
767 block_mig_state
.submitted
< MAX_PARALLEL_IO
&&
768 (block_mig_state
.submitted
+ block_mig_state
.read_done
) <
771 if (block_mig_state
.bulk_completed
== 0) {
772 /* first finish the bulk phase */
773 if (blk_mig_save_bulked_block(f
) == 0) {
774 /* finished saving bulk on all devices */
775 block_mig_state
.bulk_completed
= 1;
779 /* Always called with iothread lock taken for
780 * simplicity, block_save_complete also calls it.
782 qemu_mutex_lock_iothread();
783 ret
= blk_mig_save_dirty_block(f
, 1);
784 qemu_mutex_unlock_iothread();
791 /* no more dirty blocks */
802 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
803 delta_bytes
= qemu_file_total_transferred(f
) - last_bytes
;
804 if (delta_bytes
> 0) {
806 } else if (delta_bytes
< 0) {
813 /* Called with iothread lock taken. */
815 static int block_save_complete(QEMUFile
*f
, void *opaque
)
819 trace_migration_block_save("complete", block_mig_state
.submitted
,
820 block_mig_state
.transferred
);
827 blk_mig_reset_dirty_cursor();
829 /* we know for sure that save bulk is completed and
830 all async read completed */
832 assert(block_mig_state
.submitted
== 0);
836 ret
= blk_mig_save_dirty_block(f
, 0);
842 /* report completion */
843 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
845 trace_migration_block_save_complete();
847 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
849 /* Make sure that our BlockBackends are gone, so that the block driver
850 * nodes can be inactivated. */
851 block_migration_cleanup_bmds();
856 static void block_state_pending(void *opaque
, uint64_t *must_precopy
,
857 uint64_t *can_postcopy
)
859 /* Estimate pending number of bytes to send */
862 qemu_mutex_lock_iothread();
863 pending
= get_remaining_dirty();
864 qemu_mutex_unlock_iothread();
867 pending
+= block_mig_state
.submitted
* BLK_MIG_BLOCK_SIZE
+
868 block_mig_state
.read_done
* BLK_MIG_BLOCK_SIZE
;
871 /* Report at least one block pending during bulk phase */
872 if (!pending
&& !block_mig_state
.bulk_completed
) {
873 pending
= BLK_MIG_BLOCK_SIZE
;
876 trace_migration_block_state_pending(pending
);
877 /* We don't do postcopy */
878 *must_precopy
+= pending
;
881 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
883 static int banner_printed
;
885 char device_name
[256];
887 BlockBackend
*blk
, *blk_prev
= NULL
;
888 Error
*local_err
= NULL
;
890 int64_t total_sectors
= 0;
894 int cluster_size
= BLK_MIG_BLOCK_SIZE
;
897 addr
= qemu_get_be64(f
);
899 flags
= addr
& (BDRV_SECTOR_SIZE
- 1);
900 addr
>>= BDRV_SECTOR_BITS
;
902 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
903 /* get device name */
904 len
= qemu_get_byte(f
);
905 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
906 device_name
[len
] = '\0';
908 blk
= blk_by_name(device_name
);
910 fprintf(stderr
, "Error unknown block device %s\n",
915 if (blk
!= blk_prev
) {
917 total_sectors
= blk_nb_sectors(blk
);
918 if (total_sectors
<= 0) {
919 error_report("Error getting length of block device %s",
924 blk_activate(blk
, &local_err
);
926 error_report_err(local_err
);
930 ret
= bdrv_get_info(blk_bs(blk
), &bdi
);
931 if (ret
== 0 && bdi
.cluster_size
> 0 &&
932 bdi
.cluster_size
<= BLK_MIG_BLOCK_SIZE
&&
933 BLK_MIG_BLOCK_SIZE
% bdi
.cluster_size
== 0) {
934 cluster_size
= bdi
.cluster_size
;
936 cluster_size
= BLK_MIG_BLOCK_SIZE
;
940 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
941 nr_sectors
= total_sectors
- addr
;
943 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
946 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
947 ret
= blk_pwrite_zeroes(blk
, addr
* BDRV_SECTOR_SIZE
,
948 nr_sectors
* BDRV_SECTOR_SIZE
,
955 buf
= g_malloc(BLK_MIG_BLOCK_SIZE
);
956 qemu_get_buffer(f
, buf
, BLK_MIG_BLOCK_SIZE
);
957 for (i
= 0; i
< BLK_MIG_BLOCK_SIZE
/ cluster_size
; i
++) {
958 cur_addr
= addr
* BDRV_SECTOR_SIZE
+ i
* cluster_size
;
959 cur_buf
= buf
+ i
* cluster_size
;
961 if ((!block_mig_state
.zero_blocks
||
962 cluster_size
< BLK_MIG_BLOCK_SIZE
) &&
963 buffer_is_zero(cur_buf
, cluster_size
)) {
964 ret
= blk_pwrite_zeroes(blk
, cur_addr
,
968 ret
= blk_pwrite(blk
, cur_addr
, cluster_size
, cur_buf
,
981 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
982 if (!banner_printed
) {
983 printf("Receiving block device images\n");
986 printf("Completed %d %%%c", (int)addr
,
987 (addr
== 100) ? '\n' : '\r');
989 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
990 fprintf(stderr
, "Unknown block migration flags: 0x%x\n", flags
);
993 ret
= qemu_file_get_error(f
);
997 } while (!(flags
& BLK_MIG_FLAG_EOS
));
1002 static bool block_is_active(void *opaque
)
1004 return migrate_use_block();
1007 static SaveVMHandlers savevm_block_handlers
= {
1008 .save_setup
= block_save_setup
,
1009 .save_live_iterate
= block_save_iterate
,
1010 .save_live_complete_precopy
= block_save_complete
,
1011 .state_pending_exact
= block_state_pending
,
1012 .state_pending_estimate
= block_state_pending
,
1013 .load_state
= block_load
,
1014 .save_cleanup
= block_migration_cleanup
,
1015 .is_active
= block_is_active
,
1018 void blk_mig_init(void)
1020 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
1021 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
1022 qemu_mutex_init(&block_mig_state
.lock
);
1024 register_savevm_live("block", 0, 1, &savevm_block_handlers
,