1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
225afc78aaSChris Kirby  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23187d6ac0SMatt Ahrens  * Copyright (c) 2011 by Delphix. All rights reserved.
24*4e3c9f44SBill Pijewski  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/dmu_objset.h>
28fa9e4066Sahrens #include <sys/dsl_dataset.h>
29fa9e4066Sahrens #include <sys/dsl_dir.h>
3099653d4eSeschrock #include <sys/dsl_prop.h>
311d452cf5Sahrens #include <sys/dsl_synctask.h>
32fa9e4066Sahrens #include <sys/dmu_traverse.h>
33*4e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
34fa9e4066Sahrens #include <sys/dmu_tx.h>
35fa9e4066Sahrens #include <sys/arc.h>
36fa9e4066Sahrens #include <sys/zio.h>
37fa9e4066Sahrens #include <sys/zap.h>
38fa9e4066Sahrens #include <sys/unique.h>
39fa9e4066Sahrens #include <sys/zfs_context.h>
40cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
41ecd6cf80Smarks #include <sys/spa.h>
42088f3894Sahrens #include <sys/zfs_znode.h>
43c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
44842727c2SChris Kirby #include <sys/zvol.h>
453f9d6ad7SLin Ling #include <sys/dsl_scan.h>
46cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
47fa9e4066Sahrens 
48745cd3c5Smaybee static char *dsl_reaper = "the grim reaper";
49745cd3c5Smaybee 
501d452cf5Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check;
511d452cf5Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync;
52a9799022Sck static dsl_syncfunc_t dsl_dataset_set_reservation_sync;
53e1930233Sbonwick 
54cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
55cde58dbcSMatthew Ahrens 	{ \
56cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
57cde58dbcSMatthew Ahrens 		(x) = (y); \
58cde58dbcSMatthew Ahrens 		(y) = __tmp; \
59cde58dbcSMatthew Ahrens 	}
60cde58dbcSMatthew Ahrens 
6155434c77Sek #define	DS_REF_MAX	(1ULL << 62)
62fa9e4066Sahrens 
63fa9e4066Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
64fa9e4066Sahrens 
65745cd3c5Smaybee #define	DSL_DATASET_IS_DESTROYED(ds)	((ds)->ds_owner == dsl_reaper)
66745cd3c5Smaybee 
67fa9e4066Sahrens 
68a9799022Sck /*
69a9799022Sck  * Figure out how much of this delta should be propogated to the dsl_dir
70a9799022Sck  * layer.  If there's a refreservation, that space has already been
71a9799022Sck  * partially accounted for in our ancestors.
72a9799022Sck  */
73a9799022Sck static int64_t
74a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta)
75a9799022Sck {
76a9799022Sck 	uint64_t old_bytes, new_bytes;
77a9799022Sck 
78a9799022Sck 	if (ds->ds_reserved == 0)
79a9799022Sck 		return (delta);
80a9799022Sck 
81a9799022Sck 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
82a9799022Sck 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
83a9799022Sck 
84a9799022Sck 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
85a9799022Sck 	return (new_bytes - old_bytes);
86a9799022Sck }
87fa9e4066Sahrens 
88fa9e4066Sahrens void
89b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
90fa9e4066Sahrens {
91b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
92fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
93fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
94a9799022Sck 	int64_t delta;
95fa9e4066Sahrens 
963f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
97fa9e4066Sahrens 
98fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
99fa9e4066Sahrens 	/* It could have been compressed away to nothing */
100fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
101fa9e4066Sahrens 		return;
102fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
103fa9e4066Sahrens 	ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES);
104fa9e4066Sahrens 	if (ds == NULL) {
105fa9e4066Sahrens 		/*
106fa9e4066Sahrens 		 * Account for the meta-objset space in its placeholder
107fa9e4066Sahrens 		 * dsl_dir.
108fa9e4066Sahrens 		 */
109fa9e4066Sahrens 		ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */
11074e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
111fa9e4066Sahrens 		    used, compressed, uncompressed, tx);
112fa9e4066Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
113fa9e4066Sahrens 		return;
114fa9e4066Sahrens 	}
115fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1163f9d6ad7SLin Ling 
11702c8f3f0SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
118fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
119a9799022Sck 	delta = parent_delta(ds, used);
120fa9e4066Sahrens 	ds->ds_phys->ds_used_bytes += used;
121fa9e4066Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
122fa9e4066Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
123fa9e4066Sahrens 	ds->ds_phys->ds_unique_bytes += used;
124fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
12574e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
12674e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
12774e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
12874e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
12902c8f3f0SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
130fa9e4066Sahrens }
131fa9e4066Sahrens 
132cdb0ab79Smaybee int
133b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
134b24ab676SJeff Bonwick     boolean_t async)
135fa9e4066Sahrens {
136fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
137cdb0ab79Smaybee 		return (0);
138fa9e4066Sahrens 
139b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
140b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
141b24ab676SJeff Bonwick 
142b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
143b24ab676SJeff Bonwick 	int compressed = BP_GET_PSIZE(bp);
144b24ab676SJeff Bonwick 	int uncompressed = BP_GET_UCSIZE(bp);
145b24ab676SJeff Bonwick 
146fa9e4066Sahrens 	ASSERT(used > 0);
147fa9e4066Sahrens 	if (ds == NULL) {
148fa9e4066Sahrens 		/*
149fa9e4066Sahrens 		 * Account for the meta-objset space in its placeholder
150fa9e4066Sahrens 		 * dataset.
151fa9e4066Sahrens 		 */
152b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
153fa9e4066Sahrens 
15474e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD,
155fa9e4066Sahrens 		    -used, -compressed, -uncompressed, tx);
156fa9e4066Sahrens 		dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx);
157cdb0ab79Smaybee 		return (used);
158fa9e4066Sahrens 	}
159fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
160fa9e4066Sahrens 
16174e7dc98SMatthew Ahrens 	ASSERT(!dsl_dataset_is_snapshot(ds));
162fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
163fa9e4066Sahrens 
164fa9e4066Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
165a9799022Sck 		int64_t delta;
166c717a561Smaybee 
1673f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
168b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
169fa9e4066Sahrens 
17002c8f3f0SMatthew Ahrens 		mutex_enter(&ds->ds_dir->dd_lock);
171fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
172a9799022Sck 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
173a9799022Sck 		    !DS_UNIQUE_IS_ACCURATE(ds));
174a9799022Sck 		delta = parent_delta(ds, -used);
175fa9e4066Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
176fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
17774e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
178a9799022Sck 		    delta, -compressed, -uncompressed, tx);
17974e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
18074e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
18102c8f3f0SMatthew Ahrens 		mutex_exit(&ds->ds_dir->dd_lock);
182fa9e4066Sahrens 	} else {
183fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
184b24ab676SJeff Bonwick 		if (async) {
185b24ab676SJeff Bonwick 			/*
186b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
187b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
188cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
189b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
190b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
191b24ab676SJeff Bonwick 			 */
192cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
193b24ab676SJeff Bonwick 		} else {
194cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
195b24ab676SJeff Bonwick 		}
196a4611edeSahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
197a4611edeSahrens 		    ds->ds_phys->ds_prev_snap_obj);
198a4611edeSahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
199fa9e4066Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
200a4611edeSahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
201a4611edeSahrens 		    ds->ds_object && bp->blk_birth >
202a4611edeSahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
203a4611edeSahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
204a4611edeSahrens 			mutex_enter(&ds->ds_prev->ds_lock);
205a4611edeSahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
206a4611edeSahrens 			mutex_exit(&ds->ds_prev->ds_lock);
207fa9e4066Sahrens 		}
2083f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
20974e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
21074e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
21174e7dc98SMatthew Ahrens 		}
212fa9e4066Sahrens 	}
213fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
214fa9e4066Sahrens 	ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used);
215fa9e4066Sahrens 	ds->ds_phys->ds_used_bytes -= used;
216fa9e4066Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
217fa9e4066Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
218fa9e4066Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
219fa9e4066Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
220fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
221cdb0ab79Smaybee 
222cdb0ab79Smaybee 	return (used);
223fa9e4066Sahrens }
224fa9e4066Sahrens 
225ea8dc4b6Seschrock uint64_t
226ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
227fa9e4066Sahrens {
228a2eea2e1Sahrens 	uint64_t trysnap = 0;
229a2eea2e1Sahrens 
230fa9e4066Sahrens 	if (ds == NULL)
231ea8dc4b6Seschrock 		return (0);
232fa9e4066Sahrens 	/*
233fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
234fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
235fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
236fa9e4066Sahrens 	 * consume, which is OK.
237fa9e4066Sahrens 	 *
238fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
239fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
240fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
241fa9e4066Sahrens 	 */
242a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
243a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
244a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
245a2eea2e1Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
246ea8dc4b6Seschrock }
247ea8dc4b6Seschrock 
2483d692628SSanjeev Bagewadi boolean_t
249c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
250c7cd2421SGeorge Wilson     uint64_t blk_birth)
251ea8dc4b6Seschrock {
252c7cd2421SGeorge Wilson 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
253c7cd2421SGeorge Wilson 		return (B_FALSE);
254c7cd2421SGeorge Wilson 
255837b568bSGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
256c7cd2421SGeorge Wilson 
257c7cd2421SGeorge Wilson 	return (B_TRUE);
258fa9e4066Sahrens }
259fa9e4066Sahrens 
260fa9e4066Sahrens /* ARGSUSED */
261fa9e4066Sahrens static void
262fa9e4066Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
263fa9e4066Sahrens {
264fa9e4066Sahrens 	dsl_dataset_t *ds = dsv;
265fa9e4066Sahrens 
266745cd3c5Smaybee 	ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds));
267fa9e4066Sahrens 
26891ebeef5Sahrens 	unique_remove(ds->ds_fsid_guid);
269fa9e4066Sahrens 
270503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
271503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
272fa9e4066Sahrens 
273fa9e4066Sahrens 	if (ds->ds_prev) {
274745cd3c5Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
275fa9e4066Sahrens 		ds->ds_prev = NULL;
276fa9e4066Sahrens 	}
277fa9e4066Sahrens 
278cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
279cde58dbcSMatthew Ahrens 	if (db != NULL) {
280cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
281cde58dbcSMatthew Ahrens 	} else {
282cde58dbcSMatthew Ahrens 		ASSERT(ds->ds_deadlist.dl_dbuf == NULL);
283cde58dbcSMatthew Ahrens 		ASSERT(!ds->ds_deadlist.dl_oldfmt);
284cde58dbcSMatthew Ahrens 	}
285745cd3c5Smaybee 	if (ds->ds_dir)
286745cd3c5Smaybee 		dsl_dir_close(ds->ds_dir, ds);
287fa9e4066Sahrens 
28891ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
289fa9e4066Sahrens 
2905ad82045Snd 	mutex_destroy(&ds->ds_lock);
291f4b94bdeSMatthew Ahrens 	mutex_destroy(&ds->ds_recvlock);
29291ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
293745cd3c5Smaybee 	rw_destroy(&ds->ds_rwlock);
294745cd3c5Smaybee 	cv_destroy(&ds->ds_exclusive_cv);
2955ad82045Snd 
296fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
297fa9e4066Sahrens }
298fa9e4066Sahrens 
299ea8dc4b6Seschrock static int
300fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
301fa9e4066Sahrens {
302fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
303fa9e4066Sahrens 	int err;
304fa9e4066Sahrens 	dmu_buf_t *headdbuf;
305fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
306fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
307fa9e4066Sahrens 
308fa9e4066Sahrens 	if (ds->ds_snapname[0])
309ea8dc4b6Seschrock 		return (0);
310fa9e4066Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
311ea8dc4b6Seschrock 		return (0);
312fa9e4066Sahrens 
313ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
314ea8dc4b6Seschrock 	    FTAG, &headdbuf);
315ea8dc4b6Seschrock 	if (err)
316ea8dc4b6Seschrock 		return (err);
317fa9e4066Sahrens 	headphys = headdbuf->db_data;
318fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
319e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
320ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
321ea8dc4b6Seschrock 	return (err);
322fa9e4066Sahrens }
323fa9e4066Sahrens 
324ab04eb8eStimh static int
325745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
326ab04eb8eStimh {
327745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
328745cd3c5Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
329ab04eb8eStimh 	matchtype_t mt;
330ab04eb8eStimh 	int err;
331ab04eb8eStimh 
332745cd3c5Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
333ab04eb8eStimh 		mt = MT_FIRST;
334ab04eb8eStimh 	else
335ab04eb8eStimh 		mt = MT_EXACT;
336ab04eb8eStimh 
337745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
338ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
339ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
340745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
341ab04eb8eStimh 	return (err);
342ab04eb8eStimh }
343ab04eb8eStimh 
344ab04eb8eStimh static int
345745cd3c5Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx)
346ab04eb8eStimh {
347745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
348745cd3c5Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
349ab04eb8eStimh 	matchtype_t mt;
350ab04eb8eStimh 	int err;
351ab04eb8eStimh 
35271eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
35371eb0538SChris Kirby 
354745cd3c5Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
355ab04eb8eStimh 		mt = MT_FIRST;
356ab04eb8eStimh 	else
357ab04eb8eStimh 		mt = MT_EXACT;
358ab04eb8eStimh 
359745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
360ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
361745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
362ab04eb8eStimh 	return (err);
363ab04eb8eStimh }
364ab04eb8eStimh 
365745cd3c5Smaybee static int
366745cd3c5Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag,
367745cd3c5Smaybee     dsl_dataset_t **dsp)
368fa9e4066Sahrens {
369fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
370fa9e4066Sahrens 	dmu_buf_t *dbuf;
371fa9e4066Sahrens 	dsl_dataset_t *ds;
372ea8dc4b6Seschrock 	int err;
373a7f53a56SChris Kirby 	dmu_object_info_t doi;
374fa9e4066Sahrens 
375fa9e4066Sahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
376fa9e4066Sahrens 	    dsl_pool_sync_context(dp));
377fa9e4066Sahrens 
378ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
379ea8dc4b6Seschrock 	if (err)
380ea8dc4b6Seschrock 		return (err);
381a7f53a56SChris Kirby 
382a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
383a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
384a7f53a56SChris Kirby 	if (doi.doi_type != DMU_OT_DSL_DATASET)
385a7f53a56SChris Kirby 		return (EINVAL);
386a7f53a56SChris Kirby 
387fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
388fa9e4066Sahrens 	if (ds == NULL) {
389fa9e4066Sahrens 		dsl_dataset_t *winner;
390fa9e4066Sahrens 
391fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
392fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
393fa9e4066Sahrens 		ds->ds_object = dsobj;
394fa9e4066Sahrens 		ds->ds_phys = dbuf->db_data;
395fa9e4066Sahrens 
3965ad82045Snd 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
397f4b94bdeSMatthew Ahrens 		mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL);
39891ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
399*4e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
400*4e3c9f44SBill Pijewski 
401745cd3c5Smaybee 		rw_init(&ds->ds_rwlock, 0, 0, 0);
402745cd3c5Smaybee 		cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL);
4035ad82045Snd 
404cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
405cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
406fa9e4066Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
407cde58dbcSMatthew Ahrens 
408*4e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
409*4e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
410*4e3c9f44SBill Pijewski 
411ea8dc4b6Seschrock 		if (err == 0) {
412ea8dc4b6Seschrock 			err = dsl_dir_open_obj(dp,
413ea8dc4b6Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
414ea8dc4b6Seschrock 		}
415ea8dc4b6Seschrock 		if (err) {
4165ad82045Snd 			mutex_destroy(&ds->ds_lock);
417f4b94bdeSMatthew Ahrens 			mutex_destroy(&ds->ds_recvlock);
41891ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
419745cd3c5Smaybee 			rw_destroy(&ds->ds_rwlock);
420745cd3c5Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
421cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
422cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
423ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
424ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
425ea8dc4b6Seschrock 			return (err);
426ea8dc4b6Seschrock 		}
427fa9e4066Sahrens 
42874e7dc98SMatthew Ahrens 		if (!dsl_dataset_is_snapshot(ds)) {
429fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
430fa9e4066Sahrens 			if (ds->ds_phys->ds_prev_snap_obj) {
431745cd3c5Smaybee 				err = dsl_dataset_get_ref(dp,
432745cd3c5Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
433745cd3c5Smaybee 				    ds, &ds->ds_prev);
434fa9e4066Sahrens 			}
435842727c2SChris Kirby 		} else {
436842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
437842727c2SChris Kirby 				err = dsl_dataset_get_snapname(ds);
438842727c2SChris Kirby 			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
439842727c2SChris Kirby 				err = zap_count(
440842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
441842727c2SChris Kirby 				    ds->ds_phys->ds_userrefs_obj,
442842727c2SChris Kirby 				    &ds->ds_userrefs);
443842727c2SChris Kirby 			}
444fa9e4066Sahrens 		}
445fa9e4066Sahrens 
44674e7dc98SMatthew Ahrens 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
44727345066Sck 			/*
44827345066Sck 			 * In sync context, we're called with either no lock
44927345066Sck 			 * or with the write lock.  If we're not syncing,
45027345066Sck 			 * we're always called with the read lock held.
45127345066Sck 			 */
452cb625fb5Sck 			boolean_t need_lock =
45327345066Sck 			    !RW_WRITE_HELD(&dp->dp_config_rwlock) &&
45427345066Sck 			    dsl_pool_sync_context(dp);
455cb625fb5Sck 
456cb625fb5Sck 			if (need_lock)
457cb625fb5Sck 				rw_enter(&dp->dp_config_rwlock, RW_READER);
458cb625fb5Sck 
459bb0ade09Sahrens 			err = dsl_prop_get_ds(ds,
460cb625fb5Sck 			    "refreservation", sizeof (uint64_t), 1,
461cb625fb5Sck 			    &ds->ds_reserved, NULL);
462cb625fb5Sck 			if (err == 0) {
463bb0ade09Sahrens 				err = dsl_prop_get_ds(ds,
464cb625fb5Sck 				    "refquota", sizeof (uint64_t), 1,
465cb625fb5Sck 				    &ds->ds_quota, NULL);
466cb625fb5Sck 			}
467cb625fb5Sck 
468cb625fb5Sck 			if (need_lock)
469cb625fb5Sck 				rw_exit(&dp->dp_config_rwlock);
470cb625fb5Sck 		} else {
471cb625fb5Sck 			ds->ds_reserved = ds->ds_quota = 0;
472cb625fb5Sck 		}
473cb625fb5Sck 
474ea8dc4b6Seschrock 		if (err == 0) {
475ea8dc4b6Seschrock 			winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys,
476ea8dc4b6Seschrock 			    dsl_dataset_evict);
477ea8dc4b6Seschrock 		}
478ea8dc4b6Seschrock 		if (err || winner) {
479cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
480cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
481745cd3c5Smaybee 			if (ds->ds_prev)
482745cd3c5Smaybee 				dsl_dataset_drop_ref(ds->ds_prev, ds);
483fa9e4066Sahrens 			dsl_dir_close(ds->ds_dir, ds);
4845ad82045Snd 			mutex_destroy(&ds->ds_lock);
485f4b94bdeSMatthew Ahrens 			mutex_destroy(&ds->ds_recvlock);
48691ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
487745cd3c5Smaybee 			rw_destroy(&ds->ds_rwlock);
488745cd3c5Smaybee 			cv_destroy(&ds->ds_exclusive_cv);
489fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
490ea8dc4b6Seschrock 			if (err) {
491ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
492ea8dc4b6Seschrock 				return (err);
493ea8dc4b6Seschrock 			}
494fa9e4066Sahrens 			ds = winner;
495fa9e4066Sahrens 		} else {
49691ebeef5Sahrens 			ds->ds_fsid_guid =
497fa9e4066Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
498fa9e4066Sahrens 		}
499fa9e4066Sahrens 	}
500fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
501fa9e4066Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
502088f3894Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
503afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
50484db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
505fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
506745cd3c5Smaybee 	if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) {
507fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
508745cd3c5Smaybee 		dmu_buf_rele(ds->ds_dbuf, tag);
509745cd3c5Smaybee 		return (ENOENT);
510fa9e4066Sahrens 	}
511fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
512ea8dc4b6Seschrock 	*dsp = ds;
513ea8dc4b6Seschrock 	return (0);
514fa9e4066Sahrens }
515fa9e4066Sahrens 
516745cd3c5Smaybee static int
517745cd3c5Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag)
518745cd3c5Smaybee {
519745cd3c5Smaybee 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
520745cd3c5Smaybee 
521745cd3c5Smaybee 	/*
522745cd3c5Smaybee 	 * In syncing context we don't want the rwlock lock: there
523745cd3c5Smaybee 	 * may be an existing writer waiting for sync phase to
524745cd3c5Smaybee 	 * finish.  We don't need to worry about such writers, since
525745cd3c5Smaybee 	 * sync phase is single-threaded, so the writer can't be
526745cd3c5Smaybee 	 * doing anything while we are active.
527745cd3c5Smaybee 	 */
528745cd3c5Smaybee 	if (dsl_pool_sync_context(dp)) {
529745cd3c5Smaybee 		ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
530745cd3c5Smaybee 		return (0);
531745cd3c5Smaybee 	}
532745cd3c5Smaybee 
533745cd3c5Smaybee 	/*
534745cd3c5Smaybee 	 * Normal users will hold the ds_rwlock as a READER until they
535745cd3c5Smaybee 	 * are finished (i.e., call dsl_dataset_rele()).  "Owners" will
536745cd3c5Smaybee 	 * drop their READER lock after they set the ds_owner field.
537745cd3c5Smaybee 	 *
538745cd3c5Smaybee 	 * If the dataset is being destroyed, the destroy thread will
539745cd3c5Smaybee 	 * obtain a WRITER lock for exclusive access after it's done its
540745cd3c5Smaybee 	 * open-context work and then change the ds_owner to
541745cd3c5Smaybee 	 * dsl_reaper once destruction is assured.  So threads
542745cd3c5Smaybee 	 * may block here temporarily, until the "destructability" of
543745cd3c5Smaybee 	 * the dataset is determined.
544745cd3c5Smaybee 	 */
545745cd3c5Smaybee 	ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock));
546745cd3c5Smaybee 	mutex_enter(&ds->ds_lock);
547745cd3c5Smaybee 	while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) {
548745cd3c5Smaybee 		rw_exit(&dp->dp_config_rwlock);
549745cd3c5Smaybee 		cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock);
550745cd3c5Smaybee 		if (DSL_DATASET_IS_DESTROYED(ds)) {
551745cd3c5Smaybee 			mutex_exit(&ds->ds_lock);
552745cd3c5Smaybee 			dsl_dataset_drop_ref(ds, tag);
553745cd3c5Smaybee 			rw_enter(&dp->dp_config_rwlock, RW_READER);
554745cd3c5Smaybee 			return (ENOENT);
555745cd3c5Smaybee 		}
556620252bcSChris Kirby 		/*
557620252bcSChris Kirby 		 * The dp_config_rwlock lives above the ds_lock. And
558620252bcSChris Kirby 		 * we need to check DSL_DATASET_IS_DESTROYED() while
559620252bcSChris Kirby 		 * holding the ds_lock, so we have to drop and reacquire
560620252bcSChris Kirby 		 * the ds_lock here.
561620252bcSChris Kirby 		 */
562620252bcSChris Kirby 		mutex_exit(&ds->ds_lock);
563745cd3c5Smaybee 		rw_enter(&dp->dp_config_rwlock, RW_READER);
564620252bcSChris Kirby 		mutex_enter(&ds->ds_lock);
565745cd3c5Smaybee 	}
566745cd3c5Smaybee 	mutex_exit(&ds->ds_lock);
567745cd3c5Smaybee 	return (0);
568745cd3c5Smaybee }
569745cd3c5Smaybee 
570745cd3c5Smaybee int
571745cd3c5Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
572745cd3c5Smaybee     dsl_dataset_t **dsp)
573745cd3c5Smaybee {
574745cd3c5Smaybee 	int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp);
575745cd3c5Smaybee 
576745cd3c5Smaybee 	if (err)
577745cd3c5Smaybee 		return (err);
578745cd3c5Smaybee 	return (dsl_dataset_hold_ref(*dsp, tag));
579745cd3c5Smaybee }
580745cd3c5Smaybee 
581745cd3c5Smaybee int
582503ad85cSMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok,
583503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
584745cd3c5Smaybee {
585503ad85cSMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
586745cd3c5Smaybee 	if (err)
587745cd3c5Smaybee 		return (err);
588503ad85cSMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
589503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
5904f5064b7SMark J Musante 		*dsp = NULL;
591745cd3c5Smaybee 		return (EBUSY);
592745cd3c5Smaybee 	}
593745cd3c5Smaybee 	return (0);
594745cd3c5Smaybee }
595745cd3c5Smaybee 
596fa9e4066Sahrens int
597745cd3c5Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp)
598fa9e4066Sahrens {
599fa9e4066Sahrens 	dsl_dir_t *dd;
600fa9e4066Sahrens 	dsl_pool_t *dp;
601745cd3c5Smaybee 	const char *snapname;
602fa9e4066Sahrens 	uint64_t obj;
603fa9e4066Sahrens 	int err = 0;
604fa9e4066Sahrens 
605745cd3c5Smaybee 	err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname);
606ea8dc4b6Seschrock 	if (err)
607ea8dc4b6Seschrock 		return (err);
608fa9e4066Sahrens 
609fa9e4066Sahrens 	dp = dd->dd_pool;
610fa9e4066Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
611fa9e4066Sahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
612745cd3c5Smaybee 	if (obj)
613745cd3c5Smaybee 		err = dsl_dataset_get_ref(dp, obj, tag, dsp);
614745cd3c5Smaybee 	else
615fa9e4066Sahrens 		err = ENOENT;
616745cd3c5Smaybee 	if (err)
617fa9e4066Sahrens 		goto out;
618fa9e4066Sahrens 
619745cd3c5Smaybee 	err = dsl_dataset_hold_ref(*dsp, tag);
620fa9e4066Sahrens 
621745cd3c5Smaybee 	/* we may be looking for a snapshot */
622745cd3c5Smaybee 	if (err == 0 && snapname != NULL) {
623745cd3c5Smaybee 		dsl_dataset_t *ds = NULL;
624fa9e4066Sahrens 
625745cd3c5Smaybee 		if (*snapname++ != '@') {
626745cd3c5Smaybee 			dsl_dataset_rele(*dsp, tag);
627fa9e4066Sahrens 			err = ENOENT;
628fa9e4066Sahrens 			goto out;
629fa9e4066Sahrens 		}
630fa9e4066Sahrens 
631745cd3c5Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
632745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
633745cd3c5Smaybee 		if (err == 0)
634745cd3c5Smaybee 			err = dsl_dataset_get_ref(dp, obj, tag, &ds);
635745cd3c5Smaybee 		dsl_dataset_rele(*dsp, tag);
636745cd3c5Smaybee 
637745cd3c5Smaybee 		ASSERT3U((err == 0), ==, (ds != NULL));
638745cd3c5Smaybee 
639745cd3c5Smaybee 		if (ds) {
640745cd3c5Smaybee 			mutex_enter(&ds->ds_lock);
641745cd3c5Smaybee 			if (ds->ds_snapname[0] == 0)
642745cd3c5Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
643745cd3c5Smaybee 				    sizeof (ds->ds_snapname));
644745cd3c5Smaybee 			mutex_exit(&ds->ds_lock);
645745cd3c5Smaybee 			err = dsl_dataset_hold_ref(ds, tag);
646745cd3c5Smaybee 			*dsp = err ? NULL : ds;
647fa9e4066Sahrens 		}
648fa9e4066Sahrens 	}
649fa9e4066Sahrens out:
650fa9e4066Sahrens 	rw_exit(&dp->dp_config_rwlock);
651fa9e4066Sahrens 	dsl_dir_close(dd, FTAG);
652fa9e4066Sahrens 	return (err);
653fa9e4066Sahrens }
654fa9e4066Sahrens 
655fa9e4066Sahrens int
656503ad85cSMatthew Ahrens dsl_dataset_own(const char *name, boolean_t inconsistentok,
657503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
658fa9e4066Sahrens {
659503ad85cSMatthew Ahrens 	int err = dsl_dataset_hold(name, tag, dsp);
660745cd3c5Smaybee 	if (err)
661745cd3c5Smaybee 		return (err);
662503ad85cSMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) {
663503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
664745cd3c5Smaybee 		return (EBUSY);
665745cd3c5Smaybee 	}
666745cd3c5Smaybee 	return (0);
667fa9e4066Sahrens }
668fa9e4066Sahrens 
669fa9e4066Sahrens void
670fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
671fa9e4066Sahrens {
672fa9e4066Sahrens 	if (ds == NULL) {
673fa9e4066Sahrens 		(void) strcpy(name, "mos");
674fa9e4066Sahrens 	} else {
675fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
676ea8dc4b6Seschrock 		VERIFY(0 == dsl_dataset_get_snapname(ds));
677fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
678fa9e4066Sahrens 			(void) strcat(name, "@");
679745cd3c5Smaybee 			/*
680745cd3c5Smaybee 			 * We use a "recursive" mutex so that we
681745cd3c5Smaybee 			 * can call dprintf_ds() with ds_lock held.
682745cd3c5Smaybee 			 */
683fa9e4066Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
684fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
685fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
686fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
687fa9e4066Sahrens 			} else {
688fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
689fa9e4066Sahrens 			}
690fa9e4066Sahrens 		}
691fa9e4066Sahrens 	}
692fa9e4066Sahrens }
693fa9e4066Sahrens 
694b7661cccSmmusante static int
695b7661cccSmmusante dsl_dataset_namelen(dsl_dataset_t *ds)
696b7661cccSmmusante {
697b7661cccSmmusante 	int result;
698b7661cccSmmusante 
699b7661cccSmmusante 	if (ds == NULL) {
700b7661cccSmmusante 		result = 3;	/* "mos" */
701b7661cccSmmusante 	} else {
702b7661cccSmmusante 		result = dsl_dir_namelen(ds->ds_dir);
703b7661cccSmmusante 		VERIFY(0 == dsl_dataset_get_snapname(ds));
704b7661cccSmmusante 		if (ds->ds_snapname[0]) {
705b7661cccSmmusante 			++result;	/* adding one for the @-sign */
706b7661cccSmmusante 			if (!MUTEX_HELD(&ds->ds_lock)) {
707b7661cccSmmusante 				mutex_enter(&ds->ds_lock);
708b7661cccSmmusante 				result += strlen(ds->ds_snapname);
709b7661cccSmmusante 				mutex_exit(&ds->ds_lock);
710b7661cccSmmusante 			} else {
711b7661cccSmmusante 				result += strlen(ds->ds_snapname);
712b7661cccSmmusante 			}
713b7661cccSmmusante 		}
714b7661cccSmmusante 	}
715b7661cccSmmusante 
716b7661cccSmmusante 	return (result);
717b7661cccSmmusante }
718b7661cccSmmusante 
719088f3894Sahrens void
720745cd3c5Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag)
721fa9e4066Sahrens {
722ea8dc4b6Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
723fa9e4066Sahrens }
724fa9e4066Sahrens 
7253cb34c60Sahrens void
726745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
7273cb34c60Sahrens {
728745cd3c5Smaybee 	if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) {
729745cd3c5Smaybee 		rw_exit(&ds->ds_rwlock);
730745cd3c5Smaybee 	}
731745cd3c5Smaybee 	dsl_dataset_drop_ref(ds, tag);
732745cd3c5Smaybee }
733745cd3c5Smaybee 
734745cd3c5Smaybee void
735503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
736745cd3c5Smaybee {
737503ad85cSMatthew Ahrens 	ASSERT((ds->ds_owner == tag && ds->ds_dbuf) ||
738745cd3c5Smaybee 	    (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL));
739745cd3c5Smaybee 
7403cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
741745cd3c5Smaybee 	ds->ds_owner = NULL;
742745cd3c5Smaybee 	if (RW_WRITE_HELD(&ds->ds_rwlock)) {
743745cd3c5Smaybee 		rw_exit(&ds->ds_rwlock);
744745cd3c5Smaybee 		cv_broadcast(&ds->ds_exclusive_cv);
745745cd3c5Smaybee 	}
7463cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
747745cd3c5Smaybee 	if (ds->ds_dbuf)
748503ad85cSMatthew Ahrens 		dsl_dataset_drop_ref(ds, tag);
749745cd3c5Smaybee 	else
750cde58dbcSMatthew Ahrens 		dsl_dataset_evict(NULL, ds);
7513cb34c60Sahrens }
7523cb34c60Sahrens 
7533cb34c60Sahrens boolean_t
754503ad85cSMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag)
7553cb34c60Sahrens {
756745cd3c5Smaybee 	boolean_t gotit = FALSE;
757745cd3c5Smaybee 
7583cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
759745cd3c5Smaybee 	if (ds->ds_owner == NULL &&
760745cd3c5Smaybee 	    (!DS_IS_INCONSISTENT(ds) || inconsistentok)) {
761503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
762745cd3c5Smaybee 		if (!dsl_pool_sync_context(ds->ds_dir->dd_pool))
763745cd3c5Smaybee 			rw_exit(&ds->ds_rwlock);
764745cd3c5Smaybee 		gotit = TRUE;
7653cb34c60Sahrens 	}
7663cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
767745cd3c5Smaybee 	return (gotit);
768745cd3c5Smaybee }
769745cd3c5Smaybee 
770745cd3c5Smaybee void
771745cd3c5Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner)
772745cd3c5Smaybee {
773745cd3c5Smaybee 	ASSERT3P(owner, ==, ds->ds_owner);
774745cd3c5Smaybee 	if (!RW_WRITE_HELD(&ds->ds_rwlock))
775745cd3c5Smaybee 		rw_enter(&ds->ds_rwlock, RW_WRITER);
7763cb34c60Sahrens }
7773cb34c60Sahrens 
7781d452cf5Sahrens uint64_t
779088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
780ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
781fa9e4066Sahrens {
7823cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
783fa9e4066Sahrens 	dmu_buf_t *dbuf;
784fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
7853cb34c60Sahrens 	uint64_t dsobj;
786fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
787fa9e4066Sahrens 
788088f3894Sahrens 	if (origin == NULL)
789088f3894Sahrens 		origin = dp->dp_origin_snap;
790088f3894Sahrens 
7913cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7923cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
793fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7943cb34c60Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
795fa9e4066Sahrens 
7961649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
7971649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
798ea8dc4b6Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
799fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
800fa9e4066Sahrens 	dsphys = dbuf->db_data;
801745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
802fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
803ab04eb8eStimh 	dsphys->ds_flags = flags;
804fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
805fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
806fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
807fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
808ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
809ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
810fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
811088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
812a9799022Sck 
813cde58dbcSMatthew Ahrens 	if (origin == NULL) {
814cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
815cde58dbcSMatthew Ahrens 	} else {
816cde58dbcSMatthew Ahrens 		dsl_dataset_t *ohds;
817cde58dbcSMatthew Ahrens 
8183cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
819fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
8203cb34c60Sahrens 		    origin->ds_phys->ds_creation_txg;
821fa9e4066Sahrens 		dsphys->ds_used_bytes =
8223cb34c60Sahrens 		    origin->ds_phys->ds_used_bytes;
823fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
8243cb34c60Sahrens 		    origin->ds_phys->ds_compressed_bytes;
825fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
8263cb34c60Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
8273cb34c60Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
828579ae4d5Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
829fa9e4066Sahrens 
8303cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
8313cb34c60Sahrens 		origin->ds_phys->ds_num_children++;
832fa9e4066Sahrens 
833cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
834cde58dbcSMatthew Ahrens 		    origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
835cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
836cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
837cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
838cde58dbcSMatthew Ahrens 
839088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
840088f3894Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
841088f3894Sahrens 				origin->ds_phys->ds_next_clones_obj =
842088f3894Sahrens 				    zap_create(mos,
843088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
844088f3894Sahrens 			}
845088f3894Sahrens 			VERIFY(0 == zap_add_int(mos,
846088f3894Sahrens 			    origin->ds_phys->ds_next_clones_obj,
847088f3894Sahrens 			    dsobj, tx));
848088f3894Sahrens 		}
849088f3894Sahrens 
850fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
8513cb34c60Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
852cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
853cde58dbcSMatthew Ahrens 			if (origin->ds_dir->dd_phys->dd_clones == 0) {
854cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
855cde58dbcSMatthew Ahrens 				origin->ds_dir->dd_phys->dd_clones =
856cde58dbcSMatthew Ahrens 				    zap_create(mos,
857cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
858cde58dbcSMatthew Ahrens 			}
859cde58dbcSMatthew Ahrens 			VERIFY3U(0, ==, zap_add_int(mos,
860cde58dbcSMatthew Ahrens 			    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
861cde58dbcSMatthew Ahrens 		}
862fa9e4066Sahrens 	}
863ab04eb8eStimh 
864ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
865ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
866ab04eb8eStimh 
867ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
868fa9e4066Sahrens 
869fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
870fa9e4066Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
8713cb34c60Sahrens 
8723cb34c60Sahrens 	return (dsobj);
8733cb34c60Sahrens }
8743cb34c60Sahrens 
8753cb34c60Sahrens uint64_t
876ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
877ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8783cb34c60Sahrens {
8793cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8803cb34c60Sahrens 	uint64_t dsobj, ddobj;
8813cb34c60Sahrens 	dsl_dir_t *dd;
8823cb34c60Sahrens 
8833cb34c60Sahrens 	ASSERT(lastname[0] != '@');
8843cb34c60Sahrens 
885088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8863cb34c60Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd));
8873cb34c60Sahrens 
888088f3894Sahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx);
8893cb34c60Sahrens 
8903cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8913cb34c60Sahrens 
892fa9e4066Sahrens 	dsl_dir_close(dd, FTAG);
893fa9e4066Sahrens 
894feaa74e4SMark Maybee 	/*
895feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
896feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
897feaa74e4SMark Maybee 	 */
898feaa74e4SMark Maybee 	if (origin != NULL) {
899feaa74e4SMark Maybee 		dsl_dataset_t *ds;
900feaa74e4SMark Maybee 		objset_t *os;
901feaa74e4SMark Maybee 
902feaa74e4SMark Maybee 		VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
903feaa74e4SMark Maybee 		VERIFY3U(0, ==, dmu_objset_from_ds(ds, &os));
904feaa74e4SMark Maybee 		bzero(&os->os_zil_header, sizeof (os->os_zil_header));
905feaa74e4SMark Maybee 		dsl_dataset_dirty(ds, tx);
906feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
907feaa74e4SMark Maybee 	}
908feaa74e4SMark Maybee 
9091d452cf5Sahrens 	return (dsobj);
910fa9e4066Sahrens }
911fa9e4066Sahrens 
9121d452cf5Sahrens /*
91319b94df9SMatthew Ahrens  * The snapshots must all be in the same pool.
9141d452cf5Sahrens  */
9151d452cf5Sahrens int
91619b94df9SMatthew Ahrens dmu_snapshots_destroy_nvl(nvlist_t *snaps, boolean_t defer, char *failed)
9171d452cf5Sahrens {
9181d452cf5Sahrens 	int err;
9191d452cf5Sahrens 	dsl_sync_task_t *dst;
9201d452cf5Sahrens 	spa_t *spa;
92119b94df9SMatthew Ahrens 	nvpair_t *pair;
92219b94df9SMatthew Ahrens 	dsl_sync_task_group_t *dstg;
923e5351341SMatthew Ahrens 
92419b94df9SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
92519b94df9SMatthew Ahrens 	if (pair == NULL)
92619b94df9SMatthew Ahrens 		return (0);
92719b94df9SMatthew Ahrens 
92819b94df9SMatthew Ahrens 	err = spa_open(nvpair_name(pair), &spa, FTAG);
9291d452cf5Sahrens 	if (err)
9301d452cf5Sahrens 		return (err);
93119b94df9SMatthew Ahrens 	dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
9321d452cf5Sahrens 
93319b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
93419b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(snaps, pair)) {
93519b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
93619b94df9SMatthew Ahrens 		int err;
93719b94df9SMatthew Ahrens 
93819b94df9SMatthew Ahrens 		err = dsl_dataset_own(nvpair_name(pair), B_TRUE, dstg, &ds);
93919b94df9SMatthew Ahrens 		if (err == 0) {
94019b94df9SMatthew Ahrens 			struct dsl_ds_destroyarg *dsda;
94119b94df9SMatthew Ahrens 
94219b94df9SMatthew Ahrens 			dsl_dataset_make_exclusive(ds, dstg);
94319b94df9SMatthew Ahrens 			dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg),
94419b94df9SMatthew Ahrens 			    KM_SLEEP);
94519b94df9SMatthew Ahrens 			dsda->ds = ds;
94619b94df9SMatthew Ahrens 			dsda->defer = defer;
94719b94df9SMatthew Ahrens 			dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
94819b94df9SMatthew Ahrens 			    dsl_dataset_destroy_sync, dsda, dstg, 0);
94919b94df9SMatthew Ahrens 		} else if (err == ENOENT) {
95019b94df9SMatthew Ahrens 			err = 0;
95119b94df9SMatthew Ahrens 		} else {
95219b94df9SMatthew Ahrens 			(void) strcpy(failed, nvpair_name(pair));
95319b94df9SMatthew Ahrens 			break;
95419b94df9SMatthew Ahrens 		}
95519b94df9SMatthew Ahrens 	}
9561d452cf5Sahrens 
9571d452cf5Sahrens 	if (err == 0)
95819b94df9SMatthew Ahrens 		err = dsl_sync_task_group_wait(dstg);
9591d452cf5Sahrens 
96019b94df9SMatthew Ahrens 	for (dst = list_head(&dstg->dstg_tasks); dst;
96119b94df9SMatthew Ahrens 	    dst = list_next(&dstg->dstg_tasks, dst)) {
962842727c2SChris Kirby 		struct dsl_ds_destroyarg *dsda = dst->dst_arg1;
963842727c2SChris Kirby 		dsl_dataset_t *ds = dsda->ds;
964842727c2SChris Kirby 
965745cd3c5Smaybee 		/*
966745cd3c5Smaybee 		 * Return the file system name that triggered the error
967745cd3c5Smaybee 		 */
9681d452cf5Sahrens 		if (dst->dst_err) {
96919b94df9SMatthew Ahrens 			dsl_dataset_name(ds, failed);
970e1930233Sbonwick 		}
971842727c2SChris Kirby 		ASSERT3P(dsda->rm_origin, ==, NULL);
97219b94df9SMatthew Ahrens 		dsl_dataset_disown(ds, dstg);
973842727c2SChris Kirby 		kmem_free(dsda, sizeof (struct dsl_ds_destroyarg));
974fa9e4066Sahrens 	}
975fa9e4066Sahrens 
97619b94df9SMatthew Ahrens 	dsl_sync_task_group_destroy(dstg);
9771d452cf5Sahrens 	spa_close(spa, FTAG);
978fa9e4066Sahrens 	return (err);
97919b94df9SMatthew Ahrens 
980fa9e4066Sahrens }
981fa9e4066Sahrens 
982842727c2SChris Kirby static boolean_t
983842727c2SChris Kirby dsl_dataset_might_destroy_origin(dsl_dataset_t *ds)
984842727c2SChris Kirby {
985842727c2SChris Kirby 	boolean_t might_destroy = B_FALSE;
986842727c2SChris Kirby 
987842727c2SChris Kirby 	mutex_enter(&ds->ds_lock);
988842727c2SChris Kirby 	if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 &&
989842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds))
990842727c2SChris Kirby 		might_destroy = B_TRUE;
991842727c2SChris Kirby 	mutex_exit(&ds->ds_lock);
992842727c2SChris Kirby 
993842727c2SChris Kirby 	return (might_destroy);
994842727c2SChris Kirby }
995842727c2SChris Kirby 
996842727c2SChris Kirby /*
997842727c2SChris Kirby  * If we're removing a clone, and these three conditions are true:
998842727c2SChris Kirby  *	1) the clone's origin has no other children
999842727c2SChris Kirby  *	2) the clone's origin has no user references
1000842727c2SChris Kirby  *	3) the clone's origin has been marked for deferred destruction
1001842727c2SChris Kirby  * Then, prepare to remove the origin as part of this sync task group.
1002842727c2SChris Kirby  */
1003842727c2SChris Kirby static int
1004842727c2SChris Kirby dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag)
1005842727c2SChris Kirby {
1006842727c2SChris Kirby 	dsl_dataset_t *ds = dsda->ds;
1007842727c2SChris Kirby 	dsl_dataset_t *origin = ds->ds_prev;
1008842727c2SChris Kirby 
1009842727c2SChris Kirby 	if (dsl_dataset_might_destroy_origin(origin)) {
1010842727c2SChris Kirby 		char *name;
1011842727c2SChris Kirby 		int namelen;
1012842727c2SChris Kirby 		int error;
1013842727c2SChris Kirby 
1014842727c2SChris Kirby 		namelen = dsl_dataset_namelen(origin) + 1;
1015842727c2SChris Kirby 		name = kmem_alloc(namelen, KM_SLEEP);
1016842727c2SChris Kirby 		dsl_dataset_name(origin, name);
1017842727c2SChris Kirby #ifdef _KERNEL
1018842727c2SChris Kirby 		error = zfs_unmount_snap(name, NULL);
1019842727c2SChris Kirby 		if (error) {
1020842727c2SChris Kirby 			kmem_free(name, namelen);
1021842727c2SChris Kirby 			return (error);
1022842727c2SChris Kirby 		}
1023842727c2SChris Kirby #endif
1024503ad85cSMatthew Ahrens 		error = dsl_dataset_own(name, B_TRUE, tag, &origin);
1025842727c2SChris Kirby 		kmem_free(name, namelen);
1026842727c2SChris Kirby 		if (error)
1027842727c2SChris Kirby 			return (error);
1028842727c2SChris Kirby 		dsda->rm_origin = origin;
1029842727c2SChris Kirby 		dsl_dataset_make_exclusive(origin, tag);
1030842727c2SChris Kirby 	}
1031842727c2SChris Kirby 
1032842727c2SChris Kirby 	return (0);
1033842727c2SChris Kirby }
1034842727c2SChris Kirby 
10353cb34c60Sahrens /*
1036745cd3c5Smaybee  * ds must be opened as OWNER.  On return (whether successful or not),
1037745cd3c5Smaybee  * ds will be closed and caller can no longer dereference it.
10383cb34c60Sahrens  */
1039fa9e4066Sahrens int
1040842727c2SChris Kirby dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer)
1041fa9e4066Sahrens {
1042fa9e4066Sahrens 	int err;
10431d452cf5Sahrens 	dsl_sync_task_group_t *dstg;
10441d452cf5Sahrens 	objset_t *os;
1045fa9e4066Sahrens 	dsl_dir_t *dd;
10461d452cf5Sahrens 	uint64_t obj;
104792241e0bSTom Erickson 	struct dsl_ds_destroyarg dsda = { 0 };
104892241e0bSTom Erickson 	dsl_dataset_t dummy_ds = { 0 };
1049842727c2SChris Kirby 
1050842727c2SChris Kirby 	dsda.ds = ds;
10511d452cf5Sahrens 
10523cb34c60Sahrens 	if (dsl_dataset_is_snapshot(ds)) {
10531d452cf5Sahrens 		/* Destroying a snapshot is simpler */
1054745cd3c5Smaybee 		dsl_dataset_make_exclusive(ds, tag);
10553baa08fcSek 
1056842727c2SChris Kirby 		dsda.defer = defer;
10571d452cf5Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10581d452cf5Sahrens 		    dsl_dataset_destroy_check, dsl_dataset_destroy_sync,
1059842727c2SChris Kirby 		    &dsda, tag, 0);
1060842727c2SChris Kirby 		ASSERT3P(dsda.rm_origin, ==, NULL);
10613cb34c60Sahrens 		goto out;
1062922d9a97SChris Kirby 	} else if (defer) {
1063922d9a97SChris Kirby 		err = EINVAL;
1064922d9a97SChris Kirby 		goto out;
10651d452cf5Sahrens 	}
1066fa9e4066Sahrens 
10671d452cf5Sahrens 	dd = ds->ds_dir;
106892241e0bSTom Erickson 	dummy_ds.ds_dir = dd;
106992241e0bSTom Erickson 	dummy_ds.ds_object = ds->ds_object;
1070fa9e4066Sahrens 
10711d452cf5Sahrens 	/*
10721d452cf5Sahrens 	 * Check for errors and mark this ds as inconsistent, in
10731d452cf5Sahrens 	 * case we crash while freeing the objects.
10741d452cf5Sahrens 	 */
10751d452cf5Sahrens 	err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check,
10761d452cf5Sahrens 	    dsl_dataset_destroy_begin_sync, ds, NULL, 0);
10773cb34c60Sahrens 	if (err)
10783cb34c60Sahrens 		goto out;
10793cb34c60Sahrens 
1080503ad85cSMatthew Ahrens 	err = dmu_objset_from_ds(ds, &os);
10813cb34c60Sahrens 	if (err)
10823cb34c60Sahrens 		goto out;
1083fa9e4066Sahrens 
10841d452cf5Sahrens 	/*
10851d452cf5Sahrens 	 * remove the objects in open context, so that we won't
10861d452cf5Sahrens 	 * have too much to do in syncing context.
10871d452cf5Sahrens 	 */
10886754306eSahrens 	for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE,
10896754306eSahrens 	    ds->ds_phys->ds_prev_snap_txg)) {
1090cdb0ab79Smaybee 		/*
1091cdb0ab79Smaybee 		 * Ignore errors, if there is not enough disk space
1092cdb0ab79Smaybee 		 * we will deal with it in dsl_dataset_destroy_sync().
1093cdb0ab79Smaybee 		 */
1094cdb0ab79Smaybee 		(void) dmu_free_object(os, obj);
10951d452cf5Sahrens 	}
1096feaa74e4SMark Maybee 	if (err != ESRCH)
1097feaa74e4SMark Maybee 		goto out;
10981d452cf5Sahrens 
109914843421SMatthew Ahrens 	/*
1100feaa74e4SMark Maybee 	 * Only the ZIL knows how to free log blocks.
1101feaa74e4SMark Maybee 	 */
1102feaa74e4SMark Maybee 	zil_destroy(dmu_objset_zil(os), B_FALSE);
1103feaa74e4SMark Maybee 
1104feaa74e4SMark Maybee 	/*
1105feaa74e4SMark Maybee 	 * Sync out all in-flight IO.
110614843421SMatthew Ahrens 	 */
110714843421SMatthew Ahrens 	txg_wait_synced(dd->dd_pool, 0);
110814843421SMatthew Ahrens 
110914843421SMatthew Ahrens 	/*
111014843421SMatthew Ahrens 	 * If we managed to free all the objects in open
111114843421SMatthew Ahrens 	 * context, the user space accounting should be zero.
111214843421SMatthew Ahrens 	 */
111314843421SMatthew Ahrens 	if (ds->ds_phys->ds_bp.blk_fill == 0 &&
1114503ad85cSMatthew Ahrens 	    dmu_objset_userused_enabled(os)) {
111514843421SMatthew Ahrens 		uint64_t count;
111614843421SMatthew Ahrens 
111714843421SMatthew Ahrens 		ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 ||
111814843421SMatthew Ahrens 		    count == 0);
111914843421SMatthew Ahrens 		ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 ||
112014843421SMatthew Ahrens 		    count == 0);
112114843421SMatthew Ahrens 	}
112214843421SMatthew Ahrens 
112368038c2cSmaybee 	rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
112468038c2cSmaybee 	err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd);
112568038c2cSmaybee 	rw_exit(&dd->dd_pool->dp_config_rwlock);
112668038c2cSmaybee 
112768038c2cSmaybee 	if (err)
112868038c2cSmaybee 		goto out;
112968038c2cSmaybee 
11301d452cf5Sahrens 	/*
11311d452cf5Sahrens 	 * Blow away the dsl_dir + head dataset.
11321d452cf5Sahrens 	 */
1133745cd3c5Smaybee 	dsl_dataset_make_exclusive(ds, tag);
1134842727c2SChris Kirby 	/*
1135842727c2SChris Kirby 	 * If we're removing a clone, we might also need to remove its
1136842727c2SChris Kirby 	 * origin.
1137842727c2SChris Kirby 	 */
1138842727c2SChris Kirby 	do {
1139842727c2SChris Kirby 		dsda.need_prep = B_FALSE;
1140842727c2SChris Kirby 		if (dsl_dir_is_clone(dd)) {
1141842727c2SChris Kirby 			err = dsl_dataset_origin_rm_prep(&dsda, tag);
1142842727c2SChris Kirby 			if (err) {
1143842727c2SChris Kirby 				dsl_dir_close(dd, FTAG);
1144842727c2SChris Kirby 				goto out;
1145842727c2SChris Kirby 			}
1146842727c2SChris Kirby 		}
1147842727c2SChris Kirby 
1148842727c2SChris Kirby 		dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool);
1149842727c2SChris Kirby 		dsl_sync_task_create(dstg, dsl_dataset_destroy_check,
1150842727c2SChris Kirby 		    dsl_dataset_destroy_sync, &dsda, tag, 0);
1151842727c2SChris Kirby 		dsl_sync_task_create(dstg, dsl_dir_destroy_check,
115292241e0bSTom Erickson 		    dsl_dir_destroy_sync, &dummy_ds, FTAG, 0);
1153842727c2SChris Kirby 		err = dsl_sync_task_group_wait(dstg);
1154842727c2SChris Kirby 		dsl_sync_task_group_destroy(dstg);
1155842727c2SChris Kirby 
1156842727c2SChris Kirby 		/*
1157842727c2SChris Kirby 		 * We could be racing against 'zfs release' or 'zfs destroy -d'
1158842727c2SChris Kirby 		 * on the origin snap, in which case we can get EBUSY if we
1159842727c2SChris Kirby 		 * needed to destroy the origin snap but were not ready to
1160842727c2SChris Kirby 		 * do so.
1161842727c2SChris Kirby 		 */
1162842727c2SChris Kirby 		if (dsda.need_prep) {
1163842727c2SChris Kirby 			ASSERT(err == EBUSY);
1164842727c2SChris Kirby 			ASSERT(dsl_dir_is_clone(dd));
1165842727c2SChris Kirby 			ASSERT(dsda.rm_origin == NULL);
1166842727c2SChris Kirby 		}
1167842727c2SChris Kirby 	} while (dsda.need_prep);
1168842727c2SChris Kirby 
1169842727c2SChris Kirby 	if (dsda.rm_origin != NULL)
1170842727c2SChris Kirby 		dsl_dataset_disown(dsda.rm_origin, tag);
1171842727c2SChris Kirby 
1172745cd3c5Smaybee 	/* if it is successful, dsl_dir_destroy_sync will close the dd */
11733cb34c60Sahrens 	if (err)
11741d452cf5Sahrens 		dsl_dir_close(dd, FTAG);
11753cb34c60Sahrens out:
1176745cd3c5Smaybee 	dsl_dataset_disown(ds, tag);
1177fa9e4066Sahrens 	return (err);
1178fa9e4066Sahrens }
1179fa9e4066Sahrens 
1180c717a561Smaybee blkptr_t *
1181c717a561Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1182fa9e4066Sahrens {
1183c717a561Smaybee 	return (&ds->ds_phys->ds_bp);
1184fa9e4066Sahrens }
1185fa9e4066Sahrens 
1186fa9e4066Sahrens void
1187fa9e4066Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1188fa9e4066Sahrens {
1189fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1190fa9e4066Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1191fa9e4066Sahrens 	if (ds == NULL) {
1192fa9e4066Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1193fa9e4066Sahrens 	} else {
1194fa9e4066Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1195fa9e4066Sahrens 		ds->ds_phys->ds_bp = *bp;
1196fa9e4066Sahrens 	}
1197fa9e4066Sahrens }
1198fa9e4066Sahrens 
1199fa9e4066Sahrens spa_t *
1200fa9e4066Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1201fa9e4066Sahrens {
1202fa9e4066Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1203fa9e4066Sahrens }
1204fa9e4066Sahrens 
1205fa9e4066Sahrens void
1206fa9e4066Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1207fa9e4066Sahrens {
1208fa9e4066Sahrens 	dsl_pool_t *dp;
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 	if (ds == NULL) /* this is the meta-objset */
1211fa9e4066Sahrens 		return;
1212fa9e4066Sahrens 
1213503ad85cSMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1214a2eea2e1Sahrens 
1215a2eea2e1Sahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
1216a2eea2e1Sahrens 		panic("dirtying snapshot!");
1217fa9e4066Sahrens 
1218fa9e4066Sahrens 	dp = ds->ds_dir->dd_pool;
1219fa9e4066Sahrens 
1220fa9e4066Sahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) {
1221fa9e4066Sahrens 		/* up the hold count until we can be written out */
1222fa9e4066Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1223fa9e4066Sahrens 	}
1224fa9e4066Sahrens }
1225fa9e4066Sahrens 
1226a9799022Sck /*
1227a9799022Sck  * The unique space in the head dataset can be calculated by subtracting
1228a9799022Sck  * the space used in the most recent snapshot, that is still being used
1229a9799022Sck  * in this file system, from the space currently in use.  To figure out
1230a9799022Sck  * the space in the most recent snapshot still in use, we need to take
1231a9799022Sck  * the total space used in the snapshot and subtract out the space that
1232a9799022Sck  * has been freed up since the snapshot was taken.
1233a9799022Sck  */
1234a9799022Sck static void
1235a9799022Sck dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1236a9799022Sck {
1237a9799022Sck 	uint64_t mrs_used;
1238a9799022Sck 	uint64_t dlused, dlcomp, dluncomp;
1239a9799022Sck 
12403f9d6ad7SLin Ling 	ASSERT(!dsl_dataset_is_snapshot(ds));
1241a9799022Sck 
1242a9799022Sck 	if (ds->ds_phys->ds_prev_snap_obj != 0)
1243a9799022Sck 		mrs_used = ds->ds_prev->ds_phys->ds_used_bytes;
1244a9799022Sck 	else
1245a9799022Sck 		mrs_used = 0;
1246a9799022Sck 
1247cde58dbcSMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1248a9799022Sck 
1249a9799022Sck 	ASSERT3U(dlused, <=, mrs_used);
1250a9799022Sck 	ds->ds_phys->ds_unique_bytes =
1251a9799022Sck 	    ds->ds_phys->ds_used_bytes - (mrs_used - dlused);
1252a9799022Sck 
12533f9d6ad7SLin Ling 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1254a9799022Sck 	    SPA_VERSION_UNIQUE_ACCURATE)
1255a9799022Sck 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1256a9799022Sck }
1257a9799022Sck 
1258fa9e4066Sahrens struct killarg {
125974e7dc98SMatthew Ahrens 	dsl_dataset_t *ds;
1260fa9e4066Sahrens 	dmu_tx_t *tx;
1261fa9e4066Sahrens };
1262fa9e4066Sahrens 
126374e7dc98SMatthew Ahrens /* ARGSUSED */
1264fa9e4066Sahrens static int
12653f9d6ad7SLin Ling kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
1266b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1267fa9e4066Sahrens {
1268fa9e4066Sahrens 	struct killarg *ka = arg;
1269b24ab676SJeff Bonwick 	dmu_tx_t *tx = ka->tx;
1270fa9e4066Sahrens 
127188b7b0f2SMatthew Ahrens 	if (bp == NULL)
127288b7b0f2SMatthew Ahrens 		return (0);
1273fa9e4066Sahrens 
1274b24ab676SJeff Bonwick 	if (zb->zb_level == ZB_ZIL_LEVEL) {
1275b24ab676SJeff Bonwick 		ASSERT(zilog != NULL);
1276ab69d62fSMatthew Ahrens 		/*
1277ab69d62fSMatthew Ahrens 		 * It's a block in the intent log.  It has no
1278ab69d62fSMatthew Ahrens 		 * accounting, so just free it.
1279ab69d62fSMatthew Ahrens 		 */
1280b24ab676SJeff Bonwick 		dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
1281ab69d62fSMatthew Ahrens 	} else {
1282b24ab676SJeff Bonwick 		ASSERT(zilog == NULL);
1283ab69d62fSMatthew Ahrens 		ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
1284b24ab676SJeff Bonwick 		(void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
1285ab69d62fSMatthew Ahrens 	}
128674e7dc98SMatthew Ahrens 
1287fa9e4066Sahrens 	return (0);
1288fa9e4066Sahrens }
1289fa9e4066Sahrens 
1290e1930233Sbonwick /* ARGSUSED */
1291e1930233Sbonwick static int
12921d452cf5Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx)
1293e1930233Sbonwick {
12941d452cf5Sahrens 	dsl_dataset_t *ds = arg1;
12953cb34c60Sahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
12963cb34c60Sahrens 	uint64_t count;
12973cb34c60Sahrens 	int err;
1298e1930233Sbonwick 
1299e1930233Sbonwick 	/*
1300e1930233Sbonwick 	 * Can't delete a head dataset if there are snapshots of it.
1301e1930233Sbonwick 	 * (Except if the only snapshots are from the branch we cloned
1302e1930233Sbonwick 	 * from.)
1303e1930233Sbonwick 	 */
1304e1930233Sbonwick 	if (ds->ds_prev != NULL &&
1305e1930233Sbonwick 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
13064aed9999Svitezslav batrla - Sun Microsystems - Prague Czech Republic 		return (EBUSY);
1307e1930233Sbonwick 
13083cb34c60Sahrens 	/*
13093cb34c60Sahrens 	 * This is really a dsl_dir thing, but check it here so that
13103cb34c60Sahrens 	 * we'll be less likely to leave this dataset inconsistent &
13113cb34c60Sahrens 	 * nearly destroyed.
13123cb34c60Sahrens 	 */
13133cb34c60Sahrens 	err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
13143cb34c60Sahrens 	if (err)
13153cb34c60Sahrens 		return (err);
13163cb34c60Sahrens 	if (count != 0)
13173cb34c60Sahrens 		return (EEXIST);
13183cb34c60Sahrens 
1319e1930233Sbonwick 	return (0);
1320e1930233Sbonwick }
1321e1930233Sbonwick 
13221d452cf5Sahrens /* ARGSUSED */
13231d452cf5Sahrens static void
13243f9d6ad7SLin Ling dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, dmu_tx_t *tx)
1325fa9e4066Sahrens {
13261d452cf5Sahrens 	dsl_dataset_t *ds = arg1;
1327ecd6cf80Smarks 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1328fa9e4066Sahrens 
13291d452cf5Sahrens 	/* Mark it as inconsistent on-disk, in case we crash */
13301d452cf5Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
13311d452cf5Sahrens 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
1332ecd6cf80Smarks 
13333f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx,
13343f9d6ad7SLin Ling 	    "dataset = %llu", ds->ds_object);
13351d452cf5Sahrens }
1336fa9e4066Sahrens 
1337842727c2SChris Kirby static int
1338842727c2SChris Kirby dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag,
1339842727c2SChris Kirby     dmu_tx_t *tx)
1340842727c2SChris Kirby {
1341842727c2SChris Kirby 	dsl_dataset_t *ds = dsda->ds;
1342842727c2SChris Kirby 	dsl_dataset_t *ds_prev = ds->ds_prev;
1343842727c2SChris Kirby 
1344842727c2SChris Kirby 	if (dsl_dataset_might_destroy_origin(ds_prev)) {
1345842727c2SChris Kirby 		struct dsl_ds_destroyarg ndsda = {0};
1346842727c2SChris Kirby 
1347842727c2SChris Kirby 		/*
1348842727c2SChris Kirby 		 * If we're not prepared to remove the origin, don't remove
1349842727c2SChris Kirby 		 * the clone either.
1350842727c2SChris Kirby 		 */
1351842727c2SChris Kirby 		if (dsda->rm_origin == NULL) {
1352842727c2SChris Kirby 			dsda->need_prep = B_TRUE;
1353842727c2SChris Kirby 			return (EBUSY);
1354842727c2SChris Kirby 		}
1355842727c2SChris Kirby 
1356842727c2SChris Kirby 		ndsda.ds = ds_prev;
1357842727c2SChris Kirby 		ndsda.is_origin_rm = B_TRUE;
1358842727c2SChris Kirby 		return (dsl_dataset_destroy_check(&ndsda, tag, tx));
1359842727c2SChris Kirby 	}
1360842727c2SChris Kirby 
1361842727c2SChris Kirby 	/*
1362842727c2SChris Kirby 	 * If we're not going to remove the origin after all,
1363842727c2SChris Kirby 	 * undo the open context setup.
1364842727c2SChris Kirby 	 */
1365842727c2SChris Kirby 	if (dsda->rm_origin != NULL) {
1366842727c2SChris Kirby 		dsl_dataset_disown(dsda->rm_origin, tag);
1367842727c2SChris Kirby 		dsda->rm_origin = NULL;
1368842727c2SChris Kirby 	}
1369842727c2SChris Kirby 
1370842727c2SChris Kirby 	return (0);
1371842727c2SChris Kirby }
1372842727c2SChris Kirby 
137399d5e173STim Haley /*
137499d5e173STim Haley  * If you add new checks here, you may need to add
137599d5e173STim Haley  * additional checks to the "temporary" case in
137699d5e173STim Haley  * snapshot_check() in dmu_objset.c.
137799d5e173STim Haley  */
13781d452cf5Sahrens /* ARGSUSED */
13793cb34c60Sahrens int
13801d452cf5Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx)
13811d452cf5Sahrens {
1382842727c2SChris Kirby 	struct dsl_ds_destroyarg *dsda = arg1;
1383842727c2SChris Kirby 	dsl_dataset_t *ds = dsda->ds;
1384fa9e4066Sahrens 
1385745cd3c5Smaybee 	/* we have an owner hold, so noone else can destroy us */
1386745cd3c5Smaybee 	ASSERT(!DSL_DATASET_IS_DESTROYED(ds));
1387745cd3c5Smaybee 
1388842727c2SChris Kirby 	/*
1389842727c2SChris Kirby 	 * Only allow deferred destroy on pools that support it.
1390842727c2SChris Kirby 	 * NOTE: deferred destroy is only supported on snapshots.
1391842727c2SChris Kirby 	 */
1392842727c2SChris Kirby 	if (dsda->defer) {
1393842727c2SChris Kirby 		if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
1394842727c2SChris Kirby 		    SPA_VERSION_USERREFS)
1395842727c2SChris Kirby 			return (ENOTSUP);
1396842727c2SChris Kirby 		ASSERT(dsl_dataset_is_snapshot(ds));
1397842727c2SChris Kirby 		return (0);
1398842727c2SChris Kirby 	}
1399fa9e4066Sahrens 
1400fa9e4066Sahrens 	/*
1401fa9e4066Sahrens 	 * Can't delete a head dataset if there are snapshots of it.
1402fa9e4066Sahrens 	 * (Except if the only snapshots are from the branch we cloned
1403fa9e4066Sahrens 	 * from.)
1404fa9e4066Sahrens 	 */
1405fa9e4066Sahrens 	if (ds->ds_prev != NULL &&
14061d452cf5Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
14074aed9999Svitezslav batrla - Sun Microsystems - Prague Czech Republic 		return (EBUSY);
1408fa9e4066Sahrens 
1409fa9e4066Sahrens 	/*
1410fa9e4066Sahrens 	 * If we made changes this txg, traverse_dsl_dataset won't find
1411fa9e4066Sahrens 	 * them.  Try again.
1412fa9e4066Sahrens 	 */
14131d452cf5Sahrens 	if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg)
1414fa9e4066Sahrens 		return (EAGAIN);
14151d452cf5Sahrens 
1416842727c2SChris Kirby 	if (dsl_dataset_is_snapshot(ds)) {
1417842727c2SChris Kirby 		/*
1418842727c2SChris Kirby 		 * If this snapshot has an elevated user reference count,
1419842727c2SChris Kirby 		 * we can't destroy it yet.
1420842727c2SChris Kirby 		 */
1421842727c2SChris Kirby 		if (ds->ds_userrefs > 0 && !dsda->releasing)
1422842727c2SChris Kirby 			return (EBUSY);
1423842727c2SChris Kirby 
1424842727c2SChris Kirby 		mutex_enter(&ds->ds_lock);
1425842727c2SChris Kirby 		/*
1426842727c2SChris Kirby 		 * Can't delete a branch point. However, if we're destroying
1427842727c2SChris Kirby 		 * a clone and removing its origin due to it having a user
1428842727c2SChris Kirby 		 * hold count of 0 and having been marked for deferred destroy,
1429842727c2SChris Kirby 		 * it's OK for the origin to have a single clone.
1430842727c2SChris Kirby 		 */
1431842727c2SChris Kirby 		if (ds->ds_phys->ds_num_children >
1432842727c2SChris Kirby 		    (dsda->is_origin_rm ? 2 : 1)) {
1433842727c2SChris Kirby 			mutex_exit(&ds->ds_lock);
1434842727c2SChris Kirby 			return (EEXIST);
1435842727c2SChris Kirby 		}
1436842727c2SChris Kirby 		mutex_exit(&ds->ds_lock);
1437842727c2SChris Kirby 	} else if (dsl_dir_is_clone(ds->ds_dir)) {
1438842727c2SChris Kirby 		return (dsl_dataset_origin_check(dsda, arg2, tx));
1439842727c2SChris Kirby 	}
1440842727c2SChris Kirby 
14411d452cf5Sahrens 	/* XXX we should do some i/o error checking... */
14421d452cf5Sahrens 	return (0);
14431d452cf5Sahrens }
14441d452cf5Sahrens 
1445745cd3c5Smaybee struct refsarg {
1446745cd3c5Smaybee 	kmutex_t lock;
1447745cd3c5Smaybee 	boolean_t gone;
1448745cd3c5Smaybee 	kcondvar_t cv;
1449745cd3c5Smaybee };
1450745cd3c5Smaybee 
1451745cd3c5Smaybee /* ARGSUSED */
1452745cd3c5Smaybee static void
1453745cd3c5Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv)
1454745cd3c5Smaybee {
1455745cd3c5Smaybee 	struct refsarg *arg = argv;
1456745cd3c5Smaybee 
1457745cd3c5Smaybee 	mutex_enter(&arg->lock);
1458745cd3c5Smaybee 	arg->gone = TRUE;
1459745cd3c5Smaybee 	cv_signal(&arg->cv);
1460745cd3c5Smaybee 	mutex_exit(&arg->lock);
1461745cd3c5Smaybee }
1462745cd3c5Smaybee 
1463745cd3c5Smaybee static void
1464745cd3c5Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag)
1465745cd3c5Smaybee {
1466745cd3c5Smaybee 	struct refsarg arg;
1467745cd3c5Smaybee 
1468745cd3c5Smaybee 	mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL);
1469745cd3c5Smaybee 	cv_init(&arg.cv, NULL, CV_DEFAULT, NULL);
1470745cd3c5Smaybee 	arg.gone = FALSE;
1471745cd3c5Smaybee 	(void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys,
1472745cd3c5Smaybee 	    dsl_dataset_refs_gone);
1473745cd3c5Smaybee 	dmu_buf_rele(ds->ds_dbuf, tag);
1474745cd3c5Smaybee 	mutex_enter(&arg.lock);
1475745cd3c5Smaybee 	while (!arg.gone)
1476745cd3c5Smaybee 		cv_wait(&arg.cv, &arg.lock);
1477745cd3c5Smaybee 	ASSERT(arg.gone);
1478745cd3c5Smaybee 	mutex_exit(&arg.lock);
1479745cd3c5Smaybee 	ds->ds_dbuf = NULL;
1480745cd3c5Smaybee 	ds->ds_phys = NULL;
1481745cd3c5Smaybee 	mutex_destroy(&arg.lock);
1482745cd3c5Smaybee 	cv_destroy(&arg.cv);
1483745cd3c5Smaybee }
1484745cd3c5Smaybee 
1485c33e334fSMatthew Ahrens static void
1486c33e334fSMatthew Ahrens remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx)
1487c33e334fSMatthew Ahrens {
1488c33e334fSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1489c33e334fSMatthew Ahrens 	uint64_t count;
1490c33e334fSMatthew Ahrens 	int err;
1491c33e334fSMatthew Ahrens 
1492c33e334fSMatthew Ahrens 	ASSERT(ds->ds_phys->ds_num_children >= 2);
1493c33e334fSMatthew Ahrens 	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
1494c33e334fSMatthew Ahrens 	/*
1495c33e334fSMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
1496c33e334fSMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
1497c33e334fSMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
1498c33e334fSMatthew Ahrens 	 * If we knew that the pool was created after
1499c33e334fSMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1500c33e334fSMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
1501c33e334fSMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
1502c33e334fSMatthew Ahrens 	 * remove this one.
1503c33e334fSMatthew Ahrens 	 */
1504c33e334fSMatthew Ahrens 	if (err != ENOENT) {
1505c33e334fSMatthew Ahrens 		VERIFY3U(err, ==, 0);
1506c33e334fSMatthew Ahrens 	}
1507c33e334fSMatthew Ahrens 	ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
1508c33e334fSMatthew Ahrens 	    &count));
1509c33e334fSMatthew Ahrens 	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
1510c33e334fSMatthew Ahrens }
1511c33e334fSMatthew Ahrens 
1512cde58dbcSMatthew Ahrens static void
1513cde58dbcSMatthew Ahrens dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
1514cde58dbcSMatthew Ahrens {
1515cde58dbcSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1516cde58dbcSMatthew Ahrens 	zap_cursor_t zc;
1517cde58dbcSMatthew Ahrens 	zap_attribute_t za;
1518cde58dbcSMatthew Ahrens 
1519cde58dbcSMatthew Ahrens 	/*
1520cde58dbcSMatthew Ahrens 	 * If it is the old version, dd_clones doesn't exist so we can't
1521cde58dbcSMatthew Ahrens 	 * find the clones, but deadlist_remove_key() is a no-op so it
1522cde58dbcSMatthew Ahrens 	 * doesn't matter.
1523cde58dbcSMatthew Ahrens 	 */
1524cde58dbcSMatthew Ahrens 	if (ds->ds_dir->dd_phys->dd_clones == 0)
1525cde58dbcSMatthew Ahrens 		return;
1526cde58dbcSMatthew Ahrens 
1527cde58dbcSMatthew Ahrens 	for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
1528cde58dbcSMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
1529cde58dbcSMatthew Ahrens 	    zap_cursor_advance(&zc)) {
1530cde58dbcSMatthew Ahrens 		dsl_dataset_t *clone;
1531cde58dbcSMatthew Ahrens 
1532cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
1533cde58dbcSMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
1534cde58dbcSMatthew Ahrens 		if (clone->ds_dir->dd_origin_txg > mintxg) {
1535cde58dbcSMatthew Ahrens 			dsl_deadlist_remove_key(&clone->ds_deadlist,
1536cde58dbcSMatthew Ahrens 			    mintxg, tx);
1537cde58dbcSMatthew Ahrens 			dsl_dataset_remove_clones_key(clone, mintxg, tx);
1538cde58dbcSMatthew Ahrens 		}
1539cde58dbcSMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
1540cde58dbcSMatthew Ahrens 	}
1541cde58dbcSMatthew Ahrens 	zap_cursor_fini(&zc);
1542cde58dbcSMatthew Ahrens }
1543cde58dbcSMatthew Ahrens 
1544cde58dbcSMatthew Ahrens struct process_old_arg {
1545cde58dbcSMatthew Ahrens 	dsl_dataset_t *ds;
1546cde58dbcSMatthew Ahrens 	dsl_dataset_t *ds_prev;
1547cde58dbcSMatthew Ahrens 	boolean_t after_branch_point;
1548cde58dbcSMatthew Ahrens 	zio_t *pio;
1549cde58dbcSMatthew Ahrens 	uint64_t used, comp, uncomp;
1550cde58dbcSMatthew Ahrens };
1551cde58dbcSMatthew Ahrens 
1552cde58dbcSMatthew Ahrens static int
1553cde58dbcSMatthew Ahrens process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1554cde58dbcSMatthew Ahrens {
1555cde58dbcSMatthew Ahrens 	struct process_old_arg *poa = arg;
1556cde58dbcSMatthew Ahrens 	dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
1557cde58dbcSMatthew Ahrens 
1558cde58dbcSMatthew Ahrens 	if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
1559cde58dbcSMatthew Ahrens 		dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
1560cde58dbcSMatthew Ahrens 		if (poa->ds_prev && !poa->after_branch_point &&
1561cde58dbcSMatthew Ahrens 		    bp->blk_birth >
1562cde58dbcSMatthew Ahrens 		    poa->ds_prev->ds_phys->ds_prev_snap_txg) {
1563cde58dbcSMatthew Ahrens 			poa->ds_prev->ds_phys->ds_unique_bytes +=
1564cde58dbcSMatthew Ahrens 			    bp_get_dsize_sync(dp->dp_spa, bp);
1565cde58dbcSMatthew Ahrens 		}
1566cde58dbcSMatthew Ahrens 	} else {
1567cde58dbcSMatthew Ahrens 		poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
1568cde58dbcSMatthew Ahrens 		poa->comp += BP_GET_PSIZE(bp);
1569cde58dbcSMatthew Ahrens 		poa->uncomp += BP_GET_UCSIZE(bp);
1570cde58dbcSMatthew Ahrens 		dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
1571cde58dbcSMatthew Ahrens 	}
1572cde58dbcSMatthew Ahrens 	return (0);
1573cde58dbcSMatthew Ahrens }
1574cde58dbcSMatthew Ahrens 
1575cde58dbcSMatthew Ahrens static void
1576cde58dbcSMatthew Ahrens process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
1577cde58dbcSMatthew Ahrens     dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
1578cde58dbcSMatthew Ahrens {
1579cde58dbcSMatthew Ahrens 	struct process_old_arg poa = { 0 };
1580cde58dbcSMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1581cde58dbcSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
1582cde58dbcSMatthew Ahrens 
1583cde58dbcSMatthew Ahrens 	ASSERT(ds->ds_deadlist.dl_oldfmt);
1584cde58dbcSMatthew Ahrens 	ASSERT(ds_next->ds_deadlist.dl_oldfmt);
1585cde58dbcSMatthew Ahrens 
1586cde58dbcSMatthew Ahrens 	poa.ds = ds;
1587cde58dbcSMatthew Ahrens 	poa.ds_prev = ds_prev;
1588cde58dbcSMatthew Ahrens 	poa.after_branch_point = after_branch_point;
1589cde58dbcSMatthew Ahrens 	poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1590cde58dbcSMatthew Ahrens 	VERIFY3U(0, ==, bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
1591cde58dbcSMatthew Ahrens 	    process_old_cb, &poa, tx));
1592cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(poa.pio), ==, 0);
1593cde58dbcSMatthew Ahrens 	ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
1594cde58dbcSMatthew Ahrens 
1595cde58dbcSMatthew Ahrens 	/* change snapused */
1596cde58dbcSMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1597cde58dbcSMatthew Ahrens 	    -poa.used, -poa.comp, -poa.uncomp, tx);
1598cde58dbcSMatthew Ahrens 
1599cde58dbcSMatthew Ahrens 	/* swap next's deadlist to our deadlist */
1600cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1601cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds_next->ds_deadlist);
1602cde58dbcSMatthew Ahrens 	SWITCH64(ds_next->ds_phys->ds_deadlist_obj,
1603cde58dbcSMatthew Ahrens 	    ds->ds_phys->ds_deadlist_obj);
1604cde58dbcSMatthew Ahrens 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1605cde58dbcSMatthew Ahrens 	dsl_deadlist_open(&ds_next->ds_deadlist, mos,
1606cde58dbcSMatthew Ahrens 	    ds_next->ds_phys->ds_deadlist_obj);
1607cde58dbcSMatthew Ahrens }
1608cde58dbcSMatthew Ahrens 
16093cb34c60Sahrens void
16103f9d6ad7SLin Ling dsl_dataset_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx)
16111d452cf5Sahrens {
1612842727c2SChris Kirby 	struct dsl_ds_destroyarg *dsda = arg1;
1613842727c2SChris Kirby 	dsl_dataset_t *ds = dsda->ds;
16141d452cf5Sahrens 	int err;
16151d452cf5Sahrens 	int after_branch_point = FALSE;
16161d452cf5Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
16171d452cf5Sahrens 	objset_t *mos = dp->dp_meta_objset;
16181d452cf5Sahrens 	dsl_dataset_t *ds_prev = NULL;
161999d5e173STim Haley 	boolean_t wont_destroy;
16201d452cf5Sahrens 	uint64_t obj;
16211d452cf5Sahrens 
162299d5e173STim Haley 	wont_destroy = (dsda->defer &&
162399d5e173STim Haley 	    (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1));
162499d5e173STim Haley 
162599d5e173STim Haley 	ASSERT(ds->ds_owner || wont_destroy);
1626842727c2SChris Kirby 	ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1);
16271d452cf5Sahrens 	ASSERT(ds->ds_prev == NULL ||
16281d452cf5Sahrens 	    ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
16291d452cf5Sahrens 	ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
16301d452cf5Sahrens 
163199d5e173STim Haley 	if (wont_destroy) {
1632842727c2SChris Kirby 		ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
163399d5e173STim Haley 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
163499d5e173STim Haley 		ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
163599d5e173STim Haley 		return;
1636842727c2SChris Kirby 	}
1637842727c2SChris Kirby 
1638745cd3c5Smaybee 	/* signal any waiters that this dataset is going away */
1639745cd3c5Smaybee 	mutex_enter(&ds->ds_lock);
1640745cd3c5Smaybee 	ds->ds_owner = dsl_reaper;
1641745cd3c5Smaybee 	cv_broadcast(&ds->ds_exclusive_cv);
1642745cd3c5Smaybee 	mutex_exit(&ds->ds_lock);
1643745cd3c5Smaybee 
1644a9799022Sck 	/* Remove our reservation */
1645a9799022Sck 	if (ds->ds_reserved != 0) {
164692241e0bSTom Erickson 		dsl_prop_setarg_t psa;
164792241e0bSTom Erickson 		uint64_t value = 0;
164892241e0bSTom Erickson 
164992241e0bSTom Erickson 		dsl_prop_setarg_init_uint64(&psa, "refreservation",
165092241e0bSTom Erickson 		    (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
165192241e0bSTom Erickson 		    &value);
165292241e0bSTom Erickson 		psa.psa_effective_value = 0;	/* predict default value */
165392241e0bSTom Erickson 
16543f9d6ad7SLin Ling 		dsl_dataset_set_reservation_sync(ds, &psa, tx);
1655a9799022Sck 		ASSERT3U(ds->ds_reserved, ==, 0);
1656a9799022Sck 	}
1657a9799022Sck 
16581d452cf5Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
16591d452cf5Sahrens 
16603f9d6ad7SLin Ling 	dsl_scan_ds_destroyed(ds, tx);
1661088f3894Sahrens 
16621d452cf5Sahrens 	obj = ds->ds_object;
1663fa9e4066Sahrens 
1664fa9e4066Sahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
1665fa9e4066Sahrens 		if (ds->ds_prev) {
1666fa9e4066Sahrens 			ds_prev = ds->ds_prev;
1667fa9e4066Sahrens 		} else {
1668745cd3c5Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
1669745cd3c5Smaybee 			    ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
1670fa9e4066Sahrens 		}
1671fa9e4066Sahrens 		after_branch_point =
1672fa9e4066Sahrens 		    (ds_prev->ds_phys->ds_next_snap_obj != obj);
1673fa9e4066Sahrens 
1674fa9e4066Sahrens 		dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
1675088f3894Sahrens 		if (after_branch_point &&
1676088f3894Sahrens 		    ds_prev->ds_phys->ds_next_clones_obj != 0) {
1677c33e334fSMatthew Ahrens 			remove_from_next_clones(ds_prev, obj, tx);
1678088f3894Sahrens 			if (ds->ds_phys->ds_next_snap_obj != 0) {
1679088f3894Sahrens 				VERIFY(0 == zap_add_int(mos,
1680088f3894Sahrens 				    ds_prev->ds_phys->ds_next_clones_obj,
1681088f3894Sahrens 				    ds->ds_phys->ds_next_snap_obj, tx));
1682088f3894Sahrens 			}
1683088f3894Sahrens 		}
1684fa9e4066Sahrens 		if (after_branch_point &&
1685fa9e4066Sahrens 		    ds->ds_phys->ds_next_snap_obj == 0) {
1686fa9e4066Sahrens 			/* This clone is toast. */
1687fa9e4066Sahrens 			ASSERT(ds_prev->ds_phys->ds_num_children > 1);
1688fa9e4066Sahrens 			ds_prev->ds_phys->ds_num_children--;
1689842727c2SChris Kirby 
1690842727c2SChris Kirby 			/*
1691842727c2SChris Kirby 			 * If the clone's origin has no other clones, no
1692842727c2SChris Kirby 			 * user holds, and has been marked for deferred
1693842727c2SChris Kirby 			 * deletion, then we should have done the necessary
1694842727c2SChris Kirby 			 * destroy setup for it.
1695842727c2SChris Kirby 			 */
1696842727c2SChris Kirby 			if (ds_prev->ds_phys->ds_num_children == 1 &&
1697842727c2SChris Kirby 			    ds_prev->ds_userrefs == 0 &&
1698842727c2SChris Kirby 			    DS_IS_DEFER_DESTROY(ds_prev)) {
1699842727c2SChris Kirby 				ASSERT3P(dsda->rm_origin, !=, NULL);
1700842727c2SChris Kirby 			} else {
1701842727c2SChris Kirby 				ASSERT3P(dsda->rm_origin, ==, NULL);
1702842727c2SChris Kirby 			}
1703fa9e4066Sahrens 		} else if (!after_branch_point) {
1704fa9e4066Sahrens 			ds_prev->ds_phys->ds_next_snap_obj =
1705fa9e4066Sahrens 			    ds->ds_phys->ds_next_snap_obj;
1706fa9e4066Sahrens 		}
1707fa9e4066Sahrens 	}
1708fa9e4066Sahrens 
17093f9d6ad7SLin Ling 	if (dsl_dataset_is_snapshot(ds)) {
1710fa9e4066Sahrens 		dsl_dataset_t *ds_next;
1711a9799022Sck 		uint64_t old_unique;
1712cde58dbcSMatthew Ahrens 		uint64_t used = 0, comp = 0, uncomp = 0;
1713fa9e4066Sahrens 
1714745cd3c5Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
1715745cd3c5Smaybee 		    ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
1716fa9e4066Sahrens 		ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
1717fa9e4066Sahrens 
17183f9d6ad7SLin Ling 		old_unique = ds_next->ds_phys->ds_unique_bytes;
1719a9799022Sck 
1720fa9e4066Sahrens 		dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
1721fa9e4066Sahrens 		ds_next->ds_phys->ds_prev_snap_obj =
1722fa9e4066Sahrens 		    ds->ds_phys->ds_prev_snap_obj;
1723fa9e4066Sahrens 		ds_next->ds_phys->ds_prev_snap_txg =
1724fa9e4066Sahrens 		    ds->ds_phys->ds_prev_snap_txg;
1725fa9e4066Sahrens 		ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
1726fa9e4066Sahrens 		    ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
1727fa9e4066Sahrens 
17283113f7ceSGeorge Wilson 
1729cde58dbcSMatthew Ahrens 		if (ds_next->ds_deadlist.dl_oldfmt) {
1730cde58dbcSMatthew Ahrens 			process_old_deadlist(ds, ds_prev, ds_next,
1731cde58dbcSMatthew Ahrens 			    after_branch_point, tx);
1732cde58dbcSMatthew Ahrens 		} else {
1733cde58dbcSMatthew Ahrens 			/* Adjust prev's unique space. */
1734cde58dbcSMatthew Ahrens 			if (ds_prev && !after_branch_point) {
1735cde58dbcSMatthew Ahrens 				dsl_deadlist_space_range(&ds_next->ds_deadlist,
1736cde58dbcSMatthew Ahrens 				    ds_prev->ds_phys->ds_prev_snap_txg,
1737cde58dbcSMatthew Ahrens 				    ds->ds_phys->ds_prev_snap_txg,
1738cde58dbcSMatthew Ahrens 				    &used, &comp, &uncomp);
1739cde58dbcSMatthew Ahrens 				ds_prev->ds_phys->ds_unique_bytes += used;
1740fa9e4066Sahrens 			}
174174e7dc98SMatthew Ahrens 
1742cde58dbcSMatthew Ahrens 			/* Adjust snapused. */
1743cde58dbcSMatthew Ahrens 			dsl_deadlist_space_range(&ds_next->ds_deadlist,
1744cde58dbcSMatthew Ahrens 			    ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
1745cde58dbcSMatthew Ahrens 			    &used, &comp, &uncomp);
1746cde58dbcSMatthew Ahrens 			dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
1747cde58dbcSMatthew Ahrens 			    -used, -comp, -uncomp, tx);
1748cde58dbcSMatthew Ahrens 
1749cde58dbcSMatthew Ahrens 			/* Move blocks to be freed to pool's free list. */
1750cde58dbcSMatthew Ahrens 			dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
1751cde58dbcSMatthew Ahrens 			    &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
1752cde58dbcSMatthew Ahrens 			    tx);
1753cde58dbcSMatthew Ahrens 			dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
1754cde58dbcSMatthew Ahrens 			    DD_USED_HEAD, used, comp, uncomp, tx);
1755cde58dbcSMatthew Ahrens 			dsl_dir_dirty(tx->tx_pool->dp_free_dir, tx);
1756cde58dbcSMatthew Ahrens 
1757cde58dbcSMatthew Ahrens 			/* Merge our deadlist into next's and free it. */
1758cde58dbcSMatthew Ahrens 			dsl_deadlist_merge(&ds_next->ds_deadlist,
1759cde58dbcSMatthew Ahrens 			    ds->ds_phys->ds_deadlist_obj, tx);
1760cde58dbcSMatthew Ahrens 		}
1761cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
1762cde58dbcSMatthew Ahrens 		dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
1763fa9e4066Sahrens 
1764cde58dbcSMatthew Ahrens 		/* Collapse range in clone heads */
1765cde58dbcSMatthew Ahrens 		dsl_dataset_remove_clones_key(ds,
1766cde58dbcSMatthew Ahrens 		    ds->ds_phys->ds_creation_txg, tx);
1767fa9e4066Sahrens 
17683f9d6ad7SLin Ling 		if (dsl_dataset_is_snapshot(ds_next)) {
1769cde58dbcSMatthew Ahrens 			dsl_dataset_t *ds_nextnext;
1770cde58dbcSMatthew Ahrens 
1771fa9e4066Sahrens 			/*
1772fa9e4066Sahrens 			 * Update next's unique to include blocks which
1773fa9e4066Sahrens 			 * were previously shared by only this snapshot
1774fa9e4066Sahrens 			 * and it.  Those blocks will be born after the
1775fa9e4066Sahrens 			 * prev snap and before this snap, and will have
1776fa9e4066Sahrens 			 * died after the next snap and before the one
1777fa9e4066Sahrens 			 * after that (ie. be on the snap after next's
1778fa9e4066Sahrens 			 * deadlist).
1779fa9e4066Sahrens 			 */
1780745cd3c5Smaybee 			VERIFY(0 == dsl_dataset_hold_obj(dp,
1781745cd3c5Smaybee 			    ds_next->ds_phys->ds_next_snap_obj,
1782cde58dbcSMatthew Ahrens 			    FTAG, &ds_nextnext));
1783cde58dbcSMatthew Ahrens 			dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
178474e7dc98SMatthew Ahrens 			    ds->ds_phys->ds_prev_snap_txg,
1785cde58dbcSMatthew Ahrens 			    ds->ds_phys->ds_creation_txg,
1786cde58dbcSMatthew Ahrens 			    &used, &comp, &uncomp);
1787cde58dbcSMatthew Ahrens 			ds_next->ds_phys->ds_unique_bytes += used;
1788cde58dbcSMatthew Ahrens 			dsl_dataset_rele(ds_nextnext, FTAG);
1789fa9e4066Sahrens 			ASSERT3P(ds_next->ds_prev, ==, NULL);
1790cde58dbcSMatthew Ahrens 
1791cde58dbcSMatthew Ahrens 			/* Collapse range in this head. */
1792cde58dbcSMatthew Ahrens 			dsl_dataset_t *hds;
1793cde58dbcSMatthew Ahrens 			VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
1794cde58dbcSMatthew Ahrens 			    ds->ds_dir->dd_phys->dd_head_dataset_obj,
1795cde58dbcSMatthew Ahrens 			    FTAG, &hds));
1796cde58dbcSMatthew Ahrens 			dsl_deadlist_remove_key(&hds->ds_deadlist,
1797cde58dbcSMatthew Ahrens 			    ds->ds_phys->ds_creation_txg, tx);
1798cde58dbcSMatthew Ahrens 			dsl_dataset_rele(hds, FTAG);
1799cde58dbcSMatthew Ahrens 
1800fa9e4066Sahrens 		} else {
1801fa9e4066Sahrens 			ASSERT3P(ds_next->ds_prev, ==, ds);
1802745cd3c5Smaybee 			dsl_dataset_drop_ref(ds_next->ds_prev, ds_next);
1803745cd3c5Smaybee 			ds_next->ds_prev = NULL;
1804fa9e4066Sahrens 			if (ds_prev) {
1805745cd3c5Smaybee 				VERIFY(0 == dsl_dataset_get_ref(dp,
1806745cd3c5Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
1807745cd3c5Smaybee 				    ds_next, &ds_next->ds_prev));
1808fa9e4066Sahrens 			}
1809a9799022Sck 
1810a9799022Sck 			dsl_dataset_recalc_head_uniq(ds_next);
1811a9799022Sck 
1812a9799022Sck 			/*
1813a9799022Sck 			 * Reduce the amount of our unconsmed refreservation
1814a9799022Sck 			 * being charged to our parent by the amount of
1815a9799022Sck 			 * new unique data we have gained.
1816a9799022Sck 			 */
1817a9799022Sck 			if (old_unique < ds_next->ds_reserved) {
1818a9799022Sck 				int64_t mrsdelta;
1819a9799022Sck 				uint64_t new_unique =
1820a9799022Sck 				    ds_next->ds_phys->ds_unique_bytes;
1821a9799022Sck 
1822a9799022Sck 				ASSERT(old_unique <= new_unique);
1823a9799022Sck 				mrsdelta = MIN(new_unique - old_unique,
1824a9799022Sck 				    ds_next->ds_reserved - old_unique);
182574e7dc98SMatthew Ahrens 				dsl_dir_diduse_space(ds->ds_dir,
182674e7dc98SMatthew Ahrens 				    DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
1827a9799022Sck 			}
1828fa9e4066Sahrens 		}
1829745cd3c5Smaybee 		dsl_dataset_rele(ds_next, FTAG);
1830fa9e4066Sahrens 	} else {
1831fa9e4066Sahrens 		/*
1832fa9e4066Sahrens 		 * There's no next snapshot, so this is a head dataset.
1833fa9e4066Sahrens 		 * Destroy the deadlist.  Unless it's a clone, the
1834fa9e4066Sahrens 		 * deadlist should be empty.  (If it's a clone, it's
1835fa9e4066Sahrens 		 * safe to ignore the deadlist contents.)
1836fa9e4066Sahrens 		 */
1837fa9e4066Sahrens 		struct killarg ka;
1838fa9e4066Sahrens 
1839cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
1840cde58dbcSMatthew Ahrens 		dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
1841fa9e4066Sahrens 		ds->ds_phys->ds_deadlist_obj = 0;
1842fa9e4066Sahrens 
1843fa9e4066Sahrens 		/*
1844fa9e4066Sahrens 		 * Free everything that we point to (that's born after
1845fa9e4066Sahrens 		 * the previous snapshot, if we are a clone)
1846fa9e4066Sahrens 		 *
184774e7dc98SMatthew Ahrens 		 * NB: this should be very quick, because we already
184874e7dc98SMatthew Ahrens 		 * freed all the objects in open context.
1849fa9e4066Sahrens 		 */
185074e7dc98SMatthew Ahrens 		ka.ds = ds;
1851fa9e4066Sahrens 		ka.tx = tx;
185288b7b0f2SMatthew Ahrens 		err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg,
185388b7b0f2SMatthew Ahrens 		    TRAVERSE_POST, kill_blkptr, &ka);
1854fa9e4066Sahrens 		ASSERT3U(err, ==, 0);
18553e78c5fbSChris Kirby 		ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
185674e7dc98SMatthew Ahrens 		    ds->ds_phys->ds_unique_bytes == 0);
1857ca45db41SChris Kirby 
1858ca45db41SChris Kirby 		if (ds->ds_prev != NULL) {
1859cde58dbcSMatthew Ahrens 			if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1860cde58dbcSMatthew Ahrens 				VERIFY3U(0, ==, zap_remove_int(mos,
1861cde58dbcSMatthew Ahrens 				    ds->ds_prev->ds_dir->dd_phys->dd_clones,
1862cde58dbcSMatthew Ahrens 				    ds->ds_object, tx));
1863cde58dbcSMatthew Ahrens 			}
1864ca45db41SChris Kirby 			dsl_dataset_rele(ds->ds_prev, ds);
1865ca45db41SChris Kirby 			ds->ds_prev = ds_prev = NULL;
1866ca45db41SChris Kirby 		}
1867fa9e4066Sahrens 	}
1868fa9e4066Sahrens 
18696e0cbcaaSMatthew Ahrens 	/*
18706e0cbcaaSMatthew Ahrens 	 * This must be done after the dsl_traverse(), because it will
18716e0cbcaaSMatthew Ahrens 	 * re-open the objset.
18726e0cbcaaSMatthew Ahrens 	 */
18736e0cbcaaSMatthew Ahrens 	if (ds->ds_objset) {
18746e0cbcaaSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
18756e0cbcaaSMatthew Ahrens 		ds->ds_objset = NULL;
18766e0cbcaaSMatthew Ahrens 	}
18776e0cbcaaSMatthew Ahrens 
18781d452cf5Sahrens 	if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) {
1879745cd3c5Smaybee 		/* Erase the link in the dir */
18801d452cf5Sahrens 		dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
18811d452cf5Sahrens 		ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
1882745cd3c5Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
1883745cd3c5Smaybee 		err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx);
1884745cd3c5Smaybee 		ASSERT(err == 0);
1885fa9e4066Sahrens 	} else {
1886fa9e4066Sahrens 		/* remove from snapshot namespace */
1887fa9e4066Sahrens 		dsl_dataset_t *ds_head;
1888745cd3c5Smaybee 		ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
1889745cd3c5Smaybee 		VERIFY(0 == dsl_dataset_hold_obj(dp,
1890745cd3c5Smaybee 		    ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
18918660574dSahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
1892fa9e4066Sahrens #ifdef ZFS_DEBUG
1893fa9e4066Sahrens 		{
1894fa9e4066Sahrens 			uint64_t val;
1895ab04eb8eStimh 
1896745cd3c5Smaybee 			err = dsl_dataset_snap_lookup(ds_head,
1897ab04eb8eStimh 			    ds->ds_snapname, &val);
1898fa9e4066Sahrens 			ASSERT3U(err, ==, 0);
1899fa9e4066Sahrens 			ASSERT3U(val, ==, obj);
1900fa9e4066Sahrens 		}
1901fa9e4066Sahrens #endif
1902745cd3c5Smaybee 		err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx);
1903fa9e4066Sahrens 		ASSERT(err == 0);
1904745cd3c5Smaybee 		dsl_dataset_rele(ds_head, FTAG);
1905fa9e4066Sahrens 	}
1906fa9e4066Sahrens 
1907fa9e4066Sahrens 	if (ds_prev && ds->ds_prev != ds_prev)
1908745cd3c5Smaybee 		dsl_dataset_rele(ds_prev, FTAG);
1909fa9e4066Sahrens 
1910990b4856Slling 	spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
19113f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_DESTROY, dp->dp_spa, tx,
19123f9d6ad7SLin Ling 	    "dataset = %llu", ds->ds_object);
1913ecd6cf80Smarks 
1914088f3894Sahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
1915088f3894Sahrens 		uint64_t count;
1916088f3894Sahrens 		ASSERT(0 == zap_count(mos,
1917088f3894Sahrens 		    ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
1918088f3894Sahrens 		VERIFY(0 == dmu_object_free(mos,
1919088f3894Sahrens 		    ds->ds_phys->ds_next_clones_obj, tx));
1920088f3894Sahrens 	}
192174e7dc98SMatthew Ahrens 	if (ds->ds_phys->ds_props_obj != 0)
192274e7dc98SMatthew Ahrens 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
1923842727c2SChris Kirby 	if (ds->ds_phys->ds_userrefs_obj != 0)
1924842727c2SChris Kirby 		VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
1925745cd3c5Smaybee 	dsl_dir_close(ds->ds_dir, ds);
1926745cd3c5Smaybee 	ds->ds_dir = NULL;
1927745cd3c5Smaybee 	dsl_dataset_drain_refs(ds, tag);
19281d452cf5Sahrens 	VERIFY(0 == dmu_object_free(mos, obj, tx));
1929842727c2SChris Kirby 
1930842727c2SChris Kirby 	if (dsda->rm_origin) {
1931842727c2SChris Kirby 		/*
1932842727c2SChris Kirby 		 * Remove the origin of the clone we just destroyed.
1933842727c2SChris Kirby 		 */
1934842727c2SChris Kirby 		struct dsl_ds_destroyarg ndsda = {0};
1935842727c2SChris Kirby 
1936ca45db41SChris Kirby 		ndsda.ds = dsda->rm_origin;
19373f9d6ad7SLin Ling 		dsl_dataset_destroy_sync(&ndsda, tag, tx);
1938842727c2SChris Kirby 	}
1939fa9e4066Sahrens }
1940fa9e4066Sahrens 
1941a9799022Sck static int
1942a9799022Sck dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1943a9799022Sck {
1944a9799022Sck 	uint64_t asize;
1945a9799022Sck 
1946a9799022Sck 	if (!dmu_tx_is_syncing(tx))
1947a9799022Sck 		return (0);
1948a9799022Sck 
1949a9799022Sck 	/*
1950a9799022Sck 	 * If there's an fs-only reservation, any blocks that might become
1951a9799022Sck 	 * owned by the snapshot dataset must be accommodated by space
1952a9799022Sck 	 * outside of the reservation.
1953a9799022Sck 	 */
19543f9d6ad7SLin Ling 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
19553f9d6ad7SLin Ling 	asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
19566e0cbcaaSMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1957a9799022Sck 		return (ENOSPC);
1958a9799022Sck 
1959a9799022Sck 	/*
1960a9799022Sck 	 * Propogate any reserved space for this snapshot to other
1961a9799022Sck 	 * snapshot checks in this sync group.
1962a9799022Sck 	 */
1963a9799022Sck 	if (asize > 0)
1964a9799022Sck 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1965a9799022Sck 
1966a9799022Sck 	return (0);
1967a9799022Sck }
1968a9799022Sck 
1969fa9e4066Sahrens int
19701d452cf5Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
1971fa9e4066Sahrens {
19723cb34c60Sahrens 	dsl_dataset_t *ds = arg1;
19731d452cf5Sahrens 	const char *snapname = arg2;
1974fa9e4066Sahrens 	int err;
19751d452cf5Sahrens 	uint64_t value;
1976fa9e4066Sahrens 
19771d452cf5Sahrens 	/*
19781d452cf5Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
19791d452cf5Sahrens 	 * is already one, try again.
19801d452cf5Sahrens 	 */
19811d452cf5Sahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
19821d452cf5Sahrens 		return (EAGAIN);
1983fa9e4066Sahrens 
19841d452cf5Sahrens 	/*
19851d452cf5Sahrens 	 * Check for conflicting name snapshot name.
19861d452cf5Sahrens 	 */
1987745cd3c5Smaybee 	err = dsl_dataset_snap_lookup(ds, snapname, &value);
19881d452cf5Sahrens 	if (err == 0)
1989fa9e4066Sahrens 		return (EEXIST);
19901d452cf5Sahrens 	if (err != ENOENT)
19911d452cf5Sahrens 		return (err);
1992fa9e4066Sahrens 
1993b7661cccSmmusante 	/*
1994b7661cccSmmusante 	 * Check that the dataset's name is not too long.  Name consists
1995b7661cccSmmusante 	 * of the dataset's length + 1 for the @-sign + snapshot name's length
1996b7661cccSmmusante 	 */
1997b7661cccSmmusante 	if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN)
1998b7661cccSmmusante 		return (ENAMETOOLONG);
1999b7661cccSmmusante 
2000a9799022Sck 	err = dsl_dataset_snapshot_reserve_space(ds, tx);
2001a9799022Sck 	if (err)
2002a9799022Sck 		return (err);
2003a9799022Sck 
20041d452cf5Sahrens 	ds->ds_trysnap_txg = tx->tx_txg;
20051d452cf5Sahrens 	return (0);
20061d452cf5Sahrens }
2007fa9e4066Sahrens 
20081d452cf5Sahrens void
20093f9d6ad7SLin Ling dsl_dataset_snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
20101d452cf5Sahrens {
20113cb34c60Sahrens 	dsl_dataset_t *ds = arg1;
20121d452cf5Sahrens 	const char *snapname = arg2;
20131d452cf5Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
20141d452cf5Sahrens 	dmu_buf_t *dbuf;
20151d452cf5Sahrens 	dsl_dataset_phys_t *dsphys;
2016088f3894Sahrens 	uint64_t dsobj, crtxg;
20171d452cf5Sahrens 	objset_t *mos = dp->dp_meta_objset;
20181d452cf5Sahrens 	int err;
2019fa9e4066Sahrens 
20201d452cf5Sahrens 	ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock));
2021fa9e4066Sahrens 
2022088f3894Sahrens 	/*
2023088f3894Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
2024088f3894Sahrens 	 */
2025088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
2026088f3894Sahrens 		crtxg = 1;
2027088f3894Sahrens 	else
2028088f3894Sahrens 		crtxg = tx->tx_txg;
2029088f3894Sahrens 
20301649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
20311649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
2032ea8dc4b6Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
2033fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
2034fa9e4066Sahrens 	dsphys = dbuf->db_data;
2035745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
20361d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
2037fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
2038fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
2039fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
2040fa9e4066Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
2041fa9e4066Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
2042fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
2043fa9e4066Sahrens 	dsphys->ds_num_children = 1;
2044fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
2045088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
2046fa9e4066Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
2047fa9e4066Sahrens 	dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes;
2048fa9e4066Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
2049fa9e4066Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
205099653d4eSeschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
2051fa9e4066Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
2052ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
2053fa9e4066Sahrens 
20541d452cf5Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
20551d452cf5Sahrens 	if (ds->ds_prev) {
2056088f3894Sahrens 		uint64_t next_clones_obj =
2057088f3894Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
20581d452cf5Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
2059fa9e4066Sahrens 		    ds->ds_object ||
20601d452cf5Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
20611d452cf5Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
20621d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2063fa9e4066Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
20641d452cf5Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
20651d452cf5Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
2066088f3894Sahrens 		} else if (next_clones_obj != 0) {
2067c33e334fSMatthew Ahrens 			remove_from_next_clones(ds->ds_prev,
2068c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
2069088f3894Sahrens 			VERIFY3U(0, ==, zap_add_int(mos,
2070088f3894Sahrens 			    next_clones_obj, dsobj, tx));
2071fa9e4066Sahrens 		}
2072fa9e4066Sahrens 	}
2073fa9e4066Sahrens 
2074a9799022Sck 	/*
2075a9799022Sck 	 * If we have a reference-reservation on this dataset, we will
2076a9799022Sck 	 * need to increase the amount of refreservation being charged
2077a9799022Sck 	 * since our unique space is going to zero.
2078a9799022Sck 	 */
2079a9799022Sck 	if (ds->ds_reserved) {
20803f9d6ad7SLin Ling 		int64_t delta;
20813f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
20823f9d6ad7SLin Ling 		delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
208374e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
20843f9d6ad7SLin Ling 		    delta, 0, 0, tx);
2085a9799022Sck 	}
2086a9799022Sck 
2087fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
2088cde58dbcSMatthew Ahrens 	zfs_dbgmsg("taking snapshot %s@%s/%llu; newkey=%llu",
2089cde58dbcSMatthew Ahrens 	    ds->ds_dir->dd_myname, snapname, dsobj,
2090cde58dbcSMatthew Ahrens 	    ds->ds_phys->ds_prev_snap_txg);
2091cde58dbcSMatthew Ahrens 	ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
2092cde58dbcSMatthew Ahrens 	    UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
2093cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
2094cde58dbcSMatthew Ahrens 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
2095cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
2096cde58dbcSMatthew Ahrens 	    ds->ds_phys->ds_prev_snap_txg, tx);
2097cde58dbcSMatthew Ahrens 
2098a4611edeSahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
2099fa9e4066Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
2100088f3894Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
2101fa9e4066Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
2102a9799022Sck 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
2103a9799022Sck 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
2104fa9e4066Sahrens 
2105fa9e4066Sahrens 	err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
2106fa9e4066Sahrens 	    snapname, 8, 1, &dsobj, tx);
2107fa9e4066Sahrens 	ASSERT(err == 0);
2108fa9e4066Sahrens 
2109fa9e4066Sahrens 	if (ds->ds_prev)
2110745cd3c5Smaybee 		dsl_dataset_drop_ref(ds->ds_prev, ds);
2111745cd3c5Smaybee 	VERIFY(0 == dsl_dataset_get_ref(dp,
2112745cd3c5Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
2113ecd6cf80Smarks 
21143f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
2115088f3894Sahrens 
211671eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
211771eb0538SChris Kirby 
21183f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_SNAPSHOT, dp->dp_spa, tx,
211940feaa91Sahrens 	    "dataset = %llu", dsobj);
2120fa9e4066Sahrens }
2121fa9e4066Sahrens 
2122fa9e4066Sahrens void
2123c717a561Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
2124fa9e4066Sahrens {
2125fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
2126503ad85cSMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
2127fa9e4066Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
2128fa9e4066Sahrens 
212991ebeef5Sahrens 	/*
213091ebeef5Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
213191ebeef5Sahrens 	 * sync it out now.
213291ebeef5Sahrens 	 */
213391ebeef5Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
213491ebeef5Sahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
213591ebeef5Sahrens 
2136fa9e4066Sahrens 	dsl_dir_dirty(ds->ds_dir, tx);
2137503ad85cSMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
2138fa9e4066Sahrens }
2139fa9e4066Sahrens 
214019b94df9SMatthew Ahrens static void
214119b94df9SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
214219b94df9SMatthew Ahrens {
214319b94df9SMatthew Ahrens 	uint64_t count = 0;
214419b94df9SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
214519b94df9SMatthew Ahrens 	zap_cursor_t zc;
214619b94df9SMatthew Ahrens 	zap_attribute_t za;
214719b94df9SMatthew Ahrens 	nvlist_t *propval;
214819b94df9SMatthew Ahrens 	nvlist_t *val;
214919b94df9SMatthew Ahrens 
215019b94df9SMatthew Ahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
215119b94df9SMatthew Ahrens 	VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
215219b94df9SMatthew Ahrens 	VERIFY(nvlist_alloc(&val, NV_UNIQUE_NAME, KM_SLEEP) == 0);
215319b94df9SMatthew Ahrens 
215419b94df9SMatthew Ahrens 	/*
215519b94df9SMatthew Ahrens 	 * There may me missing entries in ds_next_clones_obj
215619b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
215719b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
215819b94df9SMatthew Ahrens 	 */
215919b94df9SMatthew Ahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
216019b94df9SMatthew Ahrens 		ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj,
216119b94df9SMatthew Ahrens 		    &count));
216219b94df9SMatthew Ahrens 	}
216319b94df9SMatthew Ahrens 	if (count != ds->ds_phys->ds_num_children - 1) {
216419b94df9SMatthew Ahrens 		goto fail;
216519b94df9SMatthew Ahrens 	}
216619b94df9SMatthew Ahrens 	for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
216719b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
216819b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
216919b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
217019b94df9SMatthew Ahrens 		char buf[ZFS_MAXNAMELEN];
217119b94df9SMatthew Ahrens 		if (dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
217219b94df9SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone) != 0) {
217319b94df9SMatthew Ahrens 			goto fail;
217419b94df9SMatthew Ahrens 		}
217519b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
217619b94df9SMatthew Ahrens 		VERIFY(nvlist_add_boolean(val, buf) == 0);
217719b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
217819b94df9SMatthew Ahrens 	}
217919b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
218019b94df9SMatthew Ahrens 	VERIFY(nvlist_add_nvlist(propval, ZPROP_VALUE, val) == 0);
218119b94df9SMatthew Ahrens 	VERIFY(nvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
218219b94df9SMatthew Ahrens 	    propval) == 0);
218319b94df9SMatthew Ahrens fail:
218419b94df9SMatthew Ahrens 	nvlist_free(val);
218519b94df9SMatthew Ahrens 	nvlist_free(propval);
218619b94df9SMatthew Ahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
218719b94df9SMatthew Ahrens }
218819b94df9SMatthew Ahrens 
2189fa9e4066Sahrens void
2190a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2191fa9e4066Sahrens {
2192187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
2193a9799022Sck 
2194a2eea2e1Sahrens 	dsl_dir_stats(ds->ds_dir, nv);
2195fa9e4066Sahrens 
2196a9799022Sck 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
2197a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
2198a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
2199a9799022Sck 
2200a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2201a2eea2e1Sahrens 	    ds->ds_phys->ds_creation_time);
2202a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2203a2eea2e1Sahrens 	    ds->ds_phys->ds_creation_txg);
2204a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2205a9799022Sck 	    ds->ds_quota);
2206a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2207a9799022Sck 	    ds->ds_reserved);
2208c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2209c5904d13Seschrock 	    ds->ds_phys->ds_guid);
22101d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
22113f9d6ad7SLin Ling 	    ds->ds_phys->ds_unique_bytes);
22121d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
22131d713200SEric Schrock 	    ds->ds_object);
221492241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
221592241e0bSTom Erickson 	    ds->ds_userrefs);
2216842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2217842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2218fa9e4066Sahrens 
221919b94df9SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
222019b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
222119b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
222219b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
222319b94df9SMatthew Ahrens 
222419b94df9SMatthew Ahrens 		rw_enter(&dp->dp_config_rwlock, RW_READER);
222519b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
222619b94df9SMatthew Ahrens 		    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
222719b94df9SMatthew Ahrens 		rw_exit(&dp->dp_config_rwlock);
222819b94df9SMatthew Ahrens 		if (err == 0) {
222919b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
223019b94df9SMatthew Ahrens 			    &comp, &uncomp);
223119b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
223219b94df9SMatthew Ahrens 			if (err == 0) {
223319b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
223419b94df9SMatthew Ahrens 				    written);
223519b94df9SMatthew Ahrens 			}
223619b94df9SMatthew Ahrens 		}
223719b94df9SMatthew Ahrens 	}
223819b94df9SMatthew Ahrens 
2239187d6ac0SMatt Ahrens 	ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
2240187d6ac0SMatt Ahrens 	    (ds->ds_phys->ds_uncompressed_bytes * 100 /
2241187d6ac0SMatt Ahrens 	    ds->ds_phys->ds_compressed_bytes);
2242187d6ac0SMatt Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
2243187d6ac0SMatt Ahrens 
2244fa9e4066Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
2245fa9e4066Sahrens 		/*
2246fa9e4066Sahrens 		 * This is a snapshot; override the dd's space used with
2247a2eea2e1Sahrens 		 * our unique space and compression ratio.
2248fa9e4066Sahrens 		 */
2249a2eea2e1Sahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2250a2eea2e1Sahrens 		    ds->ds_phys->ds_unique_bytes);
2251187d6ac0SMatt Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
225219b94df9SMatthew Ahrens 
225319b94df9SMatthew Ahrens 		get_clones_stat(ds, nv);
2254fa9e4066Sahrens 	}
2255fa9e4066Sahrens }
2256fa9e4066Sahrens 
2257a2eea2e1Sahrens void
2258a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2259a2eea2e1Sahrens {
2260a2eea2e1Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
2261a2eea2e1Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
22623cb34c60Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
2263a2eea2e1Sahrens 	if (ds->ds_phys->ds_next_snap_obj) {
2264a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
2265a2eea2e1Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
2266ebedde84SEric Taylor 	} else {
2267ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
2268ebedde84SEric Taylor 		stat->dds_num_clones = 0;
2269a2eea2e1Sahrens 	}
2270a2eea2e1Sahrens 
2271a2eea2e1Sahrens 	/* clone origin is really a dsl_dir thing... */
22724ccbb6e7Sahrens 	rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
2273088f3894Sahrens 	if (dsl_dir_is_clone(ds->ds_dir)) {
2274a2eea2e1Sahrens 		dsl_dataset_t *ods;
2275a2eea2e1Sahrens 
2276745cd3c5Smaybee 		VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool,
2277745cd3c5Smaybee 		    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
22783cb34c60Sahrens 		dsl_dataset_name(ods, stat->dds_origin);
2279745cd3c5Smaybee 		dsl_dataset_drop_ref(ods, FTAG);
2280ebedde84SEric Taylor 	} else {
2281ebedde84SEric Taylor 		stat->dds_origin[0] = '\0';
2282a2eea2e1Sahrens 	}
22834ccbb6e7Sahrens 	rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
2284a2eea2e1Sahrens }
2285a2eea2e1Sahrens 
2286a2eea2e1Sahrens uint64_t
2287a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2288a2eea2e1Sahrens {
228991ebeef5Sahrens 	return (ds->ds_fsid_guid);
2290a2eea2e1Sahrens }
2291a2eea2e1Sahrens 
2292a2eea2e1Sahrens void
2293a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
2294a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
2295a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
2296fa9e4066Sahrens {
2297a2eea2e1Sahrens 	*refdbytesp = ds->ds_phys->ds_used_bytes;
2298a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2299a9799022Sck 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
2300a9799022Sck 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
2301a9799022Sck 	if (ds->ds_quota != 0) {
2302a9799022Sck 		/*
2303a9799022Sck 		 * Adjust available bytes according to refquota
2304a9799022Sck 		 */
2305a9799022Sck 		if (*refdbytesp < ds->ds_quota)
2306a9799022Sck 			*availbytesp = MIN(*availbytesp,
2307a9799022Sck 			    ds->ds_quota - *refdbytesp);
2308a9799022Sck 		else
2309a9799022Sck 			*availbytesp = 0;
2310a9799022Sck 	}
2311a2eea2e1Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
2312a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
2313fa9e4066Sahrens }
2314fa9e4066Sahrens 
2315f18faf3fSek boolean_t
2316f18faf3fSek dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds)
2317f18faf3fSek {
2318f18faf3fSek 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2319f18faf3fSek 
2320f18faf3fSek 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) ||
2321f18faf3fSek 	    dsl_pool_sync_context(dp));
2322f18faf3fSek 	if (ds->ds_prev == NULL)
2323f18faf3fSek 		return (B_FALSE);
2324f18faf3fSek 	if (ds->ds_phys->ds_bp.blk_birth >
23256e0cbcaaSMatthew Ahrens 	    ds->ds_prev->ds_phys->ds_creation_txg) {
23266e0cbcaaSMatthew Ahrens 		objset_t *os, *os_prev;
23276e0cbcaaSMatthew Ahrens 		/*
23286e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
23296e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
23306e0cbcaaSMatthew Ahrens 		 * modified.
23316e0cbcaaSMatthew Ahrens 		 */
23326e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
23336e0cbcaaSMatthew Ahrens 			return (B_TRUE);
23346e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0)
23356e0cbcaaSMatthew Ahrens 			return (B_TRUE);
23366e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
23376e0cbcaaSMatthew Ahrens 		    &os_prev->os_phys->os_meta_dnode,
23386e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
23396e0cbcaaSMatthew Ahrens 	}
2340f18faf3fSek 	return (B_FALSE);
2341f18faf3fSek }
2342f18faf3fSek 
23431d452cf5Sahrens /* ARGSUSED */
2344fa9e4066Sahrens static int
23451d452cf5Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx)
2346fa9e4066Sahrens {
23471d452cf5Sahrens 	dsl_dataset_t *ds = arg1;
23481d452cf5Sahrens 	char *newsnapname = arg2;
23491d452cf5Sahrens 	dsl_dir_t *dd = ds->ds_dir;
23501d452cf5Sahrens 	dsl_dataset_t *hds;
2351fa9e4066Sahrens 	uint64_t val;
23521d452cf5Sahrens 	int err;
2353fa9e4066Sahrens 
2354745cd3c5Smaybee 	err = dsl_dataset_hold_obj(dd->dd_pool,
2355745cd3c5Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds);
2356fa9e4066Sahrens 	if (err)
2357fa9e4066Sahrens 		return (err);
2358fa9e4066Sahrens 
23591d452cf5Sahrens 	/* new name better not be in use */
2360745cd3c5Smaybee 	err = dsl_dataset_snap_lookup(hds, newsnapname, &val);
2361745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
23621d452cf5Sahrens 
23631d452cf5Sahrens 	if (err == 0)
23641d452cf5Sahrens 		err = EEXIST;
23651d452cf5Sahrens 	else if (err == ENOENT)
23661d452cf5Sahrens 		err = 0;
2367cdf5b4caSmmusante 
2368cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
2369cdf5b4caSmmusante 	if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN)
2370cdf5b4caSmmusante 		err = ENAMETOOLONG;
2371cdf5b4caSmmusante 
23721d452cf5Sahrens 	return (err);
23731d452cf5Sahrens }
2374fa9e4066Sahrens 
23751d452cf5Sahrens static void
23763f9d6ad7SLin Ling dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx)
23771d452cf5Sahrens {
23781d452cf5Sahrens 	dsl_dataset_t *ds = arg1;
2379ecd6cf80Smarks 	const char *newsnapname = arg2;
23801d452cf5Sahrens 	dsl_dir_t *dd = ds->ds_dir;
23811d452cf5Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
23821d452cf5Sahrens 	dsl_dataset_t *hds;
23831d452cf5Sahrens 	int err;
2384fa9e4066Sahrens 
23851d452cf5Sahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj != 0);
2386fa9e4066Sahrens 
2387745cd3c5Smaybee 	VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool,
2388745cd3c5Smaybee 	    dd->dd_phys->dd_head_dataset_obj, FTAG, &hds));
2389fa9e4066Sahrens 
23901d452cf5Sahrens 	VERIFY(0 == dsl_dataset_get_snapname(ds));
2391745cd3c5Smaybee 	err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx);
2392fa9e4066Sahrens 	ASSERT3U(err, ==, 0);
23931d452cf5Sahrens 	mutex_enter(&ds->ds_lock);
23941d452cf5Sahrens 	(void) strcpy(ds->ds_snapname, newsnapname);
23951d452cf5Sahrens 	mutex_exit(&ds->ds_lock);
23961d452cf5Sahrens 	err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj,
23971d452cf5Sahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx);
2398fa9e4066Sahrens 	ASSERT3U(err, ==, 0);
2399fa9e4066Sahrens 
24003f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx,
24013f9d6ad7SLin Ling 	    "dataset = %llu", ds->ds_object);
2402745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
2403fa9e4066Sahrens }
2404fa9e4066Sahrens 
2405f18faf3fSek struct renamesnaparg {
2406cdf5b4caSmmusante 	dsl_sync_task_group_t *dstg;
2407cdf5b4caSmmusante 	char failed[MAXPATHLEN];
2408cdf5b4caSmmusante 	char *oldsnap;
2409cdf5b4caSmmusante 	char *newsnap;
2410cdf5b4caSmmusante };
2411cdf5b4caSmmusante 
2412cdf5b4caSmmusante static int
2413fd136879SMatthew Ahrens dsl_snapshot_rename_one(const char *name, void *arg)
2414cdf5b4caSmmusante {
2415f18faf3fSek 	struct renamesnaparg *ra = arg;
2416cdf5b4caSmmusante 	dsl_dataset_t *ds = NULL;
2417fd136879SMatthew Ahrens 	char *snapname;
2418cdf5b4caSmmusante 	int err;
2419cdf5b4caSmmusante 
2420fd136879SMatthew Ahrens 	snapname = kmem_asprintf("%s@%s", name, ra->oldsnap);
2421fd136879SMatthew Ahrens 	(void) strlcpy(ra->failed, snapname, sizeof (ra->failed));
2422ecd6cf80Smarks 
2423ecd6cf80Smarks 	/*
2424ecd6cf80Smarks 	 * For recursive snapshot renames the parent won't be changing
2425ecd6cf80Smarks 	 * so we just pass name for both the to/from argument.
2426ecd6cf80Smarks 	 */
2427fd136879SMatthew Ahrens 	err = zfs_secpolicy_rename_perms(snapname, snapname, CRED());
2428fd136879SMatthew Ahrens 	if (err != 0) {
2429fd136879SMatthew Ahrens 		strfree(snapname);
2430fd136879SMatthew Ahrens 		return (err == ENOENT ? 0 : err);
2431ecd6cf80Smarks 	}
2432ecd6cf80Smarks 
2433745cd3c5Smaybee #ifdef _KERNEL
2434745cd3c5Smaybee 	/*
2435745cd3c5Smaybee 	 * For all filesystems undergoing rename, we'll need to unmount it.
2436745cd3c5Smaybee 	 */
2437fd136879SMatthew Ahrens 	(void) zfs_unmount_snap(snapname, NULL);
2438745cd3c5Smaybee #endif
2439fd136879SMatthew Ahrens 	err = dsl_dataset_hold(snapname, ra->dstg, &ds);
24403f1f8012SMatthew Ahrens 	strfree(snapname);
24413f1f8012SMatthew Ahrens 	if (err != 0)
2442fd136879SMatthew Ahrens 		return (err == ENOENT ? 0 : err);
2443cdf5b4caSmmusante 
2444cdf5b4caSmmusante 	dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check,
2445cdf5b4caSmmusante 	    dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0);
2446cdf5b4caSmmusante 
2447cdf5b4caSmmusante 	return (0);
2448cdf5b4caSmmusante }
2449cdf5b4caSmmusante 
2450cdf5b4caSmmusante static int
2451cdf5b4caSmmusante dsl_recursive_rename(char *oldname, const char *newname)
2452cdf5b4caSmmusante {
2453cdf5b4caSmmusante 	int err;
2454f18faf3fSek 	struct renamesnaparg *ra;
2455cdf5b4caSmmusante 	dsl_sync_task_t *dst;
2456cdf5b4caSmmusante 	spa_t *spa;
2457cdf5b4caSmmusante 	char *cp, *fsname = spa_strdup(oldname);
2458fd136879SMatthew Ahrens 	int len = strlen(oldname) + 1;
2459cdf5b4caSmmusante 
2460cdf5b4caSmmusante 	/* truncate the snapshot name to get the fsname */
2461cdf5b4caSmmusante 	cp = strchr(fsname, '@');
2462cdf5b4caSmmusante 	*cp = '\0';
2463cdf5b4caSmmusante 
246440feaa91Sahrens 	err = spa_open(fsname, &spa, FTAG);
2465cdf5b4caSmmusante 	if (err) {
2466fd136879SMatthew Ahrens 		kmem_free(fsname, len);
2467cdf5b4caSmmusante 		return (err);
2468cdf5b4caSmmusante 	}
2469f18faf3fSek 	ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP);
2470cdf5b4caSmmusante 	ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
2471cdf5b4caSmmusante 
2472cdf5b4caSmmusante 	ra->oldsnap = strchr(oldname, '@') + 1;
2473cdf5b4caSmmusante 	ra->newsnap = strchr(newname, '@') + 1;
2474cdf5b4caSmmusante 	*ra->failed = '\0';
2475cdf5b4caSmmusante 
2476cdf5b4caSmmusante 	err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra,
2477cdf5b4caSmmusante 	    DS_FIND_CHILDREN);
2478fd136879SMatthew Ahrens 	kmem_free(fsname, len);
2479cdf5b4caSmmusante 
2480cdf5b4caSmmusante 	if (err == 0) {
2481cdf5b4caSmmusante 		err = dsl_sync_task_group_wait(ra->dstg);
2482cdf5b4caSmmusante 	}
2483cdf5b4caSmmusante 
2484cdf5b4caSmmusante 	for (dst = list_head(&ra->dstg->dstg_tasks); dst;
2485cdf5b4caSmmusante 	    dst = list_next(&ra->dstg->dstg_tasks, dst)) {
2486cdf5b4caSmmusante 		dsl_dataset_t *ds = dst->dst_arg1;
2487cdf5b4caSmmusante 		if (dst->dst_err) {
2488cdf5b4caSmmusante 			dsl_dir_name(ds->ds_dir, ra->failed);
2489fd136879SMatthew Ahrens 			(void) strlcat(ra->failed, "@", sizeof (ra->failed));
2490fd136879SMatthew Ahrens 			(void) strlcat(ra->failed, ra->newsnap,
2491fd136879SMatthew Ahrens 			    sizeof (ra->failed));
2492cdf5b4caSmmusante 		}
2493745cd3c5Smaybee 		dsl_dataset_rele(ds, ra->dstg);
2494cdf5b4caSmmusante 	}
2495cdf5b4caSmmusante 
2496ecd6cf80Smarks 	if (err)
2497fd136879SMatthew Ahrens 		(void) strlcpy(oldname, ra->failed, sizeof (ra->failed));
2498cdf5b4caSmmusante 
2499cdf5b4caSmmusante 	dsl_sync_task_group_destroy(ra->dstg);
2500f18faf3fSek 	kmem_free(ra, sizeof (struct renamesnaparg));
2501cdf5b4caSmmusante 	spa_close(spa, FTAG);
2502cdf5b4caSmmusante 	return (err);
2503cdf5b4caSmmusante }
2504cdf5b4caSmmusante 
25053a5a36beSmmusante static int
2506fd136879SMatthew Ahrens dsl_valid_rename(const char *oldname, void *arg)
25073a5a36beSmmusante {
25083a5a36beSmmusante 	int delta = *(int *)arg;
25093a5a36beSmmusante 
25103a5a36beSmmusante 	if (strlen(oldname) + delta >= MAXNAMELEN)
25113a5a36beSmmusante 		return (ENAMETOOLONG);
25123a5a36beSmmusante 
25133a5a36beSmmusante 	return (0);
25143a5a36beSmmusante }
25153a5a36beSmmusante 
2516fa9e4066Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename
2517fa9e4066Sahrens int
2518745cd3c5Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive)
2519fa9e4066Sahrens {
2520fa9e4066Sahrens 	dsl_dir_t *dd;
25211d452cf5Sahrens 	dsl_dataset_t *ds;
2522fa9e4066Sahrens 	const char *tail;
2523fa9e4066Sahrens 	int err;
2524fa9e4066Sahrens 
25251d452cf5Sahrens 	err = dsl_dir_open(oldname, FTAG, &dd, &tail);
2526ea8dc4b6Seschrock 	if (err)
2527ea8dc4b6Seschrock 		return (err);
2528370c1af0SSanjeev Bagewadi 
2529fa9e4066Sahrens 	if (tail == NULL) {
25303a5a36beSmmusante 		int delta = strlen(newname) - strlen(oldname);
25313a5a36beSmmusante 
2532088f3894Sahrens 		/* if we're growing, validate child name lengths */
25333a5a36beSmmusante 		if (delta > 0)
25343a5a36beSmmusante 			err = dmu_objset_find(oldname, dsl_valid_rename,
25353a5a36beSmmusante 			    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
25363a5a36beSmmusante 
2537b91a2f0bSMatthew Ahrens 		if (err == 0)
25383a5a36beSmmusante 			err = dsl_dir_rename(dd, newname);
2539fa9e4066Sahrens 		dsl_dir_close(dd, FTAG);
2540fa9e4066Sahrens 		return (err);
2541fa9e4066Sahrens 	}
2542370c1af0SSanjeev Bagewadi 
2543fa9e4066Sahrens 	if (tail[0] != '@') {
2544681d9761SEric Taylor 		/* the name ended in a nonexistent component */
2545fa9e4066Sahrens 		dsl_dir_close(dd, FTAG);
2546fa9e4066Sahrens 		return (ENOENT);
2547fa9e4066Sahrens 	}
2548fa9e4066Sahrens 
2549fa9e4066Sahrens 	dsl_dir_close(dd, FTAG);
25501d452cf5Sahrens 
25511d452cf5Sahrens 	/* new name must be snapshot in same filesystem */
25521d452cf5Sahrens 	tail = strchr(newname, '@');
25531d452cf5Sahrens 	if (tail == NULL)
25541d452cf5Sahrens 		return (EINVAL);
25551d452cf5Sahrens 	tail++;
25561d452cf5Sahrens 	if (strncmp(oldname, newname, tail - newname) != 0)
25571d452cf5Sahrens 		return (EXDEV);
25581d452cf5Sahrens 
2559cdf5b4caSmmusante 	if (recursive) {
2560cdf5b4caSmmusante 		err = dsl_recursive_rename(oldname, newname);
2561cdf5b4caSmmusante 	} else {
2562745cd3c5Smaybee 		err = dsl_dataset_hold(oldname, FTAG, &ds);
2563cdf5b4caSmmusante 		if (err)
2564cdf5b4caSmmusante 			return (err);
25651d452cf5Sahrens 
2566cdf5b4caSmmusante 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
2567cdf5b4caSmmusante 		    dsl_dataset_snapshot_rename_check,
2568cdf5b4caSmmusante 		    dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1);
25691d452cf5Sahrens 
2570745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
2571cdf5b4caSmmusante 	}
25721d452cf5Sahrens 
2573fa9e4066Sahrens 	return (err);
2574fa9e4066Sahrens }
257599653d4eSeschrock 
2576088f3894Sahrens struct promotenode {
2577745cd3c5Smaybee 	list_node_t link;
2578745cd3c5Smaybee 	dsl_dataset_t *ds;
2579745cd3c5Smaybee };
2580745cd3c5Smaybee 
25811d452cf5Sahrens struct promotearg {
258274e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
25833f9d6ad7SLin Ling 	dsl_dataset_t *origin_origin;
258474e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2585681d9761SEric Taylor 	char *err_ds;
25861d452cf5Sahrens };
25871d452cf5Sahrens 
258874e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
25893f9d6ad7SLin Ling static boolean_t snaplist_unstable(list_t *l);
259074e7dc98SMatthew Ahrens 
259199653d4eSeschrock static int
25921d452cf5Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx)
259399653d4eSeschrock {
25941d452cf5Sahrens 	dsl_dataset_t *hds = arg1;
25951d452cf5Sahrens 	struct promotearg *pa = arg2;
259674e7dc98SMatthew Ahrens 	struct promotenode *snap = list_head(&pa->shared_snaps);
2597745cd3c5Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
2598745cd3c5Smaybee 	int err;
2599cde58dbcSMatthew Ahrens 	uint64_t unused;
26001d452cf5Sahrens 
2601088f3894Sahrens 	/* Check that it is a real clone */
2602088f3894Sahrens 	if (!dsl_dir_is_clone(hds->ds_dir))
260399653d4eSeschrock 		return (EINVAL);
260499653d4eSeschrock 
26051d452cf5Sahrens 	/* Since this is so expensive, don't do the preliminary check */
26061d452cf5Sahrens 	if (!dmu_tx_is_syncing(tx))
26071d452cf5Sahrens 		return (0);
26081d452cf5Sahrens 
2609745cd3c5Smaybee 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)
2610745cd3c5Smaybee 		return (EXDEV);
261199653d4eSeschrock 
26123cb34c60Sahrens 	/* compute origin's new unique space */
261374e7dc98SMatthew Ahrens 	snap = list_tail(&pa->clone_snaps);
261474e7dc98SMatthew Ahrens 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2615cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2616cde58dbcSMatthew Ahrens 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
2617cde58dbcSMatthew Ahrens 	    &pa->unique, &unused, &unused);
261899653d4eSeschrock 
2619745cd3c5Smaybee 	/*
2620745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2621745cd3c5Smaybee 	 *
262274e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
262374e7dc98SMatthew Ahrens 	 * to used for each snapshot:
262474e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
262574e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
262674e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2627745cd3c5Smaybee 	 * So a sequence would look like:
262874e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2629745cd3c5Smaybee 	 * Which simplifies to:
263074e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2631745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
263274e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2633745cd3c5Smaybee 	 */
2634745cd3c5Smaybee 	pa->used = origin_ds->ds_phys->ds_used_bytes;
2635745cd3c5Smaybee 	pa->comp = origin_ds->ds_phys->ds_compressed_bytes;
2636745cd3c5Smaybee 	pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
263774e7dc98SMatthew Ahrens 	for (snap = list_head(&pa->shared_snaps); snap;
263874e7dc98SMatthew Ahrens 	    snap = list_next(&pa->shared_snaps, snap)) {
263999653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2640745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
264199653d4eSeschrock 
264299653d4eSeschrock 		/* Check that the snapshot name does not conflict */
264374e7dc98SMatthew Ahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
2644745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2645681d9761SEric Taylor 		if (err == 0) {
2646681d9761SEric Taylor 			err = EEXIST;
2647681d9761SEric Taylor 			goto out;
2648681d9761SEric Taylor 		}
2649745cd3c5Smaybee 		if (err != ENOENT)
2650681d9761SEric Taylor 			goto out;
265199653d4eSeschrock 
2652745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
265374e7dc98SMatthew Ahrens 		if (ds->ds_phys->ds_prev_snap_obj == 0)
265474e7dc98SMatthew Ahrens 			continue;
265574e7dc98SMatthew Ahrens 
2656cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2657cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
265874e7dc98SMatthew Ahrens 		pa->used += dlused;
265974e7dc98SMatthew Ahrens 		pa->comp += dlcomp;
266074e7dc98SMatthew Ahrens 		pa->uncomp += dluncomp;
266174e7dc98SMatthew Ahrens 	}
2662745cd3c5Smaybee 
2663745cd3c5Smaybee 	/*
2664745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
2665745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
2666745cd3c5Smaybee 	 */
266774e7dc98SMatthew Ahrens 	if (pa->origin_origin) {
266874e7dc98SMatthew Ahrens 		pa->used -= pa->origin_origin->ds_phys->ds_used_bytes;
266974e7dc98SMatthew Ahrens 		pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes;
267074e7dc98SMatthew Ahrens 		pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes;
267199653d4eSeschrock 	}
267299653d4eSeschrock 
267399653d4eSeschrock 	/* Check that there is enough space here */
267474e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
267574e7dc98SMatthew Ahrens 	    pa->used);
267674e7dc98SMatthew Ahrens 	if (err)
267774e7dc98SMatthew Ahrens 		return (err);
267874e7dc98SMatthew Ahrens 
267974e7dc98SMatthew Ahrens 	/*
268074e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
268174e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
268274e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
268374e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
268474e7dc98SMatthew Ahrens 	 */
268574e7dc98SMatthew Ahrens 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
268674e7dc98SMatthew Ahrens 		uint64_t space;
268774e7dc98SMatthew Ahrens 
268874e7dc98SMatthew Ahrens 		/*
268974e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
26903f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
2691cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
269274e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
269374e7dc98SMatthew Ahrens 		 * iterate over all bps.
269474e7dc98SMatthew Ahrens 		 */
269574e7dc98SMatthew Ahrens 		snap = list_head(&pa->origin_snaps);
269674e7dc98SMatthew Ahrens 		err = snaplist_space(&pa->shared_snaps,
26973f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &pa->cloneusedsnap);
269874e7dc98SMatthew Ahrens 		if (err)
269974e7dc98SMatthew Ahrens 			return (err);
270074e7dc98SMatthew Ahrens 
270174e7dc98SMatthew Ahrens 		err = snaplist_space(&pa->clone_snaps,
27023f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
270374e7dc98SMatthew Ahrens 		if (err)
270474e7dc98SMatthew Ahrens 			return (err);
270574e7dc98SMatthew Ahrens 		pa->cloneusedsnap += space;
270674e7dc98SMatthew Ahrens 	}
270774e7dc98SMatthew Ahrens 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
270874e7dc98SMatthew Ahrens 		err = snaplist_space(&pa->origin_snaps,
270974e7dc98SMatthew Ahrens 		    origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap);
271074e7dc98SMatthew Ahrens 		if (err)
271174e7dc98SMatthew Ahrens 			return (err);
2712745cd3c5Smaybee 	}
27131d452cf5Sahrens 
271474e7dc98SMatthew Ahrens 	return (0);
2715681d9761SEric Taylor out:
2716681d9761SEric Taylor 	pa->err_ds =  snap->ds->ds_snapname;
2717681d9761SEric Taylor 	return (err);
27181d452cf5Sahrens }
271999653d4eSeschrock 
27201d452cf5Sahrens static void
27213f9d6ad7SLin Ling dsl_dataset_promote_sync(void *arg1, void *arg2, dmu_tx_t *tx)
27221d452cf5Sahrens {
27231d452cf5Sahrens 	dsl_dataset_t *hds = arg1;
27241d452cf5Sahrens 	struct promotearg *pa = arg2;
272574e7dc98SMatthew Ahrens 	struct promotenode *snap = list_head(&pa->shared_snaps);
2726745cd3c5Smaybee 	dsl_dataset_t *origin_ds = snap->ds;
272774e7dc98SMatthew Ahrens 	dsl_dataset_t *origin_head;
27281d452cf5Sahrens 	dsl_dir_t *dd = hds->ds_dir;
27291d452cf5Sahrens 	dsl_pool_t *dp = hds->ds_dir->dd_pool;
27303cb34c60Sahrens 	dsl_dir_t *odd = NULL;
2731088f3894Sahrens 	uint64_t oldnext_obj;
273274e7dc98SMatthew Ahrens 	int64_t delta;
27331d452cf5Sahrens 
27341d452cf5Sahrens 	ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE));
27351d452cf5Sahrens 
273674e7dc98SMatthew Ahrens 	snap = list_head(&pa->origin_snaps);
273774e7dc98SMatthew Ahrens 	origin_head = snap->ds;
273874e7dc98SMatthew Ahrens 
27390b69c2f0Sahrens 	/*
27403cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
27410b69c2f0Sahrens 	 * changing.
27420b69c2f0Sahrens 	 */
27433cb34c60Sahrens 	VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object,
27443cb34c60Sahrens 	    NULL, FTAG, &odd));
274599653d4eSeschrock 
2746745cd3c5Smaybee 	/* change origin's next snap */
2747745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2748088f3894Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
274974e7dc98SMatthew Ahrens 	snap = list_tail(&pa->clone_snaps);
275074e7dc98SMatthew Ahrens 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
275174e7dc98SMatthew Ahrens 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2752745cd3c5Smaybee 
2753088f3894Sahrens 	/* change the origin's next clone */
2754088f3894Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
2755c33e334fSMatthew Ahrens 		remove_from_next_clones(origin_ds, snap->ds->ds_object, tx);
2756088f3894Sahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2757088f3894Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
2758088f3894Sahrens 		    oldnext_obj, tx));
2759088f3894Sahrens 	}
2760088f3894Sahrens 
2761745cd3c5Smaybee 	/* change origin */
2762745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2763745cd3c5Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2764745cd3c5Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
27653f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2766745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2767745cd3c5Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
27683f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
27693f9d6ad7SLin Ling 	    origin_ds->ds_phys->ds_creation_txg;
2770745cd3c5Smaybee 
2771cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2772cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2773cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
2774cde58dbcSMatthew Ahrens 		    odd->dd_phys->dd_clones, hds->ds_object, tx));
2775cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2776cde58dbcSMatthew Ahrens 		    pa->origin_origin->ds_dir->dd_phys->dd_clones,
2777cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2778cde58dbcSMatthew Ahrens 
2779cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
2780cde58dbcSMatthew Ahrens 		    pa->origin_origin->ds_dir->dd_phys->dd_clones,
2781cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
2782cde58dbcSMatthew Ahrens 		if (dd->dd_phys->dd_clones == 0) {
2783cde58dbcSMatthew Ahrens 			dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2784cde58dbcSMatthew Ahrens 			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2785cde58dbcSMatthew Ahrens 		}
2786cde58dbcSMatthew Ahrens 		VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
2787cde58dbcSMatthew Ahrens 		    dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2788cde58dbcSMatthew Ahrens 
2789cde58dbcSMatthew Ahrens 	}
2790cde58dbcSMatthew Ahrens 
279199653d4eSeschrock 	/* move snapshots to this dir */
279274e7dc98SMatthew Ahrens 	for (snap = list_head(&pa->shared_snaps); snap;
279374e7dc98SMatthew Ahrens 	    snap = list_next(&pa->shared_snaps, snap)) {
2794745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
279599653d4eSeschrock 
27963baa08fcSek 		/* unregister props as dsl_dir is changing */
2797503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2798503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2799503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
28003baa08fcSek 		}
280199653d4eSeschrock 		/* move snap name entry */
280274e7dc98SMatthew Ahrens 		VERIFY(0 == dsl_dataset_get_snapname(ds));
280374e7dc98SMatthew Ahrens 		VERIFY(0 == dsl_dataset_snap_remove(origin_head,
2804745cd3c5Smaybee 		    ds->ds_snapname, tx));
28051d452cf5Sahrens 		VERIFY(0 == zap_add(dp->dp_meta_objset,
280699653d4eSeschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
280799653d4eSeschrock 		    8, 1, &ds->ds_object, tx));
2808cde58dbcSMatthew Ahrens 
280999653d4eSeschrock 		/* change containing dsl_dir */
281099653d4eSeschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
28113cb34c60Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
281299653d4eSeschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
28133cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
281499653d4eSeschrock 		dsl_dir_close(ds->ds_dir, ds);
28151d452cf5Sahrens 		VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object,
281699653d4eSeschrock 		    NULL, ds, &ds->ds_dir));
281799653d4eSeschrock 
2818cde58dbcSMatthew Ahrens 		/* move any clone references */
2819cde58dbcSMatthew Ahrens 		if (ds->ds_phys->ds_next_clones_obj &&
2820cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2821cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2822cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2823cde58dbcSMatthew Ahrens 
2824cde58dbcSMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
2825cde58dbcSMatthew Ahrens 			    ds->ds_phys->ds_next_clones_obj);
2826cde58dbcSMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
2827cde58dbcSMatthew Ahrens 			    zap_cursor_advance(&zc)) {
2828cde58dbcSMatthew Ahrens 				dsl_dataset_t *cnds;
2829cde58dbcSMatthew Ahrens 				uint64_t o;
2830cde58dbcSMatthew Ahrens 
2831cde58dbcSMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
2832cde58dbcSMatthew Ahrens 					/*
2833cde58dbcSMatthew Ahrens 					 * We've already moved the
2834cde58dbcSMatthew Ahrens 					 * origin's reference.
2835cde58dbcSMatthew Ahrens 					 */
2836cde58dbcSMatthew Ahrens 					continue;
2837cde58dbcSMatthew Ahrens 				}
2838cde58dbcSMatthew Ahrens 
2839cde58dbcSMatthew Ahrens 				VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
2840cde58dbcSMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
2841cde58dbcSMatthew Ahrens 				o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2842cde58dbcSMatthew Ahrens 
2843cde58dbcSMatthew Ahrens 				VERIFY3U(zap_remove_int(dp->dp_meta_objset,
2844cde58dbcSMatthew Ahrens 				    odd->dd_phys->dd_clones, o, tx), ==, 0);
2845cde58dbcSMatthew Ahrens 				VERIFY3U(zap_add_int(dp->dp_meta_objset,
2846cde58dbcSMatthew Ahrens 				    dd->dd_phys->dd_clones, o, tx), ==, 0);
2847cde58dbcSMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
2848cde58dbcSMatthew Ahrens 			}
2849cde58dbcSMatthew Ahrens 			zap_cursor_fini(&zc);
2850cde58dbcSMatthew Ahrens 		}
2851cde58dbcSMatthew Ahrens 
285299653d4eSeschrock 		ASSERT3U(dsl_prop_numcb(ds), ==, 0);
285374e7dc98SMatthew Ahrens 	}
285474e7dc98SMatthew Ahrens 
285574e7dc98SMatthew Ahrens 	/*
285674e7dc98SMatthew Ahrens 	 * Change space accounting.
285774e7dc98SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
285874e7dc98SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
285974e7dc98SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
286074e7dc98SMatthew Ahrens 	 */
286174e7dc98SMatthew Ahrens 
286274e7dc98SMatthew Ahrens 	delta = pa->cloneusedsnap -
286374e7dc98SMatthew Ahrens 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
286474e7dc98SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
286574e7dc98SMatthew Ahrens 	ASSERT3U(pa->used, >=, delta);
286674e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
286774e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
286874e7dc98SMatthew Ahrens 	    pa->used - delta, pa->comp, pa->uncomp, tx);
286974e7dc98SMatthew Ahrens 
287074e7dc98SMatthew Ahrens 	delta = pa->originusedsnap -
287174e7dc98SMatthew Ahrens 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
287274e7dc98SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
287374e7dc98SMatthew Ahrens 	ASSERT3U(pa->used, >=, -delta);
287474e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
287574e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
287674e7dc98SMatthew Ahrens 	    -pa->used - delta, -pa->comp, -pa->uncomp, tx);
287799653d4eSeschrock 
28783cb34c60Sahrens 	origin_ds->ds_phys->ds_unique_bytes = pa->unique;
287999653d4eSeschrock 
2880ecd6cf80Smarks 	/* log history record */
28813f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx,
28823f9d6ad7SLin Ling 	    "dataset = %llu", hds->ds_object);
2883ecd6cf80Smarks 
28843cb34c60Sahrens 	dsl_dir_close(odd, FTAG);
288599653d4eSeschrock }
288699653d4eSeschrock 
288774e7dc98SMatthew Ahrens static char *snaplist_tag = "snaplist";
288874e7dc98SMatthew Ahrens /*
288974e7dc98SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
289074e7dc98SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
289174e7dc98SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
289274e7dc98SMatthew Ahrens  * snapshots back to this dataset's origin.
289374e7dc98SMatthew Ahrens  */
289474e7dc98SMatthew Ahrens static int
289574e7dc98SMatthew Ahrens snaplist_make(dsl_pool_t *dp, boolean_t own,
289674e7dc98SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l)
289774e7dc98SMatthew Ahrens {
289874e7dc98SMatthew Ahrens 	uint64_t obj = last_obj;
289974e7dc98SMatthew Ahrens 
290074e7dc98SMatthew Ahrens 	ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock));
290174e7dc98SMatthew Ahrens 
290274e7dc98SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
290374e7dc98SMatthew Ahrens 	    offsetof(struct promotenode, link));
290474e7dc98SMatthew Ahrens 
290574e7dc98SMatthew Ahrens 	while (obj != first_obj) {
290674e7dc98SMatthew Ahrens 		dsl_dataset_t *ds;
290774e7dc98SMatthew Ahrens 		struct promotenode *snap;
290874e7dc98SMatthew Ahrens 		int err;
290974e7dc98SMatthew Ahrens 
291074e7dc98SMatthew Ahrens 		if (own) {
291174e7dc98SMatthew Ahrens 			err = dsl_dataset_own_obj(dp, obj,
291274e7dc98SMatthew Ahrens 			    0, snaplist_tag, &ds);
291374e7dc98SMatthew Ahrens 			if (err == 0)
291474e7dc98SMatthew Ahrens 				dsl_dataset_make_exclusive(ds, snaplist_tag);
291574e7dc98SMatthew Ahrens 		} else {
291674e7dc98SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds);
291774e7dc98SMatthew Ahrens 		}
291874e7dc98SMatthew Ahrens 		if (err == ENOENT) {
291974e7dc98SMatthew Ahrens 			/* lost race with snapshot destroy */
292074e7dc98SMatthew Ahrens 			struct promotenode *last = list_tail(l);
292174e7dc98SMatthew Ahrens 			ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj);
292274e7dc98SMatthew Ahrens 			obj = last->ds->ds_phys->ds_prev_snap_obj;
292374e7dc98SMatthew Ahrens 			continue;
292474e7dc98SMatthew Ahrens 		} else if (err) {
292574e7dc98SMatthew Ahrens 			return (err);
292674e7dc98SMatthew Ahrens 		}
292774e7dc98SMatthew Ahrens 
292874e7dc98SMatthew Ahrens 		if (first_obj == 0)
292974e7dc98SMatthew Ahrens 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
293074e7dc98SMatthew Ahrens 
293174e7dc98SMatthew Ahrens 		snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP);
293274e7dc98SMatthew Ahrens 		snap->ds = ds;
293374e7dc98SMatthew Ahrens 		list_insert_tail(l, snap);
293474e7dc98SMatthew Ahrens 		obj = ds->ds_phys->ds_prev_snap_obj;
293574e7dc98SMatthew Ahrens 	}
293674e7dc98SMatthew Ahrens 
293774e7dc98SMatthew Ahrens 	return (0);
293874e7dc98SMatthew Ahrens }
293974e7dc98SMatthew Ahrens 
294074e7dc98SMatthew Ahrens static int
294174e7dc98SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
294274e7dc98SMatthew Ahrens {
294374e7dc98SMatthew Ahrens 	struct promotenode *snap;
294474e7dc98SMatthew Ahrens 
294574e7dc98SMatthew Ahrens 	*spacep = 0;
294674e7dc98SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2947cde58dbcSMatthew Ahrens 		uint64_t used, comp, uncomp;
2948cde58dbcSMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2949cde58dbcSMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
295074e7dc98SMatthew Ahrens 		*spacep += used;
295174e7dc98SMatthew Ahrens 	}
295274e7dc98SMatthew Ahrens 	return (0);
295374e7dc98SMatthew Ahrens }
295474e7dc98SMatthew Ahrens 
295574e7dc98SMatthew Ahrens static void
295674e7dc98SMatthew Ahrens snaplist_destroy(list_t *l, boolean_t own)
295774e7dc98SMatthew Ahrens {
295874e7dc98SMatthew Ahrens 	struct promotenode *snap;
295974e7dc98SMatthew Ahrens 
29604f5064b7SMark J Musante 	if (!l || !list_link_active(&l->list_head))
296174e7dc98SMatthew Ahrens 		return;
296274e7dc98SMatthew Ahrens 
296374e7dc98SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
296474e7dc98SMatthew Ahrens 		list_remove(l, snap);
296574e7dc98SMatthew Ahrens 		if (own)
296674e7dc98SMatthew Ahrens 			dsl_dataset_disown(snap->ds, snaplist_tag);
296774e7dc98SMatthew Ahrens 		else
296874e7dc98SMatthew Ahrens 			dsl_dataset_rele(snap->ds, snaplist_tag);
296974e7dc98SMatthew Ahrens 		kmem_free(snap, sizeof (struct promotenode));
297074e7dc98SMatthew Ahrens 	}
297174e7dc98SMatthew Ahrens 	list_destroy(l);
297274e7dc98SMatthew Ahrens }
297374e7dc98SMatthew Ahrens 
297474e7dc98SMatthew Ahrens /*
297574e7dc98SMatthew Ahrens  * Promote a clone.  Nomenclature note:
297674e7dc98SMatthew Ahrens  * "clone" or "cds": the original clone which is being promoted
297774e7dc98SMatthew Ahrens  * "origin" or "ods": the snapshot which is originally clone's origin
297874e7dc98SMatthew Ahrens  * "origin head" or "ohds": the dataset which is the head
297974e7dc98SMatthew Ahrens  * (filesystem/volume) for the origin
298074e7dc98SMatthew Ahrens  * "origin origin": the origin of the origin's filesystem (typically
298174e7dc98SMatthew Ahrens  * NULL, indicating that the clone is not a clone of a clone).
298274e7dc98SMatthew Ahrens  */
298399653d4eSeschrock int
2984681d9761SEric Taylor dsl_dataset_promote(const char *name, char *conflsnap)
298599653d4eSeschrock {
298699653d4eSeschrock 	dsl_dataset_t *ds;
2987745cd3c5Smaybee 	dsl_dir_t *dd;
2988745cd3c5Smaybee 	dsl_pool_t *dp;
298999653d4eSeschrock 	dmu_object_info_t doi;
299074e7dc98SMatthew Ahrens 	struct promotearg pa = { 0 };
2991088f3894Sahrens 	struct promotenode *snap;
2992745cd3c5Smaybee 	int err;
299399653d4eSeschrock 
2994745cd3c5Smaybee 	err = dsl_dataset_hold(name, FTAG, &ds);
299599653d4eSeschrock 	if (err)
299699653d4eSeschrock 		return (err);
2997745cd3c5Smaybee 	dd = ds->ds_dir;
2998745cd3c5Smaybee 	dp = dd->dd_pool;
299999653d4eSeschrock 
3000745cd3c5Smaybee 	err = dmu_object_info(dp->dp_meta_objset,
300199653d4eSeschrock 	    ds->ds_phys->ds_snapnames_zapobj, &doi);
300299653d4eSeschrock 	if (err) {
3003745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
300499653d4eSeschrock 		return (err);
300599653d4eSeschrock 	}
300699653d4eSeschrock 
300774e7dc98SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) {
300874e7dc98SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
300974e7dc98SMatthew Ahrens 		return (EINVAL);
301074e7dc98SMatthew Ahrens 	}
301174e7dc98SMatthew Ahrens 
3012745cd3c5Smaybee 	/*
3013745cd3c5Smaybee 	 * We are going to inherit all the snapshots taken before our
3014745cd3c5Smaybee 	 * origin (i.e., our new origin will be our parent's origin).
3015745cd3c5Smaybee 	 * Take ownership of them so that we can rename them into our
3016745cd3c5Smaybee 	 * namespace.
3017745cd3c5Smaybee 	 */
3018745cd3c5Smaybee 	rw_enter(&dp->dp_config_rwlock, RW_READER);
3019088f3894Sahrens 
302074e7dc98SMatthew Ahrens 	err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj,
302174e7dc98SMatthew Ahrens 	    &pa.shared_snaps);
302274e7dc98SMatthew Ahrens 	if (err != 0)
302374e7dc98SMatthew Ahrens 		goto out;
3024088f3894Sahrens 
302574e7dc98SMatthew Ahrens 	err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps);
302674e7dc98SMatthew Ahrens 	if (err != 0)
302774e7dc98SMatthew Ahrens 		goto out;
3028088f3894Sahrens 
302974e7dc98SMatthew Ahrens 	snap = list_head(&pa.shared_snaps);
303074e7dc98SMatthew Ahrens 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
303174e7dc98SMatthew Ahrens 	err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj,
303274e7dc98SMatthew Ahrens 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps);
303374e7dc98SMatthew Ahrens 	if (err != 0)
303474e7dc98SMatthew Ahrens 		goto out;
3035088f3894Sahrens 
3036cde58dbcSMatthew Ahrens 	if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
3037cde58dbcSMatthew Ahrens 		err = dsl_dataset_hold_obj(dp,
303874e7dc98SMatthew Ahrens 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
3039cde58dbcSMatthew Ahrens 		    FTAG, &pa.origin_origin);
304074e7dc98SMatthew Ahrens 		if (err != 0)
304174e7dc98SMatthew Ahrens 			goto out;
304274e7dc98SMatthew Ahrens 	}
3043745cd3c5Smaybee 
304474e7dc98SMatthew Ahrens out:
304574e7dc98SMatthew Ahrens 	rw_exit(&dp->dp_config_rwlock);
3046745cd3c5Smaybee 
304799653d4eSeschrock 	/*
304899653d4eSeschrock 	 * Add in 128x the snapnames zapobj size, since we will be moving
304999653d4eSeschrock 	 * a bunch of snapnames to the promoted ds, and dirtying their
305099653d4eSeschrock 	 * bonus buffers.
305199653d4eSeschrock 	 */
305274e7dc98SMatthew Ahrens 	if (err == 0) {
305374e7dc98SMatthew Ahrens 		err = dsl_sync_task_do(dp, dsl_dataset_promote_check,
305474e7dc98SMatthew Ahrens 		    dsl_dataset_promote_sync, ds, &pa,
3055b24ab676SJeff Bonwick 		    2 + 2 * doi.doi_physical_blocks_512);
3056681d9761SEric Taylor 		if (err && pa.err_ds && conflsnap)
3057681d9761SEric Taylor 			(void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN);
3058745cd3c5Smaybee 	}
305974e7dc98SMatthew Ahrens 
306074e7dc98SMatthew Ahrens 	snaplist_destroy(&pa.shared_snaps, B_TRUE);
306174e7dc98SMatthew Ahrens 	snaplist_destroy(&pa.clone_snaps, B_FALSE);
306274e7dc98SMatthew Ahrens 	snaplist_destroy(&pa.origin_snaps, B_FALSE);
306374e7dc98SMatthew Ahrens 	if (pa.origin_origin)
3064cde58dbcSMatthew Ahrens 		dsl_dataset_rele(pa.origin_origin, FTAG);
3065745cd3c5Smaybee 	dsl_dataset_rele(ds, FTAG);
306699653d4eSeschrock 	return (err);
306799653d4eSeschrock }
3068b1b8ab34Slling 
30693cb34c60Sahrens struct cloneswaparg {
30703cb34c60Sahrens 	dsl_dataset_t *cds; /* clone dataset */
30713cb34c60Sahrens 	dsl_dataset_t *ohds; /* origin's head dataset */
30723cb34c60Sahrens 	boolean_t force;
3073a9b821a0Sck 	int64_t unused_refres_delta; /* change in unconsumed refreservation */
30743cb34c60Sahrens };
3075f18faf3fSek 
3076f18faf3fSek /* ARGSUSED */
3077f18faf3fSek static int
3078f18faf3fSek dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx)
3079f18faf3fSek {
30803cb34c60Sahrens 	struct cloneswaparg *csa = arg1;
3081f18faf3fSek 
30823cb34c60Sahrens 	/* they should both be heads */
30833cb34c60Sahrens 	if (dsl_dataset_is_snapshot(csa->cds) ||
30843cb34c60Sahrens 	    dsl_dataset_is_snapshot(csa->ohds))
3085f18faf3fSek 		return (EINVAL);
3086f18faf3fSek 
30873cb34c60Sahrens 	/* the branch point should be just before them */
30883cb34c60Sahrens 	if (csa->cds->ds_prev != csa->ohds->ds_prev)
3089f18faf3fSek 		return (EINVAL);
3090f18faf3fSek 
3091ae46e4c7SMatthew Ahrens 	/* cds should be the clone (unless they are unrelated) */
3092ae46e4c7SMatthew Ahrens 	if (csa->cds->ds_prev != NULL &&
3093ae46e4c7SMatthew Ahrens 	    csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap &&
3094ae46e4c7SMatthew Ahrens 	    csa->ohds->ds_object !=
3095ae46e4c7SMatthew Ahrens 	    csa->cds->ds_prev->ds_phys->ds_next_snap_obj)
30963cb34c60Sahrens 		return (EINVAL);
3097f18faf3fSek 
30983cb34c60Sahrens 	/* the clone should be a child of the origin */
30993cb34c60Sahrens 	if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir)
31003cb34c60Sahrens 		return (EINVAL);
3101f18faf3fSek 
31023cb34c60Sahrens 	/* ohds shouldn't be modified unless 'force' */
31033cb34c60Sahrens 	if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds))
31043cb34c60Sahrens 		return (ETXTBSY);
3105a9b821a0Sck 
3106a9b821a0Sck 	/* adjust amount of any unconsumed refreservation */
3107a9b821a0Sck 	csa->unused_refres_delta =
3108a9b821a0Sck 	    (int64_t)MIN(csa->ohds->ds_reserved,
3109a9b821a0Sck 	    csa->ohds->ds_phys->ds_unique_bytes) -
3110a9b821a0Sck 	    (int64_t)MIN(csa->ohds->ds_reserved,
3111a9b821a0Sck 	    csa->cds->ds_phys->ds_unique_bytes);
3112a9b821a0Sck 
3113a9b821a0Sck 	if (csa->unused_refres_delta > 0 &&
3114a9b821a0Sck 	    csa->unused_refres_delta >
3115a9b821a0Sck 	    dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE))
3116a9b821a0Sck 		return (ENOSPC);
3117a9b821a0Sck 
3118c4cbca4fSChris Kirby 	if (csa->ohds->ds_quota != 0 &&
3119c4cbca4fSChris Kirby 	    csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota)
3120c4cbca4fSChris Kirby 		return (EDQUOT);
3121c4cbca4fSChris Kirby 
31223cb34c60Sahrens 	return (0);
3123f18faf3fSek }
3124f18faf3fSek 
3125f18faf3fSek /* ARGSUSED */
3126f18faf3fSek static void
31273f9d6ad7SLin Ling dsl_dataset_clone_swap_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3128f18faf3fSek {
31293cb34c60Sahrens 	struct cloneswaparg *csa = arg1;
31303cb34c60Sahrens 	dsl_pool_t *dp = csa->cds->ds_dir->dd_pool;
3131f18faf3fSek 
3132a9b821a0Sck 	ASSERT(csa->cds->ds_reserved == 0);
3133c4cbca4fSChris Kirby 	ASSERT(csa->ohds->ds_quota == 0 ||
3134c4cbca4fSChris Kirby 	    csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota);
3135a9b821a0Sck 
31363cb34c60Sahrens 	dmu_buf_will_dirty(csa->cds->ds_dbuf, tx);
31373cb34c60Sahrens 	dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx);
3138f18faf3fSek 
3139503ad85cSMatthew Ahrens 	if (csa->cds->ds_objset != NULL) {
3140503ad85cSMatthew Ahrens 		dmu_objset_evict(csa->cds->ds_objset);
3141503ad85cSMatthew Ahrens 		csa->cds->ds_objset = NULL;
31423cb34c60Sahrens 	}
3143f18faf3fSek 
3144503ad85cSMatthew Ahrens 	if (csa->ohds->ds_objset != NULL) {
3145503ad85cSMatthew Ahrens 		dmu_objset_evict(csa->ohds->ds_objset);
3146503ad85cSMatthew Ahrens 		csa->ohds->ds_objset = NULL;
31473cb34c60Sahrens 	}
3148f18faf3fSek 
3149ae46e4c7SMatthew Ahrens 	/*
3150ae46e4c7SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
3151ae46e4c7SMatthew Ahrens 	 */
3152ae46e4c7SMatthew Ahrens 	if (csa->cds->ds_prev) {
3153ae46e4c7SMatthew Ahrens 		dsl_dataset_t *origin = csa->cds->ds_prev;
3154cde58dbcSMatthew Ahrens 		uint64_t comp, uncomp;
3155cde58dbcSMatthew Ahrens 
3156ae46e4c7SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
3157cde58dbcSMatthew Ahrens 		dsl_deadlist_space_range(&csa->cds->ds_deadlist,
3158ae46e4c7SMatthew Ahrens 		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
3159cde58dbcSMatthew Ahrens 		    &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
3160ae46e4c7SMatthew Ahrens 	}
3161f18faf3fSek 
3162f18faf3fSek 	/* swap blkptrs */
3163f18faf3fSek 	{
3164f18faf3fSek 		blkptr_t tmp;
31653cb34c60Sahrens 		tmp = csa->ohds->ds_phys->ds_bp;
31663cb34c60Sahrens 		csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp;
31673cb34c60Sahrens 		csa->cds->ds_phys->ds_bp = tmp;
3168f18faf3fSek 	}
3169f18faf3fSek 
3170f18faf3fSek 	/* set dd_*_bytes */
3171f18faf3fSek 	{
3172f18faf3fSek 		int64_t dused, dcomp, duncomp;
3173f18faf3fSek 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
3174f18faf3fSek 		uint64_t odl_used, odl_comp, odl_uncomp;
3175f18faf3fSek 
317674e7dc98SMatthew Ahrens 		ASSERT3U(csa->cds->ds_dir->dd_phys->
317774e7dc98SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
317874e7dc98SMatthew Ahrens 
3179cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&csa->cds->ds_deadlist,
3180cde58dbcSMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
3181cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&csa->ohds->ds_deadlist,
3182cde58dbcSMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
318374e7dc98SMatthew Ahrens 
31843cb34c60Sahrens 		dused = csa->cds->ds_phys->ds_used_bytes + cdl_used -
31853cb34c60Sahrens 		    (csa->ohds->ds_phys->ds_used_bytes + odl_used);
31863cb34c60Sahrens 		dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp -
31873cb34c60Sahrens 		    (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp);
31883cb34c60Sahrens 		duncomp = csa->cds->ds_phys->ds_uncompressed_bytes +
31893cb34c60Sahrens 		    cdl_uncomp -
31903cb34c60Sahrens 		    (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp);
31913cb34c60Sahrens 
319274e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD,
31933cb34c60Sahrens 		    dused, dcomp, duncomp, tx);
319474e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD,
31953cb34c60Sahrens 		    -dused, -dcomp, -duncomp, tx);
319674e7dc98SMatthew Ahrens 
319774e7dc98SMatthew Ahrens 		/*
319874e7dc98SMatthew Ahrens 		 * The difference in the space used by snapshots is the
319974e7dc98SMatthew Ahrens 		 * difference in snapshot space due to the head's
320074e7dc98SMatthew Ahrens 		 * deadlist (since that's the only thing that's
320174e7dc98SMatthew Ahrens 		 * changing that affects the snapused).
320274e7dc98SMatthew Ahrens 		 */
3203cde58dbcSMatthew Ahrens 		dsl_deadlist_space_range(&csa->cds->ds_deadlist,
3204cde58dbcSMatthew Ahrens 		    csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX,
3205cde58dbcSMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
3206cde58dbcSMatthew Ahrens 		dsl_deadlist_space_range(&csa->ohds->ds_deadlist,
3207cde58dbcSMatthew Ahrens 		    csa->ohds->ds_dir->dd_origin_txg, UINT64_MAX,
3208cde58dbcSMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
320974e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used,
321074e7dc98SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
32113cb34c60Sahrens 	}
32123cb34c60Sahrens 
3213f18faf3fSek 	/* swap ds_*_bytes */
32143cb34c60Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_used_bytes,
32153cb34c60Sahrens 	    csa->cds->ds_phys->ds_used_bytes);
32163cb34c60Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes,
32173cb34c60Sahrens 	    csa->cds->ds_phys->ds_compressed_bytes);
32183cb34c60Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes,
32193cb34c60Sahrens 	    csa->cds->ds_phys->ds_uncompressed_bytes);
3220a9b821a0Sck 	SWITCH64(csa->ohds->ds_phys->ds_unique_bytes,
3221a9b821a0Sck 	    csa->cds->ds_phys->ds_unique_bytes);
3222a9b821a0Sck 
3223a9b821a0Sck 	/* apply any parent delta for change in unconsumed refreservation */
322474e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV,
322574e7dc98SMatthew Ahrens 	    csa->unused_refres_delta, 0, 0, tx);
3226f18faf3fSek 
3227cde58dbcSMatthew Ahrens 	/*
3228cde58dbcSMatthew Ahrens 	 * Swap deadlists.
3229cde58dbcSMatthew Ahrens 	 */
3230cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&csa->cds->ds_deadlist);
3231cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&csa->ohds->ds_deadlist);
32323cb34c60Sahrens 	SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj,
32333cb34c60Sahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
3234cde58dbcSMatthew Ahrens 	dsl_deadlist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset,
3235cde58dbcSMatthew Ahrens 	    csa->cds->ds_phys->ds_deadlist_obj);
3236cde58dbcSMatthew Ahrens 	dsl_deadlist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset,
3237cde58dbcSMatthew Ahrens 	    csa->ohds->ds_phys->ds_deadlist_obj);
323888b7b0f2SMatthew Ahrens 
32393f9d6ad7SLin Ling 	dsl_scan_ds_clone_swapped(csa->ohds, csa->cds, tx);
3240f18faf3fSek }
3241f18faf3fSek 
3242f18faf3fSek /*
3243ae46e4c7SMatthew Ahrens  * Swap 'clone' with its origin head datasets.  Used at the end of "zfs
3244ae46e4c7SMatthew Ahrens  * recv" into an existing fs to swizzle the file system to the new
3245ae46e4c7SMatthew Ahrens  * version, and by "zfs rollback".  Can also be used to swap two
3246ae46e4c7SMatthew Ahrens  * independent head datasets if neither has any snapshots.
3247f18faf3fSek  */
3248f18faf3fSek int
32493cb34c60Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
32503cb34c60Sahrens     boolean_t force)
3251f18faf3fSek {
32523cb34c60Sahrens 	struct cloneswaparg csa;
3253745cd3c5Smaybee 	int error;
3254f18faf3fSek 
3255745cd3c5Smaybee 	ASSERT(clone->ds_owner);
3256745cd3c5Smaybee 	ASSERT(origin_head->ds_owner);
3257745cd3c5Smaybee retry:
325877972028SChris Kirby 	/*
325977972028SChris Kirby 	 * Need exclusive access for the swap. If we're swapping these
326077972028SChris Kirby 	 * datasets back after an error, we already hold the locks.
326177972028SChris Kirby 	 */
326277972028SChris Kirby 	if (!RW_WRITE_HELD(&clone->ds_rwlock))
326377972028SChris Kirby 		rw_enter(&clone->ds_rwlock, RW_WRITER);
326477972028SChris Kirby 	if (!RW_WRITE_HELD(&origin_head->ds_rwlock) &&
326577972028SChris Kirby 	    !rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) {
3266745cd3c5Smaybee 		rw_exit(&clone->ds_rwlock);
3267745cd3c5Smaybee 		rw_enter(&origin_head->ds_rwlock, RW_WRITER);
3268745cd3c5Smaybee 		if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) {
3269745cd3c5Smaybee 			rw_exit(&origin_head->ds_rwlock);
3270745cd3c5Smaybee 			goto retry;
3271745cd3c5Smaybee 		}
3272745cd3c5Smaybee 	}
32733cb34c60Sahrens 	csa.cds = clone;
32743cb34c60Sahrens 	csa.ohds = origin_head;
32753cb34c60Sahrens 	csa.force = force;
3276745cd3c5Smaybee 	error = dsl_sync_task_do(clone->ds_dir->dd_pool,
3277f18faf3fSek 	    dsl_dataset_clone_swap_check,
3278745cd3c5Smaybee 	    dsl_dataset_clone_swap_sync, &csa, NULL, 9);
3279745cd3c5Smaybee 	return (error);
3280f18faf3fSek }
3281f18faf3fSek 
3282b1b8ab34Slling /*
3283b1b8ab34Slling  * Given a pool name and a dataset object number in that pool,
3284b1b8ab34Slling  * return the name of that dataset.
3285b1b8ab34Slling  */
3286b1b8ab34Slling int
3287b1b8ab34Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3288b1b8ab34Slling {
3289b1b8ab34Slling 	spa_t *spa;
3290b1b8ab34Slling 	dsl_pool_t *dp;
3291745cd3c5Smaybee 	dsl_dataset_t *ds;
3292b1b8ab34Slling 	int error;
3293b1b8ab34Slling 
3294b1b8ab34Slling 	if ((error = spa_open(pname, &spa, FTAG)) != 0)
3295b1b8ab34Slling 		return (error);
3296b1b8ab34Slling 	dp = spa_get_dsl(spa);
3297b1b8ab34Slling 	rw_enter(&dp->dp_config_rwlock, RW_READER);
3298745cd3c5Smaybee 	if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) {
3299745cd3c5Smaybee 		dsl_dataset_name(ds, buf);
3300745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
3301b1b8ab34Slling 	}
3302b1b8ab34Slling 	rw_exit(&dp->dp_config_rwlock);
3303b1b8ab34Slling 	spa_close(spa, FTAG);
3304b1b8ab34Slling 
3305745cd3c5Smaybee 	return (error);
3306b1b8ab34Slling }
3307a9799022Sck 
3308a9799022Sck int
3309a9799022Sck dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3310745cd3c5Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3311a9799022Sck {
3312a9799022Sck 	int error = 0;
3313a9799022Sck 
3314a9799022Sck 	ASSERT3S(asize, >, 0);
3315a9799022Sck 
33169082849eSck 	/*
33179082849eSck 	 * *ref_rsrv is the portion of asize that will come from any
33189082849eSck 	 * unconsumed refreservation space.
33199082849eSck 	 */
33209082849eSck 	*ref_rsrv = 0;
33219082849eSck 
3322a9799022Sck 	mutex_enter(&ds->ds_lock);
3323a9799022Sck 	/*
3324a9799022Sck 	 * Make a space adjustment for reserved bytes.
3325a9799022Sck 	 */
3326a9799022Sck 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
3327a9799022Sck 		ASSERT3U(*used, >=,
3328a9799022Sck 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
3329a9799022Sck 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
33309082849eSck 		*ref_rsrv =
33319082849eSck 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
3332a9799022Sck 	}
3333a9799022Sck 
3334a9799022Sck 	if (!check_quota || ds->ds_quota == 0) {
3335a9799022Sck 		mutex_exit(&ds->ds_lock);
3336a9799022Sck 		return (0);
3337a9799022Sck 	}
3338a9799022Sck 	/*
3339a9799022Sck 	 * If they are requesting more space, and our current estimate
3340a9799022Sck 	 * is over quota, they get to try again unless the actual
3341a9799022Sck 	 * on-disk is over quota and there are no pending changes (which
3342a9799022Sck 	 * may free up space for us).
3343a9799022Sck 	 */
3344a9799022Sck 	if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) {
3345a9799022Sck 		if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota)
3346a9799022Sck 			error = ERESTART;
3347a9799022Sck 		else
3348a9799022Sck 			error = EDQUOT;
3349a9799022Sck 	}
3350a9799022Sck 	mutex_exit(&ds->ds_lock);
3351a9799022Sck 
3352a9799022Sck 	return (error);
3353a9799022Sck }
3354a9799022Sck 
3355a9799022Sck /* ARGSUSED */
3356a9799022Sck static int
3357a9799022Sck dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx)
3358a9799022Sck {
3359a9799022Sck 	dsl_dataset_t *ds = arg1;
336092241e0bSTom Erickson 	dsl_prop_setarg_t *psa = arg2;
336192241e0bSTom Erickson 	int err;
3362a9799022Sck 
3363a9799022Sck 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA)
3364a9799022Sck 		return (ENOTSUP);
3365a9799022Sck 
336692241e0bSTom Erickson 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
336792241e0bSTom Erickson 		return (err);
336892241e0bSTom Erickson 
336992241e0bSTom Erickson 	if (psa->psa_effective_value == 0)
3370a9799022Sck 		return (0);
3371a9799022Sck 
337292241e0bSTom Erickson 	if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes ||
337392241e0bSTom Erickson 	    psa->psa_effective_value < ds->ds_reserved)
3374a9799022Sck 		return (ENOSPC);
3375a9799022Sck 
3376a9799022Sck 	return (0);
3377a9799022Sck }
3378a9799022Sck 
33793f9d6ad7SLin Ling extern void dsl_prop_set_sync(void *, void *, dmu_tx_t *);
338092241e0bSTom Erickson 
3381a9799022Sck void
33823f9d6ad7SLin Ling dsl_dataset_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3383a9799022Sck {
3384a9799022Sck 	dsl_dataset_t *ds = arg1;
338592241e0bSTom Erickson 	dsl_prop_setarg_t *psa = arg2;
338692241e0bSTom Erickson 	uint64_t effective_value = psa->psa_effective_value;
3387a9799022Sck 
33883f9d6ad7SLin Ling 	dsl_prop_set_sync(ds, psa, tx);
338992241e0bSTom Erickson 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
3390a9799022Sck 
339192241e0bSTom Erickson 	if (ds->ds_quota != effective_value) {
339292241e0bSTom Erickson 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
339392241e0bSTom Erickson 		ds->ds_quota = effective_value;
3394a9799022Sck 
33953f9d6ad7SLin Ling 		spa_history_log_internal(LOG_DS_REFQUOTA,
33963f9d6ad7SLin Ling 		    ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu ",
339792241e0bSTom Erickson 		    (longlong_t)ds->ds_quota, ds->ds_object);
339892241e0bSTom Erickson 	}
3399a9799022Sck }
3400a9799022Sck 
3401a9799022Sck int
340292241e0bSTom Erickson dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota)
3403a9799022Sck {
3404a9799022Sck 	dsl_dataset_t *ds;
340592241e0bSTom Erickson 	dsl_prop_setarg_t psa;
3406a9799022Sck 	int err;
3407a9799022Sck 
340892241e0bSTom Erickson 	dsl_prop_setarg_init_uint64(&psa, "refquota", source, &quota);
340992241e0bSTom Erickson 
3410745cd3c5Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
3411a9799022Sck 	if (err)
3412a9799022Sck 		return (err);
3413a9799022Sck 
341492241e0bSTom Erickson 	/*
341592241e0bSTom Erickson 	 * If someone removes a file, then tries to set the quota, we
341692241e0bSTom Erickson 	 * want to make sure the file freeing takes effect.
341792241e0bSTom Erickson 	 */
341892241e0bSTom Erickson 	txg_wait_open(ds->ds_dir->dd_pool, 0);
341992241e0bSTom Erickson 
342092241e0bSTom Erickson 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
342192241e0bSTom Erickson 	    dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync,
342292241e0bSTom Erickson 	    ds, &psa, 0);
3423a9799022Sck 
3424745cd3c5Smaybee 	dsl_dataset_rele(ds, FTAG);
3425a9799022Sck 	return (err);
3426a9799022Sck }
3427a9799022Sck 
3428a9799022Sck static int
3429a9799022Sck dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx)
3430a9799022Sck {
3431a9799022Sck 	dsl_dataset_t *ds = arg1;
343292241e0bSTom Erickson 	dsl_prop_setarg_t *psa = arg2;
343392241e0bSTom Erickson 	uint64_t effective_value;
3434a9799022Sck 	uint64_t unique;
343592241e0bSTom Erickson 	int err;
3436a9799022Sck 
3437a9799022Sck 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
3438a9799022Sck 	    SPA_VERSION_REFRESERVATION)
3439a9799022Sck 		return (ENOTSUP);
3440a9799022Sck 
3441a9799022Sck 	if (dsl_dataset_is_snapshot(ds))
3442a9799022Sck 		return (EINVAL);
3443a9799022Sck 
344492241e0bSTom Erickson 	if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0)
344592241e0bSTom Erickson 		return (err);
344692241e0bSTom Erickson 
344792241e0bSTom Erickson 	effective_value = psa->psa_effective_value;
344892241e0bSTom Erickson 
3449a9799022Sck 	/*
3450a9799022Sck 	 * If we are doing the preliminary check in open context, the
3451a9799022Sck 	 * space estimates may be inaccurate.
3452a9799022Sck 	 */
3453a9799022Sck 	if (!dmu_tx_is_syncing(tx))
3454a9799022Sck 		return (0);
3455a9799022Sck 
3456a9799022Sck 	mutex_enter(&ds->ds_lock);
34573f9d6ad7SLin Ling 	if (!DS_UNIQUE_IS_ACCURATE(ds))
34583f9d6ad7SLin Ling 		dsl_dataset_recalc_head_uniq(ds);
34593f9d6ad7SLin Ling 	unique = ds->ds_phys->ds_unique_bytes;
3460a9799022Sck 	mutex_exit(&ds->ds_lock);
3461a9799022Sck 
346292241e0bSTom Erickson 	if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) {
346392241e0bSTom Erickson 		uint64_t delta = MAX(unique, effective_value) -
3464379c004dSEric Schrock 		    MAX(unique, ds->ds_reserved);
3465379c004dSEric Schrock 
3466379c004dSEric Schrock 		if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
3467379c004dSEric Schrock 			return (ENOSPC);
3468379c004dSEric Schrock 		if (ds->ds_quota > 0 &&
346992241e0bSTom Erickson 		    effective_value > ds->ds_quota)
3470379c004dSEric Schrock 			return (ENOSPC);
3471379c004dSEric Schrock 	}
3472a9799022Sck 
3473a9799022Sck 	return (0);
3474a9799022Sck }
3475a9799022Sck 
3476a9799022Sck static void
34773f9d6ad7SLin Ling dsl_dataset_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3478a9799022Sck {
3479a9799022Sck 	dsl_dataset_t *ds = arg1;
348092241e0bSTom Erickson 	dsl_prop_setarg_t *psa = arg2;
348192241e0bSTom Erickson 	uint64_t effective_value = psa->psa_effective_value;
348202c8f3f0SMatthew Ahrens 	uint64_t unique;
348302c8f3f0SMatthew Ahrens 	int64_t delta;
348402c8f3f0SMatthew Ahrens 
34853f9d6ad7SLin Ling 	dsl_prop_set_sync(ds, psa, tx);
348692241e0bSTom Erickson 	DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa);
348792241e0bSTom Erickson 
348802c8f3f0SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
348902c8f3f0SMatthew Ahrens 
349002c8f3f0SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
349102c8f3f0SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
34923f9d6ad7SLin Ling 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
34933f9d6ad7SLin Ling 	unique = ds->ds_phys->ds_unique_bytes;
349492241e0bSTom Erickson 	delta = MAX(0, (int64_t)(effective_value - unique)) -
349502c8f3f0SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
349692241e0bSTom Erickson 	ds->ds_reserved = effective_value;
349702c8f3f0SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
349802c8f3f0SMatthew Ahrens 
349902c8f3f0SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
350002c8f3f0SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
3501a9799022Sck 
35023f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_REFRESERV,
35033f9d6ad7SLin Ling 	    ds->ds_dir->dd_pool->dp_spa, tx, "%lld dataset = %llu",
350492241e0bSTom Erickson 	    (longlong_t)effective_value, ds->ds_object);
3505a9799022Sck }
3506a9799022Sck 
3507a9799022Sck int
350892241e0bSTom Erickson dsl_dataset_set_reservation(const char *dsname, zprop_source_t source,
350992241e0bSTom Erickson     uint64_t reservation)
3510a9799022Sck {
3511a9799022Sck 	dsl_dataset_t *ds;
351292241e0bSTom Erickson 	dsl_prop_setarg_t psa;
3513a9799022Sck 	int err;
3514a9799022Sck 
351592241e0bSTom Erickson 	dsl_prop_setarg_init_uint64(&psa, "refreservation", source,
351692241e0bSTom Erickson 	    &reservation);
351792241e0bSTom Erickson 
3518745cd3c5Smaybee 	err = dsl_dataset_hold(dsname, FTAG, &ds);
3519a9799022Sck 	if (err)
3520a9799022Sck 		return (err);
3521a9799022Sck 
3522a9799022Sck 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
3523a9799022Sck 	    dsl_dataset_set_reservation_check,
352492241e0bSTom Erickson 	    dsl_dataset_set_reservation_sync, ds, &psa, 0);
352592241e0bSTom Erickson 
3526745cd3c5Smaybee 	dsl_dataset_rele(ds, FTAG);
3527a9799022Sck 	return (err);
3528a9799022Sck }
3529842727c2SChris Kirby 
3530c99e4bdcSChris Kirby typedef struct zfs_hold_cleanup_arg {
3531a7f53a56SChris Kirby 	dsl_pool_t *dp;
3532a7f53a56SChris Kirby 	uint64_t dsobj;
3533c99e4bdcSChris Kirby 	char htag[MAXNAMELEN];
3534c99e4bdcSChris Kirby } zfs_hold_cleanup_arg_t;
3535c99e4bdcSChris Kirby 
3536c99e4bdcSChris Kirby static void
3537c99e4bdcSChris Kirby dsl_dataset_user_release_onexit(void *arg)
3538c99e4bdcSChris Kirby {
3539c99e4bdcSChris Kirby 	zfs_hold_cleanup_arg_t *ca = arg;
3540c99e4bdcSChris Kirby 
3541a7f53a56SChris Kirby 	(void) dsl_dataset_user_release_tmp(ca->dp, ca->dsobj, ca->htag,
3542a7f53a56SChris Kirby 	    B_TRUE);
3543c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t));
3544c99e4bdcSChris Kirby }
3545c99e4bdcSChris Kirby 
3546a7f53a56SChris Kirby void
3547a7f53a56SChris Kirby dsl_register_onexit_hold_cleanup(dsl_dataset_t *ds, const char *htag,
3548a7f53a56SChris Kirby     minor_t minor)
3549a7f53a56SChris Kirby {
3550a7f53a56SChris Kirby 	zfs_hold_cleanup_arg_t *ca;
3551a7f53a56SChris Kirby 
3552a7f53a56SChris Kirby 	ca = kmem_alloc(sizeof (zfs_hold_cleanup_arg_t), KM_SLEEP);
3553a7f53a56SChris Kirby 	ca->dp = ds->ds_dir->dd_pool;
3554a7f53a56SChris Kirby 	ca->dsobj = ds->ds_object;
3555a7f53a56SChris Kirby 	(void) strlcpy(ca->htag, htag, sizeof (ca->htag));
3556a7f53a56SChris Kirby 	VERIFY3U(0, ==, zfs_onexit_add_cb(minor,
3557a7f53a56SChris Kirby 	    dsl_dataset_user_release_onexit, ca, NULL));
3558a7f53a56SChris Kirby }
3559a7f53a56SChris Kirby 
356015508ac0SChris Kirby /*
356199d5e173STim Haley  * If you add new checks here, you may need to add
356299d5e173STim Haley  * additional checks to the "temporary" case in
356399d5e173STim Haley  * snapshot_check() in dmu_objset.c.
356415508ac0SChris Kirby  */
3565842727c2SChris Kirby static int
3566842727c2SChris Kirby dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx)
3567842727c2SChris Kirby {
3568842727c2SChris Kirby 	dsl_dataset_t *ds = arg1;
3569ca45db41SChris Kirby 	struct dsl_ds_holdarg *ha = arg2;
3570ca45db41SChris Kirby 	char *htag = ha->htag;
3571842727c2SChris Kirby 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3572842727c2SChris Kirby 	int error = 0;
3573842727c2SChris Kirby 
3574842727c2SChris Kirby 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3575842727c2SChris Kirby 		return (ENOTSUP);
3576842727c2SChris Kirby 
3577842727c2SChris Kirby 	if (!dsl_dataset_is_snapshot(ds))
3578842727c2SChris Kirby 		return (EINVAL);
3579842727c2SChris Kirby 
3580842727c2SChris Kirby 	/* tags must be unique */
3581842727c2SChris Kirby 	mutex_enter(&ds->ds_lock);
3582842727c2SChris Kirby 	if (ds->ds_phys->ds_userrefs_obj) {
3583842727c2SChris Kirby 		error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag,
3584842727c2SChris Kirby 		    8, 1, tx);
3585842727c2SChris Kirby 		if (error == 0)
3586842727c2SChris Kirby 			error = EEXIST;
3587842727c2SChris Kirby 		else if (error == ENOENT)
3588842727c2SChris Kirby 			error = 0;
3589842727c2SChris Kirby 	}
3590842727c2SChris Kirby 	mutex_exit(&ds->ds_lock);
3591842727c2SChris Kirby 
359215508ac0SChris Kirby 	if (error == 0 && ha->temphold &&
359315508ac0SChris Kirby 	    strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
359415508ac0SChris Kirby 		error = E2BIG;
359515508ac0SChris Kirby 
3596842727c2SChris Kirby 	return (error);
3597842727c2SChris Kirby }
3598842727c2SChris Kirby 
359999d5e173STim Haley void
36003f9d6ad7SLin Ling dsl_dataset_user_hold_sync(void *arg1, void *arg2, dmu_tx_t *tx)
3601842727c2SChris Kirby {
3602842727c2SChris Kirby 	dsl_dataset_t *ds = arg1;
3603ca45db41SChris Kirby 	struct dsl_ds_holdarg *ha = arg2;
3604ca45db41SChris Kirby 	char *htag = ha->htag;
3605ca45db41SChris Kirby 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
3606ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
360715508ac0SChris Kirby 	uint64_t now = gethrestime_sec();
3608842727c2SChris Kirby 	uint64_t zapobj;
3609842727c2SChris Kirby 
3610842727c2SChris Kirby 	mutex_enter(&ds->ds_lock);
3611842727c2SChris Kirby 	if (ds->ds_phys->ds_userrefs_obj == 0) {
3612842727c2SChris Kirby 		/*
3613842727c2SChris Kirby 		 * This is the first user hold for this dataset.  Create
3614842727c2SChris Kirby 		 * the userrefs zap object.
3615842727c2SChris Kirby 		 */
3616842727c2SChris Kirby 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
3617842727c2SChris Kirby 		zapobj = ds->ds_phys->ds_userrefs_obj =
3618842727c2SChris Kirby 		    zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx);
3619842727c2SChris Kirby 	} else {
3620842727c2SChris Kirby 		zapobj = ds->ds_phys->ds_userrefs_obj;
3621842727c2SChris Kirby 	}
3622842727c2SChris Kirby 	ds->ds_userrefs++;
3623842727c2SChris Kirby 	mutex_exit(&ds->ds_lock);
3624842727c2SChris Kirby 
3625842727c2SChris Kirby 	VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx));
3626842727c2SChris Kirby 
3627ca45db41SChris Kirby 	if (ha->temphold) {
3628ca45db41SChris Kirby 		VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object,
3629ca45db41SChris Kirby 		    htag, &now, tx));
3630ca45db41SChris Kirby 	}
3631ca45db41SChris Kirby 
36323f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_USER_HOLD,
36333f9d6ad7SLin Ling 	    dp->dp_spa, tx, "<%s> temp = %d dataset = %llu", htag,
3634ca45db41SChris Kirby 	    (int)ha->temphold, ds->ds_object);
3635842727c2SChris Kirby }
3636842727c2SChris Kirby 
3637842727c2SChris Kirby static int
3638fd136879SMatthew Ahrens dsl_dataset_user_hold_one(const char *dsname, void *arg)
3639842727c2SChris Kirby {
3640842727c2SChris Kirby 	struct dsl_ds_holdarg *ha = arg;
3641842727c2SChris Kirby 	dsl_dataset_t *ds;
3642842727c2SChris Kirby 	int error;
3643842727c2SChris Kirby 	char *name;
3644842727c2SChris Kirby 
3645842727c2SChris Kirby 	/* alloc a buffer to hold dsname@snapname plus terminating NULL */
3646ae46e4c7SMatthew Ahrens 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3647842727c2SChris Kirby 	error = dsl_dataset_hold(name, ha->dstg, &ds);
3648ae46e4c7SMatthew Ahrens 	strfree(name);
3649842727c2SChris Kirby 	if (error == 0) {
3650d7747cbcSChris Kirby 		ha->gotone = B_TRUE;
3651842727c2SChris Kirby 		dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check,
3652ca45db41SChris Kirby 		    dsl_dataset_user_hold_sync, ds, ha, 0);
3653842727c2SChris Kirby 	} else if (error == ENOENT && ha->recursive) {
3654842727c2SChris Kirby 		error = 0;
3655842727c2SChris Kirby 	} else {
3656fd136879SMatthew Ahrens 		(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3657842727c2SChris Kirby 	}
3658842727c2SChris Kirby 	return (error);
3659842727c2SChris Kirby }
3660842727c2SChris Kirby 
3661a7f53a56SChris Kirby int
3662a7f53a56SChris Kirby dsl_dataset_user_hold_for_send(dsl_dataset_t *ds, char *htag,
3663a7f53a56SChris Kirby     boolean_t temphold)
3664a7f53a56SChris Kirby {
3665a7f53a56SChris Kirby 	struct dsl_ds_holdarg *ha;
3666a7f53a56SChris Kirby 	int error;
3667a7f53a56SChris Kirby 
3668a7f53a56SChris Kirby 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3669a7f53a56SChris Kirby 	ha->htag = htag;
3670a7f53a56SChris Kirby 	ha->temphold = temphold;
3671a7f53a56SChris Kirby 	error = dsl_sync_task_do(ds->ds_dir->dd_pool,
3672a7f53a56SChris Kirby 	    dsl_dataset_user_hold_check, dsl_dataset_user_hold_sync,
3673a7f53a56SChris Kirby 	    ds, ha, 0);
3674a7f53a56SChris Kirby 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3675a7f53a56SChris Kirby 
3676a7f53a56SChris Kirby 	return (error);
3677a7f53a56SChris Kirby }
3678a7f53a56SChris Kirby 
3679842727c2SChris Kirby int
3680842727c2SChris Kirby dsl_dataset_user_hold(char *dsname, char *snapname, char *htag,
3681c99e4bdcSChris Kirby     boolean_t recursive, boolean_t temphold, int cleanup_fd)
3682842727c2SChris Kirby {
3683842727c2SChris Kirby 	struct dsl_ds_holdarg *ha;
3684842727c2SChris Kirby 	dsl_sync_task_t *dst;
3685842727c2SChris Kirby 	spa_t *spa;
3686842727c2SChris Kirby 	int error;
3687a7f53a56SChris Kirby 	minor_t minor = 0;
3688a7f53a56SChris Kirby 
3689a7f53a56SChris Kirby 	if (cleanup_fd != -1) {
3690a7f53a56SChris Kirby 		/* Currently we only support cleanup-on-exit of tempholds. */
3691a7f53a56SChris Kirby 		if (!temphold)
3692a7f53a56SChris Kirby 			return (EINVAL);
3693a7f53a56SChris Kirby 		error = zfs_onexit_fd_hold(cleanup_fd, &minor);
3694a7f53a56SChris Kirby 		if (error)
3695a7f53a56SChris Kirby 			return (error);
3696a7f53a56SChris Kirby 	}
3697842727c2SChris Kirby 
3698842727c2SChris Kirby 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3699842727c2SChris Kirby 
3700842727c2SChris Kirby 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3701842727c2SChris Kirby 
3702842727c2SChris Kirby 	error = spa_open(dsname, &spa, FTAG);
3703842727c2SChris Kirby 	if (error) {
3704842727c2SChris Kirby 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3705a7f53a56SChris Kirby 		if (cleanup_fd != -1)
3706a7f53a56SChris Kirby 			zfs_onexit_fd_rele(cleanup_fd);
3707842727c2SChris Kirby 		return (error);
3708842727c2SChris Kirby 	}
3709842727c2SChris Kirby 
3710842727c2SChris Kirby 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
3711842727c2SChris Kirby 	ha->htag = htag;
3712842727c2SChris Kirby 	ha->snapname = snapname;
3713842727c2SChris Kirby 	ha->recursive = recursive;
3714ca45db41SChris Kirby 	ha->temphold = temphold;
3715c99e4bdcSChris Kirby 
3716842727c2SChris Kirby 	if (recursive) {
3717842727c2SChris Kirby 		error = dmu_objset_find(dsname, dsl_dataset_user_hold_one,
3718842727c2SChris Kirby 		    ha, DS_FIND_CHILDREN);
3719842727c2SChris Kirby 	} else {
3720842727c2SChris Kirby 		error = dsl_dataset_user_hold_one(dsname, ha);
3721842727c2SChris Kirby 	}
3722842727c2SChris Kirby 	if (error == 0)
3723842727c2SChris Kirby 		error = dsl_sync_task_group_wait(ha->dstg);
3724842727c2SChris Kirby 
3725842727c2SChris Kirby 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
3726842727c2SChris Kirby 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
3727842727c2SChris Kirby 		dsl_dataset_t *ds = dst->dst_arg1;
3728842727c2SChris Kirby 
3729842727c2SChris Kirby 		if (dst->dst_err) {
3730842727c2SChris Kirby 			dsl_dataset_name(ds, ha->failed);
3731842727c2SChris Kirby 			*strchr(ha->failed, '@') = '\0';
3732a7f53a56SChris Kirby 		} else if (error == 0 && minor != 0 && temphold) {
3733a7f53a56SChris Kirby 			/*
3734a7f53a56SChris Kirby 			 * If this hold is to be released upon process exit,
3735a7f53a56SChris Kirby 			 * register that action now.
3736a7f53a56SChris Kirby 			 */
3737a7f53a56SChris Kirby 			dsl_register_onexit_hold_cleanup(ds, htag, minor);
3738842727c2SChris Kirby 		}
3739842727c2SChris Kirby 		dsl_dataset_rele(ds, ha->dstg);
3740842727c2SChris Kirby 	}
3741842727c2SChris Kirby 
3742d7747cbcSChris Kirby 	if (error == 0 && recursive && !ha->gotone)
3743d7747cbcSChris Kirby 		error = ENOENT;
3744d7747cbcSChris Kirby 
3745842727c2SChris Kirby 	if (error)
3746fd136879SMatthew Ahrens 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
3747842727c2SChris Kirby 
3748842727c2SChris Kirby 	dsl_sync_task_group_destroy(ha->dstg);
3749c99e4bdcSChris Kirby 
3750842727c2SChris Kirby 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3751842727c2SChris Kirby 	spa_close(spa, FTAG);
3752a7f53a56SChris Kirby 	if (cleanup_fd != -1)
3753a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
3754842727c2SChris Kirby 	return (error);
3755842727c2SChris Kirby }
3756842727c2SChris Kirby 
3757842727c2SChris Kirby struct dsl_ds_releasearg {
3758842727c2SChris Kirby 	dsl_dataset_t *ds;
3759842727c2SChris Kirby 	const char *htag;
3760842727c2SChris Kirby 	boolean_t own;		/* do we own or just hold ds? */
3761842727c2SChris Kirby };
3762842727c2SChris Kirby 
3763842727c2SChris Kirby static int
3764842727c2SChris Kirby dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag,
3765842727c2SChris Kirby     boolean_t *might_destroy)
3766842727c2SChris Kirby {
3767842727c2SChris Kirby 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3768842727c2SChris Kirby 	uint64_t zapobj;
3769842727c2SChris Kirby 	uint64_t tmp;
3770842727c2SChris Kirby 	int error;
3771842727c2SChris Kirby 
3772842727c2SChris Kirby 	*might_destroy = B_FALSE;
3773842727c2SChris Kirby 
3774842727c2SChris Kirby 	mutex_enter(&ds->ds_lock);
3775842727c2SChris Kirby 	zapobj = ds->ds_phys->ds_userrefs_obj;
3776842727c2SChris Kirby 	if (zapobj == 0) {
3777842727c2SChris Kirby 		/* The tag can't possibly exist */
3778842727c2SChris Kirby 		mutex_exit(&ds->ds_lock);
3779842727c2SChris Kirby 		return (ESRCH);
3780842727c2SChris Kirby 	}
3781842727c2SChris Kirby 
3782842727c2SChris Kirby 	/* Make sure the tag exists */
3783842727c2SChris Kirby 	error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp);
3784842727c2SChris Kirby 	if (error) {
3785842727c2SChris Kirby 		mutex_exit(&ds->ds_lock);
3786842727c2SChris Kirby 		if (error == ENOENT)
3787842727c2SChris Kirby 			error = ESRCH;
3788842727c2SChris Kirby 		return (error);
3789842727c2SChris Kirby 	}
3790842727c2SChris Kirby 
3791842727c2SChris Kirby 	if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 &&
3792842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds))
3793842727c2SChris Kirby 		*might_destroy = B_TRUE;
3794842727c2SChris Kirby 
3795842727c2SChris Kirby 	mutex_exit(&ds->ds_lock);
3796842727c2SChris Kirby 	return (0);
3797842727c2SChris Kirby }
3798842727c2SChris Kirby 
3799842727c2SChris Kirby static int
3800842727c2SChris Kirby dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx)
3801842727c2SChris Kirby {
3802842727c2SChris Kirby 	struct dsl_ds_releasearg *ra = arg1;
3803842727c2SChris Kirby 	dsl_dataset_t *ds = ra->ds;
3804842727c2SChris Kirby 	boolean_t might_destroy;
3805842727c2SChris Kirby 	int error;
3806842727c2SChris Kirby 
3807842727c2SChris Kirby 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS)
3808842727c2SChris Kirby 		return (ENOTSUP);
3809842727c2SChris Kirby 
3810842727c2SChris Kirby 	error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy);
3811842727c2SChris Kirby 	if (error)
3812842727c2SChris Kirby 		return (error);
3813842727c2SChris Kirby 
3814842727c2SChris Kirby 	if (might_destroy) {
3815842727c2SChris Kirby 		struct dsl_ds_destroyarg dsda = {0};
3816842727c2SChris Kirby 
3817842727c2SChris Kirby 		if (dmu_tx_is_syncing(tx)) {
3818842727c2SChris Kirby 			/*
3819842727c2SChris Kirby 			 * If we're not prepared to remove the snapshot,
3820842727c2SChris Kirby 			 * we can't allow the release to happen right now.
3821842727c2SChris Kirby 			 */
3822842727c2SChris Kirby 			if (!ra->own)
3823842727c2SChris Kirby 				return (EBUSY);
3824842727c2SChris Kirby 		}
3825842727c2SChris Kirby 		dsda.ds = ds;
3826842727c2SChris Kirby 		dsda.releasing = B_TRUE;
3827842727c2SChris Kirby 		return (dsl_dataset_destroy_check(&dsda, tag, tx));
3828842727c2SChris Kirby 	}
3829842727c2SChris Kirby 
3830842727c2SChris Kirby 	return (0);
3831842727c2SChris Kirby }
3832842727c2SChris Kirby 
3833842727c2SChris Kirby static void
38343f9d6ad7SLin Ling dsl_dataset_user_release_sync(void *arg1, void *tag, dmu_tx_t *tx)
3835842727c2SChris Kirby {
3836842727c2SChris Kirby 	struct dsl_ds_releasearg *ra = arg1;
3837842727c2SChris Kirby 	dsl_dataset_t *ds = ra->ds;
3838ca45db41SChris Kirby 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
3839ca45db41SChris Kirby 	objset_t *mos = dp->dp_meta_objset;
3840842727c2SChris Kirby 	uint64_t zapobj;
3841842727c2SChris Kirby 	uint64_t dsobj = ds->ds_object;
3842842727c2SChris Kirby 	uint64_t refs;
3843ca45db41SChris Kirby 	int error;
3844842727c2SChris Kirby 
3845842727c2SChris Kirby 	mutex_enter(&ds->ds_lock);
3846842727c2SChris Kirby 	ds->ds_userrefs--;
3847842727c2SChris Kirby 	refs = ds->ds_userrefs;
3848842727c2SChris Kirby 	mutex_exit(&ds->ds_lock);
3849ca45db41SChris Kirby 	error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx);
3850ca45db41SChris Kirby 	VERIFY(error == 0 || error == ENOENT);
3851842727c2SChris Kirby 	zapobj = ds->ds_phys->ds_userrefs_obj;
3852842727c2SChris Kirby 	VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx));
3853842727c2SChris Kirby 	if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 &&
3854842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds)) {
3855842727c2SChris Kirby 		struct dsl_ds_destroyarg dsda = {0};
3856842727c2SChris Kirby 
3857842727c2SChris Kirby 		ASSERT(ra->own);
3858842727c2SChris Kirby 		dsda.ds = ds;
3859842727c2SChris Kirby 		dsda.releasing = B_TRUE;
3860842727c2SChris Kirby 		/* We already did the destroy_check */
38613f9d6ad7SLin Ling 		dsl_dataset_destroy_sync(&dsda, tag, tx);
3862842727c2SChris Kirby 	}
3863842727c2SChris Kirby 
38643f9d6ad7SLin Ling 	spa_history_log_internal(LOG_DS_USER_RELEASE,
38653f9d6ad7SLin Ling 	    dp->dp_spa, tx, "<%s> %lld dataset = %llu",
3866842727c2SChris Kirby 	    ra->htag, (longlong_t)refs, dsobj);
3867842727c2SChris Kirby }
3868842727c2SChris Kirby 
3869842727c2SChris Kirby static int
3870fd136879SMatthew Ahrens dsl_dataset_user_release_one(const char *dsname, void *arg)
3871842727c2SChris Kirby {
3872842727c2SChris Kirby 	struct dsl_ds_holdarg *ha = arg;
3873842727c2SChris Kirby 	struct dsl_ds_releasearg *ra;
3874842727c2SChris Kirby 	dsl_dataset_t *ds;
3875842727c2SChris Kirby 	int error;
3876842727c2SChris Kirby 	void *dtag = ha->dstg;
3877842727c2SChris Kirby 	char *name;
3878842727c2SChris Kirby 	boolean_t own = B_FALSE;
3879842727c2SChris Kirby 	boolean_t might_destroy;
3880842727c2SChris Kirby 
3881842727c2SChris Kirby 	/* alloc a buffer to hold dsname@snapname, plus the terminating NULL */
3882ae46e4c7SMatthew Ahrens 	name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3883842727c2SChris Kirby 	error = dsl_dataset_hold(name, dtag, &ds);
3884ae46e4c7SMatthew Ahrens 	strfree(name);
3885842727c2SChris Kirby 	if (error == ENOENT && ha->recursive)
3886842727c2SChris Kirby 		return (0);
3887fd136879SMatthew Ahrens 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3888842727c2SChris Kirby 	if (error)
3889842727c2SChris Kirby 		return (error);
3890842727c2SChris Kirby 
3891d7747cbcSChris Kirby 	ha->gotone = B_TRUE;
3892d7747cbcSChris Kirby 
3893842727c2SChris Kirby 	ASSERT(dsl_dataset_is_snapshot(ds));
3894842727c2SChris Kirby 
3895842727c2SChris Kirby 	error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy);
3896842727c2SChris Kirby 	if (error) {
3897842727c2SChris Kirby 		dsl_dataset_rele(ds, dtag);
3898842727c2SChris Kirby 		return (error);
3899842727c2SChris Kirby 	}
3900842727c2SChris Kirby 
3901842727c2SChris Kirby 	if (might_destroy) {
3902842727c2SChris Kirby #ifdef _KERNEL
39035afc78aaSChris Kirby 		name = kmem_asprintf("%s@%s", dsname, ha->snapname);
3904842727c2SChris Kirby 		error = zfs_unmount_snap(name, NULL);
39055afc78aaSChris Kirby 		strfree(name);
3906842727c2SChris Kirby 		if (error) {
3907842727c2SChris Kirby 			dsl_dataset_rele(ds, dtag);
3908842727c2SChris Kirby 			return (error);
3909842727c2SChris Kirby 		}
3910842727c2SChris Kirby #endif
3911503ad85cSMatthew Ahrens 		if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) {
3912842727c2SChris Kirby 			dsl_dataset_rele(ds, dtag);
3913842727c2SChris Kirby 			return (EBUSY);
3914842727c2SChris Kirby 		} else {
3915842727c2SChris Kirby 			own = B_TRUE;
3916842727c2SChris Kirby 			dsl_dataset_make_exclusive(ds, dtag);
3917842727c2SChris Kirby 		}
3918842727c2SChris Kirby 	}
3919842727c2SChris Kirby 
3920842727c2SChris Kirby 	ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP);
3921842727c2SChris Kirby 	ra->ds = ds;
3922842727c2SChris Kirby 	ra->htag = ha->htag;
3923842727c2SChris Kirby 	ra->own = own;
3924842727c2SChris Kirby 	dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check,
3925842727c2SChris Kirby 	    dsl_dataset_user_release_sync, ra, dtag, 0);
3926842727c2SChris Kirby 
3927842727c2SChris Kirby 	return (0);
3928842727c2SChris Kirby }
3929842727c2SChris Kirby 
3930842727c2SChris Kirby int
3931842727c2SChris Kirby dsl_dataset_user_release(char *dsname, char *snapname, char *htag,
3932842727c2SChris Kirby     boolean_t recursive)
3933842727c2SChris Kirby {
3934842727c2SChris Kirby 	struct dsl_ds_holdarg *ha;
3935842727c2SChris Kirby 	dsl_sync_task_t *dst;
3936842727c2SChris Kirby 	spa_t *spa;
3937842727c2SChris Kirby 	int error;
3938842727c2SChris Kirby 
3939620252bcSChris Kirby top:
3940842727c2SChris Kirby 	ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP);
3941842727c2SChris Kirby 
3942842727c2SChris Kirby 	(void) strlcpy(ha->failed, dsname, sizeof (ha->failed));
3943842727c2SChris Kirby 
3944842727c2SChris Kirby 	error = spa_open(dsname, &spa, FTAG);
3945842727c2SChris Kirby 	if (error) {
3946842727c2SChris Kirby 		kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3947842727c2SChris Kirby 		return (error);
3948842727c2SChris Kirby 	}
3949842727c2SChris Kirby 
3950842727c2SChris Kirby 	ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
3951842727c2SChris Kirby 	ha->htag = htag;
3952842727c2SChris Kirby 	ha->snapname = snapname;
3953842727c2SChris Kirby 	ha->recursive = recursive;
3954842727c2SChris Kirby 	if (recursive) {
3955842727c2SChris Kirby 		error = dmu_objset_find(dsname, dsl_dataset_user_release_one,
3956842727c2SChris Kirby 		    ha, DS_FIND_CHILDREN);
3957842727c2SChris Kirby 	} else {
3958842727c2SChris Kirby 		error = dsl_dataset_user_release_one(dsname, ha);
3959842727c2SChris Kirby 	}
3960842727c2SChris Kirby 	if (error == 0)
3961842727c2SChris Kirby 		error = dsl_sync_task_group_wait(ha->dstg);
3962842727c2SChris Kirby 
3963842727c2SChris Kirby 	for (dst = list_head(&ha->dstg->dstg_tasks); dst;
3964842727c2SChris Kirby 	    dst = list_next(&ha->dstg->dstg_tasks, dst)) {
3965842727c2SChris Kirby 		struct dsl_ds_releasearg *ra = dst->dst_arg1;
3966842727c2SChris Kirby 		dsl_dataset_t *ds = ra->ds;
3967842727c2SChris Kirby 
3968842727c2SChris Kirby 		if (dst->dst_err)
3969842727c2SChris Kirby 			dsl_dataset_name(ds, ha->failed);
3970842727c2SChris Kirby 
3971842727c2SChris Kirby 		if (ra->own)
3972842727c2SChris Kirby 			dsl_dataset_disown(ds, ha->dstg);
3973842727c2SChris Kirby 		else
3974842727c2SChris Kirby 			dsl_dataset_rele(ds, ha->dstg);
3975842727c2SChris Kirby 
3976842727c2SChris Kirby 		kmem_free(ra, sizeof (struct dsl_ds_releasearg));
3977842727c2SChris Kirby 	}
3978842727c2SChris Kirby 
3979d7747cbcSChris Kirby 	if (error == 0 && recursive && !ha->gotone)
3980d7747cbcSChris Kirby 		error = ENOENT;
3981d7747cbcSChris Kirby 
3982620252bcSChris Kirby 	if (error && error != EBUSY)
3983fd136879SMatthew Ahrens 		(void) strlcpy(dsname, ha->failed, sizeof (ha->failed));
3984842727c2SChris Kirby 
3985842727c2SChris Kirby 	dsl_sync_task_group_destroy(ha->dstg);
3986842727c2SChris Kirby 	kmem_free(ha, sizeof (struct dsl_ds_holdarg));
3987842727c2SChris Kirby 	spa_close(spa, FTAG);
3988620252bcSChris Kirby 
3989620252bcSChris Kirby 	/*
3990620252bcSChris Kirby 	 * We can get EBUSY if we were racing with deferred destroy and
3991620252bcSChris Kirby 	 * dsl_dataset_user_release_check() hadn't done the necessary
3992620252bcSChris Kirby 	 * open context setup.  We can also get EBUSY if we're racing
3993620252bcSChris Kirby 	 * with destroy and that thread is the ds_owner.  Either way
3994620252bcSChris Kirby 	 * the busy condition should be transient, and we should retry
3995620252bcSChris Kirby 	 * the release operation.
3996620252bcSChris Kirby 	 */
3997620252bcSChris Kirby 	if (error == EBUSY)
3998620252bcSChris Kirby 		goto top;
3999620252bcSChris Kirby 
4000842727c2SChris Kirby 	return (error);
4001842727c2SChris Kirby }
4002842727c2SChris Kirby 
4003ca45db41SChris Kirby /*
4004a7f53a56SChris Kirby  * Called at spa_load time (with retry == B_FALSE) to release a stale
4005a7f53a56SChris Kirby  * temporary user hold. Also called by the onexit code (with retry == B_TRUE).
4006ca45db41SChris Kirby  */
4007ca45db41SChris Kirby int
4008a7f53a56SChris Kirby dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag,
4009a7f53a56SChris Kirby     boolean_t retry)
4010ca45db41SChris Kirby {
4011ca45db41SChris Kirby 	dsl_dataset_t *ds;
4012ca45db41SChris Kirby 	char *snap;
4013ca45db41SChris Kirby 	char *name;
4014ca45db41SChris Kirby 	int namelen;
4015ca45db41SChris Kirby 	int error;
4016ca45db41SChris Kirby 
4017a7f53a56SChris Kirby 	do {
4018a7f53a56SChris Kirby 		rw_enter(&dp->dp_config_rwlock, RW_READER);
4019a7f53a56SChris Kirby 		error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
4020a7f53a56SChris Kirby 		rw_exit(&dp->dp_config_rwlock);
4021a7f53a56SChris Kirby 		if (error)
4022a7f53a56SChris Kirby 			return (error);
4023a7f53a56SChris Kirby 		namelen = dsl_dataset_namelen(ds)+1;
4024a7f53a56SChris Kirby 		name = kmem_alloc(namelen, KM_SLEEP);
4025a7f53a56SChris Kirby 		dsl_dataset_name(ds, name);
4026a7f53a56SChris Kirby 		dsl_dataset_rele(ds, FTAG);
4027ca45db41SChris Kirby 
4028a7f53a56SChris Kirby 		snap = strchr(name, '@');
4029a7f53a56SChris Kirby 		*snap = '\0';
4030a7f53a56SChris Kirby 		++snap;
4031a7f53a56SChris Kirby 		error = dsl_dataset_user_release(name, snap, htag, B_FALSE);
4032a7f53a56SChris Kirby 		kmem_free(name, namelen);
4033a7f53a56SChris Kirby 
4034a7f53a56SChris Kirby 		/*
4035a7f53a56SChris Kirby 		 * The object can't have been destroyed because we have a hold,
4036a7f53a56SChris Kirby 		 * but it might have been renamed, resulting in ENOENT.  Retry
4037a7f53a56SChris Kirby 		 * if we've been requested to do so.
4038a7f53a56SChris Kirby 		 *
4039a7f53a56SChris Kirby 		 * It would be nice if we could use the dsobj all the way
4040a7f53a56SChris Kirby 		 * through and avoid ENOENT entirely.  But we might need to
4041a7f53a56SChris Kirby 		 * unmount the snapshot, and there's currently no way to lookup
4042a7f53a56SChris Kirby 		 * a vfsp using a ZFS object id.
4043a7f53a56SChris Kirby 		 */
4044a7f53a56SChris Kirby 	} while ((error == ENOENT) && retry);
4045a7f53a56SChris Kirby 
4046a7f53a56SChris Kirby 	return (error);
4047ca45db41SChris Kirby }
4048ca45db41SChris Kirby 
4049842727c2SChris Kirby int
4050842727c2SChris Kirby dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp)
4051842727c2SChris Kirby {
4052842727c2SChris Kirby 	dsl_dataset_t *ds;
4053842727c2SChris Kirby 	int err;
4054842727c2SChris Kirby 
4055842727c2SChris Kirby 	err = dsl_dataset_hold(dsname, FTAG, &ds);
4056842727c2SChris Kirby 	if (err)
4057842727c2SChris Kirby 		return (err);
4058842727c2SChris Kirby 
4059842727c2SChris Kirby 	VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP));
4060842727c2SChris Kirby 	if (ds->ds_phys->ds_userrefs_obj != 0) {
4061842727c2SChris Kirby 		zap_attribute_t *za;
4062842727c2SChris Kirby 		zap_cursor_t zc;
4063842727c2SChris Kirby 
4064842727c2SChris Kirby 		za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
4065842727c2SChris Kirby 		for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset,
4066842727c2SChris Kirby 		    ds->ds_phys->ds_userrefs_obj);
4067842727c2SChris Kirby 		    zap_cursor_retrieve(&zc, za) == 0;
4068842727c2SChris Kirby 		    zap_cursor_advance(&zc)) {
4069842727c2SChris Kirby 			VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name,
4070842727c2SChris Kirby 			    za->za_first_integer));
4071842727c2SChris Kirby 		}
4072842727c2SChris Kirby 		zap_cursor_fini(&zc);
4073842727c2SChris Kirby 		kmem_free(za, sizeof (zap_attribute_t));
4074842727c2SChris Kirby 	}
4075842727c2SChris Kirby 	dsl_dataset_rele(ds, FTAG);
4076842727c2SChris Kirby 	return (0);
4077842727c2SChris Kirby }
4078503ad85cSMatthew Ahrens 
4079503ad85cSMatthew Ahrens /*
408019b94df9SMatthew Ahrens  * Note, this function is used as the callback for dmu_objset_find().  We
4081503ad85cSMatthew Ahrens  * always return 0 so that we will continue to find and process
4082503ad85cSMatthew Ahrens  * inconsistent datasets, even if we encounter an error trying to
4083503ad85cSMatthew Ahrens  * process one of them.
4084503ad85cSMatthew Ahrens  */
4085503ad85cSMatthew Ahrens /* ARGSUSED */
4086503ad85cSMatthew Ahrens int
4087fd136879SMatthew Ahrens dsl_destroy_inconsistent(const char *dsname, void *arg)
4088503ad85cSMatthew Ahrens {
4089503ad85cSMatthew Ahrens 	dsl_dataset_t *ds;
4090503ad85cSMatthew Ahrens 
4091503ad85cSMatthew Ahrens 	if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) {
4092503ad85cSMatthew Ahrens 		if (DS_IS_INCONSISTENT(ds))
4093503ad85cSMatthew Ahrens 			(void) dsl_dataset_destroy(ds, FTAG, B_FALSE);
4094503ad85cSMatthew Ahrens 		else
4095503ad85cSMatthew Ahrens 			dsl_dataset_disown(ds, FTAG);
4096503ad85cSMatthew Ahrens 	}
4097503ad85cSMatthew Ahrens 	return (0);
4098503ad85cSMatthew Ahrens }
409919b94df9SMatthew Ahrens 
410019b94df9SMatthew Ahrens /*
410119b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
410219b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
410319b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
410419b94df9SMatthew Ahrens  * fail and return EINVAL.
410519b94df9SMatthew Ahrens  *
410619b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
410719b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
410819b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
410919b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
411019b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
411119b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
411219b94df9SMatthew Ahrens  *
411319b94df9SMatthew Ahrens  * space freed                         [---------------------]
411419b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
411519b94df9SMatthew Ahrens  *                                         oldsnap            new
411619b94df9SMatthew Ahrens  */
411719b94df9SMatthew Ahrens int
411819b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
411919b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
412019b94df9SMatthew Ahrens {
412119b94df9SMatthew Ahrens 	int err = 0;
412219b94df9SMatthew Ahrens 	uint64_t snapobj;
412319b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
412419b94df9SMatthew Ahrens 
412519b94df9SMatthew Ahrens 	*usedp = 0;
412619b94df9SMatthew Ahrens 	*usedp += new->ds_phys->ds_used_bytes;
412719b94df9SMatthew Ahrens 	*usedp -= oldsnap->ds_phys->ds_used_bytes;
412819b94df9SMatthew Ahrens 
412919b94df9SMatthew Ahrens 	*compp = 0;
413019b94df9SMatthew Ahrens 	*compp += new->ds_phys->ds_compressed_bytes;
413119b94df9SMatthew Ahrens 	*compp -= oldsnap->ds_phys->ds_compressed_bytes;
413219b94df9SMatthew Ahrens 
413319b94df9SMatthew Ahrens 	*uncompp = 0;
413419b94df9SMatthew Ahrens 	*uncompp += new->ds_phys->ds_uncompressed_bytes;
413519b94df9SMatthew Ahrens 	*uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
413619b94df9SMatthew Ahrens 
413719b94df9SMatthew Ahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
413819b94df9SMatthew Ahrens 	snapobj = new->ds_object;
413919b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
414019b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
414119b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
414219b94df9SMatthew Ahrens 
414319b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
414419b94df9SMatthew Ahrens 		if (err != 0)
414519b94df9SMatthew Ahrens 			break;
414619b94df9SMatthew Ahrens 
414719b94df9SMatthew Ahrens 		if (snap->ds_phys->ds_prev_snap_txg ==
414819b94df9SMatthew Ahrens 		    oldsnap->ds_phys->ds_creation_txg) {
414919b94df9SMatthew Ahrens 			/*
415019b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
415119b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
415219b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
415319b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
415419b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
415519b94df9SMatthew Ahrens 			 * optimization itself.
415619b94df9SMatthew Ahrens 			 */
415719b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
415819b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
415919b94df9SMatthew Ahrens 		} else {
416019b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
416119b94df9SMatthew Ahrens 			    0, oldsnap->ds_phys->ds_creation_txg,
416219b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
416319b94df9SMatthew Ahrens 		}
416419b94df9SMatthew Ahrens 		*usedp += used;
416519b94df9SMatthew Ahrens 		*compp += comp;
416619b94df9SMatthew Ahrens 		*uncompp += uncomp;
416719b94df9SMatthew Ahrens 
416819b94df9SMatthew Ahrens 		/*
416919b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
417019b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
417119b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
417219b94df9SMatthew Ahrens 		 */
417319b94df9SMatthew Ahrens 		snapobj = snap->ds_phys->ds_prev_snap_obj;
417419b94df9SMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
417519b94df9SMatthew Ahrens 		if (snapobj == 0) {
417619b94df9SMatthew Ahrens 			err = EINVAL;
417719b94df9SMatthew Ahrens 			break;
417819b94df9SMatthew Ahrens 		}
417919b94df9SMatthew Ahrens 
418019b94df9SMatthew Ahrens 	}
418119b94df9SMatthew Ahrens 	rw_exit(&dp->dp_config_rwlock);
418219b94df9SMatthew Ahrens 	return (err);
418319b94df9SMatthew Ahrens }
418419b94df9SMatthew Ahrens 
418519b94df9SMatthew Ahrens /*
418619b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
418719b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
418819b94df9SMatthew Ahrens  *
418919b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
419019b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
419119b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
419219b94df9SMatthew Ahrens  *
419319b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
419419b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
419519b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
419619b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
419719b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
419819b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
419919b94df9SMatthew Ahrens  */
420019b94df9SMatthew Ahrens int
420119b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
420219b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
420319b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
420419b94df9SMatthew Ahrens {
420519b94df9SMatthew Ahrens 	int err = 0;
420619b94df9SMatthew Ahrens 	uint64_t snapobj;
420719b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
420819b94df9SMatthew Ahrens 
420919b94df9SMatthew Ahrens 	ASSERT(dsl_dataset_is_snapshot(firstsnap));
421019b94df9SMatthew Ahrens 	ASSERT(dsl_dataset_is_snapshot(lastsnap));
421119b94df9SMatthew Ahrens 
421219b94df9SMatthew Ahrens 	/*
421319b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
421419b94df9SMatthew Ahrens 	 * is before lastsnap.
421519b94df9SMatthew Ahrens 	 */
421619b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
421719b94df9SMatthew Ahrens 	    firstsnap->ds_phys->ds_creation_txg >
421819b94df9SMatthew Ahrens 	    lastsnap->ds_phys->ds_creation_txg)
421919b94df9SMatthew Ahrens 		return (EINVAL);
422019b94df9SMatthew Ahrens 
422119b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
422219b94df9SMatthew Ahrens 
422319b94df9SMatthew Ahrens 	rw_enter(&dp->dp_config_rwlock, RW_READER);
422419b94df9SMatthew Ahrens 	snapobj = lastsnap->ds_phys->ds_next_snap_obj;
422519b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
422619b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
422719b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
422819b94df9SMatthew Ahrens 
422919b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
423019b94df9SMatthew Ahrens 		if (err != 0)
423119b94df9SMatthew Ahrens 			break;
423219b94df9SMatthew Ahrens 
423319b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
423419b94df9SMatthew Ahrens 		    firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
423519b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
423619b94df9SMatthew Ahrens 		*usedp += used;
423719b94df9SMatthew Ahrens 		*compp += comp;
423819b94df9SMatthew Ahrens 		*uncompp += uncomp;
423919b94df9SMatthew Ahrens 
424019b94df9SMatthew Ahrens 		snapobj = ds->ds_phys->ds_prev_snap_obj;
424119b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
424219b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
424319b94df9SMatthew Ahrens 	}
424419b94df9SMatthew Ahrens 	rw_exit(&dp->dp_config_rwlock);
424519b94df9SMatthew Ahrens 	return (err);
424619b94df9SMatthew Ahrens }
4247