]>
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 { | |
b64bd51e | 40 | QemuMutex *mutex; |
ebab2259 | 41 | HBitmap *bitmap; /* Dirty sector bitmap implementation */ |
fb933437 | 42 | HBitmap *meta; /* Meta dirty bitmap */ |
ebab2259 FZ |
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) */ | |
8bfc932e VSO |
46 | bool disabled; /* Bitmap is disabled. It ignores all writes to |
47 | the device */ | |
dc162c8e | 48 | int active_iterators; /* How many iterators are active */ |
d6883bc9 VSO |
49 | bool readonly; /* Bitmap is read-only. This field also |
50 | prevents the respective image from being | |
51 | modified (i.e. blocks writes and discards). | |
52 | Such operations must fail and both the image | |
53 | and this bitmap must remain unchanged while | |
54 | this flag is set. */ | |
a0319aac VSO |
55 | bool autoload; /* For persistent bitmaps: bitmap must be |
56 | autoloaded on image opening */ | |
a88b179f | 57 | bool persistent; /* bitmap must be saved to owner disk image */ |
ebab2259 FZ |
58 | QLIST_ENTRY(BdrvDirtyBitmap) list; |
59 | }; | |
60 | ||
dc162c8e FZ |
61 | struct BdrvDirtyBitmapIter { |
62 | HBitmapIter hbi; | |
63 | BdrvDirtyBitmap *bitmap; | |
64 | }; | |
65 | ||
2119882c PB |
66 | static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs) |
67 | { | |
68 | qemu_mutex_lock(&bs->dirty_bitmap_mutex); | |
69 | } | |
70 | ||
71 | static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs) | |
72 | { | |
73 | qemu_mutex_unlock(&bs->dirty_bitmap_mutex); | |
74 | } | |
75 | ||
b64bd51e PB |
76 | void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap) |
77 | { | |
78 | qemu_mutex_lock(bitmap->mutex); | |
79 | } | |
80 | ||
81 | void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap) | |
82 | { | |
83 | qemu_mutex_unlock(bitmap->mutex); | |
84 | } | |
85 | ||
2119882c | 86 | /* Called with BQL or dirty_bitmap lock taken. */ |
ebab2259 FZ |
87 | BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name) |
88 | { | |
89 | BdrvDirtyBitmap *bm; | |
90 | ||
91 | assert(name); | |
92 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
93 | if (bm->name && !strcmp(name, bm->name)) { | |
94 | return bm; | |
95 | } | |
96 | } | |
97 | return NULL; | |
98 | } | |
99 | ||
2119882c | 100 | /* Called with BQL taken. */ |
ebab2259 FZ |
101 | void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap) |
102 | { | |
103 | assert(!bdrv_dirty_bitmap_frozen(bitmap)); | |
104 | g_free(bitmap->name); | |
105 | bitmap->name = NULL; | |
a88b179f | 106 | bitmap->persistent = false; |
a0319aac | 107 | bitmap->autoload = false; |
ebab2259 FZ |
108 | } |
109 | ||
2119882c | 110 | /* Called with BQL taken. */ |
ebab2259 FZ |
111 | BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, |
112 | uint32_t granularity, | |
113 | const char *name, | |
114 | Error **errp) | |
115 | { | |
116 | int64_t bitmap_size; | |
117 | BdrvDirtyBitmap *bitmap; | |
118 | uint32_t sector_granularity; | |
119 | ||
120 | assert((granularity & (granularity - 1)) == 0); | |
121 | ||
122 | if (name && bdrv_find_dirty_bitmap(bs, name)) { | |
123 | error_setg(errp, "Bitmap already exists: %s", name); | |
124 | return NULL; | |
125 | } | |
126 | sector_granularity = granularity >> BDRV_SECTOR_BITS; | |
127 | assert(sector_granularity); | |
128 | bitmap_size = bdrv_nb_sectors(bs); | |
129 | if (bitmap_size < 0) { | |
130 | error_setg_errno(errp, -bitmap_size, "could not get length of device"); | |
131 | errno = -bitmap_size; | |
132 | return NULL; | |
133 | } | |
134 | bitmap = g_new0(BdrvDirtyBitmap, 1); | |
b64bd51e | 135 | bitmap->mutex = &bs->dirty_bitmap_mutex; |
ebab2259 FZ |
136 | bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(sector_granularity)); |
137 | bitmap->size = bitmap_size; | |
138 | bitmap->name = g_strdup(name); | |
139 | bitmap->disabled = false; | |
2119882c | 140 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 | 141 | QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); |
2119882c | 142 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
143 | return bitmap; |
144 | } | |
145 | ||
fb933437 FZ |
146 | /* bdrv_create_meta_dirty_bitmap |
147 | * | |
148 | * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e. | |
149 | * when a dirty status bit in @bitmap is changed (either from reset to set or | |
150 | * the other way around), its respective meta dirty bitmap bit will be marked | |
151 | * dirty as well. | |
152 | * | |
153 | * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap. | |
154 | * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap | |
155 | * track. | |
156 | */ | |
157 | void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap, | |
158 | int chunk_size) | |
159 | { | |
160 | assert(!bitmap->meta); | |
b64bd51e | 161 | qemu_mutex_lock(bitmap->mutex); |
fb933437 FZ |
162 | bitmap->meta = hbitmap_create_meta(bitmap->bitmap, |
163 | chunk_size * BITS_PER_BYTE); | |
b64bd51e | 164 | qemu_mutex_unlock(bitmap->mutex); |
fb933437 FZ |
165 | } |
166 | ||
167 | void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap) | |
168 | { | |
169 | assert(bitmap->meta); | |
b64bd51e | 170 | qemu_mutex_lock(bitmap->mutex); |
fb933437 FZ |
171 | hbitmap_free_meta(bitmap->bitmap); |
172 | bitmap->meta = NULL; | |
b64bd51e | 173 | qemu_mutex_unlock(bitmap->mutex); |
fb933437 FZ |
174 | } |
175 | ||
b64bd51e PB |
176 | int bdrv_dirty_bitmap_get_meta_locked(BlockDriverState *bs, |
177 | BdrvDirtyBitmap *bitmap, int64_t sector, | |
178 | int nb_sectors) | |
fb933437 FZ |
179 | { |
180 | uint64_t i; | |
181 | int sectors_per_bit = 1 << hbitmap_granularity(bitmap->meta); | |
182 | ||
183 | /* To optimize: we can make hbitmap to internally check the range in a | |
184 | * coarse level, or at least do it word by word. */ | |
185 | for (i = sector; i < sector + nb_sectors; i += sectors_per_bit) { | |
186 | if (hbitmap_get(bitmap->meta, i)) { | |
187 | return true; | |
188 | } | |
189 | } | |
190 | return false; | |
191 | } | |
192 | ||
b64bd51e PB |
193 | int bdrv_dirty_bitmap_get_meta(BlockDriverState *bs, |
194 | BdrvDirtyBitmap *bitmap, int64_t sector, | |
195 | int nb_sectors) | |
196 | { | |
197 | bool dirty; | |
198 | ||
199 | qemu_mutex_lock(bitmap->mutex); | |
200 | dirty = bdrv_dirty_bitmap_get_meta_locked(bs, bitmap, sector, nb_sectors); | |
201 | qemu_mutex_unlock(bitmap->mutex); | |
202 | ||
203 | return dirty; | |
204 | } | |
205 | ||
fb933437 FZ |
206 | void bdrv_dirty_bitmap_reset_meta(BlockDriverState *bs, |
207 | BdrvDirtyBitmap *bitmap, int64_t sector, | |
208 | int nb_sectors) | |
209 | { | |
b64bd51e | 210 | qemu_mutex_lock(bitmap->mutex); |
fb933437 | 211 | hbitmap_reset(bitmap->meta, sector, nb_sectors); |
b64bd51e | 212 | qemu_mutex_unlock(bitmap->mutex); |
fb933437 FZ |
213 | } |
214 | ||
15891fac FZ |
215 | int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap) |
216 | { | |
217 | return bitmap->size; | |
218 | } | |
219 | ||
220 | const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap) | |
221 | { | |
222 | return bitmap->name; | |
223 | } | |
224 | ||
2119882c | 225 | /* Called with BQL taken. */ |
ebab2259 FZ |
226 | bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap) |
227 | { | |
228 | return bitmap->successor; | |
229 | } | |
230 | ||
2119882c | 231 | /* Called with BQL taken. */ |
ebab2259 FZ |
232 | bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap) |
233 | { | |
234 | return !(bitmap->disabled || bitmap->successor); | |
235 | } | |
236 | ||
2119882c | 237 | /* Called with BQL taken. */ |
ebab2259 FZ |
238 | DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap) |
239 | { | |
240 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
241 | return DIRTY_BITMAP_STATUS_FROZEN; | |
242 | } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
243 | return DIRTY_BITMAP_STATUS_DISABLED; | |
244 | } else { | |
245 | return DIRTY_BITMAP_STATUS_ACTIVE; | |
246 | } | |
247 | } | |
248 | ||
249 | /** | |
250 | * Create a successor bitmap destined to replace this bitmap after an operation. | |
251 | * Requires that the bitmap is not frozen and has no successor. | |
2119882c | 252 | * Called with BQL taken. |
ebab2259 FZ |
253 | */ |
254 | int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs, | |
255 | BdrvDirtyBitmap *bitmap, Error **errp) | |
256 | { | |
257 | uint64_t granularity; | |
258 | BdrvDirtyBitmap *child; | |
259 | ||
260 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
261 | error_setg(errp, "Cannot create a successor for a bitmap that is " | |
262 | "currently frozen"); | |
263 | return -1; | |
264 | } | |
265 | assert(!bitmap->successor); | |
266 | ||
267 | /* Create an anonymous successor */ | |
268 | granularity = bdrv_dirty_bitmap_granularity(bitmap); | |
269 | child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); | |
270 | if (!child) { | |
271 | return -1; | |
272 | } | |
273 | ||
274 | /* Successor will be on or off based on our current state. */ | |
275 | child->disabled = bitmap->disabled; | |
276 | ||
277 | /* Install the successor and freeze the parent */ | |
278 | bitmap->successor = child; | |
279 | return 0; | |
280 | } | |
281 | ||
282 | /** | |
283 | * For a bitmap with a successor, yield our name to the successor, | |
284 | * delete the old bitmap, and return a handle to the new bitmap. | |
2119882c | 285 | * Called with BQL taken. |
ebab2259 FZ |
286 | */ |
287 | BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs, | |
288 | BdrvDirtyBitmap *bitmap, | |
289 | Error **errp) | |
290 | { | |
291 | char *name; | |
292 | BdrvDirtyBitmap *successor = bitmap->successor; | |
293 | ||
294 | if (successor == NULL) { | |
295 | error_setg(errp, "Cannot relinquish control if " | |
296 | "there's no successor present"); | |
297 | return NULL; | |
298 | } | |
299 | ||
300 | name = bitmap->name; | |
301 | bitmap->name = NULL; | |
302 | successor->name = name; | |
303 | bitmap->successor = NULL; | |
a88b179f VSO |
304 | successor->persistent = bitmap->persistent; |
305 | bitmap->persistent = false; | |
a0319aac VSO |
306 | successor->autoload = bitmap->autoload; |
307 | bitmap->autoload = false; | |
ebab2259 FZ |
308 | bdrv_release_dirty_bitmap(bs, bitmap); |
309 | ||
310 | return successor; | |
311 | } | |
312 | ||
313 | /** | |
314 | * In cases of failure where we can no longer safely delete the parent, | |
315 | * we may wish to re-join the parent and child/successor. | |
316 | * The merged parent will be un-frozen, but not explicitly re-enabled. | |
2119882c | 317 | * Called with BQL taken. |
ebab2259 FZ |
318 | */ |
319 | BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs, | |
320 | BdrvDirtyBitmap *parent, | |
321 | Error **errp) | |
322 | { | |
323 | BdrvDirtyBitmap *successor = parent->successor; | |
324 | ||
325 | if (!successor) { | |
326 | error_setg(errp, "Cannot reclaim a successor when none is present"); | |
327 | return NULL; | |
328 | } | |
329 | ||
330 | if (!hbitmap_merge(parent->bitmap, successor->bitmap)) { | |
331 | error_setg(errp, "Merging of parent and successor bitmap failed"); | |
332 | return NULL; | |
333 | } | |
334 | bdrv_release_dirty_bitmap(bs, successor); | |
335 | parent->successor = NULL; | |
336 | ||
337 | return parent; | |
338 | } | |
339 | ||
340 | /** | |
341 | * Truncates _all_ bitmaps attached to a BDS. | |
2119882c | 342 | * Called with BQL taken. |
ebab2259 FZ |
343 | */ |
344 | void bdrv_dirty_bitmap_truncate(BlockDriverState *bs) | |
345 | { | |
346 | BdrvDirtyBitmap *bitmap; | |
347 | uint64_t size = bdrv_nb_sectors(bs); | |
348 | ||
2119882c | 349 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 FZ |
350 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
351 | assert(!bdrv_dirty_bitmap_frozen(bitmap)); | |
dc162c8e | 352 | assert(!bitmap->active_iterators); |
ebab2259 FZ |
353 | hbitmap_truncate(bitmap->bitmap, size); |
354 | bitmap->size = size; | |
355 | } | |
2119882c | 356 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
357 | } |
358 | ||
2119882c | 359 | /* Called with BQL taken. */ |
ebab2259 FZ |
360 | static void bdrv_do_release_matching_dirty_bitmap(BlockDriverState *bs, |
361 | BdrvDirtyBitmap *bitmap, | |
362 | bool only_named) | |
363 | { | |
364 | BdrvDirtyBitmap *bm, *next; | |
2119882c | 365 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 FZ |
366 | QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { |
367 | if ((!bitmap || bm == bitmap) && (!only_named || bm->name)) { | |
dc162c8e | 368 | assert(!bm->active_iterators); |
ebab2259 | 369 | assert(!bdrv_dirty_bitmap_frozen(bm)); |
fb933437 | 370 | assert(!bm->meta); |
ebab2259 FZ |
371 | QLIST_REMOVE(bm, list); |
372 | hbitmap_free(bm->bitmap); | |
373 | g_free(bm->name); | |
374 | g_free(bm); | |
375 | ||
376 | if (bitmap) { | |
2119882c | 377 | goto out; |
ebab2259 FZ |
378 | } |
379 | } | |
380 | } | |
7105007a FZ |
381 | if (bitmap) { |
382 | abort(); | |
383 | } | |
2119882c PB |
384 | |
385 | out: | |
386 | bdrv_dirty_bitmaps_unlock(bs); | |
ebab2259 FZ |
387 | } |
388 | ||
2119882c | 389 | /* Called with BQL taken. */ |
ebab2259 FZ |
390 | void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) |
391 | { | |
392 | bdrv_do_release_matching_dirty_bitmap(bs, bitmap, false); | |
393 | } | |
394 | ||
395 | /** | |
396 | * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()). | |
397 | * There must not be any frozen bitmaps attached. | |
56f364e6 | 398 | * This function does not remove persistent bitmaps from the storage. |
2119882c | 399 | * Called with BQL taken. |
ebab2259 FZ |
400 | */ |
401 | void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs) | |
402 | { | |
403 | bdrv_do_release_matching_dirty_bitmap(bs, NULL, true); | |
404 | } | |
405 | ||
56f364e6 VSO |
406 | /** |
407 | * Remove persistent dirty bitmap from the storage if it exists. | |
408 | * Absence of bitmap is not an error, because we have the following scenario: | |
409 | * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no | |
410 | * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should | |
411 | * not fail. | |
412 | * This function doesn't release corresponding BdrvDirtyBitmap. | |
413 | */ | |
414 | void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, | |
415 | const char *name, | |
416 | Error **errp) | |
417 | { | |
418 | if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) { | |
419 | bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp); | |
420 | } | |
421 | } | |
422 | ||
2119882c | 423 | /* Called with BQL taken. */ |
ebab2259 FZ |
424 | void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) |
425 | { | |
426 | assert(!bdrv_dirty_bitmap_frozen(bitmap)); | |
427 | bitmap->disabled = true; | |
428 | } | |
429 | ||
2119882c | 430 | /* Called with BQL taken. */ |
ebab2259 FZ |
431 | void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) |
432 | { | |
433 | assert(!bdrv_dirty_bitmap_frozen(bitmap)); | |
434 | bitmap->disabled = false; | |
435 | } | |
436 | ||
437 | BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) | |
438 | { | |
439 | BdrvDirtyBitmap *bm; | |
440 | BlockDirtyInfoList *list = NULL; | |
441 | BlockDirtyInfoList **plist = &list; | |
442 | ||
2119882c | 443 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 FZ |
444 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { |
445 | BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); | |
446 | BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1); | |
447 | info->count = bdrv_get_dirty_count(bm); | |
448 | info->granularity = bdrv_dirty_bitmap_granularity(bm); | |
449 | info->has_name = !!bm->name; | |
450 | info->name = g_strdup(bm->name); | |
451 | info->status = bdrv_dirty_bitmap_status(bm); | |
452 | entry->value = info; | |
453 | *plist = entry; | |
454 | plist = &entry->next; | |
455 | } | |
2119882c | 456 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
457 | |
458 | return list; | |
459 | } | |
460 | ||
b64bd51e PB |
461 | /* Called within bdrv_dirty_bitmap_lock..unlock */ |
462 | int bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, | |
463 | int64_t sector) | |
ebab2259 FZ |
464 | { |
465 | if (bitmap) { | |
466 | return hbitmap_get(bitmap->bitmap, sector); | |
467 | } else { | |
468 | return 0; | |
469 | } | |
470 | } | |
471 | ||
472 | /** | |
473 | * Chooses a default granularity based on the existing cluster size, | |
474 | * but clamped between [4K, 64K]. Defaults to 64K in the case that there | |
475 | * is no cluster size information available. | |
476 | */ | |
477 | uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) | |
478 | { | |
479 | BlockDriverInfo bdi; | |
480 | uint32_t granularity; | |
481 | ||
482 | if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { | |
483 | granularity = MAX(4096, bdi.cluster_size); | |
484 | granularity = MIN(65536, granularity); | |
485 | } else { | |
486 | granularity = 65536; | |
487 | } | |
488 | ||
489 | return granularity; | |
490 | } | |
491 | ||
ba06ff1a | 492 | uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap) |
ebab2259 FZ |
493 | { |
494 | return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap); | |
495 | } | |
496 | ||
6d3f4049 FZ |
497 | uint32_t bdrv_dirty_bitmap_meta_granularity(BdrvDirtyBitmap *bitmap) |
498 | { | |
499 | return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->meta); | |
500 | } | |
501 | ||
dc162c8e FZ |
502 | BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap, |
503 | uint64_t first_sector) | |
504 | { | |
505 | BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); | |
506 | hbitmap_iter_init(&iter->hbi, bitmap->bitmap, first_sector); | |
507 | iter->bitmap = bitmap; | |
508 | bitmap->active_iterators++; | |
509 | return iter; | |
510 | } | |
511 | ||
6d3f4049 FZ |
512 | BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap) |
513 | { | |
514 | BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); | |
515 | hbitmap_iter_init(&iter->hbi, bitmap->meta, 0); | |
516 | iter->bitmap = bitmap; | |
517 | bitmap->active_iterators++; | |
518 | return iter; | |
519 | } | |
520 | ||
dc162c8e FZ |
521 | void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter) |
522 | { | |
523 | if (!iter) { | |
524 | return; | |
525 | } | |
526 | assert(iter->bitmap->active_iterators > 0); | |
527 | iter->bitmap->active_iterators--; | |
528 | g_free(iter); | |
529 | } | |
530 | ||
531 | int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter) | |
ebab2259 | 532 | { |
dc162c8e | 533 | return hbitmap_iter_next(&iter->hbi); |
ebab2259 FZ |
534 | } |
535 | ||
b64bd51e PB |
536 | /* Called within bdrv_dirty_bitmap_lock..unlock */ |
537 | void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, | |
538 | int64_t cur_sector, int64_t nr_sectors) | |
539 | { | |
540 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
d6883bc9 | 541 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
b64bd51e PB |
542 | hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); |
543 | } | |
544 | ||
ebab2259 | 545 | void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, |
6d078599 | 546 | int64_t cur_sector, int64_t nr_sectors) |
b64bd51e PB |
547 | { |
548 | bdrv_dirty_bitmap_lock(bitmap); | |
549 | bdrv_set_dirty_bitmap_locked(bitmap, cur_sector, nr_sectors); | |
550 | bdrv_dirty_bitmap_unlock(bitmap); | |
551 | } | |
552 | ||
553 | /* Called within bdrv_dirty_bitmap_lock..unlock */ | |
554 | void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, | |
555 | int64_t cur_sector, int64_t nr_sectors) | |
ebab2259 FZ |
556 | { |
557 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
d6883bc9 | 558 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
b64bd51e | 559 | hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); |
ebab2259 FZ |
560 | } |
561 | ||
562 | void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, | |
6d078599 | 563 | int64_t cur_sector, int64_t nr_sectors) |
ebab2259 | 564 | { |
b64bd51e PB |
565 | bdrv_dirty_bitmap_lock(bitmap); |
566 | bdrv_reset_dirty_bitmap_locked(bitmap, cur_sector, nr_sectors); | |
567 | bdrv_dirty_bitmap_unlock(bitmap); | |
ebab2259 FZ |
568 | } |
569 | ||
570 | void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out) | |
571 | { | |
572 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
d6883bc9 | 573 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
b64bd51e | 574 | bdrv_dirty_bitmap_lock(bitmap); |
ebab2259 FZ |
575 | if (!out) { |
576 | hbitmap_reset_all(bitmap->bitmap); | |
577 | } else { | |
578 | HBitmap *backup = bitmap->bitmap; | |
579 | bitmap->bitmap = hbitmap_alloc(bitmap->size, | |
580 | hbitmap_granularity(backup)); | |
581 | *out = backup; | |
582 | } | |
b64bd51e | 583 | bdrv_dirty_bitmap_unlock(bitmap); |
ebab2259 FZ |
584 | } |
585 | ||
586 | void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in) | |
587 | { | |
588 | HBitmap *tmp = bitmap->bitmap; | |
589 | assert(bdrv_dirty_bitmap_enabled(bitmap)); | |
d6883bc9 | 590 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ebab2259 FZ |
591 | bitmap->bitmap = in; |
592 | hbitmap_free(tmp); | |
593 | } | |
594 | ||
882c36f5 VSO |
595 | uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap, |
596 | uint64_t start, uint64_t count) | |
597 | { | |
598 | return hbitmap_serialization_size(bitmap->bitmap, start, count); | |
599 | } | |
600 | ||
601 | uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap) | |
602 | { | |
603 | return hbitmap_serialization_granularity(bitmap->bitmap); | |
604 | } | |
605 | ||
606 | void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap, | |
607 | uint8_t *buf, uint64_t start, | |
608 | uint64_t count) | |
609 | { | |
610 | hbitmap_serialize_part(bitmap->bitmap, buf, start, count); | |
611 | } | |
612 | ||
613 | void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap, | |
614 | uint8_t *buf, uint64_t start, | |
615 | uint64_t count, bool finish) | |
616 | { | |
617 | hbitmap_deserialize_part(bitmap->bitmap, buf, start, count, finish); | |
618 | } | |
619 | ||
620 | void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap, | |
621 | uint64_t start, uint64_t count, | |
622 | bool finish) | |
623 | { | |
624 | hbitmap_deserialize_zeroes(bitmap->bitmap, start, count, finish); | |
6bdc8b71 VSO |
625 | } |
626 | ||
627 | void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap, | |
628 | uint64_t start, uint64_t count, | |
629 | bool finish) | |
630 | { | |
631 | hbitmap_deserialize_ones(bitmap->bitmap, start, count, finish); | |
882c36f5 VSO |
632 | } |
633 | ||
634 | void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap) | |
635 | { | |
636 | hbitmap_deserialize_finish(bitmap->bitmap); | |
637 | } | |
638 | ||
ebab2259 | 639 | void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, |
6d078599 | 640 | int64_t nr_sectors) |
ebab2259 FZ |
641 | { |
642 | BdrvDirtyBitmap *bitmap; | |
2119882c PB |
643 | |
644 | if (QLIST_EMPTY(&bs->dirty_bitmaps)) { | |
645 | return; | |
646 | } | |
647 | ||
648 | bdrv_dirty_bitmaps_lock(bs); | |
ebab2259 FZ |
649 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
650 | if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
651 | continue; | |
652 | } | |
d6883bc9 | 653 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ebab2259 FZ |
654 | hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); |
655 | } | |
2119882c | 656 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
657 | } |
658 | ||
659 | /** | |
dc162c8e | 660 | * Advance a BdrvDirtyBitmapIter to an arbitrary offset. |
ebab2259 | 661 | */ |
dc162c8e | 662 | void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t sector_num) |
ebab2259 | 663 | { |
dc162c8e | 664 | hbitmap_iter_init(&iter->hbi, iter->hbi.hb, sector_num); |
ebab2259 FZ |
665 | } |
666 | ||
667 | int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) | |
668 | { | |
669 | return hbitmap_count(bitmap->bitmap); | |
670 | } | |
6d3f4049 FZ |
671 | |
672 | int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap) | |
673 | { | |
674 | return hbitmap_count(bitmap->meta); | |
675 | } | |
d6883bc9 VSO |
676 | |
677 | bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap) | |
678 | { | |
679 | return bitmap->readonly; | |
680 | } | |
681 | ||
682 | /* Called with BQL taken. */ | |
683 | void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value) | |
684 | { | |
685 | qemu_mutex_lock(bitmap->mutex); | |
686 | bitmap->readonly = value; | |
687 | qemu_mutex_unlock(bitmap->mutex); | |
688 | } | |
689 | ||
690 | bool bdrv_has_readonly_bitmaps(BlockDriverState *bs) | |
691 | { | |
692 | BdrvDirtyBitmap *bm; | |
693 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
694 | if (bm->readonly) { | |
695 | return true; | |
696 | } | |
697 | } | |
698 | ||
699 | return false; | |
700 | } | |
a0319aac VSO |
701 | |
702 | /* Called with BQL taken. */ | |
703 | void bdrv_dirty_bitmap_set_autoload(BdrvDirtyBitmap *bitmap, bool autoload) | |
704 | { | |
705 | qemu_mutex_lock(bitmap->mutex); | |
706 | bitmap->autoload = autoload; | |
707 | qemu_mutex_unlock(bitmap->mutex); | |
708 | } | |
709 | ||
710 | bool bdrv_dirty_bitmap_get_autoload(const BdrvDirtyBitmap *bitmap) | |
711 | { | |
712 | return bitmap->autoload; | |
713 | } | |
a88b179f VSO |
714 | |
715 | /* Called with BQL taken. */ | |
716 | void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap *bitmap, bool persistent) | |
717 | { | |
718 | qemu_mutex_lock(bitmap->mutex); | |
719 | bitmap->persistent = persistent; | |
720 | qemu_mutex_unlock(bitmap->mutex); | |
721 | } | |
722 | ||
723 | bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap *bitmap) | |
724 | { | |
725 | return bitmap->persistent; | |
726 | } | |
727 | ||
728 | bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs) | |
729 | { | |
730 | BdrvDirtyBitmap *bm; | |
731 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
732 | if (bm->persistent && !bm->readonly) { | |
733 | return true; | |
734 | } | |
735 | } | |
736 | ||
737 | return false; | |
738 | } | |
3dd10a06 VSO |
739 | |
740 | BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs, | |
741 | BdrvDirtyBitmap *bitmap) | |
742 | { | |
743 | return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) : | |
744 | QLIST_NEXT(bitmap, list); | |
745 | } | |
a3b52535 VSO |
746 | |
747 | char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp) | |
748 | { | |
749 | return hbitmap_sha256(bitmap->bitmap, errp); | |
750 | } |