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