xref: /qemu/migration/block-dirty-bitmap.c (revision 138ca49a)
1 /*
2  * Block dirty bitmap postcopy migration
3  *
4  * Copyright IBM, Corp. 2009
5  * Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
6  *
7  * Authors:
8  *  Liran Schour   <lirans@il.ibm.com>
9  *  Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
10  *
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.
15  *
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.
18  *
19  *                                ***
20  *
21  * Here postcopy migration of dirty bitmaps is realized. Only QMP-addressable
22  * bitmaps are migrated.
23  *
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.
27  *
28  * format of migration:
29  *
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     ] /
36  *
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
45  *
46  * # Complete of bitmap migration (flags & COMPLETE)
47  * header
48  *
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    ] /
55  *
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.
59  */
60 
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"
79 
80 #define CHUNK_SIZE     (1 << 10)
81 
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
89  */
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
97 
98 #define DIRTY_BITMAP_MIG_EXTRA_FLAGS        0x80
99 
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
104 
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;
116 
117     /* For bulk phase. */
118     bool bulk_completed;
119     uint64_t cur_sector;
120 } SaveBitmapState;
121 
122 /* State of the dirty bitmap migration (DBM) during save process */
123 typedef struct DBMSaveState {
124     QSIMPLEQ_HEAD(, SaveBitmapState) dbms_list;
125 
126     bool bulk_completed;
127     bool no_bitmaps;
128 
129     /* for send_bitmap_bits() */
130     BlockDriverState *prev_bs;
131     BdrvDirtyBitmap *prev_bitmap;
132 } DBMSaveState;
133 
134 typedef struct LoadBitmapState {
135     BlockDriverState *bs;
136     BdrvDirtyBitmap *bitmap;
137     bool migrated;
138     bool enabled;
139 } LoadBitmapState;
140 
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;
149 
150     bool before_vm_start_handled; /* set in dirty_bitmap_mig_before_vm_start */
151 
152     /*
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.
158      */
159     bool cancelled;
160 
161     GSList *bitmaps;
162     QemuMutex lock; /* protect bitmaps */
163 } DBMLoadState;
164 
165 typedef struct DBMState {
166     DBMSaveState save;
167     DBMLoadState load;
168 } DBMState;
169 
170 static DBMState dbm_state;
171 
172 /* For hash tables that map node/bitmap names to aliases */
173 typedef struct AliasMapInnerNode {
174     char *string;
175     GHashTable *subtree;
176 } AliasMapInnerNode;
177 
178 static void free_alias_map_inner_node(void *amin_ptr)
179 {
180     AliasMapInnerNode *amin = amin_ptr;
181 
182     g_free(amin->string);
183     g_hash_table_unref(amin->subtree);
184     g_free(amin);
185 }
186 
187 /**
188  * Construct an alias map based on the given QMP structure.
189  *
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.)
195  *
196  * @bbm is the block_bitmap_mapping from the migration parameters.
197  *
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).
201  *
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).
205  *
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.
210  */
211 static GHashTable *construct_alias_map(const BitmapMigrationNodeAliasList *bbm,
212                                        bool name_to_alias,
213                                        Error **errp)
214 {
215     GHashTable *alias_map;
216     size_t max_node_name_len = sizeof_field(BlockDriverState, node_name) - 1;
217 
218     alias_map = g_hash_table_new_full(g_str_hash, g_str_equal,
219                                       g_free, free_alias_map_inner_node);
220 
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;
227 
228         if (!id_wellformed(bmna->alias)) {
229             error_setg(errp, "The node alias '%s' is not well-formed",
230                        bmna->alias);
231             goto fail;
232         }
233 
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;
238         }
239 
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;
244         }
245 
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;
251             }
252 
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;
260             }
261 
262             node_map_from = bmna->alias;
263             node_map_to = bmna->node_name;
264         }
265 
266         bitmaps_map = g_hash_table_new_full(g_str_hash, g_str_equal,
267                                             g_free, g_free);
268 
269         amin = g_new(AliasMapInnerNode, 1);
270         *amin = (AliasMapInnerNode){
271             .string = g_strdup(node_map_to),
272             .subtree = bitmaps_map,
273         };
274 
275         g_hash_table_insert(alias_map, g_strdup(node_map_from), amin);
276 
277         for (bmbal = bmna->bitmaps; bmbal; bmbal = bmbal->next) {
278             const BitmapMigrationBitmapAlias *bmba = bmbal->value;
279             const char *bmap_map_from, *bmap_map_to;
280 
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;
286             }
287 
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;
292             }
293 
294             if (name_to_alias) {
295                 bmap_map_from = bmba->name;
296                 bmap_map_to = bmba->alias;
297 
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;
302                 }
303             } else {
304                 bmap_map_from = bmba->alias;
305                 bmap_map_to = bmba->name;
306 
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;
311                 }
312             }
313 
314             g_hash_table_insert(bitmaps_map,
315                                 g_strdup(bmap_map_from), g_strdup(bmap_map_to));
316         }
317     }
318 
319     return alias_map;
320 
321 fail:
322     g_hash_table_destroy(alias_map);
323     return NULL;
324 }
325 
326 /**
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.)
331  *
332  * Returns true if and only if the mapping is valid.
333  */
334 bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
335                                       Error **errp)
336 {
337     GHashTable *alias_map;
338 
339     alias_map = construct_alias_map(bbm, true, errp);
340     if (!alias_map) {
341         return false;
342     }
343     g_hash_table_destroy(alias_map);
344 
345     alias_map = construct_alias_map(bbm, false, errp);
346     if (!alias_map) {
347         return false;
348     }
349     g_hash_table_destroy(alias_map);
350 
351     return true;
352 }
353 
354 static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
355 {
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);
361         }
362     }
363 
364     return flags;
365 }
366 
367 static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
368 {
369     /* The code currently does not send flags as more than one byte */
370     assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));
371 
372     qemu_put_byte(f, flags);
373 }
374 
375 static void send_bitmap_header(QEMUFile *f, DBMSaveState *s,
376                                SaveBitmapState *dbms, uint32_t additional_flags)
377 {
378     BlockDriverState *bs = dbms->bs;
379     BdrvDirtyBitmap *bitmap = dbms->bitmap;
380     uint32_t flags = additional_flags;
381     trace_send_bitmap_header_enter();
382 
383     if (bs != s->prev_bs) {
384         s->prev_bs = bs;
385         flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
386     }
387 
388     if (bitmap != s->prev_bitmap) {
389         s->prev_bitmap = bitmap;
390         flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
391     }
392 
393     qemu_put_bitmap_flags(f, flags);
394 
395     if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
396         qemu_put_counted_string(f, dbms->node_alias);
397     }
398 
399     if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
400         qemu_put_counted_string(f, dbms->bitmap_alias);
401     }
402 }
403 
404 static void send_bitmap_start(QEMUFile *f, DBMSaveState *s,
405                               SaveBitmapState *dbms)
406 {
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);
410 }
411 
412 static void send_bitmap_complete(QEMUFile *f, DBMSaveState *s,
413                                  SaveBitmapState *dbms)
414 {
415     send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
416 }
417 
418 static void send_bitmap_bits(QEMUFile *f, DBMSaveState *s,
419                              SaveBitmapState *dbms,
420                              uint64_t start_sector, uint32_t nr_sectors)
421 {
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;
431 
432     bdrv_dirty_bitmap_serialize_part(
433         dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
434         (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
435 
436     if (buffer_is_zero(buf, buf_size)) {
437         g_free(buf);
438         buf = NULL;
439         flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
440     }
441 
442     trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);
443 
444     send_bitmap_header(f, s, dbms, flags);
445 
446     qemu_put_be64(f, start_sector);
447     qemu_put_be32(f, nr_sectors);
448 
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);
457     }
458 
459     g_free(buf);
460 }
461 
462 /* Called with iothread lock taken.  */
463 static void dirty_bitmap_do_save_cleanup(DBMSaveState *s)
464 {
465     SaveBitmapState *dbms;
466 
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);
474     }
475 }
476 
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)
480 {
481     BdrvDirtyBitmap *bitmap;
482     SaveBitmapState *dbms;
483     GHashTable *bitmap_aliases;
484     const char *node_alias, *bitmap_name, *bitmap_alias;
485     Error *local_err = NULL;
486 
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)));
489 
490     FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
491         if (bdrv_dirty_bitmap_name(bitmap)) {
492             break;
493         }
494     }
495     if (!bitmap) {
496         return 0;
497     }
498 
499     bitmap_name = bdrv_dirty_bitmap_name(bitmap);
500 
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;
505     }
506 
507     if (alias_map) {
508         const AliasMapInnerNode *amin = g_hash_table_lookup(alias_map, bs_name);
509 
510         if (!amin) {
511             /* Skip bitmaps on nodes with no alias */
512             return 0;
513         }
514 
515         node_alias = amin->string;
516         bitmap_aliases = amin->subtree;
517     } else {
518         node_alias = bs_name;
519         bitmap_aliases = NULL;
520     }
521 
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;
527     }
528 
529     FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
530         bitmap_name = bdrv_dirty_bitmap_name(bitmap);
531         if (!bitmap_name) {
532             continue;
533         }
534 
535         if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, &local_err)) {
536             error_report_err(local_err);
537             return -1;
538         }
539 
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;
545             }
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;
552             }
553             bitmap_alias = bitmap_name;
554         }
555 
556         bdrv_ref(bs);
557         bdrv_dirty_bitmap_set_busy(bitmap, true);
558 
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 * 8LLU *
566             (bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS);
567         assert(dbms->sectors_per_chunk != 0);
568         if (bdrv_dirty_bitmap_enabled(bitmap)) {
569             dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
570         }
571         if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
572             dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
573         }
574 
575         QSIMPLEQ_INSERT_TAIL(&s->dbms_list, dbms, entry);
576     }
577 
578     return 0;
579 }
580 
581 /* Called with iothread lock taken. */
582 static int init_dirty_bitmap_migration(DBMSaveState *s)
583 {
584     BlockDriverState *bs;
585     SaveBitmapState *dbms;
586     GHashTable *handled_by_blk = g_hash_table_new(NULL, NULL);
587     BlockBackend *blk;
588     const MigrationParameters *mig_params = &migrate_get_current()->parameters;
589     GHashTable *alias_map = NULL;
590 
591     if (mig_params->has_block_bitmap_mapping) {
592         alias_map = construct_alias_map(mig_params->block_bitmap_mapping, true,
593                                         &error_abort);
594     }
595 
596     s->bulk_completed = false;
597     s->prev_bs = NULL;
598     s->prev_bitmap = NULL;
599     s->no_bitmaps = false;
600 
601     if (!alias_map) {
602         /*
603          * Use blockdevice name for direct (or filtered) children of named block
604          * backends.
605          */
606         for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
607             const char *name = blk_name(blk);
608 
609             if (!name || strcmp(name, "") == 0) {
610                 continue;
611             }
612 
613             bs = blk_bs(blk);
614 
615             /* Skip filters without bitmaps */
616             while (bs && bs->drv && bs->drv->is_filter &&
617                    !bdrv_has_named_bitmaps(bs))
618             {
619                 bs = bdrv_filter_bs(bs);
620             }
621 
622             if (bs && bs->drv && !bs->drv->is_filter) {
623                 if (add_bitmaps_to_list(s, bs, name, NULL)) {
624                     goto fail;
625                 }
626                 g_hash_table_add(handled_by_blk, bs);
627             }
628         }
629     }
630 
631     for (bs = bdrv_next_all_states(NULL); bs; bs = bdrv_next_all_states(bs)) {
632         if (g_hash_table_contains(handled_by_blk, bs)) {
633             continue;
634         }
635 
636         if (add_bitmaps_to_list(s, bs, bdrv_get_node_name(bs), alias_map)) {
637             goto fail;
638         }
639     }
640 
641     /* unset migration flags here, to not roll back it */
642     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
643         bdrv_dirty_bitmap_skip_store(dbms->bitmap, true);
644     }
645 
646     if (QSIMPLEQ_EMPTY(&s->dbms_list)) {
647         s->no_bitmaps = true;
648     }
649 
650     g_hash_table_destroy(handled_by_blk);
651     if (alias_map) {
652         g_hash_table_destroy(alias_map);
653     }
654 
655     return 0;
656 
657 fail:
658     g_hash_table_destroy(handled_by_blk);
659     if (alias_map) {
660         g_hash_table_destroy(alias_map);
661     }
662     dirty_bitmap_do_save_cleanup(s);
663 
664     return -1;
665 }
666 
667 /* Called with no lock taken.  */
668 static void bulk_phase_send_chunk(QEMUFile *f, DBMSaveState *s,
669                                   SaveBitmapState *dbms)
670 {
671     uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
672                              dbms->sectors_per_chunk);
673 
674     send_bitmap_bits(f, s, dbms, dbms->cur_sector, nr_sectors);
675 
676     dbms->cur_sector += nr_sectors;
677     if (dbms->cur_sector >= dbms->total_sectors) {
678         dbms->bulk_completed = true;
679     }
680 }
681 
682 /* Called with no lock taken.  */
683 static void bulk_phase(QEMUFile *f, DBMSaveState *s, bool limit)
684 {
685     SaveBitmapState *dbms;
686 
687     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
688         while (!dbms->bulk_completed) {
689             bulk_phase_send_chunk(f, s, dbms);
690             if (limit && qemu_file_rate_limit(f)) {
691                 return;
692             }
693         }
694     }
695 
696     s->bulk_completed = true;
697 }
698 
699 /* for SaveVMHandlers */
700 static void dirty_bitmap_save_cleanup(void *opaque)
701 {
702     DBMSaveState *s = &((DBMState *)opaque)->save;
703 
704     dirty_bitmap_do_save_cleanup(s);
705 }
706 
707 static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
708 {
709     DBMSaveState *s = &((DBMState *)opaque)->save;
710 
711     trace_dirty_bitmap_save_iterate(migration_in_postcopy());
712 
713     if (migration_in_postcopy() && !s->bulk_completed) {
714         bulk_phase(f, s, true);
715     }
716 
717     qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
718 
719     return s->bulk_completed;
720 }
721 
722 /* Called with iothread lock taken.  */
723 
724 static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
725 {
726     DBMSaveState *s = &((DBMState *)opaque)->save;
727     SaveBitmapState *dbms;
728     trace_dirty_bitmap_save_complete_enter();
729 
730     if (!s->bulk_completed) {
731         bulk_phase(f, s, false);
732     }
733 
734     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
735         send_bitmap_complete(f, s, dbms);
736     }
737 
738     qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
739 
740     trace_dirty_bitmap_save_complete_finish();
741 
742     dirty_bitmap_save_cleanup(opaque);
743     return 0;
744 }
745 
746 static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
747                                       uint64_t max_size,
748                                       uint64_t *res_precopy_only,
749                                       uint64_t *res_compatible,
750                                       uint64_t *res_postcopy_only)
751 {
752     DBMSaveState *s = &((DBMState *)opaque)->save;
753     SaveBitmapState *dbms;
754     uint64_t pending = 0;
755 
756     qemu_mutex_lock_iothread();
757 
758     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
759         uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
760         uint64_t sectors = dbms->bulk_completed ? 0 :
761                            dbms->total_sectors - dbms->cur_sector;
762 
763         pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
764     }
765 
766     qemu_mutex_unlock_iothread();
767 
768     trace_dirty_bitmap_save_pending(pending, max_size);
769 
770     *res_postcopy_only += pending;
771 }
772 
773 /* First occurrence of this bitmap. It should be created if doesn't exist */
774 static int dirty_bitmap_load_start(QEMUFile *f, DBMLoadState *s)
775 {
776     Error *local_err = NULL;
777     uint32_t granularity = qemu_get_be32(f);
778     uint8_t flags = qemu_get_byte(f);
779     LoadBitmapState *b;
780 
781     if (s->cancelled) {
782         return 0;
783     }
784 
785     if (s->bitmap) {
786         error_report("Bitmap with the same name ('%s') already exists on "
787                      "destination", bdrv_dirty_bitmap_name(s->bitmap));
788         return -EINVAL;
789     } else {
790         s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
791                                              s->bitmap_name, &local_err);
792         if (!s->bitmap) {
793             error_report_err(local_err);
794             return -EINVAL;
795         }
796     }
797 
798     if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
799         error_report("Unknown flags in migrated dirty bitmap header: %x",
800                      flags);
801         return -EINVAL;
802     }
803 
804     if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
805         bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
806     }
807 
808     bdrv_disable_dirty_bitmap(s->bitmap);
809     if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
810         bdrv_dirty_bitmap_create_successor(s->bitmap, &local_err);
811         if (local_err) {
812             error_report_err(local_err);
813             return -EINVAL;
814         }
815     }
816 
817     b = g_new(LoadBitmapState, 1);
818     b->bs = s->bs;
819     b->bitmap = s->bitmap;
820     b->migrated = false;
821     b->enabled = flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
822 
823     s->bitmaps = g_slist_prepend(s->bitmaps, b);
824 
825     return 0;
826 }
827 
828 /*
829  * before_vm_start_handle_item
830  *
831  * g_slist_foreach helper
832  *
833  * item is LoadBitmapState*
834  * opaque is DBMLoadState*
835  */
836 static void before_vm_start_handle_item(void *item, void *opaque)
837 {
838     DBMLoadState *s = opaque;
839     LoadBitmapState *b = item;
840 
841     if (b->enabled) {
842         if (b->migrated) {
843             bdrv_enable_dirty_bitmap(b->bitmap);
844         } else {
845             bdrv_dirty_bitmap_enable_successor(b->bitmap);
846         }
847     }
848 
849     if (b->migrated) {
850         s->bitmaps = g_slist_remove(s->bitmaps, b);
851         g_free(b);
852     }
853 }
854 
855 void dirty_bitmap_mig_before_vm_start(void)
856 {
857     DBMLoadState *s = &dbm_state.load;
858     qemu_mutex_lock(&s->lock);
859 
860     assert(!s->before_vm_start_handled);
861     g_slist_foreach(s->bitmaps, before_vm_start_handle_item, s);
862     s->before_vm_start_handled = true;
863 
864     qemu_mutex_unlock(&s->lock);
865 }
866 
867 static void cancel_incoming_locked(DBMLoadState *s)
868 {
869     GSList *item;
870 
871     if (s->cancelled) {
872         return;
873     }
874 
875     s->cancelled = true;
876     s->bs = NULL;
877     s->bitmap = NULL;
878 
879     /* Drop all unfinished bitmaps */
880     for (item = s->bitmaps; item; item = g_slist_next(item)) {
881         LoadBitmapState *b = item->data;
882 
883         /*
884          * Bitmap must be unfinished, as finished bitmaps should already be
885          * removed from the list.
886          */
887         assert(!s->before_vm_start_handled || !b->migrated);
888         if (bdrv_dirty_bitmap_has_successor(b->bitmap)) {
889             bdrv_reclaim_dirty_bitmap(b->bitmap, &error_abort);
890         }
891         bdrv_release_dirty_bitmap(b->bitmap);
892     }
893 
894     g_slist_free_full(s->bitmaps, g_free);
895     s->bitmaps = NULL;
896 }
897 
898 void dirty_bitmap_mig_cancel_outgoing(void)
899 {
900     dirty_bitmap_do_save_cleanup(&dbm_state.save);
901 }
902 
903 void dirty_bitmap_mig_cancel_incoming(void)
904 {
905     DBMLoadState *s = &dbm_state.load;
906 
907     qemu_mutex_lock(&s->lock);
908 
909     cancel_incoming_locked(s);
910 
911     qemu_mutex_unlock(&s->lock);
912 }
913 
914 static void dirty_bitmap_load_complete(QEMUFile *f, DBMLoadState *s)
915 {
916     GSList *item;
917     trace_dirty_bitmap_load_complete();
918 
919     if (s->cancelled) {
920         return;
921     }
922 
923     bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
924 
925     if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
926         bdrv_reclaim_dirty_bitmap(s->bitmap, &error_abort);
927     }
928 
929     for (item = s->bitmaps; item; item = g_slist_next(item)) {
930         LoadBitmapState *b = item->data;
931 
932         if (b->bitmap == s->bitmap) {
933             b->migrated = true;
934             if (s->before_vm_start_handled) {
935                 s->bitmaps = g_slist_remove(s->bitmaps, b);
936                 g_free(b);
937             }
938             break;
939         }
940     }
941 }
942 
943 static int dirty_bitmap_load_bits(QEMUFile *f, DBMLoadState *s)
944 {
945     uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
946     uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
947     trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
948                                        nr_bytes >> BDRV_SECTOR_BITS);
949 
950     if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
951         trace_dirty_bitmap_load_bits_zeroes();
952         if (!s->cancelled) {
953             bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte,
954                                                  nr_bytes, false);
955         }
956     } else {
957         size_t ret;
958         g_autofree uint8_t *buf = NULL;
959         uint64_t buf_size = qemu_get_be64(f);
960         uint64_t needed_size;
961 
962         /*
963          * The actual check for buf_size is done a bit later. We can't do it in
964          * cancelled mode as we don't have the bitmap to check the constraints
965          * (so, we allocate a buffer and read prior to the check). On the other
966          * hand, we shouldn't blindly g_malloc the number from the stream.
967          * Actually one chunk should not be larger than CHUNK_SIZE. Let's allow
968          * a bit larger (which means that bitmap migration will fail anyway and
969          * the whole migration will most probably fail soon due to broken
970          * stream).
971          */
972         if (buf_size > 10 * CHUNK_SIZE) {
973             error_report("Bitmap migration stream buffer allocation request "
974                          "is too large");
975             return -EIO;
976         }
977 
978         buf = g_malloc(buf_size);
979         ret = qemu_get_buffer(f, buf, buf_size);
980         if (ret != buf_size) {
981             error_report("Failed to read bitmap bits");
982             return -EIO;
983         }
984 
985         if (s->cancelled) {
986             return 0;
987         }
988 
989         needed_size = bdrv_dirty_bitmap_serialization_size(s->bitmap,
990                                                            first_byte,
991                                                            nr_bytes);
992 
993         if (needed_size > buf_size ||
994             buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
995              /* Here used same alignment as in send_bitmap_bits */
996         ) {
997             error_report("Migrated bitmap granularity doesn't "
998                          "match the destination bitmap '%s' granularity",
999                          bdrv_dirty_bitmap_name(s->bitmap));
1000             cancel_incoming_locked(s);
1001             return 0;
1002         }
1003 
1004         bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
1005                                            false);
1006     }
1007 
1008     return 0;
1009 }
1010 
1011 static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s,
1012                                     GHashTable *alias_map)
1013 {
1014     GHashTable *bitmap_alias_map = NULL;
1015     Error *local_err = NULL;
1016     bool nothing;
1017     s->flags = qemu_get_bitmap_flags(f);
1018     trace_dirty_bitmap_load_header(s->flags);
1019 
1020     nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
1021 
1022     if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
1023         if (!qemu_get_counted_string(f, s->node_alias)) {
1024             error_report("Unable to read node alias string");
1025             return -EINVAL;
1026         }
1027 
1028         if (!s->cancelled) {
1029             if (alias_map) {
1030                 const AliasMapInnerNode *amin;
1031 
1032                 amin = g_hash_table_lookup(alias_map, s->node_alias);
1033                 if (!amin) {
1034                     error_setg(&local_err, "Error: Unknown node alias '%s'",
1035                                s->node_alias);
1036                     s->bs = NULL;
1037                 } else {
1038                     bitmap_alias_map = amin->subtree;
1039                     s->bs = bdrv_lookup_bs(NULL, amin->string, &local_err);
1040                 }
1041             } else {
1042                 s->bs = bdrv_lookup_bs(s->node_alias, s->node_alias,
1043                                        &local_err);
1044             }
1045             if (!s->bs) {
1046                 error_report_err(local_err);
1047                 cancel_incoming_locked(s);
1048             }
1049         }
1050     } else if (s->bs) {
1051         if (alias_map) {
1052             const AliasMapInnerNode *amin;
1053 
1054             /* Must be present in the map, or s->bs would not be set */
1055             amin = g_hash_table_lookup(alias_map, s->node_alias);
1056             assert(amin != NULL);
1057 
1058             bitmap_alias_map = amin->subtree;
1059         }
1060     } else if (!nothing && !s->cancelled) {
1061         error_report("Error: block device name is not set");
1062         cancel_incoming_locked(s);
1063     }
1064 
1065     assert(nothing || s->cancelled || !!alias_map == !!bitmap_alias_map);
1066 
1067     if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
1068         const char *bitmap_name;
1069 
1070         if (!qemu_get_counted_string(f, s->bitmap_alias)) {
1071             error_report("Unable to read bitmap alias string");
1072             return -EINVAL;
1073         }
1074 
1075         bitmap_name = s->bitmap_alias;
1076         if (!s->cancelled && bitmap_alias_map) {
1077             bitmap_name = g_hash_table_lookup(bitmap_alias_map,
1078                                               s->bitmap_alias);
1079             if (!bitmap_name) {
1080                 error_report("Error: Unknown bitmap alias '%s' on node "
1081                              "'%s' (alias '%s')", s->bitmap_alias,
1082                              s->bs->node_name, s->node_alias);
1083                 cancel_incoming_locked(s);
1084             }
1085         }
1086 
1087         if (!s->cancelled) {
1088             g_strlcpy(s->bitmap_name, bitmap_name, sizeof(s->bitmap_name));
1089             s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
1090 
1091             /*
1092              * bitmap may be NULL here, it wouldn't be an error if it is the
1093              * first occurrence of the bitmap
1094              */
1095             if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
1096                 error_report("Error: unknown dirty bitmap "
1097                              "'%s' for block device '%s'",
1098                              s->bitmap_name, s->bs->node_name);
1099                 cancel_incoming_locked(s);
1100             }
1101         }
1102     } else if (!s->bitmap && !nothing && !s->cancelled) {
1103         error_report("Error: block device name is not set");
1104         cancel_incoming_locked(s);
1105     }
1106 
1107     return 0;
1108 }
1109 
1110 /*
1111  * dirty_bitmap_load
1112  *
1113  * Load sequence of dirty bitmap chunks. Return error only on fatal io stream
1114  * violations. On other errors just cancel bitmaps incoming migration and return
1115  * 0.
1116  *
1117  * Note, than when incoming bitmap migration is canceled, we still must read all
1118  * our chunks (and just ignore them), to not affect other migration objects.
1119  */
1120 static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
1121 {
1122     GHashTable *alias_map = NULL;
1123     const MigrationParameters *mig_params = &migrate_get_current()->parameters;
1124     DBMLoadState *s = &((DBMState *)opaque)->load;
1125     int ret = 0;
1126 
1127     trace_dirty_bitmap_load_enter();
1128 
1129     if (version_id != 1) {
1130         QEMU_LOCK_GUARD(&s->lock);
1131         cancel_incoming_locked(s);
1132         return -EINVAL;
1133     }
1134 
1135     if (mig_params->has_block_bitmap_mapping) {
1136         alias_map = construct_alias_map(mig_params->block_bitmap_mapping,
1137                                         false, &error_abort);
1138     }
1139 
1140     do {
1141         QEMU_LOCK_GUARD(&s->lock);
1142 
1143         ret = dirty_bitmap_load_header(f, s, alias_map);
1144         if (ret < 0) {
1145             cancel_incoming_locked(s);
1146             goto fail;
1147         }
1148 
1149         if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) {
1150             ret = dirty_bitmap_load_start(f, s);
1151         } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
1152             dirty_bitmap_load_complete(f, s);
1153         } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
1154             ret = dirty_bitmap_load_bits(f, s);
1155         }
1156 
1157         if (!ret) {
1158             ret = qemu_file_get_error(f);
1159         }
1160 
1161         if (ret) {
1162             cancel_incoming_locked(s);
1163             goto fail;
1164         }
1165     } while (!(s->flags & DIRTY_BITMAP_MIG_FLAG_EOS));
1166 
1167     trace_dirty_bitmap_load_success();
1168     ret = 0;
1169 fail:
1170     if (alias_map) {
1171         g_hash_table_destroy(alias_map);
1172     }
1173     return ret;
1174 }
1175 
1176 static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
1177 {
1178     DBMSaveState *s = &((DBMState *)opaque)->save;
1179     SaveBitmapState *dbms = NULL;
1180     if (init_dirty_bitmap_migration(s) < 0) {
1181         return -1;
1182     }
1183 
1184     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
1185         send_bitmap_start(f, s, dbms);
1186     }
1187     qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
1188 
1189     return 0;
1190 }
1191 
1192 static bool dirty_bitmap_is_active(void *opaque)
1193 {
1194     DBMSaveState *s = &((DBMState *)opaque)->save;
1195 
1196     return migrate_dirty_bitmaps() && !s->no_bitmaps;
1197 }
1198 
1199 static bool dirty_bitmap_is_active_iterate(void *opaque)
1200 {
1201     return dirty_bitmap_is_active(opaque) && !runstate_is_running();
1202 }
1203 
1204 static bool dirty_bitmap_has_postcopy(void *opaque)
1205 {
1206     return true;
1207 }
1208 
1209 static SaveVMHandlers savevm_dirty_bitmap_handlers = {
1210     .save_setup = dirty_bitmap_save_setup,
1211     .save_live_complete_postcopy = dirty_bitmap_save_complete,
1212     .save_live_complete_precopy = dirty_bitmap_save_complete,
1213     .has_postcopy = dirty_bitmap_has_postcopy,
1214     .save_live_pending = dirty_bitmap_save_pending,
1215     .save_live_iterate = dirty_bitmap_save_iterate,
1216     .is_active_iterate = dirty_bitmap_is_active_iterate,
1217     .load_state = dirty_bitmap_load,
1218     .save_cleanup = dirty_bitmap_save_cleanup,
1219     .is_active = dirty_bitmap_is_active,
1220 };
1221 
1222 void dirty_bitmap_mig_init(void)
1223 {
1224     QSIMPLEQ_INIT(&dbm_state.save.dbms_list);
1225     qemu_mutex_init(&dbm_state.load.lock);
1226 
1227     register_savevm_live("dirty-bitmap", 0, 1,
1228                          &savevm_dirty_bitmap_handlers,
1229                          &dbm_state);
1230 }
1231