dsl_destroy.c revision c166b69d29138aed7a415fe7cef698e54c6ae945
13b2aab1Matthew Ahrens/*
23b2aab1Matthew Ahrens * CDDL HEADER START
33b2aab1Matthew Ahrens *
43b2aab1Matthew Ahrens * The contents of this file are subject to the terms of the
53b2aab1Matthew Ahrens * Common Development and Distribution License (the "License").
63b2aab1Matthew Ahrens * You may not use this file except in compliance with the License.
73b2aab1Matthew Ahrens *
83b2aab1Matthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93b2aab1Matthew Ahrens * or http://www.opensolaris.org/os/licensing.
103b2aab1Matthew Ahrens * See the License for the specific language governing permissions
113b2aab1Matthew Ahrens * and limitations under the License.
123b2aab1Matthew Ahrens *
133b2aab1Matthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
143b2aab1Matthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153b2aab1Matthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
163b2aab1Matthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
173b2aab1Matthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
183b2aab1Matthew Ahrens *
193b2aab1Matthew Ahrens * CDDL HEADER END
203b2aab1Matthew Ahrens */
213b2aab1Matthew Ahrens/*
223b2aab1Matthew Ahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23ca0cc39Matthew Ahrens * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24a7a845eSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
25a2afb61Jerry Jelinek * Copyright (c) 2013 by Joyent, Inc. All rights reserved.
26c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
273b2aab1Matthew Ahrens */
283b2aab1Matthew Ahrens
293b2aab1Matthew Ahrens#include <sys/zfs_context.h>
303b2aab1Matthew Ahrens#include <sys/dsl_userhold.h>
313b2aab1Matthew Ahrens#include <sys/dsl_dataset.h>
323b2aab1Matthew Ahrens#include <sys/dsl_synctask.h>
333b2aab1Matthew Ahrens#include <sys/dmu_tx.h>
343b2aab1Matthew Ahrens#include <sys/dsl_pool.h>
353b2aab1Matthew Ahrens#include <sys/dsl_dir.h>
363b2aab1Matthew Ahrens#include <sys/dmu_traverse.h>
373b2aab1Matthew Ahrens#include <sys/dsl_scan.h>
383b2aab1Matthew Ahrens#include <sys/dmu_objset.h>
393b2aab1Matthew Ahrens#include <sys/zap.h>
403b2aab1Matthew Ahrens#include <sys/zfeature.h>
413b2aab1Matthew Ahrens#include <sys/zfs_ioctl.h>
423b2aab1Matthew Ahrens#include <sys/dsl_deleg.h>
432acef22Matthew Ahrens#include <sys/dmu_impl.h>
443b2aab1Matthew Ahrens
453b2aab1Matthew Ahrenstypedef struct dmu_snapshots_destroy_arg {
463b2aab1Matthew Ahrens	nvlist_t *dsda_snaps;
473b2aab1Matthew Ahrens	nvlist_t *dsda_successful_snaps;
483b2aab1Matthew Ahrens	boolean_t dsda_defer;
493b2aab1Matthew Ahrens	nvlist_t *dsda_errlist;
503b2aab1Matthew Ahrens} dmu_snapshots_destroy_arg_t;
513b2aab1Matthew Ahrens
5234f2f8cMatthew Ahrensint
533b2aab1Matthew Ahrensdsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
543b2aab1Matthew Ahrens{
55bc9014eJustin Gibbs	if (!ds->ds_is_snapshot)
56be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
573b2aab1Matthew Ahrens
583b2aab1Matthew Ahrens	if (dsl_dataset_long_held(ds))
59be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
603b2aab1Matthew Ahrens
613b2aab1Matthew Ahrens	/*
623b2aab1Matthew Ahrens	 * Only allow deferred destroy on pools that support it.
633b2aab1Matthew Ahrens	 * NOTE: deferred destroy is only supported on snapshots.
643b2aab1Matthew Ahrens	 */
653b2aab1Matthew Ahrens	if (defer) {
663b2aab1Matthew Ahrens		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
673b2aab1Matthew Ahrens		    SPA_VERSION_USERREFS)
68be6fd75Matthew Ahrens			return (SET_ERROR(ENOTSUP));
693b2aab1Matthew Ahrens		return (0);
703b2aab1Matthew Ahrens	}
713b2aab1Matthew Ahrens
723b2aab1Matthew Ahrens	/*
733b2aab1Matthew Ahrens	 * If this snapshot has an elevated user reference count,
743b2aab1Matthew Ahrens	 * we can't destroy it yet.
753b2aab1Matthew Ahrens	 */
763b2aab1Matthew Ahrens	if (ds->ds_userrefs > 0)
77be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
783b2aab1Matthew Ahrens
793b2aab1Matthew Ahrens	/*
803b2aab1Matthew Ahrens	 * Can't delete a branch point.
813b2aab1Matthew Ahrens	 */
82c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_num_children > 1)
83be6fd75Matthew Ahrens		return (SET_ERROR(EEXIST));
843b2aab1Matthew Ahrens
853b2aab1Matthew Ahrens	return (0);
863b2aab1Matthew Ahrens}
873b2aab1Matthew Ahrens
883b2aab1Matthew Ahrensstatic int
893b2aab1Matthew Ahrensdsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
903b2aab1Matthew Ahrens{
913b2aab1Matthew Ahrens	dmu_snapshots_destroy_arg_t *dsda = arg;
923b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
933b2aab1Matthew Ahrens	nvpair_t *pair;
943b2aab1Matthew Ahrens	int error = 0;
953b2aab1Matthew Ahrens
963b2aab1Matthew Ahrens	if (!dmu_tx_is_syncing(tx))
973b2aab1Matthew Ahrens		return (0);
983b2aab1Matthew Ahrens
993b2aab1Matthew Ahrens	for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
1003b2aab1Matthew Ahrens	    pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
1013b2aab1Matthew Ahrens		dsl_dataset_t *ds;
1023b2aab1Matthew Ahrens
1033b2aab1Matthew Ahrens		error = dsl_dataset_hold(dp, nvpair_name(pair),
1043b2aab1Matthew Ahrens		    FTAG, &ds);
1053b2aab1Matthew Ahrens
1063b2aab1Matthew Ahrens		/*
1073b2aab1Matthew Ahrens		 * If the snapshot does not exist, silently ignore it
1083b2aab1Matthew Ahrens		 * (it's "already destroyed").
1093b2aab1Matthew Ahrens		 */
1103b2aab1Matthew Ahrens		if (error == ENOENT)
1113b2aab1Matthew Ahrens			continue;
1123b2aab1Matthew Ahrens
1133b2aab1Matthew Ahrens		if (error == 0) {
1143b2aab1Matthew Ahrens			error = dsl_destroy_snapshot_check_impl(ds,
1153b2aab1Matthew Ahrens			    dsda->dsda_defer);
1163b2aab1Matthew Ahrens			dsl_dataset_rele(ds, FTAG);
1173b2aab1Matthew Ahrens		}
1183b2aab1Matthew Ahrens
1193b2aab1Matthew Ahrens		if (error == 0) {
1203b2aab1Matthew Ahrens			fnvlist_add_boolean(dsda->dsda_successful_snaps,
1213b2aab1Matthew Ahrens			    nvpair_name(pair));
1223b2aab1Matthew Ahrens		} else {
1233b2aab1Matthew Ahrens			fnvlist_add_int32(dsda->dsda_errlist,
1243b2aab1Matthew Ahrens			    nvpair_name(pair), error);
1253b2aab1Matthew Ahrens		}
1263b2aab1Matthew Ahrens	}
1273b2aab1Matthew Ahrens
1283b2aab1Matthew Ahrens	pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
1293b2aab1Matthew Ahrens	if (pair != NULL)
1303b2aab1Matthew Ahrens		return (fnvpair_value_int32(pair));
131a7a845eSteven Hartland
1323b2aab1Matthew Ahrens	return (0);
1333b2aab1Matthew Ahrens}
1343b2aab1Matthew Ahrens
1353b2aab1Matthew Ahrensstruct process_old_arg {
1363b2aab1Matthew Ahrens	dsl_dataset_t *ds;
1373b2aab1Matthew Ahrens	dsl_dataset_t *ds_prev;
1383b2aab1Matthew Ahrens	boolean_t after_branch_point;
1393b2aab1Matthew Ahrens	zio_t *pio;
1403b2aab1Matthew Ahrens	uint64_t used, comp, uncomp;
1413b2aab1Matthew Ahrens};
1423b2aab1Matthew Ahrens
1433b2aab1Matthew Ahrensstatic int
1443b2aab1Matthew Ahrensprocess_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1453b2aab1Matthew Ahrens{
1463b2aab1Matthew Ahrens	struct process_old_arg *poa = arg;
1473b2aab1Matthew Ahrens	dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
1483b2aab1Matthew Ahrens
14943466aaMax Grossman	ASSERT(!BP_IS_HOLE(bp));
15043466aaMax Grossman
151c137962Justin T. Gibbs	if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
1523b2aab1Matthew Ahrens		dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
1533b2aab1Matthew Ahrens		if (poa->ds_prev && !poa->after_branch_point &&
1543b2aab1Matthew Ahrens		    bp->blk_birth >
155c137962Justin T. Gibbs		    dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
156c137962Justin T. Gibbs			dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
1573b2aab1Matthew Ahrens			    bp_get_dsize_sync(dp->dp_spa, bp);
1583b2aab1Matthew Ahrens		}
1593b2aab1Matthew Ahrens	} else {
1603b2aab1Matthew Ahrens		poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
1613b2aab1Matthew Ahrens		poa->comp += BP_GET_PSIZE(bp);
1623b2aab1Matthew Ahrens		poa->uncomp += BP_GET_UCSIZE(bp);
1633b2aab1Matthew Ahrens		dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
1643b2aab1Matthew Ahrens	}
1653b2aab1Matthew Ahrens	return (0);
1663b2aab1Matthew Ahrens}
1673b2aab1Matthew Ahrens
1683b2aab1Matthew Ahrensstatic void
1693b2aab1Matthew Ahrensprocess_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
1703b2aab1Matthew Ahrens    dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
1713b2aab1Matthew Ahrens{
1723b2aab1Matthew Ahrens	struct process_old_arg poa = { 0 };
1733b2aab1Matthew Ahrens	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1743b2aab1Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
1753b2aab1Matthew Ahrens	uint64_t deadlist_obj;
1763b2aab1Matthew Ahrens
1773b2aab1Matthew Ahrens	ASSERT(ds->ds_deadlist.dl_oldfmt);
1783b2aab1Matthew Ahrens	ASSERT(ds_next->ds_deadlist.dl_oldfmt);
1793b2aab1Matthew Ahrens
1803b2aab1Matthew Ahrens	poa.ds = ds;
1813b2aab1Matthew Ahrens	poa.ds_prev = ds_prev;
1823b2aab1Matthew Ahrens	poa.after_branch_point = after_branch_point;
1833b2aab1Matthew Ahrens	poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1843b2aab1Matthew Ahrens	VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
1853b2aab1Matthew Ahrens	    process_old_cb, &poa, tx));
1863b2aab1Matthew Ahrens	VERIFY0(zio_wait(poa.pio));
187c137962Justin T. Gibbs	ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
1883b2aab1Matthew Ahrens
1893b2aab1Matthew Ahrens	/* change snapused */
1903b2aab1Matthew Ahrens	dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1913b2aab1Matthew Ahrens	    -poa.used, -poa.comp, -poa.uncomp, tx);
1923b2aab1Matthew Ahrens
1933b2aab1Matthew Ahrens	/* swap next's deadlist to our deadlist */
1943b2aab1Matthew Ahrens	dsl_deadlist_close(&ds->ds_deadlist);
1953b2aab1Matthew Ahrens	dsl_deadlist_close(&ds_next->ds_deadlist);
196c137962Justin T. Gibbs	deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
197c137962Justin T. Gibbs	dsl_dataset_phys(ds)->ds_deadlist_obj =
198c137962Justin T. Gibbs	    dsl_dataset_phys(ds_next)->ds_deadlist_obj;
199c137962Justin T. Gibbs	dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj;
200c137962Justin T. Gibbs	dsl_deadlist_open(&ds->ds_deadlist, mos,
201c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_deadlist_obj);
2023b2aab1Matthew Ahrens	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
203c137962Justin T. Gibbs	    dsl_dataset_phys(ds_next)->ds_deadlist_obj);
2043b2aab1Matthew Ahrens}
2053b2aab1Matthew Ahrens
2063b2aab1Matthew Ahrensstatic void
2073b2aab1Matthew Ahrensdsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
2083b2aab1Matthew Ahrens{
2093b2aab1Matthew Ahrens	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2103b2aab1Matthew Ahrens	zap_cursor_t zc;
2113b2aab1Matthew Ahrens	zap_attribute_t za;
2123b2aab1Matthew Ahrens
2133b2aab1Matthew Ahrens	/*
2143b2aab1Matthew Ahrens	 * If it is the old version, dd_clones doesn't exist so we can't
2153b2aab1Matthew Ahrens	 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
2163b2aab1Matthew Ahrens	 * doesn't matter.
2173b2aab1Matthew Ahrens	 */
218c137962Justin T. Gibbs	if (dsl_dir_phys(ds->ds_dir)->dd_clones == 0)
2193b2aab1Matthew Ahrens		return;
2203b2aab1Matthew Ahrens
221c137962Justin T. Gibbs	for (zap_cursor_init(&zc, mos, dsl_dir_phys(ds->ds_dir)->dd_clones);
2223b2aab1Matthew Ahrens	    zap_cursor_retrieve(&zc, &za) == 0;
2233b2aab1Matthew Ahrens	    zap_cursor_advance(&zc)) {
2243b2aab1Matthew Ahrens		dsl_dataset_t *clone;
2253b2aab1Matthew Ahrens
2263b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
2273b2aab1Matthew Ahrens		    za.za_first_integer, FTAG, &clone));
2283b2aab1Matthew Ahrens		if (clone->ds_dir->dd_origin_txg > mintxg) {
2293b2aab1Matthew Ahrens			dsl_deadlist_remove_key(&clone->ds_deadlist,
2303b2aab1Matthew Ahrens			    mintxg, tx);
2313b2aab1Matthew Ahrens			dsl_dataset_remove_clones_key(clone, mintxg, tx);
2323b2aab1Matthew Ahrens		}
2333b2aab1Matthew Ahrens		dsl_dataset_rele(clone, FTAG);
2343b2aab1Matthew Ahrens	}
2353b2aab1Matthew Ahrens	zap_cursor_fini(&zc);
2363b2aab1Matthew Ahrens}
2373b2aab1Matthew Ahrens
2383b2aab1Matthew Ahrensvoid
2393b2aab1Matthew Ahrensdsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
2403b2aab1Matthew Ahrens{
2413b2aab1Matthew Ahrens	int err;
2423b2aab1Matthew Ahrens	int after_branch_point = FALSE;
2433b2aab1Matthew Ahrens	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2443b2aab1Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
2453b2aab1Matthew Ahrens	dsl_dataset_t *ds_prev = NULL;
2463b2aab1Matthew Ahrens	uint64_t obj;
2473b2aab1Matthew Ahrens
2483b2aab1Matthew Ahrens	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
249c166b69Paul Dagnelie	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
250c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
251c166b69Paul Dagnelie	rrw_exit(&ds->ds_bp_rwlock, FTAG);
2523b2aab1Matthew Ahrens	ASSERT(refcount_is_zero(&ds->ds_longholds));
2533b2aab1Matthew Ahrens
2543b2aab1Matthew Ahrens	if (defer &&
255c137962Justin T. Gibbs	    (ds->ds_userrefs > 0 ||
256c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_num_children > 1)) {
2573b2aab1Matthew Ahrens		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
2583b2aab1Matthew Ahrens		dmu_buf_will_dirty(ds->ds_dbuf, tx);
259c137962Justin T. Gibbs		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
2603b2aab1Matthew Ahrens		spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
2613b2aab1Matthew Ahrens		return;
2623b2aab1Matthew Ahrens	}
2633b2aab1Matthew Ahrens
264c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
2653b2aab1Matthew Ahrens
2663b2aab1Matthew Ahrens	/* We need to log before removing it from the namespace. */
2673b2aab1Matthew Ahrens	spa_history_log_internal_ds(ds, "destroy", tx, "");
2683b2aab1Matthew Ahrens
2693b2aab1Matthew Ahrens	dsl_scan_ds_destroyed(ds, tx);
2703b2aab1Matthew Ahrens
2713b2aab1Matthew Ahrens	obj = ds->ds_object;
2723b2aab1Matthew Ahrens
273ca0cc39Matthew Ahrens	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
274ca0cc39Matthew Ahrens		if (ds->ds_feature_inuse[f]) {
275ca0cc39Matthew Ahrens			dsl_dataset_deactivate_feature(obj, f, tx);
276ca0cc39Matthew Ahrens			ds->ds_feature_inuse[f] = B_FALSE;
277ca0cc39Matthew Ahrens		}
278b515258Matthew Ahrens	}
279c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2803b2aab1Matthew Ahrens		ASSERT3P(ds->ds_prev, ==, NULL);
2813b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(dp,
282c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
2833b2aab1Matthew Ahrens		after_branch_point =
284c137962Justin T. Gibbs		    (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
2853b2aab1Matthew Ahrens
2863b2aab1Matthew Ahrens		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
2873b2aab1Matthew Ahrens		if (after_branch_point &&
288c137962Justin T. Gibbs		    dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
2893b2aab1Matthew Ahrens			dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
290c137962Justin T. Gibbs			if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
2913b2aab1Matthew Ahrens				VERIFY0(zap_add_int(mos,
292c137962Justin T. Gibbs				    dsl_dataset_phys(ds_prev)->
293c137962Justin T. Gibbs				    ds_next_clones_obj,
294c137962Justin T. Gibbs				    dsl_dataset_phys(ds)->ds_next_snap_obj,
295c137962Justin T. Gibbs				    tx));
2963b2aab1Matthew Ahrens			}
2973b2aab1Matthew Ahrens		}
2983b2aab1Matthew Ahrens		if (!after_branch_point) {
299c137962Justin T. Gibbs			dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
300c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_next_snap_obj;
3013b2aab1Matthew Ahrens		}
3023b2aab1Matthew Ahrens	}
3033b2aab1Matthew Ahrens
3043b2aab1Matthew Ahrens	dsl_dataset_t *ds_next;
3053b2aab1Matthew Ahrens	uint64_t old_unique;
3063b2aab1Matthew Ahrens	uint64_t used = 0, comp = 0, uncomp = 0;
3073b2aab1Matthew Ahrens
3083b2aab1Matthew Ahrens	VERIFY0(dsl_dataset_hold_obj(dp,
309c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
310c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
3113b2aab1Matthew Ahrens
312c137962Justin T. Gibbs	old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
3133b2aab1Matthew Ahrens
3143b2aab1Matthew Ahrens	dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
315c137962Justin T. Gibbs	dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
316c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_prev_snap_obj;
317c137962Justin T. Gibbs	dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
318c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
319c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
320c137962Justin T. Gibbs	    ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
3213b2aab1Matthew Ahrens
3223b2aab1Matthew Ahrens	if (ds_next->ds_deadlist.dl_oldfmt) {
3233b2aab1Matthew Ahrens		process_old_deadlist(ds, ds_prev, ds_next,
3243b2aab1Matthew Ahrens		    after_branch_point, tx);
3253b2aab1Matthew Ahrens	} else {
3263b2aab1Matthew Ahrens		/* Adjust prev's unique space. */
3273b2aab1Matthew Ahrens		if (ds_prev && !after_branch_point) {
3283b2aab1Matthew Ahrens			dsl_deadlist_space_range(&ds_next->ds_deadlist,
329c137962Justin T. Gibbs			    dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
330c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_prev_snap_txg,
3313b2aab1Matthew Ahrens			    &used, &comp, &uncomp);
332c137962Justin T. Gibbs			dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
3333b2aab1Matthew Ahrens		}
3343b2aab1Matthew Ahrens
3353b2aab1Matthew Ahrens		/* Adjust snapused. */
3363b2aab1Matthew Ahrens		dsl_deadlist_space_range(&ds_next->ds_deadlist,
337c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
3383b2aab1Matthew Ahrens		    &used, &comp, &uncomp);
3393b2aab1Matthew Ahrens		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
3403b2aab1Matthew Ahrens		    -used, -comp, -uncomp, tx);
3413b2aab1Matthew Ahrens
3423b2aab1Matthew Ahrens		/* Move blocks to be freed to pool's free list. */
3433b2aab1Matthew Ahrens		dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
344c137962Justin T. Gibbs		    &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
3453b2aab1Matthew Ahrens		    tx);
3463b2aab1Matthew Ahrens		dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
3473b2aab1Matthew Ahrens		    DD_USED_HEAD, used, comp, uncomp, tx);
3483b2aab1Matthew Ahrens
3493b2aab1Matthew Ahrens		/* Merge our deadlist into next's and free it. */
3503b2aab1Matthew Ahrens		dsl_deadlist_merge(&ds_next->ds_deadlist,
351c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3523b2aab1Matthew Ahrens	}
3533b2aab1Matthew Ahrens	dsl_deadlist_close(&ds->ds_deadlist);
354c137962Justin T. Gibbs	dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3553b2aab1Matthew Ahrens	dmu_buf_will_dirty(ds->ds_dbuf, tx);
356c137962Justin T. Gibbs	dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
3573b2aab1Matthew Ahrens
3583b2aab1Matthew Ahrens	/* Collapse range in clone heads */
3593b2aab1Matthew Ahrens	dsl_dataset_remove_clones_key(ds,
360c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_creation_txg, tx);
3613b2aab1Matthew Ahrens
362bc9014eJustin Gibbs	if (ds_next->ds_is_snapshot) {
3633b2aab1Matthew Ahrens		dsl_dataset_t *ds_nextnext;
3643b2aab1Matthew Ahrens
3653b2aab1Matthew Ahrens		/*
3663b2aab1Matthew Ahrens		 * Update next's unique to include blocks which
3673b2aab1Matthew Ahrens		 * were previously shared by only this snapshot
3683b2aab1Matthew Ahrens		 * and it.  Those blocks will be born after the
3693b2aab1Matthew Ahrens		 * prev snap and before this snap, and will have
3703b2aab1Matthew Ahrens		 * died after the next snap and before the one
3713b2aab1Matthew Ahrens		 * after that (ie. be on the snap after next's
3723b2aab1Matthew Ahrens		 * deadlist).
3733b2aab1Matthew Ahrens		 */
3743b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(dp,
375c137962Justin T. Gibbs		    dsl_dataset_phys(ds_next)->ds_next_snap_obj,
376c137962Justin T. Gibbs		    FTAG, &ds_nextnext));
3773b2aab1Matthew Ahrens		dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
378c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_prev_snap_txg,
379c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_creation_txg,
3803b2aab1Matthew Ahrens		    &used, &comp, &uncomp);
381c137962Justin T. Gibbs		dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
3823b2aab1Matthew Ahrens		dsl_dataset_rele(ds_nextnext, FTAG);
3833b2aab1Matthew Ahrens		ASSERT3P(ds_next->ds_prev, ==, NULL);
3843b2aab1Matthew Ahrens
3853b2aab1Matthew Ahrens		/* Collapse range in this head. */
3863b2aab1Matthew Ahrens		dsl_dataset_t *hds;
3873b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(dp,
388c137962Justin T. Gibbs		    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
3893b2aab1Matthew Ahrens		dsl_deadlist_remove_key(&hds->ds_deadlist,
390c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_creation_txg, tx);
3913b2aab1Matthew Ahrens		dsl_dataset_rele(hds, FTAG);
3923b2aab1Matthew Ahrens
3933b2aab1Matthew Ahrens	} else {
3943b2aab1Matthew Ahrens		ASSERT3P(ds_next->ds_prev, ==, ds);
3953b2aab1Matthew Ahrens		dsl_dataset_rele(ds_next->ds_prev, ds_next);
3963b2aab1Matthew Ahrens		ds_next->ds_prev = NULL;
3973b2aab1Matthew Ahrens		if (ds_prev) {
3983b2aab1Matthew Ahrens			VERIFY0(dsl_dataset_hold_obj(dp,
399c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_prev_snap_obj,
4003b2aab1Matthew Ahrens			    ds_next, &ds_next->ds_prev));
4013b2aab1Matthew Ahrens		}
4023b2aab1Matthew Ahrens
4033b2aab1Matthew Ahrens		dsl_dataset_recalc_head_uniq(ds_next);
4043b2aab1Matthew Ahrens
4053b2aab1Matthew Ahrens		/*
4063b2aab1Matthew Ahrens		 * Reduce the amount of our unconsumed refreservation
4073b2aab1Matthew Ahrens		 * being charged to our parent by the amount of
4083b2aab1Matthew Ahrens		 * new unique data we have gained.
4093b2aab1Matthew Ahrens		 */
4103b2aab1Matthew Ahrens		if (old_unique < ds_next->ds_reserved) {
4113b2aab1Matthew Ahrens			int64_t mrsdelta;
4123b2aab1Matthew Ahrens			uint64_t new_unique =
413c137962Justin T. Gibbs			    dsl_dataset_phys(ds_next)->ds_unique_bytes;
4143b2aab1Matthew Ahrens
4153b2aab1Matthew Ahrens			ASSERT(old_unique <= new_unique);
4163b2aab1Matthew Ahrens			mrsdelta = MIN(new_unique - old_unique,
4173b2aab1Matthew Ahrens			    ds_next->ds_reserved - old_unique);
4183b2aab1Matthew Ahrens			dsl_dir_diduse_space(ds->ds_dir,
4193b2aab1Matthew Ahrens			    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
4203b2aab1Matthew Ahrens		}
4213b2aab1Matthew Ahrens	}
4223b2aab1Matthew Ahrens	dsl_dataset_rele(ds_next, FTAG);
4233b2aab1Matthew Ahrens
4243b2aab1Matthew Ahrens	/*
4253b2aab1Matthew Ahrens	 * This must be done after the dsl_traverse(), because it will
4263b2aab1Matthew Ahrens	 * re-open the objset.
4273b2aab1Matthew Ahrens	 */
4283b2aab1Matthew Ahrens	if (ds->ds_objset) {
4293b2aab1Matthew Ahrens		dmu_objset_evict(ds->ds_objset);
4303b2aab1Matthew Ahrens		ds->ds_objset = NULL;
4313b2aab1Matthew Ahrens	}
4323b2aab1Matthew Ahrens
4333b2aab1Matthew Ahrens	/* remove from snapshot namespace */
4343b2aab1Matthew Ahrens	dsl_dataset_t *ds_head;
435c137962Justin T. Gibbs	ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
4363b2aab1Matthew Ahrens	VERIFY0(dsl_dataset_hold_obj(dp,
437c137962Justin T. Gibbs	    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
4383b2aab1Matthew Ahrens	VERIFY0(dsl_dataset_get_snapname(ds));
4393b2aab1Matthew Ahrens#ifdef ZFS_DEBUG
4403b2aab1Matthew Ahrens	{
4413b2aab1Matthew Ahrens		uint64_t val;
4423b2aab1Matthew Ahrens
4433b2aab1Matthew Ahrens		err = dsl_dataset_snap_lookup(ds_head,
4443b2aab1Matthew Ahrens		    ds->ds_snapname, &val);
4453b2aab1Matthew Ahrens		ASSERT0(err);
4463b2aab1Matthew Ahrens		ASSERT3U(val, ==, obj);
4473b2aab1Matthew Ahrens	}
4483b2aab1Matthew Ahrens#endif
449a2afb61Jerry Jelinek	VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
4503b2aab1Matthew Ahrens	dsl_dataset_rele(ds_head, FTAG);
4513b2aab1Matthew Ahrens
4523b2aab1Matthew Ahrens	if (ds_prev != NULL)
4533b2aab1Matthew Ahrens		dsl_dataset_rele(ds_prev, FTAG);
4543b2aab1Matthew Ahrens
4553b2aab1Matthew Ahrens	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
4563b2aab1Matthew Ahrens
457c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
4583b2aab1Matthew Ahrens		uint64_t count;
4593b2aab1Matthew Ahrens		ASSERT0(zap_count(mos,
460c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
461c137962Justin T. Gibbs		    count == 0);
4623b2aab1Matthew Ahrens		VERIFY0(dmu_object_free(mos,
463c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
4643b2aab1Matthew Ahrens	}
465c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_props_obj != 0)
466c137962Justin T. Gibbs		VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
467c137962Justin T. Gibbs		    tx));
468c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
469c137962Justin T. Gibbs		VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
470c137962Justin T. Gibbs		    tx));
4713b2aab1Matthew Ahrens	dsl_dir_rele(ds->ds_dir, ds);
4723b2aab1Matthew Ahrens	ds->ds_dir = NULL;
4732acef22Matthew Ahrens	dmu_object_free_zapified(mos, obj, tx);
4743b2aab1Matthew Ahrens}
4753b2aab1Matthew Ahrens
4763b2aab1Matthew Ahrensstatic void
4773b2aab1Matthew Ahrensdsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
4783b2aab1Matthew Ahrens{
4793b2aab1Matthew Ahrens	dmu_snapshots_destroy_arg_t *dsda = arg;
4803b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
4813b2aab1Matthew Ahrens	nvpair_t *pair;
4823b2aab1Matthew Ahrens
4833b2aab1Matthew Ahrens	for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
4843b2aab1Matthew Ahrens	    pair != NULL;
4853b2aab1Matthew Ahrens	    pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
4863b2aab1Matthew Ahrens		dsl_dataset_t *ds;
4873b2aab1Matthew Ahrens
4883b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
4893b2aab1Matthew Ahrens
4903b2aab1Matthew Ahrens		dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
4913b2aab1Matthew Ahrens		dsl_dataset_rele(ds, FTAG);
4923b2aab1Matthew Ahrens	}
4933b2aab1Matthew Ahrens}
4943b2aab1Matthew Ahrens
4953b2aab1Matthew Ahrens/*
4963b2aab1Matthew Ahrens * The semantics of this function are described in the comment above
4973b2aab1Matthew Ahrens * lzc_destroy_snaps().  To summarize:
4983b2aab1Matthew Ahrens *
4993b2aab1Matthew Ahrens * The snapshots must all be in the same pool.
5003b2aab1Matthew Ahrens *
5013b2aab1Matthew Ahrens * Snapshots that don't exist will be silently ignored (considered to be
5023b2aab1Matthew Ahrens * "already deleted").
5033b2aab1Matthew Ahrens *
5043b2aab1Matthew Ahrens * On success, all snaps will be destroyed and this will return 0.
5053b2aab1Matthew Ahrens * On failure, no snaps will be destroyed, the errlist will be filled in,
5063b2aab1Matthew Ahrens * and this will return an errno.
5073b2aab1Matthew Ahrens */
5083b2aab1Matthew Ahrensint
5093b2aab1Matthew Ahrensdsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
5103b2aab1Matthew Ahrens    nvlist_t *errlist)
5113b2aab1Matthew Ahrens{
5123b2aab1Matthew Ahrens	dmu_snapshots_destroy_arg_t dsda;
5133b2aab1Matthew Ahrens	int error;
5143b2aab1Matthew Ahrens	nvpair_t *pair;
5153b2aab1Matthew Ahrens
5163b2aab1Matthew Ahrens	pair = nvlist_next_nvpair(snaps, NULL);
5173b2aab1Matthew Ahrens	if (pair == NULL)
5183b2aab1Matthew Ahrens		return (0);
5193b2aab1Matthew Ahrens
5203b2aab1Matthew Ahrens	dsda.dsda_snaps = snaps;
5213b2aab1Matthew Ahrens	dsda.dsda_successful_snaps = fnvlist_alloc();
5223b2aab1Matthew Ahrens	dsda.dsda_defer = defer;
5233b2aab1Matthew Ahrens	dsda.dsda_errlist = errlist;
5243b2aab1Matthew Ahrens
5253b2aab1Matthew Ahrens	error = dsl_sync_task(nvpair_name(pair),
5263b2aab1Matthew Ahrens	    dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
5277d46dc6Matthew Ahrens	    &dsda, 0, ZFS_SPACE_CHECK_NONE);
5283b2aab1Matthew Ahrens	fnvlist_free(dsda.dsda_successful_snaps);
5293b2aab1Matthew Ahrens
5303b2aab1Matthew Ahrens	return (error);
5313b2aab1Matthew Ahrens}
5323b2aab1Matthew Ahrens
5333b2aab1Matthew Ahrensint
5343b2aab1Matthew Ahrensdsl_destroy_snapshot(const char *name, boolean_t defer)
5353b2aab1Matthew Ahrens{
5363b2aab1Matthew Ahrens	int error;
5373b2aab1Matthew Ahrens	nvlist_t *nvl = fnvlist_alloc();
5383b2aab1Matthew Ahrens	nvlist_t *errlist = fnvlist_alloc();
5393b2aab1Matthew Ahrens
5403b2aab1Matthew Ahrens	fnvlist_add_boolean(nvl, name);
5413b2aab1Matthew Ahrens	error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
5423b2aab1Matthew Ahrens	fnvlist_free(errlist);
5433b2aab1Matthew Ahrens	fnvlist_free(nvl);
5443b2aab1Matthew Ahrens	return (error);
5453b2aab1Matthew Ahrens}
5463b2aab1Matthew Ahrens
5473b2aab1Matthew Ahrensstruct killarg {
5483b2aab1Matthew Ahrens	dsl_dataset_t *ds;
5493b2aab1Matthew Ahrens	dmu_tx_t *tx;
5503b2aab1Matthew Ahrens};
5513b2aab1Matthew Ahrens
5523b2aab1Matthew Ahrens/* ARGSUSED */
5533b2aab1Matthew Ahrensstatic int
5543b2aab1Matthew Ahrenskill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5557802d7bMatthew Ahrens    const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
5563b2aab1Matthew Ahrens{
5573b2aab1Matthew Ahrens	struct killarg *ka = arg;
5583b2aab1Matthew Ahrens	dmu_tx_t *tx = ka->tx;
5593b2aab1Matthew Ahrens
560a2cdcddPaul Dagnelie	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
5613b2aab1Matthew Ahrens		return (0);
5623b2aab1Matthew Ahrens
5633b2aab1Matthew Ahrens	if (zb->zb_level == ZB_ZIL_LEVEL) {
5643b2aab1Matthew Ahrens		ASSERT(zilog != NULL);
5653b2aab1Matthew Ahrens		/*
5663b2aab1Matthew Ahrens		 * It's a block in the intent log.  It has no
5673b2aab1Matthew Ahrens		 * accounting, so just free it.
5683b2aab1Matthew Ahrens		 */
5693b2aab1Matthew Ahrens		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
5703b2aab1Matthew Ahrens	} else {
5713b2aab1Matthew Ahrens		ASSERT(zilog == NULL);
572c137962Justin T. Gibbs		ASSERT3U(bp->blk_birth, >,
573c137962Justin T. Gibbs		    dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
5743b2aab1Matthew Ahrens		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
5753b2aab1Matthew Ahrens	}
5763b2aab1Matthew Ahrens
5773b2aab1Matthew Ahrens	return (0);
5783b2aab1Matthew Ahrens}
5793b2aab1Matthew Ahrens
5803b2aab1Matthew Ahrensstatic void
5813b2aab1Matthew Ahrensold_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
5823b2aab1Matthew Ahrens{
5833b2aab1Matthew Ahrens	struct killarg ka;
5843b2aab1Matthew Ahrens
5853b2aab1Matthew Ahrens	/*
5863b2aab1Matthew Ahrens	 * Free everything that we point to (that's born after
5873b2aab1Matthew Ahrens	 * the previous snapshot, if we are a clone)
5883b2aab1Matthew Ahrens	 *
5893b2aab1Matthew Ahrens	 * NB: this should be very quick, because we already
5903b2aab1Matthew Ahrens	 * freed all the objects in open context.
5913b2aab1Matthew Ahrens	 */
5923b2aab1Matthew Ahrens	ka.ds = ds;
5933b2aab1Matthew Ahrens	ka.tx = tx;
5943b2aab1Matthew Ahrens	VERIFY0(traverse_dataset(ds,
595c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST,
5963b2aab1Matthew Ahrens	    kill_blkptr, &ka));
597c137962Justin T. Gibbs	ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
598c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_unique_bytes == 0);
5993b2aab1Matthew Ahrens}
6003b2aab1Matthew Ahrens
6013b2aab1Matthew Ahrenstypedef struct dsl_destroy_head_arg {
6023b2aab1Matthew Ahrens	const char *ddha_name;
6033b2aab1Matthew Ahrens} dsl_destroy_head_arg_t;
6043b2aab1Matthew Ahrens
6053b2aab1Matthew Ahrensint
6063b2aab1Matthew Ahrensdsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
6073b2aab1Matthew Ahrens{
6083b2aab1Matthew Ahrens	int error;
6093b2aab1Matthew Ahrens	uint64_t count;
6103b2aab1Matthew Ahrens	objset_t *mos;
6113b2aab1Matthew Ahrens
612bc9014eJustin Gibbs	ASSERT(!ds->ds_is_snapshot);
613bc9014eJustin Gibbs	if (ds->ds_is_snapshot)
614be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
6153b2aab1Matthew Ahrens
6163b2aab1Matthew Ahrens	if (refcount_count(&ds->ds_longholds) != expected_holds)
617be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
6183b2aab1Matthew Ahrens
6193b2aab1Matthew Ahrens	mos = ds->ds_dir->dd_pool->dp_meta_objset;
6203b2aab1Matthew Ahrens
6213b2aab1Matthew Ahrens	/*
6223b2aab1Matthew Ahrens	 * Can't delete a head dataset if there are snapshots of it.
6233b2aab1Matthew Ahrens	 * (Except if the only snapshots are from the branch we cloned
6243b2aab1Matthew Ahrens	 * from.)
6253b2aab1Matthew Ahrens	 */
6263b2aab1Matthew Ahrens	if (ds->ds_prev != NULL &&
627c137962Justin T. Gibbs	    dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
628be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
6293b2aab1Matthew Ahrens
6303b2aab1Matthew Ahrens	/*
6313b2aab1Matthew Ahrens	 * Can't delete if there are children of this fs.
6323b2aab1Matthew Ahrens	 */
6333b2aab1Matthew Ahrens	error = zap_count(mos,
634c137962Justin T. Gibbs	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
6353b2aab1Matthew Ahrens	if (error != 0)
6363b2aab1Matthew Ahrens		return (error);
6373b2aab1Matthew Ahrens	if (count != 0)
638be6fd75Matthew Ahrens		return (SET_ERROR(EEXIST));
6393b2aab1Matthew Ahrens
6403b2aab1Matthew Ahrens	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
641c137962Justin T. Gibbs	    dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
6423b2aab1Matthew Ahrens	    ds->ds_prev->ds_userrefs == 0) {
6433b2aab1Matthew Ahrens		/* We need to remove the origin snapshot as well. */
6443b2aab1Matthew Ahrens		if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
645be6fd75Matthew Ahrens			return (SET_ERROR(EBUSY));
6463b2aab1Matthew Ahrens	}
6473b2aab1Matthew Ahrens	return (0);
6483b2aab1Matthew Ahrens}
6493b2aab1Matthew Ahrens
6503b2aab1Matthew Ahrensstatic int
6513b2aab1Matthew Ahrensdsl_destroy_head_check(void *arg, dmu_tx_t *tx)
6523b2aab1Matthew Ahrens{
6533b2aab1Matthew Ahrens	dsl_destroy_head_arg_t *ddha = arg;
6543b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
6553b2aab1Matthew Ahrens	dsl_dataset_t *ds;
6563b2aab1Matthew Ahrens	int error;
6573b2aab1Matthew Ahrens
6583b2aab1Matthew Ahrens	error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
6593b2aab1Matthew Ahrens	if (error != 0)
6603b2aab1Matthew Ahrens		return (error);
6613b2aab1Matthew Ahrens
6623b2aab1Matthew Ahrens	error = dsl_destroy_head_check_impl(ds, 0);
6633b2aab1Matthew Ahrens	dsl_dataset_rele(ds, FTAG);
6643b2aab1Matthew Ahrens	return (error);
6653b2aab1Matthew Ahrens}
6663b2aab1Matthew Ahrens
6673b2aab1Matthew Ahrensstatic void
6683b2aab1Matthew Ahrensdsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
6693b2aab1Matthew Ahrens{
6703b2aab1Matthew Ahrens	dsl_dir_t *dd;
6713b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
6723b2aab1Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
6733b2aab1Matthew Ahrens	dd_used_t t;
6743b2aab1Matthew Ahrens
6753b2aab1Matthew Ahrens	ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
6763b2aab1Matthew Ahrens
6773b2aab1Matthew Ahrens	VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
6783b2aab1Matthew Ahrens
679c137962Justin T. Gibbs	ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
6803b2aab1Matthew Ahrens
6813b2aab1Matthew Ahrens	/*
682a2afb61Jerry Jelinek	 * Decrement the filesystem count for all parent filesystems.
683a2afb61Jerry Jelinek	 *
684a2afb61Jerry Jelinek	 * When we receive an incremental stream into a filesystem that already
685a2afb61Jerry Jelinek	 * exists, a temporary clone is created.  We never count this temporary
686a2afb61Jerry Jelinek	 * clone, whose name begins with a '%'.
687a2afb61Jerry Jelinek	 */
688a2afb61Jerry Jelinek	if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
689a2afb61Jerry Jelinek		dsl_fs_ss_count_adjust(dd->dd_parent, -1,
690a2afb61Jerry Jelinek		    DD_FIELD_FILESYSTEM_COUNT, tx);
691a2afb61Jerry Jelinek
692a2afb61Jerry Jelinek	/*
6933b2aab1Matthew Ahrens	 * Remove our reservation. The impl() routine avoids setting the
6943b2aab1Matthew Ahrens	 * actual property, which would require the (already destroyed) ds.
6953b2aab1Matthew Ahrens	 */
6963b2aab1Matthew Ahrens	dsl_dir_set_reservation_sync_impl(dd, 0, tx);
6973b2aab1Matthew Ahrens
698c137962Justin T. Gibbs	ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
699c137962Justin T. Gibbs	ASSERT0(dsl_dir_phys(dd)->dd_reserved);
7003b2aab1Matthew Ahrens	for (t = 0; t < DD_USED_NUM; t++)
701c137962Justin T. Gibbs		ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
7023b2aab1Matthew Ahrens
703c137962Justin T. Gibbs	VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
704c137962Justin T. Gibbs	VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
705c137962Justin T. Gibbs	VERIFY0(dsl_deleg_destroy(mos, dsl_dir_phys(dd)->dd_deleg_zapobj, tx));
7063b2aab1Matthew Ahrens	VERIFY0(zap_remove(mos,
707c137962Justin T. Gibbs	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
708c137962Justin T. Gibbs	    dd->dd_myname, tx));
7093b2aab1Matthew Ahrens
7103b2aab1Matthew Ahrens	dsl_dir_rele(dd, FTAG);
7112acef22Matthew Ahrens	dmu_object_free_zapified(mos, ddobj, tx);
7123b2aab1Matthew Ahrens}
7133b2aab1Matthew Ahrens
7143b2aab1Matthew Ahrensvoid
7153b2aab1Matthew Ahrensdsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
7163b2aab1Matthew Ahrens{
7173b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
7183b2aab1Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
7193b2aab1Matthew Ahrens	uint64_t obj, ddobj, prevobj = 0;
7203b2aab1Matthew Ahrens	boolean_t rmorigin;
7213b2aab1Matthew Ahrens
722c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
7233b2aab1Matthew Ahrens	ASSERT(ds->ds_prev == NULL ||
724c137962Justin T. Gibbs	    dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj != ds->ds_object);
725c166b69Paul Dagnelie	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
726c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
727c166b69Paul Dagnelie	rrw_exit(&ds->ds_bp_rwlock, FTAG);
7283b2aab1Matthew Ahrens	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
7293b2aab1Matthew Ahrens
7303b2aab1Matthew Ahrens	/* We need to log before removing it from the namespace. */
7313b2aab1Matthew Ahrens	spa_history_log_internal_ds(ds, "destroy", tx, "");
7323b2aab1Matthew Ahrens
7333b2aab1Matthew Ahrens	rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
7343b2aab1Matthew Ahrens	    DS_IS_DEFER_DESTROY(ds->ds_prev) &&
735c137962Justin T. Gibbs	    dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
7363b2aab1Matthew Ahrens	    ds->ds_prev->ds_userrefs == 0);
7373b2aab1Matthew Ahrens
7385d7b4d4Matthew Ahrens	/* Remove our reservation. */
7393b2aab1Matthew Ahrens	if (ds->ds_reserved != 0) {
7403b2aab1Matthew Ahrens		dsl_dataset_set_refreservation_sync_impl(ds,
7413b2aab1Matthew Ahrens		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
7423b2aab1Matthew Ahrens		    0, tx);
7433b2aab1Matthew Ahrens		ASSERT0(ds->ds_reserved);
7443b2aab1Matthew Ahrens	}
7453b2aab1Matthew Ahrens
746ca0cc39Matthew Ahrens	obj = ds->ds_object;
747b515258Matthew Ahrens
748ca0cc39Matthew Ahrens	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
749ca0cc39Matthew Ahrens		if (ds->ds_feature_inuse[f]) {
750ca0cc39Matthew Ahrens			dsl_dataset_deactivate_feature(obj, f, tx);
751ca0cc39Matthew Ahrens			ds->ds_feature_inuse[f] = B_FALSE;
752ca0cc39Matthew Ahrens		}
753ca0cc39Matthew Ahrens	}
7543b2aab1Matthew Ahrens
755ca0cc39Matthew Ahrens	dsl_scan_ds_destroyed(ds, tx);
7563b2aab1Matthew Ahrens
757c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
7583b2aab1Matthew Ahrens		/* This is a clone */
7593b2aab1Matthew Ahrens		ASSERT(ds->ds_prev != NULL);
760c137962Justin T. Gibbs		ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj, !=,
761c137962Justin T. Gibbs		    obj);
762c137962Justin T. Gibbs		ASSERT0(dsl_dataset_phys(ds)->ds_next_snap_obj);
7633b2aab1Matthew Ahrens
7643b2aab1Matthew Ahrens		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
765c137962Justin T. Gibbs		if (dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj != 0) {
7663b2aab1Matthew Ahrens			dsl_dataset_remove_from_next_clones(ds->ds_prev,
7673b2aab1Matthew Ahrens			    obj, tx);
7683b2aab1Matthew Ahrens		}
7693b2aab1Matthew Ahrens
770c137962Justin T. Gibbs		ASSERT3U(dsl_dataset_phys(ds->ds_prev)->ds_num_children, >, 1);
771c137962Justin T. Gibbs		dsl_dataset_phys(ds->ds_prev)->ds_num_children--;
7723b2aab1Matthew Ahrens	}
7733b2aab1Matthew Ahrens
7743b2aab1Matthew Ahrens	/*
7753b2aab1Matthew Ahrens	 * Destroy the deadlist.  Unless it's a clone, the
7763b2aab1Matthew Ahrens	 * deadlist should be empty.  (If it's a clone, it's
7773b2aab1Matthew Ahrens	 * safe to ignore the deadlist contents.)
7783b2aab1Matthew Ahrens	 */
7793b2aab1Matthew Ahrens	dsl_deadlist_close(&ds->ds_deadlist);
780c137962Justin T. Gibbs	dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
7813b2aab1Matthew Ahrens	dmu_buf_will_dirty(ds->ds_dbuf, tx);
782c137962Justin T. Gibbs	dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
7833b2aab1Matthew Ahrens
7842acef22Matthew Ahrens	objset_t *os;
7853b2aab1Matthew Ahrens	VERIFY0(dmu_objset_from_ds(ds, &os));
7863b2aab1Matthew Ahrens
7872acef22Matthew Ahrens	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY)) {
7883b2aab1Matthew Ahrens		old_synchronous_dataset_destroy(ds, tx);
7893b2aab1Matthew Ahrens	} else {
7903b2aab1Matthew Ahrens		/*
7913b2aab1Matthew Ahrens		 * Move the bptree into the pool's list of trees to
7923b2aab1Matthew Ahrens		 * clean up and update space accounting information.
7933b2aab1Matthew Ahrens		 */
7943b2aab1Matthew Ahrens		uint64_t used, comp, uncomp;
7953b2aab1Matthew Ahrens
7963b2aab1Matthew Ahrens		zil_destroy_sync(dmu_objset_zil(os), tx);
7973b2aab1Matthew Ahrens
7982acef22Matthew Ahrens		if (!spa_feature_is_active(dp->dp_spa,
7992acef22Matthew Ahrens		    SPA_FEATURE_ASYNC_DESTROY)) {
8004a92375George Wilson			dsl_scan_t *scn = dp->dp_scan;
8012acef22Matthew Ahrens			spa_feature_incr(dp->dp_spa, SPA_FEATURE_ASYNC_DESTROY,
8022acef22Matthew Ahrens			    tx);
8033b2aab1Matthew Ahrens			dp->dp_bptree_obj = bptree_alloc(mos, tx);
8043b2aab1Matthew Ahrens			VERIFY0(zap_add(mos,
8053b2aab1Matthew Ahrens			    DMU_POOL_DIRECTORY_OBJECT,
8063b2aab1Matthew Ahrens			    DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
8073b2aab1Matthew Ahrens			    &dp->dp_bptree_obj, tx));
8084a92375George Wilson			ASSERT(!scn->scn_async_destroying);
8094a92375George Wilson			scn->scn_async_destroying = B_TRUE;
8103b2aab1Matthew Ahrens		}
8113b2aab1Matthew Ahrens
812c137962Justin T. Gibbs		used = dsl_dir_phys(ds->ds_dir)->dd_used_bytes;
813c137962Justin T. Gibbs		comp = dsl_dir_phys(ds->ds_dir)->dd_compressed_bytes;
814c137962Justin T. Gibbs		uncomp = dsl_dir_phys(ds->ds_dir)->dd_uncompressed_bytes;
8153b2aab1Matthew Ahrens
8163b2aab1Matthew Ahrens		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
817c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_unique_bytes == used);
818