4 * Copyright (c) 2016-2017 Red Hat. Inc
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
27 #include "block/block_int.h"
28 #include "block/blockjob.h"
30 struct BdrvDirtyBitmap
{
32 HBitmap
*bitmap
; /* Dirty bitmap implementation */
33 HBitmap
*meta
; /* Meta dirty bitmap */
34 bool busy
; /* Bitmap is busy, it can't be used via QMP */
35 BdrvDirtyBitmap
*successor
; /* Anonymous child, if any. */
36 char *name
; /* Optional non-empty unique ID */
37 int64_t size
; /* Size of the bitmap, in bytes */
38 bool disabled
; /* Bitmap is disabled. It ignores all writes to
40 int active_iterators
; /* How many iterators are active */
41 bool readonly
; /* Bitmap is read-only. This field also
42 prevents the respective image from being
43 modified (i.e. blocks writes and discards).
44 Such operations must fail and both the image
45 and this bitmap must remain unchanged while
47 bool persistent
; /* bitmap must be saved to owner disk image */
48 bool inconsistent
; /* bitmap is persistent, but inconsistent.
49 It cannot be used at all in any way, except
50 a QMP user can remove it. */
51 bool migration
; /* Bitmap is selected for migration, it should
52 not be stored on the next inactivation
53 (persistent flag doesn't matter until next
55 QLIST_ENTRY(BdrvDirtyBitmap
) list
;
58 struct BdrvDirtyBitmapIter
{
60 BdrvDirtyBitmap
*bitmap
;
63 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState
*bs
)
65 qemu_mutex_lock(&bs
->dirty_bitmap_mutex
);
68 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState
*bs
)
70 qemu_mutex_unlock(&bs
->dirty_bitmap_mutex
);
73 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap
*bitmap
)
75 qemu_mutex_lock(bitmap
->mutex
);
78 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap
*bitmap
)
80 qemu_mutex_unlock(bitmap
->mutex
);
83 /* Called with BQL or dirty_bitmap lock taken. */
84 BdrvDirtyBitmap
*bdrv_find_dirty_bitmap(BlockDriverState
*bs
, const char *name
)
89 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
90 if (bm
->name
&& !strcmp(name
, bm
->name
)) {
97 /* Called with BQL taken. */
98 BdrvDirtyBitmap
*bdrv_create_dirty_bitmap(BlockDriverState
*bs
,
104 BdrvDirtyBitmap
*bitmap
;
106 assert(is_power_of_2(granularity
) && granularity
>= BDRV_SECTOR_SIZE
);
108 if (name
&& bdrv_find_dirty_bitmap(bs
, name
)) {
109 error_setg(errp
, "Bitmap already exists: %s", name
);
112 bitmap_size
= bdrv_getlength(bs
);
113 if (bitmap_size
< 0) {
114 error_setg_errno(errp
, -bitmap_size
, "could not get length of device");
115 errno
= -bitmap_size
;
118 bitmap
= g_new0(BdrvDirtyBitmap
, 1);
119 bitmap
->mutex
= &bs
->dirty_bitmap_mutex
;
120 bitmap
->bitmap
= hbitmap_alloc(bitmap_size
, ctz32(granularity
));
121 bitmap
->size
= bitmap_size
;
122 bitmap
->name
= g_strdup(name
);
123 bitmap
->disabled
= false;
124 bdrv_dirty_bitmaps_lock(bs
);
125 QLIST_INSERT_HEAD(&bs
->dirty_bitmaps
, bitmap
, list
);
126 bdrv_dirty_bitmaps_unlock(bs
);
130 /* bdrv_create_meta_dirty_bitmap
132 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
133 * when a dirty status bit in @bitmap is changed (either from reset to set or
134 * the other way around), its respective meta dirty bitmap bit will be marked
137 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
138 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
141 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
144 assert(!bitmap
->meta
);
145 qemu_mutex_lock(bitmap
->mutex
);
146 bitmap
->meta
= hbitmap_create_meta(bitmap
->bitmap
,
147 chunk_size
* BITS_PER_BYTE
);
148 qemu_mutex_unlock(bitmap
->mutex
);
151 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
153 assert(bitmap
->meta
);
154 qemu_mutex_lock(bitmap
->mutex
);
155 hbitmap_free_meta(bitmap
->bitmap
);
157 qemu_mutex_unlock(bitmap
->mutex
);
160 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap
*bitmap
)
165 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap
*bitmap
)
170 /* Called with BQL taken. */
171 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap
*bitmap
)
173 return bitmap
->successor
;
176 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap
*bitmap
)
181 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap
*bitmap
, bool busy
)
183 qemu_mutex_lock(bitmap
->mutex
);
185 qemu_mutex_unlock(bitmap
->mutex
);
188 /* Called with BQL taken. */
189 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap
*bitmap
)
191 return !bitmap
->disabled
;
195 * bdrv_dirty_bitmap_status: This API is now deprecated.
196 * Called with BQL taken.
198 * A BdrvDirtyBitmap can be in four possible user-visible states:
199 * (1) Active: successor is NULL, and disabled is false: full r/w mode
200 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
201 * guest writes are dropped, but monitor writes are possible,
202 * through commands like merge and clear.
203 * (3) Frozen: successor is not NULL.
204 * A frozen bitmap cannot be renamed, deleted, cleared, set,
205 * enabled, merged to, etc. A frozen bitmap can only abdicate()
207 * In this state, the anonymous successor bitmap may be either
208 * Active and recording writes from the guest (e.g. backup jobs),
209 * or it can be Disabled and not recording writes.
210 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
211 * in any way from the monitor.
212 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
213 * is unusable by QEMU. It can be deleted to remove it from
216 DirtyBitmapStatus
bdrv_dirty_bitmap_status(BdrvDirtyBitmap
*bitmap
)
218 if (bdrv_dirty_bitmap_inconsistent(bitmap
)) {
219 return DIRTY_BITMAP_STATUS_INCONSISTENT
;
220 } else if (bdrv_dirty_bitmap_has_successor(bitmap
)) {
221 return DIRTY_BITMAP_STATUS_FROZEN
;
222 } else if (bdrv_dirty_bitmap_busy(bitmap
)) {
223 return DIRTY_BITMAP_STATUS_LOCKED
;
224 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
225 return DIRTY_BITMAP_STATUS_DISABLED
;
227 return DIRTY_BITMAP_STATUS_ACTIVE
;
231 /* Called with BQL taken. */
232 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap
*bitmap
)
234 return !bitmap
->disabled
|| (bitmap
->successor
&&
235 !bitmap
->successor
->disabled
);
238 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap
*bitmap
, uint32_t flags
,
241 if ((flags
& BDRV_BITMAP_BUSY
) && bdrv_dirty_bitmap_busy(bitmap
)) {
242 error_setg(errp
, "Bitmap '%s' is currently in use by another"
243 " operation and cannot be used", bitmap
->name
);
247 if ((flags
& BDRV_BITMAP_RO
) && bdrv_dirty_bitmap_readonly(bitmap
)) {
248 error_setg(errp
, "Bitmap '%s' is readonly and cannot be modified",
253 if ((flags
& BDRV_BITMAP_INCONSISTENT
) &&
254 bdrv_dirty_bitmap_inconsistent(bitmap
)) {
255 error_setg(errp
, "Bitmap '%s' is inconsistent and cannot be used",
257 error_append_hint(errp
, "Try block-dirty-bitmap-remove to delete"
258 " this bitmap from disk");
266 * Create a successor bitmap destined to replace this bitmap after an operation.
267 * Requires that the bitmap is not marked busy and has no successor.
268 * The successor will be enabled if the parent bitmap was.
269 * Called with BQL taken.
271 int bdrv_dirty_bitmap_create_successor(BlockDriverState
*bs
,
272 BdrvDirtyBitmap
*bitmap
, Error
**errp
)
274 uint64_t granularity
;
275 BdrvDirtyBitmap
*child
;
277 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_BUSY
, errp
)) {
280 if (bdrv_dirty_bitmap_has_successor(bitmap
)) {
281 error_setg(errp
, "Cannot create a successor for a bitmap that already "
286 /* Create an anonymous successor */
287 granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
288 child
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
293 /* Successor will be on or off based on our current state. */
294 child
->disabled
= bitmap
->disabled
;
295 bitmap
->disabled
= true;
297 /* Install the successor and mark the parent as busy */
298 bitmap
->successor
= child
;
303 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
)
305 bitmap
->disabled
= false;
308 /* Called with BQL taken. */
309 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap
*bitmap
)
311 assert(bitmap
->mutex
== bitmap
->successor
->mutex
);
312 qemu_mutex_lock(bitmap
->mutex
);
313 bdrv_enable_dirty_bitmap_locked(bitmap
->successor
);
314 qemu_mutex_unlock(bitmap
->mutex
);
317 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
318 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
)
320 assert(!bitmap
->active_iterators
);
321 assert(!bdrv_dirty_bitmap_busy(bitmap
));
322 assert(!bdrv_dirty_bitmap_has_successor(bitmap
));
323 assert(!bitmap
->meta
);
324 QLIST_REMOVE(bitmap
, list
);
325 hbitmap_free(bitmap
->bitmap
);
326 g_free(bitmap
->name
);
331 * For a bitmap with a successor, yield our name to the successor,
332 * delete the old bitmap, and return a handle to the new bitmap.
333 * Called with BQL taken.
335 BdrvDirtyBitmap
*bdrv_dirty_bitmap_abdicate(BlockDriverState
*bs
,
336 BdrvDirtyBitmap
*bitmap
,
340 BdrvDirtyBitmap
*successor
= bitmap
->successor
;
342 if (successor
== NULL
) {
343 error_setg(errp
, "Cannot relinquish control if "
344 "there's no successor present");
350 successor
->name
= name
;
351 bitmap
->successor
= NULL
;
352 successor
->persistent
= bitmap
->persistent
;
353 bitmap
->persistent
= false;
354 bitmap
->busy
= false;
355 bdrv_release_dirty_bitmap(bs
, bitmap
);
361 * In cases of failure where we can no longer safely delete the parent,
362 * we may wish to re-join the parent and child/successor.
363 * The merged parent will be marked as not busy.
364 * The marged parent will be enabled if and only if the successor was enabled.
365 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
367 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap_locked(BlockDriverState
*bs
,
368 BdrvDirtyBitmap
*parent
,
371 BdrvDirtyBitmap
*successor
= parent
->successor
;
374 error_setg(errp
, "Cannot reclaim a successor when none is present");
378 if (!hbitmap_merge(parent
->bitmap
, successor
->bitmap
, parent
->bitmap
)) {
379 error_setg(errp
, "Merging of parent and successor bitmap failed");
383 parent
->disabled
= successor
->disabled
;
384 parent
->busy
= false;
385 bdrv_release_dirty_bitmap_locked(successor
);
386 parent
->successor
= NULL
;
391 /* Called with BQL taken. */
392 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap(BlockDriverState
*bs
,
393 BdrvDirtyBitmap
*parent
,
396 BdrvDirtyBitmap
*ret
;
398 qemu_mutex_lock(parent
->mutex
);
399 ret
= bdrv_reclaim_dirty_bitmap_locked(bs
, parent
, errp
);
400 qemu_mutex_unlock(parent
->mutex
);
406 * Truncates _all_ bitmaps attached to a BDS.
407 * Called with BQL taken.
409 void bdrv_dirty_bitmap_truncate(BlockDriverState
*bs
, int64_t bytes
)
411 BdrvDirtyBitmap
*bitmap
;
413 bdrv_dirty_bitmaps_lock(bs
);
414 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
415 assert(!bdrv_dirty_bitmap_busy(bitmap
));
416 assert(!bdrv_dirty_bitmap_has_successor(bitmap
));
417 assert(!bitmap
->active_iterators
);
418 hbitmap_truncate(bitmap
->bitmap
, bytes
);
419 bitmap
->size
= bytes
;
421 bdrv_dirty_bitmaps_unlock(bs
);
424 /* Called with BQL taken. */
425 void bdrv_release_dirty_bitmap(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
)
427 bdrv_dirty_bitmaps_lock(bs
);
428 bdrv_release_dirty_bitmap_locked(bitmap
);
429 bdrv_dirty_bitmaps_unlock(bs
);
433 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
434 * There must not be any busy bitmaps attached.
435 * This function does not remove persistent bitmaps from the storage.
436 * Called with BQL taken.
438 void bdrv_release_named_dirty_bitmaps(BlockDriverState
*bs
)
440 BdrvDirtyBitmap
*bm
, *next
;
442 bdrv_dirty_bitmaps_lock(bs
);
443 QLIST_FOREACH_SAFE(bm
, &bs
->dirty_bitmaps
, list
, next
) {
444 if (bdrv_dirty_bitmap_name(bm
)) {
445 bdrv_release_dirty_bitmap_locked(bm
);
448 bdrv_dirty_bitmaps_unlock(bs
);
452 * Remove persistent dirty bitmap from the storage if it exists.
453 * Absence of bitmap is not an error, because we have the following scenario:
454 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
455 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
457 * This function doesn't release corresponding BdrvDirtyBitmap.
459 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
463 if (bs
->drv
&& bs
->drv
->bdrv_remove_persistent_dirty_bitmap
) {
464 bs
->drv
->bdrv_remove_persistent_dirty_bitmap(bs
, name
, errp
);
468 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
470 bdrv_dirty_bitmap_lock(bitmap
);
471 bitmap
->disabled
= true;
472 bdrv_dirty_bitmap_unlock(bitmap
);
475 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
477 bdrv_dirty_bitmap_lock(bitmap
);
478 bdrv_enable_dirty_bitmap_locked(bitmap
);
479 bdrv_dirty_bitmap_unlock(bitmap
);
482 BlockDirtyInfoList
*bdrv_query_dirty_bitmaps(BlockDriverState
*bs
)
485 BlockDirtyInfoList
*list
= NULL
;
486 BlockDirtyInfoList
**plist
= &list
;
488 bdrv_dirty_bitmaps_lock(bs
);
489 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
490 BlockDirtyInfo
*info
= g_new0(BlockDirtyInfo
, 1);
491 BlockDirtyInfoList
*entry
= g_new0(BlockDirtyInfoList
, 1);
492 info
->count
= bdrv_get_dirty_count(bm
);
493 info
->granularity
= bdrv_dirty_bitmap_granularity(bm
);
494 info
->has_name
= !!bm
->name
;
495 info
->name
= g_strdup(bm
->name
);
496 info
->status
= bdrv_dirty_bitmap_status(bm
);
497 info
->recording
= bdrv_dirty_bitmap_recording(bm
);
498 info
->busy
= bdrv_dirty_bitmap_busy(bm
);
499 info
->persistent
= bm
->persistent
;
500 info
->has_inconsistent
= bm
->inconsistent
;
501 info
->inconsistent
= bm
->inconsistent
;
504 plist
= &entry
->next
;
506 bdrv_dirty_bitmaps_unlock(bs
);
511 /* Called within bdrv_dirty_bitmap_lock..unlock */
512 bool bdrv_get_dirty_locked(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
,
516 return hbitmap_get(bitmap
->bitmap
, offset
);
523 * Chooses a default granularity based on the existing cluster size,
524 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
525 * is no cluster size information available.
527 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState
*bs
)
530 uint32_t granularity
;
532 if (bdrv_get_info(bs
, &bdi
) >= 0 && bdi
.cluster_size
> 0) {
533 granularity
= MAX(4096, bdi
.cluster_size
);
534 granularity
= MIN(65536, granularity
);
542 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap
*bitmap
)
544 return 1U << hbitmap_granularity(bitmap
->bitmap
);
547 BdrvDirtyBitmapIter
*bdrv_dirty_iter_new(BdrvDirtyBitmap
*bitmap
)
549 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
550 hbitmap_iter_init(&iter
->hbi
, bitmap
->bitmap
, 0);
551 iter
->bitmap
= bitmap
;
552 bitmap
->active_iterators
++;
556 BdrvDirtyBitmapIter
*bdrv_dirty_meta_iter_new(BdrvDirtyBitmap
*bitmap
)
558 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
559 hbitmap_iter_init(&iter
->hbi
, bitmap
->meta
, 0);
560 iter
->bitmap
= bitmap
;
561 bitmap
->active_iterators
++;
565 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter
*iter
)
570 assert(iter
->bitmap
->active_iterators
> 0);
571 iter
->bitmap
->active_iterators
--;
575 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter
*iter
)
577 return hbitmap_iter_next(&iter
->hbi
);
580 /* Called within bdrv_dirty_bitmap_lock..unlock */
581 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
,
582 int64_t offset
, int64_t bytes
)
584 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
585 hbitmap_set(bitmap
->bitmap
, offset
, bytes
);
588 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
589 int64_t offset
, int64_t bytes
)
591 bdrv_dirty_bitmap_lock(bitmap
);
592 bdrv_set_dirty_bitmap_locked(bitmap
, offset
, bytes
);
593 bdrv_dirty_bitmap_unlock(bitmap
);
596 /* Called within bdrv_dirty_bitmap_lock..unlock */
597 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
,
598 int64_t offset
, int64_t bytes
)
600 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
601 hbitmap_reset(bitmap
->bitmap
, offset
, bytes
);
604 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
605 int64_t offset
, int64_t bytes
)
607 bdrv_dirty_bitmap_lock(bitmap
);
608 bdrv_reset_dirty_bitmap_locked(bitmap
, offset
, bytes
);
609 bdrv_dirty_bitmap_unlock(bitmap
);
612 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
**out
)
614 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
615 bdrv_dirty_bitmap_lock(bitmap
);
617 hbitmap_reset_all(bitmap
->bitmap
);
619 HBitmap
*backup
= bitmap
->bitmap
;
620 bitmap
->bitmap
= hbitmap_alloc(bitmap
->size
,
621 hbitmap_granularity(backup
));
624 bdrv_dirty_bitmap_unlock(bitmap
);
627 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
*backup
)
629 HBitmap
*tmp
= bitmap
->bitmap
;
630 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
631 bitmap
->bitmap
= backup
;
635 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap
*bitmap
,
636 uint64_t offset
, uint64_t bytes
)
638 return hbitmap_serialization_size(bitmap
->bitmap
, offset
, bytes
);
641 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap
*bitmap
)
643 return hbitmap_serialization_align(bitmap
->bitmap
);
646 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap
*bitmap
,
647 uint8_t *buf
, uint64_t offset
,
650 hbitmap_serialize_part(bitmap
->bitmap
, buf
, offset
, bytes
);
653 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap
*bitmap
,
654 uint8_t *buf
, uint64_t offset
,
655 uint64_t bytes
, bool finish
)
657 hbitmap_deserialize_part(bitmap
->bitmap
, buf
, offset
, bytes
, finish
);
660 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap
*bitmap
,
661 uint64_t offset
, uint64_t bytes
,
664 hbitmap_deserialize_zeroes(bitmap
->bitmap
, offset
, bytes
, finish
);
667 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap
*bitmap
,
668 uint64_t offset
, uint64_t bytes
,
671 hbitmap_deserialize_ones(bitmap
->bitmap
, offset
, bytes
, finish
);
674 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap
*bitmap
)
676 hbitmap_deserialize_finish(bitmap
->bitmap
);
679 void bdrv_set_dirty(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
681 BdrvDirtyBitmap
*bitmap
;
683 if (QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
687 bdrv_dirty_bitmaps_lock(bs
);
688 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
689 if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
692 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
693 hbitmap_set(bitmap
->bitmap
, offset
, bytes
);
695 bdrv_dirty_bitmaps_unlock(bs
);
699 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
701 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter
*iter
, int64_t offset
)
703 hbitmap_iter_init(&iter
->hbi
, iter
->hbi
.hb
, offset
);
706 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap
*bitmap
)
708 return hbitmap_count(bitmap
->bitmap
);
711 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap
*bitmap
)
713 return hbitmap_count(bitmap
->meta
);
716 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap
*bitmap
)
718 return bitmap
->readonly
;
721 /* Called with BQL taken. */
722 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap
*bitmap
, bool value
)
724 qemu_mutex_lock(bitmap
->mutex
);
725 bitmap
->readonly
= value
;
726 qemu_mutex_unlock(bitmap
->mutex
);
729 bool bdrv_has_readonly_bitmaps(BlockDriverState
*bs
)
732 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
741 /* Called with BQL taken. */
742 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap
*bitmap
, bool persistent
)
744 qemu_mutex_lock(bitmap
->mutex
);
745 bitmap
->persistent
= persistent
;
746 qemu_mutex_unlock(bitmap
->mutex
);
749 /* Called with BQL taken. */
750 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap
*bitmap
)
752 qemu_mutex_lock(bitmap
->mutex
);
753 assert(bitmap
->persistent
== true);
754 bitmap
->inconsistent
= true;
755 bitmap
->disabled
= true;
756 qemu_mutex_unlock(bitmap
->mutex
);
759 /* Called with BQL taken. */
760 void bdrv_dirty_bitmap_set_migration(BdrvDirtyBitmap
*bitmap
, bool migration
)
762 qemu_mutex_lock(bitmap
->mutex
);
763 bitmap
->migration
= migration
;
764 qemu_mutex_unlock(bitmap
->mutex
);
767 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap
*bitmap
)
769 return bitmap
->persistent
&& !bitmap
->migration
;
772 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap
*bitmap
)
774 return bitmap
->inconsistent
;
777 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState
*bs
)
780 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
781 if (bm
->persistent
&& !bm
->readonly
&& !bm
->migration
) {
789 BdrvDirtyBitmap
*bdrv_dirty_bitmap_next(BlockDriverState
*bs
,
790 BdrvDirtyBitmap
*bitmap
)
792 return bitmap
== NULL
? QLIST_FIRST(&bs
->dirty_bitmaps
) :
793 QLIST_NEXT(bitmap
, list
);
796 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap
*bitmap
, Error
**errp
)
798 return hbitmap_sha256(bitmap
->bitmap
, errp
);
801 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap
*bitmap
, uint64_t offset
,
804 return hbitmap_next_zero(bitmap
->bitmap
, offset
, bytes
);
807 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap
*bitmap
,
808 uint64_t *offset
, uint64_t *bytes
)
810 return hbitmap_next_dirty_area(bitmap
->bitmap
, offset
, bytes
);
813 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap
*dest
, const BdrvDirtyBitmap
*src
,
814 HBitmap
**backup
, Error
**errp
)
818 qemu_mutex_lock(dest
->mutex
);
819 if (src
->mutex
!= dest
->mutex
) {
820 qemu_mutex_lock(src
->mutex
);
823 if (bdrv_dirty_bitmap_check(dest
, BDRV_BITMAP_DEFAULT
, errp
)) {
827 if (bdrv_dirty_bitmap_check(src
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
831 if (!hbitmap_can_merge(dest
->bitmap
, src
->bitmap
)) {
832 error_setg(errp
, "Bitmaps are incompatible and can't be merged");
837 *backup
= dest
->bitmap
;
838 dest
->bitmap
= hbitmap_alloc(dest
->size
, hbitmap_granularity(*backup
));
839 ret
= hbitmap_merge(*backup
, src
->bitmap
, dest
->bitmap
);
841 ret
= hbitmap_merge(dest
->bitmap
, src
->bitmap
, dest
->bitmap
);
846 qemu_mutex_unlock(dest
->mutex
);
847 if (src
->mutex
!= dest
->mutex
) {
848 qemu_mutex_unlock(src
->mutex
);