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