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