]> git.proxmox.com Git - mirror_qemu.git/blame - block/dirty-bitmap.c
block/dirty-bitmap: drop meta
[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 {
b64bd51e 32 QemuMutex *mutex;
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{
74 qemu_mutex_lock(bitmap->mutex);
75}
76
77void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
78{
79 qemu_mutex_unlock(bitmap->mutex);
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
FZ
106
107 if (name && bdrv_find_dirty_bitmap(bs, name)) {
108 error_setg(errp, "Bitmap already exists: %s", name);
109 return NULL;
110 }
993e6525 111 bitmap_size = bdrv_getlength(bs);
ebab2259
FZ
112 if (bitmap_size < 0) {
113 error_setg_errno(errp, -bitmap_size, "could not get length of device");
114 errno = -bitmap_size;
115 return NULL;
116 }
117 bitmap = g_new0(BdrvDirtyBitmap, 1);
b64bd51e 118 bitmap->mutex = &bs->dirty_bitmap_mutex;
ca759622 119 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
ebab2259
FZ
120 bitmap->size = bitmap_size;
121 bitmap->name = g_strdup(name);
122 bitmap->disabled = false;
2119882c 123 bdrv_dirty_bitmaps_lock(bs);
ebab2259 124 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
2119882c 125 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
126 return bitmap;
127}
128
15891fac
FZ
129int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
130{
993e6525 131 return bitmap->size;
15891fac
FZ
132}
133
134const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
135{
136 return bitmap->name;
137}
138
2119882c 139/* Called with BQL taken. */
50a47257 140bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
ebab2259
FZ
141{
142 return bitmap->successor;
143}
144
3ae96d66 145static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
27a1b301
JS
146{
147 return bitmap->busy;
993edc0c
JS
148}
149
27a1b301 150void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
4f43e953
VSO
151{
152 qemu_mutex_lock(bitmap->mutex);
27a1b301 153 bitmap->busy = busy;
4f43e953
VSO
154 qemu_mutex_unlock(bitmap->mutex);
155}
156
2119882c 157/* Called with BQL taken. */
ebab2259
FZ
158bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
159{
8b2e20f6 160 return !bitmap->disabled;
ebab2259
FZ
161}
162
1e6fddcd
JS
163/**
164 * bdrv_dirty_bitmap_status: This API is now deprecated.
165 * Called with BQL taken.
166 *
167 * A BdrvDirtyBitmap can be in four possible user-visible states:
168 * (1) Active: successor is NULL, and disabled is false: full r/w mode
169 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
170 * guest writes are dropped, but monitor writes are possible,
171 * through commands like merge and clear.
172 * (3) Frozen: successor is not NULL.
173 * A frozen bitmap cannot be renamed, deleted, cleared, set,
174 * enabled, merged to, etc. A frozen bitmap can only abdicate()
175 * or reclaim().
176 * In this state, the anonymous successor bitmap may be either
177 * Active and recording writes from the guest (e.g. backup jobs),
178 * or it can be Disabled and not recording writes.
179 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
180 * in any way from the monitor.
0064cfef
JS
181 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
182 * is unusable by QEMU. It can be deleted to remove it from
183 * the qcow2.
1e6fddcd 184 */
ebab2259
FZ
185DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
186{
0064cfef
JS
187 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
188 return DIRTY_BITMAP_STATUS_INCONSISTENT;
189 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
ebab2259 190 return DIRTY_BITMAP_STATUS_FROZEN;
27a1b301 191 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
4f43e953 192 return DIRTY_BITMAP_STATUS_LOCKED;
ebab2259
FZ
193 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
194 return DIRTY_BITMAP_STATUS_DISABLED;
195 } else {
196 return DIRTY_BITMAP_STATUS_ACTIVE;
197 }
198}
199
4db6ceb0
JS
200/* Called with BQL taken. */
201static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
202{
203 return !bitmap->disabled || (bitmap->successor &&
204 !bitmap->successor->disabled);
205}
206
3ae96d66
JS
207int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
208 Error **errp)
209{
210 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
211 error_setg(errp, "Bitmap '%s' is currently in use by another"
212 " operation and cannot be used", bitmap->name);
213 return -1;
214 }
215
216 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
217 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
218 bitmap->name);
219 return -1;
220 }
221
222 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
223 bdrv_dirty_bitmap_inconsistent(bitmap)) {
224 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
225 bitmap->name);
226 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
227 " this bitmap from disk");
228 return -1;
229 }
230
231 return 0;
232}
233
ebab2259
FZ
234/**
235 * Create a successor bitmap destined to replace this bitmap after an operation.
27a1b301 236 * Requires that the bitmap is not marked busy and has no successor.
8b2e20f6 237 * The successor will be enabled if the parent bitmap was.
2119882c 238 * Called with BQL taken.
ebab2259
FZ
239 */
240int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
241 BdrvDirtyBitmap *bitmap, Error **errp)
242{
243 uint64_t granularity;
244 BdrvDirtyBitmap *child;
245
3ae96d66 246 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
50a47257
JS
247 return -1;
248 }
249 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
250 error_setg(errp, "Cannot create a successor for a bitmap that already "
251 "has one");
ebab2259
FZ
252 return -1;
253 }
ebab2259
FZ
254
255 /* Create an anonymous successor */
256 granularity = bdrv_dirty_bitmap_granularity(bitmap);
257 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
258 if (!child) {
259 return -1;
260 }
261
262 /* Successor will be on or off based on our current state. */
263 child->disabled = bitmap->disabled;
8b2e20f6 264 bitmap->disabled = true;
ebab2259 265
27a1b301 266 /* Install the successor and mark the parent as busy */
ebab2259 267 bitmap->successor = child;
27a1b301 268 bitmap->busy = true;
ebab2259
FZ
269 return 0;
270}
271
92bcea40
VSO
272void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
273{
92bcea40
VSO
274 bitmap->disabled = false;
275}
276
e73a265e
VSO
277/* Called with BQL taken. */
278void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
279{
58f72b96 280 assert(bitmap->mutex == bitmap->successor->mutex);
e73a265e 281 qemu_mutex_lock(bitmap->mutex);
58f72b96 282 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
e73a265e
VSO
283 qemu_mutex_unlock(bitmap->mutex);
284}
285
b133c27f
PB
286/* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
287static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
604ab74b 288{
b133c27f 289 assert(!bitmap->active_iterators);
27a1b301 290 assert(!bdrv_dirty_bitmap_busy(bitmap));
50a47257 291 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
b133c27f
PB
292 QLIST_REMOVE(bitmap, list);
293 hbitmap_free(bitmap->bitmap);
294 g_free(bitmap->name);
295 g_free(bitmap);
604ab74b
VSO
296}
297
ebab2259
FZ
298/**
299 * For a bitmap with a successor, yield our name to the successor,
300 * delete the old bitmap, and return a handle to the new bitmap.
2119882c 301 * Called with BQL taken.
ebab2259
FZ
302 */
303BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
304 BdrvDirtyBitmap *bitmap,
305 Error **errp)
306{
307 char *name;
308 BdrvDirtyBitmap *successor = bitmap->successor;
309
310 if (successor == NULL) {
311 error_setg(errp, "Cannot relinquish control if "
312 "there's no successor present");
313 return NULL;
314 }
315
316 name = bitmap->name;
317 bitmap->name = NULL;
318 successor->name = name;
319 bitmap->successor = NULL;
a88b179f
VSO
320 successor->persistent = bitmap->persistent;
321 bitmap->persistent = false;
27a1b301 322 bitmap->busy = false;
ebab2259
FZ
323 bdrv_release_dirty_bitmap(bs, bitmap);
324
325 return successor;
326}
327
328/**
329 * In cases of failure where we can no longer safely delete the parent,
330 * we may wish to re-join the parent and child/successor.
27a1b301 331 * The merged parent will be marked as not busy.
8b2e20f6 332 * The marged parent will be enabled if and only if the successor was enabled.
044ee8e1 333 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
ebab2259 334 */
044ee8e1
VSO
335BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
336 BdrvDirtyBitmap *parent,
337 Error **errp)
ebab2259 338{
044ee8e1 339 BdrvDirtyBitmap *successor = parent->successor;
ebab2259
FZ
340
341 if (!successor) {
342 error_setg(errp, "Cannot reclaim a successor when none is present");
343 return NULL;
344 }
345
fa000f2f 346 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
ebab2259
FZ
347 error_setg(errp, "Merging of parent and successor bitmap failed");
348 return NULL;
349 }
8b2e20f6
JS
350
351 parent->disabled = successor->disabled;
27a1b301 352 parent->busy = false;
b133c27f 353 bdrv_release_dirty_bitmap_locked(successor);
ebab2259
FZ
354 parent->successor = NULL;
355
044ee8e1
VSO
356 return parent;
357}
358
359/* Called with BQL taken. */
360BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
361 BdrvDirtyBitmap *parent,
362 Error **errp)
363{
364 BdrvDirtyBitmap *ret;
365
366 qemu_mutex_lock(parent->mutex);
367 ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
604ab74b
VSO
368 qemu_mutex_unlock(parent->mutex);
369
044ee8e1 370 return ret;
ebab2259
FZ
371}
372
373/**
374 * Truncates _all_ bitmaps attached to a BDS.
2119882c 375 * Called with BQL taken.
ebab2259 376 */
1b6cc579 377void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
ebab2259
FZ
378{
379 BdrvDirtyBitmap *bitmap;
ebab2259 380
2119882c 381 bdrv_dirty_bitmaps_lock(bs);
ebab2259 382 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
27a1b301 383 assert(!bdrv_dirty_bitmap_busy(bitmap));
50a47257 384 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
dc162c8e 385 assert(!bitmap->active_iterators);
ca759622 386 hbitmap_truncate(bitmap->bitmap, bytes);
993e6525 387 bitmap->size = bytes;
ebab2259 388 }
2119882c 389 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
390}
391
2119882c 392/* Called with BQL taken. */
ebab2259
FZ
393void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
394{
b133c27f
PB
395 bdrv_dirty_bitmaps_lock(bs);
396 bdrv_release_dirty_bitmap_locked(bitmap);
397 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
398}
399
400/**
401 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
27a1b301 402 * There must not be any busy bitmaps attached.
56f364e6 403 * This function does not remove persistent bitmaps from the storage.
2119882c 404 * Called with BQL taken.
ebab2259
FZ
405 */
406void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
407{
b133c27f
PB
408 BdrvDirtyBitmap *bm, *next;
409
410 bdrv_dirty_bitmaps_lock(bs);
411 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
412 if (bdrv_dirty_bitmap_name(bm)) {
413 bdrv_release_dirty_bitmap_locked(bm);
414 }
415 }
416 bdrv_dirty_bitmaps_unlock(bs);
615b5dcf
VSO
417}
418
56f364e6
VSO
419/**
420 * Remove persistent dirty bitmap from the storage if it exists.
421 * Absence of bitmap is not an error, because we have the following scenario:
422 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
423 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
424 * not fail.
425 * This function doesn't release corresponding BdrvDirtyBitmap.
426 */
d2c3080e
VSO
427static int coroutine_fn
428bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
429 Error **errp)
56f364e6 430{
d2c3080e
VSO
431 if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) {
432 return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
56f364e6 433 }
b56a1e31
VSO
434
435 return 0;
56f364e6
VSO
436}
437
d2c3080e
VSO
438typedef struct BdrvRemovePersistentDirtyBitmapCo {
439 BlockDriverState *bs;
440 const char *name;
441 Error **errp;
442 int ret;
443} BdrvRemovePersistentDirtyBitmapCo;
444
445static void coroutine_fn
446bdrv_co_remove_persistent_dirty_bitmap_entry(void *opaque)
447{
448 BdrvRemovePersistentDirtyBitmapCo *s = opaque;
449
450 s->ret = bdrv_co_remove_persistent_dirty_bitmap(s->bs, s->name, s->errp);
451 aio_wait_kick();
452}
453
454int bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
455 Error **errp)
456{
457 if (qemu_in_coroutine()) {
458 return bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp);
459 } else {
460 Coroutine *co;
461 BdrvRemovePersistentDirtyBitmapCo s = {
462 .bs = bs,
463 .name = name,
464 .errp = errp,
465 .ret = -EINPROGRESS,
466 };
467
468 co = qemu_coroutine_create(bdrv_co_remove_persistent_dirty_bitmap_entry,
469 &s);
470 bdrv_coroutine_enter(bs, co);
471 BDRV_POLL_WHILE(bs, s.ret == -EINPROGRESS);
472
473 return s.ret;
474 }
475}
476
477static bool coroutine_fn
478bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
479 uint32_t granularity, Error **errp)
85cc8a4f
VSO
480{
481 BlockDriver *drv = bs->drv;
482
483 if (!drv) {
484 error_setg_errno(errp, ENOMEDIUM,
485 "Can't store persistent bitmaps to %s",
486 bdrv_get_device_or_node_name(bs));
487 return false;
488 }
489
d2c3080e 490 if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
85cc8a4f
VSO
491 error_setg_errno(errp, ENOTSUP,
492 "Can't store persistent bitmaps to %s",
493 bdrv_get_device_or_node_name(bs));
494 return false;
495 }
496
d2c3080e
VSO
497 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
498}
499
500typedef struct BdrvCanStoreNewDirtyBitmapCo {
501 BlockDriverState *bs;
502 const char *name;
503 uint32_t granularity;
504 Error **errp;
505 bool ret;
506
507 bool in_progress;
508} BdrvCanStoreNewDirtyBitmapCo;
509
510static void coroutine_fn bdrv_co_can_store_new_dirty_bitmap_entry(void *opaque)
511{
512 BdrvCanStoreNewDirtyBitmapCo *s = opaque;
513
514 s->ret = bdrv_co_can_store_new_dirty_bitmap(s->bs, s->name, s->granularity,
515 s->errp);
516 s->in_progress = false;
517 aio_wait_kick();
518}
519
520bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
521 uint32_t granularity, Error **errp)
522{
523 if (qemu_in_coroutine()) {
524 return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
525 } else {
526 Coroutine *co;
527 BdrvCanStoreNewDirtyBitmapCo s = {
528 .bs = bs,
529 .name = name,
530 .granularity = granularity,
531 .errp = errp,
532 .in_progress = true,
533 };
534
535 co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry,
536 &s);
537 bdrv_coroutine_enter(bs, co);
538 BDRV_POLL_WHILE(bs, s.in_progress);
539
540 return s.ret;
541 }
85cc8a4f
VSO
542}
543
ebab2259
FZ
544void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
545{
8b1402ce 546 bdrv_dirty_bitmap_lock(bitmap);
ebab2259 547 bitmap->disabled = true;
8b1402ce 548 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
549}
550
551void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
552{
8b1402ce 553 bdrv_dirty_bitmap_lock(bitmap);
92bcea40 554 bdrv_enable_dirty_bitmap_locked(bitmap);
8b1402ce 555 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
556}
557
558BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
559{
560 BdrvDirtyBitmap *bm;
561 BlockDirtyInfoList *list = NULL;
562 BlockDirtyInfoList **plist = &list;
563
2119882c 564 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
565 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
566 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
567 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
9a46dba7 568 info->count = bdrv_get_dirty_count(bm);
ebab2259
FZ
569 info->granularity = bdrv_dirty_bitmap_granularity(bm);
570 info->has_name = !!bm->name;
571 info->name = g_strdup(bm->name);
572 info->status = bdrv_dirty_bitmap_status(bm);
4db6ceb0 573 info->recording = bdrv_dirty_bitmap_recording(bm);
27a1b301 574 info->busy = bdrv_dirty_bitmap_busy(bm);
f67cf661 575 info->persistent = bm->persistent;
b0f45559
JS
576 info->has_inconsistent = bm->inconsistent;
577 info->inconsistent = bm->inconsistent;
ebab2259
FZ
578 entry->value = info;
579 *plist = entry;
580 plist = &entry->next;
581 }
2119882c 582 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
583
584 return list;
585}
586
b64bd51e 587/* Called within bdrv_dirty_bitmap_lock..unlock */
28636b82 588bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
ebab2259 589{
28636b82
JS
590 return hbitmap_get(bitmap->bitmap, offset);
591}
592
593bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset)
594{
595 bool ret;
596 bdrv_dirty_bitmap_lock(bitmap);
597 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset);
598 bdrv_dirty_bitmap_unlock(bitmap);
599
600 return ret;
ebab2259
FZ
601}
602
603/**
604 * Chooses a default granularity based on the existing cluster size,
605 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
606 * is no cluster size information available.
607 */
608uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
609{
610 BlockDriverInfo bdi;
611 uint32_t granularity;
612
613 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
614 granularity = MAX(4096, bdi.cluster_size);
615 granularity = MIN(65536, granularity);
616 } else {
617 granularity = 65536;
618 }
619
620 return granularity;
621}
622
ba06ff1a 623uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
ebab2259 624{
ca759622 625 return 1U << hbitmap_granularity(bitmap->bitmap);
ebab2259
FZ
626}
627
715a74d8 628BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
dc162c8e
FZ
629{
630 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
715a74d8 631 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
dc162c8e
FZ
632 iter->bitmap = bitmap;
633 bitmap->active_iterators++;
634 return iter;
635}
636
637void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
638{
639 if (!iter) {
640 return;
641 }
642 assert(iter->bitmap->active_iterators > 0);
643 iter->bitmap->active_iterators--;
644 g_free(iter);
645}
646
647int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
ebab2259 648{
19c021e1 649 return hbitmap_iter_next(&iter->hbi);
ebab2259
FZ
650}
651
b64bd51e
PB
652/* Called within bdrv_dirty_bitmap_lock..unlock */
653void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 654 int64_t offset, int64_t bytes)
b64bd51e 655{
d6883bc9 656 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 657 hbitmap_set(bitmap->bitmap, offset, bytes);
b64bd51e
PB
658}
659
ebab2259 660void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 661 int64_t offset, int64_t bytes)
b64bd51e
PB
662{
663 bdrv_dirty_bitmap_lock(bitmap);
e0d7f73e 664 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
b64bd51e
PB
665 bdrv_dirty_bitmap_unlock(bitmap);
666}
667
668/* Called within bdrv_dirty_bitmap_lock..unlock */
669void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 670 int64_t offset, int64_t bytes)
ebab2259 671{
d6883bc9 672 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 673 hbitmap_reset(bitmap->bitmap, offset, bytes);
ebab2259
FZ
674}
675
676void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 677 int64_t offset, int64_t bytes)
ebab2259 678{
b64bd51e 679 bdrv_dirty_bitmap_lock(bitmap);
e0d7f73e 680 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
b64bd51e 681 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
682}
683
684void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
685{
d6883bc9 686 assert(!bdrv_dirty_bitmap_readonly(bitmap));
b64bd51e 687 bdrv_dirty_bitmap_lock(bitmap);
ebab2259
FZ
688 if (!out) {
689 hbitmap_reset_all(bitmap->bitmap);
690 } else {
691 HBitmap *backup = bitmap->bitmap;
ca759622 692 bitmap->bitmap = hbitmap_alloc(bitmap->size,
ebab2259
FZ
693 hbitmap_granularity(backup));
694 *out = backup;
695 }
b64bd51e 696 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
697}
698
56bd6624 699void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
ebab2259
FZ
700{
701 HBitmap *tmp = bitmap->bitmap;
d6883bc9 702 assert(!bdrv_dirty_bitmap_readonly(bitmap));
56bd6624 703 bitmap->bitmap = backup;
ebab2259
FZ
704 hbitmap_free(tmp);
705}
706
882c36f5 707uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
86f6ae67 708 uint64_t offset, uint64_t bytes)
882c36f5 709{
ca759622 710 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
882c36f5
VSO
711}
712
713uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
714{
ca759622 715 return hbitmap_serialization_align(bitmap->bitmap);
882c36f5
VSO
716}
717
718void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
86f6ae67
EB
719 uint8_t *buf, uint64_t offset,
720 uint64_t bytes)
882c36f5 721{
ca759622 722 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
882c36f5
VSO
723}
724
725void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
86f6ae67
EB
726 uint8_t *buf, uint64_t offset,
727 uint64_t bytes, bool finish)
882c36f5 728{
ca759622 729 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
882c36f5
VSO
730}
731
732void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
86f6ae67 733 uint64_t offset, uint64_t bytes,
882c36f5
VSO
734 bool finish)
735{
ca759622 736 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
6bdc8b71
VSO
737}
738
739void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
86f6ae67 740 uint64_t offset, uint64_t bytes,
6bdc8b71
VSO
741 bool finish)
742{
ca759622 743 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
882c36f5
VSO
744}
745
746void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
747{
748 hbitmap_deserialize_finish(bitmap->bitmap);
749}
750
0fdf1a4f 751void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
ebab2259
FZ
752{
753 BdrvDirtyBitmap *bitmap;
2119882c
PB
754
755 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
756 return;
757 }
758
759 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
760 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
761 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
762 continue;
763 }
d6883bc9 764 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 765 hbitmap_set(bitmap->bitmap, offset, bytes);
ebab2259 766 }
2119882c 767 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
768}
769
770/**
dc162c8e 771 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
ebab2259 772 */
715a74d8 773void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
ebab2259 774{
ca759622 775 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
ebab2259
FZ
776}
777
778int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
779{
ca759622 780 return hbitmap_count(bitmap->bitmap);
ebab2259 781}
6d3f4049 782
d6883bc9
VSO
783bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
784{
785 return bitmap->readonly;
786}
787
788/* Called with BQL taken. */
789void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
790{
791 qemu_mutex_lock(bitmap->mutex);
792 bitmap->readonly = value;
793 qemu_mutex_unlock(bitmap->mutex);
794}
795
796bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
797{
798 BdrvDirtyBitmap *bm;
799 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
800 if (bm->readonly) {
801 return true;
802 }
803 }
804
805 return false;
806}
a0319aac 807
a88b179f 808/* Called with BQL taken. */
796a3798 809void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
a88b179f
VSO
810{
811 qemu_mutex_lock(bitmap->mutex);
812 bitmap->persistent = persistent;
813 qemu_mutex_unlock(bitmap->mutex);
814}
815
b0f45559
JS
816/* Called with BQL taken. */
817void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
818{
819 qemu_mutex_lock(bitmap->mutex);
820 assert(bitmap->persistent == true);
821 bitmap->inconsistent = true;
822 bitmap->disabled = true;
823 qemu_mutex_unlock(bitmap->mutex);
824}
825
9c98f145 826/* Called with BQL taken. */
c4e4b0fa 827void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
9c98f145
VSO
828{
829 qemu_mutex_lock(bitmap->mutex);
c4e4b0fa 830 bitmap->skip_store = skip;
9c98f145
VSO
831 qemu_mutex_unlock(bitmap->mutex);
832}
833
796a3798 834bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
a88b179f 835{
c4e4b0fa 836 return bitmap->persistent && !bitmap->skip_store;
a88b179f
VSO
837}
838
b0f45559
JS
839bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
840{
841 return bitmap->inconsistent;
842}
843
a88b179f
VSO
844bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
845{
846 BdrvDirtyBitmap *bm;
847 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
c4e4b0fa 848 if (bm->persistent && !bm->readonly && !bm->skip_store) {
a88b179f
VSO
849 return true;
850 }
851 }
852
853 return false;
854}
3dd10a06
VSO
855
856BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
857 BdrvDirtyBitmap *bitmap)
858{
859 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
860 QLIST_NEXT(bitmap, list);
861}
a3b52535
VSO
862
863char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
864{
865 return hbitmap_sha256(bitmap->bitmap, errp);
866}
56207df5 867
76d570dc
VSO
868int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
869 uint64_t bytes)
56207df5 870{
76d570dc 871 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
56207df5 872}
b598e531 873
a78a1a48
VSO
874bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
875 uint64_t *offset, uint64_t *bytes)
876{
877 return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
878}
879
b7661ca5
JS
880/**
881 * bdrv_merge_dirty_bitmap: merge src into dest.
882 * Ensures permissions on bitmaps are reasonable; use for public API.
883 *
884 * @backup: If provided, make a copy of dest here prior to merge.
885 */
b598e531 886void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
fa000f2f 887 HBitmap **backup, Error **errp)
b598e531 888{
fa000f2f
VSO
889 bool ret;
890
b598e531 891 qemu_mutex_lock(dest->mutex);
eff0829b
VSO
892 if (src->mutex != dest->mutex) {
893 qemu_mutex_lock(src->mutex);
894 }
b598e531 895
3ae96d66 896 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
06bf5006
VSO
897 goto out;
898 }
899
cb8e58e3
JS
900 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
901 goto out;
902 }
b598e531 903
fa000f2f 904 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
b598e531 905 error_setg(errp, "Bitmaps are incompatible and can't be merged");
06bf5006 906 goto out;
b598e531
VSO
907 }
908
b7661ca5
JS
909 ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
910 assert(ret);
911
912out:
913 qemu_mutex_unlock(dest->mutex);
914 if (src->mutex != dest->mutex) {
915 qemu_mutex_unlock(src->mutex);
916 }
917}
918
919/**
920 * bdrv_dirty_bitmap_merge_internal: merge src into dest.
921 * Does NOT check bitmap permissions; not suitable for use as public API.
922 *
923 * @backup: If provided, make a copy of dest here prior to merge.
924 * @lock: If true, lock and unlock bitmaps on the way in/out.
925 * returns true if the merge succeeded; false if unattempted.
926 */
927bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
928 const BdrvDirtyBitmap *src,
929 HBitmap **backup,
930 bool lock)
931{
932 bool ret;
933
934 assert(!bdrv_dirty_bitmap_readonly(dest));
935 assert(!bdrv_dirty_bitmap_inconsistent(dest));
936 assert(!bdrv_dirty_bitmap_inconsistent(src));
937
938 if (lock) {
939 qemu_mutex_lock(dest->mutex);
940 if (src->mutex != dest->mutex) {
941 qemu_mutex_lock(src->mutex);
942 }
943 }
944
fa000f2f
VSO
945 if (backup) {
946 *backup = dest->bitmap;
947 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
948 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
949 } else {
950 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
951 }
fa000f2f 952
b7661ca5
JS
953 if (lock) {
954 qemu_mutex_unlock(dest->mutex);
955 if (src->mutex != dest->mutex) {
956 qemu_mutex_unlock(src->mutex);
957 }
eff0829b 958 }
b7661ca5
JS
959
960 return ret;
b598e531 961}