s390x/pci: fix pcistb length
[qemu/ar7.git] / block / dirty-bitmap.c
blobc01319b188c3c37e380092ca7c5a5b69f8ebc1b3
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;
170 * bdrv_dirty_bitmap_status: This API is now deprecated.
171 * Called with BQL taken.
173 * A BdrvDirtyBitmap can be in four possible user-visible states:
174 * (1) Active: successor is NULL, and disabled is false: full r/w mode
175 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
176 * guest writes are dropped, but monitor writes are possible,
177 * through commands like merge and clear.
178 * (3) Frozen: successor is not NULL.
179 * A frozen bitmap cannot be renamed, deleted, cleared, set,
180 * enabled, merged to, etc. A frozen bitmap can only abdicate()
181 * or reclaim().
182 * In this state, the anonymous successor bitmap may be either
183 * Active and recording writes from the guest (e.g. backup jobs),
184 * or it can be Disabled and not recording writes.
185 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
186 * in any way from the monitor.
187 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
188 * is unusable by QEMU. It can be deleted to remove it from
189 * the qcow2.
191 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
193 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
194 return DIRTY_BITMAP_STATUS_INCONSISTENT;
195 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
196 return DIRTY_BITMAP_STATUS_FROZEN;
197 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
198 return DIRTY_BITMAP_STATUS_LOCKED;
199 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
200 return DIRTY_BITMAP_STATUS_DISABLED;
201 } else {
202 return DIRTY_BITMAP_STATUS_ACTIVE;
206 /* Called with BQL taken. */
207 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
209 return !bitmap->disabled || (bitmap->successor &&
210 !bitmap->successor->disabled);
213 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
214 Error **errp)
216 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
217 error_setg(errp, "Bitmap '%s' is currently in use by another"
218 " operation and cannot be used", bitmap->name);
219 return -1;
222 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
223 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
224 bitmap->name);
225 return -1;
228 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
229 bdrv_dirty_bitmap_inconsistent(bitmap)) {
230 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
231 bitmap->name);
232 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
233 " this bitmap from disk");
234 return -1;
237 return 0;
241 * Create a successor bitmap destined to replace this bitmap after an operation.
242 * Requires that the bitmap is not marked busy and has no successor.
243 * The successor will be enabled if the parent bitmap was.
244 * Called with BQL taken.
246 int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp)
248 uint64_t granularity;
249 BdrvDirtyBitmap *child;
251 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
252 return -1;
254 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
255 error_setg(errp, "Cannot create a successor for a bitmap that already "
256 "has one");
257 return -1;
260 /* Create an anonymous successor */
261 granularity = bdrv_dirty_bitmap_granularity(bitmap);
262 child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp);
263 if (!child) {
264 return -1;
267 /* Successor will be on or off based on our current state. */
268 child->disabled = bitmap->disabled;
269 bitmap->disabled = true;
271 /* Install the successor and mark the parent as busy */
272 bitmap->successor = child;
273 bitmap->busy = true;
274 return 0;
277 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
279 bitmap->disabled = false;
282 /* Called with BQL taken. */
283 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
285 assert(bitmap->bs == bitmap->successor->bs);
286 bdrv_dirty_bitmaps_lock(bitmap->bs);
287 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
288 bdrv_dirty_bitmaps_unlock(bitmap->bs);
291 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
292 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
294 assert(!bitmap->active_iterators);
295 assert(!bdrv_dirty_bitmap_busy(bitmap));
296 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
297 QLIST_REMOVE(bitmap, list);
298 hbitmap_free(bitmap->bitmap);
299 g_free(bitmap->name);
300 g_free(bitmap);
304 * For a bitmap with a successor, yield our name to the successor,
305 * delete the old bitmap, and return a handle to the new bitmap.
306 * Called with BQL taken.
308 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap,
309 Error **errp)
311 char *name;
312 BdrvDirtyBitmap *successor = bitmap->successor;
314 if (successor == NULL) {
315 error_setg(errp, "Cannot relinquish control if "
316 "there's no successor present");
317 return NULL;
320 name = bitmap->name;
321 bitmap->name = NULL;
322 successor->name = name;
323 bitmap->successor = NULL;
324 successor->persistent = bitmap->persistent;
325 bitmap->persistent = false;
326 bitmap->busy = false;
327 bdrv_release_dirty_bitmap(bitmap);
329 return successor;
333 * In cases of failure where we can no longer safely delete the parent,
334 * we may wish to re-join the parent and child/successor.
335 * The merged parent will be marked as not busy.
336 * The marged parent will be enabled if and only if the successor was enabled.
337 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
339 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent,
340 Error **errp)
342 BdrvDirtyBitmap *successor = parent->successor;
344 if (!successor) {
345 error_setg(errp, "Cannot reclaim a successor when none is present");
346 return NULL;
349 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
350 error_setg(errp, "Merging of parent and successor bitmap failed");
351 return NULL;
354 parent->disabled = successor->disabled;
355 parent->busy = false;
356 bdrv_release_dirty_bitmap_locked(successor);
357 parent->successor = NULL;
359 return parent;
362 /* Called with BQL taken. */
363 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent,
364 Error **errp)
366 BdrvDirtyBitmap *ret;
368 bdrv_dirty_bitmaps_lock(parent->bs);
369 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp);
370 bdrv_dirty_bitmaps_unlock(parent->bs);
372 return ret;
376 * Truncates _all_ bitmaps attached to a BDS.
377 * Called with BQL taken.
379 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
381 BdrvDirtyBitmap *bitmap;
383 bdrv_dirty_bitmaps_lock(bs);
384 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
385 assert(!bdrv_dirty_bitmap_busy(bitmap));
386 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
387 assert(!bitmap->active_iterators);
388 hbitmap_truncate(bitmap->bitmap, bytes);
389 bitmap->size = bytes;
391 bdrv_dirty_bitmaps_unlock(bs);
394 /* Called with BQL taken. */
395 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap)
397 BlockDriverState *bs = bitmap->bs;
399 bdrv_dirty_bitmaps_lock(bs);
400 bdrv_release_dirty_bitmap_locked(bitmap);
401 bdrv_dirty_bitmaps_unlock(bs);
405 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
406 * There must not be any busy bitmaps attached.
407 * This function does not remove persistent bitmaps from the storage.
408 * Called with BQL taken.
410 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
412 BdrvDirtyBitmap *bm, *next;
414 bdrv_dirty_bitmaps_lock(bs);
415 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
416 if (bdrv_dirty_bitmap_name(bm)) {
417 bdrv_release_dirty_bitmap_locked(bm);
420 bdrv_dirty_bitmaps_unlock(bs);
424 * Remove persistent dirty bitmap from the storage if it exists.
425 * Absence of bitmap is not an error, because we have the following scenario:
426 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
427 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
428 * not fail.
429 * This function doesn't release corresponding BdrvDirtyBitmap.
431 static int coroutine_fn
432 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
433 Error **errp)
435 if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) {
436 return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
439 return 0;
442 typedef struct BdrvRemovePersistentDirtyBitmapCo {
443 BlockDriverState *bs;
444 const char *name;
445 Error **errp;
446 int ret;
447 } BdrvRemovePersistentDirtyBitmapCo;
449 static void coroutine_fn
450 bdrv_co_remove_persistent_dirty_bitmap_entry(void *opaque)
452 BdrvRemovePersistentDirtyBitmapCo *s = opaque;
454 s->ret = bdrv_co_remove_persistent_dirty_bitmap(s->bs, s->name, s->errp);
455 aio_wait_kick();
458 int bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
459 Error **errp)
461 if (qemu_in_coroutine()) {
462 return bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
463 } else {
464 Coroutine *co;
465 BdrvRemovePersistentDirtyBitmapCo s = {
466 .bs = bs,
467 .name = name,
468 .errp = errp,
469 .ret = -EINPROGRESS,
472 co = qemu_coroutine_create(bdrv_co_remove_persistent_dirty_bitmap_entry,
473 &s);
474 bdrv_coroutine_enter(bs, co);
475 BDRV_POLL_WHILE(bs, s.ret == -EINPROGRESS);
477 return s.ret;
481 bool
482 bdrv_supports_persistent_dirty_bitmap(BlockDriverState *bs)
484 if (bs->drv && bs->drv->bdrv_supports_persistent_dirty_bitmap) {
485 return bs->drv->bdrv_supports_persistent_dirty_bitmap(bs);
487 return false;
490 static bool coroutine_fn
491 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
492 uint32_t granularity, Error **errp)
494 BlockDriver *drv = bs->drv;
496 if (!drv) {
497 error_setg_errno(errp, ENOMEDIUM,
498 "Can't store persistent bitmaps to %s",
499 bdrv_get_device_or_node_name(bs));
500 return false;
503 if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
504 error_setg_errno(errp, ENOTSUP,
505 "Can't store persistent bitmaps to %s",
506 bdrv_get_device_or_node_name(bs));
507 return false;
510 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
513 typedef struct BdrvCanStoreNewDirtyBitmapCo {
514 BlockDriverState *bs;
515 const char *name;
516 uint32_t granularity;
517 Error **errp;
518 bool ret;
520 bool in_progress;
521 } BdrvCanStoreNewDirtyBitmapCo;
523 static void coroutine_fn bdrv_co_can_store_new_dirty_bitmap_entry(void *opaque)
525 BdrvCanStoreNewDirtyBitmapCo *s = opaque;
527 s->ret = bdrv_co_can_store_new_dirty_bitmap(s->bs, s->name, s->granularity,
528 s->errp);
529 s->in_progress = false;
530 aio_wait_kick();
533 bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
534 uint32_t granularity, Error **errp)
536 if (qemu_in_coroutine()) {
537 return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
538 } else {
539 Coroutine *co;
540 BdrvCanStoreNewDirtyBitmapCo s = {
541 .bs = bs,
542 .name = name,
543 .granularity = granularity,
544 .errp = errp,
545 .in_progress = true,
548 co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry,
549 &s);
550 bdrv_coroutine_enter(bs, co);
551 BDRV_POLL_WHILE(bs, s.in_progress);
553 return s.ret;
557 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
559 bdrv_dirty_bitmaps_lock(bitmap->bs);
560 bitmap->disabled = true;
561 bdrv_dirty_bitmaps_unlock(bitmap->bs);
564 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
566 bdrv_dirty_bitmaps_lock(bitmap->bs);
567 bdrv_enable_dirty_bitmap_locked(bitmap);
568 bdrv_dirty_bitmaps_unlock(bitmap->bs);
571 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
573 BdrvDirtyBitmap *bm;
574 BlockDirtyInfoList *list = NULL;
575 BlockDirtyInfoList **plist = &list;
577 bdrv_dirty_bitmaps_lock(bs);
578 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
579 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
580 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
581 info->count = bdrv_get_dirty_count(bm);
582 info->granularity = bdrv_dirty_bitmap_granularity(bm);
583 info->has_name = !!bm->name;
584 info->name = g_strdup(bm->name);
585 info->status = bdrv_dirty_bitmap_status(bm);
586 info->recording = bdrv_dirty_bitmap_recording(bm);
587 info->busy = bdrv_dirty_bitmap_busy(bm);
588 info->persistent = bm->persistent;
589 info->has_inconsistent = bm->inconsistent;
590 info->inconsistent = bm->inconsistent;
591 entry->value = info;
592 *plist = entry;
593 plist = &entry->next;
595 bdrv_dirty_bitmaps_unlock(bs);
597 return list;
600 /* Called within bdrv_dirty_bitmap_lock..unlock */
601 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
603 return hbitmap_get(bitmap->bitmap, offset);
606 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
608 bool ret;
609 bdrv_dirty_bitmaps_lock(bitmap->bs);
610 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
611 bdrv_dirty_bitmaps_unlock(bitmap->bs);
613 return ret;
617 * Chooses a default granularity based on the existing cluster size,
618 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
619 * is no cluster size information available.
621 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
623 BlockDriverInfo bdi;
624 uint32_t granularity;
626 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
627 granularity = MAX(4096, bdi.cluster_size);
628 granularity = MIN(65536, granularity);
629 } else {
630 granularity = 65536;
633 return granularity;
636 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
638 return 1U << hbitmap_granularity(bitmap->bitmap);
641 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
643 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
644 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
645 iter->bitmap = bitmap;
646 bitmap->active_iterators++;
647 return iter;
650 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
652 if (!iter) {
653 return;
655 assert(iter->bitmap->active_iterators > 0);
656 iter->bitmap->active_iterators--;
657 g_free(iter);
660 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
662 return hbitmap_iter_next(&iter->hbi);
665 /* Called within bdrv_dirty_bitmap_lock..unlock */
666 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
667 int64_t offset, int64_t bytes)
669 assert(!bdrv_dirty_bitmap_readonly(bitmap));
670 hbitmap_set(bitmap->bitmap, offset, bytes);
673 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
674 int64_t offset, int64_t bytes)
676 bdrv_dirty_bitmaps_lock(bitmap->bs);
677 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
678 bdrv_dirty_bitmaps_unlock(bitmap->bs);
681 /* Called within bdrv_dirty_bitmap_lock..unlock */
682 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
683 int64_t offset, int64_t bytes)
685 assert(!bdrv_dirty_bitmap_readonly(bitmap));
686 hbitmap_reset(bitmap->bitmap, offset, bytes);
689 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
690 int64_t offset, int64_t bytes)
692 bdrv_dirty_bitmaps_lock(bitmap->bs);
693 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
694 bdrv_dirty_bitmaps_unlock(bitmap->bs);
697 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
699 assert(!bdrv_dirty_bitmap_readonly(bitmap));
700 bdrv_dirty_bitmaps_lock(bitmap->bs);
701 if (!out) {
702 hbitmap_reset_all(bitmap->bitmap);
703 } else {
704 HBitmap *backup = bitmap->bitmap;
705 bitmap->bitmap = hbitmap_alloc(bitmap->size,
706 hbitmap_granularity(backup));
707 *out = backup;
709 bdrv_dirty_bitmaps_unlock(bitmap->bs);
712 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
714 HBitmap *tmp = bitmap->bitmap;
715 assert(!bdrv_dirty_bitmap_readonly(bitmap));
716 bitmap->bitmap = backup;
717 hbitmap_free(tmp);
720 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
721 uint64_t offset, uint64_t bytes)
723 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
726 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
728 return hbitmap_serialization_align(bitmap->bitmap);
731 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
732 uint8_t *buf, uint64_t offset,
733 uint64_t bytes)
735 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
738 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
739 uint8_t *buf, uint64_t offset,
740 uint64_t bytes, bool finish)
742 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
745 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
746 uint64_t offset, uint64_t bytes,
747 bool finish)
749 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
752 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
753 uint64_t offset, uint64_t bytes,
754 bool finish)
756 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
759 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
761 hbitmap_deserialize_finish(bitmap->bitmap);
764 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
766 BdrvDirtyBitmap *bitmap;
768 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
769 return;
772 bdrv_dirty_bitmaps_lock(bs);
773 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
774 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
775 continue;
777 assert(!bdrv_dirty_bitmap_readonly(bitmap));
778 hbitmap_set(bitmap->bitmap, offset, bytes);
780 bdrv_dirty_bitmaps_unlock(bs);
784 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
786 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
788 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
791 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
793 return hbitmap_count(bitmap->bitmap);
796 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
798 return bitmap->readonly;
801 /* Called with BQL taken. */
802 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
804 bdrv_dirty_bitmaps_lock(bitmap->bs);
805 bitmap->readonly = value;
806 bdrv_dirty_bitmaps_unlock(bitmap->bs);
809 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
811 BdrvDirtyBitmap *bm;
812 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
813 if (bm->readonly) {
814 return true;
818 return false;
821 bool bdrv_has_named_bitmaps(BlockDriverState *bs)
823 BdrvDirtyBitmap *bm;
825 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
826 if (bdrv_dirty_bitmap_name(bm)) {
827 return true;
831 return false;
834 /* Called with BQL taken. */
835 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
837 bdrv_dirty_bitmaps_lock(bitmap->bs);
838 bitmap->persistent = persistent;
839 bdrv_dirty_bitmaps_unlock(bitmap->bs);
842 /* Called with BQL taken. */
843 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
845 bdrv_dirty_bitmaps_lock(bitmap->bs);
846 assert(bitmap->persistent == true);
847 bitmap->inconsistent = true;
848 bitmap->disabled = true;
849 bdrv_dirty_bitmaps_unlock(bitmap->bs);
852 /* Called with BQL taken. */
853 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
855 bdrv_dirty_bitmaps_lock(bitmap->bs);
856 bitmap->skip_store = skip;
857 bdrv_dirty_bitmaps_unlock(bitmap->bs);
860 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
862 return bitmap->persistent && !bitmap->skip_store;
865 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
867 return bitmap->inconsistent;
870 BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs)
872 return QLIST_FIRST(&bs->dirty_bitmaps);
875 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap)
877 return QLIST_NEXT(bitmap, list);
880 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
882 return hbitmap_sha256(bitmap->bitmap, errp);
885 int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *bitmap, int64_t offset,
886 int64_t bytes)
888 return hbitmap_next_dirty(bitmap->bitmap, offset, bytes);
891 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset,
892 int64_t bytes)
894 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
897 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
898 int64_t start, int64_t end, int64_t max_dirty_count,
899 int64_t *dirty_start, int64_t *dirty_count)
901 return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_count,
902 dirty_start, dirty_count);
906 * bdrv_merge_dirty_bitmap: merge src into dest.
907 * Ensures permissions on bitmaps are reasonable; use for public API.
909 * @backup: If provided, make a copy of dest here prior to merge.
911 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
912 HBitmap **backup, Error **errp)
914 bool ret;
916 bdrv_dirty_bitmaps_lock(dest->bs);
917 if (src->bs != dest->bs) {
918 bdrv_dirty_bitmaps_lock(src->bs);
921 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
922 goto out;
925 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
926 goto out;
929 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
930 error_setg(errp, "Bitmaps are incompatible and can't be merged");
931 goto out;
934 ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
935 assert(ret);
937 out:
938 bdrv_dirty_bitmaps_unlock(dest->bs);
939 if (src->bs != dest->bs) {
940 bdrv_dirty_bitmaps_unlock(src->bs);
945 * bdrv_dirty_bitmap_merge_internal: merge src into dest.
946 * Does NOT check bitmap permissions; not suitable for use as public API.
948 * @backup: If provided, make a copy of dest here prior to merge.
949 * @lock: If true, lock and unlock bitmaps on the way in/out.
950 * returns true if the merge succeeded; false if unattempted.
952 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
953 const BdrvDirtyBitmap *src,
954 HBitmap **backup,
955 bool lock)
957 bool ret;
959 assert(!bdrv_dirty_bitmap_readonly(dest));
960 assert(!bdrv_dirty_bitmap_inconsistent(dest));
961 assert(!bdrv_dirty_bitmap_inconsistent(src));
963 if (lock) {
964 bdrv_dirty_bitmaps_lock(dest->bs);
965 if (src->bs != dest->bs) {
966 bdrv_dirty_bitmaps_lock(src->bs);
970 if (backup) {
971 *backup = dest->bitmap;
972 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
973 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
974 } else {
975 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
978 if (lock) {
979 bdrv_dirty_bitmaps_unlock(dest->bs);
980 if (src->bs != dest->bs) {
981 bdrv_dirty_bitmaps_unlock(src->bs);
985 return ret;