13b2aab18SMatthew Ahrens /* 23b2aab18SMatthew Ahrens * CDDL HEADER START 33b2aab18SMatthew Ahrens * 43b2aab18SMatthew Ahrens * The contents of this file are subject to the terms of the 53b2aab18SMatthew Ahrens * Common Development and Distribution License (the "License"). 63b2aab18SMatthew Ahrens * You may not use this file except in compliance with the License. 73b2aab18SMatthew Ahrens * 83b2aab18SMatthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 93b2aab18SMatthew Ahrens * or http://www.opensolaris.org/os/licensing. 103b2aab18SMatthew Ahrens * See the License for the specific language governing permissions 113b2aab18SMatthew Ahrens * and limitations under the License. 123b2aab18SMatthew Ahrens * 133b2aab18SMatthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each 143b2aab18SMatthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 153b2aab18SMatthew Ahrens * If applicable, add the following below this CDDL HEADER, with the 163b2aab18SMatthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying 173b2aab18SMatthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner] 183b2aab18SMatthew Ahrens * 193b2aab18SMatthew Ahrens * CDDL HEADER END 203b2aab18SMatthew Ahrens */ 213b2aab18SMatthew Ahrens /* 223b2aab18SMatthew Ahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23dfc11533SChris Williamson * Copyright (c) 2012, 2016 by Delphix. All rights reserved. 24a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved. 25a2afb611SJerry Jelinek * Copyright (c) 2013 by Joyent, Inc. All rights reserved. 26c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 273b2aab18SMatthew Ahrens */ 283b2aab18SMatthew Ahrens 293b2aab18SMatthew Ahrens #include <sys/zfs_context.h> 303b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 313b2aab18SMatthew Ahrens #include <sys/dsl_dataset.h> 323b2aab18SMatthew Ahrens #include <sys/dsl_synctask.h> 33dfc11533SChris Williamson #include <sys/dsl_destroy.h> 343b2aab18SMatthew Ahrens #include <sys/dmu_tx.h> 353b2aab18SMatthew Ahrens #include <sys/dsl_pool.h> 363b2aab18SMatthew Ahrens #include <sys/dsl_dir.h> 373b2aab18SMatthew Ahrens #include <sys/dmu_traverse.h> 383b2aab18SMatthew Ahrens #include <sys/dsl_scan.h> 393b2aab18SMatthew Ahrens #include <sys/dmu_objset.h> 403b2aab18SMatthew Ahrens #include <sys/zap.h> 413b2aab18SMatthew Ahrens #include <sys/zfeature.h> 423b2aab18SMatthew Ahrens #include <sys/zfs_ioctl.h> 433b2aab18SMatthew Ahrens #include <sys/dsl_deleg.h> 442acef22dSMatthew Ahrens #include <sys/dmu_impl.h> 45dfc11533SChris Williamson #include <sys/zcp.h> 463b2aab18SMatthew Ahrens 4734f2f8cfSMatthew Ahrens int 483b2aab18SMatthew Ahrens dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer) 493b2aab18SMatthew Ahrens { 50bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) 51be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 523b2aab18SMatthew Ahrens 533b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) 54be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 553b2aab18SMatthew Ahrens 563b2aab18SMatthew Ahrens /* 573b2aab18SMatthew Ahrens * Only allow deferred destroy on pools that support it. 583b2aab18SMatthew Ahrens * NOTE: deferred destroy is only supported on snapshots. 593b2aab18SMatthew Ahrens */ 603b2aab18SMatthew Ahrens if (defer) { 613b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 623b2aab18SMatthew Ahrens SPA_VERSION_USERREFS) 63be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 643b2aab18SMatthew Ahrens return (0); 653b2aab18SMatthew Ahrens } 663b2aab18SMatthew Ahrens 673b2aab18SMatthew Ahrens /* 683b2aab18SMatthew Ahrens * If this snapshot has an elevated user reference count, 693b2aab18SMatthew Ahrens * we can't destroy it yet. 703b2aab18SMatthew Ahrens */ 713b2aab18SMatthew Ahrens if (ds->ds_userrefs > 0) 72be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 733b2aab18SMatthew Ahrens 743b2aab18SMatthew Ahrens /* 753b2aab18SMatthew Ahrens * Can't delete a branch point. 763b2aab18SMatthew Ahrens */ 77c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_num_children > 1) 78be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 793b2aab18SMatthew Ahrens 803b2aab18SMatthew Ahrens return (0); 813b2aab18SMatthew Ahrens } 823b2aab18SMatthew Ahrens 83dfc11533SChris Williamson int 843b2aab18SMatthew Ahrens dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx) 853b2aab18SMatthew Ahrens { 86dfc11533SChris Williamson dsl_destroy_snapshot_arg_t *ddsa = arg; 87dfc11533SChris Williamson const char *dsname = ddsa->ddsa_name; 88dfc11533SChris Williamson boolean_t defer = ddsa->ddsa_defer; 89dfc11533SChris Williamson 903b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 913b2aab18SMatthew Ahrens int error = 0; 92dfc11533SChris Williamson dsl_dataset_t *ds; 933b2aab18SMatthew Ahrens 94dfc11533SChris Williamson error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 953b2aab18SMatthew Ahrens 96dfc11533SChris Williamson /* 97dfc11533SChris Williamson * If the snapshot does not exist, silently ignore it, and 98dfc11533SChris Williamson * dsl_destroy_snapshot_sync() will be a no-op 99dfc11533SChris Williamson * (it's "already destroyed"). 100dfc11533SChris Williamson */ 101dfc11533SChris Williamson if (error == ENOENT) 102dfc11533SChris Williamson return (0); 1033b2aab18SMatthew Ahrens 104dfc11533SChris Williamson if (error == 0) { 105dfc11533SChris Williamson error = dsl_destroy_snapshot_check_impl(ds, defer); 106dfc11533SChris Williamson dsl_dataset_rele(ds, FTAG); 1073b2aab18SMatthew Ahrens } 1083b2aab18SMatthew Ahrens 109dfc11533SChris Williamson return (error); 1103b2aab18SMatthew Ahrens } 1113b2aab18SMatthew Ahrens 1123b2aab18SMatthew Ahrens struct process_old_arg { 1133b2aab18SMatthew Ahrens dsl_dataset_t *ds; 1143b2aab18SMatthew Ahrens dsl_dataset_t *ds_prev; 1153b2aab18SMatthew Ahrens boolean_t after_branch_point; 1163b2aab18SMatthew Ahrens zio_t *pio; 1173b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 1183b2aab18SMatthew Ahrens }; 1193b2aab18SMatthew Ahrens 1203b2aab18SMatthew Ahrens static int 1213b2aab18SMatthew Ahrens process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1223b2aab18SMatthew Ahrens { 1233b2aab18SMatthew Ahrens struct process_old_arg *poa = arg; 1243b2aab18SMatthew Ahrens dsl_pool_t *dp = poa->ds->ds_dir->dd_pool; 1253b2aab18SMatthew Ahrens 12643466aaeSMax Grossman ASSERT(!BP_IS_HOLE(bp)); 12743466aaeSMax Grossman 128c1379625SJustin T. Gibbs if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) { 1293b2aab18SMatthew Ahrens dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx); 1303b2aab18SMatthew Ahrens if (poa->ds_prev && !poa->after_branch_point && 1313b2aab18SMatthew Ahrens bp->blk_birth > 132c1379625SJustin T. Gibbs dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) { 133c1379625SJustin T. Gibbs dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes += 1343b2aab18SMatthew Ahrens bp_get_dsize_sync(dp->dp_spa, bp); 1353b2aab18SMatthew Ahrens } 1363b2aab18SMatthew Ahrens } else { 1373b2aab18SMatthew Ahrens poa->used += bp_get_dsize_sync(dp->dp_spa, bp); 1383b2aab18SMatthew Ahrens poa->comp += BP_GET_PSIZE(bp); 1393b2aab18SMatthew Ahrens poa->uncomp += BP_GET_UCSIZE(bp); 1403b2aab18SMatthew Ahrens dsl_free_sync(poa->pio, dp, tx->tx_txg, bp); 1413b2aab18SMatthew Ahrens } 1423b2aab18SMatthew Ahrens return (0); 1433b2aab18SMatthew Ahrens } 1443b2aab18SMatthew Ahrens 1453b2aab18SMatthew Ahrens static void 1463b2aab18SMatthew Ahrens process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev, 1473b2aab18SMatthew Ahrens dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx) 1483b2aab18SMatthew Ahrens { 1493b2aab18SMatthew Ahrens struct process_old_arg poa = { 0 }; 1503b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1513b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 1523b2aab18SMatthew Ahrens uint64_t deadlist_obj; 1533b2aab18SMatthew Ahrens 1543b2aab18SMatthew Ahrens ASSERT(ds->ds_deadlist.dl_oldfmt); 1553b2aab18SMatthew Ahrens ASSERT(ds_next->ds_deadlist.dl_oldfmt); 1563b2aab18SMatthew Ahrens 1573b2aab18SMatthew Ahrens poa.ds = ds; 1583b2aab18SMatthew Ahrens poa.ds_prev = ds_prev; 1593b2aab18SMatthew Ahrens poa.after_branch_point = after_branch_point; 1603b2aab18SMatthew Ahrens poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1613b2aab18SMatthew Ahrens VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj, 1623b2aab18SMatthew Ahrens process_old_cb, &poa, tx)); 1633b2aab18SMatthew Ahrens VERIFY0(zio_wait(poa.pio)); 164c1379625SJustin T. Gibbs ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes); 1653b2aab18SMatthew Ahrens 1663b2aab18SMatthew Ahrens /* change snapused */ 1673b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 1683b2aab18SMatthew Ahrens -poa.used, -poa.comp, -poa.uncomp, tx); 1693b2aab18SMatthew Ahrens 1703b2aab18SMatthew Ahrens /* swap next's deadlist to our deadlist */ 1713b2aab18SMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1723b2aab18SMatthew Ahrens dsl_deadlist_close(&ds_next->ds_deadlist); 173c1379625SJustin T. Gibbs deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 174c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 175c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_deadlist_obj; 176c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj; 177c1379625SJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos, 178c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj); 1793b2aab18SMatthew Ahrens dsl_deadlist_open(&ds_next->ds_deadlist, mos, 180c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_deadlist_obj); 1813b2aab18SMatthew Ahrens } 1823b2aab18SMatthew Ahrens 1833b2aab18SMatthew Ahrens static void 1843b2aab18SMatthew Ahrens dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx) 1853b2aab18SMatthew Ahrens { 1863b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1873b2aab18SMatthew Ahrens zap_cursor_t zc; 1883b2aab18SMatthew Ahrens zap_attribute_t za; 1893b2aab18SMatthew Ahrens 1903b2aab18SMatthew Ahrens /* 1913b2aab18SMatthew Ahrens * If it is the old version, dd_clones doesn't exist so we can't 1923b2aab18SMatthew Ahrens * find the clones, but dsl_deadlist_remove_key() is a no-op so it 1933b2aab18SMatthew Ahrens * doesn't matter. 1943b2aab18SMatthew Ahrens */ 195c1379625SJustin T. Gibbs if (dsl_dir_phys(ds->ds_dir)->dd_clones == 0) 1963b2aab18SMatthew Ahrens return; 1973b2aab18SMatthew Ahrens 198c1379625SJustin T. Gibbs for (zap_cursor_init(&zc, mos, dsl_dir_phys(ds->ds_dir)->dd_clones); 1993b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 2003b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 2013b2aab18SMatthew Ahrens dsl_dataset_t *clone; 2023b2aab18SMatthew Ahrens 2033b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 2043b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 2053b2aab18SMatthew Ahrens if (clone->ds_dir->dd_origin_txg > mintxg) { 2063b2aab18SMatthew Ahrens dsl_deadlist_remove_key(&clone->ds_deadlist, 2073b2aab18SMatthew Ahrens mintxg, tx); 2083b2aab18SMatthew Ahrens dsl_dataset_remove_clones_key(clone, mintxg, tx); 2093b2aab18SMatthew Ahrens } 2103b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 2113b2aab18SMatthew Ahrens } 2123b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 2133b2aab18SMatthew Ahrens } 2143b2aab18SMatthew Ahrens 2153b2aab18SMatthew Ahrens void 2163b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx) 2173b2aab18SMatthew Ahrens { 2183b2aab18SMatthew Ahrens int err; 2193b2aab18SMatthew Ahrens int after_branch_point = FALSE; 2203b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 2213b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 2223b2aab18SMatthew Ahrens dsl_dataset_t *ds_prev = NULL; 2233b2aab18SMatthew Ahrens uint64_t obj; 2243b2aab18SMatthew Ahrens 2253b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 226c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 227c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg); 228c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 2293b2aab18SMatthew Ahrens ASSERT(refcount_is_zero(&ds->ds_longholds)); 2303b2aab18SMatthew Ahrens 2313b2aab18SMatthew Ahrens if (defer && 232c1379625SJustin T. Gibbs (ds->ds_userrefs > 0 || 233c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children > 1)) { 2343b2aab18SMatthew Ahrens ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 2353b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 236c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY; 2373b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "defer_destroy", tx, ""); 2383b2aab18SMatthew Ahrens return; 2393b2aab18SMatthew Ahrens } 2403b2aab18SMatthew Ahrens 241c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1); 2423b2aab18SMatthew Ahrens 2433b2aab18SMatthew Ahrens /* We need to log before removing it from the namespace. */ 2443b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "destroy", tx, ""); 2453b2aab18SMatthew Ahrens 2463b2aab18SMatthew Ahrens dsl_scan_ds_destroyed(ds, tx); 2473b2aab18SMatthew Ahrens 2483b2aab18SMatthew Ahrens obj = ds->ds_object; 2493b2aab18SMatthew Ahrens 250ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 251ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) { 252ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(obj, f, tx); 253ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_FALSE; 254ca0cc391SMatthew Ahrens } 255b5152584SMatthew Ahrens } 256c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 2573b2aab18SMatthew Ahrens ASSERT3P(ds->ds_prev, ==, NULL); 2583b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 259c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev)); 2603b2aab18SMatthew Ahrens after_branch_point = 261c1379625SJustin T. Gibbs (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj); 2623b2aab18SMatthew Ahrens 2633b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 2643b2aab18SMatthew Ahrens if (after_branch_point && 265c1379625SJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) { 2663b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds_prev, obj, tx); 267c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) { 2683b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 269c1379625SJustin T. Gibbs dsl_dataset_phys(ds_prev)-> 270c1379625SJustin T. Gibbs ds_next_clones_obj, 271c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_snap_obj, 272c1379625SJustin T. Gibbs tx)); 2733b2aab18SMatthew Ahrens } 2743b2aab18SMatthew Ahrens } 2753b2aab18SMatthew Ahrens if (!after_branch_point) { 276c1379625SJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_next_snap_obj = 277c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_snap_obj; 2783b2aab18SMatthew Ahrens } 2793b2aab18SMatthew Ahrens } 2803b2aab18SMatthew Ahrens 2813b2aab18SMatthew Ahrens dsl_dataset_t *ds_next; 2823b2aab18SMatthew Ahrens uint64_t old_unique; 2833b2aab18SMatthew Ahrens uint64_t used = 0, comp = 0, uncomp = 0; 2843b2aab18SMatthew Ahrens 2853b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 286c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next)); 287c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj); 2883b2aab18SMatthew Ahrens 289c1379625SJustin T. Gibbs old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes; 2903b2aab18SMatthew Ahrens 2913b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 292c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_prev_snap_obj = 293c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj; 294c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_prev_snap_txg = 295c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg; 296c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 297c1379625SJustin T. Gibbs ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0); 2983b2aab18SMatthew Ahrens 2993b2aab18SMatthew Ahrens if (ds_next->ds_deadlist.dl_oldfmt) { 3003b2aab18SMatthew Ahrens process_old_deadlist(ds, ds_prev, ds_next, 3013b2aab18SMatthew Ahrens after_branch_point, tx); 3023b2aab18SMatthew Ahrens } else { 3033b2aab18SMatthew Ahrens /* Adjust prev's unique space. */ 3043b2aab18SMatthew Ahrens if (ds_prev && !after_branch_point) { 3053b2aab18SMatthew Ahrens dsl_deadlist_space_range(&ds_next->ds_deadlist, 306c1379625SJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_prev_snap_txg, 307c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, 3083b2aab18SMatthew Ahrens &used, &comp, &uncomp); 309c1379625SJustin T. Gibbs dsl_dataset_phys(ds_prev)->ds_unique_bytes += used; 3103b2aab18SMatthew Ahrens } 3113b2aab18SMatthew Ahrens 3123b2aab18SMatthew Ahrens /* Adjust snapused. */ 3133b2aab18SMatthew Ahrens dsl_deadlist_space_range(&ds_next->ds_deadlist, 314c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX, 3153b2aab18SMatthew Ahrens &used, &comp, &uncomp); 3163b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 3173b2aab18SMatthew Ahrens -used, -comp, -uncomp, tx); 3183b2aab18SMatthew Ahrens 3193b2aab18SMatthew Ahrens /* Move blocks to be freed to pool's free list. */ 3203b2aab18SMatthew Ahrens dsl_deadlist_move_bpobj(&ds_next->ds_deadlist, 321c1379625SJustin T. Gibbs &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg, 3223b2aab18SMatthew Ahrens tx); 3233b2aab18SMatthew Ahrens dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, 3243b2aab18SMatthew Ahrens DD_USED_HEAD, used, comp, uncomp, tx); 3253b2aab18SMatthew Ahrens 3263b2aab18SMatthew Ahrens /* Merge our deadlist into next's and free it. */ 3273b2aab18SMatthew Ahrens dsl_deadlist_merge(&ds_next->ds_deadlist, 328c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj, tx); 3293b2aab18SMatthew Ahrens } 3303b2aab18SMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 331c1379625SJustin T. Gibbs dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx); 3323b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 333c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 0; 3343b2aab18SMatthew Ahrens 3353b2aab18SMatthew Ahrens /* Collapse range in clone heads */ 3363b2aab18SMatthew Ahrens dsl_dataset_remove_clones_key(ds, 337c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg, tx); 3383b2aab18SMatthew Ahrens 339bc9014e6SJustin Gibbs if (ds_next->ds_is_snapshot) { 3403b2aab18SMatthew Ahrens dsl_dataset_t *ds_nextnext; 3413b2aab18SMatthew Ahrens 3423b2aab18SMatthew Ahrens /* 3433b2aab18SMatthew Ahrens * Update next's unique to include blocks which 3443b2aab18SMatthew Ahrens * were previously shared by only this snapshot 3453b2aab18SMatthew Ahrens * and it. Those blocks will be born after the 3463b2aab18SMatthew Ahrens * prev snap and before this snap, and will have 3473b2aab18SMatthew Ahrens * died after the next snap and before the one 3483b2aab18SMatthew Ahrens * after that (ie. be on the snap after next's 3493b2aab18SMatthew Ahrens * deadlist). 3503b2aab18SMatthew Ahrens */ 3513b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 352c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_next_snap_obj, 353c1379625SJustin T. Gibbs FTAG, &ds_nextnext)); 3543b2aab18SMatthew Ahrens dsl_deadlist_space_range(&ds_nextnext->ds_deadlist, 355c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, 356c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg, 3573b2aab18SMatthew Ahrens &used, &comp, &uncomp); 358c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_unique_bytes += used; 3593b2aab18SMatthew Ahrens dsl_dataset_rele(ds_nextnext, FTAG); 3603b2aab18SMatthew Ahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 3613b2aab18SMatthew Ahrens 3623b2aab18SMatthew Ahrens /* Collapse range in this head. */ 3633b2aab18SMatthew Ahrens dsl_dataset_t *hds; 3643b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 365c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds)); 3663b2aab18SMatthew Ahrens dsl_deadlist_remove_key(&hds->ds_deadlist, 367c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg, tx); 3683b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 3693b2aab18SMatthew Ahrens 3703b2aab18SMatthew Ahrens } else { 3713b2aab18SMatthew Ahrens ASSERT3P(ds_next->ds_prev, ==, ds); 3723b2aab18SMatthew Ahrens dsl_dataset_rele(ds_next->ds_prev, ds_next); 3733b2aab18SMatthew Ahrens ds_next->ds_prev = NULL; 3743b2aab18SMatthew Ahrens if (ds_prev) { 3753b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 376c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, 3773b2aab18SMatthew Ahrens ds_next, &ds_next->ds_prev)); 3783b2aab18SMatthew Ahrens } 3793b2aab18SMatthew Ahrens 3803b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds_next); 3813b2aab18SMatthew Ahrens 3823b2aab18SMatthew Ahrens /* 3833b2aab18SMatthew Ahrens * Reduce the amount of our unconsumed refreservation 3843b2aab18SMatthew Ahrens * being charged to our parent by the amount of 3853b2aab18SMatthew Ahrens * new unique data we have gained. 3863b2aab18SMatthew Ahrens */ 3873b2aab18SMatthew Ahrens if (old_unique < ds_next->ds_reserved) { 3883b2aab18SMatthew Ahrens int64_t mrsdelta; 3893b2aab18SMatthew Ahrens uint64_t new_unique = 390c1379625SJustin T. Gibbs dsl_dataset_phys(ds_next)->ds_unique_bytes; 3913b2aab18SMatthew Ahrens 3923b2aab18SMatthew Ahrens ASSERT(old_unique <= new_unique); 3933b2aab18SMatthew Ahrens mrsdelta = MIN(new_unique - old_unique, 3943b2aab18SMatthew Ahrens ds_next->ds_reserved - old_unique); 3953b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, 3963b2aab18SMatthew Ahrens DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 3973b2aab18SMatthew Ahrens } 3983b2aab18SMatthew Ahrens } 3993b2aab18SMatthew Ahrens dsl_dataset_rele(ds_next, FTAG); 4003b2aab18SMatthew Ahrens 4013b2aab18SMatthew Ahrens /* 4023b2aab18SMatthew Ahrens * This must be done after the dsl_traverse(), because it will 4033b2aab18SMatthew Ahrens * re-open the objset. 4043b2aab18SMatthew Ahrens */ 4053b2aab18SMatthew Ahrens if (ds->ds_objset) { 4063b2aab18SMatthew Ahrens dmu_objset_evict(ds->ds_objset); 4073b2aab18SMatthew Ahrens ds->ds_objset = NULL; 4083b2aab18SMatthew Ahrens } 4093b2aab18SMatthew Ahrens 4103b2aab18SMatthew Ahrens /* remove from snapshot namespace */ 4113b2aab18SMatthew Ahrens dsl_dataset_t *ds_head; 412c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0); 4133b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 414c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head)); 4153b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 4163b2aab18SMatthew Ahrens #ifdef ZFS_DEBUG 4173b2aab18SMatthew Ahrens { 4183b2aab18SMatthew Ahrens uint64_t val; 4193b2aab18SMatthew Ahrens 4203b2aab18SMatthew Ahrens err = dsl_dataset_snap_lookup(ds_head, 4213b2aab18SMatthew Ahrens ds->ds_snapname, &val); 4223b2aab18SMatthew Ahrens ASSERT0(err); 4233b2aab18SMatthew Ahrens ASSERT3U(val, ==, obj); 4243b2aab18SMatthew Ahrens } 4253b2aab18SMatthew Ahrens #endif 426a2afb611SJerry Jelinek VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE)); 4273b2aab18SMatthew Ahrens dsl_dataset_rele(ds_head, FTAG); 4283b2aab18SMatthew Ahrens 4293b2aab18SMatthew Ahrens if (ds_prev != NULL) 4303b2aab18SMatthew Ahrens dsl_dataset_rele(ds_prev, FTAG); 4313b2aab18SMatthew Ahrens 4323b2aab18SMatthew Ahrens spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 4333b2aab18SMatthew Ahrens 434c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 4353b2aab18SMatthew Ahrens uint64_t count; 4363b2aab18SMatthew Ahrens ASSERT0(zap_count(mos, 437c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj, &count) && 438c1379625SJustin T. Gibbs count == 0); 4393b2aab18SMatthew Ahrens VERIFY0(dmu_object_free(mos, 440c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj, tx)); 4413b2aab18SMatthew Ahrens } 442c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_props_obj != 0) 443c1379625SJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj, 444c1379625SJustin T. Gibbs tx)); 445c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0) 446c1379625SJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj, 447c1379625SJustin T. Gibbs tx)); 4483b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 4493b2aab18SMatthew Ahrens ds->ds_dir = NULL; 4502acef22dSMatthew Ahrens dmu_object_free_zapified(mos, obj, tx); 4513b2aab18SMatthew Ahrens } 4523b2aab18SMatthew Ahrens 453dfc11533SChris Williamson void 4543b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx) 4553b2aab18SMatthew Ahrens { 456dfc11533SChris Williamson dsl_destroy_snapshot_arg_t *ddsa = arg; 457dfc11533SChris Williamson const char *dsname = ddsa->ddsa_name; 458dfc11533SChris Williamson boolean_t defer = ddsa->ddsa_defer; 4593b2aab18SMatthew Ahrens 460dfc11533SChris Williamson dsl_pool_t *dp = dmu_tx_pool(tx); 461dfc11533SChris Williamson dsl_dataset_t *ds; 4623b2aab18SMatthew Ahrens 463dfc11533SChris Williamson int error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 464dfc11533SChris Williamson if (error == ENOENT) 465dfc11533SChris Williamson return; 466dfc11533SChris Williamson ASSERT0(error); 467dfc11533SChris Williamson dsl_destroy_snapshot_sync_impl(ds, defer, tx); 468dfc11533SChris Williamson dsl_dataset_rele(ds, FTAG); 4693b2aab18SMatthew Ahrens } 4703b2aab18SMatthew Ahrens 4713b2aab18SMatthew Ahrens /* 4723b2aab18SMatthew Ahrens * The semantics of this function are described in the comment above 4733b2aab18SMatthew Ahrens * lzc_destroy_snaps(). To summarize: 4743b2aab18SMatthew Ahrens * 4753b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 4763b2aab18SMatthew Ahrens * 4773b2aab18SMatthew Ahrens * Snapshots that don't exist will be silently ignored (considered to be 4783b2aab18SMatthew Ahrens * "already deleted"). 4793b2aab18SMatthew Ahrens * 4803b2aab18SMatthew Ahrens * On success, all snaps will be destroyed and this will return 0. 4813b2aab18SMatthew Ahrens * On failure, no snaps will be destroyed, the errlist will be filled in, 4823b2aab18SMatthew Ahrens * and this will return an errno. 4833b2aab18SMatthew Ahrens */ 4843b2aab18SMatthew Ahrens int 4853b2aab18SMatthew Ahrens dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer, 4863b2aab18SMatthew Ahrens nvlist_t *errlist) 4873b2aab18SMatthew Ahrens { 488dfc11533SChris Williamson if (nvlist_next_nvpair(snaps, NULL) == NULL) 4893b2aab18SMatthew Ahrens return (0); 4903b2aab18SMatthew Ahrens 491dfc11533SChris Williamson /* 492dfc11533SChris Williamson * lzc_destroy_snaps() is documented to take an nvlist whose 493*ed992b0aSSerapheim Dimitropoulos * values "don't matter". We need to convert that nvlist to 494*ed992b0aSSerapheim Dimitropoulos * one that we know can be converted to LUA. We also don't 495*ed992b0aSSerapheim Dimitropoulos * care about any duplicate entries because the nvlist will 496*ed992b0aSSerapheim Dimitropoulos * be converted to a LUA table which should take care of this. 497dfc11533SChris Williamson */ 498*ed992b0aSSerapheim Dimitropoulos nvlist_t *snaps_normalized; 499*ed992b0aSSerapheim Dimitropoulos VERIFY0(nvlist_alloc(&snaps_normalized, 0, KM_SLEEP)); 500dfc11533SChris Williamson for (nvpair_t *pair = nvlist_next_nvpair(snaps, NULL); 501dfc11533SChris Williamson pair != NULL; pair = nvlist_next_nvpair(snaps, pair)) { 502dfc11533SChris Williamson fnvlist_add_boolean_value(snaps_normalized, 503dfc11533SChris Williamson nvpair_name(pair), B_TRUE); 504dfc11533SChris Williamson } 505*ed992b0aSSerapheim Dimitropoulos 506*ed992b0aSSerapheim Dimitropoulos nvlist_t *arg; 507*ed992b0aSSerapheim Dimitropoulos VERIFY0(nvlist_alloc(&arg, 0, KM_SLEEP)); 508dfc11533SChris Williamson fnvlist_add_nvlist(arg, "snaps", snaps_normalized); 509e6ab4525SMatthew Ahrens fnvlist_free(snaps_normalized); 510dfc11533SChris Williamson fnvlist_add_boolean_value(arg, "defer", defer); 511dfc11533SChris Williamson 512*ed992b0aSSerapheim Dimitropoulos nvlist_t *wrapper; 513*ed992b0aSSerapheim Dimitropoulos VERIFY0(nvlist_alloc(&wrapper, 0, KM_SLEEP)); 514dfc11533SChris Williamson fnvlist_add_nvlist(wrapper, ZCP_ARG_ARGLIST, arg); 515dfc11533SChris Williamson fnvlist_free(arg); 516dfc11533SChris Williamson 517dfc11533SChris Williamson const char *program = 518dfc11533SChris Williamson "arg = ...\n" 519dfc11533SChris Williamson "snaps = arg['snaps']\n" 520dfc11533SChris Williamson "defer = arg['defer']\n" 521dfc11533SChris Williamson "errors = { }\n" 522dfc11533SChris Williamson "has_errors = false\n" 523dfc11533SChris Williamson "for snap, v in pairs(snaps) do\n" 524dfc11533SChris Williamson " errno = zfs.check.destroy{snap, defer=defer}\n" 525dfc11533SChris Williamson " zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n" 526dfc11533SChris Williamson " if errno == ENOENT then\n" 527dfc11533SChris Williamson " snaps[snap] = nil\n" 528dfc11533SChris Williamson " elseif errno ~= 0 then\n" 529dfc11533SChris Williamson " errors[snap] = errno\n" 530dfc11533SChris Williamson " has_errors = true\n" 531dfc11533SChris Williamson " end\n" 532dfc11533SChris Williamson "end\n" 533dfc11533SChris Williamson "if has_errors then\n" 534dfc11533SChris Williamson " return errors\n" 535dfc11533SChris Williamson "end\n" 536dfc11533SChris Williamson "for snap, v in pairs(snaps) do\n" 537dfc11533SChris Williamson " errno = zfs.sync.destroy{snap, defer=defer}\n" 538dfc11533SChris Williamson " assert(errno == 0)\n" 539dfc11533SChris Williamson "end\n" 540dfc11533SChris Williamson "return { }\n"; 541dfc11533SChris Williamson 542dfc11533SChris Williamson nvlist_t *result = fnvlist_alloc(); 543dfc11533SChris Williamson int error = zcp_eval(nvpair_name(nvlist_next_nvpair(snaps, NULL)), 544dfc11533SChris Williamson program, 545dfc11533SChris Williamson 0, 546dfc11533SChris Williamson zfs_lua_max_memlimit, 547*ed992b0aSSerapheim Dimitropoulos nvlist_next_nvpair(wrapper, NULL), result); 548dfc11533SChris Williamson if (error != 0) { 549dfc11533SChris Williamson char *errorstr = NULL; 550dfc11533SChris Williamson (void) nvlist_lookup_string(result, ZCP_RET_ERROR, &errorstr); 551dfc11533SChris Williamson if (errorstr != NULL) { 552dfc11533SChris Williamson zfs_dbgmsg(errorstr); 553dfc11533SChris Williamson } 554dfc11533SChris Williamson return (error); 555dfc11533SChris Williamson } 556dfc11533SChris Williamson fnvlist_free(wrapper); 5573b2aab18SMatthew Ahrens 558dfc11533SChris Williamson /* 559dfc11533SChris Williamson * lzc_destroy_snaps() is documented to fill the errlist with 560dfc11533SChris Williamson * int32 values, so we need to covert the int64 values that are 561dfc11533SChris Williamson * returned from LUA. 562dfc11533SChris Williamson */ 563dfc11533SChris Williamson int rv = 0; 564dfc11533SChris Williamson nvlist_t *errlist_raw = fnvlist_lookup_nvlist(result, ZCP_RET_RETURN); 565dfc11533SChris Williamson for (nvpair_t *pair = nvlist_next_nvpair(errlist_raw, NULL); 566dfc11533SChris Williamson pair != NULL; pair = nvlist_next_nvpair(errlist_raw, pair)) { 567dfc11533SChris Williamson int32_t val = (int32_t)fnvpair_value_int64(pair); 568dfc11533SChris Williamson if (rv == 0) 569dfc11533SChris Williamson rv = val; 570dfc11533SChris Williamson fnvlist_add_int32(errlist, nvpair_name(pair), val); 571dfc11533SChris Williamson } 572dfc11533SChris Williamson fnvlist_free(result); 573dfc11533SChris Williamson return (rv); 5743b2aab18SMatthew Ahrens } 5753b2aab18SMatthew Ahrens 5763b2aab18SMatthew Ahrens int 5773b2aab18SMatthew Ahrens dsl_destroy_snapshot(const char *name, boolean_t defer) 5783b2aab18SMatthew Ahrens { 5793b2aab18SMatthew Ahrens int error; 5803b2aab18SMatthew Ahrens nvlist_t *nvl = fnvlist_alloc(); 5813b2aab18SMatthew Ahrens nvlist_t *errlist = fnvlist_alloc(); 5823b2aab18SMatthew Ahrens 5833b2aab18SMatthew Ahrens fnvlist_add_boolean(nvl, name); 5843b2aab18SMatthew Ahrens error = dsl_destroy_snapshots_nvl(nvl, defer, errlist); 5853b2aab18SMatthew Ahrens fnvlist_free(errlist); 5863b2aab18SMatthew Ahrens fnvlist_free(nvl); 5873b2aab18SMatthew Ahrens return (error); 5883b2aab18SMatthew Ahrens } 5893b2aab18SMatthew Ahrens 5903b2aab18SMatthew Ahrens struct killarg { 5913b2aab18SMatthew Ahrens dsl_dataset_t *ds; 5923b2aab18SMatthew Ahrens dmu_tx_t *tx; 5933b2aab18SMatthew Ahrens }; 5943b2aab18SMatthew Ahrens 5953b2aab18SMatthew Ahrens /* ARGSUSED */ 5963b2aab18SMatthew Ahrens static int 5973b2aab18SMatthew Ahrens kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 5987802d7bfSMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 5993b2aab18SMatthew Ahrens { 6003b2aab18SMatthew Ahrens struct killarg *ka = arg; 6013b2aab18SMatthew Ahrens dmu_tx_t *tx = ka->tx; 6023b2aab18SMatthew Ahrens 603a2cdcdd2SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 6043b2aab18SMatthew Ahrens return (0); 6053b2aab18SMatthew Ahrens 6063b2aab18SMatthew Ahrens if (zb->zb_level == ZB_ZIL_LEVEL) { 6073b2aab18SMatthew Ahrens ASSERT(zilog != NULL); 6083b2aab18SMatthew Ahrens /* 6093b2aab18SMatthew Ahrens * It's a block in the intent log. It has no 6103b2aab18SMatthew Ahrens * accounting, so just free it. 6113b2aab18SMatthew Ahrens */ 6123b2aab18SMatthew Ahrens dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp); 6133b2aab18SMatthew Ahrens } else { 6143b2aab18SMatthew Ahrens ASSERT(zilog == NULL); 615c1379625SJustin T. Gibbs ASSERT3U(bp->blk_birth, >, 616c1379625SJustin T. Gibbs dsl_dataset_phys(ka->ds)->ds_prev_snap_txg); 6173b2aab18SMatthew Ahrens (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE); 6183b2aab18SMatthew Ahrens } 6193b2aab18SMatthew Ahrens 6203b2aab18SMatthew Ahrens return (0); 6213b2aab18SMatthew Ahrens } 6223b2aab18SMatthew Ahrens 6233b2aab18SMatthew Ahrens static void 6243b2aab18SMatthew Ahrens old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx) 6253b2aab18SMatthew Ahrens { 6263b2aab18SMatthew Ahrens struct killarg ka; 6273b2aab18SMatthew Ahrens 6283b2aab18SMatthew Ahrens /* 6293b2aab18SMatthew Ahrens * Free everything that we point to (that's born after 6303b2aab18SMatthew Ahrens * the previous snapshot, if we are a clone) 6313b2aab18SMatthew Ahrens * 6323b2aab18SMatthew Ahrens * NB: this should be very quick, because we already 6333b2aab18SMatthew Ahrens * freed all the objects in open context. 6343b2aab18SMatthew Ahrens */ 6353b2aab18SMatthew Ahrens ka.ds = ds; 6363b2aab18SMatthew Ahrens ka.tx = tx; 6373b2aab18SMatthew Ahrens VERIFY0(traverse_dataset(ds, 638c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST, 6393b2aab18SMatthew Ahrens kill_blkptr, &ka)); 640c1379625SJustin T. Gibbs ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 641c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes == 0); 6423b2aab18SMatthew Ahrens } 6433b2aab18SMatthew Ahrens 6443b2aab18SMatthew Ahrens int 6453b2aab18SMatthew Ahrens dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds) 6463b2aab18SMatthew Ahrens { 6473b2aab18SMatthew Ahrens int error; 6483b2aab18SMatthew Ahrens uint64_t count; 6493b2aab18SMatthew Ahrens objset_t *mos; 6503b2aab18SMatthew Ahrens 651bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 652bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) 653be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 6543b2aab18SMatthew Ahrens 6553b2aab18SMatthew Ahrens if (refcount_count(&ds->ds_longholds) != expected_holds) 656be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 6573b2aab18SMatthew Ahrens 6583b2aab18SMatthew Ahrens mos = ds->ds_dir->dd_pool->dp_meta_objset; 6593b2aab18SMatthew Ahrens 6603b2aab18SMatthew Ahrens /* 6613b2aab18SMatthew Ahrens * Can't delete a head dataset if there are snapshots of it. 6623b2aab18SMatthew Ahrens * (Except if the only snapshots are from the branch we cloned 6633b2aab18SMatthew Ahrens * from.) 6643b2aab18SMatthew Ahrens */ 6653b2aab18SMatthew Ahrens if (ds->ds_prev != NULL && 666c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object) 667be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 6683b2aab18SMatthew Ahrens 6693b2aab18SMatthew Ahrens /* 6703b2aab18SMatthew Ahrens * Can't delete if there are children of this fs. 6713b2aab18SMatthew Ahrens */ 6723b2aab18SMatthew Ahrens error = zap_count(mos, 673c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count); 6743b2aab18SMatthew Ahrens if (error != 0) 6753b2aab18SMatthew Ahrens return (error); 6763b2aab18SMatthew Ahrens if (count != 0) 677be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 6783b2aab18SMatthew Ahrens 6793b2aab18SMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) && 680c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 && 6813b2aab18SMatthew Ahrens ds->ds_prev->ds_userrefs == 0) { 6823b2aab18SMatthew Ahrens /* We need to remove the origin snapshot as well. */ 6833b2aab18SMatthew Ahrens if (!refcount_is_zero(&ds->ds_prev->ds_longholds)) 684be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 6853b2aab18SMatthew Ahrens } 6863b2aab18SMatthew Ahrens return (0); 6873b2aab18SMatthew Ahrens } 6883b2aab18SMatthew Ahrens 689dfc11533SChris Williamson int 6903b2aab18SMatthew Ahrens dsl_destroy_head_check(void *arg, dmu_tx_t *tx) 6913b2aab18SMatthew Ahrens { 6923b2aab18SMatthew Ahrens dsl_destroy_head_arg_t *ddha = arg; 6933b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 6943b2aab18SMatthew Ahrens dsl_dataset_t *ds; 6953b2aab18SMatthew Ahrens int error; 6963b2aab18SMatthew Ahrens 6973b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds); 6983b2aab18SMatthew Ahrens if (error != 0) 6993b2aab18SMatthew Ahrens return (error); 7003b2aab18SMatthew Ahrens 7013b2aab18SMatthew Ahrens error = dsl_destroy_head_check_impl(ds, 0); 7023b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 7033b2aab18SMatthew Ahrens return (error); 7043b2aab18SMatthew Ahrens } 7053b2aab18SMatthew Ahrens 7063b2aab18SMatthew Ahrens static void 7073b2aab18SMatthew Ahrens dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx) 7083b2aab18SMatthew Ahrens { 7093b2aab18SMatthew Ahrens dsl_dir_t *dd; 7103b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 7113b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 7123b2aab18SMatthew Ahrens dd_used_t t; 7133b2aab18SMatthew Ahrens 7143b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock)); 7153b2aab18SMatthew Ahrens 7163b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd)); 7173b2aab18SMatthew Ahrens 718c1379625SJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj); 7193b2aab18SMatthew Ahrens 720a2afb611SJerry Jelinek /* 721a2afb611SJerry Jelinek * Decrement the filesystem count for all parent filesystems. 722a2afb611SJerry Jelinek * 723a2afb611SJerry Jelinek * When we receive an incremental stream into a filesystem that already 724a2afb611SJerry Jelinek * exists, a temporary clone is created. We never count this temporary 725a2afb611SJerry Jelinek * clone, whose name begins with a '%'. 726a2afb611SJerry Jelinek */ 727a2afb611SJerry Jelinek if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL) 728a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -1, 729a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx); 730a2afb611SJerry Jelinek 7313b2aab18SMatthew Ahrens /* 7323b2aab18SMatthew Ahrens * Remove our reservation. The impl() routine avoids setting the 7333b2aab18SMatthew Ahrens * actual property, which would require the (already destroyed) ds. 7343b2aab18SMatthew Ahrens */ 7353b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync_impl(dd, 0, tx); 7363b2aab18SMatthew Ahrens 737c1379625SJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_used_bytes); 738c1379625SJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_reserved); 7393b2aab18SMatthew Ahrens for (t = 0; t < DD_USED_NUM; t++) 740c1379625SJustin T. Gibbs ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]); 7413b2aab18SMatthew Ahrens 742c1379625SJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx)); 743c1379625SJustin T. Gibbs VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx)); 744c1379625SJustin T. Gibbs VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx)); 7453b2aab18SMatthew Ahrens VERIFY0(zap_remove(mos, 746c1379625SJustin T. Gibbs dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj, 747c1379625SJustin T. Gibbs dd->dd_myname, tx)); 7483b2aab18SMatthew Ahrens 7493b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 7502acef22dSMatthew Ahrens dmu_object_free_zapified(mos, ddobj, tx); 7513b2aab18SMatthew Ahrens } 7523b2aab18SMatthew Ahrens 7533b2aab18SMatthew Ahrens void 7543b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx) 7553b2aab18SMatthew Ahrens { 7563b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 7573b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 7583b2aab18SMatthew Ahrens uint64_t obj, ddobj, prevobj = 0; 7593b2aab18SMatthew Ahrens boolean_t rmorigin; 7603b2aab18SMatthew Ahrens 761c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1); 7623b2aab18SMatthew Ahrens ASSERT(ds->ds_prev == NULL || 763c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object); 764c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 765c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg); 766c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 7673b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 7683b2aab18SMatthew Ahrens 7693b2aab18SMatthew Ahrens /* We need to log before removing it from the namespace. */ 7703b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "destroy", tx, ""); 7713b2aab18SMatthew Ahrens 7723b2aab18SMatthew Ahrens rmorigin = (dsl_dir_is_clone(ds->ds_dir) && 7733b2aab18SMatthew Ahrens DS_IS_DEFER_DESTROY(ds->ds_prev) && 774c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 && 7753b2aab18SMatthew Ahrens ds->ds_prev->ds_userrefs == 0); 7763b2aab18SMatthew Ahrens 7775d7b4d43SMatthew Ahrens /* Remove our reservation. */ 7783b2aab18SMatthew Ahrens if (ds->ds_reserved != 0) { 7793b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 7803b2aab18SMatthew Ahrens (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED), 7813b2aab18SMatthew Ahrens 0, tx); 7823b2aab18SMatthew Ahrens ASSERT0(ds->ds_reserved); 7833b2aab18SMatthew Ahrens } 7843b2aab18SMatthew Ahrens 785ca0cc391SMatthew Ahrens obj = ds->ds_object; 786b5152584SMatthew Ahrens 787ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 788ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) { 789ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(obj, f, tx); 790ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_FALSE; 791ca0cc391SMatthew Ahrens } 792ca0cc391SMatthew Ahrens } 7933b2aab18SMatthew Ahrens 794ca0cc391SMatthew Ahrens dsl_scan_ds_destroyed(ds, tx); 7953b2aab18SMatthew Ahrens 796c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 7973b2aab18SMatthew Ahrens /* This is a clone */ 7983b2aab18SMatthew Ahrens ASSERT(ds->ds_prev != NULL); 799c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=, 800c1379625SJustin T. Gibbs obj); 801c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj); 8023b2aab18SMatthew Ahrens 8033b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 804c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) { 8053b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 8063b2aab18SMatthew Ahrens obj, tx); 8073b2aab18SMatthew Ahrens } 8083b2aab18SMatthew Ahrens 809c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1); 810c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children--; 8113b2aab18SMatthew Ahrens } 8123b2aab18SMatthew Ahrens 8133b2aab18SMatthew Ahrens /* 8143b2aab18SMatthew Ahrens * Destroy the deadlist. Unless it's a clone, the 8153b2aab18SMatthew Ahrens * deadlist should be empty. (If it's a clone, it's 8163b2aab18SMatthew Ahrens * safe to ignore the deadlist contents.) 8173b2aab18SMatthew Ahrens */ 8183b2aab18SMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 819c1379625SJustin T. Gibbs dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx); 8203b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 821c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 0; 8223b2aab18SMatthew Ahrens 8232acef22dSMatthew Ahrens objset_t *os; 8243b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 8253b2aab18SMatthew Ahrens 8262acef22dSMatthew Ahrens if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) { 8273b2aab18SMatthew Ahrens old_synchronous_dataset_destroy(ds, tx); 8283b2aab18SMatthew Ahrens } else { 8293b2aab18SMatthew Ahrens /* 8303b2aab18SMatthew Ahrens * Move the bptree into the pool's list of trees to 8313b2aab18SMatthew Ahrens * clean up and update space accounting information. 8323b2aab18SMatthew Ahrens */ 8333b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 8343b2aab18SMatthew Ahrens 8353b2aab18SMatthew Ahrens zil_destroy_sync(dmu_objset_zil(os), tx); 8363b2aab18SMatthew Ahrens 8372acef22dSMatthew Ahrens if (!spa_feature_is_active(dp->dp_spa, 8382acef22dSMatthew Ahrens SPA_FEATURE_ASYNC_DESTROY)) { 8394a923759SGeorge Wilson dsl_scan_t *scn = dp->dp_scan; 8402acef22dSMatthew Ahrens spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY, 8412acef22dSMatthew Ahrens tx); 8423b2aab18SMatthew Ahrens dp->dp_bptree_obj = bptree_alloc(mos, tx); 8433b2aab18SMatthew Ahrens VERIFY0(zap_add(mos, 8443b2aab18SMatthew Ahrens DMU_POOL_DIRECTORY_OBJECT, 8453b2aab18SMatthew Ahrens DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1, 8463b2aab18SMatthew Ahrens &dp->dp_bptree_obj, tx)); 8474a923759SGeorge Wilson ASSERT(!scn->scn_async_destroying); 8484a923759SGeorge Wilson scn->scn_async_destroying = B_TRUE; 8493b2aab18SMatthew Ahrens } 8503b2aab18SMatthew Ahrens 851c1379625SJustin T. Gibbs used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes; 852c1379625SJustin T. Gibbs comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes; 853c1379625SJustin T. Gibbs uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes; 8543b2aab18SMatthew Ahrens 8553b2aab18SMatthew Ahrens ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 856c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes == used); 8573b2aab18SMatthew Ahrens 858c166b69dSPaul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); 8593b2aab18SMatthew Ahrens bptree_add(mos, dp->dp_bptree_obj, 860c1379625SJustin T. Gibbs &dsl_dataset_phys(ds)->ds_bp, 861c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, 8623b2aab18SMatthew Ahrens used, comp, uncomp, tx); 863c166b69dSPaul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG); 8643b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 8653b2aab18SMatthew Ahrens -used, -comp, -uncomp, tx); 8663b2aab18SMatthew Ahrens dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD, 8673b2aab18SMatthew Ahrens used, comp, uncomp, tx); 8683b2aab18SMatthew Ahrens } 8693b2aab18SMatthew Ahrens 8703b2aab18SMatthew Ahrens if (ds->ds_prev != NULL) { 8713b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 8723b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(mos, 873c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones, 8743b2aab18SMatthew Ahrens ds->ds_object, tx)); 8753b2aab18SMatthew Ahrens } 8763b2aab18SMatthew Ahrens prevobj = ds->ds_prev->ds_object; 8773b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 8783b2aab18SMatthew Ahrens ds->ds_prev = NULL; 8793b2aab18SMatthew Ahrens } 8803b2aab18SMatthew Ahrens 8813b2aab18SMatthew Ahrens /* 8823b2aab18SMatthew Ahrens * This must be done after the dsl_traverse(), because it will 8833b2aab18SMatthew Ahrens * re-open the objset. 8843b2aab18SMatthew Ahrens */ 8853b2aab18SMatthew Ahrens if (ds->ds_objset) { 8863b2aab18SMatthew Ahrens dmu_objset_evict(ds->ds_objset); 8873b2aab18SMatthew Ahrens ds->ds_objset = NULL; 8883b2aab18SMatthew Ahrens } 8893b2aab18SMatthew Ahrens 8903b2aab18SMatthew Ahrens /* Erase the link in the dir */ 8913b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 892c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0; 8933b2aab18SMatthew Ahrens ddobj = ds->ds_dir->dd_object; 894c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0); 895c1379625SJustin T. Gibbs VERIFY0(zap_destroy(mos, 896c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx)); 8973b2aab18SMatthew Ahrens 89878f17100SMatthew Ahrens if (ds->ds_bookmarks != 0) { 899c1379625SJustin T. Gibbs VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx)); 90078f17100SMatthew Ahrens spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx); 90178f17100SMatthew Ahrens } 90278f17100SMatthew Ahrens 9033b2aab18SMatthew Ahrens spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 9043b2aab18SMatthew Ahrens 905c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj); 906c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_props_obj); 907c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj); 9083b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 9093b2aab18SMatthew Ahrens ds->ds_dir = NULL; 9102acef22dSMatthew Ahrens dmu_object_free_zapified(mos, obj, tx); 9113b2aab18SMatthew Ahrens 9123b2aab18SMatthew Ahrens dsl_dir_destroy_sync(ddobj, tx); 9133b2aab18SMatthew Ahrens 9143b2aab18SMatthew Ahrens if (rmorigin) { 9153b2aab18SMatthew Ahrens dsl_dataset_t *prev; 9163b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev)); 9173b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx); 9183b2aab18SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 9193b2aab18SMatthew Ahrens } 9203b2aab18SMatthew Ahrens } 9213b2aab18SMatthew Ahrens 922dfc11533SChris Williamson void 9233b2aab18SMatthew Ahrens dsl_destroy_head_sync(void *arg, dmu_tx_t *tx) 9243b2aab18SMatthew Ahrens { 9253b2aab18SMatthew Ahrens dsl_destroy_head_arg_t *ddha = arg; 9263b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 9273b2aab18SMatthew Ahrens dsl_dataset_t *ds; 9283b2aab18SMatthew Ahrens 9293b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds)); 9303b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(ds, tx); 9313b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 9323b2aab18SMatthew Ahrens } 9333b2aab18SMatthew Ahrens 9343b2aab18SMatthew Ahrens static void 9353b2aab18SMatthew Ahrens dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx) 9363b2aab18SMatthew Ahrens { 9373b2aab18SMatthew Ahrens dsl_destroy_head_arg_t *ddha = arg; 9383b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 9393b2aab18SMatthew Ahrens dsl_dataset_t *ds; 9403b2aab18SMatthew Ahrens 9413b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds)); 9423b2aab18SMatthew Ahrens 9433b2aab18SMatthew Ahrens /* Mark it as inconsistent on-disk, in case we crash */ 9443b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 945c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT; 9463b2aab18SMatthew Ahrens 9473b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "destroy begin", tx, ""); 9483b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 9493b2aab18SMatthew Ahrens } 9503b2aab18SMatthew Ahrens 9513b2aab18SMatthew Ahrens int 9523b2aab18SMatthew Ahrens dsl_destroy_head(const char *name) 9533b2aab18SMatthew Ahrens { 9543b2aab18SMatthew Ahrens dsl_destroy_head_arg_t ddha; 9553b2aab18SMatthew Ahrens int error; 9563b2aab18SMatthew Ahrens spa_t *spa; 9573b2aab18SMatthew Ahrens boolean_t isenabled; 9583b2aab18SMatthew Ahrens 9593b2aab18SMatthew Ahrens #ifdef _KERNEL 9603b2aab18SMatthew Ahrens zfs_destroy_unmount_origin(name); 9613b2aab18SMatthew Ahrens #endif 9623b2aab18SMatthew Ahrens 9633b2aab18SMatthew Ahrens error = spa_open(name, &spa, FTAG); 9643b2aab18SMatthew Ahrens if (error != 0) 9653b2aab18SMatthew Ahrens return (error); 9662acef22dSMatthew Ahrens isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY); 9673b2aab18SMatthew Ahrens spa_close(spa, FTAG); 9683b2aab18SMatthew Ahrens 9693b2aab18SMatthew Ahrens ddha.ddha_name = name; 9703b2aab18SMatthew Ahrens 9713b2aab18SMatthew Ahrens if (!isenabled) { 9723b2aab18SMatthew Ahrens objset_t *os; 9733b2aab18SMatthew Ahrens 9743b2aab18SMatthew Ahrens error = dsl_sync_task(name, dsl_destroy_head_check, 9757d46dc6cSMatthew Ahrens dsl_destroy_head_begin_sync, &ddha, 9767d46dc6cSMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE); 9773b2aab18SMatthew Ahrens if (error != 0) 9783b2aab18SMatthew Ahrens return (error); 9793b2aab18SMatthew Ahrens 9803b2aab18SMatthew Ahrens /* 9813b2aab18SMatthew Ahrens * Head deletion is processed in one txg on old pools; 9823b2aab18SMatthew Ahrens * remove the objects from open context so that the txg sync 9833b2aab18SMatthew Ahrens * is not too long. 9843b2aab18SMatthew Ahrens */ 9853b2aab18SMatthew Ahrens error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os); 9863b2aab18SMatthew Ahrens if (error == 0) { 9873b2aab18SMatthew Ahrens uint64_t prev_snap_txg = 988c1379625SJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))-> 989c1379625SJustin T. Gibbs ds_prev_snap_txg; 9903b2aab18SMatthew Ahrens for (uint64_t obj = 0; error == 0; 9913b2aab18SMatthew Ahrens error = dmu_object_next(os, &obj, FALSE, 9923b2aab18SMatthew Ahrens prev_snap_txg)) 993713d6c20SMatthew Ahrens (void) dmu_free_long_object(os, obj); 9943b2aab18SMatthew Ahrens /* sync out all frees */ 9953b2aab18SMatthew Ahrens txg_wait_synced(dmu_objset_pool(os), 0); 9963b2aab18SMatthew Ahrens dmu_objset_disown(os, FTAG); 9973b2aab18SMatthew Ahrens } 9983b2aab18SMatthew Ahrens } 9993b2aab18SMatthew Ahrens 10003b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_destroy_head_check, 10017d46dc6cSMatthew Ahrens dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_NONE)); 10023b2aab18SMatthew Ahrens } 10033b2aab18SMatthew Ahrens 10043b2aab18SMatthew Ahrens /* 10053b2aab18SMatthew Ahrens * Note, this function is used as the callback for dmu_objset_find(). We 10063b2aab18SMatthew Ahrens * always return 0 so that we will continue to find and process 10073b2aab18SMatthew Ahrens * inconsistent datasets, even if we encounter an error trying to 10083b2aab18SMatthew Ahrens * process one of them. 10093b2aab18SMatthew Ahrens */ 10103b2aab18SMatthew Ahrens /* ARGSUSED */ 10113b2aab18SMatthew Ahrens int 10123b2aab18SMatthew Ahrens dsl_destroy_inconsistent(const char *dsname, void *arg) 10133b2aab18SMatthew Ahrens { 10143b2aab18SMatthew Ahrens objset_t *os; 10153b2aab18SMatthew Ahrens 10163b2aab18SMatthew Ahrens if (dmu_objset_hold(dsname, FTAG, &os) == 0) { 10179c3fd121SMatthew Ahrens boolean_t need_destroy = DS_IS_INCONSISTENT(dmu_objset_ds(os)); 10189c3fd121SMatthew Ahrens 10199c3fd121SMatthew Ahrens /* 10209c3fd121SMatthew Ahrens * If the dataset is inconsistent because a resumable receive 10219c3fd121SMatthew Ahrens * has failed, then do not destroy it. 10229c3fd121SMatthew Ahrens */ 10239c3fd121SMatthew Ahrens if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os))) 10249c3fd121SMatthew Ahrens need_destroy = B_FALSE; 10259c3fd121SMatthew Ahrens 10263b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 10279c3fd121SMatthew Ahrens if (need_destroy) 10283b2aab18SMatthew Ahrens (void) dsl_destroy_head(dsname); 10293b2aab18SMatthew Ahrens } 10303b2aab18SMatthew Ahrens return (0); 10313b2aab18SMatthew Ahrens } 1032