]>
Commit | Line | Data |
---|---|---|
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 | */ | |
39 | struct 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 |
50 | struct BdrvDirtyBitmapIter { |
51 | HBitmapIter hbi; | |
52 | BdrvDirtyBitmap *bitmap; | |
53 | }; | |
54 | ||
ebab2259 FZ |
55 | BdrvDirtyBitmap *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 | ||
68 | void 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 | ||
75 | BdrvDirtyBitmap *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 | */ | |
118 | void 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 | ||
126 | void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap) | |
127 | { | |
128 | assert(bitmap->meta); | |
129 | hbitmap_free_meta(bitmap->bitmap); | |
130 | bitmap->meta = NULL; | |
131 | } | |
132 | ||
133 | int 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 | ||
150 | void 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 | ||
15891fac FZ |
157 | int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap) |
158 | { | |
159 | return bitmap->size; | |
160 | } | |
161 | ||
162 | const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap) | |
163 | { | |
164 | return bitmap->name; | |
165 | } | |
166 | ||
ebab2259 FZ |
167 | bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap) |
168 | { | |
169 | return bitmap->successor; | |
170 | } | |
171 | ||
172 | bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap) | |
173 | { | |
174 | return !(bitmap->disabled || bitmap->successor); | |
175 | } | |
176 | ||
177 | DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap) | |
178 | { | |
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; | |
185 | } | |
186 | } | |
187 | ||
188 | /** | |
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. | |
191 | */ | |
192 | int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs, | |
193 | BdrvDirtyBitmap *bitmap, Error **errp) | |
194 | { | |
195 | uint64_t granularity; | |
196 | BdrvDirtyBitmap *child; | |
197 | ||
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; | |
202 | } | |
203 | assert(!bitmap->successor); | |
204 | ||
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; | |
210 | } | |
211 | ||
212 | /* Successor will be on or off based on our current state. */ | |
213 | child->disabled = bitmap->disabled; | |
214 | ||
215 | /* Install the successor and freeze the parent */ | |
216 | bitmap->successor = child; | |
217 | return 0; | |
218 | } | |
219 | ||
220 | /** | |
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. | |
223 | */ | |
224 | BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs, | |
225 | BdrvDirtyBitmap *bitmap, | |
226 | Error **errp) | |
227 | { | |
228 | char *name; | |
229 | BdrvDirtyBitmap *successor = bitmap->successor; | |
230 | ||
231 | if (successor == NULL) { | |
232 | error_setg(errp, "Cannot relinquish control if " | |
233 | "there's no successor present"); | |
234 | return NULL; | |
235 | } | |
236 | ||
237 | name = bitmap->name; | |
238 | bitmap->name = NULL; | |
239 | successor->name = name; | |
240 | bitmap->successor = NULL; | |
241 | bdrv_release_dirty_bitmap(bs, bitmap); | |
242 | ||
243 | return successor; | |
244 | } | |
245 | ||
246 | /** | |
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. | |
250 | */ | |
251 | BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs, | |
252 | BdrvDirtyBitmap *parent, | |
253 | Error **errp) | |
254 | { | |
255 | BdrvDirtyBitmap *successor = parent->successor; | |
256 | ||
257 | if (!successor) { | |
258 | error_setg(errp, "Cannot reclaim a successor when none is present"); | |
259 | return NULL; | |
260 | } | |
261 | ||
262 | if (!hbitmap_merge(parent->bitmap, successor->bitmap)) { | |
263 | error_setg(errp, "Merging of parent and successor bitmap failed"); | |
264 | return NULL; | |
265 | } | |
266 | bdrv_release_dirty_bitmap(bs, successor); | |
267 | parent->successor = NULL; | |
268 | ||
269 | return parent; | |
270 | } | |
271 | ||
272 | /** | |
273 | * Truncates _all_ bitmaps attached to a BDS. | |
274 | */ | |
275 | void bdrv_dirty_bitmap_truncate(BlockDriverState *bs) | |
276 | { | |
277 | BdrvDirtyBitmap *bitmap; | |
278 | uint64_t size = bdrv_nb_sectors(bs); | |
279 | ||
280 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { | |
281 | assert(!bdrv_dirty_bitmap_frozen(bitmap)); | |
dc162c8e | 282 | assert(!bitmap->active_iterators); |
ebab2259 FZ |
283 | hbitmap_truncate(bitmap->bitmap, size); |
284 | bitmap->size = size; | |
285 | } | |
286 | } | |
287 | ||
288 | static void bdrv_do_release_matching_dirty_bitmap(BlockDriverState *bs, | |
289 | BdrvDirtyBitmap *bitmap, | |
290 | bool only_named) | |
291 | { | |
292 | BdrvDirtyBitmap *bm, *next; | |
293 | QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { | |
294 | if ((!bitmap || bm == bitmap) && (!only_named || bm->name)) { | |
dc162c8e | 295 | assert(!bm->active_iterators); |
ebab2259 | 296 | assert(!bdrv_dirty_bitmap_frozen(bm)); |
fb933437 | 297 | assert(!bm->meta); |
ebab2259 FZ |
298 | QLIST_REMOVE(bm, list); |
299 | hbitmap_free(bm->bitmap); | |
300 | g_free(bm->name); | |
301 | g_free(bm); | |
302 | ||
303 | if (bitmap) { | |
304 | return; | |
305 | } | |
306 | } | |
307 | } | |
308 | } | |
309 | ||
310 | void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) | |
311 | { | |
312 | bdrv_do_release_matching_dirty_bitmap(bs, bitmap, false); | |
313 | } | |
314 | ||
315 | /** | |
316 | * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()). | |
317 | * There must not be any frozen bitmaps attached. | |
318 | */ | |
319 | void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs) | |
320 | { | |
321 | bdrv_do_release_matching_dirty_bitmap(bs, NULL, true); | |
322 | } | |
323 | ||
324 | void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) | |
325 | { | |
326 | assert(!bdrv_dirty_bitmap_frozen(bitmap)); | |
327 | bitmap->disabled = true; | |
328 | } | |
329 | ||
330 | void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) | |
331 | { | |
332 | assert(!bdrv_dirty_bitmap_frozen(bitmap)); | |
333 | bitmap->disabled = false; | |
334 | } | |
335 | ||
336 | BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) | |
337 | { | |
338 | BdrvDirtyBitmap *bm; | |
339 | BlockDirtyInfoList *list = NULL; | |
340 | BlockDirtyInfoList **plist = &list; | |
341 | ||
342 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
343 | BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); | |
344 | BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1); | |
345 | info->count = bdrv_get_dirty_count(bm); | |
346 | info->granularity = bdrv_dirty_bitmap_granularity(bm); | |
347 | info->has_name = !!bm->name; | |
348 | info->name = g_strdup(bm->name); | |
349 | info->status = bdrv_dirty_bitmap_status(bm); | |
350 | entry->value = info; | |
351 | *plist = entry; | |
352 | plist = &entry->next; | |
353 | } | |
354 | ||
355 | return list; | |
356 | } | |
357 | ||
358 | int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, | |
359 | int64_t sector) | |
360 | { | |
361 | if (bitmap) { | |
362 | return hbitmap_get(bitmap->bitmap, sector); | |
363 | } else { | |
364 | return 0; | |
365 | } | |
366 | } | |
367 | ||
368 | /** | |
369 | * Chooses a default granularity based on the existing cluster size, | |
370 | * but clamped between [4K, 64K]. Defaults to 64K in the case that there | |
371 | * is no cluster size information available. | |
372 | */ | |
373 | uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) | |
374 | { | |
375 | BlockDriverInfo bdi; | |
376 | uint32_t granularity; | |
377 | ||
378 | if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { | |
379 | granularity = MAX(4096, bdi.cluster_size); | |
380 | granularity = MIN(65536, granularity); | |
381 | } else { | |
382 | granularity = 65536; | |
383 | } | |
384 | ||
385 | return granularity; | |
386 | } | |
387 | ||
388 | uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap) | |
389 | { | |
390 | return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap); | |
391 | } | |
392 | ||
dc162c8e FZ |
393 | BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap, |
394 | uint64_t first_sector) | |
395 | { | |
396 | BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); | |
397 | hbitmap_iter_init(&iter->hbi, bitmap->bitmap, first_sector); | |
398 | iter->bitmap = bitmap; | |
399 | bitmap->active_iterators++; | |
400 | return iter; | |
401 | } | |
402 | ||
403 | void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter) | |
404 | { | |
405 | if (!iter) { | |
406 | return; | |
407 | } | |
408 | assert(iter->bitmap->active_iterators > 0); | |
409 | iter->bitmap->active_iterators--; | |
410 | g_free(iter); | |
411 | } | |
412 | ||
413 | int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter) | |
ebab2259 | 414 | { |
dc162c8e | 415 | return hbitmap_iter_next(&iter->hbi); |
ebab2259 FZ |
416 | } |
417 | ||
418 | void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, | |
6d078599 | 419 | int64_t cur_sector, int64_t nr_sectors) |
ebab2259 FZ |
420 | { |
421 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
422 | hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); | |
423 | } | |
424 | ||
425 | void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, | |
6d078599 | 426 | int64_t cur_sector, int64_t nr_sectors) |
ebab2259 FZ |
427 | { |
428 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
429 | hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); | |
430 | } | |
431 | ||
432 | void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out) | |
433 | { | |
434 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
435 | if (!out) { | |
436 | hbitmap_reset_all(bitmap->bitmap); | |
437 | } else { | |
438 | HBitmap *backup = bitmap->bitmap; | |
439 | bitmap->bitmap = hbitmap_alloc(bitmap->size, | |
440 | hbitmap_granularity(backup)); | |
441 | *out = backup; | |
442 | } | |
443 | } | |
444 | ||
445 | void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in) | |
446 | { | |
447 | HBitmap *tmp = bitmap->bitmap; | |
448 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
449 | bitmap->bitmap = in; | |
450 | hbitmap_free(tmp); | |
451 | } | |
452 | ||
453 | void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, | |
6d078599 | 454 | int64_t nr_sectors) |
ebab2259 FZ |
455 | { |
456 | BdrvDirtyBitmap *bitmap; | |
457 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { | |
458 | if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
459 | continue; | |
460 | } | |
461 | hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); | |
462 | } | |
463 | } | |
464 | ||
465 | /** | |
dc162c8e | 466 | * Advance a BdrvDirtyBitmapIter to an arbitrary offset. |
ebab2259 | 467 | */ |
dc162c8e | 468 | void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t sector_num) |
ebab2259 | 469 | { |
dc162c8e | 470 | hbitmap_iter_init(&iter->hbi, iter->hbi.hb, sector_num); |
ebab2259 FZ |
471 | } |
472 | ||
473 | int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) | |
474 | { | |
475 | return hbitmap_count(bitmap->bitmap); | |
476 | } |