]> git.proxmox.com Git - mirror_qemu.git/blob - block/dirty-bitmap.c
block/dirty-bitmap: drop meta
[mirror_qemu.git] / block / dirty-bitmap.c
1 /*
2 * Block Dirty Bitmap
3 *
4 * Copyright (c) 2016-2017 Red Hat. Inc
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"
25 #include "qapi/error.h"
26 #include "trace.h"
27 #include "block/block_int.h"
28 #include "block/blockjob.h"
29 #include "qemu/main-loop.h"
30
31 struct BdrvDirtyBitmap {
32 QemuMutex *mutex;
33 HBitmap *bitmap; /* Dirty bitmap implementation */
34 bool busy; /* Bitmap is busy, it can't be used via QMP */
35 BdrvDirtyBitmap *successor; /* Anonymous child, if any. */
36 char *name; /* Optional non-empty unique ID */
37 int64_t size; /* Size of the bitmap, in bytes */
38 bool disabled; /* Bitmap is disabled. It ignores all writes to
39 the device */
40 int active_iterators; /* How many iterators are active */
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. */
47 bool persistent; /* bitmap must be saved to owner disk image */
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. */
51 bool skip_store; /* We are either migrating or deleting this
52 * bitmap; it should not be stored on the next
53 * inactivation. */
54 QLIST_ENTRY(BdrvDirtyBitmap) list;
55 };
56
57 struct BdrvDirtyBitmapIter {
58 HBitmapIter hbi;
59 BdrvDirtyBitmap *bitmap;
60 };
61
62 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
63 {
64 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
65 }
66
67 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
68 {
69 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
70 }
71
72 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
73 {
74 qemu_mutex_lock(bitmap->mutex);
75 }
76
77 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
78 {
79 qemu_mutex_unlock(bitmap->mutex);
80 }
81
82 /* Called with BQL or dirty_bitmap lock taken. */
83 BdrvDirtyBitmap *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
96 /* Called with BQL taken. */
97 BdrvDirtyBitmap *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;
104
105 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
106
107 if (name && bdrv_find_dirty_bitmap(bs, name)) {
108 error_setg(errp, "Bitmap already exists: %s", name);
109 return NULL;
110 }
111 bitmap_size = bdrv_getlength(bs);
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);
118 bitmap->mutex = &bs->dirty_bitmap_mutex;
119 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
120 bitmap->size = bitmap_size;
121 bitmap->name = g_strdup(name);
122 bitmap->disabled = false;
123 bdrv_dirty_bitmaps_lock(bs);
124 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
125 bdrv_dirty_bitmaps_unlock(bs);
126 return bitmap;
127 }
128
129 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
130 {
131 return bitmap->size;
132 }
133
134 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
135 {
136 return bitmap->name;
137 }
138
139 /* Called with BQL taken. */
140 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
141 {
142 return bitmap->successor;
143 }
144
145 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
146 {
147 return bitmap->busy;
148 }
149
150 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
151 {
152 qemu_mutex_lock(bitmap->mutex);
153 bitmap->busy = busy;
154 qemu_mutex_unlock(bitmap->mutex);
155 }
156
157 /* Called with BQL taken. */
158 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
159 {
160 return !bitmap->disabled;
161 }
162
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.
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.
184 */
185 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
186 {
187 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
188 return DIRTY_BITMAP_STATUS_INCONSISTENT;
189 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
190 return DIRTY_BITMAP_STATUS_FROZEN;
191 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
192 return DIRTY_BITMAP_STATUS_LOCKED;
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
200 /* Called with BQL taken. */
201 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
202 {
203 return !bitmap->disabled || (bitmap->successor &&
204 !bitmap->successor->disabled);
205 }
206
207 int 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
234 /**
235 * Create a successor bitmap destined to replace this bitmap after an operation.
236 * Requires that the bitmap is not marked busy and has no successor.
237 * The successor will be enabled if the parent bitmap was.
238 * Called with BQL taken.
239 */
240 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
241 BdrvDirtyBitmap *bitmap, Error **errp)
242 {
243 uint64_t granularity;
244 BdrvDirtyBitmap *child;
245
246 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
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");
252 return -1;
253 }
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;
264 bitmap->disabled = true;
265
266 /* Install the successor and mark the parent as busy */
267 bitmap->successor = child;
268 bitmap->busy = true;
269 return 0;
270 }
271
272 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
273 {
274 bitmap->disabled = false;
275 }
276
277 /* Called with BQL taken. */
278 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
279 {
280 assert(bitmap->mutex == bitmap->successor->mutex);
281 qemu_mutex_lock(bitmap->mutex);
282 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
283 qemu_mutex_unlock(bitmap->mutex);
284 }
285
286 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
287 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
288 {
289 assert(!bitmap->active_iterators);
290 assert(!bdrv_dirty_bitmap_busy(bitmap));
291 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
292 QLIST_REMOVE(bitmap, list);
293 hbitmap_free(bitmap->bitmap);
294 g_free(bitmap->name);
295 g_free(bitmap);
296 }
297
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.
301 * Called with BQL taken.
302 */
303 BdrvDirtyBitmap *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;
320 successor->persistent = bitmap->persistent;
321 bitmap->persistent = false;
322 bitmap->busy = false;
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.
331 * The merged parent will be marked as not busy.
332 * The marged parent will be enabled if and only if the successor was enabled.
333 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
334 */
335 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
336 BdrvDirtyBitmap *parent,
337 Error **errp)
338 {
339 BdrvDirtyBitmap *successor = parent->successor;
340
341 if (!successor) {
342 error_setg(errp, "Cannot reclaim a successor when none is present");
343 return NULL;
344 }
345
346 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
347 error_setg(errp, "Merging of parent and successor bitmap failed");
348 return NULL;
349 }
350
351 parent->disabled = successor->disabled;
352 parent->busy = false;
353 bdrv_release_dirty_bitmap_locked(successor);
354 parent->successor = NULL;
355
356 return parent;
357 }
358
359 /* Called with BQL taken. */
360 BdrvDirtyBitmap *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);
368 qemu_mutex_unlock(parent->mutex);
369
370 return ret;
371 }
372
373 /**
374 * Truncates _all_ bitmaps attached to a BDS.
375 * Called with BQL taken.
376 */
377 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
378 {
379 BdrvDirtyBitmap *bitmap;
380
381 bdrv_dirty_bitmaps_lock(bs);
382 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
383 assert(!bdrv_dirty_bitmap_busy(bitmap));
384 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
385 assert(!bitmap->active_iterators);
386 hbitmap_truncate(bitmap->bitmap, bytes);
387 bitmap->size = bytes;
388 }
389 bdrv_dirty_bitmaps_unlock(bs);
390 }
391
392 /* Called with BQL taken. */
393 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
394 {
395 bdrv_dirty_bitmaps_lock(bs);
396 bdrv_release_dirty_bitmap_locked(bitmap);
397 bdrv_dirty_bitmaps_unlock(bs);
398 }
399
400 /**
401 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
402 * There must not be any busy bitmaps attached.
403 * This function does not remove persistent bitmaps from the storage.
404 * Called with BQL taken.
405 */
406 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
407 {
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);
417 }
418
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 */
427 static int coroutine_fn
428 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name,
429 Error **errp)
430 {
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);
433 }
434
435 return 0;
436 }
437
438 typedef struct BdrvRemovePersistentDirtyBitmapCo {
439 BlockDriverState *bs;
440 const char *name;
441 Error **errp;
442 int ret;
443 } BdrvRemovePersistentDirtyBitmapCo;
444
445 static void coroutine_fn
446 bdrv_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
454 int 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
477 static bool coroutine_fn
478 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
479 uint32_t granularity, Error **errp)
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
490 if (!drv->bdrv_co_can_store_new_dirty_bitmap) {
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
497 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp);
498 }
499
500 typedef 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
510 static 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
520 bool 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 }
542 }
543
544 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
545 {
546 bdrv_dirty_bitmap_lock(bitmap);
547 bitmap->disabled = true;
548 bdrv_dirty_bitmap_unlock(bitmap);
549 }
550
551 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
552 {
553 bdrv_dirty_bitmap_lock(bitmap);
554 bdrv_enable_dirty_bitmap_locked(bitmap);
555 bdrv_dirty_bitmap_unlock(bitmap);
556 }
557
558 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
559 {
560 BdrvDirtyBitmap *bm;
561 BlockDirtyInfoList *list = NULL;
562 BlockDirtyInfoList **plist = &list;
563
564 bdrv_dirty_bitmaps_lock(bs);
565 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
566 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
567 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
568 info->count = bdrv_get_dirty_count(bm);
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);
573 info->recording = bdrv_dirty_bitmap_recording(bm);
574 info->busy = bdrv_dirty_bitmap_busy(bm);
575 info->persistent = bm->persistent;
576 info->has_inconsistent = bm->inconsistent;
577 info->inconsistent = bm->inconsistent;
578 entry->value = info;
579 *plist = entry;
580 plist = &entry->next;
581 }
582 bdrv_dirty_bitmaps_unlock(bs);
583
584 return list;
585 }
586
587 /* Called within bdrv_dirty_bitmap_lock..unlock */
588 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset)
589 {
590 return hbitmap_get(bitmap->bitmap, offset);
591 }
592
593 bool 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;
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 */
608 uint32_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
623 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
624 {
625 return 1U << hbitmap_granularity(bitmap->bitmap);
626 }
627
628 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
629 {
630 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
631 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
632 iter->bitmap = bitmap;
633 bitmap->active_iterators++;
634 return iter;
635 }
636
637 void 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
647 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
648 {
649 return hbitmap_iter_next(&iter->hbi);
650 }
651
652 /* Called within bdrv_dirty_bitmap_lock..unlock */
653 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
654 int64_t offset, int64_t bytes)
655 {
656 assert(!bdrv_dirty_bitmap_readonly(bitmap));
657 hbitmap_set(bitmap->bitmap, offset, bytes);
658 }
659
660 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
661 int64_t offset, int64_t bytes)
662 {
663 bdrv_dirty_bitmap_lock(bitmap);
664 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
665 bdrv_dirty_bitmap_unlock(bitmap);
666 }
667
668 /* Called within bdrv_dirty_bitmap_lock..unlock */
669 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
670 int64_t offset, int64_t bytes)
671 {
672 assert(!bdrv_dirty_bitmap_readonly(bitmap));
673 hbitmap_reset(bitmap->bitmap, offset, bytes);
674 }
675
676 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
677 int64_t offset, int64_t bytes)
678 {
679 bdrv_dirty_bitmap_lock(bitmap);
680 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
681 bdrv_dirty_bitmap_unlock(bitmap);
682 }
683
684 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
685 {
686 assert(!bdrv_dirty_bitmap_readonly(bitmap));
687 bdrv_dirty_bitmap_lock(bitmap);
688 if (!out) {
689 hbitmap_reset_all(bitmap->bitmap);
690 } else {
691 HBitmap *backup = bitmap->bitmap;
692 bitmap->bitmap = hbitmap_alloc(bitmap->size,
693 hbitmap_granularity(backup));
694 *out = backup;
695 }
696 bdrv_dirty_bitmap_unlock(bitmap);
697 }
698
699 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
700 {
701 HBitmap *tmp = bitmap->bitmap;
702 assert(!bdrv_dirty_bitmap_readonly(bitmap));
703 bitmap->bitmap = backup;
704 hbitmap_free(tmp);
705 }
706
707 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
708 uint64_t offset, uint64_t bytes)
709 {
710 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
711 }
712
713 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
714 {
715 return hbitmap_serialization_align(bitmap->bitmap);
716 }
717
718 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
719 uint8_t *buf, uint64_t offset,
720 uint64_t bytes)
721 {
722 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
723 }
724
725 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
726 uint8_t *buf, uint64_t offset,
727 uint64_t bytes, bool finish)
728 {
729 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
730 }
731
732 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
733 uint64_t offset, uint64_t bytes,
734 bool finish)
735 {
736 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
737 }
738
739 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
740 uint64_t offset, uint64_t bytes,
741 bool finish)
742 {
743 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
744 }
745
746 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
747 {
748 hbitmap_deserialize_finish(bitmap->bitmap);
749 }
750
751 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
752 {
753 BdrvDirtyBitmap *bitmap;
754
755 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
756 return;
757 }
758
759 bdrv_dirty_bitmaps_lock(bs);
760 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
761 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
762 continue;
763 }
764 assert(!bdrv_dirty_bitmap_readonly(bitmap));
765 hbitmap_set(bitmap->bitmap, offset, bytes);
766 }
767 bdrv_dirty_bitmaps_unlock(bs);
768 }
769
770 /**
771 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
772 */
773 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
774 {
775 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
776 }
777
778 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
779 {
780 return hbitmap_count(bitmap->bitmap);
781 }
782
783 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
784 {
785 return bitmap->readonly;
786 }
787
788 /* Called with BQL taken. */
789 void 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
796 bool 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 }
807
808 /* Called with BQL taken. */
809 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent)
810 {
811 qemu_mutex_lock(bitmap->mutex);
812 bitmap->persistent = persistent;
813 qemu_mutex_unlock(bitmap->mutex);
814 }
815
816 /* Called with BQL taken. */
817 void 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
826 /* Called with BQL taken. */
827 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip)
828 {
829 qemu_mutex_lock(bitmap->mutex);
830 bitmap->skip_store = skip;
831 qemu_mutex_unlock(bitmap->mutex);
832 }
833
834 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap)
835 {
836 return bitmap->persistent && !bitmap->skip_store;
837 }
838
839 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap)
840 {
841 return bitmap->inconsistent;
842 }
843
844 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
845 {
846 BdrvDirtyBitmap *bm;
847 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
848 if (bm->persistent && !bm->readonly && !bm->skip_store) {
849 return true;
850 }
851 }
852
853 return false;
854 }
855
856 BdrvDirtyBitmap *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 }
862
863 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
864 {
865 return hbitmap_sha256(bitmap->bitmap, errp);
866 }
867
868 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
869 uint64_t bytes)
870 {
871 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
872 }
873
874 bool 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
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 */
886 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
887 HBitmap **backup, Error **errp)
888 {
889 bool ret;
890
891 qemu_mutex_lock(dest->mutex);
892 if (src->mutex != dest->mutex) {
893 qemu_mutex_lock(src->mutex);
894 }
895
896 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) {
897 goto out;
898 }
899
900 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) {
901 goto out;
902 }
903
904 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
905 error_setg(errp, "Bitmaps are incompatible and can't be merged");
906 goto out;
907 }
908
909 ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false);
910 assert(ret);
911
912 out:
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 */
927 bool 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
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 }
952
953 if (lock) {
954 qemu_mutex_unlock(dest->mutex);
955 if (src->mutex != dest->mutex) {
956 qemu_mutex_unlock(src->mutex);
957 }
958 }
959
960 return ret;
961 }