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