aspeed: fby35: Add a bootrom for the BMC
[qemu.git] / block / dirty-bitmap.c
blobbf3dc0512a602ba15f9323c8f3f396205d6b14a5
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"
29 #include "qemu/main-loop.h"
31 struct BdrvDirtyBitmap {
32 BlockDriverState *bs;
33 HBitmap *bitmap; /* Dirty bitmap implementation */
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 bdrv_dirty_bitmaps_lock(bitmap->bs);
77 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
79 bdrv_dirty_bitmaps_unlock(bitmap->bs);
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) {
108 if (bdrv_find_dirty_bitmap(bs, name)) {
109 error_setg(errp, "Bitmap already exists: %s", name);
110 return NULL;
112 if (strlen(name) > BDRV_BITMAP_MAX_NAME_SIZE) {
113 error_setg(errp, "Bitmap name too long: %s", name);
114 return NULL;
117 bitmap_size = bdrv_getlength(bs);
118 if (bitmap_size < 0) {
119 error_setg_errno(errp, -bitmap_size, "could not get length of device");
120 errno = -bitmap_size;
121 return NULL;
123 bitmap = g_new0(BdrvDirtyBitmap, 1);
124 bitmap->bs = bs;
125 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
126 bitmap->size = bitmap_size;
127 bitmap->name = g_strdup(name);
128 bitmap->disabled = false;
129 bdrv_dirty_bitmaps_lock(bs);
130 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
131 bdrv_dirty_bitmaps_unlock(bs);
132 return bitmap;
135 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
137 return bitmap->size;
140 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
142 return bitmap->name;
145 /* Called with BQL taken. */
146 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
148 return bitmap->successor;
151 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
153 return bitmap->busy;
156 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
158 bdrv_dirty_bitmaps_lock(bitmap->bs);
159 bitmap->busy = busy;
160 bdrv_dirty_bitmaps_unlock(bitmap->bs);
163 /* Called with BQL taken. */
164 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
166 return !bitmap->disabled;
169 /* Called with BQL taken. */
170 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
172 return !bitmap->disabled || (bitmap->successor &&
173 !bitmap->successor->disabled);
176 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
177 Error **errp)
179 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
180 error_setg(errp, "Bitmap '%s' is currently in use by another"
181 " operation and cannot be used", bitmap->name);
182 return -1;
185 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
186 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
187 bitmap->name);
188 return -1;
191 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
192 bdrv_dirty_bitmap_inconsistent(bitmap)) {
193 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
194 bitmap->name);
195 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
196 " this bitmap from disk\n");
197 return -1;
200 return 0;
204 * Create a successor bitmap destined to replace this bitmap after an operation.
205 * Requires that the bitmap is not marked busy and has no successor.
206 * The successor will be enabled if the parent bitmap was.
207 * Called with BQL taken.
209 int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp)
211 uint64_t granularity;
212 BdrvDirtyBitmap *child;
214 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
215 return -1;
217 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
218 error_setg(errp, "Cannot create a successor for a bitmap that already "
219 "has one");
220 return -1;
223 /* Create an anonymous successor */
224 granularity = bdrv_dirty_bitmap_granularity(bitmap);
225 child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp);
226 if (!child) {
227 return -1;
230 /* Successor will be on or off based on our current state. */
231 child->disabled = bitmap->disabled;
232 bitmap->disabled = true;
234 /* Install the successor and mark the parent as busy */
235 bitmap->successor = child;
236 bitmap->busy = true;
237 return 0;
240 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
242 bitmap->disabled = false;
245 /* Called with BQL taken. */
246 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
248 assert(bitmap->bs == bitmap->successor->bs);
249 bdrv_dirty_bitmaps_lock(bitmap->bs);
250 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
251 bdrv_dirty_bitmaps_unlock(bitmap->bs);
254 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
255 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
257 assert(!bitmap->active_iterators);
258 assert(!bdrv_dirty_bitmap_busy(bitmap));
259 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
260 QLIST_REMOVE(bitmap, list);
261 hbitmap_free(bitmap->bitmap);
262 g_free(bitmap->name);
263 g_free(bitmap);
267 * For a bitmap with a successor, yield our name to the successor,
268 * delete the old bitmap, and return a handle to the new bitmap.
269 * Called with BQL taken.
271 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap,
272 Error **errp)
274 char *name;
275 BdrvDirtyBitmap *successor = bitmap->successor;
277 if (successor == NULL) {
278 error_setg(errp, "Cannot relinquish control if "
279 "there's no successor present");
280 return NULL;
283 name = bitmap->name;
284 bitmap->name = NULL;
285 successor->name = name;
286 bitmap->successor = NULL;
287 successor->persistent = bitmap->persistent;
288 bitmap->persistent = false;
289 bitmap->busy = false;
290 bdrv_release_dirty_bitmap(bitmap);
292 return successor;
296 * In cases of failure where we can no longer safely delete the parent,
297 * we may wish to re-join the parent and child/successor.
298 * The merged parent will be marked as not busy.
299 * The marged parent will be enabled if and only if the successor was enabled.
300 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
302 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent,
303 Error **errp)
305 BdrvDirtyBitmap *successor = parent->successor;
307 if (!successor) {
308 error_setg(errp, "Cannot reclaim a successor when none is present");
309 return NULL;
312 hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap);
314 parent->disabled = successor->disabled;
315 parent->busy = false;
316 bdrv_release_dirty_bitmap_locked(successor);
317 parent->successor = NULL;
319 return parent;
322 /* Called with BQL taken. */
323 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent,
324 Error **errp)
326 BdrvDirtyBitmap *ret;
328 bdrv_dirty_bitmaps_lock(parent->bs);
329 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp);
330 bdrv_dirty_bitmaps_unlock(parent->bs);
332 return ret;
336 * Truncates _all_ bitmaps attached to a BDS.
337 * Called with BQL taken.
339 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
341 BdrvDirtyBitmap *bitmap;
343 bdrv_dirty_bitmaps_lock(bs);
344 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
345 assert(!bdrv_dirty_bitmap_busy(bitmap));
346 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
347 assert(!bitmap->active_iterators);
348 hbitmap_truncate(bitmap->bitmap, bytes);
349 bitmap->size = bytes;
351 bdrv_dirty_bitmaps_unlock(bs);
354 /* Called with BQL taken. */
355 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap)
357 BlockDriverState *bs = bitmap->bs;
359 bdrv_dirty_bitmaps_lock(bs);
360 bdrv_release_dirty_bitmap_locked(bitmap);
361 bdrv_dirty_bitmaps_unlock(bs);
365 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
366 * There must not be any busy bitmaps attached.
367 * This function does not remove persistent bitmaps from the storage.
368 * Called with BQL taken.
370 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
372 BdrvDirtyBitmap *bm, *next;
374 bdrv_dirty_bitmaps_lock(bs);
375 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
376 if (bdrv_dirty_bitmap_name(bm)) {
377 bdrv_release_dirty_bitmap_locked(bm);
380 bdrv_dirty_bitmaps_unlock(bs);
384 * Remove persistent dirty bitmap from the storage if it exists.
385 * Absence of bitmap is not an error, because we have the following scenario:
386 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
387 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
388 * not fail.
389 * This function doesn't release corresponding BdrvDirtyBitmap.
391 static int coroutine_fn
392 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
393 Error **errp)
395 if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) {
396 return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
399 return 0;
402 typedef struct BdrvRemovePersistentDirtyBitmapCo {
403 BlockDriverState *bs;
404 const char *name;
405 Error **errp;
406 int ret;
407 } BdrvRemovePersistentDirtyBitmapCo;
409 static void coroutine_fn
410 bdrv_co_remove_persistent_dirty_bitmap_entry(void *opaque)
412 BdrvRemovePersistentDirtyBitmapCo *s = opaque;
414 s->ret = bdrv_co_remove_persistent_dirty_bitmap(s->bs, s->name, s->errp);
415 aio_wait_kick();
418 int bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
419 Error **errp)
421 if (qemu_in_coroutine()) {
422 return bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
423 } else {
424 Coroutine *co;
425 BdrvRemovePersistentDirtyBitmapCo s = {
426 .bs = bs,
427 .name = name,
428 .errp = errp,
429 .ret = -EINPROGRESS,
432 co = qemu_coroutine_create(bdrv_co_remove_persistent_dirty_bitmap_entry,
433 &s);
434 bdrv_coroutine_enter(bs, co);
435 BDRV_POLL_WHILE(bs, s.ret == -EINPROGRESS);
437 return s.ret;
441 bool
442 bdrv_supports_persistent_dirty_bitmap(BlockDriverState *bs)
444 if (bs->drv && bs->drv->bdrv_supports_persistent_dirty_bitmap) {
445 return bs->drv->bdrv_supports_persistent_dirty_bitmap(bs);
447 return false;
450 static bool coroutine_fn
451 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
452 uint32_t granularity, Error **errp)
454 BlockDriver *drv = bs->drv;
456 if (!drv) {
457 error_setg_errno(errp, ENOMEDIUM,
458 "Can't store persistent bitmaps to %s",
459 bdrv_get_device_or_node_name(bs));
460 return false;
463 if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
464 error_setg_errno(errp, ENOTSUP,
465 "Can't store persistent bitmaps to %s",
466 bdrv_get_device_or_node_name(bs));
467 return false;
470 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
473 typedef struct BdrvCanStoreNewDirtyBitmapCo {
474 BlockDriverState *bs;
475 const char *name;
476 uint32_t granularity;
477 Error **errp;
478 bool ret;
480 bool in_progress;
481 } BdrvCanStoreNewDirtyBitmapCo;
483 static void coroutine_fn bdrv_co_can_store_new_dirty_bitmap_entry(void *opaque)
485 BdrvCanStoreNewDirtyBitmapCo *s = opaque;
487 s->ret = bdrv_co_can_store_new_dirty_bitmap(s->bs, s->name, s->granularity,
488 s->errp);
489 s->in_progress = false;
490 aio_wait_kick();
493 bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
494 uint32_t granularity, Error **errp)
496 IO_CODE();
497 if (qemu_in_coroutine()) {
498 return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
499 } else {
500 Coroutine *co;
501 BdrvCanStoreNewDirtyBitmapCo s = {
502 .bs = bs,
503 .name = name,
504 .granularity = granularity,
505 .errp = errp,
506 .in_progress = true,
509 co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry,
510 &s);
511 bdrv_coroutine_enter(bs, co);
512 BDRV_POLL_WHILE(bs, s.in_progress);
514 return s.ret;
518 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
520 bdrv_dirty_bitmaps_lock(bitmap->bs);
521 bitmap->disabled = true;
522 bdrv_dirty_bitmaps_unlock(bitmap->bs);
525 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
527 bdrv_dirty_bitmaps_lock(bitmap->bs);
528 bdrv_enable_dirty_bitmap_locked(bitmap);
529 bdrv_dirty_bitmaps_unlock(bitmap->bs);
532 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
534 BdrvDirtyBitmap *bm;
535 BlockDirtyInfoList *list = NULL;
536 BlockDirtyInfoList **tail = &list;
538 bdrv_dirty_bitmaps_lock(bs);
539 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
540 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
542 info->count = bdrv_get_dirty_count(bm);
543 info->granularity = bdrv_dirty_bitmap_granularity(bm);
544 info->has_name = !!bm->name;
545 info->name = g_strdup(bm->name);
546 info->recording = bdrv_dirty_bitmap_recording(bm);
547 info->busy = bdrv_dirty_bitmap_busy(bm);
548 info->persistent = bm->persistent;
549 info->has_inconsistent = bm->inconsistent;
550 info->inconsistent = bm->inconsistent;
551 QAPI_LIST_APPEND(tail, info);
553 bdrv_dirty_bitmaps_unlock(bs);
555 return list;
558 /* Called within bdrv_dirty_bitmap_lock..unlock */
559 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
561 return hbitmap_get(bitmap->bitmap, offset);
564 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
566 bool ret;
567 bdrv_dirty_bitmaps_lock(bitmap->bs);
568 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
569 bdrv_dirty_bitmaps_unlock(bitmap->bs);
571 return ret;
575 * Chooses a default granularity based on the existing cluster size,
576 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
577 * is no cluster size information available.
579 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
581 BlockDriverInfo bdi;
582 uint32_t granularity;
584 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
585 granularity = MAX(4096, bdi.cluster_size);
586 granularity = MIN(65536, granularity);
587 } else {
588 granularity = 65536;
591 return granularity;
594 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
596 return 1U << hbitmap_granularity(bitmap->bitmap);
599 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
601 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
602 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
603 iter->bitmap = bitmap;
604 bitmap->active_iterators++;
605 return iter;
608 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
610 if (!iter) {
611 return;
613 assert(iter->bitmap->active_iterators > 0);
614 iter->bitmap->active_iterators--;
615 g_free(iter);
618 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
620 return hbitmap_iter_next(&iter->hbi);
623 /* Called within bdrv_dirty_bitmap_lock..unlock */
624 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
625 int64_t offset, int64_t bytes)
627 assert(!bdrv_dirty_bitmap_readonly(bitmap));
628 hbitmap_set(bitmap->bitmap, offset, bytes);
631 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
632 int64_t offset, int64_t bytes)
634 bdrv_dirty_bitmaps_lock(bitmap->bs);
635 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
636 bdrv_dirty_bitmaps_unlock(bitmap->bs);
639 /* Called within bdrv_dirty_bitmap_lock..unlock */
640 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
641 int64_t offset, int64_t bytes)
643 assert(!bdrv_dirty_bitmap_readonly(bitmap));
644 hbitmap_reset(bitmap->bitmap, offset, bytes);
647 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
648 int64_t offset, int64_t bytes)
650 bdrv_dirty_bitmaps_lock(bitmap->bs);
651 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
652 bdrv_dirty_bitmaps_unlock(bitmap->bs);
655 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
657 IO_CODE();
658 assert(!bdrv_dirty_bitmap_readonly(bitmap));
659 bdrv_dirty_bitmaps_lock(bitmap->bs);
660 if (!out) {
661 hbitmap_reset_all(bitmap->bitmap);
662 } else {
663 HBitmap *backup = bitmap->bitmap;
664 bitmap->bitmap = hbitmap_alloc(bitmap->size,
665 hbitmap_granularity(backup));
666 *out = backup;
668 bdrv_dirty_bitmaps_unlock(bitmap->bs);
671 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
673 HBitmap *tmp = bitmap->bitmap;
674 assert(!bdrv_dirty_bitmap_readonly(bitmap));
675 GLOBAL_STATE_CODE();
676 bitmap->bitmap = backup;
677 hbitmap_free(tmp);
680 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
681 uint64_t offset, uint64_t bytes)
683 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
686 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
688 return hbitmap_serialization_align(bitmap->bitmap);
691 /* Return the disk size covered by a chunk of serialized bitmap data. */
692 uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size,
693 const BdrvDirtyBitmap *bitmap)
695 uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
696 uint64_t limit = granularity * (serialized_chunk_size << 3);
698 assert(QEMU_IS_ALIGNED(limit,
699 bdrv_dirty_bitmap_serialization_align(bitmap)));
700 return limit;
704 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
705 uint8_t *buf, uint64_t offset,
706 uint64_t bytes)
708 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
711 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
712 uint8_t *buf, uint64_t offset,
713 uint64_t bytes, bool finish)
715 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
718 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
719 uint64_t offset, uint64_t bytes,
720 bool finish)
722 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
725 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
726 uint64_t offset, uint64_t bytes,
727 bool finish)
729 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
732 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
734 hbitmap_deserialize_finish(bitmap->bitmap);
737 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
739 BdrvDirtyBitmap *bitmap;
740 IO_CODE();
742 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
743 return;
746 bdrv_dirty_bitmaps_lock(bs);
747 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
748 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
749 continue;
751 assert(!bdrv_dirty_bitmap_readonly(bitmap));
752 hbitmap_set(bitmap->bitmap, offset, bytes);
754 bdrv_dirty_bitmaps_unlock(bs);
758 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
760 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
762 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
765 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
767 return hbitmap_count(bitmap->bitmap);
770 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
772 return bitmap->readonly;
775 /* Called with BQL taken. */
776 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
778 bdrv_dirty_bitmaps_lock(bitmap->bs);
779 bitmap->readonly = value;
780 bdrv_dirty_bitmaps_unlock(bitmap->bs);
783 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
785 BdrvDirtyBitmap *bm;
786 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
787 if (bm->readonly) {
788 return true;
792 return false;
795 bool bdrv_has_named_bitmaps(BlockDriverState *bs)
797 BdrvDirtyBitmap *bm;
799 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
800 if (bdrv_dirty_bitmap_name(bm)) {
801 return true;
805 return false;
808 /* Called with BQL taken. */
809 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
811 bdrv_dirty_bitmaps_lock(bitmap->bs);
812 bitmap->persistent = persistent;
813 bdrv_dirty_bitmaps_unlock(bitmap->bs);
816 /* Called with BQL taken. */
817 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
819 bdrv_dirty_bitmaps_lock(bitmap->bs);
820 assert(bitmap->persistent == true);
821 bitmap->inconsistent = true;
822 bitmap->disabled = true;
823 bdrv_dirty_bitmaps_unlock(bitmap->bs);
826 /* Called with BQL taken. */
827 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
829 bdrv_dirty_bitmaps_lock(bitmap->bs);
830 bitmap->skip_store = skip;
831 bdrv_dirty_bitmaps_unlock(bitmap->bs);
834 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
836 return bitmap->persistent && !bitmap->skip_store;
839 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
841 return bitmap->inconsistent;
844 BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs)
846 return QLIST_FIRST(&bs->dirty_bitmaps);
849 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap)
851 return QLIST_NEXT(bitmap, list);
854 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
856 return hbitmap_sha256(bitmap->bitmap, errp);
859 int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *bitmap, int64_t offset,
860 int64_t bytes)
862 return hbitmap_next_dirty(bitmap->bitmap, offset, bytes);
865 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset,
866 int64_t bytes)
868 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
871 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
872 int64_t start, int64_t end, int64_t max_dirty_count,
873 int64_t *dirty_start, int64_t *dirty_count)
875 return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_count,
876 dirty_start, dirty_count);
879 bool bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap, int64_t offset,
880 int64_t bytes, int64_t *count)
882 return hbitmap_status(bitmap->bitmap, offset, bytes, count);
886 * bdrv_merge_dirty_bitmap: merge src into dest.
887 * Ensures permissions on bitmaps are reasonable; use for public API.
889 * @backup: If provided, make a copy of dest here prior to merge.
891 * Returns true on success, false on failure. In case of failure bitmaps are
892 * untouched.
894 bool bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
895 HBitmap **backup, Error **errp)
897 bool ret = false;
899 bdrv_dirty_bitmaps_lock(dest->bs);
900 if (src->bs != dest->bs) {
901 bdrv_dirty_bitmaps_lock(src->bs);
904 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
905 goto out;
908 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
909 goto out;
912 if (bdrv_dirty_bitmap_size(src) != bdrv_dirty_bitmap_size(dest)) {
913 error_setg(errp, "Bitmaps are of different sizes (destination size is %"
914 PRId64 ", source size is %" PRId64 ") and can't be merged",
915 bdrv_dirty_bitmap_size(dest), bdrv_dirty_bitmap_size(src));
916 goto out;
919 bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
920 ret = true;
922 out:
923 bdrv_dirty_bitmaps_unlock(dest->bs);
924 if (src->bs != dest->bs) {
925 bdrv_dirty_bitmaps_unlock(src->bs);
928 return ret;
932 * bdrv_dirty_bitmap_merge_internal: merge src into dest.
933 * Does NOT check bitmap permissions; not suitable for use as public API.
934 * @dest, @src and @backup (if not NULL) must have same size.
936 * @backup: If provided, make a copy of dest here prior to merge.
937 * @lock: If true, lock and unlock bitmaps on the way in/out.
939 void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
940 const BdrvDirtyBitmap *src,
941 HBitmap **backup,
942 bool lock)
944 IO_CODE();
946 assert(!bdrv_dirty_bitmap_readonly(dest));
947 assert(!bdrv_dirty_bitmap_inconsistent(dest));
948 assert(!bdrv_dirty_bitmap_inconsistent(src));
950 if (lock) {
951 bdrv_dirty_bitmaps_lock(dest->bs);
952 if (src->bs != dest->bs) {
953 bdrv_dirty_bitmaps_lock(src->bs);
957 if (backup) {
958 *backup = dest->bitmap;
959 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
960 hbitmap_merge(*backup, src->bitmap, dest->bitmap);
961 } else {
962 hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
965 if (lock) {
966 bdrv_dirty_bitmaps_unlock(dest->bs);
967 if (src->bs != dest->bs) {
968 bdrv_dirty_bitmaps_unlock(src->bs);