]>
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" | |
d2c3080e | 29 | #include "qemu/main-loop.h" |
ebab2259 | 30 | |
ebab2259 | 31 | struct BdrvDirtyBitmap { |
5deb6cbd | 32 | BlockDriverState *bs; |
ca759622 | 33 | HBitmap *bitmap; /* Dirty bitmap implementation */ |
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 | { | |
1e638301 | 74 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
b64bd51e PB |
75 | } |
76 | ||
77 | void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap) | |
78 | { | |
1e638301 | 79 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
b64bd51e PB |
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 | 106 | |
cf7c49cf EB |
107 | if (name) { |
108 | if (bdrv_find_dirty_bitmap(bs, name)) { | |
109 | error_setg(errp, "Bitmap already exists: %s", name); | |
110 | return NULL; | |
111 | } | |
112 | if (strlen(name) > BDRV_BITMAP_MAX_NAME_SIZE) { | |
113 | error_setg(errp, "Bitmap name too long: %s", name); | |
114 | return NULL; | |
115 | } | |
ebab2259 | 116 | } |
993e6525 | 117 | bitmap_size = bdrv_getlength(bs); |
ebab2259 FZ |
118 | if (bitmap_size < 0) { |
119 | error_setg_errno(errp, -bitmap_size, "could not get length of device"); | |
120 | errno = -bitmap_size; | |
121 | return NULL; | |
122 | } | |
123 | bitmap = g_new0(BdrvDirtyBitmap, 1); | |
5deb6cbd | 124 | bitmap->bs = bs; |
ca759622 | 125 | bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity)); |
ebab2259 FZ |
126 | bitmap->size = bitmap_size; |
127 | bitmap->name = g_strdup(name); | |
128 | bitmap->disabled = false; | |
2119882c | 129 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 | 130 | QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); |
2119882c | 131 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
132 | return bitmap; |
133 | } | |
134 | ||
15891fac FZ |
135 | int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap) |
136 | { | |
993e6525 | 137 | return bitmap->size; |
15891fac FZ |
138 | } |
139 | ||
140 | const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap) | |
141 | { | |
142 | return bitmap->name; | |
143 | } | |
144 | ||
2119882c | 145 | /* Called with BQL taken. */ |
50a47257 | 146 | bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap) |
ebab2259 FZ |
147 | { |
148 | return bitmap->successor; | |
149 | } | |
150 | ||
3ae96d66 | 151 | static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap) |
27a1b301 JS |
152 | { |
153 | return bitmap->busy; | |
993edc0c JS |
154 | } |
155 | ||
27a1b301 | 156 | void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy) |
4f43e953 | 157 | { |
1e638301 | 158 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
27a1b301 | 159 | bitmap->busy = busy; |
1e638301 | 160 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
4f43e953 VSO |
161 | } |
162 | ||
2119882c | 163 | /* Called with BQL taken. */ |
ebab2259 FZ |
164 | bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap) |
165 | { | |
8b2e20f6 | 166 | return !bitmap->disabled; |
ebab2259 FZ |
167 | } |
168 | ||
4db6ceb0 JS |
169 | /* Called with BQL taken. */ |
170 | static bool bdrv_dirty_bitmap_recording(BdrvDirtyBitmap *bitmap) | |
171 | { | |
172 | return !bitmap->disabled || (bitmap->successor && | |
173 | !bitmap->successor->disabled); | |
174 | } | |
175 | ||
3ae96d66 JS |
176 | int bdrv_dirty_bitmap_check(const BdrvDirtyBitmap *bitmap, uint32_t flags, |
177 | Error **errp) | |
178 | { | |
179 | if ((flags & BDRV_BITMAP_BUSY) && bdrv_dirty_bitmap_busy(bitmap)) { | |
180 | error_setg(errp, "Bitmap '%s' is currently in use by another" | |
181 | " operation and cannot be used", bitmap->name); | |
182 | return -1; | |
183 | } | |
184 | ||
185 | if ((flags & BDRV_BITMAP_RO) && bdrv_dirty_bitmap_readonly(bitmap)) { | |
186 | error_setg(errp, "Bitmap '%s' is readonly and cannot be modified", | |
187 | bitmap->name); | |
188 | return -1; | |
189 | } | |
190 | ||
191 | if ((flags & BDRV_BITMAP_INCONSISTENT) && | |
192 | bdrv_dirty_bitmap_inconsistent(bitmap)) { | |
193 | error_setg(errp, "Bitmap '%s' is inconsistent and cannot be used", | |
194 | bitmap->name); | |
195 | error_append_hint(errp, "Try block-dirty-bitmap-remove to delete" | |
196 | " this bitmap from disk"); | |
197 | return -1; | |
198 | } | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
ebab2259 FZ |
203 | /** |
204 | * Create a successor bitmap destined to replace this bitmap after an operation. | |
27a1b301 | 205 | * Requires that the bitmap is not marked busy and has no successor. |
8b2e20f6 | 206 | * The successor will be enabled if the parent bitmap was. |
2119882c | 207 | * Called with BQL taken. |
ebab2259 | 208 | */ |
5deb6cbd | 209 | int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp) |
ebab2259 FZ |
210 | { |
211 | uint64_t granularity; | |
212 | BdrvDirtyBitmap *child; | |
213 | ||
3ae96d66 | 214 | if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) { |
50a47257 JS |
215 | return -1; |
216 | } | |
217 | if (bdrv_dirty_bitmap_has_successor(bitmap)) { | |
218 | error_setg(errp, "Cannot create a successor for a bitmap that already " | |
219 | "has one"); | |
ebab2259 FZ |
220 | return -1; |
221 | } | |
ebab2259 FZ |
222 | |
223 | /* Create an anonymous successor */ | |
224 | granularity = bdrv_dirty_bitmap_granularity(bitmap); | |
5deb6cbd | 225 | child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp); |
ebab2259 FZ |
226 | if (!child) { |
227 | return -1; | |
228 | } | |
229 | ||
230 | /* Successor will be on or off based on our current state. */ | |
231 | child->disabled = bitmap->disabled; | |
8b2e20f6 | 232 | bitmap->disabled = true; |
ebab2259 | 233 | |
27a1b301 | 234 | /* Install the successor and mark the parent as busy */ |
ebab2259 | 235 | bitmap->successor = child; |
27a1b301 | 236 | bitmap->busy = true; |
ebab2259 FZ |
237 | return 0; |
238 | } | |
239 | ||
92bcea40 VSO |
240 | void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap) |
241 | { | |
92bcea40 VSO |
242 | bitmap->disabled = false; |
243 | } | |
244 | ||
e73a265e VSO |
245 | /* Called with BQL taken. */ |
246 | void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap) | |
247 | { | |
1e638301 VSO |
248 | assert(bitmap->bs == bitmap->successor->bs); |
249 | bdrv_dirty_bitmaps_lock(bitmap->bs); | |
58f72b96 | 250 | bdrv_enable_dirty_bitmap_locked(bitmap->successor); |
1e638301 | 251 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
e73a265e VSO |
252 | } |
253 | ||
b133c27f PB |
254 | /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */ |
255 | static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap) | |
604ab74b | 256 | { |
b133c27f | 257 | assert(!bitmap->active_iterators); |
27a1b301 | 258 | assert(!bdrv_dirty_bitmap_busy(bitmap)); |
50a47257 | 259 | assert(!bdrv_dirty_bitmap_has_successor(bitmap)); |
b133c27f PB |
260 | QLIST_REMOVE(bitmap, list); |
261 | hbitmap_free(bitmap->bitmap); | |
262 | g_free(bitmap->name); | |
263 | g_free(bitmap); | |
604ab74b VSO |
264 | } |
265 | ||
ebab2259 FZ |
266 | /** |
267 | * For a bitmap with a successor, yield our name to the successor, | |
268 | * delete the old bitmap, and return a handle to the new bitmap. | |
2119882c | 269 | * Called with BQL taken. |
ebab2259 | 270 | */ |
5deb6cbd | 271 | BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap, |
ebab2259 FZ |
272 | Error **errp) |
273 | { | |
274 | char *name; | |
275 | BdrvDirtyBitmap *successor = bitmap->successor; | |
276 | ||
277 | if (successor == NULL) { | |
278 | error_setg(errp, "Cannot relinquish control if " | |
279 | "there's no successor present"); | |
280 | return NULL; | |
281 | } | |
282 | ||
283 | name = bitmap->name; | |
284 | bitmap->name = NULL; | |
285 | successor->name = name; | |
286 | bitmap->successor = NULL; | |
a88b179f VSO |
287 | successor->persistent = bitmap->persistent; |
288 | bitmap->persistent = false; | |
27a1b301 | 289 | bitmap->busy = false; |
5deb6cbd | 290 | bdrv_release_dirty_bitmap(bitmap); |
ebab2259 FZ |
291 | |
292 | return successor; | |
293 | } | |
294 | ||
295 | /** | |
296 | * In cases of failure where we can no longer safely delete the parent, | |
297 | * we may wish to re-join the parent and child/successor. | |
27a1b301 | 298 | * The merged parent will be marked as not busy. |
8b2e20f6 | 299 | * The marged parent will be enabled if and only if the successor was enabled. |
044ee8e1 | 300 | * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. |
ebab2259 | 301 | */ |
5deb6cbd | 302 | BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent, |
044ee8e1 | 303 | Error **errp) |
ebab2259 | 304 | { |
044ee8e1 | 305 | BdrvDirtyBitmap *successor = parent->successor; |
ebab2259 FZ |
306 | |
307 | if (!successor) { | |
308 | error_setg(errp, "Cannot reclaim a successor when none is present"); | |
309 | return NULL; | |
310 | } | |
311 | ||
fa000f2f | 312 | if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) { |
ebab2259 FZ |
313 | error_setg(errp, "Merging of parent and successor bitmap failed"); |
314 | return NULL; | |
315 | } | |
8b2e20f6 JS |
316 | |
317 | parent->disabled = successor->disabled; | |
27a1b301 | 318 | parent->busy = false; |
b133c27f | 319 | bdrv_release_dirty_bitmap_locked(successor); |
ebab2259 FZ |
320 | parent->successor = NULL; |
321 | ||
044ee8e1 VSO |
322 | return parent; |
323 | } | |
324 | ||
325 | /* Called with BQL taken. */ | |
5deb6cbd | 326 | BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent, |
044ee8e1 VSO |
327 | Error **errp) |
328 | { | |
329 | BdrvDirtyBitmap *ret; | |
330 | ||
1e638301 | 331 | bdrv_dirty_bitmaps_lock(parent->bs); |
5deb6cbd | 332 | ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp); |
1e638301 | 333 | bdrv_dirty_bitmaps_unlock(parent->bs); |
604ab74b | 334 | |
044ee8e1 | 335 | return ret; |
ebab2259 FZ |
336 | } |
337 | ||
338 | /** | |
339 | * Truncates _all_ bitmaps attached to a BDS. | |
2119882c | 340 | * Called with BQL taken. |
ebab2259 | 341 | */ |
1b6cc579 | 342 | void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes) |
ebab2259 FZ |
343 | { |
344 | BdrvDirtyBitmap *bitmap; | |
ebab2259 | 345 | |
2119882c | 346 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 | 347 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
27a1b301 | 348 | assert(!bdrv_dirty_bitmap_busy(bitmap)); |
50a47257 | 349 | assert(!bdrv_dirty_bitmap_has_successor(bitmap)); |
dc162c8e | 350 | assert(!bitmap->active_iterators); |
ca759622 | 351 | hbitmap_truncate(bitmap->bitmap, bytes); |
993e6525 | 352 | bitmap->size = bytes; |
ebab2259 | 353 | } |
2119882c | 354 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
355 | } |
356 | ||
2119882c | 357 | /* Called with BQL taken. */ |
5deb6cbd | 358 | void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap) |
ebab2259 | 359 | { |
5deb6cbd VSO |
360 | BlockDriverState *bs = bitmap->bs; |
361 | ||
b133c27f PB |
362 | bdrv_dirty_bitmaps_lock(bs); |
363 | bdrv_release_dirty_bitmap_locked(bitmap); | |
364 | bdrv_dirty_bitmaps_unlock(bs); | |
ebab2259 FZ |
365 | } |
366 | ||
367 | /** | |
368 | * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()). | |
27a1b301 | 369 | * There must not be any busy bitmaps attached. |
56f364e6 | 370 | * This function does not remove persistent bitmaps from the storage. |
2119882c | 371 | * Called with BQL taken. |
ebab2259 FZ |
372 | */ |
373 | void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs) | |
374 | { | |
b133c27f PB |
375 | BdrvDirtyBitmap *bm, *next; |
376 | ||
377 | bdrv_dirty_bitmaps_lock(bs); | |
378 | QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { | |
379 | if (bdrv_dirty_bitmap_name(bm)) { | |
380 | bdrv_release_dirty_bitmap_locked(bm); | |
381 | } | |
382 | } | |
383 | bdrv_dirty_bitmaps_unlock(bs); | |
615b5dcf VSO |
384 | } |
385 | ||
56f364e6 VSO |
386 | /** |
387 | * Remove persistent dirty bitmap from the storage if it exists. | |
388 | * Absence of bitmap is not an error, because we have the following scenario: | |
389 | * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no | |
390 | * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should | |
391 | * not fail. | |
392 | * This function doesn't release corresponding BdrvDirtyBitmap. | |
393 | */ | |
d2c3080e VSO |
394 | static int coroutine_fn |
395 | bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name, | |
396 | Error **errp) | |
56f364e6 | 397 | { |
d2c3080e VSO |
398 | if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) { |
399 | return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp); | |
56f364e6 | 400 | } |
b56a1e31 VSO |
401 | |
402 | return 0; | |
56f364e6 VSO |
403 | } |
404 | ||
d2c3080e VSO |
405 | typedef struct BdrvRemovePersistentDirtyBitmapCo { |
406 | BlockDriverState *bs; | |
407 | const char *name; | |
408 | Error **errp; | |
409 | int ret; | |
410 | } BdrvRemovePersistentDirtyBitmapCo; | |
411 | ||
412 | static void coroutine_fn | |
413 | bdrv_co_remove_persistent_dirty_bitmap_entry(void *opaque) | |
414 | { | |
415 | BdrvRemovePersistentDirtyBitmapCo *s = opaque; | |
416 | ||
417 | s->ret = bdrv_co_remove_persistent_dirty_bitmap(s->bs, s->name, s->errp); | |
418 | aio_wait_kick(); | |
419 | } | |
420 | ||
421 | int bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name, | |
422 | Error **errp) | |
423 | { | |
424 | if (qemu_in_coroutine()) { | |
425 | return bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp); | |
426 | } else { | |
427 | Coroutine *co; | |
428 | BdrvRemovePersistentDirtyBitmapCo s = { | |
429 | .bs = bs, | |
430 | .name = name, | |
431 | .errp = errp, | |
432 | .ret = -EINPROGRESS, | |
433 | }; | |
434 | ||
435 | co = qemu_coroutine_create(bdrv_co_remove_persistent_dirty_bitmap_entry, | |
436 | &s); | |
437 | bdrv_coroutine_enter(bs, co); | |
438 | BDRV_POLL_WHILE(bs, s.ret == -EINPROGRESS); | |
439 | ||
440 | return s.ret; | |
441 | } | |
442 | } | |
443 | ||
ef893b5c EB |
444 | bool |
445 | bdrv_supports_persistent_dirty_bitmap(BlockDriverState *bs) | |
446 | { | |
447 | if (bs->drv && bs->drv->bdrv_supports_persistent_dirty_bitmap) { | |
448 | return bs->drv->bdrv_supports_persistent_dirty_bitmap(bs); | |
449 | } | |
450 | return false; | |
451 | } | |
452 | ||
d2c3080e VSO |
453 | static bool coroutine_fn |
454 | bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, | |
455 | uint32_t granularity, Error **errp) | |
85cc8a4f VSO |
456 | { |
457 | BlockDriver *drv = bs->drv; | |
458 | ||
459 | if (!drv) { | |
460 | error_setg_errno(errp, ENOMEDIUM, | |
461 | "Can't store persistent bitmaps to %s", | |
462 | bdrv_get_device_or_node_name(bs)); | |
463 | return false; | |
464 | } | |
465 | ||
d2c3080e | 466 | if (!drv->bdrv_co_can_store_new_dirty_bitmap) { |
85cc8a4f VSO |
467 | error_setg_errno(errp, ENOTSUP, |
468 | "Can't store persistent bitmaps to %s", | |
469 | bdrv_get_device_or_node_name(bs)); | |
470 | return false; | |
471 | } | |
472 | ||
d2c3080e VSO |
473 | return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp); |
474 | } | |
475 | ||
476 | typedef struct BdrvCanStoreNewDirtyBitmapCo { | |
477 | BlockDriverState *bs; | |
478 | const char *name; | |
479 | uint32_t granularity; | |
480 | Error **errp; | |
481 | bool ret; | |
482 | ||
483 | bool in_progress; | |
484 | } BdrvCanStoreNewDirtyBitmapCo; | |
485 | ||
486 | static void coroutine_fn bdrv_co_can_store_new_dirty_bitmap_entry(void *opaque) | |
487 | { | |
488 | BdrvCanStoreNewDirtyBitmapCo *s = opaque; | |
489 | ||
490 | s->ret = bdrv_co_can_store_new_dirty_bitmap(s->bs, s->name, s->granularity, | |
491 | s->errp); | |
492 | s->in_progress = false; | |
493 | aio_wait_kick(); | |
494 | } | |
495 | ||
496 | bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, | |
497 | uint32_t granularity, Error **errp) | |
498 | { | |
499 | if (qemu_in_coroutine()) { | |
500 | return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp); | |
501 | } else { | |
502 | Coroutine *co; | |
503 | BdrvCanStoreNewDirtyBitmapCo s = { | |
504 | .bs = bs, | |
505 | .name = name, | |
506 | .granularity = granularity, | |
507 | .errp = errp, | |
508 | .in_progress = true, | |
509 | }; | |
510 | ||
511 | co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry, | |
512 | &s); | |
513 | bdrv_coroutine_enter(bs, co); | |
514 | BDRV_POLL_WHILE(bs, s.in_progress); | |
515 | ||
516 | return s.ret; | |
517 | } | |
85cc8a4f VSO |
518 | } |
519 | ||
ebab2259 FZ |
520 | void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) |
521 | { | |
1e638301 | 522 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
ebab2259 | 523 | bitmap->disabled = true; |
1e638301 | 524 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
ebab2259 FZ |
525 | } |
526 | ||
527 | void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) | |
528 | { | |
1e638301 | 529 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
92bcea40 | 530 | bdrv_enable_dirty_bitmap_locked(bitmap); |
1e638301 | 531 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
ebab2259 FZ |
532 | } |
533 | ||
534 | BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) | |
535 | { | |
536 | BdrvDirtyBitmap *bm; | |
537 | BlockDirtyInfoList *list = NULL; | |
c3033fd3 | 538 | BlockDirtyInfoList **tail = &list; |
ebab2259 | 539 | |
2119882c | 540 | bdrv_dirty_bitmaps_lock(bs); |
ebab2259 FZ |
541 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { |
542 | BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); | |
c3033fd3 | 543 | |
9a46dba7 | 544 | info->count = bdrv_get_dirty_count(bm); |
ebab2259 FZ |
545 | info->granularity = bdrv_dirty_bitmap_granularity(bm); |
546 | info->has_name = !!bm->name; | |
547 | info->name = g_strdup(bm->name); | |
4db6ceb0 | 548 | info->recording = bdrv_dirty_bitmap_recording(bm); |
27a1b301 | 549 | info->busy = bdrv_dirty_bitmap_busy(bm); |
f67cf661 | 550 | info->persistent = bm->persistent; |
b0f45559 JS |
551 | info->has_inconsistent = bm->inconsistent; |
552 | info->inconsistent = bm->inconsistent; | |
c3033fd3 | 553 | QAPI_LIST_APPEND(tail, info); |
ebab2259 | 554 | } |
2119882c | 555 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
556 | |
557 | return list; | |
558 | } | |
559 | ||
b64bd51e | 560 | /* Called within bdrv_dirty_bitmap_lock..unlock */ |
28636b82 | 561 | bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset) |
ebab2259 | 562 | { |
28636b82 JS |
563 | return hbitmap_get(bitmap->bitmap, offset); |
564 | } | |
565 | ||
566 | bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset) | |
567 | { | |
568 | bool ret; | |
1e638301 | 569 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
28636b82 | 570 | ret = bdrv_dirty_bitmap_get_locked(bitmap, offset); |
1e638301 | 571 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
28636b82 JS |
572 | |
573 | return ret; | |
ebab2259 FZ |
574 | } |
575 | ||
576 | /** | |
577 | * Chooses a default granularity based on the existing cluster size, | |
578 | * but clamped between [4K, 64K]. Defaults to 64K in the case that there | |
579 | * is no cluster size information available. | |
580 | */ | |
581 | uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) | |
582 | { | |
583 | BlockDriverInfo bdi; | |
584 | uint32_t granularity; | |
585 | ||
586 | if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { | |
587 | granularity = MAX(4096, bdi.cluster_size); | |
588 | granularity = MIN(65536, granularity); | |
589 | } else { | |
590 | granularity = 65536; | |
591 | } | |
592 | ||
593 | return granularity; | |
594 | } | |
595 | ||
ba06ff1a | 596 | uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap) |
ebab2259 | 597 | { |
ca759622 | 598 | return 1U << hbitmap_granularity(bitmap->bitmap); |
ebab2259 FZ |
599 | } |
600 | ||
715a74d8 | 601 | BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap) |
dc162c8e FZ |
602 | { |
603 | BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); | |
715a74d8 | 604 | hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0); |
dc162c8e FZ |
605 | iter->bitmap = bitmap; |
606 | bitmap->active_iterators++; | |
607 | return iter; | |
608 | } | |
609 | ||
610 | void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter) | |
611 | { | |
612 | if (!iter) { | |
613 | return; | |
614 | } | |
615 | assert(iter->bitmap->active_iterators > 0); | |
616 | iter->bitmap->active_iterators--; | |
617 | g_free(iter); | |
618 | } | |
619 | ||
620 | int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter) | |
ebab2259 | 621 | { |
19c021e1 | 622 | return hbitmap_iter_next(&iter->hbi); |
ebab2259 FZ |
623 | } |
624 | ||
b64bd51e PB |
625 | /* Called within bdrv_dirty_bitmap_lock..unlock */ |
626 | void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, | |
e0d7f73e | 627 | int64_t offset, int64_t bytes) |
b64bd51e | 628 | { |
d6883bc9 | 629 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ca759622 | 630 | hbitmap_set(bitmap->bitmap, offset, bytes); |
b64bd51e PB |
631 | } |
632 | ||
ebab2259 | 633 | void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, |
e0d7f73e | 634 | int64_t offset, int64_t bytes) |
b64bd51e | 635 | { |
1e638301 | 636 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
e0d7f73e | 637 | bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes); |
1e638301 | 638 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
b64bd51e PB |
639 | } |
640 | ||
641 | /* Called within bdrv_dirty_bitmap_lock..unlock */ | |
642 | void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, | |
e0d7f73e | 643 | int64_t offset, int64_t bytes) |
ebab2259 | 644 | { |
d6883bc9 | 645 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ca759622 | 646 | hbitmap_reset(bitmap->bitmap, offset, bytes); |
ebab2259 FZ |
647 | } |
648 | ||
649 | void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, | |
e0d7f73e | 650 | int64_t offset, int64_t bytes) |
ebab2259 | 651 | { |
1e638301 | 652 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
e0d7f73e | 653 | bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes); |
1e638301 | 654 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
ebab2259 FZ |
655 | } |
656 | ||
657 | void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out) | |
658 | { | |
d6883bc9 | 659 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
1e638301 | 660 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
ebab2259 FZ |
661 | if (!out) { |
662 | hbitmap_reset_all(bitmap->bitmap); | |
663 | } else { | |
664 | HBitmap *backup = bitmap->bitmap; | |
ca759622 | 665 | bitmap->bitmap = hbitmap_alloc(bitmap->size, |
ebab2259 FZ |
666 | hbitmap_granularity(backup)); |
667 | *out = backup; | |
668 | } | |
1e638301 | 669 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
ebab2259 FZ |
670 | } |
671 | ||
56bd6624 | 672 | void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup) |
ebab2259 FZ |
673 | { |
674 | HBitmap *tmp = bitmap->bitmap; | |
d6883bc9 | 675 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
56bd6624 | 676 | bitmap->bitmap = backup; |
ebab2259 FZ |
677 | hbitmap_free(tmp); |
678 | } | |
679 | ||
882c36f5 | 680 | uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap, |
86f6ae67 | 681 | uint64_t offset, uint64_t bytes) |
882c36f5 | 682 | { |
ca759622 | 683 | return hbitmap_serialization_size(bitmap->bitmap, offset, bytes); |
882c36f5 VSO |
684 | } |
685 | ||
686 | uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap) | |
687 | { | |
ca759622 | 688 | return hbitmap_serialization_align(bitmap->bitmap); |
882c36f5 VSO |
689 | } |
690 | ||
35f428ba VSO |
691 | /* Return the disk size covered by a chunk of serialized bitmap data. */ |
692 | uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, | |
693 | const BdrvDirtyBitmap *bitmap) | |
694 | { | |
695 | uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap); | |
696 | uint64_t limit = granularity * (serialized_chunk_size << 3); | |
697 | ||
698 | assert(QEMU_IS_ALIGNED(limit, | |
699 | bdrv_dirty_bitmap_serialization_align(bitmap))); | |
700 | return limit; | |
701 | } | |
702 | ||
703 | ||
882c36f5 | 704 | void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap, |
86f6ae67 EB |
705 | uint8_t *buf, uint64_t offset, |
706 | uint64_t bytes) | |
882c36f5 | 707 | { |
ca759622 | 708 | hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes); |
882c36f5 VSO |
709 | } |
710 | ||
711 | void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap, | |
86f6ae67 EB |
712 | uint8_t *buf, uint64_t offset, |
713 | uint64_t bytes, bool finish) | |
882c36f5 | 714 | { |
ca759622 | 715 | hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish); |
882c36f5 VSO |
716 | } |
717 | ||
718 | void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap, | |
86f6ae67 | 719 | uint64_t offset, uint64_t bytes, |
882c36f5 VSO |
720 | bool finish) |
721 | { | |
ca759622 | 722 | hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish); |
6bdc8b71 VSO |
723 | } |
724 | ||
725 | void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap, | |
86f6ae67 | 726 | uint64_t offset, uint64_t bytes, |
6bdc8b71 VSO |
727 | bool finish) |
728 | { | |
ca759622 | 729 | hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish); |
882c36f5 VSO |
730 | } |
731 | ||
732 | void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap) | |
733 | { | |
734 | hbitmap_deserialize_finish(bitmap->bitmap); | |
735 | } | |
736 | ||
0fdf1a4f | 737 | void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes) |
ebab2259 FZ |
738 | { |
739 | BdrvDirtyBitmap *bitmap; | |
2119882c PB |
740 | |
741 | if (QLIST_EMPTY(&bs->dirty_bitmaps)) { | |
742 | return; | |
743 | } | |
744 | ||
745 | bdrv_dirty_bitmaps_lock(bs); | |
ebab2259 FZ |
746 | QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { |
747 | if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
748 | continue; | |
749 | } | |
d6883bc9 | 750 | assert(!bdrv_dirty_bitmap_readonly(bitmap)); |
ca759622 | 751 | hbitmap_set(bitmap->bitmap, offset, bytes); |
ebab2259 | 752 | } |
2119882c | 753 | bdrv_dirty_bitmaps_unlock(bs); |
ebab2259 FZ |
754 | } |
755 | ||
756 | /** | |
dc162c8e | 757 | * Advance a BdrvDirtyBitmapIter to an arbitrary offset. |
ebab2259 | 758 | */ |
715a74d8 | 759 | void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset) |
ebab2259 | 760 | { |
ca759622 | 761 | hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset); |
ebab2259 FZ |
762 | } |
763 | ||
764 | int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) | |
765 | { | |
ca759622 | 766 | return hbitmap_count(bitmap->bitmap); |
ebab2259 | 767 | } |
6d3f4049 | 768 | |
d6883bc9 VSO |
769 | bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap) |
770 | { | |
771 | return bitmap->readonly; | |
772 | } | |
773 | ||
774 | /* Called with BQL taken. */ | |
775 | void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value) | |
776 | { | |
1e638301 | 777 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
d6883bc9 | 778 | bitmap->readonly = value; |
1e638301 | 779 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
d6883bc9 VSO |
780 | } |
781 | ||
782 | bool bdrv_has_readonly_bitmaps(BlockDriverState *bs) | |
783 | { | |
784 | BdrvDirtyBitmap *bm; | |
785 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
786 | if (bm->readonly) { | |
787 | return true; | |
788 | } | |
789 | } | |
790 | ||
791 | return false; | |
792 | } | |
a0319aac | 793 | |
7ae89a0d VSO |
794 | bool bdrv_has_named_bitmaps(BlockDriverState *bs) |
795 | { | |
796 | BdrvDirtyBitmap *bm; | |
797 | ||
798 | QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { | |
799 | if (bdrv_dirty_bitmap_name(bm)) { | |
800 | return true; | |
801 | } | |
802 | } | |
803 | ||
804 | return false; | |
805 | } | |
806 | ||
a88b179f | 807 | /* Called with BQL taken. */ |
796a3798 | 808 | void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent) |
a88b179f | 809 | { |
1e638301 | 810 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
a88b179f | 811 | bitmap->persistent = persistent; |
1e638301 | 812 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
a88b179f VSO |
813 | } |
814 | ||
b0f45559 JS |
815 | /* Called with BQL taken. */ |
816 | void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap) | |
817 | { | |
1e638301 | 818 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
b0f45559 JS |
819 | assert(bitmap->persistent == true); |
820 | bitmap->inconsistent = true; | |
821 | bitmap->disabled = true; | |
1e638301 | 822 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
b0f45559 JS |
823 | } |
824 | ||
9c98f145 | 825 | /* Called with BQL taken. */ |
c4e4b0fa | 826 | void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip) |
9c98f145 | 827 | { |
1e638301 | 828 | bdrv_dirty_bitmaps_lock(bitmap->bs); |
c4e4b0fa | 829 | bitmap->skip_store = skip; |
1e638301 | 830 | bdrv_dirty_bitmaps_unlock(bitmap->bs); |
9c98f145 VSO |
831 | } |
832 | ||
796a3798 | 833 | bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap) |
a88b179f | 834 | { |
c4e4b0fa | 835 | return bitmap->persistent && !bitmap->skip_store; |
a88b179f VSO |
836 | } |
837 | ||
b0f45559 JS |
838 | bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap) |
839 | { | |
840 | return bitmap->inconsistent; | |
841 | } | |
842 | ||
ef9041a7 | 843 | BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs) |
3dd10a06 | 844 | { |
ef9041a7 VSO |
845 | return QLIST_FIRST(&bs->dirty_bitmaps); |
846 | } | |
847 | ||
848 | BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap) | |
849 | { | |
850 | return QLIST_NEXT(bitmap, list); | |
3dd10a06 | 851 | } |
a3b52535 VSO |
852 | |
853 | char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp) | |
854 | { | |
855 | return hbitmap_sha256(bitmap->bitmap, errp); | |
856 | } | |
56207df5 | 857 | |
9399c54b VSO |
858 | int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *bitmap, int64_t offset, |
859 | int64_t bytes) | |
860 | { | |
861 | return hbitmap_next_dirty(bitmap->bitmap, offset, bytes); | |
862 | } | |
863 | ||
642700fd VSO |
864 | int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset, |
865 | int64_t bytes) | |
56207df5 | 866 | { |
76d570dc | 867 | return hbitmap_next_zero(bitmap->bitmap, offset, bytes); |
56207df5 | 868 | } |
b598e531 | 869 | |
a78a1a48 | 870 | bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, |
299ea9ff VSO |
871 | int64_t start, int64_t end, int64_t max_dirty_count, |
872 | int64_t *dirty_start, int64_t *dirty_count) | |
a78a1a48 | 873 | { |
299ea9ff VSO |
874 | return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_count, |
875 | dirty_start, dirty_count); | |
a78a1a48 VSO |
876 | } |
877 | ||
b7661ca5 JS |
878 | /** |
879 | * bdrv_merge_dirty_bitmap: merge src into dest. | |
880 | * Ensures permissions on bitmaps are reasonable; use for public API. | |
881 | * | |
882 | * @backup: If provided, make a copy of dest here prior to merge. | |
883 | */ | |
b598e531 | 884 | void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src, |
fa000f2f | 885 | HBitmap **backup, Error **errp) |
b598e531 | 886 | { |
fa000f2f VSO |
887 | bool ret; |
888 | ||
1e638301 VSO |
889 | bdrv_dirty_bitmaps_lock(dest->bs); |
890 | if (src->bs != dest->bs) { | |
891 | bdrv_dirty_bitmaps_lock(src->bs); | |
eff0829b | 892 | } |
b598e531 | 893 | |
3ae96d66 | 894 | if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) { |
06bf5006 VSO |
895 | goto out; |
896 | } | |
897 | ||
cb8e58e3 JS |
898 | if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) { |
899 | goto out; | |
900 | } | |
b598e531 | 901 | |
fa000f2f | 902 | if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) { |
b598e531 | 903 | error_setg(errp, "Bitmaps are incompatible and can't be merged"); |
06bf5006 | 904 | goto out; |
b598e531 VSO |
905 | } |
906 | ||
b7661ca5 JS |
907 | ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false); |
908 | assert(ret); | |
909 | ||
910 | out: | |
1e638301 VSO |
911 | bdrv_dirty_bitmaps_unlock(dest->bs); |
912 | if (src->bs != dest->bs) { | |
913 | bdrv_dirty_bitmaps_unlock(src->bs); | |
b7661ca5 JS |
914 | } |
915 | } | |
916 | ||
917 | /** | |
918 | * bdrv_dirty_bitmap_merge_internal: merge src into dest. | |
919 | * Does NOT check bitmap permissions; not suitable for use as public API. | |
920 | * | |
921 | * @backup: If provided, make a copy of dest here prior to merge. | |
922 | * @lock: If true, lock and unlock bitmaps on the way in/out. | |
923 | * returns true if the merge succeeded; false if unattempted. | |
924 | */ | |
925 | bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest, | |
926 | const BdrvDirtyBitmap *src, | |
927 | HBitmap **backup, | |
928 | bool lock) | |
929 | { | |
930 | bool ret; | |
931 | ||
932 | assert(!bdrv_dirty_bitmap_readonly(dest)); | |
933 | assert(!bdrv_dirty_bitmap_inconsistent(dest)); | |
934 | assert(!bdrv_dirty_bitmap_inconsistent(src)); | |
935 | ||
936 | if (lock) { | |
1e638301 VSO |
937 | bdrv_dirty_bitmaps_lock(dest->bs); |
938 | if (src->bs != dest->bs) { | |
939 | bdrv_dirty_bitmaps_lock(src->bs); | |
b7661ca5 JS |
940 | } |
941 | } | |
942 | ||
fa000f2f VSO |
943 | if (backup) { |
944 | *backup = dest->bitmap; | |
945 | dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup)); | |
946 | ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap); | |
947 | } else { | |
948 | ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap); | |
949 | } | |
fa000f2f | 950 | |
b7661ca5 | 951 | if (lock) { |
1e638301 VSO |
952 | bdrv_dirty_bitmaps_unlock(dest->bs); |
953 | if (src->bs != dest->bs) { | |
954 | bdrv_dirty_bitmaps_unlock(src->bs); | |
b7661ca5 | 955 | } |
eff0829b | 956 | } |
b7661ca5 JS |
957 | |
958 | return ret; | |
b598e531 | 959 | } |