ivshmem: Fix unplug of device "ivshmem-plain"
[qemu/armbru.git] / block / dirty-bitmap.c
blobc9b8a6fd5201cd7a3bfc6cdcbc89f9a99ddb2363
1 /*
2 * Block Dirty Bitmap
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
27 #include "trace.h"
28 #include "block/block_int.h"
29 #include "block/blockjob.h"
31 /**
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 {
40 QemuMutex *mutex;
41 HBitmap *bitmap; /* Dirty bitmap implementation */
42 HBitmap *meta; /* Meta dirty bitmap */
43 bool qmp_locked; /* Bitmap is locked, it can't be modified
44 through QMP */
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
49 the device */
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
56 this flag is set. */
57 bool persistent; /* bitmap must be saved to owner disk image */
58 QLIST_ENTRY(BdrvDirtyBitmap) list;
61 struct BdrvDirtyBitmapIter {
62 HBitmapIter hbi;
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)
89 BdrvDirtyBitmap *bm;
91 assert(name);
92 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
93 if (bm->name && !strcmp(name, bm->name)) {
94 return bm;
97 return NULL;
100 /* Called with BQL taken. */
101 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
102 uint32_t granularity,
103 const char *name,
104 Error **errp)
106 int64_t bitmap_size;
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);
113 return NULL;
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;
119 return NULL;
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);
130 return bitmap;
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
138 * dirty as well.
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
142 * track.
144 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
145 int chunk_size)
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);
159 bitmap->meta = NULL;
160 qemu_mutex_unlock(bitmap->mutex);
163 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
165 return bitmap->size;
168 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
170 return bitmap->name;
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;
206 } else {
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 "
224 "currently frozen");
225 return -1;
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);
232 if (!child) {
233 return -1;
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;
241 return 0;
244 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
246 assert(!bdrv_dirty_bitmap_frozen(bitmap));
247 bitmap->disabled = false;
250 /* Called with BQL taken. */
251 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
253 assert(bitmap->mutex == bitmap->successor->mutex);
254 qemu_mutex_lock(bitmap->mutex);
255 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
256 qemu_mutex_unlock(bitmap->mutex);
259 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
260 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
262 assert(!bitmap->active_iterators);
263 assert(!bdrv_dirty_bitmap_frozen(bitmap));
264 assert(!bitmap->meta);
265 QLIST_REMOVE(bitmap, list);
266 hbitmap_free(bitmap->bitmap);
267 g_free(bitmap->name);
268 g_free(bitmap);
272 * For a bitmap with a successor, yield our name to the successor,
273 * delete the old bitmap, and return a handle to the new bitmap.
274 * Called with BQL taken.
276 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
277 BdrvDirtyBitmap *bitmap,
278 Error **errp)
280 char *name;
281 BdrvDirtyBitmap *successor = bitmap->successor;
283 if (successor == NULL) {
284 error_setg(errp, "Cannot relinquish control if "
285 "there's no successor present");
286 return NULL;
289 name = bitmap->name;
290 bitmap->name = NULL;
291 successor->name = name;
292 bitmap->successor = NULL;
293 successor->persistent = bitmap->persistent;
294 bitmap->persistent = false;
295 bdrv_release_dirty_bitmap(bs, bitmap);
297 return successor;
301 * In cases of failure where we can no longer safely delete the parent,
302 * we may wish to re-join the parent and child/successor.
303 * The merged parent will be un-frozen, but not explicitly re-enabled.
304 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
306 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
307 BdrvDirtyBitmap *parent,
308 Error **errp)
310 BdrvDirtyBitmap *successor = parent->successor;
312 if (!successor) {
313 error_setg(errp, "Cannot reclaim a successor when none is present");
314 return NULL;
317 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
318 error_setg(errp, "Merging of parent and successor bitmap failed");
319 return NULL;
321 bdrv_release_dirty_bitmap_locked(successor);
322 parent->successor = NULL;
324 return parent;
327 /* Called with BQL taken. */
328 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
329 BdrvDirtyBitmap *parent,
330 Error **errp)
332 BdrvDirtyBitmap *ret;
334 qemu_mutex_lock(parent->mutex);
335 ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
336 qemu_mutex_unlock(parent->mutex);
338 return ret;
342 * Truncates _all_ bitmaps attached to a BDS.
343 * Called with BQL taken.
345 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
347 BdrvDirtyBitmap *bitmap;
349 bdrv_dirty_bitmaps_lock(bs);
350 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
351 assert(!bdrv_dirty_bitmap_frozen(bitmap));
352 assert(!bitmap->active_iterators);
353 hbitmap_truncate(bitmap->bitmap, bytes);
354 bitmap->size = bytes;
356 bdrv_dirty_bitmaps_unlock(bs);
359 /* Called with BQL taken. */
360 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
362 bdrv_dirty_bitmaps_lock(bs);
363 bdrv_release_dirty_bitmap_locked(bitmap);
364 bdrv_dirty_bitmaps_unlock(bs);
368 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
369 * There must not be any frozen bitmaps attached.
370 * This function does not remove persistent bitmaps from the storage.
371 * Called with BQL taken.
373 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
375 BdrvDirtyBitmap *bm, *next;
377 bdrv_dirty_bitmaps_lock(bs);
378 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
379 if (bdrv_dirty_bitmap_name(bm)) {
380 bdrv_release_dirty_bitmap_locked(bm);
383 bdrv_dirty_bitmaps_unlock(bs);
387 * Release all persistent dirty bitmaps attached to a BDS (for use in
388 * bdrv_inactivate_recurse()).
389 * There must not be any frozen bitmaps attached.
390 * This function does not remove persistent bitmaps from the storage.
391 * Called with BQL taken.
393 void bdrv_release_persistent_dirty_bitmaps(BlockDriverState *bs)
395 BdrvDirtyBitmap *bm, *next;
397 bdrv_dirty_bitmaps_lock(bs);
398 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
399 if (bdrv_dirty_bitmap_get_persistance(bm)) {
400 bdrv_release_dirty_bitmap_locked(bm);
403 bdrv_dirty_bitmaps_unlock(bs);
407 * Remove persistent dirty bitmap from the storage if it exists.
408 * Absence of bitmap is not an error, because we have the following scenario:
409 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
410 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
411 * not fail.
412 * This function doesn't release corresponding BdrvDirtyBitmap.
414 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
415 const char *name,
416 Error **errp)
418 if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
419 bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
423 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
425 bdrv_dirty_bitmap_lock(bitmap);
426 assert(!bdrv_dirty_bitmap_frozen(bitmap));
427 bitmap->disabled = true;
428 bdrv_dirty_bitmap_unlock(bitmap);
431 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
433 bdrv_dirty_bitmap_lock(bitmap);
434 bdrv_enable_dirty_bitmap_locked(bitmap);
435 bdrv_dirty_bitmap_unlock(bitmap);
438 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
440 BdrvDirtyBitmap *bm;
441 BlockDirtyInfoList *list = NULL;
442 BlockDirtyInfoList **plist = &list;
444 bdrv_dirty_bitmaps_lock(bs);
445 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
446 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
447 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
448 info->count = bdrv_get_dirty_count(bm);
449 info->granularity = bdrv_dirty_bitmap_granularity(bm);
450 info->has_name = !!bm->name;
451 info->name = g_strdup(bm->name);
452 info->status = bdrv_dirty_bitmap_status(bm);
453 entry->value = info;
454 *plist = entry;
455 plist = &entry->next;
457 bdrv_dirty_bitmaps_unlock(bs);
459 return list;
462 /* Called within bdrv_dirty_bitmap_lock..unlock */
463 bool bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
464 int64_t offset)
466 if (bitmap) {
467 return hbitmap_get(bitmap->bitmap, offset);
468 } else {
469 return false;
474 * Chooses a default granularity based on the existing cluster size,
475 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
476 * is no cluster size information available.
478 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
480 BlockDriverInfo bdi;
481 uint32_t granularity;
483 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
484 granularity = MAX(4096, bdi.cluster_size);
485 granularity = MIN(65536, granularity);
486 } else {
487 granularity = 65536;
490 return granularity;
493 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
495 return 1U << hbitmap_granularity(bitmap->bitmap);
498 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
500 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
501 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
502 iter->bitmap = bitmap;
503 bitmap->active_iterators++;
504 return iter;
507 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
509 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
510 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
511 iter->bitmap = bitmap;
512 bitmap->active_iterators++;
513 return iter;
516 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
518 if (!iter) {
519 return;
521 assert(iter->bitmap->active_iterators > 0);
522 iter->bitmap->active_iterators--;
523 g_free(iter);
526 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
528 return hbitmap_iter_next(&iter->hbi, true);
532 * Return the next consecutively dirty area in the dirty bitmap
533 * belonging to the given iterator @iter.
535 * @max_offset: Maximum value that may be returned for
536 * *offset + *bytes
537 * @offset: Will contain the start offset of the next dirty area
538 * @bytes: Will contain the length of the next dirty area
540 * Returns: True if a dirty area could be found before max_offset
541 * (which means that *offset and *bytes then contain valid
542 * values), false otherwise.
544 * Note that @iter is never advanced if false is returned. If an area
545 * is found (which means that true is returned), it will be advanced
546 * past that area.
548 bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
549 uint64_t *offset, int *bytes)
551 uint32_t granularity = bdrv_dirty_bitmap_granularity(iter->bitmap);
552 uint64_t gran_max_offset;
553 int64_t ret;
554 int size;
556 if (max_offset == iter->bitmap->size) {
557 /* If max_offset points to the image end, round it up by the
558 * bitmap granularity */
559 gran_max_offset = ROUND_UP(max_offset, granularity);
560 } else {
561 gran_max_offset = max_offset;
564 ret = hbitmap_iter_next(&iter->hbi, false);
565 if (ret < 0 || ret + granularity > gran_max_offset) {
566 return false;
569 *offset = ret;
570 size = 0;
572 assert(granularity <= INT_MAX);
574 do {
575 /* Advance iterator */
576 ret = hbitmap_iter_next(&iter->hbi, true);
577 size += granularity;
578 } while (ret + granularity <= gran_max_offset &&
579 hbitmap_iter_next(&iter->hbi, false) == ret + granularity &&
580 size <= INT_MAX - granularity);
582 *bytes = MIN(size, max_offset - *offset);
583 return true;
586 /* Called within bdrv_dirty_bitmap_lock..unlock */
587 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
588 int64_t offset, int64_t bytes)
590 assert(bdrv_dirty_bitmap_enabled(bitmap));
591 assert(!bdrv_dirty_bitmap_readonly(bitmap));
592 hbitmap_set(bitmap->bitmap, offset, bytes);
595 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
596 int64_t offset, int64_t bytes)
598 bdrv_dirty_bitmap_lock(bitmap);
599 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
600 bdrv_dirty_bitmap_unlock(bitmap);
603 /* Called within bdrv_dirty_bitmap_lock..unlock */
604 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
605 int64_t offset, int64_t bytes)
607 assert(bdrv_dirty_bitmap_enabled(bitmap));
608 assert(!bdrv_dirty_bitmap_readonly(bitmap));
609 hbitmap_reset(bitmap->bitmap, offset, bytes);
612 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
613 int64_t offset, int64_t bytes)
615 bdrv_dirty_bitmap_lock(bitmap);
616 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
617 bdrv_dirty_bitmap_unlock(bitmap);
620 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
622 assert(bdrv_dirty_bitmap_enabled(bitmap));
623 assert(!bdrv_dirty_bitmap_readonly(bitmap));
624 bdrv_dirty_bitmap_lock(bitmap);
625 if (!out) {
626 hbitmap_reset_all(bitmap->bitmap);
627 } else {
628 HBitmap *backup = bitmap->bitmap;
629 bitmap->bitmap = hbitmap_alloc(bitmap->size,
630 hbitmap_granularity(backup));
631 *out = backup;
633 bdrv_dirty_bitmap_unlock(bitmap);
636 void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
638 HBitmap *tmp = bitmap->bitmap;
639 assert(bdrv_dirty_bitmap_enabled(bitmap));
640 assert(!bdrv_dirty_bitmap_readonly(bitmap));
641 bitmap->bitmap = in;
642 hbitmap_free(tmp);
645 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
646 uint64_t offset, uint64_t bytes)
648 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
651 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
653 return hbitmap_serialization_align(bitmap->bitmap);
656 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
657 uint8_t *buf, uint64_t offset,
658 uint64_t bytes)
660 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
663 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
664 uint8_t *buf, uint64_t offset,
665 uint64_t bytes, bool finish)
667 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
670 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
671 uint64_t offset, uint64_t bytes,
672 bool finish)
674 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
677 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
678 uint64_t offset, uint64_t bytes,
679 bool finish)
681 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
684 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
686 hbitmap_deserialize_finish(bitmap->bitmap);
689 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
691 BdrvDirtyBitmap *bitmap;
693 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
694 return;
697 bdrv_dirty_bitmaps_lock(bs);
698 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
699 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
700 continue;
702 assert(!bdrv_dirty_bitmap_readonly(bitmap));
703 hbitmap_set(bitmap->bitmap, offset, bytes);
705 bdrv_dirty_bitmaps_unlock(bs);
709 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
711 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
713 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
716 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
718 return hbitmap_count(bitmap->bitmap);
721 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
723 return hbitmap_count(bitmap->meta);
726 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
728 return bitmap->readonly;
731 /* Called with BQL taken. */
732 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
734 qemu_mutex_lock(bitmap->mutex);
735 bitmap->readonly = value;
736 qemu_mutex_unlock(bitmap->mutex);
739 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
741 BdrvDirtyBitmap *bm;
742 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
743 if (bm->readonly) {
744 return true;
748 return false;
751 /* Called with BQL taken. */
752 void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap *bitmap, bool persistent)
754 qemu_mutex_lock(bitmap->mutex);
755 bitmap->persistent = persistent;
756 qemu_mutex_unlock(bitmap->mutex);
759 bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap *bitmap)
761 return bitmap->persistent;
764 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
766 BdrvDirtyBitmap *bm;
767 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
768 if (bm->persistent && !bm->readonly) {
769 return true;
773 return false;
776 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
777 BdrvDirtyBitmap *bitmap)
779 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
780 QLIST_NEXT(bitmap, list);
783 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
785 return hbitmap_sha256(bitmap->bitmap, errp);
788 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset)
790 return hbitmap_next_zero(bitmap->bitmap, offset);
793 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
794 Error **errp)
796 /* only bitmaps from one bds are supported */
797 assert(dest->mutex == src->mutex);
799 qemu_mutex_lock(dest->mutex);
801 assert(bdrv_dirty_bitmap_enabled(dest));
802 assert(!bdrv_dirty_bitmap_readonly(dest));
804 if (!hbitmap_merge(dest->bitmap, src->bitmap)) {
805 error_setg(errp, "Bitmaps are incompatible and can't be merged");
808 qemu_mutex_unlock(dest->mutex);