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
{
40 HBitmap
*bitmap
; /* Dirty sector bitmap implementation */
41 BdrvDirtyBitmap
*successor
; /* Anonymous child; implies frozen status */
42 char *name
; /* Optional non-empty unique ID */
43 int64_t size
; /* Size of the bitmap (Number of sectors) */
44 bool disabled
; /* Bitmap is read-only */
45 QLIST_ENTRY(BdrvDirtyBitmap
) list
;
48 BdrvDirtyBitmap
*bdrv_find_dirty_bitmap(BlockDriverState
*bs
, const char *name
)
53 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
54 if (bm
->name
&& !strcmp(name
, bm
->name
)) {
61 void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap
*bitmap
)
63 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
68 BdrvDirtyBitmap
*bdrv_create_dirty_bitmap(BlockDriverState
*bs
,
74 BdrvDirtyBitmap
*bitmap
;
75 uint32_t sector_granularity
;
77 assert((granularity
& (granularity
- 1)) == 0);
79 if (name
&& bdrv_find_dirty_bitmap(bs
, name
)) {
80 error_setg(errp
, "Bitmap already exists: %s", name
);
83 sector_granularity
= granularity
>> BDRV_SECTOR_BITS
;
84 assert(sector_granularity
);
85 bitmap_size
= bdrv_nb_sectors(bs
);
86 if (bitmap_size
< 0) {
87 error_setg_errno(errp
, -bitmap_size
, "could not get length of device");
91 bitmap
= g_new0(BdrvDirtyBitmap
, 1);
92 bitmap
->bitmap
= hbitmap_alloc(bitmap_size
, ctz32(sector_granularity
));
93 bitmap
->size
= bitmap_size
;
94 bitmap
->name
= g_strdup(name
);
95 bitmap
->disabled
= false;
96 QLIST_INSERT_HEAD(&bs
->dirty_bitmaps
, bitmap
, list
);
100 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap
*bitmap
)
102 return bitmap
->successor
;
105 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap
*bitmap
)
107 return !(bitmap
->disabled
|| bitmap
->successor
);
110 DirtyBitmapStatus
bdrv_dirty_bitmap_status(BdrvDirtyBitmap
*bitmap
)
112 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
113 return DIRTY_BITMAP_STATUS_FROZEN
;
114 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
115 return DIRTY_BITMAP_STATUS_DISABLED
;
117 return DIRTY_BITMAP_STATUS_ACTIVE
;
122 * Create a successor bitmap destined to replace this bitmap after an operation.
123 * Requires that the bitmap is not frozen and has no successor.
125 int bdrv_dirty_bitmap_create_successor(BlockDriverState
*bs
,
126 BdrvDirtyBitmap
*bitmap
, Error
**errp
)
128 uint64_t granularity
;
129 BdrvDirtyBitmap
*child
;
131 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
132 error_setg(errp
, "Cannot create a successor for a bitmap that is "
136 assert(!bitmap
->successor
);
138 /* Create an anonymous successor */
139 granularity
= bdrv_dirty_bitmap_granularity(bitmap
);
140 child
= bdrv_create_dirty_bitmap(bs
, granularity
, NULL
, errp
);
145 /* Successor will be on or off based on our current state. */
146 child
->disabled
= bitmap
->disabled
;
148 /* Install the successor and freeze the parent */
149 bitmap
->successor
= child
;
154 * For a bitmap with a successor, yield our name to the successor,
155 * delete the old bitmap, and return a handle to the new bitmap.
157 BdrvDirtyBitmap
*bdrv_dirty_bitmap_abdicate(BlockDriverState
*bs
,
158 BdrvDirtyBitmap
*bitmap
,
162 BdrvDirtyBitmap
*successor
= bitmap
->successor
;
164 if (successor
== NULL
) {
165 error_setg(errp
, "Cannot relinquish control if "
166 "there's no successor present");
172 successor
->name
= name
;
173 bitmap
->successor
= NULL
;
174 bdrv_release_dirty_bitmap(bs
, bitmap
);
180 * In cases of failure where we can no longer safely delete the parent,
181 * we may wish to re-join the parent and child/successor.
182 * The merged parent will be un-frozen, but not explicitly re-enabled.
184 BdrvDirtyBitmap
*bdrv_reclaim_dirty_bitmap(BlockDriverState
*bs
,
185 BdrvDirtyBitmap
*parent
,
188 BdrvDirtyBitmap
*successor
= parent
->successor
;
191 error_setg(errp
, "Cannot reclaim a successor when none is present");
195 if (!hbitmap_merge(parent
->bitmap
, successor
->bitmap
)) {
196 error_setg(errp
, "Merging of parent and successor bitmap failed");
199 bdrv_release_dirty_bitmap(bs
, successor
);
200 parent
->successor
= NULL
;
206 * Truncates _all_ bitmaps attached to a BDS.
208 void bdrv_dirty_bitmap_truncate(BlockDriverState
*bs
)
210 BdrvDirtyBitmap
*bitmap
;
211 uint64_t size
= bdrv_nb_sectors(bs
);
213 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
214 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
215 hbitmap_truncate(bitmap
->bitmap
, size
);
220 static void bdrv_do_release_matching_dirty_bitmap(BlockDriverState
*bs
,
221 BdrvDirtyBitmap
*bitmap
,
224 BdrvDirtyBitmap
*bm
, *next
;
225 QLIST_FOREACH_SAFE(bm
, &bs
->dirty_bitmaps
, list
, next
) {
226 if ((!bitmap
|| bm
== bitmap
) && (!only_named
|| bm
->name
)) {
227 assert(!bdrv_dirty_bitmap_frozen(bm
));
228 QLIST_REMOVE(bm
, list
);
229 hbitmap_free(bm
->bitmap
);
240 void bdrv_release_dirty_bitmap(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
)
242 bdrv_do_release_matching_dirty_bitmap(bs
, bitmap
, false);
246 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
247 * There must not be any frozen bitmaps attached.
249 void bdrv_release_named_dirty_bitmaps(BlockDriverState
*bs
)
251 bdrv_do_release_matching_dirty_bitmap(bs
, NULL
, true);
254 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
256 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
257 bitmap
->disabled
= true;
260 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap
*bitmap
)
262 assert(!bdrv_dirty_bitmap_frozen(bitmap
));
263 bitmap
->disabled
= false;
266 BlockDirtyInfoList
*bdrv_query_dirty_bitmaps(BlockDriverState
*bs
)
269 BlockDirtyInfoList
*list
= NULL
;
270 BlockDirtyInfoList
**plist
= &list
;
272 QLIST_FOREACH(bm
, &bs
->dirty_bitmaps
, list
) {
273 BlockDirtyInfo
*info
= g_new0(BlockDirtyInfo
, 1);
274 BlockDirtyInfoList
*entry
= g_new0(BlockDirtyInfoList
, 1);
275 info
->count
= bdrv_get_dirty_count(bm
);
276 info
->granularity
= bdrv_dirty_bitmap_granularity(bm
);
277 info
->has_name
= !!bm
->name
;
278 info
->name
= g_strdup(bm
->name
);
279 info
->status
= bdrv_dirty_bitmap_status(bm
);
282 plist
= &entry
->next
;
288 int bdrv_get_dirty(BlockDriverState
*bs
, BdrvDirtyBitmap
*bitmap
,
292 return hbitmap_get(bitmap
->bitmap
, sector
);
299 * Chooses a default granularity based on the existing cluster size,
300 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
301 * is no cluster size information available.
303 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState
*bs
)
306 uint32_t granularity
;
308 if (bdrv_get_info(bs
, &bdi
) >= 0 && bdi
.cluster_size
> 0) {
309 granularity
= MAX(4096, bdi
.cluster_size
);
310 granularity
= MIN(65536, granularity
);
318 uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap
*bitmap
)
320 return BDRV_SECTOR_SIZE
<< hbitmap_granularity(bitmap
->bitmap
);
323 void bdrv_dirty_iter_init(BdrvDirtyBitmap
*bitmap
, HBitmapIter
*hbi
)
325 hbitmap_iter_init(hbi
, bitmap
->bitmap
, 0);
328 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
329 int64_t cur_sector
, int64_t nr_sectors
)
331 assert(bdrv_dirty_bitmap_enabled(bitmap
));
332 hbitmap_set(bitmap
->bitmap
, cur_sector
, nr_sectors
);
335 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap
*bitmap
,
336 int64_t cur_sector
, int64_t nr_sectors
)
338 assert(bdrv_dirty_bitmap_enabled(bitmap
));
339 hbitmap_reset(bitmap
->bitmap
, cur_sector
, nr_sectors
);
342 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
**out
)
344 assert(bdrv_dirty_bitmap_enabled(bitmap
));
346 hbitmap_reset_all(bitmap
->bitmap
);
348 HBitmap
*backup
= bitmap
->bitmap
;
349 bitmap
->bitmap
= hbitmap_alloc(bitmap
->size
,
350 hbitmap_granularity(backup
));
355 void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
*in
)
357 HBitmap
*tmp
= bitmap
->bitmap
;
358 assert(bdrv_dirty_bitmap_enabled(bitmap
));
363 void bdrv_set_dirty(BlockDriverState
*bs
, int64_t cur_sector
,
366 BdrvDirtyBitmap
*bitmap
;
367 QLIST_FOREACH(bitmap
, &bs
->dirty_bitmaps
, list
) {
368 if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
371 hbitmap_set(bitmap
->bitmap
, cur_sector
, nr_sectors
);
376 * Advance an HBitmapIter to an arbitrary offset.
378 void bdrv_set_dirty_iter(HBitmapIter
*hbi
, int64_t offset
)
381 hbitmap_iter_init(hbi
, hbi
->hb
, offset
);
384 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap
*bitmap
)
386 return hbitmap_count(bitmap
->bitmap
);