]> git.proxmox.com Git - mirror_qemu.git/blob - block/dirty-bitmap.c
block/dirty-bitmap: add bs link
[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 BlockDriverState *bs;
34 HBitmap *bitmap; /* Dirty bitmap implementation */
35 bool busy; /* Bitmap is busy, it can't be used via QMP */
36 BdrvDirtyBitmap *successor; /* Anonymous child, if any. */
37 char *name; /* Optional non-empty unique ID */
38 int64_t size; /* Size of the bitmap, in bytes */
39 bool disabled; /* Bitmap is disabled. It ignores all writes to
40 the device */
41 int active_iterators; /* How many iterators are active */
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. */
48 bool persistent; /* bitmap must be saved to owner disk image */
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. */
52 bool skip_store; /* We are either migrating or deleting this
53 * bitmap; it should not be stored on the next
54 * inactivation. */
55 QLIST_ENTRY(BdrvDirtyBitmap) list;
56 };
57
58 struct BdrvDirtyBitmapIter {
59 HBitmapIter hbi;
60 BdrvDirtyBitmap *bitmap;
61 };
62
63 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
64 {
65 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
66 }
67
68 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
69 {
70 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
71 }
72
73 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
74 {
75 qemu_mutex_lock(bitmap->mutex);
76 }
77
78 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
79 {
80 qemu_mutex_unlock(bitmap->mutex);
81 }
82
83 /* Called with BQL or dirty_bitmap lock taken. */
84 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
85 {
86 BdrvDirtyBitmap *bm;
87
88 assert(name);
89 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
90 if (bm->name && !strcmp(name, bm->name)) {
91 return bm;
92 }
93 }
94 return NULL;
95 }
96
97 /* Called with BQL taken. */
98 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
99 uint32_t granularity,
100 const char *name,
101 Error **errp)
102 {
103 int64_t bitmap_size;
104 BdrvDirtyBitmap *bitmap;
105
106 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
107
108 if (name && bdrv_find_dirty_bitmap(bs, name)) {
109 error_setg(errp, "Bitmap already exists: %s", name);
110 return NULL;
111 }
112 bitmap_size = bdrv_getlength(bs);
113 if (bitmap_size < 0) {
114 error_setg_errno(errp, -bitmap_size, "could not get length of device");
115 errno = -bitmap_size;
116 return NULL;
117 }
118 bitmap = g_new0(BdrvDirtyBitmap, 1);
119 bitmap->bs = bs;
120 bitmap->mutex = &bs->dirty_bitmap_mutex;
121 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
122 bitmap->size = bitmap_size;
123 bitmap->name = g_strdup(name);
124 bitmap->disabled = false;
125 bdrv_dirty_bitmaps_lock(bs);
126 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
127 bdrv_dirty_bitmaps_unlock(bs);
128 return bitmap;
129 }
130
131 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
132 {
133 return bitmap->size;
134 }
135
136 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
137 {
138 return bitmap->name;
139 }
140
141 /* Called with BQL taken. */
142 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap)
143 {
144 return bitmap->successor;
145 }
146
147 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap)
148 {
149 return bitmap->busy;
150 }
151
152 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy)
153 {
154 qemu_mutex_lock(bitmap->mutex);
155 bitmap->busy = busy;
156 qemu_mutex_unlock(bitmap->mutex);
157 }
158
159 /* Called with BQL taken. */
160 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
161 {
162 return !bitmap->disabled;
163 }
164
165 /**
166 * bdrv_dirty_bitmap_status: This API is now deprecated.
167 * Called with BQL taken.
168 *
169 * A BdrvDirtyBitmap can be in four possible user-visible states:
170 * (1) Active: successor is NULL, and disabled is false: full r/w mode
171 * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode,
172 * guest writes are dropped, but monitor writes are possible,
173 * through commands like merge and clear.
174 * (3) Frozen: successor is not NULL.
175 * A frozen bitmap cannot be renamed, deleted, cleared, set,
176 * enabled, merged to, etc. A frozen bitmap can only abdicate()
177 * or reclaim().
178 * In this state, the anonymous successor bitmap may be either
179 * Active and recording writes from the guest (e.g. backup jobs),
180 * or it can be Disabled and not recording writes.
181 * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap
182 * in any way from the monitor.
183 * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and
184 * is unusable by QEMU. It can be deleted to remove it from
185 * the qcow2.
186 */
187 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
188 {
189 if (bdrv_dirty_bitmap_inconsistent(bitmap)) {
190 return DIRTY_BITMAP_STATUS_INCONSISTENT;
191 } else if (bdrv_dirty_bitmap_has_successor(bitmap)) {
192 return DIRTY_BITMAP_STATUS_FROZEN;
193 } else if (bdrv_dirty_bitmap_busy(bitmap)) {
194 return DIRTY_BITMAP_STATUS_LOCKED;
195 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
196 return DIRTY_BITMAP_STATUS_DISABLED;
197 } else {
198 return DIRTY_BITMAP_STATUS_ACTIVE;
199 }
200 }
201
202 /* Called with BQL taken. */
203 static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap)
204 {
205 return !bitmap->disabled || (bitmap->successor &&
206 !bitmap->successor->disabled);
207 }
208
209 int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags,
210 Error **errp)
211 {
212 if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) {
213 error_setg(errp, "Bitmap '%s' is currently in use by another"
214 " operation and cannot be used", bitmap->name);
215 return -1;
216 }
217
218 if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) {
219 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
220 bitmap->name);
221 return -1;
222 }
223
224 if ((flags & BDRV_BITMAP_INCONSISTENT) &&
225 bdrv_dirty_bitmap_inconsistent(bitmap)) {
226 error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used",
227 bitmap->name);
228 error_append_hint(errp, "Try block-dirty-bitmap-remove to delete"
229 " this bitmap from disk");
230 return -1;
231 }
232
233 return 0;
234 }
235
236 /**
237 * Create a successor bitmap destined to replace this bitmap after an operation.
238 * Requires that the bitmap is not marked busy and has no successor.
239 * The successor will be enabled if the parent bitmap was.
240 * Called with BQL taken.
241 */
242 int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp)
243 {
244 uint64_t granularity;
245 BdrvDirtyBitmap *child;
246
247 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) {
248 return -1;
249 }
250 if (bdrv_dirty_bitmap_has_successor(bitmap)) {
251 error_setg(errp, "Cannot create a successor for a bitmap that already "
252 "has one");
253 return -1;
254 }
255
256 /* Create an anonymous successor */
257 granularity = bdrv_dirty_bitmap_granularity(bitmap);
258 child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp);
259 if (!child) {
260 return -1;
261 }
262
263 /* Successor will be on or off based on our current state. */
264 child->disabled = bitmap->disabled;
265 bitmap->disabled = true;
266
267 /* Install the successor and mark the parent as busy */
268 bitmap->successor = child;
269 bitmap->busy = true;
270 return 0;
271 }
272
273 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
274 {
275 bitmap->disabled = false;
276 }
277
278 /* Called with BQL taken. */
279 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
280 {
281 assert(bitmap->mutex == bitmap->successor->mutex);
282 qemu_mutex_lock(bitmap->mutex);
283 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
284 qemu_mutex_unlock(bitmap->mutex);
285 }
286
287 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
288 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
289 {
290 assert(!bitmap->active_iterators);
291 assert(!bdrv_dirty_bitmap_busy(bitmap));
292 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
293 QLIST_REMOVE(bitmap, list);
294 hbitmap_free(bitmap->bitmap);
295 g_free(bitmap->name);
296 g_free(bitmap);
297 }
298
299 /**
300 * For a bitmap with a successor, yield our name to the successor,
301 * delete the old bitmap, and return a handle to the new bitmap.
302 * Called with BQL taken.
303 */
304 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(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(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(BdrvDirtyBitmap *parent,
336 Error **errp)
337 {
338 BdrvDirtyBitmap *successor = parent->successor;
339
340 if (!successor) {
341 error_setg(errp, "Cannot reclaim a successor when none is present");
342 return NULL;
343 }
344
345 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
346 error_setg(errp, "Merging of parent and successor bitmap failed");
347 return NULL;
348 }
349
350 parent->disabled = successor->disabled;
351 parent->busy = false;
352 bdrv_release_dirty_bitmap_locked(successor);
353 parent->successor = NULL;
354
355 return parent;
356 }
357
358 /* Called with BQL taken. */
359 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent,
360 Error **errp)
361 {
362 BdrvDirtyBitmap *ret;
363
364 qemu_mutex_lock(parent->mutex);
365 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp);
366 qemu_mutex_unlock(parent->mutex);
367
368 return ret;
369 }
370
371 /**
372 * Truncates _all_ bitmaps attached to a BDS.
373 * Called with BQL taken.
374 */
375 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
376 {
377 BdrvDirtyBitmap *bitmap;
378
379 bdrv_dirty_bitmaps_lock(bs);
380 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
381 assert(!bdrv_dirty_bitmap_busy(bitmap));
382 assert(!bdrv_dirty_bitmap_has_successor(bitmap));
383 assert(!bitmap->active_iterators);
384 hbitmap_truncate(bitmap->bitmap, bytes);
385 bitmap->size = bytes;
386 }
387 bdrv_dirty_bitmaps_unlock(bs);
388 }
389
390 /* Called with BQL taken. */
391 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap)
392 {
393 BlockDriverState *bs = bitmap->bs;
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 }