hw: virtio-pmem: detach the element fromt the virtqueue when error occurs
[qemu/ar7.git] / migration / block-dirty-bitmap.c
blob5bef793ac0ebccc4c600b883b88dea4dd7a69ba3
1 /*
2 * Block dirty bitmap postcopy migration
4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
7 * Authors:
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.
19 * ***
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)
38 * header
39 * be64: granularity
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)
47 * header
49 * # Data chunk of bitmap migration
50 * header
51 * be64: start sector
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
58 * chunk.
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 "qemu/id.h"
76 #include "qapi/error.h"
77 #include "qapi/qapi-commands-migration.h"
78 #include "trace.h"
80 #define CHUNK_SIZE (1 << 10)
82 /* Flags occupy one, two or four bytes (Big Endian). The size is determined as
83 * follows:
84 * in first (most significant) byte bit 8 is clear --> one byte
85 * in first byte bit 8 is set --> two or four bytes, depending on second
86 * byte:
87 * | in second byte bit 8 is clear --> two bytes
88 * | in second byte bit 8 is set --> four bytes
90 #define DIRTY_BITMAP_MIG_FLAG_EOS 0x01
91 #define DIRTY_BITMAP_MIG_FLAG_ZEROES 0x02
92 #define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME 0x04
93 #define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME 0x08
94 #define DIRTY_BITMAP_MIG_FLAG_START 0x10
95 #define DIRTY_BITMAP_MIG_FLAG_COMPLETE 0x20
96 #define DIRTY_BITMAP_MIG_FLAG_BITS 0x40
98 #define DIRTY_BITMAP_MIG_EXTRA_FLAGS 0x80
100 #define DIRTY_BITMAP_MIG_START_FLAG_ENABLED 0x01
101 #define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT 0x02
102 /* 0x04 was "AUTOLOAD" flags on older versions, now it is ignored */
103 #define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK 0xf8
105 /* State of one bitmap during save process */
106 typedef struct SaveBitmapState {
107 /* Written during setup phase. */
108 BlockDriverState *bs;
109 char *node_alias;
110 char *bitmap_alias;
111 BdrvDirtyBitmap *bitmap;
112 uint64_t total_sectors;
113 uint64_t sectors_per_chunk;
114 QSIMPLEQ_ENTRY(SaveBitmapState) entry;
115 uint8_t flags;
117 /* For bulk phase. */
118 bool bulk_completed;
119 uint64_t cur_sector;
120 } SaveBitmapState;
122 /* State of the dirty bitmap migration (DBM) during save process */
123 typedef struct DBMSaveState {
124 QSIMPLEQ_HEAD(, SaveBitmapState) dbms_list;
126 bool bulk_completed;
127 bool no_bitmaps;
129 /* for send_bitmap_bits() */
130 BlockDriverState *prev_bs;
131 BdrvDirtyBitmap *prev_bitmap;
132 } DBMSaveState;
134 typedef struct LoadBitmapState {
135 BlockDriverState *bs;
136 BdrvDirtyBitmap *bitmap;
137 bool migrated;
138 bool enabled;
139 } LoadBitmapState;
141 /* State of the dirty bitmap migration (DBM) during load process */
142 typedef struct DBMLoadState {
143 uint32_t flags;
144 char node_alias[256];
145 char bitmap_alias[256];
146 char bitmap_name[BDRV_BITMAP_MAX_NAME_SIZE + 1];
147 BlockDriverState *bs;
148 BdrvDirtyBitmap *bitmap;
150 bool before_vm_start_handled; /* set in dirty_bitmap_mig_before_vm_start */
153 * cancelled
154 * Incoming migration is cancelled for some reason. That means that we
155 * still should read our chunks from migration stream, to not affect other
156 * migration objects (like RAM), but just ignore them and do not touch any
157 * bitmaps or nodes.
159 bool cancelled;
161 GSList *bitmaps;
162 QemuMutex lock; /* protect bitmaps */
163 } DBMLoadState;
165 typedef struct DBMState {
166 DBMSaveState save;
167 DBMLoadState load;
168 } DBMState;
170 static DBMState dbm_state;
172 /* For hash tables that map node/bitmap names to aliases */
173 typedef struct AliasMapInnerNode {
174 char *string;
175 GHashTable *subtree;
176 } AliasMapInnerNode;
178 static void free_alias_map_inner_node(void *amin_ptr)
180 AliasMapInnerNode *amin = amin_ptr;
182 g_free(amin->string);
183 g_hash_table_unref(amin->subtree);
184 g_free(amin);
188 * Construct an alias map based on the given QMP structure.
190 * (Note that we cannot store such maps in the MigrationParameters
191 * object, because that struct is defined by the QAPI schema, which
192 * makes it basically impossible to have dicts with arbitrary keys.
193 * Therefore, we instead have to construct these maps when migration
194 * starts.)
196 * @bbm is the block_bitmap_mapping from the migration parameters.
198 * If @name_to_alias is true, the returned hash table will map node
199 * and bitmap names to their respective aliases (for outgoing
200 * migration).
202 * If @name_to_alias is false, the returned hash table will map node
203 * and bitmap aliases to their respective names (for incoming
204 * migration).
206 * The hash table maps node names/aliases to AliasMapInnerNode
207 * objects, whose .string is the respective node alias/name, and whose
208 * .subtree table maps bitmap names/aliases to the respective bitmap
209 * alias/name.
211 static GHashTable *construct_alias_map(const BitmapMigrationNodeAliasList *bbm,
212 bool name_to_alias,
213 Error **errp)
215 GHashTable *alias_map;
216 size_t max_node_name_len = sizeof_field(BlockDriverState, node_name) - 1;
218 alias_map = g_hash_table_new_full(g_str_hash, g_str_equal,
219 g_free, free_alias_map_inner_node);
221 for (; bbm; bbm = bbm->next) {
222 const BitmapMigrationNodeAlias *bmna = bbm->value;
223 const BitmapMigrationBitmapAliasList *bmbal;
224 AliasMapInnerNode *amin;
225 GHashTable *bitmaps_map;
226 const char *node_map_from, *node_map_to;
228 if (!id_wellformed(bmna->alias)) {
229 error_setg(errp, "The node alias '%s' is not well-formed",
230 bmna->alias);
231 goto fail;
234 if (strlen(bmna->alias) > UINT8_MAX) {
235 error_setg(errp, "The node alias '%s' is longer than %u bytes",
236 bmna->alias, UINT8_MAX);
237 goto fail;
240 if (strlen(bmna->node_name) > max_node_name_len) {
241 error_setg(errp, "The node name '%s' is longer than %zu bytes",
242 bmna->node_name, max_node_name_len);
243 goto fail;
246 if (name_to_alias) {
247 if (g_hash_table_contains(alias_map, bmna->node_name)) {
248 error_setg(errp, "The node name '%s' is mapped twice",
249 bmna->node_name);
250 goto fail;
253 node_map_from = bmna->node_name;
254 node_map_to = bmna->alias;
255 } else {
256 if (g_hash_table_contains(alias_map, bmna->alias)) {
257 error_setg(errp, "The node alias '%s' is used twice",
258 bmna->alias);
259 goto fail;
262 node_map_from = bmna->alias;
263 node_map_to = bmna->node_name;
266 bitmaps_map = g_hash_table_new_full(g_str_hash, g_str_equal,
267 g_free, g_free);
269 amin = g_new(AliasMapInnerNode, 1);
270 *amin = (AliasMapInnerNode){
271 .string = g_strdup(node_map_to),
272 .subtree = bitmaps_map,
275 g_hash_table_insert(alias_map, g_strdup(node_map_from), amin);
277 for (bmbal = bmna->bitmaps; bmbal; bmbal = bmbal->next) {
278 const BitmapMigrationBitmapAlias *bmba = bmbal->value;
279 const char *bmap_map_from, *bmap_map_to;
281 if (strlen(bmba->alias) > UINT8_MAX) {
282 error_setg(errp,
283 "The bitmap alias '%s' is longer than %u bytes",
284 bmba->alias, UINT8_MAX);
285 goto fail;
288 if (strlen(bmba->name) > BDRV_BITMAP_MAX_NAME_SIZE) {
289 error_setg(errp, "The bitmap name '%s' is longer than %d bytes",
290 bmba->name, BDRV_BITMAP_MAX_NAME_SIZE);
291 goto fail;
294 if (name_to_alias) {
295 bmap_map_from = bmba->name;
296 bmap_map_to = bmba->alias;
298 if (g_hash_table_contains(bitmaps_map, bmba->name)) {
299 error_setg(errp, "The bitmap '%s'/'%s' is mapped twice",
300 bmna->node_name, bmba->name);
301 goto fail;
303 } else {
304 bmap_map_from = bmba->alias;
305 bmap_map_to = bmba->name;
307 if (g_hash_table_contains(bitmaps_map, bmba->alias)) {
308 error_setg(errp, "The bitmap alias '%s'/'%s' is used twice",
309 bmna->alias, bmba->alias);
310 goto fail;
314 g_hash_table_insert(bitmaps_map,
315 g_strdup(bmap_map_from), g_strdup(bmap_map_to));
319 return alias_map;
321 fail:
322 g_hash_table_destroy(alias_map);
323 return NULL;
327 * Run construct_alias_map() in both directions to check whether @bbm
328 * is valid.
329 * (This function is to be used by migration/migration.c to validate
330 * the user-specified block-bitmap-mapping migration parameter.)
332 * Returns true if and only if the mapping is valid.
334 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
335 Error **errp)
337 GHashTable *alias_map;
339 alias_map = construct_alias_map(bbm, true, errp);
340 if (!alias_map) {
341 return false;
343 g_hash_table_destroy(alias_map);
345 alias_map = construct_alias_map(bbm, false, errp);
346 if (!alias_map) {
347 return false;
349 g_hash_table_destroy(alias_map);
351 return true;
354 static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
356 uint8_t flags = qemu_get_byte(f);
357 if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
358 flags = flags << 8 | qemu_get_byte(f);
359 if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
360 flags = flags << 16 | qemu_get_be16(f);
364 return flags;
367 static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
369 /* The code currently does not send flags as more than one byte */
370 assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));
372 qemu_put_byte(f, flags);
375 static void send_bitmap_header(QEMUFile *f, DBMSaveState *s,
376 SaveBitmapState *dbms, uint32_t additional_flags)
378 BlockDriverState *bs = dbms->bs;
379 BdrvDirtyBitmap *bitmap = dbms->bitmap;
380 uint32_t flags = additional_flags;
381 trace_send_bitmap_header_enter();
383 if (bs != s->prev_bs) {
384 s->prev_bs = bs;
385 flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
388 if (bitmap != s->prev_bitmap) {
389 s->prev_bitmap = bitmap;
390 flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
393 qemu_put_bitmap_flags(f, flags);
395 if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
396 qemu_put_counted_string(f, dbms->node_alias);
399 if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
400 qemu_put_counted_string(f, dbms->bitmap_alias);
404 static void send_bitmap_start(QEMUFile *f, DBMSaveState *s,
405 SaveBitmapState *dbms)
407 send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_START);
408 qemu_put_be32(f, bdrv_dirty_bitmap_granularity(dbms->bitmap));
409 qemu_put_byte(f, dbms->flags);
412 static void send_bitmap_complete(QEMUFile *f, DBMSaveState *s,
413 SaveBitmapState *dbms)
415 send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
418 static void send_bitmap_bits(QEMUFile *f, DBMSaveState *s,
419 SaveBitmapState *dbms,
420 uint64_t start_sector, uint32_t nr_sectors)
422 /* align for buffer_is_zero() */
423 uint64_t align = 4 * sizeof(long);
424 uint64_t unaligned_size =
425 bdrv_dirty_bitmap_serialization_size(
426 dbms->bitmap, start_sector << BDRV_SECTOR_BITS,
427 (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
428 uint64_t buf_size = QEMU_ALIGN_UP(unaligned_size, align);
429 uint8_t *buf = g_malloc0(buf_size);
430 uint32_t flags = DIRTY_BITMAP_MIG_FLAG_BITS;
432 bdrv_dirty_bitmap_serialize_part(
433 dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
434 (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
436 if (buffer_is_zero(buf, buf_size)) {
437 g_free(buf);
438 buf = NULL;
439 flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
442 trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);
444 send_bitmap_header(f, s, dbms, flags);
446 qemu_put_be64(f, start_sector);
447 qemu_put_be32(f, nr_sectors);
449 /* if a block is zero we need to flush here since the network
450 * bandwidth is now a lot higher than the storage device bandwidth.
451 * thus if we queue zero blocks we slow down the migration. */
452 if (flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
453 qemu_fflush(f);
454 } else {
455 qemu_put_be64(f, buf_size);
456 qemu_put_buffer(f, buf, buf_size);
459 g_free(buf);
462 /* Called with iothread lock taken. */
463 static void dirty_bitmap_do_save_cleanup(DBMSaveState *s)
465 SaveBitmapState *dbms;
467 while ((dbms = QSIMPLEQ_FIRST(&s->dbms_list)) != NULL) {
468 QSIMPLEQ_REMOVE_HEAD(&s->dbms_list, entry);
469 bdrv_dirty_bitmap_set_busy(dbms->bitmap, false);
470 bdrv_unref(dbms->bs);
471 g_free(dbms->node_alias);
472 g_free(dbms->bitmap_alias);
473 g_free(dbms);
477 /* Called with iothread lock taken. */
478 static int add_bitmaps_to_list(DBMSaveState *s, BlockDriverState *bs,
479 const char *bs_name, GHashTable *alias_map)
481 BdrvDirtyBitmap *bitmap;
482 SaveBitmapState *dbms;
483 GHashTable *bitmap_aliases;
484 const char *node_alias, *bitmap_name, *bitmap_alias;
485 Error *local_err = NULL;
487 /* When an alias map is given, @bs_name must be @bs's node name */
488 assert(!alias_map || !strcmp(bs_name, bdrv_get_node_name(bs)));
490 FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
491 if (bdrv_dirty_bitmap_name(bitmap)) {
492 break;
495 if (!bitmap) {
496 return 0;
499 bitmap_name = bdrv_dirty_bitmap_name(bitmap);
501 if (!bs_name || strcmp(bs_name, "") == 0) {
502 error_report("Bitmap '%s' in unnamed node can't be migrated",
503 bitmap_name);
504 return -1;
507 if (alias_map) {
508 const AliasMapInnerNode *amin = g_hash_table_lookup(alias_map, bs_name);
510 if (!amin) {
511 /* Skip bitmaps on nodes with no alias */
512 return 0;
515 node_alias = amin->string;
516 bitmap_aliases = amin->subtree;
517 } else {
518 node_alias = bs_name;
519 bitmap_aliases = NULL;
522 if (node_alias[0] == '#') {
523 error_report("Bitmap '%s' in a node with auto-generated "
524 "name '%s' can't be migrated",
525 bitmap_name, node_alias);
526 return -1;
529 FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
530 bitmap_name = bdrv_dirty_bitmap_name(bitmap);
531 if (!bitmap_name) {
532 continue;
535 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, &local_err)) {
536 error_report_err(local_err);
537 return -1;
540 if (bitmap_aliases) {
541 bitmap_alias = g_hash_table_lookup(bitmap_aliases, bitmap_name);
542 if (!bitmap_alias) {
543 /* Skip bitmaps with no alias */
544 continue;
546 } else {
547 if (strlen(bitmap_name) > UINT8_MAX) {
548 error_report("Cannot migrate bitmap '%s' on node '%s': "
549 "Name is longer than %u bytes",
550 bitmap_name, bs_name, UINT8_MAX);
551 return -1;
553 bitmap_alias = bitmap_name;
556 bdrv_ref(bs);
557 bdrv_dirty_bitmap_set_busy(bitmap, true);
559 dbms = g_new0(SaveBitmapState, 1);
560 dbms->bs = bs;
561 dbms->node_alias = g_strdup(node_alias);
562 dbms->bitmap_alias = g_strdup(bitmap_alias);
563 dbms->bitmap = bitmap;
564 dbms->total_sectors = bdrv_nb_sectors(bs);
565 dbms->sectors_per_chunk = CHUNK_SIZE * 8 *
566 bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS;
567 if (bdrv_dirty_bitmap_enabled(bitmap)) {
568 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
570 if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
571 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
574 QSIMPLEQ_INSERT_TAIL(&s->dbms_list, dbms, entry);
577 return 0;
580 /* Called with iothread lock taken. */
581 static int init_dirty_bitmap_migration(DBMSaveState *s)
583 BlockDriverState *bs;
584 SaveBitmapState *dbms;
585 GHashTable *handled_by_blk = g_hash_table_new(NULL, NULL);
586 BlockBackend *blk;
587 const MigrationParameters *mig_params = &migrate_get_current()->parameters;
588 GHashTable *alias_map = NULL;
590 if (mig_params->has_block_bitmap_mapping) {
591 alias_map = construct_alias_map(mig_params->block_bitmap_mapping, true,
592 &error_abort);
595 s->bulk_completed = false;
596 s->prev_bs = NULL;
597 s->prev_bitmap = NULL;
598 s->no_bitmaps = false;
600 if (!alias_map) {
602 * Use blockdevice name for direct (or filtered) children of named block
603 * backends.
605 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
606 const char *name = blk_name(blk);
608 if (!name || strcmp(name, "") == 0) {
609 continue;
612 bs = blk_bs(blk);
614 /* Skip filters without bitmaps */
615 while (bs && bs->drv && bs->drv->is_filter &&
616 !bdrv_has_named_bitmaps(bs))
618 bs = bdrv_filter_bs(bs);
621 if (bs && bs->drv && !bs->drv->is_filter) {
622 if (add_bitmaps_to_list(s, bs, name, NULL)) {
623 goto fail;
625 g_hash_table_add(handled_by_blk, bs);
630 for (bs = bdrv_next_all_states(NULL); bs; bs = bdrv_next_all_states(bs)) {
631 if (g_hash_table_contains(handled_by_blk, bs)) {
632 continue;
635 if (add_bitmaps_to_list(s, bs, bdrv_get_node_name(bs), alias_map)) {
636 goto fail;
640 /* unset migration flags here, to not roll back it */
641 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
642 bdrv_dirty_bitmap_skip_store(dbms->bitmap, true);
645 if (QSIMPLEQ_EMPTY(&s->dbms_list)) {
646 s->no_bitmaps = true;
649 g_hash_table_destroy(handled_by_blk);
650 if (alias_map) {
651 g_hash_table_destroy(alias_map);
654 return 0;
656 fail:
657 g_hash_table_destroy(handled_by_blk);
658 if (alias_map) {
659 g_hash_table_destroy(alias_map);
661 dirty_bitmap_do_save_cleanup(s);
663 return -1;
666 /* Called with no lock taken. */
667 static void bulk_phase_send_chunk(QEMUFile *f, DBMSaveState *s,
668 SaveBitmapState *dbms)
670 uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
671 dbms->sectors_per_chunk);
673 send_bitmap_bits(f, s, dbms, dbms->cur_sector, nr_sectors);
675 dbms->cur_sector += nr_sectors;
676 if (dbms->cur_sector >= dbms->total_sectors) {
677 dbms->bulk_completed = true;
681 /* Called with no lock taken. */
682 static void bulk_phase(QEMUFile *f, DBMSaveState *s, bool limit)
684 SaveBitmapState *dbms;
686 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
687 while (!dbms->bulk_completed) {
688 bulk_phase_send_chunk(f, s, dbms);
689 if (limit && qemu_file_rate_limit(f)) {
690 return;
695 s->bulk_completed = true;
698 /* for SaveVMHandlers */
699 static void dirty_bitmap_save_cleanup(void *opaque)
701 DBMSaveState *s = &((DBMState *)opaque)->save;
703 dirty_bitmap_do_save_cleanup(s);
706 static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
708 DBMSaveState *s = &((DBMState *)opaque)->save;
710 trace_dirty_bitmap_save_iterate(migration_in_postcopy());
712 if (migration_in_postcopy() && !s->bulk_completed) {
713 bulk_phase(f, s, true);
716 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
718 return s->bulk_completed;
721 /* Called with iothread lock taken. */
723 static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
725 DBMSaveState *s = &((DBMState *)opaque)->save;
726 SaveBitmapState *dbms;
727 trace_dirty_bitmap_save_complete_enter();
729 if (!s->bulk_completed) {
730 bulk_phase(f, s, false);
733 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
734 send_bitmap_complete(f, s, dbms);
737 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
739 trace_dirty_bitmap_save_complete_finish();
741 dirty_bitmap_save_cleanup(opaque);
742 return 0;
745 static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
746 uint64_t max_size,
747 uint64_t *res_precopy_only,
748 uint64_t *res_compatible,
749 uint64_t *res_postcopy_only)
751 DBMSaveState *s = &((DBMState *)opaque)->save;
752 SaveBitmapState *dbms;
753 uint64_t pending = 0;
755 qemu_mutex_lock_iothread();
757 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
758 uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
759 uint64_t sectors = dbms->bulk_completed ? 0 :
760 dbms->total_sectors - dbms->cur_sector;
762 pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
765 qemu_mutex_unlock_iothread();
767 trace_dirty_bitmap_save_pending(pending, max_size);
769 *res_postcopy_only += pending;
772 /* First occurrence of this bitmap. It should be created if doesn't exist */
773 static int dirty_bitmap_load_start(QEMUFile *f, DBMLoadState *s)
775 Error *local_err = NULL;
776 uint32_t granularity = qemu_get_be32(f);
777 uint8_t flags = qemu_get_byte(f);
778 LoadBitmapState *b;
780 if (s->cancelled) {
781 return 0;
784 if (s->bitmap) {
785 error_report("Bitmap with the same name ('%s') already exists on "
786 "destination", bdrv_dirty_bitmap_name(s->bitmap));
787 return -EINVAL;
788 } else {
789 s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
790 s->bitmap_name, &local_err);
791 if (!s->bitmap) {
792 error_report_err(local_err);
793 return -EINVAL;
797 if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
798 error_report("Unknown flags in migrated dirty bitmap header: %x",
799 flags);
800 return -EINVAL;
803 if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
804 bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
807 bdrv_disable_dirty_bitmap(s->bitmap);
808 if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
809 bdrv_dirty_bitmap_create_successor(s->bitmap, &local_err);
810 if (local_err) {
811 error_report_err(local_err);
812 return -EINVAL;
816 b = g_new(LoadBitmapState, 1);
817 b->bs = s->bs;
818 b->bitmap = s->bitmap;
819 b->migrated = false;
820 b->enabled = flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
822 s->bitmaps = g_slist_prepend(s->bitmaps, b);
824 return 0;
828 * before_vm_start_handle_item
830 * g_slist_foreach helper
832 * item is LoadBitmapState*
833 * opaque is DBMLoadState*
835 static void before_vm_start_handle_item(void *item, void *opaque)
837 DBMLoadState *s = opaque;
838 LoadBitmapState *b = item;
840 if (b->enabled) {
841 if (b->migrated) {
842 bdrv_enable_dirty_bitmap(b->bitmap);
843 } else {
844 bdrv_dirty_bitmap_enable_successor(b->bitmap);
848 if (b->migrated) {
849 s->bitmaps = g_slist_remove(s->bitmaps, b);
850 g_free(b);
854 void dirty_bitmap_mig_before_vm_start(void)
856 DBMLoadState *s = &dbm_state.load;
857 qemu_mutex_lock(&s->lock);
859 assert(!s->before_vm_start_handled);
860 g_slist_foreach(s->bitmaps, before_vm_start_handle_item, s);
861 s->before_vm_start_handled = true;
863 qemu_mutex_unlock(&s->lock);
866 static void cancel_incoming_locked(DBMLoadState *s)
868 GSList *item;
870 if (s->cancelled) {
871 return;
874 s->cancelled = true;
875 s->bs = NULL;
876 s->bitmap = NULL;
878 /* Drop all unfinished bitmaps */
879 for (item = s->bitmaps; item; item = g_slist_next(item)) {
880 LoadBitmapState *b = item->data;
883 * Bitmap must be unfinished, as finished bitmaps should already be
884 * removed from the list.
886 assert(!s->before_vm_start_handled || !b->migrated);
887 if (bdrv_dirty_bitmap_has_successor(b->bitmap)) {
888 bdrv_reclaim_dirty_bitmap(b->bitmap, &error_abort);
890 bdrv_release_dirty_bitmap(b->bitmap);
893 g_slist_free_full(s->bitmaps, g_free);
894 s->bitmaps = NULL;
897 void dirty_bitmap_mig_cancel_outgoing(void)
899 dirty_bitmap_do_save_cleanup(&dbm_state.save);
902 void dirty_bitmap_mig_cancel_incoming(void)
904 DBMLoadState *s = &dbm_state.load;
906 qemu_mutex_lock(&s->lock);
908 cancel_incoming_locked(s);
910 qemu_mutex_unlock(&s->lock);
913 static void dirty_bitmap_load_complete(QEMUFile *f, DBMLoadState *s)
915 GSList *item;
916 trace_dirty_bitmap_load_complete();
918 if (s->cancelled) {
919 return;
922 bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
924 if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
925 bdrv_reclaim_dirty_bitmap(s->bitmap, &error_abort);
928 for (item = s->bitmaps; item; item = g_slist_next(item)) {
929 LoadBitmapState *b = item->data;
931 if (b->bitmap == s->bitmap) {
932 b->migrated = true;
933 if (s->before_vm_start_handled) {
934 s->bitmaps = g_slist_remove(s->bitmaps, b);
935 g_free(b);
937 break;
942 static int dirty_bitmap_load_bits(QEMUFile *f, DBMLoadState *s)
944 uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
945 uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
946 trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
947 nr_bytes >> BDRV_SECTOR_BITS);
949 if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
950 trace_dirty_bitmap_load_bits_zeroes();
951 if (!s->cancelled) {
952 bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte,
953 nr_bytes, false);
955 } else {
956 size_t ret;
957 g_autofree uint8_t *buf = NULL;
958 uint64_t buf_size = qemu_get_be64(f);
959 uint64_t needed_size;
962 * The actual check for buf_size is done a bit later. We can't do it in
963 * cancelled mode as we don't have the bitmap to check the constraints
964 * (so, we allocate a buffer and read prior to the check). On the other
965 * hand, we shouldn't blindly g_malloc the number from the stream.
966 * Actually one chunk should not be larger than CHUNK_SIZE. Let's allow
967 * a bit larger (which means that bitmap migration will fail anyway and
968 * the whole migration will most probably fail soon due to broken
969 * stream).
971 if (buf_size > 10 * CHUNK_SIZE) {
972 error_report("Bitmap migration stream buffer allocation request "
973 "is too large");
974 return -EIO;
977 buf = g_malloc(buf_size);
978 ret = qemu_get_buffer(f, buf, buf_size);
979 if (ret != buf_size) {
980 error_report("Failed to read bitmap bits");
981 return -EIO;
984 if (s->cancelled) {
985 return 0;
988 needed_size = bdrv_dirty_bitmap_serialization_size(s->bitmap,
989 first_byte,
990 nr_bytes);
992 if (needed_size > buf_size ||
993 buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
994 /* Here used same alignment as in send_bitmap_bits */
996 error_report("Migrated bitmap granularity doesn't "
997 "match the destination bitmap '%s' granularity",
998 bdrv_dirty_bitmap_name(s->bitmap));
999 cancel_incoming_locked(s);
1000 return 0;
1003 bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
1004 false);
1007 return 0;
1010 static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s,
1011 GHashTable *alias_map)
1013 GHashTable *bitmap_alias_map = NULL;
1014 Error *local_err = NULL;
1015 bool nothing;
1016 s->flags = qemu_get_bitmap_flags(f);
1017 trace_dirty_bitmap_load_header(s->flags);
1019 nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
1021 if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
1022 if (!qemu_get_counted_string(f, s->node_alias)) {
1023 error_report("Unable to read node alias string");
1024 return -EINVAL;
1027 if (!s->cancelled) {
1028 if (alias_map) {
1029 const AliasMapInnerNode *amin;
1031 amin = g_hash_table_lookup(alias_map, s->node_alias);
1032 if (!amin) {
1033 error_setg(&local_err, "Error: Unknown node alias '%s'",
1034 s->node_alias);
1035 s->bs = NULL;
1036 } else {
1037 bitmap_alias_map = amin->subtree;
1038 s->bs = bdrv_lookup_bs(NULL, amin->string, &local_err);
1040 } else {
1041 s->bs = bdrv_lookup_bs(s->node_alias, s->node_alias,
1042 &local_err);
1044 if (!s->bs) {
1045 error_report_err(local_err);
1046 cancel_incoming_locked(s);
1049 } else if (s->bs) {
1050 if (alias_map) {
1051 const AliasMapInnerNode *amin;
1053 /* Must be present in the map, or s->bs would not be set */
1054 amin = g_hash_table_lookup(alias_map, s->node_alias);
1055 assert(amin != NULL);
1057 bitmap_alias_map = amin->subtree;
1059 } else if (!nothing && !s->cancelled) {
1060 error_report("Error: block device name is not set");
1061 cancel_incoming_locked(s);
1064 assert(nothing || s->cancelled || !!alias_map == !!bitmap_alias_map);
1066 if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
1067 const char *bitmap_name;
1069 if (!qemu_get_counted_string(f, s->bitmap_alias)) {
1070 error_report("Unable to read bitmap alias string");
1071 return -EINVAL;
1074 if (!s->cancelled) {
1075 if (bitmap_alias_map) {
1076 bitmap_name = g_hash_table_lookup(bitmap_alias_map,
1077 s->bitmap_alias);
1078 if (!bitmap_name) {
1079 error_report("Error: Unknown bitmap alias '%s' on node "
1080 "'%s' (alias '%s')", s->bitmap_alias,
1081 s->bs->node_name, s->node_alias);
1082 cancel_incoming_locked(s);
1084 } else {
1085 bitmap_name = s->bitmap_alias;
1089 if (!s->cancelled) {
1090 g_strlcpy(s->bitmap_name, bitmap_name, sizeof(s->bitmap_name));
1091 s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
1094 * bitmap may be NULL here, it wouldn't be an error if it is the
1095 * first occurrence of the bitmap
1097 if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
1098 error_report("Error: unknown dirty bitmap "
1099 "'%s' for block device '%s'",
1100 s->bitmap_name, s->bs->node_name);
1101 cancel_incoming_locked(s);
1104 } else if (!s->bitmap && !nothing && !s->cancelled) {
1105 error_report("Error: block device name is not set");
1106 cancel_incoming_locked(s);
1109 return 0;
1113 * dirty_bitmap_load
1115 * Load sequence of dirty bitmap chunks. Return error only on fatal io stream
1116 * violations. On other errors just cancel bitmaps incoming migration and return
1117 * 0.
1119 * Note, than when incoming bitmap migration is canceled, we still must read all
1120 * our chunks (and just ignore them), to not affect other migration objects.
1122 static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
1124 GHashTable *alias_map = NULL;
1125 const MigrationParameters *mig_params = &migrate_get_current()->parameters;
1126 DBMLoadState *s = &((DBMState *)opaque)->load;
1127 int ret = 0;
1129 trace_dirty_bitmap_load_enter();
1131 if (version_id != 1) {
1132 QEMU_LOCK_GUARD(&s->lock);
1133 cancel_incoming_locked(s);
1134 return -EINVAL;
1137 if (mig_params->has_block_bitmap_mapping) {
1138 alias_map = construct_alias_map(mig_params->block_bitmap_mapping,
1139 false, &error_abort);
1142 do {
1143 QEMU_LOCK_GUARD(&s->lock);
1145 ret = dirty_bitmap_load_header(f, s, alias_map);
1146 if (ret < 0) {
1147 cancel_incoming_locked(s);
1148 goto fail;
1151 if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) {
1152 ret = dirty_bitmap_load_start(f, s);
1153 } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
1154 dirty_bitmap_load_complete(f, s);
1155 } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
1156 ret = dirty_bitmap_load_bits(f, s);
1159 if (!ret) {
1160 ret = qemu_file_get_error(f);
1163 if (ret) {
1164 cancel_incoming_locked(s);
1165 goto fail;
1167 } while (!(s->flags & DIRTY_BITMAP_MIG_FLAG_EOS));
1169 trace_dirty_bitmap_load_success();
1170 ret = 0;
1171 fail:
1172 if (alias_map) {
1173 g_hash_table_destroy(alias_map);
1175 return ret;
1178 static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
1180 DBMSaveState *s = &((DBMState *)opaque)->save;
1181 SaveBitmapState *dbms = NULL;
1182 if (init_dirty_bitmap_migration(s) < 0) {
1183 return -1;
1186 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
1187 send_bitmap_start(f, s, dbms);
1189 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
1191 return 0;
1194 static bool dirty_bitmap_is_active(void *opaque)
1196 DBMSaveState *s = &((DBMState *)opaque)->save;
1198 return migrate_dirty_bitmaps() && !s->no_bitmaps;
1201 static bool dirty_bitmap_is_active_iterate(void *opaque)
1203 return dirty_bitmap_is_active(opaque) && !runstate_is_running();
1206 static bool dirty_bitmap_has_postcopy(void *opaque)
1208 return true;
1211 static SaveVMHandlers savevm_dirty_bitmap_handlers = {
1212 .save_setup = dirty_bitmap_save_setup,
1213 .save_live_complete_postcopy = dirty_bitmap_save_complete,
1214 .save_live_complete_precopy = dirty_bitmap_save_complete,
1215 .has_postcopy = dirty_bitmap_has_postcopy,
1216 .save_live_pending = dirty_bitmap_save_pending,
1217 .save_live_iterate = dirty_bitmap_save_iterate,
1218 .is_active_iterate = dirty_bitmap_is_active_iterate,
1219 .load_state = dirty_bitmap_load,
1220 .save_cleanup = dirty_bitmap_save_cleanup,
1221 .is_active = dirty_bitmap_is_active,
1224 void dirty_bitmap_mig_init(void)
1226 QSIMPLEQ_INIT(&dbm_state.save.dbms_list);
1227 qemu_mutex_init(&dbm_state.load.lock);
1229 register_savevm_live("dirty-bitmap", 0, 1,
1230 &savevm_dirty_bitmap_handlers,
1231 &dbm_state);