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"
26 #include "qemu-common.h"
28 #include "block/block_int.h"
29 #include "block/blockjob.h"
32 * A BdrvDirtyBitmap can be in three possible states:
33 * (1) successor is NULL and disabled is false: full r/w mode
34 * (2) successor is NULL and disabled is true: read only mode ("disabled")
35 * (3) successor is set: frozen mode.
36 * A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set,
37 * or enabled. A frozen bitmap can only abdicate() or reclaim().
39 struct BdrvDirtyBitmap
{
41 HBitmap
*bitmap
; /* Dirty bitmap implementation */
42 HBitmap
*meta
; /* Meta dirty bitmap */
43 bool qmp_locked
; /* Bitmap is locked, it can't be modified
45 BdrvDirtyBitmap
*successor
; /* Anonymous child; implies frozen status */
46 char *name
; /* Optional non-empty unique ID */
47 int64_t size
; /* Size of the bitmap, in bytes */
48 bool disabled
; /* Bitmap is disabled. It ignores all writes to
50 int active_iterators
; /* How many iterators are active */
51 bool readonly
; /* Bitmap is read-only. This field also
52 prevents the respective image from being
53 modified (i.e. blocks writes and discards).
54 Such operations must fail and both the image
55 and this bitmap must remain unchanged while
57 bool persistent
; /* bitmap must be saved to owner disk image */
58 QLIST_ENTRY(BdrvDirtyBitmap
) list
;
61 struct BdrvDirtyBitmapIter
{
63 BdrvDirtyBitmap
*bitmap
;
66 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState
*bs
)
68 qemu_mutex_lock(&bs
->dirty_bitmap_mutex
);
71 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState
*bs
)
73 qemu_mutex_unlock(&bs
->dirty_bitmap_mutex
);
76 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap
*bitmap
)
78 qemu_mutex_lock(bitmap
->mutex
);
81 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap
*bitmap
)
83 qemu_mutex_unlock(bitmap
->mutex
);
86 /* Called with BQL or dirty_bitmap lock taken. */
87 BdrvDirtyBitmap
*bdrv_find_dirty_bitmap(BlockDriverState
*bs
, const char *name
)
92 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
93 if (bm
->name
&& !strcmp(name
, bm
->name
)) {
100 /* Called with BQL taken. */
101 BdrvDirtyBitmap
*bdrv_create_dirty_bitmap(BlockDriverState
*bs
,
102 uint32_t granularity
,
107 BdrvDirtyBitmap
*bitmap
;
109 assert(is_power_of_2(granularity
) && granularity
>= BDRV_SECTOR_SIZE
);
111 if (name
&& bdrv_find_dirty_bitmap(bs
, name
)) {
112 error_setg(errp
, "Bitmap already exists: %s", name
);
115 bitmap_size
= bdrv_getlength(bs
);
116 if (bitmap_size
< 0) {
117 error_setg_errno(errp
, -bitmap_size
, "could not get length of device");
118 errno
= -bitmap_size
;
121 bitmap
= g_new0(BdrvDirtyBitmap
, 1);
122 bitmap
->mutex
= &bs
->dirty_bitmap_mutex
;
123 bitmap
->bitmap
= hbitmap_alloc(bitmap_size
, ctz32(granularity
));
124 bitmap
->size
= bitmap_size
;
125 bitmap
->name
= g_strdup(name
);
126 bitmap
->disabled
= false;
127 bdrv_dirty_bitmaps_lock(bs
);
128 QLIST_INSERT_HEAD(&bs
->dirty_bitmaps
, bitmap
, list
);
129 bdrv_dirty_bitmaps_unlock(bs
);
133 /* bdrv_create_meta_dirty_bitmap
135 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
136 * when a dirty status bit in @bitmap is changed (either from reset to set or
137 * the other way around), its respective meta dirty bitmap bit will be marked
140 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
141 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
144 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
147 assert(!bitmap
->meta
);
148 qemu_mutex_lock(bitmap
->mutex
);
149 bitmap
->meta
= hbitmap_create_meta(bitmap
->bitmap
,
150 chunk_size
* BITS_PER_BYTE
);
151 qemu_mutex_unlock(bitmap
->mutex
);
154 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
156 assert(bitmap
->meta
);
157 qemu_mutex_lock(bitmap
->mutex
);
158 hbitmap_free_meta(bitmap
->bitmap
);
160 qemu_mutex_unlock(bitmap
->mutex
);
163 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap
*bitmap
)
168 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap
*bitmap
)
173 /* Called with BQL taken. */
174 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap
*bitmap
)
176 return bitmap
->successor
;
179 void bdrv_dirty_bitmap_set_qmp_locked(BdrvDirtyBitmap
*bitmap
, bool qmp_locked
)
181 qemu_mutex_lock(bitmap
->mutex
);
182 bitmap
->qmp_locked
= qmp_locked
;
183 qemu_mutex_unlock(bitmap
->mutex
);
186 bool bdrv_dirty_bitmap_qmp_locked(BdrvDirtyBitmap
*bitmap
)
188 return bitmap
->qmp_locked
;
191 /* Called with BQL taken. */
192 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap
*bitmap
)
194 return !(bitmap
->disabled
|| bitmap
->successor
);
197 /* Called with BQL taken. */
198 DirtyBitmapStatus
bdrv_dirty_bitmap_status(BdrvDirtyBitmap
*bitmap
)
200 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
201 return DIRTY_BITMAP_STATUS_FROZEN
;
202 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
203 return DIRTY_BITMAP_STATUS_LOCKED
;
204 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
205 return DIRTY_BITMAP_STATUS_DISABLED
;
207 return DIRTY_BITMAP_STATUS_ACTIVE
;
212 * Create a successor bitmap destined to replace this bitmap after an operation.
213 * Requires that the bitmap is not frozen and has no successor.
214 * Called with BQL taken.
216 int bdrv_dirty_bitmap_create_successor(BlockDriverState
*bs
,
217 BdrvDirtyBitmap
*bitmap
, Error
**errp
)
219 uint64_t granularity
;
220 BdrvDirtyBitmap
*child
;
222 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
223 error_setg(errp
, "Cannot create a successor for a bitmap that is "
227 assert(!bitmap
->successor
);
229 /* Create an anonymous successor */
230 granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
231 child
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
236 /* Successor will be on or off based on our current state. */
237 child
->disabled
= bitmap
->disabled
;
239 /* Install the successor and freeze the parent */
240 bitmap
->successor
= child
;
244 /* Called with BQL taken. */
245 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap
*bitmap
)
247 qemu_mutex_lock(bitmap
->mutex
);
248 bdrv_enable_dirty_bitmap(bitmap
->successor
);
249 qemu_mutex_unlock(bitmap
->mutex
);
252 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
253 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
)
255 assert(!bitmap
->active_iterators
);
256 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
257 assert(!bitmap
->meta
);
258 QLIST_REMOVE(bitmap
, list
);
259 hbitmap_free(bitmap
->bitmap
);
260 g_free(bitmap
->name
);
265 * For a bitmap with a successor, yield our name to the successor,
266 * delete the old bitmap, and return a handle to the new bitmap.
267 * Called with BQL taken.
269 BdrvDirtyBitmap
*bdrv_dirty_bitmap_abdicate(BlockDriverState
*bs
,
270 BdrvDirtyBitmap
*bitmap
,
274 BdrvDirtyBitmap
*successor
= bitmap
->successor
;
276 if (successor
== NULL
) {
277 error_setg(errp
, "Cannot relinquish control if "
278 "there's no successor present");
284 successor
->name
= name
;
285 bitmap
->successor
= NULL
;
286 successor
->persistent
= bitmap
->persistent
;
287 bitmap
->persistent
= false;
288 bdrv_release_dirty_bitmap(bs
, bitmap
);
294 * In cases of failure where we can no longer safely delete the parent,
295 * we may wish to re-join the parent and child/successor.
296 * The merged parent will be un-frozen, but not explicitly re-enabled.
297 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
299 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap_locked(BlockDriverState
*bs
,
300 BdrvDirtyBitmap
*parent
,
303 BdrvDirtyBitmap
*successor
= parent
->successor
;
306 error_setg(errp
, "Cannot reclaim a successor when none is present");
310 if (!hbitmap_merge(parent
->bitmap
, successor
->bitmap
)) {
311 error_setg(errp
, "Merging of parent and successor bitmap failed");
314 bdrv_release_dirty_bitmap_locked(successor
);
315 parent
->successor
= NULL
;
320 /* Called with BQL taken. */
321 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap(BlockDriverState
*bs
,
322 BdrvDirtyBitmap
*parent
,
325 BdrvDirtyBitmap
*ret
;
327 qemu_mutex_lock(parent
->mutex
);
328 ret
= bdrv_reclaim_dirty_bitmap_locked(bs
, parent
, errp
);
329 qemu_mutex_unlock(parent
->mutex
);
335 * Truncates _all_ bitmaps attached to a BDS.
336 * Called with BQL taken.
338 void bdrv_dirty_bitmap_truncate(BlockDriverState
*bs
, int64_t bytes
)
340 BdrvDirtyBitmap
*bitmap
;
342 bdrv_dirty_bitmaps_lock(bs
);
343 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
344 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
345 assert(!bitmap
->active_iterators
);
346 hbitmap_truncate(bitmap
->bitmap
, bytes
);
347 bitmap
->size
= bytes
;
349 bdrv_dirty_bitmaps_unlock(bs
);
352 /* Called with BQL taken. */
353 void bdrv_release_dirty_bitmap(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
)
355 bdrv_dirty_bitmaps_lock(bs
);
356 bdrv_release_dirty_bitmap_locked(bitmap
);
357 bdrv_dirty_bitmaps_unlock(bs
);
361 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
362 * There must not be any frozen bitmaps attached.
363 * This function does not remove persistent bitmaps from the storage.
364 * Called with BQL taken.
366 void bdrv_release_named_dirty_bitmaps(BlockDriverState
*bs
)
368 BdrvDirtyBitmap
*bm
, *next
;
370 bdrv_dirty_bitmaps_lock(bs
);
371 QLIST_FOREACH_SAFE(bm
, &bs
->dirty_bitmaps
, list
, next
) {
372 if (bdrv_dirty_bitmap_name(bm
)) {
373 bdrv_release_dirty_bitmap_locked(bm
);
376 bdrv_dirty_bitmaps_unlock(bs
);
380 * Release all persistent dirty bitmaps attached to a BDS (for use in
381 * bdrv_inactivate_recurse()).
382 * There must not be any frozen bitmaps attached.
383 * This function does not remove persistent bitmaps from the storage.
384 * Called with BQL taken.
386 void bdrv_release_persistent_dirty_bitmaps(BlockDriverState
*bs
)
388 BdrvDirtyBitmap
*bm
, *next
;
390 bdrv_dirty_bitmaps_lock(bs
);
391 QLIST_FOREACH_SAFE(bm
, &bs
->dirty_bitmaps
, list
, next
) {
392 if (bdrv_dirty_bitmap_get_persistance(bm
)) {
393 bdrv_release_dirty_bitmap_locked(bm
);
396 bdrv_dirty_bitmaps_unlock(bs
);
400 * Remove persistent dirty bitmap from the storage if it exists.
401 * Absence of bitmap is not an error, because we have the following scenario:
402 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
403 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
405 * This function doesn't release corresponding BdrvDirtyBitmap.
407 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
411 if (bs
->drv
&& bs
->drv
->bdrv_remove_persistent_dirty_bitmap
) {
412 bs
->drv
->bdrv_remove_persistent_dirty_bitmap(bs
, name
, errp
);
416 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
418 bdrv_dirty_bitmap_lock(bitmap
);
419 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
420 bitmap
->disabled
= true;
421 bdrv_dirty_bitmap_unlock(bitmap
);
424 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
426 bdrv_dirty_bitmap_lock(bitmap
);
427 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
428 bitmap
->disabled
= false;
429 bdrv_dirty_bitmap_unlock(bitmap
);
432 BlockDirtyInfoList
*bdrv_query_dirty_bitmaps(BlockDriverState
*bs
)
435 BlockDirtyInfoList
*list
= NULL
;
436 BlockDirtyInfoList
**plist
= &list
;
438 bdrv_dirty_bitmaps_lock(bs
);
439 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
440 BlockDirtyInfo
*info
= g_new0(BlockDirtyInfo
, 1);
441 BlockDirtyInfoList
*entry
= g_new0(BlockDirtyInfoList
, 1);
442 info
->count
= bdrv_get_dirty_count(bm
);
443 info
->granularity
= bdrv_dirty_bitmap_granularity(bm
);
444 info
->has_name
= !!bm
->name
;
445 info
->name
= g_strdup(bm
->name
);
446 info
->status
= bdrv_dirty_bitmap_status(bm
);
449 plist
= &entry
->next
;
451 bdrv_dirty_bitmaps_unlock(bs
);
456 /* Called within bdrv_dirty_bitmap_lock..unlock */
457 bool bdrv_get_dirty_locked(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
,
461 return hbitmap_get(bitmap
->bitmap
, offset
);
468 * Chooses a default granularity based on the existing cluster size,
469 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
470 * is no cluster size information available.
472 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState
*bs
)
475 uint32_t granularity
;
477 if (bdrv_get_info(bs
, &bdi
) >= 0 && bdi
.cluster_size
> 0) {
478 granularity
= MAX(4096, bdi
.cluster_size
);
479 granularity
= MIN(65536, granularity
);
487 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap
*bitmap
)
489 return 1U << hbitmap_granularity(bitmap
->bitmap
);
492 BdrvDirtyBitmapIter
*bdrv_dirty_iter_new(BdrvDirtyBitmap
*bitmap
)
494 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
495 hbitmap_iter_init(&iter
->hbi
, bitmap
->bitmap
, 0);
496 iter
->bitmap
= bitmap
;
497 bitmap
->active_iterators
++;
501 BdrvDirtyBitmapIter
*bdrv_dirty_meta_iter_new(BdrvDirtyBitmap
*bitmap
)
503 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
504 hbitmap_iter_init(&iter
->hbi
, bitmap
->meta
, 0);
505 iter
->bitmap
= bitmap
;
506 bitmap
->active_iterators
++;
510 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter
*iter
)
515 assert(iter
->bitmap
->active_iterators
> 0);
516 iter
->bitmap
->active_iterators
--;
520 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter
*iter
)
522 return hbitmap_iter_next(&iter
->hbi
, true);
526 * Return the next consecutively dirty area in the dirty bitmap
527 * belonging to the given iterator @iter.
529 * @max_offset: Maximum value that may be returned for
531 * @offset: Will contain the start offset of the next dirty area
532 * @bytes: Will contain the length of the next dirty area
534 * Returns: True if a dirty area could be found before max_offset
535 * (which means that *offset and *bytes then contain valid
536 * values), false otherwise.
538 * Note that @iter is never advanced if false is returned. If an area
539 * is found (which means that true is returned), it will be advanced
542 bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter
*iter
, uint64_t max_offset
,
543 uint64_t *offset
, int *bytes
)
545 uint32_t granularity
= bdrv_dirty_bitmap_granularity(iter
->bitmap
);
546 uint64_t gran_max_offset
;
550 if (max_offset
== iter
->bitmap
->size
) {
551 /* If max_offset points to the image end, round it up by the
552 * bitmap granularity */
553 gran_max_offset
= ROUND_UP(max_offset
, granularity
);
555 gran_max_offset
= max_offset
;
558 ret
= hbitmap_iter_next(&iter
->hbi
, false);
559 if (ret
< 0 || ret
+ granularity
> gran_max_offset
) {
566 assert(granularity
<= INT_MAX
);
569 /* Advance iterator */
570 ret
= hbitmap_iter_next(&iter
->hbi
, true);
572 } while (ret
+ granularity
<= gran_max_offset
&&
573 hbitmap_iter_next(&iter
->hbi
, false) == ret
+ granularity
&&
574 size
<= INT_MAX
- granularity
);
576 *bytes
= MIN(size
, max_offset
- *offset
);
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_enabled(bitmap
));
585 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
586 hbitmap_set(bitmap
->bitmap
, offset
, bytes
);
589 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
590 int64_t offset
, int64_t bytes
)
592 bdrv_dirty_bitmap_lock(bitmap
);
593 bdrv_set_dirty_bitmap_locked(bitmap
, offset
, bytes
);
594 bdrv_dirty_bitmap_unlock(bitmap
);
597 /* Called within bdrv_dirty_bitmap_lock..unlock */
598 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
,
599 int64_t offset
, int64_t bytes
)
601 assert(bdrv_dirty_bitmap_enabled(bitmap
));
602 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
603 hbitmap_reset(bitmap
->bitmap
, offset
, bytes
);
606 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
607 int64_t offset
, int64_t bytes
)
609 bdrv_dirty_bitmap_lock(bitmap
);
610 bdrv_reset_dirty_bitmap_locked(bitmap
, offset
, bytes
);
611 bdrv_dirty_bitmap_unlock(bitmap
);
614 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
**out
)
616 assert(bdrv_dirty_bitmap_enabled(bitmap
));
617 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
618 bdrv_dirty_bitmap_lock(bitmap
);
620 hbitmap_reset_all(bitmap
->bitmap
);
622 HBitmap
*backup
= bitmap
->bitmap
;
623 bitmap
->bitmap
= hbitmap_alloc(bitmap
->size
,
624 hbitmap_granularity(backup
));
627 bdrv_dirty_bitmap_unlock(bitmap
);
630 void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
*in
)
632 HBitmap
*tmp
= bitmap
->bitmap
;
633 assert(bdrv_dirty_bitmap_enabled(bitmap
));
634 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
639 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap
*bitmap
,
640 uint64_t offset
, uint64_t bytes
)
642 return hbitmap_serialization_size(bitmap
->bitmap
, offset
, bytes
);
645 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap
*bitmap
)
647 return hbitmap_serialization_align(bitmap
->bitmap
);
650 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap
*bitmap
,
651 uint8_t *buf
, uint64_t offset
,
654 hbitmap_serialize_part(bitmap
->bitmap
, buf
, offset
, bytes
);
657 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap
*bitmap
,
658 uint8_t *buf
, uint64_t offset
,
659 uint64_t bytes
, bool finish
)
661 hbitmap_deserialize_part(bitmap
->bitmap
, buf
, offset
, bytes
, finish
);
664 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap
*bitmap
,
665 uint64_t offset
, uint64_t bytes
,
668 hbitmap_deserialize_zeroes(bitmap
->bitmap
, offset
, bytes
, finish
);
671 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap
*bitmap
,
672 uint64_t offset
, uint64_t bytes
,
675 hbitmap_deserialize_ones(bitmap
->bitmap
, offset
, bytes
, finish
);
678 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap
*bitmap
)
680 hbitmap_deserialize_finish(bitmap
->bitmap
);
683 void bdrv_set_dirty(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
685 BdrvDirtyBitmap
*bitmap
;
687 if (QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
691 bdrv_dirty_bitmaps_lock(bs
);
692 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
693 if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
696 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
697 hbitmap_set(bitmap
->bitmap
, offset
, bytes
);
699 bdrv_dirty_bitmaps_unlock(bs
);
703 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
705 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter
*iter
, int64_t offset
)
707 hbitmap_iter_init(&iter
->hbi
, iter
->hbi
.hb
, offset
);
710 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap
*bitmap
)
712 return hbitmap_count(bitmap
->bitmap
);
715 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap
*bitmap
)
717 return hbitmap_count(bitmap
->meta
);
720 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap
*bitmap
)
722 return bitmap
->readonly
;
725 /* Called with BQL taken. */
726 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap
*bitmap
, bool value
)
728 qemu_mutex_lock(bitmap
->mutex
);
729 bitmap
->readonly
= value
;
730 qemu_mutex_unlock(bitmap
->mutex
);
733 bool bdrv_has_readonly_bitmaps(BlockDriverState
*bs
)
736 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
745 /* Called with BQL taken. */
746 void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap
*bitmap
, bool persistent
)
748 qemu_mutex_lock(bitmap
->mutex
);
749 bitmap
->persistent
= persistent
;
750 qemu_mutex_unlock(bitmap
->mutex
);
753 bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap
*bitmap
)
755 return bitmap
->persistent
;
758 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState
*bs
)
761 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
762 if (bm
->persistent
&& !bm
->readonly
) {
770 BdrvDirtyBitmap
*bdrv_dirty_bitmap_next(BlockDriverState
*bs
,
771 BdrvDirtyBitmap
*bitmap
)
773 return bitmap
== NULL
? QLIST_FIRST(&bs
->dirty_bitmaps
) :
774 QLIST_NEXT(bitmap
, list
);
777 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap
*bitmap
, Error
**errp
)
779 return hbitmap_sha256(bitmap
->bitmap
, errp
);
782 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap
*bitmap
, uint64_t offset
)
784 return hbitmap_next_zero(bitmap
->bitmap
, offset
);
787 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap
*dest
, const BdrvDirtyBitmap
*src
,
790 /* only bitmaps from one bds are supported */
791 assert(dest
->mutex
== src
->mutex
);
793 qemu_mutex_lock(dest
->mutex
);
795 assert(bdrv_dirty_bitmap_enabled(dest
));
796 assert(!bdrv_dirty_bitmap_readonly(dest
));
798 if (!hbitmap_merge(dest
->bitmap
, src
->bitmap
)) {
799 error_setg(errp
, "Bitmaps are incompatible and can't be merged");
802 qemu_mutex_unlock(dest
->mutex
);