xref: /openbmc/qemu/block/dirty-bitmap.c (revision 016d4b01)
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 "qemu-common.h"
27 #include "trace.h"
28 #include "block/block_int.h"
29 #include "block/blockjob.h"
30 
31 /**
32  * A BdrvDirtyBitmap can be in three possible states:
33  * (1) successor is NULL and disabled is false: full r/w mode
34  * (2) successor is NULL and disabled is true: read only mode ("disabled")
35  * (3) successor is set: frozen mode.
36  *     A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set,
37  *     or enabled. A frozen bitmap can only abdicate() or reclaim().
38  */
39 struct BdrvDirtyBitmap {
40     QemuMutex *mutex;
41     HBitmap *bitmap;            /* Dirty bitmap implementation */
42     HBitmap *meta;              /* Meta dirty bitmap */
43     bool qmp_locked;            /* Bitmap is locked, it can't be modified
44                                    through QMP */
45     BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */
46     char *name;                 /* Optional non-empty unique ID */
47     int64_t size;               /* Size of the bitmap, in bytes */
48     bool disabled;              /* Bitmap is disabled. It ignores all writes to
49                                    the device */
50     int active_iterators;       /* How many iterators are active */
51     bool readonly;              /* Bitmap is read-only. This field also
52                                    prevents the respective image from being
53                                    modified (i.e. blocks writes and discards).
54                                    Such operations must fail and both the image
55                                    and this bitmap must remain unchanged while
56                                    this flag is set. */
57     bool persistent;            /* bitmap must be saved to owner disk image */
58     QLIST_ENTRY(BdrvDirtyBitmap) list;
59 };
60 
61 struct BdrvDirtyBitmapIter {
62     HBitmapIter hbi;
63     BdrvDirtyBitmap *bitmap;
64 };
65 
66 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
67 {
68     qemu_mutex_lock(&bs->dirty_bitmap_mutex);
69 }
70 
71 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
72 {
73     qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
74 }
75 
76 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
77 {
78     qemu_mutex_lock(bitmap->mutex);
79 }
80 
81 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
82 {
83     qemu_mutex_unlock(bitmap->mutex);
84 }
85 
86 /* Called with BQL or dirty_bitmap lock taken.  */
87 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
88 {
89     BdrvDirtyBitmap *bm;
90 
91     assert(name);
92     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
93         if (bm->name && !strcmp(name, bm->name)) {
94             return bm;
95         }
96     }
97     return NULL;
98 }
99 
100 /* Called with BQL taken.  */
101 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
102                                           uint32_t granularity,
103                                           const char *name,
104                                           Error **errp)
105 {
106     int64_t bitmap_size;
107     BdrvDirtyBitmap *bitmap;
108 
109     assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
110 
111     if (name && bdrv_find_dirty_bitmap(bs, name)) {
112         error_setg(errp, "Bitmap already exists: %s", name);
113         return NULL;
114     }
115     bitmap_size = bdrv_getlength(bs);
116     if (bitmap_size < 0) {
117         error_setg_errno(errp, -bitmap_size, "could not get length of device");
118         errno = -bitmap_size;
119         return NULL;
120     }
121     bitmap = g_new0(BdrvDirtyBitmap, 1);
122     bitmap->mutex = &bs->dirty_bitmap_mutex;
123     bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
124     bitmap->size = bitmap_size;
125     bitmap->name = g_strdup(name);
126     bitmap->disabled = false;
127     bdrv_dirty_bitmaps_lock(bs);
128     QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
129     bdrv_dirty_bitmaps_unlock(bs);
130     return bitmap;
131 }
132 
133 /* bdrv_create_meta_dirty_bitmap
134  *
135  * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
136  * when a dirty status bit in @bitmap is changed (either from reset to set or
137  * the other way around), its respective meta dirty bitmap bit will be marked
138  * dirty as well.
139  *
140  * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
141  * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
142  * track.
143  */
144 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
145                                    int chunk_size)
146 {
147     assert(!bitmap->meta);
148     qemu_mutex_lock(bitmap->mutex);
149     bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
150                                        chunk_size * BITS_PER_BYTE);
151     qemu_mutex_unlock(bitmap->mutex);
152 }
153 
154 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
155 {
156     assert(bitmap->meta);
157     qemu_mutex_lock(bitmap->mutex);
158     hbitmap_free_meta(bitmap->bitmap);
159     bitmap->meta = NULL;
160     qemu_mutex_unlock(bitmap->mutex);
161 }
162 
163 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
164 {
165     return bitmap->size;
166 }
167 
168 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
169 {
170     return bitmap->name;
171 }
172 
173 /* Called with BQL taken.  */
174 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
175 {
176     return bitmap->successor;
177 }
178 
179 void bdrv_dirty_bitmap_set_qmp_locked(BdrvDirtyBitmap *bitmap, bool qmp_locked)
180 {
181     qemu_mutex_lock(bitmap->mutex);
182     bitmap->qmp_locked = qmp_locked;
183     qemu_mutex_unlock(bitmap->mutex);
184 }
185 
186 bool bdrv_dirty_bitmap_qmp_locked(BdrvDirtyBitmap *bitmap)
187 {
188     return bitmap->qmp_locked;
189 }
190 
191 /* Called with BQL taken.  */
192 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
193 {
194     return !(bitmap->disabled || bitmap->successor);
195 }
196 
197 /* Called with BQL taken.  */
198 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
199 {
200     if (bdrv_dirty_bitmap_frozen(bitmap)) {
201         return DIRTY_BITMAP_STATUS_FROZEN;
202     } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) {
203         return DIRTY_BITMAP_STATUS_LOCKED;
204     } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
205         return DIRTY_BITMAP_STATUS_DISABLED;
206     } else {
207         return DIRTY_BITMAP_STATUS_ACTIVE;
208     }
209 }
210 
211 /**
212  * Create a successor bitmap destined to replace this bitmap after an operation.
213  * Requires that the bitmap is not frozen and has no successor.
214  * Called with BQL taken.
215  */
216 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
217                                        BdrvDirtyBitmap *bitmap, Error **errp)
218 {
219     uint64_t granularity;
220     BdrvDirtyBitmap *child;
221 
222     if (bdrv_dirty_bitmap_frozen(bitmap)) {
223         error_setg(errp, "Cannot create a successor for a bitmap that is "
224                    "currently frozen");
225         return -1;
226     }
227     assert(!bitmap->successor);
228 
229     /* Create an anonymous successor */
230     granularity = bdrv_dirty_bitmap_granularity(bitmap);
231     child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
232     if (!child) {
233         return -1;
234     }
235 
236     /* Successor will be on or off based on our current state. */
237     child->disabled = bitmap->disabled;
238 
239     /* Install the successor and freeze the parent */
240     bitmap->successor = child;
241     return 0;
242 }
243 
244 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
245 {
246     assert(!bdrv_dirty_bitmap_frozen(bitmap));
247     bitmap->disabled = false;
248 }
249 
250 /* Called with BQL taken. */
251 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
252 {
253     assert(bitmap->mutex == bitmap->successor->mutex);
254     qemu_mutex_lock(bitmap->mutex);
255     bdrv_enable_dirty_bitmap_locked(bitmap->successor);
256     qemu_mutex_unlock(bitmap->mutex);
257 }
258 
259 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.  */
260 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
261 {
262     assert(!bitmap->active_iterators);
263     assert(!bdrv_dirty_bitmap_frozen(bitmap));
264     assert(!bitmap->meta);
265     QLIST_REMOVE(bitmap, list);
266     hbitmap_free(bitmap->bitmap);
267     g_free(bitmap->name);
268     g_free(bitmap);
269 }
270 
271 /**
272  * For a bitmap with a successor, yield our name to the successor,
273  * delete the old bitmap, and return a handle to the new bitmap.
274  * Called with BQL taken.
275  */
276 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
277                                             BdrvDirtyBitmap *bitmap,
278                                             Error **errp)
279 {
280     char *name;
281     BdrvDirtyBitmap *successor = bitmap->successor;
282 
283     if (successor == NULL) {
284         error_setg(errp, "Cannot relinquish control if "
285                    "there's no successor present");
286         return NULL;
287     }
288 
289     name = bitmap->name;
290     bitmap->name = NULL;
291     successor->name = name;
292     bitmap->successor = NULL;
293     successor->persistent = bitmap->persistent;
294     bitmap->persistent = false;
295     bdrv_release_dirty_bitmap(bs, bitmap);
296 
297     return successor;
298 }
299 
300 /**
301  * In cases of failure where we can no longer safely delete the parent,
302  * we may wish to re-join the parent and child/successor.
303  * The merged parent will be un-frozen, but not explicitly re-enabled.
304  * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
305  */
306 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
307                                                   BdrvDirtyBitmap *parent,
308                                                   Error **errp)
309 {
310     BdrvDirtyBitmap *successor = parent->successor;
311 
312     if (!successor) {
313         error_setg(errp, "Cannot reclaim a successor when none is present");
314         return NULL;
315     }
316 
317     if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
318         error_setg(errp, "Merging of parent and successor bitmap failed");
319         return NULL;
320     }
321     bdrv_release_dirty_bitmap_locked(successor);
322     parent->successor = NULL;
323 
324     return parent;
325 }
326 
327 /* Called with BQL taken. */
328 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
329                                            BdrvDirtyBitmap *parent,
330                                            Error **errp)
331 {
332     BdrvDirtyBitmap *ret;
333 
334     qemu_mutex_lock(parent->mutex);
335     ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
336     qemu_mutex_unlock(parent->mutex);
337 
338     return ret;
339 }
340 
341 /**
342  * Truncates _all_ bitmaps attached to a BDS.
343  * Called with BQL taken.
344  */
345 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
346 {
347     BdrvDirtyBitmap *bitmap;
348 
349     bdrv_dirty_bitmaps_lock(bs);
350     QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
351         assert(!bdrv_dirty_bitmap_frozen(bitmap));
352         assert(!bitmap->active_iterators);
353         hbitmap_truncate(bitmap->bitmap, bytes);
354         bitmap->size = bytes;
355     }
356     bdrv_dirty_bitmaps_unlock(bs);
357 }
358 
359 /* Called with BQL taken.  */
360 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
361 {
362     bdrv_dirty_bitmaps_lock(bs);
363     bdrv_release_dirty_bitmap_locked(bitmap);
364     bdrv_dirty_bitmaps_unlock(bs);
365 }
366 
367 /**
368  * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
369  * There must not be any frozen bitmaps attached.
370  * This function does not remove persistent bitmaps from the storage.
371  * Called with BQL taken.
372  */
373 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
374 {
375     BdrvDirtyBitmap *bm, *next;
376 
377     bdrv_dirty_bitmaps_lock(bs);
378     QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
379         if (bdrv_dirty_bitmap_name(bm)) {
380             bdrv_release_dirty_bitmap_locked(bm);
381         }
382     }
383     bdrv_dirty_bitmaps_unlock(bs);
384 }
385 
386 /**
387  * Release all persistent dirty bitmaps attached to a BDS (for use in
388  * bdrv_inactivate_recurse()).
389  * There must not be any frozen bitmaps attached.
390  * This function does not remove persistent bitmaps from the storage.
391  * Called with BQL taken.
392  */
393 void bdrv_release_persistent_dirty_bitmaps(BlockDriverState *bs)
394 {
395     BdrvDirtyBitmap *bm, *next;
396 
397     bdrv_dirty_bitmaps_lock(bs);
398     QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
399         if (bdrv_dirty_bitmap_get_persistance(bm)) {
400             bdrv_release_dirty_bitmap_locked(bm);
401         }
402     }
403     bdrv_dirty_bitmaps_unlock(bs);
404 }
405 
406 /**
407  * Remove persistent dirty bitmap from the storage if it exists.
408  * Absence of bitmap is not an error, because we have the following scenario:
409  * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
410  * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
411  * not fail.
412  * This function doesn't release corresponding BdrvDirtyBitmap.
413  */
414 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
415                                          const char *name,
416                                          Error **errp)
417 {
418     if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
419         bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
420     }
421 }
422 
423 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
424 {
425     bdrv_dirty_bitmap_lock(bitmap);
426     assert(!bdrv_dirty_bitmap_frozen(bitmap));
427     bitmap->disabled = true;
428     bdrv_dirty_bitmap_unlock(bitmap);
429 }
430 
431 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
432 {
433     bdrv_dirty_bitmap_lock(bitmap);
434     bdrv_enable_dirty_bitmap_locked(bitmap);
435     bdrv_dirty_bitmap_unlock(bitmap);
436 }
437 
438 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
439 {
440     BdrvDirtyBitmap *bm;
441     BlockDirtyInfoList *list = NULL;
442     BlockDirtyInfoList **plist = &list;
443 
444     bdrv_dirty_bitmaps_lock(bs);
445     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
446         BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
447         BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
448         info->count = bdrv_get_dirty_count(bm);
449         info->granularity = bdrv_dirty_bitmap_granularity(bm);
450         info->has_name = !!bm->name;
451         info->name = g_strdup(bm->name);
452         info->status = bdrv_dirty_bitmap_status(bm);
453         entry->value = info;
454         *plist = entry;
455         plist = &entry->next;
456     }
457     bdrv_dirty_bitmaps_unlock(bs);
458 
459     return list;
460 }
461 
462 /* Called within bdrv_dirty_bitmap_lock..unlock */
463 bool bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
464                            int64_t offset)
465 {
466     if (bitmap) {
467         return hbitmap_get(bitmap->bitmap, offset);
468     } else {
469         return false;
470     }
471 }
472 
473 /**
474  * Chooses a default granularity based on the existing cluster size,
475  * but clamped between [4K, 64K]. Defaults to 64K in the case that there
476  * is no cluster size information available.
477  */
478 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
479 {
480     BlockDriverInfo bdi;
481     uint32_t granularity;
482 
483     if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
484         granularity = MAX(4096, bdi.cluster_size);
485         granularity = MIN(65536, granularity);
486     } else {
487         granularity = 65536;
488     }
489 
490     return granularity;
491 }
492 
493 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
494 {
495     return 1U << hbitmap_granularity(bitmap->bitmap);
496 }
497 
498 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
499 {
500     BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
501     hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
502     iter->bitmap = bitmap;
503     bitmap->active_iterators++;
504     return iter;
505 }
506 
507 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
508 {
509     BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
510     hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
511     iter->bitmap = bitmap;
512     bitmap->active_iterators++;
513     return iter;
514 }
515 
516 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
517 {
518     if (!iter) {
519         return;
520     }
521     assert(iter->bitmap->active_iterators > 0);
522     iter->bitmap->active_iterators--;
523     g_free(iter);
524 }
525 
526 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
527 {
528     return hbitmap_iter_next(&iter->hbi, true);
529 }
530 
531 /**
532  * Return the next consecutively dirty area in the dirty bitmap
533  * belonging to the given iterator @iter.
534  *
535  * @max_offset: Maximum value that may be returned for
536  *              *offset + *bytes
537  * @offset:     Will contain the start offset of the next dirty area
538  * @bytes:      Will contain the length of the next dirty area
539  *
540  * Returns: True if a dirty area could be found before max_offset
541  *          (which means that *offset and *bytes then contain valid
542  *          values), false otherwise.
543  *
544  * Note that @iter is never advanced if false is returned.  If an area
545  * is found (which means that true is returned), it will be advanced
546  * past that area.
547  */
548 bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
549                                uint64_t *offset, int *bytes)
550 {
551     uint32_t granularity = bdrv_dirty_bitmap_granularity(iter->bitmap);
552     uint64_t gran_max_offset;
553     int64_t ret;
554     int size;
555 
556     if (max_offset == iter->bitmap->size) {
557         /* If max_offset points to the image end, round it up by the
558          * bitmap granularity */
559         gran_max_offset = ROUND_UP(max_offset, granularity);
560     } else {
561         gran_max_offset = max_offset;
562     }
563 
564     ret = hbitmap_iter_next(&iter->hbi, false);
565     if (ret < 0 || ret + granularity > gran_max_offset) {
566         return false;
567     }
568 
569     *offset = ret;
570     size = 0;
571 
572     assert(granularity <= INT_MAX);
573 
574     do {
575         /* Advance iterator */
576         ret = hbitmap_iter_next(&iter->hbi, true);
577         size += granularity;
578     } while (ret + granularity <= gran_max_offset &&
579              hbitmap_iter_next(&iter->hbi, false) == ret + granularity &&
580              size <= INT_MAX - granularity);
581 
582     *bytes = MIN(size, max_offset - *offset);
583     return true;
584 }
585 
586 /* Called within bdrv_dirty_bitmap_lock..unlock */
587 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
588                                   int64_t offset, int64_t bytes)
589 {
590     assert(bdrv_dirty_bitmap_enabled(bitmap));
591     assert(!bdrv_dirty_bitmap_readonly(bitmap));
592     hbitmap_set(bitmap->bitmap, offset, bytes);
593 }
594 
595 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
596                            int64_t offset, int64_t bytes)
597 {
598     bdrv_dirty_bitmap_lock(bitmap);
599     bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
600     bdrv_dirty_bitmap_unlock(bitmap);
601 }
602 
603 /* Called within bdrv_dirty_bitmap_lock..unlock */
604 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
605                                     int64_t offset, int64_t bytes)
606 {
607     assert(bdrv_dirty_bitmap_enabled(bitmap));
608     assert(!bdrv_dirty_bitmap_readonly(bitmap));
609     hbitmap_reset(bitmap->bitmap, offset, bytes);
610 }
611 
612 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
613                              int64_t offset, int64_t bytes)
614 {
615     bdrv_dirty_bitmap_lock(bitmap);
616     bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
617     bdrv_dirty_bitmap_unlock(bitmap);
618 }
619 
620 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
621 {
622     assert(bdrv_dirty_bitmap_enabled(bitmap));
623     assert(!bdrv_dirty_bitmap_readonly(bitmap));
624     bdrv_dirty_bitmap_lock(bitmap);
625     if (!out) {
626         hbitmap_reset_all(bitmap->bitmap);
627     } else {
628         HBitmap *backup = bitmap->bitmap;
629         bitmap->bitmap = hbitmap_alloc(bitmap->size,
630                                        hbitmap_granularity(backup));
631         *out = backup;
632     }
633     bdrv_dirty_bitmap_unlock(bitmap);
634 }
635 
636 void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
637 {
638     HBitmap *tmp = bitmap->bitmap;
639     assert(bdrv_dirty_bitmap_enabled(bitmap));
640     assert(!bdrv_dirty_bitmap_readonly(bitmap));
641     bitmap->bitmap = in;
642     hbitmap_free(tmp);
643 }
644 
645 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
646                                               uint64_t offset, uint64_t bytes)
647 {
648     return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
649 }
650 
651 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
652 {
653     return hbitmap_serialization_align(bitmap->bitmap);
654 }
655 
656 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
657                                       uint8_t *buf, uint64_t offset,
658                                       uint64_t bytes)
659 {
660     hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
661 }
662 
663 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
664                                         uint8_t *buf, uint64_t offset,
665                                         uint64_t bytes, bool finish)
666 {
667     hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
668 }
669 
670 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
671                                           uint64_t offset, uint64_t bytes,
672                                           bool finish)
673 {
674     hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
675 }
676 
677 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
678                                         uint64_t offset, uint64_t bytes,
679                                         bool finish)
680 {
681     hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
682 }
683 
684 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
685 {
686     hbitmap_deserialize_finish(bitmap->bitmap);
687 }
688 
689 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
690 {
691     BdrvDirtyBitmap *bitmap;
692 
693     if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
694         return;
695     }
696 
697     bdrv_dirty_bitmaps_lock(bs);
698     QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
699         if (!bdrv_dirty_bitmap_enabled(bitmap)) {
700             continue;
701         }
702         assert(!bdrv_dirty_bitmap_readonly(bitmap));
703         hbitmap_set(bitmap->bitmap, offset, bytes);
704     }
705     bdrv_dirty_bitmaps_unlock(bs);
706 }
707 
708 /**
709  * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
710  */
711 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
712 {
713     hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
714 }
715 
716 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
717 {
718     return hbitmap_count(bitmap->bitmap);
719 }
720 
721 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
722 {
723     return hbitmap_count(bitmap->meta);
724 }
725 
726 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
727 {
728     return bitmap->readonly;
729 }
730 
731 /* Called with BQL taken. */
732 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
733 {
734     qemu_mutex_lock(bitmap->mutex);
735     bitmap->readonly = value;
736     qemu_mutex_unlock(bitmap->mutex);
737 }
738 
739 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
740 {
741     BdrvDirtyBitmap *bm;
742     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
743         if (bm->readonly) {
744             return true;
745         }
746     }
747 
748     return false;
749 }
750 
751 /* Called with BQL taken. */
752 void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap *bitmap, bool persistent)
753 {
754     qemu_mutex_lock(bitmap->mutex);
755     bitmap->persistent = persistent;
756     qemu_mutex_unlock(bitmap->mutex);
757 }
758 
759 bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap *bitmap)
760 {
761     return bitmap->persistent;
762 }
763 
764 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
765 {
766     BdrvDirtyBitmap *bm;
767     QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
768         if (bm->persistent && !bm->readonly) {
769             return true;
770         }
771     }
772 
773     return false;
774 }
775 
776 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
777                                         BdrvDirtyBitmap *bitmap)
778 {
779     return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
780                             QLIST_NEXT(bitmap, list);
781 }
782 
783 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
784 {
785     return hbitmap_sha256(bitmap->bitmap, errp);
786 }
787 
788 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset)
789 {
790     return hbitmap_next_zero(bitmap->bitmap, offset);
791 }
792 
793 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
794                              Error **errp)
795 {
796     /* only bitmaps from one bds are supported */
797     assert(dest->mutex == src->mutex);
798 
799     qemu_mutex_lock(dest->mutex);
800 
801     assert(bdrv_dirty_bitmap_enabled(dest));
802     assert(!bdrv_dirty_bitmap_readonly(dest));
803 
804     if (!hbitmap_merge(dest->bitmap, src->bitmap)) {
805         error_setg(errp, "Bitmaps are incompatible and can't be merged");
806     }
807 
808     qemu_mutex_unlock(dest->mutex);
809 }
810