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 http://www.opensolaris.org/os/licensing. 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) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 24 * Copyright (c) 2013 Steven Hartland. All rights reserved. 25 * Copyright (c) 2013 by Joyent, Inc. All rights reserved. 26 * Copyright (c) 2016 Actifio, Inc. All rights reserved. 27 */ 28 29 #include <sys/zfs_context.h> 30 #include <sys/dsl_userhold.h> 31 #include <sys/dsl_dataset.h> 32 #include <sys/dsl_synctask.h> 33 #include <sys/dsl_destroy.h> 34 #include <sys/dsl_bookmark.h> 35 #include <sys/dmu_tx.h> 36 #include <sys/dsl_pool.h> 37 #include <sys/dsl_dir.h> 38 #include <sys/dmu_traverse.h> 39 #include <sys/dsl_scan.h> 40 #include <sys/dmu_objset.h> 41 #include <sys/zap.h> 42 #include <sys/zfeature.h> 43 #include <sys/zfs_ioctl.h> 44 #include <sys/dsl_deleg.h> 45 #include <sys/dmu_impl.h> 46 #include <sys/zvol.h> 47 #include <sys/zcp.h> 48 #include <sys/dsl_deadlist.h> 49 #include <sys/zthr.h> 50 #include <sys/spa_impl.h> 51 52 int 53 dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer) 54 { 55 if (!ds->ds_is_snapshot) 56 return (SET_ERROR(EINVAL)); 57 58 if (dsl_dataset_long_held(ds)) 59 return (SET_ERROR(EBUSY)); 60 61 /* 62 * Only allow deferred destroy on pools that support it. 63 * NOTE: deferred destroy is only supported on snapshots. 64 */ 65 if (defer) { 66 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 67 SPA_VERSION_USERREFS) 68 return (SET_ERROR(ENOTSUP)); 69 return (0); 70 } 71 72 /* 73 * If this snapshot has an elevated user reference count, 74 * we can't destroy it yet. 75 */ 76 if (ds->ds_userrefs > 0) 77 return (SET_ERROR(EBUSY)); 78 79 /* 80 * Can't delete a branch point. 81 */ 82 if (dsl_dataset_phys(ds)->ds_num_children > 1) 83 return (SET_ERROR(EEXIST)); 84 85 return (0); 86 } 87 88 int 89 dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx) 90 { 91 dsl_destroy_snapshot_arg_t *ddsa = arg; 92 const char *dsname = ddsa->ddsa_name; 93 boolean_t defer = ddsa->ddsa_defer; 94 95 dsl_pool_t *dp = dmu_tx_pool(tx); 96 int error = 0; 97 dsl_dataset_t *ds; 98 99 error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 100 101 /* 102 * If the snapshot does not exist, silently ignore it, and 103 * dsl_destroy_snapshot_sync() will be a no-op 104 * (it's "already destroyed"). 105 */ 106 if (error == ENOENT) 107 return (0); 108 109 if (error == 0) { 110 error = dsl_destroy_snapshot_check_impl(ds, defer); 111 dsl_dataset_rele(ds, FTAG); 112 } 113 114 return (error); 115 } 116 117 struct process_old_arg { 118 dsl_dataset_t *ds; 119 dsl_dataset_t *ds_prev; 120 boolean_t after_branch_point; 121 zio_t *pio; 122 uint64_t used, comp, uncomp; 123 }; 124 125 static int 126 process_old_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx) 127 { 128 struct process_old_arg *poa = arg; 129 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool; 130 131 ASSERT(!BP_IS_HOLE(bp)); 132 133 if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) { 134 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, bp_freed, tx); 135 if (poa->ds_prev && !poa->after_branch_point && 136 bp->blk_birth > 137 dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) { 138 dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes += 139 bp_get_dsize_sync(dp->dp_spa, bp); 140 } 141 } else { 142 poa->used += bp_get_dsize_sync(dp->dp_spa, bp); 143 poa->comp += BP_GET_PSIZE(bp); 144 poa->uncomp += BP_GET_UCSIZE(bp); 145 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp); 146 } 147 return (0); 148 } 149 150 static void 151 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev, 152 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx) 153 { 154 struct process_old_arg poa = { 0 }; 155 dsl_pool_t *dp = ds->ds_dir->dd_pool; 156 objset_t *mos = dp->dp_meta_objset; 157 uint64_t deadlist_obj; 158 159 ASSERT(ds->ds_deadlist.dl_oldfmt); 160 ASSERT(ds_next->ds_deadlist.dl_oldfmt); 161 162 poa.ds = ds; 163 poa.ds_prev = ds_prev; 164 poa.after_branch_point = after_branch_point; 165 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 166 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj, 167 process_old_cb, &poa, tx)); 168 VERIFY0(zio_wait(poa.pio)); 169 ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes); 170 171 /* change snapused */ 172 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 173 -poa.used, -poa.comp, -poa.uncomp, tx); 174 175 /* swap next's deadlist to our deadlist */ 176 dsl_deadlist_close(&ds->ds_deadlist); 177 dsl_deadlist_close(&ds_next->ds_deadlist); 178 deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 179 dsl_dataset_phys(ds)->ds_deadlist_obj = 180 dsl_dataset_phys(ds_next)->ds_deadlist_obj; 181 dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj; 182 dsl_deadlist_open(&ds->ds_deadlist, mos, 183 dsl_dataset_phys(ds)->ds_deadlist_obj); 184 dsl_deadlist_open(&ds_next->ds_deadlist, mos, 185 dsl_dataset_phys(ds_next)->ds_deadlist_obj); 186 } 187 188 typedef struct remaining_clones_key { 189 dsl_dataset_t *rck_clone; 190 list_node_t rck_node; 191 } remaining_clones_key_t; 192 193 static remaining_clones_key_t * 194 rck_alloc(dsl_dataset_t *clone) 195 { 196 remaining_clones_key_t *rck = kmem_alloc(sizeof (*rck), KM_SLEEP); 197 rck->rck_clone = clone; 198 return (rck); 199 } 200 201 static void 202 dsl_dir_remove_clones_key_impl(dsl_dir_t *dd, uint64_t mintxg, dmu_tx_t *tx, 203 list_t *stack, void *tag) 204 { 205 objset_t *mos = dd->dd_pool->dp_meta_objset; 206 207 /* 208 * If it is the old version, dd_clones doesn't exist so we can't 209 * find the clones, but dsl_deadlist_remove_key() is a no-op so it 210 * doesn't matter. 211 */ 212 if (dsl_dir_phys(dd)->dd_clones == 0) 213 return; 214 215 zap_cursor_t *zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 216 zap_attribute_t *za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 217 218 for (zap_cursor_init(zc, mos, dsl_dir_phys(dd)->dd_clones); 219 zap_cursor_retrieve(zc, za) == 0; 220 zap_cursor_advance(zc)) { 221 dsl_dataset_t *clone; 222 223 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 224 za->za_first_integer, tag, &clone)); 225 226 if (clone->ds_dir->dd_origin_txg > mintxg) { 227 dsl_deadlist_remove_key(&clone->ds_deadlist, 228 mintxg, tx); 229 230 if (dsl_dataset_remap_deadlist_exists(clone)) { 231 dsl_deadlist_remove_key( 232 &clone->ds_remap_deadlist, mintxg, tx); 233 } 234 235 list_insert_head(stack, rck_alloc(clone)); 236 } else { 237 dsl_dataset_rele(clone, tag); 238 } 239 } 240 zap_cursor_fini(zc); 241 242 kmem_free(za, sizeof (zap_attribute_t)); 243 kmem_free(zc, sizeof (zap_cursor_t)); 244 } 245 246 void 247 dsl_dir_remove_clones_key(dsl_dir_t *top_dd, uint64_t mintxg, dmu_tx_t *tx) 248 { 249 list_t stack; 250 251 list_create(&stack, sizeof (remaining_clones_key_t), 252 offsetof(remaining_clones_key_t, rck_node)); 253 254 dsl_dir_remove_clones_key_impl(top_dd, mintxg, tx, &stack, FTAG); 255 for (remaining_clones_key_t *rck = list_remove_head(&stack); 256 rck != NULL; rck = list_remove_head(&stack)) { 257 dsl_dataset_t *clone = rck->rck_clone; 258 dsl_dir_t *clone_dir = clone->ds_dir; 259 260 kmem_free(rck, sizeof (*rck)); 261 262 dsl_dir_remove_clones_key_impl(clone_dir, mintxg, tx, 263 &stack, FTAG); 264 dsl_dataset_rele(clone, FTAG); 265 } 266 267 list_destroy(&stack); 268 } 269 270 static void 271 dsl_destroy_snapshot_handle_remaps(dsl_dataset_t *ds, dsl_dataset_t *ds_next, 272 dmu_tx_t *tx) 273 { 274 dsl_pool_t *dp = ds->ds_dir->dd_pool; 275 276 /* Move blocks to be obsoleted to pool's obsolete list. */ 277 if (dsl_dataset_remap_deadlist_exists(ds_next)) { 278 if (!bpobj_is_open(&dp->dp_obsolete_bpobj)) 279 dsl_pool_create_obsolete_bpobj(dp, tx); 280 281 dsl_deadlist_move_bpobj(&ds_next->ds_remap_deadlist, 282 &dp->dp_obsolete_bpobj, 283 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 284 } 285 286 /* Merge our deadlist into next's and free it. */ 287 if (dsl_dataset_remap_deadlist_exists(ds)) { 288 uint64_t remap_deadlist_object = 289 dsl_dataset_get_remap_deadlist_object(ds); 290 ASSERT(remap_deadlist_object != 0); 291 292 mutex_enter(&ds_next->ds_remap_deadlist_lock); 293 if (!dsl_dataset_remap_deadlist_exists(ds_next)) 294 dsl_dataset_create_remap_deadlist(ds_next, tx); 295 mutex_exit(&ds_next->ds_remap_deadlist_lock); 296 297 dsl_deadlist_merge(&ds_next->ds_remap_deadlist, 298 remap_deadlist_object, tx); 299 dsl_dataset_destroy_remap_deadlist(ds, tx); 300 } 301 } 302 303 void 304 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx) 305 { 306 int after_branch_point = FALSE; 307 dsl_pool_t *dp = ds->ds_dir->dd_pool; 308 objset_t *mos = dp->dp_meta_objset; 309 dsl_dataset_t *ds_prev = NULL; 310 uint64_t obj; 311 312 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 313 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 314 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg); 315 rrw_exit(&ds->ds_bp_rwlock, FTAG); 316 ASSERT(zfs_refcount_is_zero(&ds->ds_longholds)); 317 318 if (defer && 319 (ds->ds_userrefs > 0 || 320 dsl_dataset_phys(ds)->ds_num_children > 1)) { 321 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 322 dmu_buf_will_dirty(ds->ds_dbuf, tx); 323 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY; 324 spa_history_log_internal_ds(ds, "defer_destroy", tx, " "); 325 return; 326 } 327 328 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1); 329 330 /* We need to log before removing it from the namespace. */ 331 spa_history_log_internal_ds(ds, "destroy", tx, " "); 332 333 dsl_scan_ds_destroyed(ds, tx); 334 335 obj = ds->ds_object; 336 337 boolean_t book_exists = dsl_bookmark_ds_destroyed(ds, tx); 338 339 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 340 if (dsl_dataset_feature_is_active(ds, f)) 341 dsl_dataset_deactivate_feature(ds, f, tx); 342 } 343 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 344 ASSERT3P(ds->ds_prev, ==, NULL); 345 VERIFY0(dsl_dataset_hold_obj(dp, 346 dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev)); 347 after_branch_point = 348 (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj); 349 350 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 351 if (after_branch_point && 352 dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) { 353 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx); 354 if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) { 355 VERIFY0(zap_add_int(mos, 356 dsl_dataset_phys(ds_prev)-> 357 ds_next_clones_obj, 358 dsl_dataset_phys(ds)->ds_next_snap_obj, 359 tx)); 360 } 361 } 362 if (!after_branch_point) { 363 dsl_dataset_phys(ds_prev)->ds_next_snap_obj = 364 dsl_dataset_phys(ds)->ds_next_snap_obj; 365 } 366 } 367 368 dsl_dataset_t *ds_next; 369 uint64_t old_unique; 370 uint64_t used = 0, comp = 0, uncomp = 0; 371 372 VERIFY0(dsl_dataset_hold_obj(dp, 373 dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next)); 374 ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj); 375 376 old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes; 377 378 dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 379 dsl_dataset_phys(ds_next)->ds_prev_snap_obj = 380 dsl_dataset_phys(ds)->ds_prev_snap_obj; 381 dsl_dataset_phys(ds_next)->ds_prev_snap_txg = 382 dsl_dataset_phys(ds)->ds_prev_snap_txg; 383 ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 384 ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0); 385 386 if (ds_next->ds_deadlist.dl_oldfmt) { 387 process_old_deadlist(ds, ds_prev, ds_next, 388 after_branch_point, tx); 389 } else { 390 /* Adjust prev's unique space. */ 391 if (ds_prev && !after_branch_point) { 392 dsl_deadlist_space_range(&ds_next->ds_deadlist, 393 dsl_dataset_phys(ds_prev)->ds_prev_snap_txg, 394 dsl_dataset_phys(ds)->ds_prev_snap_txg, 395 &used, &comp, &uncomp); 396 dsl_dataset_phys(ds_prev)->ds_unique_bytes += used; 397 } 398 399 /* Adjust snapused. */ 400 dsl_deadlist_space_range(&ds_next->ds_deadlist, 401 dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX, 402 &used, &comp, &uncomp); 403 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 404 -used, -comp, -uncomp, tx); 405 406 /* Move blocks to be freed to pool's free list. */ 407 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist, 408 &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg, 409 tx); 410 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, 411 DD_USED_HEAD, used, comp, uncomp, tx); 412 413 /* Merge our deadlist into next's and free it. */ 414 dsl_deadlist_merge(&ds_next->ds_deadlist, 415 dsl_dataset_phys(ds)->ds_deadlist_obj, tx); 416 417 /* 418 * We are done with the deadlist tree (generated/used 419 * by dsl_deadlist_move_bpobj() and dsl_deadlist_merge()). 420 * Discard it to save memory. 421 */ 422 dsl_deadlist_discard_tree(&ds_next->ds_deadlist); 423 } 424 425 dsl_deadlist_close(&ds->ds_deadlist); 426 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx); 427 dmu_buf_will_dirty(ds->ds_dbuf, tx); 428 dsl_dataset_phys(ds)->ds_deadlist_obj = 0; 429 430 dsl_destroy_snapshot_handle_remaps(ds, ds_next, tx); 431 432 if (!book_exists) { 433 /* Collapse range in clone heads */ 434 dsl_dir_remove_clones_key(ds->ds_dir, 435 dsl_dataset_phys(ds)->ds_creation_txg, tx); 436 } 437 438 if (ds_next->ds_is_snapshot) { 439 dsl_dataset_t *ds_nextnext; 440 441 /* 442 * Update next's unique to include blocks which 443 * were previously shared by only this snapshot 444 * and it. Those blocks will be born after the 445 * prev snap and before this snap, and will have 446 * died after the next snap and before the one 447 * after that (ie. be on the snap after next's 448 * deadlist). 449 */ 450 VERIFY0(dsl_dataset_hold_obj(dp, 451 dsl_dataset_phys(ds_next)->ds_next_snap_obj, 452 FTAG, &ds_nextnext)); 453 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist, 454 dsl_dataset_phys(ds)->ds_prev_snap_txg, 455 dsl_dataset_phys(ds)->ds_creation_txg, 456 &used, &comp, &uncomp); 457 dsl_dataset_phys(ds_next)->ds_unique_bytes += used; 458 dsl_dataset_rele(ds_nextnext, FTAG); 459 ASSERT3P(ds_next->ds_prev, ==, NULL); 460 461 /* Collapse range in this head. */ 462 dsl_dataset_t *hds; 463 VERIFY0(dsl_dataset_hold_obj(dp, 464 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, 465 FTAG, &hds)); 466 if (!book_exists) { 467 /* Collapse range in this head. */ 468 dsl_deadlist_remove_key(&hds->ds_deadlist, 469 dsl_dataset_phys(ds)->ds_creation_txg, tx); 470 } 471 if (dsl_dataset_remap_deadlist_exists(hds)) { 472 dsl_deadlist_remove_key(&hds->ds_remap_deadlist, 473 dsl_dataset_phys(ds)->ds_creation_txg, tx); 474 } 475 dsl_dataset_rele(hds, FTAG); 476 477 } else { 478 ASSERT3P(ds_next->ds_prev, ==, ds); 479 dsl_dataset_rele(ds_next->ds_prev, ds_next); 480 ds_next->ds_prev = NULL; 481 if (ds_prev) { 482 VERIFY0(dsl_dataset_hold_obj(dp, 483 dsl_dataset_phys(ds)->ds_prev_snap_obj, 484 ds_next, &ds_next->ds_prev)); 485 } 486 487 dsl_dataset_recalc_head_uniq(ds_next); 488 489 /* 490 * Reduce the amount of our unconsumed refreservation 491 * being charged to our parent by the amount of 492 * new unique data we have gained. 493 */ 494 if (old_unique < ds_next->ds_reserved) { 495 int64_t mrsdelta; 496 uint64_t new_unique = 497 dsl_dataset_phys(ds_next)->ds_unique_bytes; 498 499 ASSERT(old_unique <= new_unique); 500 mrsdelta = MIN(new_unique - old_unique, 501 ds_next->ds_reserved - old_unique); 502 dsl_dir_diduse_space(ds->ds_dir, 503 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 504 } 505 } 506 dsl_dataset_rele(ds_next, FTAG); 507 508 /* 509 * This must be done after the dsl_traverse(), because it will 510 * re-open the objset. 511 */ 512 if (ds->ds_objset) { 513 dmu_objset_evict(ds->ds_objset); 514 ds->ds_objset = NULL; 515 } 516 517 /* remove from snapshot namespace */ 518 dsl_dataset_t *ds_head; 519 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0); 520 VERIFY0(dsl_dataset_hold_obj(dp, 521 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head)); 522 VERIFY0(dsl_dataset_get_snapname(ds)); 523 #ifdef ZFS_DEBUG 524 { 525 uint64_t val; 526 int err; 527 528 err = dsl_dataset_snap_lookup(ds_head, 529 ds->ds_snapname, &val); 530 ASSERT0(err); 531 ASSERT3U(val, ==, obj); 532 } 533 #endif 534 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE)); 535 dsl_dataset_rele(ds_head, FTAG); 536 537 if (ds_prev != NULL) 538 dsl_dataset_rele(ds_prev, FTAG); 539 540 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 541 542 if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 543 uint64_t count __maybe_unused; 544 ASSERT0(zap_count(mos, 545 dsl_dataset_phys(ds)->ds_next_clones_obj, &count) && 546 count == 0); 547 VERIFY0(dmu_object_free(mos, 548 dsl_dataset_phys(ds)->ds_next_clones_obj, tx)); 549 } 550 if (dsl_dataset_phys(ds)->ds_props_obj != 0) 551 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj, 552 tx)); 553 if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0) 554 VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj, 555 tx)); 556 dsl_dir_rele(ds->ds_dir, ds); 557 ds->ds_dir = NULL; 558 dmu_object_free_zapified(mos, obj, tx); 559 } 560 561 void 562 dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx) 563 { 564 dsl_destroy_snapshot_arg_t *ddsa = arg; 565 const char *dsname = ddsa->ddsa_name; 566 boolean_t defer = ddsa->ddsa_defer; 567 568 dsl_pool_t *dp = dmu_tx_pool(tx); 569 dsl_dataset_t *ds; 570 571 int error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 572 if (error == ENOENT) 573 return; 574 ASSERT0(error); 575 dsl_destroy_snapshot_sync_impl(ds, defer, tx); 576 zvol_remove_minors(dp->dp_spa, dsname, B_TRUE); 577 dsl_dataset_rele(ds, FTAG); 578 } 579 580 /* 581 * The semantics of this function are described in the comment above 582 * lzc_destroy_snaps(). To summarize: 583 * 584 * The snapshots must all be in the same pool. 585 * 586 * Snapshots that don't exist will be silently ignored (considered to be 587 * "already deleted"). 588 * 589 * On success, all snaps will be destroyed and this will return 0. 590 * On failure, no snaps will be destroyed, the errlist will be filled in, 591 * and this will return an errno. 592 */ 593 int 594 dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer, 595 nvlist_t *errlist) 596 { 597 if (nvlist_next_nvpair(snaps, NULL) == NULL) 598 return (0); 599 600 /* 601 * lzc_destroy_snaps() is documented to take an nvlist whose 602 * values "don't matter". We need to convert that nvlist to 603 * one that we know can be converted to LUA. We also don't 604 * care about any duplicate entries because the nvlist will 605 * be converted to a LUA table which should take care of this. 606 */ 607 nvlist_t *snaps_normalized; 608 VERIFY0(nvlist_alloc(&snaps_normalized, 0, KM_SLEEP)); 609 for (nvpair_t *pair = nvlist_next_nvpair(snaps, NULL); 610 pair != NULL; pair = nvlist_next_nvpair(snaps, pair)) { 611 fnvlist_add_boolean_value(snaps_normalized, 612 nvpair_name(pair), B_TRUE); 613 } 614 615 nvlist_t *arg; 616 VERIFY0(nvlist_alloc(&arg, 0, KM_SLEEP)); 617 fnvlist_add_nvlist(arg, "snaps", snaps_normalized); 618 fnvlist_free(snaps_normalized); 619 fnvlist_add_boolean_value(arg, "defer", defer); 620 621 nvlist_t *wrapper; 622 VERIFY0(nvlist_alloc(&wrapper, 0, KM_SLEEP)); 623 fnvlist_add_nvlist(wrapper, ZCP_ARG_ARGLIST, arg); 624 fnvlist_free(arg); 625 626 const char *program = 627 "arg = ...\n" 628 "snaps = arg['snaps']\n" 629 "defer = arg['defer']\n" 630 "errors = { }\n" 631 "has_errors = false\n" 632 "for snap, v in pairs(snaps) do\n" 633 " errno = zfs.check.destroy{snap, defer=defer}\n" 634 " zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n" 635 " if errno == ENOENT then\n" 636 " snaps[snap] = nil\n" 637 " elseif errno ~= 0 then\n" 638 " errors[snap] = errno\n" 639 " has_errors = true\n" 640 " end\n" 641 "end\n" 642 "if has_errors then\n" 643 " return errors\n" 644 "end\n" 645 "for snap, v in pairs(snaps) do\n" 646 " errno = zfs.sync.destroy{snap, defer=defer}\n" 647 " assert(errno == 0)\n" 648 "end\n" 649 "return { }\n"; 650 651 nvlist_t *result = fnvlist_alloc(); 652 int error = zcp_eval(nvpair_name(nvlist_next_nvpair(snaps, NULL)), 653 program, 654 B_TRUE, 655 0, 656 zfs_lua_max_memlimit, 657 nvlist_next_nvpair(wrapper, NULL), result); 658 if (error != 0) { 659 char *errorstr = NULL; 660 (void) nvlist_lookup_string(result, ZCP_RET_ERROR, &errorstr); 661 if (errorstr != NULL) { 662 zfs_dbgmsg(errorstr); 663 } 664 fnvlist_free(wrapper); 665 fnvlist_free(result); 666 return (error); 667 } 668 fnvlist_free(wrapper); 669 670 /* 671 * lzc_destroy_snaps() is documented to fill the errlist with 672 * int32 values, so we need to convert the int64 values that are 673 * returned from LUA. 674 */ 675 int rv = 0; 676 nvlist_t *errlist_raw = fnvlist_lookup_nvlist(result, ZCP_RET_RETURN); 677 for (nvpair_t *pair = nvlist_next_nvpair(errlist_raw, NULL); 678 pair != NULL; pair = nvlist_next_nvpair(errlist_raw, pair)) { 679 int32_t val = (int32_t)fnvpair_value_int64(pair); 680 if (rv == 0) 681 rv = val; 682 fnvlist_add_int32(errlist, nvpair_name(pair), val); 683 } 684 fnvlist_free(result); 685 return (rv); 686 } 687 688 int 689 dsl_destroy_snapshot(const char *name, boolean_t defer) 690 { 691 int error; 692 nvlist_t *nvl = fnvlist_alloc(); 693 nvlist_t *errlist = fnvlist_alloc(); 694 695 fnvlist_add_boolean(nvl, name); 696 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist); 697 fnvlist_free(errlist); 698 fnvlist_free(nvl); 699 return (error); 700 } 701 702 struct killarg { 703 dsl_dataset_t *ds; 704 dmu_tx_t *tx; 705 }; 706 707 /* ARGSUSED */ 708 static int 709 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 710 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 711 { 712 struct killarg *ka = arg; 713 dmu_tx_t *tx = ka->tx; 714 715 if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || 716 BP_IS_EMBEDDED(bp)) 717 return (0); 718 719 if (zb->zb_level == ZB_ZIL_LEVEL) { 720 ASSERT(zilog != NULL); 721 /* 722 * It's a block in the intent log. It has no 723 * accounting, so just free it. 724 */ 725 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp); 726 } else { 727 ASSERT(zilog == NULL); 728 ASSERT3U(bp->blk_birth, >, 729 dsl_dataset_phys(ka->ds)->ds_prev_snap_txg); 730 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE); 731 } 732 733 return (0); 734 } 735 736 static void 737 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx) 738 { 739 struct killarg ka; 740 741 spa_history_log_internal_ds(ds, "destroy", tx, 742 "(synchronous, mintxg=%llu)", 743 (long long)dsl_dataset_phys(ds)->ds_prev_snap_txg); 744 745 /* 746 * Free everything that we point to (that's born after 747 * the previous snapshot, if we are a clone) 748 * 749 * NB: this should be very quick, because we already 750 * freed all the objects in open context. 751 */ 752 ka.ds = ds; 753 ka.tx = tx; 754 VERIFY0(traverse_dataset(ds, 755 dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST | 756 TRAVERSE_NO_DECRYPT, kill_blkptr, &ka)); 757 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 758 dsl_dataset_phys(ds)->ds_unique_bytes == 0); 759 } 760 761 int 762 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds) 763 { 764 int error; 765 uint64_t count; 766 objset_t *mos; 767 768 ASSERT(!ds->ds_is_snapshot); 769 if (ds->ds_is_snapshot) 770 return (SET_ERROR(EINVAL)); 771 772 if (zfs_refcount_count(&ds->ds_longholds) != expected_holds) 773 return (SET_ERROR(EBUSY)); 774 775 ASSERT0(ds->ds_dir->dd_activity_waiters); 776 777 mos = ds->ds_dir->dd_pool->dp_meta_objset; 778 779 /* 780 * Can't delete a head dataset if there are snapshots of it. 781 * (Except if the only snapshots are from the branch we cloned 782 * from.) 783 */ 784 if (ds->ds_prev != NULL && 785 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object) 786 return (SET_ERROR(EBUSY)); 787 788 /* 789 * Can't delete if there are children of this fs. 790 */ 791 error = zap_count(mos, 792 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count); 793 if (error != 0) 794 return (error); 795 if (count != 0) 796 return (SET_ERROR(EEXIST)); 797 798 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) && 799 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 && 800 ds->ds_prev->ds_userrefs == 0) { 801 /* We need to remove the origin snapshot as well. */ 802 if (!zfs_refcount_is_zero(&ds->ds_prev->ds_longholds)) 803 return (SET_ERROR(EBUSY)); 804 } 805 return (0); 806 } 807 808 int 809 dsl_destroy_head_check(void *arg, dmu_tx_t *tx) 810 { 811 dsl_destroy_head_arg_t *ddha = arg; 812 dsl_pool_t *dp = dmu_tx_pool(tx); 813 dsl_dataset_t *ds; 814 int error; 815 816 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds); 817 if (error != 0) 818 return (error); 819 820 error = dsl_destroy_head_check_impl(ds, 0); 821 dsl_dataset_rele(ds, FTAG); 822 return (error); 823 } 824 825 static void 826 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx) 827 { 828 dsl_dir_t *dd; 829 dsl_pool_t *dp = dmu_tx_pool(tx); 830 objset_t *mos = dp->dp_meta_objset; 831 dd_used_t t; 832 833 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock)); 834 835 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd)); 836 837 ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj); 838 839 /* Decrement the filesystem count for all parent filesystems. */ 840 if (dd->dd_parent != NULL) 841 dsl_fs_ss_count_adjust(dd->dd_parent, -1, 842 DD_FIELD_FILESYSTEM_COUNT, tx); 843 844 /* 845 * Remove our reservation. The impl() routine avoids setting the 846 * actual property, which would require the (already destroyed) ds. 847 */ 848 dsl_dir_set_reservation_sync_impl(dd, 0, tx); 849 850 ASSERT0(dsl_dir_phys(dd)->dd_used_bytes); 851 ASSERT0(dsl_dir_phys(dd)->dd_reserved); 852 for (t = 0; t < DD_USED_NUM; t++) 853 ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]); 854 855 if (dd->dd_crypto_obj != 0) { 856 dsl_crypto_key_destroy_sync(dd->dd_crypto_obj, tx); 857 (void) spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object); 858 } 859 860 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx)); 861 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx)); 862 if (dsl_dir_phys(dd)->dd_clones != 0) 863 VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_clones, tx)); 864 VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx)); 865 VERIFY0(zap_remove(mos, 866 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj, 867 dd->dd_myname, tx)); 868 869 dsl_dir_rele(dd, FTAG); 870 dmu_object_free_zapified(mos, ddobj, tx); 871 } 872 873 static void 874 dsl_clone_destroy_assert(dsl_dir_t *dd) 875 { 876 uint64_t used, comp, uncomp; 877 878 ASSERT(dsl_dir_is_clone(dd)); 879 dsl_deadlist_space(&dd->dd_livelist, &used, &comp, &uncomp); 880 881 ASSERT3U(dsl_dir_phys(dd)->dd_used_bytes, ==, used); 882 ASSERT3U(dsl_dir_phys(dd)->dd_compressed_bytes, ==, comp); 883 /* 884 * Greater than because we do not track embedded block pointers in 885 * the livelist 886 */ 887 ASSERT3U(dsl_dir_phys(dd)->dd_uncompressed_bytes, >=, uncomp); 888 889 ASSERT(list_is_empty(&dd->dd_pending_allocs.bpl_list)); 890 ASSERT(list_is_empty(&dd->dd_pending_frees.bpl_list)); 891 } 892 893 /* 894 * Start the delete process for a clone. Free its zil, verify the space usage 895 * and queue the blkptrs for deletion by adding the livelist to the pool-wide 896 * delete queue. 897 */ 898 static void 899 dsl_async_clone_destroy(dsl_dataset_t *ds, dmu_tx_t *tx) 900 { 901 uint64_t zap_obj, to_delete, used, comp, uncomp; 902 objset_t *os; 903 dsl_dir_t *dd = ds->ds_dir; 904 dsl_pool_t *dp = dmu_tx_pool(tx); 905 objset_t *mos = dp->dp_meta_objset; 906 spa_t *spa = dmu_tx_pool(tx)->dp_spa; 907 VERIFY0(dmu_objset_from_ds(ds, &os)); 908 909 uint64_t mintxg = 0; 910 dsl_deadlist_entry_t *dle = dsl_deadlist_first(&dd->dd_livelist); 911 if (dle != NULL) 912 mintxg = dle->dle_mintxg; 913 914 spa_history_log_internal_ds(ds, "destroy", tx, 915 "(livelist, mintxg=%llu)", (long long)mintxg); 916 917 /* Check that the clone is in a correct state to be deleted */ 918 dsl_clone_destroy_assert(dd); 919 920 /* Destroy the zil */ 921 zil_destroy_sync(dmu_objset_zil(os), tx); 922 923 VERIFY0(zap_lookup(mos, dd->dd_object, 924 DD_FIELD_LIVELIST, sizeof (uint64_t), 1, &to_delete)); 925 /* Initialize deleted_clones entry to track livelists to cleanup */ 926 int error = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT, 927 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj); 928 if (error == ENOENT) { 929 zap_obj = zap_create(mos, DMU_OTN_ZAP_METADATA, 930 DMU_OT_NONE, 0, tx); 931 VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 932 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, 933 &(zap_obj), tx)); 934 spa->spa_livelists_to_delete = zap_obj; 935 } else if (error != 0) { 936 zfs_panic_recover("zfs: error %d was returned while looking " 937 "up DMU_POOL_DELETED_CLONES in the zap", error); 938 return; 939 } 940 VERIFY0(zap_add_int(mos, zap_obj, to_delete, tx)); 941 942 /* Clone is no longer using space, now tracked by dp_free_dir */ 943 dsl_deadlist_space(&dd->dd_livelist, &used, &comp, &uncomp); 944 dsl_dir_diduse_space(dd, DD_USED_HEAD, 945 -used, -comp, -dsl_dir_phys(dd)->dd_uncompressed_bytes, 946 tx); 947 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD, 948 used, comp, uncomp, tx); 949 dsl_dir_remove_livelist(dd, tx, B_FALSE); 950 zthr_wakeup(spa->spa_livelist_delete_zthr); 951 } 952 953 /* 954 * Move the bptree into the pool's list of trees to clean up, update space 955 * accounting information and destroy the zil. 956 */ 957 static void 958 dsl_async_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx) 959 { 960 uint64_t used, comp, uncomp; 961 objset_t *os; 962 963 VERIFY0(dmu_objset_from_ds(ds, &os)); 964 dsl_pool_t *dp = dmu_tx_pool(tx); 965 objset_t *mos = dp->dp_meta_objset; 966 967 spa_history_log_internal_ds(ds, "destroy", tx, 968 "(bptree, mintxg=%llu)", 969 (long long)dsl_dataset_phys(ds)->ds_prev_snap_txg); 970 971 zil_destroy_sync(dmu_objset_zil(os), tx); 972 973 if (!spa_feature_is_active(dp->dp_spa, 974 SPA_FEATURE_ASYNC_DESTROY)) { 975 dsl_scan_t *scn = dp->dp_scan; 976 spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY, 977 tx); 978 dp->dp_bptree_obj = bptree_alloc(mos, tx); 979 VERIFY0(zap_add(mos, 980 DMU_POOL_DIRECTORY_OBJECT, 981 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, 982 &dp->dp_bptree_obj, tx)); 983 ASSERT(!scn->scn_async_destroying); 984 scn->scn_async_destroying = B_TRUE; 985 } 986 987 used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes; 988 comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes; 989 uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes; 990 991 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 992 dsl_dataset_phys(ds)->ds_unique_bytes == used); 993 994 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 995 bptree_add(mos, dp->dp_bptree_obj, 996 &dsl_dataset_phys(ds)->ds_bp, 997 dsl_dataset_phys(ds)->ds_prev_snap_txg, 998 used, comp, uncomp, tx); 999 rrw_exit(&ds->ds_bp_rwlock, FTAG); 1000 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 1001 -used, -comp, -uncomp, tx); 1002 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD, 1003 used, comp, uncomp, tx); 1004 } 1005 1006 void 1007 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx) 1008 { 1009 dsl_pool_t *dp = dmu_tx_pool(tx); 1010 objset_t *mos = dp->dp_meta_objset; 1011 uint64_t obj, ddobj, prevobj = 0; 1012 boolean_t rmorigin; 1013 1014 ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1); 1015 ASSERT(ds->ds_prev == NULL || 1016 dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object); 1017 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 1018 ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg); 1019 rrw_exit(&ds->ds_bp_rwlock, FTAG); 1020 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1021 1022 dsl_dir_cancel_waiters(ds->ds_dir); 1023 1024 rmorigin = (dsl_dir_is_clone(ds->ds_dir) && 1025 DS_IS_DEFER_DESTROY(ds->ds_prev) && 1026 dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 && 1027 ds->ds_prev->ds_userrefs == 0); 1028 1029 /* Remove our reservation. */ 1030 if (ds->ds_reserved != 0) { 1031 dsl_dataset_set_refreservation_sync_impl(ds, 1032 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED), 1033 0, tx); 1034 ASSERT0(ds->ds_reserved); 1035 } 1036 1037 obj = ds->ds_object; 1038 1039 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1040 if (dsl_dataset_feature_is_active(ds, f)) 1041 dsl_dataset_deactivate_feature(ds, f, tx); 1042 } 1043 1044 dsl_scan_ds_destroyed(ds, tx); 1045 1046 if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 1047 /* This is a clone */ 1048 ASSERT(ds->ds_prev != NULL); 1049 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=, 1050 obj); 1051 ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj); 1052 1053 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1054 if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) { 1055 dsl_dataset_remove_from_next_clones(ds->ds_prev, 1056 obj, tx); 1057 } 1058 1059 ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1); 1060 dsl_dataset_phys(ds->ds_prev)->ds_num_children--; 1061 } 1062 1063 /* 1064 * Destroy the deadlist. Unless it's a clone, the 1065 * deadlist should be empty since the dataset has no snapshots. 1066 * (If it's a clone, it's safe to ignore the deadlist contents 1067 * since they are still referenced by the origin snapshot.) 1068 */ 1069 dsl_deadlist_close(&ds->ds_deadlist); 1070 dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx); 1071 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1072 dsl_dataset_phys(ds)->ds_deadlist_obj = 0; 1073 1074 if (dsl_dataset_remap_deadlist_exists(ds)) 1075 dsl_dataset_destroy_remap_deadlist(ds, tx); 1076 1077 /* 1078 * Each destroy is responsible for both destroying (enqueuing 1079 * to be destroyed) the blkptrs comprising the dataset as well as 1080 * those belonging to the zil. 1081 */ 1082 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist)) { 1083 dsl_async_clone_destroy(ds, tx); 1084 } else if (spa_feature_is_enabled(dp->dp_spa, 1085 SPA_FEATURE_ASYNC_DESTROY)) { 1086 dsl_async_dataset_destroy(ds, tx); 1087 } else { 1088 old_synchronous_dataset_destroy(ds, tx); 1089 } 1090 1091 if (ds->ds_prev != NULL) { 1092 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 1093 VERIFY0(zap_remove_int(mos, 1094 dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones, 1095 ds->ds_object, tx)); 1096 } 1097 prevobj = ds->ds_prev->ds_object; 1098 dsl_dataset_rele(ds->ds_prev, ds); 1099 ds->ds_prev = NULL; 1100 } 1101 1102 /* 1103 * This must be done after the dsl_traverse(), because it will 1104 * re-open the objset. 1105 */ 1106 if (ds->ds_objset) { 1107 dmu_objset_evict(ds->ds_objset); 1108 ds->ds_objset = NULL; 1109 } 1110 1111 /* Erase the link in the dir */ 1112 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1113 dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0; 1114 ddobj = ds->ds_dir->dd_object; 1115 ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0); 1116 VERIFY0(zap_destroy(mos, 1117 dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx)); 1118 1119 if (ds->ds_bookmarks_obj != 0) { 1120 void *cookie = NULL; 1121 dsl_bookmark_node_t *dbn; 1122 1123 while ((dbn = avl_destroy_nodes(&ds->ds_bookmarks, &cookie)) != 1124 NULL) { 1125 if (dbn->dbn_phys.zbm_redaction_obj != 0) { 1126 VERIFY0(dmu_object_free(mos, 1127 dbn->dbn_phys.zbm_redaction_obj, tx)); 1128 spa_feature_decr(dmu_objset_spa(mos), 1129 SPA_FEATURE_REDACTION_BOOKMARKS, tx); 1130 } 1131 if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) { 1132 spa_feature_decr(dmu_objset_spa(mos), 1133 SPA_FEATURE_BOOKMARK_WRITTEN, tx); 1134 } 1135 spa_strfree(dbn->dbn_name); 1136 mutex_destroy(&dbn->dbn_lock); 1137 kmem_free(dbn, sizeof (*dbn)); 1138 } 1139 avl_destroy(&ds->ds_bookmarks); 1140 VERIFY0(zap_destroy(mos, ds->ds_bookmarks_obj, tx)); 1141 spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx); 1142 } 1143 1144 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 1145 1146 ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj); 1147 ASSERT0(dsl_dataset_phys(ds)->ds_props_obj); 1148 ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj); 1149 dsl_dir_rele(ds->ds_dir, ds); 1150 ds->ds_dir = NULL; 1151 dmu_object_free_zapified(mos, obj, tx); 1152 1153 dsl_dir_destroy_sync(ddobj, tx); 1154 1155 if (rmorigin) { 1156 dsl_dataset_t *prev; 1157 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev)); 1158 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx); 1159 dsl_dataset_rele(prev, FTAG); 1160 } 1161 } 1162 1163 void 1164 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx) 1165 { 1166 dsl_destroy_head_arg_t *ddha = arg; 1167 dsl_pool_t *dp = dmu_tx_pool(tx); 1168 dsl_dataset_t *ds; 1169 1170 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds)); 1171 dsl_destroy_head_sync_impl(ds, tx); 1172 zvol_remove_minors(dp->dp_spa, ddha->ddha_name, B_TRUE); 1173 dsl_dataset_rele(ds, FTAG); 1174 } 1175 1176 static void 1177 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx) 1178 { 1179 dsl_destroy_head_arg_t *ddha = arg; 1180 dsl_pool_t *dp = dmu_tx_pool(tx); 1181 dsl_dataset_t *ds; 1182 1183 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds)); 1184 1185 /* Mark it as inconsistent on-disk, in case we crash */ 1186 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1187 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT; 1188 1189 spa_history_log_internal_ds(ds, "destroy begin", tx, " "); 1190 dsl_dataset_rele(ds, FTAG); 1191 } 1192 1193 int 1194 dsl_destroy_head(const char *name) 1195 { 1196 dsl_destroy_head_arg_t ddha; 1197 int error; 1198 spa_t *spa; 1199 boolean_t isenabled; 1200 1201 #ifdef _KERNEL 1202 zfs_destroy_unmount_origin(name); 1203 #endif 1204 1205 error = spa_open(name, &spa, FTAG); 1206 if (error != 0) 1207 return (error); 1208 isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY); 1209 spa_close(spa, FTAG); 1210 1211 ddha.ddha_name = name; 1212 1213 if (!isenabled) { 1214 objset_t *os; 1215 1216 error = dsl_sync_task(name, dsl_destroy_head_check, 1217 dsl_destroy_head_begin_sync, &ddha, 1218 0, ZFS_SPACE_CHECK_DESTROY); 1219 if (error != 0) 1220 return (error); 1221 1222 /* 1223 * Head deletion is processed in one txg on old pools; 1224 * remove the objects from open context so that the txg sync 1225 * is not too long. This optimization can only work for 1226 * encrypted datasets if the wrapping key is loaded. 1227 */ 1228 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, B_TRUE, 1229 FTAG, &os); 1230 if (error == 0) { 1231 uint64_t prev_snap_txg = 1232 dsl_dataset_phys(dmu_objset_ds(os))-> 1233 ds_prev_snap_txg; 1234 for (uint64_t obj = 0; error == 0; 1235 error = dmu_object_next(os, &obj, FALSE, 1236 prev_snap_txg)) 1237 (void) dmu_free_long_object(os, obj); 1238 /* sync out all frees */ 1239 txg_wait_synced(dmu_objset_pool(os), 0); 1240 dmu_objset_disown(os, B_TRUE, FTAG); 1241 } 1242 } 1243 1244 return (dsl_sync_task(name, dsl_destroy_head_check, 1245 dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_DESTROY)); 1246 } 1247 1248 /* 1249 * Note, this function is used as the callback for dmu_objset_find(). We 1250 * always return 0 so that we will continue to find and process 1251 * inconsistent datasets, even if we encounter an error trying to 1252 * process one of them. 1253 */ 1254 /* ARGSUSED */ 1255 int 1256 dsl_destroy_inconsistent(const char *dsname, void *arg) 1257 { 1258 objset_t *os; 1259 1260 if (dmu_objset_hold(dsname, FTAG, &os) == 0) { 1261 boolean_t need_destroy = DS_IS_INCONSISTENT(dmu_objset_ds(os)); 1262 1263 /* 1264 * If the dataset is inconsistent because a resumable receive 1265 * has failed, then do not destroy it. 1266 */ 1267 if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os))) 1268 need_destroy = B_FALSE; 1269 1270 dmu_objset_rele(os, FTAG); 1271 if (need_destroy) 1272 (void) dsl_destroy_head(dsname); 1273 } 1274 return (0); 1275 } 1276 1277 1278 #if defined(_KERNEL) 1279 EXPORT_SYMBOL(dsl_destroy_head); 1280 EXPORT_SYMBOL(dsl_destroy_head_sync_impl); 1281 EXPORT_SYMBOL(dsl_dataset_user_hold_check_one); 1282 EXPORT_SYMBOL(dsl_destroy_snapshot_sync_impl); 1283 EXPORT_SYMBOL(dsl_destroy_inconsistent); 1284 EXPORT_SYMBOL(dsl_dataset_user_release_tmp); 1285 EXPORT_SYMBOL(dsl_destroy_head_check_impl); 1286 #endif 1287