xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_destroy.c (revision c3d26abc9ee97b4f60233556aadeb57e0bd30bb9)
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.
23ca0cc391SMatthew Ahrens  * Copyright (c) 2012, 2015 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.
26*c3d26abcSMatthew 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>
333b2aab18SMatthew Ahrens #include <sys/dmu_tx.h>
343b2aab18SMatthew Ahrens #include <sys/dsl_pool.h>
353b2aab18SMatthew Ahrens #include <sys/dsl_dir.h>
363b2aab18SMatthew Ahrens #include <sys/dmu_traverse.h>
373b2aab18SMatthew Ahrens #include <sys/dsl_scan.h>
383b2aab18SMatthew Ahrens #include <sys/dmu_objset.h>
393b2aab18SMatthew Ahrens #include <sys/zap.h>
403b2aab18SMatthew Ahrens #include <sys/zfeature.h>
413b2aab18SMatthew Ahrens #include <sys/zfs_ioctl.h>
423b2aab18SMatthew Ahrens #include <sys/dsl_deleg.h>
432acef22dSMatthew Ahrens #include <sys/dmu_impl.h>
443b2aab18SMatthew Ahrens 
453b2aab18SMatthew Ahrens typedef struct dmu_snapshots_destroy_arg {
463b2aab18SMatthew Ahrens 	nvlist_t *dsda_snaps;
473b2aab18SMatthew Ahrens 	nvlist_t *dsda_successful_snaps;
483b2aab18SMatthew Ahrens 	boolean_t dsda_defer;
493b2aab18SMatthew Ahrens 	nvlist_t *dsda_errlist;
503b2aab18SMatthew Ahrens } dmu_snapshots_destroy_arg_t;
513b2aab18SMatthew Ahrens 
5234f2f8cfSMatthew Ahrens int
533b2aab18SMatthew Ahrens dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
543b2aab18SMatthew Ahrens {
55bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot)
56be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
573b2aab18SMatthew Ahrens 
583b2aab18SMatthew Ahrens 	if (dsl_dataset_long_held(ds))
59be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
603b2aab18SMatthew Ahrens 
613b2aab18SMatthew Ahrens 	/*
623b2aab18SMatthew Ahrens 	 * Only allow deferred destroy on pools that support it.
633b2aab18SMatthew Ahrens 	 * NOTE: deferred destroy is only supported on snapshots.
643b2aab18SMatthew Ahrens 	 */
653b2aab18SMatthew Ahrens 	if (defer) {
663b2aab18SMatthew Ahrens 		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
673b2aab18SMatthew Ahrens 		    SPA_VERSION_USERREFS)
68be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOTSUP));
693b2aab18SMatthew Ahrens 		return (0);
703b2aab18SMatthew Ahrens 	}
713b2aab18SMatthew Ahrens 
723b2aab18SMatthew Ahrens 	/*
733b2aab18SMatthew Ahrens 	 * If this snapshot has an elevated user reference count,
743b2aab18SMatthew Ahrens 	 * we can't destroy it yet.
753b2aab18SMatthew Ahrens 	 */
763b2aab18SMatthew Ahrens 	if (ds->ds_userrefs > 0)
77be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
783b2aab18SMatthew Ahrens 
793b2aab18SMatthew Ahrens 	/*
803b2aab18SMatthew Ahrens 	 * Can't delete a branch point.
813b2aab18SMatthew Ahrens 	 */
82c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_num_children > 1)
83be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
843b2aab18SMatthew Ahrens 
853b2aab18SMatthew Ahrens 	return (0);
863b2aab18SMatthew Ahrens }
873b2aab18SMatthew Ahrens 
883b2aab18SMatthew Ahrens static int
893b2aab18SMatthew Ahrens dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
903b2aab18SMatthew Ahrens {
913b2aab18SMatthew Ahrens 	dmu_snapshots_destroy_arg_t *dsda = arg;
923b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
933b2aab18SMatthew Ahrens 	nvpair_t *pair;
943b2aab18SMatthew Ahrens 	int error = 0;
953b2aab18SMatthew Ahrens 
963b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
973b2aab18SMatthew Ahrens 		return (0);
983b2aab18SMatthew Ahrens 
993b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
1003b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
1013b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
1023b2aab18SMatthew Ahrens 
1033b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, nvpair_name(pair),
1043b2aab18SMatthew Ahrens 		    FTAG, &ds);
1053b2aab18SMatthew Ahrens 
1063b2aab18SMatthew Ahrens 		/*
1073b2aab18SMatthew Ahrens 		 * If the snapshot does not exist, silently ignore it
1083b2aab18SMatthew Ahrens 		 * (it's "already destroyed").
1093b2aab18SMatthew Ahrens 		 */
1103b2aab18SMatthew Ahrens 		if (error == ENOENT)
1113b2aab18SMatthew Ahrens 			continue;
1123b2aab18SMatthew Ahrens 
1133b2aab18SMatthew Ahrens 		if (error == 0) {
1143b2aab18SMatthew Ahrens 			error = dsl_destroy_snapshot_check_impl(ds,
1153b2aab18SMatthew Ahrens 			    dsda->dsda_defer);
1163b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
1173b2aab18SMatthew Ahrens 		}
1183b2aab18SMatthew Ahrens 
1193b2aab18SMatthew Ahrens 		if (error == 0) {
1203b2aab18SMatthew Ahrens 			fnvlist_add_boolean(dsda->dsda_successful_snaps,
1213b2aab18SMatthew Ahrens 			    nvpair_name(pair));
1223b2aab18SMatthew Ahrens 		} else {
1233b2aab18SMatthew Ahrens 			fnvlist_add_int32(dsda->dsda_errlist,
1243b2aab18SMatthew Ahrens 			    nvpair_name(pair), error);
1253b2aab18SMatthew Ahrens 		}
1263b2aab18SMatthew Ahrens 	}
1273b2aab18SMatthew Ahrens 
1283b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
1293b2aab18SMatthew Ahrens 	if (pair != NULL)
1303b2aab18SMatthew Ahrens 		return (fnvpair_value_int32(pair));
131a7a845e4SSteven Hartland 
1323b2aab18SMatthew Ahrens 	return (0);
1333b2aab18SMatthew Ahrens }
1343b2aab18SMatthew Ahrens 
1353b2aab18SMatthew Ahrens struct process_old_arg {
1363b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
1373b2aab18SMatthew Ahrens 	dsl_dataset_t *ds_prev;
1383b2aab18SMatthew Ahrens 	boolean_t after_branch_point;
1393b2aab18SMatthew Ahrens 	zio_t *pio;
1403b2aab18SMatthew Ahrens 	uint64_t used, comp, uncomp;
1413b2aab18SMatthew Ahrens };
1423b2aab18SMatthew Ahrens 
1433b2aab18SMatthew Ahrens static int
1443b2aab18SMatthew Ahrens process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1453b2aab18SMatthew Ahrens {
1463b2aab18SMatthew Ahrens 	struct process_old_arg *poa = arg;
1473b2aab18SMatthew Ahrens 	dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
1483b2aab18SMatthew Ahrens 
14943466aaeSMax Grossman 	ASSERT(!BP_IS_HOLE(bp));
15043466aaeSMax Grossman 
151c1379625SJustin T. Gibbs 	if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
1523b2aab18SMatthew Ahrens 		dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
1533b2aab18SMatthew Ahrens 		if (poa->ds_prev && !poa->after_branch_point &&
1543b2aab18SMatthew Ahrens 		    bp->blk_birth >
155c1379625SJustin T. Gibbs 		    dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
156c1379625SJustin T. Gibbs 			dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
1573b2aab18SMatthew Ahrens 			    bp_get_dsize_sync(dp->dp_spa, bp);
1583b2aab18SMatthew Ahrens 		}
1593b2aab18SMatthew Ahrens 	} else {
1603b2aab18SMatthew Ahrens 		poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
1613b2aab18SMatthew Ahrens 		poa->comp += BP_GET_PSIZE(bp);
1623b2aab18SMatthew Ahrens 		poa->uncomp += BP_GET_UCSIZE(bp);
1633b2aab18SMatthew Ahrens 		dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
1643b2aab18SMatthew Ahrens 	}
1653b2aab18SMatthew Ahrens 	return (0);
1663b2aab18SMatthew Ahrens }
1673b2aab18SMatthew Ahrens 
1683b2aab18SMatthew Ahrens static void
1693b2aab18SMatthew Ahrens process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
1703b2aab18SMatthew Ahrens     dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
1713b2aab18SMatthew Ahrens {
1723b2aab18SMatthew Ahrens 	struct process_old_arg poa = { 0 };
1733b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1743b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
1753b2aab18SMatthew Ahrens 	uint64_t deadlist_obj;
1763b2aab18SMatthew Ahrens 
1773b2aab18SMatthew Ahrens 	ASSERT(ds->ds_deadlist.dl_oldfmt);
1783b2aab18SMatthew Ahrens 	ASSERT(ds_next->ds_deadlist.dl_oldfmt);
1793b2aab18SMatthew Ahrens 
1803b2aab18SMatthew Ahrens 	poa.ds = ds;
1813b2aab18SMatthew Ahrens 	poa.ds_prev = ds_prev;
1823b2aab18SMatthew Ahrens 	poa.after_branch_point = after_branch_point;
1833b2aab18SMatthew Ahrens 	poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1843b2aab18SMatthew Ahrens 	VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
1853b2aab18SMatthew Ahrens 	    process_old_cb, &poa, tx));
1863b2aab18SMatthew Ahrens 	VERIFY0(zio_wait(poa.pio));
187c1379625SJustin T. Gibbs 	ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
1883b2aab18SMatthew Ahrens 
1893b2aab18SMatthew Ahrens 	/* change snapused */
1903b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1913b2aab18SMatthew Ahrens 	    -poa.used, -poa.comp, -poa.uncomp, tx);
1923b2aab18SMatthew Ahrens 
1933b2aab18SMatthew Ahrens 	/* swap next's deadlist to our deadlist */
1943b2aab18SMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1953b2aab18SMatthew Ahrens 	dsl_deadlist_close(&ds_next->ds_deadlist);
196c1379625SJustin T. Gibbs 	deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
197c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj =
198c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds_next)->ds_deadlist_obj;
199c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj;
200c1379625SJustin T. Gibbs 	dsl_deadlist_open(&ds->ds_deadlist, mos,
201c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
2023b2aab18SMatthew Ahrens 	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
203c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds_next)->ds_deadlist_obj);
2043b2aab18SMatthew Ahrens }
2053b2aab18SMatthew Ahrens 
2063b2aab18SMatthew Ahrens static void
2073b2aab18SMatthew Ahrens dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
2083b2aab18SMatthew Ahrens {
2093b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2103b2aab18SMatthew Ahrens 	zap_cursor_t zc;
2113b2aab18SMatthew Ahrens 	zap_attribute_t za;
2123b2aab18SMatthew Ahrens 
2133b2aab18SMatthew Ahrens 	/*
2143b2aab18SMatthew Ahrens 	 * If it is the old version, dd_clones doesn't exist so we can't
2153b2aab18SMatthew Ahrens 	 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
2163b2aab18SMatthew Ahrens 	 * doesn't matter.
2173b2aab18SMatthew Ahrens 	 */
218c1379625SJustin T. Gibbs 	if (dsl_dir_phys(ds->ds_dir)->dd_clones == 0)
2193b2aab18SMatthew Ahrens 		return;
2203b2aab18SMatthew Ahrens 
221c1379625SJustin T. Gibbs 	for (zap_cursor_init(&zc, mos, dsl_dir_phys(ds->ds_dir)->dd_clones);
2223b2aab18SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
2233b2aab18SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
2243b2aab18SMatthew Ahrens 		dsl_dataset_t *clone;
2253b2aab18SMatthew Ahrens 
2263b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
2273b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
2283b2aab18SMatthew Ahrens 		if (clone->ds_dir->dd_origin_txg > mintxg) {
2293b2aab18SMatthew Ahrens 			dsl_deadlist_remove_key(&clone->ds_deadlist,
2303b2aab18SMatthew Ahrens 			    mintxg, tx);
2313b2aab18SMatthew Ahrens 			dsl_dataset_remove_clones_key(clone, mintxg, tx);
2323b2aab18SMatthew Ahrens 		}
2333b2aab18SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
2343b2aab18SMatthew Ahrens 	}
2353b2aab18SMatthew Ahrens 	zap_cursor_fini(&zc);
2363b2aab18SMatthew Ahrens }
2373b2aab18SMatthew Ahrens 
2383b2aab18SMatthew Ahrens void
2393b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
2403b2aab18SMatthew Ahrens {
2413b2aab18SMatthew Ahrens 	int err;
2423b2aab18SMatthew Ahrens 	int after_branch_point = FALSE;
2433b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2443b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
2453b2aab18SMatthew Ahrens 	dsl_dataset_t *ds_prev = NULL;
2463b2aab18SMatthew Ahrens 	uint64_t obj;
2473b2aab18SMatthew Ahrens 
2483b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
249c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
2503b2aab18SMatthew Ahrens 	ASSERT(refcount_is_zero(&ds->ds_longholds));
2513b2aab18SMatthew Ahrens 
2523b2aab18SMatthew Ahrens 	if (defer &&
253c1379625SJustin T. Gibbs 	    (ds->ds_userrefs > 0 ||
254c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_num_children > 1)) {
2553b2aab18SMatthew Ahrens 		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
2563b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
257c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
2583b2aab18SMatthew Ahrens 		spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
2593b2aab18SMatthew Ahrens 		return;
2603b2aab18SMatthew Ahrens 	}
2613b2aab18SMatthew Ahrens 
262c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
2633b2aab18SMatthew Ahrens 
2643b2aab18SMatthew Ahrens 	/* We need to log before removing it from the namespace. */
2653b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "destroy", tx, "");
2663b2aab18SMatthew Ahrens 
2673b2aab18SMatthew Ahrens 	dsl_scan_ds_destroyed(ds, tx);
2683b2aab18SMatthew Ahrens 
2693b2aab18SMatthew Ahrens 	obj = ds->ds_object;
2703b2aab18SMatthew Ahrens 
271ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
272ca0cc391SMatthew Ahrens 		if (ds->ds_feature_inuse[f]) {
273ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(obj, f, tx);
274ca0cc391SMatthew Ahrens 			ds->ds_feature_inuse[f] = B_FALSE;
275ca0cc391SMatthew Ahrens 		}
276b5152584SMatthew Ahrens 	}
277c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2783b2aab18SMatthew Ahrens 		ASSERT3P(ds->ds_prev, ==, NULL);
2793b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
280c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
2813b2aab18SMatthew Ahrens 		after_branch_point =
282c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
2833b2aab18SMatthew Ahrens 
2843b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
2853b2aab18SMatthew Ahrens 		if (after_branch_point &&
286c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
2873b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
288c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
2893b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(mos,
290c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds_prev)->
291c1379625SJustin T. Gibbs 				    ds_next_clones_obj,
292c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_next_snap_obj,
293c1379625SJustin T. Gibbs 				    tx));
2943b2aab18SMatthew Ahrens 			}
2953b2aab18SMatthew Ahrens 		}
2963b2aab18SMatthew Ahrens 		if (!after_branch_point) {
297c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
298c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_snap_obj;
2993b2aab18SMatthew Ahrens 		}
3003b2aab18SMatthew Ahrens 	}
3013b2aab18SMatthew Ahrens 
3023b2aab18SMatthew Ahrens 	dsl_dataset_t *ds_next;
3033b2aab18SMatthew Ahrens 	uint64_t old_unique;
3043b2aab18SMatthew Ahrens 	uint64_t used = 0, comp = 0, uncomp = 0;
3053b2aab18SMatthew Ahrens 
3063b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
307c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
308c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
3093b2aab18SMatthew Ahrens 
310c1379625SJustin T. Gibbs 	old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
3113b2aab18SMatthew Ahrens 
3123b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
313c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
314c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj;
315c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
316c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
317c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
318c1379625SJustin T. Gibbs 	    ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
3193b2aab18SMatthew Ahrens 
3203b2aab18SMatthew Ahrens 	if (ds_next->ds_deadlist.dl_oldfmt) {
3213b2aab18SMatthew Ahrens 		process_old_deadlist(ds, ds_prev, ds_next,
3223b2aab18SMatthew Ahrens 		    after_branch_point, tx);
3233b2aab18SMatthew Ahrens 	} else {
3243b2aab18SMatthew Ahrens 		/* Adjust prev's unique space. */
3253b2aab18SMatthew Ahrens 		if (ds_prev && !after_branch_point) {
3263b2aab18SMatthew Ahrens 			dsl_deadlist_space_range(&ds_next->ds_deadlist,
327c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
328c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_prev_snap_txg,
3293b2aab18SMatthew Ahrens 			    &used, &comp, &uncomp);
330c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
3313b2aab18SMatthew Ahrens 		}
3323b2aab18SMatthew Ahrens 
3333b2aab18SMatthew Ahrens 		/* Adjust snapused. */
3343b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&ds_next->ds_deadlist,
335c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
3363b2aab18SMatthew Ahrens 		    &used, &comp, &uncomp);
3373b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
3383b2aab18SMatthew Ahrens 		    -used, -comp, -uncomp, tx);
3393b2aab18SMatthew Ahrens 
3403b2aab18SMatthew Ahrens 		/* Move blocks to be freed to pool's free list. */
3413b2aab18SMatthew Ahrens 		dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
342c1379625SJustin T. Gibbs 		    &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
3433b2aab18SMatthew Ahrens 		    tx);
3443b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
3453b2aab18SMatthew Ahrens 		    DD_USED_HEAD, used, comp, uncomp, tx);
3463b2aab18SMatthew Ahrens 
3473b2aab18SMatthew Ahrens 		/* Merge our deadlist into next's and free it. */
3483b2aab18SMatthew Ahrens 		dsl_deadlist_merge(&ds_next->ds_deadlist,
349c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3503b2aab18SMatthew Ahrens 	}
3513b2aab18SMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
352c1379625SJustin T. Gibbs 	dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3533b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
354c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
3553b2aab18SMatthew Ahrens 
3563b2aab18SMatthew Ahrens 	/* Collapse range in clone heads */
3573b2aab18SMatthew Ahrens 	dsl_dataset_remove_clones_key(ds,
358c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_txg, tx);
3593b2aab18SMatthew Ahrens 
360bc9014e6SJustin Gibbs 	if (ds_next->ds_is_snapshot) {
3613b2aab18SMatthew Ahrens 		dsl_dataset_t *ds_nextnext;
3623b2aab18SMatthew Ahrens 
3633b2aab18SMatthew Ahrens 		/*
3643b2aab18SMatthew Ahrens 		 * Update next's unique to include blocks which
3653b2aab18SMatthew Ahrens 		 * were previously shared by only this snapshot
3663b2aab18SMatthew Ahrens 		 * and it.  Those blocks will be born after the
3673b2aab18SMatthew Ahrens 		 * prev snap and before this snap, and will have
3683b2aab18SMatthew Ahrens 		 * died after the next snap and before the one
3693b2aab18SMatthew Ahrens 		 * after that (ie. be on the snap after next's
3703b2aab18SMatthew Ahrens 		 * deadlist).
3713b2aab18SMatthew Ahrens 		 */
3723b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
373c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds_next)->ds_next_snap_obj,
374c1379625SJustin T. Gibbs 		    FTAG, &ds_nextnext));
3753b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
376c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_txg,
377c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_creation_txg,
3783b2aab18SMatthew Ahrens 		    &used, &comp, &uncomp);
379c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
3803b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds_nextnext, FTAG);
3813b2aab18SMatthew Ahrens 		ASSERT3P(ds_next->ds_prev, ==, NULL);
3823b2aab18SMatthew Ahrens 
3833b2aab18SMatthew Ahrens 		/* Collapse range in this head. */
3843b2aab18SMatthew Ahrens 		dsl_dataset_t *hds;
3853b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
386c1379625SJustin T. Gibbs 		    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
3873b2aab18SMatthew Ahrens 		dsl_deadlist_remove_key(&hds->ds_deadlist,
388c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_creation_txg, tx);
3893b2aab18SMatthew Ahrens 		dsl_dataset_rele(hds, FTAG);
3903b2aab18SMatthew Ahrens 
3913b2aab18SMatthew Ahrens 	} else {
3923b2aab18SMatthew Ahrens 		ASSERT3P(ds_next->ds_prev, ==, ds);
3933b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds_next->ds_prev, ds_next);
3943b2aab18SMatthew Ahrens 		ds_next->ds_prev = NULL;
3953b2aab18SMatthew Ahrens 		if (ds_prev) {
3963b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
397c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_prev_snap_obj,
3983b2aab18SMatthew Ahrens 			    ds_next, &ds_next->ds_prev));
3993b2aab18SMatthew Ahrens 		}
4003b2aab18SMatthew Ahrens 
4013b2aab18SMatthew Ahrens 		dsl_dataset_recalc_head_uniq(ds_next);
4023b2aab18SMatthew Ahrens 
4033b2aab18SMatthew Ahrens 		/*
4043b2aab18SMatthew Ahrens 		 * Reduce the amount of our unconsumed refreservation
4053b2aab18SMatthew Ahrens 		 * being charged to our parent by the amount of
4063b2aab18SMatthew Ahrens 		 * new unique data we have gained.
4073b2aab18SMatthew Ahrens 		 */
4083b2aab18SMatthew Ahrens 		if (old_unique < ds_next->ds_reserved) {
4093b2aab18SMatthew Ahrens 			int64_t mrsdelta;
4103b2aab18SMatthew Ahrens 			uint64_t new_unique =
411c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds_next)->ds_unique_bytes;
4123b2aab18SMatthew Ahrens 
4133b2aab18SMatthew Ahrens 			ASSERT(old_unique <= new_unique);
4143b2aab18SMatthew Ahrens 			mrsdelta = MIN(new_unique - old_unique,
4153b2aab18SMatthew Ahrens 			    ds_next->ds_reserved - old_unique);
4163b2aab18SMatthew Ahrens 			dsl_dir_diduse_space(ds->ds_dir,
4173b2aab18SMatthew Ahrens 			    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
4183b2aab18SMatthew Ahrens 		}
4193b2aab18SMatthew Ahrens 	}
4203b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds_next, FTAG);
4213b2aab18SMatthew Ahrens 
4223b2aab18SMatthew Ahrens 	/*
4233b2aab18SMatthew Ahrens 	 * This must be done after the dsl_traverse(), because it will
4243b2aab18SMatthew Ahrens 	 * re-open the objset.
4253b2aab18SMatthew Ahrens 	 */
4263b2aab18SMatthew Ahrens 	if (ds->ds_objset) {
4273b2aab18SMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
4283b2aab18SMatthew Ahrens 		ds->ds_objset = NULL;
4293b2aab18SMatthew Ahrens 	}
4303b2aab18SMatthew Ahrens 
4313b2aab18SMatthew Ahrens 	/* remove from snapshot namespace */
4323b2aab18SMatthew Ahrens 	dsl_dataset_t *ds_head;
433c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
4343b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
435c1379625SJustin T. Gibbs 	    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
4363b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_get_snapname(ds));
4373b2aab18SMatthew Ahrens #ifdef ZFS_DEBUG
4383b2aab18SMatthew Ahrens 	{
4393b2aab18SMatthew Ahrens 		uint64_t val;
4403b2aab18SMatthew Ahrens 
4413b2aab18SMatthew Ahrens 		err = dsl_dataset_snap_lookup(ds_head,
4423b2aab18SMatthew Ahrens 		    ds->ds_snapname, &val);
4433b2aab18SMatthew Ahrens 		ASSERT0(err);
4443b2aab18SMatthew Ahrens 		ASSERT3U(val, ==, obj);
4453b2aab18SMatthew Ahrens 	}
4463b2aab18SMatthew Ahrens #endif
447a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
4483b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds_head, FTAG);
4493b2aab18SMatthew Ahrens 
4503b2aab18SMatthew Ahrens 	if (ds_prev != NULL)
4513b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds_prev, FTAG);
4523b2aab18SMatthew Ahrens 
4533b2aab18SMatthew Ahrens 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
4543b2aab18SMatthew Ahrens 
455c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
4563b2aab18SMatthew Ahrens 		uint64_t count;
4573b2aab18SMatthew Ahrens 		ASSERT0(zap_count(mos,
458c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
459c1379625SJustin T. Gibbs 		    count == 0);
4603b2aab18SMatthew Ahrens 		VERIFY0(dmu_object_free(mos,
461c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
4623b2aab18SMatthew Ahrens 	}
463c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_props_obj != 0)
464c1379625SJustin T. Gibbs 		VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
465c1379625SJustin T. Gibbs 		    tx));
466c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
467c1379625SJustin T. Gibbs 		VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
468c1379625SJustin T. Gibbs 		    tx));
4693b2aab18SMatthew Ahrens 	dsl_dir_rele(ds->ds_dir, ds);
4703b2aab18SMatthew Ahrens 	ds->ds_dir = NULL;
4712acef22dSMatthew Ahrens 	dmu_object_free_zapified(mos, obj, tx);
4723b2aab18SMatthew Ahrens }
4733b2aab18SMatthew Ahrens 
4743b2aab18SMatthew Ahrens static void
4753b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
4763b2aab18SMatthew Ahrens {
4773b2aab18SMatthew Ahrens 	dmu_snapshots_destroy_arg_t *dsda = arg;
4783b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
4793b2aab18SMatthew Ahrens 	nvpair_t *pair;
4803b2aab18SMatthew Ahrens 
4813b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
4823b2aab18SMatthew Ahrens 	    pair != NULL;
4833b2aab18SMatthew Ahrens 	    pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
4843b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
4853b2aab18SMatthew Ahrens 
4863b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
4873b2aab18SMatthew Ahrens 
4883b2aab18SMatthew Ahrens 		dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
4893b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
4903b2aab18SMatthew Ahrens 	}
4913b2aab18SMatthew Ahrens }
4923b2aab18SMatthew Ahrens 
4933b2aab18SMatthew Ahrens /*
4943b2aab18SMatthew Ahrens  * The semantics of this function are described in the comment above
4953b2aab18SMatthew Ahrens  * lzc_destroy_snaps().  To summarize:
4963b2aab18SMatthew Ahrens  *
4973b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
4983b2aab18SMatthew Ahrens  *
4993b2aab18SMatthew Ahrens  * Snapshots that don't exist will be silently ignored (considered to be
5003b2aab18SMatthew Ahrens  * "already deleted").
5013b2aab18SMatthew Ahrens  *
5023b2aab18SMatthew Ahrens  * On success, all snaps will be destroyed and this will return 0.
5033b2aab18SMatthew Ahrens  * On failure, no snaps will be destroyed, the errlist will be filled in,
5043b2aab18SMatthew Ahrens  * and this will return an errno.
5053b2aab18SMatthew Ahrens  */
5063b2aab18SMatthew Ahrens int
5073b2aab18SMatthew Ahrens dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
5083b2aab18SMatthew Ahrens     nvlist_t *errlist)
5093b2aab18SMatthew Ahrens {
5103b2aab18SMatthew Ahrens 	dmu_snapshots_destroy_arg_t dsda;
5113b2aab18SMatthew Ahrens 	int error;
5123b2aab18SMatthew Ahrens 	nvpair_t *pair;
5133b2aab18SMatthew Ahrens 
5143b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
5153b2aab18SMatthew Ahrens 	if (pair == NULL)
5163b2aab18SMatthew Ahrens 		return (0);
5173b2aab18SMatthew Ahrens 
5183b2aab18SMatthew Ahrens 	dsda.dsda_snaps = snaps;
5193b2aab18SMatthew Ahrens 	dsda.dsda_successful_snaps = fnvlist_alloc();
5203b2aab18SMatthew Ahrens 	dsda.dsda_defer = defer;
5213b2aab18SMatthew Ahrens 	dsda.dsda_errlist = errlist;
5223b2aab18SMatthew Ahrens 
5233b2aab18SMatthew Ahrens 	error = dsl_sync_task(nvpair_name(pair),
5243b2aab18SMatthew Ahrens 	    dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
5257d46dc6cSMatthew Ahrens 	    &dsda, 0, ZFS_SPACE_CHECK_NONE);
5263b2aab18SMatthew Ahrens 	fnvlist_free(dsda.dsda_successful_snaps);
5273b2aab18SMatthew Ahrens 
5283b2aab18SMatthew Ahrens 	return (error);
5293b2aab18SMatthew Ahrens }
5303b2aab18SMatthew Ahrens 
5313b2aab18SMatthew Ahrens int
5323b2aab18SMatthew Ahrens dsl_destroy_snapshot(const char *name, boolean_t defer)
5333b2aab18SMatthew Ahrens {
5343b2aab18SMatthew Ahrens 	int error;
5353b2aab18SMatthew Ahrens 	nvlist_t *nvl = fnvlist_alloc();
5363b2aab18SMatthew Ahrens 	nvlist_t *errlist = fnvlist_alloc();
5373b2aab18SMatthew Ahrens 
5383b2aab18SMatthew Ahrens 	fnvlist_add_boolean(nvl, name);
5393b2aab18SMatthew Ahrens 	error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
5403b2aab18SMatthew Ahrens 	fnvlist_free(errlist);
5413b2aab18SMatthew Ahrens 	fnvlist_free(nvl);
5423b2aab18SMatthew Ahrens 	return (error);
5433b2aab18SMatthew Ahrens }
5443b2aab18SMatthew Ahrens 
5453b2aab18SMatthew Ahrens struct killarg {
5463b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
5473b2aab18SMatthew Ahrens 	dmu_tx_t *tx;
5483b2aab18SMatthew Ahrens };
5493b2aab18SMatthew Ahrens 
5503b2aab18SMatthew Ahrens /* ARGSUSED */
5513b2aab18SMatthew Ahrens static int
5523b2aab18SMatthew Ahrens kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5537802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
5543b2aab18SMatthew Ahrens {
5553b2aab18SMatthew Ahrens 	struct killarg *ka = arg;
5563b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ka->tx;
5573b2aab18SMatthew Ahrens 
558a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
5593b2aab18SMatthew Ahrens 		return (0);
5603b2aab18SMatthew Ahrens 
5613b2aab18SMatthew Ahrens 	if (zb->zb_level == ZB_ZIL_LEVEL) {
5623b2aab18SMatthew Ahrens 		ASSERT(zilog != NULL);
5633b2aab18SMatthew Ahrens 		/*
5643b2aab18SMatthew Ahrens 		 * It's a block in the intent log.  It has no
5653b2aab18SMatthew Ahrens 		 * accounting, so just free it.
5663b2aab18SMatthew Ahrens 		 */
5673b2aab18SMatthew Ahrens 		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
5683b2aab18SMatthew Ahrens 	} else {
5693b2aab18SMatthew Ahrens 		ASSERT(zilog == NULL);
570c1379625SJustin T. Gibbs 		ASSERT3U(bp->blk_birth, >,
571c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
5723b2aab18SMatthew Ahrens 		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
5733b2aab18SMatthew Ahrens 	}
5743b2aab18SMatthew Ahrens 
5753b2aab18SMatthew Ahrens 	return (0);
5763b2aab18SMatthew Ahrens }
5773b2aab18SMatthew Ahrens 
5783b2aab18SMatthew Ahrens static void
5793b2aab18SMatthew Ahrens old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
5803b2aab18SMatthew Ahrens {
5813b2aab18SMatthew Ahrens 	struct killarg ka;
5823b2aab18SMatthew Ahrens 
5833b2aab18SMatthew Ahrens 	/*
5843b2aab18SMatthew Ahrens 	 * Free everything that we point to (that's born after
5853b2aab18SMatthew Ahrens 	 * the previous snapshot, if we are a clone)
5863b2aab18SMatthew Ahrens 	 *
5873b2aab18SMatthew Ahrens 	 * NB: this should be very quick, because we already
5883b2aab18SMatthew Ahrens 	 * freed all the objects in open context.
5893b2aab18SMatthew Ahrens 	 */
5903b2aab18SMatthew Ahrens 	ka.ds = ds;
5913b2aab18SMatthew Ahrens 	ka.tx = tx;
5923b2aab18SMatthew Ahrens 	VERIFY0(traverse_dataset(ds,
593c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST,
5943b2aab18SMatthew Ahrens 	    kill_blkptr, &ka));
595c1379625SJustin T. Gibbs 	ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
596c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes == 0);
5973b2aab18SMatthew Ahrens }
5983b2aab18SMatthew Ahrens 
5993b2aab18SMatthew Ahrens typedef struct dsl_destroy_head_arg {
6003b2aab18SMatthew Ahrens 	const char *ddha_name;
6013b2aab18SMatthew Ahrens } dsl_destroy_head_arg_t;
6023b2aab18SMatthew Ahrens 
6033b2aab18SMatthew Ahrens int
6043b2aab18SMatthew Ahrens dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
6053b2aab18SMatthew Ahrens {
6063b2aab18SMatthew Ahrens 	int error;
6073b2aab18SMatthew Ahrens 	uint64_t count;
6083b2aab18SMatthew Ahrens 	objset_t *mos;
6093b2aab18SMatthew Ahrens 
610bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
611bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot)
612be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
6133b2aab18SMatthew Ahrens 
6143b2aab18SMatthew Ahrens 	if (refcount_count(&ds->ds_longholds) != expected_holds)
615be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6163b2aab18SMatthew Ahrens 
6173b2aab18SMatthew Ahrens 	mos = ds->ds_dir->dd_pool->dp_meta_objset;
6183b2aab18SMatthew Ahrens 
6193b2aab18SMatthew Ahrens 	/*
6203b2aab18SMatthew Ahrens 	 * Can't delete a head dataset if there are snapshots of it.
6213b2aab18SMatthew Ahrens 	 * (Except if the only snapshots are from the branch we cloned
6223b2aab18SMatthew Ahrens 	 * from.)
6233b2aab18SMatthew Ahrens 	 */
6243b2aab18SMatthew Ahrens 	if (ds->ds_prev != NULL &&
625c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
626be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6273b2aab18SMatthew Ahrens 
6283b2aab18SMatthew Ahrens 	/*
6293b2aab18SMatthew Ahrens 	 * Can't delete if there are children of this fs.
6303b2aab18SMatthew Ahrens 	 */
6313b2aab18SMatthew Ahrens 	error = zap_count(mos,
632c1379625SJustin T. Gibbs 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
6333b2aab18SMatthew Ahrens 	if (error != 0)
6343b2aab18SMatthew Ahrens 		return (error);
6353b2aab18SMatthew Ahrens 	if (count != 0)
636be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
6373b2aab18SMatthew Ahrens 
6383b2aab18SMatthew Ahrens 	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
639c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
6403b2aab18SMatthew Ahrens 	    ds->ds_prev->ds_userrefs == 0) {
6413b2aab18SMatthew Ahrens 		/* We need to remove the origin snapshot as well. */
6423b2aab18SMatthew Ahrens 		if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
643be6fd75aSMatthew Ahrens 			return (SET_ERROR(EBUSY));
6443b2aab18SMatthew Ahrens 	}
6453b2aab18SMatthew Ahrens 	return (0);
6463b2aab18SMatthew Ahrens }
6473b2aab18SMatthew Ahrens 
6483b2aab18SMatthew Ahrens static int
6493b2aab18SMatthew Ahrens dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
6503b2aab18SMatthew Ahrens {
6513b2aab18SMatthew Ahrens 	dsl_destroy_head_arg_t *ddha = arg;
6523b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
6533b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
6543b2aab18SMatthew Ahrens 	int error;
6553b2aab18SMatthew Ahrens 
6563b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
6573b2aab18SMatthew Ahrens 	if (error != 0)
6583b2aab18SMatthew Ahrens 		return (error);
6593b2aab18SMatthew Ahrens 
6603b2aab18SMatthew Ahrens 	error = dsl_destroy_head_check_impl(ds, 0);
6613b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
6623b2aab18SMatthew Ahrens 	return (error);
6633b2aab18SMatthew Ahrens }
6643b2aab18SMatthew Ahrens 
6653b2aab18SMatthew Ahrens static void
6663b2aab18SMatthew Ahrens dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
6673b2aab18SMatthew Ahrens {
6683b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
6693b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
6703b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
6713b2aab18SMatthew Ahrens 	dd_used_t t;
6723b2aab18SMatthew Ahrens 
6733b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
6743b2aab18SMatthew Ahrens 
6753b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
6763b2aab18SMatthew Ahrens 
677c1379625SJustin T. Gibbs 	ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
6783b2aab18SMatthew Ahrens 
679a2afb611SJerry Jelinek 	/*
680a2afb611SJerry Jelinek 	 * Decrement the filesystem count for all parent filesystems.
681a2afb611SJerry Jelinek 	 *
682a2afb611SJerry Jelinek 	 * When we receive an incremental stream into a filesystem that already
683a2afb611SJerry Jelinek 	 * exists, a temporary clone is created.  We never count this temporary
684a2afb611SJerry Jelinek 	 * clone, whose name begins with a '%'.
685a2afb611SJerry Jelinek 	 */
686a2afb611SJerry Jelinek 	if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
687a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -1,
688a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
689a2afb611SJerry Jelinek 
6903b2aab18SMatthew Ahrens 	/*
6913b2aab18SMatthew Ahrens 	 * Remove our reservation. The impl() routine avoids setting the
6923b2aab18SMatthew Ahrens 	 * actual property, which would require the (already destroyed) ds.
6933b2aab18SMatthew Ahrens 	 */
6943b2aab18SMatthew Ahrens 	dsl_dir_set_reservation_sync_impl(dd, 0, tx);
6953b2aab18SMatthew Ahrens 
696c1379625SJustin T. Gibbs 	ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
697c1379625SJustin T. Gibbs 	ASSERT0(dsl_dir_phys(dd)->dd_reserved);
6983b2aab18SMatthew Ahrens 	for (t = 0; t < DD_USED_NUM; t++)
699c1379625SJustin T. Gibbs 		ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
7003b2aab18SMatthew Ahrens 
701c1379625SJustin T. Gibbs 	VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
702c1379625SJustin T. Gibbs 	VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
703c1379625SJustin T. Gibbs 	VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx));
7043b2aab18SMatthew Ahrens 	VERIFY0(zap_remove(mos,
705c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
706c1379625SJustin T. Gibbs 	    dd->dd_myname, tx));
7073b2aab18SMatthew Ahrens 
7083b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
7092acef22dSMatthew Ahrens 	dmu_object_free_zapified(mos, ddobj, tx);
7103b2aab18SMatthew Ahrens }
7113b2aab18SMatthew Ahrens 
7123b2aab18SMatthew Ahrens void
7133b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
7143b2aab18SMatthew Ahrens {
7153b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
7163b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
7173b2aab18SMatthew Ahrens 	uint64_t obj, ddobj, prevobj = 0;
7183b2aab18SMatthew Ahrens 	boolean_t rmorigin;
7193b2aab18SMatthew Ahrens 
720c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
7213b2aab18SMatthew Ahrens 	ASSERT(ds->ds_prev == NULL ||
722c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object);
723c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
7243b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
7253b2aab18SMatthew Ahrens 
7263b2aab18SMatthew Ahrens 	/* We need to log before removing it from the namespace. */
7273b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "destroy", tx, "");
7283b2aab18SMatthew Ahrens 
7293b2aab18SMatthew Ahrens 	rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
7303b2aab18SMatthew Ahrens 	    DS_IS_DEFER_DESTROY(ds->ds_prev) &&
731c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
7323b2aab18SMatthew Ahrens 	    ds->ds_prev->ds_userrefs == 0);
7333b2aab18SMatthew Ahrens 
7345d7b4d43SMatthew Ahrens 	/* Remove our reservation. */
7353b2aab18SMatthew Ahrens 	if (ds->ds_reserved != 0) {
7363b2aab18SMatthew Ahrens 		dsl_dataset_set_refreservation_sync_impl(ds,
7373b2aab18SMatthew Ahrens 		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
7383b2aab18SMatthew Ahrens 		    0, tx);
7393b2aab18SMatthew Ahrens 		ASSERT0(ds->ds_reserved);
7403b2aab18SMatthew Ahrens 	}
7413b2aab18SMatthew Ahrens 
742ca0cc391SMatthew Ahrens 	obj = ds->ds_object;
743b5152584SMatthew Ahrens 
744ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
745ca0cc391SMatthew Ahrens 		if (ds->ds_feature_inuse[f]) {
746ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(obj, f, tx);
747ca0cc391SMatthew Ahrens 			ds->ds_feature_inuse[f] = B_FALSE;
748ca0cc391SMatthew Ahrens 		}
749ca0cc391SMatthew Ahrens 	}
7503b2aab18SMatthew Ahrens 
751ca0cc391SMatthew Ahrens 	dsl_scan_ds_destroyed(ds, tx);
7523b2aab18SMatthew Ahrens 
753c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
7543b2aab18SMatthew Ahrens 		/* This is a clone */
7553b2aab18SMatthew Ahrens 		ASSERT(ds->ds_prev != NULL);
756c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
757c1379625SJustin T. Gibbs 		    obj);
758c1379625SJustin T. Gibbs 		ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
7593b2aab18SMatthew Ahrens 
7603b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
761c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
7623b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
7633b2aab18SMatthew Ahrens 			    obj, tx);
7643b2aab18SMatthew Ahrens 		}
7653b2aab18SMatthew Ahrens 
766c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
767c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
7683b2aab18SMatthew Ahrens 	}
7693b2aab18SMatthew Ahrens 
7703b2aab18SMatthew Ahrens 	/*
7713b2aab18SMatthew Ahrens 	 * Destroy the deadlist.  Unless it's a clone, the
7723b2aab18SMatthew Ahrens 	 * deadlist should be empty.  (If it's a clone, it's
7733b2aab18SMatthew Ahrens 	 * safe to ignore the deadlist contents.)
7743b2aab18SMatthew Ahrens 	 */
7753b2aab18SMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
776c1379625SJustin T. Gibbs 	dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
7773b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
778c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
7793b2aab18SMatthew Ahrens 
7802acef22dSMatthew Ahrens 	objset_t *os;
7813b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
7823b2aab18SMatthew Ahrens 
7832acef22dSMatthew Ahrens 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
7843b2aab18SMatthew Ahrens 		old_synchronous_dataset_destroy(ds, tx);
7853b2aab18SMatthew Ahrens 	} else {
7863b2aab18SMatthew Ahrens 		/*
7873b2aab18SMatthew Ahrens 		 * Move the bptree into the pool's list of trees to
7883b2aab18SMatthew Ahrens 		 * clean up and update space accounting information.
7893b2aab18SMatthew Ahrens 		 */
7903b2aab18SMatthew Ahrens 		uint64_t used, comp, uncomp;
7913b2aab18SMatthew Ahrens 
7923b2aab18SMatthew Ahrens 		zil_destroy_sync(dmu_objset_zil(os), tx);
7933b2aab18SMatthew Ahrens 
7942acef22dSMatthew Ahrens 		if (!spa_feature_is_active(dp->dp_spa,
7952acef22dSMatthew Ahrens 		    SPA_FEATURE_ASYNC_DESTROY)) {
7964a923759SGeorge Wilson 			dsl_scan_t *scn = dp->dp_scan;
7972acef22dSMatthew Ahrens 			spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
7982acef22dSMatthew Ahrens 			    tx);
7993b2aab18SMatthew Ahrens 			dp->dp_bptree_obj = bptree_alloc(mos, tx);
8003b2aab18SMatthew Ahrens 			VERIFY0(zap_add(mos,
8013b2aab18SMatthew Ahrens 			    DMU_POOL_DIRECTORY_OBJECT,
8023b2aab18SMatthew Ahrens 			    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
8033b2aab18SMatthew Ahrens 			    &dp->dp_bptree_obj, tx));
8044a923759SGeorge Wilson 			ASSERT(!scn->scn_async_destroying);
8054a923759SGeorge Wilson 			scn->scn_async_destroying = B_TRUE;
8063b2aab18SMatthew Ahrens 		}
8073b2aab18SMatthew Ahrens 
808c1379625SJustin T. Gibbs 		used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes;
809c1379625SJustin T. Gibbs 		comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes;
810c1379625SJustin T. Gibbs 		uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes;
8113b2aab18SMatthew Ahrens 
8123b2aab18SMatthew Ahrens 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
813c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_unique_bytes == used);
8143b2aab18SMatthew Ahrens 
8153b2aab18SMatthew Ahrens 		bptree_add(mos, dp->dp_bptree_obj,
816c1379625SJustin T. Gibbs 		    &dsl_dataset_phys(ds)->ds_bp,
817c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_txg,
8183b2aab18SMatthew Ahrens 		    used, comp, uncomp, tx);
8193b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
8203b2aab18SMatthew Ahrens 		    -used, -comp, -uncomp, tx);
8213b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
8223b2aab18SMatthew Ahrens 		    used, comp, uncomp, tx);
8233b2aab18SMatthew Ahrens 	}
8243b2aab18SMatthew Ahrens 
8253b2aab18SMatthew Ahrens 	if (ds->ds_prev != NULL) {
8263b2aab18SMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
8273b2aab18SMatthew Ahrens 			VERIFY0(zap_remove_int(mos,
828c1379625SJustin T. Gibbs 			    dsl_dir_phys(ds->ds_prev->ds_dir)->dd_clones,
8293b2aab18SMatthew Ahrens 			    ds->ds_object, tx));
8303b2aab18SMatthew Ahrens 		}
8313b2aab18SMatthew Ahrens 		prevobj = ds->ds_prev->ds_object;
8323b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
8333b2aab18SMatthew Ahrens 		ds->ds_prev = NULL;
8343b2aab18SMatthew Ahrens 	}
8353b2aab18SMatthew Ahrens 
8363b2aab18SMatthew Ahrens 	/*
8373b2aab18SMatthew Ahrens 	 * This must be done after the dsl_traverse(), because it will
8383b2aab18SMatthew Ahrens 	 * re-open the objset.
8393b2aab18SMatthew Ahrens 	 */
8403b2aab18SMatthew Ahrens 	if (ds->ds_objset) {
8413b2aab18SMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
8423b2aab18SMatthew Ahrens 		ds->ds_objset = NULL;
8433b2aab18SMatthew Ahrens 	}
8443b2aab18SMatthew Ahrens 
8453b2aab18SMatthew Ahrens 	/* Erase the link in the dir */
8463b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
847c1379625SJustin T. Gibbs 	dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj = 0;
8483b2aab18SMatthew Ahrens 	ddobj = ds->ds_dir->dd_object;
849c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0);
850c1379625SJustin T. Gibbs 	VERIFY0(zap_destroy(mos,
851c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj, tx));
8523b2aab18SMatthew Ahrens 
85378f17100SMatthew Ahrens 	if (ds->ds_bookmarks != 0) {
854c1379625SJustin T. Gibbs 		VERIFY0(zap_destroy(mos, ds->ds_bookmarks, tx));
85578f17100SMatthew Ahrens 		spa_feature_decr(dp->dp_spa, SPA_FEATURE_BOOKMARKS, tx);
85678f17100SMatthew Ahrens 	}
85778f17100SMatthew Ahrens 
8583b2aab18SMatthew Ahrens 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
8593b2aab18SMatthew Ahrens 
860c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(ds)->ds_next_clones_obj);
861c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(ds)->ds_props_obj);
862c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(ds)->ds_userrefs_obj);
8633b2aab18SMatthew Ahrens 	dsl_dir_rele(ds->ds_dir, ds);
8643b2aab18SMatthew Ahrens 	ds->ds_dir = NULL;
8652acef22dSMatthew Ahrens 	dmu_object_free_zapified(mos, obj, tx);
8663b2aab18SMatthew Ahrens 
8673b2aab18SMatthew Ahrens 	dsl_dir_destroy_sync(ddobj, tx);
8683b2aab18SMatthew Ahrens 
8693b2aab18SMatthew Ahrens 	if (rmorigin) {
8703b2aab18SMatthew Ahrens 		dsl_dataset_t *prev;
8713b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
8723b2aab18SMatthew Ahrens 		dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
8733b2aab18SMatthew Ahrens 		dsl_dataset_rele(prev, FTAG);
8743b2aab18SMatthew Ahrens 	}
8753b2aab18SMatthew Ahrens }
8763b2aab18SMatthew Ahrens 
8773b2aab18SMatthew Ahrens static void
8783b2aab18SMatthew Ahrens dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
8793b2aab18SMatthew Ahrens {
8803b2aab18SMatthew Ahrens 	dsl_destroy_head_arg_t *ddha = arg;
8813b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8823b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
8833b2aab18SMatthew Ahrens 
8843b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
8853b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(ds, tx);
8863b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
8873b2aab18SMatthew Ahrens }
8883b2aab18SMatthew Ahrens 
8893b2aab18SMatthew Ahrens static void
8903b2aab18SMatthew Ahrens dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
8913b2aab18SMatthew Ahrens {
8923b2aab18SMatthew Ahrens 	dsl_destroy_head_arg_t *ddha = arg;
8933b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8943b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
8953b2aab18SMatthew Ahrens 
8963b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
8973b2aab18SMatthew Ahrens 
8983b2aab18SMatthew Ahrens 	/* Mark it as inconsistent on-disk, in case we crash */
8993b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
900c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
9013b2aab18SMatthew Ahrens 
9023b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "destroy begin", tx, "");
9033b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
9043b2aab18SMatthew Ahrens }
9053b2aab18SMatthew Ahrens 
9063b2aab18SMatthew Ahrens int
9073b2aab18SMatthew Ahrens dsl_destroy_head(const char *name)
9083b2aab18SMatthew Ahrens {
9093b2aab18SMatthew Ahrens 	dsl_destroy_head_arg_t ddha;
9103b2aab18SMatthew Ahrens 	int error;
9113b2aab18SMatthew Ahrens 	spa_t *spa;
9123b2aab18SMatthew Ahrens 	boolean_t isenabled;
9133b2aab18SMatthew Ahrens 
9143b2aab18SMatthew Ahrens #ifdef _KERNEL
9153b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
9163b2aab18SMatthew Ahrens #endif
9173b2aab18SMatthew Ahrens 
9183b2aab18SMatthew Ahrens 	error = spa_open(name, &spa, FTAG);
9193b2aab18SMatthew Ahrens 	if (error != 0)
9203b2aab18SMatthew Ahrens 		return (error);
9212acef22dSMatthew Ahrens 	isenabled = spa_feature_is_enabled(spa, SPA_FEATURE_ASYNC_DESTROY);
9223b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
9233b2aab18SMatthew Ahrens 
9243b2aab18SMatthew Ahrens 	ddha.ddha_name = name;
9253b2aab18SMatthew Ahrens 
9263b2aab18SMatthew Ahrens 	if (!isenabled) {
9273b2aab18SMatthew Ahrens 		objset_t *os;
9283b2aab18SMatthew Ahrens 
9293b2aab18SMatthew Ahrens 		error = dsl_sync_task(name, dsl_destroy_head_check,
9307d46dc6cSMatthew Ahrens 		    dsl_destroy_head_begin_sync, &ddha,
9317d46dc6cSMatthew Ahrens 		    0, ZFS_SPACE_CHECK_NONE);
9323b2aab18SMatthew Ahrens 		if (error != 0)
9333b2aab18SMatthew Ahrens 			return (error);
9343b2aab18SMatthew Ahrens 
9353b2aab18SMatthew Ahrens 		/*
9363b2aab18SMatthew Ahrens 		 * Head deletion is processed in one txg on old pools;
9373b2aab18SMatthew Ahrens 		 * remove the objects from open context so that the txg sync
9383b2aab18SMatthew Ahrens 		 * is not too long.
9393b2aab18SMatthew Ahrens 		 */
9403b2aab18SMatthew Ahrens 		error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
9413b2aab18SMatthew Ahrens 		if (error == 0) {
9423b2aab18SMatthew Ahrens 			uint64_t prev_snap_txg =
943c1379625SJustin T. Gibbs 			    dsl_dataset_phys(dmu_objset_ds(os))->
944c1379625SJustin T. Gibbs 			    ds_prev_snap_txg;
9453b2aab18SMatthew Ahrens 			for (uint64_t obj = 0; error == 0;
9463b2aab18SMatthew Ahrens 			    error = dmu_object_next(os, &obj, FALSE,
9473b2aab18SMatthew Ahrens 			    prev_snap_txg))
948713d6c20SMatthew Ahrens 				(void) dmu_free_long_object(os, obj);
9493b2aab18SMatthew Ahrens 			/* sync out all frees */
9503b2aab18SMatthew Ahrens 			txg_wait_synced(dmu_objset_pool(os), 0);
9513b2aab18SMatthew Ahrens 			dmu_objset_disown(os, FTAG);
9523b2aab18SMatthew Ahrens 		}
9533b2aab18SMatthew Ahrens 	}
9543b2aab18SMatthew Ahrens 
9553b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_destroy_head_check,
9567d46dc6cSMatthew Ahrens 	    dsl_destroy_head_sync, &ddha, 0, ZFS_SPACE_CHECK_NONE));
9573b2aab18SMatthew Ahrens }
9583b2aab18SMatthew Ahrens 
9593b2aab18SMatthew Ahrens /*
9603b2aab18SMatthew Ahrens  * Note, this function is used as the callback for dmu_objset_find().  We
9613b2aab18SMatthew Ahrens  * always return 0 so that we will continue to find and process
9623b2aab18SMatthew Ahrens  * inconsistent datasets, even if we encounter an error trying to
9633b2aab18SMatthew Ahrens  * process one of them.
9643b2aab18SMatthew Ahrens  */
9653b2aab18SMatthew Ahrens /* ARGSUSED */
9663b2aab18SMatthew Ahrens int
9673b2aab18SMatthew Ahrens dsl_destroy_inconsistent(const char *dsname, void *arg)
9683b2aab18SMatthew Ahrens {
9693b2aab18SMatthew Ahrens 	objset_t *os;
9703b2aab18SMatthew Ahrens 
9713b2aab18SMatthew Ahrens 	if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
9729c3fd121SMatthew Ahrens 		boolean_t need_destroy = DS_IS_INCONSISTENT(dmu_objset_ds(os));
9739c3fd121SMatthew Ahrens 
9749c3fd121SMatthew Ahrens 		/*
9759c3fd121SMatthew Ahrens 		 * If the dataset is inconsistent because a resumable receive
9769c3fd121SMatthew Ahrens 		 * has failed, then do not destroy it.
9779c3fd121SMatthew Ahrens 		 */
9789c3fd121SMatthew Ahrens 		if (dsl_dataset_has_resume_receive_state(dmu_objset_ds(os)))
9799c3fd121SMatthew Ahrens 			need_destroy = B_FALSE;
9809c3fd121SMatthew Ahrens 
9813b2aab18SMatthew Ahrens 		dmu_objset_rele(os, FTAG);
9829c3fd121SMatthew Ahrens 		if (need_destroy)
9833b2aab18SMatthew Ahrens 			(void) dsl_destroy_head(dsname);
9843b2aab18SMatthew Ahrens 	}
9853b2aab18SMatthew Ahrens 	return (0);
9863b2aab18SMatthew Ahrens }
987