xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision d2b3cbbd7f3a37bc7c01b526d3eb312acd070423)
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.
23b461c746SMatthew Ahrens  * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
24a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
2503d1795fSAlexander Stetsenko  * Copyright (c) 2014 RackTop Systems.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/dmu_objset.h>
29fa9e4066Sahrens #include <sys/dsl_dataset.h>
30fa9e4066Sahrens #include <sys/dsl_dir.h>
3199653d4eSeschrock #include <sys/dsl_prop.h>
321d452cf5Sahrens #include <sys/dsl_synctask.h>
33fa9e4066Sahrens #include <sys/dmu_traverse.h>
344e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
35fa9e4066Sahrens #include <sys/dmu_tx.h>
36fa9e4066Sahrens #include <sys/arc.h>
37fa9e4066Sahrens #include <sys/zio.h>
38fa9e4066Sahrens #include <sys/zap.h>
39ad135b5dSChristopher Siden #include <sys/zfeature.h>
40fa9e4066Sahrens #include <sys/unique.h>
41fa9e4066Sahrens #include <sys/zfs_context.h>
42cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
43ecd6cf80Smarks #include <sys/spa.h>
44088f3894Sahrens #include <sys/zfs_znode.h>
45c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
46842727c2SChris Kirby #include <sys/zvol.h>
473f9d6ad7SLin Ling #include <sys/dsl_scan.h>
48cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
493b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
503b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
5178f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
52e1930233Sbonwick 
53cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
54cde58dbcSMatthew Ahrens 	{ \
55cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
56cde58dbcSMatthew Ahrens 		(x) = (y); \
57cde58dbcSMatthew Ahrens 		(y) = __tmp; \
58cde58dbcSMatthew Ahrens 	}
59cde58dbcSMatthew Ahrens 
6055434c77Sek #define	DS_REF_MAX	(1ULL << 62)
61fa9e4066Sahrens 
62fa9e4066Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
63fa9e4066Sahrens 
64a9799022Sck /*
65a9799022Sck  * Figure out how much of this delta should be propogated to the dsl_dir
66a9799022Sck  * layer.  If there's a refreservation, that space has already been
67a9799022Sck  * partially accounted for in our ancestors.
68a9799022Sck  */
69a9799022Sck static int64_t
70a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta)
71a9799022Sck {
72a9799022Sck 	uint64_t old_bytes, new_bytes;
73a9799022Sck 
74a9799022Sck 	if (ds->ds_reserved == 0)
75a9799022Sck 		return (delta);
76a9799022Sck 
77a9799022Sck 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
78a9799022Sck 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
79a9799022Sck 
80a9799022Sck 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
81a9799022Sck 	return (new_bytes - old_bytes);
82a9799022Sck }
83fa9e4066Sahrens 
84fa9e4066Sahrens void
85b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
86fa9e4066Sahrens {
87b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
88fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
89fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
90a9799022Sck 	int64_t delta;
91fa9e4066Sahrens 
923f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
93fa9e4066Sahrens 
94fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
95fa9e4066Sahrens 	/* It could have been compressed away to nothing */
96fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
97fa9e4066Sahrens 		return;
98fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
99ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
100fa9e4066Sahrens 	if (ds == NULL) {
101ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
102ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
103fa9e4066Sahrens 		return;
104fa9e4066Sahrens 	}
1053f9d6ad7SLin Ling 
106b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
107fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
108a9799022Sck 	delta = parent_delta(ds, used);
109ad135b5dSChristopher Siden 	ds->ds_phys->ds_referenced_bytes += used;
110fa9e4066Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
111fa9e4066Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
112fa9e4066Sahrens 	ds->ds_phys->ds_unique_bytes += used;
113fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
11474e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
11574e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
11674e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
11774e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
118fa9e4066Sahrens }
119fa9e4066Sahrens 
120cdb0ab79Smaybee int
121b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
122b24ab676SJeff Bonwick     boolean_t async)
123fa9e4066Sahrens {
12443466aaeSMax Grossman 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
12543466aaeSMax Grossman 	int compressed = BP_GET_PSIZE(bp);
12643466aaeSMax Grossman 	int uncompressed = BP_GET_UCSIZE(bp);
12743466aaeSMax Grossman 
128fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
129cdb0ab79Smaybee 		return (0);
130fa9e4066Sahrens 
131b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
132b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
133b24ab676SJeff Bonwick 
134fa9e4066Sahrens 	if (ds == NULL) {
135b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
136ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
137ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
138cdb0ab79Smaybee 		return (used);
139fa9e4066Sahrens 	}
140fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
141fa9e4066Sahrens 
14274e7dc98SMatthew Ahrens 	ASSERT(!dsl_dataset_is_snapshot(ds));
143fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
144fa9e4066Sahrens 
145fa9e4066Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
146a9799022Sck 		int64_t delta;
147c717a561Smaybee 
1483f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
149b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
150fa9e4066Sahrens 
151fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
152a9799022Sck 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
153a9799022Sck 		    !DS_UNIQUE_IS_ACCURATE(ds));
154a9799022Sck 		delta = parent_delta(ds, -used);
155fa9e4066Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
156fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
15774e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
158a9799022Sck 		    delta, -compressed, -uncompressed, tx);
15974e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
16074e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
161fa9e4066Sahrens 	} else {
162fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
163b24ab676SJeff Bonwick 		if (async) {
164b24ab676SJeff Bonwick 			/*
165b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
166b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
167cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
168b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
169b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
170b24ab676SJeff Bonwick 			 */
171cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
172b24ab676SJeff Bonwick 		} else {
173cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
174b24ab676SJeff Bonwick 		}
175a4611edeSahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
176a4611edeSahrens 		    ds->ds_phys->ds_prev_snap_obj);
177a4611edeSahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
178fa9e4066Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
179a4611edeSahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
180a4611edeSahrens 		    ds->ds_object && bp->blk_birth >
181a4611edeSahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
182a4611edeSahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
183a4611edeSahrens 			mutex_enter(&ds->ds_prev->ds_lock);
184a4611edeSahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
185a4611edeSahrens 			mutex_exit(&ds->ds_prev->ds_lock);
186fa9e4066Sahrens 		}
1873f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
18874e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
18974e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
19074e7dc98SMatthew Ahrens 		}
191fa9e4066Sahrens 	}
192fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
193ad135b5dSChristopher Siden 	ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
194ad135b5dSChristopher Siden 	ds->ds_phys->ds_referenced_bytes -= used;
195fa9e4066Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
196fa9e4066Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
197fa9e4066Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
198fa9e4066Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
199fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
200cdb0ab79Smaybee 
201cdb0ab79Smaybee 	return (used);
202fa9e4066Sahrens }
203fa9e4066Sahrens 
204ea8dc4b6Seschrock uint64_t
205ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
206fa9e4066Sahrens {
207a2eea2e1Sahrens 	uint64_t trysnap = 0;
208a2eea2e1Sahrens 
209fa9e4066Sahrens 	if (ds == NULL)
210ea8dc4b6Seschrock 		return (0);
211fa9e4066Sahrens 	/*
212fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
213fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
214fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
215fa9e4066Sahrens 	 * consume, which is OK.
216fa9e4066Sahrens 	 *
217fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
218fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
219fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
220fa9e4066Sahrens 	 */
221a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
222a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
223a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
224a2eea2e1Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
225ea8dc4b6Seschrock }
226ea8dc4b6Seschrock 
2273d692628SSanjeev Bagewadi boolean_t
228c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
229c7cd2421SGeorge Wilson     uint64_t blk_birth)
230ea8dc4b6Seschrock {
23143466aaeSMax Grossman 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
23243466aaeSMax Grossman 	    (bp != NULL && BP_IS_HOLE(bp)))
233c7cd2421SGeorge Wilson 		return (B_FALSE);
234c7cd2421SGeorge Wilson 
235837b568bSGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
236c7cd2421SGeorge Wilson 
237c7cd2421SGeorge Wilson 	return (B_TRUE);
238fa9e4066Sahrens }
239fa9e4066Sahrens 
240fa9e4066Sahrens /* ARGSUSED */
241fa9e4066Sahrens static void
242fa9e4066Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
243fa9e4066Sahrens {
244fa9e4066Sahrens 	dsl_dataset_t *ds = dsv;
245fa9e4066Sahrens 
2463b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
247fa9e4066Sahrens 
24891ebeef5Sahrens 	unique_remove(ds->ds_fsid_guid);
249fa9e4066Sahrens 
250503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
251503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
252fa9e4066Sahrens 
253fa9e4066Sahrens 	if (ds->ds_prev) {
2543b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
255fa9e4066Sahrens 		ds->ds_prev = NULL;
256fa9e4066Sahrens 	}
257fa9e4066Sahrens 
258cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
2593b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_deadlist_obj != 0)
260cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
261745cd3c5Smaybee 	if (ds->ds_dir)
2623b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
263fa9e4066Sahrens 
26491ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
265fa9e4066Sahrens 
2665ad82045Snd 	mutex_destroy(&ds->ds_lock);
26791ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
268*d2b3cbbdSJorgen Lundman 	mutex_destroy(&ds->ds_sendstream_lock);
2693b2aab18SMatthew Ahrens 	refcount_destroy(&ds->ds_longholds);
2705ad82045Snd 
271fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
272fa9e4066Sahrens }
273fa9e4066Sahrens 
2743b2aab18SMatthew Ahrens int
275fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
276fa9e4066Sahrens {
277fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
278fa9e4066Sahrens 	int err;
279fa9e4066Sahrens 	dmu_buf_t *headdbuf;
280fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
281fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
282fa9e4066Sahrens 
283fa9e4066Sahrens 	if (ds->ds_snapname[0])
284ea8dc4b6Seschrock 		return (0);
285fa9e4066Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
286ea8dc4b6Seschrock 		return (0);
287fa9e4066Sahrens 
288ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
289ea8dc4b6Seschrock 	    FTAG, &headdbuf);
2903b2aab18SMatthew Ahrens 	if (err != 0)
291ea8dc4b6Seschrock 		return (err);
292fa9e4066Sahrens 	headphys = headdbuf->db_data;
293fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
294e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
295ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
296ea8dc4b6Seschrock 	return (err);
297fa9e4066Sahrens }
298fa9e4066Sahrens 
2993b2aab18SMatthew Ahrens int
300745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
301ab04eb8eStimh {
302745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
303745cd3c5Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
304ab04eb8eStimh 	matchtype_t mt;
305ab04eb8eStimh 	int err;
306ab04eb8eStimh 
307745cd3c5Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
308ab04eb8eStimh 		mt = MT_FIRST;
309ab04eb8eStimh 	else
310ab04eb8eStimh 		mt = MT_EXACT;
311ab04eb8eStimh 
312745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
313ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
314ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
315745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
316ab04eb8eStimh 	return (err);
317ab04eb8eStimh }
318ab04eb8eStimh 
3193b2aab18SMatthew Ahrens int
320a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
321a2afb611SJerry Jelinek     boolean_t adj_cnt)
322ab04eb8eStimh {
323745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
324745cd3c5Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
325ab04eb8eStimh 	matchtype_t mt;
326ab04eb8eStimh 	int err;
327ab04eb8eStimh 
32871eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
32971eb0538SChris Kirby 
330745cd3c5Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
331ab04eb8eStimh 		mt = MT_FIRST;
332ab04eb8eStimh 	else
333ab04eb8eStimh 		mt = MT_EXACT;
334ab04eb8eStimh 
335745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
336ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
337745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
338a2afb611SJerry Jelinek 
339a2afb611SJerry Jelinek 	if (err == 0 && adj_cnt)
340a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
341a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
342a2afb611SJerry Jelinek 
343ab04eb8eStimh 	return (err);
344ab04eb8eStimh }
345ab04eb8eStimh 
3463b2aab18SMatthew Ahrens int
3473b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
348745cd3c5Smaybee     dsl_dataset_t **dsp)
349fa9e4066Sahrens {
350fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
351fa9e4066Sahrens 	dmu_buf_t *dbuf;
352fa9e4066Sahrens 	dsl_dataset_t *ds;
353ea8dc4b6Seschrock 	int err;
354a7f53a56SChris Kirby 	dmu_object_info_t doi;
355fa9e4066Sahrens 
3563b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
357fa9e4066Sahrens 
358ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3593b2aab18SMatthew Ahrens 	if (err != 0)
360ea8dc4b6Seschrock 		return (err);
361a7f53a56SChris Kirby 
362a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
363a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
3642acef22dSMatthew Ahrens 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
365b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
366be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
367b287be1bSWill Andrews 	}
368a7f53a56SChris Kirby 
369fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
370fa9e4066Sahrens 	if (ds == NULL) {
371d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
372fa9e4066Sahrens 
373fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
374fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
375fa9e4066Sahrens 		ds->ds_object = dsobj;
376fa9e4066Sahrens 		ds->ds_phys = dbuf->db_data;
377fa9e4066Sahrens 
3785ad82045Snd 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
37991ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3804e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
3813b2aab18SMatthew Ahrens 		refcount_create(&ds->ds_longholds);
3825ad82045Snd 
383cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
384cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
385fa9e4066Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
386cde58dbcSMatthew Ahrens 
3874e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
3884e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
3894e3c9f44SBill Pijewski 
390ea8dc4b6Seschrock 		if (err == 0) {
3913b2aab18SMatthew Ahrens 			err = dsl_dir_hold_obj(dp,
392ea8dc4b6Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
393ea8dc4b6Seschrock 		}
3943b2aab18SMatthew Ahrens 		if (err != 0) {
3955ad82045Snd 			mutex_destroy(&ds->ds_lock);
39691ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
397*d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
3983b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
399cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
400cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
401ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
402ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
403ea8dc4b6Seschrock 			return (err);
404ea8dc4b6Seschrock 		}
405fa9e4066Sahrens 
40674e7dc98SMatthew Ahrens 		if (!dsl_dataset_is_snapshot(ds)) {
407fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
4083b2aab18SMatthew Ahrens 			if (ds->ds_phys->ds_prev_snap_obj != 0) {
4093b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
410745cd3c5Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
411745cd3c5Smaybee 				    ds, &ds->ds_prev);
412fa9e4066Sahrens 			}
41378f17100SMatthew Ahrens 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
41478f17100SMatthew Ahrens 				int zaperr = zap_lookup(mos, ds->ds_object,
41578f17100SMatthew Ahrens 				    DS_FIELD_BOOKMARK_NAMES,
41678f17100SMatthew Ahrens 				    sizeof (ds->ds_bookmarks), 1,
41778f17100SMatthew Ahrens 				    &ds->ds_bookmarks);
41878f17100SMatthew Ahrens 				if (zaperr != ENOENT)
41978f17100SMatthew Ahrens 					VERIFY0(zaperr);
42078f17100SMatthew Ahrens 			}
421842727c2SChris Kirby 		} else {
422842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
423842727c2SChris Kirby 				err = dsl_dataset_get_snapname(ds);
424842727c2SChris Kirby 			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
425842727c2SChris Kirby 				err = zap_count(
426842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
427842727c2SChris Kirby 				    ds->ds_phys->ds_userrefs_obj,
428842727c2SChris Kirby 				    &ds->ds_userrefs);
429842727c2SChris Kirby 			}
430fa9e4066Sahrens 		}
431fa9e4066Sahrens 
43274e7dc98SMatthew Ahrens 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
4333b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
4343b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4353b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
436cb625fb5Sck 			if (err == 0) {
4373b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
4383b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4393b2aab18SMatthew Ahrens 				    &ds->ds_quota);
440cb625fb5Sck 			}
441cb625fb5Sck 		} else {
442cb625fb5Sck 			ds->ds_reserved = ds->ds_quota = 0;
443cb625fb5Sck 		}
444cb625fb5Sck 
445d5285caeSGeorge Wilson 		if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
446d5285caeSGeorge Wilson 		    &ds->ds_phys, dsl_dataset_evict)) != NULL) {
447cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
448cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
449745cd3c5Smaybee 			if (ds->ds_prev)
4503b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
4513b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
4525ad82045Snd 			mutex_destroy(&ds->ds_lock);
45391ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
454*d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
4553b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
456fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4573b2aab18SMatthew Ahrens 			if (err != 0) {
458ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
459ea8dc4b6Seschrock 				return (err);
460ea8dc4b6Seschrock 			}
461fa9e4066Sahrens 			ds = winner;
462fa9e4066Sahrens 		} else {
46391ebeef5Sahrens 			ds->ds_fsid_guid =
464fa9e4066Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
465fa9e4066Sahrens 		}
466fa9e4066Sahrens 	}
467fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
468fa9e4066Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
469088f3894Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
470afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
47184db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
472ea8dc4b6Seschrock 	*dsp = ds;
473ea8dc4b6Seschrock 	return (0);
474fa9e4066Sahrens }
475fa9e4066Sahrens 
476745cd3c5Smaybee int
4773b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name,
478503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
479fa9e4066Sahrens {
480fa9e4066Sahrens 	dsl_dir_t *dd;
481745cd3c5Smaybee 	const char *snapname;
482fa9e4066Sahrens 	uint64_t obj;
483fa9e4066Sahrens 	int err = 0;
484fa9e4066Sahrens 
4853b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
4863b2aab18SMatthew Ahrens 	if (err != 0)
487ea8dc4b6Seschrock 		return (err);
488fa9e4066Sahrens 
4893b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
490fa9e4066Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
4913b2aab18SMatthew Ahrens 	if (obj != 0)
4923b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
493745cd3c5Smaybee 	else
494be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
495fa9e4066Sahrens 
496745cd3c5Smaybee 	/* we may be looking for a snapshot */
497745cd3c5Smaybee 	if (err == 0 && snapname != NULL) {
4983b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
499fa9e4066Sahrens 
500745cd3c5Smaybee 		if (*snapname++ != '@') {
501745cd3c5Smaybee 			dsl_dataset_rele(*dsp, tag);
5023b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
503be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
504fa9e4066Sahrens 		}
505fa9e4066Sahrens 
506745cd3c5Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
507745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
508745cd3c5Smaybee 		if (err == 0)
5093b2aab18SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
510745cd3c5Smaybee 		dsl_dataset_rele(*dsp, tag);
511745cd3c5Smaybee 
5123b2aab18SMatthew Ahrens 		if (err == 0) {
513745cd3c5Smaybee 			mutex_enter(&ds->ds_lock);
514745cd3c5Smaybee 			if (ds->ds_snapname[0] == 0)
515745cd3c5Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
516745cd3c5Smaybee 				    sizeof (ds->ds_snapname));
517745cd3c5Smaybee 			mutex_exit(&ds->ds_lock);
5183b2aab18SMatthew Ahrens 			*dsp = ds;
519fa9e4066Sahrens 		}
520fa9e4066Sahrens 	}
5213b2aab18SMatthew Ahrens 
5223b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
523fa9e4066Sahrens 	return (err);
524fa9e4066Sahrens }
525fa9e4066Sahrens 
526fa9e4066Sahrens int
5273b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
5283b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
5293b2aab18SMatthew Ahrens {
5303b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
5313b2aab18SMatthew Ahrens 	if (err != 0)
5323b2aab18SMatthew Ahrens 		return (err);
5333b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
5343b2aab18SMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
5353b2aab18SMatthew Ahrens 		*dsp = NULL;
536be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
5373b2aab18SMatthew Ahrens 	}
5383b2aab18SMatthew Ahrens 	return (0);
5393b2aab18SMatthew Ahrens }
5403b2aab18SMatthew Ahrens 
5413b2aab18SMatthew Ahrens int
5423b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name,
543503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
544fa9e4066Sahrens {
5453b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold(dp, name, tag, dsp);
5463b2aab18SMatthew Ahrens 	if (err != 0)
547745cd3c5Smaybee 		return (err);
5483b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
549503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
550be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
551745cd3c5Smaybee 	}
552745cd3c5Smaybee 	return (0);
553fa9e4066Sahrens }
554fa9e4066Sahrens 
5553b2aab18SMatthew Ahrens /*
5563b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
5573b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
5583b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
5593b2aab18SMatthew Ahrens  *
5603b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
5613b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
5623b2aab18SMatthew Ahrens  * and accessed.
5633b2aab18SMatthew Ahrens  */
5643b2aab18SMatthew Ahrens void
5653b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
5663b2aab18SMatthew Ahrens {
5673b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
5683b2aab18SMatthew Ahrens 	(void) refcount_add(&ds->ds_longholds, tag);
5693b2aab18SMatthew Ahrens }
5703b2aab18SMatthew Ahrens 
5713b2aab18SMatthew Ahrens void
5723b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
5733b2aab18SMatthew Ahrens {
5743b2aab18SMatthew Ahrens 	(void) refcount_remove(&ds->ds_longholds, tag);
5753b2aab18SMatthew Ahrens }
5763b2aab18SMatthew Ahrens 
5773b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
5783b2aab18SMatthew Ahrens boolean_t
5793b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
5803b2aab18SMatthew Ahrens {
5813b2aab18SMatthew Ahrens 	return (!refcount_is_zero(&ds->ds_longholds));
5823b2aab18SMatthew Ahrens }
5833b2aab18SMatthew Ahrens 
584fa9e4066Sahrens void
585fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
586fa9e4066Sahrens {
587fa9e4066Sahrens 	if (ds == NULL) {
588fa9e4066Sahrens 		(void) strcpy(name, "mos");
589fa9e4066Sahrens 	} else {
590fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
5913b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
592fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
593fa9e4066Sahrens 			(void) strcat(name, "@");
594745cd3c5Smaybee 			/*
595745cd3c5Smaybee 			 * We use a "recursive" mutex so that we
596745cd3c5Smaybee 			 * can call dprintf_ds() with ds_lock held.
597745cd3c5Smaybee 			 */
598fa9e4066Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
599fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
600fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
601fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
602fa9e4066Sahrens 			} else {
603fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
604fa9e4066Sahrens 			}
605fa9e4066Sahrens 		}
606fa9e4066Sahrens 	}
607fa9e4066Sahrens }
608fa9e4066Sahrens 
6093cb34c60Sahrens void
610745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6113cb34c60Sahrens {
6123b2aab18SMatthew Ahrens 	dmu_buf_rele(ds->ds_dbuf, tag);
613745cd3c5Smaybee }
614745cd3c5Smaybee 
615745cd3c5Smaybee void
616503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
617745cd3c5Smaybee {
6183b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
619745cd3c5Smaybee 
6203cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
621745cd3c5Smaybee 	ds->ds_owner = NULL;
6223cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
6233b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
6243b2aab18SMatthew Ahrens 	if (ds->ds_dbuf != NULL)
6253b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, tag);
626745cd3c5Smaybee 	else
627cde58dbcSMatthew Ahrens 		dsl_dataset_evict(NULL, ds);
6283cb34c60Sahrens }
6293cb34c60Sahrens 
6303cb34c60Sahrens boolean_t
6313b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
6323cb34c60Sahrens {
633745cd3c5Smaybee 	boolean_t gotit = FALSE;
634745cd3c5Smaybee 
6353cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
6363b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
637503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
6383b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
639745cd3c5Smaybee 		gotit = TRUE;
6403cb34c60Sahrens 	}
6413cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
642745cd3c5Smaybee 	return (gotit);
643745cd3c5Smaybee }
644745cd3c5Smaybee 
6451d452cf5Sahrens uint64_t
646088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
647ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
648fa9e4066Sahrens {
6493cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
650fa9e4066Sahrens 	dmu_buf_t *dbuf;
651fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
6523cb34c60Sahrens 	uint64_t dsobj;
653fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
654fa9e4066Sahrens 
655088f3894Sahrens 	if (origin == NULL)
656088f3894Sahrens 		origin = dp->dp_origin_snap;
657088f3894Sahrens 
6583cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
6593cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
660fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
6613cb34c60Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
662fa9e4066Sahrens 
6631649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
6641649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
6653b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
666fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
667fa9e4066Sahrens 	dsphys = dbuf->db_data;
668745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
669fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
670ab04eb8eStimh 	dsphys->ds_flags = flags;
671fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
672fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
673fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
674fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
675ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
676ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
677fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
678088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
679a9799022Sck 
680cde58dbcSMatthew Ahrens 	if (origin == NULL) {
681cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
682cde58dbcSMatthew Ahrens 	} else {
6833b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
684cde58dbcSMatthew Ahrens 
6853cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
686fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
6873cb34c60Sahrens 		    origin->ds_phys->ds_creation_txg;
688ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
689ad135b5dSChristopher Siden 		    origin->ds_phys->ds_referenced_bytes;
690fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
6913cb34c60Sahrens 		    origin->ds_phys->ds_compressed_bytes;
692fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
6933cb34c60Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
6943cb34c60Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
695579ae4d5Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
696fa9e4066Sahrens 
6973cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
6983cb34c60Sahrens 		origin->ds_phys->ds_num_children++;
699fa9e4066Sahrens 
7003b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
701cde58dbcSMatthew Ahrens 		    origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
702cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
703cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
704cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
705cde58dbcSMatthew Ahrens 
706088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
707088f3894Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
708088f3894Sahrens 				origin->ds_phys->ds_next_clones_obj =
709088f3894Sahrens 				    zap_create(mos,
710088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
711088f3894Sahrens 			}
7123b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
7133b2aab18SMatthew Ahrens 			    origin->ds_phys->ds_next_clones_obj, dsobj, tx));
714088f3894Sahrens 		}
715088f3894Sahrens 
716fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
7173cb34c60Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
718cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
719cde58dbcSMatthew Ahrens 			if (origin->ds_dir->dd_phys->dd_clones == 0) {
720cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
721cde58dbcSMatthew Ahrens 				origin->ds_dir->dd_phys->dd_clones =
722cde58dbcSMatthew Ahrens 				    zap_create(mos,
723cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
724cde58dbcSMatthew Ahrens 			}
7253b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
726cde58dbcSMatthew Ahrens 			    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
727cde58dbcSMatthew Ahrens 		}
728fa9e4066Sahrens 	}
729ab04eb8eStimh 
730ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
731ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
732ab04eb8eStimh 
733ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
734fa9e4066Sahrens 
735fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
736fa9e4066Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
7373cb34c60Sahrens 
7383cb34c60Sahrens 	return (dsobj);
7393cb34c60Sahrens }
7403cb34c60Sahrens 
7413b2aab18SMatthew Ahrens static void
7423b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
7433b2aab18SMatthew Ahrens {
7443b2aab18SMatthew Ahrens 	objset_t *os;
7453b2aab18SMatthew Ahrens 
7463b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
7473b2aab18SMatthew Ahrens 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
7483b2aab18SMatthew Ahrens 	dsl_dataset_dirty(ds, tx);
7493b2aab18SMatthew Ahrens }
7503b2aab18SMatthew Ahrens 
7513cb34c60Sahrens uint64_t
752ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
753ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
7543cb34c60Sahrens {
7553cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
7563cb34c60Sahrens 	uint64_t dsobj, ddobj;
7573cb34c60Sahrens 	dsl_dir_t *dd;
7583cb34c60Sahrens 
7593b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
7603cb34c60Sahrens 	ASSERT(lastname[0] != '@');
7613cb34c60Sahrens 
762088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
7633b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
7643cb34c60Sahrens 
7653b2aab18SMatthew Ahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
7663b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
7673cb34c60Sahrens 
7683cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
7693cb34c60Sahrens 
770a2afb611SJerry Jelinek 	/*
771a2afb611SJerry Jelinek 	 * Since we're creating a new node we know it's a leaf, so we can
772a2afb611SJerry Jelinek 	 * initialize the counts if the limit feature is active.
773a2afb611SJerry Jelinek 	 */
774a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
775a2afb611SJerry Jelinek 		uint64_t cnt = 0;
776a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
777a2afb611SJerry Jelinek 
778a2afb611SJerry Jelinek 		dsl_dir_zapify(dd, tx);
779a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
780a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
781a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
782a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
783a2afb611SJerry Jelinek 	}
784a2afb611SJerry Jelinek 
7853b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
786fa9e4066Sahrens 
787feaa74e4SMark Maybee 	/*
788feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
789feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
790feaa74e4SMark Maybee 	 */
7913b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
792feaa74e4SMark Maybee 		dsl_dataset_t *ds;
793feaa74e4SMark Maybee 
7943b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
7953b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
796feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
797feaa74e4SMark Maybee 	}
798feaa74e4SMark Maybee 
7991d452cf5Sahrens 	return (dsobj);
800fa9e4066Sahrens }
801fa9e4066Sahrens 
8021d452cf5Sahrens /*
8033b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
8043b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
8053b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
8063b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
8073b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
8083b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
8091d452cf5Sahrens  */
8103b2aab18SMatthew Ahrens void
8113b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
8121d452cf5Sahrens {
8133b2aab18SMatthew Ahrens 	uint64_t mrs_used;
8143b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
8151d452cf5Sahrens 
8163b2aab18SMatthew Ahrens 	ASSERT(!dsl_dataset_is_snapshot(ds));
8171d452cf5Sahrens 
8183b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0)
8193b2aab18SMatthew Ahrens 		mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
8203b2aab18SMatthew Ahrens 	else
8213b2aab18SMatthew Ahrens 		mrs_used = 0;
822842727c2SChris Kirby 
8233b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
824fa9e4066Sahrens 
8253b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
8263b2aab18SMatthew Ahrens 	ds->ds_phys->ds_unique_bytes =
8273b2aab18SMatthew Ahrens 	    ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
82819b94df9SMatthew Ahrens 
8293b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
8303b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
8313b2aab18SMatthew Ahrens 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
832fa9e4066Sahrens }
833fa9e4066Sahrens 
8343b2aab18SMatthew Ahrens void
8353b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
8363b2aab18SMatthew Ahrens     dmu_tx_t *tx)
837842727c2SChris Kirby {
8383b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
8393b2aab18SMatthew Ahrens 	uint64_t count;
8403b2aab18SMatthew Ahrens 	int err;
8413b2aab18SMatthew Ahrens 
8423b2aab18SMatthew Ahrens 	ASSERT(ds->ds_phys->ds_num_children >= 2);
8433b2aab18SMatthew Ahrens 	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
8443b2aab18SMatthew Ahrens 	/*
8453b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
8463b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
8473b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
8483b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
8493b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
8503b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
8513b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
8523b2aab18SMatthew Ahrens 	 * remove this one.
8533b2aab18SMatthew Ahrens 	 */
8543b2aab18SMatthew Ahrens 	if (err != ENOENT)
8553b2aab18SMatthew Ahrens 		VERIFY0(err);
8563b2aab18SMatthew Ahrens 	ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
8573b2aab18SMatthew Ahrens 	    &count));
8583b2aab18SMatthew Ahrens 	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
8593b2aab18SMatthew Ahrens }
860842727c2SChris Kirby 
861842727c2SChris Kirby 
8623b2aab18SMatthew Ahrens blkptr_t *
8633b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds)
8643b2aab18SMatthew Ahrens {
8653b2aab18SMatthew Ahrens 	return (&ds->ds_phys->ds_bp);
866842727c2SChris Kirby }
867842727c2SChris Kirby 
8683b2aab18SMatthew Ahrens void
8693b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
870842727c2SChris Kirby {
8713b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
8723b2aab18SMatthew Ahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
8733b2aab18SMatthew Ahrens 	if (ds == NULL) {
8743b2aab18SMatthew Ahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
8753b2aab18SMatthew Ahrens 	} else {
8763b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
8773b2aab18SMatthew Ahrens 		ds->ds_phys->ds_bp = *bp;
878842727c2SChris Kirby 	}
8793b2aab18SMatthew Ahrens }
880842727c2SChris Kirby 
8813b2aab18SMatthew Ahrens spa_t *
8823b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
8833b2aab18SMatthew Ahrens {
8843b2aab18SMatthew Ahrens 	return (ds->ds_dir->dd_pool->dp_spa);
885842727c2SChris Kirby }
886842727c2SChris Kirby 
8873b2aab18SMatthew Ahrens void
8883b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
889fa9e4066Sahrens {
8903b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
891842727c2SChris Kirby 
8923b2aab18SMatthew Ahrens 	if (ds == NULL) /* this is the meta-objset */
8933b2aab18SMatthew Ahrens 		return;
8941d452cf5Sahrens 
8953b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
896fa9e4066Sahrens 
8973b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
8983b2aab18SMatthew Ahrens 		panic("dirtying snapshot!");
899fa9e4066Sahrens 
9003b2aab18SMatthew Ahrens 	dp = ds->ds_dir->dd_pool;
901ce636f8bSMatthew Ahrens 
9023b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
9033b2aab18SMatthew Ahrens 		/* up the hold count until we can be written out */
9043b2aab18SMatthew Ahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
9053b2aab18SMatthew Ahrens 	}
9063b2aab18SMatthew Ahrens }
907fa9e4066Sahrens 
9082e2c1355SMatthew Ahrens boolean_t
9092e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
9102e2c1355SMatthew Ahrens {
9112e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
9122e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
9132e2c1355SMatthew Ahrens 		    ds, t))
9142e2c1355SMatthew Ahrens 			return (B_TRUE);
9152e2c1355SMatthew Ahrens 	}
9162e2c1355SMatthew Ahrens 	return (B_FALSE);
9172e2c1355SMatthew Ahrens }
9182e2c1355SMatthew Ahrens 
919fa9e4066Sahrens static int
9203b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
921fa9e4066Sahrens {
9223b2aab18SMatthew Ahrens 	uint64_t asize;
923fa9e4066Sahrens 
9243b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
92588b7b0f2SMatthew Ahrens 		return (0);
926fa9e4066Sahrens 
927e1930233Sbonwick 	/*
9283b2aab18SMatthew Ahrens 	 * If there's an fs-only reservation, any blocks that might become
9293b2aab18SMatthew Ahrens 	 * owned by the snapshot dataset must be accommodated by space
9303b2aab18SMatthew Ahrens 	 * outside of the reservation.
931e1930233Sbonwick 	 */
9323b2aab18SMatthew Ahrens 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
9333b2aab18SMatthew Ahrens 	asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
9343b2aab18SMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
935be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
936e1930233Sbonwick 
9373cb34c60Sahrens 	/*
9383b2aab18SMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
9393b2aab18SMatthew Ahrens 	 * snapshot checks in this sync group.
9403cb34c60Sahrens 	 */
9413b2aab18SMatthew Ahrens 	if (asize > 0)
9423b2aab18SMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
9433cb34c60Sahrens 
944e1930233Sbonwick 	return (0);
945e1930233Sbonwick }
946e1930233Sbonwick 
9473b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg {
9483b2aab18SMatthew Ahrens 	nvlist_t *ddsa_snaps;
9493b2aab18SMatthew Ahrens 	nvlist_t *ddsa_props;
9503b2aab18SMatthew Ahrens 	nvlist_t *ddsa_errors;
951a2afb611SJerry Jelinek 	cred_t *ddsa_cr;
9523b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t;
953842727c2SChris Kirby 
9543cb34c60Sahrens int
9553b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
956a2afb611SJerry Jelinek     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
9571d452cf5Sahrens {
9583b2aab18SMatthew Ahrens 	int error;
9593b2aab18SMatthew Ahrens 	uint64_t value;
960fa9e4066Sahrens 
9613b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
962745cd3c5Smaybee 
9633b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
964842727c2SChris Kirby 		return (0);
965fa9e4066Sahrens 
966fa9e4066Sahrens 	/*
9673b2aab18SMatthew Ahrens 	 * We don't allow multiple snapshots of the same txg.  If there
9683b2aab18SMatthew Ahrens 	 * is already one, try again.
969fa9e4066Sahrens 	 */
9703b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
971be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
972fa9e4066Sahrens 
973fa9e4066Sahrens 	/*
9743b2aab18SMatthew Ahrens 	 * Check for conflicting snapshot name.
975fa9e4066Sahrens 	 */
9763b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
9773b2aab18SMatthew Ahrens 	if (error == 0)
978be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
9793b2aab18SMatthew Ahrens 	if (error != ENOENT)
9803b2aab18SMatthew Ahrens 		return (error);
981842727c2SChris Kirby 
982ca48f36fSKeith M Wesolowski 	/*
983ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
984ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
985ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
986ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
987ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
988ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
989ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
990ca48f36fSKeith M Wesolowski 	 */
991ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
992ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
993ca48f36fSKeith M Wesolowski 
994a2afb611SJerry Jelinek 	/*
995a2afb611SJerry Jelinek 	 * Skip the check for temporary snapshots or if we have already checked
996a2afb611SJerry Jelinek 	 * the counts in dsl_dataset_snapshot_check. This means we really only
997a2afb611SJerry Jelinek 	 * check the count here when we're receiving a stream.
998a2afb611SJerry Jelinek 	 */
999a2afb611SJerry Jelinek 	if (cnt != 0 && cr != NULL) {
1000a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1001a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1002a2afb611SJerry Jelinek 		if (error != 0)
1003a2afb611SJerry Jelinek 			return (error);
1004a2afb611SJerry Jelinek 	}
1005a2afb611SJerry Jelinek 
10063b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
10073b2aab18SMatthew Ahrens 	if (error != 0)
10083b2aab18SMatthew Ahrens 		return (error);
1009842727c2SChris Kirby 
10101d452cf5Sahrens 	return (0);
10111d452cf5Sahrens }
10121d452cf5Sahrens 
10133b2aab18SMatthew Ahrens static int
10143b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1015745cd3c5Smaybee {
10163b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
10173b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
10183b2aab18SMatthew Ahrens 	nvpair_t *pair;
10193b2aab18SMatthew Ahrens 	int rv = 0;
10203b2aab18SMatthew Ahrens 
1021a2afb611SJerry Jelinek 	/*
1022a2afb611SJerry Jelinek 	 * Pre-compute how many total new snapshots will be created for each
1023a2afb611SJerry Jelinek 	 * level in the tree and below. This is needed for validating the
1024a2afb611SJerry Jelinek 	 * snapshot limit when either taking a recursive snapshot or when
1025a2afb611SJerry Jelinek 	 * taking multiple snapshots.
1026a2afb611SJerry Jelinek 	 *
1027a2afb611SJerry Jelinek 	 * The problem is that the counts are not actually adjusted when
1028a2afb611SJerry Jelinek 	 * we are checking, only when we finally sync. For a single snapshot,
1029a2afb611SJerry Jelinek 	 * this is easy, the count will increase by 1 at each node up the tree,
1030a2afb611SJerry Jelinek 	 * but its more complicated for the recursive/multiple snapshot case.
1031a2afb611SJerry Jelinek 	 *
1032a2afb611SJerry Jelinek 	 * The dsl_fs_ss_limit_check function does recursively check the count
1033a2afb611SJerry Jelinek 	 * at each level up the tree but since it is validating each snapshot
1034a2afb611SJerry Jelinek 	 * independently we need to be sure that we are validating the complete
1035a2afb611SJerry Jelinek 	 * count for the entire set of snapshots. We do this by rolling up the
1036a2afb611SJerry Jelinek 	 * counts for each component of the name into an nvlist and then
1037a2afb611SJerry Jelinek 	 * checking each of those cases with the aggregated count.
1038a2afb611SJerry Jelinek 	 *
1039a2afb611SJerry Jelinek 	 * This approach properly handles not only the recursive snapshot
1040a2afb611SJerry Jelinek 	 * case (where we get all of those on the ddsa_snaps list) but also
1041a2afb611SJerry Jelinek 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1042a2afb611SJerry Jelinek 	 * validate the limit on 'a' using a count of 2).
1043a2afb611SJerry Jelinek 	 *
1044a2afb611SJerry Jelinek 	 * We validate the snapshot names in the third loop and only report
1045a2afb611SJerry Jelinek 	 * name errors once.
1046a2afb611SJerry Jelinek 	 */
1047a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1048a2afb611SJerry Jelinek 		nvlist_t *cnt_track = NULL;
1049a2afb611SJerry Jelinek 		cnt_track = fnvlist_alloc();
1050a2afb611SJerry Jelinek 
1051a2afb611SJerry Jelinek 		/* Rollup aggregated counts into the cnt_track list */
1052a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1053a2afb611SJerry Jelinek 		    pair != NULL;
1054a2afb611SJerry Jelinek 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1055a2afb611SJerry Jelinek 			char *pdelim;
1056a2afb611SJerry Jelinek 			uint64_t val;
1057a2afb611SJerry Jelinek 			char nm[MAXPATHLEN];
1058a2afb611SJerry Jelinek 
1059a2afb611SJerry Jelinek 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1060a2afb611SJerry Jelinek 			pdelim = strchr(nm, '@');
1061a2afb611SJerry Jelinek 			if (pdelim == NULL)
1062a2afb611SJerry Jelinek 				continue;
1063a2afb611SJerry Jelinek 			*pdelim = '\0';
1064a2afb611SJerry Jelinek 
1065a2afb611SJerry Jelinek 			do {
1066a2afb611SJerry Jelinek 				if (nvlist_lookup_uint64(cnt_track, nm,
1067a2afb611SJerry Jelinek 				    &val) == 0) {
1068a2afb611SJerry Jelinek 					/* update existing entry */
1069a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm,
1070a2afb611SJerry Jelinek 					    val + 1);
1071a2afb611SJerry Jelinek 				} else {
1072a2afb611SJerry Jelinek 					/* add to list */
1073a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm, 1);
1074a2afb611SJerry Jelinek 				}
1075a2afb611SJerry Jelinek 
1076a2afb611SJerry Jelinek 				pdelim = strrchr(nm, '/');
1077a2afb611SJerry Jelinek 				if (pdelim != NULL)
1078a2afb611SJerry Jelinek 					*pdelim = '\0';
1079a2afb611SJerry Jelinek 			} while (pdelim != NULL);
1080a2afb611SJerry Jelinek 		}
1081a2afb611SJerry Jelinek 
1082a2afb611SJerry Jelinek 		/* Check aggregated counts at each level */
1083a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1084a2afb611SJerry Jelinek 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1085a2afb611SJerry Jelinek 			int error = 0;
1086a2afb611SJerry Jelinek 			char *name;
1087a2afb611SJerry Jelinek 			uint64_t cnt = 0;
1088a2afb611SJerry Jelinek 			dsl_dataset_t *ds;
1089a2afb611SJerry Jelinek 
1090a2afb611SJerry Jelinek 			name = nvpair_name(pair);
1091a2afb611SJerry Jelinek 			cnt = fnvpair_value_uint64(pair);
1092a2afb611SJerry Jelinek 			ASSERT(cnt > 0);
1093a2afb611SJerry Jelinek 
1094a2afb611SJerry Jelinek 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1095a2afb611SJerry Jelinek 			if (error == 0) {
1096a2afb611SJerry Jelinek 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1097a2afb611SJerry Jelinek 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1098a2afb611SJerry Jelinek 				    ddsa->ddsa_cr);
1099a2afb611SJerry Jelinek 				dsl_dataset_rele(ds, FTAG);
1100a2afb611SJerry Jelinek 			}
1101a2afb611SJerry Jelinek 
1102a2afb611SJerry Jelinek 			if (error != 0) {
1103a2afb611SJerry Jelinek 				if (ddsa->ddsa_errors != NULL)
1104a2afb611SJerry Jelinek 					fnvlist_add_int32(ddsa->ddsa_errors,
1105a2afb611SJerry Jelinek 					    name, error);
1106a2afb611SJerry Jelinek 				rv = error;
1107a2afb611SJerry Jelinek 				/* only report one error for this check */
1108a2afb611SJerry Jelinek 				break;
1109a2afb611SJerry Jelinek 			}
1110a2afb611SJerry Jelinek 		}
1111a2afb611SJerry Jelinek 		nvlist_free(cnt_track);
1112a2afb611SJerry Jelinek 	}
1113a2afb611SJerry Jelinek 
11143b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
11153b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
11163b2aab18SMatthew Ahrens 		int error = 0;
11173b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
11183b2aab18SMatthew Ahrens 		char *name, *atp;
11193b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
11203b2aab18SMatthew Ahrens 
11213b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
11223b2aab18SMatthew Ahrens 		if (strlen(name) >= MAXNAMELEN)
1123be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
11243b2aab18SMatthew Ahrens 		if (error == 0) {
11253b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
11263b2aab18SMatthew Ahrens 			if (atp == NULL)
1127be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
11283b2aab18SMatthew Ahrens 			if (error == 0)
11293b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
11303b2aab18SMatthew Ahrens 		}
11313b2aab18SMatthew Ahrens 		if (error == 0)
11323b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
11333b2aab18SMatthew Ahrens 		if (error == 0) {
1134a2afb611SJerry Jelinek 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
11353b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
1136a2afb611SJerry Jelinek 			    atp + 1, tx, B_FALSE, 0, NULL);
11373b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
11383b2aab18SMatthew Ahrens 		}
1139745cd3c5Smaybee 
11403b2aab18SMatthew Ahrens 		if (error != 0) {
11413b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
11423b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
11433b2aab18SMatthew Ahrens 				    name, error);
11443b2aab18SMatthew Ahrens 			}
11453b2aab18SMatthew Ahrens 			rv = error;
11463b2aab18SMatthew Ahrens 		}
11473b2aab18SMatthew Ahrens 	}
1148a2afb611SJerry Jelinek 
11493b2aab18SMatthew Ahrens 	return (rv);
1150745cd3c5Smaybee }
1151745cd3c5Smaybee 
11523b2aab18SMatthew Ahrens void
11533b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
11543b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1155745cd3c5Smaybee {
11563b2aab18SMatthew Ahrens 	static zil_header_t zero_zil;
1157745cd3c5Smaybee 
11583b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
11593b2aab18SMatthew Ahrens 	dmu_buf_t *dbuf;
11603b2aab18SMatthew Ahrens 	dsl_dataset_phys_t *dsphys;
11613b2aab18SMatthew Ahrens 	uint64_t dsobj, crtxg;
11623b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
11633b2aab18SMatthew Ahrens 	objset_t *os;
1164745cd3c5Smaybee 
11653b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1166c33e334fSMatthew Ahrens 
1167c33e334fSMatthew Ahrens 	/*
11683b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
11693b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1170c33e334fSMatthew Ahrens 	 */
11713b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
11723b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
11733b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
11743b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
1175c33e334fSMatthew Ahrens 
1176a2afb611SJerry Jelinek 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1177cde58dbcSMatthew Ahrens 
1178cde58dbcSMatthew Ahrens 	/*
11793b2aab18SMatthew Ahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1180088f3894Sahrens 	 */
1181088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1182088f3894Sahrens 		crtxg = 1;
1183088f3894Sahrens 	else
1184088f3894Sahrens 		crtxg = tx->tx_txg;
1185088f3894Sahrens 
11861649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
11871649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
11883b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1189fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1190fa9e4066Sahrens 	dsphys = dbuf->db_data;
1191745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
11921d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1193fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1194fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1195fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
1196fa9e4066Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1197fa9e4066Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1198fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1199fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1200fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1201088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
1202fa9e4066Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1203ad135b5dSChristopher Siden 	dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1204fa9e4066Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1205fa9e4066Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
120699653d4eSeschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1207fa9e4066Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
1208ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1209fa9e4066Sahrens 
12101d452cf5Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
12111d452cf5Sahrens 	if (ds->ds_prev) {
1212088f3894Sahrens 		uint64_t next_clones_obj =
1213088f3894Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
12141d452cf5Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1215fa9e4066Sahrens 		    ds->ds_object ||
12161d452cf5Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
12171d452cf5Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
12181d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1219fa9e4066Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
12201d452cf5Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
12211d452cf5Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1222088f3894Sahrens 		} else if (next_clones_obj != 0) {
12233b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1224c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
12253b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1226088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1227fa9e4066Sahrens 		}
1228fa9e4066Sahrens 	}
1229fa9e4066Sahrens 
1230a9799022Sck 	/*
1231a9799022Sck 	 * If we have a reference-reservation on this dataset, we will
1232a9799022Sck 	 * need to increase the amount of refreservation being charged
1233a9799022Sck 	 * since our unique space is going to zero.
1234a9799022Sck 	 */
1235a9799022Sck 	if (ds->ds_reserved) {
12363f9d6ad7SLin Ling 		int64_t delta;
12373f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
12383f9d6ad7SLin Ling 		delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
123974e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
12403f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1241a9799022Sck 	}
1242a9799022Sck 
1243fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1244cde58dbcSMatthew Ahrens 	ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1245cde58dbcSMatthew Ahrens 	    UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1246cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1247cde58dbcSMatthew Ahrens 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1248cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
1249cde58dbcSMatthew Ahrens 	    ds->ds_phys->ds_prev_snap_txg, tx);
1250cde58dbcSMatthew Ahrens 
1251a4611edeSahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1252fa9e4066Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
1253088f3894Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1254fa9e4066Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
1255a9799022Sck 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1256a9799022Sck 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1257fa9e4066Sahrens 
12583b2aab18SMatthew Ahrens 	VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
12593b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1260fa9e4066Sahrens 
1261fa9e4066Sahrens 	if (ds->ds_prev)
12623b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
12633b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
1264745cd3c5Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1265ecd6cf80Smarks 
12663f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1267088f3894Sahrens 
126871eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
126971eb0538SChris Kirby 
12704445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1271fa9e4066Sahrens }
1272fa9e4066Sahrens 
12733b2aab18SMatthew Ahrens static void
12743b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1275fa9e4066Sahrens {
12763b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
12773b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
12783b2aab18SMatthew Ahrens 	nvpair_t *pair;
127991ebeef5Sahrens 
12803b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
12813b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
12823b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
12833b2aab18SMatthew Ahrens 		char *name, *atp;
12843b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
12853b2aab18SMatthew Ahrens 
12863b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
12873b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
12883b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
12893b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
12903b2aab18SMatthew Ahrens 
12913b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
12923b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
12933b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
12943b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
12953b2aab18SMatthew Ahrens 		}
12963b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
12973b2aab18SMatthew Ahrens 	}
1298fa9e4066Sahrens }
1299fa9e4066Sahrens 
13003b2aab18SMatthew Ahrens /*
13013b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
13023b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
13033b2aab18SMatthew Ahrens  */
13043b2aab18SMatthew Ahrens int
13053b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
130619b94df9SMatthew Ahrens {
13073b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
13083b2aab18SMatthew Ahrens 	nvpair_t *pair;
13093b2aab18SMatthew Ahrens 	boolean_t needsuspend;
13103b2aab18SMatthew Ahrens 	int error;
13113b2aab18SMatthew Ahrens 	spa_t *spa;
13123b2aab18SMatthew Ahrens 	char *firstname;
13133b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
131419b94df9SMatthew Ahrens 
13153b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
13163b2aab18SMatthew Ahrens 	if (pair == NULL)
13173b2aab18SMatthew Ahrens 		return (0);
13183b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
13193b2aab18SMatthew Ahrens 
13203b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
13213b2aab18SMatthew Ahrens 	if (error != 0)
13223b2aab18SMatthew Ahrens 		return (error);
13233b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
13243b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
13253b2aab18SMatthew Ahrens 
13263b2aab18SMatthew Ahrens 	if (needsuspend) {
13273b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
13283b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
13293b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
13303b2aab18SMatthew Ahrens 			char fsname[MAXNAMELEN];
13313b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
13323b2aab18SMatthew Ahrens 			char *atp;
13333b2aab18SMatthew Ahrens 			void *cookie;
13343b2aab18SMatthew Ahrens 
13353b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
13363b2aab18SMatthew Ahrens 			if (atp == NULL) {
1337be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
13383b2aab18SMatthew Ahrens 				break;
13393b2aab18SMatthew Ahrens 			}
13403b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
13413b2aab18SMatthew Ahrens 
13423b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
13433b2aab18SMatthew Ahrens 			if (error != 0)
13443b2aab18SMatthew Ahrens 				break;
13453b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
13463b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
13473b2aab18SMatthew Ahrens 		}
13483b2aab18SMatthew Ahrens 	}
13493b2aab18SMatthew Ahrens 
13503b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
13513b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
13523b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
1353a2afb611SJerry Jelinek 	ddsa.ddsa_cr = CRED();
13543b2aab18SMatthew Ahrens 
13553b2aab18SMatthew Ahrens 	if (error == 0) {
13563b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
13573b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
13587d46dc6cSMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
13593b2aab18SMatthew Ahrens 	}
13603b2aab18SMatthew Ahrens 
13613b2aab18SMatthew Ahrens 	if (suspended != NULL) {
13623b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
13633b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
13643b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
13653b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
13663b2aab18SMatthew Ahrens 		}
13673b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
13683b2aab18SMatthew Ahrens 	}
13693b2aab18SMatthew Ahrens 
13703b2aab18SMatthew Ahrens 	return (error);
13713b2aab18SMatthew Ahrens }
13723b2aab18SMatthew Ahrens 
13733b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
13743b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
13753b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
13763b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
13773b2aab18SMatthew Ahrens 	const char *ddsta_htag;
13783b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
13793b2aab18SMatthew Ahrens 
13803b2aab18SMatthew Ahrens static int
13813b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
13823b2aab18SMatthew Ahrens {
13833b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
13843b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
13853b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
13863b2aab18SMatthew Ahrens 	int error;
13873b2aab18SMatthew Ahrens 
13883b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
13893b2aab18SMatthew Ahrens 	if (error != 0)
13903b2aab18SMatthew Ahrens 		return (error);
13913b2aab18SMatthew Ahrens 
1392a2afb611SJerry Jelinek 	/* NULL cred means no limit check for tmp snapshot */
1393ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1394a2afb611SJerry Jelinek 	    tx, B_FALSE, 0, NULL);
13953b2aab18SMatthew Ahrens 	if (error != 0) {
13963b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13973b2aab18SMatthew Ahrens 		return (error);
13983b2aab18SMatthew Ahrens 	}
13993b2aab18SMatthew Ahrens 
14003b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
14013b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1402be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
14033b2aab18SMatthew Ahrens 	}
14043b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
14053b2aab18SMatthew Ahrens 	    B_TRUE, tx);
14063b2aab18SMatthew Ahrens 	if (error != 0) {
14073b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14083b2aab18SMatthew Ahrens 		return (error);
14093b2aab18SMatthew Ahrens 	}
14103b2aab18SMatthew Ahrens 
14113b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14123b2aab18SMatthew Ahrens 	return (0);
14133b2aab18SMatthew Ahrens }
14143b2aab18SMatthew Ahrens 
14153b2aab18SMatthew Ahrens static void
14163b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
14173b2aab18SMatthew Ahrens {
14183b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
14193b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14203b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14213b2aab18SMatthew Ahrens 
14223b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
14233b2aab18SMatthew Ahrens 
14243b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
14253b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
14263b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
14273b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
14283b2aab18SMatthew Ahrens 
14293b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14303b2aab18SMatthew Ahrens }
14313b2aab18SMatthew Ahrens 
14323b2aab18SMatthew Ahrens int
14333b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
14343b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
14353b2aab18SMatthew Ahrens {
14363b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
14373b2aab18SMatthew Ahrens 	int error;
14383b2aab18SMatthew Ahrens 	spa_t *spa;
14393b2aab18SMatthew Ahrens 	boolean_t needsuspend;
14403b2aab18SMatthew Ahrens 	void *cookie;
14413b2aab18SMatthew Ahrens 
14423b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
14433b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
14443b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
14453b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
14463b2aab18SMatthew Ahrens 
14473b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
14483b2aab18SMatthew Ahrens 	if (error != 0)
14493b2aab18SMatthew Ahrens 		return (error);
14503b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
14513b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
14523b2aab18SMatthew Ahrens 
14533b2aab18SMatthew Ahrens 	if (needsuspend) {
14543b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
14553b2aab18SMatthew Ahrens 		if (error != 0)
14563b2aab18SMatthew Ahrens 			return (error);
14573b2aab18SMatthew Ahrens 	}
14583b2aab18SMatthew Ahrens 
14593b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
14607d46dc6cSMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
14613b2aab18SMatthew Ahrens 
14623b2aab18SMatthew Ahrens 	if (needsuspend)
14633b2aab18SMatthew Ahrens 		zil_resume(cookie);
14643b2aab18SMatthew Ahrens 	return (error);
14653b2aab18SMatthew Ahrens }
14663b2aab18SMatthew Ahrens 
14673b2aab18SMatthew Ahrens 
14683b2aab18SMatthew Ahrens void
14693b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
14703b2aab18SMatthew Ahrens {
14713b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
14723b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
14733b2aab18SMatthew Ahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
14743b2aab18SMatthew Ahrens 
14753b2aab18SMatthew Ahrens 	/*
14763b2aab18SMatthew Ahrens 	 * in case we had to change ds_fsid_guid when we opened it,
14773b2aab18SMatthew Ahrens 	 * sync it out now.
14783b2aab18SMatthew Ahrens 	 */
14793b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
14803b2aab18SMatthew Ahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
14813b2aab18SMatthew Ahrens 
14823b2aab18SMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
14833b2aab18SMatthew Ahrens }
14843b2aab18SMatthew Ahrens 
14853b2aab18SMatthew Ahrens static void
14863b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
14873b2aab18SMatthew Ahrens {
14883b2aab18SMatthew Ahrens 	uint64_t count = 0;
14893b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
14903b2aab18SMatthew Ahrens 	zap_cursor_t zc;
14913b2aab18SMatthew Ahrens 	zap_attribute_t za;
14923b2aab18SMatthew Ahrens 	nvlist_t *propval = fnvlist_alloc();
14933b2aab18SMatthew Ahrens 	nvlist_t *val = fnvlist_alloc();
14943b2aab18SMatthew Ahrens 
14953b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
149619b94df9SMatthew Ahrens 
149719b94df9SMatthew Ahrens 	/*
14983b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
149919b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
150019b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
150119b94df9SMatthew Ahrens 	 */
150219b94df9SMatthew Ahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
150303d1795fSAlexander Stetsenko 		VERIFY0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
150419b94df9SMatthew Ahrens 		    &count));
150519b94df9SMatthew Ahrens 	}
15063b2aab18SMatthew Ahrens 	if (count != ds->ds_phys->ds_num_children - 1)
150719b94df9SMatthew Ahrens 		goto fail;
150819b94df9SMatthew Ahrens 	for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
150919b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
151019b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
151119b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
151219b94df9SMatthew Ahrens 		char buf[ZFS_MAXNAMELEN];
15133b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
15143b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
151519b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
15163b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
151719b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
151819b94df9SMatthew Ahrens 	}
151919b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
15203b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
15213b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
152219b94df9SMatthew Ahrens fail:
152319b94df9SMatthew Ahrens 	nvlist_free(val);
152419b94df9SMatthew Ahrens 	nvlist_free(propval);
152519b94df9SMatthew Ahrens }
152619b94df9SMatthew Ahrens 
1527fa9e4066Sahrens void
1528a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1529fa9e4066Sahrens {
15303b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1531187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
1532a9799022Sck 
15333b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
15343b2aab18SMatthew Ahrens 
15354445fffbSMatthew Ahrens 	ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
15364445fffbSMatthew Ahrens 	    (ds->ds_phys->ds_uncompressed_bytes * 100 /
15374445fffbSMatthew Ahrens 	    ds->ds_phys->ds_compressed_bytes);
15384445fffbSMatthew Ahrens 
15394445fffbSMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
154077372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
154177372cb0SMatthew Ahrens 	    ds->ds_phys->ds_uncompressed_bytes);
15424445fffbSMatthew Ahrens 
15434445fffbSMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
15444445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
15454445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
15464445fffbSMatthew Ahrens 		    ds->ds_phys->ds_unique_bytes);
15474445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
15484445fffbSMatthew Ahrens 	} else {
1549b461c746SMatthew Ahrens 		if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
1550b461c746SMatthew Ahrens 			char buf[MAXNAMELEN];
1551b461c746SMatthew Ahrens 			dsl_dataset_name(ds->ds_prev, buf);
1552b461c746SMatthew Ahrens 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1553b461c746SMatthew Ahrens 		}
1554b461c746SMatthew Ahrens 
15554445fffbSMatthew Ahrens 		dsl_dir_stats(ds->ds_dir, nv);
15564445fffbSMatthew Ahrens 	}
1557fa9e4066Sahrens 
1558a9799022Sck 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1559a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1560a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1561a9799022Sck 
1562a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1563a2eea2e1Sahrens 	    ds->ds_phys->ds_creation_time);
1564a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1565a2eea2e1Sahrens 	    ds->ds_phys->ds_creation_txg);
1566a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1567a9799022Sck 	    ds->ds_quota);
1568a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1569a9799022Sck 	    ds->ds_reserved);
1570c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1571c5904d13Seschrock 	    ds->ds_phys->ds_guid);
15721d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
15733f9d6ad7SLin Ling 	    ds->ds_phys->ds_unique_bytes);
15741d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
15751d713200SEric Schrock 	    ds->ds_object);
157692241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
157792241e0bSTom Erickson 	    ds->ds_userrefs);
1578842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1579842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1580fa9e4066Sahrens 
158119b94df9SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
158219b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
158319b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
158419b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
158519b94df9SMatthew Ahrens 
158619b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
158719b94df9SMatthew Ahrens 		    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
158819b94df9SMatthew Ahrens 		if (err == 0) {
158919b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
159019b94df9SMatthew Ahrens 			    &comp, &uncomp);
159119b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
159219b94df9SMatthew Ahrens 			if (err == 0) {
159319b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
159419b94df9SMatthew Ahrens 				    written);
159519b94df9SMatthew Ahrens 			}
159619b94df9SMatthew Ahrens 		}
159719b94df9SMatthew Ahrens 	}
1598fa9e4066Sahrens }
1599fa9e4066Sahrens 
1600a2eea2e1Sahrens void
1601a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1602a2eea2e1Sahrens {
16033b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
16043b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
16053b2aab18SMatthew Ahrens 
1606a2eea2e1Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1607a2eea2e1Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
16083cb34c60Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
16094445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
16104445fffbSMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
1611a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
1612a2eea2e1Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1613ebedde84SEric Taylor 	} else {
1614ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
1615ebedde84SEric Taylor 		stat->dds_num_clones = 0;
1616a2eea2e1Sahrens 
16174445fffbSMatthew Ahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
16184445fffbSMatthew Ahrens 			dsl_dataset_t *ods;
1619a2eea2e1Sahrens 
16203b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
16214445fffbSMatthew Ahrens 			    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
16224445fffbSMatthew Ahrens 			dsl_dataset_name(ods, stat->dds_origin);
16233b2aab18SMatthew Ahrens 			dsl_dataset_rele(ods, FTAG);
16244445fffbSMatthew Ahrens 		}
1625a2eea2e1Sahrens 	}
1626a2eea2e1Sahrens }
1627a2eea2e1Sahrens 
1628a2eea2e1Sahrens uint64_t
1629a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1630a2eea2e1Sahrens {
163191ebeef5Sahrens 	return (ds->ds_fsid_guid);
1632a2eea2e1Sahrens }
1633a2eea2e1Sahrens 
1634a2eea2e1Sahrens void
1635a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
1636a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
1637a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1638fa9e4066Sahrens {
1639ad135b5dSChristopher Siden 	*refdbytesp = ds->ds_phys->ds_referenced_bytes;
1640a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1641a9799022Sck 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1642a9799022Sck 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1643a9799022Sck 	if (ds->ds_quota != 0) {
1644a9799022Sck 		/*
1645a9799022Sck 		 * Adjust available bytes according to refquota
1646a9799022Sck 		 */
1647a9799022Sck 		if (*refdbytesp < ds->ds_quota)
1648a9799022Sck 			*availbytesp = MIN(*availbytesp,
1649a9799022Sck 			    ds->ds_quota - *refdbytesp);
1650a9799022Sck 		else
1651a9799022Sck 			*availbytesp = 0;
1652a9799022Sck 	}
16535d7b4d43SMatthew Ahrens 	*usedobjsp = BP_GET_FILL(&ds->ds_phys->ds_bp);
1654a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1655fa9e4066Sahrens }
1656fa9e4066Sahrens 
1657f18faf3fSek boolean_t
165834f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1659f18faf3fSek {
1660f18faf3fSek 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1661f18faf3fSek 
16623b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
166334f2f8cfSMatthew Ahrens 	if (snap == NULL)
1664f18faf3fSek 		return (B_FALSE);
1665f18faf3fSek 	if (ds->ds_phys->ds_bp.blk_birth >
166634f2f8cfSMatthew Ahrens 	    snap->ds_phys->ds_creation_txg) {
166734f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
16686e0cbcaaSMatthew Ahrens 		/*
16696e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
16706e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
16716e0cbcaaSMatthew Ahrens 		 * modified.
16726e0cbcaaSMatthew Ahrens 		 */
16736e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
16746e0cbcaaSMatthew Ahrens 			return (B_TRUE);
167534f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
16766e0cbcaaSMatthew Ahrens 			return (B_TRUE);
16776e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
167834f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
16796e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
16806e0cbcaaSMatthew Ahrens 	}
1681f18faf3fSek 	return (B_FALSE);
1682f18faf3fSek }
1683f18faf3fSek 
16843b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
16853b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
16863b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
16873b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
16883b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
16893b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
16903b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
16913b2aab18SMatthew Ahrens 
16921d452cf5Sahrens /* ARGSUSED */
1693fa9e4066Sahrens static int
16943b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
16953b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1696fa9e4066Sahrens {
16973b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
16983b2aab18SMatthew Ahrens 	int error;
1699fa9e4066Sahrens 	uint64_t val;
1700fa9e4066Sahrens 
17013b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
17023b2aab18SMatthew Ahrens 	if (error != 0) {
17033b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
17043b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
17053b2aab18SMatthew Ahrens 	}
17061d452cf5Sahrens 
17073b2aab18SMatthew Ahrens 	/* new name should not exist */
17083b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
17093b2aab18SMatthew Ahrens 	if (error == 0)
1710be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
17113b2aab18SMatthew Ahrens 	else if (error == ENOENT)
17123b2aab18SMatthew Ahrens 		error = 0;
1713cdf5b4caSmmusante 
1714cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
17153b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
17163b2aab18SMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1717be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
1718cdf5b4caSmmusante 
17193b2aab18SMatthew Ahrens 	return (error);
17201d452cf5Sahrens }
1721fa9e4066Sahrens 
17223b2aab18SMatthew Ahrens static int
17233b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
17241d452cf5Sahrens {
17253b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
17263b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17271d452cf5Sahrens 	dsl_dataset_t *hds;
17283b2aab18SMatthew Ahrens 	int error;
1729fa9e4066Sahrens 
17303b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
17313b2aab18SMatthew Ahrens 	if (error != 0)
17323b2aab18SMatthew Ahrens 		return (error);
1733fa9e4066Sahrens 
17343b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
17353b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
17363b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
17373b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
17383b2aab18SMatthew Ahrens 	} else {
17393b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
17403b2aab18SMatthew Ahrens 	}
1741745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
17423b2aab18SMatthew Ahrens 	return (error);
1743fa9e4066Sahrens }
1744fa9e4066Sahrens 
1745cdf5b4caSmmusante static int
17463b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
17473b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1748cdf5b4caSmmusante {
17493b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
17503b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
17513b2aab18SMatthew Ahrens 	uint64_t val;
17523b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
17533b2aab18SMatthew Ahrens 	int error;
1754ecd6cf80Smarks 
17553b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
17563b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
17573b2aab18SMatthew Ahrens 	if (error == ENOENT) {
17583b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
17593b2aab18SMatthew Ahrens 		return (0);
1760ecd6cf80Smarks 	}
1761ecd6cf80Smarks 
17623b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
17633b2aab18SMatthew Ahrens 
17643b2aab18SMatthew Ahrens 	/* log before we change the name */
17653b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
17663b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
1767cdf5b4caSmmusante 
1768a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
1769a2afb611SJerry Jelinek 	    B_FALSE));
17703b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
17713b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
17723b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
17733b2aab18SMatthew Ahrens 	VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
17743b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1775cdf5b4caSmmusante 
17763b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1777cdf5b4caSmmusante 	return (0);
1778cdf5b4caSmmusante }
1779cdf5b4caSmmusante 
17803b2aab18SMatthew Ahrens static void
17813b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1782cdf5b4caSmmusante {
17833b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
17843b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17853b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
1786cdf5b4caSmmusante 
17873b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
17883b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
17893b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
17903b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
17913b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
17923b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
17933b2aab18SMatthew Ahrens 	} else {
17943b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1795cdf5b4caSmmusante 	}
17963b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
1797cdf5b4caSmmusante }
1798cdf5b4caSmmusante 
17993b2aab18SMatthew Ahrens int
18003b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
18013b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
18023a5a36beSmmusante {
18033b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
18043a5a36beSmmusante 
18053b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
18063b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
18073b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
18083b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
18093a5a36beSmmusante 
18103b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
18117d46dc6cSMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
18127d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
18133a5a36beSmmusante }
18143a5a36beSmmusante 
181591948b51SKeith M Wesolowski /*
181691948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
181791948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
181891948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
181991948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
182091948b51SKeith M Wesolowski  * when we release the long hold.
182191948b51SKeith M Wesolowski  */
182291948b51SKeith M Wesolowski static int
182391948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
182491948b51SKeith M Wesolowski {
182591948b51SKeith M Wesolowski 	boolean_t held;
182691948b51SKeith M Wesolowski 
182791948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
182891948b51SKeith M Wesolowski 		return (0);
182991948b51SKeith M Wesolowski 
183091948b51SKeith M Wesolowski 	if (owner != NULL) {
183191948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
183291948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
183391948b51SKeith M Wesolowski 	}
183491948b51SKeith M Wesolowski 
183591948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
183691948b51SKeith M Wesolowski 
183791948b51SKeith M Wesolowski 	if (owner != NULL)
183891948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
183991948b51SKeith M Wesolowski 
184091948b51SKeith M Wesolowski 	if (held)
184191948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
184291948b51SKeith M Wesolowski 
184391948b51SKeith M Wesolowski 	return (0);
184491948b51SKeith M Wesolowski }
184591948b51SKeith M Wesolowski 
184691948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg {
184791948b51SKeith M Wesolowski 	const char *ddra_fsname;
184891948b51SKeith M Wesolowski 	void *ddra_owner;
1849a7027df1SMatthew Ahrens 	nvlist_t *ddra_result;
185091948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t;
185191948b51SKeith M Wesolowski 
18523b2aab18SMatthew Ahrens static int
18533b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1854fa9e4066Sahrens {
185591948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
18563b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18571d452cf5Sahrens 	dsl_dataset_t *ds;
18583b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
18593b2aab18SMatthew Ahrens 	int error;
1860fa9e4066Sahrens 
186191948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
18623b2aab18SMatthew Ahrens 	if (error != 0)
18633b2aab18SMatthew Ahrens 		return (error);
1864370c1af0SSanjeev Bagewadi 
18653b2aab18SMatthew Ahrens 	/* must not be a snapshot */
18663b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
18673b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1868be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
18693b2aab18SMatthew Ahrens 	}
18703a5a36beSmmusante 
18713b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
18723b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
18733b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1874be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
18753b2aab18SMatthew Ahrens 	}
18763a5a36beSmmusante 
187778f17100SMatthew Ahrens 	/* must not have any bookmarks after the most recent snapshot */
187878f17100SMatthew Ahrens 	nvlist_t *proprequest = fnvlist_alloc();
187978f17100SMatthew Ahrens 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
188078f17100SMatthew Ahrens 	nvlist_t *bookmarks = fnvlist_alloc();
188178f17100SMatthew Ahrens 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
188278f17100SMatthew Ahrens 	fnvlist_free(proprequest);
188378f17100SMatthew Ahrens 	if (error != 0)
188478f17100SMatthew Ahrens 		return (error);
188578f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
188678f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
188778f17100SMatthew Ahrens 		nvlist_t *valuenv =
188878f17100SMatthew Ahrens 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
188978f17100SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
189078f17100SMatthew Ahrens 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
189178f17100SMatthew Ahrens 		if (createtxg > ds->ds_phys->ds_prev_snap_txg) {
189278f17100SMatthew Ahrens 			fnvlist_free(bookmarks);
189378f17100SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
189478f17100SMatthew Ahrens 			return (SET_ERROR(EEXIST));
189578f17100SMatthew Ahrens 		}
189678f17100SMatthew Ahrens 	}
189778f17100SMatthew Ahrens 	fnvlist_free(bookmarks);
189878f17100SMatthew Ahrens 
189991948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
190091948b51SKeith M Wesolowski 	if (error != 0) {
19013b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
190291948b51SKeith M Wesolowski 		return (error);
19033b2aab18SMatthew Ahrens 	}
19043b2aab18SMatthew Ahrens 
19053b2aab18SMatthew Ahrens 	/*
19063b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
19073b2aab18SMatthew Ahrens 	 * the refquota.
19083b2aab18SMatthew Ahrens 	 */
19093b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
19103b2aab18SMatthew Ahrens 	    ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
19113b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1912be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
1913fa9e4066Sahrens 	}
1914370c1af0SSanjeev Bagewadi 
19153b2aab18SMatthew Ahrens 	/*
19163b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
19173b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
19183b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
19193b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
19203b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
19213b2aab18SMatthew Ahrens 	 */
19223b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
19233b2aab18SMatthew Ahrens 	    ds->ds_phys->ds_unique_bytes);
19243b2aab18SMatthew Ahrens 
19253b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
19263b2aab18SMatthew Ahrens 	    unused_refres_delta >
19273b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
19283b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1929be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
1930fa9e4066Sahrens 	}
1931fa9e4066Sahrens 
19323b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
19333b2aab18SMatthew Ahrens 	return (0);
19343b2aab18SMatthew Ahrens }
19351d452cf5Sahrens 
19363b2aab18SMatthew Ahrens static void
19373b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
19383b2aab18SMatthew Ahrens {
193991948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
19403b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
19413b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
19423b2aab18SMatthew Ahrens 	uint64_t cloneobj;
1943a7027df1SMatthew Ahrens 	char namebuf[ZFS_MAXNAMELEN];
19441d452cf5Sahrens 
194591948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
19461d452cf5Sahrens 
1947a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
1948a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
1949a7027df1SMatthew Ahrens 
19503b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
19513b2aab18SMatthew Ahrens 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
19521d452cf5Sahrens 
19533b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
19541d452cf5Sahrens 
19553b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
19563b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
19573b2aab18SMatthew Ahrens 
19583b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
19593b2aab18SMatthew Ahrens 
19603b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
19613b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
19623b2aab18SMatthew Ahrens }
19633b2aab18SMatthew Ahrens 
196491948b51SKeith M Wesolowski /*
1965a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
1966a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
1967a7027df1SMatthew Ahrens  * in the result nvlist.
196891948b51SKeith M Wesolowski  *
1969a7027df1SMatthew Ahrens  * If owner != NULL:
197091948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
197191948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
197291948b51SKeith M Wesolowski  *   succeed or not.
197391948b51SKeith M Wesolowski  *
197491948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
197591948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
197691948b51SKeith M Wesolowski  */
19773b2aab18SMatthew Ahrens int
1978a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
19793b2aab18SMatthew Ahrens {
198091948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
198191948b51SKeith M Wesolowski 
198291948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
198391948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
1984a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
198591948b51SKeith M Wesolowski 
19863b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
19877d46dc6cSMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra,
19887d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
1989fa9e4066Sahrens }
199099653d4eSeschrock 
1991088f3894Sahrens struct promotenode {
1992745cd3c5Smaybee 	list_node_t link;
1993745cd3c5Smaybee 	dsl_dataset_t *ds;
1994745cd3c5Smaybee };
1995745cd3c5Smaybee 
19963b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg {
19973b2aab18SMatthew Ahrens 	const char *ddpa_clonename;
19983b2aab18SMatthew Ahrens 	dsl_dataset_t *ddpa_clone;
199974e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
20003b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_origin; /* origin of the origin */
200174e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2002681d9761SEric Taylor 	char *err_ds;
2003a2afb611SJerry Jelinek 	cred_t *cr;
20043b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t;
20051d452cf5Sahrens 
200674e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
20073b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
20083b2aab18SMatthew Ahrens     void *tag);
20093b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
201074e7dc98SMatthew Ahrens 
201199653d4eSeschrock static int
20123b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
201399653d4eSeschrock {
20143b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
20153b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20163b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
20173b2aab18SMatthew Ahrens 	struct promotenode *snap;
20183b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
2019745cd3c5Smaybee 	int err;
2020cde58dbcSMatthew Ahrens 	uint64_t unused;
2021a2afb611SJerry Jelinek 	uint64_t ss_mv_cnt;
20221d452cf5Sahrens 
20233b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
20243b2aab18SMatthew Ahrens 	if (err != 0)
20253b2aab18SMatthew Ahrens 		return (err);
202699653d4eSeschrock 
20273b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
20281d452cf5Sahrens 
20293b2aab18SMatthew Ahrens 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
20303b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
2031be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
20323b2aab18SMatthew Ahrens 	}
20333b2aab18SMatthew Ahrens 
20343b2aab18SMatthew Ahrens 	/*
20353b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
20363b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
20373b2aab18SMatthew Ahrens 	 */
20383b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
20393b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
20403b2aab18SMatthew Ahrens 		return (0);
20413b2aab18SMatthew Ahrens 	}
20423b2aab18SMatthew Ahrens 
20433b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
20443b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
204599653d4eSeschrock 
20463cb34c60Sahrens 	/* compute origin's new unique space */
20473b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
204874e7dc98SMatthew Ahrens 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
2049cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2050cde58dbcSMatthew Ahrens 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
20513b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
205299653d4eSeschrock 
2053745cd3c5Smaybee 	/*
2054745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2055745cd3c5Smaybee 	 *
205674e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
20573b2aab18SMatthew Ahrens 	 * to used by each snapshot:
205874e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
205974e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
206074e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2061745cd3c5Smaybee 	 * So a sequence would look like:
206274e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2063745cd3c5Smaybee 	 * Which simplifies to:
206474e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2065745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
206674e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2067745cd3c5Smaybee 	 */
2068a2afb611SJerry Jelinek 	ss_mv_cnt = 0;
20693b2aab18SMatthew Ahrens 	ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
20703b2aab18SMatthew Ahrens 	ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
20713b2aab18SMatthew Ahrens 	ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
20723b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
20733b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
207499653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2075745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
207699653d4eSeschrock 
2077a2afb611SJerry Jelinek 		ss_mv_cnt++;
2078a2afb611SJerry Jelinek 
20793b2aab18SMatthew Ahrens 		/*
20803b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
20813b2aab18SMatthew Ahrens 		 * the objset.
20823b2aab18SMatthew Ahrens 		 */
20833b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
2084be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
20853b2aab18SMatthew Ahrens 			goto out;
20863b2aab18SMatthew Ahrens 		}
20873b2aab18SMatthew Ahrens 
208899653d4eSeschrock 		/* Check that the snapshot name does not conflict */
20893b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
2090745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2091681d9761SEric Taylor 		if (err == 0) {
20923b2aab18SMatthew Ahrens 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2093be6fd75aSMatthew Ahrens 			err = SET_ERROR(EEXIST);
2094681d9761SEric Taylor 			goto out;
2095681d9761SEric Taylor 		}
2096745cd3c5Smaybee 		if (err != ENOENT)
2097681d9761SEric Taylor 			goto out;
209899653d4eSeschrock 
2099745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
210074e7dc98SMatthew Ahrens 		if (ds->ds_phys->ds_prev_snap_obj == 0)
210174e7dc98SMatthew Ahrens 			continue;
210274e7dc98SMatthew Ahrens 
2103cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2104cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
21053b2aab18SMatthew Ahrens 		ddpa->used += dlused;
21063b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
21073b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
210874e7dc98SMatthew Ahrens 	}
2109745cd3c5Smaybee 
2110745cd3c5Smaybee 	/*
2111745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
2112745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
2113745cd3c5Smaybee 	 */
21143b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
21153b2aab18SMatthew Ahrens 		ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
21163b2aab18SMatthew Ahrens 		ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
21173b2aab18SMatthew Ahrens 		ddpa->uncomp -=
21183b2aab18SMatthew Ahrens 		    ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
211999653d4eSeschrock 	}
212099653d4eSeschrock 
2121a2afb611SJerry Jelinek 	/* Check that there is enough space and limit headroom here */
212274e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2123a2afb611SJerry Jelinek 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
21243b2aab18SMatthew Ahrens 	if (err != 0)
21253b2aab18SMatthew Ahrens 		goto out;
212674e7dc98SMatthew Ahrens 
212774e7dc98SMatthew Ahrens 	/*
212874e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
212974e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
213074e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
213174e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
213274e7dc98SMatthew Ahrens 	 */
213374e7dc98SMatthew Ahrens 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
213474e7dc98SMatthew Ahrens 		uint64_t space;
213574e7dc98SMatthew Ahrens 
213674e7dc98SMatthew Ahrens 		/*
213774e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
21383f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
2139cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
214074e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
214174e7dc98SMatthew Ahrens 		 * iterate over all bps.
214274e7dc98SMatthew Ahrens 		 */
21433b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
21443b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
21453b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
21463b2aab18SMatthew Ahrens 		if (err != 0)
21473b2aab18SMatthew Ahrens 			goto out;
214874e7dc98SMatthew Ahrens 
21493b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
21503f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
21513b2aab18SMatthew Ahrens 		if (err != 0)
21523b2aab18SMatthew Ahrens 			goto out;
21533b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
215474e7dc98SMatthew Ahrens 	}
215574e7dc98SMatthew Ahrens 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
21563b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
21573b2aab18SMatthew Ahrens 		    origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
21583b2aab18SMatthew Ahrens 		if (err != 0)
21593b2aab18SMatthew Ahrens 			goto out;
2160745cd3c5Smaybee 	}
21611d452cf5Sahrens 
2162681d9761SEric Taylor out:
21633b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2164681d9761SEric Taylor 	return (err);
21651d452cf5Sahrens }
216699653d4eSeschrock 
21671d452cf5Sahrens static void
21683b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
21691d452cf5Sahrens {
21703b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
21713b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
21723b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
21733b2aab18SMatthew Ahrens 	struct promotenode *snap;
21743b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
21753b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_head;
21763b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
21773cb34c60Sahrens 	dsl_dir_t *odd = NULL;
2178088f3894Sahrens 	uint64_t oldnext_obj;
217974e7dc98SMatthew Ahrens 	int64_t delta;
21801d452cf5Sahrens 
21813b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
21823b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
21833b2aab18SMatthew Ahrens 
21843b2aab18SMatthew Ahrens 	ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
21851d452cf5Sahrens 
21863b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
21873b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
21883b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
21893b2aab18SMatthew Ahrens 
21903b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
219174e7dc98SMatthew Ahrens 	origin_head = snap->ds;
219274e7dc98SMatthew Ahrens 
21930b69c2f0Sahrens 	/*
21943cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
21950b69c2f0Sahrens 	 * changing.
21960b69c2f0Sahrens 	 */
21973b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
21983cb34c60Sahrens 	    NULL, FTAG, &odd));
219999653d4eSeschrock 
2200745cd3c5Smaybee 	/* change origin's next snap */
2201745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2202088f3894Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
22033b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
220474e7dc98SMatthew Ahrens 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
220574e7dc98SMatthew Ahrens 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2206745cd3c5Smaybee 
2207088f3894Sahrens 	/* change the origin's next clone */
2208088f3894Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
22093b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
22103b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
22113b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2212088f3894Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
2213088f3894Sahrens 		    oldnext_obj, tx));
2214088f3894Sahrens 	}
2215088f3894Sahrens 
2216745cd3c5Smaybee 	/* change origin */
2217745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2218745cd3c5Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2219745cd3c5Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
22203f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2221745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2222745cd3c5Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
22233f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
22243f9d6ad7SLin Ling 	    origin_ds->ds_phys->ds_creation_txg;
2225745cd3c5Smaybee 
2226cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2227cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
22283b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2229cde58dbcSMatthew Ahrens 		    odd->dd_phys->dd_clones, hds->ds_object, tx));
22303b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
22313b2aab18SMatthew Ahrens 		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2232cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2233cde58dbcSMatthew Ahrens 
22343b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
22353b2aab18SMatthew Ahrens 		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2236cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
2237cde58dbcSMatthew Ahrens 		if (dd->dd_phys->dd_clones == 0) {
2238cde58dbcSMatthew Ahrens 			dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2239cde58dbcSMatthew Ahrens 			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2240cde58dbcSMatthew Ahrens 		}
22413b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2242cde58dbcSMatthew Ahrens 		    dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2243cde58dbcSMatthew Ahrens 	}
2244cde58dbcSMatthew Ahrens 
224599653d4eSeschrock 	/* move snapshots to this dir */
22463b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
22473b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2248745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
224999653d4eSeschrock 
22503b2aab18SMatthew Ahrens 		/*
22513b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
22523b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
22533b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
22543b2aab18SMatthew Ahrens 		 */
2255503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2256503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2257503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
22583baa08fcSek 		}
22593b2aab18SMatthew Ahrens 
226099653d4eSeschrock 		/* move snap name entry */
22613b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
22623b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2263a2afb611SJerry Jelinek 		    ds->ds_snapname, tx, B_TRUE));
22643b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
226599653d4eSeschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
226699653d4eSeschrock 		    8, 1, &ds->ds_object, tx));
2267a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2268a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2269cde58dbcSMatthew Ahrens 
227099653d4eSeschrock 		/* change containing dsl_dir */
227199653d4eSeschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
22723cb34c60Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
227399653d4eSeschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
22743cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
22753b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
22763b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
227799653d4eSeschrock 		    NULL, ds, &ds->ds_dir));
227899653d4eSeschrock 
2279cde58dbcSMatthew Ahrens 		/* move any clone references */
2280cde58dbcSMatthew Ahrens 		if (ds->ds_phys->ds_next_clones_obj &&
2281cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2282cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2283cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2284cde58dbcSMatthew Ahrens 
22853b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
22863b2aab18SMatthew Ahrens 			    ds->ds_phys->ds_next_clones_obj);
22873b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
22883b2aab18SMatthew Ahrens 			    zap_cursor_advance(&zc)) {
22893b2aab18SMatthew Ahrens 				dsl_dataset_t *cnds;
22903b2aab18SMatthew Ahrens 				uint64_t o;
2291a9799022Sck 
22923b2aab18SMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
22933b2aab18SMatthew Ahrens 					/*
22943b2aab18SMatthew Ahrens 					 * We've already moved the
22953b2aab18SMatthew Ahrens 					 * origin's reference.
22963b2aab18SMatthew Ahrens 					 */
22973b2aab18SMatthew Ahrens 					continue;
22983b2aab18SMatthew Ahrens 				}
2299a9799022Sck 
23003b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
23013b2aab18SMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
23023b2aab18SMatthew Ahrens 				o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2303a9799022Sck 
23043b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
23053b2aab18SMatthew Ahrens 				    odd->dd_phys->dd_clones, o, tx));
23063b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
23073b2aab18SMatthew Ahrens 				    dd->dd_phys->dd_clones, o, tx));
23083b2aab18SMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
23093b2aab18SMatthew Ahrens 			}
23103b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
23113b2aab18SMatthew Ahrens 		}
23129082849eSck 
23133b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
2314a9799022Sck 	}
2315a9799022Sck 
2316a9799022Sck 	/*
23173b2aab18SMatthew Ahrens 	 * Change space accounting.
23183b2aab18SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
23193b2aab18SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
23203b2aab18SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
2321a9799022Sck 	 */
2322a9799022Sck 
23233b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
23243b2aab18SMatthew Ahrens 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
23253b2aab18SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
23263b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
23273b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
23283b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
23293b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
23303b2aab18SMatthew Ahrens 
23313b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
23323b2aab18SMatthew Ahrens 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
23333b2aab18SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
23343b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
23353b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
23363b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
23373b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
23383b2aab18SMatthew Ahrens 
23393b2aab18SMatthew Ahrens 	origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
23403b2aab18SMatthew Ahrens 
23413b2aab18SMatthew Ahrens 	/* log history record */
23423b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
23433b2aab18SMatthew Ahrens 
23443b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
23453b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2346a9799022Sck }
2347a9799022Sck 
23483b2aab18SMatthew Ahrens /*
23493b2aab18SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
23503b2aab18SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
23513b2aab18SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
23523b2aab18SMatthew Ahrens  * snapshots back to this dataset's origin.
23533b2aab18SMatthew Ahrens  */
2354a9799022Sck static int
23553b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
23563b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2357a9799022Sck {
23583b2aab18SMatthew Ahrens 	uint64_t obj = last_obj;
2359a9799022Sck 
23603b2aab18SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
23613b2aab18SMatthew Ahrens 	    offsetof(struct promotenode, link));
2362a9799022Sck 
23633b2aab18SMatthew Ahrens 	while (obj != first_obj) {
23643b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
23653b2aab18SMatthew Ahrens 		struct promotenode *snap;
23663b2aab18SMatthew Ahrens 		int err;
236792241e0bSTom Erickson 
23683b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
23693b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
23703b2aab18SMatthew Ahrens 		if (err != 0)
23713b2aab18SMatthew Ahrens 			return (err);
2372a9799022Sck 
23733b2aab18SMatthew Ahrens 		if (first_obj == 0)
23743b2aab18SMatthew Ahrens 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
23753b2aab18SMatthew Ahrens 
23763b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
23773b2aab18SMatthew Ahrens 		snap->ds = ds;
23783b2aab18SMatthew Ahrens 		list_insert_tail(l, snap);
23793b2aab18SMatthew Ahrens 		obj = ds->ds_phys->ds_prev_snap_obj;
23803b2aab18SMatthew Ahrens 	}
2381a9799022Sck 
2382a9799022Sck 	return (0);
2383a9799022Sck }
2384a9799022Sck 
23853b2aab18SMatthew Ahrens static int
23863b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2387a9799022Sck {
23883b2aab18SMatthew Ahrens 	struct promotenode *snap;
2389a9799022Sck 
23903b2aab18SMatthew Ahrens 	*spacep = 0;
23913b2aab18SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
23923b2aab18SMatthew Ahrens 		uint64_t used, comp, uncomp;
23933b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
23943b2aab18SMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
23953b2aab18SMatthew Ahrens 		*spacep += used;
239692241e0bSTom Erickson 	}
23973b2aab18SMatthew Ahrens 	return (0);
2398a9799022Sck }
2399a9799022Sck 
24003b2aab18SMatthew Ahrens static void
24013b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
2402a9799022Sck {
24033b2aab18SMatthew Ahrens 	struct promotenode *snap;
240492241e0bSTom Erickson 
24053b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
24063b2aab18SMatthew Ahrens 		return;
2407a9799022Sck 
24083b2aab18SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
24093b2aab18SMatthew Ahrens 		list_remove(l, snap);
24103b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
24113b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
24123b2aab18SMatthew Ahrens 	}
24133b2aab18SMatthew Ahrens 	list_destroy(l);
2414a9799022Sck }
2415a9799022Sck 
2416a9799022Sck static int
24173b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2418a9799022Sck {
24193b2aab18SMatthew Ahrens 	int error;
24203b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
24213b2aab18SMatthew Ahrens 	struct promotenode *snap;
2422a9799022Sck 
24233b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
24243b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
24253b2aab18SMatthew Ahrens 	if (error != 0)
24263b2aab18SMatthew Ahrens 		return (error);
24273b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
2428a9799022Sck 
24293b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
24303b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
24313b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2432be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
24333b2aab18SMatthew Ahrens 	}
2434a9799022Sck 
24353b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
24363b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
24373b2aab18SMatthew Ahrens 	if (error != 0)
24383b2aab18SMatthew Ahrens 		goto out;
2439a9799022Sck 
24403b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
24413b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
24423b2aab18SMatthew Ahrens 	if (error != 0)
24433b2aab18SMatthew Ahrens 		goto out;
2444a9799022Sck 
24453b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
24463b2aab18SMatthew Ahrens 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
24473b2aab18SMatthew Ahrens 	error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
24483b2aab18SMatthew Ahrens 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
24493b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
24503b2aab18SMatthew Ahrens 	if (error != 0)
24513b2aab18SMatthew Ahrens 		goto out;
2452379c004dSEric Schrock 
24533b2aab18SMatthew Ahrens 	if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
24543b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
24553b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
24563b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
24573b2aab18SMatthew Ahrens 		if (error != 0)
24583b2aab18SMatthew Ahrens 			goto out;
2459379c004dSEric Schrock 	}
24603b2aab18SMatthew Ahrens out:
24613b2aab18SMatthew Ahrens 	if (error != 0)
24623b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
24633b2aab18SMatthew Ahrens 	return (error);
2464a9799022Sck }
2465a9799022Sck 
2466a9799022Sck static void
24673b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2468a9799022Sck {
24693b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
24703b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
24713b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
24723b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
24733b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
24743b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
24753b2aab18SMatthew Ahrens }
247602c8f3f0SMatthew Ahrens 
24773b2aab18SMatthew Ahrens /*
24783b2aab18SMatthew Ahrens  * Promote a clone.
24793b2aab18SMatthew Ahrens  *
24803b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
24813b2aab18SMatthew Ahrens  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
24823b2aab18SMatthew Ahrens  */
24833b2aab18SMatthew Ahrens int
24843b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap)
24853b2aab18SMatthew Ahrens {
24863b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
24873b2aab18SMatthew Ahrens 	uint64_t numsnaps;
24883b2aab18SMatthew Ahrens 	int error;
24893b2aab18SMatthew Ahrens 	objset_t *os;
249092241e0bSTom Erickson 
24913b2aab18SMatthew Ahrens 	/*
24923b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
24933b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
24943b2aab18SMatthew Ahrens 	 */
24953b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
24963b2aab18SMatthew Ahrens 	if (error != 0)
24973b2aab18SMatthew Ahrens 		return (error);
24983b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
24993b2aab18SMatthew Ahrens 	    dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
25003b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
25013b2aab18SMatthew Ahrens 	if (error != 0)
25023b2aab18SMatthew Ahrens 		return (error);
250302c8f3f0SMatthew Ahrens 
25043b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
25053b2aab18SMatthew Ahrens 	ddpa.err_ds = conflsnap;
2506a2afb611SJerry Jelinek 	ddpa.cr = CRED();
250702c8f3f0SMatthew Ahrens 
25083b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_dataset_promote_check,
25097d46dc6cSMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa,
25107d46dc6cSMatthew Ahrens 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2511a9799022Sck }
2512a9799022Sck 
2513a9799022Sck int
25143b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
251591948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2516a9799022Sck {
25173b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2518a9799022Sck 
25193b2aab18SMatthew Ahrens 	/* they should both be heads */
25203b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(clone) ||
25213b2aab18SMatthew Ahrens 	    dsl_dataset_is_snapshot(origin_head))
2522be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
252392241e0bSTom Erickson 
252434f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
252534f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
2526be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2527a9799022Sck 
25283b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
25293b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
25303b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
253134f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2532be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
253392241e0bSTom Erickson 
25343b2aab18SMatthew Ahrens 	/* the clone should be a child of the origin */
25353b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2536be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2537842727c2SChris Kirby 
25383b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
253934f2f8cfSMatthew Ahrens 	if (!force &&
254034f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2541be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
2542c99e4bdcSChris Kirby 
25433b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
254491948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2545be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
25463b2aab18SMatthew Ahrens 
25473b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
25483b2aab18SMatthew Ahrens 	unused_refres_delta =
25493b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
25503b2aab18SMatthew Ahrens 	    origin_head->ds_phys->ds_unique_bytes) -
25513b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
25523b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes);
25533b2aab18SMatthew Ahrens 
25543b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
25553b2aab18SMatthew Ahrens 	    unused_refres_delta >
25563b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2557be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
25583b2aab18SMatthew Ahrens 
25593b2aab18SMatthew Ahrens 	/* clone can't be over the head's refquota */
25603b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
25613b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2562be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2563c99e4bdcSChris Kirby 
25643b2aab18SMatthew Ahrens 	return (0);
2565c99e4bdcSChris Kirby }
2566c99e4bdcSChris Kirby 
2567a7f53a56SChris Kirby void
25683b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
25693b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2570a7f53a56SChris Kirby {
25713b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
25723b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2573a7f53a56SChris Kirby 
25743b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
25753b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
25763b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
257734f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2578842727c2SChris Kirby 
25793b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
25803b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2581842727c2SChris Kirby 
25823b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
25833b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
25843b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
25853b2aab18SMatthew Ahrens 	}
2586842727c2SChris Kirby 
25873b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
25883b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
25893b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
2590842727c2SChris Kirby 	}
2591842727c2SChris Kirby 
25923b2aab18SMatthew Ahrens 	unused_refres_delta =
25933b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
25943b2aab18SMatthew Ahrens 	    origin_head->ds_phys->ds_unique_bytes) -
25953b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
25963b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes);
25973b2aab18SMatthew Ahrens 
25983b2aab18SMatthew Ahrens 	/*
25993b2aab18SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
26003b2aab18SMatthew Ahrens 	 */
26013b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
26023b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
26033b2aab18SMatthew Ahrens 		uint64_t comp, uncomp;
26043b2aab18SMatthew Ahrens 
26053b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
26063b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
26073b2aab18SMatthew Ahrens 		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
26083b2aab18SMatthew Ahrens 		    &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
26093b2aab18SMatthew Ahrens 	}
26103b2aab18SMatthew Ahrens 
26113b2aab18SMatthew Ahrens 	/* swap blkptrs */
26123b2aab18SMatthew Ahrens 	{
26133b2aab18SMatthew Ahrens 		blkptr_t tmp;
26143b2aab18SMatthew Ahrens 		tmp = origin_head->ds_phys->ds_bp;
26153b2aab18SMatthew Ahrens 		origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
26163b2aab18SMatthew Ahrens 		clone->ds_phys->ds_bp = tmp;
26173b2aab18SMatthew Ahrens 	}
26183b2aab18SMatthew Ahrens 
26193b2aab18SMatthew Ahrens 	/* set dd_*_bytes */
26203b2aab18SMatthew Ahrens 	{
26213b2aab18SMatthew Ahrens 		int64_t dused, dcomp, duncomp;
26223b2aab18SMatthew Ahrens 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
26233b2aab18SMatthew Ahrens 		uint64_t odl_used, odl_comp, odl_uncomp;
26243b2aab18SMatthew Ahrens 
26253b2aab18SMatthew Ahrens 		ASSERT3U(clone->ds_dir->dd_phys->
26263b2aab18SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
26273b2aab18SMatthew Ahrens 
26283b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
26293b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
26303b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
26313b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
263215508ac0SChris Kirby 
26333b2aab18SMatthew Ahrens 		dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
26343b2aab18SMatthew Ahrens 		    (origin_head->ds_phys->ds_referenced_bytes + odl_used);
26353b2aab18SMatthew Ahrens 		dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
26363b2aab18SMatthew Ahrens 		    (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
26373b2aab18SMatthew Ahrens 		duncomp = clone->ds_phys->ds_uncompressed_bytes +
26383b2aab18SMatthew Ahrens 		    cdl_uncomp -
26393b2aab18SMatthew Ahrens 		    (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2640842727c2SChris Kirby 
26413b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
26423b2aab18SMatthew Ahrens 		    dused, dcomp, duncomp, tx);
26433b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
26443b2aab18SMatthew Ahrens 		    -dused, -dcomp, -duncomp, tx);
2645842727c2SChris Kirby 
2646842727c2SChris Kirby 		/*
26473b2aab18SMatthew Ahrens 		 * The difference in the space used by snapshots is the
26483b2aab18SMatthew Ahrens 		 * difference in snapshot space due to the head's
26493b2aab18SMatthew Ahrens 		 * deadlist (since that's the only thing that's
26503b2aab18SMatthew Ahrens 		 * changing that affects the snapused).
2651842727c2SChris Kirby 		 */
26523b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
26533b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
26543b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
26553b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
26563b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
26573b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
26583b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
26593b2aab18SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
2660842727c2SChris Kirby 	}
2661842727c2SChris Kirby 
26623b2aab18SMatthew Ahrens 	/* swap ds_*_bytes */
26633b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
26643b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_referenced_bytes);
26653b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
26663b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_compressed_bytes);
26673b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
26683b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_uncompressed_bytes);
26693b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_unique_bytes,
26703b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes);
2671842727c2SChris Kirby 
26723b2aab18SMatthew Ahrens 	/* apply any parent delta for change in unconsumed refreservation */
26733b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
26743b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
2675ca45db41SChris Kirby 
26763b2aab18SMatthew Ahrens 	/*
26773b2aab18SMatthew Ahrens 	 * Swap deadlists.
26783b2aab18SMatthew Ahrens 	 */
26793b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
26803b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
26813b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
26823b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_deadlist_obj);
26833b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
26843b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_deadlist_obj);
26853b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
26863b2aab18SMatthew Ahrens 	    origin_head->ds_phys->ds_deadlist_obj);
2687842727c2SChris Kirby 
26883b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2689842727c2SChris Kirby 
26903b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
26913b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
2692842727c2SChris Kirby }
2693842727c2SChris Kirby 
26943b2aab18SMatthew Ahrens /*
26953b2aab18SMatthew Ahrens  * Given a pool name and a dataset object number in that pool,
26963b2aab18SMatthew Ahrens  * return the name of that dataset.
26973b2aab18SMatthew Ahrens  */
2698a7f53a56SChris Kirby int
26993b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2700a7f53a56SChris Kirby {
27013b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
27023b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2703a7f53a56SChris Kirby 	int error;
2704a7f53a56SChris Kirby 
27053b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
27063b2aab18SMatthew Ahrens 	if (error != 0)
27073b2aab18SMatthew Ahrens 		return (error);
27083b2aab18SMatthew Ahrens 
27093b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
27103b2aab18SMatthew Ahrens 	if (error == 0) {
27113b2aab18SMatthew Ahrens 		dsl_dataset_name(ds, buf);
27123b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
27133b2aab18SMatthew Ahrens 	}
27143b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
2715a7f53a56SChris Kirby 
2716a7f53a56SChris Kirby 	return (error);
2717a7f53a56SChris Kirby }
2718a7f53a56SChris Kirby 
2719842727c2SChris Kirby int
27203b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
27213b2aab18SMatthew Ahrens     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2722842727c2SChris Kirby {
27233b2aab18SMatthew Ahrens 	int error = 0;
2724842727c2SChris Kirby 
27253b2aab18SMatthew Ahrens 	ASSERT3S(asize, >, 0);
2726842727c2SChris Kirby 
27273b2aab18SMatthew Ahrens 	/*
27283b2aab18SMatthew Ahrens 	 * *ref_rsrv is the portion of asize that will come from any
27293b2aab18SMatthew Ahrens 	 * unconsumed refreservation space.
27303b2aab18SMatthew Ahrens 	 */
27313b2aab18SMatthew Ahrens 	*ref_rsrv = 0;
2732842727c2SChris Kirby 
27333b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
27343b2aab18SMatthew Ahrens 	/*
27353b2aab18SMatthew Ahrens 	 * Make a space adjustment for reserved bytes.
27363b2aab18SMatthew Ahrens 	 */
27373b2aab18SMatthew Ahrens 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
27383b2aab18SMatthew Ahrens 		ASSERT3U(*used, >=,
27393b2aab18SMatthew Ahrens 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
27403b2aab18SMatthew Ahrens 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
27413b2aab18SMatthew Ahrens 		*ref_rsrv =
27423b2aab18SMatthew Ahrens 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
2743842727c2SChris Kirby 	}
2744842727c2SChris Kirby 
27453b2aab18SMatthew Ahrens 	if (!check_quota || ds->ds_quota == 0) {
27463b2aab18SMatthew Ahrens 		mutex_exit(&ds->ds_lock);
27473b2aab18SMatthew Ahrens 		return (0);
2748842727c2SChris Kirby 	}
27493b2aab18SMatthew Ahrens 	/*
27503b2aab18SMatthew Ahrens 	 * If they are requesting more space, and our current estimate
27513b2aab18SMatthew Ahrens 	 * is over quota, they get to try again unless the actual
27523b2aab18SMatthew Ahrens 	 * on-disk is over quota and there are no pending changes (which
27533b2aab18SMatthew Ahrens 	 * may free up space for us).
27543b2aab18SMatthew Ahrens 	 */
27553b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
27563b2aab18SMatthew Ahrens 		if (inflight > 0 ||
27573b2aab18SMatthew Ahrens 		    ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2758be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
27593b2aab18SMatthew Ahrens 		else
2760be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
2761842727c2SChris Kirby 	}
27623b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2763842727c2SChris Kirby 
2764842727c2SChris Kirby 	return (error);
2765842727c2SChris Kirby }
2766842727c2SChris Kirby 
27673b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
27683b2aab18SMatthew Ahrens 	const char *ddsqra_name;
27693b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
27703b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
27713b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
2772842727c2SChris Kirby 
27733b2aab18SMatthew Ahrens 
27743b2aab18SMatthew Ahrens /* ARGSUSED */
2775842727c2SChris Kirby static int
27763b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2777842727c2SChris Kirby {
27783b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
27793b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
27803b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2781842727c2SChris Kirby 	int error;
27823b2aab18SMatthew Ahrens 	uint64_t newval;
2783842727c2SChris Kirby 
27843b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2785be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
2786842727c2SChris Kirby 
27873b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
27883b2aab18SMatthew Ahrens 	if (error != 0)
27893b2aab18SMatthew Ahrens 		return (error);
27903b2aab18SMatthew Ahrens 
27913b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
27923b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2793be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2794842727c2SChris Kirby 	}
2795842727c2SChris Kirby 
27963b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
27973b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
27983b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
27993b2aab18SMatthew Ahrens 	if (error != 0) {
28003b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2801842727c2SChris Kirby 		return (error);
2802842727c2SChris Kirby 	}
2803842727c2SChris Kirby 
28043b2aab18SMatthew Ahrens 	if (newval == 0) {
28053b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
28063b2aab18SMatthew Ahrens 		return (0);
28073b2aab18SMatthew Ahrens 	}
2808842727c2SChris Kirby 
28093b2aab18SMatthew Ahrens 	if (newval < ds->ds_phys->ds_referenced_bytes ||
28103b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
28113b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2812be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
28133b2aab18SMatthew Ahrens 	}
28143b2aab18SMatthew Ahrens 
28153b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2816842727c2SChris Kirby 	return (0);
2817842727c2SChris Kirby }
2818842727c2SChris Kirby 
28193b2aab18SMatthew Ahrens static void
28203b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2821842727c2SChris Kirby {
28223b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
28233b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
28243b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
28253b2aab18SMatthew Ahrens 	uint64_t newval;
2826842727c2SChris Kirby 
28273b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2828842727c2SChris Kirby 
28293b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
28303b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
28313b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
28323b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
2833842727c2SChris Kirby 
28343b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
28353b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2836842727c2SChris Kirby 
28373b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
28383b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
28393b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
2840842727c2SChris Kirby 	}
28413b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2842842727c2SChris Kirby }
2843842727c2SChris Kirby 
28443b2aab18SMatthew Ahrens int
28453b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
28463b2aab18SMatthew Ahrens     uint64_t refquota)
2847842727c2SChris Kirby {
28483b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
2849842727c2SChris Kirby 
28503b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
28513b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
28523b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
28533b2aab18SMatthew Ahrens 
28543b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
28557d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
2856842727c2SChris Kirby }
2857842727c2SChris Kirby 
2858842727c2SChris Kirby static int
28593b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2860842727c2SChris Kirby {
28613b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
28623b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
2863842727c2SChris Kirby 	dsl_dataset_t *ds;
2864842727c2SChris Kirby 	int error;
28653b2aab18SMatthew Ahrens 	uint64_t newval, unique;
2866d7747cbcSChris Kirby 
28673b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2868be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
2869842727c2SChris Kirby 
28703b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
28713b2aab18SMatthew Ahrens 	if (error != 0)
2872842727c2SChris Kirby 		return (error);
2873842727c2SChris Kirby 
28743b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
28753b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2876be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2877842727c2SChris Kirby 	}
2878842727c2SChris Kirby 
28793b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
28803b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
28813b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
28823b2aab18SMatthew Ahrens 	if (error != 0) {
28833b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2884842727c2SChris Kirby 		return (error);
2885842727c2SChris Kirby 	}
2886842727c2SChris Kirby 
28873b2aab18SMatthew Ahrens 	/*
28883b2aab18SMatthew Ahrens 	 * If we are doing the preliminary check in open context, the
28893b2aab18SMatthew Ahrens 	 * space estimates may be inaccurate.
28903b2aab18SMatthew Ahrens 	 */
28913b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
28923b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
28933b2aab18SMatthew Ahrens 		return (0);
2894842727c2SChris Kirby 	}
2895842727c2SChris Kirby 
28963b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
28973b2aab18SMatthew Ahrens 	if (!DS_UNIQUE_IS_ACCURATE(ds))
28983b2aab18SMatthew Ahrens 		dsl_dataset_recalc_head_uniq(ds);
28993b2aab18SMatthew Ahrens 	unique = ds->ds_phys->ds_unique_bytes;
29003b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2901842727c2SChris Kirby 
29023b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
29033b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
29043b2aab18SMatthew Ahrens 		    MAX(unique, ds->ds_reserved);
2905842727c2SChris Kirby 
29063b2aab18SMatthew Ahrens 		if (delta >
29073b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
29083b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
29093b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
2910be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
29113b2aab18SMatthew Ahrens 		}
2912842727c2SChris Kirby 	}
2913842727c2SChris Kirby 
29143b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
29153b2aab18SMatthew Ahrens 	return (0);
2916842727c2SChris Kirby }
2917842727c2SChris Kirby 
29183b2aab18SMatthew Ahrens void
29193b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
29203b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2921ca45db41SChris Kirby {
29223b2aab18SMatthew Ahrens 	uint64_t newval;
29233b2aab18SMatthew Ahrens 	uint64_t unique;
29243b2aab18SMatthew Ahrens 	int64_t delta;
2925ca45db41SChris Kirby 
29263b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
29273b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
2928ca45db41SChris Kirby 
29293b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
29303b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2931a7f53a56SChris Kirby 
29323b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
29333b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
29343b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
29353b2aab18SMatthew Ahrens 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
29363b2aab18SMatthew Ahrens 	unique = ds->ds_phys->ds_unique_bytes;
29373b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
29383b2aab18SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
29393b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
29403b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2941a7f53a56SChris Kirby 
29423b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
29433b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
2944ca45db41SChris Kirby }
2945ca45db41SChris Kirby 
29463b2aab18SMatthew Ahrens static void
29473b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2948842727c2SChris Kirby {
29493b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
29503b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
2951842727c2SChris Kirby 	dsl_dataset_t *ds;
2952842727c2SChris Kirby 
29533b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
29543b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
29553b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2956842727c2SChris Kirby 	dsl_dataset_rele(ds, FTAG);
2957842727c2SChris Kirby }
2958503ad85cSMatthew Ahrens 
2959503ad85cSMatthew Ahrens int
29603b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
29613b2aab18SMatthew Ahrens     uint64_t refreservation)
2962503ad85cSMatthew Ahrens {
29633b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
2964503ad85cSMatthew Ahrens 
29653b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
29663b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
29673b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
29683b2aab18SMatthew Ahrens 
29693b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
29707d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refreservation_sync, &ddsqra,
29717d46dc6cSMatthew Ahrens 	    0, ZFS_SPACE_CHECK_NONE));
2972503ad85cSMatthew Ahrens }
297319b94df9SMatthew Ahrens 
297419b94df9SMatthew Ahrens /*
297519b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
297619b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
297719b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
297819b94df9SMatthew Ahrens  * fail and return EINVAL.
297919b94df9SMatthew Ahrens  *
298019b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
298119b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
298219b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
298319b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
298419b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
298519b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
298619b94df9SMatthew Ahrens  *
298719b94df9SMatthew Ahrens  * space freed                         [---------------------]
298819b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
298919b94df9SMatthew Ahrens  *                                         oldsnap            new
299019b94df9SMatthew Ahrens  */
299119b94df9SMatthew Ahrens int
299219b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
299319b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
299419b94df9SMatthew Ahrens {
299519b94df9SMatthew Ahrens 	int err = 0;
299619b94df9SMatthew Ahrens 	uint64_t snapobj;
299719b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
299819b94df9SMatthew Ahrens 
29993b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
30003b2aab18SMatthew Ahrens 
300119b94df9SMatthew Ahrens 	*usedp = 0;
3002ad135b5dSChristopher Siden 	*usedp += new->ds_phys->ds_referenced_bytes;
3003ad135b5dSChristopher Siden 	*usedp -= oldsnap->ds_phys->ds_referenced_bytes;
300419b94df9SMatthew Ahrens 
300519b94df9SMatthew Ahrens 	*compp = 0;
300619b94df9SMatthew Ahrens 	*compp += new->ds_phys->ds_compressed_bytes;
300719b94df9SMatthew Ahrens 	*compp -= oldsnap->ds_phys->ds_compressed_bytes;
300819b94df9SMatthew Ahrens 
300919b94df9SMatthew Ahrens 	*uncompp = 0;
301019b94df9SMatthew Ahrens 	*uncompp += new->ds_phys->ds_uncompressed_bytes;
301119b94df9SMatthew Ahrens 	*uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
301219b94df9SMatthew Ahrens 
301319b94df9SMatthew Ahrens 	snapobj = new->ds_object;
301419b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
301519b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
301619b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
301719b94df9SMatthew Ahrens 
3018ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
3019ad135b5dSChristopher Siden 			snap = new;
3020ad135b5dSChristopher Siden 		} else {
3021ad135b5dSChristopher Siden 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3022ad135b5dSChristopher Siden 			if (err != 0)
3023ad135b5dSChristopher Siden 				break;
3024ad135b5dSChristopher Siden 		}
302519b94df9SMatthew Ahrens 
302619b94df9SMatthew Ahrens 		if (snap->ds_phys->ds_prev_snap_txg ==
302719b94df9SMatthew Ahrens 		    oldsnap->ds_phys->ds_creation_txg) {
302819b94df9SMatthew Ahrens 			/*
302919b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
303019b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
303119b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
303219b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
303319b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
303419b94df9SMatthew Ahrens 			 * optimization itself.
303519b94df9SMatthew Ahrens 			 */
303619b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
303719b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
303819b94df9SMatthew Ahrens 		} else {
303919b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
304019b94df9SMatthew Ahrens 			    0, oldsnap->ds_phys->ds_creation_txg,
304119b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
304219b94df9SMatthew Ahrens 		}
304319b94df9SMatthew Ahrens 		*usedp += used;
304419b94df9SMatthew Ahrens 		*compp += comp;
304519b94df9SMatthew Ahrens 		*uncompp += uncomp;
304619b94df9SMatthew Ahrens 
304719b94df9SMatthew Ahrens 		/*
304819b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
304919b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
305019b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
305119b94df9SMatthew Ahrens 		 */
305219b94df9SMatthew Ahrens 		snapobj = snap->ds_phys->ds_prev_snap_obj;
3053ad135b5dSChristopher Siden 		if (snap != new)
3054ad135b5dSChristopher Siden 			dsl_dataset_rele(snap, FTAG);
305519b94df9SMatthew Ahrens 		if (snapobj == 0) {
3056be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
305719b94df9SMatthew Ahrens 			break;
305819b94df9SMatthew Ahrens 		}
305919b94df9SMatthew Ahrens 
306019b94df9SMatthew Ahrens 	}
306119b94df9SMatthew Ahrens 	return (err);
306219b94df9SMatthew Ahrens }
306319b94df9SMatthew Ahrens 
306419b94df9SMatthew Ahrens /*
306519b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
306619b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
306719b94df9SMatthew Ahrens  *
306819b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
306919b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
307019b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
307119b94df9SMatthew Ahrens  *
307219b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
307319b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
307419b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
307519b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
307619b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
307719b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
307819b94df9SMatthew Ahrens  */
307919b94df9SMatthew Ahrens int
308019b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
308119b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
308219b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
308319b94df9SMatthew Ahrens {
308419b94df9SMatthew Ahrens 	int err = 0;
308519b94df9SMatthew Ahrens 	uint64_t snapobj;
308619b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
308719b94df9SMatthew Ahrens 
308819b94df9SMatthew Ahrens 	ASSERT(dsl_dataset_is_snapshot(firstsnap));
308919b94df9SMatthew Ahrens 	ASSERT(dsl_dataset_is_snapshot(lastsnap));
309019b94df9SMatthew Ahrens 
309119b94df9SMatthew Ahrens 	/*
309219b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
309319b94df9SMatthew Ahrens 	 * is before lastsnap.
309419b94df9SMatthew Ahrens 	 */
309519b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
309619b94df9SMatthew Ahrens 	    firstsnap->ds_phys->ds_creation_txg >
309719b94df9SMatthew Ahrens 	    lastsnap->ds_phys->ds_creation_txg)
3098be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
309919b94df9SMatthew Ahrens 
310019b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
310119b94df9SMatthew Ahrens 
310219b94df9SMatthew Ahrens 	snapobj = lastsnap->ds_phys->ds_next_snap_obj;
310319b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
310419b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
310519b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
310619b94df9SMatthew Ahrens 
310719b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
310819b94df9SMatthew Ahrens 		if (err != 0)
310919b94df9SMatthew Ahrens 			break;
311019b94df9SMatthew Ahrens 
311119b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
311219b94df9SMatthew Ahrens 		    firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
311319b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
311419b94df9SMatthew Ahrens 		*usedp += used;
311519b94df9SMatthew Ahrens 		*compp += comp;
311619b94df9SMatthew Ahrens 		*uncompp += uncomp;
311719b94df9SMatthew Ahrens 
311819b94df9SMatthew Ahrens 		snapobj = ds->ds_phys->ds_prev_snap_obj;
311919b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
312019b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
312119b94df9SMatthew Ahrens 	}
312219b94df9SMatthew Ahrens 	return (err);
312319b94df9SMatthew Ahrens }
31243b2aab18SMatthew Ahrens 
31253b2aab18SMatthew Ahrens /*
31263b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
31273b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
31283b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
31293b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
31303b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
313178f17100SMatthew Ahrens  *
313278f17100SMatthew Ahrens  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
31333b2aab18SMatthew Ahrens  */
31343b2aab18SMatthew Ahrens boolean_t
313578f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
313678f17100SMatthew Ahrens 	uint64_t earlier_txg)
31373b2aab18SMatthew Ahrens {
31383b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
31393b2aab18SMatthew Ahrens 	int error;
31403b2aab18SMatthew Ahrens 	boolean_t ret;
31413b2aab18SMatthew Ahrens 
31423b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
314378f17100SMatthew Ahrens 	ASSERT(dsl_dataset_is_snapshot(earlier) || earlier_txg != 0);
314478f17100SMatthew Ahrens 
314578f17100SMatthew Ahrens 	if (earlier_txg == 0)
314678f17100SMatthew Ahrens 		earlier_txg = earlier->ds_phys->ds_creation_txg;
31473b2aab18SMatthew Ahrens 
314878f17100SMatthew Ahrens 	if (dsl_dataset_is_snapshot(later) &&
314978f17100SMatthew Ahrens 	    earlier_txg >= later->ds_phys->ds_creation_txg)
31503b2aab18SMatthew Ahrens 		return (B_FALSE);
31513b2aab18SMatthew Ahrens 
31523b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
31533b2aab18SMatthew Ahrens 		return (B_TRUE);
31543b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
31553b2aab18SMatthew Ahrens 		return (B_FALSE);
31563b2aab18SMatthew Ahrens 
31573b2aab18SMatthew Ahrens 	if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
31583b2aab18SMatthew Ahrens 		return (B_TRUE);
31593b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
31603b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
31613b2aab18SMatthew Ahrens 	    later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
31623b2aab18SMatthew Ahrens 	if (error != 0)
31633b2aab18SMatthew Ahrens 		return (B_FALSE);
316478f17100SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
31653b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
31663b2aab18SMatthew Ahrens 	return (ret);
31673b2aab18SMatthew Ahrens }
31682acef22dSMatthew Ahrens 
31692acef22dSMatthew Ahrens 
31702acef22dSMatthew Ahrens void
31712acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
31722acef22dSMatthew Ahrens {
31732acef22dSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
31742acef22dSMatthew Ahrens 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
31752acef22dSMatthew Ahrens }
3176