]>
Commit | Line | Data |
---|---|---|
ebab2259 FZ |
1 | /* |
2 | * Block Dirty Bitmap | |
3 | * | |
1b6cc579 | 4 | * Copyright (c) 2016-2017 Red Hat. Inc |
ebab2259 FZ |
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 "trace.h" |
27 | #include "block/block_int.h" | |
28 | #include "block/blockjob.h" | |
29 | ||
ebab2259 | 30 | struct BdrvDirtyBitmap { |
b64bd51e | 31 | QemuMutex *mutex; |
ca759622 | 32 | HBitmap *bitmap; /* Dirty bitmap implementation */ |
fb933437 | 33 | HBitmap *meta; /* Meta dirty bitmap */ |
27a1b301 | 34 | bool busy; /* Bitmap is busy, it can't be used via QMP */ |
21d2376f | 35 | BdrvDirtyBitmap *successor; /* Anonymous child, if any. */ |
ebab2259 | 36 | char *name; /* Optional non-empty unique ID */ |
993e6525 | 37 | int64_t size; /* Size of the bitmap, in bytes */ |
8bfc932e VSO |
38 | bool disabled; /* Bitmap is disabled. It ignores all writes to |
39 | the device */ | |
dc162c8e | 40 | int active_iterators; /* How many iterators are active */ |
d6883bc9 VSO |
41 | bool readonly; /* Bitmap is read-only. This field also |
42 | prevents the respective image from being | |
43 | modified (i.e. blocks writes and discards). | |
44 | Such operations must fail and both the image | |
45 | and this bitmap must remain unchanged while | |
46 | this flag is set. */ | |
a88b179f | 47 | bool persistent; /* bitmap must be saved to owner disk image */ |
b0f45559 JS |
48 | bool inconsistent; /* bitmap is persistent, but inconsistent. |
49 | It cannot be used at all in any way, except | |
50 | a QMP user can remove it. */ | |
c4e4b0fa JS |
51 | bool skip_store; /* We are either migrating or deleting this |
52 | * bitmap; it should not be stored on the next | |
53 | * inactivation. */ | |
ebab2259 FZ |
54 | QLIST_ENTRY(BdrvDirtyBitmap) list; |
55 | }; | |
56 | ||
dc162c8e FZ |
57 | struct BdrvDirtyBitmapIter { |
58 | HBitmapIter hbi; | |
59 | BdrvDirtyBitmap *bitmap; | |
60 | }; | |
61 | ||
2119882c PB |
62 | static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs) |
63 | { | |
64 | qemu_mutex_lock(&bs->dirty_bitmap_mutex); | |
65 | } | |
66 | ||
67 | static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs) | |
68 | { | |
69 | qemu_mutex_unlock(&bs->dirty_bitmap_mutex); | |
70 | } | |
71 | ||
b64bd51e PB |
72 | void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap) |
73 | { | |
74 | qemu_mutex_lock(bitmap->mutex); | |
75 | } | |
76 | ||
77 | void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap) | |
78 | { | |
79 | qemu_mutex_unlock(bitmap->mutex); | |
80 | } | |
81 | ||
2119882c | 82 | /* Called with BQL or dirty_bitmap lock taken. */ |
ebab2259 FZ |
83 | BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name) |
84 | { | |
85 | BdrvDirtyBitmap *bm; | |
86 | ||
87 | assert(name); | |
88 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
89 | if (bm->name && !strcmp(name, bm->name)) { | |
90 | return bm; | |
91 | } | |
92 | } | |
93 | return NULL; | |
94 | } | |
95 | ||
2119882c | 96 | /* Called with BQL taken. */ |
ebab2259 FZ |
97 | BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, |
98 | uint32_t granularity, | |
99 | const char *name, | |
100 | Error **errp) | |
101 | { | |
102 | int64_t bitmap_size; | |
103 | BdrvDirtyBitmap *bitmap; | |
ebab2259 | 104 | |
993e6525 | 105 | assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE); |
ebab2259 FZ |
106 | |
107 | if (name && bdrv_find_dirty_bitmap(bs, name)) { | |
108 | error_setg(errp, "Bitmap already exists: %s", name); | |
109 | return NULL; | |
110 | } | |
993e6525 | 111 | bitmap_size = bdrv_getlength(bs); |
ebab2259 FZ |
112 | if (bitmap_size < 0) { |
113 | error_setg_errno(errp, -bitmap_size, "could not get length of device"); | |
114 | errno = -bitmap_size; | |
115 | return NULL; | |
116 | } | |
117 | bitmap = g_new0(BdrvDirtyBitmap, 1); | |
b64bd51e | 118 | bitmap->mutex = &bs->dirty_bitmap_mutex; |
ca759622 | 119 | bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity)); |
ebab2259 FZ |
120 | bitmap->size = bitmap_size; |
121 | bitmap->name = g_strdup(name); | |
122 | bitmap->disabled = false; | |
2119882c | 123 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 | 124 | QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); |
2119882c | 125 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
126 | return bitmap; |
127 | } | |
128 | ||
fb933437 FZ |
129 | /* bdrv_create_meta_dirty_bitmap |
130 | * | |
131 | * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e. | |
132 | * when a dirty status bit in @bitmap is changed (either from reset to set or | |
133 | * the other way around), its respective meta dirty bitmap bit will be marked | |
134 | * dirty as well. | |
135 | * | |
136 | * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap. | |
137 | * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap | |
138 | * track. | |
139 | */ | |
140 | void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap, | |
141 | int chunk_size) | |
142 | { | |
143 | assert(!bitmap->meta); | |
b64bd51e | 144 | qemu_mutex_lock(bitmap->mutex); |
fb933437 FZ |
145 | bitmap->meta = hbitmap_create_meta(bitmap->bitmap, |
146 | chunk_size * BITS_PER_BYTE); | |
b64bd51e | 147 | qemu_mutex_unlock(bitmap->mutex); |
fb933437 FZ |
148 | } |
149 | ||
150 | void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap) | |
151 | { | |
152 | assert(bitmap->meta); | |
b64bd51e | 153 | qemu_mutex_lock(bitmap->mutex); |
fb933437 FZ |
154 | hbitmap_free_meta(bitmap->bitmap); |
155 | bitmap->meta = NULL; | |
b64bd51e | 156 | qemu_mutex_unlock(bitmap->mutex); |
fb933437 FZ |
157 | } |
158 | ||
15891fac FZ |
159 | int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap) |
160 | { | |
993e6525 | 161 | return bitmap->size; |
15891fac FZ |
162 | } |
163 | ||
164 | const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap) | |
165 | { | |
166 | return bitmap->name; | |
167 | } | |
168 | ||
2119882c | 169 | /* Called with BQL taken. */ |
50a47257 | 170 | bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap) |
ebab2259 FZ |
171 | { |
172 | return bitmap->successor; | |
173 | } | |
174 | ||
3ae96d66 | 175 | static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap) |
27a1b301 JS |
176 | { |
177 | return bitmap->busy; | |
993edc0c JS |
178 | } |
179 | ||
27a1b301 | 180 | void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy) |
4f43e953 VSO |
181 | { |
182 | qemu_mutex_lock(bitmap->mutex); | |
27a1b301 | 183 | bitmap->busy = busy; |
4f43e953 VSO |
184 | qemu_mutex_unlock(bitmap->mutex); |
185 | } | |
186 | ||
2119882c | 187 | /* Called with BQL taken. */ |
ebab2259 FZ |
188 | bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap) |
189 | { | |
8b2e20f6 | 190 | return !bitmap->disabled; |
ebab2259 FZ |
191 | } |
192 | ||
1e6fddcd JS |
193 | /** |
194 | * bdrv_dirty_bitmap_status: This API is now deprecated. | |
195 | * Called with BQL taken. | |
196 | * | |
197 | * A BdrvDirtyBitmap can be in four possible user-visible states: | |
198 | * (1) Active: successor is NULL, and disabled is false: full r/w mode | |
199 | * (2) Disabled: successor is NULL, and disabled is true: qualified r/w mode, | |
200 | * guest writes are dropped, but monitor writes are possible, | |
201 | * through commands like merge and clear. | |
202 | * (3) Frozen: successor is not NULL. | |
203 | * A frozen bitmap cannot be renamed, deleted, cleared, set, | |
204 | * enabled, merged to, etc. A frozen bitmap can only abdicate() | |
205 | * or reclaim(). | |
206 | * In this state, the anonymous successor bitmap may be either | |
207 | * Active and recording writes from the guest (e.g. backup jobs), | |
208 | * or it can be Disabled and not recording writes. | |
209 | * (4) Locked: Whether Active or Disabled, the user cannot modify this bitmap | |
210 | * in any way from the monitor. | |
0064cfef JS |
211 | * (5) Inconsistent: This is a persistent bitmap whose "in use" bit is set, and |
212 | * is unusable by QEMU. It can be deleted to remove it from | |
213 | * the qcow2. | |
1e6fddcd | 214 | */ |
ebab2259 FZ |
215 | DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap) |
216 | { | |
0064cfef JS |
217 | if (bdrv_dirty_bitmap_inconsistent(bitmap)) { |
218 | return DIRTY_BITMAP_STATUS_INCONSISTENT; | |
219 | } else if (bdrv_dirty_bitmap_has_successor(bitmap)) { | |
ebab2259 | 220 | return DIRTY_BITMAP_STATUS_FROZEN; |
27a1b301 | 221 | } else if (bdrv_dirty_bitmap_busy(bitmap)) { |
4f43e953 | 222 | return DIRTY_BITMAP_STATUS_LOCKED; |
ebab2259 FZ |
223 | } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { |
224 | return DIRTY_BITMAP_STATUS_DISABLED; | |
225 | } else { | |
226 | return DIRTY_BITMAP_STATUS_ACTIVE; | |
227 | } | |
228 | } | |
229 | ||
4db6ceb0 JS |
230 | /* Called with BQL taken. */ |
231 | static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap) | |
232 | { | |
233 | return !bitmap->disabled || (bitmap->successor && | |
234 | !bitmap->successor->disabled); | |
235 | } | |
236 | ||
3ae96d66 JS |
237 | int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags, |
238 | Error **errp) | |
239 | { | |
240 | if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) { | |
241 | error_setg(errp, "Bitmap '%s' is currently in use by another" | |
242 | " operation and cannot be used", bitmap->name); | |
243 | return -1; | |
244 | } | |
245 | ||
246 | if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) { | |
247 | error_setg(errp, "Bitmap '%s' is readonly and cannot be modified", | |
248 | bitmap->name); | |
249 | return -1; | |
250 | } | |
251 | ||
252 | if ((flags & BDRV_BITMAP_INCONSISTENT) && | |
253 | bdrv_dirty_bitmap_inconsistent(bitmap)) { | |
254 | error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used", | |
255 | bitmap->name); | |
256 | error_append_hint(errp, "Try block-dirty-bitmap-remove to delete" | |
257 | " this bitmap from disk"); | |
258 | return -1; | |
259 | } | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
ebab2259 FZ |
264 | /** |
265 | * Create a successor bitmap destined to replace this bitmap after an operation. | |
27a1b301 | 266 | * Requires that the bitmap is not marked busy and has no successor. |
8b2e20f6 | 267 | * The successor will be enabled if the parent bitmap was. |
2119882c | 268 | * Called with BQL taken. |
ebab2259 FZ |
269 | */ |
270 | int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs, | |
271 | BdrvDirtyBitmap *bitmap, Error **errp) | |
272 | { | |
273 | uint64_t granularity; | |
274 | BdrvDirtyBitmap *child; | |
275 | ||
3ae96d66 | 276 | if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) { |
50a47257 JS |
277 | return -1; |
278 | } | |
279 | if (bdrv_dirty_bitmap_has_successor(bitmap)) { | |
280 | error_setg(errp, "Cannot create a successor for a bitmap that already " | |
281 | "has one"); | |
ebab2259 FZ |
282 | return -1; |
283 | } | |
ebab2259 FZ |
284 | |
285 | /* Create an anonymous successor */ | |
286 | granularity = bdrv_dirty_bitmap_granularity(bitmap); | |
287 | child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); | |
288 | if (!child) { | |
289 | return -1; | |
290 | } | |
291 | ||
292 | /* Successor will be on or off based on our current state. */ | |
293 | child->disabled = bitmap->disabled; | |
8b2e20f6 | 294 | bitmap->disabled = true; |
ebab2259 | 295 | |
27a1b301 | 296 | /* Install the successor and mark the parent as busy */ |
ebab2259 | 297 | bitmap->successor = child; |
27a1b301 | 298 | bitmap->busy = true; |
ebab2259 FZ |
299 | return 0; |
300 | } | |
301 | ||
92bcea40 VSO |
302 | void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap) |
303 | { | |
92bcea40 VSO |
304 | bitmap->disabled = false; |
305 | } | |
306 | ||
e73a265e VSO |
307 | /* Called with BQL taken. */ |
308 | void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap) | |
309 | { | |
58f72b96 | 310 | assert(bitmap->mutex == bitmap->successor->mutex); |
e73a265e | 311 | qemu_mutex_lock(bitmap->mutex); |
58f72b96 | 312 | bdrv_enable_dirty_bitmap_locked(bitmap->successor); |
e73a265e VSO |
313 | qemu_mutex_unlock(bitmap->mutex); |
314 | } | |
315 | ||
b133c27f PB |
316 | /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */ |
317 | static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap) | |
604ab74b | 318 | { |
b133c27f | 319 | assert(!bitmap->active_iterators); |
27a1b301 | 320 | assert(!bdrv_dirty_bitmap_busy(bitmap)); |
50a47257 | 321 | assert(!bdrv_dirty_bitmap_has_successor(bitmap)); |
b133c27f PB |
322 | assert(!bitmap->meta); |
323 | QLIST_REMOVE(bitmap, list); | |
324 | hbitmap_free(bitmap->bitmap); | |
325 | g_free(bitmap->name); | |
326 | g_free(bitmap); | |
604ab74b VSO |
327 | } |
328 | ||
ebab2259 FZ |
329 | /** |
330 | * For a bitmap with a successor, yield our name to the successor, | |
331 | * delete the old bitmap, and return a handle to the new bitmap. | |
2119882c | 332 | * Called with BQL taken. |
ebab2259 FZ |
333 | */ |
334 | BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs, | |
335 | BdrvDirtyBitmap *bitmap, | |
336 | Error **errp) | |
337 | { | |
338 | char *name; | |
339 | BdrvDirtyBitmap *successor = bitmap->successor; | |
340 | ||
341 | if (successor == NULL) { | |
342 | error_setg(errp, "Cannot relinquish control if " | |
343 | "there's no successor present"); | |
344 | return NULL; | |
345 | } | |
346 | ||
347 | name = bitmap->name; | |
348 | bitmap->name = NULL; | |
349 | successor->name = name; | |
350 | bitmap->successor = NULL; | |
a88b179f VSO |
351 | successor->persistent = bitmap->persistent; |
352 | bitmap->persistent = false; | |
27a1b301 | 353 | bitmap->busy = false; |
ebab2259 FZ |
354 | bdrv_release_dirty_bitmap(bs, bitmap); |
355 | ||
356 | return successor; | |
357 | } | |
358 | ||
359 | /** | |
360 | * In cases of failure where we can no longer safely delete the parent, | |
361 | * we may wish to re-join the parent and child/successor. | |
27a1b301 | 362 | * The merged parent will be marked as not busy. |
8b2e20f6 | 363 | * The marged parent will be enabled if and only if the successor was enabled. |
044ee8e1 | 364 | * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. |
ebab2259 | 365 | */ |
044ee8e1 VSO |
366 | BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs, |
367 | BdrvDirtyBitmap *parent, | |
368 | Error **errp) | |
ebab2259 | 369 | { |
044ee8e1 | 370 | BdrvDirtyBitmap *successor = parent->successor; |
ebab2259 FZ |
371 | |
372 | if (!successor) { | |
373 | error_setg(errp, "Cannot reclaim a successor when none is present"); | |
374 | return NULL; | |
375 | } | |
376 | ||
fa000f2f | 377 | if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) { |
ebab2259 FZ |
378 | error_setg(errp, "Merging of parent and successor bitmap failed"); |
379 | return NULL; | |
380 | } | |
8b2e20f6 JS |
381 | |
382 | parent->disabled = successor->disabled; | |
27a1b301 | 383 | parent->busy = false; |
b133c27f | 384 | bdrv_release_dirty_bitmap_locked(successor); |
ebab2259 FZ |
385 | parent->successor = NULL; |
386 | ||
044ee8e1 VSO |
387 | return parent; |
388 | } | |
389 | ||
390 | /* Called with BQL taken. */ | |
391 | BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs, | |
392 | BdrvDirtyBitmap *parent, | |
393 | Error **errp) | |
394 | { | |
395 | BdrvDirtyBitmap *ret; | |
396 | ||
397 | qemu_mutex_lock(parent->mutex); | |
398 | ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp); | |
604ab74b VSO |
399 | qemu_mutex_unlock(parent->mutex); |
400 | ||
044ee8e1 | 401 | return ret; |
ebab2259 FZ |
402 | } |
403 | ||
404 | /** | |
405 | * Truncates _all_ bitmaps attached to a BDS. | |
2119882c | 406 | * Called with BQL taken. |
ebab2259 | 407 | */ |
1b6cc579 | 408 | void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes) |
ebab2259 FZ |
409 | { |
410 | BdrvDirtyBitmap *bitmap; | |
ebab2259 | 411 | |
2119882c | 412 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 | 413 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
27a1b301 | 414 | assert(!bdrv_dirty_bitmap_busy(bitmap)); |
50a47257 | 415 | assert(!bdrv_dirty_bitmap_has_successor(bitmap)); |
dc162c8e | 416 | assert(!bitmap->active_iterators); |
ca759622 | 417 | hbitmap_truncate(bitmap->bitmap, bytes); |
993e6525 | 418 | bitmap->size = bytes; |
ebab2259 | 419 | } |
2119882c | 420 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
421 | } |
422 | ||
2119882c | 423 | /* Called with BQL taken. */ |
ebab2259 FZ |
424 | void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) |
425 | { | |
b133c27f PB |
426 | bdrv_dirty_bitmaps_lock(bs); |
427 | bdrv_release_dirty_bitmap_locked(bitmap); | |
428 | bdrv_dirty_bitmaps_unlock(bs); | |
ebab2259 FZ |
429 | } |
430 | ||
431 | /** | |
432 | * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()). | |
27a1b301 | 433 | * There must not be any busy bitmaps attached. |
56f364e6 | 434 | * This function does not remove persistent bitmaps from the storage. |
2119882c | 435 | * Called with BQL taken. |
ebab2259 FZ |
436 | */ |
437 | void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs) | |
438 | { | |
b133c27f PB |
439 | BdrvDirtyBitmap *bm, *next; |
440 | ||
441 | bdrv_dirty_bitmaps_lock(bs); | |
442 | QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { | |
443 | if (bdrv_dirty_bitmap_name(bm)) { | |
444 | bdrv_release_dirty_bitmap_locked(bm); | |
445 | } | |
446 | } | |
447 | bdrv_dirty_bitmaps_unlock(bs); | |
615b5dcf VSO |
448 | } |
449 | ||
56f364e6 VSO |
450 | /** |
451 | * Remove persistent dirty bitmap from the storage if it exists. | |
452 | * Absence of bitmap is not an error, because we have the following scenario: | |
453 | * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no | |
454 | * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should | |
455 | * not fail. | |
456 | * This function doesn't release corresponding BdrvDirtyBitmap. | |
457 | */ | |
458 | void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, | |
459 | const char *name, | |
460 | Error **errp) | |
461 | { | |
462 | if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) { | |
463 | bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp); | |
464 | } | |
465 | } | |
466 | ||
ebab2259 FZ |
467 | void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) |
468 | { | |
8b1402ce | 469 | bdrv_dirty_bitmap_lock(bitmap); |
ebab2259 | 470 | bitmap->disabled = true; |
8b1402ce | 471 | bdrv_dirty_bitmap_unlock(bitmap); |
ebab2259 FZ |
472 | } |
473 | ||
474 | void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) | |
475 | { | |
8b1402ce | 476 | bdrv_dirty_bitmap_lock(bitmap); |
92bcea40 | 477 | bdrv_enable_dirty_bitmap_locked(bitmap); |
8b1402ce | 478 | bdrv_dirty_bitmap_unlock(bitmap); |
ebab2259 FZ |
479 | } |
480 | ||
481 | BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) | |
482 | { | |
483 | BdrvDirtyBitmap *bm; | |
484 | BlockDirtyInfoList *list = NULL; | |
485 | BlockDirtyInfoList **plist = &list; | |
486 | ||
2119882c | 487 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 FZ |
488 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { |
489 | BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); | |
490 | BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1); | |
9a46dba7 | 491 | info->count = bdrv_get_dirty_count(bm); |
ebab2259 FZ |
492 | info->granularity = bdrv_dirty_bitmap_granularity(bm); |
493 | info->has_name = !!bm->name; | |
494 | info->name = g_strdup(bm->name); | |
495 | info->status = bdrv_dirty_bitmap_status(bm); | |
4db6ceb0 | 496 | info->recording = bdrv_dirty_bitmap_recording(bm); |
27a1b301 | 497 | info->busy = bdrv_dirty_bitmap_busy(bm); |
f67cf661 | 498 | info->persistent = bm->persistent; |
b0f45559 JS |
499 | info->has_inconsistent = bm->inconsistent; |
500 | info->inconsistent = bm->inconsistent; | |
ebab2259 FZ |
501 | entry->value = info; |
502 | *plist = entry; | |
503 | plist = &entry->next; | |
504 | } | |
2119882c | 505 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
506 | |
507 | return list; | |
508 | } | |
509 | ||
b64bd51e | 510 | /* Called within bdrv_dirty_bitmap_lock..unlock */ |
28636b82 | 511 | bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset) |
ebab2259 | 512 | { |
28636b82 JS |
513 | return hbitmap_get(bitmap->bitmap, offset); |
514 | } | |
515 | ||
516 | bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset) | |
517 | { | |
518 | bool ret; | |
519 | bdrv_dirty_bitmap_lock(bitmap); | |
520 | ret = bdrv_dirty_bitmap_get_locked(bitmap, offset); | |
521 | bdrv_dirty_bitmap_unlock(bitmap); | |
522 | ||
523 | return ret; | |
ebab2259 FZ |
524 | } |
525 | ||
526 | /** | |
527 | * Chooses a default granularity based on the existing cluster size, | |
528 | * but clamped between [4K, 64K]. Defaults to 64K in the case that there | |
529 | * is no cluster size information available. | |
530 | */ | |
531 | uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) | |
532 | { | |
533 | BlockDriverInfo bdi; | |
534 | uint32_t granularity; | |
535 | ||
536 | if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { | |
537 | granularity = MAX(4096, bdi.cluster_size); | |
538 | granularity = MIN(65536, granularity); | |
539 | } else { | |
540 | granularity = 65536; | |
541 | } | |
542 | ||
543 | return granularity; | |
544 | } | |
545 | ||
ba06ff1a | 546 | uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap) |
ebab2259 | 547 | { |
ca759622 | 548 | return 1U << hbitmap_granularity(bitmap->bitmap); |
ebab2259 FZ |
549 | } |
550 | ||
715a74d8 | 551 | BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap) |
dc162c8e FZ |
552 | { |
553 | BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); | |
715a74d8 | 554 | hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0); |
dc162c8e FZ |
555 | iter->bitmap = bitmap; |
556 | bitmap->active_iterators++; | |
557 | return iter; | |
558 | } | |
559 | ||
6d3f4049 FZ |
560 | BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap) |
561 | { | |
562 | BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); | |
563 | hbitmap_iter_init(&iter->hbi, bitmap->meta, 0); | |
564 | iter->bitmap = bitmap; | |
565 | bitmap->active_iterators++; | |
566 | return iter; | |
567 | } | |
568 | ||
dc162c8e FZ |
569 | void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter) |
570 | { | |
571 | if (!iter) { | |
572 | return; | |
573 | } | |
574 | assert(iter->bitmap->active_iterators > 0); | |
575 | iter->bitmap->active_iterators--; | |
576 | g_free(iter); | |
577 | } | |
578 | ||
579 | int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter) | |
ebab2259 | 580 | { |
19c021e1 | 581 | return hbitmap_iter_next(&iter->hbi); |
ebab2259 FZ |
582 | } |
583 | ||
b64bd51e PB |
584 | /* Called within bdrv_dirty_bitmap_lock..unlock */ |
585 | void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, | |
e0d7f73e | 586 | int64_t offset, int64_t bytes) |
b64bd51e | 587 | { |
d6883bc9 | 588 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ca759622 | 589 | hbitmap_set(bitmap->bitmap, offset, bytes); |
b64bd51e PB |
590 | } |
591 | ||
ebab2259 | 592 | void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, |
e0d7f73e | 593 | int64_t offset, int64_t bytes) |
b64bd51e PB |
594 | { |
595 | bdrv_dirty_bitmap_lock(bitmap); | |
e0d7f73e | 596 | bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes); |
b64bd51e PB |
597 | bdrv_dirty_bitmap_unlock(bitmap); |
598 | } | |
599 | ||
600 | /* Called within bdrv_dirty_bitmap_lock..unlock */ | |
601 | void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, | |
e0d7f73e | 602 | int64_t offset, int64_t bytes) |
ebab2259 | 603 | { |
d6883bc9 | 604 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ca759622 | 605 | hbitmap_reset(bitmap->bitmap, offset, bytes); |
ebab2259 FZ |
606 | } |
607 | ||
608 | void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, | |
e0d7f73e | 609 | int64_t offset, int64_t bytes) |
ebab2259 | 610 | { |
b64bd51e | 611 | bdrv_dirty_bitmap_lock(bitmap); |
e0d7f73e | 612 | bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes); |
b64bd51e | 613 | bdrv_dirty_bitmap_unlock(bitmap); |
ebab2259 FZ |
614 | } |
615 | ||
616 | void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out) | |
617 | { | |
d6883bc9 | 618 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
b64bd51e | 619 | bdrv_dirty_bitmap_lock(bitmap); |
ebab2259 FZ |
620 | if (!out) { |
621 | hbitmap_reset_all(bitmap->bitmap); | |
622 | } else { | |
623 | HBitmap *backup = bitmap->bitmap; | |
ca759622 | 624 | bitmap->bitmap = hbitmap_alloc(bitmap->size, |
ebab2259 FZ |
625 | hbitmap_granularity(backup)); |
626 | *out = backup; | |
627 | } | |
b64bd51e | 628 | bdrv_dirty_bitmap_unlock(bitmap); |
ebab2259 FZ |
629 | } |
630 | ||
56bd6624 | 631 | void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup) |
ebab2259 FZ |
632 | { |
633 | HBitmap *tmp = bitmap->bitmap; | |
d6883bc9 | 634 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
56bd6624 | 635 | bitmap->bitmap = backup; |
ebab2259 FZ |
636 | hbitmap_free(tmp); |
637 | } | |
638 | ||
882c36f5 | 639 | uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap, |
86f6ae67 | 640 | uint64_t offset, uint64_t bytes) |
882c36f5 | 641 | { |
ca759622 | 642 | return hbitmap_serialization_size(bitmap->bitmap, offset, bytes); |
882c36f5 VSO |
643 | } |
644 | ||
645 | uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap) | |
646 | { | |
ca759622 | 647 | return hbitmap_serialization_align(bitmap->bitmap); |
882c36f5 VSO |
648 | } |
649 | ||
650 | void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap, | |
86f6ae67 EB |
651 | uint8_t *buf, uint64_t offset, |
652 | uint64_t bytes) | |
882c36f5 | 653 | { |
ca759622 | 654 | hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes); |
882c36f5 VSO |
655 | } |
656 | ||
657 | void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap, | |
86f6ae67 EB |
658 | uint8_t *buf, uint64_t offset, |
659 | uint64_t bytes, bool finish) | |
882c36f5 | 660 | { |
ca759622 | 661 | hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish); |
882c36f5 VSO |
662 | } |
663 | ||
664 | void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap, | |
86f6ae67 | 665 | uint64_t offset, uint64_t bytes, |
882c36f5 VSO |
666 | bool finish) |
667 | { | |
ca759622 | 668 | hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish); |
6bdc8b71 VSO |
669 | } |
670 | ||
671 | void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap, | |
86f6ae67 | 672 | uint64_t offset, uint64_t bytes, |
6bdc8b71 VSO |
673 | bool finish) |
674 | { | |
ca759622 | 675 | hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish); |
882c36f5 VSO |
676 | } |
677 | ||
678 | void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap) | |
679 | { | |
680 | hbitmap_deserialize_finish(bitmap->bitmap); | |
681 | } | |
682 | ||
0fdf1a4f | 683 | void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes) |
ebab2259 FZ |
684 | { |
685 | BdrvDirtyBitmap *bitmap; | |
2119882c PB |
686 | |
687 | if (QLIST_EMPTY(&bs->dirty_bitmaps)) { | |
688 | return; | |
689 | } | |
690 | ||
691 | bdrv_dirty_bitmaps_lock(bs); | |
ebab2259 FZ |
692 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
693 | if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
694 | continue; | |
695 | } | |
d6883bc9 | 696 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ca759622 | 697 | hbitmap_set(bitmap->bitmap, offset, bytes); |
ebab2259 | 698 | } |
2119882c | 699 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
700 | } |
701 | ||
702 | /** | |
dc162c8e | 703 | * Advance a BdrvDirtyBitmapIter to an arbitrary offset. |
ebab2259 | 704 | */ |
715a74d8 | 705 | void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset) |
ebab2259 | 706 | { |
ca759622 | 707 | hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset); |
ebab2259 FZ |
708 | } |
709 | ||
710 | int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) | |
711 | { | |
ca759622 | 712 | return hbitmap_count(bitmap->bitmap); |
ebab2259 | 713 | } |
6d3f4049 FZ |
714 | |
715 | int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap) | |
716 | { | |
717 | return hbitmap_count(bitmap->meta); | |
718 | } | |
d6883bc9 VSO |
719 | |
720 | bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap) | |
721 | { | |
722 | return bitmap->readonly; | |
723 | } | |
724 | ||
725 | /* Called with BQL taken. */ | |
726 | void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value) | |
727 | { | |
728 | qemu_mutex_lock(bitmap->mutex); | |
729 | bitmap->readonly = value; | |
730 | qemu_mutex_unlock(bitmap->mutex); | |
731 | } | |
732 | ||
733 | bool bdrv_has_readonly_bitmaps(BlockDriverState *bs) | |
734 | { | |
735 | BdrvDirtyBitmap *bm; | |
736 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
737 | if (bm->readonly) { | |
738 | return true; | |
739 | } | |
740 | } | |
741 | ||
742 | return false; | |
743 | } | |
a0319aac | 744 | |
a88b179f | 745 | /* Called with BQL taken. */ |
796a3798 | 746 | void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent) |
a88b179f VSO |
747 | { |
748 | qemu_mutex_lock(bitmap->mutex); | |
749 | bitmap->persistent = persistent; | |
750 | qemu_mutex_unlock(bitmap->mutex); | |
751 | } | |
752 | ||
b0f45559 JS |
753 | /* Called with BQL taken. */ |
754 | void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap) | |
755 | { | |
756 | qemu_mutex_lock(bitmap->mutex); | |
757 | assert(bitmap->persistent == true); | |
758 | bitmap->inconsistent = true; | |
759 | bitmap->disabled = true; | |
760 | qemu_mutex_unlock(bitmap->mutex); | |
761 | } | |
762 | ||
9c98f145 | 763 | /* Called with BQL taken. */ |
c4e4b0fa | 764 | void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip) |
9c98f145 VSO |
765 | { |
766 | qemu_mutex_lock(bitmap->mutex); | |
c4e4b0fa | 767 | bitmap->skip_store = skip; |
9c98f145 VSO |
768 | qemu_mutex_unlock(bitmap->mutex); |
769 | } | |
770 | ||
796a3798 | 771 | bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap) |
a88b179f | 772 | { |
c4e4b0fa | 773 | return bitmap->persistent && !bitmap->skip_store; |
a88b179f VSO |
774 | } |
775 | ||
b0f45559 JS |
776 | bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap) |
777 | { | |
778 | return bitmap->inconsistent; | |
779 | } | |
780 | ||
a88b179f VSO |
781 | bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs) |
782 | { | |
783 | BdrvDirtyBitmap *bm; | |
784 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
c4e4b0fa | 785 | if (bm->persistent && !bm->readonly && !bm->skip_store) { |
a88b179f VSO |
786 | return true; |
787 | } | |
788 | } | |
789 | ||
790 | return false; | |
791 | } | |
3dd10a06 VSO |
792 | |
793 | BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs, | |
794 | BdrvDirtyBitmap *bitmap) | |
795 | { | |
796 | return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) : | |
797 | QLIST_NEXT(bitmap, list); | |
798 | } | |
a3b52535 VSO |
799 | |
800 | char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp) | |
801 | { | |
802 | return hbitmap_sha256(bitmap->bitmap, errp); | |
803 | } | |
56207df5 | 804 | |
76d570dc VSO |
805 | int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset, |
806 | uint64_t bytes) | |
56207df5 | 807 | { |
76d570dc | 808 | return hbitmap_next_zero(bitmap->bitmap, offset, bytes); |
56207df5 | 809 | } |
b598e531 | 810 | |
a78a1a48 VSO |
811 | bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, |
812 | uint64_t *offset, uint64_t *bytes) | |
813 | { | |
814 | return hbitmap_next_dirty_area(bitmap->bitmap, offset, bytes); | |
815 | } | |
816 | ||
b7661ca5 JS |
817 | /** |
818 | * bdrv_merge_dirty_bitmap: merge src into dest. | |
819 | * Ensures permissions on bitmaps are reasonable; use for public API. | |
820 | * | |
821 | * @backup: If provided, make a copy of dest here prior to merge. | |
822 | */ | |
b598e531 | 823 | void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src, |
fa000f2f | 824 | HBitmap **backup, Error **errp) |
b598e531 | 825 | { |
fa000f2f VSO |
826 | bool ret; |
827 | ||
b598e531 | 828 | qemu_mutex_lock(dest->mutex); |
eff0829b VSO |
829 | if (src->mutex != dest->mutex) { |
830 | qemu_mutex_lock(src->mutex); | |
831 | } | |
b598e531 | 832 | |
3ae96d66 | 833 | if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) { |
06bf5006 VSO |
834 | goto out; |
835 | } | |
836 | ||
cb8e58e3 JS |
837 | if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) { |
838 | goto out; | |
839 | } | |
b598e531 | 840 | |
fa000f2f | 841 | if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) { |
b598e531 | 842 | error_setg(errp, "Bitmaps are incompatible and can't be merged"); |
06bf5006 | 843 | goto out; |
b598e531 VSO |
844 | } |
845 | ||
b7661ca5 JS |
846 | ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false); |
847 | assert(ret); | |
848 | ||
849 | out: | |
850 | qemu_mutex_unlock(dest->mutex); | |
851 | if (src->mutex != dest->mutex) { | |
852 | qemu_mutex_unlock(src->mutex); | |
853 | } | |
854 | } | |
855 | ||
856 | /** | |
857 | * bdrv_dirty_bitmap_merge_internal: merge src into dest. | |
858 | * Does NOT check bitmap permissions; not suitable for use as public API. | |
859 | * | |
860 | * @backup: If provided, make a copy of dest here prior to merge. | |
861 | * @lock: If true, lock and unlock bitmaps on the way in/out. | |
862 | * returns true if the merge succeeded; false if unattempted. | |
863 | */ | |
864 | bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest, | |
865 | const BdrvDirtyBitmap *src, | |
866 | HBitmap **backup, | |
867 | bool lock) | |
868 | { | |
869 | bool ret; | |
870 | ||
871 | assert(!bdrv_dirty_bitmap_readonly(dest)); | |
872 | assert(!bdrv_dirty_bitmap_inconsistent(dest)); | |
873 | assert(!bdrv_dirty_bitmap_inconsistent(src)); | |
874 | ||
875 | if (lock) { | |
876 | qemu_mutex_lock(dest->mutex); | |
877 | if (src->mutex != dest->mutex) { | |
878 | qemu_mutex_lock(src->mutex); | |
879 | } | |
880 | } | |
881 | ||
fa000f2f VSO |
882 | if (backup) { |
883 | *backup = dest->bitmap; | |
884 | dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup)); | |
885 | ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap); | |
886 | } else { | |
887 | ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap); | |
888 | } | |
fa000f2f | 889 | |
b7661ca5 JS |
890 | if (lock) { |
891 | qemu_mutex_unlock(dest->mutex); | |
892 | if (src->mutex != dest->mutex) { | |
893 | qemu_mutex_unlock(src->mutex); | |
894 | } | |
eff0829b | 895 | } |
b7661ca5 JS |
896 | |
897 | return ret; | |
b598e531 | 898 | } |