1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012, 2019 by Delphix. All rights reserved. 24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 25 */ 26 27 #include <sys/dmu.h> 28 #include <sys/zap.h> 29 #include <sys/zfs_context.h> 30 #include <sys/dsl_pool.h> 31 #include <sys/dsl_dataset.h> 32 33 /* 34 * Deadlist concurrency: 35 * 36 * Deadlists can only be modified from the syncing thread. 37 * 38 * Except for dsl_deadlist_insert(), it can only be modified with the 39 * dp_config_rwlock held with RW_WRITER. 40 * 41 * The accessors (dsl_deadlist_space() and dsl_deadlist_space_range()) can 42 * be called concurrently, from open context, with the dl_config_rwlock held 43 * with RW_READER. 44 * 45 * Therefore, we only need to provide locking between dsl_deadlist_insert() and 46 * the accessors, protecting: 47 * dl_phys->dl_used,comp,uncomp 48 * and protecting the dl_tree from being loaded. 49 * The locking is provided by dl_lock. Note that locking on the bpobj_t 50 * provides its own locking, and dl_oldfmt is immutable. 51 */ 52 53 /* 54 * Livelist Overview 55 * ================ 56 * 57 * Livelists use the same 'deadlist_t' struct as deadlists and are also used 58 * to track blkptrs over the lifetime of a dataset. Livelists however, belong 59 * to clones and track the blkptrs that are clone-specific (were born after 60 * the clone's creation). The exception is embedded block pointers which are 61 * not included in livelists because they do not need to be freed. 62 * 63 * When it comes time to delete the clone, the livelist provides a quick 64 * reference as to what needs to be freed. For this reason, livelists also track 65 * when clone-specific blkptrs are freed before deletion to prevent double 66 * frees. Each blkptr in a livelist is marked as a FREE or an ALLOC and the 67 * deletion algorithm iterates backwards over the livelist, matching 68 * FREE/ALLOC pairs and then freeing those ALLOCs which remain. livelists 69 * are also updated in the case when blkptrs are remapped: the old version 70 * of the blkptr is cancelled out with a FREE and the new version is tracked 71 * with an ALLOC. 72 * 73 * To bound the amount of memory required for deletion, livelists over a 74 * certain size are spread over multiple entries. Entries are grouped by 75 * birth txg so we can be sure the ALLOC/FREE pair for a given blkptr will 76 * be in the same entry. This allows us to delete livelists incrementally 77 * over multiple syncs, one entry at a time. 78 * 79 * During the lifetime of the clone, livelists can get extremely large. 80 * Their size is managed by periodic condensing (preemptively cancelling out 81 * FREE/ALLOC pairs). Livelists are disabled when a clone is promoted or when 82 * the shared space between the clone and its origin is so small that it 83 * doesn't make sense to use livelists anymore. 84 */ 85 86 /* 87 * The threshold sublist size at which we create a new sub-livelist for the 88 * next txg. However, since blkptrs of the same transaction group must be in 89 * the same sub-list, the actual sublist size may exceed this. When picking the 90 * size we had to balance the fact that larger sublists mean fewer sublists 91 * (decreasing the cost of insertion) against the consideration that sublists 92 * will be loaded into memory and shouldn't take up an inordinate amount of 93 * space. We settled on ~500000 entries, corresponding to roughly 128M. 94 */ 95 uint64_t zfs_livelist_max_entries = 500000; 96 97 /* 98 * We can approximate how much of a performance gain a livelist will give us 99 * based on the percentage of blocks shared between the clone and its origin. 100 * 0 percent shared means that the clone has completely diverged and that the 101 * old method is maximally effective: every read from the block tree will 102 * result in lots of frees. Livelists give us gains when they track blocks 103 * scattered across the tree, when one read in the old method might only 104 * result in a few frees. Once the clone has been overwritten enough, 105 * writes are no longer sparse and we'll no longer get much of a benefit from 106 * tracking them with a livelist. We chose a lower limit of 75 percent shared 107 * (25 percent overwritten). This means that 1/4 of all block pointers will be 108 * freed (e.g. each read frees 256, out of a max of 1024) so we expect livelists 109 * to make deletion 4x faster. Once the amount of shared space drops below this 110 * threshold, the clone will revert to the old deletion method. 111 */ 112 int zfs_livelist_min_percent_shared = 75; 113 114 static int 115 dsl_deadlist_compare(const void *arg1, const void *arg2) 116 { 117 const dsl_deadlist_entry_t *dle1 = arg1; 118 const dsl_deadlist_entry_t *dle2 = arg2; 119 120 return (TREE_CMP(dle1->dle_mintxg, dle2->dle_mintxg)); 121 } 122 123 static int 124 dsl_deadlist_cache_compare(const void *arg1, const void *arg2) 125 { 126 const dsl_deadlist_cache_entry_t *dlce1 = arg1; 127 const dsl_deadlist_cache_entry_t *dlce2 = arg2; 128 129 return (TREE_CMP(dlce1->dlce_mintxg, dlce2->dlce_mintxg)); 130 } 131 132 static void 133 dsl_deadlist_load_tree(dsl_deadlist_t *dl) 134 { 135 zap_cursor_t zc; 136 zap_attribute_t *za; 137 int error; 138 139 ASSERT(MUTEX_HELD(&dl->dl_lock)); 140 141 ASSERT(!dl->dl_oldfmt); 142 if (dl->dl_havecache) { 143 /* 144 * After loading the tree, the caller may modify the tree, 145 * e.g. to add or remove nodes, or to make a node no longer 146 * refer to the empty_bpobj. These changes would make the 147 * dl_cache incorrect. Therefore we discard the cache here, 148 * so that it can't become incorrect. 149 */ 150 dsl_deadlist_cache_entry_t *dlce; 151 void *cookie = NULL; 152 while ((dlce = avl_destroy_nodes(&dl->dl_cache, &cookie)) 153 != NULL) { 154 kmem_free(dlce, sizeof (*dlce)); 155 } 156 avl_destroy(&dl->dl_cache); 157 dl->dl_havecache = B_FALSE; 158 } 159 if (dl->dl_havetree) 160 return; 161 162 za = zap_attribute_alloc(); 163 avl_create(&dl->dl_tree, dsl_deadlist_compare, 164 sizeof (dsl_deadlist_entry_t), 165 offsetof(dsl_deadlist_entry_t, dle_node)); 166 for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object); 167 (error = zap_cursor_retrieve(&zc, za)) == 0; 168 zap_cursor_advance(&zc)) { 169 dsl_deadlist_entry_t *dle = kmem_alloc(sizeof (*dle), KM_SLEEP); 170 dle->dle_mintxg = zfs_strtonum(za->za_name, NULL); 171 172 /* 173 * Prefetch all the bpobj's so that we do that i/o 174 * in parallel. Then open them all in a second pass. 175 */ 176 dle->dle_bpobj.bpo_object = za->za_first_integer; 177 dmu_prefetch_dnode(dl->dl_os, dle->dle_bpobj.bpo_object, 178 ZIO_PRIORITY_SYNC_READ); 179 180 avl_add(&dl->dl_tree, dle); 181 } 182 VERIFY3U(error, ==, ENOENT); 183 zap_cursor_fini(&zc); 184 zap_attribute_free(za); 185 186 for (dsl_deadlist_entry_t *dle = avl_first(&dl->dl_tree); 187 dle != NULL; dle = AVL_NEXT(&dl->dl_tree, dle)) { 188 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os, 189 dle->dle_bpobj.bpo_object)); 190 } 191 dl->dl_havetree = B_TRUE; 192 } 193 194 /* 195 * Load only the non-empty bpobj's into the dl_cache. The cache is an analog 196 * of the dl_tree, but contains only non-empty_bpobj nodes from the ZAP. It 197 * is used only for gathering space statistics. The dl_cache has two 198 * advantages over the dl_tree: 199 * 200 * 1. Loading the dl_cache is ~5x faster than loading the dl_tree (if it's 201 * mostly empty_bpobj's), due to less CPU overhead to open the empty_bpobj 202 * many times and to inquire about its (zero) space stats many times. 203 * 204 * 2. The dl_cache uses less memory than the dl_tree. We only need to load 205 * the dl_tree of snapshots when deleting a snapshot, after which we free the 206 * dl_tree with dsl_deadlist_discard_tree 207 */ 208 static void 209 dsl_deadlist_load_cache(dsl_deadlist_t *dl) 210 { 211 zap_cursor_t zc; 212 zap_attribute_t *za; 213 int error; 214 215 ASSERT(MUTEX_HELD(&dl->dl_lock)); 216 217 ASSERT(!dl->dl_oldfmt); 218 if (dl->dl_havecache) 219 return; 220 221 uint64_t empty_bpobj = dmu_objset_pool(dl->dl_os)->dp_empty_bpobj; 222 223 avl_create(&dl->dl_cache, dsl_deadlist_cache_compare, 224 sizeof (dsl_deadlist_cache_entry_t), 225 offsetof(dsl_deadlist_cache_entry_t, dlce_node)); 226 za = zap_attribute_alloc(); 227 for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object); 228 (error = zap_cursor_retrieve(&zc, za)) == 0; 229 zap_cursor_advance(&zc)) { 230 if (za->za_first_integer == empty_bpobj) 231 continue; 232 dsl_deadlist_cache_entry_t *dlce = 233 kmem_zalloc(sizeof (*dlce), KM_SLEEP); 234 dlce->dlce_mintxg = zfs_strtonum(za->za_name, NULL); 235 236 /* 237 * Prefetch all the bpobj's so that we do that i/o 238 * in parallel. Then open them all in a second pass. 239 */ 240 dlce->dlce_bpobj = za->za_first_integer; 241 dmu_prefetch_dnode(dl->dl_os, dlce->dlce_bpobj, 242 ZIO_PRIORITY_SYNC_READ); 243 avl_add(&dl->dl_cache, dlce); 244 } 245 VERIFY3U(error, ==, ENOENT); 246 zap_cursor_fini(&zc); 247 zap_attribute_free(za); 248 249 for (dsl_deadlist_cache_entry_t *dlce = avl_first(&dl->dl_cache); 250 dlce != NULL; dlce = AVL_NEXT(&dl->dl_cache, dlce)) { 251 bpobj_t bpo; 252 VERIFY0(bpobj_open(&bpo, dl->dl_os, dlce->dlce_bpobj)); 253 254 VERIFY0(bpobj_space(&bpo, 255 &dlce->dlce_bytes, &dlce->dlce_comp, &dlce->dlce_uncomp)); 256 bpobj_close(&bpo); 257 } 258 dl->dl_havecache = B_TRUE; 259 } 260 261 /* 262 * Discard the tree to save memory. 263 */ 264 void 265 dsl_deadlist_discard_tree(dsl_deadlist_t *dl) 266 { 267 mutex_enter(&dl->dl_lock); 268 269 if (!dl->dl_havetree) { 270 mutex_exit(&dl->dl_lock); 271 return; 272 } 273 dsl_deadlist_entry_t *dle; 274 void *cookie = NULL; 275 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie)) != NULL) { 276 bpobj_close(&dle->dle_bpobj); 277 kmem_free(dle, sizeof (*dle)); 278 } 279 avl_destroy(&dl->dl_tree); 280 281 dl->dl_havetree = B_FALSE; 282 mutex_exit(&dl->dl_lock); 283 } 284 285 void 286 dsl_deadlist_iterate(dsl_deadlist_t *dl, deadlist_iter_t func, void *args) 287 { 288 dsl_deadlist_entry_t *dle; 289 290 ASSERT(dsl_deadlist_is_open(dl)); 291 292 mutex_enter(&dl->dl_lock); 293 dsl_deadlist_load_tree(dl); 294 mutex_exit(&dl->dl_lock); 295 for (dle = avl_first(&dl->dl_tree); dle != NULL; 296 dle = AVL_NEXT(&dl->dl_tree, dle)) { 297 if (func(args, dle) != 0) 298 break; 299 } 300 } 301 302 void 303 dsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object) 304 { 305 dmu_object_info_t doi; 306 307 ASSERT(!dsl_deadlist_is_open(dl)); 308 309 mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL); 310 dl->dl_os = os; 311 dl->dl_object = object; 312 VERIFY0(dmu_bonus_hold(os, object, dl, &dl->dl_dbuf)); 313 dmu_object_info_from_db(dl->dl_dbuf, &doi); 314 if (doi.doi_type == DMU_OT_BPOBJ) { 315 dmu_buf_rele(dl->dl_dbuf, dl); 316 dl->dl_dbuf = NULL; 317 dl->dl_oldfmt = B_TRUE; 318 VERIFY0(bpobj_open(&dl->dl_bpobj, os, object)); 319 return; 320 } 321 322 dl->dl_oldfmt = B_FALSE; 323 dl->dl_phys = dl->dl_dbuf->db_data; 324 dl->dl_havetree = B_FALSE; 325 dl->dl_havecache = B_FALSE; 326 } 327 328 boolean_t 329 dsl_deadlist_is_open(dsl_deadlist_t *dl) 330 { 331 return (dl->dl_os != NULL); 332 } 333 334 void 335 dsl_deadlist_close(dsl_deadlist_t *dl) 336 { 337 ASSERT(dsl_deadlist_is_open(dl)); 338 mutex_destroy(&dl->dl_lock); 339 340 if (dl->dl_oldfmt) { 341 dl->dl_oldfmt = B_FALSE; 342 bpobj_close(&dl->dl_bpobj); 343 dl->dl_os = NULL; 344 dl->dl_object = 0; 345 return; 346 } 347 348 if (dl->dl_havetree) { 349 dsl_deadlist_entry_t *dle; 350 void *cookie = NULL; 351 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie)) 352 != NULL) { 353 bpobj_close(&dle->dle_bpobj); 354 kmem_free(dle, sizeof (*dle)); 355 } 356 avl_destroy(&dl->dl_tree); 357 } 358 if (dl->dl_havecache) { 359 dsl_deadlist_cache_entry_t *dlce; 360 void *cookie = NULL; 361 while ((dlce = avl_destroy_nodes(&dl->dl_cache, &cookie)) 362 != NULL) { 363 kmem_free(dlce, sizeof (*dlce)); 364 } 365 avl_destroy(&dl->dl_cache); 366 } 367 dmu_buf_rele(dl->dl_dbuf, dl); 368 dl->dl_dbuf = NULL; 369 dl->dl_phys = NULL; 370 dl->dl_os = NULL; 371 dl->dl_object = 0; 372 } 373 374 uint64_t 375 dsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx) 376 { 377 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS) 378 return (bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx)); 379 return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR, 380 sizeof (dsl_deadlist_phys_t), tx)); 381 } 382 383 void 384 dsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx) 385 { 386 dmu_object_info_t doi; 387 zap_cursor_t zc; 388 zap_attribute_t *za; 389 int error; 390 391 VERIFY0(dmu_object_info(os, dlobj, &doi)); 392 if (doi.doi_type == DMU_OT_BPOBJ) { 393 bpobj_free(os, dlobj, tx); 394 return; 395 } 396 397 za = zap_attribute_alloc(); 398 for (zap_cursor_init(&zc, os, dlobj); 399 (error = zap_cursor_retrieve(&zc, za)) == 0; 400 zap_cursor_advance(&zc)) { 401 uint64_t obj = za->za_first_integer; 402 if (obj == dmu_objset_pool(os)->dp_empty_bpobj) 403 bpobj_decr_empty(os, tx); 404 else 405 bpobj_free(os, obj, tx); 406 } 407 VERIFY3U(error, ==, ENOENT); 408 zap_cursor_fini(&zc); 409 zap_attribute_free(za); 410 VERIFY0(dmu_object_free(os, dlobj, tx)); 411 } 412 413 static void 414 dle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle, 415 const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx) 416 { 417 ASSERT(MUTEX_HELD(&dl->dl_lock)); 418 if (dle->dle_bpobj.bpo_object == 419 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) { 420 uint64_t obj = bpobj_alloc(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx); 421 bpobj_close(&dle->dle_bpobj); 422 bpobj_decr_empty(dl->dl_os, tx); 423 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os, obj)); 424 VERIFY0(zap_update_int_key(dl->dl_os, dl->dl_object, 425 dle->dle_mintxg, obj, tx)); 426 } 427 bpobj_enqueue(&dle->dle_bpobj, bp, bp_freed, tx); 428 } 429 430 static void 431 dle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle, 432 uint64_t obj, dmu_tx_t *tx) 433 { 434 ASSERT(MUTEX_HELD(&dl->dl_lock)); 435 if (dle->dle_bpobj.bpo_object != 436 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) { 437 bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx); 438 } else { 439 bpobj_close(&dle->dle_bpobj); 440 bpobj_decr_empty(dl->dl_os, tx); 441 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os, obj)); 442 VERIFY0(zap_update_int_key(dl->dl_os, dl->dl_object, 443 dle->dle_mintxg, obj, tx)); 444 } 445 } 446 447 /* 448 * Prefetch metadata required for dle_enqueue_subobj(). 449 */ 450 static void 451 dle_prefetch_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle, 452 uint64_t obj) 453 { 454 if (dle->dle_bpobj.bpo_object != 455 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) 456 bpobj_prefetch_subobj(&dle->dle_bpobj, obj); 457 } 458 459 void 460 dsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, boolean_t bp_freed, 461 dmu_tx_t *tx) 462 { 463 dsl_deadlist_entry_t dle_tofind; 464 dsl_deadlist_entry_t *dle; 465 avl_index_t where; 466 467 if (dl->dl_oldfmt) { 468 bpobj_enqueue(&dl->dl_bpobj, bp, bp_freed, tx); 469 return; 470 } 471 472 mutex_enter(&dl->dl_lock); 473 dsl_deadlist_load_tree(dl); 474 475 dmu_buf_will_dirty(dl->dl_dbuf, tx); 476 477 int sign = bp_freed ? -1 : +1; 478 dl->dl_phys->dl_used += 479 sign * bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp); 480 dl->dl_phys->dl_comp += sign * BP_GET_PSIZE(bp); 481 dl->dl_phys->dl_uncomp += sign * BP_GET_UCSIZE(bp); 482 483 dle_tofind.dle_mintxg = BP_GET_LOGICAL_BIRTH(bp); 484 dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 485 if (dle == NULL) 486 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE); 487 else 488 dle = AVL_PREV(&dl->dl_tree, dle); 489 490 if (dle == NULL) { 491 zfs_panic_recover("blkptr at %p has invalid BLK_BIRTH %llu", 492 bp, (longlong_t)BP_GET_LOGICAL_BIRTH(bp)); 493 dle = avl_first(&dl->dl_tree); 494 } 495 496 ASSERT3P(dle, !=, NULL); 497 dle_enqueue(dl, dle, bp, bp_freed, tx); 498 mutex_exit(&dl->dl_lock); 499 } 500 501 int 502 dsl_deadlist_insert_alloc_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 503 { 504 dsl_deadlist_t *dl = arg; 505 dsl_deadlist_insert(dl, bp, B_FALSE, tx); 506 return (0); 507 } 508 509 int 510 dsl_deadlist_insert_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 511 { 512 dsl_deadlist_t *dl = arg; 513 dsl_deadlist_insert(dl, bp, B_TRUE, tx); 514 return (0); 515 } 516 517 /* 518 * Insert new key in deadlist, which must be > all current entries. 519 * mintxg is not inclusive. 520 */ 521 void 522 dsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx) 523 { 524 uint64_t obj; 525 dsl_deadlist_entry_t *dle; 526 527 if (dl->dl_oldfmt) 528 return; 529 530 dle = kmem_alloc(sizeof (*dle), KM_SLEEP); 531 dle->dle_mintxg = mintxg; 532 533 mutex_enter(&dl->dl_lock); 534 dsl_deadlist_load_tree(dl); 535 536 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx); 537 VERIFY0(bpobj_open(&dle->dle_bpobj, dl->dl_os, obj)); 538 avl_add(&dl->dl_tree, dle); 539 540 VERIFY0(zap_add_int_key(dl->dl_os, dl->dl_object, 541 mintxg, obj, tx)); 542 mutex_exit(&dl->dl_lock); 543 } 544 545 /* 546 * Remove this key, merging its entries into the previous key. 547 */ 548 void 549 dsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx) 550 { 551 dsl_deadlist_entry_t dle_tofind; 552 dsl_deadlist_entry_t *dle, *dle_prev; 553 554 if (dl->dl_oldfmt) 555 return; 556 mutex_enter(&dl->dl_lock); 557 dsl_deadlist_load_tree(dl); 558 559 dle_tofind.dle_mintxg = mintxg; 560 dle = avl_find(&dl->dl_tree, &dle_tofind, NULL); 561 ASSERT3P(dle, !=, NULL); 562 dle_prev = AVL_PREV(&dl->dl_tree, dle); 563 ASSERT3P(dle_prev, !=, NULL); 564 565 dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx); 566 567 avl_remove(&dl->dl_tree, dle); 568 bpobj_close(&dle->dle_bpobj); 569 kmem_free(dle, sizeof (*dle)); 570 571 VERIFY0(zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx)); 572 mutex_exit(&dl->dl_lock); 573 } 574 575 /* 576 * Remove a deadlist entry and all of its contents by removing the entry from 577 * the deadlist's avl tree, freeing the entry's bpobj and adjusting the 578 * deadlist's space accounting accordingly. 579 */ 580 void 581 dsl_deadlist_remove_entry(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx) 582 { 583 uint64_t used, comp, uncomp; 584 dsl_deadlist_entry_t dle_tofind; 585 dsl_deadlist_entry_t *dle; 586 objset_t *os = dl->dl_os; 587 588 if (dl->dl_oldfmt) 589 return; 590 591 mutex_enter(&dl->dl_lock); 592 dsl_deadlist_load_tree(dl); 593 594 dle_tofind.dle_mintxg = mintxg; 595 dle = avl_find(&dl->dl_tree, &dle_tofind, NULL); 596 VERIFY3P(dle, !=, NULL); 597 598 avl_remove(&dl->dl_tree, dle); 599 VERIFY0(zap_remove_int(os, dl->dl_object, mintxg, tx)); 600 VERIFY0(bpobj_space(&dle->dle_bpobj, &used, &comp, &uncomp)); 601 dmu_buf_will_dirty(dl->dl_dbuf, tx); 602 dl->dl_phys->dl_used -= used; 603 dl->dl_phys->dl_comp -= comp; 604 dl->dl_phys->dl_uncomp -= uncomp; 605 if (dle->dle_bpobj.bpo_object == dmu_objset_pool(os)->dp_empty_bpobj) { 606 bpobj_decr_empty(os, tx); 607 } else { 608 bpobj_free(os, dle->dle_bpobj.bpo_object, tx); 609 } 610 bpobj_close(&dle->dle_bpobj); 611 kmem_free(dle, sizeof (*dle)); 612 mutex_exit(&dl->dl_lock); 613 } 614 615 /* 616 * Clear out the contents of a deadlist_entry by freeing its bpobj, 617 * replacing it with an empty bpobj and adjusting the deadlist's 618 * space accounting 619 */ 620 void 621 dsl_deadlist_clear_entry(dsl_deadlist_entry_t *dle, dsl_deadlist_t *dl, 622 dmu_tx_t *tx) 623 { 624 uint64_t new_obj, used, comp, uncomp; 625 objset_t *os = dl->dl_os; 626 627 mutex_enter(&dl->dl_lock); 628 VERIFY0(zap_remove_int(os, dl->dl_object, dle->dle_mintxg, tx)); 629 VERIFY0(bpobj_space(&dle->dle_bpobj, &used, &comp, &uncomp)); 630 dmu_buf_will_dirty(dl->dl_dbuf, tx); 631 dl->dl_phys->dl_used -= used; 632 dl->dl_phys->dl_comp -= comp; 633 dl->dl_phys->dl_uncomp -= uncomp; 634 if (dle->dle_bpobj.bpo_object == dmu_objset_pool(os)->dp_empty_bpobj) 635 bpobj_decr_empty(os, tx); 636 else 637 bpobj_free(os, dle->dle_bpobj.bpo_object, tx); 638 bpobj_close(&dle->dle_bpobj); 639 new_obj = bpobj_alloc_empty(os, SPA_OLD_MAXBLOCKSIZE, tx); 640 VERIFY0(bpobj_open(&dle->dle_bpobj, os, new_obj)); 641 VERIFY0(zap_add_int_key(os, dl->dl_object, dle->dle_mintxg, 642 new_obj, tx)); 643 ASSERT(bpobj_is_empty(&dle->dle_bpobj)); 644 mutex_exit(&dl->dl_lock); 645 } 646 647 /* 648 * Return the first entry in deadlist's avl tree 649 */ 650 dsl_deadlist_entry_t * 651 dsl_deadlist_first(dsl_deadlist_t *dl) 652 { 653 dsl_deadlist_entry_t *dle; 654 655 mutex_enter(&dl->dl_lock); 656 dsl_deadlist_load_tree(dl); 657 dle = avl_first(&dl->dl_tree); 658 mutex_exit(&dl->dl_lock); 659 660 return (dle); 661 } 662 663 /* 664 * Return the last entry in deadlist's avl tree 665 */ 666 dsl_deadlist_entry_t * 667 dsl_deadlist_last(dsl_deadlist_t *dl) 668 { 669 dsl_deadlist_entry_t *dle; 670 671 mutex_enter(&dl->dl_lock); 672 dsl_deadlist_load_tree(dl); 673 dle = avl_last(&dl->dl_tree); 674 mutex_exit(&dl->dl_lock); 675 676 return (dle); 677 } 678 679 /* 680 * Walk ds's snapshots to regenerate generate ZAP & AVL. 681 */ 682 static void 683 dsl_deadlist_regenerate(objset_t *os, uint64_t dlobj, 684 uint64_t mrs_obj, dmu_tx_t *tx) 685 { 686 dsl_deadlist_t dl = { 0 }; 687 dsl_pool_t *dp = dmu_objset_pool(os); 688 689 dsl_deadlist_open(&dl, os, dlobj); 690 if (dl.dl_oldfmt) { 691 dsl_deadlist_close(&dl); 692 return; 693 } 694 695 while (mrs_obj != 0) { 696 dsl_dataset_t *ds; 697 VERIFY0(dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds)); 698 dsl_deadlist_add_key(&dl, 699 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 700 mrs_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 701 dsl_dataset_rele(ds, FTAG); 702 } 703 dsl_deadlist_close(&dl); 704 } 705 706 uint64_t 707 dsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg, 708 uint64_t mrs_obj, dmu_tx_t *tx) 709 { 710 dsl_deadlist_entry_t *dle; 711 uint64_t newobj; 712 713 newobj = dsl_deadlist_alloc(dl->dl_os, tx); 714 715 if (dl->dl_oldfmt) { 716 dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx); 717 return (newobj); 718 } 719 720 mutex_enter(&dl->dl_lock); 721 dsl_deadlist_load_tree(dl); 722 723 for (dle = avl_first(&dl->dl_tree); dle; 724 dle = AVL_NEXT(&dl->dl_tree, dle)) { 725 uint64_t obj; 726 727 if (dle->dle_mintxg >= maxtxg) 728 break; 729 730 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx); 731 VERIFY0(zap_add_int_key(dl->dl_os, newobj, 732 dle->dle_mintxg, obj, tx)); 733 } 734 mutex_exit(&dl->dl_lock); 735 return (newobj); 736 } 737 738 void 739 dsl_deadlist_space(dsl_deadlist_t *dl, 740 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 741 { 742 ASSERT(dsl_deadlist_is_open(dl)); 743 if (dl->dl_oldfmt) { 744 VERIFY0(bpobj_space(&dl->dl_bpobj, 745 usedp, compp, uncompp)); 746 return; 747 } 748 749 mutex_enter(&dl->dl_lock); 750 *usedp = dl->dl_phys->dl_used; 751 *compp = dl->dl_phys->dl_comp; 752 *uncompp = dl->dl_phys->dl_uncomp; 753 mutex_exit(&dl->dl_lock); 754 } 755 756 /* 757 * return space used in the range (mintxg, maxtxg]. 758 * Includes maxtxg, does not include mintxg. 759 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is 760 * UINT64_MAX). 761 */ 762 void 763 dsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg, 764 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 765 { 766 dsl_deadlist_cache_entry_t *dlce; 767 dsl_deadlist_cache_entry_t dlce_tofind; 768 avl_index_t where; 769 770 if (dl->dl_oldfmt) { 771 VERIFY0(bpobj_space_range(&dl->dl_bpobj, 772 mintxg, maxtxg, usedp, compp, uncompp)); 773 return; 774 } 775 776 *usedp = *compp = *uncompp = 0; 777 778 mutex_enter(&dl->dl_lock); 779 dsl_deadlist_load_cache(dl); 780 dlce_tofind.dlce_mintxg = mintxg; 781 dlce = avl_find(&dl->dl_cache, &dlce_tofind, &where); 782 783 /* 784 * If this mintxg doesn't exist, it may be an empty_bpobj which 785 * is omitted from the sparse tree. Start at the next non-empty 786 * entry. 787 */ 788 if (dlce == NULL) 789 dlce = avl_nearest(&dl->dl_cache, where, AVL_AFTER); 790 791 for (; dlce && dlce->dlce_mintxg < maxtxg; 792 dlce = AVL_NEXT(&dl->dl_tree, dlce)) { 793 *usedp += dlce->dlce_bytes; 794 *compp += dlce->dlce_comp; 795 *uncompp += dlce->dlce_uncomp; 796 } 797 798 mutex_exit(&dl->dl_lock); 799 } 800 801 static void 802 dsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth, 803 dmu_tx_t *tx) 804 { 805 dsl_deadlist_entry_t dle_tofind; 806 dsl_deadlist_entry_t *dle; 807 avl_index_t where; 808 uint64_t used, comp, uncomp; 809 bpobj_t bpo; 810 811 ASSERT(MUTEX_HELD(&dl->dl_lock)); 812 813 VERIFY0(bpobj_open(&bpo, dl->dl_os, obj)); 814 VERIFY0(bpobj_space(&bpo, &used, &comp, &uncomp)); 815 bpobj_close(&bpo); 816 817 dsl_deadlist_load_tree(dl); 818 819 dmu_buf_will_dirty(dl->dl_dbuf, tx); 820 dl->dl_phys->dl_used += used; 821 dl->dl_phys->dl_comp += comp; 822 dl->dl_phys->dl_uncomp += uncomp; 823 824 dle_tofind.dle_mintxg = birth; 825 dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 826 if (dle == NULL) 827 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE); 828 dle_enqueue_subobj(dl, dle, obj, tx); 829 } 830 831 /* 832 * Prefetch metadata required for dsl_deadlist_insert_bpobj(). 833 */ 834 static void 835 dsl_deadlist_prefetch_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth) 836 { 837 dsl_deadlist_entry_t dle_tofind; 838 dsl_deadlist_entry_t *dle; 839 avl_index_t where; 840 841 ASSERT(MUTEX_HELD(&dl->dl_lock)); 842 843 dsl_deadlist_load_tree(dl); 844 845 dle_tofind.dle_mintxg = birth; 846 dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 847 if (dle == NULL) 848 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE); 849 dle_prefetch_subobj(dl, dle, obj); 850 } 851 852 static int 853 dsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, 854 dmu_tx_t *tx) 855 { 856 dsl_deadlist_t *dl = arg; 857 dsl_deadlist_insert(dl, bp, bp_freed, tx); 858 return (0); 859 } 860 861 /* 862 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as 863 * an empty deadlist. 864 */ 865 void 866 dsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx) 867 { 868 zap_cursor_t zc, pzc; 869 zap_attribute_t *za, *pza; 870 dmu_buf_t *bonus; 871 dsl_deadlist_phys_t *dlp; 872 dmu_object_info_t doi; 873 int error, perror, i; 874 875 VERIFY0(dmu_object_info(dl->dl_os, obj, &doi)); 876 if (doi.doi_type == DMU_OT_BPOBJ) { 877 bpobj_t bpo; 878 VERIFY0(bpobj_open(&bpo, dl->dl_os, obj)); 879 VERIFY0(bpobj_iterate(&bpo, dsl_deadlist_insert_cb, dl, tx)); 880 bpobj_close(&bpo); 881 return; 882 } 883 884 za = zap_attribute_alloc(); 885 pza = zap_attribute_alloc(); 886 887 mutex_enter(&dl->dl_lock); 888 /* 889 * Prefetch up to 128 deadlists first and then more as we progress. 890 * The limit is a balance between ARC use and diminishing returns. 891 */ 892 for (zap_cursor_init(&pzc, dl->dl_os, obj), i = 0; 893 (perror = zap_cursor_retrieve(&pzc, pza)) == 0 && i < 128; 894 zap_cursor_advance(&pzc), i++) { 895 dsl_deadlist_prefetch_bpobj(dl, pza->za_first_integer, 896 zfs_strtonum(pza->za_name, NULL)); 897 } 898 for (zap_cursor_init(&zc, dl->dl_os, obj); 899 (error = zap_cursor_retrieve(&zc, za)) == 0; 900 zap_cursor_advance(&zc)) { 901 dsl_deadlist_insert_bpobj(dl, za->za_first_integer, 902 zfs_strtonum(za->za_name, NULL), tx); 903 VERIFY0(zap_remove(dl->dl_os, obj, za->za_name, tx)); 904 if (perror == 0) { 905 dsl_deadlist_prefetch_bpobj(dl, pza->za_first_integer, 906 zfs_strtonum(pza->za_name, NULL)); 907 zap_cursor_advance(&pzc); 908 perror = zap_cursor_retrieve(&pzc, pza); 909 } 910 } 911 VERIFY3U(error, ==, ENOENT); 912 zap_cursor_fini(&zc); 913 zap_cursor_fini(&pzc); 914 915 VERIFY0(dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus)); 916 dlp = bonus->db_data; 917 dmu_buf_will_dirty(bonus, tx); 918 memset(dlp, 0, sizeof (*dlp)); 919 dmu_buf_rele(bonus, FTAG); 920 mutex_exit(&dl->dl_lock); 921 922 zap_attribute_free(za); 923 zap_attribute_free(pza); 924 } 925 926 /* 927 * Remove entries on dl that are born > mintxg, and put them on the bpobj. 928 */ 929 void 930 dsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg, 931 dmu_tx_t *tx) 932 { 933 dsl_deadlist_entry_t dle_tofind; 934 dsl_deadlist_entry_t *dle, *pdle; 935 avl_index_t where; 936 int i; 937 938 ASSERT(!dl->dl_oldfmt); 939 940 mutex_enter(&dl->dl_lock); 941 dmu_buf_will_dirty(dl->dl_dbuf, tx); 942 dsl_deadlist_load_tree(dl); 943 944 dle_tofind.dle_mintxg = mintxg; 945 dle = avl_find(&dl->dl_tree, &dle_tofind, &where); 946 if (dle == NULL) 947 dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER); 948 /* 949 * Prefetch up to 128 deadlists first and then more as we progress. 950 * The limit is a balance between ARC use and diminishing returns. 951 */ 952 for (pdle = dle, i = 0; pdle && i < 128; i++) { 953 bpobj_prefetch_subobj(bpo, pdle->dle_bpobj.bpo_object); 954 pdle = AVL_NEXT(&dl->dl_tree, pdle); 955 } 956 while (dle) { 957 uint64_t used, comp, uncomp; 958 dsl_deadlist_entry_t *dle_next; 959 960 bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx); 961 if (pdle) { 962 bpobj_prefetch_subobj(bpo, pdle->dle_bpobj.bpo_object); 963 pdle = AVL_NEXT(&dl->dl_tree, pdle); 964 } 965 966 VERIFY0(bpobj_space(&dle->dle_bpobj, 967 &used, &comp, &uncomp)); 968 ASSERT3U(dl->dl_phys->dl_used, >=, used); 969 ASSERT3U(dl->dl_phys->dl_comp, >=, comp); 970 ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp); 971 dl->dl_phys->dl_used -= used; 972 dl->dl_phys->dl_comp -= comp; 973 dl->dl_phys->dl_uncomp -= uncomp; 974 975 VERIFY0(zap_remove_int(dl->dl_os, dl->dl_object, 976 dle->dle_mintxg, tx)); 977 978 dle_next = AVL_NEXT(&dl->dl_tree, dle); 979 avl_remove(&dl->dl_tree, dle); 980 bpobj_close(&dle->dle_bpobj); 981 kmem_free(dle, sizeof (*dle)); 982 dle = dle_next; 983 } 984 mutex_exit(&dl->dl_lock); 985 } 986 987 typedef struct livelist_entry { 988 blkptr_t le_bp; 989 uint32_t le_refcnt; 990 avl_node_t le_node; 991 } livelist_entry_t; 992 993 static int 994 livelist_compare(const void *larg, const void *rarg) 995 { 996 const blkptr_t *l = &((livelist_entry_t *)larg)->le_bp; 997 const blkptr_t *r = &((livelist_entry_t *)rarg)->le_bp; 998 999 /* Sort them according to dva[0] */ 1000 uint64_t l_dva0_vdev = DVA_GET_VDEV(&l->blk_dva[0]); 1001 uint64_t r_dva0_vdev = DVA_GET_VDEV(&r->blk_dva[0]); 1002 1003 if (l_dva0_vdev != r_dva0_vdev) 1004 return (TREE_CMP(l_dva0_vdev, r_dva0_vdev)); 1005 1006 /* if vdevs are equal, sort by offsets. */ 1007 uint64_t l_dva0_offset = DVA_GET_OFFSET(&l->blk_dva[0]); 1008 uint64_t r_dva0_offset = DVA_GET_OFFSET(&r->blk_dva[0]); 1009 return (TREE_CMP(l_dva0_offset, r_dva0_offset)); 1010 } 1011 1012 struct livelist_iter_arg { 1013 avl_tree_t *avl; 1014 bplist_t *to_free; 1015 zthr_t *t; 1016 }; 1017 1018 /* 1019 * Expects an AVL tree which is incrementally filled will FREE blkptrs 1020 * and used to match up ALLOC/FREE pairs. ALLOC'd blkptrs without a 1021 * corresponding FREE are stored in the supplied bplist. 1022 * 1023 * Note that multiple FREE and ALLOC entries for the same blkptr may be 1024 * encountered when dedup or block cloning is involved. For this reason we 1025 * keep a refcount for all the FREE entries of each blkptr and ensure that 1026 * each of those FREE entries has a corresponding ALLOC preceding it. 1027 */ 1028 static int 1029 dsl_livelist_iterate(void *arg, const blkptr_t *bp, boolean_t bp_freed, 1030 dmu_tx_t *tx) 1031 { 1032 struct livelist_iter_arg *lia = arg; 1033 avl_tree_t *avl = lia->avl; 1034 bplist_t *to_free = lia->to_free; 1035 zthr_t *t = lia->t; 1036 ASSERT(tx == NULL); 1037 1038 if ((t != NULL) && (zthr_has_waiters(t) || zthr_iscancelled(t))) 1039 return (SET_ERROR(EINTR)); 1040 1041 livelist_entry_t node; 1042 node.le_bp = *bp; 1043 livelist_entry_t *found = avl_find(avl, &node, NULL); 1044 if (found) { 1045 ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(&found->le_bp)); 1046 ASSERT3U(BP_GET_CHECKSUM(bp), ==, 1047 BP_GET_CHECKSUM(&found->le_bp)); 1048 ASSERT3U(BP_GET_BIRTH(bp), ==, BP_GET_BIRTH(&found->le_bp)); 1049 } 1050 if (bp_freed) { 1051 if (found == NULL) { 1052 /* first free entry for this blkptr */ 1053 livelist_entry_t *e = 1054 kmem_alloc(sizeof (livelist_entry_t), KM_SLEEP); 1055 e->le_bp = *bp; 1056 e->le_refcnt = 1; 1057 avl_add(avl, e); 1058 } else { 1059 /* 1060 * Deduped or cloned block free. We could assert D bit 1061 * for dedup, but there is no such one for cloning. 1062 */ 1063 ASSERT3U(found->le_refcnt + 1, >, found->le_refcnt); 1064 found->le_refcnt++; 1065 } 1066 } else { 1067 if (found == NULL) { 1068 /* block is currently marked as allocated */ 1069 bplist_append(to_free, bp); 1070 } else { 1071 /* alloc matches a free entry */ 1072 ASSERT3U(found->le_refcnt, !=, 0); 1073 found->le_refcnt--; 1074 if (found->le_refcnt == 0) { 1075 /* all tracked free pairs have been matched */ 1076 avl_remove(avl, found); 1077 kmem_free(found, sizeof (livelist_entry_t)); 1078 } 1079 } 1080 } 1081 return (0); 1082 } 1083 1084 /* 1085 * Accepts a bpobj and a bplist. Will insert into the bplist the blkptrs 1086 * which have an ALLOC entry but no matching FREE 1087 */ 1088 int 1089 dsl_process_sub_livelist(bpobj_t *bpobj, bplist_t *to_free, zthr_t *t, 1090 uint64_t *size) 1091 { 1092 avl_tree_t avl; 1093 avl_create(&avl, livelist_compare, sizeof (livelist_entry_t), 1094 offsetof(livelist_entry_t, le_node)); 1095 1096 /* process the sublist */ 1097 struct livelist_iter_arg arg = { 1098 .avl = &avl, 1099 .to_free = to_free, 1100 .t = t 1101 }; 1102 int err = bpobj_iterate_nofree(bpobj, dsl_livelist_iterate, &arg, size); 1103 VERIFY(err != 0 || avl_numnodes(&avl) == 0); 1104 1105 void *cookie = NULL; 1106 livelist_entry_t *le = NULL; 1107 while ((le = avl_destroy_nodes(&avl, &cookie)) != NULL) { 1108 kmem_free(le, sizeof (livelist_entry_t)); 1109 } 1110 avl_destroy(&avl); 1111 return (err); 1112 } 1113 1114 ZFS_MODULE_PARAM(zfs_livelist, zfs_livelist_, max_entries, U64, ZMOD_RW, 1115 "Size to start the next sub-livelist in a livelist"); 1116 1117 ZFS_MODULE_PARAM(zfs_livelist, zfs_livelist_, min_percent_shared, INT, ZMOD_RW, 1118 "Threshold at which livelist is disabled"); 1119