xref: /openbmc/qemu/block/dirty-bitmap.c (revision 86044b24)
1 /*
2  * Block Dirty Bitmap
3  *
4  * Copyright (c) 2016-2017 Red Hat. Inc
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
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"
30 
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;
55 };
56 
57 struct BdrvDirtyBitmapIter {
58     HBitmapIter hbi;
59     BdrvDirtyBitmap *bitmap;
60 };
61 
62 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
63 {
64     qemu_mutex_lock(&bs->dirty_bitmap_mutex);
65 }
66 
67 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
68 {
69     qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
70 }
71 
72 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
73 {
74     bdrv_dirty_bitmaps_lock(bitmap->bs);
75 }
76 
77 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
78 {
79     bdrv_dirty_bitmaps_unlock(bitmap->bs);
80 }
81 
82 /* Called with BQL or dirty_bitmap lock taken.  */
83 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
84 {
85     BdrvDirtyBitmap *bm;
86 
87     assert(name);
88     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
89         if (bm->name && !strcmp(name, bm->name)) {
90             return bm;
91         }
92     }
93     return NULL;
94 }
95 
96 /* Called with BQL taken.  */
97 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
98                                           uint32_t granularity,
99                                           const char *name,
100                                           Error **errp)
101 {
102     int64_t bitmap_size;
103     BdrvDirtyBitmap *bitmap;
104 
105     assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
106 
107     if (name && bdrv_find_dirty_bitmap(bs, name)) {
108         error_setg(errp, "Bitmap already exists: %s", name);
109         return NULL;
110     }
111     bitmap_size = bdrv_getlength(bs);
112     if (bitmap_size < 0) {
113         error_setg_errno(errp, -bitmap_size, "could not get length of device");
114         errno = -bitmap_size;
115         return NULL;
116     }
117     bitmap = g_new0(BdrvDirtyBitmap, 1);
118     bitmap->bs = bs;
119     bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
120     bitmap->size = bitmap_size;
121     bitmap->name = g_strdup(name);
122     bitmap->disabled = false;
123     bdrv_dirty_bitmaps_lock(bs);
124     QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
125     bdrv_dirty_bitmaps_unlock(bs);
126     return bitmap;
127 }
128 
129 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
130 {
131     return bitmap->size;
132 }
133 
134 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
135 {
136     return bitmap->name;
137 }
138 
139 /* Called with BQL taken.  */
140 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
141 {
142     return bitmap->successor;
143 }
144 
145 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
146 {
147     return bitmap->busy;
148 }
149 
150 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
151 {
152     bdrv_dirty_bitmaps_lock(bitmap->bs);
153     bitmap->busy = busy;
154     bdrv_dirty_bitmaps_unlock(bitmap->bs);
155 }
156 
157 /* Called with BQL taken.  */
158 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
159 {
160     return !bitmap->disabled;
161 }
162 
163 /**
164  * bdrv_dirty_bitmap_status: This API is now deprecated.
165  * Called with BQL taken.
166  *
167  * A BdrvDirtyBitmap can be in four possible user-visible states:
168  * (1) Active:   successor is NULL, and disabled is false: full r/w mode
169  * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
170  *               guest writes are dropped, but monitor writes are possible,
171  *               through commands like merge and clear.
172  * (3) Frozen:   successor is not NULL.
173  *               A frozen bitmap cannot be renamed, deleted, cleared, set,
174  *               enabled, merged to, etc. A frozen bitmap can only abdicate()
175  *               or reclaim().
176  *               In this state, the anonymous successor bitmap may be either
177  *               Active and recording writes from the guest (e.g. backup jobs),
178  *               or it can be Disabled and not recording writes.
179  * (4) Locked:   Whether Active or Disabled, the user cannot modify this bitmap
180  *               in any way from the monitor.
181  * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
182  *                   is unusable by QEMU. It can be deleted to remove it from
183  *                   the qcow2.
184  */
185 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
186 {
187     if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
188         return DIRTY_BITMAP_STATUS_INCONSISTENT;
189     } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
190         return DIRTY_BITMAP_STATUS_FROZEN;
191     } else if (bdrv_dirty_bitmap_busy(bitmap)) {
192         return DIRTY_BITMAP_STATUS_LOCKED;
193     } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
194         return DIRTY_BITMAP_STATUS_DISABLED;
195     } else {
196         return DIRTY_BITMAP_STATUS_ACTIVE;
197     }
198 }
199 
200 /* Called with BQL taken.  */
201 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
202 {
203     return !bitmap->disabled || (bitmap->successor &&
204                                  !bitmap->successor->disabled);
205 }
206 
207 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
208                             Error **errp)
209 {
210     if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
211         error_setg(errp, "Bitmap '%s' is currently in use by another"
212                    " operation and cannot be used", bitmap->name);
213         return -1;
214     }
215 
216     if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
217         error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
218                    bitmap->name);
219         return -1;
220     }
221 
222     if ((flags & BDRV_BITMAP_INCONSISTENT) &&
223         bdrv_dirty_bitmap_inconsistent(bitmap)) {
224         error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
225                    bitmap->name);
226         error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
227                           " this bitmap from disk");
228         return -1;
229     }
230 
231     return 0;
232 }
233 
234 /**
235  * Create a successor bitmap destined to replace this bitmap after an operation.
236  * Requires that the bitmap is not marked busy and has no successor.
237  * The successor will be enabled if the parent bitmap was.
238  * Called with BQL taken.
239  */
240 int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp)
241 {
242     uint64_t granularity;
243     BdrvDirtyBitmap *child;
244 
245     if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
246         return -1;
247     }
248     if (bdrv_dirty_bitmap_has_successor(bitmap)) {
249         error_setg(errp, "Cannot create a successor for a bitmap that already "
250                    "has one");
251         return -1;
252     }
253 
254     /* Create an anonymous successor */
255     granularity = bdrv_dirty_bitmap_granularity(bitmap);
256     child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp);
257     if (!child) {
258         return -1;
259     }
260 
261     /* Successor will be on or off based on our current state. */
262     child->disabled = bitmap->disabled;
263     bitmap->disabled = true;
264 
265     /* Install the successor and mark the parent as busy */
266     bitmap->successor = child;
267     bitmap->busy = true;
268     return 0;
269 }
270 
271 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
272 {
273     bitmap->disabled = false;
274 }
275 
276 /* Called with BQL taken. */
277 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
278 {
279     assert(bitmap->bs == bitmap->successor->bs);
280     bdrv_dirty_bitmaps_lock(bitmap->bs);
281     bdrv_enable_dirty_bitmap_locked(bitmap->successor);
282     bdrv_dirty_bitmaps_unlock(bitmap->bs);
283 }
284 
285 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.  */
286 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
287 {
288     assert(!bitmap->active_iterators);
289     assert(!bdrv_dirty_bitmap_busy(bitmap));
290     assert(!bdrv_dirty_bitmap_has_successor(bitmap));
291     QLIST_REMOVE(bitmap, list);
292     hbitmap_free(bitmap->bitmap);
293     g_free(bitmap->name);
294     g_free(bitmap);
295 }
296 
297 /**
298  * For a bitmap with a successor, yield our name to the successor,
299  * delete the old bitmap, and return a handle to the new bitmap.
300  * Called with BQL taken.
301  */
302 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap,
303                                             Error **errp)
304 {
305     char *name;
306     BdrvDirtyBitmap *successor = bitmap->successor;
307 
308     if (successor == NULL) {
309         error_setg(errp, "Cannot relinquish control if "
310                    "there's no successor present");
311         return NULL;
312     }
313 
314     name = bitmap->name;
315     bitmap->name = NULL;
316     successor->name = name;
317     bitmap->successor = NULL;
318     successor->persistent = bitmap->persistent;
319     bitmap->persistent = false;
320     bitmap->busy = false;
321     bdrv_release_dirty_bitmap(bitmap);
322 
323     return successor;
324 }
325 
326 /**
327  * In cases of failure where we can no longer safely delete the parent,
328  * we may wish to re-join the parent and child/successor.
329  * The merged parent will be marked as not busy.
330  * The marged parent will be enabled if and only if the successor was enabled.
331  * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
332  */
333 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent,
334                                                   Error **errp)
335 {
336     BdrvDirtyBitmap *successor = parent->successor;
337 
338     if (!successor) {
339         error_setg(errp, "Cannot reclaim a successor when none is present");
340         return NULL;
341     }
342 
343     if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
344         error_setg(errp, "Merging of parent and successor bitmap failed");
345         return NULL;
346     }
347 
348     parent->disabled = successor->disabled;
349     parent->busy = false;
350     bdrv_release_dirty_bitmap_locked(successor);
351     parent->successor = NULL;
352 
353     return parent;
354 }
355 
356 /* Called with BQL taken. */
357 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent,
358                                            Error **errp)
359 {
360     BdrvDirtyBitmap *ret;
361 
362     bdrv_dirty_bitmaps_lock(parent->bs);
363     ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp);
364     bdrv_dirty_bitmaps_unlock(parent->bs);
365 
366     return ret;
367 }
368 
369 /**
370  * Truncates _all_ bitmaps attached to a BDS.
371  * Called with BQL taken.
372  */
373 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
374 {
375     BdrvDirtyBitmap *bitmap;
376 
377     bdrv_dirty_bitmaps_lock(bs);
378     QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
379         assert(!bdrv_dirty_bitmap_busy(bitmap));
380         assert(!bdrv_dirty_bitmap_has_successor(bitmap));
381         assert(!bitmap->active_iterators);
382         hbitmap_truncate(bitmap->bitmap, bytes);
383         bitmap->size = bytes;
384     }
385     bdrv_dirty_bitmaps_unlock(bs);
386 }
387 
388 /* Called with BQL taken.  */
389 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap)
390 {
391     BlockDriverState *bs = bitmap->bs;
392 
393     bdrv_dirty_bitmaps_lock(bs);
394     bdrv_release_dirty_bitmap_locked(bitmap);
395     bdrv_dirty_bitmaps_unlock(bs);
396 }
397 
398 /**
399  * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
400  * There must not be any busy bitmaps attached.
401  * This function does not remove persistent bitmaps from the storage.
402  * Called with BQL taken.
403  */
404 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
405 {
406     BdrvDirtyBitmap *bm, *next;
407 
408     bdrv_dirty_bitmaps_lock(bs);
409     QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
410         if (bdrv_dirty_bitmap_name(bm)) {
411             bdrv_release_dirty_bitmap_locked(bm);
412         }
413     }
414     bdrv_dirty_bitmaps_unlock(bs);
415 }
416 
417 /**
418  * Remove persistent dirty bitmap from the storage if it exists.
419  * Absence of bitmap is not an error, because we have the following scenario:
420  * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
421  * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
422  * not fail.
423  * This function doesn't release corresponding BdrvDirtyBitmap.
424  */
425 static int coroutine_fn
426 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
427                                        Error **errp)
428 {
429     if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) {
430         return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
431     }
432 
433     return 0;
434 }
435 
436 typedef struct BdrvRemovePersistentDirtyBitmapCo {
437     BlockDriverState *bs;
438     const char *name;
439     Error **errp;
440     int ret;
441 } BdrvRemovePersistentDirtyBitmapCo;
442 
443 static void coroutine_fn
444 bdrv_co_remove_persistent_dirty_bitmap_entry(void *opaque)
445 {
446     BdrvRemovePersistentDirtyBitmapCo *s = opaque;
447 
448     s->ret = bdrv_co_remove_persistent_dirty_bitmap(s->bs, s->name, s->errp);
449     aio_wait_kick();
450 }
451 
452 int bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
453                                         Error **errp)
454 {
455     if (qemu_in_coroutine()) {
456         return bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
457     } else {
458         Coroutine *co;
459         BdrvRemovePersistentDirtyBitmapCo s = {
460             .bs = bs,
461             .name = name,
462             .errp = errp,
463             .ret = -EINPROGRESS,
464         };
465 
466         co = qemu_coroutine_create(bdrv_co_remove_persistent_dirty_bitmap_entry,
467                                    &s);
468         bdrv_coroutine_enter(bs, co);
469         BDRV_POLL_WHILE(bs, s.ret == -EINPROGRESS);
470 
471         return s.ret;
472     }
473 }
474 
475 static bool coroutine_fn
476 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
477                                    uint32_t granularity, Error **errp)
478 {
479     BlockDriver *drv = bs->drv;
480 
481     if (!drv) {
482         error_setg_errno(errp, ENOMEDIUM,
483                          "Can't store persistent bitmaps to %s",
484                          bdrv_get_device_or_node_name(bs));
485         return false;
486     }
487 
488     if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
489         error_setg_errno(errp, ENOTSUP,
490                          "Can't store persistent bitmaps to %s",
491                          bdrv_get_device_or_node_name(bs));
492         return false;
493     }
494 
495     return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
496 }
497 
498 typedef struct BdrvCanStoreNewDirtyBitmapCo {
499     BlockDriverState *bs;
500     const char *name;
501     uint32_t granularity;
502     Error **errp;
503     bool ret;
504 
505     bool in_progress;
506 } BdrvCanStoreNewDirtyBitmapCo;
507 
508 static void coroutine_fn bdrv_co_can_store_new_dirty_bitmap_entry(void *opaque)
509 {
510     BdrvCanStoreNewDirtyBitmapCo *s = opaque;
511 
512     s->ret = bdrv_co_can_store_new_dirty_bitmap(s->bs, s->name, s->granularity,
513                                                 s->errp);
514     s->in_progress = false;
515     aio_wait_kick();
516 }
517 
518 bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
519                                      uint32_t granularity, Error **errp)
520 {
521     if (qemu_in_coroutine()) {
522         return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
523     } else {
524         Coroutine *co;
525         BdrvCanStoreNewDirtyBitmapCo s = {
526             .bs = bs,
527             .name = name,
528             .granularity = granularity,
529             .errp = errp,
530             .in_progress = true,
531         };
532 
533         co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry,
534                                    &s);
535         bdrv_coroutine_enter(bs, co);
536         BDRV_POLL_WHILE(bs, s.in_progress);
537 
538         return s.ret;
539     }
540 }
541 
542 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
543 {
544     bdrv_dirty_bitmaps_lock(bitmap->bs);
545     bitmap->disabled = true;
546     bdrv_dirty_bitmaps_unlock(bitmap->bs);
547 }
548 
549 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
550 {
551     bdrv_dirty_bitmaps_lock(bitmap->bs);
552     bdrv_enable_dirty_bitmap_locked(bitmap);
553     bdrv_dirty_bitmaps_unlock(bitmap->bs);
554 }
555 
556 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
557 {
558     BdrvDirtyBitmap *bm;
559     BlockDirtyInfoList *list = NULL;
560     BlockDirtyInfoList **plist = &list;
561 
562     bdrv_dirty_bitmaps_lock(bs);
563     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
564         BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
565         BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
566         info->count = bdrv_get_dirty_count(bm);
567         info->granularity = bdrv_dirty_bitmap_granularity(bm);
568         info->has_name = !!bm->name;
569         info->name = g_strdup(bm->name);
570         info->status = bdrv_dirty_bitmap_status(bm);
571         info->recording = bdrv_dirty_bitmap_recording(bm);
572         info->busy = bdrv_dirty_bitmap_busy(bm);
573         info->persistent = bm->persistent;
574         info->has_inconsistent = bm->inconsistent;
575         info->inconsistent = bm->inconsistent;
576         entry->value = info;
577         *plist = entry;
578         plist = &entry->next;
579     }
580     bdrv_dirty_bitmaps_unlock(bs);
581 
582     return list;
583 }
584 
585 /* Called within bdrv_dirty_bitmap_lock..unlock */
586 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
587 {
588     return hbitmap_get(bitmap->bitmap, offset);
589 }
590 
591 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
592 {
593     bool ret;
594     bdrv_dirty_bitmaps_lock(bitmap->bs);
595     ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
596     bdrv_dirty_bitmaps_unlock(bitmap->bs);
597 
598     return ret;
599 }
600 
601 /**
602  * Chooses a default granularity based on the existing cluster size,
603  * but clamped between [4K, 64K]. Defaults to 64K in the case that there
604  * is no cluster size information available.
605  */
606 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
607 {
608     BlockDriverInfo bdi;
609     uint32_t granularity;
610 
611     if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
612         granularity = MAX(4096, bdi.cluster_size);
613         granularity = MIN(65536, granularity);
614     } else {
615         granularity = 65536;
616     }
617 
618     return granularity;
619 }
620 
621 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
622 {
623     return 1U << hbitmap_granularity(bitmap->bitmap);
624 }
625 
626 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
627 {
628     BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
629     hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
630     iter->bitmap = bitmap;
631     bitmap->active_iterators++;
632     return iter;
633 }
634 
635 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
636 {
637     if (!iter) {
638         return;
639     }
640     assert(iter->bitmap->active_iterators > 0);
641     iter->bitmap->active_iterators--;
642     g_free(iter);
643 }
644 
645 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
646 {
647     return hbitmap_iter_next(&iter->hbi);
648 }
649 
650 /* Called within bdrv_dirty_bitmap_lock..unlock */
651 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
652                                   int64_t offset, int64_t bytes)
653 {
654     assert(!bdrv_dirty_bitmap_readonly(bitmap));
655     hbitmap_set(bitmap->bitmap, offset, bytes);
656 }
657 
658 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
659                            int64_t offset, int64_t bytes)
660 {
661     bdrv_dirty_bitmaps_lock(bitmap->bs);
662     bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
663     bdrv_dirty_bitmaps_unlock(bitmap->bs);
664 }
665 
666 /* Called within bdrv_dirty_bitmap_lock..unlock */
667 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
668                                     int64_t offset, int64_t bytes)
669 {
670     assert(!bdrv_dirty_bitmap_readonly(bitmap));
671     hbitmap_reset(bitmap->bitmap, offset, bytes);
672 }
673 
674 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
675                              int64_t offset, int64_t bytes)
676 {
677     bdrv_dirty_bitmaps_lock(bitmap->bs);
678     bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
679     bdrv_dirty_bitmaps_unlock(bitmap->bs);
680 }
681 
682 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
683 {
684     assert(!bdrv_dirty_bitmap_readonly(bitmap));
685     bdrv_dirty_bitmaps_lock(bitmap->bs);
686     if (!out) {
687         hbitmap_reset_all(bitmap->bitmap);
688     } else {
689         HBitmap *backup = bitmap->bitmap;
690         bitmap->bitmap = hbitmap_alloc(bitmap->size,
691                                        hbitmap_granularity(backup));
692         *out = backup;
693     }
694     bdrv_dirty_bitmaps_unlock(bitmap->bs);
695 }
696 
697 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
698 {
699     HBitmap *tmp = bitmap->bitmap;
700     assert(!bdrv_dirty_bitmap_readonly(bitmap));
701     bitmap->bitmap = backup;
702     hbitmap_free(tmp);
703 }
704 
705 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
706                                               uint64_t offset, uint64_t bytes)
707 {
708     return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
709 }
710 
711 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
712 {
713     return hbitmap_serialization_align(bitmap->bitmap);
714 }
715 
716 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
717                                       uint8_t *buf, uint64_t offset,
718                                       uint64_t bytes)
719 {
720     hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
721 }
722 
723 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
724                                         uint8_t *buf, uint64_t offset,
725                                         uint64_t bytes, bool finish)
726 {
727     hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
728 }
729 
730 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
731                                           uint64_t offset, uint64_t bytes,
732                                           bool finish)
733 {
734     hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
735 }
736 
737 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
738                                         uint64_t offset, uint64_t bytes,
739                                         bool finish)
740 {
741     hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
742 }
743 
744 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
745 {
746     hbitmap_deserialize_finish(bitmap->bitmap);
747 }
748 
749 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
750 {
751     BdrvDirtyBitmap *bitmap;
752 
753     if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
754         return;
755     }
756 
757     bdrv_dirty_bitmaps_lock(bs);
758     QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
759         if (!bdrv_dirty_bitmap_enabled(bitmap)) {
760             continue;
761         }
762         assert(!bdrv_dirty_bitmap_readonly(bitmap));
763         hbitmap_set(bitmap->bitmap, offset, bytes);
764     }
765     bdrv_dirty_bitmaps_unlock(bs);
766 }
767 
768 /**
769  * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
770  */
771 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
772 {
773     hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
774 }
775 
776 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
777 {
778     return hbitmap_count(bitmap->bitmap);
779 }
780 
781 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
782 {
783     return bitmap->readonly;
784 }
785 
786 /* Called with BQL taken. */
787 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
788 {
789     bdrv_dirty_bitmaps_lock(bitmap->bs);
790     bitmap->readonly = value;
791     bdrv_dirty_bitmaps_unlock(bitmap->bs);
792 }
793 
794 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
795 {
796     BdrvDirtyBitmap *bm;
797     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
798         if (bm->readonly) {
799             return true;
800         }
801     }
802 
803     return false;
804 }
805 
806 /* Called with BQL taken. */
807 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
808 {
809     bdrv_dirty_bitmaps_lock(bitmap->bs);
810     bitmap->persistent = persistent;
811     bdrv_dirty_bitmaps_unlock(bitmap->bs);
812 }
813 
814 /* Called with BQL taken. */
815 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
816 {
817     bdrv_dirty_bitmaps_lock(bitmap->bs);
818     assert(bitmap->persistent == true);
819     bitmap->inconsistent = true;
820     bitmap->disabled = true;
821     bdrv_dirty_bitmaps_unlock(bitmap->bs);
822 }
823 
824 /* Called with BQL taken. */
825 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
826 {
827     bdrv_dirty_bitmaps_lock(bitmap->bs);
828     bitmap->skip_store = skip;
829     bdrv_dirty_bitmaps_unlock(bitmap->bs);
830 }
831 
832 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
833 {
834     return bitmap->persistent && !bitmap->skip_store;
835 }
836 
837 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
838 {
839     return bitmap->inconsistent;
840 }
841 
842 BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs)
843 {
844     return QLIST_FIRST(&bs->dirty_bitmaps);
845 }
846 
847 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap)
848 {
849     return QLIST_NEXT(bitmap, list);
850 }
851 
852 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
853 {
854     return hbitmap_sha256(bitmap->bitmap, errp);
855 }
856 
857 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
858                                     uint64_t bytes)
859 {
860     return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
861 }
862 
863 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
864                                        uint64_t *offset, uint64_t *bytes)
865 {
866     return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
867 }
868 
869 /**
870  * bdrv_merge_dirty_bitmap: merge src into dest.
871  * Ensures permissions on bitmaps are reasonable; use for public API.
872  *
873  * @backup: If provided, make a copy of dest here prior to merge.
874  */
875 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
876                              HBitmap **backup, Error **errp)
877 {
878     bool ret;
879 
880     bdrv_dirty_bitmaps_lock(dest->bs);
881     if (src->bs != dest->bs) {
882         bdrv_dirty_bitmaps_lock(src->bs);
883     }
884 
885     if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
886         goto out;
887     }
888 
889     if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
890         goto out;
891     }
892 
893     if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
894         error_setg(errp, "Bitmaps are incompatible and can't be merged");
895         goto out;
896     }
897 
898     ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
899     assert(ret);
900 
901 out:
902     bdrv_dirty_bitmaps_unlock(dest->bs);
903     if (src->bs != dest->bs) {
904         bdrv_dirty_bitmaps_unlock(src->bs);
905     }
906 }
907 
908 /**
909  * bdrv_dirty_bitmap_merge_internal: merge src into dest.
910  * Does NOT check bitmap permissions; not suitable for use as public API.
911  *
912  * @backup: If provided, make a copy of dest here prior to merge.
913  * @lock: If true, lock and unlock bitmaps on the way in/out.
914  * returns true if the merge succeeded; false if unattempted.
915  */
916 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
917                                       const BdrvDirtyBitmap *src,
918                                       HBitmap **backup,
919                                       bool lock)
920 {
921     bool ret;
922 
923     assert(!bdrv_dirty_bitmap_readonly(dest));
924     assert(!bdrv_dirty_bitmap_inconsistent(dest));
925     assert(!bdrv_dirty_bitmap_inconsistent(src));
926 
927     if (lock) {
928         bdrv_dirty_bitmaps_lock(dest->bs);
929         if (src->bs != dest->bs) {
930             bdrv_dirty_bitmaps_lock(src->bs);
931         }
932     }
933 
934     if (backup) {
935         *backup = dest->bitmap;
936         dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
937         ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
938     } else {
939         ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
940     }
941 
942     if (lock) {
943         bdrv_dirty_bitmaps_unlock(dest->bs);
944         if (src->bs != dest->bs) {
945             bdrv_dirty_bitmaps_unlock(src->bs);
946         }
947     }
948 
949     return ret;
950 }
951