1 /* 2 * Block Dirty Bitmap 3 * 4 * Copyright (c) 2016-2017 Red Hat. Inc 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 #include "qemu/osdep.h" 25 #include "qapi/error.h" 26 #include "trace.h" 27 #include "block/block_int.h" 28 #include "block/blockjob.h" 29 #include "qemu/main-loop.h" 30 31 struct BdrvDirtyBitmap { 32 BlockDriverState *bs; 33 HBitmap *bitmap; /* Dirty bitmap implementation */ 34 bool busy; /* Bitmap is busy, it can't be used via QMP */ 35 BdrvDirtyBitmap *successor; /* Anonymous child, if any. */ 36 char *name; /* Optional non-empty unique ID */ 37 int64_t size; /* Size of the bitmap, in bytes */ 38 bool disabled; /* Bitmap is disabled. It ignores all writes to 39 the device */ 40 int active_iterators; /* How many iterators are active */ 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. */ 47 bool persistent; /* bitmap must be saved to owner disk image */ 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. */ 51 bool skip_store; /* We are either migrating or deleting this 52 * bitmap; it should not be stored on the next 53 * inactivation. */ 54 QLIST_ENTRY(BdrvDirtyBitmap) list; 55 }; 56 57 struct BdrvDirtyBitmapIter { 58 HBitmapIter hbi; 59 BdrvDirtyBitmap *bitmap; 60 }; 61 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 72 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap) 73 { 74 bdrv_dirty_bitmaps_lock(bitmap->bs); 75 } 76 77 void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap) 78 { 79 bdrv_dirty_bitmaps_unlock(bitmap->bs); 80 } 81 82 /* Called with BQL or dirty_bitmap lock taken. */ 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 96 /* Called with BQL taken. */ 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; 104 105 assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE); 106 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 } 116 } 117 bitmap_size = bdrv_getlength(bs); 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); 124 bitmap->bs = bs; 125 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity)); 126 bitmap->size = bitmap_size; 127 bitmap->name = g_strdup(name); 128 bitmap->disabled = false; 129 bdrv_dirty_bitmaps_lock(bs); 130 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 131 bdrv_dirty_bitmaps_unlock(bs); 132 return bitmap; 133 } 134 135 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap) 136 { 137 return bitmap->size; 138 } 139 140 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap) 141 { 142 return bitmap->name; 143 } 144 145 /* Called with BQL taken. */ 146 bool bdrv_dirty_bitmap_has_successor(BdrvDirtyBitmap *bitmap) 147 { 148 return bitmap->successor; 149 } 150 151 static bool bdrv_dirty_bitmap_busy(const BdrvDirtyBitmap *bitmap) 152 { 153 return bitmap->busy; 154 } 155 156 void bdrv_dirty_bitmap_set_busy(BdrvDirtyBitmap *bitmap, bool busy) 157 { 158 bdrv_dirty_bitmaps_lock(bitmap->bs); 159 bitmap->busy = busy; 160 bdrv_dirty_bitmaps_unlock(bitmap->bs); 161 } 162 163 /* Called with BQL taken. */ 164 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap) 165 { 166 return !bitmap->disabled; 167 } 168 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 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\n"); 197 return -1; 198 } 199 200 return 0; 201 } 202 203 /** 204 * Create a successor bitmap destined to replace this bitmap after an operation. 205 * Requires that the bitmap is not marked busy and has no successor. 206 * The successor will be enabled if the parent bitmap was. 207 * Called with BQL taken. 208 */ 209 int bdrv_dirty_bitmap_create_successor(BdrvDirtyBitmap *bitmap, Error **errp) 210 { 211 uint64_t granularity; 212 BdrvDirtyBitmap *child; 213 214 if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY, errp)) { 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"); 220 return -1; 221 } 222 223 /* Create an anonymous successor */ 224 granularity = bdrv_dirty_bitmap_granularity(bitmap); 225 child = bdrv_create_dirty_bitmap(bitmap->bs, granularity, NULL, errp); 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; 232 bitmap->disabled = true; 233 234 /* Install the successor and mark the parent as busy */ 235 bitmap->successor = child; 236 bitmap->busy = true; 237 return 0; 238 } 239 240 void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap) 241 { 242 bitmap->disabled = false; 243 } 244 245 /* Called with BQL taken. */ 246 void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap) 247 { 248 assert(bitmap->bs == bitmap->successor->bs); 249 bdrv_dirty_bitmaps_lock(bitmap->bs); 250 bdrv_enable_dirty_bitmap_locked(bitmap->successor); 251 bdrv_dirty_bitmaps_unlock(bitmap->bs); 252 } 253 254 /* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */ 255 static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap) 256 { 257 assert(!bitmap->active_iterators); 258 assert(!bdrv_dirty_bitmap_busy(bitmap)); 259 assert(!bdrv_dirty_bitmap_has_successor(bitmap)); 260 QLIST_REMOVE(bitmap, list); 261 hbitmap_free(bitmap->bitmap); 262 g_free(bitmap->name); 263 g_free(bitmap); 264 } 265 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. 269 * Called with BQL taken. 270 */ 271 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BdrvDirtyBitmap *bitmap, 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; 287 successor->persistent = bitmap->persistent; 288 bitmap->persistent = false; 289 bitmap->busy = false; 290 bdrv_release_dirty_bitmap(bitmap); 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. 298 * The merged parent will be marked as not busy. 299 * The marged parent will be enabled if and only if the successor was enabled. 300 * Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. 301 */ 302 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BdrvDirtyBitmap *parent, 303 Error **errp) 304 { 305 BdrvDirtyBitmap *successor = parent->successor; 306 307 if (!successor) { 308 error_setg(errp, "Cannot reclaim a successor when none is present"); 309 return NULL; 310 } 311 312 if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) { 313 error_setg(errp, "Merging of parent and successor bitmap failed"); 314 return NULL; 315 } 316 317 parent->disabled = successor->disabled; 318 parent->busy = false; 319 bdrv_release_dirty_bitmap_locked(successor); 320 parent->successor = NULL; 321 322 return parent; 323 } 324 325 /* Called with BQL taken. */ 326 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent, 327 Error **errp) 328 { 329 BdrvDirtyBitmap *ret; 330 331 bdrv_dirty_bitmaps_lock(parent->bs); 332 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp); 333 bdrv_dirty_bitmaps_unlock(parent->bs); 334 335 return ret; 336 } 337 338 /** 339 * Truncates _all_ bitmaps attached to a BDS. 340 * Called with BQL taken. 341 */ 342 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes) 343 { 344 BdrvDirtyBitmap *bitmap; 345 346 bdrv_dirty_bitmaps_lock(bs); 347 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 348 assert(!bdrv_dirty_bitmap_busy(bitmap)); 349 assert(!bdrv_dirty_bitmap_has_successor(bitmap)); 350 assert(!bitmap->active_iterators); 351 hbitmap_truncate(bitmap->bitmap, bytes); 352 bitmap->size = bytes; 353 } 354 bdrv_dirty_bitmaps_unlock(bs); 355 } 356 357 /* Called with BQL taken. */ 358 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap) 359 { 360 BlockDriverState *bs = bitmap->bs; 361 362 bdrv_dirty_bitmaps_lock(bs); 363 bdrv_release_dirty_bitmap_locked(bitmap); 364 bdrv_dirty_bitmaps_unlock(bs); 365 } 366 367 /** 368 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()). 369 * There must not be any busy bitmaps attached. 370 * This function does not remove persistent bitmaps from the storage. 371 * Called with BQL taken. 372 */ 373 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs) 374 { 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); 384 } 385 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 */ 394 static int coroutine_fn 395 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name, 396 Error **errp) 397 { 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); 400 } 401 402 return 0; 403 } 404 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 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 453 static bool coroutine_fn 454 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, 455 uint32_t granularity, Error **errp) 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 466 if (!drv->bdrv_co_can_store_new_dirty_bitmap) { 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 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 IO_CODE(); 500 if (qemu_in_coroutine()) { 501 return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp); 502 } else { 503 Coroutine *co; 504 BdrvCanStoreNewDirtyBitmapCo s = { 505 .bs = bs, 506 .name = name, 507 .granularity = granularity, 508 .errp = errp, 509 .in_progress = true, 510 }; 511 512 co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry, 513 &s); 514 bdrv_coroutine_enter(bs, co); 515 BDRV_POLL_WHILE(bs, s.in_progress); 516 517 return s.ret; 518 } 519 } 520 521 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 522 { 523 bdrv_dirty_bitmaps_lock(bitmap->bs); 524 bitmap->disabled = true; 525 bdrv_dirty_bitmaps_unlock(bitmap->bs); 526 } 527 528 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 529 { 530 bdrv_dirty_bitmaps_lock(bitmap->bs); 531 bdrv_enable_dirty_bitmap_locked(bitmap); 532 bdrv_dirty_bitmaps_unlock(bitmap->bs); 533 } 534 535 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 536 { 537 BdrvDirtyBitmap *bm; 538 BlockDirtyInfoList *list = NULL; 539 BlockDirtyInfoList **tail = &list; 540 541 bdrv_dirty_bitmaps_lock(bs); 542 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 543 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); 544 545 info->count = bdrv_get_dirty_count(bm); 546 info->granularity = bdrv_dirty_bitmap_granularity(bm); 547 info->has_name = !!bm->name; 548 info->name = g_strdup(bm->name); 549 info->recording = bdrv_dirty_bitmap_recording(bm); 550 info->busy = bdrv_dirty_bitmap_busy(bm); 551 info->persistent = bm->persistent; 552 info->has_inconsistent = bm->inconsistent; 553 info->inconsistent = bm->inconsistent; 554 QAPI_LIST_APPEND(tail, info); 555 } 556 bdrv_dirty_bitmaps_unlock(bs); 557 558 return list; 559 } 560 561 /* Called within bdrv_dirty_bitmap_lock..unlock */ 562 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset) 563 { 564 return hbitmap_get(bitmap->bitmap, offset); 565 } 566 567 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset) 568 { 569 bool ret; 570 bdrv_dirty_bitmaps_lock(bitmap->bs); 571 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset); 572 bdrv_dirty_bitmaps_unlock(bitmap->bs); 573 574 return ret; 575 } 576 577 /** 578 * Chooses a default granularity based on the existing cluster size, 579 * but clamped between [4K, 64K]. Defaults to 64K in the case that there 580 * is no cluster size information available. 581 */ 582 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) 583 { 584 BlockDriverInfo bdi; 585 uint32_t granularity; 586 587 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { 588 granularity = MAX(4096, bdi.cluster_size); 589 granularity = MIN(65536, granularity); 590 } else { 591 granularity = 65536; 592 } 593 594 return granularity; 595 } 596 597 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap) 598 { 599 return 1U << hbitmap_granularity(bitmap->bitmap); 600 } 601 602 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap) 603 { 604 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); 605 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0); 606 iter->bitmap = bitmap; 607 bitmap->active_iterators++; 608 return iter; 609 } 610 611 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter) 612 { 613 if (!iter) { 614 return; 615 } 616 assert(iter->bitmap->active_iterators > 0); 617 iter->bitmap->active_iterators--; 618 g_free(iter); 619 } 620 621 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter) 622 { 623 return hbitmap_iter_next(&iter->hbi); 624 } 625 626 /* Called within bdrv_dirty_bitmap_lock..unlock */ 627 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, 628 int64_t offset, int64_t bytes) 629 { 630 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 631 hbitmap_set(bitmap->bitmap, offset, bytes); 632 } 633 634 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, 635 int64_t offset, int64_t bytes) 636 { 637 bdrv_dirty_bitmaps_lock(bitmap->bs); 638 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes); 639 bdrv_dirty_bitmaps_unlock(bitmap->bs); 640 } 641 642 /* Called within bdrv_dirty_bitmap_lock..unlock */ 643 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, 644 int64_t offset, int64_t bytes) 645 { 646 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 647 hbitmap_reset(bitmap->bitmap, offset, bytes); 648 } 649 650 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, 651 int64_t offset, int64_t bytes) 652 { 653 bdrv_dirty_bitmaps_lock(bitmap->bs); 654 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes); 655 bdrv_dirty_bitmaps_unlock(bitmap->bs); 656 } 657 658 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out) 659 { 660 IO_CODE(); 661 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 662 bdrv_dirty_bitmaps_lock(bitmap->bs); 663 if (!out) { 664 hbitmap_reset_all(bitmap->bitmap); 665 } else { 666 HBitmap *backup = bitmap->bitmap; 667 bitmap->bitmap = hbitmap_alloc(bitmap->size, 668 hbitmap_granularity(backup)); 669 *out = backup; 670 } 671 bdrv_dirty_bitmaps_unlock(bitmap->bs); 672 } 673 674 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup) 675 { 676 HBitmap *tmp = bitmap->bitmap; 677 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 678 GLOBAL_STATE_CODE(); 679 bitmap->bitmap = backup; 680 hbitmap_free(tmp); 681 } 682 683 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap, 684 uint64_t offset, uint64_t bytes) 685 { 686 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes); 687 } 688 689 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap) 690 { 691 return hbitmap_serialization_align(bitmap->bitmap); 692 } 693 694 /* Return the disk size covered by a chunk of serialized bitmap data. */ 695 uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, 696 const BdrvDirtyBitmap *bitmap) 697 { 698 uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap); 699 uint64_t limit = granularity * (serialized_chunk_size << 3); 700 701 assert(QEMU_IS_ALIGNED(limit, 702 bdrv_dirty_bitmap_serialization_align(bitmap))); 703 return limit; 704 } 705 706 707 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap, 708 uint8_t *buf, uint64_t offset, 709 uint64_t bytes) 710 { 711 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes); 712 } 713 714 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap, 715 uint8_t *buf, uint64_t offset, 716 uint64_t bytes, bool finish) 717 { 718 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish); 719 } 720 721 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap, 722 uint64_t offset, uint64_t bytes, 723 bool finish) 724 { 725 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish); 726 } 727 728 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap, 729 uint64_t offset, uint64_t bytes, 730 bool finish) 731 { 732 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish); 733 } 734 735 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap) 736 { 737 hbitmap_deserialize_finish(bitmap->bitmap); 738 } 739 740 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes) 741 { 742 BdrvDirtyBitmap *bitmap; 743 IO_CODE(); 744 745 if (QLIST_EMPTY(&bs->dirty_bitmaps)) { 746 return; 747 } 748 749 bdrv_dirty_bitmaps_lock(bs); 750 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 751 if (!bdrv_dirty_bitmap_enabled(bitmap)) { 752 continue; 753 } 754 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 755 hbitmap_set(bitmap->bitmap, offset, bytes); 756 } 757 bdrv_dirty_bitmaps_unlock(bs); 758 } 759 760 /** 761 * Advance a BdrvDirtyBitmapIter to an arbitrary offset. 762 */ 763 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset) 764 { 765 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset); 766 } 767 768 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) 769 { 770 return hbitmap_count(bitmap->bitmap); 771 } 772 773 bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap) 774 { 775 return bitmap->readonly; 776 } 777 778 /* Called with BQL taken. */ 779 void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value) 780 { 781 bdrv_dirty_bitmaps_lock(bitmap->bs); 782 bitmap->readonly = value; 783 bdrv_dirty_bitmaps_unlock(bitmap->bs); 784 } 785 786 bool bdrv_has_readonly_bitmaps(BlockDriverState *bs) 787 { 788 BdrvDirtyBitmap *bm; 789 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 790 if (bm->readonly) { 791 return true; 792 } 793 } 794 795 return false; 796 } 797 798 bool bdrv_has_named_bitmaps(BlockDriverState *bs) 799 { 800 BdrvDirtyBitmap *bm; 801 802 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 803 if (bdrv_dirty_bitmap_name(bm)) { 804 return true; 805 } 806 } 807 808 return false; 809 } 810 811 /* Called with BQL taken. */ 812 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent) 813 { 814 bdrv_dirty_bitmaps_lock(bitmap->bs); 815 bitmap->persistent = persistent; 816 bdrv_dirty_bitmaps_unlock(bitmap->bs); 817 } 818 819 /* Called with BQL taken. */ 820 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap) 821 { 822 bdrv_dirty_bitmaps_lock(bitmap->bs); 823 assert(bitmap->persistent == true); 824 bitmap->inconsistent = true; 825 bitmap->disabled = true; 826 bdrv_dirty_bitmaps_unlock(bitmap->bs); 827 } 828 829 /* Called with BQL taken. */ 830 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip) 831 { 832 bdrv_dirty_bitmaps_lock(bitmap->bs); 833 bitmap->skip_store = skip; 834 bdrv_dirty_bitmaps_unlock(bitmap->bs); 835 } 836 837 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap) 838 { 839 return bitmap->persistent && !bitmap->skip_store; 840 } 841 842 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap) 843 { 844 return bitmap->inconsistent; 845 } 846 847 BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs) 848 { 849 return QLIST_FIRST(&bs->dirty_bitmaps); 850 } 851 852 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap) 853 { 854 return QLIST_NEXT(bitmap, list); 855 } 856 857 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp) 858 { 859 return hbitmap_sha256(bitmap->bitmap, errp); 860 } 861 862 int64_t bdrv_dirty_bitmap_next_dirty(BdrvDirtyBitmap *bitmap, int64_t offset, 863 int64_t bytes) 864 { 865 return hbitmap_next_dirty(bitmap->bitmap, offset, bytes); 866 } 867 868 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset, 869 int64_t bytes) 870 { 871 return hbitmap_next_zero(bitmap->bitmap, offset, bytes); 872 } 873 874 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, 875 int64_t start, int64_t end, int64_t max_dirty_count, 876 int64_t *dirty_start, int64_t *dirty_count) 877 { 878 return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_count, 879 dirty_start, dirty_count); 880 } 881 882 bool bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap, int64_t offset, 883 int64_t bytes, int64_t *count) 884 { 885 return hbitmap_status(bitmap->bitmap, offset, bytes, count); 886 } 887 888 /** 889 * bdrv_merge_dirty_bitmap: merge src into dest. 890 * Ensures permissions on bitmaps are reasonable; use for public API. 891 * 892 * @backup: If provided, make a copy of dest here prior to merge. 893 * 894 * Returns true on success, false on failure. In case of failure bitmaps are 895 * untouched. 896 */ 897 bool bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src, 898 HBitmap **backup, Error **errp) 899 { 900 bool ret = false; 901 902 bdrv_dirty_bitmaps_lock(dest->bs); 903 if (src->bs != dest->bs) { 904 bdrv_dirty_bitmaps_lock(src->bs); 905 } 906 907 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) { 908 goto out; 909 } 910 911 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) { 912 goto out; 913 } 914 915 if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) { 916 error_setg(errp, "Bitmaps are incompatible and can't be merged"); 917 goto out; 918 } 919 920 ret = bdrv_dirty_bitmap_merge_internal(dest, src, backup, false); 921 assert(ret); 922 923 out: 924 bdrv_dirty_bitmaps_unlock(dest->bs); 925 if (src->bs != dest->bs) { 926 bdrv_dirty_bitmaps_unlock(src->bs); 927 } 928 929 return ret; 930 } 931 932 /** 933 * bdrv_dirty_bitmap_merge_internal: merge src into dest. 934 * Does NOT check bitmap permissions; not suitable for use as public API. 935 * 936 * @backup: If provided, make a copy of dest here prior to merge. 937 * @lock: If true, lock and unlock bitmaps on the way in/out. 938 * returns true if the merge succeeded; false if unattempted. 939 */ 940 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest, 941 const BdrvDirtyBitmap *src, 942 HBitmap **backup, 943 bool lock) 944 { 945 bool ret; 946 IO_CODE(); 947 948 assert(!bdrv_dirty_bitmap_readonly(dest)); 949 assert(!bdrv_dirty_bitmap_inconsistent(dest)); 950 assert(!bdrv_dirty_bitmap_inconsistent(src)); 951 952 if (lock) { 953 bdrv_dirty_bitmaps_lock(dest->bs); 954 if (src->bs != dest->bs) { 955 bdrv_dirty_bitmaps_lock(src->bs); 956 } 957 } 958 959 if (backup) { 960 *backup = dest->bitmap; 961 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup)); 962 ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap); 963 } else { 964 ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap); 965 } 966 967 if (lock) { 968 bdrv_dirty_bitmaps_unlock(dest->bs); 969 if (src->bs != dest->bs) { 970 bdrv_dirty_bitmaps_unlock(src->bs); 971 } 972 } 973 974 return ret; 975 } 976