target/arm: Enable FPU for Cortex-M4 and Cortex-M33
[qemu/ar7.git] / block / dirty-bitmap.c
blob59e6ebb8618330fd3ee02bb531438e406986c025
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 struct BdrvDirtyBitmap {
32 QemuMutex *mutex;
33 HBitmap *bitmap; /* Dirty bitmap implementation */
34 HBitmap *meta; /* Meta dirty bitmap */
35 bool busy; /* Bitmap is busy, it can't be used via QMP */
36 BdrvDirtyBitmap *successor; /* Anonymous child, if any. */
37 char *name; /* Optional non-empty unique ID */
38 int64_t size; /* Size of the bitmap, in bytes */
39 bool disabled; /* Bitmap is disabled. It ignores all writes to
40 the device */
41 int active_iterators; /* How many iterators are active */
42 bool readonly; /* Bitmap is read-only. This field also
43 prevents the respective image from being
44 modified (i.e. blocks writes and discards).
45 Such operations must fail and both the image
46 and this bitmap must remain unchanged while
47 this flag is set. */
48 bool persistent; /* bitmap must be saved to owner disk image */
49 bool inconsistent; /* bitmap is persistent, but inconsistent.
50 It cannot be used at all in any way, except
51 a QMP user can remove it. */
52 bool migration; /* Bitmap is selected for migration, it should
53 not be stored on the next inactivation
54 (persistent flag doesn't matter until next
55 invalidation).*/
56 QLIST_ENTRY(BdrvDirtyBitmap) list;
59 struct BdrvDirtyBitmapIter {
60 HBitmapIter hbi;
61 BdrvDirtyBitmap *bitmap;
64 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
66 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
69 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
71 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
74 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
76 qemu_mutex_lock(bitmap->mutex);
79 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
81 qemu_mutex_unlock(bitmap->mutex);
84 /* Called with BQL or dirty_bitmap lock taken. */
85 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
87 BdrvDirtyBitmap *bm;
89 assert(name);
90 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
91 if (bm->name && !strcmp(name, bm->name)) {
92 return bm;
95 return NULL;
98 /* Called with BQL taken. */
99 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
100 uint32_t granularity,
101 const char *name,
102 Error **errp)
104 int64_t bitmap_size;
105 BdrvDirtyBitmap *bitmap;
107 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
109 if (name && bdrv_find_dirty_bitmap(bs, name)) {
110 error_setg(errp, "Bitmap already exists: %s", name);
111 return NULL;
113 bitmap_size = bdrv_getlength(bs);
114 if (bitmap_size < 0) {
115 error_setg_errno(errp, -bitmap_size, "could not get length of device");
116 errno = -bitmap_size;
117 return NULL;
119 bitmap = g_new0(BdrvDirtyBitmap, 1);
120 bitmap->mutex = &bs->dirty_bitmap_mutex;
121 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
122 bitmap->size = bitmap_size;
123 bitmap->name = g_strdup(name);
124 bitmap->disabled = false;
125 bdrv_dirty_bitmaps_lock(bs);
126 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
127 bdrv_dirty_bitmaps_unlock(bs);
128 return bitmap;
131 /* bdrv_create_meta_dirty_bitmap
133 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
134 * when a dirty status bit in @bitmap is changed (either from reset to set or
135 * the other way around), its respective meta dirty bitmap bit will be marked
136 * dirty as well.
138 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
139 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
140 * track.
142 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
143 int chunk_size)
145 assert(!bitmap->meta);
146 qemu_mutex_lock(bitmap->mutex);
147 bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
148 chunk_size * BITS_PER_BYTE);
149 qemu_mutex_unlock(bitmap->mutex);
152 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
154 assert(bitmap->meta);
155 qemu_mutex_lock(bitmap->mutex);
156 hbitmap_free_meta(bitmap->bitmap);
157 bitmap->meta = NULL;
158 qemu_mutex_unlock(bitmap->mutex);
161 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
163 return bitmap->size;
166 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
168 return bitmap->name;
171 /* Called with BQL taken. */
172 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
174 return bitmap->successor;
177 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
179 return bitmap->busy;
182 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
184 qemu_mutex_lock(bitmap->mutex);
185 bitmap->busy = busy;
186 qemu_mutex_unlock(bitmap->mutex);
189 /* Called with BQL taken. */
190 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
192 return !bitmap->disabled;
196 * bdrv_dirty_bitmap_status: This API is now deprecated.
197 * Called with BQL taken.
199 * A BdrvDirtyBitmap can be in four possible user-visible states:
200 * (1) Active: successor is NULL, and disabled is false: full r/w mode
201 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
202 * guest writes are dropped, but monitor writes are possible,
203 * through commands like merge and clear.
204 * (3) Frozen: successor is not NULL.
205 * A frozen bitmap cannot be renamed, deleted, cleared, set,
206 * enabled, merged to, etc. A frozen bitmap can only abdicate()
207 * or reclaim().
208 * In this state, the anonymous successor bitmap may be either
209 * Active and recording writes from the guest (e.g. backup jobs),
210 * or it can be Disabled and not recording writes.
211 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
212 * in any way from the monitor.
213 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
214 * is unusable by QEMU. It can be deleted to remove it from
215 * the qcow2.
217 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
219 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
220 return DIRTY_BITMAP_STATUS_INCONSISTENT;
221 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
222 return DIRTY_BITMAP_STATUS_FROZEN;
223 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
224 return DIRTY_BITMAP_STATUS_LOCKED;
225 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
226 return DIRTY_BITMAP_STATUS_DISABLED;
227 } else {
228 return DIRTY_BITMAP_STATUS_ACTIVE;
232 /* Called with BQL taken. */
233 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
235 return !bitmap->disabled || (bitmap->successor &&
236 !bitmap->successor->disabled);
239 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
240 Error **errp)
242 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
243 error_setg(errp, "Bitmap '%s' is currently in use by another"
244 " operation and cannot be used", bitmap->name);
245 return -1;
248 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
249 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
250 bitmap->name);
251 return -1;
254 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
255 bdrv_dirty_bitmap_inconsistent(bitmap)) {
256 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
257 bitmap->name);
258 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
259 " this bitmap from disk");
260 return -1;
263 return 0;
267 * Create a successor bitmap destined to replace this bitmap after an operation.
268 * Requires that the bitmap is not marked busy and has no successor.
269 * The successor will be enabled if the parent bitmap was.
270 * Called with BQL taken.
272 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
273 BdrvDirtyBitmap *bitmap, Error **errp)
275 uint64_t granularity;
276 BdrvDirtyBitmap *child;
278 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
279 return -1;
281 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
282 error_setg(errp, "Cannot create a successor for a bitmap that already "
283 "has one");
284 return -1;
287 /* Create an anonymous successor */
288 granularity = bdrv_dirty_bitmap_granularity(bitmap);
289 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
290 if (!child) {
291 return -1;
294 /* Successor will be on or off based on our current state. */
295 child->disabled = bitmap->disabled;
296 bitmap->disabled = true;
298 /* Install the successor and mark the parent as busy */
299 bitmap->successor = child;
300 bitmap->busy = true;
301 return 0;
304 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
306 bitmap->disabled = false;
309 /* Called with BQL taken. */
310 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
312 assert(bitmap->mutex == bitmap->successor->mutex);
313 qemu_mutex_lock(bitmap->mutex);
314 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
315 qemu_mutex_unlock(bitmap->mutex);
318 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
319 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
321 assert(!bitmap->active_iterators);
322 assert(!bdrv_dirty_bitmap_busy(bitmap));
323 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
324 assert(!bitmap->meta);
325 QLIST_REMOVE(bitmap, list);
326 hbitmap_free(bitmap->bitmap);
327 g_free(bitmap->name);
328 g_free(bitmap);
332 * For a bitmap with a successor, yield our name to the successor,
333 * delete the old bitmap, and return a handle to the new bitmap.
334 * Called with BQL taken.
336 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
337 BdrvDirtyBitmap *bitmap,
338 Error **errp)
340 char *name;
341 BdrvDirtyBitmap *successor = bitmap->successor;
343 if (successor == NULL) {
344 error_setg(errp, "Cannot relinquish control if "
345 "there's no successor present");
346 return NULL;
349 name = bitmap->name;
350 bitmap->name = NULL;
351 successor->name = name;
352 bitmap->successor = NULL;
353 successor->persistent = bitmap->persistent;
354 bitmap->persistent = false;
355 bitmap->busy = false;
356 bdrv_release_dirty_bitmap(bs, bitmap);
358 return successor;
362 * In cases of failure where we can no longer safely delete the parent,
363 * we may wish to re-join the parent and child/successor.
364 * The merged parent will be marked as not busy.
365 * The marged parent will be enabled if and only if the successor was enabled.
366 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
368 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
369 BdrvDirtyBitmap *parent,
370 Error **errp)
372 BdrvDirtyBitmap *successor = parent->successor;
374 if (!successor) {
375 error_setg(errp, "Cannot reclaim a successor when none is present");
376 return NULL;
379 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
380 error_setg(errp, "Merging of parent and successor bitmap failed");
381 return NULL;
384 parent->disabled = successor->disabled;
385 parent->busy = false;
386 bdrv_release_dirty_bitmap_locked(successor);
387 parent->successor = NULL;
389 return parent;
392 /* Called with BQL taken. */
393 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
394 BdrvDirtyBitmap *parent,
395 Error **errp)
397 BdrvDirtyBitmap *ret;
399 qemu_mutex_lock(parent->mutex);
400 ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
401 qemu_mutex_unlock(parent->mutex);
403 return ret;
407 * Truncates _all_ bitmaps attached to a BDS.
408 * Called with BQL taken.
410 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
412 BdrvDirtyBitmap *bitmap;
414 bdrv_dirty_bitmaps_lock(bs);
415 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
416 assert(!bdrv_dirty_bitmap_busy(bitmap));
417 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
418 assert(!bitmap->active_iterators);
419 hbitmap_truncate(bitmap->bitmap, bytes);
420 bitmap->size = bytes;
422 bdrv_dirty_bitmaps_unlock(bs);
425 /* Called with BQL taken. */
426 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
428 bdrv_dirty_bitmaps_lock(bs);
429 bdrv_release_dirty_bitmap_locked(bitmap);
430 bdrv_dirty_bitmaps_unlock(bs);
434 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
435 * There must not be any busy bitmaps attached.
436 * This function does not remove persistent bitmaps from the storage.
437 * Called with BQL taken.
439 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
441 BdrvDirtyBitmap *bm, *next;
443 bdrv_dirty_bitmaps_lock(bs);
444 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
445 if (bdrv_dirty_bitmap_name(bm)) {
446 bdrv_release_dirty_bitmap_locked(bm);
449 bdrv_dirty_bitmaps_unlock(bs);
453 * Remove persistent dirty bitmap from the storage if it exists.
454 * Absence of bitmap is not an error, because we have the following scenario:
455 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
456 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
457 * not fail.
458 * This function doesn't release corresponding BdrvDirtyBitmap.
460 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
461 const char *name,
462 Error **errp)
464 if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
465 bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
469 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
471 bdrv_dirty_bitmap_lock(bitmap);
472 bitmap->disabled = true;
473 bdrv_dirty_bitmap_unlock(bitmap);
476 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
478 bdrv_dirty_bitmap_lock(bitmap);
479 bdrv_enable_dirty_bitmap_locked(bitmap);
480 bdrv_dirty_bitmap_unlock(bitmap);
483 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
485 BdrvDirtyBitmap *bm;
486 BlockDirtyInfoList *list = NULL;
487 BlockDirtyInfoList **plist = &list;
489 bdrv_dirty_bitmaps_lock(bs);
490 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
491 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
492 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
493 info->count = bdrv_get_dirty_count(bm);
494 info->granularity = bdrv_dirty_bitmap_granularity(bm);
495 info->has_name = !!bm->name;
496 info->name = g_strdup(bm->name);
497 info->status = bdrv_dirty_bitmap_status(bm);
498 info->recording = bdrv_dirty_bitmap_recording(bm);
499 info->busy = bdrv_dirty_bitmap_busy(bm);
500 info->persistent = bm->persistent;
501 info->has_inconsistent = bm->inconsistent;
502 info->inconsistent = bm->inconsistent;
503 entry->value = info;
504 *plist = entry;
505 plist = &entry->next;
507 bdrv_dirty_bitmaps_unlock(bs);
509 return list;
512 /* Called within bdrv_dirty_bitmap_lock..unlock */
513 bool bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
514 int64_t offset)
516 if (bitmap) {
517 return hbitmap_get(bitmap->bitmap, offset);
518 } else {
519 return false;
524 * Chooses a default granularity based on the existing cluster size,
525 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
526 * is no cluster size information available.
528 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
530 BlockDriverInfo bdi;
531 uint32_t granularity;
533 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
534 granularity = MAX(4096, bdi.cluster_size);
535 granularity = MIN(65536, granularity);
536 } else {
537 granularity = 65536;
540 return granularity;
543 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
545 return 1U << hbitmap_granularity(bitmap->bitmap);
548 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
550 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
551 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
552 iter->bitmap = bitmap;
553 bitmap->active_iterators++;
554 return iter;
557 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
559 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
560 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
561 iter->bitmap = bitmap;
562 bitmap->active_iterators++;
563 return iter;
566 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
568 if (!iter) {
569 return;
571 assert(iter->bitmap->active_iterators > 0);
572 iter->bitmap->active_iterators--;
573 g_free(iter);
576 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
578 return hbitmap_iter_next(&iter->hbi);
581 /* Called within bdrv_dirty_bitmap_lock..unlock */
582 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
583 int64_t offset, int64_t bytes)
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_readonly(bitmap));
602 hbitmap_reset(bitmap->bitmap, offset, bytes);
605 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
606 int64_t offset, int64_t bytes)
608 bdrv_dirty_bitmap_lock(bitmap);
609 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
610 bdrv_dirty_bitmap_unlock(bitmap);
613 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
615 assert(!bdrv_dirty_bitmap_readonly(bitmap));
616 bdrv_dirty_bitmap_lock(bitmap);
617 if (!out) {
618 hbitmap_reset_all(bitmap->bitmap);
619 } else {
620 HBitmap *backup = bitmap->bitmap;
621 bitmap->bitmap = hbitmap_alloc(bitmap->size,
622 hbitmap_granularity(backup));
623 *out = backup;
625 bdrv_dirty_bitmap_unlock(bitmap);
628 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
630 HBitmap *tmp = bitmap->bitmap;
631 assert(!bdrv_dirty_bitmap_readonly(bitmap));
632 bitmap->bitmap = backup;
633 hbitmap_free(tmp);
636 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
637 uint64_t offset, uint64_t bytes)
639 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
642 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
644 return hbitmap_serialization_align(bitmap->bitmap);
647 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
648 uint8_t *buf, uint64_t offset,
649 uint64_t bytes)
651 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
654 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
655 uint8_t *buf, uint64_t offset,
656 uint64_t bytes, bool finish)
658 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
661 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
662 uint64_t offset, uint64_t bytes,
663 bool finish)
665 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
668 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
669 uint64_t offset, uint64_t bytes,
670 bool finish)
672 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
675 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
677 hbitmap_deserialize_finish(bitmap->bitmap);
680 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
682 BdrvDirtyBitmap *bitmap;
684 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
685 return;
688 bdrv_dirty_bitmaps_lock(bs);
689 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
690 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
691 continue;
693 assert(!bdrv_dirty_bitmap_readonly(bitmap));
694 hbitmap_set(bitmap->bitmap, offset, bytes);
696 bdrv_dirty_bitmaps_unlock(bs);
700 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
702 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
704 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
707 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
709 return hbitmap_count(bitmap->bitmap);
712 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
714 return hbitmap_count(bitmap->meta);
717 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
719 return bitmap->readonly;
722 /* Called with BQL taken. */
723 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
725 qemu_mutex_lock(bitmap->mutex);
726 bitmap->readonly = value;
727 qemu_mutex_unlock(bitmap->mutex);
730 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
732 BdrvDirtyBitmap *bm;
733 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
734 if (bm->readonly) {
735 return true;
739 return false;
742 /* Called with BQL taken. */
743 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
745 qemu_mutex_lock(bitmap->mutex);
746 bitmap->persistent = persistent;
747 qemu_mutex_unlock(bitmap->mutex);
750 /* Called with BQL taken. */
751 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
753 qemu_mutex_lock(bitmap->mutex);
754 assert(bitmap->persistent == true);
755 bitmap->inconsistent = true;
756 bitmap->disabled = true;
757 qemu_mutex_unlock(bitmap->mutex);
760 /* Called with BQL taken. */
761 void bdrv_dirty_bitmap_set_migration(BdrvDirtyBitmap *bitmap, bool migration)
763 qemu_mutex_lock(bitmap->mutex);
764 bitmap->migration = migration;
765 qemu_mutex_unlock(bitmap->mutex);
768 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
770 return bitmap->persistent && !bitmap->migration;
773 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
775 return bitmap->inconsistent;
778 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
780 BdrvDirtyBitmap *bm;
781 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
782 if (bm->persistent && !bm->readonly && !bm->migration) {
783 return true;
787 return false;
790 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
791 BdrvDirtyBitmap *bitmap)
793 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
794 QLIST_NEXT(bitmap, list);
797 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
799 return hbitmap_sha256(bitmap->bitmap, errp);
802 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
803 uint64_t bytes)
805 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
808 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
809 uint64_t *offset, uint64_t *bytes)
811 return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
814 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
815 HBitmap **backup, Error **errp)
817 bool ret;
819 /* only bitmaps from one bds are supported */
820 assert(dest->mutex == src->mutex);
822 qemu_mutex_lock(dest->mutex);
824 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
825 goto out;
828 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
829 goto out;
832 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
833 error_setg(errp, "Bitmaps are incompatible and can't be merged");
834 goto out;
837 if (backup) {
838 *backup = dest->bitmap;
839 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
840 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
841 } else {
842 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
844 assert(ret);
846 out:
847 qemu_mutex_unlock(dest->mutex);