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