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 name size ] \  flags & DEVICE_NAME
33  * [ n bytes: node name     ] /
34  * [ 1 byte: bitmap name size ] \  flags & BITMAP_NAME
35  * [ n bytes: bitmap name     ] /
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 "qapi/error.h"
76 #include "trace.h"
77 
78 #define CHUNK_SIZE     (1 << 10)
79 
80 /* Flags occupy one, two or four bytes (Big Endian). The size is determined as
81  * follows:
82  * in first (most significant) byte bit 8 is clear  -->  one byte
83  * in first byte bit 8 is set    -->  two or four bytes, depending on second
84  *                                    byte:
85  *    | in second byte bit 8 is clear  -->  two bytes
86  *    | in second byte bit 8 is set    -->  four bytes
87  */
88 #define DIRTY_BITMAP_MIG_FLAG_EOS           0x01
89 #define DIRTY_BITMAP_MIG_FLAG_ZEROES        0x02
90 #define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME   0x04
91 #define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME   0x08
92 #define DIRTY_BITMAP_MIG_FLAG_START         0x10
93 #define DIRTY_BITMAP_MIG_FLAG_COMPLETE      0x20
94 #define DIRTY_BITMAP_MIG_FLAG_BITS          0x40
95 
96 #define DIRTY_BITMAP_MIG_EXTRA_FLAGS        0x80
97 
98 #define DIRTY_BITMAP_MIG_START_FLAG_ENABLED          0x01
99 #define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT       0x02
100 /* 0x04 was "AUTOLOAD" flags on older versions, now it is ignored */
101 #define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK    0xf8
102 
103 /* State of one bitmap during save process */
104 typedef struct SaveBitmapState {
105     /* Written during setup phase. */
106     BlockDriverState *bs;
107     const char *node_name;
108     BdrvDirtyBitmap *bitmap;
109     uint64_t total_sectors;
110     uint64_t sectors_per_chunk;
111     QSIMPLEQ_ENTRY(SaveBitmapState) entry;
112     uint8_t flags;
113 
114     /* For bulk phase. */
115     bool bulk_completed;
116     uint64_t cur_sector;
117 } SaveBitmapState;
118 
119 /* State of the dirty bitmap migration (DBM) during save process */
120 typedef struct DBMSaveState {
121     QSIMPLEQ_HEAD(, SaveBitmapState) dbms_list;
122 
123     bool bulk_completed;
124     bool no_bitmaps;
125 
126     /* for send_bitmap_bits() */
127     BlockDriverState *prev_bs;
128     BdrvDirtyBitmap *prev_bitmap;
129 } DBMSaveState;
130 
131 typedef struct LoadBitmapState {
132     BlockDriverState *bs;
133     BdrvDirtyBitmap *bitmap;
134     bool migrated;
135     bool enabled;
136 } LoadBitmapState;
137 
138 /* State of the dirty bitmap migration (DBM) during load process */
139 typedef struct DBMLoadState {
140     uint32_t flags;
141     char node_name[256];
142     char bitmap_name[256];
143     BlockDriverState *bs;
144     BdrvDirtyBitmap *bitmap;
145 
146     bool before_vm_start_handled; /* set in dirty_bitmap_mig_before_vm_start */
147 
148     /*
149      * cancelled
150      * Incoming migration is cancelled for some reason. That means that we
151      * still should read our chunks from migration stream, to not affect other
152      * migration objects (like RAM), but just ignore them and do not touch any
153      * bitmaps or nodes.
154      */
155     bool cancelled;
156 
157     GSList *bitmaps;
158     QemuMutex lock; /* protect bitmaps */
159 } DBMLoadState;
160 
161 typedef struct DBMState {
162     DBMSaveState save;
163     DBMLoadState load;
164 } DBMState;
165 
166 static DBMState dbm_state;
167 
168 static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
169 {
170     uint8_t flags = qemu_get_byte(f);
171     if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
172         flags = flags << 8 | qemu_get_byte(f);
173         if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
174             flags = flags << 16 | qemu_get_be16(f);
175         }
176     }
177 
178     return flags;
179 }
180 
181 static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
182 {
183     /* The code currently does not send flags as more than one byte */
184     assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));
185 
186     qemu_put_byte(f, flags);
187 }
188 
189 static void send_bitmap_header(QEMUFile *f, DBMSaveState *s,
190                                SaveBitmapState *dbms, uint32_t additional_flags)
191 {
192     BlockDriverState *bs = dbms->bs;
193     BdrvDirtyBitmap *bitmap = dbms->bitmap;
194     uint32_t flags = additional_flags;
195     trace_send_bitmap_header_enter();
196 
197     if (bs != s->prev_bs) {
198         s->prev_bs = bs;
199         flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
200     }
201 
202     if (bitmap != s->prev_bitmap) {
203         s->prev_bitmap = bitmap;
204         flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
205     }
206 
207     qemu_put_bitmap_flags(f, flags);
208 
209     if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
210         qemu_put_counted_string(f, dbms->node_name);
211     }
212 
213     if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
214         qemu_put_counted_string(f, bdrv_dirty_bitmap_name(bitmap));
215     }
216 }
217 
218 static void send_bitmap_start(QEMUFile *f, DBMSaveState *s,
219                               SaveBitmapState *dbms)
220 {
221     send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_START);
222     qemu_put_be32(f, bdrv_dirty_bitmap_granularity(dbms->bitmap));
223     qemu_put_byte(f, dbms->flags);
224 }
225 
226 static void send_bitmap_complete(QEMUFile *f, DBMSaveState *s,
227                                  SaveBitmapState *dbms)
228 {
229     send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
230 }
231 
232 static void send_bitmap_bits(QEMUFile *f, DBMSaveState *s,
233                              SaveBitmapState *dbms,
234                              uint64_t start_sector, uint32_t nr_sectors)
235 {
236     /* align for buffer_is_zero() */
237     uint64_t align = 4 * sizeof(long);
238     uint64_t unaligned_size =
239         bdrv_dirty_bitmap_serialization_size(
240             dbms->bitmap, start_sector << BDRV_SECTOR_BITS,
241             (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
242     uint64_t buf_size = QEMU_ALIGN_UP(unaligned_size, align);
243     uint8_t *buf = g_malloc0(buf_size);
244     uint32_t flags = DIRTY_BITMAP_MIG_FLAG_BITS;
245 
246     bdrv_dirty_bitmap_serialize_part(
247         dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
248         (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
249 
250     if (buffer_is_zero(buf, buf_size)) {
251         g_free(buf);
252         buf = NULL;
253         flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
254     }
255 
256     trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);
257 
258     send_bitmap_header(f, s, dbms, flags);
259 
260     qemu_put_be64(f, start_sector);
261     qemu_put_be32(f, nr_sectors);
262 
263     /* if a block is zero we need to flush here since the network
264      * bandwidth is now a lot higher than the storage device bandwidth.
265      * thus if we queue zero blocks we slow down the migration. */
266     if (flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
267         qemu_fflush(f);
268     } else {
269         qemu_put_be64(f, buf_size);
270         qemu_put_buffer(f, buf, buf_size);
271     }
272 
273     g_free(buf);
274 }
275 
276 /* Called with iothread lock taken.  */
277 static void dirty_bitmap_do_save_cleanup(DBMSaveState *s)
278 {
279     SaveBitmapState *dbms;
280 
281     while ((dbms = QSIMPLEQ_FIRST(&s->dbms_list)) != NULL) {
282         QSIMPLEQ_REMOVE_HEAD(&s->dbms_list, entry);
283         bdrv_dirty_bitmap_set_busy(dbms->bitmap, false);
284         bdrv_unref(dbms->bs);
285         g_free(dbms);
286     }
287 }
288 
289 /* Called with iothread lock taken. */
290 static int add_bitmaps_to_list(DBMSaveState *s, BlockDriverState *bs,
291                                const char *bs_name)
292 {
293     BdrvDirtyBitmap *bitmap;
294     SaveBitmapState *dbms;
295     Error *local_err = NULL;
296 
297     FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
298         if (bdrv_dirty_bitmap_name(bitmap)) {
299             break;
300         }
301     }
302     if (!bitmap) {
303         return 0;
304     }
305 
306     if (!bs_name || strcmp(bs_name, "") == 0) {
307         error_report("Bitmap '%s' in unnamed node can't be migrated",
308                      bdrv_dirty_bitmap_name(bitmap));
309         return -1;
310     }
311 
312     if (bs_name[0] == '#') {
313         error_report("Bitmap '%s' in a node with auto-generated "
314                      "name '%s' can't be migrated",
315                      bdrv_dirty_bitmap_name(bitmap), bs_name);
316         return -1;
317     }
318 
319     FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
320         if (!bdrv_dirty_bitmap_name(bitmap)) {
321             continue;
322         }
323 
324         if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, &local_err)) {
325             error_report_err(local_err);
326             return -1;
327         }
328 
329         bdrv_ref(bs);
330         bdrv_dirty_bitmap_set_busy(bitmap, true);
331 
332         dbms = g_new0(SaveBitmapState, 1);
333         dbms->bs = bs;
334         dbms->node_name = bs_name;
335         dbms->bitmap = bitmap;
336         dbms->total_sectors = bdrv_nb_sectors(bs);
337         dbms->sectors_per_chunk = CHUNK_SIZE * 8 *
338             bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS;
339         if (bdrv_dirty_bitmap_enabled(bitmap)) {
340             dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
341         }
342         if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
343             dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
344         }
345 
346         QSIMPLEQ_INSERT_TAIL(&s->dbms_list, dbms, entry);
347     }
348 
349     return 0;
350 }
351 
352 /* Called with iothread lock taken. */
353 static int init_dirty_bitmap_migration(DBMSaveState *s)
354 {
355     BlockDriverState *bs;
356     SaveBitmapState *dbms;
357     GHashTable *handled_by_blk = g_hash_table_new(NULL, NULL);
358     BlockBackend *blk;
359 
360     s->bulk_completed = false;
361     s->prev_bs = NULL;
362     s->prev_bitmap = NULL;
363     s->no_bitmaps = false;
364 
365     /*
366      * Use blockdevice name for direct (or filtered) children of named block
367      * backends.
368      */
369     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
370         const char *name = blk_name(blk);
371 
372         if (!name || strcmp(name, "") == 0) {
373             continue;
374         }
375 
376         bs = blk_bs(blk);
377 
378         /* Skip filters without bitmaps */
379         while (bs && bs->drv && bs->drv->is_filter &&
380                !bdrv_has_named_bitmaps(bs))
381         {
382             if (bs->backing) {
383                 bs = bs->backing->bs;
384             } else if (bs->file) {
385                 bs = bs->file->bs;
386             } else {
387                 bs = NULL;
388             }
389         }
390 
391         if (bs && bs->drv && !bs->drv->is_filter) {
392             if (add_bitmaps_to_list(s, bs, name)) {
393                 goto fail;
394             }
395             g_hash_table_add(handled_by_blk, bs);
396         }
397     }
398 
399     for (bs = bdrv_next_all_states(NULL); bs; bs = bdrv_next_all_states(bs)) {
400         if (g_hash_table_contains(handled_by_blk, bs)) {
401             continue;
402         }
403 
404         if (add_bitmaps_to_list(s, bs, bdrv_get_node_name(bs))) {
405             goto fail;
406         }
407     }
408 
409     /* unset migration flags here, to not roll back it */
410     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
411         bdrv_dirty_bitmap_skip_store(dbms->bitmap, true);
412     }
413 
414     if (QSIMPLEQ_EMPTY(&s->dbms_list)) {
415         s->no_bitmaps = true;
416     }
417 
418     g_hash_table_destroy(handled_by_blk);
419 
420     return 0;
421 
422 fail:
423     g_hash_table_destroy(handled_by_blk);
424     dirty_bitmap_do_save_cleanup(s);
425 
426     return -1;
427 }
428 
429 /* Called with no lock taken.  */
430 static void bulk_phase_send_chunk(QEMUFile *f, DBMSaveState *s,
431                                   SaveBitmapState *dbms)
432 {
433     uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
434                              dbms->sectors_per_chunk);
435 
436     send_bitmap_bits(f, s, dbms, dbms->cur_sector, nr_sectors);
437 
438     dbms->cur_sector += nr_sectors;
439     if (dbms->cur_sector >= dbms->total_sectors) {
440         dbms->bulk_completed = true;
441     }
442 }
443 
444 /* Called with no lock taken.  */
445 static void bulk_phase(QEMUFile *f, DBMSaveState *s, bool limit)
446 {
447     SaveBitmapState *dbms;
448 
449     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
450         while (!dbms->bulk_completed) {
451             bulk_phase_send_chunk(f, s, dbms);
452             if (limit && qemu_file_rate_limit(f)) {
453                 return;
454             }
455         }
456     }
457 
458     s->bulk_completed = true;
459 }
460 
461 /* for SaveVMHandlers */
462 static void dirty_bitmap_save_cleanup(void *opaque)
463 {
464     DBMSaveState *s = &((DBMState *)opaque)->save;
465 
466     dirty_bitmap_do_save_cleanup(s);
467 }
468 
469 static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
470 {
471     DBMSaveState *s = &((DBMState *)opaque)->save;
472 
473     trace_dirty_bitmap_save_iterate(migration_in_postcopy());
474 
475     if (migration_in_postcopy() && !s->bulk_completed) {
476         bulk_phase(f, s, true);
477     }
478 
479     qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
480 
481     return s->bulk_completed;
482 }
483 
484 /* Called with iothread lock taken.  */
485 
486 static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
487 {
488     DBMSaveState *s = &((DBMState *)opaque)->save;
489     SaveBitmapState *dbms;
490     trace_dirty_bitmap_save_complete_enter();
491 
492     if (!s->bulk_completed) {
493         bulk_phase(f, s, false);
494     }
495 
496     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
497         send_bitmap_complete(f, s, dbms);
498     }
499 
500     qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
501 
502     trace_dirty_bitmap_save_complete_finish();
503 
504     dirty_bitmap_save_cleanup(opaque);
505     return 0;
506 }
507 
508 static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
509                                       uint64_t max_size,
510                                       uint64_t *res_precopy_only,
511                                       uint64_t *res_compatible,
512                                       uint64_t *res_postcopy_only)
513 {
514     DBMSaveState *s = &((DBMState *)opaque)->save;
515     SaveBitmapState *dbms;
516     uint64_t pending = 0;
517 
518     qemu_mutex_lock_iothread();
519 
520     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
521         uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
522         uint64_t sectors = dbms->bulk_completed ? 0 :
523                            dbms->total_sectors - dbms->cur_sector;
524 
525         pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
526     }
527 
528     qemu_mutex_unlock_iothread();
529 
530     trace_dirty_bitmap_save_pending(pending, max_size);
531 
532     *res_postcopy_only += pending;
533 }
534 
535 /* First occurrence of this bitmap. It should be created if doesn't exist */
536 static int dirty_bitmap_load_start(QEMUFile *f, DBMLoadState *s)
537 {
538     Error *local_err = NULL;
539     uint32_t granularity = qemu_get_be32(f);
540     uint8_t flags = qemu_get_byte(f);
541     LoadBitmapState *b;
542 
543     if (s->cancelled) {
544         return 0;
545     }
546 
547     if (s->bitmap) {
548         error_report("Bitmap with the same name ('%s') already exists on "
549                      "destination", bdrv_dirty_bitmap_name(s->bitmap));
550         return -EINVAL;
551     } else {
552         s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
553                                              s->bitmap_name, &local_err);
554         if (!s->bitmap) {
555             error_report_err(local_err);
556             return -EINVAL;
557         }
558     }
559 
560     if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
561         error_report("Unknown flags in migrated dirty bitmap header: %x",
562                      flags);
563         return -EINVAL;
564     }
565 
566     if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
567         bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
568     }
569 
570     bdrv_disable_dirty_bitmap(s->bitmap);
571     if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
572         bdrv_dirty_bitmap_create_successor(s->bitmap, &local_err);
573         if (local_err) {
574             error_report_err(local_err);
575             return -EINVAL;
576         }
577     }
578 
579     b = g_new(LoadBitmapState, 1);
580     b->bs = s->bs;
581     b->bitmap = s->bitmap;
582     b->migrated = false;
583     b->enabled = flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
584 
585     s->bitmaps = g_slist_prepend(s->bitmaps, b);
586 
587     return 0;
588 }
589 
590 /*
591  * before_vm_start_handle_item
592  *
593  * g_slist_foreach helper
594  *
595  * item is LoadBitmapState*
596  * opaque is DBMLoadState*
597  */
598 static void before_vm_start_handle_item(void *item, void *opaque)
599 {
600     DBMLoadState *s = opaque;
601     LoadBitmapState *b = item;
602 
603     if (b->enabled) {
604         if (b->migrated) {
605             bdrv_enable_dirty_bitmap(b->bitmap);
606         } else {
607             bdrv_dirty_bitmap_enable_successor(b->bitmap);
608         }
609     }
610 
611     if (b->migrated) {
612         s->bitmaps = g_slist_remove(s->bitmaps, b);
613         g_free(b);
614     }
615 }
616 
617 void dirty_bitmap_mig_before_vm_start(void)
618 {
619     DBMLoadState *s = &dbm_state.load;
620     qemu_mutex_lock(&s->lock);
621 
622     assert(!s->before_vm_start_handled);
623     g_slist_foreach(s->bitmaps, before_vm_start_handle_item, s);
624     s->before_vm_start_handled = true;
625 
626     qemu_mutex_unlock(&s->lock);
627 }
628 
629 static void cancel_incoming_locked(DBMLoadState *s)
630 {
631     GSList *item;
632 
633     if (s->cancelled) {
634         return;
635     }
636 
637     s->cancelled = true;
638     s->bs = NULL;
639     s->bitmap = NULL;
640 
641     /* Drop all unfinished bitmaps */
642     for (item = s->bitmaps; item; item = g_slist_next(item)) {
643         LoadBitmapState *b = item->data;
644 
645         /*
646          * Bitmap must be unfinished, as finished bitmaps should already be
647          * removed from the list.
648          */
649         assert(!s->before_vm_start_handled || !b->migrated);
650         if (bdrv_dirty_bitmap_has_successor(b->bitmap)) {
651             bdrv_reclaim_dirty_bitmap(b->bitmap, &error_abort);
652         }
653         bdrv_release_dirty_bitmap(b->bitmap);
654     }
655 
656     g_slist_free_full(s->bitmaps, g_free);
657     s->bitmaps = NULL;
658 }
659 
660 void dirty_bitmap_mig_cancel_outgoing(void)
661 {
662     dirty_bitmap_do_save_cleanup(&dbm_state.save);
663 }
664 
665 void dirty_bitmap_mig_cancel_incoming(void)
666 {
667     DBMLoadState *s = &dbm_state.load;
668 
669     qemu_mutex_lock(&s->lock);
670 
671     cancel_incoming_locked(s);
672 
673     qemu_mutex_unlock(&s->lock);
674 }
675 
676 static void dirty_bitmap_load_complete(QEMUFile *f, DBMLoadState *s)
677 {
678     GSList *item;
679     trace_dirty_bitmap_load_complete();
680 
681     if (s->cancelled) {
682         return;
683     }
684 
685     bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
686 
687     if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
688         bdrv_reclaim_dirty_bitmap(s->bitmap, &error_abort);
689     }
690 
691     for (item = s->bitmaps; item; item = g_slist_next(item)) {
692         LoadBitmapState *b = item->data;
693 
694         if (b->bitmap == s->bitmap) {
695             b->migrated = true;
696             if (s->before_vm_start_handled) {
697                 s->bitmaps = g_slist_remove(s->bitmaps, b);
698                 g_free(b);
699             }
700             break;
701         }
702     }
703 }
704 
705 static int dirty_bitmap_load_bits(QEMUFile *f, DBMLoadState *s)
706 {
707     uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
708     uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
709     trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
710                                        nr_bytes >> BDRV_SECTOR_BITS);
711 
712     if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
713         trace_dirty_bitmap_load_bits_zeroes();
714         if (!s->cancelled) {
715             bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte,
716                                                  nr_bytes, false);
717         }
718     } else {
719         size_t ret;
720         g_autofree uint8_t *buf = NULL;
721         uint64_t buf_size = qemu_get_be64(f);
722         uint64_t needed_size;
723 
724         /*
725          * The actual check for buf_size is done a bit later. We can't do it in
726          * cancelled mode as we don't have the bitmap to check the constraints
727          * (so, we allocate a buffer and read prior to the check). On the other
728          * hand, we shouldn't blindly g_malloc the number from the stream.
729          * Actually one chunk should not be larger than CHUNK_SIZE. Let's allow
730          * a bit larger (which means that bitmap migration will fail anyway and
731          * the whole migration will most probably fail soon due to broken
732          * stream).
733          */
734         if (buf_size > 10 * CHUNK_SIZE) {
735             error_report("Bitmap migration stream buffer allocation request "
736                          "is too large");
737             return -EIO;
738         }
739 
740         buf = g_malloc(buf_size);
741         ret = qemu_get_buffer(f, buf, buf_size);
742         if (ret != buf_size) {
743             error_report("Failed to read bitmap bits");
744             return -EIO;
745         }
746 
747         if (s->cancelled) {
748             return 0;
749         }
750 
751         needed_size = bdrv_dirty_bitmap_serialization_size(s->bitmap,
752                                                            first_byte,
753                                                            nr_bytes);
754 
755         if (needed_size > buf_size ||
756             buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
757              /* Here used same alignment as in send_bitmap_bits */
758         ) {
759             error_report("Migrated bitmap granularity doesn't "
760                          "match the destination bitmap '%s' granularity",
761                          bdrv_dirty_bitmap_name(s->bitmap));
762             cancel_incoming_locked(s);
763             return 0;
764         }
765 
766         bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
767                                            false);
768     }
769 
770     return 0;
771 }
772 
773 static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s)
774 {
775     Error *local_err = NULL;
776     bool nothing;
777     s->flags = qemu_get_bitmap_flags(f);
778     trace_dirty_bitmap_load_header(s->flags);
779 
780     nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
781 
782     if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
783         if (!qemu_get_counted_string(f, s->node_name)) {
784             error_report("Unable to read node name string");
785             return -EINVAL;
786         }
787         if (!s->cancelled) {
788             s->bs = bdrv_lookup_bs(s->node_name, s->node_name, &local_err);
789             if (!s->bs) {
790                 error_report_err(local_err);
791                 cancel_incoming_locked(s);
792             }
793         }
794     } else if (!s->bs && !nothing && !s->cancelled) {
795         error_report("Error: block device name is not set");
796         cancel_incoming_locked(s);
797     }
798 
799     if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
800         if (!qemu_get_counted_string(f, s->bitmap_name)) {
801             error_report("Unable to read bitmap name string");
802             return -EINVAL;
803         }
804         if (!s->cancelled) {
805             s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
806 
807             /*
808              * bitmap may be NULL here, it wouldn't be an error if it is the
809              * first occurrence of the bitmap
810              */
811             if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
812                 error_report("Error: unknown dirty bitmap "
813                              "'%s' for block device '%s'",
814                              s->bitmap_name, s->node_name);
815                 cancel_incoming_locked(s);
816             }
817         }
818     } else if (!s->bitmap && !nothing && !s->cancelled) {
819         error_report("Error: block device name is not set");
820         cancel_incoming_locked(s);
821     }
822 
823     return 0;
824 }
825 
826 /*
827  * dirty_bitmap_load
828  *
829  * Load sequence of dirty bitmap chunks. Return error only on fatal io stream
830  * violations. On other errors just cancel bitmaps incoming migration and return
831  * 0.
832  *
833  * Note, than when incoming bitmap migration is canceled, we still must read all
834  * our chunks (and just ignore them), to not affect other migration objects.
835  */
836 static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
837 {
838     DBMLoadState *s = &((DBMState *)opaque)->load;
839     int ret = 0;
840 
841     trace_dirty_bitmap_load_enter();
842 
843     if (version_id != 1) {
844         QEMU_LOCK_GUARD(&s->lock);
845         cancel_incoming_locked(s);
846         return -EINVAL;
847     }
848 
849     do {
850         QEMU_LOCK_GUARD(&s->lock);
851 
852         ret = dirty_bitmap_load_header(f, s);
853         if (ret < 0) {
854             cancel_incoming_locked(s);
855             return ret;
856         }
857 
858         if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) {
859             ret = dirty_bitmap_load_start(f, s);
860         } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
861             dirty_bitmap_load_complete(f, s);
862         } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
863             ret = dirty_bitmap_load_bits(f, s);
864         }
865 
866         if (!ret) {
867             ret = qemu_file_get_error(f);
868         }
869 
870         if (ret) {
871             cancel_incoming_locked(s);
872             return ret;
873         }
874     } while (!(s->flags & DIRTY_BITMAP_MIG_FLAG_EOS));
875 
876     trace_dirty_bitmap_load_success();
877     return 0;
878 }
879 
880 static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
881 {
882     DBMSaveState *s = &((DBMState *)opaque)->save;
883     SaveBitmapState *dbms = NULL;
884     if (init_dirty_bitmap_migration(s) < 0) {
885         return -1;
886     }
887 
888     QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
889         send_bitmap_start(f, s, dbms);
890     }
891     qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
892 
893     return 0;
894 }
895 
896 static bool dirty_bitmap_is_active(void *opaque)
897 {
898     DBMSaveState *s = &((DBMState *)opaque)->save;
899 
900     return migrate_dirty_bitmaps() && !s->no_bitmaps;
901 }
902 
903 static bool dirty_bitmap_is_active_iterate(void *opaque)
904 {
905     return dirty_bitmap_is_active(opaque) && !runstate_is_running();
906 }
907 
908 static bool dirty_bitmap_has_postcopy(void *opaque)
909 {
910     return true;
911 }
912 
913 static SaveVMHandlers savevm_dirty_bitmap_handlers = {
914     .save_setup = dirty_bitmap_save_setup,
915     .save_live_complete_postcopy = dirty_bitmap_save_complete,
916     .save_live_complete_precopy = dirty_bitmap_save_complete,
917     .has_postcopy = dirty_bitmap_has_postcopy,
918     .save_live_pending = dirty_bitmap_save_pending,
919     .save_live_iterate = dirty_bitmap_save_iterate,
920     .is_active_iterate = dirty_bitmap_is_active_iterate,
921     .load_state = dirty_bitmap_load,
922     .save_cleanup = dirty_bitmap_save_cleanup,
923     .is_active = dirty_bitmap_is_active,
924 };
925 
926 void dirty_bitmap_mig_init(void)
927 {
928     QSIMPLEQ_INIT(&dbm_state.save.dbms_list);
929     qemu_mutex_init(&dbm_state.load.lock);
930 
931     register_savevm_live("dirty-bitmap", 0, 1,
932                          &savevm_dirty_bitmap_handlers,
933                          &dbm_state);
934 }
935