disas/cris: Avoid unintended sign extension
[qemu/ar7.git] / block / dirty-bitmap.c
blob519737c8d390c43014aa840390c70490749e265c
1 /*
2 * Block Dirty Bitmap
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
22 * THE SOFTWARE.
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"
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().
39 struct BdrvDirtyBitmap {
40 HBitmap *bitmap; /* Dirty sector bitmap implementation */
41 HBitmap *meta; /* Meta dirty bitmap */
42 BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */
43 char *name; /* Optional non-empty unique ID */
44 int64_t size; /* Size of the bitmap (Number of sectors) */
45 bool disabled; /* Bitmap is read-only */
46 int active_iterators; /* How many iterators are active */
47 QLIST_ENTRY(BdrvDirtyBitmap) list;
50 struct BdrvDirtyBitmapIter {
51 HBitmapIter hbi;
52 BdrvDirtyBitmap *bitmap;
55 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
57 BdrvDirtyBitmap *bm;
59 assert(name);
60 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
61 if (bm->name && !strcmp(name, bm->name)) {
62 return bm;
65 return NULL;
68 void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap)
70 assert(!bdrv_dirty_bitmap_frozen(bitmap));
71 g_free(bitmap->name);
72 bitmap->name = NULL;
75 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
76 uint32_t granularity,
77 const char *name,
78 Error **errp)
80 int64_t bitmap_size;
81 BdrvDirtyBitmap *bitmap;
82 uint32_t sector_granularity;
84 assert((granularity & (granularity - 1)) == 0);
86 if (name && bdrv_find_dirty_bitmap(bs, name)) {
87 error_setg(errp, "Bitmap already exists: %s", name);
88 return NULL;
90 sector_granularity = granularity >> BDRV_SECTOR_BITS;
91 assert(sector_granularity);
92 bitmap_size = bdrv_nb_sectors(bs);
93 if (bitmap_size < 0) {
94 error_setg_errno(errp, -bitmap_size, "could not get length of device");
95 errno = -bitmap_size;
96 return NULL;
98 bitmap = g_new0(BdrvDirtyBitmap, 1);
99 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(sector_granularity));
100 bitmap->size = bitmap_size;
101 bitmap->name = g_strdup(name);
102 bitmap->disabled = false;
103 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
104 return bitmap;
107 /* bdrv_create_meta_dirty_bitmap
109 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
110 * when a dirty status bit in @bitmap is changed (either from reset to set or
111 * the other way around), its respective meta dirty bitmap bit will be marked
112 * dirty as well.
114 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
115 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
116 * track.
118 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
119 int chunk_size)
121 assert(!bitmap->meta);
122 bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
123 chunk_size * BITS_PER_BYTE);
126 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
128 assert(bitmap->meta);
129 hbitmap_free_meta(bitmap->bitmap);
130 bitmap->meta = NULL;
133 int bdrv_dirty_bitmap_get_meta(BlockDriverState *bs,
134 BdrvDirtyBitmap *bitmap, int64_t sector,
135 int nb_sectors)
137 uint64_t i;
138 int sectors_per_bit = 1 << hbitmap_granularity(bitmap->meta);
140 /* To optimize: we can make hbitmap to internally check the range in a
141 * coarse level, or at least do it word by word. */
142 for (i = sector; i < sector + nb_sectors; i += sectors_per_bit) {
143 if (hbitmap_get(bitmap->meta, i)) {
144 return true;
147 return false;
150 void bdrv_dirty_bitmap_reset_meta(BlockDriverState *bs,
151 BdrvDirtyBitmap *bitmap, int64_t sector,
152 int nb_sectors)
154 hbitmap_reset(bitmap->meta, sector, nb_sectors);
157 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
159 return bitmap->size;
162 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
164 return bitmap->name;
167 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
169 return bitmap->successor;
172 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
174 return !(bitmap->disabled || bitmap->successor);
177 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
179 if (bdrv_dirty_bitmap_frozen(bitmap)) {
180 return DIRTY_BITMAP_STATUS_FROZEN;
181 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
182 return DIRTY_BITMAP_STATUS_DISABLED;
183 } else {
184 return DIRTY_BITMAP_STATUS_ACTIVE;
189 * Create a successor bitmap destined to replace this bitmap after an operation.
190 * Requires that the bitmap is not frozen and has no successor.
192 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
193 BdrvDirtyBitmap *bitmap, Error **errp)
195 uint64_t granularity;
196 BdrvDirtyBitmap *child;
198 if (bdrv_dirty_bitmap_frozen(bitmap)) {
199 error_setg(errp, "Cannot create a successor for a bitmap that is "
200 "currently frozen");
201 return -1;
203 assert(!bitmap->successor);
205 /* Create an anonymous successor */
206 granularity = bdrv_dirty_bitmap_granularity(bitmap);
207 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
208 if (!child) {
209 return -1;
212 /* Successor will be on or off based on our current state. */
213 child->disabled = bitmap->disabled;
215 /* Install the successor and freeze the parent */
216 bitmap->successor = child;
217 return 0;
221 * For a bitmap with a successor, yield our name to the successor,
222 * delete the old bitmap, and return a handle to the new bitmap.
224 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
225 BdrvDirtyBitmap *bitmap,
226 Error **errp)
228 char *name;
229 BdrvDirtyBitmap *successor = bitmap->successor;
231 if (successor == NULL) {
232 error_setg(errp, "Cannot relinquish control if "
233 "there's no successor present");
234 return NULL;
237 name = bitmap->name;
238 bitmap->name = NULL;
239 successor->name = name;
240 bitmap->successor = NULL;
241 bdrv_release_dirty_bitmap(bs, bitmap);
243 return successor;
247 * In cases of failure where we can no longer safely delete the parent,
248 * we may wish to re-join the parent and child/successor.
249 * The merged parent will be un-frozen, but not explicitly re-enabled.
251 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
252 BdrvDirtyBitmap *parent,
253 Error **errp)
255 BdrvDirtyBitmap *successor = parent->successor;
257 if (!successor) {
258 error_setg(errp, "Cannot reclaim a successor when none is present");
259 return NULL;
262 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
263 error_setg(errp, "Merging of parent and successor bitmap failed");
264 return NULL;
266 bdrv_release_dirty_bitmap(bs, successor);
267 parent->successor = NULL;
269 return parent;
273 * Truncates _all_ bitmaps attached to a BDS.
275 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs)
277 BdrvDirtyBitmap *bitmap;
278 uint64_t size = bdrv_nb_sectors(bs);
280 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
281 assert(!bdrv_dirty_bitmap_frozen(bitmap));
282 assert(!bitmap->active_iterators);
283 hbitmap_truncate(bitmap->bitmap, size);
284 bitmap->size = size;
288 static void bdrv_do_release_matching_dirty_bitmap(BlockDriverState *bs,
289 BdrvDirtyBitmap *bitmap,
290 bool only_named)
292 BdrvDirtyBitmap *bm, *next;
293 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
294 if ((!bitmap || bm == bitmap) && (!only_named || bm->name)) {
295 assert(!bm->active_iterators);
296 assert(!bdrv_dirty_bitmap_frozen(bm));
297 assert(!bm->meta);
298 QLIST_REMOVE(bm, list);
299 hbitmap_free(bm->bitmap);
300 g_free(bm->name);
301 g_free(bm);
303 if (bitmap) {
304 return;
308 if (bitmap) {
309 abort();
313 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
315 bdrv_do_release_matching_dirty_bitmap(bs, bitmap, false);
319 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
320 * There must not be any frozen bitmaps attached.
322 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
324 bdrv_do_release_matching_dirty_bitmap(bs, NULL, true);
327 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
329 assert(!bdrv_dirty_bitmap_frozen(bitmap));
330 bitmap->disabled = true;
333 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
335 assert(!bdrv_dirty_bitmap_frozen(bitmap));
336 bitmap->disabled = false;
339 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
341 BdrvDirtyBitmap *bm;
342 BlockDirtyInfoList *list = NULL;
343 BlockDirtyInfoList **plist = &list;
345 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
346 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
347 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
348 info->count = bdrv_get_dirty_count(bm);
349 info->granularity = bdrv_dirty_bitmap_granularity(bm);
350 info->has_name = !!bm->name;
351 info->name = g_strdup(bm->name);
352 info->status = bdrv_dirty_bitmap_status(bm);
353 entry->value = info;
354 *plist = entry;
355 plist = &entry->next;
358 return list;
361 int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
362 int64_t sector)
364 if (bitmap) {
365 return hbitmap_get(bitmap->bitmap, sector);
366 } else {
367 return 0;
372 * Chooses a default granularity based on the existing cluster size,
373 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
374 * is no cluster size information available.
376 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
378 BlockDriverInfo bdi;
379 uint32_t granularity;
381 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
382 granularity = MAX(4096, bdi.cluster_size);
383 granularity = MIN(65536, granularity);
384 } else {
385 granularity = 65536;
388 return granularity;
391 uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap)
393 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap);
396 uint32_t bdrv_dirty_bitmap_meta_granularity(BdrvDirtyBitmap *bitmap)
398 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->meta);
401 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap,
402 uint64_t first_sector)
404 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
405 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, first_sector);
406 iter->bitmap = bitmap;
407 bitmap->active_iterators++;
408 return iter;
411 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
413 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
414 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
415 iter->bitmap = bitmap;
416 bitmap->active_iterators++;
417 return iter;
420 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
422 if (!iter) {
423 return;
425 assert(iter->bitmap->active_iterators > 0);
426 iter->bitmap->active_iterators--;
427 g_free(iter);
430 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
432 return hbitmap_iter_next(&iter->hbi);
435 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
436 int64_t cur_sector, int64_t nr_sectors)
438 assert(bdrv_dirty_bitmap_enabled(bitmap));
439 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
442 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
443 int64_t cur_sector, int64_t nr_sectors)
445 assert(bdrv_dirty_bitmap_enabled(bitmap));
446 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors);
449 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
451 assert(bdrv_dirty_bitmap_enabled(bitmap));
452 if (!out) {
453 hbitmap_reset_all(bitmap->bitmap);
454 } else {
455 HBitmap *backup = bitmap->bitmap;
456 bitmap->bitmap = hbitmap_alloc(bitmap->size,
457 hbitmap_granularity(backup));
458 *out = backup;
462 void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
464 HBitmap *tmp = bitmap->bitmap;
465 assert(bdrv_dirty_bitmap_enabled(bitmap));
466 bitmap->bitmap = in;
467 hbitmap_free(tmp);
470 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
471 uint64_t start, uint64_t count)
473 return hbitmap_serialization_size(bitmap->bitmap, start, count);
476 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
478 return hbitmap_serialization_granularity(bitmap->bitmap);
481 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
482 uint8_t *buf, uint64_t start,
483 uint64_t count)
485 hbitmap_serialize_part(bitmap->bitmap, buf, start, count);
488 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
489 uint8_t *buf, uint64_t start,
490 uint64_t count, bool finish)
492 hbitmap_deserialize_part(bitmap->bitmap, buf, start, count, finish);
495 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
496 uint64_t start, uint64_t count,
497 bool finish)
499 hbitmap_deserialize_zeroes(bitmap->bitmap, start, count, finish);
502 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
504 hbitmap_deserialize_finish(bitmap->bitmap);
507 void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
508 int64_t nr_sectors)
510 BdrvDirtyBitmap *bitmap;
511 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
512 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
513 continue;
515 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
520 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
522 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t sector_num)
524 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, sector_num);
527 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
529 return hbitmap_count(bitmap->bitmap);
532 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
534 return hbitmap_count(bitmap->meta);