]> git.proxmox.com Git - mirror_qemu.git/blame - block/dirty-bitmap.c
Merge remote-tracking branch 'remotes/berrange/tags/socket-next-pull-request' into...
[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 */
ebab2259
FZ
43 BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */
44 char *name; /* Optional non-empty unique ID */
993e6525 45 int64_t size; /* Size of the bitmap, in bytes */
8bfc932e
VSO
46 bool disabled; /* Bitmap is disabled. It ignores all writes to
47 the device */
dc162c8e 48 int active_iterators; /* How many iterators are active */
d6883bc9
VSO
49 bool readonly; /* Bitmap is read-only. This field also
50 prevents the respective image from being
51 modified (i.e. blocks writes and discards).
52 Such operations must fail and both the image
53 and this bitmap must remain unchanged while
54 this flag is set. */
a88b179f 55 bool persistent; /* bitmap must be saved to owner disk image */
ebab2259
FZ
56 QLIST_ENTRY(BdrvDirtyBitmap) list;
57};
58
dc162c8e
FZ
59struct BdrvDirtyBitmapIter {
60 HBitmapIter hbi;
61 BdrvDirtyBitmap *bitmap;
62};
63
2119882c
PB
64static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
65{
66 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
67}
68
69static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
70{
71 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
72}
73
b64bd51e
PB
74void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
75{
76 qemu_mutex_lock(bitmap->mutex);
77}
78
79void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
80{
81 qemu_mutex_unlock(bitmap->mutex);
82}
83
2119882c 84/* Called with BQL or dirty_bitmap lock taken. */
ebab2259
FZ
85BdrvDirtyBitmap *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
2119882c 98/* Called with BQL taken. */
ebab2259
FZ
99void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap)
100{
101 assert(!bdrv_dirty_bitmap_frozen(bitmap));
102 g_free(bitmap->name);
103 bitmap->name = NULL;
a88b179f 104 bitmap->persistent = false;
ebab2259
FZ
105}
106
2119882c 107/* Called with BQL taken. */
ebab2259
FZ
108BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
109 uint32_t granularity,
110 const char *name,
111 Error **errp)
112{
113 int64_t bitmap_size;
114 BdrvDirtyBitmap *bitmap;
ebab2259 115
993e6525 116 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
ebab2259
FZ
117
118 if (name && bdrv_find_dirty_bitmap(bs, name)) {
119 error_setg(errp, "Bitmap already exists: %s", name);
120 return NULL;
121 }
993e6525 122 bitmap_size = bdrv_getlength(bs);
ebab2259
FZ
123 if (bitmap_size < 0) {
124 error_setg_errno(errp, -bitmap_size, "could not get length of device");
125 errno = -bitmap_size;
126 return NULL;
127 }
128 bitmap = g_new0(BdrvDirtyBitmap, 1);
b64bd51e 129 bitmap->mutex = &bs->dirty_bitmap_mutex;
ca759622 130 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
ebab2259
FZ
131 bitmap->size = bitmap_size;
132 bitmap->name = g_strdup(name);
133 bitmap->disabled = false;
2119882c 134 bdrv_dirty_bitmaps_lock(bs);
ebab2259 135 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
2119882c 136 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
137 return bitmap;
138}
139
fb933437
FZ
140/* bdrv_create_meta_dirty_bitmap
141 *
142 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
143 * when a dirty status bit in @bitmap is changed (either from reset to set or
144 * the other way around), its respective meta dirty bitmap bit will be marked
145 * dirty as well.
146 *
147 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
148 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
149 * track.
150 */
151void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
152 int chunk_size)
153{
154 assert(!bitmap->meta);
b64bd51e 155 qemu_mutex_lock(bitmap->mutex);
fb933437
FZ
156 bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
157 chunk_size * BITS_PER_BYTE);
b64bd51e 158 qemu_mutex_unlock(bitmap->mutex);
fb933437
FZ
159}
160
161void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
162{
163 assert(bitmap->meta);
b64bd51e 164 qemu_mutex_lock(bitmap->mutex);
fb933437
FZ
165 hbitmap_free_meta(bitmap->bitmap);
166 bitmap->meta = NULL;
b64bd51e 167 qemu_mutex_unlock(bitmap->mutex);
fb933437
FZ
168}
169
15891fac
FZ
170int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
171{
993e6525 172 return bitmap->size;
15891fac
FZ
173}
174
175const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
176{
177 return bitmap->name;
178}
179
2119882c 180/* Called with BQL taken. */
ebab2259
FZ
181bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
182{
183 return bitmap->successor;
184}
185
2119882c 186/* Called with BQL taken. */
ebab2259
FZ
187bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
188{
189 return !(bitmap->disabled || bitmap->successor);
190}
191
2119882c 192/* Called with BQL taken. */
ebab2259
FZ
193DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
194{
195 if (bdrv_dirty_bitmap_frozen(bitmap)) {
196 return DIRTY_BITMAP_STATUS_FROZEN;
197 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
198 return DIRTY_BITMAP_STATUS_DISABLED;
199 } else {
200 return DIRTY_BITMAP_STATUS_ACTIVE;
201 }
202}
203
204/**
205 * Create a successor bitmap destined to replace this bitmap after an operation.
206 * Requires that the bitmap is not frozen and has no successor.
2119882c 207 * Called with BQL taken.
ebab2259
FZ
208 */
209int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
210 BdrvDirtyBitmap *bitmap, Error **errp)
211{
212 uint64_t granularity;
213 BdrvDirtyBitmap *child;
214
215 if (bdrv_dirty_bitmap_frozen(bitmap)) {
216 error_setg(errp, "Cannot create a successor for a bitmap that is "
217 "currently frozen");
218 return -1;
219 }
220 assert(!bitmap->successor);
221
222 /* Create an anonymous successor */
223 granularity = bdrv_dirty_bitmap_granularity(bitmap);
224 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
225 if (!child) {
226 return -1;
227 }
228
229 /* Successor will be on or off based on our current state. */
230 child->disabled = bitmap->disabled;
231
232 /* Install the successor and freeze the parent */
233 bitmap->successor = child;
234 return 0;
235}
236
237/**
238 * For a bitmap with a successor, yield our name to the successor,
239 * delete the old bitmap, and return a handle to the new bitmap.
2119882c 240 * Called with BQL taken.
ebab2259
FZ
241 */
242BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
243 BdrvDirtyBitmap *bitmap,
244 Error **errp)
245{
246 char *name;
247 BdrvDirtyBitmap *successor = bitmap->successor;
248
249 if (successor == NULL) {
250 error_setg(errp, "Cannot relinquish control if "
251 "there's no successor present");
252 return NULL;
253 }
254
255 name = bitmap->name;
256 bitmap->name = NULL;
257 successor->name = name;
258 bitmap->successor = NULL;
a88b179f
VSO
259 successor->persistent = bitmap->persistent;
260 bitmap->persistent = false;
ebab2259
FZ
261 bdrv_release_dirty_bitmap(bs, bitmap);
262
263 return successor;
264}
265
266/**
267 * In cases of failure where we can no longer safely delete the parent,
268 * we may wish to re-join the parent and child/successor.
269 * The merged parent will be un-frozen, but not explicitly re-enabled.
2119882c 270 * Called with BQL taken.
ebab2259
FZ
271 */
272BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
273 BdrvDirtyBitmap *parent,
274 Error **errp)
275{
276 BdrvDirtyBitmap *successor = parent->successor;
277
278 if (!successor) {
279 error_setg(errp, "Cannot reclaim a successor when none is present");
280 return NULL;
281 }
282
283 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
284 error_setg(errp, "Merging of parent and successor bitmap failed");
285 return NULL;
286 }
287 bdrv_release_dirty_bitmap(bs, successor);
288 parent->successor = NULL;
289
290 return parent;
291}
292
293/**
294 * Truncates _all_ bitmaps attached to a BDS.
2119882c 295 * Called with BQL taken.
ebab2259 296 */
1b6cc579 297void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
ebab2259
FZ
298{
299 BdrvDirtyBitmap *bitmap;
ebab2259 300
2119882c 301 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
302 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
303 assert(!bdrv_dirty_bitmap_frozen(bitmap));
dc162c8e 304 assert(!bitmap->active_iterators);
ca759622 305 hbitmap_truncate(bitmap->bitmap, bytes);
993e6525 306 bitmap->size = bytes;
ebab2259 307 }
2119882c 308 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
309}
310
615b5dcf
VSO
311static bool bdrv_dirty_bitmap_has_name(BdrvDirtyBitmap *bitmap)
312{
313 return !!bdrv_dirty_bitmap_name(bitmap);
314}
315
2119882c 316/* Called with BQL taken. */
615b5dcf
VSO
317static void bdrv_do_release_matching_dirty_bitmap(
318 BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
319 bool (*cond)(BdrvDirtyBitmap *bitmap))
ebab2259
FZ
320{
321 BdrvDirtyBitmap *bm, *next;
2119882c 322 bdrv_dirty_bitmaps_lock(bs);
ebab2259 323 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
615b5dcf 324 if ((!bitmap || bm == bitmap) && (!cond || cond(bm))) {
dc162c8e 325 assert(!bm->active_iterators);
ebab2259 326 assert(!bdrv_dirty_bitmap_frozen(bm));
fb933437 327 assert(!bm->meta);
ebab2259
FZ
328 QLIST_REMOVE(bm, list);
329 hbitmap_free(bm->bitmap);
330 g_free(bm->name);
331 g_free(bm);
332
333 if (bitmap) {
2119882c 334 goto out;
ebab2259
FZ
335 }
336 }
337 }
7105007a
FZ
338 if (bitmap) {
339 abort();
340 }
2119882c
PB
341
342out:
343 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
344}
345
2119882c 346/* Called with BQL taken. */
ebab2259
FZ
347void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
348{
615b5dcf 349 bdrv_do_release_matching_dirty_bitmap(bs, bitmap, NULL);
ebab2259
FZ
350}
351
352/**
353 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
354 * There must not be any frozen bitmaps attached.
56f364e6 355 * This function does not remove persistent bitmaps from the storage.
2119882c 356 * Called with BQL taken.
ebab2259
FZ
357 */
358void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
359{
615b5dcf
VSO
360 bdrv_do_release_matching_dirty_bitmap(bs, NULL, bdrv_dirty_bitmap_has_name);
361}
362
363/**
364 * Release all persistent dirty bitmaps attached to a BDS (for use in
365 * bdrv_inactivate_recurse()).
366 * There must not be any frozen bitmaps attached.
367 * This function does not remove persistent bitmaps from the storage.
368 */
369void bdrv_release_persistent_dirty_bitmaps(BlockDriverState *bs)
370{
371 bdrv_do_release_matching_dirty_bitmap(bs, NULL,
372 bdrv_dirty_bitmap_get_persistance);
ebab2259
FZ
373}
374
56f364e6
VSO
375/**
376 * Remove persistent dirty bitmap from the storage if it exists.
377 * Absence of bitmap is not an error, because we have the following scenario:
378 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
379 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
380 * not fail.
381 * This function doesn't release corresponding BdrvDirtyBitmap.
382 */
383void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
384 const char *name,
385 Error **errp)
386{
387 if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
388 bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
389 }
390}
391
2119882c 392/* Called with BQL taken. */
ebab2259
FZ
393void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
394{
395 assert(!bdrv_dirty_bitmap_frozen(bitmap));
396 bitmap->disabled = true;
397}
398
2119882c 399/* Called with BQL taken. */
ebab2259
FZ
400void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
401{
402 assert(!bdrv_dirty_bitmap_frozen(bitmap));
403 bitmap->disabled = false;
404}
405
406BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
407{
408 BdrvDirtyBitmap *bm;
409 BlockDirtyInfoList *list = NULL;
410 BlockDirtyInfoList **plist = &list;
411
2119882c 412 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
413 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
414 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
415 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
9a46dba7 416 info->count = bdrv_get_dirty_count(bm);
ebab2259
FZ
417 info->granularity = bdrv_dirty_bitmap_granularity(bm);
418 info->has_name = !!bm->name;
419 info->name = g_strdup(bm->name);
420 info->status = bdrv_dirty_bitmap_status(bm);
421 entry->value = info;
422 *plist = entry;
423 plist = &entry->next;
424 }
2119882c 425 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
426
427 return list;
428}
429
b64bd51e 430/* Called within bdrv_dirty_bitmap_lock..unlock */
3b5d4df0
EB
431bool bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
432 int64_t offset)
ebab2259
FZ
433{
434 if (bitmap) {
ca759622 435 return hbitmap_get(bitmap->bitmap, offset);
ebab2259 436 } else {
3b5d4df0 437 return false;
ebab2259
FZ
438 }
439}
440
441/**
442 * Chooses a default granularity based on the existing cluster size,
443 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
444 * is no cluster size information available.
445 */
446uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
447{
448 BlockDriverInfo bdi;
449 uint32_t granularity;
450
451 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
452 granularity = MAX(4096, bdi.cluster_size);
453 granularity = MIN(65536, granularity);
454 } else {
455 granularity = 65536;
456 }
457
458 return granularity;
459}
460
ba06ff1a 461uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
ebab2259 462{
ca759622 463 return 1U << hbitmap_granularity(bitmap->bitmap);
ebab2259
FZ
464}
465
715a74d8 466BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
dc162c8e
FZ
467{
468 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
715a74d8 469 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
dc162c8e
FZ
470 iter->bitmap = bitmap;
471 bitmap->active_iterators++;
472 return iter;
473}
474
6d3f4049
FZ
475BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
476{
477 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
478 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
479 iter->bitmap = bitmap;
480 bitmap->active_iterators++;
481 return iter;
482}
483
dc162c8e
FZ
484void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
485{
486 if (!iter) {
487 return;
488 }
489 assert(iter->bitmap->active_iterators > 0);
490 iter->bitmap->active_iterators--;
491 g_free(iter);
492}
493
494int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
ebab2259 495{
ca759622 496 return hbitmap_iter_next(&iter->hbi);
ebab2259
FZ
497}
498
b64bd51e
PB
499/* Called within bdrv_dirty_bitmap_lock..unlock */
500void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 501 int64_t offset, int64_t bytes)
b64bd51e
PB
502{
503 assert(bdrv_dirty_bitmap_enabled(bitmap));
d6883bc9 504 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 505 hbitmap_set(bitmap->bitmap, offset, bytes);
b64bd51e
PB
506}
507
ebab2259 508void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 509 int64_t offset, int64_t bytes)
b64bd51e
PB
510{
511 bdrv_dirty_bitmap_lock(bitmap);
e0d7f73e 512 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
b64bd51e
PB
513 bdrv_dirty_bitmap_unlock(bitmap);
514}
515
516/* Called within bdrv_dirty_bitmap_lock..unlock */
517void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 518 int64_t offset, int64_t bytes)
ebab2259
FZ
519{
520 assert(bdrv_dirty_bitmap_enabled(bitmap));
d6883bc9 521 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 522 hbitmap_reset(bitmap->bitmap, offset, bytes);
ebab2259
FZ
523}
524
525void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 526 int64_t offset, int64_t bytes)
ebab2259 527{
b64bd51e 528 bdrv_dirty_bitmap_lock(bitmap);
e0d7f73e 529 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
b64bd51e 530 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
531}
532
533void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
534{
535 assert(bdrv_dirty_bitmap_enabled(bitmap));
d6883bc9 536 assert(!bdrv_dirty_bitmap_readonly(bitmap));
b64bd51e 537 bdrv_dirty_bitmap_lock(bitmap);
ebab2259
FZ
538 if (!out) {
539 hbitmap_reset_all(bitmap->bitmap);
540 } else {
541 HBitmap *backup = bitmap->bitmap;
ca759622 542 bitmap->bitmap = hbitmap_alloc(bitmap->size,
ebab2259
FZ
543 hbitmap_granularity(backup));
544 *out = backup;
545 }
b64bd51e 546 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
547}
548
549void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
550{
551 HBitmap *tmp = bitmap->bitmap;
552 assert(bdrv_dirty_bitmap_enabled(bitmap));
d6883bc9 553 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ebab2259
FZ
554 bitmap->bitmap = in;
555 hbitmap_free(tmp);
556}
557
882c36f5 558uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
86f6ae67 559 uint64_t offset, uint64_t bytes)
882c36f5 560{
ca759622 561 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
882c36f5
VSO
562}
563
564uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
565{
ca759622 566 return hbitmap_serialization_align(bitmap->bitmap);
882c36f5
VSO
567}
568
569void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
86f6ae67
EB
570 uint8_t *buf, uint64_t offset,
571 uint64_t bytes)
882c36f5 572{
ca759622 573 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
882c36f5
VSO
574}
575
576void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
86f6ae67
EB
577 uint8_t *buf, uint64_t offset,
578 uint64_t bytes, bool finish)
882c36f5 579{
ca759622 580 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
882c36f5
VSO
581}
582
583void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
86f6ae67 584 uint64_t offset, uint64_t bytes,
882c36f5
VSO
585 bool finish)
586{
ca759622 587 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
6bdc8b71
VSO
588}
589
590void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
86f6ae67 591 uint64_t offset, uint64_t bytes,
6bdc8b71
VSO
592 bool finish)
593{
ca759622 594 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
882c36f5
VSO
595}
596
597void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
598{
599 hbitmap_deserialize_finish(bitmap->bitmap);
600}
601
0fdf1a4f 602void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
ebab2259
FZ
603{
604 BdrvDirtyBitmap *bitmap;
2119882c
PB
605
606 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
607 return;
608 }
609
610 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
611 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
612 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
613 continue;
614 }
d6883bc9 615 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 616 hbitmap_set(bitmap->bitmap, offset, bytes);
ebab2259 617 }
2119882c 618 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
619}
620
621/**
dc162c8e 622 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
ebab2259 623 */
715a74d8 624void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
ebab2259 625{
ca759622 626 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
ebab2259
FZ
627}
628
629int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
630{
ca759622 631 return hbitmap_count(bitmap->bitmap);
ebab2259 632}
6d3f4049
FZ
633
634int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
635{
636 return hbitmap_count(bitmap->meta);
637}
d6883bc9
VSO
638
639bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
640{
641 return bitmap->readonly;
642}
643
644/* Called with BQL taken. */
645void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
646{
647 qemu_mutex_lock(bitmap->mutex);
648 bitmap->readonly = value;
649 qemu_mutex_unlock(bitmap->mutex);
650}
651
652bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
653{
654 BdrvDirtyBitmap *bm;
655 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
656 if (bm->readonly) {
657 return true;
658 }
659 }
660
661 return false;
662}
a0319aac 663
a88b179f
VSO
664/* Called with BQL taken. */
665void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap *bitmap, bool persistent)
666{
667 qemu_mutex_lock(bitmap->mutex);
668 bitmap->persistent = persistent;
669 qemu_mutex_unlock(bitmap->mutex);
670}
671
672bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap *bitmap)
673{
674 return bitmap->persistent;
675}
676
677bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
678{
679 BdrvDirtyBitmap *bm;
680 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
681 if (bm->persistent && !bm->readonly) {
682 return true;
683 }
684 }
685
686 return false;
687}
3dd10a06
VSO
688
689BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
690 BdrvDirtyBitmap *bitmap)
691{
692 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
693 QLIST_NEXT(bitmap, list);
694}
a3b52535
VSO
695
696char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
697{
698 return hbitmap_sha256(bitmap->bitmap, errp);
699}
56207df5
VSO
700
701int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset)
702{
703 return hbitmap_next_zero(bitmap->bitmap, offset);
704}