arm/kvm: convert to kvm_get_one_reg
[qemu/ar7.git] / block / dirty-bitmap.c
blob13a1979755d8a52b2369a6f3bdd1145a7bdeae4f
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-io.h"
28 #include "block/block_int.h"
29 #include "block/blockjob.h"
30 #include "block/dirty-bitmap.h"
31 #include "qemu/main-loop.h"
33 struct BdrvDirtyBitmap {
34 BlockDriverState *bs;
35 HBitmap *bitmap; /* Dirty bitmap implementation */
36 bool busy; /* Bitmap is busy, it can't be used via QMP */
37 BdrvDirtyBitmap *successor; /* Anonymous child, if any. */
38 char *name; /* Optional non-empty unique ID */
39 int64_t size; /* Size of the bitmap, in bytes */
40 bool disabled; /* Bitmap is disabled. It ignores all writes to
41 the device */
42 int active_iterators; /* How many iterators are active */
43 bool readonly; /* Bitmap is read-only. This field also
44 prevents the respective image from being
45 modified (i.e. blocks writes and discards).
46 Such operations must fail and both the image
47 and this bitmap must remain unchanged while
48 this flag is set. */
49 bool persistent; /* bitmap must be saved to owner disk image */
50 bool inconsistent; /* bitmap is persistent, but inconsistent.
51 It cannot be used at all in any way, except
52 a QMP user can remove it. */
53 bool skip_store; /* We are either migrating or deleting this
54 * bitmap; it should not be stored on the next
55 * inactivation. */
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 bdrv_dirty_bitmaps_lock(bitmap->bs);
79 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
81 bdrv_dirty_bitmaps_unlock(bitmap->bs);
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) {
110 if (bdrv_find_dirty_bitmap(bs, name)) {
111 error_setg(errp, "Bitmap already exists: %s", name);
112 return NULL;
114 if (strlen(name) > BDRV_BITMAP_MAX_NAME_SIZE) {
115 error_setg(errp, "Bitmap name too long: %s", name);
116 return NULL;
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;
123 return NULL;
125 bitmap = g_new0(BdrvDirtyBitmap, 1);
126 bitmap->bs = bs;
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);
134 return bitmap;
137 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
139 return bitmap->size;
142 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
144 return bitmap->name;
147 /* Called with BQL taken. */
148 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
150 return bitmap->successor;
153 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
155 return bitmap->busy;
158 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
160 bdrv_dirty_bitmaps_lock(bitmap->bs);
161 bitmap->busy = busy;
162 bdrv_dirty_bitmaps_unlock(bitmap->bs);
165 /* Called with BQL taken. */
166 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
168 return !bitmap->disabled;
171 /* Called with BQL taken. */
172 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
174 return !bitmap->disabled || (bitmap->successor &&
175 !bitmap->successor->disabled);
178 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
179 Error **errp)
181 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
182 error_setg(errp, "Bitmap '%s' is currently in use by another"
183 " operation and cannot be used", bitmap->name);
184 return -1;
187 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
188 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
189 bitmap->name);
190 return -1;
193 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
194 bdrv_dirty_bitmap_inconsistent(bitmap)) {
195 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
196 bitmap->name);
197 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
198 " this bitmap from disk\n");
199 return -1;
202 return 0;
206 * Create a successor bitmap destined to replace this bitmap after an operation.
207 * Requires that the bitmap is not marked busy and has no successor.
208 * The successor will be enabled if the parent bitmap was.
209 * Called with BQL taken.
211 int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp)
213 uint64_t granularity;
214 BdrvDirtyBitmap *child;
216 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
217 return -1;
219 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
220 error_setg(errp, "Cannot create a successor for a bitmap that already "
221 "has one");
222 return -1;
225 /* Create an anonymous successor */
226 granularity = bdrv_dirty_bitmap_granularity(bitmap);
227 child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp);
228 if (!child) {
229 return -1;
232 /* Successor will be on or off based on our current state. */
233 child->disabled = bitmap->disabled;
234 bitmap->disabled = true;
236 /* Install the successor and mark the parent as busy */
237 bitmap->successor = child;
238 bitmap->busy = true;
239 return 0;
242 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
244 bitmap->disabled = false;
247 /* Called with BQL taken. */
248 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
250 assert(bitmap->bs == bitmap->successor->bs);
251 bdrv_dirty_bitmaps_lock(bitmap->bs);
252 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
253 bdrv_dirty_bitmaps_unlock(bitmap->bs);
256 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
257 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
259 assert(!bitmap->active_iterators);
260 assert(!bdrv_dirty_bitmap_busy(bitmap));
261 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
262 QLIST_REMOVE(bitmap, list);
263 hbitmap_free(bitmap->bitmap);
264 g_free(bitmap->name);
265 g_free(bitmap);
269 * For a bitmap with a successor, yield our name to the successor,
270 * delete the old bitmap, and return a handle to the new bitmap.
271 * Called with BQL taken.
273 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap,
274 Error **errp)
276 char *name;
277 BdrvDirtyBitmap *successor = bitmap->successor;
279 if (successor == NULL) {
280 error_setg(errp, "Cannot relinquish control if "
281 "there's no successor present");
282 return NULL;
285 name = bitmap->name;
286 bitmap->name = NULL;
287 successor->name = name;
288 bitmap->successor = NULL;
289 successor->persistent = bitmap->persistent;
290 bitmap->persistent = false;
291 bitmap->busy = false;
292 bdrv_release_dirty_bitmap(bitmap);
294 return successor;
298 * In cases of failure where we can no longer safely delete the parent,
299 * we may wish to re-join the parent and child/successor.
300 * The merged parent will be marked as not busy.
301 * The marged parent will be enabled if and only if the successor was enabled.
302 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
304 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent,
305 Error **errp)
307 BdrvDirtyBitmap *successor = parent->successor;
309 if (!successor) {
310 error_setg(errp, "Cannot reclaim a successor when none is present");
311 return NULL;
314 hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap);
316 parent->disabled = successor->disabled;
317 parent->busy = false;
318 bdrv_release_dirty_bitmap_locked(successor);
319 parent->successor = NULL;
321 return parent;
324 /* Called with BQL taken. */
325 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent,
326 Error **errp)
328 BdrvDirtyBitmap *ret;
330 bdrv_dirty_bitmaps_lock(parent->bs);
331 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp);
332 bdrv_dirty_bitmaps_unlock(parent->bs);
334 return ret;
338 * Truncates _all_ bitmaps attached to a BDS.
339 * Called with BQL taken.
341 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
343 BdrvDirtyBitmap *bitmap;
345 bdrv_dirty_bitmaps_lock(bs);
346 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
347 assert(!bdrv_dirty_bitmap_busy(bitmap));
348 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
349 assert(!bitmap->active_iterators);
350 hbitmap_truncate(bitmap->bitmap, bytes);
351 bitmap->size = bytes;
353 bdrv_dirty_bitmaps_unlock(bs);
356 /* Called with BQL taken. */
357 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap)
359 BlockDriverState *bs = bitmap->bs;
361 bdrv_dirty_bitmaps_lock(bs);
362 bdrv_release_dirty_bitmap_locked(bitmap);
363 bdrv_dirty_bitmaps_unlock(bs);
367 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
368 * There must not be any busy bitmaps attached.
369 * This function does not remove persistent bitmaps from the storage.
370 * Called with BQL taken.
372 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
374 BdrvDirtyBitmap *bm, *next;
376 bdrv_dirty_bitmaps_lock(bs);
377 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
378 if (bdrv_dirty_bitmap_name(bm)) {
379 bdrv_release_dirty_bitmap_locked(bm);
382 bdrv_dirty_bitmaps_unlock(bs);
386 * Remove persistent dirty bitmap from the storage if it exists.
387 * Absence of bitmap is not an error, because we have the following scenario:
388 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
389 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
390 * not fail.
391 * This function doesn't release corresponding BdrvDirtyBitmap.
393 int coroutine_fn
394 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
395 Error **errp)
397 assert_bdrv_graph_readable();
398 if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) {
399 return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
402 return 0;
405 bool
406 bdrv_supports_persistent_dirty_bitmap(BlockDriverState *bs)
408 if (bs->drv && bs->drv->bdrv_supports_persistent_dirty_bitmap) {
409 return bs->drv->bdrv_supports_persistent_dirty_bitmap(bs);
411 return false;
414 bool coroutine_fn
415 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
416 uint32_t granularity, Error **errp)
418 BlockDriver *drv = bs->drv;
419 assert_bdrv_graph_readable();
421 if (!drv) {
422 error_setg_errno(errp, ENOMEDIUM,
423 "Can't store persistent bitmaps to %s",
424 bdrv_get_device_or_node_name(bs));
425 return false;
428 if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
429 error_setg_errno(errp, ENOTSUP,
430 "Can't store persistent bitmaps to %s",
431 bdrv_get_device_or_node_name(bs));
432 return false;
435 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
438 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
440 bdrv_dirty_bitmaps_lock(bitmap->bs);
441 bitmap->disabled = true;
442 bdrv_dirty_bitmaps_unlock(bitmap->bs);
445 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
447 bdrv_dirty_bitmaps_lock(bitmap->bs);
448 bdrv_enable_dirty_bitmap_locked(bitmap);
449 bdrv_dirty_bitmaps_unlock(bitmap->bs);
452 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
454 BdrvDirtyBitmap *bm;
455 BlockDirtyInfoList *list = NULL;
456 BlockDirtyInfoList **tail = &list;
458 bdrv_dirty_bitmaps_lock(bs);
459 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
460 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
462 info->count = bdrv_get_dirty_count(bm);
463 info->granularity = bdrv_dirty_bitmap_granularity(bm);
464 info->name = g_strdup(bm->name);
465 info->recording = bdrv_dirty_bitmap_recording(bm);
466 info->busy = bdrv_dirty_bitmap_busy(bm);
467 info->persistent = bm->persistent;
468 info->has_inconsistent = bm->inconsistent;
469 info->inconsistent = bm->inconsistent;
470 QAPI_LIST_APPEND(tail, info);
472 bdrv_dirty_bitmaps_unlock(bs);
474 return list;
477 /* Called within bdrv_dirty_bitmap_lock..unlock */
478 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
480 return hbitmap_get(bitmap->bitmap, offset);
483 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
485 bool ret;
486 bdrv_dirty_bitmaps_lock(bitmap->bs);
487 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
488 bdrv_dirty_bitmaps_unlock(bitmap->bs);
490 return ret;
494 * Chooses a default granularity based on the existing cluster size,
495 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
496 * is no cluster size information available.
498 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
500 BlockDriverInfo bdi;
501 uint32_t granularity;
503 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
504 granularity = MAX(4096, bdi.cluster_size);
505 granularity = MIN(65536, granularity);
506 } else {
507 granularity = 65536;
510 return granularity;
513 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
515 return 1U << hbitmap_granularity(bitmap->bitmap);
518 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
520 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
521 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
522 iter->bitmap = bitmap;
523 bitmap->active_iterators++;
524 return iter;
527 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
529 if (!iter) {
530 return;
532 assert(iter->bitmap->active_iterators > 0);
533 iter->bitmap->active_iterators--;
534 g_free(iter);
537 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
539 return hbitmap_iter_next(&iter->hbi);
542 /* Called within bdrv_dirty_bitmap_lock..unlock */
543 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
544 int64_t offset, int64_t bytes)
546 assert(!bdrv_dirty_bitmap_readonly(bitmap));
547 hbitmap_set(bitmap->bitmap, offset, bytes);
550 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
551 int64_t offset, int64_t bytes)
553 bdrv_dirty_bitmaps_lock(bitmap->bs);
554 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
555 bdrv_dirty_bitmaps_unlock(bitmap->bs);
558 /* Called within bdrv_dirty_bitmap_lock..unlock */
559 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
560 int64_t offset, int64_t bytes)
562 assert(!bdrv_dirty_bitmap_readonly(bitmap));
563 hbitmap_reset(bitmap->bitmap, offset, bytes);
566 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
567 int64_t offset, int64_t bytes)
569 bdrv_dirty_bitmaps_lock(bitmap->bs);
570 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
571 bdrv_dirty_bitmaps_unlock(bitmap->bs);
574 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
576 IO_CODE();
577 assert(!bdrv_dirty_bitmap_readonly(bitmap));
578 bdrv_dirty_bitmaps_lock(bitmap->bs);
579 if (!out) {
580 hbitmap_reset_all(bitmap->bitmap);
581 } else {
582 HBitmap *backup = bitmap->bitmap;
583 bitmap->bitmap = hbitmap_alloc(bitmap->size,
584 hbitmap_granularity(backup));
585 *out = backup;
587 bdrv_dirty_bitmaps_unlock(bitmap->bs);
590 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
592 HBitmap *tmp = bitmap->bitmap;
593 assert(!bdrv_dirty_bitmap_readonly(bitmap));
594 GLOBAL_STATE_CODE();
595 bitmap->bitmap = backup;
596 hbitmap_free(tmp);
599 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
600 uint64_t offset, uint64_t bytes)
602 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
605 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
607 return hbitmap_serialization_align(bitmap->bitmap);
610 /* Return the disk size covered by a chunk of serialized bitmap data. */
611 uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size,
612 const BdrvDirtyBitmap *bitmap)
614 uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
615 uint64_t limit = granularity * (serialized_chunk_size << 3);
617 assert(QEMU_IS_ALIGNED(limit,
618 bdrv_dirty_bitmap_serialization_align(bitmap)));
619 return limit;
623 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
624 uint8_t *buf, uint64_t offset,
625 uint64_t bytes)
627 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
630 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
631 uint8_t *buf, uint64_t offset,
632 uint64_t bytes, bool finish)
634 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
637 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
638 uint64_t offset, uint64_t bytes,
639 bool finish)
641 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
644 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
645 uint64_t offset, uint64_t bytes,
646 bool finish)
648 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
651 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
653 hbitmap_deserialize_finish(bitmap->bitmap);
656 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
658 BdrvDirtyBitmap *bitmap;
659 IO_CODE();
661 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
662 return;
665 bdrv_dirty_bitmaps_lock(bs);
666 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
667 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
668 continue;
670 assert(!bdrv_dirty_bitmap_readonly(bitmap));
671 hbitmap_set(bitmap->bitmap, offset, bytes);
673 bdrv_dirty_bitmaps_unlock(bs);
677 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
679 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
681 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
684 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
686 return hbitmap_count(bitmap->bitmap);
689 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
691 return bitmap->readonly;
694 /* Called with BQL taken. */
695 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
697 bdrv_dirty_bitmaps_lock(bitmap->bs);
698 bitmap->readonly = value;
699 bdrv_dirty_bitmaps_unlock(bitmap->bs);
702 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
704 BdrvDirtyBitmap *bm;
705 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
706 if (bm->readonly) {
707 return true;
711 return false;
714 bool bdrv_has_named_bitmaps(BlockDriverState *bs)
716 BdrvDirtyBitmap *bm;
718 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
719 if (bdrv_dirty_bitmap_name(bm)) {
720 return true;
724 return false;
727 /* Called with BQL taken. */
728 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
730 bdrv_dirty_bitmaps_lock(bitmap->bs);
731 bitmap->persistent = persistent;
732 bdrv_dirty_bitmaps_unlock(bitmap->bs);
735 /* Called with BQL taken. */
736 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
738 bdrv_dirty_bitmaps_lock(bitmap->bs);
739 assert(bitmap->persistent == true);
740 bitmap->inconsistent = true;
741 bitmap->disabled = true;
742 bdrv_dirty_bitmaps_unlock(bitmap->bs);
745 /* Called with BQL taken. */
746 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
748 bdrv_dirty_bitmaps_lock(bitmap->bs);
749 bitmap->skip_store = skip;
750 bdrv_dirty_bitmaps_unlock(bitmap->bs);
753 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
755 return bitmap->persistent && !bitmap->skip_store;
758 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
760 return bitmap->inconsistent;
763 BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs)
765 return QLIST_FIRST(&bs->dirty_bitmaps);
768 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap)
770 return QLIST_NEXT(bitmap, list);
773 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
775 return hbitmap_sha256(bitmap->bitmap, errp);
778 int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *bitmap, int64_t offset,
779 int64_t bytes)
781 return hbitmap_next_dirty(bitmap->bitmap, offset, bytes);
784 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset,
785 int64_t bytes)
787 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
790 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
791 int64_t start, int64_t end, int64_t max_dirty_count,
792 int64_t *dirty_start, int64_t *dirty_count)
794 return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_count,
795 dirty_start, dirty_count);
798 bool bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap, int64_t offset,
799 int64_t bytes, int64_t *count)
801 return hbitmap_status(bitmap->bitmap, offset, bytes, count);
805 * bdrv_merge_dirty_bitmap: merge src into dest.
806 * Ensures permissions on bitmaps are reasonable; use for public API.
808 * @backup: If provided, make a copy of dest here prior to merge.
810 * Returns true on success, false on failure. In case of failure bitmaps are
811 * untouched.
813 bool bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
814 HBitmap **backup, Error **errp)
816 bool ret = false;
818 bdrv_dirty_bitmaps_lock(dest->bs);
819 if (src->bs != dest->bs) {
820 bdrv_dirty_bitmaps_lock(src->bs);
823 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
824 goto out;
827 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
828 goto out;
831 if (bdrv_dirty_bitmap_size(src) != bdrv_dirty_bitmap_size(dest)) {
832 error_setg(errp, "Bitmaps are of different sizes (destination size is %"
833 PRId64 ", source size is %" PRId64 ") and can't be merged",
834 bdrv_dirty_bitmap_size(dest), bdrv_dirty_bitmap_size(src));
835 goto out;
838 bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
839 ret = true;
841 out:
842 bdrv_dirty_bitmaps_unlock(dest->bs);
843 if (src->bs != dest->bs) {
844 bdrv_dirty_bitmaps_unlock(src->bs);
847 return ret;
851 * bdrv_dirty_bitmap_merge_internal: merge src into dest.
852 * Does NOT check bitmap permissions; not suitable for use as public API.
853 * @dest, @src and @backup (if not NULL) must have same size.
855 * @backup: If provided, make a copy of dest here prior to merge.
856 * @lock: If true, lock and unlock bitmaps on the way in/out.
858 void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
859 const BdrvDirtyBitmap *src,
860 HBitmap **backup,
861 bool lock)
863 IO_CODE();
865 assert(!bdrv_dirty_bitmap_readonly(dest));
866 assert(!bdrv_dirty_bitmap_inconsistent(dest));
867 assert(!bdrv_dirty_bitmap_inconsistent(src));
869 if (lock) {
870 bdrv_dirty_bitmaps_lock(dest->bs);
871 if (src->bs != dest->bs) {
872 bdrv_dirty_bitmaps_lock(src->bs);
876 if (backup) {
877 *backup = dest->bitmap;
878 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
879 hbitmap_merge(*backup, src->bitmap, dest->bitmap);
880 } else {
881 hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
884 if (lock) {
885 bdrv_dirty_bitmaps_unlock(dest->bs);
886 if (src->bs != dest->bs) {
887 bdrv_dirty_bitmaps_unlock(src->bs);