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