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 bool migration
; /* Bitmap is selected for migration, it should
59 not be stored on the next inactivation
60 (persistent flag doesn't matter until next
62 QLIST_ENTRY(BdrvDirtyBitmap
) list
;
65 struct BdrvDirtyBitmapIter
{
67 BdrvDirtyBitmap
*bitmap
;
70 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState
*bs
)
72 qemu_mutex_lock(&bs
->dirty_bitmap_mutex
);
75 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState
*bs
)
77 qemu_mutex_unlock(&bs
->dirty_bitmap_mutex
);
80 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap
*bitmap
)
82 qemu_mutex_lock(bitmap
->mutex
);
85 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap
*bitmap
)
87 qemu_mutex_unlock(bitmap
->mutex
);
90 /* Called with BQL or dirty_bitmap lock taken. */
91 BdrvDirtyBitmap
*bdrv_find_dirty_bitmap(BlockDriverState
*bs
, const char *name
)
96 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
97 if (bm
->name
&& !strcmp(name
, bm
->name
)) {
104 /* Called with BQL taken. */
105 BdrvDirtyBitmap
*bdrv_create_dirty_bitmap(BlockDriverState
*bs
,
106 uint32_t granularity
,
111 BdrvDirtyBitmap
*bitmap
;
113 assert(is_power_of_2(granularity
) && granularity
>= BDRV_SECTOR_SIZE
);
115 if (name
&& bdrv_find_dirty_bitmap(bs
, name
)) {
116 error_setg(errp
, "Bitmap already exists: %s", name
);
119 bitmap_size
= bdrv_getlength(bs
);
120 if (bitmap_size
< 0) {
121 error_setg_errno(errp
, -bitmap_size
, "could not get length of device");
122 errno
= -bitmap_size
;
125 bitmap
= g_new0(BdrvDirtyBitmap
, 1);
126 bitmap
->mutex
= &bs
->dirty_bitmap_mutex
;
127 bitmap
->bitmap
= hbitmap_alloc(bitmap_size
, ctz32(granularity
));
128 bitmap
->size
= bitmap_size
;
129 bitmap
->name
= g_strdup(name
);
130 bitmap
->disabled
= false;
131 bdrv_dirty_bitmaps_lock(bs
);
132 QLIST_INSERT_HEAD(&bs
->dirty_bitmaps
, bitmap
, list
);
133 bdrv_dirty_bitmaps_unlock(bs
);
137 /* bdrv_create_meta_dirty_bitmap
139 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
140 * when a dirty status bit in @bitmap is changed (either from reset to set or
141 * the other way around), its respective meta dirty bitmap bit will be marked
144 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
145 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
148 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
151 assert(!bitmap
->meta
);
152 qemu_mutex_lock(bitmap
->mutex
);
153 bitmap
->meta
= hbitmap_create_meta(bitmap
->bitmap
,
154 chunk_size
* BITS_PER_BYTE
);
155 qemu_mutex_unlock(bitmap
->mutex
);
158 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
160 assert(bitmap
->meta
);
161 qemu_mutex_lock(bitmap
->mutex
);
162 hbitmap_free_meta(bitmap
->bitmap
);
164 qemu_mutex_unlock(bitmap
->mutex
);
167 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap
*bitmap
)
172 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap
*bitmap
)
177 /* Called with BQL taken. */
178 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap
*bitmap
)
180 return bitmap
->successor
;
183 /* Both conditions disallow user-modification via QMP. */
184 bool bdrv_dirty_bitmap_user_locked(BdrvDirtyBitmap
*bitmap
) {
185 return bdrv_dirty_bitmap_frozen(bitmap
) ||
186 bdrv_dirty_bitmap_qmp_locked(bitmap
);
189 void bdrv_dirty_bitmap_set_qmp_locked(BdrvDirtyBitmap
*bitmap
, bool qmp_locked
)
191 qemu_mutex_lock(bitmap
->mutex
);
192 bitmap
->qmp_locked
= qmp_locked
;
193 qemu_mutex_unlock(bitmap
->mutex
);
196 bool bdrv_dirty_bitmap_qmp_locked(BdrvDirtyBitmap
*bitmap
)
198 return bitmap
->qmp_locked
;
201 /* Called with BQL taken. */
202 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap
*bitmap
)
204 return !(bitmap
->disabled
|| bitmap
->successor
);
207 /* Called with BQL taken. */
208 DirtyBitmapStatus
bdrv_dirty_bitmap_status(BdrvDirtyBitmap
*bitmap
)
210 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
211 return DIRTY_BITMAP_STATUS_FROZEN
;
212 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
213 return DIRTY_BITMAP_STATUS_LOCKED
;
214 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
215 return DIRTY_BITMAP_STATUS_DISABLED
;
217 return DIRTY_BITMAP_STATUS_ACTIVE
;
222 * Create a successor bitmap destined to replace this bitmap after an operation.
223 * Requires that the bitmap is not frozen and has no successor.
224 * Called with BQL taken.
226 int bdrv_dirty_bitmap_create_successor(BlockDriverState
*bs
,
227 BdrvDirtyBitmap
*bitmap
, Error
**errp
)
229 uint64_t granularity
;
230 BdrvDirtyBitmap
*child
;
232 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
233 error_setg(errp
, "Cannot create a successor for a bitmap that is "
237 assert(!bitmap
->successor
);
239 /* Create an anonymous successor */
240 granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
241 child
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
246 /* Successor will be on or off based on our current state. */
247 child
->disabled
= bitmap
->disabled
;
249 /* Install the successor and freeze the parent */
250 bitmap
->successor
= child
;
254 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
)
256 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
257 bitmap
->disabled
= false;
260 /* Called with BQL taken. */
261 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap
*bitmap
)
263 assert(bitmap
->mutex
== bitmap
->successor
->mutex
);
264 qemu_mutex_lock(bitmap
->mutex
);
265 bdrv_enable_dirty_bitmap_locked(bitmap
->successor
);
266 qemu_mutex_unlock(bitmap
->mutex
);
269 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
270 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
)
272 assert(!bitmap
->active_iterators
);
273 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
274 assert(!bitmap
->meta
);
275 QLIST_REMOVE(bitmap
, list
);
276 hbitmap_free(bitmap
->bitmap
);
277 g_free(bitmap
->name
);
282 * For a bitmap with a successor, yield our name to the successor,
283 * delete the old bitmap, and return a handle to the new bitmap.
284 * Called with BQL taken.
286 BdrvDirtyBitmap
*bdrv_dirty_bitmap_abdicate(BlockDriverState
*bs
,
287 BdrvDirtyBitmap
*bitmap
,
291 BdrvDirtyBitmap
*successor
= bitmap
->successor
;
293 if (successor
== NULL
) {
294 error_setg(errp
, "Cannot relinquish control if "
295 "there's no successor present");
301 successor
->name
= name
;
302 bitmap
->successor
= NULL
;
303 successor
->persistent
= bitmap
->persistent
;
304 bitmap
->persistent
= false;
305 bdrv_release_dirty_bitmap(bs
, bitmap
);
311 * In cases of failure where we can no longer safely delete the parent,
312 * we may wish to re-join the parent and child/successor.
313 * The merged parent will be un-frozen, but not explicitly re-enabled.
314 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
316 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap_locked(BlockDriverState
*bs
,
317 BdrvDirtyBitmap
*parent
,
320 BdrvDirtyBitmap
*successor
= parent
->successor
;
323 error_setg(errp
, "Cannot reclaim a successor when none is present");
327 if (!hbitmap_merge(parent
->bitmap
, successor
->bitmap
, parent
->bitmap
)) {
328 error_setg(errp
, "Merging of parent and successor bitmap failed");
331 bdrv_release_dirty_bitmap_locked(successor
);
332 parent
->successor
= NULL
;
337 /* Called with BQL taken. */
338 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap(BlockDriverState
*bs
,
339 BdrvDirtyBitmap
*parent
,
342 BdrvDirtyBitmap
*ret
;
344 qemu_mutex_lock(parent
->mutex
);
345 ret
= bdrv_reclaim_dirty_bitmap_locked(bs
, parent
, errp
);
346 qemu_mutex_unlock(parent
->mutex
);
352 * Truncates _all_ bitmaps attached to a BDS.
353 * Called with BQL taken.
355 void bdrv_dirty_bitmap_truncate(BlockDriverState
*bs
, int64_t bytes
)
357 BdrvDirtyBitmap
*bitmap
;
359 bdrv_dirty_bitmaps_lock(bs
);
360 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
361 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
362 assert(!bitmap
->active_iterators
);
363 hbitmap_truncate(bitmap
->bitmap
, bytes
);
364 bitmap
->size
= bytes
;
366 bdrv_dirty_bitmaps_unlock(bs
);
369 /* Called with BQL taken. */
370 void bdrv_release_dirty_bitmap(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
)
372 bdrv_dirty_bitmaps_lock(bs
);
373 bdrv_release_dirty_bitmap_locked(bitmap
);
374 bdrv_dirty_bitmaps_unlock(bs
);
378 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
379 * There must not be any frozen bitmaps attached.
380 * This function does not remove persistent bitmaps from the storage.
381 * Called with BQL taken.
383 void bdrv_release_named_dirty_bitmaps(BlockDriverState
*bs
)
385 BdrvDirtyBitmap
*bm
, *next
;
387 bdrv_dirty_bitmaps_lock(bs
);
388 QLIST_FOREACH_SAFE(bm
, &bs
->dirty_bitmaps
, list
, next
) {
389 if (bdrv_dirty_bitmap_name(bm
)) {
390 bdrv_release_dirty_bitmap_locked(bm
);
393 bdrv_dirty_bitmaps_unlock(bs
);
397 * Remove persistent dirty bitmap from the storage if it exists.
398 * Absence of bitmap is not an error, because we have the following scenario:
399 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
400 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
402 * This function doesn't release corresponding BdrvDirtyBitmap.
404 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState
*bs
,
408 if (bs
->drv
&& bs
->drv
->bdrv_remove_persistent_dirty_bitmap
) {
409 bs
->drv
->bdrv_remove_persistent_dirty_bitmap(bs
, name
, errp
);
413 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
415 bdrv_dirty_bitmap_lock(bitmap
);
416 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
417 bitmap
->disabled
= true;
418 bdrv_dirty_bitmap_unlock(bitmap
);
421 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
423 bdrv_dirty_bitmap_lock(bitmap
);
424 bdrv_enable_dirty_bitmap_locked(bitmap
);
425 bdrv_dirty_bitmap_unlock(bitmap
);
428 BlockDirtyInfoList
*bdrv_query_dirty_bitmaps(BlockDriverState
*bs
)
431 BlockDirtyInfoList
*list
= NULL
;
432 BlockDirtyInfoList
**plist
= &list
;
434 bdrv_dirty_bitmaps_lock(bs
);
435 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
436 BlockDirtyInfo
*info
= g_new0(BlockDirtyInfo
, 1);
437 BlockDirtyInfoList
*entry
= g_new0(BlockDirtyInfoList
, 1);
438 info
->count
= bdrv_get_dirty_count(bm
);
439 info
->granularity
= bdrv_dirty_bitmap_granularity(bm
);
440 info
->has_name
= !!bm
->name
;
441 info
->name
= g_strdup(bm
->name
);
442 info
->status
= bdrv_dirty_bitmap_status(bm
);
445 plist
= &entry
->next
;
447 bdrv_dirty_bitmaps_unlock(bs
);
452 /* Called within bdrv_dirty_bitmap_lock..unlock */
453 bool bdrv_get_dirty_locked(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
,
457 return hbitmap_get(bitmap
->bitmap
, offset
);
464 * Chooses a default granularity based on the existing cluster size,
465 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
466 * is no cluster size information available.
468 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState
*bs
)
471 uint32_t granularity
;
473 if (bdrv_get_info(bs
, &bdi
) >= 0 && bdi
.cluster_size
> 0) {
474 granularity
= MAX(4096, bdi
.cluster_size
);
475 granularity
= MIN(65536, granularity
);
483 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap
*bitmap
)
485 return 1U << hbitmap_granularity(bitmap
->bitmap
);
488 BdrvDirtyBitmapIter
*bdrv_dirty_iter_new(BdrvDirtyBitmap
*bitmap
)
490 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
491 hbitmap_iter_init(&iter
->hbi
, bitmap
->bitmap
, 0);
492 iter
->bitmap
= bitmap
;
493 bitmap
->active_iterators
++;
497 BdrvDirtyBitmapIter
*bdrv_dirty_meta_iter_new(BdrvDirtyBitmap
*bitmap
)
499 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
500 hbitmap_iter_init(&iter
->hbi
, bitmap
->meta
, 0);
501 iter
->bitmap
= bitmap
;
502 bitmap
->active_iterators
++;
506 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter
*iter
)
511 assert(iter
->bitmap
->active_iterators
> 0);
512 iter
->bitmap
->active_iterators
--;
516 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter
*iter
)
518 return hbitmap_iter_next(&iter
->hbi
, true);
522 * Return the next consecutively dirty area in the dirty bitmap
523 * belonging to the given iterator @iter.
525 * @max_offset: Maximum value that may be returned for
527 * @offset: Will contain the start offset of the next dirty area
528 * @bytes: Will contain the length of the next dirty area
530 * Returns: True if a dirty area could be found before max_offset
531 * (which means that *offset and *bytes then contain valid
532 * values), false otherwise.
534 * Note that @iter is never advanced if false is returned. If an area
535 * is found (which means that true is returned), it will be advanced
538 bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter
*iter
, uint64_t max_offset
,
539 uint64_t *offset
, int *bytes
)
541 uint32_t granularity
= bdrv_dirty_bitmap_granularity(iter
->bitmap
);
542 uint64_t gran_max_offset
;
546 if (max_offset
== iter
->bitmap
->size
) {
547 /* If max_offset points to the image end, round it up by the
548 * bitmap granularity */
549 gran_max_offset
= ROUND_UP(max_offset
, granularity
);
551 gran_max_offset
= max_offset
;
554 ret
= hbitmap_iter_next(&iter
->hbi
, false);
555 if (ret
< 0 || ret
+ granularity
> gran_max_offset
) {
562 assert(granularity
<= INT_MAX
);
565 /* Advance iterator */
566 ret
= hbitmap_iter_next(&iter
->hbi
, true);
568 } while (ret
+ granularity
<= gran_max_offset
&&
569 hbitmap_iter_next(&iter
->hbi
, false) == ret
+ granularity
&&
570 size
<= INT_MAX
- granularity
);
572 *bytes
= MIN(size
, max_offset
- *offset
);
576 /* Called within bdrv_dirty_bitmap_lock..unlock */
577 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
,
578 int64_t offset
, int64_t bytes
)
580 assert(bdrv_dirty_bitmap_enabled(bitmap
));
581 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
582 hbitmap_set(bitmap
->bitmap
, offset
, bytes
);
585 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
586 int64_t offset
, int64_t bytes
)
588 bdrv_dirty_bitmap_lock(bitmap
);
589 bdrv_set_dirty_bitmap_locked(bitmap
, offset
, bytes
);
590 bdrv_dirty_bitmap_unlock(bitmap
);
593 /* Called within bdrv_dirty_bitmap_lock..unlock */
594 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
,
595 int64_t offset
, int64_t bytes
)
597 assert(bdrv_dirty_bitmap_enabled(bitmap
));
598 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
599 hbitmap_reset(bitmap
->bitmap
, offset
, bytes
);
602 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
603 int64_t offset
, int64_t bytes
)
605 bdrv_dirty_bitmap_lock(bitmap
);
606 bdrv_reset_dirty_bitmap_locked(bitmap
, offset
, bytes
);
607 bdrv_dirty_bitmap_unlock(bitmap
);
610 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
**out
)
612 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
613 bdrv_dirty_bitmap_lock(bitmap
);
615 hbitmap_reset_all(bitmap
->bitmap
);
617 HBitmap
*backup
= bitmap
->bitmap
;
618 bitmap
->bitmap
= hbitmap_alloc(bitmap
->size
,
619 hbitmap_granularity(backup
));
622 bdrv_dirty_bitmap_unlock(bitmap
);
625 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
*backup
)
627 HBitmap
*tmp
= bitmap
->bitmap
;
628 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
629 bitmap
->bitmap
= backup
;
633 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap
*bitmap
,
634 uint64_t offset
, uint64_t bytes
)
636 return hbitmap_serialization_size(bitmap
->bitmap
, offset
, bytes
);
639 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap
*bitmap
)
641 return hbitmap_serialization_align(bitmap
->bitmap
);
644 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap
*bitmap
,
645 uint8_t *buf
, uint64_t offset
,
648 hbitmap_serialize_part(bitmap
->bitmap
, buf
, offset
, bytes
);
651 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap
*bitmap
,
652 uint8_t *buf
, uint64_t offset
,
653 uint64_t bytes
, bool finish
)
655 hbitmap_deserialize_part(bitmap
->bitmap
, buf
, offset
, bytes
, finish
);
658 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap
*bitmap
,
659 uint64_t offset
, uint64_t bytes
,
662 hbitmap_deserialize_zeroes(bitmap
->bitmap
, offset
, bytes
, finish
);
665 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap
*bitmap
,
666 uint64_t offset
, uint64_t bytes
,
669 hbitmap_deserialize_ones(bitmap
->bitmap
, offset
, bytes
, finish
);
672 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap
*bitmap
)
674 hbitmap_deserialize_finish(bitmap
->bitmap
);
677 void bdrv_set_dirty(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
679 BdrvDirtyBitmap
*bitmap
;
681 if (QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
685 bdrv_dirty_bitmaps_lock(bs
);
686 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
687 if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
690 assert(!bdrv_dirty_bitmap_readonly(bitmap
));
691 hbitmap_set(bitmap
->bitmap
, offset
, bytes
);
693 bdrv_dirty_bitmaps_unlock(bs
);
697 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
699 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter
*iter
, int64_t offset
)
701 hbitmap_iter_init(&iter
->hbi
, iter
->hbi
.hb
, offset
);
704 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap
*bitmap
)
706 return hbitmap_count(bitmap
->bitmap
);
709 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap
*bitmap
)
711 return hbitmap_count(bitmap
->meta
);
714 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap
*bitmap
)
716 return bitmap
->readonly
;
719 /* Called with BQL taken. */
720 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap
*bitmap
, bool value
)
722 qemu_mutex_lock(bitmap
->mutex
);
723 bitmap
->readonly
= value
;
724 qemu_mutex_unlock(bitmap
->mutex
);
727 bool bdrv_has_readonly_bitmaps(BlockDriverState
*bs
)
730 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
739 /* Called with BQL taken. */
740 void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap
*bitmap
, bool persistent
)
742 qemu_mutex_lock(bitmap
->mutex
);
743 bitmap
->persistent
= persistent
;
744 qemu_mutex_unlock(bitmap
->mutex
);
747 /* Called with BQL taken. */
748 void bdrv_dirty_bitmap_set_migration(BdrvDirtyBitmap
*bitmap
, bool migration
)
750 qemu_mutex_lock(bitmap
->mutex
);
751 bitmap
->migration
= migration
;
752 qemu_mutex_unlock(bitmap
->mutex
);
755 bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap
*bitmap
)
757 return bitmap
->persistent
&& !bitmap
->migration
;
760 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState
*bs
)
763 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
764 if (bm
->persistent
&& !bm
->readonly
&& !bm
->migration
) {
772 BdrvDirtyBitmap
*bdrv_dirty_bitmap_next(BlockDriverState
*bs
,
773 BdrvDirtyBitmap
*bitmap
)
775 return bitmap
== NULL
? QLIST_FIRST(&bs
->dirty_bitmaps
) :
776 QLIST_NEXT(bitmap
, list
);
779 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap
*bitmap
, Error
**errp
)
781 return hbitmap_sha256(bitmap
->bitmap
, errp
);
784 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap
*bitmap
, uint64_t offset
)
786 return hbitmap_next_zero(bitmap
->bitmap
, offset
);
789 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap
*dest
, const BdrvDirtyBitmap
*src
,
790 HBitmap
**backup
, Error
**errp
)
794 /* only bitmaps from one bds are supported */
795 assert(dest
->mutex
== src
->mutex
);
797 qemu_mutex_lock(dest
->mutex
);
799 if (bdrv_dirty_bitmap_user_locked(dest
)) {
800 error_setg(errp
, "Bitmap '%s' is currently in use by another"
801 " operation and cannot be modified", dest
->name
);
805 if (bdrv_dirty_bitmap_readonly(dest
)) {
806 error_setg(errp
, "Bitmap '%s' is readonly and cannot be modified",
811 if (!hbitmap_can_merge(dest
->bitmap
, src
->bitmap
)) {
812 error_setg(errp
, "Bitmaps are incompatible and can't be merged");
817 *backup
= dest
->bitmap
;
818 dest
->bitmap
= hbitmap_alloc(dest
->size
, hbitmap_granularity(*backup
));
819 ret
= hbitmap_merge(*backup
, src
->bitmap
, dest
->bitmap
);
821 ret
= hbitmap_merge(dest
->bitmap
, src
->bitmap
, dest
->bitmap
);
826 qemu_mutex_unlock(dest
->mutex
);