1 /* 2 * Block Dirty Bitmap 3 * 4 * Copyright (c) 2016 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 "qemu-common.h" 27 #include "trace.h" 28 #include "block/block_int.h" 29 #include "block/blockjob.h" 30 31 /** 32 * A BdrvDirtyBitmap can be in three possible states: 33 * (1) successor is NULL and disabled is false: full r/w mode 34 * (2) successor is NULL and disabled is true: read only mode ("disabled") 35 * (3) successor is set: frozen mode. 36 * A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set, 37 * or enabled. A frozen bitmap can only abdicate() or reclaim(). 38 */ 39 struct BdrvDirtyBitmap { 40 HBitmap *bitmap; /* Dirty sector bitmap implementation */ 41 HBitmap *meta; /* Meta dirty bitmap */ 42 BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */ 43 char *name; /* Optional non-empty unique ID */ 44 int64_t size; /* Size of the bitmap (Number of sectors) */ 45 bool disabled; /* Bitmap is read-only */ 46 int active_iterators; /* How many iterators are active */ 47 QLIST_ENTRY(BdrvDirtyBitmap) list; 48 }; 49 50 struct BdrvDirtyBitmapIter { 51 HBitmapIter hbi; 52 BdrvDirtyBitmap *bitmap; 53 }; 54 55 BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name) 56 { 57 BdrvDirtyBitmap *bm; 58 59 assert(name); 60 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 61 if (bm->name && !strcmp(name, bm->name)) { 62 return bm; 63 } 64 } 65 return NULL; 66 } 67 68 void bdrv_dirty_bitmap_make_anon(BdrvDirtyBitmap *bitmap) 69 { 70 assert(!bdrv_dirty_bitmap_frozen(bitmap)); 71 g_free(bitmap->name); 72 bitmap->name = NULL; 73 } 74 75 BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, 76 uint32_t granularity, 77 const char *name, 78 Error **errp) 79 { 80 int64_t bitmap_size; 81 BdrvDirtyBitmap *bitmap; 82 uint32_t sector_granularity; 83 84 assert((granularity & (granularity - 1)) == 0); 85 86 if (name && bdrv_find_dirty_bitmap(bs, name)) { 87 error_setg(errp, "Bitmap already exists: %s", name); 88 return NULL; 89 } 90 sector_granularity = granularity >> BDRV_SECTOR_BITS; 91 assert(sector_granularity); 92 bitmap_size = bdrv_nb_sectors(bs); 93 if (bitmap_size < 0) { 94 error_setg_errno(errp, -bitmap_size, "could not get length of device"); 95 errno = -bitmap_size; 96 return NULL; 97 } 98 bitmap = g_new0(BdrvDirtyBitmap, 1); 99 bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(sector_granularity)); 100 bitmap->size = bitmap_size; 101 bitmap->name = g_strdup(name); 102 bitmap->disabled = false; 103 QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 104 return bitmap; 105 } 106 107 /* bdrv_create_meta_dirty_bitmap 108 * 109 * Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e. 110 * when a dirty status bit in @bitmap is changed (either from reset to set or 111 * the other way around), its respective meta dirty bitmap bit will be marked 112 * dirty as well. 113 * 114 * @bitmap: the block dirty bitmap for which to create a meta dirty bitmap. 115 * @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap 116 * track. 117 */ 118 void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap, 119 int chunk_size) 120 { 121 assert(!bitmap->meta); 122 bitmap->meta = hbitmap_create_meta(bitmap->bitmap, 123 chunk_size * BITS_PER_BYTE); 124 } 125 126 void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap) 127 { 128 assert(bitmap->meta); 129 hbitmap_free_meta(bitmap->bitmap); 130 bitmap->meta = NULL; 131 } 132 133 int bdrv_dirty_bitmap_get_meta(BlockDriverState *bs, 134 BdrvDirtyBitmap *bitmap, int64_t sector, 135 int nb_sectors) 136 { 137 uint64_t i; 138 int sectors_per_bit = 1 << hbitmap_granularity(bitmap->meta); 139 140 /* To optimize: we can make hbitmap to internally check the range in a 141 * coarse level, or at least do it word by word. */ 142 for (i = sector; i < sector + nb_sectors; i += sectors_per_bit) { 143 if (hbitmap_get(bitmap->meta, i)) { 144 return true; 145 } 146 } 147 return false; 148 } 149 150 void bdrv_dirty_bitmap_reset_meta(BlockDriverState *bs, 151 BdrvDirtyBitmap *bitmap, int64_t sector, 152 int nb_sectors) 153 { 154 hbitmap_reset(bitmap->meta, sector, nb_sectors); 155 } 156 157 int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap) 158 { 159 return bitmap->size; 160 } 161 162 const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap) 163 { 164 return bitmap->name; 165 } 166 167 bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap) 168 { 169 return bitmap->successor; 170 } 171 172 bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap) 173 { 174 return !(bitmap->disabled || bitmap->successor); 175 } 176 177 DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap) 178 { 179 if (bdrv_dirty_bitmap_frozen(bitmap)) { 180 return DIRTY_BITMAP_STATUS_FROZEN; 181 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 182 return DIRTY_BITMAP_STATUS_DISABLED; 183 } else { 184 return DIRTY_BITMAP_STATUS_ACTIVE; 185 } 186 } 187 188 /** 189 * Create a successor bitmap destined to replace this bitmap after an operation. 190 * Requires that the bitmap is not frozen and has no successor. 191 */ 192 int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs, 193 BdrvDirtyBitmap *bitmap, Error **errp) 194 { 195 uint64_t granularity; 196 BdrvDirtyBitmap *child; 197 198 if (bdrv_dirty_bitmap_frozen(bitmap)) { 199 error_setg(errp, "Cannot create a successor for a bitmap that is " 200 "currently frozen"); 201 return -1; 202 } 203 assert(!bitmap->successor); 204 205 /* Create an anonymous successor */ 206 granularity = bdrv_dirty_bitmap_granularity(bitmap); 207 child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp); 208 if (!child) { 209 return -1; 210 } 211 212 /* Successor will be on or off based on our current state. */ 213 child->disabled = bitmap->disabled; 214 215 /* Install the successor and freeze the parent */ 216 bitmap->successor = child; 217 return 0; 218 } 219 220 /** 221 * For a bitmap with a successor, yield our name to the successor, 222 * delete the old bitmap, and return a handle to the new bitmap. 223 */ 224 BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs, 225 BdrvDirtyBitmap *bitmap, 226 Error **errp) 227 { 228 char *name; 229 BdrvDirtyBitmap *successor = bitmap->successor; 230 231 if (successor == NULL) { 232 error_setg(errp, "Cannot relinquish control if " 233 "there's no successor present"); 234 return NULL; 235 } 236 237 name = bitmap->name; 238 bitmap->name = NULL; 239 successor->name = name; 240 bitmap->successor = NULL; 241 bdrv_release_dirty_bitmap(bs, bitmap); 242 243 return successor; 244 } 245 246 /** 247 * In cases of failure where we can no longer safely delete the parent, 248 * we may wish to re-join the parent and child/successor. 249 * The merged parent will be un-frozen, but not explicitly re-enabled. 250 */ 251 BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs, 252 BdrvDirtyBitmap *parent, 253 Error **errp) 254 { 255 BdrvDirtyBitmap *successor = parent->successor; 256 257 if (!successor) { 258 error_setg(errp, "Cannot reclaim a successor when none is present"); 259 return NULL; 260 } 261 262 if (!hbitmap_merge(parent->bitmap, successor->bitmap)) { 263 error_setg(errp, "Merging of parent and successor bitmap failed"); 264 return NULL; 265 } 266 bdrv_release_dirty_bitmap(bs, successor); 267 parent->successor = NULL; 268 269 return parent; 270 } 271 272 /** 273 * Truncates _all_ bitmaps attached to a BDS. 274 */ 275 void bdrv_dirty_bitmap_truncate(BlockDriverState *bs) 276 { 277 BdrvDirtyBitmap *bitmap; 278 uint64_t size = bdrv_nb_sectors(bs); 279 280 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 281 assert(!bdrv_dirty_bitmap_frozen(bitmap)); 282 assert(!bitmap->active_iterators); 283 hbitmap_truncate(bitmap->bitmap, size); 284 bitmap->size = size; 285 } 286 } 287 288 static void bdrv_do_release_matching_dirty_bitmap(BlockDriverState *bs, 289 BdrvDirtyBitmap *bitmap, 290 bool only_named) 291 { 292 BdrvDirtyBitmap *bm, *next; 293 QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 294 if ((!bitmap || bm == bitmap) && (!only_named || bm->name)) { 295 assert(!bm->active_iterators); 296 assert(!bdrv_dirty_bitmap_frozen(bm)); 297 assert(!bm->meta); 298 QLIST_REMOVE(bm, list); 299 hbitmap_free(bm->bitmap); 300 g_free(bm->name); 301 g_free(bm); 302 303 if (bitmap) { 304 return; 305 } 306 } 307 } 308 if (bitmap) { 309 abort(); 310 } 311 } 312 313 void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 314 { 315 bdrv_do_release_matching_dirty_bitmap(bs, bitmap, false); 316 } 317 318 /** 319 * Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()). 320 * There must not be any frozen bitmaps attached. 321 */ 322 void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs) 323 { 324 bdrv_do_release_matching_dirty_bitmap(bs, NULL, true); 325 } 326 327 void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 328 { 329 assert(!bdrv_dirty_bitmap_frozen(bitmap)); 330 bitmap->disabled = true; 331 } 332 333 void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap) 334 { 335 assert(!bdrv_dirty_bitmap_frozen(bitmap)); 336 bitmap->disabled = false; 337 } 338 339 BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 340 { 341 BdrvDirtyBitmap *bm; 342 BlockDirtyInfoList *list = NULL; 343 BlockDirtyInfoList **plist = &list; 344 345 QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 346 BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1); 347 BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1); 348 info->count = bdrv_get_dirty_count(bm); 349 info->granularity = bdrv_dirty_bitmap_granularity(bm); 350 info->has_name = !!bm->name; 351 info->name = g_strdup(bm->name); 352 info->status = bdrv_dirty_bitmap_status(bm); 353 entry->value = info; 354 *plist = entry; 355 plist = &entry->next; 356 } 357 358 return list; 359 } 360 361 int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, 362 int64_t sector) 363 { 364 if (bitmap) { 365 return hbitmap_get(bitmap->bitmap, sector); 366 } else { 367 return 0; 368 } 369 } 370 371 /** 372 * Chooses a default granularity based on the existing cluster size, 373 * but clamped between [4K, 64K]. Defaults to 64K in the case that there 374 * is no cluster size information available. 375 */ 376 uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs) 377 { 378 BlockDriverInfo bdi; 379 uint32_t granularity; 380 381 if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) { 382 granularity = MAX(4096, bdi.cluster_size); 383 granularity = MIN(65536, granularity); 384 } else { 385 granularity = 65536; 386 } 387 388 return granularity; 389 } 390 391 uint32_t bdrv_dirty_bitmap_granularity(BdrvDirtyBitmap *bitmap) 392 { 393 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap); 394 } 395 396 uint32_t bdrv_dirty_bitmap_meta_granularity(BdrvDirtyBitmap *bitmap) 397 { 398 return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->meta); 399 } 400 401 BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap, 402 uint64_t first_sector) 403 { 404 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); 405 hbitmap_iter_init(&iter->hbi, bitmap->bitmap, first_sector); 406 iter->bitmap = bitmap; 407 bitmap->active_iterators++; 408 return iter; 409 } 410 411 BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap) 412 { 413 BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1); 414 hbitmap_iter_init(&iter->hbi, bitmap->meta, 0); 415 iter->bitmap = bitmap; 416 bitmap->active_iterators++; 417 return iter; 418 } 419 420 void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter) 421 { 422 if (!iter) { 423 return; 424 } 425 assert(iter->bitmap->active_iterators > 0); 426 iter->bitmap->active_iterators--; 427 g_free(iter); 428 } 429 430 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter) 431 { 432 return hbitmap_iter_next(&iter->hbi); 433 } 434 435 void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap, 436 int64_t cur_sector, int64_t nr_sectors) 437 { 438 assert(bdrv_dirty_bitmap_enabled(bitmap)); 439 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 440 } 441 442 void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap, 443 int64_t cur_sector, int64_t nr_sectors) 444 { 445 assert(bdrv_dirty_bitmap_enabled(bitmap)); 446 hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 447 } 448 449 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out) 450 { 451 assert(bdrv_dirty_bitmap_enabled(bitmap)); 452 if (!out) { 453 hbitmap_reset_all(bitmap->bitmap); 454 } else { 455 HBitmap *backup = bitmap->bitmap; 456 bitmap->bitmap = hbitmap_alloc(bitmap->size, 457 hbitmap_granularity(backup)); 458 *out = backup; 459 } 460 } 461 462 void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in) 463 { 464 HBitmap *tmp = bitmap->bitmap; 465 assert(bdrv_dirty_bitmap_enabled(bitmap)); 466 bitmap->bitmap = in; 467 hbitmap_free(tmp); 468 } 469 470 uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap, 471 uint64_t start, uint64_t count) 472 { 473 return hbitmap_serialization_size(bitmap->bitmap, start, count); 474 } 475 476 uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap) 477 { 478 return hbitmap_serialization_granularity(bitmap->bitmap); 479 } 480 481 void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap, 482 uint8_t *buf, uint64_t start, 483 uint64_t count) 484 { 485 hbitmap_serialize_part(bitmap->bitmap, buf, start, count); 486 } 487 488 void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap, 489 uint8_t *buf, uint64_t start, 490 uint64_t count, bool finish) 491 { 492 hbitmap_deserialize_part(bitmap->bitmap, buf, start, count, finish); 493 } 494 495 void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap, 496 uint64_t start, uint64_t count, 497 bool finish) 498 { 499 hbitmap_deserialize_zeroes(bitmap->bitmap, start, count, finish); 500 } 501 502 void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap) 503 { 504 hbitmap_deserialize_finish(bitmap->bitmap); 505 } 506 507 void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 508 int64_t nr_sectors) 509 { 510 BdrvDirtyBitmap *bitmap; 511 QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 512 if (!bdrv_dirty_bitmap_enabled(bitmap)) { 513 continue; 514 } 515 hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 516 } 517 } 518 519 /** 520 * Advance a BdrvDirtyBitmapIter to an arbitrary offset. 521 */ 522 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t sector_num) 523 { 524 hbitmap_iter_init(&iter->hbi, iter->hbi.hb, sector_num); 525 } 526 527 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap) 528 { 529 return hbitmap_count(bitmap->bitmap); 530 } 531 532 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap) 533 { 534 return hbitmap_count(bitmap->meta); 535 } 536