]> git.proxmox.com Git - mirror_qemu.git/blame - block/dirty-bitmap.c
block: Support meta dirty bitmap
[mirror_qemu.git] / block / dirty-bitmap.c
CommitLineData
ebab2259
FZ
1/*
2 * Block Dirty Bitmap
3 *
4 * Copyright (c) 2016 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"
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 {
40 HBitmap *bitmap; /* Dirty sector bitmap implementation */
fb933437 41 HBitmap *meta; /* Meta dirty bitmap */
ebab2259
FZ
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 */
dc162c8e 46 int active_iterators; /* How many iterators are active */
ebab2259
FZ
47 QLIST_ENTRY(BdrvDirtyBitmap) list;
48};
49
dc162c8e
FZ
50struct BdrvDirtyBitmapIter {
51 HBitmapIter hbi;
52 BdrvDirtyBitmap *bitmap;
53};
54
ebab2259
FZ
55BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
56{
57 BdrvDirtyBitmap *bm;
58
59 assert(name);
60 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
61 if (bm->name && !strcmp(name, bm->name)) {
62 return bm;
63 }
64 }
65 return NULL;
66}
67
68void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap)
69{
70 assert(!bdrv_dirty_bitmap_frozen(bitmap));
71 g_free(bitmap->name);
72 bitmap->name = NULL;
73}
74
75BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
76 uint32_t granularity,
77 const char *name,
78 Error **errp)
79{
80 int64_t bitmap_size;
81 BdrvDirtyBitmap *bitmap;
82 uint32_t sector_granularity;
83
84 assert((granularity & (granularity - 1)) == 0);
85
86 if (name && bdrv_find_dirty_bitmap(bs, name)) {
87 error_setg(errp, "Bitmap already exists: %s", name);
88 return NULL;
89 }
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;
97 }
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;
105}
106
fb933437
FZ
107/* bdrv_create_meta_dirty_bitmap
108 *
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.
113 *
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.
117 */
118void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
119 int chunk_size)
120{
121 assert(!bitmap->meta);
122 bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
123 chunk_size * BITS_PER_BYTE);
124}
125
126void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
127{
128 assert(bitmap->meta);
129 hbitmap_free_meta(bitmap->bitmap);
130 bitmap->meta = NULL;
131}
132
133int bdrv_dirty_bitmap_get_meta(BlockDriverState *bs,
134 BdrvDirtyBitmap *bitmap, int64_t sector,
135 int nb_sectors)
136{
137 uint64_t i;
138 int sectors_per_bit = 1 << hbitmap_granularity(bitmap->meta);
139
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;
145 }
146 }
147 return false;
148}
149
150void bdrv_dirty_bitmap_reset_meta(BlockDriverState *bs,
151 BdrvDirtyBitmap *bitmap, int64_t sector,
152 int nb_sectors)
153{
154 hbitmap_reset(bitmap->meta, sector, nb_sectors);
155}
156
ebab2259
FZ
157bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
158{
159 return bitmap->successor;
160}
161
162bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
163{
164 return !(bitmap->disabled || bitmap->successor);
165}
166
167DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
168{
169 if (bdrv_dirty_bitmap_frozen(bitmap)) {
170 return DIRTY_BITMAP_STATUS_FROZEN;
171 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
172 return DIRTY_BITMAP_STATUS_DISABLED;
173 } else {
174 return DIRTY_BITMAP_STATUS_ACTIVE;
175 }
176}
177
178/**
179 * Create a successor bitmap destined to replace this bitmap after an operation.
180 * Requires that the bitmap is not frozen and has no successor.
181 */
182int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
183 BdrvDirtyBitmap *bitmap, Error **errp)
184{
185 uint64_t granularity;
186 BdrvDirtyBitmap *child;
187
188 if (bdrv_dirty_bitmap_frozen(bitmap)) {
189 error_setg(errp, "Cannot create a successor for a bitmap that is "
190 "currently frozen");
191 return -1;
192 }
193 assert(!bitmap->successor);
194
195 /* Create an anonymous successor */
196 granularity = bdrv_dirty_bitmap_granularity(bitmap);
197 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
198 if (!child) {
199 return -1;
200 }
201
202 /* Successor will be on or off based on our current state. */
203 child->disabled = bitmap->disabled;
204
205 /* Install the successor and freeze the parent */
206 bitmap->successor = child;
207 return 0;
208}
209
210/**
211 * For a bitmap with a successor, yield our name to the successor,
212 * delete the old bitmap, and return a handle to the new bitmap.
213 */
214BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
215 BdrvDirtyBitmap *bitmap,
216 Error **errp)
217{
218 char *name;
219 BdrvDirtyBitmap *successor = bitmap->successor;
220
221 if (successor == NULL) {
222 error_setg(errp, "Cannot relinquish control if "
223 "there's no successor present");
224 return NULL;
225 }
226
227 name = bitmap->name;
228 bitmap->name = NULL;
229 successor->name = name;
230 bitmap->successor = NULL;
231 bdrv_release_dirty_bitmap(bs, bitmap);
232
233 return successor;
234}
235
236/**
237 * In cases of failure where we can no longer safely delete the parent,
238 * we may wish to re-join the parent and child/successor.
239 * The merged parent will be un-frozen, but not explicitly re-enabled.
240 */
241BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
242 BdrvDirtyBitmap *parent,
243 Error **errp)
244{
245 BdrvDirtyBitmap *successor = parent->successor;
246
247 if (!successor) {
248 error_setg(errp, "Cannot reclaim a successor when none is present");
249 return NULL;
250 }
251
252 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
253 error_setg(errp, "Merging of parent and successor bitmap failed");
254 return NULL;
255 }
256 bdrv_release_dirty_bitmap(bs, successor);
257 parent->successor = NULL;
258
259 return parent;
260}
261
262/**
263 * Truncates _all_ bitmaps attached to a BDS.
264 */
265void bdrv_dirty_bitmap_truncate(BlockDriverState *bs)
266{
267 BdrvDirtyBitmap *bitmap;
268 uint64_t size = bdrv_nb_sectors(bs);
269
270 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
271 assert(!bdrv_dirty_bitmap_frozen(bitmap));
dc162c8e 272 assert(!bitmap->active_iterators);
ebab2259
FZ
273 hbitmap_truncate(bitmap->bitmap, size);
274 bitmap->size = size;
275 }
276}
277
278static void bdrv_do_release_matching_dirty_bitmap(BlockDriverState *bs,
279 BdrvDirtyBitmap *bitmap,
280 bool only_named)
281{
282 BdrvDirtyBitmap *bm, *next;
283 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
284 if ((!bitmap || bm == bitmap) && (!only_named || bm->name)) {
dc162c8e 285 assert(!bm->active_iterators);
ebab2259 286 assert(!bdrv_dirty_bitmap_frozen(bm));
fb933437 287 assert(!bm->meta);
ebab2259
FZ
288 QLIST_REMOVE(bm, list);
289 hbitmap_free(bm->bitmap);
290 g_free(bm->name);
291 g_free(bm);
292
293 if (bitmap) {
294 return;
295 }
296 }
297 }
298}
299
300void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
301{
302 bdrv_do_release_matching_dirty_bitmap(bs, bitmap, false);
303}
304
305/**
306 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
307 * There must not be any frozen bitmaps attached.
308 */
309void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
310{
311 bdrv_do_release_matching_dirty_bitmap(bs, NULL, true);
312}
313
314void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
315{
316 assert(!bdrv_dirty_bitmap_frozen(bitmap));
317 bitmap->disabled = true;
318}
319
320void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
321{
322 assert(!bdrv_dirty_bitmap_frozen(bitmap));
323 bitmap->disabled = false;
324}
325
326BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
327{
328 BdrvDirtyBitmap *bm;
329 BlockDirtyInfoList *list = NULL;
330 BlockDirtyInfoList **plist = &list;
331
332 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
333 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
334 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
335 info->count = bdrv_get_dirty_count(bm);
336 info->granularity = bdrv_dirty_bitmap_granularity(bm);
337 info->has_name = !!bm->name;
338 info->name = g_strdup(bm->name);
339 info->status = bdrv_dirty_bitmap_status(bm);
340 entry->value = info;
341 *plist = entry;
342 plist = &entry->next;
343 }
344
345 return list;
346}
347
348int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
349 int64_t sector)
350{
351 if (bitmap) {
352 return hbitmap_get(bitmap->bitmap, sector);
353 } else {
354 return 0;
355 }
356}
357
358/**
359 * Chooses a default granularity based on the existing cluster size,
360 * but clamped between [4K, 64K]. Defaults to 64K in the case that there
361 * is no cluster size information available.
362 */
363uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
364{
365 BlockDriverInfo bdi;
366 uint32_t granularity;
367
368 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
369 granularity = MAX(4096, bdi.cluster_size);
370 granularity = MIN(65536, granularity);
371 } else {
372 granularity = 65536;
373 }
374
375 return granularity;
376}
377
378uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap)
379{
380 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap);
381}
382
dc162c8e
FZ
383BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap,
384 uint64_t first_sector)
385{
386 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
387 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, first_sector);
388 iter->bitmap = bitmap;
389 bitmap->active_iterators++;
390 return iter;
391}
392
393void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
394{
395 if (!iter) {
396 return;
397 }
398 assert(iter->bitmap->active_iterators > 0);
399 iter->bitmap->active_iterators--;
400 g_free(iter);
401}
402
403int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
ebab2259 404{
dc162c8e 405 return hbitmap_iter_next(&iter->hbi);
ebab2259
FZ
406}
407
408void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
6d078599 409 int64_t cur_sector, int64_t nr_sectors)
ebab2259
FZ
410{
411 assert(bdrv_dirty_bitmap_enabled(bitmap));
412 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
413}
414
415void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
6d078599 416 int64_t cur_sector, int64_t nr_sectors)
ebab2259
FZ
417{
418 assert(bdrv_dirty_bitmap_enabled(bitmap));
419 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors);
420}
421
422void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
423{
424 assert(bdrv_dirty_bitmap_enabled(bitmap));
425 if (!out) {
426 hbitmap_reset_all(bitmap->bitmap);
427 } else {
428 HBitmap *backup = bitmap->bitmap;
429 bitmap->bitmap = hbitmap_alloc(bitmap->size,
430 hbitmap_granularity(backup));
431 *out = backup;
432 }
433}
434
435void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
436{
437 HBitmap *tmp = bitmap->bitmap;
438 assert(bdrv_dirty_bitmap_enabled(bitmap));
439 bitmap->bitmap = in;
440 hbitmap_free(tmp);
441}
442
443void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
6d078599 444 int64_t nr_sectors)
ebab2259
FZ
445{
446 BdrvDirtyBitmap *bitmap;
447 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
448 if (!bdrv_dirty_bitmap_enabled(bitmap)) {
449 continue;
450 }
451 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors);
452 }
453}
454
455/**
dc162c8e 456 * Advance a BdrvDirtyBitmapIter to an arbitrary offset.
ebab2259 457 */
dc162c8e 458void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t sector_num)
ebab2259 459{
dc162c8e 460 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, sector_num);
ebab2259
FZ
461}
462
463int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
464{
465 return hbitmap_count(bitmap->bitmap);
466}