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-common.h"
19 #include "block/block.h"
20 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
23 #include "qemu/cutils.h"
24 #include "qemu/queue.h"
25 #include "qemu/timer.h"
26 #include "migration/block.h"
27 #include "migration/migration.h"
28 #include "sysemu/blockdev.h"
29 #include "migration/qemu-file.h"
30 #include "migration/vmstate.h"
31 #include "sysemu/block-backend.h"
33 #define BLOCK_SIZE (1 << 20)
34 #define BDRV_SECTORS_PER_DIRTY_CHUNK (BLOCK_SIZE >> BDRV_SECTOR_BITS)
36 #define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
37 #define BLK_MIG_FLAG_EOS 0x02
38 #define BLK_MIG_FLAG_PROGRESS 0x04
39 #define BLK_MIG_FLAG_ZERO_BLOCK 0x08
41 #define MAX_IS_ALLOCATED_SEARCH 65536
43 #define MAX_INFLIGHT_IO 512
45 //#define DEBUG_BLK_MIGRATION
47 #ifdef DEBUG_BLK_MIGRATION
48 #define DPRINTF(fmt, ...) \
49 do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
51 #define DPRINTF(fmt, ...) \
55 typedef struct BlkMigDevState
{
56 /* Written during setup phase. Can be read without a lock. */
60 int64_t total_sectors
;
61 QSIMPLEQ_ENTRY(BlkMigDevState
) entry
;
64 /* Only used by migration thread. Does not need a lock. */
69 /* Data in the aio_bitmap is protected by block migration lock.
70 * Allocation and free happen during setup and cleanup respectively.
72 unsigned long *aio_bitmap
;
74 /* Protected by block migration lock. */
75 int64_t completed_sectors
;
77 /* During migration this is protected by iothread lock / AioContext.
78 * Allocation and free happen during setup and cleanup respectively.
80 BdrvDirtyBitmap
*dirty_bitmap
;
83 typedef struct BlkMigBlock
{
84 /* Only used by migration thread. */
93 /* Protected by block migration lock. */
95 QSIMPLEQ_ENTRY(BlkMigBlock
) entry
;
98 typedef struct BlkMigState
{
99 QSIMPLEQ_HEAD(bmds_list
, BlkMigDevState
) bmds_list
;
100 int64_t total_sector_sum
;
103 /* Protected by lock. */
104 QSIMPLEQ_HEAD(blk_list
, BlkMigBlock
) blk_list
;
108 /* Only used by migration thread. Does not need a lock. */
113 /* Lock must be taken _inside_ the iothread lock and any AioContexts. */
117 static BlkMigState block_mig_state
;
119 static void blk_mig_lock(void)
121 qemu_mutex_lock(&block_mig_state
.lock
);
124 static void blk_mig_unlock(void)
126 qemu_mutex_unlock(&block_mig_state
.lock
);
129 /* Must run outside of the iothread lock during the bulk phase,
130 * or the VM will stall.
133 static void blk_send(QEMUFile
*f
, BlkMigBlock
* blk
)
136 uint64_t flags
= BLK_MIG_FLAG_DEVICE_BLOCK
;
138 if (block_mig_state
.zero_blocks
&&
139 buffer_is_zero(blk
->buf
, BLOCK_SIZE
)) {
140 flags
|= BLK_MIG_FLAG_ZERO_BLOCK
;
143 /* sector number and flags */
144 qemu_put_be64(f
, (blk
->sector
<< BDRV_SECTOR_BITS
)
148 len
= strlen(blk
->bmds
->blk_name
);
149 qemu_put_byte(f
, len
);
150 qemu_put_buffer(f
, (uint8_t *) blk
->bmds
->blk_name
, len
);
152 /* if a block is zero we need to flush here since the network
153 * bandwidth is now a lot higher than the storage device bandwidth.
154 * thus if we queue zero blocks we slow down the migration */
155 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
160 qemu_put_buffer(f
, blk
->buf
, BLOCK_SIZE
);
163 int blk_mig_active(void)
165 return !QSIMPLEQ_EMPTY(&block_mig_state
.bmds_list
);
168 uint64_t blk_mig_bytes_transferred(void)
170 BlkMigDevState
*bmds
;
174 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
175 sum
+= bmds
->completed_sectors
;
178 return sum
<< BDRV_SECTOR_BITS
;
181 uint64_t blk_mig_bytes_remaining(void)
183 return blk_mig_bytes_total() - blk_mig_bytes_transferred();
186 uint64_t blk_mig_bytes_total(void)
188 BlkMigDevState
*bmds
;
191 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
192 sum
+= bmds
->total_sectors
;
194 return sum
<< BDRV_SECTOR_BITS
;
198 /* Called with migration lock held. */
200 static int bmds_aio_inflight(BlkMigDevState
*bmds
, int64_t sector
)
202 int64_t chunk
= sector
/ (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
;
204 if (sector
< blk_nb_sectors(bmds
->blk
)) {
205 return !!(bmds
->aio_bitmap
[chunk
/ (sizeof(unsigned long) * 8)] &
206 (1UL << (chunk
% (sizeof(unsigned long) * 8))));
212 /* Called with migration lock held. */
214 static void bmds_set_aio_inflight(BlkMigDevState
*bmds
, int64_t sector_num
,
215 int nb_sectors
, int set
)
218 unsigned long val
, idx
, bit
;
220 start
= sector_num
/ BDRV_SECTORS_PER_DIRTY_CHUNK
;
221 end
= (sector_num
+ nb_sectors
- 1) / BDRV_SECTORS_PER_DIRTY_CHUNK
;
223 for (; start
<= end
; start
++) {
224 idx
= start
/ (sizeof(unsigned long) * 8);
225 bit
= start
% (sizeof(unsigned long) * 8);
226 val
= bmds
->aio_bitmap
[idx
];
230 val
&= ~(1UL << bit
);
232 bmds
->aio_bitmap
[idx
] = val
;
236 static void alloc_aio_bitmap(BlkMigDevState
*bmds
)
238 BlockBackend
*bb
= bmds
->blk
;
241 bitmap_size
= blk_nb_sectors(bb
) + BDRV_SECTORS_PER_DIRTY_CHUNK
* 8 - 1;
242 bitmap_size
/= BDRV_SECTORS_PER_DIRTY_CHUNK
* 8;
244 bmds
->aio_bitmap
= g_malloc0(bitmap_size
);
247 /* Never hold migration lock when yielding to the main loop! */
249 static void blk_mig_read_cb(void *opaque
, int ret
)
251 BlkMigBlock
*blk
= opaque
;
256 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.blk_list
, blk
, entry
);
257 bmds_set_aio_inflight(blk
->bmds
, blk
->sector
, blk
->nr_sectors
, 0);
259 block_mig_state
.submitted
--;
260 block_mig_state
.read_done
++;
261 assert(block_mig_state
.submitted
>= 0);
265 /* Called with no lock taken. */
267 static int mig_save_device_bulk(QEMUFile
*f
, BlkMigDevState
*bmds
)
269 int64_t total_sectors
= bmds
->total_sectors
;
270 int64_t cur_sector
= bmds
->cur_sector
;
271 BlockBackend
*bb
= bmds
->blk
;
275 if (bmds
->shared_base
) {
276 qemu_mutex_lock_iothread();
277 aio_context_acquire(blk_get_aio_context(bb
));
278 /* Skip unallocated sectors; intentionally treats failure as
279 * an allocated sector */
280 while (cur_sector
< total_sectors
&&
281 !bdrv_is_allocated(blk_bs(bb
), cur_sector
,
282 MAX_IS_ALLOCATED_SEARCH
, &nr_sectors
)) {
283 cur_sector
+= nr_sectors
;
285 aio_context_release(blk_get_aio_context(bb
));
286 qemu_mutex_unlock_iothread();
289 if (cur_sector
>= total_sectors
) {
290 bmds
->cur_sector
= bmds
->completed_sectors
= total_sectors
;
294 bmds
->completed_sectors
= cur_sector
;
296 cur_sector
&= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK
- 1);
298 /* we are going to transfer a full block even if it is not allocated */
299 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
301 if (total_sectors
- cur_sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
302 nr_sectors
= total_sectors
- cur_sector
;
305 blk
= g_new(BlkMigBlock
, 1);
306 blk
->buf
= g_malloc(BLOCK_SIZE
);
308 blk
->sector
= cur_sector
;
309 blk
->nr_sectors
= nr_sectors
;
311 blk
->iov
.iov_base
= blk
->buf
;
312 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
313 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
316 block_mig_state
.submitted
++;
319 /* We do not know if bs is under the main thread (and thus does
320 * not acquire the AioContext when doing AIO) or rather under
321 * dataplane. Thus acquire both the iothread mutex and the
324 * This is ugly and will disappear when we make bdrv_* thread-safe,
325 * without the need to acquire the AioContext.
327 qemu_mutex_lock_iothread();
328 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
329 blk
->aiocb
= blk_aio_preadv(bb
, cur_sector
* BDRV_SECTOR_SIZE
, &blk
->qiov
,
330 0, blk_mig_read_cb
, blk
);
332 bdrv_reset_dirty_bitmap(bmds
->dirty_bitmap
, cur_sector
, nr_sectors
);
333 aio_context_release(blk_get_aio_context(bmds
->blk
));
334 qemu_mutex_unlock_iothread();
336 bmds
->cur_sector
= cur_sector
+ nr_sectors
;
337 return (bmds
->cur_sector
>= total_sectors
);
340 /* Called with iothread lock taken. */
342 static int set_dirty_tracking(void)
344 BlkMigDevState
*bmds
;
347 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
348 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
349 bmds
->dirty_bitmap
= bdrv_create_dirty_bitmap(blk_bs(bmds
->blk
),
350 BLOCK_SIZE
, NULL
, NULL
);
351 aio_context_release(blk_get_aio_context(bmds
->blk
));
352 if (!bmds
->dirty_bitmap
) {
360 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
361 if (bmds
->dirty_bitmap
) {
362 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
363 bdrv_release_dirty_bitmap(blk_bs(bmds
->blk
), bmds
->dirty_bitmap
);
364 aio_context_release(blk_get_aio_context(bmds
->blk
));
370 /* Called with iothread lock taken. */
372 static void unset_dirty_tracking(void)
374 BlkMigDevState
*bmds
;
376 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
377 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
378 bdrv_release_dirty_bitmap(blk_bs(bmds
->blk
), bmds
->dirty_bitmap
);
379 aio_context_release(blk_get_aio_context(bmds
->blk
));
383 static int init_blk_migration(QEMUFile
*f
)
385 BlockDriverState
*bs
;
386 BlkMigDevState
*bmds
;
391 BlkMigDevState
*bmds
;
392 BlockDriverState
*bs
;
394 Error
*local_err
= NULL
;
397 block_mig_state
.submitted
= 0;
398 block_mig_state
.read_done
= 0;
399 block_mig_state
.transferred
= 0;
400 block_mig_state
.total_sector_sum
= 0;
401 block_mig_state
.prev_progress
= -1;
402 block_mig_state
.bulk_completed
= 0;
403 block_mig_state
.zero_blocks
= migrate_zero_blocks();
405 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
408 bmds_bs
= g_malloc0(num_bs
* sizeof(*bmds_bs
));
410 for (i
= 0, bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
), i
++) {
411 if (bdrv_is_read_only(bs
)) {
415 sectors
= bdrv_nb_sectors(bs
);
421 bmds
= g_new0(BlkMigDevState
, 1);
422 bmds
->blk
= blk_new(BLK_PERM_CONSISTENT_READ
, BLK_PERM_ALL
);
423 bmds
->blk_name
= g_strdup(bdrv_get_device_name(bs
));
424 bmds
->bulk_completed
= 0;
425 bmds
->total_sectors
= sectors
;
426 bmds
->completed_sectors
= 0;
427 bmds
->shared_base
= migrate_use_block_incremental();
430 bmds_bs
[i
].bmds
= bmds
;
433 block_mig_state
.total_sector_sum
+= sectors
;
435 if (bmds
->shared_base
) {
436 DPRINTF("Start migration for %s with shared base image\n",
437 bdrv_get_device_name(bs
));
439 DPRINTF("Start full migration for %s\n", bdrv_get_device_name(bs
));
442 QSIMPLEQ_INSERT_TAIL(&block_mig_state
.bmds_list
, bmds
, entry
);
445 /* Can only insert new BDSes now because doing so while iterating block
446 * devices may end up in a deadlock (iterating the new BDSes, too). */
447 for (i
= 0; i
< num_bs
; i
++) {
448 BlkMigDevState
*bmds
= bmds_bs
[i
].bmds
;
449 BlockDriverState
*bs
= bmds_bs
[i
].bs
;
452 ret
= blk_insert_bs(bmds
->blk
, bs
, &local_err
);
454 error_report_err(local_err
);
458 alloc_aio_bitmap(bmds
);
459 error_setg(&bmds
->blocker
, "block device is in use by migration");
460 bdrv_op_block_all(bs
, bmds
->blocker
);
470 /* Called with no lock taken. */
472 static int blk_mig_save_bulked_block(QEMUFile
*f
)
474 int64_t completed_sector_sum
= 0;
475 BlkMigDevState
*bmds
;
479 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
480 if (bmds
->bulk_completed
== 0) {
481 if (mig_save_device_bulk(f
, bmds
) == 1) {
482 /* completed bulk section for this device */
483 bmds
->bulk_completed
= 1;
485 completed_sector_sum
+= bmds
->completed_sectors
;
489 completed_sector_sum
+= bmds
->completed_sectors
;
493 if (block_mig_state
.total_sector_sum
!= 0) {
494 progress
= completed_sector_sum
* 100 /
495 block_mig_state
.total_sector_sum
;
499 if (progress
!= block_mig_state
.prev_progress
) {
500 block_mig_state
.prev_progress
= progress
;
501 qemu_put_be64(f
, (progress
<< BDRV_SECTOR_BITS
)
502 | BLK_MIG_FLAG_PROGRESS
);
503 DPRINTF("Completed %d %%\r", progress
);
509 static void blk_mig_reset_dirty_cursor(void)
511 BlkMigDevState
*bmds
;
513 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
518 /* Called with iothread lock and AioContext taken. */
520 static int mig_save_device_dirty(QEMUFile
*f
, BlkMigDevState
*bmds
,
524 BlockDriverState
*bs
= blk_bs(bmds
->blk
);
525 int64_t total_sectors
= bmds
->total_sectors
;
530 for (sector
= bmds
->cur_dirty
; sector
< bmds
->total_sectors
;) {
532 if (bmds_aio_inflight(bmds
, sector
)) {
534 blk_drain(bmds
->blk
);
538 if (bdrv_get_dirty(bs
, bmds
->dirty_bitmap
, sector
)) {
540 if (total_sectors
- sector
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
541 nr_sectors
= total_sectors
- sector
;
543 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
545 blk
= g_new(BlkMigBlock
, 1);
546 blk
->buf
= g_malloc(BLOCK_SIZE
);
548 blk
->sector
= sector
;
549 blk
->nr_sectors
= nr_sectors
;
552 blk
->iov
.iov_base
= blk
->buf
;
553 blk
->iov
.iov_len
= nr_sectors
* BDRV_SECTOR_SIZE
;
554 qemu_iovec_init_external(&blk
->qiov
, &blk
->iov
, 1);
556 blk
->aiocb
= blk_aio_preadv(bmds
->blk
,
557 sector
* BDRV_SECTOR_SIZE
,
558 &blk
->qiov
, 0, blk_mig_read_cb
,
562 block_mig_state
.submitted
++;
563 bmds_set_aio_inflight(bmds
, sector
, nr_sectors
, 1);
566 ret
= blk_pread(bmds
->blk
, sector
* BDRV_SECTOR_SIZE
, blk
->buf
,
567 nr_sectors
* BDRV_SECTOR_SIZE
);
577 bdrv_reset_dirty_bitmap(bmds
->dirty_bitmap
, sector
, nr_sectors
);
578 sector
+= nr_sectors
;
579 bmds
->cur_dirty
= sector
;
583 sector
+= BDRV_SECTORS_PER_DIRTY_CHUNK
;
584 bmds
->cur_dirty
= sector
;
587 return (bmds
->cur_dirty
>= bmds
->total_sectors
);
590 DPRINTF("Error reading sector %" PRId64
"\n", sector
);
596 /* Called with iothread lock taken.
599 * 0: too much data for max_downtime
600 * 1: few enough data for max_downtime
602 static int blk_mig_save_dirty_block(QEMUFile
*f
, int is_async
)
604 BlkMigDevState
*bmds
;
607 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
608 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
609 ret
= mig_save_device_dirty(f
, bmds
, is_async
);
610 aio_context_release(blk_get_aio_context(bmds
->blk
));
619 /* Called with no locks taken. */
621 static int flush_blks(QEMUFile
*f
)
626 DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
627 __FUNCTION__
, block_mig_state
.submitted
, block_mig_state
.read_done
,
628 block_mig_state
.transferred
);
631 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
632 if (qemu_file_rate_limit(f
)) {
640 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
648 block_mig_state
.read_done
--;
649 block_mig_state
.transferred
++;
650 assert(block_mig_state
.read_done
>= 0);
654 DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__
,
655 block_mig_state
.submitted
, block_mig_state
.read_done
,
656 block_mig_state
.transferred
);
660 /* Called with iothread lock taken. */
662 static int64_t get_remaining_dirty(void)
664 BlkMigDevState
*bmds
;
667 QSIMPLEQ_FOREACH(bmds
, &block_mig_state
.bmds_list
, entry
) {
668 aio_context_acquire(blk_get_aio_context(bmds
->blk
));
669 dirty
+= bdrv_get_dirty_count(bmds
->dirty_bitmap
);
670 aio_context_release(blk_get_aio_context(bmds
->blk
));
673 return dirty
<< BDRV_SECTOR_BITS
;
676 /* Called with iothread lock taken. */
678 static void block_migration_cleanup(void *opaque
)
680 BlkMigDevState
*bmds
;
686 unset_dirty_tracking();
688 while ((bmds
= QSIMPLEQ_FIRST(&block_mig_state
.bmds_list
)) != NULL
) {
689 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.bmds_list
, entry
);
690 bdrv_op_unblock_all(blk_bs(bmds
->blk
), bmds
->blocker
);
691 error_free(bmds
->blocker
);
693 /* Save ctx, because bmds->blk can disappear during blk_unref. */
694 ctx
= blk_get_aio_context(bmds
->blk
);
695 aio_context_acquire(ctx
);
696 blk_unref(bmds
->blk
);
697 aio_context_release(ctx
);
699 g_free(bmds
->blk_name
);
700 g_free(bmds
->aio_bitmap
);
705 while ((blk
= QSIMPLEQ_FIRST(&block_mig_state
.blk_list
)) != NULL
) {
706 QSIMPLEQ_REMOVE_HEAD(&block_mig_state
.blk_list
, entry
);
713 static int block_save_setup(QEMUFile
*f
, void *opaque
)
717 DPRINTF("Enter save live setup submitted %d transferred %d\n",
718 block_mig_state
.submitted
, block_mig_state
.transferred
);
720 qemu_mutex_lock_iothread();
721 ret
= init_blk_migration(f
);
723 qemu_mutex_unlock_iothread();
727 /* start track dirty blocks */
728 ret
= set_dirty_tracking();
730 qemu_mutex_unlock_iothread();
737 blk_mig_reset_dirty_cursor();
738 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
743 static int block_save_iterate(QEMUFile
*f
, void *opaque
)
746 int64_t last_ftell
= qemu_ftell(f
);
749 DPRINTF("Enter save live iterate submitted %d transferred %d\n",
750 block_mig_state
.submitted
, block_mig_state
.transferred
);
757 blk_mig_reset_dirty_cursor();
759 /* control the rate of transfer */
761 while ((block_mig_state
.submitted
+
762 block_mig_state
.read_done
) * BLOCK_SIZE
<
763 qemu_file_get_rate_limit(f
) &&
764 (block_mig_state
.submitted
+
765 block_mig_state
.read_done
) <
768 if (block_mig_state
.bulk_completed
== 0) {
769 /* first finish the bulk phase */
770 if (blk_mig_save_bulked_block(f
) == 0) {
771 /* finished saving bulk on all devices */
772 block_mig_state
.bulk_completed
= 1;
776 /* Always called with iothread lock taken for
777 * simplicity, block_save_complete also calls it.
779 qemu_mutex_lock_iothread();
780 ret
= blk_mig_save_dirty_block(f
, 1);
781 qemu_mutex_unlock_iothread();
788 /* no more dirty blocks */
799 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
800 delta_ftell
= qemu_ftell(f
) - last_ftell
;
801 if (delta_ftell
> 0) {
803 } else if (delta_ftell
< 0) {
810 /* Called with iothread lock taken. */
812 static int block_save_complete(QEMUFile
*f
, void *opaque
)
816 DPRINTF("Enter save live complete submitted %d transferred %d\n",
817 block_mig_state
.submitted
, block_mig_state
.transferred
);
824 blk_mig_reset_dirty_cursor();
826 /* we know for sure that save bulk is completed and
827 all async read completed */
829 assert(block_mig_state
.submitted
== 0);
833 ret
= blk_mig_save_dirty_block(f
, 0);
839 /* report completion */
840 qemu_put_be64(f
, (100 << BDRV_SECTOR_BITS
) | BLK_MIG_FLAG_PROGRESS
);
842 DPRINTF("Block migration completed\n");
844 qemu_put_be64(f
, BLK_MIG_FLAG_EOS
);
849 static void block_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
,
850 uint64_t *non_postcopiable_pending
,
851 uint64_t *postcopiable_pending
)
853 /* Estimate pending number of bytes to send */
856 qemu_mutex_lock_iothread();
857 pending
= get_remaining_dirty();
858 qemu_mutex_unlock_iothread();
861 pending
+= block_mig_state
.submitted
* BLOCK_SIZE
+
862 block_mig_state
.read_done
* BLOCK_SIZE
;
865 /* Report at least one block pending during bulk phase */
866 if (pending
<= max_size
&& !block_mig_state
.bulk_completed
) {
867 pending
= max_size
+ BLOCK_SIZE
;
870 DPRINTF("Enter save live pending %" PRIu64
"\n", pending
);
871 /* We don't do postcopy */
872 *non_postcopiable_pending
+= pending
;
875 static int block_load(QEMUFile
*f
, void *opaque
, int version_id
)
877 static int banner_printed
;
879 char device_name
[256];
881 BlockBackend
*blk
, *blk_prev
= NULL
;;
882 Error
*local_err
= NULL
;
884 int64_t total_sectors
= 0;
888 int cluster_size
= BLOCK_SIZE
;
891 addr
= qemu_get_be64(f
);
893 flags
= addr
& ~BDRV_SECTOR_MASK
;
894 addr
>>= BDRV_SECTOR_BITS
;
896 if (flags
& BLK_MIG_FLAG_DEVICE_BLOCK
) {
897 /* get device name */
898 len
= qemu_get_byte(f
);
899 qemu_get_buffer(f
, (uint8_t *)device_name
, len
);
900 device_name
[len
] = '\0';
902 blk
= blk_by_name(device_name
);
904 fprintf(stderr
, "Error unknown block device %s\n",
909 if (blk
!= blk_prev
) {
911 total_sectors
= blk_nb_sectors(blk
);
912 if (total_sectors
<= 0) {
913 error_report("Error getting length of block device %s",
918 blk_invalidate_cache(blk
, &local_err
);
920 error_report_err(local_err
);
924 ret
= bdrv_get_info(blk_bs(blk
), &bdi
);
925 if (ret
== 0 && bdi
.cluster_size
> 0 &&
926 bdi
.cluster_size
<= BLOCK_SIZE
&&
927 BLOCK_SIZE
% bdi
.cluster_size
== 0) {
928 cluster_size
= bdi
.cluster_size
;
930 cluster_size
= BLOCK_SIZE
;
934 if (total_sectors
- addr
< BDRV_SECTORS_PER_DIRTY_CHUNK
) {
935 nr_sectors
= total_sectors
- addr
;
937 nr_sectors
= BDRV_SECTORS_PER_DIRTY_CHUNK
;
940 if (flags
& BLK_MIG_FLAG_ZERO_BLOCK
) {
941 ret
= blk_pwrite_zeroes(blk
, addr
* BDRV_SECTOR_SIZE
,
942 nr_sectors
* BDRV_SECTOR_SIZE
,
949 buf
= g_malloc(BLOCK_SIZE
);
950 qemu_get_buffer(f
, buf
, BLOCK_SIZE
);
951 for (i
= 0; i
< BLOCK_SIZE
/ cluster_size
; i
++) {
952 cur_addr
= addr
* BDRV_SECTOR_SIZE
+ i
* cluster_size
;
953 cur_buf
= buf
+ i
* cluster_size
;
955 if ((!block_mig_state
.zero_blocks
||
956 cluster_size
< BLOCK_SIZE
) &&
957 buffer_is_zero(cur_buf
, cluster_size
)) {
958 ret
= blk_pwrite_zeroes(blk
, cur_addr
,
962 ret
= blk_pwrite(blk
, cur_addr
, cur_buf
,
975 } else if (flags
& BLK_MIG_FLAG_PROGRESS
) {
976 if (!banner_printed
) {
977 printf("Receiving block device images\n");
980 printf("Completed %d %%%c", (int)addr
,
981 (addr
== 100) ? '\n' : '\r');
983 } else if (!(flags
& BLK_MIG_FLAG_EOS
)) {
984 fprintf(stderr
, "Unknown block migration flags: %#x\n", flags
);
987 ret
= qemu_file_get_error(f
);
991 } while (!(flags
& BLK_MIG_FLAG_EOS
));
996 static bool block_is_active(void *opaque
)
998 return migrate_use_block();
1001 static SaveVMHandlers savevm_block_handlers
= {
1002 .save_live_setup
= block_save_setup
,
1003 .save_live_iterate
= block_save_iterate
,
1004 .save_live_complete_precopy
= block_save_complete
,
1005 .save_live_pending
= block_save_pending
,
1006 .load_state
= block_load
,
1007 .cleanup
= block_migration_cleanup
,
1008 .is_active
= block_is_active
,
1011 void blk_mig_init(void)
1013 QSIMPLEQ_INIT(&block_mig_state
.bmds_list
);
1014 QSIMPLEQ_INIT(&block_mig_state
.blk_list
);
1015 qemu_mutex_init(&block_mig_state
.lock
);
1017 register_savevm_live(NULL
, "block", 0, 1, &savevm_block_handlers
,