migration/block-dirty-bitmap: rename finish_lock to just lock
[qemu.git] / migration / block-dirty-bitmap.c
blob9194807b54f1325585659349464ce17978aec74a
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 elder versions, no 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 } LoadBitmapState;
137 /* State of the dirty bitmap migration (DBM) during load process */
138 typedef struct DBMLoadState {
139 uint32_t flags;
140 char node_name[256];
141 char bitmap_name[256];
142 BlockDriverState *bs;
143 BdrvDirtyBitmap *bitmap;
145 GSList *enabled_bitmaps;
146 QemuMutex lock; /* protect enabled_bitmaps */
147 } DBMLoadState;
149 typedef struct DBMState {
150 DBMSaveState save;
151 DBMLoadState load;
152 } DBMState;
154 static DBMState dbm_state;
156 static uint32_t qemu_get_bitmap_flags(QEMUFile *f)
158 uint8_t flags = qemu_get_byte(f);
159 if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
160 flags = flags << 8 | qemu_get_byte(f);
161 if (flags & DIRTY_BITMAP_MIG_EXTRA_FLAGS) {
162 flags = flags << 16 | qemu_get_be16(f);
166 return flags;
169 static void qemu_put_bitmap_flags(QEMUFile *f, uint32_t flags)
171 /* The code currently do not send flags more than one byte */
172 assert(!(flags & (0xffffff00 | DIRTY_BITMAP_MIG_EXTRA_FLAGS)));
174 qemu_put_byte(f, flags);
177 static void send_bitmap_header(QEMUFile *f, DBMSaveState *s,
178 SaveBitmapState *dbms, uint32_t additional_flags)
180 BlockDriverState *bs = dbms->bs;
181 BdrvDirtyBitmap *bitmap = dbms->bitmap;
182 uint32_t flags = additional_flags;
183 trace_send_bitmap_header_enter();
185 if (bs != s->prev_bs) {
186 s->prev_bs = bs;
187 flags |= DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME;
190 if (bitmap != s->prev_bitmap) {
191 s->prev_bitmap = bitmap;
192 flags |= DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME;
195 qemu_put_bitmap_flags(f, flags);
197 if (flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
198 qemu_put_counted_string(f, dbms->node_name);
201 if (flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
202 qemu_put_counted_string(f, bdrv_dirty_bitmap_name(bitmap));
206 static void send_bitmap_start(QEMUFile *f, DBMSaveState *s,
207 SaveBitmapState *dbms)
209 send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_START);
210 qemu_put_be32(f, bdrv_dirty_bitmap_granularity(dbms->bitmap));
211 qemu_put_byte(f, dbms->flags);
214 static void send_bitmap_complete(QEMUFile *f, DBMSaveState *s,
215 SaveBitmapState *dbms)
217 send_bitmap_header(f, s, dbms, DIRTY_BITMAP_MIG_FLAG_COMPLETE);
220 static void send_bitmap_bits(QEMUFile *f, DBMSaveState *s,
221 SaveBitmapState *dbms,
222 uint64_t start_sector, uint32_t nr_sectors)
224 /* align for buffer_is_zero() */
225 uint64_t align = 4 * sizeof(long);
226 uint64_t unaligned_size =
227 bdrv_dirty_bitmap_serialization_size(
228 dbms->bitmap, start_sector << BDRV_SECTOR_BITS,
229 (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
230 uint64_t buf_size = QEMU_ALIGN_UP(unaligned_size, align);
231 uint8_t *buf = g_malloc0(buf_size);
232 uint32_t flags = DIRTY_BITMAP_MIG_FLAG_BITS;
234 bdrv_dirty_bitmap_serialize_part(
235 dbms->bitmap, buf, start_sector << BDRV_SECTOR_BITS,
236 (uint64_t)nr_sectors << BDRV_SECTOR_BITS);
238 if (buffer_is_zero(buf, buf_size)) {
239 g_free(buf);
240 buf = NULL;
241 flags |= DIRTY_BITMAP_MIG_FLAG_ZEROES;
244 trace_send_bitmap_bits(flags, start_sector, nr_sectors, buf_size);
246 send_bitmap_header(f, s, dbms, flags);
248 qemu_put_be64(f, start_sector);
249 qemu_put_be32(f, nr_sectors);
251 /* if a block is zero we need to flush here since the network
252 * bandwidth is now a lot higher than the storage device bandwidth.
253 * thus if we queue zero blocks we slow down the migration. */
254 if (flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
255 qemu_fflush(f);
256 } else {
257 qemu_put_be64(f, buf_size);
258 qemu_put_buffer(f, buf, buf_size);
261 g_free(buf);
264 /* Called with iothread lock taken. */
265 static void dirty_bitmap_do_save_cleanup(DBMSaveState *s)
267 SaveBitmapState *dbms;
269 while ((dbms = QSIMPLEQ_FIRST(&s->dbms_list)) != NULL) {
270 QSIMPLEQ_REMOVE_HEAD(&s->dbms_list, entry);
271 bdrv_dirty_bitmap_set_busy(dbms->bitmap, false);
272 bdrv_unref(dbms->bs);
273 g_free(dbms);
277 /* Called with iothread lock taken. */
278 static int add_bitmaps_to_list(DBMSaveState *s, BlockDriverState *bs,
279 const char *bs_name)
281 BdrvDirtyBitmap *bitmap;
282 SaveBitmapState *dbms;
283 Error *local_err = NULL;
285 FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
286 if (bdrv_dirty_bitmap_name(bitmap)) {
287 break;
290 if (!bitmap) {
291 return 0;
294 if (!bs_name || strcmp(bs_name, "") == 0) {
295 error_report("Bitmap '%s' in unnamed node can't be migrated",
296 bdrv_dirty_bitmap_name(bitmap));
297 return -1;
300 if (bs_name[0] == '#') {
301 error_report("Bitmap '%s' in a node with auto-generated "
302 "name '%s' can't be migrated",
303 bdrv_dirty_bitmap_name(bitmap), bs_name);
304 return -1;
307 FOR_EACH_DIRTY_BITMAP(bs, bitmap) {
308 if (!bdrv_dirty_bitmap_name(bitmap)) {
309 continue;
312 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, &local_err)) {
313 error_report_err(local_err);
314 return -1;
317 bdrv_ref(bs);
318 bdrv_dirty_bitmap_set_busy(bitmap, true);
320 dbms = g_new0(SaveBitmapState, 1);
321 dbms->bs = bs;
322 dbms->node_name = bs_name;
323 dbms->bitmap = bitmap;
324 dbms->total_sectors = bdrv_nb_sectors(bs);
325 dbms->sectors_per_chunk = CHUNK_SIZE * 8 *
326 bdrv_dirty_bitmap_granularity(bitmap) >> BDRV_SECTOR_BITS;
327 if (bdrv_dirty_bitmap_enabled(bitmap)) {
328 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_ENABLED;
330 if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
331 dbms->flags |= DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT;
334 QSIMPLEQ_INSERT_TAIL(&s->dbms_list, dbms, entry);
337 return 0;
340 /* Called with iothread lock taken. */
341 static int init_dirty_bitmap_migration(DBMSaveState *s)
343 BlockDriverState *bs;
344 SaveBitmapState *dbms;
345 GHashTable *handled_by_blk = g_hash_table_new(NULL, NULL);
346 BlockBackend *blk;
348 s->bulk_completed = false;
349 s->prev_bs = NULL;
350 s->prev_bitmap = NULL;
351 s->no_bitmaps = false;
354 * Use blockdevice name for direct (or filtered) children of named block
355 * backends.
357 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
358 const char *name = blk_name(blk);
360 if (!name || strcmp(name, "") == 0) {
361 continue;
364 bs = blk_bs(blk);
366 /* Skip filters without bitmaps */
367 while (bs && bs->drv && bs->drv->is_filter &&
368 !bdrv_has_named_bitmaps(bs))
370 if (bs->backing) {
371 bs = bs->backing->bs;
372 } else if (bs->file) {
373 bs = bs->file->bs;
374 } else {
375 bs = NULL;
379 if (bs && bs->drv && !bs->drv->is_filter) {
380 if (add_bitmaps_to_list(s, bs, name)) {
381 goto fail;
383 g_hash_table_add(handled_by_blk, bs);
387 for (bs = bdrv_next_all_states(NULL); bs; bs = bdrv_next_all_states(bs)) {
388 if (g_hash_table_contains(handled_by_blk, bs)) {
389 continue;
392 if (add_bitmaps_to_list(s, bs, bdrv_get_node_name(bs))) {
393 goto fail;
397 /* unset migration flags here, to not roll back it */
398 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
399 bdrv_dirty_bitmap_skip_store(dbms->bitmap, true);
402 if (QSIMPLEQ_EMPTY(&s->dbms_list)) {
403 s->no_bitmaps = true;
406 g_hash_table_destroy(handled_by_blk);
408 return 0;
410 fail:
411 g_hash_table_destroy(handled_by_blk);
412 dirty_bitmap_do_save_cleanup(s);
414 return -1;
417 /* Called with no lock taken. */
418 static void bulk_phase_send_chunk(QEMUFile *f, DBMSaveState *s,
419 SaveBitmapState *dbms)
421 uint32_t nr_sectors = MIN(dbms->total_sectors - dbms->cur_sector,
422 dbms->sectors_per_chunk);
424 send_bitmap_bits(f, s, dbms, dbms->cur_sector, nr_sectors);
426 dbms->cur_sector += nr_sectors;
427 if (dbms->cur_sector >= dbms->total_sectors) {
428 dbms->bulk_completed = true;
432 /* Called with no lock taken. */
433 static void bulk_phase(QEMUFile *f, DBMSaveState *s, bool limit)
435 SaveBitmapState *dbms;
437 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
438 while (!dbms->bulk_completed) {
439 bulk_phase_send_chunk(f, s, dbms);
440 if (limit && qemu_file_rate_limit(f)) {
441 return;
446 s->bulk_completed = true;
449 /* for SaveVMHandlers */
450 static void dirty_bitmap_save_cleanup(void *opaque)
452 DBMSaveState *s = &((DBMState *)opaque)->save;
454 dirty_bitmap_do_save_cleanup(s);
457 static int dirty_bitmap_save_iterate(QEMUFile *f, void *opaque)
459 DBMSaveState *s = &((DBMState *)opaque)->save;
461 trace_dirty_bitmap_save_iterate(migration_in_postcopy());
463 if (migration_in_postcopy() && !s->bulk_completed) {
464 bulk_phase(f, s, true);
467 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
469 return s->bulk_completed;
472 /* Called with iothread lock taken. */
474 static int dirty_bitmap_save_complete(QEMUFile *f, void *opaque)
476 DBMSaveState *s = &((DBMState *)opaque)->save;
477 SaveBitmapState *dbms;
478 trace_dirty_bitmap_save_complete_enter();
480 if (!s->bulk_completed) {
481 bulk_phase(f, s, false);
484 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
485 send_bitmap_complete(f, s, dbms);
488 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
490 trace_dirty_bitmap_save_complete_finish();
492 dirty_bitmap_save_cleanup(opaque);
493 return 0;
496 static void dirty_bitmap_save_pending(QEMUFile *f, void *opaque,
497 uint64_t max_size,
498 uint64_t *res_precopy_only,
499 uint64_t *res_compatible,
500 uint64_t *res_postcopy_only)
502 DBMSaveState *s = &((DBMState *)opaque)->save;
503 SaveBitmapState *dbms;
504 uint64_t pending = 0;
506 qemu_mutex_lock_iothread();
508 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
509 uint64_t gran = bdrv_dirty_bitmap_granularity(dbms->bitmap);
510 uint64_t sectors = dbms->bulk_completed ? 0 :
511 dbms->total_sectors - dbms->cur_sector;
513 pending += DIV_ROUND_UP(sectors * BDRV_SECTOR_SIZE, gran);
516 qemu_mutex_unlock_iothread();
518 trace_dirty_bitmap_save_pending(pending, max_size);
520 *res_postcopy_only += pending;
523 /* First occurrence of this bitmap. It should be created if doesn't exist */
524 static int dirty_bitmap_load_start(QEMUFile *f, DBMLoadState *s)
526 Error *local_err = NULL;
527 uint32_t granularity = qemu_get_be32(f);
528 uint8_t flags = qemu_get_byte(f);
530 if (s->bitmap) {
531 error_report("Bitmap with the same name ('%s') already exists on "
532 "destination", bdrv_dirty_bitmap_name(s->bitmap));
533 return -EINVAL;
534 } else {
535 s->bitmap = bdrv_create_dirty_bitmap(s->bs, granularity,
536 s->bitmap_name, &local_err);
537 if (!s->bitmap) {
538 error_report_err(local_err);
539 return -EINVAL;
543 if (flags & DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK) {
544 error_report("Unknown flags in migrated dirty bitmap header: %x",
545 flags);
546 return -EINVAL;
549 if (flags & DIRTY_BITMAP_MIG_START_FLAG_PERSISTENT) {
550 bdrv_dirty_bitmap_set_persistence(s->bitmap, true);
553 bdrv_disable_dirty_bitmap(s->bitmap);
554 if (flags & DIRTY_BITMAP_MIG_START_FLAG_ENABLED) {
555 LoadBitmapState *b;
557 bdrv_dirty_bitmap_create_successor(s->bitmap, &local_err);
558 if (local_err) {
559 error_report_err(local_err);
560 return -EINVAL;
563 b = g_new(LoadBitmapState, 1);
564 b->bs = s->bs;
565 b->bitmap = s->bitmap;
566 b->migrated = false;
567 s->enabled_bitmaps = g_slist_prepend(s->enabled_bitmaps, b);
570 return 0;
573 void dirty_bitmap_mig_before_vm_start(void)
575 DBMLoadState *s = &dbm_state.load;
576 GSList *item;
578 qemu_mutex_lock(&s->lock);
580 for (item = s->enabled_bitmaps; item; item = g_slist_next(item)) {
581 LoadBitmapState *b = item->data;
583 if (b->migrated) {
584 bdrv_enable_dirty_bitmap(b->bitmap);
585 } else {
586 bdrv_dirty_bitmap_enable_successor(b->bitmap);
589 g_free(b);
592 g_slist_free(s->enabled_bitmaps);
593 s->enabled_bitmaps = NULL;
595 qemu_mutex_unlock(&s->lock);
598 static void dirty_bitmap_load_complete(QEMUFile *f, DBMLoadState *s)
600 GSList *item;
601 trace_dirty_bitmap_load_complete();
602 bdrv_dirty_bitmap_deserialize_finish(s->bitmap);
604 qemu_mutex_lock(&s->lock);
606 for (item = s->enabled_bitmaps; item; item = g_slist_next(item)) {
607 LoadBitmapState *b = item->data;
609 if (b->bitmap == s->bitmap) {
610 b->migrated = true;
611 break;
615 if (bdrv_dirty_bitmap_has_successor(s->bitmap)) {
616 bdrv_dirty_bitmap_lock(s->bitmap);
617 if (s->enabled_bitmaps == NULL) {
618 /* in postcopy */
619 bdrv_reclaim_dirty_bitmap_locked(s->bitmap, &error_abort);
620 bdrv_enable_dirty_bitmap_locked(s->bitmap);
621 } else {
622 /* target not started, successor must be empty */
623 int64_t count = bdrv_get_dirty_count(s->bitmap);
624 BdrvDirtyBitmap *ret = bdrv_reclaim_dirty_bitmap_locked(s->bitmap,
625 NULL);
626 /* bdrv_reclaim_dirty_bitmap can fail only on no successor (it
627 * must be) or on merge fail, but merge can't fail when second
628 * bitmap is empty
630 assert(ret == s->bitmap &&
631 count == bdrv_get_dirty_count(s->bitmap));
633 bdrv_dirty_bitmap_unlock(s->bitmap);
636 qemu_mutex_unlock(&s->lock);
639 static int dirty_bitmap_load_bits(QEMUFile *f, DBMLoadState *s)
641 uint64_t first_byte = qemu_get_be64(f) << BDRV_SECTOR_BITS;
642 uint64_t nr_bytes = (uint64_t)qemu_get_be32(f) << BDRV_SECTOR_BITS;
643 trace_dirty_bitmap_load_bits_enter(first_byte >> BDRV_SECTOR_BITS,
644 nr_bytes >> BDRV_SECTOR_BITS);
646 if (s->flags & DIRTY_BITMAP_MIG_FLAG_ZEROES) {
647 trace_dirty_bitmap_load_bits_zeroes();
648 bdrv_dirty_bitmap_deserialize_zeroes(s->bitmap, first_byte, nr_bytes,
649 false);
650 } else {
651 size_t ret;
652 uint8_t *buf;
653 uint64_t buf_size = qemu_get_be64(f);
654 uint64_t needed_size =
655 bdrv_dirty_bitmap_serialization_size(s->bitmap,
656 first_byte, nr_bytes);
658 if (needed_size > buf_size ||
659 buf_size > QEMU_ALIGN_UP(needed_size, 4 * sizeof(long))
660 /* Here used same alignment as in send_bitmap_bits */
662 error_report("Migrated bitmap granularity doesn't "
663 "match the destination bitmap '%s' granularity",
664 bdrv_dirty_bitmap_name(s->bitmap));
665 return -EINVAL;
668 buf = g_malloc(buf_size);
669 ret = qemu_get_buffer(f, buf, buf_size);
670 if (ret != buf_size) {
671 error_report("Failed to read bitmap bits");
672 g_free(buf);
673 return -EIO;
676 bdrv_dirty_bitmap_deserialize_part(s->bitmap, buf, first_byte, nr_bytes,
677 false);
678 g_free(buf);
681 return 0;
684 static int dirty_bitmap_load_header(QEMUFile *f, DBMLoadState *s)
686 Error *local_err = NULL;
687 bool nothing;
688 s->flags = qemu_get_bitmap_flags(f);
689 trace_dirty_bitmap_load_header(s->flags);
691 nothing = s->flags == (s->flags & DIRTY_BITMAP_MIG_FLAG_EOS);
693 if (s->flags & DIRTY_BITMAP_MIG_FLAG_DEVICE_NAME) {
694 if (!qemu_get_counted_string(f, s->node_name)) {
695 error_report("Unable to read node name string");
696 return -EINVAL;
698 s->bs = bdrv_lookup_bs(s->node_name, s->node_name, &local_err);
699 if (!s->bs) {
700 error_report_err(local_err);
701 return -EINVAL;
703 } else if (!s->bs && !nothing) {
704 error_report("Error: block device name is not set");
705 return -EINVAL;
708 if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITMAP_NAME) {
709 if (!qemu_get_counted_string(f, s->bitmap_name)) {
710 error_report("Unable to read bitmap name string");
711 return -EINVAL;
713 s->bitmap = bdrv_find_dirty_bitmap(s->bs, s->bitmap_name);
715 /* bitmap may be NULL here, it wouldn't be an error if it is the
716 * first occurrence of the bitmap */
717 if (!s->bitmap && !(s->flags & DIRTY_BITMAP_MIG_FLAG_START)) {
718 error_report("Error: unknown dirty bitmap "
719 "'%s' for block device '%s'",
720 s->bitmap_name, s->node_name);
721 return -EINVAL;
723 } else if (!s->bitmap && !nothing) {
724 error_report("Error: block device name is not set");
725 return -EINVAL;
728 return 0;
731 static int dirty_bitmap_load(QEMUFile *f, void *opaque, int version_id)
733 DBMLoadState *s = &((DBMState *)opaque)->load;
734 int ret = 0;
736 trace_dirty_bitmap_load_enter();
738 if (version_id != 1) {
739 return -EINVAL;
742 do {
743 ret = dirty_bitmap_load_header(f, s);
744 if (ret < 0) {
745 return ret;
748 if (s->flags & DIRTY_BITMAP_MIG_FLAG_START) {
749 ret = dirty_bitmap_load_start(f, s);
750 } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_COMPLETE) {
751 dirty_bitmap_load_complete(f, s);
752 } else if (s->flags & DIRTY_BITMAP_MIG_FLAG_BITS) {
753 ret = dirty_bitmap_load_bits(f, s);
756 if (!ret) {
757 ret = qemu_file_get_error(f);
760 if (ret) {
761 return ret;
763 } while (!(s->flags & DIRTY_BITMAP_MIG_FLAG_EOS));
765 trace_dirty_bitmap_load_success();
766 return 0;
769 static int dirty_bitmap_save_setup(QEMUFile *f, void *opaque)
771 DBMSaveState *s = &((DBMState *)opaque)->save;
772 SaveBitmapState *dbms = NULL;
773 if (init_dirty_bitmap_migration(s) < 0) {
774 return -1;
777 QSIMPLEQ_FOREACH(dbms, &s->dbms_list, entry) {
778 send_bitmap_start(f, s, dbms);
780 qemu_put_bitmap_flags(f, DIRTY_BITMAP_MIG_FLAG_EOS);
782 return 0;
785 static bool dirty_bitmap_is_active(void *opaque)
787 DBMSaveState *s = &((DBMState *)opaque)->save;
789 return migrate_dirty_bitmaps() && !s->no_bitmaps;
792 static bool dirty_bitmap_is_active_iterate(void *opaque)
794 return dirty_bitmap_is_active(opaque) && !runstate_is_running();
797 static bool dirty_bitmap_has_postcopy(void *opaque)
799 return true;
802 static SaveVMHandlers savevm_dirty_bitmap_handlers = {
803 .save_setup = dirty_bitmap_save_setup,
804 .save_live_complete_postcopy = dirty_bitmap_save_complete,
805 .save_live_complete_precopy = dirty_bitmap_save_complete,
806 .has_postcopy = dirty_bitmap_has_postcopy,
807 .save_live_pending = dirty_bitmap_save_pending,
808 .save_live_iterate = dirty_bitmap_save_iterate,
809 .is_active_iterate = dirty_bitmap_is_active_iterate,
810 .load_state = dirty_bitmap_load,
811 .save_cleanup = dirty_bitmap_save_cleanup,
812 .is_active = dirty_bitmap_is_active,
815 void dirty_bitmap_mig_init(void)
817 QSIMPLEQ_INIT(&dbm_state.save.dbms_list);
818 qemu_mutex_init(&dbm_state.load.lock);
820 register_savevm_live("dirty-bitmap", 0, 1,
821 &savevm_dirty_bitmap_handlers,
822 &dbm_state);