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.
235cabbc6Prashanth Sreenivasa * Copyright (c) 2012, 2017 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>
33dfc1153Chris Williamson#include <sys/dsl_destroy.h>
343b2aab1Matthew Ahrens#include <sys/dmu_tx.h>
353b2aab1Matthew Ahrens#include <sys/dsl_pool.h>
363b2aab1Matthew Ahrens#include <sys/dsl_dir.h>
373b2aab1Matthew Ahrens#include <sys/dmu_traverse.h>
383b2aab1Matthew Ahrens#include <sys/dsl_scan.h>
393b2aab1Matthew Ahrens#include <sys/dmu_objset.h>
403b2aab1Matthew Ahrens#include <sys/zap.h>
413b2aab1Matthew Ahrens#include <sys/zfeature.h>
423b2aab1Matthew Ahrens#include <sys/zfs_ioctl.h>
433b2aab1Matthew Ahrens#include <sys/dsl_deleg.h>
442acef22Matthew Ahrens#include <sys/dmu_impl.h>
45dfc1153Chris Williamson#include <sys/zcp.h>
463b2aab1Matthew Ahrens
4734f2f8cMatthew Ahrensint
483b2aab1Matthew Ahrensdsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
493b2aab1Matthew Ahrens{
50bc9014eJustin Gibbs	if (!ds->ds_is_snapshot)
51be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
523b2aab1Matthew Ahrens
533b2aab1Matthew Ahrens	if (dsl_dataset_long_held(ds))
54be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
553b2aab1Matthew Ahrens
563b2aab1Matthew Ahrens	/*
573b2aab1Matthew Ahrens	 * Only allow deferred destroy on pools that support it.
583b2aab1Matthew Ahrens	 * NOTE: deferred destroy is only supported on snapshots.
593b2aab1Matthew Ahrens	 */
603b2aab1Matthew Ahrens	if (defer) {
613b2aab1Matthew Ahrens		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
623b2aab1Matthew Ahrens		    SPA_VERSION_USERREFS)
63be6fd75Matthew Ahrens			return (SET_ERROR(ENOTSUP));
643b2aab1Matthew Ahrens		return (0);
653b2aab1Matthew Ahrens	}
663b2aab1Matthew Ahrens
673b2aab1Matthew Ahrens	/*
683b2aab1Matthew Ahrens	 * If this snapshot has an elevated user reference count,
693b2aab1Matthew Ahrens	 * we can't destroy it yet.
703b2aab1Matthew Ahrens	 */
713b2aab1Matthew Ahrens	if (ds->ds_userrefs > 0)
72be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
733b2aab1Matthew Ahrens
743b2aab1Matthew Ahrens	/*
753b2aab1Matthew Ahrens	 * Can't delete a branch point.
763b2aab1Matthew Ahrens	 */
77c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_num_children > 1)
78be6fd75Matthew Ahrens		return (SET_ERROR(EEXIST));
793b2aab1Matthew Ahrens
803b2aab1Matthew Ahrens	return (0);
813b2aab1Matthew Ahrens}
823b2aab1Matthew Ahrens
83dfc1153Chris Williamsonint
843b2aab1Matthew Ahrensdsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
853b2aab1Matthew Ahrens{
86dfc1153Chris Williamson	dsl_destroy_snapshot_arg_t *ddsa = arg;
87dfc1153Chris Williamson	const char *dsname = ddsa->ddsa_name;
88dfc1153Chris Williamson	boolean_t defer = ddsa->ddsa_defer;
89dfc1153Chris Williamson
903b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
913b2aab1Matthew Ahrens	int error = 0;
92dfc1153Chris Williamson	dsl_dataset_t *ds;
933b2aab1Matthew Ahrens
94dfc1153Chris Williamson	error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
953b2aab1Matthew Ahrens
96dfc1153Chris Williamson	/*
97dfc1153Chris Williamson	 * If the snapshot does not exist, silently ignore it, and
98dfc1153Chris Williamson	 * dsl_destroy_snapshot_sync() will be a no-op
99dfc1153Chris Williamson	 * (it's "already destroyed").
100dfc1153Chris Williamson	 */
101dfc1153Chris Williamson	if (error == ENOENT)
102dfc1153Chris Williamson		return (0);
1033b2aab1Matthew Ahrens
104dfc1153Chris Williamson	if (error == 0) {
105dfc1153Chris Williamson		error = dsl_destroy_snapshot_check_impl(ds, defer);
106dfc1153Chris Williamson		dsl_dataset_rele(ds, FTAG);
1073b2aab1Matthew Ahrens	}
1083b2aab1Matthew Ahrens
109dfc1153Chris Williamson	return (error);
1103b2aab1Matthew Ahrens}
1113b2aab1Matthew Ahrens
1123b2aab1Matthew Ahrensstruct process_old_arg {
1133b2aab1Matthew Ahrens	dsl_dataset_t *ds;
1143b2aab1Matthew Ahrens	dsl_dataset_t *ds_prev;
1153b2aab1Matthew Ahrens	boolean_t after_branch_point;
1163b2aab1Matthew Ahrens	zio_t *pio;
1173b2aab1Matthew Ahrens	uint64_t used, comp, uncomp;
1183b2aab1Matthew Ahrens};
1193b2aab1Matthew Ahrens
1203b2aab1Matthew Ahrensstatic int
1213b2aab1Matthew Ahrensprocess_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1223b2aab1Matthew Ahrens{
1233b2aab1Matthew Ahrens	struct process_old_arg *poa = arg;
1243b2aab1Matthew Ahrens	dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
1253b2aab1Matthew Ahrens
12643466aaMax Grossman	ASSERT(!BP_IS_HOLE(bp));
12743466aaMax Grossman
128c137962Justin T. Gibbs	if (bp->blk_birth <= dsl_dataset_phys(poa->ds)->ds_prev_snap_txg) {
1293b2aab1Matthew Ahrens		dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
1303b2aab1Matthew Ahrens		if (poa->ds_prev && !poa->after_branch_point &&
1313b2aab1Matthew Ahrens		    bp->blk_birth >
132c137962Justin T. Gibbs		    dsl_dataset_phys(poa->ds_prev)->ds_prev_snap_txg) {
133c137962Justin T. Gibbs			dsl_dataset_phys(poa->ds_prev)->ds_unique_bytes +=
1343b2aab1Matthew Ahrens			    bp_get_dsize_sync(dp->dp_spa, bp);
1353b2aab1Matthew Ahrens		}
1363b2aab1Matthew Ahrens	} else {
1373b2aab1Matthew Ahrens		poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
1383b2aab1Matthew Ahrens		poa->comp += BP_GET_PSIZE(bp);
1393b2aab1Matthew Ahrens		poa->uncomp += BP_GET_UCSIZE(bp);
1403b2aab1Matthew Ahrens		dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
1413b2aab1Matthew Ahrens	}
1423b2aab1Matthew Ahrens	return (0);
1433b2aab1Matthew Ahrens}
1443b2aab1Matthew Ahrens
1453b2aab1Matthew Ahrensstatic void
1463b2aab1Matthew Ahrensprocess_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
1473b2aab1Matthew Ahrens    dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
1483b2aab1Matthew Ahrens{
1493b2aab1Matthew Ahrens	struct process_old_arg poa = { 0 };
1503b2aab1Matthew Ahrens	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1513b2aab1Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
1523b2aab1Matthew Ahrens	uint64_t deadlist_obj;
1533b2aab1Matthew Ahrens
1543b2aab1Matthew Ahrens	ASSERT(ds->ds_deadlist.dl_oldfmt);
1553b2aab1Matthew Ahrens	ASSERT(ds_next->ds_deadlist.dl_oldfmt);
1563b2aab1Matthew Ahrens
1573b2aab1Matthew Ahrens	poa.ds = ds;
1583b2aab1Matthew Ahrens	poa.ds_prev = ds_prev;
1593b2aab1Matthew Ahrens	poa.after_branch_point = after_branch_point;
1603b2aab1Matthew Ahrens	poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1613b2aab1Matthew Ahrens	VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
1623b2aab1Matthew Ahrens	    process_old_cb, &poa, tx));
1633b2aab1Matthew Ahrens	VERIFY0(zio_wait(poa.pio));
164c137962Justin T. Gibbs	ASSERT3U(poa.used, ==, dsl_dataset_phys(ds)->ds_unique_bytes);
1653b2aab1Matthew Ahrens
1663b2aab1Matthew Ahrens	/* change snapused */
1673b2aab1Matthew Ahrens	dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1683b2aab1Matthew Ahrens	    -poa.used, -poa.comp, -poa.uncomp, tx);
1693b2aab1Matthew Ahrens
1703b2aab1Matthew Ahrens	/* swap next's deadlist to our deadlist */
1713b2aab1Matthew Ahrens	dsl_deadlist_close(&ds->ds_deadlist);
1723b2aab1Matthew Ahrens	dsl_deadlist_close(&ds_next->ds_deadlist);
173c137962Justin T. Gibbs	deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
174c137962Justin T. Gibbs	dsl_dataset_phys(ds)->ds_deadlist_obj =
175c137962Justin T. Gibbs	    dsl_dataset_phys(ds_next)->ds_deadlist_obj;
176c137962Justin T. Gibbs	dsl_dataset_phys(ds_next)->ds_deadlist_obj = deadlist_obj;
177c137962Justin T. Gibbs	dsl_deadlist_open(&ds->ds_deadlist, mos,
178c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1793b2aab1Matthew Ahrens	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
180c137962Justin T. Gibbs	    dsl_dataset_phys(ds_next)->ds_deadlist_obj);
1813b2aab1Matthew Ahrens}
1823b2aab1Matthew Ahrens
1833b2aab1Matthew Ahrensstatic void
1843b2aab1Matthew Ahrensdsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
1853b2aab1Matthew Ahrens{
1863b2aab1Matthew Ahrens	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1873b2aab1Matthew Ahrens	zap_cursor_t zc;
1883b2aab1Matthew Ahrens	zap_attribute_t za;
1893b2aab1Matthew Ahrens
1903b2aab1Matthew Ahrens	/*
1913b2aab1Matthew Ahrens	 * If it is the old version, dd_clones doesn't exist so we can't
1923b2aab1Matthew Ahrens	 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
1933b2aab1Matthew Ahrens	 * doesn't matter.
1943b2aab1Matthew Ahrens	 */
195c137962Justin T. Gibbs	if (dsl_dir_phys(ds->ds_dir)->dd_clones == 0)
1963b2aab1Matthew Ahrens		return;
1973b2aab1Matthew Ahrens
198c137962Justin T. Gibbs	for (zap_cursor_init(&zc, mos, dsl_dir_phys(ds->ds_dir)->dd_clones);
1993b2aab1Matthew Ahrens	    zap_cursor_retrieve(&zc, &za) == 0;
2003b2aab1Matthew Ahrens	    zap_cursor_advance(&zc)) {
2013b2aab1Matthew Ahrens		dsl_dataset_t *clone;
2023b2aab1Matthew Ahrens
2033b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
2043b2aab1Matthew Ahrens		    za.za_first_integer, FTAG, &clone));
2053b2aab1Matthew Ahrens		if (clone->ds_dir->dd_origin_txg > mintxg) {
2063b2aab1Matthew Ahrens			dsl_deadlist_remove_key(&clone->ds_deadlist,
2073b2aab1Matthew Ahrens			    mintxg, tx);
2085cabbc6Prashanth Sreenivasa			if (dsl_dataset_remap_deadlist_exists(clone)) {
2095cabbc6Prashanth Sreenivasa				dsl_deadlist_remove_key(
2105cabbc6Prashanth Sreenivasa				    &clone->ds_remap_deadlist, mintxg, tx);
2115cabbc6Prashanth Sreenivasa			}
2123b2aab1Matthew Ahrens			dsl_dataset_remove_clones_key(clone, mintxg, tx);
2133b2aab1Matthew Ahrens		}
2143b2aab1Matthew Ahrens		dsl_dataset_rele(clone, FTAG);
2153b2aab1Matthew Ahrens	}
2163b2aab1Matthew Ahrens	zap_cursor_fini(&zc);
2173b2aab1Matthew Ahrens}
2183b2aab1Matthew Ahrens
2195cabbc6Prashanth Sreenivasastatic void
2205cabbc6Prashanth Sreenivasadsl_destroy_snapshot_handle_remaps(dsl_dataset_t *ds, dsl_dataset_t *ds_next,
2215cabbc6Prashanth Sreenivasa    dmu_tx_t *tx)
2225cabbc6Prashanth Sreenivasa{
2235cabbc6Prashanth Sreenivasa	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2245cabbc6Prashanth Sreenivasa
2255cabbc6Prashanth Sreenivasa	/* Move blocks to be obsoleted to pool's obsolete list. */
2265cabbc6Prashanth Sreenivasa	if (dsl_dataset_remap_deadlist_exists(ds_next)) {
2275cabbc6Prashanth Sreenivasa		if (!bpobj_is_open(&dp->dp_obsolete_bpobj))
2285cabbc6Prashanth Sreenivasa			dsl_pool_create_obsolete_bpobj(dp, tx);
2295cabbc6Prashanth Sreenivasa
2305cabbc6Prashanth Sreenivasa		dsl_deadlist_move_bpobj(&ds_next->ds_remap_deadlist,
2315cabbc6Prashanth Sreenivasa		    &dp->dp_obsolete_bpobj,
2325cabbc6Prashanth Sreenivasa		    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
2335cabbc6Prashanth Sreenivasa	}
2345cabbc6Prashanth Sreenivasa
2355cabbc6Prashanth Sreenivasa	/* Merge our deadlist into next's and free it. */
2365cabbc6Prashanth Sreenivasa	if (dsl_dataset_remap_deadlist_exists(ds)) {
2375cabbc6Prashanth Sreenivasa		uint64_t remap_deadlist_object =
2385cabbc6Prashanth Sreenivasa		    dsl_dataset_get_remap_deadlist_object(ds);
2395cabbc6Prashanth Sreenivasa		ASSERT(remap_deadlist_object != 0);
2405cabbc6Prashanth Sreenivasa
2415cabbc6Prashanth Sreenivasa		mutex_enter(&ds_next->ds_remap_deadlist_lock);
2425cabbc6Prashanth Sreenivasa		if (!dsl_dataset_remap_deadlist_exists(ds_next))
2435cabbc6Prashanth Sreenivasa			dsl_dataset_create_remap_deadlist(ds_next, tx);
2445cabbc6Prashanth Sreenivasa		mutex_exit(&ds_next->ds_remap_deadlist_lock);
2455cabbc6Prashanth Sreenivasa
2465cabbc6Prashanth Sreenivasa		dsl_deadlist_merge(&ds_next->ds_remap_deadlist,
2475cabbc6Prashanth Sreenivasa		    remap_deadlist_object, tx);
2485cabbc6Prashanth Sreenivasa		dsl_dataset_destroy_remap_deadlist(ds, tx);
2495cabbc6Prashanth Sreenivasa	}
2505cabbc6Prashanth Sreenivasa}
2515cabbc6Prashanth Sreenivasa
2523b2aab1Matthew Ahrensvoid
2533b2aab1Matthew Ahrensdsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
2543b2aab1Matthew Ahrens{
2553b2aab1Matthew Ahrens	int err;
2563b2aab1Matthew Ahrens	int after_branch_point = FALSE;
2573b2aab1Matthew Ahrens	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2583b2aab1Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
2593b2aab1Matthew Ahrens	dsl_dataset_t *ds_prev = NULL;
2603b2aab1Matthew Ahrens	uint64_t obj;
2613b2aab1Matthew Ahrens
2623b2aab1Matthew Ahrens	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
263c166b69Paul Dagnelie	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
264c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_bp.blk_birth, <=, tx->tx_txg);
265c166b69Paul Dagnelie	rrw_exit(&ds->ds_bp_rwlock, FTAG);
266e914aceTim Schumacher	ASSERT(zfs_refcount_is_zero(&ds->ds_longholds));
2673b2aab1Matthew Ahrens
2683b2aab1Matthew Ahrens	if (defer &&
269c137962Justin T. Gibbs	    (ds->ds_userrefs > 0 ||
270c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_num_children > 1)) {
2713b2aab1Matthew Ahrens		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
2723b2aab1Matthew Ahrens		dmu_buf_will_dirty(ds->ds_dbuf, tx);
273c137962Justin T. Gibbs		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_DEFER_DESTROY;
2743b2aab1Matthew Ahrens		spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
2753b2aab1Matthew Ahrens		return;
2763b2aab1Matthew Ahrens	}
2773b2aab1Matthew Ahrens
278c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_num_children, <=, 1);
2793b2aab1Matthew Ahrens
2803b2aab1Matthew Ahrens	/* We need to log before removing it from the namespace. */
2813b2aab1Matthew Ahrens	spa_history_log_internal_ds(ds, "destroy", tx, "");
2823b2aab1Matthew Ahrens
2833b2aab1Matthew Ahrens	dsl_scan_ds_destroyed(ds, tx);
2843b2aab1Matthew Ahrens
2853b2aab1Matthew Ahrens	obj = ds->ds_object;
2863b2aab1Matthew Ahrens
287ca0cc39Matthew Ahrens	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
288ca0cc39Matthew Ahrens		if (ds->ds_feature_inuse[f]) {
289ca0cc39Matthew Ahrens			dsl_dataset_deactivate_feature(obj, f, tx);
290ca0cc39Matthew Ahrens			ds->ds_feature_inuse[f] = B_FALSE;
291ca0cc39Matthew Ahrens		}
292b515258Matthew Ahrens	}
293c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2943b2aab1Matthew Ahrens		ASSERT3P(ds->ds_prev, ==, NULL);
2953b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(dp,
296c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &ds_prev));
2973b2aab1Matthew Ahrens		after_branch_point =
298c137962Justin T. Gibbs		    (dsl_dataset_phys(ds_prev)->ds_next_snap_obj != obj);
2993b2aab1Matthew Ahrens
3003b2aab1Matthew Ahrens		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
3013b2aab1Matthew Ahrens		if (after_branch_point &&
302c137962Justin T. Gibbs		    dsl_dataset_phys(ds_prev)->ds_next_clones_obj != 0) {
3033b2aab1Matthew Ahrens			dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
304c137962Justin T. Gibbs			if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) {
3053b2aab1Matthew Ahrens				VERIFY0(zap_add_int(mos,
306c137962Justin T. Gibbs				    dsl_dataset_phys(ds_prev)->
307c137962Justin T. Gibbs				    ds_next_clones_obj,
308c137962Justin T. Gibbs				    dsl_dataset_phys(ds)->ds_next_snap_obj,
309c137962Justin T. Gibbs				    tx));
3103b2aab1Matthew Ahrens			}
3113b2aab1Matthew Ahrens		}
3123b2aab1Matthew Ahrens		if (!after_branch_point) {
313c137962Justin T. Gibbs			dsl_dataset_phys(ds_prev)->ds_next_snap_obj =
314c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_next_snap_obj;
3153b2aab1Matthew Ahrens		}
3163b2aab1Matthew Ahrens	}
3173b2aab1Matthew Ahrens
3183b2aab1Matthew Ahrens	dsl_dataset_t *ds_next;
3193b2aab1Matthew Ahrens	uint64_t old_unique;
3203b2aab1Matthew Ahrens	uint64_t used = 0, comp = 0, uncomp = 0;
3213b2aab1Matthew Ahrens
3223b2aab1Matthew Ahrens	VERIFY0(dsl_dataset_hold_obj(dp,
323c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_next_snap_obj, FTAG, &ds_next));
324c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds_next)->ds_prev_snap_obj, ==, obj);
3253b2aab1Matthew Ahrens
326c137962Justin T. Gibbs	old_unique = dsl_dataset_phys(ds_next)->ds_unique_bytes;
3273b2aab1Matthew Ahrens
3283b2aab1Matthew Ahrens	dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
329c137962Justin T. Gibbs	dsl_dataset_phys(ds_next)->ds_prev_snap_obj =
330c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_prev_snap_obj;
331c137962Justin T. Gibbs	dsl_dataset_phys(ds_next)->ds_prev_snap_txg =
332c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_prev_snap_txg;
333c137962Justin T. Gibbs	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
334c137962Justin T. Gibbs	    ds_prev ? dsl_dataset_phys(ds_prev)->ds_creation_txg : 0);
3353b2aab1Matthew Ahrens
3363b2aab1Matthew Ahrens	if (ds_next->ds_deadlist.dl_oldfmt) {
3373b2aab1Matthew Ahrens		process_old_deadlist(ds, ds_prev, ds_next,
3383b2aab1Matthew Ahrens		    after_branch_point, tx);
3393b2aab1Matthew Ahrens	} else {
3403b2aab1Matthew Ahrens		/* Adjust prev's unique space. */
3413b2aab1Matthew Ahrens		if (ds_prev && !after_branch_point) {
3423b2aab1Matthew Ahrens			dsl_deadlist_space_range(&ds_next->ds_deadlist,
343c137962Justin T. Gibbs			    dsl_dataset_phys(ds_prev)->ds_prev_snap_txg,
344c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_prev_snap_txg,
3453b2aab1Matthew Ahrens			    &used, &comp, &uncomp);
346c137962Justin T. Gibbs			dsl_dataset_phys(ds_prev)->ds_unique_bytes += used;
3473b2aab1Matthew Ahrens		}
3483b2aab1Matthew Ahrens
3493b2aab1Matthew Ahrens		/* Adjust snapused. */
3503b2aab1Matthew Ahrens		dsl_deadlist_space_range(&ds_next->ds_deadlist,
351c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_prev_snap_txg, UINT64_MAX,
3523b2aab1Matthew Ahrens		    &used, &comp, &uncomp);
3533b2aab1Matthew Ahrens		dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
3543b2aab1Matthew Ahrens		    -used, -comp, -uncomp, tx);
3553b2aab1Matthew Ahrens
3563b2aab1Matthew Ahrens		/* Move blocks to be freed to pool's free list. */
3573b2aab1Matthew Ahrens		dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
358c137962Justin T. Gibbs		    &dp->dp_free_bpobj, dsl_dataset_phys(ds)->ds_prev_snap_txg,
3593b2aab1Matthew Ahrens		    tx);
3603b2aab1Matthew Ahrens		dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
3613b2aab1Matthew Ahrens		    DD_USED_HEAD, used, comp, uncomp, tx);
3623b2aab1Matthew Ahrens
3633b2aab1Matthew Ahrens		/* Merge our deadlist into next's and free it. */
3643b2aab1Matthew Ahrens		dsl_deadlist_merge(&ds_next->ds_deadlist,
365c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3663b2aab1Matthew Ahrens	}
3675cabbc6Prashanth Sreenivasa
3683b2aab1Matthew Ahrens	dsl_deadlist_close(&ds->ds_deadlist);
369c137962Justin T. Gibbs	dsl_deadlist_free(mos, dsl_dataset_phys(ds)->ds_deadlist_obj, tx);
3703b2aab1Matthew Ahrens	dmu_buf_will_dirty(ds->ds_dbuf, tx);
371c137962Justin T. Gibbs	dsl_dataset_phys(ds)->ds_deadlist_obj = 0;
3723b2aab1Matthew Ahrens
3735cabbc6Prashanth Sreenivasa	dsl_destroy_snapshot_handle_remaps(ds, ds_next, tx);
3745cabbc6Prashanth Sreenivasa
3753b2aab1Matthew Ahrens	/* Collapse range in clone heads */
3763b2aab1Matthew Ahrens	dsl_dataset_remove_clones_key(ds,
377c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_creation_txg, tx);
3783b2aab1Matthew Ahrens
379bc9014eJustin Gibbs	if (ds_next->ds_is_snapshot) {
3803b2aab1Matthew Ahrens		dsl_dataset_t *ds_nextnext;
3813b2aab1Matthew Ahrens
3823b2aab1Matthew Ahrens		/*
3833b2aab1Matthew Ahrens		 * Update next's unique to include blocks which
3843b2aab1Matthew Ahrens		 * were previously shared by only this snapshot
3853b2aab1Matthew Ahrens		 * and it.  Those blocks will be born after the
3863b2aab1Matthew Ahrens		 * prev snap and before this snap, and will have
3873b2aab1Matthew Ahrens		 * died after the next snap and before the one
3883b2aab1Matthew Ahrens		 * after that (ie. be on the snap after next's
3893b2aab1Matthew Ahrens		 * deadlist).
3903b2aab1Matthew Ahrens		 */
3913b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(dp,
392c137962Justin T. Gibbs		    dsl_dataset_phys(ds_next)->ds_next_snap_obj,
393c137962Justin T. Gibbs		    FTAG, &ds_nextnext));
3943b2aab1Matthew Ahrens		dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
395c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_prev_snap_txg,
396c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_creation_txg,
3973b2aab1Matthew Ahrens		    &used, &comp, &uncomp);
398c137962Justin T. Gibbs		dsl_dataset_phys(ds_next)->ds_unique_bytes += used;
3993b2aab1Matthew Ahrens		dsl_dataset_rele(ds_nextnext, FTAG);
4003b2aab1Matthew Ahrens		ASSERT3P(ds_next->ds_prev, ==, NULL);
4013b2aab1Matthew Ahrens
4023b2aab1Matthew Ahrens		/* Collapse range in this head. */
4033b2aab1Matthew Ahrens		dsl_dataset_t *hds;
4043b2aab1Matthew Ahrens		VERIFY0(dsl_dataset_hold_obj(dp,
405c137962Justin T. Gibbs		    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &hds));
4063b2aab1Matthew Ahrens		dsl_deadlist_remove_key(&hds->ds_deadlist,
407c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_creation_txg, tx);
4085cabbc6Prashanth Sreenivasa		if (dsl_dataset_remap_deadlist_exists(hds)) {
4095cabbc6Prashanth Sreenivasa			dsl_deadlist_remove_key(&hds->ds_remap_deadlist,
4105cabbc6Prashanth Sreenivasa			    dsl_dataset_phys(ds)->ds_creation_txg, tx);
4115cabbc6Prashanth Sreenivasa		}
4123b2aab1Matthew Ahrens		dsl_dataset_rele(hds, FTAG);
4133b2aab1Matthew Ahrens
4143b2aab1Matthew Ahrens	} else {
4153b2aab1Matthew Ahrens		ASSERT3P(ds_next->ds_prev, ==, ds);
4163b2aab1Matthew Ahrens		dsl_dataset_rele(ds_next->ds_prev, ds_next);
4173b2aab1Matthew Ahrens		ds_next->ds_prev = NULL;
4183b2aab1Matthew Ahrens		if (ds_prev) {
4193b2aab1Matthew Ahrens			VERIFY0(dsl_dataset_hold_obj(dp,
420c137962Justin T. Gibbs			    dsl_dataset_phys(ds)->ds_prev_snap_obj,
4213b2aab1Matthew Ahrens			    ds_next, &ds_next->ds_prev));
4223b2aab1Matthew Ahrens		}
4233b2aab1Matthew Ahrens
4243b2aab1Matthew Ahrens		dsl_dataset_recalc_head_uniq(ds_next);
4253b2aab1Matthew Ahrens
4263b2aab1Matthew Ahrens		/*
4273b2aab1Matthew Ahrens		 * Reduce the amount of our unconsumed refreservation
4283b2aab1Matthew Ahrens		 * being charged to our parent by the amount of
4293b2aab1Matthew Ahrens		 * new unique data we have gained.
4303b2aab1Matthew Ahrens		 */
4313b2aab1Matthew Ahrens		if (old_unique < ds_next->ds_reserved) {
4323b2aab1Matthew Ahrens			int64_t mrsdelta;
4333b2aab1Matthew Ahrens			uint64_t new_unique =
434c137962Justin T. Gibbs			    dsl_dataset_phys(ds_next)->ds_unique_bytes;
4353b2aab1Matthew Ahrens
4363b2aab1Matthew Ahrens			ASSERT(old_unique <= new_unique);
4373b2aab1Matthew Ahrens			mrsdelta = MIN(new_unique - old_unique,
4383b2aab1Matthew Ahrens			    ds_next->ds_reserved - old_unique);
4393b2aab1Matthew Ahrens			dsl_dir_diduse_space(ds->ds_dir,
4403b2aab1Matthew Ahrens			    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
4413b2aab1Matthew Ahrens		}
4423b2aab1Matthew Ahrens	}
4433b2aab1Matthew Ahrens	dsl_dataset_rele(ds_next, FTAG);
4443b2aab1Matthew Ahrens
4453b2aab1Matthew Ahrens	/*
4463b2aab1Matthew Ahrens	 * This must be done after the dsl_traverse(), because it will
4473b2aab1Matthew Ahrens	 * re-open the objset.
4483b2aab1Matthew Ahrens	 */
4493b2aab1Matthew Ahrens	if (ds->ds_objset) {
4503b2aab1Matthew Ahrens		dmu_objset_evict(ds->ds_objset);
4513b2aab1Matthew Ahrens		ds->ds_objset = NULL;
4523b2aab1Matthew Ahrens	}
4533b2aab1Matthew Ahrens
4543b2aab1Matthew Ahrens	/* remove from snapshot namespace */
4553b2aab1Matthew Ahrens	dsl_dataset_t *ds_head;
456c137962Justin T. Gibbs	ASSERT(dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0);
4573b2aab1Matthew Ahrens	VERIFY0(dsl_dataset_hold_obj(dp,
458c137962Justin T. Gibbs	    dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, FTAG, &ds_head));
4593b2aab1Matthew Ahrens	VERIFY0(dsl_dataset_get_snapname(ds));
4603b2aab1Matthew Ahrens#ifdef ZFS_DEBUG
4613b2aab1Matthew Ahrens	{
4623b2aab1Matthew Ahrens		uint64_t val;
4633b2aab1Matthew Ahrens
4643b2aab1Matthew Ahrens		err = dsl_dataset_snap_lookup(ds_head,
4653b2aab1Matthew Ahrens		    ds->ds_snapname, &val);
4663b2aab1Matthew Ahrens		ASSERT0(err);
4673b2aab1Matthew Ahrens		ASSERT3U(val, ==, obj);
4683b2aab1Matthew Ahrens	}
4693b2aab1Matthew Ahrens#endif
470a2afb61Jerry Jelinek	VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx, B_TRUE));
4713b2aab1Matthew Ahrens	dsl_dataset_rele(ds_head, FTAG);
4723b2aab1Matthew Ahrens
4733b2aab1Matthew Ahrens	if (ds_prev != NULL)
4743b2aab1Matthew Ahrens		dsl_dataset_rele(ds_prev, FTAG);
4753b2aab1Matthew Ahrens
4763b2aab1Matthew Ahrens	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
4773b2aab1Matthew Ahrens
478c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
4793b2aab1Matthew Ahrens		uint64_t count;
4803b2aab1Matthew Ahrens		ASSERT0(zap_count(mos,
481c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_next_clones_obj, &count) &&
482c137962Justin T. Gibbs		    count == 0);
4833b2aab1Matthew Ahrens		VERIFY0(dmu_object_free(mos,
484c137962Justin T. Gibbs		    dsl_dataset_phys(ds)->ds_next_clones_obj, tx));
4853b2aab1Matthew Ahrens	}
486c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_props_obj != 0)
487c137962Justin T. Gibbs		VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_props_obj,
488c137962Justin T. Gibbs		    tx));
489c137962Justin T. Gibbs	if (dsl_dataset_phys(ds)->ds_userrefs_obj != 0)
490c137962Justin T. Gibbs		VERIFY0(zap_destroy(mos, dsl_dataset_phys(ds)->ds_userrefs_obj,
491c137962Justin T. Gibbs		    tx));
4923b2aab1Matthew Ahrens	dsl_dir_rele(ds->ds_dir, ds);
4933b2aab1Matthew Ahrens	ds->ds_dir = NULL;
4942acef22Matthew Ahrens	dmu_object_free_zapified(mos, obj, tx);
4953b2aab1Matthew Ahrens}
4963b2aab1Matthew Ahrens
497dfc1153Chris Williamsonvoid
4983b2aab1Matthew Ahrensdsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
4993b2aab1Matthew Ahrens{
500dfc1153Chris Williamson	dsl_destroy_snapshot_arg_t *ddsa = arg;
501dfc1153Chris Williamson	const char *dsname = ddsa->ddsa_name;
502dfc1153Chris Williamson	boolean_t defer = ddsa->ddsa_defer;
5033b2aab1Matthew Ahrens
504dfc1153Chris Williamson	dsl_pool_t *dp = dmu_tx_pool(tx);
505dfc1153Chris Williamson	dsl_dataset_t *ds;
5063b2aab1Matthew Ahrens
507dfc1153Chris Williamson	int error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
508dfc1153Chris Williamson	if (error == ENOENT)
509dfc1153Chris Williamson		return;
510dfc1153Chris Williamson	ASSERT0(error);
511dfc1153Chris Williamson	dsl_destroy_snapshot_sync_impl(ds, defer, tx);
512dfc1153Chris Williamson	dsl_dataset_rele(ds, FTAG);
5133b2aab1Matthew Ahrens}
5143b2aab1Matthew Ahrens
5153b2aab1Matthew Ahrens/*
5163b2aab1Matthew Ahrens * The semantics of this function are described in the comment above
5173b2aab1Matthew Ahrens * lzc_destroy_snaps().  To summarize:
5183b2aab1Matthew Ahrens *
5193b2aab1Matthew Ahrens * The snapshots must all be in the same pool.
5203b2aab1Matthew Ahrens *
5213b2aab1Matthew Ahrens * Snapshots that don't exist will be silently ignored (considered to be
5223b2aab1Matthew Ahrens * "already deleted").
5233b2aab1Matthew Ahrens *
5243b2aab1Matthew Ahrens * On success, all snaps will be destroyed and this will return 0.
5253b2aab1Matthew Ahrens * On failure, no snaps will be destroyed, the errlist will be filled in,
5263b2aab1Matthew Ahrens * and this will return an errno.
5273b2aab1Matthew Ahrens */
5283b2aab1Matthew Ahrensint
5293b2aab1Matthew Ahrensdsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
5303b2aab1Matthew Ahrens    nvlist_t *errlist)
5313b2aab1Matthew Ahrens{
532dfc1153Chris Williamson	if (nvlist_next_nvpair(snaps, NULL) == NULL)
5333b2aab1Matthew Ahrens		return (0);
5343b2aab1Matthew Ahrens
535dfc1153Chris Williamson	/*
536dfc1153Chris Williamson	 * lzc_destroy_snaps() is documented to take an nvlist whose
537ed992b0Serapheim Dimitropoulos	 * values "don't matter".  We need to convert that nvlist to
538ed992b0Serapheim Dimitropoulos	 * one that we know can be converted to LUA. We also don't
539ed992b0Serapheim Dimitropoulos	 * care about any duplicate entries because the nvlist will
540ed992b0Serapheim Dimitropoulos	 * be converted to a LUA table which should take care of this.
541dfc1153Chris Williamson	 */
542ed992b0Serapheim Dimitropoulos	nvlist_t *snaps_normalized;
543ed992b0Serapheim Dimitropoulos	VERIFY0(nvlist_alloc(&snaps_normalized, 0, KM_SLEEP));
544dfc1153Chris Williamson	for (nvpair_t *pair = nvlist_next_nvpair(snaps, NULL);
545dfc1153Chris Williamson	    pair != NULL; pair = nvlist_next_nvpair(snaps, pair)) {
546dfc1153Chris Williamson		fnvlist_add_boolean_value(snaps_normalized,
547dfc1153Chris Williamson		    nvpair_name(pair), B_TRUE);
548dfc1153Chris Williamson	}
549ed992b0Serapheim Dimitropoulos
550ed992b0Serapheim Dimitropoulos	nvlist_t *arg;
551ed992b0Serapheim Dimitropoulos	VERIFY0(nvlist_alloc(&arg, 0, KM_SLEEP));
552dfc1153Chris Williamson	fnvlist_add_nvlist(arg, "snaps", snaps_normalized);
553e6ab452Matthew Ahrens	fnvlist_free(snaps_normalized);
554dfc1153Chris Williamson	fnvlist_add_boolean_value(arg, "defer", defer);
555dfc1153Chris Williamson
556ed992b0Serapheim Dimitropoulos	nvlist_t *wrapper;
557ed992b0Serapheim Dimitropoulos	VERIFY0(nvlist_alloc(&wrapper, 0, KM_SLEEP));
558dfc1153Chris Williamson	fnvlist_add_nvlist(wrapper, ZCP_ARG_ARGLIST, arg);
559dfc1153Chris Williamson	fnvlist_free(arg);
560dfc1153Chris Williamson
561dfc1153Chris Williamson	const char *program =
562dfc1153Chris Williamson	    "arg = ...\n"
563dfc1153Chris Williamson	    "snaps = arg['snaps']\n"
564dfc1153Chris Williamson	    "defer = arg['defer']\n"
565dfc1153Chris Williamson	    "errors = { }\n"
566dfc1153Chris Williamson	    "has_errors = false\n"
567dfc1153Chris Williamson	    "for snap, v in pairs(snaps) do\n"
568dfc1153Chris Williamson	    "    errno = zfs.check.destroy{snap, defer=defer}\n"
569dfc1153Chris Williamson	    "    zfs.debug('snap: ' .. snap .. ' errno: ' .. errno)\n"
570dfc1153Chris Williamson	    "    if errno == ENOENT then\n"
571dfc1153Chris Williamson	    "        snaps[snap] = nil\n"
572dfc1153Chris Williamson	    "    elseif errno ~= 0 then\n"
573dfc1153Chris Williamson	    "        errors[snap] = errno\n"
574dfc1153Chris Williamson	    "        has_errors = true\n"
575dfc1153Chris Williamson	    "    end\n"
576dfc1153Chris Williamson	    "end\n"
577dfc1153Chris Williamson	    "if has_errors then\n"
578dfc1153Chris Williamson	    "    return errors\n"
579dfc1153Chris Williamson	    "end\n"
580dfc1153Chris Williamson	    "for snap, v in pairs(snaps) do\n"
581dfc1153Chris Williamson	    "    errno = zfs.sync.destroy{snap, defer=defer}\n"
582dfc1153Chris Williamson	    "    assert(errno == 0)\n"
583dfc1153Chris Williamson	    "end\n"
584dfc1153Chris Williamson	    "return { }\n";
585dfc1153Chris Williamson
586dfc1153Chris Williamson	nvlist_t *result = fnvlist_alloc();
587dfc1153Chris Williamson	int error = zcp_eval(nvpair_name(nvlist_next_nvpair(snaps, NULL)),
588dfc1153Chris Williamson	    program,
589a3b2868Serapheim Dimitropoulos	    B_TRUE,
590dfc1153Chris Williamson	    0,
591dfc1153Chris Williamson	    zfs_lua_max_memlimit,
592ed992b0Serapheim Dimitropoulos	    nvlist_next_nvpair(wrapper, NULL), result);
593dfc1153Chris Williamson	if (error != 0) {
594dfc1153Chris Williamson		char *errorstr = NULL;
595dfc1153Chris Williamson		(void) nvlist_lookup_string(result, ZCP_RET_ERROR, &errorstr);
596dfc1153Chris Williamson		if (errorstr != NULL) {
597dfc1153Chris Williamson			zfs_dbgmsg(errorstr);
598dfc1153Chris Williamson		}
599dfc1153Chris Williamson		return (error);
600dfc1153Chris Williamson	}
601dfc1153Chris Williamson	fnvlist_free(wrapper);
6023b2aab1Matthew Ahrens
603dfc1153Chris Williamson	/*
604dfc1153Chris Williamson	 * lzc_destroy_snaps() is documented to fill the errlist with
605dfc1153Chris Williamson	 * int32 values, so we need to covert the int64 values that are
606dfc1153Chris Williamson	 * returned from LUA.
607dfc1153Chris Williamson	 */
608dfc1153Chris Williamson	int rv = 0;
609dfc1153Chris Williamson	nvlist_t *errlist_raw = fnvlist_lookup_nvlist(result, ZCP_RET_RETURN);
610dfc1153Chris Williamson	for (nvpair_t *pair = nvlist_next_nvpair(errlist_raw, NULL);
611dfc1153Chris Williamson	    pair != NULL; pair = nvlist_next_nvpair(errlist_raw, pair)) {
612dfc1153Chris Williamson		int32_t val = (int32_t)fnvpair_value_int64(pair);
613dfc1153Chris Williamson		if (rv == 0)
614dfc1153Chris Williamson			rv = val;
615dfc1153Chris Williamson		fnvlist_add_int32(errlist, nvpair_name(pair), val);
616dfc1153Chris Williamson	}
617dfc1153Chris Williamson	fnvlist_free(result);
618dfc1153Chris Williamson	return (rv);
6193b2aab1Matthew Ahrens}
6203b2aab1Matthew Ahrens
6213b2aab1Matthew Ahrensint
6223b2aab1Matthew Ahrensdsl_destroy_snapshot(const char *name, boolean_t defer)
6233b2aab1Matthew Ahrens{
6243b2aab1Matthew Ahrens	int error;
6253b2aab1Matthew Ahrens	nvlist_t *nvl = fnvlist_alloc();
6263b2aab1Matthew Ahrens	nvlist_t *errlist = fnvlist_alloc();
6273b2aab1Matthew Ahrens
6283b2aab1Matthew Ahrens	fnvlist_add_boolean(nvl, name);
6293b2aab1Matthew Ahrens	error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
6303b2aab1Matthew Ahrens	fnvlist_free(errlist);
6313b2aab1Matthew Ahrens	fnvlist_free(nvl);
6323b2aab1Matthew Ahrens	return (error);
6333b2aab1Matthew Ahrens}
6343b2aab1Matthew Ahrens
6353b2aab1Matthew Ahrensstruct killarg {
6363b2aab1Matthew Ahrens	dsl_dataset_t *ds;
6373b2aab1Matthew Ahrens	dmu_tx_t *tx;
6383b2aab1Matthew Ahrens};
6393b2aab1Matthew Ahrens
6403b2aab1Matthew Ahrens/* ARGSUSED */
6413b2aab1Matthew Ahrensstatic int
6423b2aab1Matthew Ahrenskill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
6437802d7bMatthew Ahrens    const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
6443b2aab1Matthew Ahrens{
6453b2aab1Matthew Ahrens	struct killarg *ka = arg;
6463b2aab1Matthew Ahrens	dmu_tx_t *tx = ka->tx;
6473b2aab1Matthew Ahrens
648a2cdcddPaul Dagnelie	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
6493b2aab1Matthew Ahrens		return (0);
6503b2aab1Matthew Ahrens
6513b2aab1Matthew Ahrens	if (zb->zb_level == ZB_ZIL_LEVEL) {
6523b2aab1Matthew Ahrens		ASSERT(zilog != NULL);
6533b2aab1Matthew Ahrens		/*
6543b2aab1Matthew Ahrens		 * It's a block in the intent log.  It has no
6553b2aab1Matthew Ahrens		 * accounting, so just free it.
6563b2aab1Matthew Ahrens		 */
6573b2aab1Matthew Ahrens		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
6583b2aab1Matthew Ahrens	} else {
6593b2aab1Matthew Ahrens		ASSERT(zilog == NULL);
660c137962Justin T. Gibbs		ASSERT3U(bp->blk_birth, >,
661c137962Justin T. Gibbs		    dsl_dataset_phys(ka->ds)->ds_prev_snap_txg);
6623b2aab1Matthew Ahrens		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
6633b2aab1Matthew Ahrens	}
6643b2aab1Matthew Ahrens
6653b2aab1Matthew Ahrens	return (0);
6663b2aab1Matthew Ahrens}
6673b2aab1Matthew Ahrens
6683b2aab1Matthew Ahrensstatic void
6693b2aab1Matthew Ahrensold_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
6703b2aab1Matthew Ahrens{
6713b2aab1Matthew Ahrens	struct killarg ka;
6723b2aab1Matthew Ahrens
6733b2aab1Matthew Ahrens	/*
6743b2aab1Matthew Ahrens	 * Free everything that we point to (that's born after
6753b2aab1Matthew Ahrens	 * the previous snapshot, if we are a clone)
6763b2aab1Matthew Ahrens	 *
6773b2aab1Matthew Ahrens	 * NB: this should be very quick, because we already
6783b2aab1Matthew Ahrens	 * freed all the objects in open context.
6793b2aab1Matthew Ahrens	 */
6803b2aab1Matthew Ahrens	ka.ds = ds;
6813b2aab1Matthew Ahrens	ka.tx = tx;
6823b2aab1Matthew Ahrens	VERIFY0(traverse_dataset(ds,
683eb63303Tom Caputi	    dsl_dataset_phys(ds)->ds_prev_snap_txg, TRAVERSE_POST |
684eb63303Tom Caputi	    TRAVERSE_NO_DECRYPT, kill_blkptr, &ka));
685c137962Justin T. Gibbs	ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
686c137962Justin T. Gibbs	    dsl_dataset_phys(ds)->ds_unique_bytes == 0);
6873b2aab1Matthew Ahrens}
6883b2aab1Matthew Ahrens
6893b2aab1Matthew Ahrensint
6903b2aab1Matthew Ahrensdsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
6913b2aab1Matthew Ahrens{
6923b2aab1Matthew Ahrens	int error;
6933b2aab1Matthew Ahrens	uint64_t count;
6943b2aab1Matthew Ahrens	objset_t *mos;
6953b2aab1Matthew Ahrens
696bc9014eJustin Gibbs	ASSERT(!ds->ds_is_snapshot);
697bc9014eJustin Gibbs	if (ds->ds_is_snapshot)
698be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
6993b2aab1Matthew Ahrens
700e914aceTim Schumacher	if (zfs_refcount_count(&ds->ds_longholds) != expected_holds)
701be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
7023b2aab1Matthew Ahrens
7033b2aab1Matthew Ahrens	mos = ds->ds_dir->dd_pool->dp_meta_objset;
7043b2aab1Matthew Ahrens
7053b2aab1Matthew Ahrens	/*
7063b2aab1Matthew Ahrens	 * Can't delete a head dataset if there are snapshots of it.
7073b2aab1Matthew Ahrens	 * (Except if the only snapshots are from the branch we cloned
7083b2aab1Matthew Ahrens	 * from.)
7093b2aab1Matthew Ahrens	 */
7103b2aab1Matthew Ahrens	if (ds->ds_prev != NULL &&
711c137962Justin T. Gibbs	    dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == ds->ds_object)
712be6fd75Matthew Ahrens		return (SET_ERROR(EBUSY));
7133b2aab1Matthew Ahrens
7143b2aab1Matthew Ahrens	/*
7153b2aab1Matthew Ahrens	 * Can't delete if there are children of this fs.
7163b2aab1Matthew Ahrens	 */
7173b2aab1Matthew Ahrens	error = zap_count(mos,
718c137962Justin T. Gibbs	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &count);
7193b2aab1Matthew Ahrens	if (error != 0)
7203b2aab1Matthew Ahrens		return (error);
7213b2aab1Matthew Ahrens	if (count != 0)
722be6fd75Matthew Ahrens		return (SET_ERROR(EEXIST));
7233b2aab1Matthew Ahrens
7243b2aab1Matthew Ahrens	if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
725c137962Justin T. Gibbs	    dsl_dataset_phys(ds->ds_prev)->ds_num_children == 2 &&
7263b2aab1Matthew Ahrens	    ds->ds_prev->ds_userrefs == 0) {
7273b2aab1Matthew Ahrens		/* We need to remove the origin snapshot as well. */
728e914aceTim Schumacher		if (!zfs_refcount_is_zero(&ds->ds_prev->ds_longholds))
729be6fd75Matthew Ahrens			return (SET_ERROR(EBUSY));
7303b2aab1Matthew Ahrens	}
7313b2aab1Matthew Ahrens	return (0);
7323b2aab1Matthew Ahrens}
7333b2aab1Matthew Ahrens
734dfc1153Chris Williamsonint
7353b2aab1Matthew Ahrensdsl_destroy_head_check(void *arg, dmu_tx_t *tx)
7363b2aab1Matthew Ahrens{
7373b2aab1Matthew Ahrens	dsl_destroy_head_arg_t *ddha = arg;
7383b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
7393b2aab1Matthew Ahrens	dsl_dataset_t *ds;
7403b2aab1Matthew Ahrens	int error;
7413b2aab1Matthew Ahrens
7423b2aab1Matthew Ahrens	error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
7433b2aab1Matthew Ahrens	if (error != 0)
7443b2aab1Matthew Ahrens		return (error);
7453b2aab1Matthew Ahrens
7463b2aab1Matthew Ahrens	error = dsl_destroy_head_check_impl(ds, 0);
7473b2aab1Matthew Ahrens	dsl_dataset_rele(ds, FTAG);
7483b2aab1Matthew Ahrens	return (error);
7493b2aab1Matthew Ahrens}
7503b2aab1Matthew Ahrens
7513b2aab1Matthew Ahrensstatic void
7523b2aab1Matthew Ahrensdsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
7533b2aab1Matthew Ahrens{
7543b2aab1Matthew Ahrens	dsl_dir_t *dd;
7553b2aab1Matthew Ahrens	dsl_pool_t *dp = dmu_tx_pool(tx);
7563b2aab1Matthew Ahrens	objset_t *mos = dp->dp_meta_objset;
7573b2aab1Matthew Ahrens	dd_used_t t;
7583b2aab1Matthew Ahrens
7593b2aab1Matthew Ahrens	ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
7603b2aab1Matthew Ahrens
7613b2aab1Matthew Ahrens	VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
7623b2aab1Matthew Ahrens
763c137962Justin T. Gibbs	ASSERT0(dsl_dir_phys(dd)->dd_head_dataset_obj);
7643b2aab1Matthew Ahrens
7653b2aab1Matthew Ahrens	/*
766a2afb61Jerry Jelinek	 * Decrement the filesystem count for all parent filesystems.
767a2afb61Jerry Jelinek	 *
768a2afb61Jerry Jelinek	 * When we receive an incremental stream into a filesystem that already
769a2afb61Jerry Jelinek	 * exists, a temporary clone is created.  We never count this temporary
770a2afb61Jerry Jelinek	 * clone, whose name begins with a '%'.
771a2afb61Jerry Jelinek	 */
772a2afb61Jerry Jelinek	if (dd->dd_myname[0] != '%' && dd->dd_parent != NULL)
773a2afb61Jerry Jelinek		dsl_fs_ss_count_adjust(dd->dd_parent, -1,
774a2afb61Jerry Jelinek		    DD_FIELD_FILESYSTEM_COUNT, tx);
775a2afb61Jerry Jelinek
776a2afb61Jerry Jelinek	/*
7773b2aab1Matthew Ahrens	 * Remove our reservation. The impl() routine avoids setting the
7783b2aab1Matthew Ahrens	 * actual property, which would require the (already destroyed) ds.
7793b2aab1Matthew Ahrens	 */
7803b2aab1Matthew Ahrens	dsl_dir_set_reservation_sync_impl(dd, 0, tx);
7813b2aab1Matthew Ahrens
782c137962Justin T. Gibbs	ASSERT0(dsl_dir_phys(dd)->dd_used_bytes);
783c137962Justin T. Gibbs	ASSERT0(dsl_dir_phys(dd)->dd_reserved);
7843b2aab1Matthew Ahrens	for (t = 0; t < DD_USED_NUM; t++)
785c137962Justin T. Gibbs		ASSERT0(dsl_dir_phys(dd)->dd_used_breakdown[t]);
7863b2aab1Matthew Ahrens
787eb63303Tom Caputi	if (dd->dd_crypto_obj != 0) {
788eb63303Tom Caputi		dsl_crypto_key_destroy_sync(dd->dd_crypto_obj, tx);
789eb63303Tom Caputi		(void) spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object);
790eb63303Tom Caputi	}
791eb63303Tom Caputi
792c137962Justin T. Gibbs	VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_child_dir_zapobj, tx));
793c137962Justin T. Gibbs	VERIFY0(zap_destroy(mos, dsl_dir_phys(dd)->dd_props_zapobj, tx));
794