]> git.proxmox.com Git - mirror_qemu.git/blame - block/dirty-bitmap.c
vhdx: Take locks for accessing bs->file
[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 26#include "trace.h"
e2c1c34f 27#include "block/block-io.h"
ebab2259
FZ
28#include "block/block_int.h"
29#include "block/blockjob.h"
e2c1c34f 30#include "block/dirty-bitmap.h"
d2c3080e 31#include "qemu/main-loop.h"
ebab2259 32
ebab2259 33struct BdrvDirtyBitmap {
5deb6cbd 34 BlockDriverState *bs;
ca759622 35 HBitmap *bitmap; /* Dirty bitmap implementation */
27a1b301 36 bool busy; /* Bitmap is busy, it can't be used via QMP */
21d2376f 37 BdrvDirtyBitmap *successor; /* Anonymous child, if any. */
ebab2259 38 char *name; /* Optional non-empty unique ID */
993e6525 39 int64_t size; /* Size of the bitmap, in bytes */
8bfc932e
VSO
40 bool disabled; /* Bitmap is disabled. It ignores all writes to
41 the device */
dc162c8e 42 int active_iterators; /* How many iterators are active */
d6883bc9
VSO
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. */
a88b179f 49 bool persistent; /* bitmap must be saved to owner disk image */
b0f45559
JS
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. */
c4e4b0fa
JS
53 bool skip_store; /* We are either migrating or deleting this
54 * bitmap; it should not be stored on the next
55 * inactivation. */
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{
1e638301 76 bdrv_dirty_bitmaps_lock(bitmap->bs);
b64bd51e
PB
77}
78
79void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
80{
1e638301 81 bdrv_dirty_bitmaps_unlock(bitmap->bs);
b64bd51e
PB
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
99BdrvDirtyBitmap *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;
ebab2259 106
993e6525 107 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
ebab2259 108
cf7c49cf
EB
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 }
ebab2259 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);
5deb6cbd 126 bitmap->bs = bs;
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
15891fac
FZ
137int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
138{
993e6525 139 return bitmap->size;
15891fac
FZ
140}
141
142const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
143{
144 return bitmap->name;
145}
146
2119882c 147/* Called with BQL taken. */
50a47257 148bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
ebab2259
FZ
149{
150 return bitmap->successor;
151}
152
3ae96d66 153static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
27a1b301
JS
154{
155 return bitmap->busy;
993edc0c
JS
156}
157
27a1b301 158void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
4f43e953 159{
1e638301 160 bdrv_dirty_bitmaps_lock(bitmap->bs);
27a1b301 161 bitmap->busy = busy;
1e638301 162 bdrv_dirty_bitmaps_unlock(bitmap->bs);
4f43e953
VSO
163}
164
2119882c 165/* Called with BQL taken. */
ebab2259
FZ
166bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
167{
8b2e20f6 168 return !bitmap->disabled;
ebab2259
FZ
169}
170
4db6ceb0
JS
171/* Called with BQL taken. */
172static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
173{
174 return !bitmap->disabled || (bitmap->successor &&
175 !bitmap->successor->disabled);
176}
177
3ae96d66
JS
178int 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"
94075c28 198 " this bitmap from disk\n");
3ae96d66
JS
199 return -1;
200 }
201
202 return 0;
203}
204
ebab2259
FZ
205/**
206 * Create a successor bitmap destined to replace this bitmap after an operation.
27a1b301 207 * Requires that the bitmap is not marked busy and has no successor.
8b2e20f6 208 * The successor will be enabled if the parent bitmap was.
2119882c 209 * Called with BQL taken.
ebab2259 210 */
5deb6cbd 211int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp)
ebab2259
FZ
212{
213 uint64_t granularity;
214 BdrvDirtyBitmap *child;
215
3ae96d66 216 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
50a47257
JS
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");
ebab2259
FZ
222 return -1;
223 }
ebab2259
FZ
224
225 /* Create an anonymous successor */
226 granularity = bdrv_dirty_bitmap_granularity(bitmap);
5deb6cbd 227 child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp);
ebab2259
FZ
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;
8b2e20f6 234 bitmap->disabled = true;
ebab2259 235
27a1b301 236 /* Install the successor and mark the parent as busy */
ebab2259 237 bitmap->successor = child;
27a1b301 238 bitmap->busy = true;
ebab2259
FZ
239 return 0;
240}
241
92bcea40
VSO
242void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
243{
92bcea40
VSO
244 bitmap->disabled = false;
245}
246
e73a265e
VSO
247/* Called with BQL taken. */
248void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
249{
1e638301
VSO
250 assert(bitmap->bs == bitmap->successor->bs);
251 bdrv_dirty_bitmaps_lock(bitmap->bs);
58f72b96 252 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
1e638301 253 bdrv_dirty_bitmaps_unlock(bitmap->bs);
e73a265e
VSO
254}
255
b133c27f
PB
256/* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
257static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
604ab74b 258{
b133c27f 259 assert(!bitmap->active_iterators);
27a1b301 260 assert(!bdrv_dirty_bitmap_busy(bitmap));
50a47257 261 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
b133c27f
PB
262 QLIST_REMOVE(bitmap, list);
263 hbitmap_free(bitmap->bitmap);
264 g_free(bitmap->name);
265 g_free(bitmap);
604ab74b
VSO
266}
267
ebab2259
FZ
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.
2119882c 271 * Called with BQL taken.
ebab2259 272 */
5deb6cbd 273BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap,
ebab2259
FZ
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;
a88b179f
VSO
289 successor->persistent = bitmap->persistent;
290 bitmap->persistent = false;
27a1b301 291 bitmap->busy = false;
5deb6cbd 292 bdrv_release_dirty_bitmap(bitmap);
ebab2259
FZ
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.
27a1b301 300 * The merged parent will be marked as not busy.
8b2e20f6 301 * The marged parent will be enabled if and only if the successor was enabled.
044ee8e1 302 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
ebab2259 303 */
5deb6cbd 304BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent,
044ee8e1 305 Error **errp)
ebab2259 306{
044ee8e1 307 BdrvDirtyBitmap *successor = parent->successor;
ebab2259
FZ
308
309 if (!successor) {
310 error_setg(errp, "Cannot reclaim a successor when none is present");
311 return NULL;
312 }
313
618af89e 314 hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap);
8b2e20f6
JS
315
316 parent->disabled = successor->disabled;
27a1b301 317 parent->busy = false;
b133c27f 318 bdrv_release_dirty_bitmap_locked(successor);
ebab2259
FZ
319 parent->successor = NULL;
320
044ee8e1
VSO
321 return parent;
322}
323
324/* Called with BQL taken. */
5deb6cbd 325BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent,
044ee8e1
VSO
326 Error **errp)
327{
328 BdrvDirtyBitmap *ret;
329
1e638301 330 bdrv_dirty_bitmaps_lock(parent->bs);
5deb6cbd 331 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp);
1e638301 332 bdrv_dirty_bitmaps_unlock(parent->bs);
604ab74b 333
044ee8e1 334 return ret;
ebab2259
FZ
335}
336
337/**
338 * Truncates _all_ bitmaps attached to a BDS.
2119882c 339 * Called with BQL taken.
ebab2259 340 */
1b6cc579 341void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
ebab2259
FZ
342{
343 BdrvDirtyBitmap *bitmap;
ebab2259 344
2119882c 345 bdrv_dirty_bitmaps_lock(bs);
ebab2259 346 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
27a1b301 347 assert(!bdrv_dirty_bitmap_busy(bitmap));
50a47257 348 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
dc162c8e 349 assert(!bitmap->active_iterators);
ca759622 350 hbitmap_truncate(bitmap->bitmap, bytes);
993e6525 351 bitmap->size = bytes;
ebab2259 352 }
2119882c 353 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
354}
355
2119882c 356/* Called with BQL taken. */
5deb6cbd 357void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap)
ebab2259 358{
5deb6cbd
VSO
359 BlockDriverState *bs = bitmap->bs;
360
b133c27f
PB
361 bdrv_dirty_bitmaps_lock(bs);
362 bdrv_release_dirty_bitmap_locked(bitmap);
363 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
364}
365
366/**
367 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
27a1b301 368 * There must not be any busy bitmaps attached.
56f364e6 369 * This function does not remove persistent bitmaps from the storage.
2119882c 370 * Called with BQL taken.
ebab2259
FZ
371 */
372void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
373{
b133c27f
PB
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);
615b5dcf
VSO
383}
384
56f364e6
VSO
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 */
0508d0be 393int coroutine_fn
d2c3080e
VSO
394bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
395 Error **errp)
56f364e6 396{
167f748d 397 assert_bdrv_graph_readable();
d2c3080e
VSO
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);
56f364e6 400 }
b56a1e31
VSO
401
402 return 0;
56f364e6
VSO
403}
404
ef893b5c
EB
405bool
406bdrv_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
0508d0be 414bool coroutine_fn
d2c3080e
VSO
415bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
416 uint32_t granularity, Error **errp)
85cc8a4f
VSO
417{
418 BlockDriver *drv = bs->drv;
167f748d 419 assert_bdrv_graph_readable();
85cc8a4f
VSO
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
d2c3080e 428 if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
85cc8a4f
VSO
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
d2c3080e
VSO
435 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
436}
437
ebab2259
FZ
438void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
439{
1e638301 440 bdrv_dirty_bitmaps_lock(bitmap->bs);
ebab2259 441 bitmap->disabled = true;
1e638301 442 bdrv_dirty_bitmaps_unlock(bitmap->bs);
ebab2259
FZ
443}
444
445void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
446{
1e638301 447 bdrv_dirty_bitmaps_lock(bitmap->bs);
92bcea40 448 bdrv_enable_dirty_bitmap_locked(bitmap);
1e638301 449 bdrv_dirty_bitmaps_unlock(bitmap->bs);
ebab2259
FZ
450}
451
452BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
453{
454 BdrvDirtyBitmap *bm;
455 BlockDirtyInfoList *list = NULL;
c3033fd3 456 BlockDirtyInfoList **tail = &list;
ebab2259 457
2119882c 458 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
459 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
460 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
c3033fd3 461
9a46dba7 462 info->count = bdrv_get_dirty_count(bm);
ebab2259 463 info->granularity = bdrv_dirty_bitmap_granularity(bm);
ebab2259 464 info->name = g_strdup(bm->name);
4db6ceb0 465 info->recording = bdrv_dirty_bitmap_recording(bm);
27a1b301 466 info->busy = bdrv_dirty_bitmap_busy(bm);
f67cf661 467 info->persistent = bm->persistent;
b0f45559
JS
468 info->has_inconsistent = bm->inconsistent;
469 info->inconsistent = bm->inconsistent;
c3033fd3 470 QAPI_LIST_APPEND(tail, info);
ebab2259 471 }
2119882c 472 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
473
474 return list;
475}
476
b64bd51e 477/* Called within bdrv_dirty_bitmap_lock..unlock */
28636b82 478bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
ebab2259 479{
28636b82
JS
480 return hbitmap_get(bitmap->bitmap, offset);
481}
482
483bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
484{
485 bool ret;
1e638301 486 bdrv_dirty_bitmaps_lock(bitmap->bs);
28636b82 487 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
1e638301 488 bdrv_dirty_bitmaps_unlock(bitmap->bs);
28636b82
JS
489
490 return ret;
ebab2259
FZ
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 */
498uint32_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
ba06ff1a 513uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
ebab2259 514{
ca759622 515 return 1U << hbitmap_granularity(bitmap->bitmap);
ebab2259
FZ
516}
517
715a74d8 518BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
dc162c8e
FZ
519{
520 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
715a74d8 521 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
dc162c8e
FZ
522 iter->bitmap = bitmap;
523 bitmap->active_iterators++;
524 return iter;
525}
526
527void 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
537int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
ebab2259 538{
19c021e1 539 return hbitmap_iter_next(&iter->hbi);
ebab2259
FZ
540}
541
b64bd51e
PB
542/* Called within bdrv_dirty_bitmap_lock..unlock */
543void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 544 int64_t offset, int64_t bytes)
b64bd51e 545{
d6883bc9 546 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 547 hbitmap_set(bitmap->bitmap, offset, bytes);
b64bd51e
PB
548}
549
ebab2259 550void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 551 int64_t offset, int64_t bytes)
b64bd51e 552{
1e638301 553 bdrv_dirty_bitmaps_lock(bitmap->bs);
e0d7f73e 554 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
1e638301 555 bdrv_dirty_bitmaps_unlock(bitmap->bs);
b64bd51e
PB
556}
557
558/* Called within bdrv_dirty_bitmap_lock..unlock */
559void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 560 int64_t offset, int64_t bytes)
ebab2259 561{
d6883bc9 562 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 563 hbitmap_reset(bitmap->bitmap, offset, bytes);
ebab2259
FZ
564}
565
566void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 567 int64_t offset, int64_t bytes)
ebab2259 568{
1e638301 569 bdrv_dirty_bitmaps_lock(bitmap->bs);
e0d7f73e 570 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
1e638301 571 bdrv_dirty_bitmaps_unlock(bitmap->bs);
ebab2259
FZ
572}
573
574void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
575{
967d7905 576 IO_CODE();
d6883bc9 577 assert(!bdrv_dirty_bitmap_readonly(bitmap));
1e638301 578 bdrv_dirty_bitmaps_lock(bitmap->bs);
ebab2259
FZ
579 if (!out) {
580 hbitmap_reset_all(bitmap->bitmap);
581 } else {
582 HBitmap *backup = bitmap->bitmap;
ca759622 583 bitmap->bitmap = hbitmap_alloc(bitmap->size,
ebab2259
FZ
584 hbitmap_granularity(backup));
585 *out = backup;
586 }
1e638301 587 bdrv_dirty_bitmaps_unlock(bitmap->bs);
ebab2259
FZ
588}
589
56bd6624 590void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
ebab2259
FZ
591{
592 HBitmap *tmp = bitmap->bitmap;
d6883bc9 593 assert(!bdrv_dirty_bitmap_readonly(bitmap));
b4ad82aa 594 GLOBAL_STATE_CODE();
56bd6624 595 bitmap->bitmap = backup;
ebab2259
FZ
596 hbitmap_free(tmp);
597}
598
882c36f5 599uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
86f6ae67 600 uint64_t offset, uint64_t bytes)
882c36f5 601{
ca759622 602 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
882c36f5
VSO
603}
604
605uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
606{
ca759622 607 return hbitmap_serialization_align(bitmap->bitmap);
882c36f5
VSO
608}
609
35f428ba
VSO
610/* Return the disk size covered by a chunk of serialized bitmap data. */
611uint64_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
882c36f5 623void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
86f6ae67
EB
624 uint8_t *buf, uint64_t offset,
625 uint64_t bytes)
882c36f5 626{
ca759622 627 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
882c36f5
VSO
628}
629
630void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
86f6ae67
EB
631 uint8_t *buf, uint64_t offset,
632 uint64_t bytes, bool finish)
882c36f5 633{
ca759622 634 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
882c36f5
VSO
635}
636
637void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
86f6ae67 638 uint64_t offset, uint64_t bytes,
882c36f5
VSO
639 bool finish)
640{
ca759622 641 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
6bdc8b71
VSO
642}
643
644void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
86f6ae67 645 uint64_t offset, uint64_t bytes,
6bdc8b71
VSO
646 bool finish)
647{
ca759622 648 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
882c36f5
VSO
649}
650
651void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
652{
653 hbitmap_deserialize_finish(bitmap->bitmap);
654}
655
0fdf1a4f 656void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
ebab2259
FZ
657{
658 BdrvDirtyBitmap *bitmap;
967d7905 659 IO_CODE();
2119882c
PB
660
661 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
662 return;
663 }
664
665 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
666 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
667 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
668 continue;
669 }
d6883bc9 670 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 671 hbitmap_set(bitmap->bitmap, offset, bytes);
ebab2259 672 }
2119882c 673 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
674}
675
676/**
dc162c8e 677 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
ebab2259 678 */
715a74d8 679void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
ebab2259 680{
ca759622 681 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
ebab2259
FZ
682}
683
684int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
685{
ca759622 686 return hbitmap_count(bitmap->bitmap);
ebab2259 687}
6d3f4049 688
d6883bc9
VSO
689bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
690{
691 return bitmap->readonly;
692}
693
694/* Called with BQL taken. */
695void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
696{
1e638301 697 bdrv_dirty_bitmaps_lock(bitmap->bs);
d6883bc9 698 bitmap->readonly = value;
1e638301 699 bdrv_dirty_bitmaps_unlock(bitmap->bs);
d6883bc9
VSO
700}
701
702bool 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}
a0319aac 713
7ae89a0d
VSO
714bool 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
a88b179f 727/* Called with BQL taken. */
796a3798 728void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
a88b179f 729{
1e638301 730 bdrv_dirty_bitmaps_lock(bitmap->bs);
a88b179f 731 bitmap->persistent = persistent;
1e638301 732 bdrv_dirty_bitmaps_unlock(bitmap->bs);
a88b179f
VSO
733}
734
b0f45559
JS
735/* Called with BQL taken. */
736void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
737{
1e638301 738 bdrv_dirty_bitmaps_lock(bitmap->bs);
b0f45559
JS
739 assert(bitmap->persistent == true);
740 bitmap->inconsistent = true;
741 bitmap->disabled = true;
1e638301 742 bdrv_dirty_bitmaps_unlock(bitmap->bs);
b0f45559
JS
743}
744
9c98f145 745/* Called with BQL taken. */
c4e4b0fa 746void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
9c98f145 747{
1e638301 748 bdrv_dirty_bitmaps_lock(bitmap->bs);
c4e4b0fa 749 bitmap->skip_store = skip;
1e638301 750 bdrv_dirty_bitmaps_unlock(bitmap->bs);
9c98f145
VSO
751}
752
796a3798 753bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
a88b179f 754{
c4e4b0fa 755 return bitmap->persistent && !bitmap->skip_store;
a88b179f
VSO
756}
757
b0f45559
JS
758bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
759{
760 return bitmap->inconsistent;
761}
762
ef9041a7 763BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs)
3dd10a06 764{
ef9041a7
VSO
765 return QLIST_FIRST(&bs->dirty_bitmaps);
766}
767
768BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap)
769{
770 return QLIST_NEXT(bitmap, list);
3dd10a06 771}
a3b52535
VSO
772
773char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
774{
775 return hbitmap_sha256(bitmap->bitmap, errp);
776}
56207df5 777
9399c54b
VSO
778int64_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
642700fd
VSO
784int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset,
785 int64_t bytes)
56207df5 786{
76d570dc 787 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
56207df5 788}
b598e531 789
a78a1a48 790bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
299ea9ff
VSO
791 int64_t start, int64_t end, int64_t max_dirty_count,
792 int64_t *dirty_start, int64_t *dirty_count)
a78a1a48 793{
299ea9ff
VSO
794 return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_count,
795 dirty_start, dirty_count);
a78a1a48
VSO
796}
797
a6426475
VSO
798bool 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
b7661ca5
JS
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.
34ffacb7
VSO
809 *
810 * Returns true on success, false on failure. In case of failure bitmaps are
811 * untouched.
b7661ca5 812 */
34ffacb7 813bool bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
fa000f2f 814 HBitmap **backup, Error **errp)
b598e531 815{
34ffacb7 816 bool ret = false;
fa000f2f 817
1e638301
VSO
818 bdrv_dirty_bitmaps_lock(dest->bs);
819 if (src->bs != dest->bs) {
820 bdrv_dirty_bitmaps_lock(src->bs);
eff0829b 821 }
b598e531 822
3ae96d66 823 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
06bf5006
VSO
824 goto out;
825 }
826
cb8e58e3
JS
827 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
828 goto out;
829 }
b598e531 830
618af89e
VSO
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));
06bf5006 835 goto out;
b598e531
VSO
836 }
837
618af89e
VSO
838 bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
839 ret = true;
b7661ca5
JS
840
841out:
1e638301
VSO
842 bdrv_dirty_bitmaps_unlock(dest->bs);
843 if (src->bs != dest->bs) {
844 bdrv_dirty_bitmaps_unlock(src->bs);
b7661ca5 845 }
34ffacb7
VSO
846
847 return ret;
b7661ca5
JS
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.
618af89e 853 * @dest, @src and @backup (if not NULL) must have same size.
b7661ca5
JS
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.
b7661ca5 857 */
618af89e 858void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
b7661ca5
JS
859 const BdrvDirtyBitmap *src,
860 HBitmap **backup,
861 bool lock)
862{
967d7905 863 IO_CODE();
b7661ca5
JS
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) {
1e638301
VSO
870 bdrv_dirty_bitmaps_lock(dest->bs);
871 if (src->bs != dest->bs) {
872 bdrv_dirty_bitmaps_lock(src->bs);
b7661ca5
JS
873 }
874 }
875
fa000f2f
VSO
876 if (backup) {
877 *backup = dest->bitmap;
878 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
618af89e 879 hbitmap_merge(*backup, src->bitmap, dest->bitmap);
fa000f2f 880 } else {
618af89e 881 hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
fa000f2f 882 }
fa000f2f 883
b7661ca5 884 if (lock) {
1e638301
VSO
885 bdrv_dirty_bitmaps_unlock(dest->bs);
886 if (src->bs != dest->bs) {
887 bdrv_dirty_bitmaps_unlock(src->bs);
b7661ca5 888 }
eff0829b 889 }
b598e531 890}