meson: convert hw/block
[qemu/ar7.git] / migration / block-dirty-bitmap.c
blob784330ebe130a5ed7605862b38ff8bc00cda19af
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 name size ] \ flags & DEVICE_NAME
33 * [ n bytes: node name ] /
34 * [ 1 byte: bitmap name size ] \ flags & BITMAP_NAME
35 * [ n bytes: bitmap name ] /
37 * # Start of bitmap migration (flags & START)
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 "qapi/error.h"
76 #include "trace.h"
78 #define CHUNK_SIZE (1 << 10)
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
88 #define DIRTY_BITMAP_MIG_FLAG_EOS 0x01
89 #define DIRTY_BITMAP_MIG_FLAG_ZEROES 0x02
90 #define DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME 0x04
91 #define DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME 0x08
92 #define DIRTY_BITMAP_MIG_FLAG_START 0x10
93 #define DIRTY_BITMAP_MIG_FLAG_COMPLETE 0x20
94 #define DIRTY_BITMAP_MIG_FLAG_BITS 0x40
96 #define DIRTY_BITMAP_MIG_EXTRA_FLAGS 0x80
98 #define DIRTY_BITMAP_MIG_START_FLAG_ENABLED 0x01
99 #define DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT 0x02
100 /* 0x04 was "AUTOLOAD" flags on older versions, now it is ignored */
101 #define DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK 0xf8
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;
114 /* For bulk phase. */
115 bool bulk_completed;
116 uint64_t cur_sector;
117 } SaveBitmapState;
119 /* State of the dirty bitmap migration (DBM) during save process */
120 typedef struct DBMSaveState {
121 QSIMPLEQ_HEAD(, SaveBitmapState) dbms_list;
123 bool bulk_completed;
124 bool no_bitmaps;
126 /* for send_bitmap_bits() */
127 BlockDriverState *prev_bs;
128 BdrvDirtyBitmap *prev_bitmap;
129 } DBMSaveState;
131 typedef struct LoadBitmapState {
132 BlockDriverState *bs;
133 BdrvDirtyBitmap *bitmap;
134 bool migrated;
135 bool enabled;
136 } LoadBitmapState;
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;
146 bool before_vm_start_handled; /* set in dirty_bitmap_mig_before_vm_start */
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.
155 bool cancelled;
157 GSList *bitmaps;
158 QemuMutex lock; /* protect bitmaps */
159 } DBMLoadState;
161 typedef struct DBMState {
162 DBMSaveState save;
163 DBMLoadState load;
164 } DBMState;
166 static DBMState dbm_state;
168 static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
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);
178 return flags;
181 static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
183 /* The code currently does not send flags as more than one byte */
184 assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));
186 qemu_put_byte(f, flags);
189 static void send_bitmap_header(QEMUFile *f, DBMSaveState *s,
190 SaveBitmapState *dbms, uint32_t additional_flags)
192 BlockDriverState *bs = dbms->bs;
193 BdrvDirtyBitmap *bitmap = dbms->bitmap;
194 uint32_t flags = additional_flags;
195 trace_send_bitmap_header_enter();
197 if (bs != s->prev_bs) {
198 s->prev_bs = bs;
199 flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
202 if (bitmap != s->prev_bitmap) {
203 s->prev_bitmap = bitmap;
204 flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
207 qemu_put_bitmap_flags(f, flags);
209 if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
210 qemu_put_counted_string(f, dbms->node_name);
213 if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
214 qemu_put_counted_string(f, bdrv_dirty_bitmap_name(bitmap));
218 static void send_bitmap_start(QEMUFile *f, DBMSaveState *s,
219 SaveBitmapState *dbms)
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);
226 static void send_bitmap_complete(QEMUFile *f, DBMSaveState *s,
227 SaveBitmapState *dbms)
229 send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
232 static void send_bitmap_bits(QEMUFile *f, DBMSaveState *s,
233 SaveBitmapState *dbms,
234 uint64_t start_sector, uint32_t nr_sectors)
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;
246 bdrv_dirty_bitmap_serialize_part(
247 dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
248 (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
250 if (buffer_is_zero(buf, buf_size)) {
251 g_free(buf);
252 buf = NULL;
253 flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
256 trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);
258 send_bitmap_header(f, s, dbms, flags);
260 qemu_put_be64(f, start_sector);
261 qemu_put_be32(f, nr_sectors);
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);
273 g_free(buf);
276 /* Called with iothread lock taken. */
277 static void dirty_bitmap_do_save_cleanup(DBMSaveState *s)
279 SaveBitmapState *dbms;
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);
289 /* Called with iothread lock taken. */
290 static int add_bitmaps_to_list(DBMSaveState *s, BlockDriverState *bs,
291 const char *bs_name)
293 BdrvDirtyBitmap *bitmap;
294 SaveBitmapState *dbms;
295 Error *local_err = NULL;
297 FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
298 if (bdrv_dirty_bitmap_name(bitmap)) {
299 break;
302 if (!bitmap) {
303 return 0;
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;
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;
319 FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
320 if (!bdrv_dirty_bitmap_name(bitmap)) {
321 continue;
324 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, &local_err)) {
325 error_report_err(local_err);
326 return -1;
329 bdrv_ref(bs);
330 bdrv_dirty_bitmap_set_busy(bitmap, true);
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;
342 if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
343 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
346 QSIMPLEQ_INSERT_TAIL(&s->dbms_list, dbms, entry);
349 return 0;
352 /* Called with iothread lock taken. */
353 static int init_dirty_bitmap_migration(DBMSaveState *s)
355 BlockDriverState *bs;
356 SaveBitmapState *dbms;
357 GHashTable *handled_by_blk = g_hash_table_new(NULL, NULL);
358 BlockBackend *blk;
360 s->bulk_completed = false;
361 s->prev_bs = NULL;
362 s->prev_bitmap = NULL;
363 s->no_bitmaps = false;
366 * Use blockdevice name for direct (or filtered) children of named block
367 * backends.
369 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
370 const char *name = blk_name(blk);
372 if (!name || strcmp(name, "") == 0) {
373 continue;
376 bs = blk_bs(blk);
378 /* Skip filters without bitmaps */
379 while (bs && bs->drv && bs->drv->is_filter &&
380 !bdrv_has_named_bitmaps(bs))
382 if (bs->backing) {
383 bs = bs->backing->bs;
384 } else if (bs->file) {
385 bs = bs->file->bs;
386 } else {
387 bs = NULL;
391 if (bs && bs->drv && !bs->drv->is_filter) {
392 if (add_bitmaps_to_list(s, bs, name)) {
393 goto fail;
395 g_hash_table_add(handled_by_blk, bs);
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;
404 if (add_bitmaps_to_list(s, bs, bdrv_get_node_name(bs))) {
405 goto fail;
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);
414 if (QSIMPLEQ_EMPTY(&s->dbms_list)) {
415 s->no_bitmaps = true;
418 g_hash_table_destroy(handled_by_blk);
420 return 0;
422 fail:
423 g_hash_table_destroy(handled_by_blk);
424 dirty_bitmap_do_save_cleanup(s);
426 return -1;
429 /* Called with no lock taken. */
430 static void bulk_phase_send_chunk(QEMUFile *f, DBMSaveState *s,
431 SaveBitmapState *dbms)
433 uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
434 dbms->sectors_per_chunk);
436 send_bitmap_bits(f, s, dbms, dbms->cur_sector, nr_sectors);
438 dbms->cur_sector += nr_sectors;
439 if (dbms->cur_sector >= dbms->total_sectors) {
440 dbms->bulk_completed = true;
444 /* Called with no lock taken. */
445 static void bulk_phase(QEMUFile *f, DBMSaveState *s, bool limit)
447 SaveBitmapState *dbms;
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;
458 s->bulk_completed = true;
461 /* for SaveVMHandlers */
462 static void dirty_bitmap_save_cleanup(void *opaque)
464 DBMSaveState *s = &((DBMState *)opaque)->save;
466 dirty_bitmap_do_save_cleanup(s);
469 static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
471 DBMSaveState *s = &((DBMState *)opaque)->save;
473 trace_dirty_bitmap_save_iterate(migration_in_postcopy());
475 if (migration_in_postcopy() && !s->bulk_completed) {
476 bulk_phase(f, s, true);
479 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
481 return s->bulk_completed;
484 /* Called with iothread lock taken. */
486 static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
488 DBMSaveState *s = &((DBMState *)opaque)->save;
489 SaveBitmapState *dbms;
490 trace_dirty_bitmap_save_complete_enter();
492 if (!s->bulk_completed) {
493 bulk_phase(f, s, false);
496 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
497 send_bitmap_complete(f, s, dbms);
500 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
502 trace_dirty_bitmap_save_complete_finish();
504 dirty_bitmap_save_cleanup(opaque);
505 return 0;
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)
514 DBMSaveState *s = &((DBMState *)opaque)->save;
515 SaveBitmapState *dbms;
516 uint64_t pending = 0;
518 qemu_mutex_lock_iothread();
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;
525 pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
528 qemu_mutex_unlock_iothread();
530 trace_dirty_bitmap_save_pending(pending, max_size);
532 *res_postcopy_only += pending;
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)
538 Error *local_err = NULL;
539 uint32_t granularity = qemu_get_be32(f);
540 uint8_t flags = qemu_get_byte(f);
541 LoadBitmapState *b;
543 if (s->cancelled) {
544 return 0;
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;
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;
566 if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
567 bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
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;
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;
585 s->bitmaps = g_slist_prepend(s->bitmaps, b);
587 return 0;
591 * before_vm_start_handle_item
593 * g_slist_foreach helper
595 * item is LoadBitmapState*
596 * opaque is DBMLoadState*
598 static void before_vm_start_handle_item(void *item, void *opaque)
600 DBMLoadState *s = opaque;
601 LoadBitmapState *b = item;
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);
611 if (b->migrated) {
612 s->bitmaps = g_slist_remove(s->bitmaps, b);
613 g_free(b);
617 void dirty_bitmap_mig_before_vm_start(void)
619 DBMLoadState *s = &dbm_state.load;
620 qemu_mutex_lock(&s->lock);
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;
626 qemu_mutex_unlock(&s->lock);
629 static void cancel_incoming_locked(DBMLoadState *s)
631 GSList *item;
633 if (s->cancelled) {
634 return;
637 s->cancelled = true;
638 s->bs = NULL;
639 s->bitmap = NULL;
641 /* Drop all unfinished bitmaps */
642 for (item = s->bitmaps; item; item = g_slist_next(item)) {
643 LoadBitmapState *b = item->data;
646 * Bitmap must be unfinished, as finished bitmaps should already be
647 * removed from the list.
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);
653 bdrv_release_dirty_bitmap(b->bitmap);
656 g_slist_free_full(s->bitmaps, g_free);
657 s->bitmaps = NULL;
660 void dirty_bitmap_mig_cancel_outgoing(void)
662 dirty_bitmap_do_save_cleanup(&dbm_state.save);
665 void dirty_bitmap_mig_cancel_incoming(void)
667 DBMLoadState *s = &dbm_state.load;
669 qemu_mutex_lock(&s->lock);
671 cancel_incoming_locked(s);
673 qemu_mutex_unlock(&s->lock);
676 static void dirty_bitmap_load_complete(QEMUFile *f, DBMLoadState *s)
678 GSList *item;
679 trace_dirty_bitmap_load_complete();
681 if (s->cancelled) {
682 return;
685 bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
687 if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
688 bdrv_reclaim_dirty_bitmap(s->bitmap, &error_abort);
691 for (item = s->bitmaps; item; item = g_slist_next(item)) {
692 LoadBitmapState *b = item->data;
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);
700 break;
705 static int dirty_bitmap_load_bits(QEMUFile *f, DBMLoadState *s)
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);
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);
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;
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).
734 if (buf_size > 10 * CHUNK_SIZE) {
735 error_report("Bitmap migration stream buffer allocation request "
736 "is too large");
737 return -EIO;
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;
747 if (s->cancelled) {
748 return 0;
751 needed_size = bdrv_dirty_bitmap_serialization_size(s->bitmap,
752 first_byte,
753 nr_bytes);
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 */
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;
766 bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
767 false);
770 return 0;
773 static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s)
775 Error *local_err = NULL;
776 bool nothing;
777 s->flags = qemu_get_bitmap_flags(f);
778 trace_dirty_bitmap_load_header(s->flags);
780 nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
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;
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);
794 } else if (!s->bs && !nothing && !s->cancelled) {
795 error_report("Error: block device name is not set");
796 cancel_incoming_locked(s);
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;
804 if (!s->cancelled) {
805 s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
808 * bitmap may be NULL here, it wouldn't be an error if it is the
809 * first occurrence of the bitmap
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);
818 } else if (!s->bitmap && !nothing && !s->cancelled) {
819 error_report("Error: block device name is not set");
820 cancel_incoming_locked(s);
823 return 0;
827 * dirty_bitmap_load
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.
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.
836 static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
838 DBMLoadState *s = &((DBMState *)opaque)->load;
839 int ret = 0;
841 trace_dirty_bitmap_load_enter();
843 if (version_id != 1) {
844 QEMU_LOCK_GUARD(&s->lock);
845 cancel_incoming_locked(s);
846 return -EINVAL;
849 do {
850 QEMU_LOCK_GUARD(&s->lock);
852 ret = dirty_bitmap_load_header(f, s);
853 if (ret < 0) {
854 cancel_incoming_locked(s);
855 return ret;
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);
866 if (!ret) {
867 ret = qemu_file_get_error(f);
870 if (ret) {
871 cancel_incoming_locked(s);
872 return ret;
874 } while (!(s->flags & DIRTY_BITMAP_MIG_FLAG_EOS));
876 trace_dirty_bitmap_load_success();
877 return 0;
880 static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
882 DBMSaveState *s = &((DBMState *)opaque)->save;
883 SaveBitmapState *dbms = NULL;
884 if (init_dirty_bitmap_migration(s) < 0) {
885 return -1;
888 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
889 send_bitmap_start(f, s, dbms);
891 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
893 return 0;
896 static bool dirty_bitmap_is_active(void *opaque)
898 DBMSaveState *s = &((DBMState *)opaque)->save;
900 return migrate_dirty_bitmaps() && !s->no_bitmaps;
903 static bool dirty_bitmap_is_active_iterate(void *opaque)
905 return dirty_bitmap_is_active(opaque) && !runstate_is_running();
908 static bool dirty_bitmap_has_postcopy(void *opaque)
910 return true;
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,
926 void dirty_bitmap_mig_init(void)
928 QSIMPLEQ_INIT(&dbm_state.save.dbms_list);
929 qemu_mutex_init(&dbm_state.load.lock);
931 register_savevm_live("dirty-bitmap", 0, 1,
932 &savevm_dirty_bitmap_handlers,
933 &dbm_state);