block/dirty-bitmap: add bs link
[qemu/ar7.git] / block / dirty-bitmap.c
blob44453ff82417eb60662dcb9a413c99d54a6d48b1
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 QemuMutex *mutex;
33 BlockDriverState *bs;
34 HBitmap *bitmap; /* Dirty bitmap implementation */
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 skip_store; /* We are either migrating or deleting this
53 * bitmap; it should not be stored on the next
54 * inactivation. */
55 QLIST_ENTRY(BdrvDirtyBitmap) list;
58 struct BdrvDirtyBitmapIter {
59 HBitmapIter hbi;
60 BdrvDirtyBitmap *bitmap;
63 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
65 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
68 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
70 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
73 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
75 qemu_mutex_lock(bitmap->mutex);
78 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
80 qemu_mutex_unlock(bitmap->mutex);
83 /* Called with BQL or dirty_bitmap lock taken. */
84 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
86 BdrvDirtyBitmap *bm;
88 assert(name);
89 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
90 if (bm->name && !strcmp(name, bm->name)) {
91 return bm;
94 return NULL;
97 /* Called with BQL taken. */
98 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
99 uint32_t granularity,
100 const char *name,
101 Error **errp)
103 int64_t bitmap_size;
104 BdrvDirtyBitmap *bitmap;
106 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
108 if (name && bdrv_find_dirty_bitmap(bs, name)) {
109 error_setg(errp, "Bitmap already exists: %s", name);
110 return NULL;
112 bitmap_size = bdrv_getlength(bs);
113 if (bitmap_size < 0) {
114 error_setg_errno(errp, -bitmap_size, "could not get length of device");
115 errno = -bitmap_size;
116 return NULL;
118 bitmap = g_new0(BdrvDirtyBitmap, 1);
119 bitmap->bs = bs;
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 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
133 return bitmap->size;
136 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
138 return bitmap->name;
141 /* Called with BQL taken. */
142 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
144 return bitmap->successor;
147 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
149 return bitmap->busy;
152 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
154 qemu_mutex_lock(bitmap->mutex);
155 bitmap->busy = busy;
156 qemu_mutex_unlock(bitmap->mutex);
159 /* Called with BQL taken. */
160 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
162 return !bitmap->disabled;
166 * bdrv_dirty_bitmap_status: This API is now deprecated.
167 * Called with BQL taken.
169 * A BdrvDirtyBitmap can be in four possible user-visible states:
170 * (1) Active: successor is NULL, and disabled is false: full r/w mode
171 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
172 * guest writes are dropped, but monitor writes are possible,
173 * through commands like merge and clear.
174 * (3) Frozen: successor is not NULL.
175 * A frozen bitmap cannot be renamed, deleted, cleared, set,
176 * enabled, merged to, etc. A frozen bitmap can only abdicate()
177 * or reclaim().
178 * In this state, the anonymous successor bitmap may be either
179 * Active and recording writes from the guest (e.g. backup jobs),
180 * or it can be Disabled and not recording writes.
181 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
182 * in any way from the monitor.
183 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
184 * is unusable by QEMU. It can be deleted to remove it from
185 * the qcow2.
187 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
189 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
190 return DIRTY_BITMAP_STATUS_INCONSISTENT;
191 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
192 return DIRTY_BITMAP_STATUS_FROZEN;
193 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
194 return DIRTY_BITMAP_STATUS_LOCKED;
195 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
196 return DIRTY_BITMAP_STATUS_DISABLED;
197 } else {
198 return DIRTY_BITMAP_STATUS_ACTIVE;
202 /* Called with BQL taken. */
203 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
205 return !bitmap->disabled || (bitmap->successor &&
206 !bitmap->successor->disabled);
209 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
210 Error **errp)
212 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
213 error_setg(errp, "Bitmap '%s' is currently in use by another"
214 " operation and cannot be used", bitmap->name);
215 return -1;
218 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
219 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
220 bitmap->name);
221 return -1;
224 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
225 bdrv_dirty_bitmap_inconsistent(bitmap)) {
226 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
227 bitmap->name);
228 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
229 " this bitmap from disk");
230 return -1;
233 return 0;
237 * Create a successor bitmap destined to replace this bitmap after an operation.
238 * Requires that the bitmap is not marked busy and has no successor.
239 * The successor will be enabled if the parent bitmap was.
240 * Called with BQL taken.
242 int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp)
244 uint64_t granularity;
245 BdrvDirtyBitmap *child;
247 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
248 return -1;
250 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
251 error_setg(errp, "Cannot create a successor for a bitmap that already "
252 "has one");
253 return -1;
256 /* Create an anonymous successor */
257 granularity = bdrv_dirty_bitmap_granularity(bitmap);
258 child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp);
259 if (!child) {
260 return -1;
263 /* Successor will be on or off based on our current state. */
264 child->disabled = bitmap->disabled;
265 bitmap->disabled = true;
267 /* Install the successor and mark the parent as busy */
268 bitmap->successor = child;
269 bitmap->busy = true;
270 return 0;
273 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
275 bitmap->disabled = false;
278 /* Called with BQL taken. */
279 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
281 assert(bitmap->mutex == bitmap->successor->mutex);
282 qemu_mutex_lock(bitmap->mutex);
283 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
284 qemu_mutex_unlock(bitmap->mutex);
287 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
288 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
290 assert(!bitmap->active_iterators);
291 assert(!bdrv_dirty_bitmap_busy(bitmap));
292 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
293 QLIST_REMOVE(bitmap, list);
294 hbitmap_free(bitmap->bitmap);
295 g_free(bitmap->name);
296 g_free(bitmap);
300 * For a bitmap with a successor, yield our name to the successor,
301 * delete the old bitmap, and return a handle to the new bitmap.
302 * Called with BQL taken.
304 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap,
305 Error **errp)
307 char *name;
308 BdrvDirtyBitmap *successor = bitmap->successor;
310 if (successor == NULL) {
311 error_setg(errp, "Cannot relinquish control if "
312 "there's no successor present");
313 return NULL;
316 name = bitmap->name;
317 bitmap->name = NULL;
318 successor->name = name;
319 bitmap->successor = NULL;
320 successor->persistent = bitmap->persistent;
321 bitmap->persistent = false;
322 bitmap->busy = false;
323 bdrv_release_dirty_bitmap(bitmap);
325 return successor;
329 * In cases of failure where we can no longer safely delete the parent,
330 * we may wish to re-join the parent and child/successor.
331 * The merged parent will be marked as not busy.
332 * The marged parent will be enabled if and only if the successor was enabled.
333 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
335 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent,
336 Error **errp)
338 BdrvDirtyBitmap *successor = parent->successor;
340 if (!successor) {
341 error_setg(errp, "Cannot reclaim a successor when none is present");
342 return NULL;
345 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
346 error_setg(errp, "Merging of parent and successor bitmap failed");
347 return NULL;
350 parent->disabled = successor->disabled;
351 parent->busy = false;
352 bdrv_release_dirty_bitmap_locked(successor);
353 parent->successor = NULL;
355 return parent;
358 /* Called with BQL taken. */
359 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent,
360 Error **errp)
362 BdrvDirtyBitmap *ret;
364 qemu_mutex_lock(parent->mutex);
365 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp);
366 qemu_mutex_unlock(parent->mutex);
368 return ret;
372 * Truncates _all_ bitmaps attached to a BDS.
373 * Called with BQL taken.
375 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
377 BdrvDirtyBitmap *bitmap;
379 bdrv_dirty_bitmaps_lock(bs);
380 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
381 assert(!bdrv_dirty_bitmap_busy(bitmap));
382 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
383 assert(!bitmap->active_iterators);
384 hbitmap_truncate(bitmap->bitmap, bytes);
385 bitmap->size = bytes;
387 bdrv_dirty_bitmaps_unlock(bs);
390 /* Called with BQL taken. */
391 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap)
393 BlockDriverState *bs = bitmap->bs;
395 bdrv_dirty_bitmaps_lock(bs);
396 bdrv_release_dirty_bitmap_locked(bitmap);
397 bdrv_dirty_bitmaps_unlock(bs);
401 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
402 * There must not be any busy bitmaps attached.
403 * This function does not remove persistent bitmaps from the storage.
404 * Called with BQL taken.
406 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
408 BdrvDirtyBitmap *bm, *next;
410 bdrv_dirty_bitmaps_lock(bs);
411 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
412 if (bdrv_dirty_bitmap_name(bm)) {
413 bdrv_release_dirty_bitmap_locked(bm);
416 bdrv_dirty_bitmaps_unlock(bs);
420 * Remove persistent dirty bitmap from the storage if it exists.
421 * Absence of bitmap is not an error, because we have the following scenario:
422 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
423 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
424 * not fail.
425 * This function doesn't release corresponding BdrvDirtyBitmap.
427 static int coroutine_fn
428 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
429 Error **errp)
431 if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) {
432 return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
435 return 0;
438 typedef struct BdrvRemovePersistentDirtyBitmapCo {
439 BlockDriverState *bs;
440 const char *name;
441 Error **errp;
442 int ret;
443 } BdrvRemovePersistentDirtyBitmapCo;
445 static void coroutine_fn
446 bdrv_co_remove_persistent_dirty_bitmap_entry(void *opaque)
448 BdrvRemovePersistentDirtyBitmapCo *s = opaque;
450 s->ret = bdrv_co_remove_persistent_dirty_bitmap(s->bs, s->name, s->errp);
451 aio_wait_kick();
454 int bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
455 Error **errp)
457 if (qemu_in_coroutine()) {
458 return bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
459 } else {
460 Coroutine *co;
461 BdrvRemovePersistentDirtyBitmapCo s = {
462 .bs = bs,
463 .name = name,
464 .errp = errp,
465 .ret = -EINPROGRESS,
468 co = qemu_coroutine_create(bdrv_co_remove_persistent_dirty_bitmap_entry,
469 &s);
470 bdrv_coroutine_enter(bs, co);
471 BDRV_POLL_WHILE(bs, s.ret == -EINPROGRESS);
473 return s.ret;
477 static bool coroutine_fn
478 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
479 uint32_t granularity, Error **errp)
481 BlockDriver *drv = bs->drv;
483 if (!drv) {
484 error_setg_errno(errp, ENOMEDIUM,
485 "Can't store persistent bitmaps to %s",
486 bdrv_get_device_or_node_name(bs));
487 return false;
490 if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
491 error_setg_errno(errp, ENOTSUP,
492 "Can't store persistent bitmaps to %s",
493 bdrv_get_device_or_node_name(bs));
494 return false;
497 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
500 typedef struct BdrvCanStoreNewDirtyBitmapCo {
501 BlockDriverState *bs;
502 const char *name;
503 uint32_t granularity;
504 Error **errp;
505 bool ret;
507 bool in_progress;
508 } BdrvCanStoreNewDirtyBitmapCo;
510 static void coroutine_fn bdrv_co_can_store_new_dirty_bitmap_entry(void *opaque)
512 BdrvCanStoreNewDirtyBitmapCo *s = opaque;
514 s->ret = bdrv_co_can_store_new_dirty_bitmap(s->bs, s->name, s->granularity,
515 s->errp);
516 s->in_progress = false;
517 aio_wait_kick();
520 bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
521 uint32_t granularity, Error **errp)
523 if (qemu_in_coroutine()) {
524 return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
525 } else {
526 Coroutine *co;
527 BdrvCanStoreNewDirtyBitmapCo s = {
528 .bs = bs,
529 .name = name,
530 .granularity = granularity,
531 .errp = errp,
532 .in_progress = true,
535 co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry,
536 &s);
537 bdrv_coroutine_enter(bs, co);
538 BDRV_POLL_WHILE(bs, s.in_progress);
540 return s.ret;
544 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
546 bdrv_dirty_bitmap_lock(bitmap);
547 bitmap->disabled = true;
548 bdrv_dirty_bitmap_unlock(bitmap);
551 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
553 bdrv_dirty_bitmap_lock(bitmap);
554 bdrv_enable_dirty_bitmap_locked(bitmap);
555 bdrv_dirty_bitmap_unlock(bitmap);
558 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
560 BdrvDirtyBitmap *bm;
561 BlockDirtyInfoList *list = NULL;
562 BlockDirtyInfoList **plist = &list;
564 bdrv_dirty_bitmaps_lock(bs);
565 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
566 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
567 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
568 info->count = bdrv_get_dirty_count(bm);
569 info->granularity = bdrv_dirty_bitmap_granularity(bm);
570 info->has_name = !!bm->name;
571 info->name = g_strdup(bm->name);
572 info->status = bdrv_dirty_bitmap_status(bm);
573 info->recording = bdrv_dirty_bitmap_recording(bm);
574 info->busy = bdrv_dirty_bitmap_busy(bm);
575 info->persistent = bm->persistent;
576 info->has_inconsistent = bm->inconsistent;
577 info->inconsistent = bm->inconsistent;
578 entry->value = info;
579 *plist = entry;
580 plist = &entry->next;
582 bdrv_dirty_bitmaps_unlock(bs);
584 return list;
587 /* Called within bdrv_dirty_bitmap_lock..unlock */
588 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
590 return hbitmap_get(bitmap->bitmap, offset);
593 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
595 bool ret;
596 bdrv_dirty_bitmap_lock(bitmap);
597 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
598 bdrv_dirty_bitmap_unlock(bitmap);
600 return ret;
604 * Chooses a default granularity based on the existing cluster size,
605 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
606 * is no cluster size information available.
608 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
610 BlockDriverInfo bdi;
611 uint32_t granularity;
613 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
614 granularity = MAX(4096, bdi.cluster_size);
615 granularity = MIN(65536, granularity);
616 } else {
617 granularity = 65536;
620 return granularity;
623 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
625 return 1U << hbitmap_granularity(bitmap->bitmap);
628 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
630 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
631 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
632 iter->bitmap = bitmap;
633 bitmap->active_iterators++;
634 return iter;
637 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
639 if (!iter) {
640 return;
642 assert(iter->bitmap->active_iterators > 0);
643 iter->bitmap->active_iterators--;
644 g_free(iter);
647 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
649 return hbitmap_iter_next(&iter->hbi);
652 /* Called within bdrv_dirty_bitmap_lock..unlock */
653 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
654 int64_t offset, int64_t bytes)
656 assert(!bdrv_dirty_bitmap_readonly(bitmap));
657 hbitmap_set(bitmap->bitmap, offset, bytes);
660 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
661 int64_t offset, int64_t bytes)
663 bdrv_dirty_bitmap_lock(bitmap);
664 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
665 bdrv_dirty_bitmap_unlock(bitmap);
668 /* Called within bdrv_dirty_bitmap_lock..unlock */
669 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
670 int64_t offset, int64_t bytes)
672 assert(!bdrv_dirty_bitmap_readonly(bitmap));
673 hbitmap_reset(bitmap->bitmap, offset, bytes);
676 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
677 int64_t offset, int64_t bytes)
679 bdrv_dirty_bitmap_lock(bitmap);
680 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
681 bdrv_dirty_bitmap_unlock(bitmap);
684 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
686 assert(!bdrv_dirty_bitmap_readonly(bitmap));
687 bdrv_dirty_bitmap_lock(bitmap);
688 if (!out) {
689 hbitmap_reset_all(bitmap->bitmap);
690 } else {
691 HBitmap *backup = bitmap->bitmap;
692 bitmap->bitmap = hbitmap_alloc(bitmap->size,
693 hbitmap_granularity(backup));
694 *out = backup;
696 bdrv_dirty_bitmap_unlock(bitmap);
699 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
701 HBitmap *tmp = bitmap->bitmap;
702 assert(!bdrv_dirty_bitmap_readonly(bitmap));
703 bitmap->bitmap = backup;
704 hbitmap_free(tmp);
707 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
708 uint64_t offset, uint64_t bytes)
710 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
713 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
715 return hbitmap_serialization_align(bitmap->bitmap);
718 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
719 uint8_t *buf, uint64_t offset,
720 uint64_t bytes)
722 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
725 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
726 uint8_t *buf, uint64_t offset,
727 uint64_t bytes, bool finish)
729 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
732 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
733 uint64_t offset, uint64_t bytes,
734 bool finish)
736 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
739 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
740 uint64_t offset, uint64_t bytes,
741 bool finish)
743 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
746 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
748 hbitmap_deserialize_finish(bitmap->bitmap);
751 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
753 BdrvDirtyBitmap *bitmap;
755 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
756 return;
759 bdrv_dirty_bitmaps_lock(bs);
760 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
761 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
762 continue;
764 assert(!bdrv_dirty_bitmap_readonly(bitmap));
765 hbitmap_set(bitmap->bitmap, offset, bytes);
767 bdrv_dirty_bitmaps_unlock(bs);
771 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
773 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
775 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
778 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
780 return hbitmap_count(bitmap->bitmap);
783 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
785 return bitmap->readonly;
788 /* Called with BQL taken. */
789 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
791 qemu_mutex_lock(bitmap->mutex);
792 bitmap->readonly = value;
793 qemu_mutex_unlock(bitmap->mutex);
796 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
798 BdrvDirtyBitmap *bm;
799 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
800 if (bm->readonly) {
801 return true;
805 return false;
808 /* Called with BQL taken. */
809 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
811 qemu_mutex_lock(bitmap->mutex);
812 bitmap->persistent = persistent;
813 qemu_mutex_unlock(bitmap->mutex);
816 /* Called with BQL taken. */
817 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
819 qemu_mutex_lock(bitmap->mutex);
820 assert(bitmap->persistent == true);
821 bitmap->inconsistent = true;
822 bitmap->disabled = true;
823 qemu_mutex_unlock(bitmap->mutex);
826 /* Called with BQL taken. */
827 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
829 qemu_mutex_lock(bitmap->mutex);
830 bitmap->skip_store = skip;
831 qemu_mutex_unlock(bitmap->mutex);
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 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
846 BdrvDirtyBitmap *bm;
847 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
848 if (bm->persistent && !bm->readonly && !bm->skip_store) {
849 return true;
853 return false;
856 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
857 BdrvDirtyBitmap *bitmap)
859 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
860 QLIST_NEXT(bitmap, list);
863 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
865 return hbitmap_sha256(bitmap->bitmap, errp);
868 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
869 uint64_t bytes)
871 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
874 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
875 uint64_t *offset, uint64_t *bytes)
877 return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
881 * bdrv_merge_dirty_bitmap: merge src into dest.
882 * Ensures permissions on bitmaps are reasonable; use for public API.
884 * @backup: If provided, make a copy of dest here prior to merge.
886 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
887 HBitmap **backup, Error **errp)
889 bool ret;
891 qemu_mutex_lock(dest->mutex);
892 if (src->mutex != dest->mutex) {
893 qemu_mutex_lock(src->mutex);
896 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
897 goto out;
900 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
901 goto out;
904 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
905 error_setg(errp, "Bitmaps are incompatible and can't be merged");
906 goto out;
909 ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
910 assert(ret);
912 out:
913 qemu_mutex_unlock(dest->mutex);
914 if (src->mutex != dest->mutex) {
915 qemu_mutex_unlock(src->mutex);
920 * bdrv_dirty_bitmap_merge_internal: merge src into dest.
921 * Does NOT check bitmap permissions; not suitable for use as public API.
923 * @backup: If provided, make a copy of dest here prior to merge.
924 * @lock: If true, lock and unlock bitmaps on the way in/out.
925 * returns true if the merge succeeded; false if unattempted.
927 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
928 const BdrvDirtyBitmap *src,
929 HBitmap **backup,
930 bool lock)
932 bool ret;
934 assert(!bdrv_dirty_bitmap_readonly(dest));
935 assert(!bdrv_dirty_bitmap_inconsistent(dest));
936 assert(!bdrv_dirty_bitmap_inconsistent(src));
938 if (lock) {
939 qemu_mutex_lock(dest->mutex);
940 if (src->mutex != dest->mutex) {
941 qemu_mutex_lock(src->mutex);
945 if (backup) {
946 *backup = dest->bitmap;
947 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
948 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
949 } else {
950 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
953 if (lock) {
954 qemu_mutex_unlock(dest->mutex);
955 if (src->mutex != dest->mutex) {
956 qemu_mutex_unlock(src->mutex);
960 return ret;