]> git.proxmox.com Git - mirror_qemu.git/blame - block/dirty-bitmap.c
Revert "vl: Fix to create migration object before block backends again"
[mirror_qemu.git] / block / dirty-bitmap.c
CommitLineData
ebab2259
FZ
1/*
2 * Block Dirty Bitmap
3 *
1b6cc579 4 * Copyright (c) 2016-2017 Red Hat. Inc
ebab2259
FZ
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu/osdep.h"
da34e65c 25#include "qapi/error.h"
ebab2259
FZ
26#include "qemu-common.h"
27#include "trace.h"
28#include "block/block_int.h"
29#include "block/blockjob.h"
30
ebab2259 31struct BdrvDirtyBitmap {
b64bd51e 32 QemuMutex *mutex;
ca759622 33 HBitmap *bitmap; /* Dirty bitmap implementation */
fb933437 34 HBitmap *meta; /* Meta dirty bitmap */
27a1b301 35 bool busy; /* Bitmap is busy, it can't be used via QMP */
21d2376f 36 BdrvDirtyBitmap *successor; /* Anonymous child, if any. */
ebab2259 37 char *name; /* Optional non-empty unique ID */
993e6525 38 int64_t size; /* Size of the bitmap, in bytes */
8bfc932e
VSO
39 bool disabled; /* Bitmap is disabled. It ignores all writes to
40 the device */
dc162c8e 41 int active_iterators; /* How many iterators are active */
d6883bc9
VSO
42 bool readonly; /* Bitmap is read-only. This field also
43 prevents the respective image from being
44 modified (i.e. blocks writes and discards).
45 Such operations must fail and both the image
46 and this bitmap must remain unchanged while
47 this flag is set. */
a88b179f 48 bool persistent; /* bitmap must be saved to owner disk image */
b0f45559
JS
49 bool inconsistent; /* bitmap is persistent, but inconsistent.
50 It cannot be used at all in any way, except
51 a QMP user can remove it. */
9c98f145
VSO
52 bool migration; /* Bitmap is selected for migration, it should
53 not be stored on the next inactivation
54 (persistent flag doesn't matter until next
55 invalidation).*/
ebab2259
FZ
56 QLIST_ENTRY(BdrvDirtyBitmap) list;
57};
58
dc162c8e
FZ
59struct BdrvDirtyBitmapIter {
60 HBitmapIter hbi;
61 BdrvDirtyBitmap *bitmap;
62};
63
2119882c
PB
64static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
65{
66 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
67}
68
69static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
70{
71 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
72}
73
b64bd51e
PB
74void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
75{
76 qemu_mutex_lock(bitmap->mutex);
77}
78
79void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
80{
81 qemu_mutex_unlock(bitmap->mutex);
82}
83
2119882c 84/* Called with BQL or dirty_bitmap lock taken. */
ebab2259
FZ
85BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
86{
87 BdrvDirtyBitmap *bm;
88
89 assert(name);
90 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
91 if (bm->name && !strcmp(name, bm->name)) {
92 return bm;
93 }
94 }
95 return NULL;
96}
97
2119882c 98/* Called with BQL taken. */
ebab2259
FZ
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
FZ
108
109 if (name && bdrv_find_dirty_bitmap(bs, name)) {
110 error_setg(errp, "Bitmap already exists: %s", name);
111 return NULL;
112 }
993e6525 113 bitmap_size = bdrv_getlength(bs);
ebab2259
FZ
114 if (bitmap_size < 0) {
115 error_setg_errno(errp, -bitmap_size, "could not get length of device");
116 errno = -bitmap_size;
117 return NULL;
118 }
119 bitmap = g_new0(BdrvDirtyBitmap, 1);
b64bd51e 120 bitmap->mutex = &bs->dirty_bitmap_mutex;
ca759622 121 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
ebab2259
FZ
122 bitmap->size = bitmap_size;
123 bitmap->name = g_strdup(name);
124 bitmap->disabled = false;
2119882c 125 bdrv_dirty_bitmaps_lock(bs);
ebab2259 126 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
2119882c 127 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
128 return bitmap;
129}
130
fb933437
FZ
131/* bdrv_create_meta_dirty_bitmap
132 *
133 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
134 * when a dirty status bit in @bitmap is changed (either from reset to set or
135 * the other way around), its respective meta dirty bitmap bit will be marked
136 * dirty as well.
137 *
138 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
139 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
140 * track.
141 */
142void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
143 int chunk_size)
144{
145 assert(!bitmap->meta);
b64bd51e 146 qemu_mutex_lock(bitmap->mutex);
fb933437
FZ
147 bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
148 chunk_size * BITS_PER_BYTE);
b64bd51e 149 qemu_mutex_unlock(bitmap->mutex);
fb933437
FZ
150}
151
152void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
153{
154 assert(bitmap->meta);
b64bd51e 155 qemu_mutex_lock(bitmap->mutex);
fb933437
FZ
156 hbitmap_free_meta(bitmap->bitmap);
157 bitmap->meta = NULL;
b64bd51e 158 qemu_mutex_unlock(bitmap->mutex);
fb933437
FZ
159}
160
15891fac
FZ
161int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
162{
993e6525 163 return bitmap->size;
15891fac
FZ
164}
165
166const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
167{
168 return bitmap->name;
169}
170
2119882c 171/* Called with BQL taken. */
50a47257 172bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
ebab2259
FZ
173{
174 return bitmap->successor;
175}
176
3ae96d66 177static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
27a1b301
JS
178{
179 return bitmap->busy;
993edc0c
JS
180}
181
27a1b301 182void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
4f43e953
VSO
183{
184 qemu_mutex_lock(bitmap->mutex);
27a1b301 185 bitmap->busy = busy;
4f43e953
VSO
186 qemu_mutex_unlock(bitmap->mutex);
187}
188
2119882c 189/* Called with BQL taken. */
ebab2259
FZ
190bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
191{
8b2e20f6 192 return !bitmap->disabled;
ebab2259
FZ
193}
194
1e6fddcd
JS
195/**
196 * bdrv_dirty_bitmap_status: This API is now deprecated.
197 * Called with BQL taken.
198 *
199 * A BdrvDirtyBitmap can be in four possible user-visible states:
200 * (1) Active: successor is NULL, and disabled is false: full r/w mode
201 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
202 * guest writes are dropped, but monitor writes are possible,
203 * through commands like merge and clear.
204 * (3) Frozen: successor is not NULL.
205 * A frozen bitmap cannot be renamed, deleted, cleared, set,
206 * enabled, merged to, etc. A frozen bitmap can only abdicate()
207 * or reclaim().
208 * In this state, the anonymous successor bitmap may be either
209 * Active and recording writes from the guest (e.g. backup jobs),
210 * or it can be Disabled and not recording writes.
211 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
212 * in any way from the monitor.
0064cfef
JS
213 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
214 * is unusable by QEMU. It can be deleted to remove it from
215 * the qcow2.
1e6fddcd 216 */
ebab2259
FZ
217DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
218{
0064cfef
JS
219 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
220 return DIRTY_BITMAP_STATUS_INCONSISTENT;
221 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
ebab2259 222 return DIRTY_BITMAP_STATUS_FROZEN;
27a1b301 223 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
4f43e953 224 return DIRTY_BITMAP_STATUS_LOCKED;
ebab2259
FZ
225 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
226 return DIRTY_BITMAP_STATUS_DISABLED;
227 } else {
228 return DIRTY_BITMAP_STATUS_ACTIVE;
229 }
230}
231
4db6ceb0
JS
232/* Called with BQL taken. */
233static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
234{
235 return !bitmap->disabled || (bitmap->successor &&
236 !bitmap->successor->disabled);
237}
238
3ae96d66
JS
239int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
240 Error **errp)
241{
242 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
243 error_setg(errp, "Bitmap '%s' is currently in use by another"
244 " operation and cannot be used", bitmap->name);
245 return -1;
246 }
247
248 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
249 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
250 bitmap->name);
251 return -1;
252 }
253
254 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
255 bdrv_dirty_bitmap_inconsistent(bitmap)) {
256 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
257 bitmap->name);
258 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
259 " this bitmap from disk");
260 return -1;
261 }
262
263 return 0;
264}
265
ebab2259
FZ
266/**
267 * Create a successor bitmap destined to replace this bitmap after an operation.
27a1b301 268 * Requires that the bitmap is not marked busy and has no successor.
8b2e20f6 269 * The successor will be enabled if the parent bitmap was.
2119882c 270 * Called with BQL taken.
ebab2259
FZ
271 */
272int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
273 BdrvDirtyBitmap *bitmap, Error **errp)
274{
275 uint64_t granularity;
276 BdrvDirtyBitmap *child;
277
3ae96d66 278 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
50a47257
JS
279 return -1;
280 }
281 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
282 error_setg(errp, "Cannot create a successor for a bitmap that already "
283 "has one");
ebab2259
FZ
284 return -1;
285 }
ebab2259
FZ
286
287 /* Create an anonymous successor */
288 granularity = bdrv_dirty_bitmap_granularity(bitmap);
289 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
290 if (!child) {
291 return -1;
292 }
293
294 /* Successor will be on or off based on our current state. */
295 child->disabled = bitmap->disabled;
8b2e20f6 296 bitmap->disabled = true;
ebab2259 297
27a1b301 298 /* Install the successor and mark the parent as busy */
ebab2259 299 bitmap->successor = child;
27a1b301 300 bitmap->busy = true;
ebab2259
FZ
301 return 0;
302}
303
92bcea40
VSO
304void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
305{
92bcea40
VSO
306 bitmap->disabled = false;
307}
308
e73a265e
VSO
309/* Called with BQL taken. */
310void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
311{
58f72b96 312 assert(bitmap->mutex == bitmap->successor->mutex);
e73a265e 313 qemu_mutex_lock(bitmap->mutex);
58f72b96 314 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
e73a265e
VSO
315 qemu_mutex_unlock(bitmap->mutex);
316}
317
b133c27f
PB
318/* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
319static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
604ab74b 320{
b133c27f 321 assert(!bitmap->active_iterators);
27a1b301 322 assert(!bdrv_dirty_bitmap_busy(bitmap));
50a47257 323 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
b133c27f
PB
324 assert(!bitmap->meta);
325 QLIST_REMOVE(bitmap, list);
326 hbitmap_free(bitmap->bitmap);
327 g_free(bitmap->name);
328 g_free(bitmap);
604ab74b
VSO
329}
330
ebab2259
FZ
331/**
332 * For a bitmap with a successor, yield our name to the successor,
333 * delete the old bitmap, and return a handle to the new bitmap.
2119882c 334 * Called with BQL taken.
ebab2259
FZ
335 */
336BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
337 BdrvDirtyBitmap *bitmap,
338 Error **errp)
339{
340 char *name;
341 BdrvDirtyBitmap *successor = bitmap->successor;
342
343 if (successor == NULL) {
344 error_setg(errp, "Cannot relinquish control if "
345 "there's no successor present");
346 return NULL;
347 }
348
349 name = bitmap->name;
350 bitmap->name = NULL;
351 successor->name = name;
352 bitmap->successor = NULL;
a88b179f
VSO
353 successor->persistent = bitmap->persistent;
354 bitmap->persistent = false;
27a1b301 355 bitmap->busy = false;
ebab2259
FZ
356 bdrv_release_dirty_bitmap(bs, bitmap);
357
358 return successor;
359}
360
361/**
362 * In cases of failure where we can no longer safely delete the parent,
363 * we may wish to re-join the parent and child/successor.
27a1b301 364 * The merged parent will be marked as not busy.
8b2e20f6 365 * The marged parent will be enabled if and only if the successor was enabled.
044ee8e1 366 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
ebab2259 367 */
044ee8e1
VSO
368BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
369 BdrvDirtyBitmap *parent,
370 Error **errp)
ebab2259 371{
044ee8e1 372 BdrvDirtyBitmap *successor = parent->successor;
ebab2259
FZ
373
374 if (!successor) {
375 error_setg(errp, "Cannot reclaim a successor when none is present");
376 return NULL;
377 }
378
fa000f2f 379 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
ebab2259
FZ
380 error_setg(errp, "Merging of parent and successor bitmap failed");
381 return NULL;
382 }
8b2e20f6
JS
383
384 parent->disabled = successor->disabled;
27a1b301 385 parent->busy = false;
b133c27f 386 bdrv_release_dirty_bitmap_locked(successor);
ebab2259
FZ
387 parent->successor = NULL;
388
044ee8e1
VSO
389 return parent;
390}
391
392/* Called with BQL taken. */
393BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
394 BdrvDirtyBitmap *parent,
395 Error **errp)
396{
397 BdrvDirtyBitmap *ret;
398
399 qemu_mutex_lock(parent->mutex);
400 ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
604ab74b
VSO
401 qemu_mutex_unlock(parent->mutex);
402
044ee8e1 403 return ret;
ebab2259
FZ
404}
405
406/**
407 * Truncates _all_ bitmaps attached to a BDS.
2119882c 408 * Called with BQL taken.
ebab2259 409 */
1b6cc579 410void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
ebab2259
FZ
411{
412 BdrvDirtyBitmap *bitmap;
ebab2259 413
2119882c 414 bdrv_dirty_bitmaps_lock(bs);
ebab2259 415 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
27a1b301 416 assert(!bdrv_dirty_bitmap_busy(bitmap));
50a47257 417 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
dc162c8e 418 assert(!bitmap->active_iterators);
ca759622 419 hbitmap_truncate(bitmap->bitmap, bytes);
993e6525 420 bitmap->size = bytes;
ebab2259 421 }
2119882c 422 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
423}
424
2119882c 425/* Called with BQL taken. */
ebab2259
FZ
426void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
427{
b133c27f
PB
428 bdrv_dirty_bitmaps_lock(bs);
429 bdrv_release_dirty_bitmap_locked(bitmap);
430 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
431}
432
433/**
434 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
27a1b301 435 * There must not be any busy bitmaps attached.
56f364e6 436 * This function does not remove persistent bitmaps from the storage.
2119882c 437 * Called with BQL taken.
ebab2259
FZ
438 */
439void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
440{
b133c27f
PB
441 BdrvDirtyBitmap *bm, *next;
442
443 bdrv_dirty_bitmaps_lock(bs);
444 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
445 if (bdrv_dirty_bitmap_name(bm)) {
446 bdrv_release_dirty_bitmap_locked(bm);
447 }
448 }
449 bdrv_dirty_bitmaps_unlock(bs);
615b5dcf
VSO
450}
451
56f364e6
VSO
452/**
453 * Remove persistent dirty bitmap from the storage if it exists.
454 * Absence of bitmap is not an error, because we have the following scenario:
455 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
456 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
457 * not fail.
458 * This function doesn't release corresponding BdrvDirtyBitmap.
459 */
460void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
461 const char *name,
462 Error **errp)
463{
464 if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
465 bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
466 }
467}
468
ebab2259
FZ
469void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
470{
8b1402ce 471 bdrv_dirty_bitmap_lock(bitmap);
ebab2259 472 bitmap->disabled = true;
8b1402ce 473 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
474}
475
476void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
477{
8b1402ce 478 bdrv_dirty_bitmap_lock(bitmap);
92bcea40 479 bdrv_enable_dirty_bitmap_locked(bitmap);
8b1402ce 480 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
481}
482
483BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
484{
485 BdrvDirtyBitmap *bm;
486 BlockDirtyInfoList *list = NULL;
487 BlockDirtyInfoList **plist = &list;
488
2119882c 489 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
490 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
491 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
492 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
9a46dba7 493 info->count = bdrv_get_dirty_count(bm);
ebab2259
FZ
494 info->granularity = bdrv_dirty_bitmap_granularity(bm);
495 info->has_name = !!bm->name;
496 info->name = g_strdup(bm->name);
497 info->status = bdrv_dirty_bitmap_status(bm);
4db6ceb0 498 info->recording = bdrv_dirty_bitmap_recording(bm);
27a1b301 499 info->busy = bdrv_dirty_bitmap_busy(bm);
f67cf661 500 info->persistent = bm->persistent;
b0f45559
JS
501 info->has_inconsistent = bm->inconsistent;
502 info->inconsistent = bm->inconsistent;
ebab2259
FZ
503 entry->value = info;
504 *plist = entry;
505 plist = &entry->next;
506 }
2119882c 507 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
508
509 return list;
510}
511
b64bd51e 512/* Called within bdrv_dirty_bitmap_lock..unlock */
3b5d4df0
EB
513bool bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
514 int64_t offset)
ebab2259
FZ
515{
516 if (bitmap) {
ca759622 517 return hbitmap_get(bitmap->bitmap, offset);
ebab2259 518 } else {
3b5d4df0 519 return false;
ebab2259
FZ
520 }
521}
522
523/**
524 * Chooses a default granularity based on the existing cluster size,
525 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
526 * is no cluster size information available.
527 */
528uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
529{
530 BlockDriverInfo bdi;
531 uint32_t granularity;
532
533 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
534 granularity = MAX(4096, bdi.cluster_size);
535 granularity = MIN(65536, granularity);
536 } else {
537 granularity = 65536;
538 }
539
540 return granularity;
541}
542
ba06ff1a 543uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
ebab2259 544{
ca759622 545 return 1U << hbitmap_granularity(bitmap->bitmap);
ebab2259
FZ
546}
547
715a74d8 548BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
dc162c8e
FZ
549{
550 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
715a74d8 551 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
dc162c8e
FZ
552 iter->bitmap = bitmap;
553 bitmap->active_iterators++;
554 return iter;
555}
556
6d3f4049
FZ
557BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
558{
559 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
560 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
561 iter->bitmap = bitmap;
562 bitmap->active_iterators++;
563 return iter;
564}
565
dc162c8e
FZ
566void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
567{
568 if (!iter) {
569 return;
570 }
571 assert(iter->bitmap->active_iterators > 0);
572 iter->bitmap->active_iterators--;
573 g_free(iter);
574}
575
576int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
ebab2259 577{
19c021e1 578 return hbitmap_iter_next(&iter->hbi);
ebab2259
FZ
579}
580
b64bd51e
PB
581/* Called within bdrv_dirty_bitmap_lock..unlock */
582void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 583 int64_t offset, int64_t bytes)
b64bd51e 584{
d6883bc9 585 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 586 hbitmap_set(bitmap->bitmap, offset, bytes);
b64bd51e
PB
587}
588
ebab2259 589void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 590 int64_t offset, int64_t bytes)
b64bd51e
PB
591{
592 bdrv_dirty_bitmap_lock(bitmap);
e0d7f73e 593 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
b64bd51e
PB
594 bdrv_dirty_bitmap_unlock(bitmap);
595}
596
597/* Called within bdrv_dirty_bitmap_lock..unlock */
598void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
e0d7f73e 599 int64_t offset, int64_t bytes)
ebab2259 600{
d6883bc9 601 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 602 hbitmap_reset(bitmap->bitmap, offset, bytes);
ebab2259
FZ
603}
604
605void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
e0d7f73e 606 int64_t offset, int64_t bytes)
ebab2259 607{
b64bd51e 608 bdrv_dirty_bitmap_lock(bitmap);
e0d7f73e 609 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
b64bd51e 610 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
611}
612
613void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
614{
d6883bc9 615 assert(!bdrv_dirty_bitmap_readonly(bitmap));
b64bd51e 616 bdrv_dirty_bitmap_lock(bitmap);
ebab2259
FZ
617 if (!out) {
618 hbitmap_reset_all(bitmap->bitmap);
619 } else {
620 HBitmap *backup = bitmap->bitmap;
ca759622 621 bitmap->bitmap = hbitmap_alloc(bitmap->size,
ebab2259
FZ
622 hbitmap_granularity(backup));
623 *out = backup;
624 }
b64bd51e 625 bdrv_dirty_bitmap_unlock(bitmap);
ebab2259
FZ
626}
627
56bd6624 628void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
ebab2259
FZ
629{
630 HBitmap *tmp = bitmap->bitmap;
d6883bc9 631 assert(!bdrv_dirty_bitmap_readonly(bitmap));
56bd6624 632 bitmap->bitmap = backup;
ebab2259
FZ
633 hbitmap_free(tmp);
634}
635
882c36f5 636uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
86f6ae67 637 uint64_t offset, uint64_t bytes)
882c36f5 638{
ca759622 639 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
882c36f5
VSO
640}
641
642uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
643{
ca759622 644 return hbitmap_serialization_align(bitmap->bitmap);
882c36f5
VSO
645}
646
647void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
86f6ae67
EB
648 uint8_t *buf, uint64_t offset,
649 uint64_t bytes)
882c36f5 650{
ca759622 651 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
882c36f5
VSO
652}
653
654void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
86f6ae67
EB
655 uint8_t *buf, uint64_t offset,
656 uint64_t bytes, bool finish)
882c36f5 657{
ca759622 658 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
882c36f5
VSO
659}
660
661void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
86f6ae67 662 uint64_t offset, uint64_t bytes,
882c36f5
VSO
663 bool finish)
664{
ca759622 665 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
6bdc8b71
VSO
666}
667
668void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
86f6ae67 669 uint64_t offset, uint64_t bytes,
6bdc8b71
VSO
670 bool finish)
671{
ca759622 672 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
882c36f5
VSO
673}
674
675void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
676{
677 hbitmap_deserialize_finish(bitmap->bitmap);
678}
679
0fdf1a4f 680void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
ebab2259
FZ
681{
682 BdrvDirtyBitmap *bitmap;
2119882c
PB
683
684 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
685 return;
686 }
687
688 bdrv_dirty_bitmaps_lock(bs);
ebab2259
FZ
689 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
690 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
691 continue;
692 }
d6883bc9 693 assert(!bdrv_dirty_bitmap_readonly(bitmap));
ca759622 694 hbitmap_set(bitmap->bitmap, offset, bytes);
ebab2259 695 }
2119882c 696 bdrv_dirty_bitmaps_unlock(bs);
ebab2259
FZ
697}
698
699/**
dc162c8e 700 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
ebab2259 701 */
715a74d8 702void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
ebab2259 703{
ca759622 704 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
ebab2259
FZ
705}
706
707int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
708{
ca759622 709 return hbitmap_count(bitmap->bitmap);
ebab2259 710}
6d3f4049
FZ
711
712int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
713{
714 return hbitmap_count(bitmap->meta);
715}
d6883bc9
VSO
716
717bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
718{
719 return bitmap->readonly;
720}
721
722/* Called with BQL taken. */
723void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
724{
725 qemu_mutex_lock(bitmap->mutex);
726 bitmap->readonly = value;
727 qemu_mutex_unlock(bitmap->mutex);
728}
729
730bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
731{
732 BdrvDirtyBitmap *bm;
733 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
734 if (bm->readonly) {
735 return true;
736 }
737 }
738
739 return false;
740}
a0319aac 741
a88b179f 742/* Called with BQL taken. */
796a3798 743void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
a88b179f
VSO
744{
745 qemu_mutex_lock(bitmap->mutex);
746 bitmap->persistent = persistent;
747 qemu_mutex_unlock(bitmap->mutex);
748}
749
b0f45559
JS
750/* Called with BQL taken. */
751void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap)
752{
753 qemu_mutex_lock(bitmap->mutex);
754 assert(bitmap->persistent == true);
755 bitmap->inconsistent = true;
756 bitmap->disabled = true;
757 qemu_mutex_unlock(bitmap->mutex);
758}
759
9c98f145
VSO
760/* Called with BQL taken. */
761void bdrv_dirty_bitmap_set_migration(BdrvDirtyBitmap *bitmap, bool migration)
762{
763 qemu_mutex_lock(bitmap->mutex);
764 bitmap->migration = migration;
765 qemu_mutex_unlock(bitmap->mutex);
766}
767
796a3798 768bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
a88b179f 769{
9c98f145 770 return bitmap->persistent && !bitmap->migration;
a88b179f
VSO
771}
772
b0f45559
JS
773bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
774{
775 return bitmap->inconsistent;
776}
777
a88b179f
VSO
778bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
779{
780 BdrvDirtyBitmap *bm;
781 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
9c98f145 782 if (bm->persistent && !bm->readonly && !bm->migration) {
a88b179f
VSO
783 return true;
784 }
785 }
786
787 return false;
788}
3dd10a06
VSO
789
790BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
791 BdrvDirtyBitmap *bitmap)
792{
793 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
794 QLIST_NEXT(bitmap, list);
795}
a3b52535
VSO
796
797char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
798{
799 return hbitmap_sha256(bitmap->bitmap, errp);
800}
56207df5 801
76d570dc
VSO
802int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
803 uint64_t bytes)
56207df5 804{
76d570dc 805 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
56207df5 806}
b598e531 807
a78a1a48
VSO
808bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
809 uint64_t *offset, uint64_t *bytes)
810{
811 return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
812}
813
b598e531 814void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
fa000f2f 815 HBitmap **backup, Error **errp)
b598e531 816{
fa000f2f
VSO
817 bool ret;
818
b598e531
VSO
819 /* only bitmaps from one bds are supported */
820 assert(dest->mutex == src->mutex);
821
822 qemu_mutex_lock(dest->mutex);
823
3ae96d66 824 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
06bf5006
VSO
825 goto out;
826 }
827
cb8e58e3
JS
828 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
829 goto out;
830 }
b598e531 831
fa000f2f 832 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
b598e531 833 error_setg(errp, "Bitmaps are incompatible and can't be merged");
06bf5006 834 goto out;
b598e531
VSO
835 }
836
fa000f2f
VSO
837 if (backup) {
838 *backup = dest->bitmap;
839 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
840 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
841 } else {
842 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
843 }
844 assert(ret);
845
06bf5006 846out:
b598e531
VSO
847 qemu_mutex_unlock(dest->mutex);
848}