nbd: Tolerate more errors to structured reply request
[qemu/ar7.git] / block / dirty-bitmap.c
blob134e0c9a0c8f1040aaef7ae18a7954818f444a0b
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 "trace.h"
27 #include "block/block_int.h"
28 #include "block/blockjob.h"
30 struct BdrvDirtyBitmap {
31 QemuMutex *mutex;
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
39 the device */
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
46 this flag is set. */
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 skip_store; /* We are either migrating or deleting this
52 * bitmap; it should not be stored on the next
53 * inactivation. */
54 QLIST_ENTRY(BdrvDirtyBitmap) list;
57 struct BdrvDirtyBitmapIter {
58 HBitmapIter hbi;
59 BdrvDirtyBitmap *bitmap;
62 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
64 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
67 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
69 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
72 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
74 qemu_mutex_lock(bitmap->mutex);
77 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
79 qemu_mutex_unlock(bitmap->mutex);
82 /* Called with BQL or dirty_bitmap lock taken. */
83 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
85 BdrvDirtyBitmap *bm;
87 assert(name);
88 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
89 if (bm->name && !strcmp(name, bm->name)) {
90 return bm;
93 return NULL;
96 /* Called with BQL taken. */
97 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
98 uint32_t granularity,
99 const char *name,
100 Error **errp)
102 int64_t bitmap_size;
103 BdrvDirtyBitmap *bitmap;
105 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
107 if (name && bdrv_find_dirty_bitmap(bs, name)) {
108 error_setg(errp, "Bitmap already exists: %s", name);
109 return NULL;
111 bitmap_size = bdrv_getlength(bs);
112 if (bitmap_size < 0) {
113 error_setg_errno(errp, -bitmap_size, "could not get length of device");
114 errno = -bitmap_size;
115 return NULL;
117 bitmap = g_new0(BdrvDirtyBitmap, 1);
118 bitmap->mutex = &bs->dirty_bitmap_mutex;
119 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
120 bitmap->size = bitmap_size;
121 bitmap->name = g_strdup(name);
122 bitmap->disabled = false;
123 bdrv_dirty_bitmaps_lock(bs);
124 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
125 bdrv_dirty_bitmaps_unlock(bs);
126 return bitmap;
129 /* bdrv_create_meta_dirty_bitmap
131 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
132 * when a dirty status bit in @bitmap is changed (either from reset to set or
133 * the other way around), its respective meta dirty bitmap bit will be marked
134 * dirty as well.
136 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
137 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
138 * track.
140 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
141 int chunk_size)
143 assert(!bitmap->meta);
144 qemu_mutex_lock(bitmap->mutex);
145 bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
146 chunk_size * BITS_PER_BYTE);
147 qemu_mutex_unlock(bitmap->mutex);
150 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
152 assert(bitmap->meta);
153 qemu_mutex_lock(bitmap->mutex);
154 hbitmap_free_meta(bitmap->bitmap);
155 bitmap->meta = NULL;
156 qemu_mutex_unlock(bitmap->mutex);
159 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
161 return bitmap->size;
164 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
166 return bitmap->name;
169 /* Called with BQL taken. */
170 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
172 return bitmap->successor;
175 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
177 return bitmap->busy;
180 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
182 qemu_mutex_lock(bitmap->mutex);
183 bitmap->busy = busy;
184 qemu_mutex_unlock(bitmap->mutex);
187 /* Called with BQL taken. */
188 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
190 return !bitmap->disabled;
194 * bdrv_dirty_bitmap_status: This API is now deprecated.
195 * Called with BQL taken.
197 * A BdrvDirtyBitmap can be in four possible user-visible states:
198 * (1) Active: successor is NULL, and disabled is false: full r/w mode
199 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
200 * guest writes are dropped, but monitor writes are possible,
201 * through commands like merge and clear.
202 * (3) Frozen: successor is not NULL.
203 * A frozen bitmap cannot be renamed, deleted, cleared, set,
204 * enabled, merged to, etc. A frozen bitmap can only abdicate()
205 * or reclaim().
206 * In this state, the anonymous successor bitmap may be either
207 * Active and recording writes from the guest (e.g. backup jobs),
208 * or it can be Disabled and not recording writes.
209 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
210 * in any way from the monitor.
211 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
212 * is unusable by QEMU. It can be deleted to remove it from
213 * the qcow2.
215 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
217 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
218 return DIRTY_BITMAP_STATUS_INCONSISTENT;
219 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
220 return DIRTY_BITMAP_STATUS_FROZEN;
221 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
222 return DIRTY_BITMAP_STATUS_LOCKED;
223 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
224 return DIRTY_BITMAP_STATUS_DISABLED;
225 } else {
226 return DIRTY_BITMAP_STATUS_ACTIVE;
230 /* Called with BQL taken. */
231 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
233 return !bitmap->disabled || (bitmap->successor &&
234 !bitmap->successor->disabled);
237 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
238 Error **errp)
240 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
241 error_setg(errp, "Bitmap '%s' is currently in use by another"
242 " operation and cannot be used", bitmap->name);
243 return -1;
246 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
247 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
248 bitmap->name);
249 return -1;
252 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
253 bdrv_dirty_bitmap_inconsistent(bitmap)) {
254 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
255 bitmap->name);
256 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
257 " this bitmap from disk");
258 return -1;
261 return 0;
265 * Create a successor bitmap destined to replace this bitmap after an operation.
266 * Requires that the bitmap is not marked busy and has no successor.
267 * The successor will be enabled if the parent bitmap was.
268 * Called with BQL taken.
270 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
271 BdrvDirtyBitmap *bitmap, Error **errp)
273 uint64_t granularity;
274 BdrvDirtyBitmap *child;
276 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
277 return -1;
279 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
280 error_setg(errp, "Cannot create a successor for a bitmap that already "
281 "has one");
282 return -1;
285 /* Create an anonymous successor */
286 granularity = bdrv_dirty_bitmap_granularity(bitmap);
287 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
288 if (!child) {
289 return -1;
292 /* Successor will be on or off based on our current state. */
293 child->disabled = bitmap->disabled;
294 bitmap->disabled = true;
296 /* Install the successor and mark the parent as busy */
297 bitmap->successor = child;
298 bitmap->busy = true;
299 return 0;
302 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
304 bitmap->disabled = false;
307 /* Called with BQL taken. */
308 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
310 assert(bitmap->mutex == bitmap->successor->mutex);
311 qemu_mutex_lock(bitmap->mutex);
312 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
313 qemu_mutex_unlock(bitmap->mutex);
316 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
317 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
319 assert(!bitmap->active_iterators);
320 assert(!bdrv_dirty_bitmap_busy(bitmap));
321 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
322 assert(!bitmap->meta);
323 QLIST_REMOVE(bitmap, list);
324 hbitmap_free(bitmap->bitmap);
325 g_free(bitmap->name);
326 g_free(bitmap);
330 * For a bitmap with a successor, yield our name to the successor,
331 * delete the old bitmap, and return a handle to the new bitmap.
332 * Called with BQL taken.
334 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
335 BdrvDirtyBitmap *bitmap,
336 Error **errp)
338 char *name;
339 BdrvDirtyBitmap *successor = bitmap->successor;
341 if (successor == NULL) {
342 error_setg(errp, "Cannot relinquish control if "
343 "there's no successor present");
344 return NULL;
347 name = bitmap->name;
348 bitmap->name = NULL;
349 successor->name = name;
350 bitmap->successor = NULL;
351 successor->persistent = bitmap->persistent;
352 bitmap->persistent = false;
353 bitmap->busy = false;
354 bdrv_release_dirty_bitmap(bs, bitmap);
356 return successor;
360 * In cases of failure where we can no longer safely delete the parent,
361 * we may wish to re-join the parent and child/successor.
362 * The merged parent will be marked as not busy.
363 * The marged parent will be enabled if and only if the successor was enabled.
364 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
366 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
367 BdrvDirtyBitmap *parent,
368 Error **errp)
370 BdrvDirtyBitmap *successor = parent->successor;
372 if (!successor) {
373 error_setg(errp, "Cannot reclaim a successor when none is present");
374 return NULL;
377 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
378 error_setg(errp, "Merging of parent and successor bitmap failed");
379 return NULL;
382 parent->disabled = successor->disabled;
383 parent->busy = false;
384 bdrv_release_dirty_bitmap_locked(successor);
385 parent->successor = NULL;
387 return parent;
390 /* Called with BQL taken. */
391 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
392 BdrvDirtyBitmap *parent,
393 Error **errp)
395 BdrvDirtyBitmap *ret;
397 qemu_mutex_lock(parent->mutex);
398 ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
399 qemu_mutex_unlock(parent->mutex);
401 return ret;
405 * Truncates _all_ bitmaps attached to a BDS.
406 * Called with BQL taken.
408 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
410 BdrvDirtyBitmap *bitmap;
412 bdrv_dirty_bitmaps_lock(bs);
413 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
414 assert(!bdrv_dirty_bitmap_busy(bitmap));
415 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
416 assert(!bitmap->active_iterators);
417 hbitmap_truncate(bitmap->bitmap, bytes);
418 bitmap->size = bytes;
420 bdrv_dirty_bitmaps_unlock(bs);
423 /* Called with BQL taken. */
424 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
426 bdrv_dirty_bitmaps_lock(bs);
427 bdrv_release_dirty_bitmap_locked(bitmap);
428 bdrv_dirty_bitmaps_unlock(bs);
432 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
433 * There must not be any busy bitmaps attached.
434 * This function does not remove persistent bitmaps from the storage.
435 * Called with BQL taken.
437 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
439 BdrvDirtyBitmap *bm, *next;
441 bdrv_dirty_bitmaps_lock(bs);
442 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
443 if (bdrv_dirty_bitmap_name(bm)) {
444 bdrv_release_dirty_bitmap_locked(bm);
447 bdrv_dirty_bitmaps_unlock(bs);
451 * Remove persistent dirty bitmap from the storage if it exists.
452 * Absence of bitmap is not an error, because we have the following scenario:
453 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
454 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
455 * not fail.
456 * This function doesn't release corresponding BdrvDirtyBitmap.
458 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
459 const char *name,
460 Error **errp)
462 if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
463 bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
467 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
469 bdrv_dirty_bitmap_lock(bitmap);
470 bitmap->disabled = true;
471 bdrv_dirty_bitmap_unlock(bitmap);
474 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
476 bdrv_dirty_bitmap_lock(bitmap);
477 bdrv_enable_dirty_bitmap_locked(bitmap);
478 bdrv_dirty_bitmap_unlock(bitmap);
481 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
483 BdrvDirtyBitmap *bm;
484 BlockDirtyInfoList *list = NULL;
485 BlockDirtyInfoList **plist = &list;
487 bdrv_dirty_bitmaps_lock(bs);
488 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
489 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
490 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
491 info->count = bdrv_get_dirty_count(bm);
492 info->granularity = bdrv_dirty_bitmap_granularity(bm);
493 info->has_name = !!bm->name;
494 info->name = g_strdup(bm->name);
495 info->status = bdrv_dirty_bitmap_status(bm);
496 info->recording = bdrv_dirty_bitmap_recording(bm);
497 info->busy = bdrv_dirty_bitmap_busy(bm);
498 info->persistent = bm->persistent;
499 info->has_inconsistent = bm->inconsistent;
500 info->inconsistent = bm->inconsistent;
501 entry->value = info;
502 *plist = entry;
503 plist = &entry->next;
505 bdrv_dirty_bitmaps_unlock(bs);
507 return list;
510 /* Called within bdrv_dirty_bitmap_lock..unlock */
511 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
513 return hbitmap_get(bitmap->bitmap, offset);
516 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
518 bool ret;
519 bdrv_dirty_bitmap_lock(bitmap);
520 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
521 bdrv_dirty_bitmap_unlock(bitmap);
523 return ret;
527 * Chooses a default granularity based on the existing cluster size,
528 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
529 * is no cluster size information available.
531 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
533 BlockDriverInfo bdi;
534 uint32_t granularity;
536 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
537 granularity = MAX(4096, bdi.cluster_size);
538 granularity = MIN(65536, granularity);
539 } else {
540 granularity = 65536;
543 return granularity;
546 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
548 return 1U << hbitmap_granularity(bitmap->bitmap);
551 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
553 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
554 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
555 iter->bitmap = bitmap;
556 bitmap->active_iterators++;
557 return iter;
560 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
562 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
563 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
564 iter->bitmap = bitmap;
565 bitmap->active_iterators++;
566 return iter;
569 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
571 if (!iter) {
572 return;
574 assert(iter->bitmap->active_iterators > 0);
575 iter->bitmap->active_iterators--;
576 g_free(iter);
579 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
581 return hbitmap_iter_next(&iter->hbi);
584 /* Called within bdrv_dirty_bitmap_lock..unlock */
585 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
586 int64_t offset, int64_t bytes)
588 assert(!bdrv_dirty_bitmap_readonly(bitmap));
589 hbitmap_set(bitmap->bitmap, offset, bytes);
592 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
593 int64_t offset, int64_t bytes)
595 bdrv_dirty_bitmap_lock(bitmap);
596 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
597 bdrv_dirty_bitmap_unlock(bitmap);
600 /* Called within bdrv_dirty_bitmap_lock..unlock */
601 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
602 int64_t offset, int64_t bytes)
604 assert(!bdrv_dirty_bitmap_readonly(bitmap));
605 hbitmap_reset(bitmap->bitmap, offset, bytes);
608 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
609 int64_t offset, int64_t bytes)
611 bdrv_dirty_bitmap_lock(bitmap);
612 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
613 bdrv_dirty_bitmap_unlock(bitmap);
616 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
618 assert(!bdrv_dirty_bitmap_readonly(bitmap));
619 bdrv_dirty_bitmap_lock(bitmap);
620 if (!out) {
621 hbitmap_reset_all(bitmap->bitmap);
622 } else {
623 HBitmap *backup = bitmap->bitmap;
624 bitmap->bitmap = hbitmap_alloc(bitmap->size,
625 hbitmap_granularity(backup));
626 *out = backup;
628 bdrv_dirty_bitmap_unlock(bitmap);
631 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
633 HBitmap *tmp = bitmap->bitmap;
634 assert(!bdrv_dirty_bitmap_readonly(bitmap));
635 bitmap->bitmap = backup;
636 hbitmap_free(tmp);
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,
652 uint64_t bytes)
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,
666 bool finish)
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,
673 bool finish)
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)) {
688 return;
691 bdrv_dirty_bitmaps_lock(bs);
692 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
693 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
694 continue;
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)
735 BdrvDirtyBitmap *bm;
736 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
737 if (bm->readonly) {
738 return true;
742 return false;
745 /* Called with BQL taken. */
746 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
748 qemu_mutex_lock(bitmap->mutex);
749 bitmap->persistent = persistent;
750 qemu_mutex_unlock(bitmap->mutex);
753 /* Called with BQL taken. */
754 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
756 qemu_mutex_lock(bitmap->mutex);
757 assert(bitmap->persistent == true);
758 bitmap->inconsistent = true;
759 bitmap->disabled = true;
760 qemu_mutex_unlock(bitmap->mutex);
763 /* Called with BQL taken. */
764 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
766 qemu_mutex_lock(bitmap->mutex);
767 bitmap->skip_store = skip;
768 qemu_mutex_unlock(bitmap->mutex);
771 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
773 return bitmap->persistent && !bitmap->skip_store;
776 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
778 return bitmap->inconsistent;
781 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
783 BdrvDirtyBitmap *bm;
784 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
785 if (bm->persistent && !bm->readonly && !bm->skip_store) {
786 return true;
790 return false;
793 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
794 BdrvDirtyBitmap *bitmap)
796 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
797 QLIST_NEXT(bitmap, list);
800 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
802 return hbitmap_sha256(bitmap->bitmap, errp);
805 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
806 uint64_t bytes)
808 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
811 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
812 uint64_t *offset, uint64_t *bytes)
814 return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
818 * bdrv_merge_dirty_bitmap: merge src into dest.
819 * Ensures permissions on bitmaps are reasonable; use for public API.
821 * @backup: If provided, make a copy of dest here prior to merge.
823 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
824 HBitmap **backup, Error **errp)
826 bool ret;
828 qemu_mutex_lock(dest->mutex);
829 if (src->mutex != dest->mutex) {
830 qemu_mutex_lock(src->mutex);
833 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
834 goto out;
837 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
838 goto out;
841 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
842 error_setg(errp, "Bitmaps are incompatible and can't be merged");
843 goto out;
846 ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
847 assert(ret);
849 out:
850 qemu_mutex_unlock(dest->mutex);
851 if (src->mutex != dest->mutex) {
852 qemu_mutex_unlock(src->mutex);
857 * bdrv_dirty_bitmap_merge_internal: merge src into dest.
858 * Does NOT check bitmap permissions; not suitable for use as public API.
860 * @backup: If provided, make a copy of dest here prior to merge.
861 * @lock: If true, lock and unlock bitmaps on the way in/out.
862 * returns true if the merge succeeded; false if unattempted.
864 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
865 const BdrvDirtyBitmap *src,
866 HBitmap **backup,
867 bool lock)
869 bool ret;
871 assert(!bdrv_dirty_bitmap_readonly(dest));
872 assert(!bdrv_dirty_bitmap_inconsistent(dest));
873 assert(!bdrv_dirty_bitmap_inconsistent(src));
875 if (lock) {
876 qemu_mutex_lock(dest->mutex);
877 if (src->mutex != dest->mutex) {
878 qemu_mutex_lock(src->mutex);
882 if (backup) {
883 *backup = dest->bitmap;
884 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
885 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
886 } else {
887 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
890 if (lock) {
891 qemu_mutex_unlock(dest->mutex);
892 if (src->mutex != dest->mutex) {
893 qemu_mutex_unlock(src->mutex);
897 return ret;