]> git.ipfire.org Git - thirdparty/qemu.git/blob - block/dirty-bitmap.c
dirty-bitmap: Expose persistent flag to 'query-block'
[thirdparty/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 "qemu-common.h"
27 #include "trace.h"
28 #include "block/block_int.h"
29 #include "block/blockjob.h"
30
31 /**
32 * A BdrvDirtyBitmap can be in three possible states:
33 * (1) successor is NULL and disabled is false: full r/w mode
34 * (2) successor is NULL and disabled is true: read only mode ("disabled")
35 * (3) successor is set: frozen mode.
36 * A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set,
37 * or enabled. A frozen bitmap can only abdicate() or reclaim().
38 */
39 struct BdrvDirtyBitmap {
40 QemuMutex *mutex;
41 HBitmap *bitmap; /* Dirty bitmap implementation */
42 HBitmap *meta; /* Meta dirty bitmap */
43 bool qmp_locked; /* Bitmap is locked, it can't be modified
44 through QMP */
45 BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */
46 char *name; /* Optional non-empty unique ID */
47 int64_t size; /* Size of the bitmap, in bytes */
48 bool disabled; /* Bitmap is disabled. It ignores all writes to
49 the device */
50 int active_iterators; /* How many iterators are active */
51 bool readonly; /* Bitmap is read-only. This field also
52 prevents the respective image from being
53 modified (i.e. blocks writes and discards).
54 Such operations must fail and both the image
55 and this bitmap must remain unchanged while
56 this flag is set. */
57 bool persistent; /* bitmap must be saved to owner disk image */
58 bool migration; /* Bitmap is selected for migration, it should
59 not be stored on the next inactivation
60 (persistent flag doesn't matter until next
61 invalidation).*/
62 QLIST_ENTRY(BdrvDirtyBitmap) list;
63 };
64
65 struct BdrvDirtyBitmapIter {
66 HBitmapIter hbi;
67 BdrvDirtyBitmap *bitmap;
68 };
69
70 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
71 {
72 qemu_mutex_lock(&bs->dirty_bitmap_mutex);
73 }
74
75 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
76 {
77 qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
78 }
79
80 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
81 {
82 qemu_mutex_lock(bitmap->mutex);
83 }
84
85 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
86 {
87 qemu_mutex_unlock(bitmap->mutex);
88 }
89
90 /* Called with BQL or dirty_bitmap lock taken. */
91 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
92 {
93 BdrvDirtyBitmap *bm;
94
95 assert(name);
96 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
97 if (bm->name && !strcmp(name, bm->name)) {
98 return bm;
99 }
100 }
101 return NULL;
102 }
103
104 /* Called with BQL taken. */
105 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
106 uint32_t granularity,
107 const char *name,
108 Error **errp)
109 {
110 int64_t bitmap_size;
111 BdrvDirtyBitmap *bitmap;
112
113 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
114
115 if (name && bdrv_find_dirty_bitmap(bs, name)) {
116 error_setg(errp, "Bitmap already exists: %s", name);
117 return NULL;
118 }
119 bitmap_size = bdrv_getlength(bs);
120 if (bitmap_size < 0) {
121 error_setg_errno(errp, -bitmap_size, "could not get length of device");
122 errno = -bitmap_size;
123 return NULL;
124 }
125 bitmap = g_new0(BdrvDirtyBitmap, 1);
126 bitmap->mutex = &bs->dirty_bitmap_mutex;
127 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
128 bitmap->size = bitmap_size;
129 bitmap->name = g_strdup(name);
130 bitmap->disabled = false;
131 bdrv_dirty_bitmaps_lock(bs);
132 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
133 bdrv_dirty_bitmaps_unlock(bs);
134 return bitmap;
135 }
136
137 /* bdrv_create_meta_dirty_bitmap
138 *
139 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
140 * when a dirty status bit in @bitmap is changed (either from reset to set or
141 * the other way around), its respective meta dirty bitmap bit will be marked
142 * dirty as well.
143 *
144 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
145 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
146 * track.
147 */
148 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
149 int chunk_size)
150 {
151 assert(!bitmap->meta);
152 qemu_mutex_lock(bitmap->mutex);
153 bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
154 chunk_size * BITS_PER_BYTE);
155 qemu_mutex_unlock(bitmap->mutex);
156 }
157
158 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
159 {
160 assert(bitmap->meta);
161 qemu_mutex_lock(bitmap->mutex);
162 hbitmap_free_meta(bitmap->bitmap);
163 bitmap->meta = NULL;
164 qemu_mutex_unlock(bitmap->mutex);
165 }
166
167 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
168 {
169 return bitmap->size;
170 }
171
172 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
173 {
174 return bitmap->name;
175 }
176
177 /* Called with BQL taken. */
178 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
179 {
180 return bitmap->successor;
181 }
182
183 /* Both conditions disallow user-modification via QMP. */
184 bool bdrv_dirty_bitmap_user_locked(BdrvDirtyBitmap *bitmap) {
185 return bdrv_dirty_bitmap_frozen(bitmap) ||
186 bdrv_dirty_bitmap_qmp_locked(bitmap);
187 }
188
189 void bdrv_dirty_bitmap_set_qmp_locked(BdrvDirtyBitmap *bitmap, bool qmp_locked)
190 {
191 qemu_mutex_lock(bitmap->mutex);
192 bitmap->qmp_locked = qmp_locked;
193 qemu_mutex_unlock(bitmap->mutex);
194 }
195
196 bool bdrv_dirty_bitmap_qmp_locked(BdrvDirtyBitmap *bitmap)
197 {
198 return bitmap->qmp_locked;
199 }
200
201 /* Called with BQL taken. */
202 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
203 {
204 return !(bitmap->disabled || bitmap->successor);
205 }
206
207 /* Called with BQL taken. */
208 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
209 {
210 if (bdrv_dirty_bitmap_frozen(bitmap)) {
211 return DIRTY_BITMAP_STATUS_FROZEN;
212 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) {
213 return DIRTY_BITMAP_STATUS_LOCKED;
214 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
215 return DIRTY_BITMAP_STATUS_DISABLED;
216 } else {
217 return DIRTY_BITMAP_STATUS_ACTIVE;
218 }
219 }
220
221 /**
222 * Create a successor bitmap destined to replace this bitmap after an operation.
223 * Requires that the bitmap is not frozen and has no successor.
224 * Called with BQL taken.
225 */
226 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
227 BdrvDirtyBitmap *bitmap, Error **errp)
228 {
229 uint64_t granularity;
230 BdrvDirtyBitmap *child;
231
232 if (bdrv_dirty_bitmap_frozen(bitmap)) {
233 error_setg(errp, "Cannot create a successor for a bitmap that is "
234 "currently frozen");
235 return -1;
236 }
237 assert(!bitmap->successor);
238
239 /* Create an anonymous successor */
240 granularity = bdrv_dirty_bitmap_granularity(bitmap);
241 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
242 if (!child) {
243 return -1;
244 }
245
246 /* Successor will be on or off based on our current state. */
247 child->disabled = bitmap->disabled;
248
249 /* Install the successor and freeze the parent */
250 bitmap->successor = child;
251 return 0;
252 }
253
254 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
255 {
256 assert(!bdrv_dirty_bitmap_frozen(bitmap));
257 bitmap->disabled = false;
258 }
259
260 /* Called with BQL taken. */
261 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
262 {
263 assert(bitmap->mutex == bitmap->successor->mutex);
264 qemu_mutex_lock(bitmap->mutex);
265 bdrv_enable_dirty_bitmap_locked(bitmap->successor);
266 qemu_mutex_unlock(bitmap->mutex);
267 }
268
269 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
270 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
271 {
272 assert(!bitmap->active_iterators);
273 assert(!bdrv_dirty_bitmap_frozen(bitmap));
274 assert(!bitmap->meta);
275 QLIST_REMOVE(bitmap, list);
276 hbitmap_free(bitmap->bitmap);
277 g_free(bitmap->name);
278 g_free(bitmap);
279 }
280
281 /**
282 * For a bitmap with a successor, yield our name to the successor,
283 * delete the old bitmap, and return a handle to the new bitmap.
284 * Called with BQL taken.
285 */
286 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
287 BdrvDirtyBitmap *bitmap,
288 Error **errp)
289 {
290 char *name;
291 BdrvDirtyBitmap *successor = bitmap->successor;
292
293 if (successor == NULL) {
294 error_setg(errp, "Cannot relinquish control if "
295 "there's no successor present");
296 return NULL;
297 }
298
299 name = bitmap->name;
300 bitmap->name = NULL;
301 successor->name = name;
302 bitmap->successor = NULL;
303 successor->persistent = bitmap->persistent;
304 bitmap->persistent = false;
305 bdrv_release_dirty_bitmap(bs, bitmap);
306
307 return successor;
308 }
309
310 /**
311 * In cases of failure where we can no longer safely delete the parent,
312 * we may wish to re-join the parent and child/successor.
313 * The merged parent will be un-frozen, but not explicitly re-enabled.
314 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
315 */
316 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
317 BdrvDirtyBitmap *parent,
318 Error **errp)
319 {
320 BdrvDirtyBitmap *successor = parent->successor;
321
322 if (!successor) {
323 error_setg(errp, "Cannot reclaim a successor when none is present");
324 return NULL;
325 }
326
327 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
328 error_setg(errp, "Merging of parent and successor bitmap failed");
329 return NULL;
330 }
331 bdrv_release_dirty_bitmap_locked(successor);
332 parent->successor = NULL;
333
334 return parent;
335 }
336
337 /* Called with BQL taken. */
338 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
339 BdrvDirtyBitmap *parent,
340 Error **errp)
341 {
342 BdrvDirtyBitmap *ret;
343
344 qemu_mutex_lock(parent->mutex);
345 ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
346 qemu_mutex_unlock(parent->mutex);
347
348 return ret;
349 }
350
351 /**
352 * Truncates _all_ bitmaps attached to a BDS.
353 * Called with BQL taken.
354 */
355 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
356 {
357 BdrvDirtyBitmap *bitmap;
358
359 bdrv_dirty_bitmaps_lock(bs);
360 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
361 assert(!bdrv_dirty_bitmap_frozen(bitmap));
362 assert(!bitmap->active_iterators);
363 hbitmap_truncate(bitmap->bitmap, bytes);
364 bitmap->size = bytes;
365 }
366 bdrv_dirty_bitmaps_unlock(bs);
367 }
368
369 /* Called with BQL taken. */
370 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
371 {
372 bdrv_dirty_bitmaps_lock(bs);
373 bdrv_release_dirty_bitmap_locked(bitmap);
374 bdrv_dirty_bitmaps_unlock(bs);
375 }
376
377 /**
378 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
379 * There must not be any frozen bitmaps attached.
380 * This function does not remove persistent bitmaps from the storage.
381 * Called with BQL taken.
382 */
383 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
384 {
385 BdrvDirtyBitmap *bm, *next;
386
387 bdrv_dirty_bitmaps_lock(bs);
388 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
389 if (bdrv_dirty_bitmap_name(bm)) {
390 bdrv_release_dirty_bitmap_locked(bm);
391 }
392 }
393 bdrv_dirty_bitmaps_unlock(bs);
394 }
395
396 /**
397 * Remove persistent dirty bitmap from the storage if it exists.
398 * Absence of bitmap is not an error, because we have the following scenario:
399 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
400 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
401 * not fail.
402 * This function doesn't release corresponding BdrvDirtyBitmap.
403 */
404 void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
405 const char *name,
406 Error **errp)
407 {
408 if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
409 bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
410 }
411 }
412
413 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
414 {
415 bdrv_dirty_bitmap_lock(bitmap);
416 assert(!bdrv_dirty_bitmap_frozen(bitmap));
417 bitmap->disabled = true;
418 bdrv_dirty_bitmap_unlock(bitmap);
419 }
420
421 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
422 {
423 bdrv_dirty_bitmap_lock(bitmap);
424 bdrv_enable_dirty_bitmap_locked(bitmap);
425 bdrv_dirty_bitmap_unlock(bitmap);
426 }
427
428 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
429 {
430 BdrvDirtyBitmap *bm;
431 BlockDirtyInfoList *list = NULL;
432 BlockDirtyInfoList **plist = &list;
433
434 bdrv_dirty_bitmaps_lock(bs);
435 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
436 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
437 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
438 info->count = bdrv_get_dirty_count(bm);
439 info->granularity = bdrv_dirty_bitmap_granularity(bm);
440 info->has_name = !!bm->name;
441 info->name = g_strdup(bm->name);
442 info->status = bdrv_dirty_bitmap_status(bm);
443 info->persistent = bm->persistent;
444 entry->value = info;
445 *plist = entry;
446 plist = &entry->next;
447 }
448 bdrv_dirty_bitmaps_unlock(bs);
449
450 return list;
451 }
452
453 /* Called within bdrv_dirty_bitmap_lock..unlock */
454 bool bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
455 int64_t offset)
456 {
457 if (bitmap) {
458 return hbitmap_get(bitmap->bitmap, offset);
459 } else {
460 return false;
461 }
462 }
463
464 /**
465 * Chooses a default granularity based on the existing cluster size,
466 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
467 * is no cluster size information available.
468 */
469 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
470 {
471 BlockDriverInfo bdi;
472 uint32_t granularity;
473
474 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
475 granularity = MAX(4096, bdi.cluster_size);
476 granularity = MIN(65536, granularity);
477 } else {
478 granularity = 65536;
479 }
480
481 return granularity;
482 }
483
484 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
485 {
486 return 1U << hbitmap_granularity(bitmap->bitmap);
487 }
488
489 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
490 {
491 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
492 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
493 iter->bitmap = bitmap;
494 bitmap->active_iterators++;
495 return iter;
496 }
497
498 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
499 {
500 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
501 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
502 iter->bitmap = bitmap;
503 bitmap->active_iterators++;
504 return iter;
505 }
506
507 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
508 {
509 if (!iter) {
510 return;
511 }
512 assert(iter->bitmap->active_iterators > 0);
513 iter->bitmap->active_iterators--;
514 g_free(iter);
515 }
516
517 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
518 {
519 return hbitmap_iter_next(&iter->hbi);
520 }
521
522 /* Called within bdrv_dirty_bitmap_lock..unlock */
523 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
524 int64_t offset, int64_t bytes)
525 {
526 assert(bdrv_dirty_bitmap_enabled(bitmap));
527 assert(!bdrv_dirty_bitmap_readonly(bitmap));
528 hbitmap_set(bitmap->bitmap, offset, bytes);
529 }
530
531 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
532 int64_t offset, int64_t bytes)
533 {
534 bdrv_dirty_bitmap_lock(bitmap);
535 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
536 bdrv_dirty_bitmap_unlock(bitmap);
537 }
538
539 /* Called within bdrv_dirty_bitmap_lock..unlock */
540 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
541 int64_t offset, int64_t bytes)
542 {
543 assert(bdrv_dirty_bitmap_enabled(bitmap));
544 assert(!bdrv_dirty_bitmap_readonly(bitmap));
545 hbitmap_reset(bitmap->bitmap, offset, bytes);
546 }
547
548 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
549 int64_t offset, int64_t bytes)
550 {
551 bdrv_dirty_bitmap_lock(bitmap);
552 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
553 bdrv_dirty_bitmap_unlock(bitmap);
554 }
555
556 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
557 {
558 assert(!bdrv_dirty_bitmap_readonly(bitmap));
559 bdrv_dirty_bitmap_lock(bitmap);
560 if (!out) {
561 hbitmap_reset_all(bitmap->bitmap);
562 } else {
563 HBitmap *backup = bitmap->bitmap;
564 bitmap->bitmap = hbitmap_alloc(bitmap->size,
565 hbitmap_granularity(backup));
566 *out = backup;
567 }
568 bdrv_dirty_bitmap_unlock(bitmap);
569 }
570
571 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup)
572 {
573 HBitmap *tmp = bitmap->bitmap;
574 assert(!bdrv_dirty_bitmap_readonly(bitmap));
575 bitmap->bitmap = backup;
576 hbitmap_free(tmp);
577 }
578
579 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
580 uint64_t offset, uint64_t bytes)
581 {
582 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
583 }
584
585 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
586 {
587 return hbitmap_serialization_align(bitmap->bitmap);
588 }
589
590 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
591 uint8_t *buf, uint64_t offset,
592 uint64_t bytes)
593 {
594 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
595 }
596
597 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
598 uint8_t *buf, uint64_t offset,
599 uint64_t bytes, bool finish)
600 {
601 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
602 }
603
604 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
605 uint64_t offset, uint64_t bytes,
606 bool finish)
607 {
608 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
609 }
610
611 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
612 uint64_t offset, uint64_t bytes,
613 bool finish)
614 {
615 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
616 }
617
618 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
619 {
620 hbitmap_deserialize_finish(bitmap->bitmap);
621 }
622
623 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
624 {
625 BdrvDirtyBitmap *bitmap;
626
627 if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
628 return;
629 }
630
631 bdrv_dirty_bitmaps_lock(bs);
632 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
633 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
634 continue;
635 }
636 assert(!bdrv_dirty_bitmap_readonly(bitmap));
637 hbitmap_set(bitmap->bitmap, offset, bytes);
638 }
639 bdrv_dirty_bitmaps_unlock(bs);
640 }
641
642 /**
643 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
644 */
645 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
646 {
647 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
648 }
649
650 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
651 {
652 return hbitmap_count(bitmap->bitmap);
653 }
654
655 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
656 {
657 return hbitmap_count(bitmap->meta);
658 }
659
660 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
661 {
662 return bitmap->readonly;
663 }
664
665 /* Called with BQL taken. */
666 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
667 {
668 qemu_mutex_lock(bitmap->mutex);
669 bitmap->readonly = value;
670 qemu_mutex_unlock(bitmap->mutex);
671 }
672
673 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
674 {
675 BdrvDirtyBitmap *bm;
676 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
677 if (bm->readonly) {
678 return true;
679 }
680 }
681
682 return false;
683 }
684
685 /* Called with BQL taken. */
686 void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap *bitmap, bool persistent)
687 {
688 qemu_mutex_lock(bitmap->mutex);
689 bitmap->persistent = persistent;
690 qemu_mutex_unlock(bitmap->mutex);
691 }
692
693 /* Called with BQL taken. */
694 void bdrv_dirty_bitmap_set_migration(BdrvDirtyBitmap *bitmap, bool migration)
695 {
696 qemu_mutex_lock(bitmap->mutex);
697 bitmap->migration = migration;
698 qemu_mutex_unlock(bitmap->mutex);
699 }
700
701 bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap *bitmap)
702 {
703 return bitmap->persistent && !bitmap->migration;
704 }
705
706 bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
707 {
708 BdrvDirtyBitmap *bm;
709 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
710 if (bm->persistent && !bm->readonly && !bm->migration) {
711 return true;
712 }
713 }
714
715 return false;
716 }
717
718 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
719 BdrvDirtyBitmap *bitmap)
720 {
721 return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
722 QLIST_NEXT(bitmap, list);
723 }
724
725 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
726 {
727 return hbitmap_sha256(bitmap->bitmap, errp);
728 }
729
730 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset,
731 uint64_t bytes)
732 {
733 return hbitmap_next_zero(bitmap->bitmap, offset, bytes);
734 }
735
736 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap,
737 uint64_t *offset, uint64_t *bytes)
738 {
739 return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes);
740 }
741
742 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
743 HBitmap **backup, Error **errp)
744 {
745 bool ret;
746
747 /* only bitmaps from one bds are supported */
748 assert(dest->mutex == src->mutex);
749
750 qemu_mutex_lock(dest->mutex);
751
752 if (bdrv_dirty_bitmap_user_locked(dest)) {
753 error_setg(errp, "Bitmap '%s' is currently in use by another"
754 " operation and cannot be modified", dest->name);
755 goto out;
756 }
757
758 if (bdrv_dirty_bitmap_readonly(dest)) {
759 error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
760 dest->name);
761 goto out;
762 }
763
764 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
765 error_setg(errp, "Bitmaps are incompatible and can't be merged");
766 goto out;
767 }
768
769 if (backup) {
770 *backup = dest->bitmap;
771 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
772 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
773 } else {
774 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
775 }
776 assert(ret);
777
778 out:
779 qemu_mutex_unlock(dest->mutex);
780 }