4 * Copyright (c) 2016 Red Hat. Inc
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
28 #include "block/block_int.h"
29 #include "block/blockjob.h"
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().
39 struct BdrvDirtyBitmap
{
41 HBitmap
*bitmap
; /* Dirty sector bitmap implementation */
42 HBitmap
*meta
; /* Meta dirty bitmap */
43 BdrvDirtyBitmap
*successor
; /* Anonymous child; implies frozen status */
44 char *name
; /* Optional non-empty unique ID */
45 int64_t size
; /* Size of the bitmap (Number of sectors) */
46 bool disabled
; /* Bitmap is read-only */
47 int active_iterators
; /* How many iterators are active */
48 QLIST_ENTRY(BdrvDirtyBitmap
) list
;
51 struct BdrvDirtyBitmapIter
{
53 BdrvDirtyBitmap
*bitmap
;
56 static inline void bdrv_dirty_bitmaps_lock(BlockDriverState
*bs
)
58 qemu_mutex_lock(&bs
->dirty_bitmap_mutex
);
61 static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState
*bs
)
63 qemu_mutex_unlock(&bs
->dirty_bitmap_mutex
);
66 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap
*bitmap
)
68 qemu_mutex_lock(bitmap
->mutex
);
71 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap
*bitmap
)
73 qemu_mutex_unlock(bitmap
->mutex
);
76 /* Called with BQL or dirty_bitmap lock taken. */
77 BdrvDirtyBitmap
*bdrv_find_dirty_bitmap(BlockDriverState
*bs
, const char *name
)
82 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
83 if (bm
->name
&& !strcmp(name
, bm
->name
)) {
90 /* Called with BQL taken. */
91 void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap
*bitmap
)
93 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
98 /* Called with BQL taken. */
99 BdrvDirtyBitmap
*bdrv_create_dirty_bitmap(BlockDriverState
*bs
,
100 uint32_t granularity
,
105 BdrvDirtyBitmap
*bitmap
;
106 uint32_t sector_granularity
;
108 assert((granularity
& (granularity
- 1)) == 0);
110 if (name
&& bdrv_find_dirty_bitmap(bs
, name
)) {
111 error_setg(errp
, "Bitmap already exists: %s", name
);
114 sector_granularity
= granularity
>> BDRV_SECTOR_BITS
;
115 assert(sector_granularity
);
116 bitmap_size
= bdrv_nb_sectors(bs
);
117 if (bitmap_size
< 0) {
118 error_setg_errno(errp
, -bitmap_size
, "could not get length of device");
119 errno
= -bitmap_size
;
122 bitmap
= g_new0(BdrvDirtyBitmap
, 1);
123 bitmap
->mutex
= &bs
->dirty_bitmap_mutex
;
124 bitmap
->bitmap
= hbitmap_alloc(bitmap_size
, ctz32(sector_granularity
));
125 bitmap
->size
= bitmap_size
;
126 bitmap
->name
= g_strdup(name
);
127 bitmap
->disabled
= false;
128 bdrv_dirty_bitmaps_lock(bs
);
129 QLIST_INSERT_HEAD(&bs
->dirty_bitmaps
, bitmap
, list
);
130 bdrv_dirty_bitmaps_unlock(bs
);
134 /* bdrv_create_meta_dirty_bitmap
136 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
137 * when a dirty status bit in @bitmap is changed (either from reset to set or
138 * the other way around), its respective meta dirty bitmap bit will be marked
141 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
142 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
145 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
148 assert(!bitmap
->meta
);
149 qemu_mutex_lock(bitmap
->mutex
);
150 bitmap
->meta
= hbitmap_create_meta(bitmap
->bitmap
,
151 chunk_size
* BITS_PER_BYTE
);
152 qemu_mutex_unlock(bitmap
->mutex
);
155 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
157 assert(bitmap
->meta
);
158 qemu_mutex_lock(bitmap
->mutex
);
159 hbitmap_free_meta(bitmap
->bitmap
);
161 qemu_mutex_unlock(bitmap
->mutex
);
164 int bdrv_dirty_bitmap_get_meta_locked(BlockDriverState
*bs
,
165 BdrvDirtyBitmap
*bitmap
, int64_t sector
,
169 int sectors_per_bit
= 1 << hbitmap_granularity(bitmap
->meta
);
171 /* To optimize: we can make hbitmap to internally check the range in a
172 * coarse level, or at least do it word by word. */
173 for (i
= sector
; i
< sector
+ nb_sectors
; i
+= sectors_per_bit
) {
174 if (hbitmap_get(bitmap
->meta
, i
)) {
181 int bdrv_dirty_bitmap_get_meta(BlockDriverState
*bs
,
182 BdrvDirtyBitmap
*bitmap
, int64_t sector
,
187 qemu_mutex_lock(bitmap
->mutex
);
188 dirty
= bdrv_dirty_bitmap_get_meta_locked(bs
, bitmap
, sector
, nb_sectors
);
189 qemu_mutex_unlock(bitmap
->mutex
);
194 void bdrv_dirty_bitmap_reset_meta(BlockDriverState
*bs
,
195 BdrvDirtyBitmap
*bitmap
, int64_t sector
,
198 qemu_mutex_lock(bitmap
->mutex
);
199 hbitmap_reset(bitmap
->meta
, sector
, nb_sectors
);
200 qemu_mutex_unlock(bitmap
->mutex
);
203 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap
*bitmap
)
208 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap
*bitmap
)
213 /* Called with BQL taken. */
214 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap
*bitmap
)
216 return bitmap
->successor
;
219 /* Called with BQL taken. */
220 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap
*bitmap
)
222 return !(bitmap
->disabled
|| bitmap
->successor
);
225 /* Called with BQL taken. */
226 DirtyBitmapStatus
bdrv_dirty_bitmap_status(BdrvDirtyBitmap
*bitmap
)
228 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
229 return DIRTY_BITMAP_STATUS_FROZEN
;
230 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
231 return DIRTY_BITMAP_STATUS_DISABLED
;
233 return DIRTY_BITMAP_STATUS_ACTIVE
;
238 * Create a successor bitmap destined to replace this bitmap after an operation.
239 * Requires that the bitmap is not frozen and has no successor.
240 * Called with BQL taken.
242 int bdrv_dirty_bitmap_create_successor(BlockDriverState
*bs
,
243 BdrvDirtyBitmap
*bitmap
, Error
**errp
)
245 uint64_t granularity
;
246 BdrvDirtyBitmap
*child
;
248 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
249 error_setg(errp
, "Cannot create a successor for a bitmap that is "
253 assert(!bitmap
->successor
);
255 /* Create an anonymous successor */
256 granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
257 child
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
262 /* Successor will be on or off based on our current state. */
263 child
->disabled
= bitmap
->disabled
;
265 /* Install the successor and freeze the parent */
266 bitmap
->successor
= child
;
271 * For a bitmap with a successor, yield our name to the successor,
272 * delete the old bitmap, and return a handle to the new bitmap.
273 * Called with BQL taken.
275 BdrvDirtyBitmap
*bdrv_dirty_bitmap_abdicate(BlockDriverState
*bs
,
276 BdrvDirtyBitmap
*bitmap
,
280 BdrvDirtyBitmap
*successor
= bitmap
->successor
;
282 if (successor
== NULL
) {
283 error_setg(errp
, "Cannot relinquish control if "
284 "there's no successor present");
290 successor
->name
= name
;
291 bitmap
->successor
= NULL
;
292 bdrv_release_dirty_bitmap(bs
, bitmap
);
298 * In cases of failure where we can no longer safely delete the parent,
299 * we may wish to re-join the parent and child/successor.
300 * The merged parent will be un-frozen, but not explicitly re-enabled.
301 * Called with BQL taken.
303 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap(BlockDriverState
*bs
,
304 BdrvDirtyBitmap
*parent
,
307 BdrvDirtyBitmap
*successor
= parent
->successor
;
310 error_setg(errp
, "Cannot reclaim a successor when none is present");
314 if (!hbitmap_merge(parent
->bitmap
, successor
->bitmap
)) {
315 error_setg(errp
, "Merging of parent and successor bitmap failed");
318 bdrv_release_dirty_bitmap(bs
, successor
);
319 parent
->successor
= NULL
;
325 * Truncates _all_ bitmaps attached to a BDS.
326 * Called with BQL taken.
328 void bdrv_dirty_bitmap_truncate(BlockDriverState
*bs
)
330 BdrvDirtyBitmap
*bitmap
;
331 uint64_t size
= bdrv_nb_sectors(bs
);
333 bdrv_dirty_bitmaps_lock(bs
);
334 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
335 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
336 assert(!bitmap
->active_iterators
);
337 hbitmap_truncate(bitmap
->bitmap
, size
);
340 bdrv_dirty_bitmaps_unlock(bs
);
343 /* Called with BQL taken. */
344 static void bdrv_do_release_matching_dirty_bitmap(BlockDriverState
*bs
,
345 BdrvDirtyBitmap
*bitmap
,
348 BdrvDirtyBitmap
*bm
, *next
;
349 bdrv_dirty_bitmaps_lock(bs
);
350 QLIST_FOREACH_SAFE(bm
, &bs
->dirty_bitmaps
, list
, next
) {
351 if ((!bitmap
|| bm
== bitmap
) && (!only_named
|| bm
->name
)) {
352 assert(!bm
->active_iterators
);
353 assert(!bdrv_dirty_bitmap_frozen(bm
));
355 QLIST_REMOVE(bm
, list
);
356 hbitmap_free(bm
->bitmap
);
370 bdrv_dirty_bitmaps_unlock(bs
);
373 /* Called with BQL taken. */
374 void bdrv_release_dirty_bitmap(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
)
376 bdrv_do_release_matching_dirty_bitmap(bs
, bitmap
, false);
380 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
381 * There must not be any frozen bitmaps attached.
382 * Called with BQL taken.
384 void bdrv_release_named_dirty_bitmaps(BlockDriverState
*bs
)
386 bdrv_do_release_matching_dirty_bitmap(bs
, NULL
, true);
389 /* Called with BQL taken. */
390 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
392 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
393 bitmap
->disabled
= true;
396 /* Called with BQL taken. */
397 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
399 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
400 bitmap
->disabled
= false;
403 BlockDirtyInfoList
*bdrv_query_dirty_bitmaps(BlockDriverState
*bs
)
406 BlockDirtyInfoList
*list
= NULL
;
407 BlockDirtyInfoList
**plist
= &list
;
409 bdrv_dirty_bitmaps_lock(bs
);
410 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
411 BlockDirtyInfo
*info
= g_new0(BlockDirtyInfo
, 1);
412 BlockDirtyInfoList
*entry
= g_new0(BlockDirtyInfoList
, 1);
413 info
->count
= bdrv_get_dirty_count(bm
);
414 info
->granularity
= bdrv_dirty_bitmap_granularity(bm
);
415 info
->has_name
= !!bm
->name
;
416 info
->name
= g_strdup(bm
->name
);
417 info
->status
= bdrv_dirty_bitmap_status(bm
);
420 plist
= &entry
->next
;
422 bdrv_dirty_bitmaps_unlock(bs
);
427 /* Called within bdrv_dirty_bitmap_lock..unlock */
428 int bdrv_get_dirty_locked(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
,
432 return hbitmap_get(bitmap
->bitmap
, sector
);
439 * Chooses a default granularity based on the existing cluster size,
440 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
441 * is no cluster size information available.
443 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState
*bs
)
446 uint32_t granularity
;
448 if (bdrv_get_info(bs
, &bdi
) >= 0 && bdi
.cluster_size
> 0) {
449 granularity
= MAX(4096, bdi
.cluster_size
);
450 granularity
= MIN(65536, granularity
);
458 uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap
*bitmap
)
460 return BDRV_SECTOR_SIZE
<< hbitmap_granularity(bitmap
->bitmap
);
463 uint32_t bdrv_dirty_bitmap_meta_granularity(BdrvDirtyBitmap
*bitmap
)
465 return BDRV_SECTOR_SIZE
<< hbitmap_granularity(bitmap
->meta
);
468 BdrvDirtyBitmapIter
*bdrv_dirty_iter_new(BdrvDirtyBitmap
*bitmap
,
469 uint64_t first_sector
)
471 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
472 hbitmap_iter_init(&iter
->hbi
, bitmap
->bitmap
, first_sector
);
473 iter
->bitmap
= bitmap
;
474 bitmap
->active_iterators
++;
478 BdrvDirtyBitmapIter
*bdrv_dirty_meta_iter_new(BdrvDirtyBitmap
*bitmap
)
480 BdrvDirtyBitmapIter
*iter
= g_new(BdrvDirtyBitmapIter
, 1);
481 hbitmap_iter_init(&iter
->hbi
, bitmap
->meta
, 0);
482 iter
->bitmap
= bitmap
;
483 bitmap
->active_iterators
++;
487 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter
*iter
)
492 assert(iter
->bitmap
->active_iterators
> 0);
493 iter
->bitmap
->active_iterators
--;
497 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter
*iter
)
499 return hbitmap_iter_next(&iter
->hbi
);
502 /* Called within bdrv_dirty_bitmap_lock..unlock */
503 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
,
504 int64_t cur_sector
, int64_t nr_sectors
)
506 assert(bdrv_dirty_bitmap_enabled(bitmap
));
507 hbitmap_set(bitmap
->bitmap
, cur_sector
, nr_sectors
);
510 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
511 int64_t cur_sector
, int64_t nr_sectors
)
513 bdrv_dirty_bitmap_lock(bitmap
);
514 bdrv_set_dirty_bitmap_locked(bitmap
, cur_sector
, nr_sectors
);
515 bdrv_dirty_bitmap_unlock(bitmap
);
518 /* Called within bdrv_dirty_bitmap_lock..unlock */
519 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap
*bitmap
,
520 int64_t cur_sector
, int64_t nr_sectors
)
522 assert(bdrv_dirty_bitmap_enabled(bitmap
));
523 hbitmap_reset(bitmap
->bitmap
, cur_sector
, nr_sectors
);
526 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
527 int64_t cur_sector
, int64_t nr_sectors
)
529 bdrv_dirty_bitmap_lock(bitmap
);
530 bdrv_reset_dirty_bitmap_locked(bitmap
, cur_sector
, nr_sectors
);
531 bdrv_dirty_bitmap_unlock(bitmap
);
534 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
**out
)
536 assert(bdrv_dirty_bitmap_enabled(bitmap
));
537 bdrv_dirty_bitmap_lock(bitmap
);
539 hbitmap_reset_all(bitmap
->bitmap
);
541 HBitmap
*backup
= bitmap
->bitmap
;
542 bitmap
->bitmap
= hbitmap_alloc(bitmap
->size
,
543 hbitmap_granularity(backup
));
546 bdrv_dirty_bitmap_unlock(bitmap
);
549 void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
*in
)
551 HBitmap
*tmp
= bitmap
->bitmap
;
552 assert(bdrv_dirty_bitmap_enabled(bitmap
));
557 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap
*bitmap
,
558 uint64_t start
, uint64_t count
)
560 return hbitmap_serialization_size(bitmap
->bitmap
, start
, count
);
563 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap
*bitmap
)
565 return hbitmap_serialization_granularity(bitmap
->bitmap
);
568 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap
*bitmap
,
569 uint8_t *buf
, uint64_t start
,
572 hbitmap_serialize_part(bitmap
->bitmap
, buf
, start
, count
);
575 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap
*bitmap
,
576 uint8_t *buf
, uint64_t start
,
577 uint64_t count
, bool finish
)
579 hbitmap_deserialize_part(bitmap
->bitmap
, buf
, start
, count
, finish
);
582 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap
*bitmap
,
583 uint64_t start
, uint64_t count
,
586 hbitmap_deserialize_zeroes(bitmap
->bitmap
, start
, count
, finish
);
589 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap
*bitmap
)
591 hbitmap_deserialize_finish(bitmap
->bitmap
);
594 void bdrv_set_dirty(BlockDriverState
*bs
, int64_t cur_sector
,
597 BdrvDirtyBitmap
*bitmap
;
599 if (QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
603 bdrv_dirty_bitmaps_lock(bs
);
604 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
605 if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
608 hbitmap_set(bitmap
->bitmap
, cur_sector
, nr_sectors
);
610 bdrv_dirty_bitmaps_unlock(bs
);
614 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
616 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter
*iter
, int64_t sector_num
)
618 hbitmap_iter_init(&iter
->hbi
, iter
->hbi
.hb
, sector_num
);
621 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap
*bitmap
)
623 return hbitmap_count(bitmap
->bitmap
);
626 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap
*bitmap
)
628 return hbitmap_count(bitmap
->meta
);