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