2 * Block dirty bitmap postcopy migration
4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
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.
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)
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)
49 * # Data chunk of bitmap migration
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
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"
78 #define CHUNK_SIZE (1 << 10)
80 /* Flags occupy one, two or four bytes (Big Endian). The size is determined as
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
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
;
114 /* For bulk phase. */
119 /* State of the dirty bitmap migration (DBM) during save process */
120 typedef struct DBMSaveState
{
121 QSIMPLEQ_HEAD(, SaveBitmapState
) dbms_list
;
126 /* for send_bitmap_bits() */
127 BlockDriverState
*prev_bs
;
128 BdrvDirtyBitmap
*prev_bitmap
;
131 typedef struct LoadBitmapState
{
132 BlockDriverState
*bs
;
133 BdrvDirtyBitmap
*bitmap
;
138 /* State of the dirty bitmap migration (DBM) during load process */
139 typedef struct DBMLoadState
{
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 */
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
158 QemuMutex lock
; /* protect bitmaps */
161 typedef struct 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
);
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
) {
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
)) {
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
) {
269 qemu_put_be64(f
, buf_size
);
270 qemu_put_buffer(f
, buf
, buf_size
);
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
);
289 /* Called with iothread lock taken. */
290 static int add_bitmaps_to_list(DBMSaveState
*s
, BlockDriverState
*bs
,
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
)) {
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
));
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
);
319 FOR_EACH_DIRTY_BITMAP(bs
, bitmap
) {
320 if (!bdrv_dirty_bitmap_name(bitmap
)) {
324 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_DEFAULT
, &local_err
)) {
325 error_report_err(local_err
);
330 bdrv_dirty_bitmap_set_busy(bitmap
, true);
332 dbms
= g_new0(SaveBitmapState
, 1);
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
);
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
);
360 s
->bulk_completed
= false;
362 s
->prev_bitmap
= NULL
;
363 s
->no_bitmaps
= false;
366 * Use blockdevice name for direct (or filtered) children of named block
369 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
370 const char *name
= blk_name(blk
);
372 if (!name
|| strcmp(name
, "") == 0) {
378 /* Skip filters without bitmaps */
379 while (bs
&& bs
->drv
&& bs
->drv
->is_filter
&&
380 !bdrv_has_named_bitmaps(bs
))
383 bs
= bs
->backing
->bs
;
384 } else if (bs
->file
) {
391 if (bs
&& bs
->drv
&& !bs
->drv
->is_filter
) {
392 if (add_bitmaps_to_list(s
, bs
, name
)) {
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
)) {
404 if (add_bitmaps_to_list(s
, bs
, bdrv_get_node_name(bs
))) {
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
);
423 g_hash_table_destroy(handled_by_blk
);
424 dirty_bitmap_do_save_cleanup(s
);
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
)) {
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
);
508 static void dirty_bitmap_save_pending(QEMUFile
*f
, void *opaque
,
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
);
548 error_report("Bitmap with the same name ('%s') already exists on "
549 "destination", bdrv_dirty_bitmap_name(s
->bitmap
));
552 s
->bitmap
= bdrv_create_dirty_bitmap(s
->bs
, granularity
,
553 s
->bitmap_name
, &local_err
);
555 error_report_err(local_err
);
560 if (flags
& DIRTY_BITMAP_MIG_START_FLAG_RESERVED_MASK
) {
561 error_report("Unknown flags in migrated dirty bitmap header: %x",
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
);
574 error_report_err(local_err
);
579 b
= g_new(LoadBitmapState
, 1);
581 b
->bitmap
= s
->bitmap
;
583 b
->enabled
= flags
& DIRTY_BITMAP_MIG_START_FLAG_ENABLED
;
585 s
->bitmaps
= g_slist_prepend(s
->bitmaps
, b
);
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
;
605 bdrv_enable_dirty_bitmap(b
->bitmap
);
607 bdrv_dirty_bitmap_enable_successor(b
->bitmap
);
612 s
->bitmaps
= g_slist_remove(s
->bitmaps
, 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
)
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
);
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
)
679 trace_dirty_bitmap_load_complete();
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
) {
696 if (s
->before_vm_start_handled
) {
697 s
->bitmaps
= g_slist_remove(s
->bitmaps
, b
);
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();
715 bdrv_dirty_bitmap_deserialize_zeroes(s
->bitmap
, first_byte
,
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
734 if (buf_size
> 10 * CHUNK_SIZE
) {
735 error_report("Bitmap migration stream buffer allocation request "
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");
751 needed_size
= bdrv_dirty_bitmap_serialization_size(s
->bitmap
,
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
);
766 bdrv_dirty_bitmap_deserialize_part(s
->bitmap
, buf
, first_byte
, nr_bytes
,
773 static int dirty_bitmap_load_header(QEMUFile
*f
, DBMLoadState
*s
)
775 Error
*local_err
= NULL
;
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");
788 s
->bs
= bdrv_lookup_bs(s
->node_name
, s
->node_name
, &local_err
);
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");
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
);
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
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
;
841 trace_dirty_bitmap_load_enter();
843 if (version_id
!= 1) {
844 QEMU_LOCK_GUARD(&s
->lock
);
845 cancel_incoming_locked(s
);
850 QEMU_LOCK_GUARD(&s
->lock
);
852 ret
= dirty_bitmap_load_header(f
, s
);
854 cancel_incoming_locked(s
);
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
);
867 ret
= qemu_file_get_error(f
);
871 cancel_incoming_locked(s
);
874 } while (!(s
->flags
& DIRTY_BITMAP_MIG_FLAG_EOS
));
876 trace_dirty_bitmap_load_success();
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) {
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
);
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
)
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
,