2 * Block dirty bitmap postcopy migration
4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
8 * Liran Schour <lirans@il.ibm.com>
9 * Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 * This file is derived from migration/block.c, so it's author and IBM copyright
14 * are here, although content is quite different.
16 * Contributions after 2012-01-13 are licensed under the terms of the
17 * GNU GPL, version 2 or (at your option) any later version.
21 * Here postcopy migration of dirty bitmaps is realized. Only QMP-addressable
22 * bitmaps are migrated.
24 * Bitmap migration implies creating bitmap with the same name and granularity
25 * in destination QEMU. If the bitmap with the same name (for the same node)
26 * already exists on destination an error will be generated.
28 * format of migration:
30 * # Header (shared for different chunk types)
31 * 1, 2 or 4 bytes: flags (see qemu_{put,put}_flags)
32 * [ 1 byte: node name size ] \ flags & DEVICE_NAME
33 * [ n bytes: node name ] /
34 * [ 1 byte: bitmap name size ] \ flags & BITMAP_NAME
35 * [ n bytes: bitmap name ] /
37 * # Start of bitmap migration (flags & START)
40 * 1 byte: bitmap flags (corresponds to BdrvDirtyBitmap)
41 * bit 0 - bitmap is enabled
42 * bit 1 - bitmap is persistent
43 * bit 2 - bitmap is autoloading
44 * bits 3-7 - reserved, must be zero
46 * # Complete of bitmap migration (flags & COMPLETE)
49 * # Data chunk of bitmap migration
52 * be32: number of sectors
53 * [ be64: buffer size ] \ ! (flags & ZEROES)
54 * [ n bytes: buffer ] /
56 * The last chunk in stream should contain flags & EOS. The chunk may skip
57 * device and/or bitmap names, assuming them to be the same with the previous
61 #include "qemu/osdep.h"
62 #include "block/block.h"
63 #include "block/block_int.h"
64 #include "sysemu/block-backend.h"
65 #include "sysemu/runstate.h"
66 #include "qemu/main-loop.h"
67 #include "qemu/error-report.h"
68 #include "migration/misc.h"
69 #include "migration/migration.h"
70 #include "qemu-file.h"
71 #include "migration/vmstate.h"
72 #include "migration/register.h"
73 #include "qemu/hbitmap.h"
74 #include "qemu/cutils.h"
75 #include "qapi/error.h"
78 #define CHUNK_SIZE (1 << 10)
80 /* Flags occupy one, two or four bytes (Big Endian). The size is determined as
82 * in first (most significant) byte bit 8 is clear --> one byte
83 * in first byte bit 8 is set --> two or four bytes, depending on second
85 * | in second byte bit 8 is clear --> two bytes
86 * | in second byte bit 8 is set --> four bytes
88 #define DIRTY_BITMAP_MIG_FLAG_EOS 0x01
89 #define DIRTY_BITMAP_MIG_FLAG_ZEROES 0x02
90 #define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME 0x04
91 #define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME 0x08
92 #define DIRTY_BITMAP_MIG_FLAG_START 0x10
93 #define DIRTY_BITMAP_MIG_FLAG_COMPLETE 0x20
94 #define DIRTY_BITMAP_MIG_FLAG_BITS 0x40
96 #define DIRTY_BITMAP_MIG_EXTRA_FLAGS 0x80
98 #define DIRTY_BITMAP_MIG_START_FLAG_ENABLED 0x01
99 #define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT 0x02
100 /* 0x04 was "AUTOLOAD" flags on elder versions, no it is ignored */
101 #define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK 0xf8
103 typedef struct DirtyBitmapMigBitmapState
{
104 /* Written during setup phase. */
105 BlockDriverState
*bs
;
106 const char *node_name
;
107 BdrvDirtyBitmap
*bitmap
;
108 uint64_t total_sectors
;
109 uint64_t sectors_per_chunk
;
110 QSIMPLEQ_ENTRY(DirtyBitmapMigBitmapState
) entry
;
113 /* For bulk phase. */
116 } DirtyBitmapMigBitmapState
;
118 typedef struct DirtyBitmapMigState
{
119 QSIMPLEQ_HEAD(, DirtyBitmapMigBitmapState
) dbms_list
;
124 /* for send_bitmap_bits() */
125 BlockDriverState
*prev_bs
;
126 BdrvDirtyBitmap
*prev_bitmap
;
127 } DirtyBitmapMigState
;
129 typedef struct DirtyBitmapLoadState
{
132 char bitmap_name
[256];
133 BlockDriverState
*bs
;
134 BdrvDirtyBitmap
*bitmap
;
135 } DirtyBitmapLoadState
;
137 static DirtyBitmapMigState dirty_bitmap_mig_state
;
139 typedef struct DirtyBitmapLoadBitmapState
{
140 BlockDriverState
*bs
;
141 BdrvDirtyBitmap
*bitmap
;
143 } DirtyBitmapLoadBitmapState
;
144 static GSList
*enabled_bitmaps
;
145 QemuMutex finish_lock
;
147 void init_dirty_bitmap_incoming_migration(void)
149 qemu_mutex_init(&finish_lock
);
152 static uint32_t qemu_get_bitmap_flags(QEMUFile
*f
)
154 uint8_t flags
= qemu_get_byte(f
);
155 if (flags
& DIRTY_BITMAP_MIG_EXTRA_FLAGS
) {
156 flags
= flags
<< 8 | qemu_get_byte(f
);
157 if (flags
& DIRTY_BITMAP_MIG_EXTRA_FLAGS
) {
158 flags
= flags
<< 16 | qemu_get_be16(f
);
165 static void qemu_put_bitmap_flags(QEMUFile
*f
, uint32_t flags
)
167 /* The code currently do not send flags more than one byte */
168 assert(!(flags
& (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS
)));
170 qemu_put_byte(f
, flags
);
173 static void send_bitmap_header(QEMUFile
*f
, DirtyBitmapMigBitmapState
*dbms
,
174 uint32_t additional_flags
)
176 BlockDriverState
*bs
= dbms
->bs
;
177 BdrvDirtyBitmap
*bitmap
= dbms
->bitmap
;
178 uint32_t flags
= additional_flags
;
179 trace_send_bitmap_header_enter();
181 if (bs
!= dirty_bitmap_mig_state
.prev_bs
) {
182 dirty_bitmap_mig_state
.prev_bs
= bs
;
183 flags
|= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME
;
186 if (bitmap
!= dirty_bitmap_mig_state
.prev_bitmap
) {
187 dirty_bitmap_mig_state
.prev_bitmap
= bitmap
;
188 flags
|= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME
;
191 qemu_put_bitmap_flags(f
, flags
);
193 if (flags
& DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME
) {
194 qemu_put_counted_string(f
, dbms
->node_name
);
197 if (flags
& DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME
) {
198 qemu_put_counted_string(f
, bdrv_dirty_bitmap_name(bitmap
));
202 static void send_bitmap_start(QEMUFile
*f
, DirtyBitmapMigBitmapState
*dbms
)
204 send_bitmap_header(f
, dbms
, DIRTY_BITMAP_MIG_FLAG_START
);
205 qemu_put_be32(f
, bdrv_dirty_bitmap_granularity(dbms
->bitmap
));
206 qemu_put_byte(f
, dbms
->flags
);
209 static void send_bitmap_complete(QEMUFile
*f
, DirtyBitmapMigBitmapState
*dbms
)
211 send_bitmap_header(f
, dbms
, DIRTY_BITMAP_MIG_FLAG_COMPLETE
);
214 static void send_bitmap_bits(QEMUFile
*f
, DirtyBitmapMigBitmapState
*dbms
,
215 uint64_t start_sector
, uint32_t nr_sectors
)
217 /* align for buffer_is_zero() */
218 uint64_t align
= 4 * sizeof(long);
219 uint64_t unaligned_size
=
220 bdrv_dirty_bitmap_serialization_size(
221 dbms
->bitmap
, start_sector
<< BDRV_SECTOR_BITS
,
222 (uint64_t)nr_sectors
<< BDRV_SECTOR_BITS
);
223 uint64_t buf_size
= QEMU_ALIGN_UP(unaligned_size
, align
);
224 uint8_t *buf
= g_malloc0(buf_size
);
225 uint32_t flags
= DIRTY_BITMAP_MIG_FLAG_BITS
;
227 bdrv_dirty_bitmap_serialize_part(
228 dbms
->bitmap
, buf
, start_sector
<< BDRV_SECTOR_BITS
,
229 (uint64_t)nr_sectors
<< BDRV_SECTOR_BITS
);
231 if (buffer_is_zero(buf
, buf_size
)) {
234 flags
|= DIRTY_BITMAP_MIG_FLAG_ZEROES
;
237 trace_send_bitmap_bits(flags
, start_sector
, nr_sectors
, buf_size
);
239 send_bitmap_header(f
, dbms
, flags
);
241 qemu_put_be64(f
, start_sector
);
242 qemu_put_be32(f
, nr_sectors
);
244 /* if a block is zero we need to flush here since the network
245 * bandwidth is now a lot higher than the storage device bandwidth.
246 * thus if we queue zero blocks we slow down the migration. */
247 if (flags
& DIRTY_BITMAP_MIG_FLAG_ZEROES
) {
250 qemu_put_be64(f
, buf_size
);
251 qemu_put_buffer(f
, buf
, buf_size
);
257 /* Called with iothread lock taken. */
258 static void dirty_bitmap_mig_cleanup(void)
260 DirtyBitmapMigBitmapState
*dbms
;
262 while ((dbms
= QSIMPLEQ_FIRST(&dirty_bitmap_mig_state
.dbms_list
)) != NULL
) {
263 QSIMPLEQ_REMOVE_HEAD(&dirty_bitmap_mig_state
.dbms_list
, entry
);
264 bdrv_dirty_bitmap_set_busy(dbms
->bitmap
, false);
265 bdrv_unref(dbms
->bs
);
270 /* Called with iothread lock taken. */
271 static int add_bitmaps_to_list(BlockDriverState
*bs
, const char *bs_name
)
273 BdrvDirtyBitmap
*bitmap
;
274 DirtyBitmapMigBitmapState
*dbms
;
275 Error
*local_err
= NULL
;
277 bitmap
= bdrv_dirty_bitmap_first(bs
);
282 if (!bs_name
|| strcmp(bs_name
, "") == 0) {
283 error_report("Bitmap '%s' in unnamed node can't be migrated",
284 bdrv_dirty_bitmap_name(bitmap
));
288 if (bs_name
[0] == '#') {
289 error_report("Bitmap '%s' in a node with auto-generated "
290 "name '%s' can't be migrated",
291 bdrv_dirty_bitmap_name(bitmap
), bs_name
);
295 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
296 if (!bdrv_dirty_bitmap_name(bitmap
)) {
300 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_DEFAULT
, &local_err
)) {
301 error_report_err(local_err
);
306 bdrv_dirty_bitmap_set_busy(bitmap
, true);
308 dbms
= g_new0(DirtyBitmapMigBitmapState
, 1);
310 dbms
->node_name
= bs_name
;
311 dbms
->bitmap
= bitmap
;
312 dbms
->total_sectors
= bdrv_nb_sectors(bs
);
313 dbms
->sectors_per_chunk
= CHUNK_SIZE
* 8 *
314 bdrv_dirty_bitmap_granularity(bitmap
) >> BDRV_SECTOR_BITS
;
315 if (bdrv_dirty_bitmap_enabled(bitmap
)) {
316 dbms
->flags
|= DIRTY_BITMAP_MIG_START_FLAG_ENABLED
;
318 if (bdrv_dirty_bitmap_get_persistence(bitmap
)) {
319 dbms
->flags
|= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT
;
322 QSIMPLEQ_INSERT_TAIL(&dirty_bitmap_mig_state
.dbms_list
,
329 /* Called with iothread lock taken. */
330 static int init_dirty_bitmap_migration(void)
332 BlockDriverState
*bs
;
333 DirtyBitmapMigBitmapState
*dbms
;
334 GHashTable
*handled_by_blk
= g_hash_table_new(NULL
, NULL
);
337 dirty_bitmap_mig_state
.bulk_completed
= false;
338 dirty_bitmap_mig_state
.prev_bs
= NULL
;
339 dirty_bitmap_mig_state
.prev_bitmap
= NULL
;
340 dirty_bitmap_mig_state
.no_bitmaps
= false;
343 * Use blockdevice name for direct (or filtered) children of named block
346 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
347 const char *name
= blk_name(blk
);
349 if (!name
|| strcmp(name
, "") == 0) {
355 /* Skip filters without bitmaps */
356 while (bs
&& bs
->drv
&& bs
->drv
->is_filter
&&
357 !bdrv_has_named_bitmaps(bs
))
360 bs
= bs
->backing
->bs
;
361 } else if (bs
->file
) {
368 if (bs
&& bs
->drv
&& !bs
->drv
->is_filter
) {
369 if (add_bitmaps_to_list(bs
, name
)) {
372 g_hash_table_add(handled_by_blk
, bs
);
376 for (bs
= bdrv_next_all_states(NULL
); bs
; bs
= bdrv_next_all_states(bs
)) {
377 if (g_hash_table_contains(handled_by_blk
, bs
)) {
381 if (add_bitmaps_to_list(bs
, bdrv_get_node_name(bs
))) {
386 /* unset migration flags here, to not roll back it */
387 QSIMPLEQ_FOREACH(dbms
, &dirty_bitmap_mig_state
.dbms_list
, entry
) {
388 bdrv_dirty_bitmap_skip_store(dbms
->bitmap
, true);
391 if (QSIMPLEQ_EMPTY(&dirty_bitmap_mig_state
.dbms_list
)) {
392 dirty_bitmap_mig_state
.no_bitmaps
= true;
395 g_hash_table_destroy(handled_by_blk
);
400 g_hash_table_destroy(handled_by_blk
);
401 dirty_bitmap_mig_cleanup();
406 /* Called with no lock taken. */
407 static void bulk_phase_send_chunk(QEMUFile
*f
, DirtyBitmapMigBitmapState
*dbms
)
409 uint32_t nr_sectors
= MIN(dbms
->total_sectors
- dbms
->cur_sector
,
410 dbms
->sectors_per_chunk
);
412 send_bitmap_bits(f
, dbms
, dbms
->cur_sector
, nr_sectors
);
414 dbms
->cur_sector
+= nr_sectors
;
415 if (dbms
->cur_sector
>= dbms
->total_sectors
) {
416 dbms
->bulk_completed
= true;
420 /* Called with no lock taken. */
421 static void bulk_phase(QEMUFile
*f
, bool limit
)
423 DirtyBitmapMigBitmapState
*dbms
;
425 QSIMPLEQ_FOREACH(dbms
, &dirty_bitmap_mig_state
.dbms_list
, entry
) {
426 while (!dbms
->bulk_completed
) {
427 bulk_phase_send_chunk(f
, dbms
);
428 if (limit
&& qemu_file_rate_limit(f
)) {
434 dirty_bitmap_mig_state
.bulk_completed
= true;
437 /* for SaveVMHandlers */
438 static void dirty_bitmap_save_cleanup(void *opaque
)
440 dirty_bitmap_mig_cleanup();
443 static int dirty_bitmap_save_iterate(QEMUFile
*f
, void *opaque
)
445 trace_dirty_bitmap_save_iterate(migration_in_postcopy());
447 if (migration_in_postcopy() && !dirty_bitmap_mig_state
.bulk_completed
) {
451 qemu_put_bitmap_flags(f
, DIRTY_BITMAP_MIG_FLAG_EOS
);
453 return dirty_bitmap_mig_state
.bulk_completed
;
456 /* Called with iothread lock taken. */
458 static int dirty_bitmap_save_complete(QEMUFile
*f
, void *opaque
)
460 DirtyBitmapMigBitmapState
*dbms
;
461 trace_dirty_bitmap_save_complete_enter();
463 if (!dirty_bitmap_mig_state
.bulk_completed
) {
464 bulk_phase(f
, false);
467 QSIMPLEQ_FOREACH(dbms
, &dirty_bitmap_mig_state
.dbms_list
, entry
) {
468 send_bitmap_complete(f
, dbms
);
471 qemu_put_bitmap_flags(f
, DIRTY_BITMAP_MIG_FLAG_EOS
);
473 trace_dirty_bitmap_save_complete_finish();
475 dirty_bitmap_mig_cleanup();
479 static void dirty_bitmap_save_pending(QEMUFile
*f
, void *opaque
,
481 uint64_t *res_precopy_only
,
482 uint64_t *res_compatible
,
483 uint64_t *res_postcopy_only
)
485 DirtyBitmapMigBitmapState
*dbms
;
486 uint64_t pending
= 0;
488 qemu_mutex_lock_iothread();
490 QSIMPLEQ_FOREACH(dbms
, &dirty_bitmap_mig_state
.dbms_list
, entry
) {
491 uint64_t gran
= bdrv_dirty_bitmap_granularity(dbms
->bitmap
);
492 uint64_t sectors
= dbms
->bulk_completed
? 0 :
493 dbms
->total_sectors
- dbms
->cur_sector
;
495 pending
+= DIV_ROUND_UP(sectors
* BDRV_SECTOR_SIZE
, gran
);
498 qemu_mutex_unlock_iothread();
500 trace_dirty_bitmap_save_pending(pending
, max_size
);
502 *res_postcopy_only
+= pending
;
505 /* First occurrence of this bitmap. It should be created if doesn't exist */
506 static int dirty_bitmap_load_start(QEMUFile
*f
, DirtyBitmapLoadState
*s
)
508 Error
*local_err
= NULL
;
509 uint32_t granularity
= qemu_get_be32(f
);
510 uint8_t flags
= qemu_get_byte(f
);
513 error_report("Bitmap with the same name ('%s') already exists on "
514 "destination", bdrv_dirty_bitmap_name(s
->bitmap
));
517 s
->bitmap
= bdrv_create_dirty_bitmap(s
->bs
, granularity
,
518 s
->bitmap_name
, &local_err
);
520 error_report_err(local_err
);
525 if (flags
& DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK
) {
526 error_report("Unknown flags in migrated dirty bitmap header: %x",
531 if (flags
& DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT
) {
532 bdrv_dirty_bitmap_set_persistence(s
->bitmap
, true);
535 bdrv_disable_dirty_bitmap(s
->bitmap
);
536 if (flags
& DIRTY_BITMAP_MIG_START_FLAG_ENABLED
) {
537 DirtyBitmapLoadBitmapState
*b
;
539 bdrv_dirty_bitmap_create_successor(s
->bitmap
, &local_err
);
541 error_report_err(local_err
);
545 b
= g_new(DirtyBitmapLoadBitmapState
, 1);
547 b
->bitmap
= s
->bitmap
;
549 enabled_bitmaps
= g_slist_prepend(enabled_bitmaps
, b
);
555 void dirty_bitmap_mig_before_vm_start(void)
559 qemu_mutex_lock(&finish_lock
);
561 for (item
= enabled_bitmaps
; item
; item
= g_slist_next(item
)) {
562 DirtyBitmapLoadBitmapState
*b
= item
->data
;
565 bdrv_enable_dirty_bitmap_locked(b
->bitmap
);
567 bdrv_dirty_bitmap_enable_successor(b
->bitmap
);
573 g_slist_free(enabled_bitmaps
);
574 enabled_bitmaps
= NULL
;
576 qemu_mutex_unlock(&finish_lock
);
579 static void dirty_bitmap_load_complete(QEMUFile
*f
, DirtyBitmapLoadState
*s
)
582 trace_dirty_bitmap_load_complete();
583 bdrv_dirty_bitmap_deserialize_finish(s
->bitmap
);
585 qemu_mutex_lock(&finish_lock
);
587 for (item
= enabled_bitmaps
; item
; item
= g_slist_next(item
)) {
588 DirtyBitmapLoadBitmapState
*b
= item
->data
;
590 if (b
->bitmap
== s
->bitmap
) {
596 if (bdrv_dirty_bitmap_has_successor(s
->bitmap
)) {
597 bdrv_dirty_bitmap_lock(s
->bitmap
);
598 if (enabled_bitmaps
== NULL
) {
600 bdrv_reclaim_dirty_bitmap_locked(s
->bitmap
, &error_abort
);
601 bdrv_enable_dirty_bitmap_locked(s
->bitmap
);
603 /* target not started, successor must be empty */
604 int64_t count
= bdrv_get_dirty_count(s
->bitmap
);
605 BdrvDirtyBitmap
*ret
= bdrv_reclaim_dirty_bitmap_locked(s
->bitmap
,
607 /* bdrv_reclaim_dirty_bitmap can fail only on no successor (it
608 * must be) or on merge fail, but merge can't fail when second
611 assert(ret
== s
->bitmap
&&
612 count
== bdrv_get_dirty_count(s
->bitmap
));
614 bdrv_dirty_bitmap_unlock(s
->bitmap
);
617 qemu_mutex_unlock(&finish_lock
);
620 static int dirty_bitmap_load_bits(QEMUFile
*f
, DirtyBitmapLoadState
*s
)
622 uint64_t first_byte
= qemu_get_be64(f
) << BDRV_SECTOR_BITS
;
623 uint64_t nr_bytes
= (uint64_t)qemu_get_be32(f
) << BDRV_SECTOR_BITS
;
624 trace_dirty_bitmap_load_bits_enter(first_byte
>> BDRV_SECTOR_BITS
,
625 nr_bytes
>> BDRV_SECTOR_BITS
);
627 if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_ZEROES
) {
628 trace_dirty_bitmap_load_bits_zeroes();
629 bdrv_dirty_bitmap_deserialize_zeroes(s
->bitmap
, first_byte
, nr_bytes
,
634 uint64_t buf_size
= qemu_get_be64(f
);
635 uint64_t needed_size
=
636 bdrv_dirty_bitmap_serialization_size(s
->bitmap
,
637 first_byte
, nr_bytes
);
639 if (needed_size
> buf_size
||
640 buf_size
> QEMU_ALIGN_UP(needed_size
, 4 * sizeof(long))
641 /* Here used same alignment as in send_bitmap_bits */
643 error_report("Migrated bitmap granularity doesn't "
644 "match the destination bitmap '%s' granularity",
645 bdrv_dirty_bitmap_name(s
->bitmap
));
649 buf
= g_malloc(buf_size
);
650 ret
= qemu_get_buffer(f
, buf
, buf_size
);
651 if (ret
!= buf_size
) {
652 error_report("Failed to read bitmap bits");
657 bdrv_dirty_bitmap_deserialize_part(s
->bitmap
, buf
, first_byte
, nr_bytes
,
665 static int dirty_bitmap_load_header(QEMUFile
*f
, DirtyBitmapLoadState
*s
)
667 Error
*local_err
= NULL
;
669 s
->flags
= qemu_get_bitmap_flags(f
);
670 trace_dirty_bitmap_load_header(s
->flags
);
672 nothing
= s
->flags
== (s
->flags
& DIRTY_BITMAP_MIG_FLAG_EOS
);
674 if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME
) {
675 if (!qemu_get_counted_string(f
, s
->node_name
)) {
676 error_report("Unable to read node name string");
679 s
->bs
= bdrv_lookup_bs(s
->node_name
, s
->node_name
, &local_err
);
681 error_report_err(local_err
);
684 } else if (!s
->bs
&& !nothing
) {
685 error_report("Error: block device name is not set");
689 if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME
) {
690 if (!qemu_get_counted_string(f
, s
->bitmap_name
)) {
691 error_report("Unable to read bitmap name string");
694 s
->bitmap
= bdrv_find_dirty_bitmap(s
->bs
, s
->bitmap_name
);
696 /* bitmap may be NULL here, it wouldn't be an error if it is the
697 * first occurrence of the bitmap */
698 if (!s
->bitmap
&& !(s
->flags
& DIRTY_BITMAP_MIG_FLAG_START
)) {
699 error_report("Error: unknown dirty bitmap "
700 "'%s' for block device '%s'",
701 s
->bitmap_name
, s
->node_name
);
704 } else if (!s
->bitmap
&& !nothing
) {
705 error_report("Error: block device name is not set");
712 static int dirty_bitmap_load(QEMUFile
*f
, void *opaque
, int version_id
)
714 static DirtyBitmapLoadState s
;
717 trace_dirty_bitmap_load_enter();
719 if (version_id
!= 1) {
724 ret
= dirty_bitmap_load_header(f
, &s
);
729 if (s
.flags
& DIRTY_BITMAP_MIG_FLAG_START
) {
730 ret
= dirty_bitmap_load_start(f
, &s
);
731 } else if (s
.flags
& DIRTY_BITMAP_MIG_FLAG_COMPLETE
) {
732 dirty_bitmap_load_complete(f
, &s
);
733 } else if (s
.flags
& DIRTY_BITMAP_MIG_FLAG_BITS
) {
734 ret
= dirty_bitmap_load_bits(f
, &s
);
738 ret
= qemu_file_get_error(f
);
744 } while (!(s
.flags
& DIRTY_BITMAP_MIG_FLAG_EOS
));
746 trace_dirty_bitmap_load_success();
750 static int dirty_bitmap_save_setup(QEMUFile
*f
, void *opaque
)
752 DirtyBitmapMigBitmapState
*dbms
= NULL
;
753 if (init_dirty_bitmap_migration() < 0) {
757 QSIMPLEQ_FOREACH(dbms
, &dirty_bitmap_mig_state
.dbms_list
, entry
) {
758 send_bitmap_start(f
, dbms
);
760 qemu_put_bitmap_flags(f
, DIRTY_BITMAP_MIG_FLAG_EOS
);
765 static bool dirty_bitmap_is_active(void *opaque
)
767 return migrate_dirty_bitmaps() && !dirty_bitmap_mig_state
.no_bitmaps
;
770 static bool dirty_bitmap_is_active_iterate(void *opaque
)
772 return dirty_bitmap_is_active(opaque
) && !runstate_is_running();
775 static bool dirty_bitmap_has_postcopy(void *opaque
)
780 static SaveVMHandlers savevm_dirty_bitmap_handlers
= {
781 .save_setup
= dirty_bitmap_save_setup
,
782 .save_live_complete_postcopy
= dirty_bitmap_save_complete
,
783 .save_live_complete_precopy
= dirty_bitmap_save_complete
,
784 .has_postcopy
= dirty_bitmap_has_postcopy
,
785 .save_live_pending
= dirty_bitmap_save_pending
,
786 .save_live_iterate
= dirty_bitmap_save_iterate
,
787 .is_active_iterate
= dirty_bitmap_is_active_iterate
,
788 .load_state
= dirty_bitmap_load
,
789 .save_cleanup
= dirty_bitmap_save_cleanup
,
790 .is_active
= dirty_bitmap_is_active
,
793 void dirty_bitmap_mig_init(void)
795 QSIMPLEQ_INIT(&dirty_bitmap_mig_state
.dbms_list
);
797 register_savevm_live("dirty-bitmap", 0, 1,
798 &savevm_dirty_bitmap_handlers
,
799 &dirty_bitmap_mig_state
);