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 alias size ] \ flags & DEVICE_NAME
33 * [ n bytes: node alias ] /
34 * [ 1 byte: bitmap alias size ] \ flags & BITMAP_NAME
35 * [ n bytes: bitmap alias ] /
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 "block/dirty-bitmap.h"
65 #include "sysemu/block-backend.h"
66 #include "sysemu/runstate.h"
67 #include "qemu/main-loop.h"
68 #include "qemu/error-report.h"
69 #include "migration/misc.h"
70 #include "migration/migration.h"
71 #include "qemu-file.h"
72 #include "migration/vmstate.h"
73 #include "migration/register.h"
74 #include "qemu/hbitmap.h"
75 #include "qemu/cutils.h"
77 #include "qapi/error.h"
78 #include "qapi/qapi-commands-migration.h"
79 #include "qapi/qapi-visit-migration.h"
80 #include "qapi/clone-visitor.h"
84 #define CHUNK_SIZE (1 << 10)
86 /* Flags occupy one, two or four bytes (Big Endian). The size is determined as
88 * in first (most significant) byte bit 8 is clear --> one byte
89 * in first byte bit 8 is set --> two or four bytes, depending on second
91 * | in second byte bit 8 is clear --> two bytes
92 * | in second byte bit 8 is set --> four bytes
94 #define DIRTY_BITMAP_MIG_FLAG_EOS 0x01
95 #define DIRTY_BITMAP_MIG_FLAG_ZEROES 0x02
96 #define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME 0x04
97 #define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME 0x08
98 #define DIRTY_BITMAP_MIG_FLAG_START 0x10
99 #define DIRTY_BITMAP_MIG_FLAG_COMPLETE 0x20
100 #define DIRTY_BITMAP_MIG_FLAG_BITS 0x40
102 #define DIRTY_BITMAP_MIG_EXTRA_FLAGS 0x80
104 #define DIRTY_BITMAP_MIG_START_FLAG_ENABLED 0x01
105 #define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT 0x02
106 /* 0x04 was "AUTOLOAD" flags on older versions, now it is ignored */
107 #define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK 0xf8
109 /* State of one bitmap during save process */
110 typedef struct SaveBitmapState
{
111 /* Written during setup phase. */
112 BlockDriverState
*bs
;
115 BdrvDirtyBitmap
*bitmap
;
116 uint64_t total_sectors
;
117 uint64_t sectors_per_chunk
;
118 QSIMPLEQ_ENTRY(SaveBitmapState
) entry
;
121 /* For bulk phase. */
126 /* State of the dirty bitmap migration (DBM) during save process */
127 typedef struct DBMSaveState
{
128 QSIMPLEQ_HEAD(, SaveBitmapState
) dbms_list
;
133 /* for send_bitmap_bits() */
134 BlockDriverState
*prev_bs
;
135 BdrvDirtyBitmap
*prev_bitmap
;
138 typedef struct LoadBitmapState
{
139 BlockDriverState
*bs
;
140 BdrvDirtyBitmap
*bitmap
;
145 /* State of the dirty bitmap migration (DBM) during load process */
146 typedef struct DBMLoadState
{
148 char node_alias
[256];
149 char bitmap_alias
[256];
150 char bitmap_name
[BDRV_BITMAP_MAX_NAME_SIZE
+ 1];
151 BlockDriverState
*bs
;
152 BdrvDirtyBitmap
*bitmap
;
154 bool before_vm_start_handled
; /* set in dirty_bitmap_mig_before_vm_start */
155 BitmapMigrationBitmapAlias
*bmap_inner
;
159 * Incoming migration is cancelled for some reason. That means that we
160 * still should read our chunks from migration stream, to not affect other
161 * migration objects (like RAM), but just ignore them and do not touch any
167 QemuMutex lock
; /* protect bitmaps */
170 typedef struct DBMState
{
175 static DBMState dbm_state
;
177 /* For hash tables that map node/bitmap names to aliases */
178 typedef struct AliasMapInnerNode
{
183 static void free_alias_map_inner_node(void *amin_ptr
)
185 AliasMapInnerNode
*amin
= amin_ptr
;
187 g_free(amin
->string
);
188 g_hash_table_unref(amin
->subtree
);
193 * Construct an alias map based on the given QMP structure.
195 * (Note that we cannot store such maps in the MigrationParameters
196 * object, because that struct is defined by the QAPI schema, which
197 * makes it basically impossible to have dicts with arbitrary keys.
198 * Therefore, we instead have to construct these maps when migration
201 * @bbm is the block_bitmap_mapping from the migration parameters.
203 * If @name_to_alias is true, the returned hash table will map node
204 * and bitmap names to their respective aliases (for outgoing
207 * If @name_to_alias is false, the returned hash table will map node
208 * and bitmap aliases to their respective names (for incoming
211 * The hash table maps node names/aliases to AliasMapInnerNode
212 * objects, whose .string is the respective node alias/name, and whose
213 * .subtree table maps bitmap names/aliases to the respective bitmap
216 static GHashTable
*construct_alias_map(const BitmapMigrationNodeAliasList
*bbm
,
220 GHashTable
*alias_map
;
221 size_t max_node_name_len
= sizeof_field(BlockDriverState
, node_name
) - 1;
223 alias_map
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
224 g_free
, free_alias_map_inner_node
);
226 for (; bbm
; bbm
= bbm
->next
) {
227 const BitmapMigrationNodeAlias
*bmna
= bbm
->value
;
228 const BitmapMigrationBitmapAliasList
*bmbal
;
229 AliasMapInnerNode
*amin
;
230 GHashTable
*bitmaps_map
;
231 const char *node_map_from
, *node_map_to
;
234 if (!id_wellformed(bmna
->alias
)) {
235 error_setg(errp
, "The node alias '%s' is not well-formed",
240 if (strlen(bmna
->alias
) > UINT8_MAX
) {
241 error_setg(errp
, "The node alias '%s' is longer than %u bytes",
242 bmna
->alias
, UINT8_MAX
);
246 if (strlen(bmna
->node_name
) > max_node_name_len
) {
247 error_setg(errp
, "The node name '%s' is longer than %zu bytes",
248 bmna
->node_name
, max_node_name_len
);
253 if (g_hash_table_contains(alias_map
, bmna
->node_name
)) {
254 error_setg(errp
, "The node name '%s' is mapped twice",
259 node_map_from
= bmna
->node_name
;
260 node_map_to
= bmna
->alias
;
262 if (g_hash_table_contains(alias_map
, bmna
->alias
)) {
263 error_setg(errp
, "The node alias '%s' is used twice",
268 node_map_from
= bmna
->alias
;
269 node_map_to
= bmna
->node_name
;
272 gdn
= (GDestroyNotify
) qapi_free_BitmapMigrationBitmapAlias
;
273 bitmaps_map
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
,
276 amin
= g_new(AliasMapInnerNode
, 1);
277 *amin
= (AliasMapInnerNode
){
278 .string
= g_strdup(node_map_to
),
279 .subtree
= bitmaps_map
,
282 g_hash_table_insert(alias_map
, g_strdup(node_map_from
), amin
);
284 for (bmbal
= bmna
->bitmaps
; bmbal
; bmbal
= bmbal
->next
) {
285 const BitmapMigrationBitmapAlias
*bmba
= bmbal
->value
;
286 const char *bmap_map_from
;
288 if (strlen(bmba
->alias
) > UINT8_MAX
) {
290 "The bitmap alias '%s' is longer than %u bytes",
291 bmba
->alias
, UINT8_MAX
);
295 if (strlen(bmba
->name
) > BDRV_BITMAP_MAX_NAME_SIZE
) {
296 error_setg(errp
, "The bitmap name '%s' is longer than %d bytes",
297 bmba
->name
, BDRV_BITMAP_MAX_NAME_SIZE
);
302 bmap_map_from
= bmba
->name
;
304 if (g_hash_table_contains(bitmaps_map
, bmba
->name
)) {
305 error_setg(errp
, "The bitmap '%s'/'%s' is mapped twice",
306 bmna
->node_name
, bmba
->name
);
310 bmap_map_from
= bmba
->alias
;
312 if (g_hash_table_contains(bitmaps_map
, bmba
->alias
)) {
313 error_setg(errp
, "The bitmap alias '%s'/'%s' is used twice",
314 bmna
->alias
, bmba
->alias
);
319 g_hash_table_insert(bitmaps_map
, g_strdup(bmap_map_from
),
320 QAPI_CLONE(BitmapMigrationBitmapAlias
, bmba
));
327 g_hash_table_destroy(alias_map
);
332 * Run construct_alias_map() in both directions to check whether @bbm
334 * (This function is to be used by migration/migration.c to validate
335 * the user-specified block-bitmap-mapping migration parameter.)
337 * Returns true if and only if the mapping is valid.
339 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList
*bbm
,
342 GHashTable
*alias_map
;
344 alias_map
= construct_alias_map(bbm
, true, errp
);
348 g_hash_table_destroy(alias_map
);
350 alias_map
= construct_alias_map(bbm
, false, errp
);
354 g_hash_table_destroy(alias_map
);
359 static uint32_t qemu_get_bitmap_flags(QEMUFile
*f
)
361 uint8_t flags
= qemu_get_byte(f
);
362 if (flags
& DIRTY_BITMAP_MIG_EXTRA_FLAGS
) {
363 flags
= flags
<< 8 | qemu_get_byte(f
);
364 if (flags
& DIRTY_BITMAP_MIG_EXTRA_FLAGS
) {
365 flags
= flags
<< 16 | qemu_get_be16(f
);
372 static void qemu_put_bitmap_flags(QEMUFile
*f
, uint32_t flags
)
374 /* The code currently does not send flags as more than one byte */
375 assert(!(flags
& (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS
)));
377 qemu_put_byte(f
, flags
);
380 static void send_bitmap_header(QEMUFile
*f
, DBMSaveState
*s
,
381 SaveBitmapState
*dbms
, uint32_t additional_flags
)
383 BlockDriverState
*bs
= dbms
->bs
;
384 BdrvDirtyBitmap
*bitmap
= dbms
->bitmap
;
385 uint32_t flags
= additional_flags
;
386 trace_send_bitmap_header_enter();
388 if (bs
!= s
->prev_bs
) {
390 flags
|= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME
;
393 if (bitmap
!= s
->prev_bitmap
) {
394 s
->prev_bitmap
= bitmap
;
395 flags
|= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME
;
398 qemu_put_bitmap_flags(f
, flags
);
400 if (flags
& DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME
) {
401 qemu_put_counted_string(f
, dbms
->node_alias
);
404 if (flags
& DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME
) {
405 qemu_put_counted_string(f
, dbms
->bitmap_alias
);
409 static void send_bitmap_start(QEMUFile
*f
, DBMSaveState
*s
,
410 SaveBitmapState
*dbms
)
412 send_bitmap_header(f
, s
, dbms
, DIRTY_BITMAP_MIG_FLAG_START
);
413 qemu_put_be32(f
, bdrv_dirty_bitmap_granularity(dbms
->bitmap
));
414 qemu_put_byte(f
, dbms
->flags
);
417 static void send_bitmap_complete(QEMUFile
*f
, DBMSaveState
*s
,
418 SaveBitmapState
*dbms
)
420 send_bitmap_header(f
, s
, dbms
, DIRTY_BITMAP_MIG_FLAG_COMPLETE
);
423 static void send_bitmap_bits(QEMUFile
*f
, DBMSaveState
*s
,
424 SaveBitmapState
*dbms
,
425 uint64_t start_sector
, uint32_t nr_sectors
)
427 /* align for buffer_is_zero() */
428 uint64_t align
= 4 * sizeof(long);
429 uint64_t unaligned_size
=
430 bdrv_dirty_bitmap_serialization_size(
431 dbms
->bitmap
, start_sector
<< BDRV_SECTOR_BITS
,
432 (uint64_t)nr_sectors
<< BDRV_SECTOR_BITS
);
433 uint64_t buf_size
= QEMU_ALIGN_UP(unaligned_size
, align
);
434 uint8_t *buf
= g_malloc0(buf_size
);
435 uint32_t flags
= DIRTY_BITMAP_MIG_FLAG_BITS
;
437 bdrv_dirty_bitmap_serialize_part(
438 dbms
->bitmap
, buf
, start_sector
<< BDRV_SECTOR_BITS
,
439 (uint64_t)nr_sectors
<< BDRV_SECTOR_BITS
);
441 if (buffer_is_zero(buf
, buf_size
)) {
444 flags
|= DIRTY_BITMAP_MIG_FLAG_ZEROES
;
447 trace_send_bitmap_bits(flags
, start_sector
, nr_sectors
, buf_size
);
449 send_bitmap_header(f
, s
, dbms
, flags
);
451 qemu_put_be64(f
, start_sector
);
452 qemu_put_be32(f
, nr_sectors
);
454 /* if a block is zero we need to flush here since the network
455 * bandwidth is now a lot higher than the storage device bandwidth.
456 * thus if we queue zero blocks we slow down the migration. */
457 if (flags
& DIRTY_BITMAP_MIG_FLAG_ZEROES
) {
460 qemu_put_be64(f
, buf_size
);
461 qemu_put_buffer(f
, buf
, buf_size
);
467 /* Called with iothread lock taken. */
468 static void dirty_bitmap_do_save_cleanup(DBMSaveState
*s
)
470 SaveBitmapState
*dbms
;
472 while ((dbms
= QSIMPLEQ_FIRST(&s
->dbms_list
)) != NULL
) {
473 QSIMPLEQ_REMOVE_HEAD(&s
->dbms_list
, entry
);
474 bdrv_dirty_bitmap_set_busy(dbms
->bitmap
, false);
475 bdrv_unref(dbms
->bs
);
476 g_free(dbms
->node_alias
);
477 g_free(dbms
->bitmap_alias
);
482 /* Called with iothread lock taken. */
483 static int add_bitmaps_to_list(DBMSaveState
*s
, BlockDriverState
*bs
,
484 const char *bs_name
, GHashTable
*alias_map
)
486 BdrvDirtyBitmap
*bitmap
;
487 SaveBitmapState
*dbms
;
488 GHashTable
*bitmap_aliases
;
489 const char *node_alias
, *bitmap_name
, *bitmap_alias
;
490 Error
*local_err
= NULL
;
492 /* When an alias map is given, @bs_name must be @bs's node name */
493 assert(!alias_map
|| !strcmp(bs_name
, bdrv_get_node_name(bs
)));
495 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
496 if (bdrv_dirty_bitmap_name(bitmap
)) {
504 bitmap_name
= bdrv_dirty_bitmap_name(bitmap
);
506 if (!bs_name
|| strcmp(bs_name
, "") == 0) {
507 error_report("Bitmap '%s' in unnamed node can't be migrated",
513 const AliasMapInnerNode
*amin
= g_hash_table_lookup(alias_map
, bs_name
);
516 /* Skip bitmaps on nodes with no alias */
520 node_alias
= amin
->string
;
521 bitmap_aliases
= amin
->subtree
;
523 node_alias
= bs_name
;
524 bitmap_aliases
= NULL
;
527 if (node_alias
[0] == '#') {
528 error_report("Bitmap '%s' in a node with auto-generated "
529 "name '%s' can't be migrated",
530 bitmap_name
, node_alias
);
534 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
535 BitmapMigrationBitmapAliasTransform
*bitmap_transform
= NULL
;
536 bitmap_name
= bdrv_dirty_bitmap_name(bitmap
);
541 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_DEFAULT
, &local_err
)) {
542 error_report_err(local_err
);
546 if (bitmap_aliases
) {
547 BitmapMigrationBitmapAlias
*bmap_inner
;
549 bmap_inner
= g_hash_table_lookup(bitmap_aliases
, bitmap_name
);
551 /* Skip bitmaps with no alias */
555 bitmap_alias
= bmap_inner
->alias
;
556 if (bmap_inner
->transform
) {
557 bitmap_transform
= bmap_inner
->transform
;
560 if (strlen(bitmap_name
) > UINT8_MAX
) {
561 error_report("Cannot migrate bitmap '%s' on node '%s': "
562 "Name is longer than %u bytes",
563 bitmap_name
, bs_name
, UINT8_MAX
);
566 bitmap_alias
= bitmap_name
;
570 bdrv_dirty_bitmap_set_busy(bitmap
, true);
572 dbms
= g_new0(SaveBitmapState
, 1);
574 dbms
->node_alias
= g_strdup(node_alias
);
575 dbms
->bitmap_alias
= g_strdup(bitmap_alias
);
576 dbms
->bitmap
= bitmap
;
577 dbms
->total_sectors
= bdrv_nb_sectors(bs
);
578 dbms
->sectors_per_chunk
= CHUNK_SIZE
* 8LLU *
579 (bdrv_dirty_bitmap_granularity(bitmap
) >> BDRV_SECTOR_BITS
);
580 assert(dbms
->sectors_per_chunk
!= 0);
581 if (bdrv_dirty_bitmap_enabled(bitmap
)) {
582 dbms
->flags
|= DIRTY_BITMAP_MIG_START_FLAG_ENABLED
;
584 if (bitmap_transform
&&
585 bitmap_transform
->has_persistent
) {
586 if (bitmap_transform
->persistent
) {
587 dbms
->flags
|= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT
;
590 if (bdrv_dirty_bitmap_get_persistence(bitmap
)) {
591 dbms
->flags
|= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT
;
595 QSIMPLEQ_INSERT_TAIL(&s
->dbms_list
, dbms
, entry
);
601 /* Called with iothread lock taken. */
602 static int init_dirty_bitmap_migration(DBMSaveState
*s
)
604 BlockDriverState
*bs
;
605 SaveBitmapState
*dbms
;
606 GHashTable
*handled_by_blk
= g_hash_table_new(NULL
, NULL
);
608 const MigrationParameters
*mig_params
= &migrate_get_current()->parameters
;
609 GHashTable
*alias_map
= NULL
;
611 if (mig_params
->has_block_bitmap_mapping
) {
612 alias_map
= construct_alias_map(mig_params
->block_bitmap_mapping
, true,
616 s
->bulk_completed
= false;
618 s
->prev_bitmap
= NULL
;
619 s
->no_bitmaps
= false;
623 * Use blockdevice name for direct (or filtered) children of named block
626 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
627 const char *name
= blk_name(blk
);
629 if (!name
|| strcmp(name
, "") == 0) {
635 /* Skip filters without bitmaps */
636 while (bs
&& bs
->drv
&& bs
->drv
->is_filter
&&
637 !bdrv_has_named_bitmaps(bs
))
639 bs
= bdrv_filter_bs(bs
);
642 if (bs
&& bs
->drv
&& !bs
->drv
->is_filter
) {
643 if (add_bitmaps_to_list(s
, bs
, name
, NULL
)) {
646 g_hash_table_add(handled_by_blk
, bs
);
651 for (bs
= bdrv_next_all_states(NULL
); bs
; bs
= bdrv_next_all_states(bs
)) {
652 if (g_hash_table_contains(handled_by_blk
, bs
)) {
656 if (add_bitmaps_to_list(s
, bs
, bdrv_get_node_name(bs
), alias_map
)) {
661 /* unset migration flags here, to not roll back it */
662 QSIMPLEQ_FOREACH(dbms
, &s
->dbms_list
, entry
) {
663 bdrv_dirty_bitmap_skip_store(dbms
->bitmap
, true);
666 if (QSIMPLEQ_EMPTY(&s
->dbms_list
)) {
667 s
->no_bitmaps
= true;
670 g_hash_table_destroy(handled_by_blk
);
672 g_hash_table_destroy(alias_map
);
678 g_hash_table_destroy(handled_by_blk
);
680 g_hash_table_destroy(alias_map
);
682 dirty_bitmap_do_save_cleanup(s
);
687 /* Called with no lock taken. */
688 static void bulk_phase_send_chunk(QEMUFile
*f
, DBMSaveState
*s
,
689 SaveBitmapState
*dbms
)
691 uint32_t nr_sectors
= MIN(dbms
->total_sectors
- dbms
->cur_sector
,
692 dbms
->sectors_per_chunk
);
694 send_bitmap_bits(f
, s
, dbms
, dbms
->cur_sector
, nr_sectors
);
696 dbms
->cur_sector
+= nr_sectors
;
697 if (dbms
->cur_sector
>= dbms
->total_sectors
) {
698 dbms
->bulk_completed
= true;
702 /* Called with no lock taken. */
703 static void bulk_phase(QEMUFile
*f
, DBMSaveState
*s
, bool limit
)
705 SaveBitmapState
*dbms
;
707 QSIMPLEQ_FOREACH(dbms
, &s
->dbms_list
, entry
) {
708 while (!dbms
->bulk_completed
) {
709 bulk_phase_send_chunk(f
, s
, dbms
);
710 if (limit
&& qemu_file_rate_limit(f
)) {
716 s
->bulk_completed
= true;
719 /* for SaveVMHandlers */
720 static void dirty_bitmap_save_cleanup(void *opaque
)
722 DBMSaveState
*s
= &((DBMState
*)opaque
)->save
;
724 dirty_bitmap_do_save_cleanup(s
);
727 static int dirty_bitmap_save_iterate(QEMUFile
*f
, void *opaque
)
729 DBMSaveState
*s
= &((DBMState
*)opaque
)->save
;
731 trace_dirty_bitmap_save_iterate(migration_in_postcopy());
733 if (migration_in_postcopy() && !s
->bulk_completed
) {
734 bulk_phase(f
, s
, true);
737 qemu_put_bitmap_flags(f
, DIRTY_BITMAP_MIG_FLAG_EOS
);
739 return s
->bulk_completed
;
742 /* Called with iothread lock taken. */
744 static int dirty_bitmap_save_complete(QEMUFile
*f
, void *opaque
)
746 DBMSaveState
*s
= &((DBMState
*)opaque
)->save
;
747 SaveBitmapState
*dbms
;
748 trace_dirty_bitmap_save_complete_enter();
750 if (!s
->bulk_completed
) {
751 bulk_phase(f
, s
, false);
754 QSIMPLEQ_FOREACH(dbms
, &s
->dbms_list
, entry
) {
755 send_bitmap_complete(f
, s
, dbms
);
758 qemu_put_bitmap_flags(f
, DIRTY_BITMAP_MIG_FLAG_EOS
);
760 trace_dirty_bitmap_save_complete_finish();
762 dirty_bitmap_save_cleanup(opaque
);
766 static void dirty_bitmap_state_pending(void *opaque
,
767 uint64_t *must_precopy
,
768 uint64_t *can_postcopy
)
770 DBMSaveState
*s
= &((DBMState
*)opaque
)->save
;
771 SaveBitmapState
*dbms
;
772 uint64_t pending
= 0;
774 qemu_mutex_lock_iothread();
776 QSIMPLEQ_FOREACH(dbms
, &s
->dbms_list
, entry
) {
777 uint64_t gran
= bdrv_dirty_bitmap_granularity(dbms
->bitmap
);
778 uint64_t sectors
= dbms
->bulk_completed
? 0 :
779 dbms
->total_sectors
- dbms
->cur_sector
;
781 pending
+= DIV_ROUND_UP(sectors
* BDRV_SECTOR_SIZE
, gran
);
784 qemu_mutex_unlock_iothread();
786 trace_dirty_bitmap_state_pending(pending
);
788 *can_postcopy
+= pending
;
791 /* First occurrence of this bitmap. It should be created if doesn't exist */
792 static int dirty_bitmap_load_start(QEMUFile
*f
, DBMLoadState
*s
)
794 Error
*local_err
= NULL
;
795 uint32_t granularity
= qemu_get_be32(f
);
796 uint8_t flags
= qemu_get_byte(f
);
805 error_report("Bitmap with the same name ('%s') already exists on "
806 "destination", bdrv_dirty_bitmap_name(s
->bitmap
));
809 s
->bitmap
= bdrv_create_dirty_bitmap(s
->bs
, granularity
,
810 s
->bitmap_name
, &local_err
);
812 error_report_err(local_err
);
817 if (flags
& DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK
) {
818 error_report("Unknown flags in migrated dirty bitmap header: %x",
824 s
->bmap_inner
->transform
&&
825 s
->bmap_inner
->transform
->has_persistent
) {
826 persistent
= s
->bmap_inner
->transform
->persistent
;
828 persistent
= flags
& DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT
;
832 bdrv_dirty_bitmap_set_persistence(s
->bitmap
, true);
835 bdrv_disable_dirty_bitmap(s
->bitmap
);
836 if (flags
& DIRTY_BITMAP_MIG_START_FLAG_ENABLED
) {
837 bdrv_dirty_bitmap_create_successor(s
->bitmap
, &local_err
);
839 error_report_err(local_err
);
843 bdrv_dirty_bitmap_set_busy(s
->bitmap
, true);
846 b
= g_new(LoadBitmapState
, 1);
848 b
->bitmap
= s
->bitmap
;
850 b
->enabled
= flags
& DIRTY_BITMAP_MIG_START_FLAG_ENABLED
;
852 s
->bitmaps
= g_slist_prepend(s
->bitmaps
, b
);
858 * before_vm_start_handle_item
860 * g_slist_foreach helper
862 * item is LoadBitmapState*
863 * opaque is DBMLoadState*
865 static void before_vm_start_handle_item(void *item
, void *opaque
)
867 DBMLoadState
*s
= opaque
;
868 LoadBitmapState
*b
= item
;
872 bdrv_enable_dirty_bitmap(b
->bitmap
);
874 bdrv_dirty_bitmap_enable_successor(b
->bitmap
);
879 s
->bitmaps
= g_slist_remove(s
->bitmaps
, b
);
884 void dirty_bitmap_mig_before_vm_start(void)
886 DBMLoadState
*s
= &dbm_state
.load
;
887 qemu_mutex_lock(&s
->lock
);
889 assert(!s
->before_vm_start_handled
);
890 g_slist_foreach(s
->bitmaps
, before_vm_start_handle_item
, s
);
891 s
->before_vm_start_handled
= true;
893 qemu_mutex_unlock(&s
->lock
);
896 static void cancel_incoming_locked(DBMLoadState
*s
)
908 /* Drop all unfinished bitmaps */
909 for (item
= s
->bitmaps
; item
; item
= g_slist_next(item
)) {
910 LoadBitmapState
*b
= item
->data
;
913 * Bitmap must be unfinished, as finished bitmaps should already be
914 * removed from the list.
916 assert(!s
->before_vm_start_handled
|| !b
->migrated
);
917 if (bdrv_dirty_bitmap_has_successor(b
->bitmap
)) {
918 bdrv_reclaim_dirty_bitmap(b
->bitmap
, &error_abort
);
920 bdrv_dirty_bitmap_set_busy(b
->bitmap
, false);
922 bdrv_release_dirty_bitmap(b
->bitmap
);
925 g_slist_free_full(s
->bitmaps
, g_free
);
929 void dirty_bitmap_mig_cancel_outgoing(void)
931 dirty_bitmap_do_save_cleanup(&dbm_state
.save
);
934 void dirty_bitmap_mig_cancel_incoming(void)
936 DBMLoadState
*s
= &dbm_state
.load
;
938 qemu_mutex_lock(&s
->lock
);
940 cancel_incoming_locked(s
);
942 qemu_mutex_unlock(&s
->lock
);
945 static void dirty_bitmap_load_complete(QEMUFile
*f
, DBMLoadState
*s
)
948 trace_dirty_bitmap_load_complete();
954 bdrv_dirty_bitmap_deserialize_finish(s
->bitmap
);
956 if (bdrv_dirty_bitmap_has_successor(s
->bitmap
)) {
957 bdrv_reclaim_dirty_bitmap(s
->bitmap
, &error_abort
);
959 bdrv_dirty_bitmap_set_busy(s
->bitmap
, false);
962 for (item
= s
->bitmaps
; item
; item
= g_slist_next(item
)) {
963 LoadBitmapState
*b
= item
->data
;
965 if (b
->bitmap
== s
->bitmap
) {
967 if (s
->before_vm_start_handled
) {
968 s
->bitmaps
= g_slist_remove(s
->bitmaps
, b
);
976 static int dirty_bitmap_load_bits(QEMUFile
*f
, DBMLoadState
*s
)
978 uint64_t first_byte
= qemu_get_be64(f
) << BDRV_SECTOR_BITS
;
979 uint64_t nr_bytes
= (uint64_t)qemu_get_be32(f
) << BDRV_SECTOR_BITS
;
980 trace_dirty_bitmap_load_bits_enter(first_byte
>> BDRV_SECTOR_BITS
,
981 nr_bytes
>> BDRV_SECTOR_BITS
);
983 if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_ZEROES
) {
984 trace_dirty_bitmap_load_bits_zeroes();
986 bdrv_dirty_bitmap_deserialize_zeroes(s
->bitmap
, first_byte
,
991 g_autofree
uint8_t *buf
= NULL
;
992 uint64_t buf_size
= qemu_get_be64(f
);
993 uint64_t needed_size
;
996 * The actual check for buf_size is done a bit later. We can't do it in
997 * cancelled mode as we don't have the bitmap to check the constraints
998 * (so, we allocate a buffer and read prior to the check). On the other
999 * hand, we shouldn't blindly g_malloc the number from the stream.
1000 * Actually one chunk should not be larger than CHUNK_SIZE. Let's allow
1001 * a bit larger (which means that bitmap migration will fail anyway and
1002 * the whole migration will most probably fail soon due to broken
1005 if (buf_size
> 10 * CHUNK_SIZE
) {
1006 error_report("Bitmap migration stream buffer allocation request "
1011 buf
= g_malloc(buf_size
);
1012 ret
= qemu_get_buffer(f
, buf
, buf_size
);
1013 if (ret
!= buf_size
) {
1014 error_report("Failed to read bitmap bits");
1022 needed_size
= bdrv_dirty_bitmap_serialization_size(s
->bitmap
,
1026 if (needed_size
> buf_size
||
1027 buf_size
> QEMU_ALIGN_UP(needed_size
, 4 * sizeof(long))
1028 /* Here used same alignment as in send_bitmap_bits */
1030 error_report("Migrated bitmap granularity doesn't "
1031 "match the destination bitmap '%s' granularity",
1032 bdrv_dirty_bitmap_name(s
->bitmap
));
1033 cancel_incoming_locked(s
);
1037 bdrv_dirty_bitmap_deserialize_part(s
->bitmap
, buf
, first_byte
, nr_bytes
,
1044 static int dirty_bitmap_load_header(QEMUFile
*f
, DBMLoadState
*s
,
1045 GHashTable
*alias_map
)
1047 GHashTable
*bitmap_alias_map
= NULL
;
1048 Error
*local_err
= NULL
;
1050 s
->flags
= qemu_get_bitmap_flags(f
);
1051 trace_dirty_bitmap_load_header(s
->flags
);
1053 nothing
= s
->flags
== (s
->flags
& DIRTY_BITMAP_MIG_FLAG_EOS
);
1055 if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME
) {
1056 if (!qemu_get_counted_string(f
, s
->node_alias
)) {
1057 error_report("Unable to read node alias string");
1061 if (!s
->cancelled
) {
1063 const AliasMapInnerNode
*amin
;
1065 amin
= g_hash_table_lookup(alias_map
, s
->node_alias
);
1067 error_setg(&local_err
, "Error: Unknown node alias '%s'",
1071 bitmap_alias_map
= amin
->subtree
;
1072 s
->bs
= bdrv_lookup_bs(NULL
, amin
->string
, &local_err
);
1075 s
->bs
= bdrv_lookup_bs(s
->node_alias
, s
->node_alias
,
1079 error_report_err(local_err
);
1080 cancel_incoming_locked(s
);
1085 const AliasMapInnerNode
*amin
;
1087 /* Must be present in the map, or s->bs would not be set */
1088 amin
= g_hash_table_lookup(alias_map
, s
->node_alias
);
1089 assert(amin
!= NULL
);
1091 bitmap_alias_map
= amin
->subtree
;
1093 } else if (!nothing
&& !s
->cancelled
) {
1094 error_report("Error: block device name is not set");
1095 cancel_incoming_locked(s
);
1098 assert(nothing
|| s
->cancelled
|| !!alias_map
== !!bitmap_alias_map
);
1100 if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME
) {
1101 const char *bitmap_name
;
1103 if (!qemu_get_counted_string(f
, s
->bitmap_alias
)) {
1104 error_report("Unable to read bitmap alias string");
1108 bitmap_name
= s
->bitmap_alias
;
1109 if (!s
->cancelled
&& bitmap_alias_map
) {
1110 BitmapMigrationBitmapAlias
*bmap_inner
;
1112 bmap_inner
= g_hash_table_lookup(bitmap_alias_map
, s
->bitmap_alias
);
1114 error_report("Error: Unknown bitmap alias '%s' on node "
1115 "'%s' (alias '%s')", s
->bitmap_alias
,
1116 s
->bs
->node_name
, s
->node_alias
);
1117 cancel_incoming_locked(s
);
1119 bitmap_name
= bmap_inner
->name
;
1122 s
->bmap_inner
= bmap_inner
;
1125 if (!s
->cancelled
) {
1126 g_strlcpy(s
->bitmap_name
, bitmap_name
, sizeof(s
->bitmap_name
));
1127 s
->bitmap
= bdrv_find_dirty_bitmap(s
->bs
, s
->bitmap_name
);
1130 * bitmap may be NULL here, it wouldn't be an error if it is the
1131 * first occurrence of the bitmap
1133 if (!s
->bitmap
&& !(s
->flags
& DIRTY_BITMAP_MIG_FLAG_START
)) {
1134 error_report("Error: unknown dirty bitmap "
1135 "'%s' for block device '%s'",
1136 s
->bitmap_name
, s
->bs
->node_name
);
1137 cancel_incoming_locked(s
);
1140 } else if (!s
->bitmap
&& !nothing
&& !s
->cancelled
) {
1141 error_report("Error: block device name is not set");
1142 cancel_incoming_locked(s
);
1151 * Load sequence of dirty bitmap chunks. Return error only on fatal io stream
1152 * violations. On other errors just cancel bitmaps incoming migration and return
1155 * Note, than when incoming bitmap migration is canceled, we still must read all
1156 * our chunks (and just ignore them), to not affect other migration objects.
1158 static int dirty_bitmap_load(QEMUFile
*f
, void *opaque
, int version_id
)
1160 GHashTable
*alias_map
= NULL
;
1161 const MigrationParameters
*mig_params
= &migrate_get_current()->parameters
;
1162 DBMLoadState
*s
= &((DBMState
*)opaque
)->load
;
1165 trace_dirty_bitmap_load_enter();
1167 if (version_id
!= 1) {
1168 QEMU_LOCK_GUARD(&s
->lock
);
1169 cancel_incoming_locked(s
);
1173 if (mig_params
->has_block_bitmap_mapping
) {
1174 alias_map
= construct_alias_map(mig_params
->block_bitmap_mapping
,
1175 false, &error_abort
);
1179 QEMU_LOCK_GUARD(&s
->lock
);
1181 ret
= dirty_bitmap_load_header(f
, s
, alias_map
);
1183 cancel_incoming_locked(s
);
1187 if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_START
) {
1188 ret
= dirty_bitmap_load_start(f
, s
);
1189 } else if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_COMPLETE
) {
1190 dirty_bitmap_load_complete(f
, s
);
1191 } else if (s
->flags
& DIRTY_BITMAP_MIG_FLAG_BITS
) {
1192 ret
= dirty_bitmap_load_bits(f
, s
);
1196 ret
= qemu_file_get_error(f
);
1200 cancel_incoming_locked(s
);
1203 } while (!(s
->flags
& DIRTY_BITMAP_MIG_FLAG_EOS
));
1205 trace_dirty_bitmap_load_success();
1209 g_hash_table_destroy(alias_map
);
1214 static int dirty_bitmap_save_setup(QEMUFile
*f
, void *opaque
)
1216 DBMSaveState
*s
= &((DBMState
*)opaque
)->save
;
1217 SaveBitmapState
*dbms
= NULL
;
1219 qemu_mutex_lock_iothread();
1220 if (init_dirty_bitmap_migration(s
) < 0) {
1221 qemu_mutex_unlock_iothread();
1225 QSIMPLEQ_FOREACH(dbms
, &s
->dbms_list
, entry
) {
1226 send_bitmap_start(f
, s
, dbms
);
1228 qemu_put_bitmap_flags(f
, DIRTY_BITMAP_MIG_FLAG_EOS
);
1229 qemu_mutex_unlock_iothread();
1233 static bool dirty_bitmap_is_active(void *opaque
)
1235 DBMSaveState
*s
= &((DBMState
*)opaque
)->save
;
1237 return migrate_dirty_bitmaps() && !s
->no_bitmaps
;
1240 static bool dirty_bitmap_is_active_iterate(void *opaque
)
1242 return dirty_bitmap_is_active(opaque
) && !runstate_is_running();
1245 static bool dirty_bitmap_has_postcopy(void *opaque
)
1250 static SaveVMHandlers savevm_dirty_bitmap_handlers
= {
1251 .save_setup
= dirty_bitmap_save_setup
,
1252 .save_live_complete_postcopy
= dirty_bitmap_save_complete
,
1253 .save_live_complete_precopy
= dirty_bitmap_save_complete
,
1254 .has_postcopy
= dirty_bitmap_has_postcopy
,
1255 .state_pending_exact
= dirty_bitmap_state_pending
,
1256 .state_pending_estimate
= dirty_bitmap_state_pending
,
1257 .save_live_iterate
= dirty_bitmap_save_iterate
,
1258 .is_active_iterate
= dirty_bitmap_is_active_iterate
,
1259 .load_state
= dirty_bitmap_load
,
1260 .save_cleanup
= dirty_bitmap_save_cleanup
,
1261 .is_active
= dirty_bitmap_is_active
,
1264 void dirty_bitmap_mig_init(void)
1266 QSIMPLEQ_INIT(&dbm_state
.save
.dbms_list
);
1267 qemu_mutex_init(&dbm_state
.load
.lock
);
1269 register_savevm_live("dirty-bitmap", 0, 1,
1270 &savevm_dirty_bitmap_handlers
,