xref: /titanic_51/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision e6546372a86065934a1b311176ecd1b1c543a222)
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.
23*e6546372SJosef 'Jeff' Sipek  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
2503d1795fSAlexander Stetsenko  * Copyright (c) 2014 RackTop Systems.
26bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
285f7a8e6dSDan McDonald  * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/dmu_objset.h>
32fa9e4066Sahrens #include <sys/dsl_dataset.h>
33fa9e4066Sahrens #include <sys/dsl_dir.h>
3499653d4eSeschrock #include <sys/dsl_prop.h>
351d452cf5Sahrens #include <sys/dsl_synctask.h>
36fa9e4066Sahrens #include <sys/dmu_traverse.h>
374e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
38fa9e4066Sahrens #include <sys/dmu_tx.h>
39fa9e4066Sahrens #include <sys/arc.h>
40fa9e4066Sahrens #include <sys/zio.h>
41fa9e4066Sahrens #include <sys/zap.h>
42ad135b5dSChristopher Siden #include <sys/zfeature.h>
43fa9e4066Sahrens #include <sys/unique.h>
44fa9e4066Sahrens #include <sys/zfs_context.h>
45cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
46ecd6cf80Smarks #include <sys/spa.h>
47088f3894Sahrens #include <sys/zfs_znode.h>
48c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
49842727c2SChris Kirby #include <sys/zvol.h>
503f9d6ad7SLin Ling #include <sys/dsl_scan.h>
51cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
523b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
533b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
5478f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
5545818ee1SMatthew Ahrens #include <sys/dmu_send.h>
5645818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
579c3fd121SMatthew Ahrens #include <sys/zio_compress.h>
589c3fd121SMatthew Ahrens #include <zfs_fletcher.h>
59e1930233Sbonwick 
60b5152584SMatthew Ahrens /*
61b5152584SMatthew Ahrens  * The SPA supports block sizes up to 16MB.  However, very large blocks
62b5152584SMatthew Ahrens  * can have an impact on i/o latency (e.g. tying up a spinning disk for
63b5152584SMatthew Ahrens  * ~300ms), and also potentially on the memory allocator.  Therefore,
64b5152584SMatthew Ahrens  * we do not allow the recordsize to be set larger than zfs_max_recordsize
65b5152584SMatthew Ahrens  * (default 1MB).  Larger blocks can be created by changing this tunable,
66b5152584SMatthew Ahrens  * and pools with larger blocks can always be imported and used, regardless
67b5152584SMatthew Ahrens  * of this setting.
68b5152584SMatthew Ahrens  */
69b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024;
70b5152584SMatthew Ahrens 
71cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
72cde58dbcSMatthew Ahrens 	{ \
73cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
74cde58dbcSMatthew Ahrens 		(x) = (y); \
75cde58dbcSMatthew Ahrens 		(y) = __tmp; \
76cde58dbcSMatthew Ahrens 	}
77cde58dbcSMatthew Ahrens 
7855434c77Sek110237 #define	DS_REF_MAX	(1ULL << 62)
79fa9e4066Sahrens 
80c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
81c1379625SJustin T. Gibbs 
825f7a8e6dSDan McDonald extern int spa_asize_inflation;
835f7a8e6dSDan McDonald 
84a9799022Sck153898 /*
85a9799022Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
86a9799022Sck153898  * layer.  If there's a refreservation, that space has already been
87a9799022Sck153898  * partially accounted for in our ancestors.
88a9799022Sck153898  */
89a9799022Sck153898 static int64_t
90a9799022Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
91a9799022Sck153898 {
92c1379625SJustin T. Gibbs 	dsl_dataset_phys_t *ds_phys;
93a9799022Sck153898 	uint64_t old_bytes, new_bytes;
94a9799022Sck153898 
95a9799022Sck153898 	if (ds->ds_reserved == 0)
96a9799022Sck153898 		return (delta);
97a9799022Sck153898 
98c1379625SJustin T. Gibbs 	ds_phys = dsl_dataset_phys(ds);
99c1379625SJustin T. Gibbs 	old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
100c1379625SJustin T. Gibbs 	new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
101a9799022Sck153898 
102a9799022Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
103a9799022Sck153898 	return (new_bytes - old_bytes);
104a9799022Sck153898 }
105fa9e4066Sahrens 
106fa9e4066Sahrens void
107b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
108fa9e4066Sahrens {
109b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
110fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
111fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
112a9799022Sck153898 	int64_t delta;
113fa9e4066Sahrens 
1143f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
115fa9e4066Sahrens 
116fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
117fa9e4066Sahrens 	/* It could have been compressed away to nothing */
118fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
119fa9e4066Sahrens 		return;
120fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
121ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
122fa9e4066Sahrens 	if (ds == NULL) {
123ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
124ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
125fa9e4066Sahrens 		return;
126fa9e4066Sahrens 	}
1273f9d6ad7SLin Ling 
128b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
129fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
130a9799022Sck153898 	delta = parent_delta(ds, used);
131c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes += used;
132c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
133c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
134c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes += used;
13545818ee1SMatthew Ahrens 
136ca0cc391SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
137ca0cc391SMatthew Ahrens 		ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
138ca0cc391SMatthew Ahrens 		    B_TRUE;
139ca0cc391SMatthew Ahrens 	}
14045818ee1SMatthew Ahrens 
14145818ee1SMatthew Ahrens 	spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
14245818ee1SMatthew Ahrens 	if (f != SPA_FEATURE_NONE)
14345818ee1SMatthew Ahrens 		ds->ds_feature_activation_needed[f] = B_TRUE;
14445818ee1SMatthew Ahrens 
145fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
14674e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
14774e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
14874e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
14974e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
150fa9e4066Sahrens }
151fa9e4066Sahrens 
152cdb0ab79Smaybee int
153b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
154b24ab676SJeff Bonwick     boolean_t async)
155fa9e4066Sahrens {
15643466aaeSMax Grossman 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
15743466aaeSMax Grossman 	int compressed = BP_GET_PSIZE(bp);
15843466aaeSMax Grossman 	int uncompressed = BP_GET_UCSIZE(bp);
15943466aaeSMax Grossman 
160fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
161cdb0ab79Smaybee 		return (0);
162fa9e4066Sahrens 
163b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
164b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
165b24ab676SJeff Bonwick 
166fa9e4066Sahrens 	if (ds == NULL) {
167b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
168ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
169ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
170cdb0ab79Smaybee 		return (used);
171fa9e4066Sahrens 	}
172fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
173fa9e4066Sahrens 
174bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
175fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
176fa9e4066Sahrens 
177c1379625SJustin T. Gibbs 	if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
178a9799022Sck153898 		int64_t delta;
179c717a561Smaybee 
1803f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
181b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
182fa9e4066Sahrens 
183fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
184c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
185a9799022Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
186a9799022Sck153898 		delta = parent_delta(ds, -used);
187c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_unique_bytes -= used;
188fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
18974e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
190a9799022Sck153898 		    delta, -compressed, -uncompressed, tx);
19174e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
19274e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
193fa9e4066Sahrens 	} else {
194fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
195b24ab676SJeff Bonwick 		if (async) {
196b24ab676SJeff Bonwick 			/*
197b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
198b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
199cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
200b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
201b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
202b24ab676SJeff Bonwick 			 */
203cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
204b24ab676SJeff Bonwick 		} else {
205cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
206b24ab676SJeff Bonwick 		}
207fa9e4066Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
208c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj);
209c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
210a4611edeSahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
211c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
21299653d4eSeschrock 		    ds->ds_object && bp->blk_birth >
213c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
214fa9e4066Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
215fa9e4066Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
216c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
217fa9e4066Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
218fa9e4066Sahrens 		}
2193f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
22074e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
22174e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
22274e7dc98SMatthew Ahrens 		}
223fa9e4066Sahrens 	}
224fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
225c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
226c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
227c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
228c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
229c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
230c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
231fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
232cdb0ab79Smaybee 
233cdb0ab79Smaybee 	return (used);
234fa9e4066Sahrens }
235fa9e4066Sahrens 
236ea8dc4b6Seschrock uint64_t
237ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
238fa9e4066Sahrens {
239a2eea2e1Sahrens 	uint64_t trysnap = 0;
240a2eea2e1Sahrens 
241fa9e4066Sahrens 	if (ds == NULL)
242ea8dc4b6Seschrock 		return (0);
243fa9e4066Sahrens 	/*
244fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
245fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
246fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
247fa9e4066Sahrens 	 * consume, which is OK.
248fa9e4066Sahrens 	 *
249fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
250fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
251fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
252fa9e4066Sahrens 	 */
253a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
254a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
255a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
256c1379625SJustin T. Gibbs 	return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap));
257ea8dc4b6Seschrock }
258ea8dc4b6Seschrock 
2593d692628SSanjeev Bagewadi boolean_t
260c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
261c7cd2421SGeorge Wilson     uint64_t blk_birth)
262ea8dc4b6Seschrock {
26343466aaeSMax Grossman 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
26443466aaeSMax Grossman 	    (bp != NULL && BP_IS_HOLE(bp)))
265c7cd2421SGeorge Wilson 		return (B_FALSE);
266c7cd2421SGeorge Wilson 
267c7cd2421SGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
268c7cd2421SGeorge Wilson 
269c7cd2421SGeorge Wilson 	return (B_TRUE);
270fa9e4066Sahrens }
271fa9e4066Sahrens 
272*e6546372SJosef 'Jeff' Sipek /*
273*e6546372SJosef 'Jeff' Sipek  * We have to release the fsid syncronously or we risk that a subsequent
274*e6546372SJosef 'Jeff' Sipek  * mount of the same dataset will fail to unique_insert the fsid.  This
275*e6546372SJosef 'Jeff' Sipek  * failure would manifest itself as the fsid of this dataset changing
276*e6546372SJosef 'Jeff' Sipek  * between mounts which makes NFS clients quite unhappy.
277*e6546372SJosef 'Jeff' Sipek  */
278fa9e4066Sahrens static void
279*e6546372SJosef 'Jeff' Sipek dsl_dataset_evict_sync(void *dbu)
280*e6546372SJosef 'Jeff' Sipek {
281*e6546372SJosef 'Jeff' Sipek 	dsl_dataset_t *ds = dbu;
282*e6546372SJosef 'Jeff' Sipek 
283*e6546372SJosef 'Jeff' Sipek 	ASSERT(ds->ds_owner == NULL);
284*e6546372SJosef 'Jeff' Sipek 
285*e6546372SJosef 'Jeff' Sipek 	unique_remove(ds->ds_fsid_guid);
286*e6546372SJosef 'Jeff' Sipek }
287*e6546372SJosef 'Jeff' Sipek 
288*e6546372SJosef 'Jeff' Sipek static void
289*e6546372SJosef 'Jeff' Sipek dsl_dataset_evict_async(void *dbu)
290fa9e4066Sahrens {
291bc9014e6SJustin Gibbs 	dsl_dataset_t *ds = dbu;
292fa9e4066Sahrens 
2933b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
294fa9e4066Sahrens 
295bc9014e6SJustin Gibbs 	ds->ds_dbuf = NULL;
296bc9014e6SJustin Gibbs 
297503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
298503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
299fa9e4066Sahrens 
300fa9e4066Sahrens 	if (ds->ds_prev) {
3013b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
302fa9e4066Sahrens 		ds->ds_prev = NULL;
303fa9e4066Sahrens 	}
304fa9e4066Sahrens 
305cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
306bc9014e6SJustin Gibbs 	if (ds->ds_deadlist.dl_os != NULL)
307cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
308745cd3c5Smaybee 	if (ds->ds_dir)
309bc9014e6SJustin Gibbs 		dsl_dir_async_rele(ds->ds_dir, ds);
310fa9e4066Sahrens 
31191ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
312fa9e4066Sahrens 
31303bad06fSJustin Gibbs 	list_destroy(&ds->ds_prop_cbs);
3145ad82045Snd150628 	mutex_destroy(&ds->ds_lock);
31591ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
316d2b3cbbdSJorgen Lundman 	mutex_destroy(&ds->ds_sendstream_lock);
3173b2aab18SMatthew Ahrens 	refcount_destroy(&ds->ds_longholds);
3185ad82045Snd150628 
319fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
320fa9e4066Sahrens }
321fa9e4066Sahrens 
3223b2aab18SMatthew Ahrens int
323fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
324fa9e4066Sahrens {
325fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
326fa9e4066Sahrens 	int err;
327fa9e4066Sahrens 	dmu_buf_t *headdbuf;
328fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
329fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
330fa9e4066Sahrens 
331fa9e4066Sahrens 	if (ds->ds_snapname[0])
332ea8dc4b6Seschrock 		return (0);
333c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
334ea8dc4b6Seschrock 		return (0);
335fa9e4066Sahrens 
336c1379625SJustin T. Gibbs 	err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
337ea8dc4b6Seschrock 	    FTAG, &headdbuf);
3383b2aab18SMatthew Ahrens 	if (err != 0)
339ea8dc4b6Seschrock 		return (err);
340fa9e4066Sahrens 	headphys = headdbuf->db_data;
341fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
342e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
343ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
344ea8dc4b6Seschrock 	return (err);
345fa9e4066Sahrens }
346fa9e4066Sahrens 
3473b2aab18SMatthew Ahrens int
348745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
349ab04eb8eStimh {
350745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
351c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
352ab04eb8eStimh 	matchtype_t mt;
353ab04eb8eStimh 	int err;
354ab04eb8eStimh 
355c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
356ab04eb8eStimh 		mt = MT_FIRST;
357ab04eb8eStimh 	else
358ab04eb8eStimh 		mt = MT_EXACT;
359ab04eb8eStimh 
360745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
361ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
362ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
363745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
364ab04eb8eStimh 	return (err);
365ab04eb8eStimh }
366ab04eb8eStimh 
3673b2aab18SMatthew Ahrens int
368a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
369a2afb611SJerry Jelinek     boolean_t adj_cnt)
370ab04eb8eStimh {
371745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
372c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
373ab04eb8eStimh 	matchtype_t mt;
374ab04eb8eStimh 	int err;
375ab04eb8eStimh 
37671eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
37771eb0538SChris Kirby 
378c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
379ab04eb8eStimh 		mt = MT_FIRST;
380ab04eb8eStimh 	else
381ab04eb8eStimh 		mt = MT_EXACT;
382ab04eb8eStimh 
383745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
384ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
385745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
386a2afb611SJerry Jelinek 
387a2afb611SJerry Jelinek 	if (err == 0 && adj_cnt)
388a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
389a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
390a2afb611SJerry Jelinek 
391ab04eb8eStimh 	return (err);
392ab04eb8eStimh }
393ab04eb8eStimh 
394e57a022bSJustin T. Gibbs boolean_t
395e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
396e57a022bSJustin T. Gibbs {
3979d47dec0SJustin T. Gibbs 	dmu_buf_t *dbuf = ds->ds_dbuf;
3989d47dec0SJustin T. Gibbs 	boolean_t result = B_FALSE;
3999d47dec0SJustin T. Gibbs 
4009d47dec0SJustin T. Gibbs 	if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
4019d47dec0SJustin T. Gibbs 	    ds->ds_object, DMU_BONUS_BLKID, tag)) {
4029d47dec0SJustin T. Gibbs 
4039d47dec0SJustin T. Gibbs 		if (ds == dmu_buf_get_user(dbuf))
4049d47dec0SJustin T. Gibbs 			result = B_TRUE;
4059d47dec0SJustin T. Gibbs 		else
4069d47dec0SJustin T. Gibbs 			dmu_buf_rele(dbuf, tag);
4079d47dec0SJustin T. Gibbs 	}
4089d47dec0SJustin T. Gibbs 
4099d47dec0SJustin T. Gibbs 	return (result);
410e57a022bSJustin T. Gibbs }
411e57a022bSJustin T. Gibbs 
4123b2aab18SMatthew Ahrens int
4133b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
414745cd3c5Smaybee     dsl_dataset_t **dsp)
415fa9e4066Sahrens {
416fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
417fa9e4066Sahrens 	dmu_buf_t *dbuf;
418fa9e4066Sahrens 	dsl_dataset_t *ds;
419ea8dc4b6Seschrock 	int err;
420a7f53a56SChris Kirby 	dmu_object_info_t doi;
421fa9e4066Sahrens 
4223b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
423fa9e4066Sahrens 
424ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
4253b2aab18SMatthew Ahrens 	if (err != 0)
426ea8dc4b6Seschrock 		return (err);
427a7f53a56SChris Kirby 
428a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
429a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
4302acef22dSMatthew Ahrens 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
431b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
432be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
433b287be1bSWill Andrews 	}
434a7f53a56SChris Kirby 
435fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
436fa9e4066Sahrens 	if (ds == NULL) {
437d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
438fa9e4066Sahrens 
439fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
440fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
441fa9e4066Sahrens 		ds->ds_object = dsobj;
442bc9014e6SJustin Gibbs 		ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
443fa9e4066Sahrens 
4445ad82045Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
44591ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
4464e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
4473b2aab18SMatthew Ahrens 		refcount_create(&ds->ds_longholds);
4485ad82045Snd150628 
449cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
450cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
451c1379625SJustin T. Gibbs 		    mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
452cde58dbcSMatthew Ahrens 
4534e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4544e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
4554e3c9f44SBill Pijewski 
45603bad06fSJustin Gibbs 		list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
45703bad06fSJustin Gibbs 		    offsetof(dsl_prop_cb_record_t, cbr_ds_node));
45803bad06fSJustin Gibbs 
459b5152584SMatthew Ahrens 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
460ca0cc391SMatthew Ahrens 			for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
461ca0cc391SMatthew Ahrens 				if (!(spa_feature_table[f].fi_flags &
462ca0cc391SMatthew Ahrens 				    ZFEATURE_FLAG_PER_DATASET))
463ca0cc391SMatthew Ahrens 					continue;
464ca0cc391SMatthew Ahrens 				err = zap_contains(mos, dsobj,
465ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_guid);
466ca0cc391SMatthew Ahrens 				if (err == 0) {
467ca0cc391SMatthew Ahrens 					ds->ds_feature_inuse[f] = B_TRUE;
468ca0cc391SMatthew Ahrens 				} else {
469ca0cc391SMatthew Ahrens 					ASSERT3U(err, ==, ENOENT);
470ca0cc391SMatthew Ahrens 					err = 0;
471ca0cc391SMatthew Ahrens 				}
472e1f3c208SJustin T. Gibbs 			}
473b5152584SMatthew Ahrens 		}
474b5152584SMatthew Ahrens 
4753b2aab18SMatthew Ahrens 		err = dsl_dir_hold_obj(dp,
476ca0cc391SMatthew Ahrens 		    dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
4773b2aab18SMatthew Ahrens 		if (err != 0) {
4785ad82045Snd150628 			mutex_destroy(&ds->ds_lock);
47991ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
480d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
4813b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
482cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
483cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
484ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
485ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
486ea8dc4b6Seschrock 			return (err);
487ea8dc4b6Seschrock 		}
488fa9e4066Sahrens 
489bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
490fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
491c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
4923b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
493c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_prev_snap_obj,
494745cd3c5Smaybee 				    ds, &ds->ds_prev);
495fa9e4066Sahrens 			}
49678f17100SMatthew Ahrens 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
49778f17100SMatthew Ahrens 				int zaperr = zap_lookup(mos, ds->ds_object,
49878f17100SMatthew Ahrens 				    DS_FIELD_BOOKMARK_NAMES,
49978f17100SMatthew Ahrens 				    sizeof (ds->ds_bookmarks), 1,
50078f17100SMatthew Ahrens 				    &ds->ds_bookmarks);
50178f17100SMatthew Ahrens 				if (zaperr != ENOENT)
50278f17100SMatthew Ahrens 					VERIFY0(zaperr);
50378f17100SMatthew Ahrens 			}
504842727c2SChris Kirby 		} else {
505842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
506ea8dc4b6Seschrock 				err = dsl_dataset_get_snapname(ds);
507c1379625SJustin T. Gibbs 			if (err == 0 &&
508c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
509842727c2SChris Kirby 				err = zap_count(
510842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
511c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_userrefs_obj,
512842727c2SChris Kirby 				    &ds->ds_userrefs);
513842727c2SChris Kirby 			}
514fa9e4066Sahrens 		}
515fa9e4066Sahrens 
516bc9014e6SJustin Gibbs 		if (err == 0 && !ds->ds_is_snapshot) {
5173b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
5183b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
5193b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
520cb625fb5Sck153898 			if (err == 0) {
5213b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
5223b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
5233b2aab18SMatthew Ahrens 				    &ds->ds_quota);
524cb625fb5Sck153898 			}
525cb625fb5Sck153898 		} else {
526cb625fb5Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
527cb625fb5Sck153898 		}
528cb625fb5Sck153898 
529*e6546372SJosef 'Jeff' Sipek 		dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
530*e6546372SJosef 'Jeff' Sipek 		    dsl_dataset_evict_async, &ds->ds_dbuf);
531bc9014e6SJustin Gibbs 		if (err == 0)
532bc9014e6SJustin Gibbs 			winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
533bc9014e6SJustin Gibbs 
534bc9014e6SJustin Gibbs 		if (err != 0 || winner != NULL) {
535cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
536cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
537745cd3c5Smaybee 			if (ds->ds_prev)
5383b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
5393b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
5405ad82045Snd150628 			mutex_destroy(&ds->ds_lock);
54191ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
542d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
5433b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
544fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
5453b2aab18SMatthew Ahrens 			if (err != 0) {
546ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
547ea8dc4b6Seschrock 				return (err);
548ea8dc4b6Seschrock 			}
549fa9e4066Sahrens 			ds = winner;
550fa9e4066Sahrens 		} else {
55191ebeef5Sahrens 			ds->ds_fsid_guid =
552c1379625SJustin T. Gibbs 			    unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
553*e6546372SJosef 'Jeff' Sipek 			if (ds->ds_fsid_guid !=
554*e6546372SJosef 'Jeff' Sipek 			    dsl_dataset_phys(ds)->ds_fsid_guid) {
555*e6546372SJosef 'Jeff' Sipek 				zfs_dbgmsg("ds_fsid_guid changed from "
556*e6546372SJosef 'Jeff' Sipek 				    "%llx to %llx for pool %s dataset id %llu",
557*e6546372SJosef 'Jeff' Sipek 				    (long long)
558*e6546372SJosef 'Jeff' Sipek 				    dsl_dataset_phys(ds)->ds_fsid_guid,
559*e6546372SJosef 'Jeff' Sipek 				    (long long)ds->ds_fsid_guid,
560*e6546372SJosef 'Jeff' Sipek 				    spa_name(dp->dp_spa),
561*e6546372SJosef 'Jeff' Sipek 				    dsobj);
562*e6546372SJosef 'Jeff' Sipek 			}
563fa9e4066Sahrens 		}
564fa9e4066Sahrens 	}
565fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
566c1379625SJustin T. Gibbs 	ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
567c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
568afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
56984db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
570ea8dc4b6Seschrock 	*dsp = ds;
571ea8dc4b6Seschrock 	return (0);
572fa9e4066Sahrens }
573fa9e4066Sahrens 
5743b2aab18SMatthew Ahrens int
5753b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name,
5763b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
577745cd3c5Smaybee {
5783b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
5793b2aab18SMatthew Ahrens 	const char *snapname;
5803b2aab18SMatthew Ahrens 	uint64_t obj;
5813b2aab18SMatthew Ahrens 	int err = 0;
582a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds;
583745cd3c5Smaybee 
5843b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
5853b2aab18SMatthew Ahrens 	if (err != 0)
5863b2aab18SMatthew Ahrens 		return (err);
587745cd3c5Smaybee 
5883b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
589c1379625SJustin T. Gibbs 	obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
5903b2aab18SMatthew Ahrens 	if (obj != 0)
591a2cdcdd2SPaul Dagnelie 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
5923b2aab18SMatthew Ahrens 	else
593be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
5943b2aab18SMatthew Ahrens 
5953b2aab18SMatthew Ahrens 	/* we may be looking for a snapshot */
5963b2aab18SMatthew Ahrens 	if (err == 0 && snapname != NULL) {
597a2cdcdd2SPaul Dagnelie 		dsl_dataset_t *snap_ds;
5983b2aab18SMatthew Ahrens 
5993b2aab18SMatthew Ahrens 		if (*snapname++ != '@') {
600a2cdcdd2SPaul Dagnelie 			dsl_dataset_rele(ds, tag);
6013b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
602be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
603745cd3c5Smaybee 		}
6043b2aab18SMatthew Ahrens 
6053b2aab18SMatthew Ahrens 		dprintf("looking for snapshot '%s'\n", snapname);
606a2cdcdd2SPaul Dagnelie 		err = dsl_dataset_snap_lookup(ds, snapname, &obj);
6073b2aab18SMatthew Ahrens 		if (err == 0)
608a2cdcdd2SPaul Dagnelie 			err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds);
609a2cdcdd2SPaul Dagnelie 		dsl_dataset_rele(ds, tag);
6103b2aab18SMatthew Ahrens 
6113b2aab18SMatthew Ahrens 		if (err == 0) {
612a2cdcdd2SPaul Dagnelie 			mutex_enter(&snap_ds->ds_lock);
613a2cdcdd2SPaul Dagnelie 			if (snap_ds->ds_snapname[0] == 0)
614a2cdcdd2SPaul Dagnelie 				(void) strlcpy(snap_ds->ds_snapname, snapname,
615a2cdcdd2SPaul Dagnelie 				    sizeof (snap_ds->ds_snapname));
616a2cdcdd2SPaul Dagnelie 			mutex_exit(&snap_ds->ds_lock);
617a2cdcdd2SPaul Dagnelie 			ds = snap_ds;
618a2cdcdd2SPaul Dagnelie 		}
619a2cdcdd2SPaul Dagnelie 	}
620a2cdcdd2SPaul Dagnelie 	if (err == 0)
6213b2aab18SMatthew Ahrens 		*dsp = ds;
6223b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
623745cd3c5Smaybee 	return (err);
624745cd3c5Smaybee }
625745cd3c5Smaybee 
626745cd3c5Smaybee int
6273b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
628503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
629745cd3c5Smaybee {
630503ad85cSMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
6313b2aab18SMatthew Ahrens 	if (err != 0)
632745cd3c5Smaybee 		return (err);
6333b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
634503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
6354f5064b7SMark J Musante 		*dsp = NULL;
636be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
637745cd3c5Smaybee 	}
638745cd3c5Smaybee 	return (0);
639745cd3c5Smaybee }
640745cd3c5Smaybee 
641745cd3c5Smaybee int
6423b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name,
643503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
644fa9e4066Sahrens {
6453b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold(dp, name, tag, dsp);
6463b2aab18SMatthew Ahrens 	if (err != 0)
647745cd3c5Smaybee 		return (err);
6483b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
649503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
650be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
651745cd3c5Smaybee 	}
652745cd3c5Smaybee 	return (0);
653fa9e4066Sahrens }
654fa9e4066Sahrens 
6553b2aab18SMatthew Ahrens /*
6563b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
6573b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
6583b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
6593b2aab18SMatthew Ahrens  *
6603b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
6613b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
6623b2aab18SMatthew Ahrens  * and accessed.
6633b2aab18SMatthew Ahrens  */
6643b2aab18SMatthew Ahrens void
6653b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
6663b2aab18SMatthew Ahrens {
6673b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
6683b2aab18SMatthew Ahrens 	(void) refcount_add(&ds->ds_longholds, tag);
6693b2aab18SMatthew Ahrens }
6703b2aab18SMatthew Ahrens 
6713b2aab18SMatthew Ahrens void
6723b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
6733b2aab18SMatthew Ahrens {
6743b2aab18SMatthew Ahrens 	(void) refcount_remove(&ds->ds_longholds, tag);
6753b2aab18SMatthew Ahrens }
6763b2aab18SMatthew Ahrens 
6773b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
6783b2aab18SMatthew Ahrens boolean_t
6793b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
6803b2aab18SMatthew Ahrens {
6813b2aab18SMatthew Ahrens 	return (!refcount_is_zero(&ds->ds_longholds));
6823b2aab18SMatthew Ahrens }
6833b2aab18SMatthew Ahrens 
684fa9e4066Sahrens void
685fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
686fa9e4066Sahrens {
687fa9e4066Sahrens 	if (ds == NULL) {
688fa9e4066Sahrens 		(void) strcpy(name, "mos");
689fa9e4066Sahrens 	} else {
690fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
6913b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
692fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
69340a5c998SMatthew Ahrens 			VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
69440a5c998SMatthew Ahrens 			    <, ZFS_MAX_DATASET_NAME_LEN);
695fa9e4066Sahrens 			/*
696fa9e4066Sahrens 			 * We use a "recursive" mutex so that we
697fa9e4066Sahrens 			 * can call dprintf_ds() with ds_lock held.
698fa9e4066Sahrens 			 */
699745cd3c5Smaybee 			if (!MUTEX_HELD(&ds->ds_lock)) {
700fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
70140a5c998SMatthew Ahrens 				VERIFY3U(strlcat(name, ds->ds_snapname,
70240a5c998SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN), <,
70340a5c998SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN);
704fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
705fa9e4066Sahrens 			} else {
70640a5c998SMatthew Ahrens 				VERIFY3U(strlcat(name, ds->ds_snapname,
70740a5c998SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN), <,
70840a5c998SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN);
709fa9e4066Sahrens 			}
710fa9e4066Sahrens 		}
711fa9e4066Sahrens 	}
712fa9e4066Sahrens }
713fa9e4066Sahrens 
71440a5c998SMatthew Ahrens int
71540a5c998SMatthew Ahrens dsl_dataset_namelen(dsl_dataset_t *ds)
71640a5c998SMatthew Ahrens {
71740a5c998SMatthew Ahrens 	VERIFY0(dsl_dataset_get_snapname(ds));
71840a5c998SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
71940a5c998SMatthew Ahrens 	int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname);
72040a5c998SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
72140a5c998SMatthew Ahrens 	return (len);
72240a5c998SMatthew Ahrens }
72340a5c998SMatthew Ahrens 
724088f3894Sahrens void
7253b2aab18SMatthew Ahrens dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
726fa9e4066Sahrens {
727ea8dc4b6Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
728fa9e4066Sahrens }
729fa9e4066Sahrens 
730fa9e4066Sahrens void
731503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
732745cd3c5Smaybee {
733d808a4fcSJustin T. Gibbs 	ASSERT3P(ds->ds_owner, ==, tag);
734d808a4fcSJustin T. Gibbs 	ASSERT(ds->ds_dbuf != NULL);
735745cd3c5Smaybee 
7363cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
737745cd3c5Smaybee 	ds->ds_owner = NULL;
7383cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
7393b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
7403b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, tag);
7413cb34c60Sahrens }
7423cb34c60Sahrens 
7433cb34c60Sahrens boolean_t
7443b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
7453cb34c60Sahrens {
746745cd3c5Smaybee 	boolean_t gotit = FALSE;
747745cd3c5Smaybee 
7489c3fd121SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
7493cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
7503b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
751503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
7523b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
753745cd3c5Smaybee 		gotit = TRUE;
7543cb34c60Sahrens 	}
7553cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
756745cd3c5Smaybee 	return (gotit);
757745cd3c5Smaybee }
758745cd3c5Smaybee 
7599c3fd121SMatthew Ahrens boolean_t
7609c3fd121SMatthew Ahrens dsl_dataset_has_owner(dsl_dataset_t *ds)
7619c3fd121SMatthew Ahrens {
7629c3fd121SMatthew Ahrens 	boolean_t rv;
7639c3fd121SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
7649c3fd121SMatthew Ahrens 	rv = (ds->ds_owner != NULL);
7659c3fd121SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
7669c3fd121SMatthew Ahrens 	return (rv);
7679c3fd121SMatthew Ahrens }
7689c3fd121SMatthew Ahrens 
769ca0cc391SMatthew Ahrens static void
770ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
771ca0cc391SMatthew Ahrens {
772ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
773ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
774ca0cc391SMatthew Ahrens 	uint64_t zero = 0;
775ca0cc391SMatthew Ahrens 
776ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
777ca0cc391SMatthew Ahrens 
778ca0cc391SMatthew Ahrens 	spa_feature_incr(spa, f, tx);
779ca0cc391SMatthew Ahrens 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
780ca0cc391SMatthew Ahrens 
781ca0cc391SMatthew Ahrens 	VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
782ca0cc391SMatthew Ahrens 	    sizeof (zero), 1, &zero, tx));
783ca0cc391SMatthew Ahrens }
784ca0cc391SMatthew Ahrens 
785ca0cc391SMatthew Ahrens void
786ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
787ca0cc391SMatthew Ahrens {
788ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
789ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
790ca0cc391SMatthew Ahrens 
791ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
792ca0cc391SMatthew Ahrens 
793ca0cc391SMatthew Ahrens 	VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
794ca0cc391SMatthew Ahrens 	spa_feature_decr(spa, f, tx);
795ca0cc391SMatthew Ahrens }
796ca0cc391SMatthew Ahrens 
7971d452cf5Sahrens uint64_t
798088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
799ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
800fa9e4066Sahrens {
8013cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
802fa9e4066Sahrens 	dmu_buf_t *dbuf;
803fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
8043cb34c60Sahrens 	uint64_t dsobj;
805fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
806fa9e4066Sahrens 
807088f3894Sahrens 	if (origin == NULL)
808088f3894Sahrens 		origin = dp->dp_origin_snap;
809088f3894Sahrens 
8103cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
811c1379625SJustin T. Gibbs 	ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
812fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
813c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
814fa9e4066Sahrens 
8151649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
8161649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
8173b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
818fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
819fa9e4066Sahrens 	dsphys = dbuf->db_data;
820745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
821fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
822ab04eb8eStimh 	dsphys->ds_flags = flags;
823fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
824fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
825fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
826fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
827ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
828ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
829fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
830088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
831a9799022Sck153898 
832cde58dbcSMatthew Ahrens 	if (origin == NULL) {
833cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
834cde58dbcSMatthew Ahrens 	} else {
8353b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
836cde58dbcSMatthew Ahrens 
8373cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
838fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
839c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_creation_txg;
840ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
841c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_referenced_bytes;
842fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
843c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_compressed_bytes;
844fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
845c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_uncompressed_bytes;
846c1379625SJustin T. Gibbs 		dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
84742fcb65eSMatthew Ahrens 
84842fcb65eSMatthew Ahrens 		/*
84942fcb65eSMatthew Ahrens 		 * Inherit flags that describe the dataset's contents
85042fcb65eSMatthew Ahrens 		 * (INCONSISTENT) or properties (Case Insensitive).
85142fcb65eSMatthew Ahrens 		 */
852c1379625SJustin T. Gibbs 		dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
85342fcb65eSMatthew Ahrens 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
854fa9e4066Sahrens 
855ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
856ca0cc391SMatthew Ahrens 			if (origin->ds_feature_inuse[f])
857ca0cc391SMatthew Ahrens 				dsl_dataset_activate_feature(dsobj, f, tx);
858ca0cc391SMatthew Ahrens 		}
859b5152584SMatthew Ahrens 
8603cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
861c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin)->ds_num_children++;
862fa9e4066Sahrens 
8633b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
864c1379625SJustin T. Gibbs 		    dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
865c1379625SJustin T. Gibbs 		    FTAG, &ohds));
866cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
867cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
868cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
869cde58dbcSMatthew Ahrens 
870088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
871c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
872c1379625SJustin T. Gibbs 				dsl_dataset_phys(origin)->ds_next_clones_obj =
873088f3894Sahrens 				    zap_create(mos,
874088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
875088f3894Sahrens 			}
8763b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
877c1379625SJustin T. Gibbs 			    dsl_dataset_phys(origin)->ds_next_clones_obj,
878c1379625SJustin T. Gibbs 			    dsobj, tx));
879088f3894Sahrens 		}
880088f3894Sahrens 
881fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
882c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
883cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
884c1379625SJustin T. Gibbs 			if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
885cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
886c1379625SJustin T. Gibbs 				dsl_dir_phys(origin->ds_dir)->dd_clones =
887cde58dbcSMatthew Ahrens 				    zap_create(mos,
888cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
889cde58dbcSMatthew Ahrens 			}
8903b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
891c1379625SJustin T. Gibbs 			    dsl_dir_phys(origin->ds_dir)->dd_clones,
892c1379625SJustin T. Gibbs 			    dsobj, tx));
893cde58dbcSMatthew Ahrens 		}
894fa9e4066Sahrens 	}
895ab04eb8eStimh 
896ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
897ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
898ab04eb8eStimh 
899ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
900fa9e4066Sahrens 
901fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
902c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
9033cb34c60Sahrens 
9043cb34c60Sahrens 	return (dsobj);
9053cb34c60Sahrens }
9063cb34c60Sahrens 
9073b2aab18SMatthew Ahrens static void
9083b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
9093b2aab18SMatthew Ahrens {
9103b2aab18SMatthew Ahrens 	objset_t *os;
9113b2aab18SMatthew Ahrens 
9123b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
9133b2aab18SMatthew Ahrens 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
9143b2aab18SMatthew Ahrens 	dsl_dataset_dirty(ds, tx);
9153b2aab18SMatthew Ahrens }
9163b2aab18SMatthew Ahrens 
9173cb34c60Sahrens uint64_t
918ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
919ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
9203cb34c60Sahrens {
9213cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
9223cb34c60Sahrens 	uint64_t dsobj, ddobj;
9233cb34c60Sahrens 	dsl_dir_t *dd;
9243cb34c60Sahrens 
9253b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
9263cb34c60Sahrens 	ASSERT(lastname[0] != '@');
9273cb34c60Sahrens 
928088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
9293b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
9303cb34c60Sahrens 
9313b2aab18SMatthew Ahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
9323b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
9333cb34c60Sahrens 
9343cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
9353cb34c60Sahrens 
936a2afb611SJerry Jelinek 	/*
937a2afb611SJerry Jelinek 	 * Since we're creating a new node we know it's a leaf, so we can
938a2afb611SJerry Jelinek 	 * initialize the counts if the limit feature is active.
939a2afb611SJerry Jelinek 	 */
940a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
941a2afb611SJerry Jelinek 		uint64_t cnt = 0;
942a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
943a2afb611SJerry Jelinek 
944a2afb611SJerry Jelinek 		dsl_dir_zapify(dd, tx);
945a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
946a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
947a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
948a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
949a2afb611SJerry Jelinek 	}
950a2afb611SJerry Jelinek 
9513b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
952fa9e4066Sahrens 
953feaa74e4SMark Maybee 	/*
954feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
955feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
956feaa74e4SMark Maybee 	 */
9573b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
958feaa74e4SMark Maybee 		dsl_dataset_t *ds;
959feaa74e4SMark Maybee 
9603b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
9613b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
962feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
963feaa74e4SMark Maybee 	}
964feaa74e4SMark Maybee 
9651d452cf5Sahrens 	return (dsobj);
9661d452cf5Sahrens }
9671d452cf5Sahrens 
96819b94df9SMatthew Ahrens /*
9693b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
9703b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
9713b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
9723b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
9733b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
9743b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
97519b94df9SMatthew Ahrens  */
9763b2aab18SMatthew Ahrens void
9773b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
9788ac09fceSRichard Lowe {
9793b2aab18SMatthew Ahrens 	uint64_t mrs_used;
9803b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
98119b94df9SMatthew Ahrens 
982bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
98319b94df9SMatthew Ahrens 
984c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
985c1379625SJustin T. Gibbs 		mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
9863b2aab18SMatthew Ahrens 	else
9873b2aab18SMatthew Ahrens 		mrs_used = 0;
98819b94df9SMatthew Ahrens 
9893b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
9901d452cf5Sahrens 
9913b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
992c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes =
993c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
994842727c2SChris Kirby 
9953b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
9963b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
997c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
9981d452cf5Sahrens }
9991d452cf5Sahrens 
10003b2aab18SMatthew Ahrens void
10013b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
10023b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1003842727c2SChris Kirby {
10043b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
100514843421SMatthew Ahrens 	uint64_t count;
10063b2aab18SMatthew Ahrens 	int err;
100714843421SMatthew Ahrens 
1008c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
1009c1379625SJustin T. Gibbs 	err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1010c1379625SJustin T. Gibbs 	    obj, tx);
1011fa9e4066Sahrens 	/*
10123b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
10133b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
10143b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
10153b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
10163b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
10173b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
10183b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
10193b2aab18SMatthew Ahrens 	 * remove this one.
1020fa9e4066Sahrens 	 */
10213b2aab18SMatthew Ahrens 	if (err != ENOENT)
10223b2aab18SMatthew Ahrens 		VERIFY0(err);
1023c1379625SJustin T. Gibbs 	ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
10243b2aab18SMatthew Ahrens 	    &count));
1025c1379625SJustin T. Gibbs 	ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
1026842727c2SChris Kirby }
1027842727c2SChris Kirby 
1028fa9e4066Sahrens 
1029c717a561Smaybee blkptr_t *
1030c717a561Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1031fa9e4066Sahrens {
1032c1379625SJustin T. Gibbs 	return (&dsl_dataset_phys(ds)->ds_bp);
1033fa9e4066Sahrens }
1034fa9e4066Sahrens 
1035fa9e4066Sahrens void
1036fa9e4066Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1037fa9e4066Sahrens {
1038fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1039fa9e4066Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1040fa9e4066Sahrens 	if (ds == NULL) {
1041fa9e4066Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1042fa9e4066Sahrens 	} else {
1043fa9e4066Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1044c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_bp = *bp;
1045fa9e4066Sahrens 	}
1046fa9e4066Sahrens }
1047fa9e4066Sahrens 
1048fa9e4066Sahrens spa_t *
1049fa9e4066Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1050fa9e4066Sahrens {
1051fa9e4066Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1052fa9e4066Sahrens }
1053fa9e4066Sahrens 
1054fa9e4066Sahrens void
1055fa9e4066Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1056fa9e4066Sahrens {
1057fa9e4066Sahrens 	dsl_pool_t *dp;
1058fa9e4066Sahrens 
1059fa9e4066Sahrens 	if (ds == NULL) /* this is the meta-objset */
1060fa9e4066Sahrens 		return;
1061fa9e4066Sahrens 
1062503ad85cSMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1063a2eea2e1Sahrens 
1064c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
1065a2eea2e1Sahrens 		panic("dirtying snapshot!");
1066fa9e4066Sahrens 
1067fa9e4066Sahrens 	dp = ds->ds_dir->dd_pool;
1068fa9e4066Sahrens 
10693b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1070fa9e4066Sahrens 		/* up the hold count until we can be written out */
1071fa9e4066Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1072fa9e4066Sahrens 	}
1073fa9e4066Sahrens }
1074fa9e4066Sahrens 
10752e2c1355SMatthew Ahrens boolean_t
10762e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
10772e2c1355SMatthew Ahrens {
10782e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
10792e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
10802e2c1355SMatthew Ahrens 		    ds, t))
10812e2c1355SMatthew Ahrens 			return (B_TRUE);
10822e2c1355SMatthew Ahrens 	}
10832e2c1355SMatthew Ahrens 	return (B_FALSE);
10842e2c1355SMatthew Ahrens }
10852e2c1355SMatthew Ahrens 
1086a9799022Sck153898 static int
1087a9799022Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1088a9799022Sck153898 {
1089a9799022Sck153898 	uint64_t asize;
1090a9799022Sck153898 
1091a9799022Sck153898 	if (!dmu_tx_is_syncing(tx))
1092a9799022Sck153898 		return (0);
1093a9799022Sck153898 
1094a9799022Sck153898 	/*
1095a9799022Sck153898 	 * If there's an fs-only reservation, any blocks that might become
1096a9799022Sck153898 	 * owned by the snapshot dataset must be accommodated by space
1097a9799022Sck153898 	 * outside of the reservation.
1098a9799022Sck153898 	 */
10993f9d6ad7SLin Ling 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1100c1379625SJustin T. Gibbs 	asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
11016e0cbcaaSMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1102be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
1103a9799022Sck153898 
1104a9799022Sck153898 	/*
11054445fffbSMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
1106a9799022Sck153898 	 * snapshot checks in this sync group.
1107a9799022Sck153898 	 */
1108a9799022Sck153898 	if (asize > 0)
1109a9799022Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1110a9799022Sck153898 
1111a9799022Sck153898 	return (0);
1112a9799022Sck153898 }
1113a9799022Sck153898 
11143b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg {
11153b2aab18SMatthew Ahrens 	nvlist_t *ddsa_snaps;
11163b2aab18SMatthew Ahrens 	nvlist_t *ddsa_props;
11173b2aab18SMatthew Ahrens 	nvlist_t *ddsa_errors;
1118a2afb611SJerry Jelinek 	cred_t *ddsa_cr;
11193b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t;
11203b2aab18SMatthew Ahrens 
11211d452cf5Sahrens int
11223b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1123a2afb611SJerry Jelinek     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
11241d452cf5Sahrens {
11253b2aab18SMatthew Ahrens 	int error;
11261d452cf5Sahrens 	uint64_t value;
1127fa9e4066Sahrens 
11283b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
11293b2aab18SMatthew Ahrens 
11303b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
11313b2aab18SMatthew Ahrens 		return (0);
11323b2aab18SMatthew Ahrens 
1133fa9e4066Sahrens 	/*
11341d452cf5Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
11351d452cf5Sahrens 	 * is already one, try again.
1136fa9e4066Sahrens 	 */
1137c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1138be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
1139fa9e4066Sahrens 
11401d452cf5Sahrens 	/*
11414445fffbSMatthew Ahrens 	 * Check for conflicting snapshot name.
11421d452cf5Sahrens 	 */
11433b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
11443b2aab18SMatthew Ahrens 	if (error == 0)
1145be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
11463b2aab18SMatthew Ahrens 	if (error != ENOENT)
11473b2aab18SMatthew Ahrens 		return (error);
11481d452cf5Sahrens 
1149ca48f36fSKeith M Wesolowski 	/*
1150ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
1151ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
1152ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
1153ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
1154ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1155ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
1156ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
1157ca48f36fSKeith M Wesolowski 	 */
1158ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
1159ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
1160ca48f36fSKeith M Wesolowski 
1161a2afb611SJerry Jelinek 	/*
1162a2afb611SJerry Jelinek 	 * Skip the check for temporary snapshots or if we have already checked
1163a2afb611SJerry Jelinek 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1164a2afb611SJerry Jelinek 	 * check the count here when we're receiving a stream.
1165a2afb611SJerry Jelinek 	 */
1166a2afb611SJerry Jelinek 	if (cnt != 0 && cr != NULL) {
1167a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1168a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1169a2afb611SJerry Jelinek 		if (error != 0)
1170a2afb611SJerry Jelinek 			return (error);
1171a2afb611SJerry Jelinek 	}
1172a2afb611SJerry Jelinek 
11733b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
11743b2aab18SMatthew Ahrens 	if (error != 0)
11753b2aab18SMatthew Ahrens 		return (error);
1176b7661cccSmmusante 
1177fa9e4066Sahrens 	return (0);
1178fa9e4066Sahrens }
1179fa9e4066Sahrens 
11803b2aab18SMatthew Ahrens static int
11813b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
11823b2aab18SMatthew Ahrens {
11833b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
11843b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11853b2aab18SMatthew Ahrens 	nvpair_t *pair;
11863b2aab18SMatthew Ahrens 	int rv = 0;
11873b2aab18SMatthew Ahrens 
1188a2afb611SJerry Jelinek 	/*
1189a2afb611SJerry Jelinek 	 * Pre-compute how many total new snapshots will be created for each
1190a2afb611SJerry Jelinek 	 * level in the tree and below. This is needed for validating the
1191a2afb611SJerry Jelinek 	 * snapshot limit when either taking a recursive snapshot or when
1192a2afb611SJerry Jelinek 	 * taking multiple snapshots.
1193a2afb611SJerry Jelinek 	 *
1194a2afb611SJerry Jelinek 	 * The problem is that the counts are not actually adjusted when
1195a2afb611SJerry Jelinek 	 * we are checking, only when we finally sync. For a single snapshot,
1196a2afb611SJerry Jelinek 	 * this is easy, the count will increase by 1 at each node up the tree,
1197a2afb611SJerry Jelinek 	 * but its more complicated for the recursive/multiple snapshot case.
1198a2afb611SJerry Jelinek 	 *
1199a2afb611SJerry Jelinek 	 * The dsl_fs_ss_limit_check function does recursively check the count
1200a2afb611SJerry Jelinek 	 * at each level up the tree but since it is validating each snapshot
1201a2afb611SJerry Jelinek 	 * independently we need to be sure that we are validating the complete
1202a2afb611SJerry Jelinek 	 * count for the entire set of snapshots. We do this by rolling up the
1203a2afb611SJerry Jelinek 	 * counts for each component of the name into an nvlist and then
1204a2afb611SJerry Jelinek 	 * checking each of those cases with the aggregated count.
1205a2afb611SJerry Jelinek 	 *
1206a2afb611SJerry Jelinek 	 * This approach properly handles not only the recursive snapshot
1207a2afb611SJerry Jelinek 	 * case (where we get all of those on the ddsa_snaps list) but also
1208a2afb611SJerry Jelinek 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1209a2afb611SJerry Jelinek 	 * validate the limit on 'a' using a count of 2).
1210a2afb611SJerry Jelinek 	 *
1211a2afb611SJerry Jelinek 	 * We validate the snapshot names in the third loop and only report
1212a2afb611SJerry Jelinek 	 * name errors once.
1213a2afb611SJerry Jelinek 	 */
1214a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1215a2afb611SJerry Jelinek 		nvlist_t *cnt_track = NULL;
1216a2afb611SJerry Jelinek 		cnt_track = fnvlist_alloc();
1217a2afb611SJerry Jelinek 
1218a2afb611SJerry Jelinek 		/* Rollup aggregated counts into the cnt_track list */
1219a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1220a2afb611SJerry Jelinek 		    pair != NULL;
1221a2afb611SJerry Jelinek 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1222a2afb611SJerry Jelinek 			char *pdelim;
1223a2afb611SJerry Jelinek 			uint64_t val;
1224a2afb611SJerry Jelinek 			char nm[MAXPATHLEN];
1225a2afb611SJerry Jelinek 
1226a2afb611SJerry Jelinek 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1227a2afb611SJerry Jelinek 			pdelim = strchr(nm, '@');
1228a2afb611SJerry Jelinek 			if (pdelim == NULL)
1229a2afb611SJerry Jelinek 				continue;
1230a2afb611SJerry Jelinek 			*pdelim = '\0';
1231a2afb611SJerry Jelinek 
1232a2afb611SJerry Jelinek 			do {
1233a2afb611SJerry Jelinek 				if (nvlist_lookup_uint64(cnt_track, nm,
1234a2afb611SJerry Jelinek 				    &val) == 0) {
1235a2afb611SJerry Jelinek 					/* update existing entry */
1236a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm,
1237a2afb611SJerry Jelinek 					    val + 1);
1238a2afb611SJerry Jelinek 				} else {
1239a2afb611SJerry Jelinek 					/* add to list */
1240a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm, 1);
1241a2afb611SJerry Jelinek 				}
1242a2afb611SJerry Jelinek 
1243a2afb611SJerry Jelinek 				pdelim = strrchr(nm, '/');
1244a2afb611SJerry Jelinek 				if (pdelim != NULL)
1245a2afb611SJerry Jelinek 					*pdelim = '\0';
1246a2afb611SJerry Jelinek 			} while (pdelim != NULL);
1247a2afb611SJerry Jelinek 		}
1248a2afb611SJerry Jelinek 
1249a2afb611SJerry Jelinek 		/* Check aggregated counts at each level */
1250a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1251a2afb611SJerry Jelinek 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1252a2afb611SJerry Jelinek 			int error = 0;
1253a2afb611SJerry Jelinek 			char *name;
1254a2afb611SJerry Jelinek 			uint64_t cnt = 0;
1255a2afb611SJerry Jelinek 			dsl_dataset_t *ds;
1256a2afb611SJerry Jelinek 
1257a2afb611SJerry Jelinek 			name = nvpair_name(pair);
1258a2afb611SJerry Jelinek 			cnt = fnvpair_value_uint64(pair);
1259a2afb611SJerry Jelinek 			ASSERT(cnt > 0);
1260a2afb611SJerry Jelinek 
1261a2afb611SJerry Jelinek 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1262a2afb611SJerry Jelinek 			if (error == 0) {
1263a2afb611SJerry Jelinek 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1264a2afb611SJerry Jelinek 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1265a2afb611SJerry Jelinek 				    ddsa->ddsa_cr);
1266a2afb611SJerry Jelinek 				dsl_dataset_rele(ds, FTAG);
1267a2afb611SJerry Jelinek 			}
1268a2afb611SJerry Jelinek 
1269a2afb611SJerry Jelinek 			if (error != 0) {
1270a2afb611SJerry Jelinek 				if (ddsa->ddsa_errors != NULL)
1271a2afb611SJerry Jelinek 					fnvlist_add_int32(ddsa->ddsa_errors,
1272a2afb611SJerry Jelinek 					    name, error);
1273a2afb611SJerry Jelinek 				rv = error;
1274a2afb611SJerry Jelinek 				/* only report one error for this check */
1275a2afb611SJerry Jelinek 				break;
1276a2afb611SJerry Jelinek 			}
1277a2afb611SJerry Jelinek 		}
1278a2afb611SJerry Jelinek 		nvlist_free(cnt_track);
1279a2afb611SJerry Jelinek 	}
1280a2afb611SJerry Jelinek 
12813b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
12823b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
12833b2aab18SMatthew Ahrens 		int error = 0;
12843b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
12853b2aab18SMatthew Ahrens 		char *name, *atp;
128640a5c998SMatthew Ahrens 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
12873b2aab18SMatthew Ahrens 
12883b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
128940a5c998SMatthew Ahrens 		if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1290be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
12913b2aab18SMatthew Ahrens 		if (error == 0) {
12923b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
12933b2aab18SMatthew Ahrens 			if (atp == NULL)
1294be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
12953b2aab18SMatthew Ahrens 			if (error == 0)
12963b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
12973b2aab18SMatthew Ahrens 		}
12983b2aab18SMatthew Ahrens 		if (error == 0)
12993b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
13003b2aab18SMatthew Ahrens 		if (error == 0) {
1301a2afb611SJerry Jelinek 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
13023b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
1303a2afb611SJerry Jelinek 			    atp + 1, tx, B_FALSE, 0, NULL);
13043b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
13053b2aab18SMatthew Ahrens 		}
13063b2aab18SMatthew Ahrens 
13073b2aab18SMatthew Ahrens 		if (error != 0) {
13083b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
13093b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
13103b2aab18SMatthew Ahrens 				    name, error);
13113b2aab18SMatthew Ahrens 			}
13123b2aab18SMatthew Ahrens 			rv = error;
13133b2aab18SMatthew Ahrens 		}
13143b2aab18SMatthew Ahrens 	}
1315a2afb611SJerry Jelinek 
13163b2aab18SMatthew Ahrens 	return (rv);
13173b2aab18SMatthew Ahrens }
13183b2aab18SMatthew Ahrens 
13191d452cf5Sahrens void
13203b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
13214445fffbSMatthew Ahrens     dmu_tx_t *tx)
1322fa9e4066Sahrens {
13233b2aab18SMatthew Ahrens 	static zil_header_t zero_zil;
13243b2aab18SMatthew Ahrens 
13251d452cf5Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1326fa9e4066Sahrens 	dmu_buf_t *dbuf;
1327fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
1328088f3894Sahrens 	uint64_t dsobj, crtxg;
1329fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
13303b2aab18SMatthew Ahrens 	objset_t *os;
1331fa9e4066Sahrens 
13323b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
13333b2aab18SMatthew Ahrens 
13343b2aab18SMatthew Ahrens 	/*
13353b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
13363b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
13373b2aab18SMatthew Ahrens 	 */
13383b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
13393b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
13403b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
13413b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
13423b2aab18SMatthew Ahrens 
1343a2afb611SJerry Jelinek 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1344fa9e4066Sahrens 
1345088f3894Sahrens 	/*
1346088f3894Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1347088f3894Sahrens 	 */
1348088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1349088f3894Sahrens 		crtxg = 1;
1350088f3894Sahrens 	else
1351088f3894Sahrens 		crtxg = tx->tx_txg;
1352088f3894Sahrens 
13531649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
13541649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13553b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1356fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1357fa9e4066Sahrens 	dsphys = dbuf->db_data;
1358745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
13591d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1360fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1361fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1362fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
1363c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1364c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1365fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1366fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1367fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1368088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
1369c1379625SJustin T. Gibbs 	dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1370c1379625SJustin T. Gibbs 	dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1371c1379625SJustin T. Gibbs 	dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1372c1379625SJustin T. Gibbs 	dsphys->ds_uncompressed_bytes =
1373c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1374c1379625SJustin T. Gibbs 	dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1375c1379625SJustin T. Gibbs 	dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1376ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1377fa9e4066Sahrens 
1378ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1379ca0cc391SMatthew Ahrens 		if (ds->ds_feature_inuse[f])
1380ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(dsobj, f, tx);
1381ca0cc391SMatthew Ahrens 	}
1382b5152584SMatthew Ahrens 
1383c1379625SJustin T. Gibbs 	ASSERT3U(ds->ds_prev != 0, ==,
1384c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
13851d452cf5Sahrens 	if (ds->ds_prev) {
1386088f3894Sahrens 		uint64_t next_clones_obj =
1387c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1388c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1389fa9e4066Sahrens 		    ds->ds_object ||
1390c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1391c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1392c1379625SJustin T. Gibbs 		    ds->ds_object) {
13931d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1394c1379625SJustin T. Gibbs 			ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1395c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1396c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1397088f3894Sahrens 		} else if (next_clones_obj != 0) {
13983b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1399c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
14003b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1401088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1402fa9e4066Sahrens 		}
1403fa9e4066Sahrens 	}
1404fa9e4066Sahrens 
1405a9799022Sck153898 	/*
1406a9799022Sck153898 	 * If we have a reference-reservation on this dataset, we will
1407a9799022Sck153898 	 * need to increase the amount of refreservation being charged
1408a9799022Sck153898 	 * since our unique space is going to zero.
1409a9799022Sck153898 	 */
1410a9799022Sck153898 	if (ds->ds_reserved) {
14113f9d6ad7SLin Ling 		int64_t delta;
14123f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1413c1379625SJustin T. Gibbs 		delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1414c1379625SJustin T. Gibbs 		    ds->ds_reserved);
141574e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
14163f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1417a9799022Sck153898 	}
1418a9799022Sck153898 
1419fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1420c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj =
1421c1379625SJustin T. Gibbs 	    dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1422c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1423cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1424c1379625SJustin T. Gibbs 	dsl_deadlist_open(&ds->ds_deadlist, mos,
1425c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1426cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
1427c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1428cde58dbcSMatthew Ahrens 
1429c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1430c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1431c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1432c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1433a9799022Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1434c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1435fa9e4066Sahrens 
1436c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
14373b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1438fa9e4066Sahrens 
1439fa9e4066Sahrens 	if (ds->ds_prev)
14403b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
14413b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
1442c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1443ecd6cf80Smarks 
14443f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1445088f3894Sahrens 
144671eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
144771eb0538SChris Kirby 
14484445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1449fa9e4066Sahrens }
1450fa9e4066Sahrens 
14513b2aab18SMatthew Ahrens static void
14523b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
14533b2aab18SMatthew Ahrens {
14543b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
14553b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14563b2aab18SMatthew Ahrens 	nvpair_t *pair;
14573b2aab18SMatthew Ahrens 
14583b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
14593b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
14603b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
14613b2aab18SMatthew Ahrens 		char *name, *atp;
146240a5c998SMatthew Ahrens 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
14633b2aab18SMatthew Ahrens 
14643b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
14653b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
14663b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
14673b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
14683b2aab18SMatthew Ahrens 
14693b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
14703b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
14713b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
14723b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
14733b2aab18SMatthew Ahrens 		}
14743b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14753b2aab18SMatthew Ahrens 	}
14763b2aab18SMatthew Ahrens }
14773b2aab18SMatthew Ahrens 
14783b2aab18SMatthew Ahrens /*
14793b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
14803b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
14813b2aab18SMatthew Ahrens  */
14823b2aab18SMatthew Ahrens int
14833b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
14843b2aab18SMatthew Ahrens {
14853b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
14863b2aab18SMatthew Ahrens 	nvpair_t *pair;
14873b2aab18SMatthew Ahrens 	boolean_t needsuspend;
14883b2aab18SMatthew Ahrens 	int error;
14893b2aab18SMatthew Ahrens 	spa_t *spa;
14903b2aab18SMatthew Ahrens 	char *firstname;
14913b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
14923b2aab18SMatthew Ahrens 
14933b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
14943b2aab18SMatthew Ahrens 	if (pair == NULL)
14953b2aab18SMatthew Ahrens 		return (0);
14963b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
14973b2aab18SMatthew Ahrens 
14983b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
14993b2aab18SMatthew Ahrens 	if (error != 0)
15003b2aab18SMatthew Ahrens 		return (error);
15013b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
15023b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
15033b2aab18SMatthew Ahrens 
15043b2aab18SMatthew Ahrens 	if (needsuspend) {
15053b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
15063b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
15073b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
150840a5c998SMatthew Ahrens 			char fsname[ZFS_MAX_DATASET_NAME_LEN];
15093b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
15103b2aab18SMatthew Ahrens 			char *atp;
15113b2aab18SMatthew Ahrens 			void *cookie;
15123b2aab18SMatthew Ahrens 
15133b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
15143b2aab18SMatthew Ahrens 			if (atp == NULL) {
1515be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
15163b2aab18SMatthew Ahrens 				break;
15173b2aab18SMatthew Ahrens 			}
15183b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
15193b2aab18SMatthew Ahrens 
15203b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
15213b2aab18SMatthew Ahrens 			if (error != 0)
15223b2aab18SMatthew Ahrens 				break;
15233b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
15243b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
15253b2aab18SMatthew Ahrens 		}
15263b2aab18SMatthew Ahrens 	}
15273b2aab18SMatthew Ahrens 
15283b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
15293b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
15303b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
1531a2afb611SJerry Jelinek 	ddsa.ddsa_cr = CRED();
15323b2aab18SMatthew Ahrens 
15333b2aab18SMatthew Ahrens 	if (error == 0) {
15343b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
15353b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
15367d46dc6cSMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
15373b2aab18SMatthew Ahrens 	}
15383b2aab18SMatthew Ahrens 
15393b2aab18SMatthew Ahrens 	if (suspended != NULL) {
15403b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
15413b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
15423b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
15433b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
15443b2aab18SMatthew Ahrens 		}
15453b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
15463b2aab18SMatthew Ahrens 	}
15473b2aab18SMatthew Ahrens 
15483b2aab18SMatthew Ahrens 	return (error);
15493b2aab18SMatthew Ahrens }
15503b2aab18SMatthew Ahrens 
15513b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
15523b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
15533b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
15543b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
15553b2aab18SMatthew Ahrens 	const char *ddsta_htag;
15563b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
15573b2aab18SMatthew Ahrens 
15583b2aab18SMatthew Ahrens static int
15593b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
15603b2aab18SMatthew Ahrens {
15613b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15623b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15633b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15643b2aab18SMatthew Ahrens 	int error;
15653b2aab18SMatthew Ahrens 
15663b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
15673b2aab18SMatthew Ahrens 	if (error != 0)
15683b2aab18SMatthew Ahrens 		return (error);
15693b2aab18SMatthew Ahrens 
1570a2afb611SJerry Jelinek 	/* NULL cred means no limit check for tmp snapshot */
1571ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1572a2afb611SJerry Jelinek 	    tx, B_FALSE, 0, NULL);
15733b2aab18SMatthew Ahrens 	if (error != 0) {
15743b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15753b2aab18SMatthew Ahrens 		return (error);
15763b2aab18SMatthew Ahrens 	}
15773b2aab18SMatthew Ahrens 
15783b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
15793b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1580be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
15813b2aab18SMatthew Ahrens 	}
15823b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
15833b2aab18SMatthew Ahrens 	    B_TRUE, tx);
15843b2aab18SMatthew Ahrens 	if (error != 0) {
15853b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15863b2aab18SMatthew Ahrens 		return (error);
15873b2aab18SMatthew Ahrens 	}
15883b2aab18SMatthew Ahrens 
15893b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15903b2aab18SMatthew Ahrens 	return (0);
15913b2aab18SMatthew Ahrens }
15923b2aab18SMatthew Ahrens 
15933b2aab18SMatthew Ahrens static void
15943b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
15953b2aab18SMatthew Ahrens {
15963b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15973b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15983b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15993b2aab18SMatthew Ahrens 
16003b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
16013b2aab18SMatthew Ahrens 
16023b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
16033b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
16043b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
16053b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
16063b2aab18SMatthew Ahrens 
16073b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
16083b2aab18SMatthew Ahrens }
16093b2aab18SMatthew Ahrens 
16103b2aab18SMatthew Ahrens int
16113b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
16123b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
16133b2aab18SMatthew Ahrens {
16143b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
16153b2aab18SMatthew Ahrens 	int error;
16163b2aab18SMatthew Ahrens 	spa_t *spa;
16173b2aab18SMatthew Ahrens 	boolean_t needsuspend;
16183b2aab18SMatthew Ahrens 	void *cookie;
16193b2aab18SMatthew Ahrens 
16203b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
16213b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
16223b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
16233b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
16243b2aab18SMatthew Ahrens 
16253b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
16263b2aab18SMatthew Ahrens 	if (error != 0)
16273b2aab18SMatthew Ahrens 		return (error);
16283b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
16293b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
16303b2aab18SMatthew Ahrens 
16313b2aab18SMatthew Ahrens 	if (needsuspend) {
16323b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
16333b2aab18SMatthew Ahrens 		if (error != 0)
16343b2aab18SMatthew Ahrens 			return (error);
16353b2aab18SMatthew Ahrens 	}
16363b2aab18SMatthew Ahrens 
16373b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
16387d46dc6cSMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
16393b2aab18SMatthew Ahrens 
16403b2aab18SMatthew Ahrens 	if (needsuspend)
16413b2aab18SMatthew Ahrens 		zil_resume(cookie);
16423b2aab18SMatthew Ahrens 	return (error);
16433b2aab18SMatthew Ahrens }
16443b2aab18SMatthew Ahrens 
16453b2aab18SMatthew Ahrens 
1646fa9e4066Sahrens void
1647c717a561Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1648fa9e4066Sahrens {
1649fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1650503ad85cSMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1651c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
1652fa9e4066Sahrens 
165391ebeef5Sahrens 	/*
165491ebeef5Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
165591ebeef5Sahrens 	 * sync it out now.
165691ebeef5Sahrens 	 */
165791ebeef5Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1658c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
165991ebeef5Sahrens 
16609c3fd121SMatthew Ahrens 	if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
16619c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16629c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
16639c3fd121SMatthew Ahrens 		    &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
16649c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16659c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
16669c3fd121SMatthew Ahrens 		    &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
16679c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16689c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
16699c3fd121SMatthew Ahrens 		    &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
16709c3fd121SMatthew Ahrens 		ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
16719c3fd121SMatthew Ahrens 		ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
16729c3fd121SMatthew Ahrens 		ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
16739c3fd121SMatthew Ahrens 	}
16749c3fd121SMatthew Ahrens 
1675503ad85cSMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
1676b5152584SMatthew Ahrens 
1677ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1678ca0cc391SMatthew Ahrens 		if (ds->ds_feature_activation_needed[f]) {
1679ca0cc391SMatthew Ahrens 			if (ds->ds_feature_inuse[f])
1680ca0cc391SMatthew Ahrens 				continue;
1681ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(ds->ds_object, f, tx);
1682ca0cc391SMatthew Ahrens 			ds->ds_feature_inuse[f] = B_TRUE;
1683ca0cc391SMatthew Ahrens 		}
1684b5152584SMatthew Ahrens 	}
1685fa9e4066Sahrens }
1686fa9e4066Sahrens 
168719b94df9SMatthew Ahrens static void
168819b94df9SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
168919b94df9SMatthew Ahrens {
169019b94df9SMatthew Ahrens 	uint64_t count = 0;
169119b94df9SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
169219b94df9SMatthew Ahrens 	zap_cursor_t zc;
169319b94df9SMatthew Ahrens 	zap_attribute_t za;
16943b2aab18SMatthew Ahrens 	nvlist_t *propval = fnvlist_alloc();
16953b2aab18SMatthew Ahrens 	nvlist_t *val = fnvlist_alloc();
169619b94df9SMatthew Ahrens 
16973b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
169819b94df9SMatthew Ahrens 
169919b94df9SMatthew Ahrens 	/*
17003b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
170119b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
170219b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
170319b94df9SMatthew Ahrens 	 */
1704c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1705c1379625SJustin T. Gibbs 		VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
170619b94df9SMatthew Ahrens 		    &count));
170719b94df9SMatthew Ahrens 	}
1708c1379625SJustin T. Gibbs 	if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
170919b94df9SMatthew Ahrens 		goto fail;
1710c1379625SJustin T. Gibbs 	for (zap_cursor_init(&zc, mos,
1711c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_next_clones_obj);
171219b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
171319b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
171419b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
171540a5c998SMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
17163b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
17173b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
171819b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
17193b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
172019b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
172119b94df9SMatthew Ahrens 	}
172219b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
17233b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
17243b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
172519b94df9SMatthew Ahrens fail:
172619b94df9SMatthew Ahrens 	nvlist_free(val);
172719b94df9SMatthew Ahrens 	nvlist_free(propval);
172819b94df9SMatthew Ahrens }
172919b94df9SMatthew Ahrens 
17309c3fd121SMatthew Ahrens static void
17319c3fd121SMatthew Ahrens get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv)
17329c3fd121SMatthew Ahrens {
17339c3fd121SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
17349c3fd121SMatthew Ahrens 
17359c3fd121SMatthew Ahrens 	if (dsl_dataset_has_resume_receive_state(ds)) {
17369c3fd121SMatthew Ahrens 		char *str;
17379c3fd121SMatthew Ahrens 		void *packed;
17389c3fd121SMatthew Ahrens 		uint8_t *compressed;
17399c3fd121SMatthew Ahrens 		uint64_t val;
17409c3fd121SMatthew Ahrens 		nvlist_t *token_nv = fnvlist_alloc();
17419c3fd121SMatthew Ahrens 		size_t packed_size, compressed_size;
17429c3fd121SMatthew Ahrens 
17439c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17449c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
17459c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "fromguid", val);
17469c3fd121SMatthew Ahrens 		}
17479c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17489c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
17499c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "object", val);
17509c3fd121SMatthew Ahrens 		}
17519c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17529c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
17539c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "offset", val);
17549c3fd121SMatthew Ahrens 		}
17559c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17569c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
17579c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "bytes", val);
17589c3fd121SMatthew Ahrens 		}
17599c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17609c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
17619c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "toguid", val);
17629c3fd121SMatthew Ahrens 		}
17639c3fd121SMatthew Ahrens 		char buf[256];
17649c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17659c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
17669c3fd121SMatthew Ahrens 			fnvlist_add_string(token_nv, "toname", buf);
17679c3fd121SMatthew Ahrens 		}
17689c3fd121SMatthew Ahrens 		if (zap_contains(dp->dp_meta_objset, ds->ds_object,
17699c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_EMBEDOK) == 0) {
17709c3fd121SMatthew Ahrens 			fnvlist_add_boolean(token_nv, "embedok");
17719c3fd121SMatthew Ahrens 		}
17729c3fd121SMatthew Ahrens 		packed = fnvlist_pack(token_nv, &packed_size);
17739c3fd121SMatthew Ahrens 		fnvlist_free(token_nv);
17749c3fd121SMatthew Ahrens 		compressed = kmem_alloc(packed_size, KM_SLEEP);
17759c3fd121SMatthew Ahrens 
17769c3fd121SMatthew Ahrens 		compressed_size = gzip_compress(packed, compressed,
17779c3fd121SMatthew Ahrens 		    packed_size, packed_size, 6);
17789c3fd121SMatthew Ahrens 
17799c3fd121SMatthew Ahrens 		zio_cksum_t cksum;
17809c3fd121SMatthew Ahrens 		fletcher_4_native(compressed, compressed_size, NULL, &cksum);
17819c3fd121SMatthew Ahrens 
17829c3fd121SMatthew Ahrens 		str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP);
17839c3fd121SMatthew Ahrens 		for (int i = 0; i < compressed_size; i++) {
17849c3fd121SMatthew Ahrens 			(void) sprintf(str + i * 2, "%02x", compressed[i]);
17859c3fd121SMatthew Ahrens 		}
17869c3fd121SMatthew Ahrens 		str[compressed_size * 2] = '\0';
17879c3fd121SMatthew Ahrens 		char *propval = kmem_asprintf("%u-%llx-%llx-%s",
17889c3fd121SMatthew Ahrens 		    ZFS_SEND_RESUME_TOKEN_VERSION,
17899c3fd121SMatthew Ahrens 		    (longlong_t)cksum.zc_word[0],
17909c3fd121SMatthew Ahrens 		    (longlong_t)packed_size, str);
17919c3fd121SMatthew Ahrens 		dsl_prop_nvlist_add_string(nv,
17929c3fd121SMatthew Ahrens 		    ZFS_PROP_RECEIVE_RESUME_TOKEN, propval);
17939c3fd121SMatthew Ahrens 		kmem_free(packed, packed_size);
17949c3fd121SMatthew Ahrens 		kmem_free(str, compressed_size * 2 + 1);
17959c3fd121SMatthew Ahrens 		kmem_free(compressed, packed_size);
17969c3fd121SMatthew Ahrens 		strfree(propval);
17979c3fd121SMatthew Ahrens 	}
17989c3fd121SMatthew Ahrens }
17999c3fd121SMatthew Ahrens 
1800fa9e4066Sahrens void
1801a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1802fa9e4066Sahrens {
18033b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1804187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
1805a9799022Sck153898 
18063b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
18073b2aab18SMatthew Ahrens 
1808c1379625SJustin T. Gibbs 	ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
1809c1379625SJustin T. Gibbs 	    (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
1810c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_compressed_bytes);
18114445fffbSMatthew Ahrens 
18124445fffbSMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
181377372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1814c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes);
18154445fffbSMatthew Ahrens 
1816bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
18174445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
18184445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1819c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_unique_bytes);
18204445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
18214445fffbSMatthew Ahrens 	} else {
1822b461c746SMatthew Ahrens 		if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
182340a5c998SMatthew Ahrens 			char buf[ZFS_MAX_DATASET_NAME_LEN];
1824b461c746SMatthew Ahrens 			dsl_dataset_name(ds->ds_prev, buf);
1825b461c746SMatthew Ahrens 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1826b461c746SMatthew Ahrens 		}
1827b461c746SMatthew Ahrens 
1828a2eea2e1Sahrens 		dsl_dir_stats(ds->ds_dir, nv);
18294445fffbSMatthew Ahrens 	}
1830fa9e4066Sahrens 
1831a9799022Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1832a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1833a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1834a9799022Sck153898 
1835a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1836c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_time);
1837a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1838c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_txg);
1839a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1840a9799022Sck153898 	    ds->ds_quota);
1841a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1842a9799022Sck153898 	    ds->ds_reserved);
1843c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1844c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_guid);
18451d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1846c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
18471d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
18481d713200SEric Schrock 	    ds->ds_object);
184992241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
185092241e0bSTom Erickson 	    ds->ds_userrefs);
1851842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1852842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1853fa9e4066Sahrens 
1854c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
185519b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
185619b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
185719b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
185819b94df9SMatthew Ahrens 
185919b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
1860c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
186119b94df9SMatthew Ahrens 		if (err == 0) {
186219b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
186319b94df9SMatthew Ahrens 			    &comp, &uncomp);
186419b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
186519b94df9SMatthew Ahrens 			if (err == 0) {
186619b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
186719b94df9SMatthew Ahrens 				    written);
186819b94df9SMatthew Ahrens 			}
186919b94df9SMatthew Ahrens 		}
187019b94df9SMatthew Ahrens 	}
18719c3fd121SMatthew Ahrens 
18729c3fd121SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds)) {
18739c3fd121SMatthew Ahrens 		/*
18749c3fd121SMatthew Ahrens 		 * A failed "newfs" (e.g. full) resumable receive leaves
18759c3fd121SMatthew Ahrens 		 * the stats set on this dataset.  Check here for the prop.
18769c3fd121SMatthew Ahrens 		 */
18779c3fd121SMatthew Ahrens 		get_receive_resume_stats(ds, nv);
18789c3fd121SMatthew Ahrens 
18799c3fd121SMatthew Ahrens 		/*
18809c3fd121SMatthew Ahrens 		 * A failed incremental resumable receive leaves the
18819c3fd121SMatthew Ahrens 		 * stats set on our child named "%recv".  Check the child
18829c3fd121SMatthew Ahrens 		 * for the prop.
18839c3fd121SMatthew Ahrens 		 */
188440a5c998SMatthew Ahrens 		/* 6 extra bytes for /%recv */
188540a5c998SMatthew Ahrens 		char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
18869c3fd121SMatthew Ahrens 		dsl_dataset_t *recv_ds;
18879c3fd121SMatthew Ahrens 		dsl_dataset_name(ds, recvname);
188840a5c998SMatthew Ahrens 		if (strlcat(recvname, "/", sizeof (recvname)) <
188940a5c998SMatthew Ahrens 		    sizeof (recvname) &&
189040a5c998SMatthew Ahrens 		    strlcat(recvname, recv_clone_name, sizeof (recvname)) <
189140a5c998SMatthew Ahrens 		    sizeof (recvname) &&
189240a5c998SMatthew Ahrens 		    dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) {
18939c3fd121SMatthew Ahrens 			get_receive_resume_stats(recv_ds, nv);
18949c3fd121SMatthew Ahrens 			dsl_dataset_rele(recv_ds, FTAG);
18959c3fd121SMatthew Ahrens 		}
18969c3fd121SMatthew Ahrens 	}
1897fa9e4066Sahrens }
1898fa9e4066Sahrens 
1899a2eea2e1Sahrens void
1900a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1901fa9e4066Sahrens {
19023b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
19033b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
19043b2aab18SMatthew Ahrens 
1905c1379625SJustin T. Gibbs 	stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
1906c1379625SJustin T. Gibbs 	stat->dds_inconsistent =
1907c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
1908c1379625SJustin T. Gibbs 	stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
19094445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
1910bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
1911a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
1912c1379625SJustin T. Gibbs 		stat->dds_num_clones =
1913c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_num_children - 1;
1914ebedde84SEric Taylor 	} else {
1915ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
1916ebedde84SEric Taylor 		stat->dds_num_clones = 0;
1917a2eea2e1Sahrens 
1918088f3894Sahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
1919a2eea2e1Sahrens 			dsl_dataset_t *ods;
1920a2eea2e1Sahrens 
19213b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
1922c1379625SJustin T. Gibbs 			    dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
1923c1379625SJustin T. Gibbs 			    FTAG, &ods));
19243cb34c60Sahrens 			dsl_dataset_name(ods, stat->dds_origin);
19253b2aab18SMatthew Ahrens 			dsl_dataset_rele(ods, FTAG);
1926a2eea2e1Sahrens 		}
1927a2eea2e1Sahrens 	}
19284445fffbSMatthew Ahrens }
1929a2eea2e1Sahrens 
1930a2eea2e1Sahrens uint64_t
1931a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1932a2eea2e1Sahrens {
193391ebeef5Sahrens 	return (ds->ds_fsid_guid);
1934a2eea2e1Sahrens }
1935a2eea2e1Sahrens 
1936a2eea2e1Sahrens void
1937a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
1938a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
1939a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1940a2eea2e1Sahrens {
1941c1379625SJustin T. Gibbs 	*refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
1942a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1943c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
1944c1379625SJustin T. Gibbs 		*availbytesp +=
1945c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
1946a9799022Sck153898 	if (ds->ds_quota != 0) {
1947a9799022Sck153898 		/*
1948a9799022Sck153898 		 * Adjust available bytes according to refquota
1949a9799022Sck153898 		 */
1950a9799022Sck153898 		if (*refdbytesp < ds->ds_quota)
1951a9799022Sck153898 			*availbytesp = MIN(*availbytesp,
1952a9799022Sck153898 			    ds->ds_quota - *refdbytesp);
1953a9799022Sck153898 		else
1954a9799022Sck153898 			*availbytesp = 0;
1955a9799022Sck153898 	}
1956c1379625SJustin T. Gibbs 	*usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
1957a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1958fa9e4066Sahrens }
1959fa9e4066Sahrens 
1960f18faf3fSek110237 boolean_t
196134f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1962f18faf3fSek110237 {
1963f18faf3fSek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1964f18faf3fSek110237 
19653b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
196634f2f8cfSMatthew Ahrens 	if (snap == NULL)
1967f18faf3fSek110237 		return (B_FALSE);
1968c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_bp.blk_birth >
1969c1379625SJustin T. Gibbs 	    dsl_dataset_phys(snap)->ds_creation_txg) {
197034f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
19716e0cbcaaSMatthew Ahrens 		/*
19726e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
19736e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
19746e0cbcaaSMatthew Ahrens 		 * modified.
19756e0cbcaaSMatthew Ahrens 		 */
19766e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
1977f18faf3fSek110237 			return (B_TRUE);
197834f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
19796e0cbcaaSMatthew Ahrens 			return (B_TRUE);
19806e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
198134f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
19826e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
19836e0cbcaaSMatthew Ahrens 	}
1984f18faf3fSek110237 	return (B_FALSE);
1985f18faf3fSek110237 }
1986f18faf3fSek110237 
19873b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
19883b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
19893b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
19903b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
19913b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
19923b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
19933b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
19943b2aab18SMatthew Ahrens 
19951d452cf5Sahrens /* ARGSUSED */
1996fa9e4066Sahrens static int
19973b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
19983b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1999fa9e4066Sahrens {
20003b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20013b2aab18SMatthew Ahrens 	int error;
2002fa9e4066Sahrens 	uint64_t val;
2003fa9e4066Sahrens 
20043b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
20053b2aab18SMatthew Ahrens 	if (error != 0) {
20063b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
20073b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
20083b2aab18SMatthew Ahrens 	}
2009fa9e4066Sahrens 
20103b2aab18SMatthew Ahrens 	/* new name should not exist */
20113b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
20123b2aab18SMatthew Ahrens 	if (error == 0)
2013be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
20143b2aab18SMatthew Ahrens 	else if (error == ENOENT)
20153b2aab18SMatthew Ahrens 		error = 0;
2016cdf5b4caSmmusante 
2017cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
20183b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
201940a5c998SMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2020be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
2021cdf5b4caSmmusante 
20223b2aab18SMatthew Ahrens 	return (error);
20233b2aab18SMatthew Ahrens }
20243b2aab18SMatthew Ahrens 
20253b2aab18SMatthew Ahrens static int
20263b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
20273b2aab18SMatthew Ahrens {
20283b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20293b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20303b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
20313b2aab18SMatthew Ahrens 	int error;
20323b2aab18SMatthew Ahrens 
20333b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
20343b2aab18SMatthew Ahrens 	if (error != 0)
20353b2aab18SMatthew Ahrens 		return (error);
20363b2aab18SMatthew Ahrens 
20373b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
20383b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
20393b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
20403b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
20413b2aab18SMatthew Ahrens 	} else {
20423b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
20433b2aab18SMatthew Ahrens 	}
20443b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
20453b2aab18SMatthew Ahrens 	return (error);
20463b2aab18SMatthew Ahrens }
20473b2aab18SMatthew Ahrens 
20483b2aab18SMatthew Ahrens static int
20493b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
20503b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
20513b2aab18SMatthew Ahrens {
20523b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20533b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
20543b2aab18SMatthew Ahrens 	uint64_t val;
20553b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
20563b2aab18SMatthew Ahrens 	int error;
20573b2aab18SMatthew Ahrens 
20583b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
20593b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
20603b2aab18SMatthew Ahrens 	if (error == ENOENT) {
20613b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
20623b2aab18SMatthew Ahrens 		return (0);
20633b2aab18SMatthew Ahrens 	}
20643b2aab18SMatthew Ahrens 
20653b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
20663b2aab18SMatthew Ahrens 
20673b2aab18SMatthew Ahrens 	/* log before we change the name */
20683b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
20693b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
20703b2aab18SMatthew Ahrens 
2071a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
2072a2afb611SJerry Jelinek 	    B_FALSE));
20733b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
20743b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
20753b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2076c1379625SJustin T. Gibbs 	VERIFY0(zap_add(dp->dp_meta_objset,
2077c1379625SJustin T. Gibbs 	    dsl_dataset_phys(hds)->ds_snapnames_zapobj,
20783b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
20793b2aab18SMatthew Ahrens 
20803b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
20813b2aab18SMatthew Ahrens 	return (0);
2082fa9e4066Sahrens }
2083fa9e4066Sahrens 
20841d452cf5Sahrens static void
20853b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
20861d452cf5Sahrens {
20873b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20883b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20891d452cf5Sahrens 	dsl_dataset_t *hds;
2090fa9e4066Sahrens 
20913b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
20923b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
20933b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
20943b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
20953b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
20963b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
20973b2aab18SMatthew Ahrens 	} else {
20983b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
20993b2aab18SMatthew Ahrens 	}
2100745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
2101fa9e4066Sahrens }
2102fa9e4066Sahrens 
2103fa9e4066Sahrens int
21043b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
21053b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
2106fa9e4066Sahrens {
21073b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
21083b2aab18SMatthew Ahrens 
21093b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
21103b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
21113b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
21123b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
21133b2aab18SMatthew Ahrens 
21143b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
21157d46dc6cSMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
21167d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
21173b2aab18SMatthew Ahrens }
21183b2aab18SMatthew Ahrens 
211991948b51SKeith M Wesolowski /*
212091948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
212191948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
212291948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
212391948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
212491948b51SKeith M Wesolowski  * when we release the long hold.
212591948b51SKeith M Wesolowski  */
212691948b51SKeith M Wesolowski static int
212791948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
212891948b51SKeith M Wesolowski {
212991948b51SKeith M Wesolowski 	boolean_t held;
213091948b51SKeith M Wesolowski 
213191948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
213291948b51SKeith M Wesolowski 		return (0);
213391948b51SKeith M Wesolowski 
213491948b51SKeith M Wesolowski 	if (owner != NULL) {
213591948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
213691948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
213791948b51SKeith M Wesolowski 	}
213891948b51SKeith M Wesolowski 
213991948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
214091948b51SKeith M Wesolowski 
214191948b51SKeith M Wesolowski 	if (owner != NULL)
214291948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
214391948b51SKeith M Wesolowski 
214491948b51SKeith M Wesolowski 	if (held)
214591948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
214691948b51SKeith M Wesolowski 
214791948b51SKeith M Wesolowski 	return (0);
214891948b51SKeith M Wesolowski }
214991948b51SKeith M Wesolowski 
215091948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg {
215191948b51SKeith M Wesolowski 	const char *ddra_fsname;
215291948b51SKeith M Wesolowski 	void *ddra_owner;
2153a7027df1SMatthew Ahrens 	nvlist_t *ddra_result;
215491948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t;
215591948b51SKeith M Wesolowski 
21563b2aab18SMatthew Ahrens static int
21573b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
21583b2aab18SMatthew Ahrens {
215991948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
21603b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
21611d452cf5Sahrens 	dsl_dataset_t *ds;
21623b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
21633b2aab18SMatthew Ahrens 	int error;
2164fa9e4066Sahrens 
216591948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
21663b2aab18SMatthew Ahrens 	if (error != 0)
21673b2aab18SMatthew Ahrens 		return (error);
2168370c1af0SSanjeev Bagewadi 
21693b2aab18SMatthew Ahrens 	/* must not be a snapshot */
2170bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
21713b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2172be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21733b2aab18SMatthew Ahrens 	}
21741d452cf5Sahrens 
21753b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
2176c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
21773b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2178be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21793b2aab18SMatthew Ahrens 	}
21801d452cf5Sahrens 
218178f17100SMatthew Ahrens 	/* must not have any bookmarks after the most recent snapshot */
218278f17100SMatthew Ahrens 	nvlist_t *proprequest = fnvlist_alloc();
218378f17100SMatthew Ahrens 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
218478f17100SMatthew Ahrens 	nvlist_t *bookmarks = fnvlist_alloc();
218578f17100SMatthew Ahrens 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
218678f17100SMatthew Ahrens 	fnvlist_free(proprequest);
218778f17100SMatthew Ahrens 	if (error != 0)
218878f17100SMatthew Ahrens 		return (error);
218978f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
219078f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
219178f17100SMatthew Ahrens 		nvlist_t *valuenv =
219278f17100SMatthew Ahrens 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
219378f17100SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
219478f17100SMatthew Ahrens 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
2195c1379625SJustin T. Gibbs 		if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
219678f17100SMatthew Ahrens 			fnvlist_free(bookmarks);
219778f17100SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
219878f17100SMatthew Ahrens 			return (SET_ERROR(EEXIST));
219978f17100SMatthew Ahrens 		}
220078f17100SMatthew Ahrens 	}
220178f17100SMatthew Ahrens 	fnvlist_free(bookmarks);
220278f17100SMatthew Ahrens 
220391948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
220491948b51SKeith M Wesolowski 	if (error != 0) {
22053b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
220691948b51SKeith M Wesolowski 		return (error);
22073b2aab18SMatthew Ahrens 	}
22081d452cf5Sahrens 
22093b2aab18SMatthew Ahrens 	/*
22103b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
22113b2aab18SMatthew Ahrens 	 * the refquota.
22123b2aab18SMatthew Ahrens 	 */
22133b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
2214c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
22153b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2216be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
22173b2aab18SMatthew Ahrens 	}
22183b2aab18SMatthew Ahrens 
22193b2aab18SMatthew Ahrens 	/*
22203b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
22213b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
22223b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
22233b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
22243b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
22253b2aab18SMatthew Ahrens 	 */
22263b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2227c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
22283b2aab18SMatthew Ahrens 
22293b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
22303b2aab18SMatthew Ahrens 	    unused_refres_delta >
22313b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
22323b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2233be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
22343b2aab18SMatthew Ahrens 	}
22353b2aab18SMatthew Ahrens 
22363b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
22373b2aab18SMatthew Ahrens 	return (0);
22383b2aab18SMatthew Ahrens }
22393b2aab18SMatthew Ahrens 
22403b2aab18SMatthew Ahrens static void
22413b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
22423b2aab18SMatthew Ahrens {
224391948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
22443b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
22453b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
22463b2aab18SMatthew Ahrens 	uint64_t cloneobj;
224740a5c998SMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
22483b2aab18SMatthew Ahrens 
224991948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
22503b2aab18SMatthew Ahrens 
2251a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
2252a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2253a7027df1SMatthew Ahrens 
22543b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
22553b2aab18SMatthew Ahrens 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
22563b2aab18SMatthew Ahrens 
22573b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
22583b2aab18SMatthew Ahrens 
22593b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
22603b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
22613b2aab18SMatthew Ahrens 
22623b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
22633b2aab18SMatthew Ahrens 
22643b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
2265745cd3c5Smaybee 	dsl_dataset_rele(ds, FTAG);
2266cdf5b4caSmmusante }
22671d452cf5Sahrens 
226891948b51SKeith M Wesolowski /*
2269a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
2270a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
2271a7027df1SMatthew Ahrens  * in the result nvlist.
227291948b51SKeith M Wesolowski  *
2273a7027df1SMatthew Ahrens  * If owner != NULL:
227491948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
227591948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
227691948b51SKeith M Wesolowski  *   succeed or not.
227791948b51SKeith M Wesolowski  *
227891948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
227991948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
228091948b51SKeith M Wesolowski  */
22813b2aab18SMatthew Ahrens int
2282a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
22833b2aab18SMatthew Ahrens {
228491948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
228591948b51SKeith M Wesolowski 
228691948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
228791948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
2288a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
228991948b51SKeith M Wesolowski 
22903b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
22917d46dc6cSMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra,
22927d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
2293fa9e4066Sahrens }
229499653d4eSeschrock 
2295088f3894Sahrens struct promotenode {
2296745cd3c5Smaybee 	list_node_t link;
2297745cd3c5Smaybee 	dsl_dataset_t *ds;
2298745cd3c5Smaybee };
2299745cd3c5Smaybee 
23003b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg {
23013b2aab18SMatthew Ahrens 	const char *ddpa_clonename;
23023b2aab18SMatthew Ahrens 	dsl_dataset_t *ddpa_clone;
230374e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
23043b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_origin; /* origin of the origin */
230574e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2306681d9761SEric Taylor 	char *err_ds;
2307a2afb611SJerry Jelinek 	cred_t *cr;
23083b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t;
23091d452cf5Sahrens 
231074e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
23113b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
23123b2aab18SMatthew Ahrens     void *tag);
23133b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
231474e7dc98SMatthew Ahrens 
231599653d4eSeschrock static int
23163b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
231799653d4eSeschrock {
23183b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
23193b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
23203b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
23213b2aab18SMatthew Ahrens 	struct promotenode *snap;
23223b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
2323745cd3c5Smaybee 	int err;
2324cde58dbcSMatthew Ahrens 	uint64_t unused;
2325a2afb611SJerry Jelinek 	uint64_t ss_mv_cnt;
2326cb5842f8SAndriy Gapon 	size_t max_snap_len;
23271d452cf5Sahrens 
23283b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
23293b2aab18SMatthew Ahrens 	if (err != 0)
23303b2aab18SMatthew Ahrens 		return (err);
233199653d4eSeschrock 
23323b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
2333cb5842f8SAndriy Gapon 	max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
23341d452cf5Sahrens 
2335c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
23363b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
2337be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
23383b2aab18SMatthew Ahrens 	}
23393b2aab18SMatthew Ahrens 
23403b2aab18SMatthew Ahrens 	/*
23413b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
23423b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
23433b2aab18SMatthew Ahrens 	 */
23443b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
23453b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
23463b2aab18SMatthew Ahrens 		return (0);
23473b2aab18SMatthew Ahrens 	}
23483b2aab18SMatthew Ahrens 
23493b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
23503b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
235199653d4eSeschrock 
235274e7dc98SMatthew Ahrens 	/* compute origin's new unique space */
23533b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2354c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2355c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2356cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2357c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
23583b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
2359745cd3c5Smaybee 
2360745cd3c5Smaybee 	/*
2361745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2362745cd3c5Smaybee 	 *
236374e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
23643b2aab18SMatthew Ahrens 	 * to used by each snapshot:
236574e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
236674e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
236774e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2368745cd3c5Smaybee 	 * So a sequence would look like:
236974e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2370745cd3c5Smaybee 	 * Which simplifies to:
237174e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2372745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
237374e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2374745cd3c5Smaybee 	 */
2375a2afb611SJerry Jelinek 	ss_mv_cnt = 0;
2376c1379625SJustin T. Gibbs 	ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2377c1379625SJustin T. Gibbs 	ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2378c1379625SJustin T. Gibbs 	ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
23793b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
23803b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
238199653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2382745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
238399653d4eSeschrock 
2384a2afb611SJerry Jelinek 		ss_mv_cnt++;
2385a2afb611SJerry Jelinek 
23863b2aab18SMatthew Ahrens 		/*
23873b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
23883b2aab18SMatthew Ahrens 		 * the objset.
23893b2aab18SMatthew Ahrens 		 */
23903b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
2391be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
23923b2aab18SMatthew Ahrens 			goto out;
23933b2aab18SMatthew Ahrens 		}
23943b2aab18SMatthew Ahrens 
239599653d4eSeschrock 		/* Check that the snapshot name does not conflict */
23963b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
2397cb5842f8SAndriy Gapon 		if (strlen(ds->ds_snapname) >= max_snap_len) {
2398cb5842f8SAndriy Gapon 			err = SET_ERROR(ENAMETOOLONG);
2399cb5842f8SAndriy Gapon 			goto out;
2400cb5842f8SAndriy Gapon 		}
2401745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2402681d9761SEric Taylor 		if (err == 0) {
24033b2aab18SMatthew Ahrens 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2404be6fd75aSMatthew Ahrens 			err = SET_ERROR(EEXIST);
2405681d9761SEric Taylor 			goto out;
2406681d9761SEric Taylor 		}
2407745cd3c5Smaybee 		if (err != ENOENT)
2408681d9761SEric Taylor 			goto out;
240999653d4eSeschrock 
2410745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
2411c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
241274e7dc98SMatthew Ahrens 			continue;
241374e7dc98SMatthew Ahrens 
2414cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2415cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
24163b2aab18SMatthew Ahrens 		ddpa->used += dlused;
24173b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
24183b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
2419745cd3c5Smaybee 	}
242099653d4eSeschrock 
242199653d4eSeschrock 	/*
2422745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
2423745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
242499653d4eSeschrock 	 */
24253b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
2426c1379625SJustin T. Gibbs 		ddpa->used -=
2427c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2428c1379625SJustin T. Gibbs 		ddpa->comp -=
2429c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
24303b2aab18SMatthew Ahrens 		ddpa->uncomp -=
2431c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->
2432c1379625SJustin T. Gibbs 		    ds_uncompressed_bytes;
243399653d4eSeschrock 	}
2434745cd3c5Smaybee 
2435a2afb611SJerry Jelinek 	/* Check that there is enough space and limit headroom here */
243674e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2437a2afb611SJerry Jelinek 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
24383b2aab18SMatthew Ahrens 	if (err != 0)
24393b2aab18SMatthew Ahrens 		goto out;
244074e7dc98SMatthew Ahrens 
244174e7dc98SMatthew Ahrens 	/*
244274e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
244374e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
244474e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
244574e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
244674e7dc98SMatthew Ahrens 	 */
2447c1379625SJustin T. Gibbs 	if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
244874e7dc98SMatthew Ahrens 		uint64_t space;
244974e7dc98SMatthew Ahrens 
245074e7dc98SMatthew Ahrens 		/*
245174e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
24523f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
2453cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
245474e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
245574e7dc98SMatthew Ahrens 		 * iterate over all bps.
245674e7dc98SMatthew Ahrens 		 */
24573b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
24583b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
24593b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
24603b2aab18SMatthew Ahrens 		if (err != 0)
24613b2aab18SMatthew Ahrens 			goto out;
246274e7dc98SMatthew Ahrens 
24633b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
24643f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
24653b2aab18SMatthew Ahrens 		if (err != 0)
24663b2aab18SMatthew Ahrens 			goto out;
24673b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
246874e7dc98SMatthew Ahrens 	}
2469c1379625SJustin T. Gibbs 	if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2470c1379625SJustin T. Gibbs 	    DD_FLAG_USED_BREAKDOWN) {
24713b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
2472c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_creation_txg,
2473c1379625SJustin T. Gibbs 		    &ddpa->originusedsnap);
24743b2aab18SMatthew Ahrens 		if (err != 0)
24753b2aab18SMatthew Ahrens 			goto out;
2476745cd3c5Smaybee 	}
247799653d4eSeschrock 
2478681d9761SEric Taylor out:
24793b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2480681d9761SEric Taylor 	return (err);
248199653d4eSeschrock }
248299653d4eSeschrock 
24831d452cf5Sahrens static void
24843b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
24851d452cf5Sahrens {
24863b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
24873b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
24883b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
24893b2aab18SMatthew Ahrens 	struct promotenode *snap;
24903b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
249174e7dc98SMatthew Ahrens 	dsl_dataset_t *origin_head;
24923b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
24933cb34c60Sahrens 	dsl_dir_t *odd = NULL;
2494088f3894Sahrens 	uint64_t oldnext_obj;
249574e7dc98SMatthew Ahrens 	int64_t delta;
24961d452cf5Sahrens 
24973b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
24983b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
24991d452cf5Sahrens 
2500c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
25013b2aab18SMatthew Ahrens 
25023b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
25033b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
25043b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
25053b2aab18SMatthew Ahrens 
25063b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
250774e7dc98SMatthew Ahrens 	origin_head = snap->ds;
250874e7dc98SMatthew Ahrens 
25090b69c2f0Sahrens 	/*
25103cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
25110b69c2f0Sahrens 	 * changing.
25120b69c2f0Sahrens 	 */
25133b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
25143cb34c60Sahrens 	    NULL, FTAG, &odd));
25151d452cf5Sahrens 
2516745cd3c5Smaybee 	/* change origin's next snap */
2517745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2518c1379625SJustin T. Gibbs 	oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
25193b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2520c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2521c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2522c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2523745cd3c5Smaybee 
2524088f3894Sahrens 	/* change the origin's next clone */
2525c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
25263b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
25273b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
25283b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2529c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2530088f3894Sahrens 		    oldnext_obj, tx));
2531088f3894Sahrens 	}
2532088f3894Sahrens 
2533745cd3c5Smaybee 	/* change origin */
2534745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2535c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2536c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
25373f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2538745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2539c1379625SJustin T. Gibbs 	dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
25403f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
2541c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
2542745cd3c5Smaybee 
2543cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2544cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
25453b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2546c1379625SJustin T. Gibbs 		    dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
25473b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2548c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2549cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2550cde58dbcSMatthew Ahrens 
25513b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2552c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2553cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
2554c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_clones == 0) {
2555c1379625SJustin T. Gibbs 			dsl_dir_phys(dd)->dd_clones =
2556c1379625SJustin T. Gibbs 			    zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2557c1379625SJustin T. Gibbs 			    DMU_OT_NONE, 0, tx);
2558cde58dbcSMatthew Ahrens 		}
25593b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2560c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2561cde58dbcSMatthew Ahrens 	}
2562cde58dbcSMatthew Ahrens 
25631d452cf5Sahrens 	/* move snapshots to this dir */
25643b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
25653b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2566745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
25671d452cf5Sahrens 
25683b2aab18SMatthew Ahrens 		/*
25693b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
25703b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
25713b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
25723b2aab18SMatthew Ahrens 		 */
2573503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2574503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2575503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
25763baa08fcSek110237 		}
25773b2aab18SMatthew Ahrens 
25781d452cf5Sahrens 		/* move snap name entry */
25793b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
25803b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2581a2afb611SJerry Jelinek 		    ds->ds_snapname, tx, B_TRUE));
25823b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
2583c1379625SJustin T. Gibbs 		    dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
25841d452cf5Sahrens 		    8, 1, &ds->ds_object, tx));
2585a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2586a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2587cde58dbcSMatthew Ahrens 
25881d452cf5Sahrens 		/* change containing dsl_dir */
25891d452cf5Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2590c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
2591c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
25923cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
25933b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
25943b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
25951d452cf5Sahrens 		    NULL, ds, &ds->ds_dir));
25961d452cf5Sahrens 
2597cde58dbcSMatthew Ahrens 		/* move any clone references */
2598c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
2599cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2600cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2601cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2602cde58dbcSMatthew Ahrens 
2603cde58dbcSMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
2604c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
2605cde58dbcSMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
2606cde58dbcSMatthew Ahrens 			    zap_cursor_advance(&zc)) {
2607cde58dbcSMatthew Ahrens 				dsl_dataset_t *cnds;
2608cde58dbcSMatthew Ahrens 				uint64_t o;
2609cde58dbcSMatthew Ahrens 
2610cde58dbcSMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
2611cde58dbcSMatthew Ahrens 					/*
2612cde58dbcSMatthew Ahrens 					 * We've already moved the
2613cde58dbcSMatthew Ahrens 					 * origin's reference.
2614cde58dbcSMatthew Ahrens 					 */
2615cde58dbcSMatthew Ahrens 					continue;
2616cde58dbcSMatthew Ahrens 				}
2617cde58dbcSMatthew Ahrens 
26183b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
2619cde58dbcSMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
2620c1379625SJustin T. Gibbs 				o = dsl_dir_phys(cnds->ds_dir)->
2621c1379625SJustin T. Gibbs 				    dd_head_dataset_obj;
2622cde58dbcSMatthew Ahrens 
26233b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
2624c1379625SJustin T. Gibbs 				    dsl_dir_phys(odd)->dd_clones, o, tx));
26253b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
2626c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd)->dd_clones, o, tx));
2627cde58dbcSMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
2628cde58dbcSMatthew Ahrens 			}
2629cde58dbcSMatthew Ahrens 			zap_cursor_fini(&zc);
2630cde58dbcSMatthew Ahrens 		}
2631cde58dbcSMatthew Ahrens 
26323b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
263374e7dc98SMatthew Ahrens 	}
26341d452cf5Sahrens 
263574e7dc98SMatthew Ahrens 	/*
263674e7dc98SMatthew Ahrens 	 * Change space accounting.
263774e7dc98SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
263874e7dc98SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
263974e7dc98SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
264074e7dc98SMatthew Ahrens 	 */
264174e7dc98SMatthew Ahrens 
26423b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
2643c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
264474e7dc98SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
26453b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
264674e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
264774e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
26483b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
264974e7dc98SMatthew Ahrens 
26503b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
2651c1379625SJustin T. Gibbs 	    dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
265274e7dc98SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
26533b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
265474e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
265574e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
26563b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
265774e7dc98SMatthew Ahrens 
2658c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
26591d452cf5Sahrens 
2660ecd6cf80Smarks 	/* log history record */
26614445fffbSMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
2662ecd6cf80Smarks 
26633b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
26643b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
26651d452cf5Sahrens }
26661d452cf5Sahrens 
266774e7dc98SMatthew Ahrens /*
266874e7dc98SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
266974e7dc98SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
267074e7dc98SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
267174e7dc98SMatthew Ahrens  * snapshots back to this dataset's origin.
267274e7dc98SMatthew Ahrens  */
267374e7dc98SMatthew Ahrens static int
26743b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
26753b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
267674e7dc98SMatthew Ahrens {
267774e7dc98SMatthew Ahrens 	uint64_t obj = last_obj;
267874e7dc98SMatthew Ahrens 
267974e7dc98SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
268074e7dc98SMatthew Ahrens 	    offsetof(struct promotenode, link));
268174e7dc98SMatthew Ahrens 
268274e7dc98SMatthew Ahrens 	while (obj != first_obj) {
268374e7dc98SMatthew Ahrens 		dsl_dataset_t *ds;
268474e7dc98SMatthew Ahrens 		struct promotenode *snap;
268574e7dc98SMatthew Ahrens 		int err;
268674e7dc98SMatthew Ahrens 
26873b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
26883b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
26893b2aab18SMatthew Ahrens 		if (err != 0)
269074e7dc98SMatthew Ahrens 			return (err);
269174e7dc98SMatthew Ahrens 
269274e7dc98SMatthew Ahrens 		if (first_obj == 0)
2693c1379625SJustin T. Gibbs 			first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
269474e7dc98SMatthew Ahrens 
26953b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
269674e7dc98SMatthew Ahrens 		snap->ds = ds;
269774e7dc98SMatthew Ahrens 		list_insert_tail(l, snap);
2698c1379625SJustin T. Gibbs 		obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
269974e7dc98SMatthew Ahrens 	}
270074e7dc98SMatthew Ahrens 
270174e7dc98SMatthew Ahrens 	return (0);
270274e7dc98SMatthew Ahrens }
270374e7dc98SMatthew Ahrens 
270474e7dc98SMatthew Ahrens static int
270574e7dc98SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
270674e7dc98SMatthew Ahrens {
270774e7dc98SMatthew Ahrens 	struct promotenode *snap;
270874e7dc98SMatthew Ahrens 
270974e7dc98SMatthew Ahrens 	*spacep = 0;
271074e7dc98SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2711cde58dbcSMatthew Ahrens 		uint64_t used, comp, uncomp;
2712cde58dbcSMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2713cde58dbcSMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
271474e7dc98SMatthew Ahrens 		*spacep += used;
271574e7dc98SMatthew Ahrens 	}
271674e7dc98SMatthew Ahrens 	return (0);
271774e7dc98SMatthew Ahrens }
271874e7dc98SMatthew Ahrens 
271974e7dc98SMatthew Ahrens static void
27203b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
272174e7dc98SMatthew Ahrens {
272274e7dc98SMatthew Ahrens 	struct promotenode *snap;
272374e7dc98SMatthew Ahrens 
27243b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
272574e7dc98SMatthew Ahrens 		return;
272674e7dc98SMatthew Ahrens 
272774e7dc98SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
272874e7dc98SMatthew Ahrens 		list_remove(l, snap);
27293b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
27303b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
273174e7dc98SMatthew Ahrens 	}
273274e7dc98SMatthew Ahrens 	list_destroy(l);
273374e7dc98SMatthew Ahrens }
273474e7dc98SMatthew Ahrens 
27353b2aab18SMatthew Ahrens static int
27363b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
27373b2aab18SMatthew Ahrens {
27383b2aab18SMatthew Ahrens 	int error;
27393b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
27403b2aab18SMatthew Ahrens 	struct promotenode *snap;
27413b2aab18SMatthew Ahrens 
27423b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
27433b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
27443b2aab18SMatthew Ahrens 	if (error != 0)
27453b2aab18SMatthew Ahrens 		return (error);
27463b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
27473b2aab18SMatthew Ahrens 
2748bc9014e6SJustin Gibbs 	if (ddpa->ddpa_clone->ds_is_snapshot ||
27493b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
27503b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2751be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
27523b2aab18SMatthew Ahrens 	}
27533b2aab18SMatthew Ahrens 
2754c1379625SJustin T. Gibbs 	error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
27553b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
27563b2aab18SMatthew Ahrens 	if (error != 0)
27573b2aab18SMatthew Ahrens 		goto out;
27583b2aab18SMatthew Ahrens 
27593b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
27603b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
27613b2aab18SMatthew Ahrens 	if (error != 0)
27623b2aab18SMatthew Ahrens 		goto out;
27633b2aab18SMatthew Ahrens 
27643b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
2765c1379625SJustin T. Gibbs 	ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
2766c1379625SJustin T. Gibbs 	error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
2767c1379625SJustin T. Gibbs 	    dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
27683b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
27693b2aab18SMatthew Ahrens 	if (error != 0)
27703b2aab18SMatthew Ahrens 		goto out;
27713b2aab18SMatthew Ahrens 
2772c1379625SJustin T. Gibbs 	if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
27733b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
2774c1379625SJustin T. Gibbs 		    dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
27753b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
27763b2aab18SMatthew Ahrens 		if (error != 0)
27773b2aab18SMatthew Ahrens 			goto out;
27783b2aab18SMatthew Ahrens 	}
27793b2aab18SMatthew Ahrens out:
27803b2aab18SMatthew Ahrens 	if (error != 0)
27813b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
27823b2aab18SMatthew Ahrens 	return (error);
27833b2aab18SMatthew Ahrens }
27843b2aab18SMatthew Ahrens 
27853b2aab18SMatthew Ahrens static void
27863b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
27873b2aab18SMatthew Ahrens {
27883b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
27893b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
27903b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
27913b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
27923b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
27933b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
27943b2aab18SMatthew Ahrens }
27953b2aab18SMatthew Ahrens 
279674e7dc98SMatthew Ahrens /*
27973b2aab18SMatthew Ahrens  * Promote a clone.
27983b2aab18SMatthew Ahrens  *
27993b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
280040a5c998SMatthew Ahrens  * in with the name.  (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
280174e7dc98SMatthew Ahrens  */
280299653d4eSeschrock int
2803681d9761SEric Taylor dsl_dataset_promote(const char *name, char *conflsnap)
280499653d4eSeschrock {
28053b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
28063b2aab18SMatthew Ahrens 	uint64_t numsnaps;
28073b2aab18SMatthew Ahrens 	int error;
28083b2aab18SMatthew Ahrens 	objset_t *os;
280974e7dc98SMatthew Ahrens 
281099653d4eSeschrock 	/*
28113b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
28123b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
2813745cd3c5Smaybee 	 */
28143b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
28153b2aab18SMatthew Ahrens 	if (error != 0)
28163b2aab18SMatthew Ahrens 		return (error);
28173b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2818c1379625SJustin T. Gibbs 	    dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2819c1379625SJustin T. Gibbs 	    &numsnaps);
28203b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
28213b2aab18SMatthew Ahrens 	if (error != 0)
28223b2aab18SMatthew Ahrens 		return (error);
2823088f3894Sahrens 
28243b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
28253b2aab18SMatthew Ahrens 	ddpa.err_ds = conflsnap;
2826a2afb611SJerry Jelinek 	ddpa.cr = CRED();
2827088f3894Sahrens 
28283b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_dataset_promote_check,
28297d46dc6cSMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa,
28307d46dc6cSMatthew Ahrens 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2831745cd3c5Smaybee }
2832088f3894Sahrens 
28333b2aab18SMatthew Ahrens int
28343b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
283591948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2836f18faf3fSek110237 {
28375f7a8e6dSDan McDonald 	/*
28385f7a8e6dSDan McDonald 	 * "slack" factor for received datasets with refquota set on them.
28395f7a8e6dSDan McDonald 	 * See the bottom of this function for details on its use.
28405f7a8e6dSDan McDonald 	 */
28415f7a8e6dSDan McDonald 	uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation;
28423b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2843f18faf3fSek110237 
28443cb34c60Sahrens 	/* they should both be heads */
2845bc9014e6SJustin Gibbs 	if (clone->ds_is_snapshot ||
2846bc9014e6SJustin Gibbs 	    origin_head->ds_is_snapshot)
2847be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2848f18faf3fSek110237 
284934f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
285034f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
2851be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2852f18faf3fSek110237 
28533b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
28543b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
28553b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
285634f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2857be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2858f18faf3fSek110237 
28593cb34c60Sahrens 	/* the clone should be a child of the origin */
28603b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2861be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2862f18faf3fSek110237 
28633b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
286434f2f8cfSMatthew Ahrens 	if (!force &&
286534f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2866be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
2867a9b821a0Sck153898 
28683b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
286991948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2870be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
2871a9b821a0Sck153898 
28723b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
28733b2aab18SMatthew Ahrens 	unused_refres_delta =
28743b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2875c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
28763b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2877c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
28783b2aab18SMatthew Ahrens 
28793b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
28803b2aab18SMatthew Ahrens 	    unused_refres_delta >
28813b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2882be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
2883a9b821a0Sck153898 
28845f7a8e6dSDan McDonald 	/*
28855f7a8e6dSDan McDonald 	 * The clone can't be too much over the head's refquota.
28865f7a8e6dSDan McDonald 	 *
28875f7a8e6dSDan McDonald 	 * To ensure that the entire refquota can be used, we allow one
28885f7a8e6dSDan McDonald 	 * transaction to exceed the the refquota.  Therefore, this check
28895f7a8e6dSDan McDonald 	 * needs to also allow for the space referenced to be more than the
28905f7a8e6dSDan McDonald 	 * refquota.  The maximum amount of space that one transaction can use
28915f7a8e6dSDan McDonald 	 * on disk is DMU_MAX_ACCESS * spa_asize_inflation.  Allowing this
28925f7a8e6dSDan McDonald 	 * overage ensures that we are able to receive a filesystem that
28935f7a8e6dSDan McDonald 	 * exceeds the refquota on the source system.
28945f7a8e6dSDan McDonald 	 *
28955f7a8e6dSDan McDonald 	 * So that overage is the refquota_slack we use below.
28965f7a8e6dSDan McDonald 	 */
28973b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
2898c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes >
28995f7a8e6dSDan McDonald 	    origin_head->ds_quota + refquota_slack)
2900be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2901c4cbca4fSChris Kirby 
29023cb34c60Sahrens 	return (0);
2903f18faf3fSek110237 }
2904f18faf3fSek110237 
29053b2aab18SMatthew Ahrens void
29063b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
29073b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2908f18faf3fSek110237 {
29093b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29103b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2911f18faf3fSek110237 
29123b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
29135f7a8e6dSDan McDonald 	/*
29145f7a8e6dSDan McDonald 	 * NOTE: On DEBUG kernels there could be a race between this and
29155f7a8e6dSDan McDonald 	 * the check function if spa_asize_inflation is adjusted...
29165f7a8e6dSDan McDonald 	 */
29173b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
29185f7a8e6dSDan McDonald 	    dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
29195f7a8e6dSDan McDonald 	    DMU_MAX_ACCESS * spa_asize_inflation);
292034f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2921a9b821a0Sck153898 
2922ca0cc391SMatthew Ahrens 	/*
2923ca0cc391SMatthew Ahrens 	 * Swap per-dataset feature flags.
2924ca0cc391SMatthew Ahrens 	 */
2925ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2926ca0cc391SMatthew Ahrens 		if (!(spa_feature_table[f].fi_flags &
2927ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET)) {
2928ca0cc391SMatthew Ahrens 			ASSERT(!clone->ds_feature_inuse[f]);
2929ca0cc391SMatthew Ahrens 			ASSERT(!origin_head->ds_feature_inuse[f]);
2930ca0cc391SMatthew Ahrens 			continue;
2931ca0cc391SMatthew Ahrens 		}
2932ca0cc391SMatthew Ahrens 
2933ca0cc391SMatthew Ahrens 		boolean_t clone_inuse = clone->ds_feature_inuse[f];
2934ca0cc391SMatthew Ahrens 		boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
2935ca0cc391SMatthew Ahrens 
2936ca0cc391SMatthew Ahrens 		if (clone_inuse) {
2937ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
2938ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_FALSE;
2939ca0cc391SMatthew Ahrens 		}
2940ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
2941ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(origin_head->ds_object,
2942ca0cc391SMatthew Ahrens 			    f, tx);
2943ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_FALSE;
2944ca0cc391SMatthew Ahrens 		}
2945ca0cc391SMatthew Ahrens 		if (clone_inuse) {
2946ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(origin_head->ds_object,
2947ca0cc391SMatthew Ahrens 			    f, tx);
2948ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_TRUE;
2949ca0cc391SMatthew Ahrens 		}
2950ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
2951ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(clone->ds_object, f, tx);
2952ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_TRUE;
2953ca0cc391SMatthew Ahrens 		}
2954ca0cc391SMatthew Ahrens 	}
2955ca0cc391SMatthew Ahrens 
29563b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
29573b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2958f18faf3fSek110237 
29593b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
29603b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
29613b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
29623cb34c60Sahrens 	}
2963f18faf3fSek110237 
29643b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
29653b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
29663b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
29673cb34c60Sahrens 	}
2968f18faf3fSek110237 
29693b2aab18SMatthew Ahrens 	unused_refres_delta =
29703b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2971c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
29723b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2973c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
29743b2aab18SMatthew Ahrens 
2975ae46e4c7SMatthew Ahrens 	/*
2976ae46e4c7SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
2977ae46e4c7SMatthew Ahrens 	 */
29783b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
29793b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
2980cde58dbcSMatthew Ahrens 		uint64_t comp, uncomp;
2981cde58dbcSMatthew Ahrens 
2982ae46e4c7SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
29833b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
2984c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
2985c1379625SJustin T. Gibbs 		    &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
2986ae46e4c7SMatthew Ahrens 	}
2987f18faf3fSek110237 
2988f18faf3fSek110237 	/* swap blkptrs */
2989f18faf3fSek110237 	{
2990f18faf3fSek110237 		blkptr_t tmp;
2991c1379625SJustin T. Gibbs 		tmp = dsl_dataset_phys(origin_head)->ds_bp;
2992c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_bp =
2993c1379625SJustin T. Gibbs 		    dsl_dataset_phys(clone)->ds_bp;
2994c1379625SJustin T. Gibbs 		dsl_dataset_phys(clone)->ds_bp = tmp;
2995f18faf3fSek110237 	}
2996f18faf3fSek110237 
2997f18faf3fSek110237 	/* set dd_*_bytes */
2998f18faf3fSek110237 	{
2999f18faf3fSek110237 		int64_t dused, dcomp, duncomp;
3000f18faf3fSek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
3001f18faf3fSek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
3002f18faf3fSek110237 
3003c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dir_phys(clone->ds_dir)->
300474e7dc98SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
300574e7dc98SMatthew Ahrens 
30063b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
3007cde58dbcSMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
30083b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
3009cde58dbcSMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
301074e7dc98SMatthew Ahrens 
3011c1379625SJustin T. Gibbs 		dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
3012c1379625SJustin T. Gibbs 		    cdl_used -
3013c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
3014c1379625SJustin T. Gibbs 		    odl_used);
3015c1379625SJustin T. Gibbs 		dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
3016c1379625SJustin T. Gibbs 		    cdl_comp -
3017c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
3018c1379625SJustin T. Gibbs 		    odl_comp);
3019c1379625SJustin T. Gibbs 		duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
30203cb34c60Sahrens 		    cdl_uncomp -
3021c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
3022c1379625SJustin T. Gibbs 		    odl_uncomp);
3023f18faf3fSek110237 
30243b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
30253cb34c60Sahrens 		    dused, dcomp, duncomp, tx);
30263b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
30273cb34c60Sahrens 		    -dused, -dcomp, -duncomp, tx);
302874e7dc98SMatthew Ahrens 
302974e7dc98SMatthew Ahrens 		/*
303074e7dc98SMatthew Ahrens 		 * The difference in the space used by snapshots is the
303174e7dc98SMatthew Ahrens 		 * difference in snapshot space due to the head's
303274e7dc98SMatthew Ahrens 		 * deadlist (since that's the only thing that's
303374e7dc98SMatthew Ahrens 		 * changing that affects the snapused).
303474e7dc98SMatthew Ahrens 		 */
30353b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
30363b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3037cde58dbcSMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
30383b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
30393b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3040cde58dbcSMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
30413b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
304274e7dc98SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
30433cb34c60Sahrens 	}
30443cb34c60Sahrens 
3045f18faf3fSek110237 	/* swap ds_*_bytes */
3046c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
3047c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes);
3048c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
3049c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_compressed_bytes);
3050c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
3051c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_uncompressed_bytes);
3052c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
3053c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
3054a9b821a0Sck153898 
3055a9b821a0Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
30563b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
30573b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
3058f18faf3fSek110237 
3059cde58dbcSMatthew Ahrens 	/*
3060cde58dbcSMatthew Ahrens 	 * Swap deadlists.
3061cde58dbcSMatthew Ahrens 	 */
30623b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
30633b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
3064c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
3065c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
30663b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
3067c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
30683b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
3069c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_deadlist_obj);
307088b7b0f2SMatthew Ahrens 
30713b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
30724445fffbSMatthew Ahrens 
30733b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
30743b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
3075f18faf3fSek110237 }
3076f18faf3fSek110237 
3077b1b8ab34Slling /*
3078b1b8ab34Slling  * Given a pool name and a dataset object number in that pool,
3079b1b8ab34Slling  * return the name of that dataset.
3080b1b8ab34Slling  */
3081b1b8ab34Slling int
3082b1b8ab34Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3083b1b8ab34Slling {
3084b1b8ab34Slling 	dsl_pool_t *dp;
3085745cd3c5Smaybee 	dsl_dataset_t *ds;
3086b1b8ab34Slling 	int error;
3087b1b8ab34Slling 
30883b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
30893b2aab18SMatthew Ahrens 	if (error != 0)
3090b1b8ab34Slling 		return (error);
30913b2aab18SMatthew Ahrens 
30923b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
30933b2aab18SMatthew Ahrens 	if (error == 0) {
3094b1b8ab34Slling 		dsl_dataset_name(ds, buf);
3095745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
3096745cd3c5Smaybee 	}
30973b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3098b1b8ab34Slling 
3099745cd3c5Smaybee 	return (error);
3100b1b8ab34Slling }
3101a9799022Sck153898 
3102a9799022Sck153898 int
3103a9799022Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3104745cd3c5Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3105a9799022Sck153898 {
3106a9799022Sck153898 	int error = 0;
3107a9799022Sck153898 
3108a9799022Sck153898 	ASSERT3S(asize, >, 0);
3109a9799022Sck153898 
31109082849eSck153898 	/*
31119082849eSck153898 	 * *ref_rsrv is the portion of asize that will come from any
31129082849eSck153898 	 * unconsumed refreservation space.
31139082849eSck153898 	 */
31149082849eSck153898 	*ref_rsrv = 0;
31159082849eSck153898 
3116a9799022Sck153898 	mutex_enter(&ds->ds_lock);
3117a9799022Sck153898 	/*
3118a9799022Sck153898 	 * Make a space adjustment for reserved bytes.
3119a9799022Sck153898 	 */
3120c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
3121a9799022Sck153898 		ASSERT3U(*used, >=,
3122c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3123c1379625SJustin T. Gibbs 		*used -=
3124c1379625SJustin T. Gibbs 		    (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
31259082849eSck153898 		*ref_rsrv =
31269082849eSck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
3127a9799022Sck153898 	}
3128a9799022Sck153898 
3129a9799022Sck153898 	if (!check_quota || ds->ds_quota == 0) {
3130a9799022Sck153898 		mutex_exit(&ds->ds_lock);
3131a9799022Sck153898 		return (0);
3132a9799022Sck153898 	}
3133a9799022Sck153898 	/*
3134a9799022Sck153898 	 * If they are requesting more space, and our current estimate
3135a9799022Sck153898 	 * is over quota, they get to try again unless the actual
3136a9799022Sck153898 	 * on-disk is over quota and there are no pending changes (which
3137a9799022Sck153898 	 * may free up space for us).
3138a9799022Sck153898 	 */
3139c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
3140c1379625SJustin T. Gibbs 	    ds->ds_quota) {
3141ad135b5dSChristopher Siden 		if (inflight > 0 ||
3142c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
3143be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
3144a9799022Sck153898 		else
3145be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
3146a9799022Sck153898 	}
3147a9799022Sck153898 	mutex_exit(&ds->ds_lock);
3148a9799022Sck153898 
3149a9799022Sck153898 	return (error);
3150a9799022Sck153898 }
3151a9799022Sck153898 
31523b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
31533b2aab18SMatthew Ahrens 	const char *ddsqra_name;
31543b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
31553b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
31563b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
31573b2aab18SMatthew Ahrens 
31583b2aab18SMatthew Ahrens 
3159a9799022Sck153898 /* ARGSUSED */
3160a9799022Sck153898 static int
31613b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
3162a9799022Sck153898 {
31633b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
31643b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
31653b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
31663b2aab18SMatthew Ahrens 	int error;
31673b2aab18SMatthew Ahrens 	uint64_t newval;
3168a9799022Sck153898 
31693b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
3170be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3171a9799022Sck153898 
31723b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
31733b2aab18SMatthew Ahrens 	if (error != 0)
31743b2aab18SMatthew Ahrens 		return (error);
317592241e0bSTom Erickson 
3176bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
31773b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3178be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31793b2aab18SMatthew Ahrens 	}
31803b2aab18SMatthew Ahrens 
31813b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
31823b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
31833b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
31843b2aab18SMatthew Ahrens 	if (error != 0) {
31853b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
31863b2aab18SMatthew Ahrens 		return (error);
31873b2aab18SMatthew Ahrens 	}
31883b2aab18SMatthew Ahrens 
31893b2aab18SMatthew Ahrens 	if (newval == 0) {
31903b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3191a9799022Sck153898 		return (0);
31923b2aab18SMatthew Ahrens 	}
3193a9799022Sck153898 
3194c1379625SJustin T. Gibbs 	if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
31953b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
31963b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3197be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
31983b2aab18SMatthew Ahrens 	}
3199a9799022Sck153898 
32003b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3201a9799022Sck153898 	return (0);
3202a9799022Sck153898 }
3203a9799022Sck153898 
32043b2aab18SMatthew Ahrens static void
32053b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3206a9799022Sck153898 {
32073b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
32083b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
32093b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
32103b2aab18SMatthew Ahrens 	uint64_t newval;
3211a9799022Sck153898 
32123b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
321392241e0bSTom Erickson 
32143b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
32153b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
32163b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
32173b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
32183b2aab18SMatthew Ahrens 
32193b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
32203b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
32213b2aab18SMatthew Ahrens 
32223b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
3223a9799022Sck153898 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
32243b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
322592241e0bSTom Erickson 	}
32263b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3227a9799022Sck153898 }
3228a9799022Sck153898 
3229a9799022Sck153898 int
32303b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
32313b2aab18SMatthew Ahrens     uint64_t refquota)
3232a9799022Sck153898 {
32333b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3234a9799022Sck153898 
32353b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
32363b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
32373b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
323892241e0bSTom Erickson 
32393b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
32407d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
3241a9799022Sck153898 }
3242a9799022Sck153898 
3243a9799022Sck153898 static int
32443b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3245a9799022Sck153898 {
32463b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
32473b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
32483b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
32493b2aab18SMatthew Ahrens 	int error;
32503b2aab18SMatthew Ahrens 	uint64_t newval, unique;
3251a9799022Sck153898 
32523b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3253be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3254a9799022Sck153898 
32553b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
32563b2aab18SMatthew Ahrens 	if (error != 0)
32573b2aab18SMatthew Ahrens 		return (error);
32583b2aab18SMatthew Ahrens 
3259bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
32603b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3261be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32623b2aab18SMatthew Ahrens 	}
3263a9799022Sck153898 
32643b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
32653b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
32663b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
32673b2aab18SMatthew Ahrens 	if (error != 0) {
32683b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
32693b2aab18SMatthew Ahrens 		return (error);
32703b2aab18SMatthew Ahrens 	}
327192241e0bSTom Erickson 
3272a9799022Sck153898 	/*
3273a9799022Sck153898 	 * If we are doing the preliminary check in open context, the
3274a9799022Sck153898 	 * space estimates may be inaccurate.
3275a9799022Sck153898 	 */
32763b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
32773b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3278a9799022Sck153898 		return (0);
32793b2aab18SMatthew Ahrens 	}
3280a9799022Sck153898 
3281a9799022Sck153898 	mutex_enter(&ds->ds_lock);
32823f9d6ad7SLin Ling 	if (!DS_UNIQUE_IS_ACCURATE(ds))
32833f9d6ad7SLin Ling 		dsl_dataset_recalc_head_uniq(ds);
3284c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3285a9799022Sck153898 	mutex_exit(&ds->ds_lock);
3286a9799022Sck153898 
32873b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
32883b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
3289379c004dSEric Schrock 		    MAX(unique, ds->ds_reserved);
3290379c004dSEric Schrock 
32913b2aab18SMatthew Ahrens 		if (delta >
32923b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
32933b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
32943b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
3295be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
3296379c004dSEric Schrock 		}
32973b2aab18SMatthew Ahrens 	}
3298a9799022Sck153898 
32993b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3300a9799022Sck153898 	return (0);
3301a9799022Sck153898 }
3302a9799022Sck153898 
33033b2aab18SMatthew Ahrens void
33043b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
33053b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3306a9799022Sck153898 {
33073b2aab18SMatthew Ahrens 	uint64_t newval;
330802c8f3f0SMatthew Ahrens 	uint64_t unique;
330902c8f3f0SMatthew Ahrens 	int64_t delta;
3310a9799022Sck153898 
33113b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
33123b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
33133b2aab18SMatthew Ahrens 
33143b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
33153b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
331692241e0bSTom Erickson 
331702c8f3f0SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
331802c8f3f0SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
331902c8f3f0SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
33203f9d6ad7SLin Ling 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3321c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
33223b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
332302c8f3f0SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
33243b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
332502c8f3f0SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
332602c8f3f0SMatthew Ahrens 
332702c8f3f0SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
332802c8f3f0SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
3329a9799022Sck153898 }
3330a9799022Sck153898 
3331c99e4bdcSChris Kirby static void
33323b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3333c99e4bdcSChris Kirby {
33343b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
33353b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3336842727c2SChris Kirby 	dsl_dataset_t *ds;
3337842727c2SChris Kirby 
33383b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
33393b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
33403b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3341ca45db41SChris Kirby 	dsl_dataset_rele(ds, FTAG);
3342ca45db41SChris Kirby }
3343ca45db41SChris Kirby 
3344842727c2SChris Kirby int
33453b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
33463b2aab18SMatthew Ahrens     uint64_t refreservation)
3347842727c2SChris Kirby {
33483b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3349842727c2SChris Kirby 
33503b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
33513b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
33523b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
3353842727c2SChris Kirby 
33543b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
33557d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refreservation_sync, &ddsqra,
33567d46dc6cSMatthew Ahrens 	    0, ZFS_SPACE_CHECK_NONE));
3357503ad85cSMatthew Ahrens }
335819b94df9SMatthew Ahrens 
335919b94df9SMatthew Ahrens /*
336019b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
336119b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
336219b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
336319b94df9SMatthew Ahrens  * fail and return EINVAL.
336419b94df9SMatthew Ahrens  *
336519b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
336619b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
336719b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
336819b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
336919b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
337019b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
337119b94df9SMatthew Ahrens  *
337219b94df9SMatthew Ahrens  * space freed                         [---------------------]
337319b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
337419b94df9SMatthew Ahrens  *                                         oldsnap            new
337519b94df9SMatthew Ahrens  */
337619b94df9SMatthew Ahrens int
337719b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
337819b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
337919b94df9SMatthew Ahrens {
338019b94df9SMatthew Ahrens 	int err = 0;
338119b94df9SMatthew Ahrens 	uint64_t snapobj;
338219b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
338319b94df9SMatthew Ahrens 
33843b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
33853b2aab18SMatthew Ahrens 
338619b94df9SMatthew Ahrens 	*usedp = 0;
3387c1379625SJustin T. Gibbs 	*usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3388c1379625SJustin T. Gibbs 	*usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
338919b94df9SMatthew Ahrens 
339019b94df9SMatthew Ahrens 	*compp = 0;
3391c1379625SJustin T. Gibbs 	*compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3392c1379625SJustin T. Gibbs 	*compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
339319b94df9SMatthew Ahrens 
339419b94df9SMatthew Ahrens 	*uncompp = 0;
3395c1379625SJustin T. Gibbs 	*uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3396c1379625SJustin T. Gibbs 	*uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
339719b94df9SMatthew Ahrens 
339819b94df9SMatthew Ahrens 	snapobj = new->ds_object;
339919b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
340019b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
340119b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
340219b94df9SMatthew Ahrens 
3403ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
3404ad135b5dSChristopher Siden 			snap = new;
3405ad135b5dSChristopher Siden 		} else {
340619b94df9SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
340719b94df9SMatthew Ahrens 			if (err != 0)
340819b94df9SMatthew Ahrens 				break;
3409ad135b5dSChristopher Siden 		}
341019b94df9SMatthew Ahrens 
3411c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3412c1379625SJustin T. Gibbs 		    dsl_dataset_phys(oldsnap)->ds_creation_txg) {
341319b94df9SMatthew Ahrens 			/*
341419b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
341519b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
341619b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
341719b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
341819b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
341919b94df9SMatthew Ahrens 			 * optimization itself.
342019b94df9SMatthew Ahrens 			 */
342119b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
342219b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
342319b94df9SMatthew Ahrens 		} else {
342419b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
3425c1379625SJustin T. Gibbs 			    0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
342619b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
342719b94df9SMatthew Ahrens 		}
342819b94df9SMatthew Ahrens 		*usedp += used;
342919b94df9SMatthew Ahrens 		*compp += comp;
343019b94df9SMatthew Ahrens 		*uncompp += uncomp;
343119b94df9SMatthew Ahrens 
343219b94df9SMatthew Ahrens 		/*
343319b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
343419b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
343519b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
343619b94df9SMatthew Ahrens 		 */
3437c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3438ad135b5dSChristopher Siden 		if (snap != new)
343919b94df9SMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
344019b94df9SMatthew Ahrens 		if (snapobj == 0) {
3441be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
344219b94df9SMatthew Ahrens 			break;
344319b94df9SMatthew Ahrens 		}
344419b94df9SMatthew Ahrens 
344519b94df9SMatthew Ahrens 	}
344619b94df9SMatthew Ahrens 	return (err);
344719b94df9SMatthew Ahrens }
344819b94df9SMatthew Ahrens 
344919b94df9SMatthew Ahrens /*
345019b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
345119b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
345219b94df9SMatthew Ahrens  *
345319b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
345419b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
345519b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
345619b94df9SMatthew Ahrens  *
345719b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
345819b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
345919b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
346019b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
346119b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
346219b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
346319b94df9SMatthew Ahrens  */
346419b94df9SMatthew Ahrens int
346519b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
346619b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
346719b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
346819b94df9SMatthew Ahrens {
346919b94df9SMatthew Ahrens 	int err = 0;
347019b94df9SMatthew Ahrens 	uint64_t snapobj;
347119b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
347219b94df9SMatthew Ahrens 
3473bc9014e6SJustin Gibbs 	ASSERT(firstsnap->ds_is_snapshot);
3474bc9014e6SJustin Gibbs 	ASSERT(lastsnap->ds_is_snapshot);
347519b94df9SMatthew Ahrens 
347619b94df9SMatthew Ahrens 	/*
347719b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
347819b94df9SMatthew Ahrens 	 * is before lastsnap.
347919b94df9SMatthew Ahrens 	 */
348019b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
3481c1379625SJustin T. Gibbs 	    dsl_dataset_phys(firstsnap)->ds_creation_txg >
3482c1379625SJustin T. Gibbs 	    dsl_dataset_phys(lastsnap)->ds_creation_txg)
3483be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
348419b94df9SMatthew Ahrens 
348519b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
348619b94df9SMatthew Ahrens 
3487c1379625SJustin T. Gibbs 	snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
348819b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
348919b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
349019b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
349119b94df9SMatthew Ahrens 
349219b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
349319b94df9SMatthew Ahrens 		if (err != 0)
349419b94df9SMatthew Ahrens 			break;
349519b94df9SMatthew Ahrens 
349619b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
3497c1379625SJustin T. Gibbs 		    dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
349819b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
349919b94df9SMatthew Ahrens 		*usedp += used;
350019b94df9SMatthew Ahrens 		*compp += comp;
350119b94df9SMatthew Ahrens 		*uncompp += uncomp;
350219b94df9SMatthew Ahrens 
3503c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
350419b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
350519b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
350619b94df9SMatthew Ahrens 	}
350719b94df9SMatthew Ahrens 	return (err);
350819b94df9SMatthew Ahrens }
35093b2aab18SMatthew Ahrens 
35103b2aab18SMatthew Ahrens /*
35113b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
35123b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
35133b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
35143b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
35153b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
351678f17100SMatthew Ahrens  *
351778f17100SMatthew Ahrens  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
35183b2aab18SMatthew Ahrens  */
35193b2aab18SMatthew Ahrens boolean_t
352078f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
352178f17100SMatthew Ahrens     uint64_t earlier_txg)
35223b2aab18SMatthew Ahrens {
35233b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
35243b2aab18SMatthew Ahrens 	int error;
35253b2aab18SMatthew Ahrens 	boolean_t ret;
35263b2aab18SMatthew Ahrens 
35273b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
3528bc9014e6SJustin Gibbs 	ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
35293b2aab18SMatthew Ahrens 
353078f17100SMatthew Ahrens 	if (earlier_txg == 0)
3531c1379625SJustin T. Gibbs 		earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
353278f17100SMatthew Ahrens 
3533bc9014e6SJustin Gibbs 	if (later->ds_is_snapshot &&
3534c1379625SJustin T. Gibbs 	    earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
35353b2aab18SMatthew Ahrens 		return (B_FALSE);
35363b2aab18SMatthew Ahrens 
35373b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
35383b2aab18SMatthew Ahrens 		return (B_TRUE);
35393b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
35403b2aab18SMatthew Ahrens 		return (B_FALSE);
35413b2aab18SMatthew Ahrens 
3542c1379625SJustin T. Gibbs 	if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
35433b2aab18SMatthew Ahrens 		return (B_TRUE);
35443b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
35453b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
3546c1379625SJustin T. Gibbs 	    dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
35473b2aab18SMatthew Ahrens 	if (error != 0)
35483b2aab18SMatthew Ahrens 		return (B_FALSE);
354978f17100SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
35503b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
35513b2aab18SMatthew Ahrens 	return (ret);
35523b2aab18SMatthew Ahrens }
35532acef22dSMatthew Ahrens 
35542acef22dSMatthew Ahrens void
35552acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
35562acef22dSMatthew Ahrens {
35572acef22dSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
35582acef22dSMatthew Ahrens 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
35592acef22dSMatthew Ahrens }
35609c3fd121SMatthew Ahrens 
35619c3fd121SMatthew Ahrens boolean_t
35629c3fd121SMatthew Ahrens dsl_dataset_is_zapified(dsl_dataset_t *ds)
35639c3fd121SMatthew Ahrens {
35649c3fd121SMatthew Ahrens 	dmu_object_info_t doi;
35659c3fd121SMatthew Ahrens 
35669c3fd121SMatthew Ahrens 	dmu_object_info_from_db(ds->ds_dbuf, &doi);
35679c3fd121SMatthew Ahrens 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
35689c3fd121SMatthew Ahrens }
35699c3fd121SMatthew Ahrens 
35709c3fd121SMatthew Ahrens boolean_t
35719c3fd121SMatthew Ahrens dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
35729c3fd121SMatthew Ahrens {
35739c3fd121SMatthew Ahrens 	return (dsl_dataset_is_zapified(ds) &&
35749c3fd121SMatthew Ahrens 	    zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
35759c3fd121SMatthew Ahrens 	    ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
35769c3fd121SMatthew Ahrens }
3577