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 hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap); 313 314 parent->disabled = successor->disabled; 315 parent->busy = false; 316 bdrv_release_dirty_bitmap_locked(successor); 317 parent->successor = NULL; 318 319 return parent; 320 } 321 322 /* Called with BQL taken. */ 323 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BdrvDirtyBitmap *parent, 324 Error **errp) 325 { 326 BdrvDirtyBitmap *ret; 327 328 bdrv_dirty_bitmaps_lock(parent->bs); 329 ret = bdrv_reclaim_dirty_bitmap_locked(parent, errp); 330 bdrv_dirty_bitmaps_unlock(parent->bs); 331 332 return ret; 333 } 334 335 /** 336 * Truncates _all_ bitmaps attached to a BDS. 337 * Called with BQL taken. 338 */ 339 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes) 340 { 341 BdrvDirtyBitmap *bitmap; 342 343 bdrv_dirty_bitmaps_lock(bs); 344 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 345 assert(!bdrv_dirty_bitmap_busy(bitmap)); 346 assert(!bdrv_dirty_bitmap_has_successor(bitmap)); 347 assert(!bitmap->active_iterators); 348 hbitmap_truncate(bitmap->bitmap, bytes); 349 bitmap->size = bytes; 350 } 351 bdrv_dirty_bitmaps_unlock(bs); 352 } 353 354 /* Called with BQL taken. */ 355 void bdrv_release_dirty_bitmap(BdrvDirtyBitmap *bitmap) 356 { 357 BlockDriverState *bs = bitmap->bs; 358 359 bdrv_dirty_bitmaps_lock(bs); 360 bdrv_release_dirty_bitmap_locked(bitmap); 361 bdrv_dirty_bitmaps_unlock(bs); 362 } 363 364 /** 365 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()). 366 * There must not be any busy bitmaps attached. 367 * This function does not remove persistent bitmaps from the storage. 368 * Called with BQL taken. 369 */ 370 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs) 371 { 372 BdrvDirtyBitmap *bm, *next; 373 374 bdrv_dirty_bitmaps_lock(bs); 375 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 376 if (bdrv_dirty_bitmap_name(bm)) { 377 bdrv_release_dirty_bitmap_locked(bm); 378 } 379 } 380 bdrv_dirty_bitmaps_unlock(bs); 381 } 382 383 /** 384 * Remove persistent dirty bitmap from the storage if it exists. 385 * Absence of bitmap is not an error, because we have the following scenario: 386 * BdrvDirtyBitmap can have .persistent = true but not yet saved and have no 387 * stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should 388 * not fail. 389 * This function doesn't release corresponding BdrvDirtyBitmap. 390 */ 391 static int coroutine_fn 392 bdrv_co_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name, 393 Error **errp) 394 { 395 if (bs->drv && bs->drv->bdrv_co_remove_persistent_dirty_bitmap) { 396 return bs->drv->bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp); 397 } 398 399 return 0; 400 } 401 402 typedef struct BdrvRemovePersistentDirtyBitmapCo { 403 BlockDriverState *bs; 404 const char *name; 405 Error **errp; 406 int ret; 407 } BdrvRemovePersistentDirtyBitmapCo; 408 409 static void coroutine_fn 410 bdrv_co_remove_persistent_dirty_bitmap_entry(void *opaque) 411 { 412 BdrvRemovePersistentDirtyBitmapCo *s = opaque; 413 414 s->ret = bdrv_co_remove_persistent_dirty_bitmap(s->bs, s->name, s->errp); 415 aio_wait_kick(); 416 } 417 418 int bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs, const char *name, 419 Error **errp) 420 { 421 if (qemu_in_coroutine()) { 422 return bdrv_co_remove_persistent_dirty_bitmap(bs, name, errp); 423 } else { 424 Coroutine *co; 425 BdrvRemovePersistentDirtyBitmapCo s = { 426 .bs = bs, 427 .name = name, 428 .errp = errp, 429 .ret = -EINPROGRESS, 430 }; 431 432 co = qemu_coroutine_create(bdrv_co_remove_persistent_dirty_bitmap_entry, 433 &s); 434 bdrv_coroutine_enter(bs, co); 435 BDRV_POLL_WHILE(bs, s.ret == -EINPROGRESS); 436 437 return s.ret; 438 } 439 } 440 441 bool 442 bdrv_supports_persistent_dirty_bitmap(BlockDriverState *bs) 443 { 444 if (bs->drv && bs->drv->bdrv_supports_persistent_dirty_bitmap) { 445 return bs->drv->bdrv_supports_persistent_dirty_bitmap(bs); 446 } 447 return false; 448 } 449 450 static bool coroutine_fn 451 bdrv_co_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, 452 uint32_t granularity, Error **errp) 453 { 454 BlockDriver *drv = bs->drv; 455 456 if (!drv) { 457 error_setg_errno(errp, ENOMEDIUM, 458 "Can't store persistent bitmaps to %s", 459 bdrv_get_device_or_node_name(bs)); 460 return false; 461 } 462 463 if (!drv->bdrv_co_can_store_new_dirty_bitmap) { 464 error_setg_errno(errp, ENOTSUP, 465 "Can't store persistent bitmaps to %s", 466 bdrv_get_device_or_node_name(bs)); 467 return false; 468 } 469 470 return drv->bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp); 471 } 472 473 typedef struct BdrvCanStoreNewDirtyBitmapCo { 474 BlockDriverState *bs; 475 const char *name; 476 uint32_t granularity; 477 Error **errp; 478 bool ret; 479 480 bool in_progress; 481 } BdrvCanStoreNewDirtyBitmapCo; 482 483 static void coroutine_fn bdrv_co_can_store_new_dirty_bitmap_entry(void *opaque) 484 { 485 BdrvCanStoreNewDirtyBitmapCo *s = opaque; 486 487 s->ret = bdrv_co_can_store_new_dirty_bitmap(s->bs, s->name, s->granularity, 488 s->errp); 489 s->in_progress = false; 490 aio_wait_kick(); 491 } 492 493 bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name, 494 uint32_t granularity, Error **errp) 495 { 496 IO_CODE(); 497 if (qemu_in_coroutine()) { 498 return bdrv_co_can_store_new_dirty_bitmap(bs, name, granularity, errp); 499 } else { 500 Coroutine *co; 501 BdrvCanStoreNewDirtyBitmapCo s = { 502 .bs = bs, 503 .name = name, 504 .granularity = granularity, 505 .errp = errp, 506 .in_progress = true, 507 }; 508 509 co = qemu_coroutine_create(bdrv_co_can_store_new_dirty_bitmap_entry, 510 &s); 511 bdrv_coroutine_enter(bs, co); 512 BDRV_POLL_WHILE(bs, s.in_progress); 513 514 return s.ret; 515 } 516 } 517 518 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 519 { 520 bdrv_dirty_bitmaps_lock(bitmap->bs); 521 bitmap->disabled = true; 522 bdrv_dirty_bitmaps_unlock(bitmap->bs); 523 } 524 525 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 526 { 527 bdrv_dirty_bitmaps_lock(bitmap->bs); 528 bdrv_enable_dirty_bitmap_locked(bitmap); 529 bdrv_dirty_bitmaps_unlock(bitmap->bs); 530 } 531 532 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 533 { 534 BdrvDirtyBitmap *bm; 535 BlockDirtyInfoList *list = NULL; 536 BlockDirtyInfoList **tail = &list; 537 538 bdrv_dirty_bitmaps_lock(bs); 539 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 540 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); 541 542 info->count = bdrv_get_dirty_count(bm); 543 info->granularity = bdrv_dirty_bitmap_granularity(bm); 544 info->name = g_strdup(bm->name); 545 info->recording = bdrv_dirty_bitmap_recording(bm); 546 info->busy = bdrv_dirty_bitmap_busy(bm); 547 info->persistent = bm->persistent; 548 info->has_inconsistent = bm->inconsistent; 549 info->inconsistent = bm->inconsistent; 550 QAPI_LIST_APPEND(tail, info); 551 } 552 bdrv_dirty_bitmaps_unlock(bs); 553 554 return list; 555 } 556 557 /* Called within bdrv_dirty_bitmap_lock..unlock */ 558 bool bdrv_dirty_bitmap_get_locked(BdrvDirtyBitmap *bitmap, int64_t offset) 559 { 560 return hbitmap_get(bitmap->bitmap, offset); 561 } 562 563 bool bdrv_dirty_bitmap_get(BdrvDirtyBitmap *bitmap, int64_t offset) 564 { 565 bool ret; 566 bdrv_dirty_bitmaps_lock(bitmap->bs); 567 ret = bdrv_dirty_bitmap_get_locked(bitmap, offset); 568 bdrv_dirty_bitmaps_unlock(bitmap->bs); 569 570 return ret; 571 } 572 573 /** 574 * Chooses a default granularity based on the existing cluster size, 575 * but clamped between [4K, 64K]. Defaults to 64K in the case that there 576 * is no cluster size information available. 577 */ 578 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) 579 { 580 BlockDriverInfo bdi; 581 uint32_t granularity; 582 583 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { 584 granularity = MAX(4096, bdi.cluster_size); 585 granularity = MIN(65536, granularity); 586 } else { 587 granularity = 65536; 588 } 589 590 return granularity; 591 } 592 593 uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap) 594 { 595 return 1U << hbitmap_granularity(bitmap->bitmap); 596 } 597 598 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap) 599 { 600 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); 601 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0); 602 iter->bitmap = bitmap; 603 bitmap->active_iterators++; 604 return iter; 605 } 606 607 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter) 608 { 609 if (!iter) { 610 return; 611 } 612 assert(iter->bitmap->active_iterators > 0); 613 iter->bitmap->active_iterators--; 614 g_free(iter); 615 } 616 617 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter) 618 { 619 return hbitmap_iter_next(&iter->hbi); 620 } 621 622 /* Called within bdrv_dirty_bitmap_lock..unlock */ 623 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, 624 int64_t offset, int64_t bytes) 625 { 626 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 627 hbitmap_set(bitmap->bitmap, offset, bytes); 628 } 629 630 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, 631 int64_t offset, int64_t bytes) 632 { 633 bdrv_dirty_bitmaps_lock(bitmap->bs); 634 bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes); 635 bdrv_dirty_bitmaps_unlock(bitmap->bs); 636 } 637 638 /* Called within bdrv_dirty_bitmap_lock..unlock */ 639 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap, 640 int64_t offset, int64_t bytes) 641 { 642 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 643 hbitmap_reset(bitmap->bitmap, offset, bytes); 644 } 645 646 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, 647 int64_t offset, int64_t bytes) 648 { 649 bdrv_dirty_bitmaps_lock(bitmap->bs); 650 bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes); 651 bdrv_dirty_bitmaps_unlock(bitmap->bs); 652 } 653 654 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out) 655 { 656 IO_CODE(); 657 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 658 bdrv_dirty_bitmaps_lock(bitmap->bs); 659 if (!out) { 660 hbitmap_reset_all(bitmap->bitmap); 661 } else { 662 HBitmap *backup = bitmap->bitmap; 663 bitmap->bitmap = hbitmap_alloc(bitmap->size, 664 hbitmap_granularity(backup)); 665 *out = backup; 666 } 667 bdrv_dirty_bitmaps_unlock(bitmap->bs); 668 } 669 670 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup) 671 { 672 HBitmap *tmp = bitmap->bitmap; 673 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 674 GLOBAL_STATE_CODE(); 675 bitmap->bitmap = backup; 676 hbitmap_free(tmp); 677 } 678 679 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap, 680 uint64_t offset, uint64_t bytes) 681 { 682 return hbitmap_serialization_size(bitmap->bitmap, offset, bytes); 683 } 684 685 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap) 686 { 687 return hbitmap_serialization_align(bitmap->bitmap); 688 } 689 690 /* Return the disk size covered by a chunk of serialized bitmap data. */ 691 uint64_t bdrv_dirty_bitmap_serialization_coverage(int serialized_chunk_size, 692 const BdrvDirtyBitmap *bitmap) 693 { 694 uint64_t granularity = bdrv_dirty_bitmap_granularity(bitmap); 695 uint64_t limit = granularity * (serialized_chunk_size << 3); 696 697 assert(QEMU_IS_ALIGNED(limit, 698 bdrv_dirty_bitmap_serialization_align(bitmap))); 699 return limit; 700 } 701 702 703 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap, 704 uint8_t *buf, uint64_t offset, 705 uint64_t bytes) 706 { 707 hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes); 708 } 709 710 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap, 711 uint8_t *buf, uint64_t offset, 712 uint64_t bytes, bool finish) 713 { 714 hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish); 715 } 716 717 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap, 718 uint64_t offset, uint64_t bytes, 719 bool finish) 720 { 721 hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish); 722 } 723 724 void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap, 725 uint64_t offset, uint64_t bytes, 726 bool finish) 727 { 728 hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish); 729 } 730 731 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap) 732 { 733 hbitmap_deserialize_finish(bitmap->bitmap); 734 } 735 736 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes) 737 { 738 BdrvDirtyBitmap *bitmap; 739 IO_CODE(); 740 741 if (QLIST_EMPTY(&bs->dirty_bitmaps)) { 742 return; 743 } 744 745 bdrv_dirty_bitmaps_lock(bs); 746 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 747 if (!bdrv_dirty_bitmap_enabled(bitmap)) { 748 continue; 749 } 750 assert(!bdrv_dirty_bitmap_readonly(bitmap)); 751 hbitmap_set(bitmap->bitmap, offset, bytes); 752 } 753 bdrv_dirty_bitmaps_unlock(bs); 754 } 755 756 /** 757 * Advance a BdrvDirtyBitmapIter to an arbitrary offset. 758 */ 759 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset) 760 { 761 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset); 762 } 763 764 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) 765 { 766 return hbitmap_count(bitmap->bitmap); 767 } 768 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 { 777 bdrv_dirty_bitmaps_lock(bitmap->bs); 778 bitmap->readonly = value; 779 bdrv_dirty_bitmaps_unlock(bitmap->bs); 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 } 793 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 807 /* Called with BQL taken. */ 808 void bdrv_dirty_bitmap_set_persistence(BdrvDirtyBitmap *bitmap, bool persistent) 809 { 810 bdrv_dirty_bitmaps_lock(bitmap->bs); 811 bitmap->persistent = persistent; 812 bdrv_dirty_bitmaps_unlock(bitmap->bs); 813 } 814 815 /* Called with BQL taken. */ 816 void bdrv_dirty_bitmap_set_inconsistent(BdrvDirtyBitmap *bitmap) 817 { 818 bdrv_dirty_bitmaps_lock(bitmap->bs); 819 assert(bitmap->persistent == true); 820 bitmap->inconsistent = true; 821 bitmap->disabled = true; 822 bdrv_dirty_bitmaps_unlock(bitmap->bs); 823 } 824 825 /* Called with BQL taken. */ 826 void bdrv_dirty_bitmap_skip_store(BdrvDirtyBitmap *bitmap, bool skip) 827 { 828 bdrv_dirty_bitmaps_lock(bitmap->bs); 829 bitmap->skip_store = skip; 830 bdrv_dirty_bitmaps_unlock(bitmap->bs); 831 } 832 833 bool bdrv_dirty_bitmap_get_persistence(BdrvDirtyBitmap *bitmap) 834 { 835 return bitmap->persistent && !bitmap->skip_store; 836 } 837 838 bool bdrv_dirty_bitmap_inconsistent(const BdrvDirtyBitmap *bitmap) 839 { 840 return bitmap->inconsistent; 841 } 842 843 BdrvDirtyBitmap *bdrv_dirty_bitmap_first(BlockDriverState *bs) 844 { 845 return QLIST_FIRST(&bs->dirty_bitmaps); 846 } 847 848 BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BdrvDirtyBitmap *bitmap) 849 { 850 return QLIST_NEXT(bitmap, list); 851 } 852 853 char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp) 854 { 855 return hbitmap_sha256(bitmap->bitmap, errp); 856 } 857 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 864 int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, int64_t offset, 865 int64_t bytes) 866 { 867 return hbitmap_next_zero(bitmap->bitmap, offset, bytes); 868 } 869 870 bool bdrv_dirty_bitmap_next_dirty_area(BdrvDirtyBitmap *bitmap, 871 int64_t start, int64_t end, int64_t max_dirty_count, 872 int64_t *dirty_start, int64_t *dirty_count) 873 { 874 return hbitmap_next_dirty_area(bitmap->bitmap, start, end, max_dirty_count, 875 dirty_start, dirty_count); 876 } 877 878 bool bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap, int64_t offset, 879 int64_t bytes, int64_t *count) 880 { 881 return hbitmap_status(bitmap->bitmap, offset, bytes, count); 882 } 883 884 /** 885 * bdrv_merge_dirty_bitmap: merge src into dest. 886 * Ensures permissions on bitmaps are reasonable; use for public API. 887 * 888 * @backup: If provided, make a copy of dest here prior to merge. 889 * 890 * Returns true on success, false on failure. In case of failure bitmaps are 891 * untouched. 892 */ 893 bool bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src, 894 HBitmap **backup, Error **errp) 895 { 896 bool ret = false; 897 898 bdrv_dirty_bitmaps_lock(dest->bs); 899 if (src->bs != dest->bs) { 900 bdrv_dirty_bitmaps_lock(src->bs); 901 } 902 903 if (bdrv_dirty_bitmap_check(dest, BDRV_BITMAP_DEFAULT, errp)) { 904 goto out; 905 } 906 907 if (bdrv_dirty_bitmap_check(src, BDRV_BITMAP_ALLOW_RO, errp)) { 908 goto out; 909 } 910 911 if (bdrv_dirty_bitmap_size(src) != bdrv_dirty_bitmap_size(dest)) { 912 error_setg(errp, "Bitmaps are of different sizes (destination size is %" 913 PRId64 ", source size is %" PRId64 ") and can't be merged", 914 bdrv_dirty_bitmap_size(dest), bdrv_dirty_bitmap_size(src)); 915 goto out; 916 } 917 918 bdrv_dirty_bitmap_merge_internal(dest, src, backup, false); 919 ret = true; 920 921 out: 922 bdrv_dirty_bitmaps_unlock(dest->bs); 923 if (src->bs != dest->bs) { 924 bdrv_dirty_bitmaps_unlock(src->bs); 925 } 926 927 return ret; 928 } 929 930 /** 931 * bdrv_dirty_bitmap_merge_internal: merge src into dest. 932 * Does NOT check bitmap permissions; not suitable for use as public API. 933 * @dest, @src and @backup (if not NULL) must have same size. 934 * 935 * @backup: If provided, make a copy of dest here prior to merge. 936 * @lock: If true, lock and unlock bitmaps on the way in/out. 937 */ 938 void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest, 939 const BdrvDirtyBitmap *src, 940 HBitmap **backup, 941 bool lock) 942 { 943 IO_CODE(); 944 945 assert(!bdrv_dirty_bitmap_readonly(dest)); 946 assert(!bdrv_dirty_bitmap_inconsistent(dest)); 947 assert(!bdrv_dirty_bitmap_inconsistent(src)); 948 949 if (lock) { 950 bdrv_dirty_bitmaps_lock(dest->bs); 951 if (src->bs != dest->bs) { 952 bdrv_dirty_bitmaps_lock(src->bs); 953 } 954 } 955 956 if (backup) { 957 *backup = dest->bitmap; 958 dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup)); 959 hbitmap_merge(*backup, src->bitmap, dest->bitmap); 960 } else { 961 hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap); 962 } 963 964 if (lock) { 965 bdrv_dirty_bitmaps_unlock(dest->bs); 966 if (src->bs != dest->bs) { 967 bdrv_dirty_bitmaps_unlock(src->bs); 968 } 969 } 970 } 971