xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision c3d26abc9ee97b4f60233556aadeb57e0bd30bb9)
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.
23ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 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.
27*c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/dmu_objset.h>
31fa9e4066Sahrens #include <sys/dsl_dataset.h>
32fa9e4066Sahrens #include <sys/dsl_dir.h>
3399653d4eSeschrock #include <sys/dsl_prop.h>
341d452cf5Sahrens #include <sys/dsl_synctask.h>
35fa9e4066Sahrens #include <sys/dmu_traverse.h>
364e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
37fa9e4066Sahrens #include <sys/dmu_tx.h>
38fa9e4066Sahrens #include <sys/arc.h>
39fa9e4066Sahrens #include <sys/zio.h>
40fa9e4066Sahrens #include <sys/zap.h>
41ad135b5dSChristopher Siden #include <sys/zfeature.h>
42fa9e4066Sahrens #include <sys/unique.h>
43fa9e4066Sahrens #include <sys/zfs_context.h>
44cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
45ecd6cf80Smarks #include <sys/spa.h>
46088f3894Sahrens #include <sys/zfs_znode.h>
47c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
48842727c2SChris Kirby #include <sys/zvol.h>
493f9d6ad7SLin Ling #include <sys/dsl_scan.h>
50cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
513b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
523b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
5378f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
5445818ee1SMatthew Ahrens #include <sys/dmu_send.h>
5545818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
569c3fd121SMatthew Ahrens #include <sys/zio_compress.h>
579c3fd121SMatthew Ahrens #include <zfs_fletcher.h>
58e1930233Sbonwick 
59b5152584SMatthew Ahrens /*
60b5152584SMatthew Ahrens  * The SPA supports block sizes up to 16MB.  However, very large blocks
61b5152584SMatthew Ahrens  * can have an impact on i/o latency (e.g. tying up a spinning disk for
62b5152584SMatthew Ahrens  * ~300ms), and also potentially on the memory allocator.  Therefore,
63b5152584SMatthew Ahrens  * we do not allow the recordsize to be set larger than zfs_max_recordsize
64b5152584SMatthew Ahrens  * (default 1MB).  Larger blocks can be created by changing this tunable,
65b5152584SMatthew Ahrens  * and pools with larger blocks can always be imported and used, regardless
66b5152584SMatthew Ahrens  * of this setting.
67b5152584SMatthew Ahrens  */
68b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024;
69b5152584SMatthew Ahrens 
70cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
71cde58dbcSMatthew Ahrens 	{ \
72cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
73cde58dbcSMatthew Ahrens 		(x) = (y); \
74cde58dbcSMatthew Ahrens 		(y) = __tmp; \
75cde58dbcSMatthew Ahrens 	}
76cde58dbcSMatthew Ahrens 
7755434c77Sek #define	DS_REF_MAX	(1ULL << 62)
78fa9e4066Sahrens 
79c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
80c1379625SJustin T. Gibbs 
81a9799022Sck /*
82a9799022Sck  * Figure out how much of this delta should be propogated to the dsl_dir
83a9799022Sck  * layer.  If there's a refreservation, that space has already been
84a9799022Sck  * partially accounted for in our ancestors.
85a9799022Sck  */
86a9799022Sck static int64_t
87a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta)
88a9799022Sck {
89c1379625SJustin T. Gibbs 	dsl_dataset_phys_t *ds_phys;
90a9799022Sck 	uint64_t old_bytes, new_bytes;
91a9799022Sck 
92a9799022Sck 	if (ds->ds_reserved == 0)
93a9799022Sck 		return (delta);
94a9799022Sck 
95c1379625SJustin T. Gibbs 	ds_phys = dsl_dataset_phys(ds);
96c1379625SJustin T. Gibbs 	old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
97c1379625SJustin T. Gibbs 	new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
98a9799022Sck 
99a9799022Sck 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
100a9799022Sck 	return (new_bytes - old_bytes);
101a9799022Sck }
102fa9e4066Sahrens 
103fa9e4066Sahrens void
104b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
105fa9e4066Sahrens {
106b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
107fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
108fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
109a9799022Sck 	int64_t delta;
110fa9e4066Sahrens 
1113f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
112fa9e4066Sahrens 
113fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
114fa9e4066Sahrens 	/* It could have been compressed away to nothing */
115fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
116fa9e4066Sahrens 		return;
117fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
118ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
119fa9e4066Sahrens 	if (ds == NULL) {
120ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
121ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
122fa9e4066Sahrens 		return;
123fa9e4066Sahrens 	}
1243f9d6ad7SLin Ling 
125b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
126fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
127a9799022Sck 	delta = parent_delta(ds, used);
128c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes += used;
129c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
130c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
131c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes += used;
13245818ee1SMatthew Ahrens 
133ca0cc391SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
134ca0cc391SMatthew Ahrens 		ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
135ca0cc391SMatthew Ahrens 		    B_TRUE;
136ca0cc391SMatthew Ahrens 	}
13745818ee1SMatthew Ahrens 
13845818ee1SMatthew Ahrens 	spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
13945818ee1SMatthew Ahrens 	if (f != SPA_FEATURE_NONE)
14045818ee1SMatthew Ahrens 		ds->ds_feature_activation_needed[f] = B_TRUE;
14145818ee1SMatthew Ahrens 
142fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
14374e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
14474e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
14574e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
14674e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
147fa9e4066Sahrens }
148fa9e4066Sahrens 
149cdb0ab79Smaybee int
150b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
151b24ab676SJeff Bonwick     boolean_t async)
152fa9e4066Sahrens {
15343466aaeSMax Grossman 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
15443466aaeSMax Grossman 	int compressed = BP_GET_PSIZE(bp);
15543466aaeSMax Grossman 	int uncompressed = BP_GET_UCSIZE(bp);
15643466aaeSMax Grossman 
157fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
158cdb0ab79Smaybee 		return (0);
159fa9e4066Sahrens 
160b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
161b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
162b24ab676SJeff Bonwick 
163fa9e4066Sahrens 	if (ds == NULL) {
164b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
165ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
166ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
167cdb0ab79Smaybee 		return (used);
168fa9e4066Sahrens 	}
169fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
170fa9e4066Sahrens 
171bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
172fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
173fa9e4066Sahrens 
174c1379625SJustin T. Gibbs 	if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
175a9799022Sck 		int64_t delta;
176c717a561Smaybee 
1773f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
178b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
179fa9e4066Sahrens 
180fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
181c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
182a9799022Sck 		    !DS_UNIQUE_IS_ACCURATE(ds));
183a9799022Sck 		delta = parent_delta(ds, -used);
184c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_unique_bytes -= used;
185fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
18674e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
187a9799022Sck 		    delta, -compressed, -uncompressed, tx);
18874e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
18974e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
190fa9e4066Sahrens 	} else {
191fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
192b24ab676SJeff Bonwick 		if (async) {
193b24ab676SJeff Bonwick 			/*
194b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
195b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
196cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
197b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
198b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
199b24ab676SJeff Bonwick 			 */
200cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
201b24ab676SJeff Bonwick 		} else {
202cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
203b24ab676SJeff Bonwick 		}
204a4611edeSahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
205c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj);
206c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
207fa9e4066Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
208c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
209a4611edeSahrens 		    ds->ds_object && bp->blk_birth >
210c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
211a4611edeSahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
212a4611edeSahrens 			mutex_enter(&ds->ds_prev->ds_lock);
213c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
214a4611edeSahrens 			mutex_exit(&ds->ds_prev->ds_lock);
215fa9e4066Sahrens 		}
2163f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
21774e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
21874e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
21974e7dc98SMatthew Ahrens 		}
220fa9e4066Sahrens 	}
221fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
222c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
223c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
224c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
225c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
226c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
227c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
228fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
229cdb0ab79Smaybee 
230cdb0ab79Smaybee 	return (used);
231fa9e4066Sahrens }
232fa9e4066Sahrens 
233ea8dc4b6Seschrock uint64_t
234ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
235fa9e4066Sahrens {
236a2eea2e1Sahrens 	uint64_t trysnap = 0;
237a2eea2e1Sahrens 
238fa9e4066Sahrens 	if (ds == NULL)
239ea8dc4b6Seschrock 		return (0);
240fa9e4066Sahrens 	/*
241fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
242fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
243fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
244fa9e4066Sahrens 	 * consume, which is OK.
245fa9e4066Sahrens 	 *
246fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
247fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
248fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
249fa9e4066Sahrens 	 */
250a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
251a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
252a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
253c1379625SJustin T. Gibbs 	return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap));
254ea8dc4b6Seschrock }
255ea8dc4b6Seschrock 
2563d692628SSanjeev Bagewadi boolean_t
257c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
258c7cd2421SGeorge Wilson     uint64_t blk_birth)
259ea8dc4b6Seschrock {
26043466aaeSMax Grossman 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
26143466aaeSMax Grossman 	    (bp != NULL && BP_IS_HOLE(bp)))
262c7cd2421SGeorge Wilson 		return (B_FALSE);
263c7cd2421SGeorge Wilson 
264837b568bSGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
265c7cd2421SGeorge Wilson 
266c7cd2421SGeorge Wilson 	return (B_TRUE);
267fa9e4066Sahrens }
268fa9e4066Sahrens 
269fa9e4066Sahrens static void
270bc9014e6SJustin Gibbs dsl_dataset_evict(void *dbu)
271fa9e4066Sahrens {
272bc9014e6SJustin Gibbs 	dsl_dataset_t *ds = dbu;
273fa9e4066Sahrens 
2743b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
275fa9e4066Sahrens 
276bc9014e6SJustin Gibbs 	ds->ds_dbuf = NULL;
277bc9014e6SJustin Gibbs 
27891ebeef5Sahrens 	unique_remove(ds->ds_fsid_guid);
279fa9e4066Sahrens 
280503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
281503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
282fa9e4066Sahrens 
283fa9e4066Sahrens 	if (ds->ds_prev) {
2843b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
285fa9e4066Sahrens 		ds->ds_prev = NULL;
286fa9e4066Sahrens 	}
287fa9e4066Sahrens 
288cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
289bc9014e6SJustin Gibbs 	if (ds->ds_deadlist.dl_os != NULL)
290cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
291745cd3c5Smaybee 	if (ds->ds_dir)
292bc9014e6SJustin Gibbs 		dsl_dir_async_rele(ds->ds_dir, ds);
293fa9e4066Sahrens 
29491ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
295fa9e4066Sahrens 
29603bad06fSJustin Gibbs 	list_destroy(&ds->ds_prop_cbs);
2975ad82045Snd 	mutex_destroy(&ds->ds_lock);
29891ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
299d2b3cbbdSJorgen Lundman 	mutex_destroy(&ds->ds_sendstream_lock);
3003b2aab18SMatthew Ahrens 	refcount_destroy(&ds->ds_longholds);
3015ad82045Snd 
302fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
303fa9e4066Sahrens }
304fa9e4066Sahrens 
3053b2aab18SMatthew Ahrens int
306fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
307fa9e4066Sahrens {
308fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
309fa9e4066Sahrens 	int err;
310fa9e4066Sahrens 	dmu_buf_t *headdbuf;
311fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
312fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
313fa9e4066Sahrens 
314fa9e4066Sahrens 	if (ds->ds_snapname[0])
315ea8dc4b6Seschrock 		return (0);
316c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
317ea8dc4b6Seschrock 		return (0);
318fa9e4066Sahrens 
319c1379625SJustin T. Gibbs 	err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
320ea8dc4b6Seschrock 	    FTAG, &headdbuf);
3213b2aab18SMatthew Ahrens 	if (err != 0)
322ea8dc4b6Seschrock 		return (err);
323fa9e4066Sahrens 	headphys = headdbuf->db_data;
324fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
325e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
326ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
327ea8dc4b6Seschrock 	return (err);
328fa9e4066Sahrens }
329fa9e4066Sahrens 
3303b2aab18SMatthew Ahrens int
331745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
332ab04eb8eStimh {
333745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
334c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
335ab04eb8eStimh 	matchtype_t mt;
336ab04eb8eStimh 	int err;
337ab04eb8eStimh 
338c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
339ab04eb8eStimh 		mt = MT_FIRST;
340ab04eb8eStimh 	else
341ab04eb8eStimh 		mt = MT_EXACT;
342ab04eb8eStimh 
343745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
344ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
345ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
346745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
347ab04eb8eStimh 	return (err);
348ab04eb8eStimh }
349ab04eb8eStimh 
3503b2aab18SMatthew Ahrens int
351a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
352a2afb611SJerry Jelinek     boolean_t adj_cnt)
353ab04eb8eStimh {
354745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
355c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
356ab04eb8eStimh 	matchtype_t mt;
357ab04eb8eStimh 	int err;
358ab04eb8eStimh 
35971eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
36071eb0538SChris Kirby 
361c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
362ab04eb8eStimh 		mt = MT_FIRST;
363ab04eb8eStimh 	else
364ab04eb8eStimh 		mt = MT_EXACT;
365ab04eb8eStimh 
366745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
367ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
368745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
369a2afb611SJerry Jelinek 
370a2afb611SJerry Jelinek 	if (err == 0 && adj_cnt)
371a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
372a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
373a2afb611SJerry Jelinek 
374ab04eb8eStimh 	return (err);
375ab04eb8eStimh }
376ab04eb8eStimh 
377e57a022bSJustin T. Gibbs boolean_t
378e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
379e57a022bSJustin T. Gibbs {
3809d47dec0SJustin T. Gibbs 	dmu_buf_t *dbuf = ds->ds_dbuf;
3819d47dec0SJustin T. Gibbs 	boolean_t result = B_FALSE;
3829d47dec0SJustin T. Gibbs 
3839d47dec0SJustin T. Gibbs 	if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
3849d47dec0SJustin T. Gibbs 	    ds->ds_object, DMU_BONUS_BLKID, tag)) {
3859d47dec0SJustin T. Gibbs 
3869d47dec0SJustin T. Gibbs 		if (ds == dmu_buf_get_user(dbuf))
3879d47dec0SJustin T. Gibbs 			result = B_TRUE;
3889d47dec0SJustin T. Gibbs 		else
3899d47dec0SJustin T. Gibbs 			dmu_buf_rele(dbuf, tag);
3909d47dec0SJustin T. Gibbs 	}
3919d47dec0SJustin T. Gibbs 
3929d47dec0SJustin T. Gibbs 	return (result);
393e57a022bSJustin T. Gibbs }
394e57a022bSJustin T. Gibbs 
3953b2aab18SMatthew Ahrens int
3963b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
397745cd3c5Smaybee     dsl_dataset_t **dsp)
398fa9e4066Sahrens {
399fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
400fa9e4066Sahrens 	dmu_buf_t *dbuf;
401fa9e4066Sahrens 	dsl_dataset_t *ds;
402ea8dc4b6Seschrock 	int err;
403a7f53a56SChris Kirby 	dmu_object_info_t doi;
404fa9e4066Sahrens 
4053b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
406fa9e4066Sahrens 
407ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
4083b2aab18SMatthew Ahrens 	if (err != 0)
409ea8dc4b6Seschrock 		return (err);
410a7f53a56SChris Kirby 
411a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
412a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
4132acef22dSMatthew Ahrens 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
414b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
415be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
416b287be1bSWill Andrews 	}
417a7f53a56SChris Kirby 
418fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
419fa9e4066Sahrens 	if (ds == NULL) {
420d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
421fa9e4066Sahrens 
422fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
423fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
424fa9e4066Sahrens 		ds->ds_object = dsobj;
425bc9014e6SJustin Gibbs 		ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
426fa9e4066Sahrens 
4275ad82045Snd 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
42891ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
4294e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
4303b2aab18SMatthew Ahrens 		refcount_create(&ds->ds_longholds);
4315ad82045Snd 
432cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
433cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
434c1379625SJustin T. Gibbs 		    mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
435cde58dbcSMatthew Ahrens 
4364e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4374e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
4384e3c9f44SBill Pijewski 
43903bad06fSJustin Gibbs 		list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
44003bad06fSJustin Gibbs 		    offsetof(dsl_prop_cb_record_t, cbr_ds_node));
44103bad06fSJustin Gibbs 
442b5152584SMatthew Ahrens 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
443ca0cc391SMatthew Ahrens 			for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
444ca0cc391SMatthew Ahrens 				if (!(spa_feature_table[f].fi_flags &
445ca0cc391SMatthew Ahrens 				    ZFEATURE_FLAG_PER_DATASET))
446ca0cc391SMatthew Ahrens 					continue;
447ca0cc391SMatthew Ahrens 				err = zap_contains(mos, dsobj,
448ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_guid);
449ca0cc391SMatthew Ahrens 				if (err == 0) {
450ca0cc391SMatthew Ahrens 					ds->ds_feature_inuse[f] = B_TRUE;
451ca0cc391SMatthew Ahrens 				} else {
452ca0cc391SMatthew Ahrens 					ASSERT3U(err, ==, ENOENT);
453ca0cc391SMatthew Ahrens 					err = 0;
454ca0cc391SMatthew Ahrens 				}
455e1f3c208SJustin T. Gibbs 			}
456b5152584SMatthew Ahrens 		}
457b5152584SMatthew Ahrens 
458ca0cc391SMatthew Ahrens 		err = dsl_dir_hold_obj(dp,
459ca0cc391SMatthew Ahrens 		    dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
4603b2aab18SMatthew Ahrens 		if (err != 0) {
4615ad82045Snd 			mutex_destroy(&ds->ds_lock);
46291ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
463d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
4643b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
465cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
466cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
467ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
468ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
469ea8dc4b6Seschrock 			return (err);
470ea8dc4b6Seschrock 		}
471fa9e4066Sahrens 
472bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
473fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
474c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
4753b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
476c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_prev_snap_obj,
477745cd3c5Smaybee 				    ds, &ds->ds_prev);
478fa9e4066Sahrens 			}
47978f17100SMatthew Ahrens 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
48078f17100SMatthew Ahrens 				int zaperr = zap_lookup(mos, ds->ds_object,
48178f17100SMatthew Ahrens 				    DS_FIELD_BOOKMARK_NAMES,
48278f17100SMatthew Ahrens 				    sizeof (ds->ds_bookmarks), 1,
48378f17100SMatthew Ahrens 				    &ds->ds_bookmarks);
48478f17100SMatthew Ahrens 				if (zaperr != ENOENT)
48578f17100SMatthew Ahrens 					VERIFY0(zaperr);
48678f17100SMatthew Ahrens 			}
487842727c2SChris Kirby 		} else {
488842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
489842727c2SChris Kirby 				err = dsl_dataset_get_snapname(ds);
490c1379625SJustin T. Gibbs 			if (err == 0 &&
491c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
492842727c2SChris Kirby 				err = zap_count(
493842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
494c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_userrefs_obj,
495842727c2SChris Kirby 				    &ds->ds_userrefs);
496842727c2SChris Kirby 			}
497fa9e4066Sahrens 		}
498fa9e4066Sahrens 
499bc9014e6SJustin Gibbs 		if (err == 0 && !ds->ds_is_snapshot) {
5003b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
5013b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
5023b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
503cb625fb5Sck 			if (err == 0) {
5043b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
5053b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
5063b2aab18SMatthew Ahrens 				    &ds->ds_quota);
507cb625fb5Sck 			}
508cb625fb5Sck 		} else {
509cb625fb5Sck 			ds->ds_reserved = ds->ds_quota = 0;
510cb625fb5Sck 		}
511cb625fb5Sck 
512bc9014e6SJustin Gibbs 		dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict, &ds->ds_dbuf);
513bc9014e6SJustin Gibbs 		if (err == 0)
514bc9014e6SJustin Gibbs 			winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
515bc9014e6SJustin Gibbs 
516bc9014e6SJustin Gibbs 		if (err != 0 || winner != NULL) {
517cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
518cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
519745cd3c5Smaybee 			if (ds->ds_prev)
5203b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
5213b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
5225ad82045Snd 			mutex_destroy(&ds->ds_lock);
52391ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
524d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
5253b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
526fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
5273b2aab18SMatthew Ahrens 			if (err != 0) {
528ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
529ea8dc4b6Seschrock 				return (err);
530ea8dc4b6Seschrock 			}
531fa9e4066Sahrens 			ds = winner;
532fa9e4066Sahrens 		} else {
53391ebeef5Sahrens 			ds->ds_fsid_guid =
534c1379625SJustin T. Gibbs 			    unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
535fa9e4066Sahrens 		}
536fa9e4066Sahrens 	}
537fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
538c1379625SJustin T. Gibbs 	ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
539c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
540afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
54184db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
542ea8dc4b6Seschrock 	*dsp = ds;
543ea8dc4b6Seschrock 	return (0);
544fa9e4066Sahrens }
545fa9e4066Sahrens 
546745cd3c5Smaybee int
5473b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name,
548503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
549fa9e4066Sahrens {
550fa9e4066Sahrens 	dsl_dir_t *dd;
551745cd3c5Smaybee 	const char *snapname;
552fa9e4066Sahrens 	uint64_t obj;
553fa9e4066Sahrens 	int err = 0;
554a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds;
555fa9e4066Sahrens 
5563b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
5573b2aab18SMatthew Ahrens 	if (err != 0)
558ea8dc4b6Seschrock 		return (err);
559fa9e4066Sahrens 
5603b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
561c1379625SJustin T. Gibbs 	obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
5623b2aab18SMatthew Ahrens 	if (obj != 0)
563a2cdcdd2SPaul Dagnelie 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
564745cd3c5Smaybee 	else
565be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
566fa9e4066Sahrens 
567745cd3c5Smaybee 	/* we may be looking for a snapshot */
568745cd3c5Smaybee 	if (err == 0 && snapname != NULL) {
569a2cdcdd2SPaul Dagnelie 		dsl_dataset_t *snap_ds;
570fa9e4066Sahrens 
571745cd3c5Smaybee 		if (*snapname++ != '@') {
572a2cdcdd2SPaul Dagnelie 			dsl_dataset_rele(ds, tag);
5733b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
574be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
575fa9e4066Sahrens 		}
576fa9e4066Sahrens 
577745cd3c5Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
578a2cdcdd2SPaul Dagnelie 		err = dsl_dataset_snap_lookup(ds, snapname, &obj);
579745cd3c5Smaybee 		if (err == 0)
580a2cdcdd2SPaul Dagnelie 			err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds);
581a2cdcdd2SPaul Dagnelie 		dsl_dataset_rele(ds, tag);
582745cd3c5Smaybee 
5833b2aab18SMatthew Ahrens 		if (err == 0) {
584a2cdcdd2SPaul Dagnelie 			mutex_enter(&snap_ds->ds_lock);
585a2cdcdd2SPaul Dagnelie 			if (snap_ds->ds_snapname[0] == 0)
586a2cdcdd2SPaul Dagnelie 				(void) strlcpy(snap_ds->ds_snapname, snapname,
587a2cdcdd2SPaul Dagnelie 				    sizeof (snap_ds->ds_snapname));
588a2cdcdd2SPaul Dagnelie 			mutex_exit(&snap_ds->ds_lock);
589a2cdcdd2SPaul Dagnelie 			ds = snap_ds;
590fa9e4066Sahrens 		}
591fa9e4066Sahrens 	}
592a2cdcdd2SPaul Dagnelie 	if (err == 0)
593a2cdcdd2SPaul Dagnelie 		*dsp = ds;
5943b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
595fa9e4066Sahrens 	return (err);
596fa9e4066Sahrens }
597fa9e4066Sahrens 
598fa9e4066Sahrens int
5993b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
6003b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
6013b2aab18SMatthew Ahrens {
6023b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
6033b2aab18SMatthew Ahrens 	if (err != 0)
6043b2aab18SMatthew Ahrens 		return (err);
6053b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
6063b2aab18SMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
6073b2aab18SMatthew Ahrens 		*dsp = NULL;
608be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6093b2aab18SMatthew Ahrens 	}
6103b2aab18SMatthew Ahrens 	return (0);
6113b2aab18SMatthew Ahrens }
6123b2aab18SMatthew Ahrens 
6133b2aab18SMatthew Ahrens int
6143b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name,
615503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
616fa9e4066Sahrens {
6173b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold(dp, name, tag, dsp);
6183b2aab18SMatthew Ahrens 	if (err != 0)
619745cd3c5Smaybee 		return (err);
6203b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
621503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
622be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
623745cd3c5Smaybee 	}
624745cd3c5Smaybee 	return (0);
625fa9e4066Sahrens }
626fa9e4066Sahrens 
6273b2aab18SMatthew Ahrens /*
6283b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
6293b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
6303b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
6313b2aab18SMatthew Ahrens  *
6323b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
6333b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
6343b2aab18SMatthew Ahrens  * and accessed.
6353b2aab18SMatthew Ahrens  */
6363b2aab18SMatthew Ahrens void
6373b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
6383b2aab18SMatthew Ahrens {
6393b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
6403b2aab18SMatthew Ahrens 	(void) refcount_add(&ds->ds_longholds, tag);
6413b2aab18SMatthew Ahrens }
6423b2aab18SMatthew Ahrens 
6433b2aab18SMatthew Ahrens void
6443b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
6453b2aab18SMatthew Ahrens {
6463b2aab18SMatthew Ahrens 	(void) refcount_remove(&ds->ds_longholds, tag);
6473b2aab18SMatthew Ahrens }
6483b2aab18SMatthew Ahrens 
6493b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
6503b2aab18SMatthew Ahrens boolean_t
6513b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
6523b2aab18SMatthew Ahrens {
6533b2aab18SMatthew Ahrens 	return (!refcount_is_zero(&ds->ds_longholds));
6543b2aab18SMatthew Ahrens }
6553b2aab18SMatthew Ahrens 
656fa9e4066Sahrens void
657fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
658fa9e4066Sahrens {
659fa9e4066Sahrens 	if (ds == NULL) {
660fa9e4066Sahrens 		(void) strcpy(name, "mos");
661fa9e4066Sahrens 	} else {
662fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
6633b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
664fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
665fa9e4066Sahrens 			(void) strcat(name, "@");
666745cd3c5Smaybee 			/*
667745cd3c5Smaybee 			 * We use a "recursive" mutex so that we
668745cd3c5Smaybee 			 * can call dprintf_ds() with ds_lock held.
669745cd3c5Smaybee 			 */
670fa9e4066Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
671fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
672fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
673fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
674fa9e4066Sahrens 			} else {
675fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
676fa9e4066Sahrens 			}
677fa9e4066Sahrens 		}
678fa9e4066Sahrens 	}
679fa9e4066Sahrens }
680fa9e4066Sahrens 
6813cb34c60Sahrens void
682745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6833cb34c60Sahrens {
6843b2aab18SMatthew Ahrens 	dmu_buf_rele(ds->ds_dbuf, tag);
685745cd3c5Smaybee }
686745cd3c5Smaybee 
687745cd3c5Smaybee void
688503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
689745cd3c5Smaybee {
690d808a4fcSJustin T. Gibbs 	ASSERT3P(ds->ds_owner, ==, tag);
691d808a4fcSJustin T. Gibbs 	ASSERT(ds->ds_dbuf != NULL);
692745cd3c5Smaybee 
6933cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
694745cd3c5Smaybee 	ds->ds_owner = NULL;
6953cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
6963b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
697d808a4fcSJustin T. Gibbs 	dsl_dataset_rele(ds, tag);
6983cb34c60Sahrens }
6993cb34c60Sahrens 
7003cb34c60Sahrens boolean_t
7013b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
7023cb34c60Sahrens {
703745cd3c5Smaybee 	boolean_t gotit = FALSE;
704745cd3c5Smaybee 
7059c3fd121SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
7063cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
7073b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
708503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
7093b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
710745cd3c5Smaybee 		gotit = TRUE;
7113cb34c60Sahrens 	}
7123cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
713745cd3c5Smaybee 	return (gotit);
714745cd3c5Smaybee }
715745cd3c5Smaybee 
7169c3fd121SMatthew Ahrens boolean_t
7179c3fd121SMatthew Ahrens dsl_dataset_has_owner(dsl_dataset_t *ds)
7189c3fd121SMatthew Ahrens {
7199c3fd121SMatthew Ahrens 	boolean_t rv;
7209c3fd121SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
7219c3fd121SMatthew Ahrens 	rv = (ds->ds_owner != NULL);
7229c3fd121SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
7239c3fd121SMatthew Ahrens 	return (rv);
7249c3fd121SMatthew Ahrens }
7259c3fd121SMatthew Ahrens 
726ca0cc391SMatthew Ahrens static void
727ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
728ca0cc391SMatthew Ahrens {
729ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
730ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
731ca0cc391SMatthew Ahrens 	uint64_t zero = 0;
732ca0cc391SMatthew Ahrens 
733ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
734ca0cc391SMatthew Ahrens 
735ca0cc391SMatthew Ahrens 	spa_feature_incr(spa, f, tx);
736ca0cc391SMatthew Ahrens 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
737ca0cc391SMatthew Ahrens 
738ca0cc391SMatthew Ahrens 	VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
739ca0cc391SMatthew Ahrens 	    sizeof (zero), 1, &zero, tx));
740ca0cc391SMatthew Ahrens }
741ca0cc391SMatthew Ahrens 
742ca0cc391SMatthew Ahrens void
743ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
744ca0cc391SMatthew Ahrens {
745ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
746ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
747ca0cc391SMatthew Ahrens 
748ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
749ca0cc391SMatthew Ahrens 
750ca0cc391SMatthew Ahrens 	VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
751ca0cc391SMatthew Ahrens 	spa_feature_decr(spa, f, tx);
752ca0cc391SMatthew Ahrens }
753ca0cc391SMatthew Ahrens 
7541d452cf5Sahrens uint64_t
755088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
756ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
757fa9e4066Sahrens {
7583cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
759fa9e4066Sahrens 	dmu_buf_t *dbuf;
760fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
7613cb34c60Sahrens 	uint64_t dsobj;
762fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
763fa9e4066Sahrens 
764088f3894Sahrens 	if (origin == NULL)
765088f3894Sahrens 		origin = dp->dp_origin_snap;
766088f3894Sahrens 
7673cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
768c1379625SJustin T. Gibbs 	ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
769fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
770c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
771fa9e4066Sahrens 
7721649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
7731649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7743b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
775fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
776fa9e4066Sahrens 	dsphys = dbuf->db_data;
777745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
778fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
779ab04eb8eStimh 	dsphys->ds_flags = flags;
780fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
781fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
782fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
783fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
784ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
785ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
786fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
787088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
788a9799022Sck 
789cde58dbcSMatthew Ahrens 	if (origin == NULL) {
790cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
791cde58dbcSMatthew Ahrens 	} else {
7923b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
793cde58dbcSMatthew Ahrens 
7943cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
795fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
796c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_creation_txg;
797ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
798c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_referenced_bytes;
799fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
800c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_compressed_bytes;
801fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
802c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_uncompressed_bytes;
803c1379625SJustin T. Gibbs 		dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
80442fcb65eSMatthew Ahrens 
80542fcb65eSMatthew Ahrens 		/*
80642fcb65eSMatthew Ahrens 		 * Inherit flags that describe the dataset's contents
80742fcb65eSMatthew Ahrens 		 * (INCONSISTENT) or properties (Case Insensitive).
80842fcb65eSMatthew Ahrens 		 */
809c1379625SJustin T. Gibbs 		dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
81042fcb65eSMatthew Ahrens 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
811fa9e4066Sahrens 
812ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
813ca0cc391SMatthew Ahrens 			if (origin->ds_feature_inuse[f])
814ca0cc391SMatthew Ahrens 				dsl_dataset_activate_feature(dsobj, f, tx);
815ca0cc391SMatthew Ahrens 		}
816b5152584SMatthew Ahrens 
8173cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
818c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin)->ds_num_children++;
819fa9e4066Sahrens 
8203b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
821c1379625SJustin T. Gibbs 		    dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
822c1379625SJustin T. Gibbs 		    FTAG, &ohds));
823cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
824cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
825cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
826cde58dbcSMatthew Ahrens 
827088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
828c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
829c1379625SJustin T. Gibbs 				dsl_dataset_phys(origin)->ds_next_clones_obj =
830088f3894Sahrens 				    zap_create(mos,
831088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
832088f3894Sahrens 			}
8333b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
834c1379625SJustin T. Gibbs 			    dsl_dataset_phys(origin)->ds_next_clones_obj,
835c1379625SJustin T. Gibbs 			    dsobj, tx));
836088f3894Sahrens 		}
837088f3894Sahrens 
838fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
839c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
840cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
841c1379625SJustin T. Gibbs 			if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
842cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
843c1379625SJustin T. Gibbs 				dsl_dir_phys(origin->ds_dir)->dd_clones =
844cde58dbcSMatthew Ahrens 				    zap_create(mos,
845cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
846cde58dbcSMatthew Ahrens 			}
8473b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
848c1379625SJustin T. Gibbs 			    dsl_dir_phys(origin->ds_dir)->dd_clones,
849c1379625SJustin T. Gibbs 			    dsobj, tx));
850cde58dbcSMatthew Ahrens 		}
851fa9e4066Sahrens 	}
852ab04eb8eStimh 
853ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
854ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
855ab04eb8eStimh 
856ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
857fa9e4066Sahrens 
858fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
859c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
8603cb34c60Sahrens 
8613cb34c60Sahrens 	return (dsobj);
8623cb34c60Sahrens }
8633cb34c60Sahrens 
8643b2aab18SMatthew Ahrens static void
8653b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
8663b2aab18SMatthew Ahrens {
8673b2aab18SMatthew Ahrens 	objset_t *os;
8683b2aab18SMatthew Ahrens 
8693b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
8703b2aab18SMatthew Ahrens 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
8713b2aab18SMatthew Ahrens 	dsl_dataset_dirty(ds, tx);
8723b2aab18SMatthew Ahrens }
8733b2aab18SMatthew Ahrens 
8743cb34c60Sahrens uint64_t
875ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
876ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8773cb34c60Sahrens {
8783cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8793cb34c60Sahrens 	uint64_t dsobj, ddobj;
8803cb34c60Sahrens 	dsl_dir_t *dd;
8813cb34c60Sahrens 
8823b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
8833cb34c60Sahrens 	ASSERT(lastname[0] != '@');
8843cb34c60Sahrens 
885088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8863b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
8873cb34c60Sahrens 
8883b2aab18SMatthew Ahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
8893b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
8903cb34c60Sahrens 
8913cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8923cb34c60Sahrens 
893a2afb611SJerry Jelinek 	/*
894a2afb611SJerry Jelinek 	 * Since we're creating a new node we know it's a leaf, so we can
895a2afb611SJerry Jelinek 	 * initialize the counts if the limit feature is active.
896a2afb611SJerry Jelinek 	 */
897a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
898a2afb611SJerry Jelinek 		uint64_t cnt = 0;
899a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
900a2afb611SJerry Jelinek 
901a2afb611SJerry Jelinek 		dsl_dir_zapify(dd, tx);
902a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
903a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
904a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
905a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
906a2afb611SJerry Jelinek 	}
907a2afb611SJerry Jelinek 
9083b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
909fa9e4066Sahrens 
910feaa74e4SMark Maybee 	/*
911feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
912feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
913feaa74e4SMark Maybee 	 */
9143b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
915feaa74e4SMark Maybee 		dsl_dataset_t *ds;
916feaa74e4SMark Maybee 
9173b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
9183b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
919feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
920feaa74e4SMark Maybee 	}
921feaa74e4SMark Maybee 
9221d452cf5Sahrens 	return (dsobj);
923fa9e4066Sahrens }
924fa9e4066Sahrens 
9251d452cf5Sahrens /*
9263b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
9273b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
9283b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
9293b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
9303b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
9313b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
9321d452cf5Sahrens  */
9333b2aab18SMatthew Ahrens void
9343b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
9351d452cf5Sahrens {
9363b2aab18SMatthew Ahrens 	uint64_t mrs_used;
9373b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
9381d452cf5Sahrens 
939bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
9401d452cf5Sahrens 
941c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
942c1379625SJustin T. Gibbs 		mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
9433b2aab18SMatthew Ahrens 	else
9443b2aab18SMatthew Ahrens 		mrs_used = 0;
945842727c2SChris Kirby 
9463b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
947fa9e4066Sahrens 
9483b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
949c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes =
950c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
95119b94df9SMatthew Ahrens 
9523b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
9533b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
954c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
955fa9e4066Sahrens }
956fa9e4066Sahrens 
9573b2aab18SMatthew Ahrens void
9583b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
9593b2aab18SMatthew Ahrens     dmu_tx_t *tx)
960842727c2SChris Kirby {
9613b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
9623b2aab18SMatthew Ahrens 	uint64_t count;
9633b2aab18SMatthew Ahrens 	int err;
9643b2aab18SMatthew Ahrens 
965c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
966c1379625SJustin T. Gibbs 	err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
967c1379625SJustin T. Gibbs 	    obj, tx);
9683b2aab18SMatthew Ahrens 	/*
9693b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
9703b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
9713b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
9723b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
9733b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
9743b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
9753b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
9763b2aab18SMatthew Ahrens 	 * remove this one.
9773b2aab18SMatthew Ahrens 	 */
9783b2aab18SMatthew Ahrens 	if (err != ENOENT)
9793b2aab18SMatthew Ahrens 		VERIFY0(err);
980c1379625SJustin T. Gibbs 	ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
9813b2aab18SMatthew Ahrens 	    &count));
982c1379625SJustin T. Gibbs 	ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
9833b2aab18SMatthew Ahrens }
984842727c2SChris Kirby 
985842727c2SChris Kirby 
9863b2aab18SMatthew Ahrens blkptr_t *
9873b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds)
9883b2aab18SMatthew Ahrens {
989c1379625SJustin T. Gibbs 	return (&dsl_dataset_phys(ds)->ds_bp);
990842727c2SChris Kirby }
991842727c2SChris Kirby 
9923b2aab18SMatthew Ahrens void
9933b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
994842727c2SChris Kirby {
9953b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
9963b2aab18SMatthew Ahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
9973b2aab18SMatthew Ahrens 	if (ds == NULL) {
9983b2aab18SMatthew Ahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
9993b2aab18SMatthew Ahrens 	} else {
10003b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
1001c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_bp = *bp;
1002842727c2SChris Kirby 	}
10033b2aab18SMatthew Ahrens }
1004842727c2SChris Kirby 
10053b2aab18SMatthew Ahrens spa_t *
10063b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
10073b2aab18SMatthew Ahrens {
10083b2aab18SMatthew Ahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1009842727c2SChris Kirby }
1010842727c2SChris Kirby 
10113b2aab18SMatthew Ahrens void
10123b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1013fa9e4066Sahrens {
10143b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
1015842727c2SChris Kirby 
10163b2aab18SMatthew Ahrens 	if (ds == NULL) /* this is the meta-objset */
10173b2aab18SMatthew Ahrens 		return;
10181d452cf5Sahrens 
10193b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1020fa9e4066Sahrens 
1021c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
10223b2aab18SMatthew Ahrens 		panic("dirtying snapshot!");
1023fa9e4066Sahrens 
10243b2aab18SMatthew Ahrens 	dp = ds->ds_dir->dd_pool;
1025ce636f8bSMatthew Ahrens 
10263b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
10273b2aab18SMatthew Ahrens 		/* up the hold count until we can be written out */
10283b2aab18SMatthew Ahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
10293b2aab18SMatthew Ahrens 	}
10303b2aab18SMatthew Ahrens }
1031fa9e4066Sahrens 
10322e2c1355SMatthew Ahrens boolean_t
10332e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
10342e2c1355SMatthew Ahrens {
10352e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
10362e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
10372e2c1355SMatthew Ahrens 		    ds, t))
10382e2c1355SMatthew Ahrens 			return (B_TRUE);
10392e2c1355SMatthew Ahrens 	}
10402e2c1355SMatthew Ahrens 	return (B_FALSE);
10412e2c1355SMatthew Ahrens }
10422e2c1355SMatthew Ahrens 
1043fa9e4066Sahrens static int
10443b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1045fa9e4066Sahrens {
10463b2aab18SMatthew Ahrens 	uint64_t asize;
1047fa9e4066Sahrens 
10483b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
104988b7b0f2SMatthew Ahrens 		return (0);
1050fa9e4066Sahrens 
1051e1930233Sbonwick 	/*
10523b2aab18SMatthew Ahrens 	 * If there's an fs-only reservation, any blocks that might become
10533b2aab18SMatthew Ahrens 	 * owned by the snapshot dataset must be accommodated by space
10543b2aab18SMatthew Ahrens 	 * outside of the reservation.
1055e1930233Sbonwick 	 */
10563b2aab18SMatthew Ahrens 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1057c1379625SJustin T. Gibbs 	asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
10583b2aab18SMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1059be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
1060e1930233Sbonwick 
10613cb34c60Sahrens 	/*
10623b2aab18SMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
10633b2aab18SMatthew Ahrens 	 * snapshot checks in this sync group.
10643cb34c60Sahrens 	 */
10653b2aab18SMatthew Ahrens 	if (asize > 0)
10663b2aab18SMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
10673cb34c60Sahrens 
1068e1930233Sbonwick 	return (0);
1069e1930233Sbonwick }
1070e1930233Sbonwick 
10713b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg {
10723b2aab18SMatthew Ahrens 	nvlist_t *ddsa_snaps;
10733b2aab18SMatthew Ahrens 	nvlist_t *ddsa_props;
10743b2aab18SMatthew Ahrens 	nvlist_t *ddsa_errors;
1075a2afb611SJerry Jelinek 	cred_t *ddsa_cr;
10763b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t;
1077842727c2SChris Kirby 
10783cb34c60Sahrens int
10793b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1080a2afb611SJerry Jelinek     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
10811d452cf5Sahrens {
10823b2aab18SMatthew Ahrens 	int error;
10833b2aab18SMatthew Ahrens 	uint64_t value;
1084fa9e4066Sahrens 
10853b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1086745cd3c5Smaybee 
10873b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
1088842727c2SChris Kirby 		return (0);
1089fa9e4066Sahrens 
1090fa9e4066Sahrens 	/*
10913b2aab18SMatthew Ahrens 	 * We don't allow multiple snapshots of the same txg.  If there
10923b2aab18SMatthew Ahrens 	 * is already one, try again.
1093fa9e4066Sahrens 	 */
1094c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1095be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
1096fa9e4066Sahrens 
1097fa9e4066Sahrens 	/*
10983b2aab18SMatthew Ahrens 	 * Check for conflicting snapshot name.
1099fa9e4066Sahrens 	 */
11003b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
11013b2aab18SMatthew Ahrens 	if (error == 0)
1102be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
11033b2aab18SMatthew Ahrens 	if (error != ENOENT)
11043b2aab18SMatthew Ahrens 		return (error);
1105842727c2SChris Kirby 
1106ca48f36fSKeith M Wesolowski 	/*
1107ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
1108ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
1109ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
1110ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
1111ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1112ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
1113ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
1114ca48f36fSKeith M Wesolowski 	 */
1115ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
1116ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
1117ca48f36fSKeith M Wesolowski 
1118a2afb611SJerry Jelinek 	/*
1119a2afb611SJerry Jelinek 	 * Skip the check for temporary snapshots or if we have already checked
1120a2afb611SJerry Jelinek 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1121a2afb611SJerry Jelinek 	 * check the count here when we're receiving a stream.
1122a2afb611SJerry Jelinek 	 */
1123a2afb611SJerry Jelinek 	if (cnt != 0 && cr != NULL) {
1124a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1125a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1126a2afb611SJerry Jelinek 		if (error != 0)
1127a2afb611SJerry Jelinek 			return (error);
1128a2afb611SJerry Jelinek 	}
1129a2afb611SJerry Jelinek 
11303b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
11313b2aab18SMatthew Ahrens 	if (error != 0)
11323b2aab18SMatthew Ahrens 		return (error);
1133842727c2SChris Kirby 
11341d452cf5Sahrens 	return (0);
11351d452cf5Sahrens }
11361d452cf5Sahrens 
11373b2aab18SMatthew Ahrens static int
11383b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1139745cd3c5Smaybee {
11403b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
11413b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11423b2aab18SMatthew Ahrens 	nvpair_t *pair;
11433b2aab18SMatthew Ahrens 	int rv = 0;
11443b2aab18SMatthew Ahrens 
1145a2afb611SJerry Jelinek 	/*
1146a2afb611SJerry Jelinek 	 * Pre-compute how many total new snapshots will be created for each
1147a2afb611SJerry Jelinek 	 * level in the tree and below. This is needed for validating the
1148a2afb611SJerry Jelinek 	 * snapshot limit when either taking a recursive snapshot or when
1149a2afb611SJerry Jelinek 	 * taking multiple snapshots.
1150a2afb611SJerry Jelinek 	 *
1151a2afb611SJerry Jelinek 	 * The problem is that the counts are not actually adjusted when
1152a2afb611SJerry Jelinek 	 * we are checking, only when we finally sync. For a single snapshot,
1153a2afb611SJerry Jelinek 	 * this is easy, the count will increase by 1 at each node up the tree,
1154a2afb611SJerry Jelinek 	 * but its more complicated for the recursive/multiple snapshot case.
1155a2afb611SJerry Jelinek 	 *
1156a2afb611SJerry Jelinek 	 * The dsl_fs_ss_limit_check function does recursively check the count
1157a2afb611SJerry Jelinek 	 * at each level up the tree but since it is validating each snapshot
1158a2afb611SJerry Jelinek 	 * independently we need to be sure that we are validating the complete
1159a2afb611SJerry Jelinek 	 * count for the entire set of snapshots. We do this by rolling up the
1160a2afb611SJerry Jelinek 	 * counts for each component of the name into an nvlist and then
1161a2afb611SJerry Jelinek 	 * checking each of those cases with the aggregated count.
1162a2afb611SJerry Jelinek 	 *
1163a2afb611SJerry Jelinek 	 * This approach properly handles not only the recursive snapshot
1164a2afb611SJerry Jelinek 	 * case (where we get all of those on the ddsa_snaps list) but also
1165a2afb611SJerry Jelinek 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1166a2afb611SJerry Jelinek 	 * validate the limit on 'a' using a count of 2).
1167a2afb611SJerry Jelinek 	 *
1168a2afb611SJerry Jelinek 	 * We validate the snapshot names in the third loop and only report
1169a2afb611SJerry Jelinek 	 * name errors once.
1170a2afb611SJerry Jelinek 	 */
1171a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1172a2afb611SJerry Jelinek 		nvlist_t *cnt_track = NULL;
1173a2afb611SJerry Jelinek 		cnt_track = fnvlist_alloc();
1174a2afb611SJerry Jelinek 
1175a2afb611SJerry Jelinek 		/* Rollup aggregated counts into the cnt_track list */
1176a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1177a2afb611SJerry Jelinek 		    pair != NULL;
1178a2afb611SJerry Jelinek 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1179a2afb611SJerry Jelinek 			char *pdelim;
1180a2afb611SJerry Jelinek 			uint64_t val;
1181a2afb611SJerry Jelinek 			char nm[MAXPATHLEN];
1182a2afb611SJerry Jelinek 
1183a2afb611SJerry Jelinek 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1184a2afb611SJerry Jelinek 			pdelim = strchr(nm, '@');
1185a2afb611SJerry Jelinek 			if (pdelim == NULL)
1186a2afb611SJerry Jelinek 				continue;
1187a2afb611SJerry Jelinek 			*pdelim = '\0';
1188a2afb611SJerry Jelinek 
1189a2afb611SJerry Jelinek 			do {
1190a2afb611SJerry Jelinek 				if (nvlist_lookup_uint64(cnt_track, nm,
1191a2afb611SJerry Jelinek 				    &val) == 0) {
1192a2afb611SJerry Jelinek 					/* update existing entry */
1193a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm,
1194a2afb611SJerry Jelinek 					    val + 1);
1195a2afb611SJerry Jelinek 				} else {
1196a2afb611SJerry Jelinek 					/* add to list */
1197a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm, 1);
1198a2afb611SJerry Jelinek 				}
1199a2afb611SJerry Jelinek 
1200a2afb611SJerry Jelinek 				pdelim = strrchr(nm, '/');
1201a2afb611SJerry Jelinek 				if (pdelim != NULL)
1202a2afb611SJerry Jelinek 					*pdelim = '\0';
1203a2afb611SJerry Jelinek 			} while (pdelim != NULL);
1204a2afb611SJerry Jelinek 		}
1205a2afb611SJerry Jelinek 
1206a2afb611SJerry Jelinek 		/* Check aggregated counts at each level */
1207a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1208a2afb611SJerry Jelinek 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1209a2afb611SJerry Jelinek 			int error = 0;
1210a2afb611SJerry Jelinek 			char *name;
1211a2afb611SJerry Jelinek 			uint64_t cnt = 0;
1212a2afb611SJerry Jelinek 			dsl_dataset_t *ds;
1213a2afb611SJerry Jelinek 
1214a2afb611SJerry Jelinek 			name = nvpair_name(pair);
1215a2afb611SJerry Jelinek 			cnt = fnvpair_value_uint64(pair);
1216a2afb611SJerry Jelinek 			ASSERT(cnt > 0);
1217a2afb611SJerry Jelinek 
1218a2afb611SJerry Jelinek 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1219a2afb611SJerry Jelinek 			if (error == 0) {
1220a2afb611SJerry Jelinek 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1221a2afb611SJerry Jelinek 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1222a2afb611SJerry Jelinek 				    ddsa->ddsa_cr);
1223a2afb611SJerry Jelinek 				dsl_dataset_rele(ds, FTAG);
1224a2afb611SJerry Jelinek 			}
1225a2afb611SJerry Jelinek 
1226a2afb611SJerry Jelinek 			if (error != 0) {
1227a2afb611SJerry Jelinek 				if (ddsa->ddsa_errors != NULL)
1228a2afb611SJerry Jelinek 					fnvlist_add_int32(ddsa->ddsa_errors,
1229a2afb611SJerry Jelinek 					    name, error);
1230a2afb611SJerry Jelinek 				rv = error;
1231a2afb611SJerry Jelinek 				/* only report one error for this check */
1232a2afb611SJerry Jelinek 				break;
1233a2afb611SJerry Jelinek 			}
1234a2afb611SJerry Jelinek 		}
1235a2afb611SJerry Jelinek 		nvlist_free(cnt_track);
1236a2afb611SJerry Jelinek 	}
1237a2afb611SJerry Jelinek 
12383b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
12393b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
12403b2aab18SMatthew Ahrens 		int error = 0;
12413b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
12423b2aab18SMatthew Ahrens 		char *name, *atp;
12433b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
12443b2aab18SMatthew Ahrens 
12453b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
12463b2aab18SMatthew Ahrens 		if (strlen(name) >= MAXNAMELEN)
1247be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
12483b2aab18SMatthew Ahrens 		if (error == 0) {
12493b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
12503b2aab18SMatthew Ahrens 			if (atp == NULL)
1251be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
12523b2aab18SMatthew Ahrens 			if (error == 0)
12533b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
12543b2aab18SMatthew Ahrens 		}
12553b2aab18SMatthew Ahrens 		if (error == 0)
12563b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
12573b2aab18SMatthew Ahrens 		if (error == 0) {
1258a2afb611SJerry Jelinek 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
12593b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
1260a2afb611SJerry Jelinek 			    atp + 1, tx, B_FALSE, 0, NULL);
12613b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
12623b2aab18SMatthew Ahrens 		}
1263745cd3c5Smaybee 
12643b2aab18SMatthew Ahrens 		if (error != 0) {
12653b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
12663b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
12673b2aab18SMatthew Ahrens 				    name, error);
12683b2aab18SMatthew Ahrens 			}
12693b2aab18SMatthew Ahrens 			rv = error;
12703b2aab18SMatthew Ahrens 		}
12713b2aab18SMatthew Ahrens 	}
1272a2afb611SJerry Jelinek 
12733b2aab18SMatthew Ahrens 	return (rv);
1274745cd3c5Smaybee }
1275745cd3c5Smaybee 
12763b2aab18SMatthew Ahrens void
12773b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
12783b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1279745cd3c5Smaybee {
12803b2aab18SMatthew Ahrens 	static zil_header_t zero_zil;
1281745cd3c5Smaybee 
12823b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
12833b2aab18SMatthew Ahrens 	dmu_buf_t *dbuf;
12843b2aab18SMatthew Ahrens 	dsl_dataset_phys_t *dsphys;
12853b2aab18SMatthew Ahrens 	uint64_t dsobj, crtxg;
12863b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
12873b2aab18SMatthew Ahrens 	objset_t *os;
1288745cd3c5Smaybee 
12893b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1290c33e334fSMatthew Ahrens 
1291c33e334fSMatthew Ahrens 	/*
12923b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
12933b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1294c33e334fSMatthew Ahrens 	 */
12953b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
12963b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
12973b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
12983b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
1299c33e334fSMatthew Ahrens 
1300a2afb611SJerry Jelinek 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1301cde58dbcSMatthew Ahrens 
1302cde58dbcSMatthew Ahrens 	/*
13033b2aab18SMatthew Ahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1304088f3894Sahrens 	 */
1305088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1306088f3894Sahrens 		crtxg = 1;
1307088f3894Sahrens 	else
1308088f3894Sahrens 		crtxg = tx->tx_txg;
1309088f3894Sahrens 
13101649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
13111649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13123b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1313fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1314fa9e4066Sahrens 	dsphys = dbuf->db_data;
1315745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
13161d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1317fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1318fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1319fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
1320c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1321c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1322fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1323fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1324fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1325088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
1326c1379625SJustin T. Gibbs 	dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1327c1379625SJustin T. Gibbs 	dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1328c1379625SJustin T. Gibbs 	dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1329c1379625SJustin T. Gibbs 	dsphys->ds_uncompressed_bytes =
1330c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1331c1379625SJustin T. Gibbs 	dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1332c1379625SJustin T. Gibbs 	dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1333ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1334fa9e4066Sahrens 
1335ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1336ca0cc391SMatthew Ahrens 		if (ds->ds_feature_inuse[f])
1337ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(dsobj, f, tx);
1338ca0cc391SMatthew Ahrens 	}
1339b5152584SMatthew Ahrens 
1340c1379625SJustin T. Gibbs 	ASSERT3U(ds->ds_prev != 0, ==,
1341c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
13421d452cf5Sahrens 	if (ds->ds_prev) {
1343088f3894Sahrens 		uint64_t next_clones_obj =
1344c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1345c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1346fa9e4066Sahrens 		    ds->ds_object ||
1347c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1348c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1349c1379625SJustin T. Gibbs 		    ds->ds_object) {
13501d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1351c1379625SJustin T. Gibbs 			ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1352c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1353c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1354088f3894Sahrens 		} else if (next_clones_obj != 0) {
13553b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1356c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
13573b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1358088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1359fa9e4066Sahrens 		}
1360fa9e4066Sahrens 	}
1361fa9e4066Sahrens 
1362a9799022Sck 	/*
1363a9799022Sck 	 * If we have a reference-reservation on this dataset, we will
1364a9799022Sck 	 * need to increase the amount of refreservation being charged
1365a9799022Sck 	 * since our unique space is going to zero.
1366a9799022Sck 	 */
1367a9799022Sck 	if (ds->ds_reserved) {
13683f9d6ad7SLin Ling 		int64_t delta;
13693f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1370c1379625SJustin T. Gibbs 		delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1371c1379625SJustin T. Gibbs 		    ds->ds_reserved);
137274e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
13733f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1374a9799022Sck 	}
1375a9799022Sck 
1376fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1377c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj =
1378c1379625SJustin T. Gibbs 	    dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1379c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1380cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1381c1379625SJustin T. Gibbs 	dsl_deadlist_open(&ds->ds_deadlist, mos,
1382c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1383cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
1384c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1385cde58dbcSMatthew Ahrens 
1386c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1387c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1388c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1389c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1390a9799022Sck 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1391c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1392fa9e4066Sahrens 
1393c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
13943b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1395fa9e4066Sahrens 
1396fa9e4066Sahrens 	if (ds->ds_prev)
13973b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
13983b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
1399c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1400ecd6cf80Smarks 
14013f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1402088f3894Sahrens 
140371eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
140471eb0538SChris Kirby 
14054445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1406fa9e4066Sahrens }
1407fa9e4066Sahrens 
14083b2aab18SMatthew Ahrens static void
14093b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1410fa9e4066Sahrens {
14113b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
14123b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14133b2aab18SMatthew Ahrens 	nvpair_t *pair;
141491ebeef5Sahrens 
14153b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
14163b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
14173b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
14183b2aab18SMatthew Ahrens 		char *name, *atp;
14193b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
14203b2aab18SMatthew Ahrens 
14213b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
14223b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
14233b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
14243b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
14253b2aab18SMatthew Ahrens 
14263b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
14273b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
14283b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
14293b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
14303b2aab18SMatthew Ahrens 		}
14313b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14323b2aab18SMatthew Ahrens 	}
1433fa9e4066Sahrens }
1434fa9e4066Sahrens 
14353b2aab18SMatthew Ahrens /*
14363b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
14373b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
14383b2aab18SMatthew Ahrens  */
14393b2aab18SMatthew Ahrens int
14403b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
144119b94df9SMatthew Ahrens {
14423b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
14433b2aab18SMatthew Ahrens 	nvpair_t *pair;
14443b2aab18SMatthew Ahrens 	boolean_t needsuspend;
14453b2aab18SMatthew Ahrens 	int error;
14463b2aab18SMatthew Ahrens 	spa_t *spa;
14473b2aab18SMatthew Ahrens 	char *firstname;
14483b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
144919b94df9SMatthew Ahrens 
14503b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
14513b2aab18SMatthew Ahrens 	if (pair == NULL)
14523b2aab18SMatthew Ahrens 		return (0);
14533b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
14543b2aab18SMatthew Ahrens 
14553b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
14563b2aab18SMatthew Ahrens 	if (error != 0)
14573b2aab18SMatthew Ahrens 		return (error);
14583b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
14593b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
14603b2aab18SMatthew Ahrens 
14613b2aab18SMatthew Ahrens 	if (needsuspend) {
14623b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
14633b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
14643b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
14653b2aab18SMatthew Ahrens 			char fsname[MAXNAMELEN];
14663b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
14673b2aab18SMatthew Ahrens 			char *atp;
14683b2aab18SMatthew Ahrens 			void *cookie;
14693b2aab18SMatthew Ahrens 
14703b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
14713b2aab18SMatthew Ahrens 			if (atp == NULL) {
1472be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
14733b2aab18SMatthew Ahrens 				break;
14743b2aab18SMatthew Ahrens 			}
14753b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
14763b2aab18SMatthew Ahrens 
14773b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
14783b2aab18SMatthew Ahrens 			if (error != 0)
14793b2aab18SMatthew Ahrens 				break;
14803b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
14813b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
14823b2aab18SMatthew Ahrens 		}
14833b2aab18SMatthew Ahrens 	}
14843b2aab18SMatthew Ahrens 
14853b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
14863b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
14873b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
1488a2afb611SJerry Jelinek 	ddsa.ddsa_cr = CRED();
14893b2aab18SMatthew Ahrens 
14903b2aab18SMatthew Ahrens 	if (error == 0) {
14913b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
14923b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
14937d46dc6cSMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
14943b2aab18SMatthew Ahrens 	}
14953b2aab18SMatthew Ahrens 
14963b2aab18SMatthew Ahrens 	if (suspended != NULL) {
14973b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
14983b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
14993b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
15003b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
15013b2aab18SMatthew Ahrens 		}
15023b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
15033b2aab18SMatthew Ahrens 	}
15043b2aab18SMatthew Ahrens 
15053b2aab18SMatthew Ahrens 	return (error);
15063b2aab18SMatthew Ahrens }
15073b2aab18SMatthew Ahrens 
15083b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
15093b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
15103b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
15113b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
15123b2aab18SMatthew Ahrens 	const char *ddsta_htag;
15133b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
15143b2aab18SMatthew Ahrens 
15153b2aab18SMatthew Ahrens static int
15163b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
15173b2aab18SMatthew Ahrens {
15183b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15193b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15203b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15213b2aab18SMatthew Ahrens 	int error;
15223b2aab18SMatthew Ahrens 
15233b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
15243b2aab18SMatthew Ahrens 	if (error != 0)
15253b2aab18SMatthew Ahrens 		return (error);
15263b2aab18SMatthew Ahrens 
1527a2afb611SJerry Jelinek 	/* NULL cred means no limit check for tmp snapshot */
1528ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1529a2afb611SJerry Jelinek 	    tx, B_FALSE, 0, NULL);
15303b2aab18SMatthew Ahrens 	if (error != 0) {
15313b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15323b2aab18SMatthew Ahrens 		return (error);
15333b2aab18SMatthew Ahrens 	}
15343b2aab18SMatthew Ahrens 
15353b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
15363b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1537be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
15383b2aab18SMatthew Ahrens 	}
15393b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
15403b2aab18SMatthew Ahrens 	    B_TRUE, tx);
15413b2aab18SMatthew Ahrens 	if (error != 0) {
15423b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15433b2aab18SMatthew Ahrens 		return (error);
15443b2aab18SMatthew Ahrens 	}
15453b2aab18SMatthew Ahrens 
15463b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15473b2aab18SMatthew Ahrens 	return (0);
15483b2aab18SMatthew Ahrens }
15493b2aab18SMatthew Ahrens 
15503b2aab18SMatthew Ahrens static void
15513b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
15523b2aab18SMatthew Ahrens {
15533b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15543b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15553b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15563b2aab18SMatthew Ahrens 
15573b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
15583b2aab18SMatthew Ahrens 
15593b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
15603b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
15613b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
15623b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
15633b2aab18SMatthew Ahrens 
15643b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15653b2aab18SMatthew Ahrens }
15663b2aab18SMatthew Ahrens 
15673b2aab18SMatthew Ahrens int
15683b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
15693b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
15703b2aab18SMatthew Ahrens {
15713b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
15723b2aab18SMatthew Ahrens 	int error;
15733b2aab18SMatthew Ahrens 	spa_t *spa;
15743b2aab18SMatthew Ahrens 	boolean_t needsuspend;
15753b2aab18SMatthew Ahrens 	void *cookie;
15763b2aab18SMatthew Ahrens 
15773b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
15783b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
15793b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
15803b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
15813b2aab18SMatthew Ahrens 
15823b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
15833b2aab18SMatthew Ahrens 	if (error != 0)
15843b2aab18SMatthew Ahrens 		return (error);
15853b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
15863b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
15873b2aab18SMatthew Ahrens 
15883b2aab18SMatthew Ahrens 	if (needsuspend) {
15893b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
15903b2aab18SMatthew Ahrens 		if (error != 0)
15913b2aab18SMatthew Ahrens 			return (error);
15923b2aab18SMatthew Ahrens 	}
15933b2aab18SMatthew Ahrens 
15943b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
15957d46dc6cSMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
15963b2aab18SMatthew Ahrens 
15973b2aab18SMatthew Ahrens 	if (needsuspend)
15983b2aab18SMatthew Ahrens 		zil_resume(cookie);
15993b2aab18SMatthew Ahrens 	return (error);
16003b2aab18SMatthew Ahrens }
16013b2aab18SMatthew Ahrens 
16023b2aab18SMatthew Ahrens 
16033b2aab18SMatthew Ahrens void
16043b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
16053b2aab18SMatthew Ahrens {
16063b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
16073b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1608c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
16093b2aab18SMatthew Ahrens 
16103b2aab18SMatthew Ahrens 	/*
16113b2aab18SMatthew Ahrens 	 * in case we had to change ds_fsid_guid when we opened it,
16123b2aab18SMatthew Ahrens 	 * sync it out now.
16133b2aab18SMatthew Ahrens 	 */
16143b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1615c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
16163b2aab18SMatthew Ahrens 
16179c3fd121SMatthew Ahrens 	if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
16189c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16199c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
16209c3fd121SMatthew Ahrens 		    &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
16219c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16229c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
16239c3fd121SMatthew Ahrens 		    &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
16249c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16259c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
16269c3fd121SMatthew Ahrens 		    &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
16279c3fd121SMatthew Ahrens 		ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
16289c3fd121SMatthew Ahrens 		ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
16299c3fd121SMatthew Ahrens 		ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
16309c3fd121SMatthew Ahrens 	}
16319c3fd121SMatthew Ahrens 
16323b2aab18SMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
1633b5152584SMatthew Ahrens 
1634ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1635ca0cc391SMatthew Ahrens 		if (ds->ds_feature_activation_needed[f]) {
1636ca0cc391SMatthew Ahrens 			if (ds->ds_feature_inuse[f])
1637ca0cc391SMatthew Ahrens 				continue;
1638ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(ds->ds_object, f, tx);
1639ca0cc391SMatthew Ahrens 			ds->ds_feature_inuse[f] = B_TRUE;
1640ca0cc391SMatthew Ahrens 		}
1641b5152584SMatthew Ahrens 	}
16423b2aab18SMatthew Ahrens }
16433b2aab18SMatthew Ahrens 
16443b2aab18SMatthew Ahrens static void
16453b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
16463b2aab18SMatthew Ahrens {
16473b2aab18SMatthew Ahrens 	uint64_t count = 0;
16483b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
16493b2aab18SMatthew Ahrens 	zap_cursor_t zc;
16503b2aab18SMatthew Ahrens 	zap_attribute_t za;
16513b2aab18SMatthew Ahrens 	nvlist_t *propval = fnvlist_alloc();
16523b2aab18SMatthew Ahrens 	nvlist_t *val = fnvlist_alloc();
16533b2aab18SMatthew Ahrens 
16543b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
165519b94df9SMatthew Ahrens 
165619b94df9SMatthew Ahrens 	/*
16573b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
165819b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
165919b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
166019b94df9SMatthew Ahrens 	 */
1661c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1662c1379625SJustin T. Gibbs 		VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
166319b94df9SMatthew Ahrens 		    &count));
166419b94df9SMatthew Ahrens 	}
1665c1379625SJustin T. Gibbs 	if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
166619b94df9SMatthew Ahrens 		goto fail;
1667c1379625SJustin T. Gibbs 	for (zap_cursor_init(&zc, mos,
1668c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_next_clones_obj);
166919b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
167019b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
167119b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
167219b94df9SMatthew Ahrens 		char buf[ZFS_MAXNAMELEN];
16733b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
16743b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
167519b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
16763b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
167719b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
167819b94df9SMatthew Ahrens 	}
167919b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
16803b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
16813b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
168219b94df9SMatthew Ahrens fail:
168319b94df9SMatthew Ahrens 	nvlist_free(val);
168419b94df9SMatthew Ahrens 	nvlist_free(propval);
168519b94df9SMatthew Ahrens }
168619b94df9SMatthew Ahrens 
16879c3fd121SMatthew Ahrens static void
16889c3fd121SMatthew Ahrens get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv)
16899c3fd121SMatthew Ahrens {
16909c3fd121SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
16919c3fd121SMatthew Ahrens 
16929c3fd121SMatthew Ahrens 	if (dsl_dataset_has_resume_receive_state(ds)) {
16939c3fd121SMatthew Ahrens 		char *str;
16949c3fd121SMatthew Ahrens 		void *packed;
16959c3fd121SMatthew Ahrens 		uint8_t *compressed;
16969c3fd121SMatthew Ahrens 		uint64_t val;
16979c3fd121SMatthew Ahrens 		nvlist_t *token_nv = fnvlist_alloc();
16989c3fd121SMatthew Ahrens 		size_t packed_size, compressed_size;
16999c3fd121SMatthew Ahrens 
17009c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17019c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
17029c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "fromguid", val);
17039c3fd121SMatthew Ahrens 		}
17049c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17059c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
17069c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "object", val);
17079c3fd121SMatthew Ahrens 		}
17089c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17099c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
17109c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "offset", val);
17119c3fd121SMatthew Ahrens 		}
17129c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17139c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
17149c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "bytes", val);
17159c3fd121SMatthew Ahrens 		}
17169c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17179c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
17189c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "toguid", val);
17199c3fd121SMatthew Ahrens 		}
17209c3fd121SMatthew Ahrens 		char buf[256];
17219c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17229c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
17239c3fd121SMatthew Ahrens 			fnvlist_add_string(token_nv, "toname", buf);
17249c3fd121SMatthew Ahrens 		}
17259c3fd121SMatthew Ahrens 		if (zap_contains(dp->dp_meta_objset, ds->ds_object,
17269c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_EMBEDOK) == 0) {
17279c3fd121SMatthew Ahrens 			fnvlist_add_boolean(token_nv, "embedok");
17289c3fd121SMatthew Ahrens 		}
17299c3fd121SMatthew Ahrens 		packed = fnvlist_pack(token_nv, &packed_size);
17309c3fd121SMatthew Ahrens 		fnvlist_free(token_nv);
17319c3fd121SMatthew Ahrens 		compressed = kmem_alloc(packed_size, KM_SLEEP);
17329c3fd121SMatthew Ahrens 
17339c3fd121SMatthew Ahrens 		compressed_size = gzip_compress(packed, compressed,
17349c3fd121SMatthew Ahrens 		    packed_size, packed_size, 6);
17359c3fd121SMatthew Ahrens 
17369c3fd121SMatthew Ahrens 		zio_cksum_t cksum;
17379c3fd121SMatthew Ahrens 		fletcher_4_native(compressed, compressed_size, NULL, &cksum);
17389c3fd121SMatthew Ahrens 
17399c3fd121SMatthew Ahrens 		str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP);
17409c3fd121SMatthew Ahrens 		for (int i = 0; i < compressed_size; i++) {
17419c3fd121SMatthew Ahrens 			(void) sprintf(str + i * 2, "%02x", compressed[i]);
17429c3fd121SMatthew Ahrens 		}
17439c3fd121SMatthew Ahrens 		str[compressed_size * 2] = '\0';
17449c3fd121SMatthew Ahrens 		char *propval = kmem_asprintf("%u-%llx-%llx-%s",
17459c3fd121SMatthew Ahrens 		    ZFS_SEND_RESUME_TOKEN_VERSION,
17469c3fd121SMatthew Ahrens 		    (longlong_t)cksum.zc_word[0],
17479c3fd121SMatthew Ahrens 		    (longlong_t)packed_size, str);
17489c3fd121SMatthew Ahrens 		dsl_prop_nvlist_add_string(nv,
17499c3fd121SMatthew Ahrens 		    ZFS_PROP_RECEIVE_RESUME_TOKEN, propval);
17509c3fd121SMatthew Ahrens 		kmem_free(packed, packed_size);
17519c3fd121SMatthew Ahrens 		kmem_free(str, compressed_size * 2 + 1);
17529c3fd121SMatthew Ahrens 		kmem_free(compressed, packed_size);
17539c3fd121SMatthew Ahrens 		strfree(propval);
17549c3fd121SMatthew Ahrens 	}
17559c3fd121SMatthew Ahrens }
17569c3fd121SMatthew Ahrens 
1757fa9e4066Sahrens void
1758a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1759fa9e4066Sahrens {
17603b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1761187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
1762a9799022Sck 
17633b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
17643b2aab18SMatthew Ahrens 
1765c1379625SJustin T. Gibbs 	ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
1766c1379625SJustin T. Gibbs 	    (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
1767c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_compressed_bytes);
17684445fffbSMatthew Ahrens 
17694445fffbSMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
177077372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1771c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes);
17724445fffbSMatthew Ahrens 
1773bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
17744445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
17754445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1776c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_unique_bytes);
17774445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
17784445fffbSMatthew Ahrens 	} else {
1779b461c746SMatthew Ahrens 		if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
1780b461c746SMatthew Ahrens 			char buf[MAXNAMELEN];
1781b461c746SMatthew Ahrens 			dsl_dataset_name(ds->ds_prev, buf);
1782b461c746SMatthew Ahrens 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1783b461c746SMatthew Ahrens 		}
1784b461c746SMatthew Ahrens 
17854445fffbSMatthew Ahrens 		dsl_dir_stats(ds->ds_dir, nv);
17864445fffbSMatthew Ahrens 	}
1787fa9e4066Sahrens 
1788a9799022Sck 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1789a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1790a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1791a9799022Sck 
1792a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1793c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_time);
1794a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1795c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_txg);
1796a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1797a9799022Sck 	    ds->ds_quota);
1798a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1799a9799022Sck 	    ds->ds_reserved);
1800c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1801c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_guid);
18021d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1803c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
18041d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
18051d713200SEric Schrock 	    ds->ds_object);
180692241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
180792241e0bSTom Erickson 	    ds->ds_userrefs);
1808842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1809842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1810fa9e4066Sahrens 
1811c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
181219b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
181319b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
181419b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
181519b94df9SMatthew Ahrens 
181619b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
1817c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
181819b94df9SMatthew Ahrens 		if (err == 0) {
181919b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
182019b94df9SMatthew Ahrens 			    &comp, &uncomp);
182119b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
182219b94df9SMatthew Ahrens 			if (err == 0) {
182319b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
182419b94df9SMatthew Ahrens 				    written);
182519b94df9SMatthew Ahrens 			}
182619b94df9SMatthew Ahrens 		}
182719b94df9SMatthew Ahrens 	}
18289c3fd121SMatthew Ahrens 
18299c3fd121SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds)) {
18309c3fd121SMatthew Ahrens 		/*
18319c3fd121SMatthew Ahrens 		 * A failed "newfs" (e.g. full) resumable receive leaves
18329c3fd121SMatthew Ahrens 		 * the stats set on this dataset.  Check here for the prop.
18339c3fd121SMatthew Ahrens 		 */
18349c3fd121SMatthew Ahrens 		get_receive_resume_stats(ds, nv);
18359c3fd121SMatthew Ahrens 
18369c3fd121SMatthew Ahrens 		/*
18379c3fd121SMatthew Ahrens 		 * A failed incremental resumable receive leaves the
18389c3fd121SMatthew Ahrens 		 * stats set on our child named "%recv".  Check the child
18399c3fd121SMatthew Ahrens 		 * for the prop.
18409c3fd121SMatthew Ahrens 		 */
18419c3fd121SMatthew Ahrens 		char recvname[ZFS_MAXNAMELEN];
18429c3fd121SMatthew Ahrens 		dsl_dataset_t *recv_ds;
18439c3fd121SMatthew Ahrens 		dsl_dataset_name(ds, recvname);
18449c3fd121SMatthew Ahrens 		(void) strcat(recvname, "/");
18459c3fd121SMatthew Ahrens 		(void) strcat(recvname, recv_clone_name);
18469c3fd121SMatthew Ahrens 		if (dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) {
18479c3fd121SMatthew Ahrens 			get_receive_resume_stats(recv_ds, nv);
18489c3fd121SMatthew Ahrens 			dsl_dataset_rele(recv_ds, FTAG);
18499c3fd121SMatthew Ahrens 		}
18509c3fd121SMatthew Ahrens 	}
1851fa9e4066Sahrens }
1852fa9e4066Sahrens 
1853a2eea2e1Sahrens void
1854a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1855a2eea2e1Sahrens {
18563b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
18573b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
18583b2aab18SMatthew Ahrens 
1859c1379625SJustin T. Gibbs 	stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
1860c1379625SJustin T. Gibbs 	stat->dds_inconsistent =
1861c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
1862c1379625SJustin T. Gibbs 	stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
18634445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
1864bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
1865a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
1866c1379625SJustin T. Gibbs 		stat->dds_num_clones =
1867c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_num_children - 1;
1868ebedde84SEric Taylor 	} else {
1869ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
1870ebedde84SEric Taylor 		stat->dds_num_clones = 0;
1871a2eea2e1Sahrens 
18724445fffbSMatthew Ahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
18734445fffbSMatthew Ahrens 			dsl_dataset_t *ods;
1874a2eea2e1Sahrens 
18753b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
1876c1379625SJustin T. Gibbs 			    dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
1877c1379625SJustin T. Gibbs 			    FTAG, &ods));
18784445fffbSMatthew Ahrens 			dsl_dataset_name(ods, stat->dds_origin);
18793b2aab18SMatthew Ahrens 			dsl_dataset_rele(ods, FTAG);
18804445fffbSMatthew Ahrens 		}
1881a2eea2e1Sahrens 	}
1882a2eea2e1Sahrens }
1883a2eea2e1Sahrens 
1884a2eea2e1Sahrens uint64_t
1885a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1886a2eea2e1Sahrens {
188791ebeef5Sahrens 	return (ds->ds_fsid_guid);
1888a2eea2e1Sahrens }
1889a2eea2e1Sahrens 
1890a2eea2e1Sahrens void
1891a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
1892a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
1893a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1894fa9e4066Sahrens {
1895c1379625SJustin T. Gibbs 	*refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
1896a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1897c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
1898c1379625SJustin T. Gibbs 		*availbytesp +=
1899c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
1900a9799022Sck 	if (ds->ds_quota != 0) {
1901a9799022Sck 		/*
1902a9799022Sck 		 * Adjust available bytes according to refquota
1903a9799022Sck 		 */
1904a9799022Sck 		if (*refdbytesp < ds->ds_quota)
1905a9799022Sck 			*availbytesp = MIN(*availbytesp,
1906a9799022Sck 			    ds->ds_quota - *refdbytesp);
1907a9799022Sck 		else
1908a9799022Sck 			*availbytesp = 0;
1909a9799022Sck 	}
1910c1379625SJustin T. Gibbs 	*usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
1911a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1912fa9e4066Sahrens }
1913fa9e4066Sahrens 
1914f18faf3fSek boolean_t
191534f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1916f18faf3fSek {
1917f18faf3fSek 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1918f18faf3fSek 
19193b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
192034f2f8cfSMatthew Ahrens 	if (snap == NULL)
1921f18faf3fSek 		return (B_FALSE);
1922c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_bp.blk_birth >
1923c1379625SJustin T. Gibbs 	    dsl_dataset_phys(snap)->ds_creation_txg) {
192434f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
19256e0cbcaaSMatthew Ahrens 		/*
19266e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
19276e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
19286e0cbcaaSMatthew Ahrens 		 * modified.
19296e0cbcaaSMatthew Ahrens 		 */
19306e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
19316e0cbcaaSMatthew Ahrens 			return (B_TRUE);
193234f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
19336e0cbcaaSMatthew Ahrens 			return (B_TRUE);
19346e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
193534f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
19366e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
19376e0cbcaaSMatthew Ahrens 	}
1938f18faf3fSek 	return (B_FALSE);
1939f18faf3fSek }
1940f18faf3fSek 
19413b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
19423b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
19433b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
19443b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
19453b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
19463b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
19473b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
19483b2aab18SMatthew Ahrens 
19491d452cf5Sahrens /* ARGSUSED */
1950fa9e4066Sahrens static int
19513b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
19523b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1953fa9e4066Sahrens {
19543b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
19553b2aab18SMatthew Ahrens 	int error;
1956fa9e4066Sahrens 	uint64_t val;
1957fa9e4066Sahrens 
19583b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
19593b2aab18SMatthew Ahrens 	if (error != 0) {
19603b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
19613b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
19623b2aab18SMatthew Ahrens 	}
19631d452cf5Sahrens 
19643b2aab18SMatthew Ahrens 	/* new name should not exist */
19653b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
19663b2aab18SMatthew Ahrens 	if (error == 0)
1967be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
19683b2aab18SMatthew Ahrens 	else if (error == ENOENT)
19693b2aab18SMatthew Ahrens 		error = 0;
1970cdf5b4caSmmusante 
1971cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
19723b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
19733b2aab18SMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1974be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
1975cdf5b4caSmmusante 
19763b2aab18SMatthew Ahrens 	return (error);
19771d452cf5Sahrens }
1978fa9e4066Sahrens 
19793b2aab18SMatthew Ahrens static int
19803b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
19811d452cf5Sahrens {
19823b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
19833b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
19841d452cf5Sahrens 	dsl_dataset_t *hds;
19853b2aab18SMatthew Ahrens 	int error;
1986fa9e4066Sahrens 
19873b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
19883b2aab18SMatthew Ahrens 	if (error != 0)
19893b2aab18SMatthew Ahrens 		return (error);
1990fa9e4066Sahrens 
19913b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
19923b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
19933b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
19943b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
19953b2aab18SMatthew Ahrens 	} else {
19963b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
19973b2aab18SMatthew Ahrens 	}
1998745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
19993b2aab18SMatthew Ahrens 	return (error);
2000fa9e4066Sahrens }
2001fa9e4066Sahrens 
2002cdf5b4caSmmusante static int
20033b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
20043b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
2005cdf5b4caSmmusante {
20063b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20073b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
20083b2aab18SMatthew Ahrens 	uint64_t val;
20093b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
20103b2aab18SMatthew Ahrens 	int error;
2011ecd6cf80Smarks 
20123b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
20133b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
20143b2aab18SMatthew Ahrens 	if (error == ENOENT) {
20153b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
20163b2aab18SMatthew Ahrens 		return (0);
2017ecd6cf80Smarks 	}
2018ecd6cf80Smarks 
20193b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
20203b2aab18SMatthew Ahrens 
20213b2aab18SMatthew Ahrens 	/* log before we change the name */
20223b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
20233b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
2024cdf5b4caSmmusante 
2025a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
2026a2afb611SJerry Jelinek 	    B_FALSE));
20273b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
20283b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
20293b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2030c1379625SJustin T. Gibbs 	VERIFY0(zap_add(dp->dp_meta_objset,
2031c1379625SJustin T. Gibbs 	    dsl_dataset_phys(hds)->ds_snapnames_zapobj,
20323b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
2033cdf5b4caSmmusante 
20343b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2035cdf5b4caSmmusante 	return (0);
2036cdf5b4caSmmusante }
2037cdf5b4caSmmusante 
20383b2aab18SMatthew Ahrens static void
20393b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
2040cdf5b4caSmmusante {
20413b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20423b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20433b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
2044cdf5b4caSmmusante 
20453b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
20463b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
20473b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
20483b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
20493b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
20503b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
20513b2aab18SMatthew Ahrens 	} else {
20523b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
2053cdf5b4caSmmusante 	}
20543b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
2055cdf5b4caSmmusante }
2056cdf5b4caSmmusante 
20573b2aab18SMatthew Ahrens int
20583b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
20593b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
20603a5a36beSmmusante {
20613b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
20623a5a36beSmmusante 
20633b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
20643b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
20653b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
20663b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
20673a5a36beSmmusante 
20683b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
20697d46dc6cSMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
20707d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
20713a5a36beSmmusante }
20723a5a36beSmmusante 
207391948b51SKeith M Wesolowski /*
207491948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
207591948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
207691948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
207791948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
207891948b51SKeith M Wesolowski  * when we release the long hold.
207991948b51SKeith M Wesolowski  */
208091948b51SKeith M Wesolowski static int
208191948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
208291948b51SKeith M Wesolowski {
208391948b51SKeith M Wesolowski 	boolean_t held;
208491948b51SKeith M Wesolowski 
208591948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
208691948b51SKeith M Wesolowski 		return (0);
208791948b51SKeith M Wesolowski 
208891948b51SKeith M Wesolowski 	if (owner != NULL) {
208991948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
209091948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
209191948b51SKeith M Wesolowski 	}
209291948b51SKeith M Wesolowski 
209391948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
209491948b51SKeith M Wesolowski 
209591948b51SKeith M Wesolowski 	if (owner != NULL)
209691948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
209791948b51SKeith M Wesolowski 
209891948b51SKeith M Wesolowski 	if (held)
209991948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
210091948b51SKeith M Wesolowski 
210191948b51SKeith M Wesolowski 	return (0);
210291948b51SKeith M Wesolowski }
210391948b51SKeith M Wesolowski 
210491948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg {
210591948b51SKeith M Wesolowski 	const char *ddra_fsname;
210691948b51SKeith M Wesolowski 	void *ddra_owner;
2107a7027df1SMatthew Ahrens 	nvlist_t *ddra_result;
210891948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t;
210991948b51SKeith M Wesolowski 
21103b2aab18SMatthew Ahrens static int
21113b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
2112fa9e4066Sahrens {
211391948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
21143b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
21151d452cf5Sahrens 	dsl_dataset_t *ds;
21163b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
21173b2aab18SMatthew Ahrens 	int error;
2118fa9e4066Sahrens 
211991948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
21203b2aab18SMatthew Ahrens 	if (error != 0)
21213b2aab18SMatthew Ahrens 		return (error);
2122370c1af0SSanjeev Bagewadi 
21233b2aab18SMatthew Ahrens 	/* must not be a snapshot */
2124bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
21253b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2126be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21273b2aab18SMatthew Ahrens 	}
21283a5a36beSmmusante 
21293b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
2130c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
21313b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2132be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21333b2aab18SMatthew Ahrens 	}
21343a5a36beSmmusante 
213578f17100SMatthew Ahrens 	/* must not have any bookmarks after the most recent snapshot */
213678f17100SMatthew Ahrens 	nvlist_t *proprequest = fnvlist_alloc();
213778f17100SMatthew Ahrens 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
213878f17100SMatthew Ahrens 	nvlist_t *bookmarks = fnvlist_alloc();
213978f17100SMatthew Ahrens 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
214078f17100SMatthew Ahrens 	fnvlist_free(proprequest);
214178f17100SMatthew Ahrens 	if (error != 0)
214278f17100SMatthew Ahrens 		return (error);
214378f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
214478f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
214578f17100SMatthew Ahrens 		nvlist_t *valuenv =
214678f17100SMatthew Ahrens 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
214778f17100SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
214878f17100SMatthew Ahrens 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
2149c1379625SJustin T. Gibbs 		if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
215078f17100SMatthew Ahrens 			fnvlist_free(bookmarks);
215178f17100SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
215278f17100SMatthew Ahrens 			return (SET_ERROR(EEXIST));
215378f17100SMatthew Ahrens 		}
215478f17100SMatthew Ahrens 	}
215578f17100SMatthew Ahrens 	fnvlist_free(bookmarks);
215678f17100SMatthew Ahrens 
215791948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
215891948b51SKeith M Wesolowski 	if (error != 0) {
21593b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
216091948b51SKeith M Wesolowski 		return (error);
21613b2aab18SMatthew Ahrens 	}
21623b2aab18SMatthew Ahrens 
21633b2aab18SMatthew Ahrens 	/*
21643b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
21653b2aab18SMatthew Ahrens 	 * the refquota.
21663b2aab18SMatthew Ahrens 	 */
21673b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
2168c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
21693b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2170be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2171fa9e4066Sahrens 	}
2172370c1af0SSanjeev Bagewadi 
21733b2aab18SMatthew Ahrens 	/*
21743b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
21753b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
21763b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
21773b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
21783b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
21793b2aab18SMatthew Ahrens 	 */
21803b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2181c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
21823b2aab18SMatthew Ahrens 
21833b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
21843b2aab18SMatthew Ahrens 	    unused_refres_delta >
21853b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
21863b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2187be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
2188fa9e4066Sahrens 	}
2189fa9e4066Sahrens 
21903b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
21913b2aab18SMatthew Ahrens 	return (0);
21923b2aab18SMatthew Ahrens }
21931d452cf5Sahrens 
21943b2aab18SMatthew Ahrens static void
21953b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
21963b2aab18SMatthew Ahrens {
219791948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
21983b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
21993b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
22003b2aab18SMatthew Ahrens 	uint64_t cloneobj;
2201a7027df1SMatthew Ahrens 	char namebuf[ZFS_MAXNAMELEN];
22021d452cf5Sahrens 
220391948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
22041d452cf5Sahrens 
2205a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
2206a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2207a7027df1SMatthew Ahrens 
22083b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
22093b2aab18SMatthew Ahrens 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
22101d452cf5Sahrens 
22113b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
22121d452cf5Sahrens 
22133b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
22143b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
22153b2aab18SMatthew Ahrens 
22163b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
22173b2aab18SMatthew Ahrens 
22183b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
22193b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
22203b2aab18SMatthew Ahrens }
22213b2aab18SMatthew Ahrens 
222291948b51SKeith M Wesolowski /*
2223a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
2224a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
2225a7027df1SMatthew Ahrens  * in the result nvlist.
222691948b51SKeith M Wesolowski  *
2227a7027df1SMatthew Ahrens  * If owner != NULL:
222891948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
222991948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
223091948b51SKeith M Wesolowski  *   succeed or not.
223191948b51SKeith M Wesolowski  *
223291948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
223391948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
223491948b51SKeith M Wesolowski  */
22353b2aab18SMatthew Ahrens int
2236a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
22373b2aab18SMatthew Ahrens {
223891948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
223991948b51SKeith M Wesolowski 
224091948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
224191948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
2242a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
224391948b51SKeith M Wesolowski 
22443b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
22457d46dc6cSMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra,
22467d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
2247fa9e4066Sahrens }
224899653d4eSeschrock 
2249088f3894Sahrens struct promotenode {
2250745cd3c5Smaybee 	list_node_t link;
2251745cd3c5Smaybee 	dsl_dataset_t *ds;
2252745cd3c5Smaybee };
2253745cd3c5Smaybee 
22543b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg {
22553b2aab18SMatthew Ahrens 	const char *ddpa_clonename;
22563b2aab18SMatthew Ahrens 	dsl_dataset_t *ddpa_clone;
225774e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
22583b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_origin; /* origin of the origin */
225974e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2260681d9761SEric Taylor 	char *err_ds;
2261a2afb611SJerry Jelinek 	cred_t *cr;
22623b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t;
22631d452cf5Sahrens 
226474e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
22653b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
22663b2aab18SMatthew Ahrens     void *tag);
22673b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
226874e7dc98SMatthew Ahrens 
226999653d4eSeschrock static int
22703b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
227199653d4eSeschrock {
22723b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
22733b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
22743b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
22753b2aab18SMatthew Ahrens 	struct promotenode *snap;
22763b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
2277745cd3c5Smaybee 	int err;
2278cde58dbcSMatthew Ahrens 	uint64_t unused;
2279a2afb611SJerry Jelinek 	uint64_t ss_mv_cnt;
2280cb5842f8SAndriy Gapon 	size_t max_snap_len;
22811d452cf5Sahrens 
22823b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
22833b2aab18SMatthew Ahrens 	if (err != 0)
22843b2aab18SMatthew Ahrens 		return (err);
228599653d4eSeschrock 
22863b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
2287cb5842f8SAndriy Gapon 	max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
22881d452cf5Sahrens 
2289c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
22903b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
2291be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
22923b2aab18SMatthew Ahrens 	}
22933b2aab18SMatthew Ahrens 
22943b2aab18SMatthew Ahrens 	/*
22953b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
22963b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
22973b2aab18SMatthew Ahrens 	 */
22983b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
22993b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
23003b2aab18SMatthew Ahrens 		return (0);
23013b2aab18SMatthew Ahrens 	}
23023b2aab18SMatthew Ahrens 
23033b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
23043b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
230599653d4eSeschrock 
23063cb34c60Sahrens 	/* compute origin's new unique space */
23073b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2308c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2309c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2310cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2311c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
23123b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
231399653d4eSeschrock 
2314745cd3c5Smaybee 	/*
2315745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2316745cd3c5Smaybee 	 *
231774e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
23183b2aab18SMatthew Ahrens 	 * to used by each snapshot:
231974e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
232074e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
232174e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2322745cd3c5Smaybee 	 * So a sequence would look like:
232374e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2324745cd3c5Smaybee 	 * Which simplifies to:
232574e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2326745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
232774e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2328745cd3c5Smaybee 	 */
2329a2afb611SJerry Jelinek 	ss_mv_cnt = 0;
2330c1379625SJustin T. Gibbs 	ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2331c1379625SJustin T. Gibbs 	ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2332c1379625SJustin T. Gibbs 	ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
23333b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
23343b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
233599653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2336745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
233799653d4eSeschrock 
2338a2afb611SJerry Jelinek 		ss_mv_cnt++;
2339a2afb611SJerry Jelinek 
23403b2aab18SMatthew Ahrens 		/*
23413b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
23423b2aab18SMatthew Ahrens 		 * the objset.
23433b2aab18SMatthew Ahrens 		 */
23443b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
2345be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
23463b2aab18SMatthew Ahrens 			goto out;
23473b2aab18SMatthew Ahrens 		}
23483b2aab18SMatthew Ahrens 
234999653d4eSeschrock 		/* Check that the snapshot name does not conflict */
23503b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
2351cb5842f8SAndriy Gapon 		if (strlen(ds->ds_snapname) >= max_snap_len) {
2352cb5842f8SAndriy Gapon 			err = SET_ERROR(ENAMETOOLONG);
2353cb5842f8SAndriy Gapon 			goto out;
2354cb5842f8SAndriy Gapon 		}
2355745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2356681d9761SEric Taylor 		if (err == 0) {
23573b2aab18SMatthew Ahrens 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2358be6fd75aSMatthew Ahrens 			err = SET_ERROR(EEXIST);
2359681d9761SEric Taylor 			goto out;
2360681d9761SEric Taylor 		}
2361745cd3c5Smaybee 		if (err != ENOENT)
2362681d9761SEric Taylor 			goto out;
236399653d4eSeschrock 
2364745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
2365c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
236674e7dc98SMatthew Ahrens 			continue;
236774e7dc98SMatthew Ahrens 
2368cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2369cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
23703b2aab18SMatthew Ahrens 		ddpa->used += dlused;
23713b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
23723b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
237374e7dc98SMatthew Ahrens 	}
2374745cd3c5Smaybee 
2375745cd3c5Smaybee 	/*
2376745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
2377745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
2378745cd3c5Smaybee 	 */
23793b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
2380c1379625SJustin T. Gibbs 		ddpa->used -=
2381c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2382c1379625SJustin T. Gibbs 		ddpa->comp -=
2383c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
23843b2aab18SMatthew Ahrens 		ddpa->uncomp -=
2385c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->
2386c1379625SJustin T. Gibbs 		    ds_uncompressed_bytes;
238799653d4eSeschrock 	}
238899653d4eSeschrock 
2389a2afb611SJerry Jelinek 	/* Check that there is enough space and limit headroom here */
239074e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2391a2afb611SJerry Jelinek 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
23923b2aab18SMatthew Ahrens 	if (err != 0)
23933b2aab18SMatthew Ahrens 		goto out;
239474e7dc98SMatthew Ahrens 
239574e7dc98SMatthew Ahrens 	/*
239674e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
239774e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
239874e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
239974e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
240074e7dc98SMatthew Ahrens 	 */
2401c1379625SJustin T. Gibbs 	if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
240274e7dc98SMatthew Ahrens 		uint64_t space;
240374e7dc98SMatthew Ahrens 
240474e7dc98SMatthew Ahrens 		/*
240574e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
24063f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
2407cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
240874e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
240974e7dc98SMatthew Ahrens 		 * iterate over all bps.
241074e7dc98SMatthew Ahrens 		 */
24113b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
24123b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
24133b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
24143b2aab18SMatthew Ahrens 		if (err != 0)
24153b2aab18SMatthew Ahrens 			goto out;
241674e7dc98SMatthew Ahrens 
24173b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
24183f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
24193b2aab18SMatthew Ahrens 		if (err != 0)
24203b2aab18SMatthew Ahrens 			goto out;
24213b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
242274e7dc98SMatthew Ahrens 	}
2423c1379625SJustin T. Gibbs 	if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2424c1379625SJustin T. Gibbs 	    DD_FLAG_USED_BREAKDOWN) {
24253b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
2426c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_creation_txg,
2427c1379625SJustin T. Gibbs 		    &ddpa->originusedsnap);
24283b2aab18SMatthew Ahrens 		if (err != 0)
24293b2aab18SMatthew Ahrens 			goto out;
2430745cd3c5Smaybee 	}
24311d452cf5Sahrens 
2432681d9761SEric Taylor out:
24333b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2434681d9761SEric Taylor 	return (err);
24351d452cf5Sahrens }
243699653d4eSeschrock 
24371d452cf5Sahrens static void
24383b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
24391d452cf5Sahrens {
24403b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
24413b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
24423b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
24433b2aab18SMatthew Ahrens 	struct promotenode *snap;
24443b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
24453b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_head;
24463b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
24473cb34c60Sahrens 	dsl_dir_t *odd = NULL;
2448088f3894Sahrens 	uint64_t oldnext_obj;
244974e7dc98SMatthew Ahrens 	int64_t delta;
24501d452cf5Sahrens 
24513b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
24523b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
24533b2aab18SMatthew Ahrens 
2454c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
24551d452cf5Sahrens 
24563b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
24573b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
24583b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
24593b2aab18SMatthew Ahrens 
24603b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
246174e7dc98SMatthew Ahrens 	origin_head = snap->ds;
246274e7dc98SMatthew Ahrens 
24630b69c2f0Sahrens 	/*
24643cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
24650b69c2f0Sahrens 	 * changing.
24660b69c2f0Sahrens 	 */
24673b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
24683cb34c60Sahrens 	    NULL, FTAG, &odd));
246999653d4eSeschrock 
2470745cd3c5Smaybee 	/* change origin's next snap */
2471745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2472c1379625SJustin T. Gibbs 	oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
24733b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2474c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2475c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2476c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2477745cd3c5Smaybee 
2478088f3894Sahrens 	/* change the origin's next clone */
2479c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
24803b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
24813b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
24823b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2483c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2484088f3894Sahrens 		    oldnext_obj, tx));
2485088f3894Sahrens 	}
2486088f3894Sahrens 
2487745cd3c5Smaybee 	/* change origin */
2488745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2489c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2490c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
24913f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2492745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2493c1379625SJustin T. Gibbs 	dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
24943f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
2495c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
2496745cd3c5Smaybee 
2497cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2498cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
24993b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2500c1379625SJustin T. Gibbs 		    dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
25013b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2502c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2503cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2504cde58dbcSMatthew Ahrens 
25053b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2506c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2507cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
2508c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_clones == 0) {
2509c1379625SJustin T. Gibbs 			dsl_dir_phys(dd)->dd_clones =
2510c1379625SJustin T. Gibbs 			    zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2511c1379625SJustin T. Gibbs 			    DMU_OT_NONE, 0, tx);
2512cde58dbcSMatthew Ahrens 		}
25133b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2514c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2515cde58dbcSMatthew Ahrens 	}
2516cde58dbcSMatthew Ahrens 
251799653d4eSeschrock 	/* move snapshots to this dir */
25183b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
25193b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2520745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
252199653d4eSeschrock 
25223b2aab18SMatthew Ahrens 		/*
25233b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
25243b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
25253b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
25263b2aab18SMatthew Ahrens 		 */
2527503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2528503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2529503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
25303baa08fcSek 		}
25313b2aab18SMatthew Ahrens 
253299653d4eSeschrock 		/* move snap name entry */
25333b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
25343b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2535a2afb611SJerry Jelinek 		    ds->ds_snapname, tx, B_TRUE));
25363b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
2537c1379625SJustin T. Gibbs 		    dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
253899653d4eSeschrock 		    8, 1, &ds->ds_object, tx));
2539a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2540a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2541cde58dbcSMatthew Ahrens 
254299653d4eSeschrock 		/* change containing dsl_dir */
254399653d4eSeschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2544c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
2545c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
25463cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
25473b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
25483b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
254999653d4eSeschrock 		    NULL, ds, &ds->ds_dir));
255099653d4eSeschrock 
2551cde58dbcSMatthew Ahrens 		/* move any clone references */
2552c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
2553cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2554cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2555cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2556cde58dbcSMatthew Ahrens 
25573b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
2558c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
25593b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
25603b2aab18SMatthew Ahrens 			    zap_cursor_advance(&zc)) {
25613b2aab18SMatthew Ahrens 				dsl_dataset_t *cnds;
25623b2aab18SMatthew Ahrens 				uint64_t o;
2563a9799022Sck 
25643b2aab18SMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
25653b2aab18SMatthew Ahrens 					/*
25663b2aab18SMatthew Ahrens 					 * We've already moved the
25673b2aab18SMatthew Ahrens 					 * origin's reference.
25683b2aab18SMatthew Ahrens 					 */
25693b2aab18SMatthew Ahrens 					continue;
25703b2aab18SMatthew Ahrens 				}
2571a9799022Sck 
25723b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
25733b2aab18SMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
2574c1379625SJustin T. Gibbs 				o = dsl_dir_phys(cnds->ds_dir)->
2575c1379625SJustin T. Gibbs 				    dd_head_dataset_obj;
2576a9799022Sck 
25773b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
2578c1379625SJustin T. Gibbs 				    dsl_dir_phys(odd)->dd_clones, o, tx));
25793b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
2580c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd)->dd_clones, o, tx));
25813b2aab18SMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
25823b2aab18SMatthew Ahrens 			}
25833b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
25843b2aab18SMatthew Ahrens 		}
25859082849eSck 
25863b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
2587a9799022Sck 	}
2588a9799022Sck 
2589a9799022Sck 	/*
25903b2aab18SMatthew Ahrens 	 * Change space accounting.
25913b2aab18SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
25923b2aab18SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
25933b2aab18SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
2594a9799022Sck 	 */
2595a9799022Sck 
25963b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
2597c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
25983b2aab18SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
25993b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
26003b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
26013b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
26023b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
26033b2aab18SMatthew Ahrens 
26043b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
2605c1379625SJustin T. Gibbs 	    dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
26063b2aab18SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
26073b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
26083b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
26093b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
26103b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
26113b2aab18SMatthew Ahrens 
2612c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
26133b2aab18SMatthew Ahrens 
26143b2aab18SMatthew Ahrens 	/* log history record */
26153b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
26163b2aab18SMatthew Ahrens 
26173b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
26183b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2619a9799022Sck }
2620a9799022Sck 
26213b2aab18SMatthew Ahrens /*
26223b2aab18SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
26233b2aab18SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
26243b2aab18SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
26253b2aab18SMatthew Ahrens  * snapshots back to this dataset's origin.
26263b2aab18SMatthew Ahrens  */
2627a9799022Sck static int
26283b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
26293b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2630a9799022Sck {
26313b2aab18SMatthew Ahrens 	uint64_t obj = last_obj;
2632a9799022Sck 
26333b2aab18SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
26343b2aab18SMatthew Ahrens 	    offsetof(struct promotenode, link));
2635a9799022Sck 
26363b2aab18SMatthew Ahrens 	while (obj != first_obj) {
26373b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
26383b2aab18SMatthew Ahrens 		struct promotenode *snap;
26393b2aab18SMatthew Ahrens 		int err;
264092241e0bSTom Erickson 
26413b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
26423b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
26433b2aab18SMatthew Ahrens 		if (err != 0)
26443b2aab18SMatthew Ahrens 			return (err);
2645a9799022Sck 
26463b2aab18SMatthew Ahrens 		if (first_obj == 0)
2647c1379625SJustin T. Gibbs 			first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
26483b2aab18SMatthew Ahrens 
26493b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
26503b2aab18SMatthew Ahrens 		snap->ds = ds;
26513b2aab18SMatthew Ahrens 		list_insert_tail(l, snap);
2652c1379625SJustin T. Gibbs 		obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
26533b2aab18SMatthew Ahrens 	}
2654a9799022Sck 
2655a9799022Sck 	return (0);
2656a9799022Sck }
2657a9799022Sck 
26583b2aab18SMatthew Ahrens static int
26593b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2660a9799022Sck {
26613b2aab18SMatthew Ahrens 	struct promotenode *snap;
2662a9799022Sck 
26633b2aab18SMatthew Ahrens 	*spacep = 0;
26643b2aab18SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
26653b2aab18SMatthew Ahrens 		uint64_t used, comp, uncomp;
26663b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
26673b2aab18SMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
26683b2aab18SMatthew Ahrens 		*spacep += used;
266992241e0bSTom Erickson 	}
26703b2aab18SMatthew Ahrens 	return (0);
2671a9799022Sck }
2672a9799022Sck 
26733b2aab18SMatthew Ahrens static void
26743b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
2675a9799022Sck {
26763b2aab18SMatthew Ahrens 	struct promotenode *snap;
267792241e0bSTom Erickson 
26783b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
26793b2aab18SMatthew Ahrens 		return;
2680a9799022Sck 
26813b2aab18SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
26823b2aab18SMatthew Ahrens 		list_remove(l, snap);
26833b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
26843b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
26853b2aab18SMatthew Ahrens 	}
26863b2aab18SMatthew Ahrens 	list_destroy(l);
2687a9799022Sck }
2688a9799022Sck 
2689a9799022Sck static int
26903b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2691a9799022Sck {
26923b2aab18SMatthew Ahrens 	int error;
26933b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
26943b2aab18SMatthew Ahrens 	struct promotenode *snap;
2695a9799022Sck 
26963b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
26973b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
26983b2aab18SMatthew Ahrens 	if (error != 0)
26993b2aab18SMatthew Ahrens 		return (error);
27003b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
2701a9799022Sck 
2702bc9014e6SJustin Gibbs 	if (ddpa->ddpa_clone->ds_is_snapshot ||
27033b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
27043b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2705be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
27063b2aab18SMatthew Ahrens 	}
2707a9799022Sck 
2708c1379625SJustin T. Gibbs 	error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
27093b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
27103b2aab18SMatthew Ahrens 	if (error != 0)
27113b2aab18SMatthew Ahrens 		goto out;
2712a9799022Sck 
27133b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
27143b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
27153b2aab18SMatthew Ahrens 	if (error != 0)
27163b2aab18SMatthew Ahrens 		goto out;
2717a9799022Sck 
27183b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
2719c1379625SJustin T. Gibbs 	ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
2720c1379625SJustin T. Gibbs 	error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
2721c1379625SJustin T. Gibbs 	    dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
27223b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
27233b2aab18SMatthew Ahrens 	if (error != 0)
27243b2aab18SMatthew Ahrens 		goto out;
2725379c004dSEric Schrock 
2726c1379625SJustin T. Gibbs 	if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
27273b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
2728c1379625SJustin T. Gibbs 		    dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
27293b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
27303b2aab18SMatthew Ahrens 		if (error != 0)
27313b2aab18SMatthew Ahrens 			goto out;
2732379c004dSEric Schrock 	}
27333b2aab18SMatthew Ahrens out:
27343b2aab18SMatthew Ahrens 	if (error != 0)
27353b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
27363b2aab18SMatthew Ahrens 	return (error);
2737a9799022Sck }
2738a9799022Sck 
2739a9799022Sck static void
27403b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2741a9799022Sck {
27423b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
27433b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
27443b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
27453b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
27463b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
27473b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
27483b2aab18SMatthew Ahrens }
274902c8f3f0SMatthew Ahrens 
27503b2aab18SMatthew Ahrens /*
27513b2aab18SMatthew Ahrens  * Promote a clone.
27523b2aab18SMatthew Ahrens  *
27533b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
27543b2aab18SMatthew Ahrens  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
27553b2aab18SMatthew Ahrens  */
27563b2aab18SMatthew Ahrens int
27573b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap)
27583b2aab18SMatthew Ahrens {
27593b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
27603b2aab18SMatthew Ahrens 	uint64_t numsnaps;
27613b2aab18SMatthew Ahrens 	int error;
27623b2aab18SMatthew Ahrens 	objset_t *os;
276392241e0bSTom Erickson 
27643b2aab18SMatthew Ahrens 	/*
27653b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
27663b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
27673b2aab18SMatthew Ahrens 	 */
27683b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
27693b2aab18SMatthew Ahrens 	if (error != 0)
27703b2aab18SMatthew Ahrens 		return (error);
27713b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2772c1379625SJustin T. Gibbs 	    dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2773c1379625SJustin T. Gibbs 	    &numsnaps);
27743b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
27753b2aab18SMatthew Ahrens 	if (error != 0)
27763b2aab18SMatthew Ahrens 		return (error);
277702c8f3f0SMatthew Ahrens 
27783b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
27793b2aab18SMatthew Ahrens 	ddpa.err_ds = conflsnap;
2780a2afb611SJerry Jelinek 	ddpa.cr = CRED();
278102c8f3f0SMatthew Ahrens 
27823b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_dataset_promote_check,
27837d46dc6cSMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa,
27847d46dc6cSMatthew Ahrens 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2785a9799022Sck }
2786a9799022Sck 
2787a9799022Sck int
27883b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
278991948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2790a9799022Sck {
27913b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2792a9799022Sck 
27933b2aab18SMatthew Ahrens 	/* they should both be heads */
2794bc9014e6SJustin Gibbs 	if (clone->ds_is_snapshot ||
2795bc9014e6SJustin Gibbs 	    origin_head->ds_is_snapshot)
2796be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
279792241e0bSTom Erickson 
279834f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
279934f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
2800be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2801a9799022Sck 
28023b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
28033b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
28043b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
280534f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2806be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
280792241e0bSTom Erickson 
28083b2aab18SMatthew Ahrens 	/* the clone should be a child of the origin */
28093b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2810be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2811842727c2SChris Kirby 
28123b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
281334f2f8cfSMatthew Ahrens 	if (!force &&
281434f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2815be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
2816c99e4bdcSChris Kirby 
28173b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
281891948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2819be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
28203b2aab18SMatthew Ahrens 
28213b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
28223b2aab18SMatthew Ahrens 	unused_refres_delta =
28233b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2824c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
28253b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2826c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
28273b2aab18SMatthew Ahrens 
28283b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
28293b2aab18SMatthew Ahrens 	    unused_refres_delta >
28303b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2831be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
28323b2aab18SMatthew Ahrens 
28333b2aab18SMatthew Ahrens 	/* clone can't be over the head's refquota */
28343b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
2835c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes >
2836c1379625SJustin T. Gibbs 	    origin_head->ds_quota)
2837be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2838c99e4bdcSChris Kirby 
28393b2aab18SMatthew Ahrens 	return (0);
2840c99e4bdcSChris Kirby }
2841c99e4bdcSChris Kirby 
2842a7f53a56SChris Kirby void
28433b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
28443b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2845a7f53a56SChris Kirby {
28463b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
28473b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2848a7f53a56SChris Kirby 
28493b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
28503b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
2851c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota);
285234f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2853842727c2SChris Kirby 
2854ca0cc391SMatthew Ahrens 	/*
2855ca0cc391SMatthew Ahrens 	 * Swap per-dataset feature flags.
2856ca0cc391SMatthew Ahrens 	 */
2857ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2858ca0cc391SMatthew Ahrens 		if (!(spa_feature_table[f].fi_flags &
2859ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET)) {
2860ca0cc391SMatthew Ahrens 			ASSERT(!clone->ds_feature_inuse[f]);
2861ca0cc391SMatthew Ahrens 			ASSERT(!origin_head->ds_feature_inuse[f]);
2862ca0cc391SMatthew Ahrens 			continue;
2863ca0cc391SMatthew Ahrens 		}
2864ca0cc391SMatthew Ahrens 
2865ca0cc391SMatthew Ahrens 		boolean_t clone_inuse = clone->ds_feature_inuse[f];
2866ca0cc391SMatthew Ahrens 		boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
2867ca0cc391SMatthew Ahrens 
2868ca0cc391SMatthew Ahrens 		if (clone_inuse) {
2869ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
2870ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_FALSE;
2871ca0cc391SMatthew Ahrens 		}
2872ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
2873ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(origin_head->ds_object,
2874ca0cc391SMatthew Ahrens 			    f, tx);
2875ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_FALSE;
2876ca0cc391SMatthew Ahrens 		}
2877ca0cc391SMatthew Ahrens 		if (clone_inuse) {
2878ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(origin_head->ds_object,
2879ca0cc391SMatthew Ahrens 			    f, tx);
2880ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_TRUE;
2881ca0cc391SMatthew Ahrens 		}
2882ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
2883ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(clone->ds_object, f, tx);
2884ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_TRUE;
2885ca0cc391SMatthew Ahrens 		}
2886ca0cc391SMatthew Ahrens 	}
2887ca0cc391SMatthew Ahrens 
28883b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
28893b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2890842727c2SChris Kirby 
28913b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
28923b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
28933b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
28943b2aab18SMatthew Ahrens 	}
2895842727c2SChris Kirby 
28963b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
28973b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
28983b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
2899842727c2SChris Kirby 	}
2900842727c2SChris Kirby 
29013b2aab18SMatthew Ahrens 	unused_refres_delta =
29023b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2903c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
29043b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2905c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
29063b2aab18SMatthew Ahrens 
29073b2aab18SMatthew Ahrens 	/*
29083b2aab18SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
29093b2aab18SMatthew Ahrens 	 */
29103b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
29113b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
29123b2aab18SMatthew Ahrens 		uint64_t comp, uncomp;
29133b2aab18SMatthew Ahrens 
29143b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
29153b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
2916c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
2917c1379625SJustin T. Gibbs 		    &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
29183b2aab18SMatthew Ahrens 	}
29193b2aab18SMatthew Ahrens 
29203b2aab18SMatthew Ahrens 	/* swap blkptrs */
29213b2aab18SMatthew Ahrens 	{
29223b2aab18SMatthew Ahrens 		blkptr_t tmp;
2923c1379625SJustin T. Gibbs 		tmp = dsl_dataset_phys(origin_head)->ds_bp;
2924c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_bp =
2925c1379625SJustin T. Gibbs 		    dsl_dataset_phys(clone)->ds_bp;
2926c1379625SJustin T. Gibbs 		dsl_dataset_phys(clone)->ds_bp = tmp;
29273b2aab18SMatthew Ahrens 	}
29283b2aab18SMatthew Ahrens 
29293b2aab18SMatthew Ahrens 	/* set dd_*_bytes */
29303b2aab18SMatthew Ahrens 	{
29313b2aab18SMatthew Ahrens 		int64_t dused, dcomp, duncomp;
29323b2aab18SMatthew Ahrens 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
29333b2aab18SMatthew Ahrens 		uint64_t odl_used, odl_comp, odl_uncomp;
29343b2aab18SMatthew Ahrens 
2935c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dir_phys(clone->ds_dir)->
29363b2aab18SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
29373b2aab18SMatthew Ahrens 
29383b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
29393b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
29403b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
29413b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
294215508ac0SChris Kirby 
2943c1379625SJustin T. Gibbs 		dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
2944c1379625SJustin T. Gibbs 		    cdl_used -
2945c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
2946c1379625SJustin T. Gibbs 		    odl_used);
2947c1379625SJustin T. Gibbs 		dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
2948c1379625SJustin T. Gibbs 		    cdl_comp -
2949c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
2950c1379625SJustin T. Gibbs 		    odl_comp);
2951c1379625SJustin T. Gibbs 		duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
29523b2aab18SMatthew Ahrens 		    cdl_uncomp -
2953c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
2954c1379625SJustin T. Gibbs 		    odl_uncomp);
2955842727c2SChris Kirby 
29563b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
29573b2aab18SMatthew Ahrens 		    dused, dcomp, duncomp, tx);
29583b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
29593b2aab18SMatthew Ahrens 		    -dused, -dcomp, -duncomp, tx);
2960842727c2SChris Kirby 
2961842727c2SChris Kirby 		/*
29623b2aab18SMatthew Ahrens 		 * The difference in the space used by snapshots is the
29633b2aab18SMatthew Ahrens 		 * difference in snapshot space due to the head's
29643b2aab18SMatthew Ahrens 		 * deadlist (since that's the only thing that's
29653b2aab18SMatthew Ahrens 		 * changing that affects the snapused).
2966842727c2SChris Kirby 		 */
29673b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
29683b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
29693b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
29703b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
29713b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
29723b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
29733b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
29743b2aab18SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
2975842727c2SChris Kirby 	}
2976842727c2SChris Kirby 
29773b2aab18SMatthew Ahrens 	/* swap ds_*_bytes */
2978c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
2979c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes);
2980c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
2981c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_compressed_bytes);
2982c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
2983c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_uncompressed_bytes);
2984c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
2985c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
2986842727c2SChris Kirby 
29873b2aab18SMatthew Ahrens 	/* apply any parent delta for change in unconsumed refreservation */
29883b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
29893b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
2990ca45db41SChris Kirby 
29913b2aab18SMatthew Ahrens 	/*
29923b2aab18SMatthew Ahrens 	 * Swap deadlists.
29933b2aab18SMatthew Ahrens 	 */
29943b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
29953b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
2996c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
2997c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
29983b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2999c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
30003b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
3001c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_deadlist_obj);
3002842727c2SChris Kirby 
30033b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
3004842727c2SChris Kirby 
30053b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
30063b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
3007842727c2SChris Kirby }
3008842727c2SChris Kirby 
30093b2aab18SMatthew Ahrens /*
30103b2aab18SMatthew Ahrens  * Given a pool name and a dataset object number in that pool,
30113b2aab18SMatthew Ahrens  * return the name of that dataset.
30123b2aab18SMatthew Ahrens  */
3013a7f53a56SChris Kirby int
30143b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3015a7f53a56SChris Kirby {
30163b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
30173b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
3018a7f53a56SChris Kirby 	int error;
3019a7f53a56SChris Kirby 
30203b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
30213b2aab18SMatthew Ahrens 	if (error != 0)
30223b2aab18SMatthew Ahrens 		return (error);
30233b2aab18SMatthew Ahrens 
30243b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
30253b2aab18SMatthew Ahrens 	if (error == 0) {
30263b2aab18SMatthew Ahrens 		dsl_dataset_name(ds, buf);
30273b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
30283b2aab18SMatthew Ahrens 	}
30293b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3030a7f53a56SChris Kirby 
3031a7f53a56SChris Kirby 	return (error);
3032a7f53a56SChris Kirby }
3033a7f53a56SChris Kirby 
3034842727c2SChris Kirby int
30353b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
30363b2aab18SMatthew Ahrens     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3037842727c2SChris Kirby {
30383b2aab18SMatthew Ahrens 	int error = 0;
3039842727c2SChris Kirby 
30403b2aab18SMatthew Ahrens 	ASSERT3S(asize, >, 0);
3041842727c2SChris Kirby 
30423b2aab18SMatthew Ahrens 	/*
30433b2aab18SMatthew Ahrens 	 * *ref_rsrv is the portion of asize that will come from any
30443b2aab18SMatthew Ahrens 	 * unconsumed refreservation space.
30453b2aab18SMatthew Ahrens 	 */
30463b2aab18SMatthew Ahrens 	*ref_rsrv = 0;
3047842727c2SChris Kirby 
30483b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
30493b2aab18SMatthew Ahrens 	/*
30503b2aab18SMatthew Ahrens 	 * Make a space adjustment for reserved bytes.
30513b2aab18SMatthew Ahrens 	 */
3052c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
30533b2aab18SMatthew Ahrens 		ASSERT3U(*used, >=,
3054c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3055c1379625SJustin T. Gibbs 		*used -=
3056c1379625SJustin T. Gibbs 		    (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
30573b2aab18SMatthew Ahrens 		*ref_rsrv =
30583b2aab18SMatthew Ahrens 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
3059842727c2SChris Kirby 	}
3060842727c2SChris Kirby 
30613b2aab18SMatthew Ahrens 	if (!check_quota || ds->ds_quota == 0) {
30623b2aab18SMatthew Ahrens 		mutex_exit(&ds->ds_lock);
30633b2aab18SMatthew Ahrens 		return (0);
3064842727c2SChris Kirby 	}
30653b2aab18SMatthew Ahrens 	/*
30663b2aab18SMatthew Ahrens 	 * If they are requesting more space, and our current estimate
30673b2aab18SMatthew Ahrens 	 * is over quota, they get to try again unless the actual
30683b2aab18SMatthew Ahrens 	 * on-disk is over quota and there are no pending changes (which
30693b2aab18SMatthew Ahrens 	 * may free up space for us).
30703b2aab18SMatthew Ahrens 	 */
3071c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
3072c1379625SJustin T. Gibbs 	    ds->ds_quota) {
30733b2aab18SMatthew Ahrens 		if (inflight > 0 ||
3074c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
3075be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
30763b2aab18SMatthew Ahrens 		else
3077be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
3078842727c2SChris Kirby 	}
30793b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3080842727c2SChris Kirby 
3081842727c2SChris Kirby 	return (error);
3082842727c2SChris Kirby }
3083842727c2SChris Kirby 
30843b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
30853b2aab18SMatthew Ahrens 	const char *ddsqra_name;
30863b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
30873b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
30883b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
3089842727c2SChris Kirby 
30903b2aab18SMatthew Ahrens 
30913b2aab18SMatthew Ahrens /* ARGSUSED */
3092842727c2SChris Kirby static int
30933b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
3094842727c2SChris Kirby {
30953b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
30963b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
30973b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
3098842727c2SChris Kirby 	int error;
30993b2aab18SMatthew Ahrens 	uint64_t newval;
3100842727c2SChris Kirby 
31013b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
3102be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3103842727c2SChris Kirby 
31043b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
31053b2aab18SMatthew Ahrens 	if (error != 0)
31063b2aab18SMatthew Ahrens 		return (error);
31073b2aab18SMatthew Ahrens 
3108bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
31093b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3110be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3111842727c2SChris Kirby 	}
3112842727c2SChris Kirby 
31133b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
31143b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
31153b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
31163b2aab18SMatthew Ahrens 	if (error != 0) {
31173b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3118842727c2SChris Kirby 		return (error);
3119842727c2SChris Kirby 	}
3120842727c2SChris Kirby 
31213b2aab18SMatthew Ahrens 	if (newval == 0) {
31223b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
31233b2aab18SMatthew Ahrens 		return (0);
31243b2aab18SMatthew Ahrens 	}
3125842727c2SChris Kirby 
3126c1379625SJustin T. Gibbs 	if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
31273b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
31283b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3129be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
31303b2aab18SMatthew Ahrens 	}
31313b2aab18SMatthew Ahrens 
31323b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3133842727c2SChris Kirby 	return (0);
3134842727c2SChris Kirby }
3135842727c2SChris Kirby 
31363b2aab18SMatthew Ahrens static void
31373b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3138842727c2SChris Kirby {
31393b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
31403b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
31413b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
31423b2aab18SMatthew Ahrens 	uint64_t newval;
3143842727c2SChris Kirby 
31443b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3145842727c2SChris Kirby 
31463b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
31473b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
31483b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
31493b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
3150842727c2SChris Kirby 
31513b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
31523b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
3153842727c2SChris Kirby 
31543b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
31553b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
31563b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
3157842727c2SChris Kirby 	}
31583b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3159842727c2SChris Kirby }
3160842727c2SChris Kirby 
31613b2aab18SMatthew Ahrens int
31623b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
31633b2aab18SMatthew Ahrens     uint64_t refquota)
3164842727c2SChris Kirby {
31653b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3166842727c2SChris Kirby 
31673b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
31683b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
31693b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
31703b2aab18SMatthew Ahrens 
31713b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
31727d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
3173842727c2SChris Kirby }
3174842727c2SChris Kirby 
3175842727c2SChris Kirby static int
31763b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3177842727c2SChris Kirby {
31783b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
31793b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3180842727c2SChris Kirby 	dsl_dataset_t *ds;
3181842727c2SChris Kirby 	int error;
31823b2aab18SMatthew Ahrens 	uint64_t newval, unique;
3183d7747cbcSChris Kirby 
31843b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3185be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3186842727c2SChris Kirby 
31873b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
31883b2aab18SMatthew Ahrens 	if (error != 0)
3189842727c2SChris Kirby 		return (error);
3190842727c2SChris Kirby 
3191bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
31923b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3193be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3194842727c2SChris Kirby 	}
3195842727c2SChris Kirby 
31963b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
31973b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
31983b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
31993b2aab18SMatthew Ahrens 	if (error != 0) {
32003b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3201842727c2SChris Kirby 		return (error);
3202842727c2SChris Kirby 	}
3203842727c2SChris Kirby 
32043b2aab18SMatthew Ahrens 	/*
32053b2aab18SMatthew Ahrens 	 * If we are doing the preliminary check in open context, the
32063b2aab18SMatthew Ahrens 	 * space estimates may be inaccurate.
32073b2aab18SMatthew Ahrens 	 */
32083b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
32093b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
32103b2aab18SMatthew Ahrens 		return (0);
3211842727c2SChris Kirby 	}
3212842727c2SChris Kirby 
32133b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
32143b2aab18SMatthew Ahrens 	if (!DS_UNIQUE_IS_ACCURATE(ds))
32153b2aab18SMatthew Ahrens 		dsl_dataset_recalc_head_uniq(ds);
3216c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
32173b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3218842727c2SChris Kirby 
32193b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
32203b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
32213b2aab18SMatthew Ahrens 		    MAX(unique, ds->ds_reserved);
3222842727c2SChris Kirby 
32233b2aab18SMatthew Ahrens 		if (delta >
32243b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
32253b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
32263b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
3227be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
32283b2aab18SMatthew Ahrens 		}
3229842727c2SChris Kirby 	}
3230842727c2SChris Kirby 
32313b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
32323b2aab18SMatthew Ahrens 	return (0);
3233842727c2SChris Kirby }
3234842727c2SChris Kirby 
32353b2aab18SMatthew Ahrens void
32363b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
32373b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3238ca45db41SChris Kirby {
32393b2aab18SMatthew Ahrens 	uint64_t newval;
32403b2aab18SMatthew Ahrens 	uint64_t unique;
32413b2aab18SMatthew Ahrens 	int64_t delta;
3242ca45db41SChris Kirby 
32433b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
32443b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
3245ca45db41SChris Kirby 
32463b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
32473b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
3248a7f53a56SChris Kirby 
32493b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
32503b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
32513b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
32523b2aab18SMatthew Ahrens 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3253c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
32543b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
32553b2aab18SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
32563b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
32573b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3258a7f53a56SChris Kirby 
32593b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
32603b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
3261ca45db41SChris Kirby }
3262ca45db41SChris Kirby 
32633b2aab18SMatthew Ahrens static void
32643b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3265842727c2SChris Kirby {
32663b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
32673b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3268842727c2SChris Kirby 	dsl_dataset_t *ds;
3269842727c2SChris Kirby 
32703b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
32713b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
32723b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3273842727c2SChris Kirby 	dsl_dataset_rele(ds, FTAG);
3274842727c2SChris Kirby }
3275503ad85cSMatthew Ahrens 
3276503ad85cSMatthew Ahrens int
32773b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
32783b2aab18SMatthew Ahrens     uint64_t refreservation)
3279503ad85cSMatthew Ahrens {
32803b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3281503ad85cSMatthew Ahrens 
32823b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
32833b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
32843b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
32853b2aab18SMatthew Ahrens 
32863b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
32877d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refreservation_sync, &ddsqra,
32887d46dc6cSMatthew Ahrens 	    0, ZFS_SPACE_CHECK_NONE));
3289503ad85cSMatthew Ahrens }
329019b94df9SMatthew Ahrens 
329119b94df9SMatthew Ahrens /*
329219b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
329319b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
329419b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
329519b94df9SMatthew Ahrens  * fail and return EINVAL.
329619b94df9SMatthew Ahrens  *
329719b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
329819b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
329919b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
330019b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
330119b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
330219b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
330319b94df9SMatthew Ahrens  *
330419b94df9SMatthew Ahrens  * space freed                         [---------------------]
330519b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
330619b94df9SMatthew Ahrens  *                                         oldsnap            new
330719b94df9SMatthew Ahrens  */
330819b94df9SMatthew Ahrens int
330919b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
331019b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
331119b94df9SMatthew Ahrens {
331219b94df9SMatthew Ahrens 	int err = 0;
331319b94df9SMatthew Ahrens 	uint64_t snapobj;
331419b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
331519b94df9SMatthew Ahrens 
33163b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
33173b2aab18SMatthew Ahrens 
331819b94df9SMatthew Ahrens 	*usedp = 0;
3319c1379625SJustin T. Gibbs 	*usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3320c1379625SJustin T. Gibbs 	*usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
332119b94df9SMatthew Ahrens 
332219b94df9SMatthew Ahrens 	*compp = 0;
3323c1379625SJustin T. Gibbs 	*compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3324c1379625SJustin T. Gibbs 	*compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
332519b94df9SMatthew Ahrens 
332619b94df9SMatthew Ahrens 	*uncompp = 0;
3327c1379625SJustin T. Gibbs 	*uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3328c1379625SJustin T. Gibbs 	*uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
332919b94df9SMatthew Ahrens 
333019b94df9SMatthew Ahrens 	snapobj = new->ds_object;
333119b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
333219b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
333319b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
333419b94df9SMatthew Ahrens 
3335ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
3336ad135b5dSChristopher Siden 			snap = new;
3337ad135b5dSChristopher Siden 		} else {
3338ad135b5dSChristopher Siden 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3339ad135b5dSChristopher Siden 			if (err != 0)
3340ad135b5dSChristopher Siden 				break;
3341ad135b5dSChristopher Siden 		}
334219b94df9SMatthew Ahrens 
3343c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3344c1379625SJustin T. Gibbs 		    dsl_dataset_phys(oldsnap)->ds_creation_txg) {
334519b94df9SMatthew Ahrens 			/*
334619b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
334719b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
334819b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
334919b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
335019b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
335119b94df9SMatthew Ahrens 			 * optimization itself.
335219b94df9SMatthew Ahrens 			 */
335319b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
335419b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
335519b94df9SMatthew Ahrens 		} else {
335619b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
3357c1379625SJustin T. Gibbs 			    0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
335819b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
335919b94df9SMatthew Ahrens 		}
336019b94df9SMatthew Ahrens 		*usedp += used;
336119b94df9SMatthew Ahrens 		*compp += comp;
336219b94df9SMatthew Ahrens 		*uncompp += uncomp;
336319b94df9SMatthew Ahrens 
336419b94df9SMatthew Ahrens 		/*
336519b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
336619b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
336719b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
336819b94df9SMatthew Ahrens 		 */
3369c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3370ad135b5dSChristopher Siden 		if (snap != new)
3371ad135b5dSChristopher Siden 			dsl_dataset_rele(snap, FTAG);
337219b94df9SMatthew Ahrens 		if (snapobj == 0) {
3373be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
337419b94df9SMatthew Ahrens 			break;
337519b94df9SMatthew Ahrens 		}
337619b94df9SMatthew Ahrens 
337719b94df9SMatthew Ahrens 	}
337819b94df9SMatthew Ahrens 	return (err);
337919b94df9SMatthew Ahrens }
338019b94df9SMatthew Ahrens 
338119b94df9SMatthew Ahrens /*
338219b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
338319b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
338419b94df9SMatthew Ahrens  *
338519b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
338619b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
338719b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
338819b94df9SMatthew Ahrens  *
338919b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
339019b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
339119b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
339219b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
339319b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
339419b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
339519b94df9SMatthew Ahrens  */
339619b94df9SMatthew Ahrens int
339719b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
339819b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
339919b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
340019b94df9SMatthew Ahrens {
340119b94df9SMatthew Ahrens 	int err = 0;
340219b94df9SMatthew Ahrens 	uint64_t snapobj;
340319b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
340419b94df9SMatthew Ahrens 
3405bc9014e6SJustin Gibbs 	ASSERT(firstsnap->ds_is_snapshot);
3406bc9014e6SJustin Gibbs 	ASSERT(lastsnap->ds_is_snapshot);
340719b94df9SMatthew Ahrens 
340819b94df9SMatthew Ahrens 	/*
340919b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
341019b94df9SMatthew Ahrens 	 * is before lastsnap.
341119b94df9SMatthew Ahrens 	 */
341219b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
3413c1379625SJustin T. Gibbs 	    dsl_dataset_phys(firstsnap)->ds_creation_txg >
3414c1379625SJustin T. Gibbs 	    dsl_dataset_phys(lastsnap)->ds_creation_txg)
3415be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
341619b94df9SMatthew Ahrens 
341719b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
341819b94df9SMatthew Ahrens 
3419c1379625SJustin T. Gibbs 	snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
342019b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
342119b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
342219b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
342319b94df9SMatthew Ahrens 
342419b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
342519b94df9SMatthew Ahrens 		if (err != 0)
342619b94df9SMatthew Ahrens 			break;
342719b94df9SMatthew Ahrens 
342819b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
3429c1379625SJustin T. Gibbs 		    dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
343019b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
343119b94df9SMatthew Ahrens 		*usedp += used;
343219b94df9SMatthew Ahrens 		*compp += comp;
343319b94df9SMatthew Ahrens 		*uncompp += uncomp;
343419b94df9SMatthew Ahrens 
3435c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
343619b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
343719b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
343819b94df9SMatthew Ahrens 	}
343919b94df9SMatthew Ahrens 	return (err);
344019b94df9SMatthew Ahrens }
34413b2aab18SMatthew Ahrens 
34423b2aab18SMatthew Ahrens /*
34433b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
34443b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
34453b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
34463b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
34473b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
344878f17100SMatthew Ahrens  *
344978f17100SMatthew Ahrens  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
34503b2aab18SMatthew Ahrens  */
34513b2aab18SMatthew Ahrens boolean_t
345278f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
34539a686fbcSPaul Dagnelie     uint64_t earlier_txg)
34543b2aab18SMatthew Ahrens {
34553b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
34563b2aab18SMatthew Ahrens 	int error;
34573b2aab18SMatthew Ahrens 	boolean_t ret;
34583b2aab18SMatthew Ahrens 
34593b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
3460bc9014e6SJustin Gibbs 	ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
346178f17100SMatthew Ahrens 
346278f17100SMatthew Ahrens 	if (earlier_txg == 0)
3463c1379625SJustin T. Gibbs 		earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
34643b2aab18SMatthew Ahrens 
3465bc9014e6SJustin Gibbs 	if (later->ds_is_snapshot &&
3466c1379625SJustin T. Gibbs 	    earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
34673b2aab18SMatthew Ahrens 		return (B_FALSE);
34683b2aab18SMatthew Ahrens 
34693b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
34703b2aab18SMatthew Ahrens 		return (B_TRUE);
34713b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
34723b2aab18SMatthew Ahrens 		return (B_FALSE);
34733b2aab18SMatthew Ahrens 
3474c1379625SJustin T. Gibbs 	if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
34753b2aab18SMatthew Ahrens 		return (B_TRUE);
34763b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
34773b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
3478c1379625SJustin T. Gibbs 	    dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
34793b2aab18SMatthew Ahrens 	if (error != 0)
34803b2aab18SMatthew Ahrens 		return (B_FALSE);
348178f17100SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
34823b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
34833b2aab18SMatthew Ahrens 	return (ret);
34843b2aab18SMatthew Ahrens }
34852acef22dSMatthew Ahrens 
34862acef22dSMatthew Ahrens void
34872acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
34882acef22dSMatthew Ahrens {
34892acef22dSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
34902acef22dSMatthew Ahrens 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
34912acef22dSMatthew Ahrens }
34929c3fd121SMatthew Ahrens 
34939c3fd121SMatthew Ahrens boolean_t
34949c3fd121SMatthew Ahrens dsl_dataset_is_zapified(dsl_dataset_t *ds)
34959c3fd121SMatthew Ahrens {
34969c3fd121SMatthew Ahrens 	dmu_object_info_t doi;
34979c3fd121SMatthew Ahrens 
34989c3fd121SMatthew Ahrens 	dmu_object_info_from_db(ds->ds_dbuf, &doi);
34999c3fd121SMatthew Ahrens 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
35009c3fd121SMatthew Ahrens }
35019c3fd121SMatthew Ahrens 
35029c3fd121SMatthew Ahrens boolean_t
35039c3fd121SMatthew Ahrens dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
35049c3fd121SMatthew Ahrens {
35059c3fd121SMatthew Ahrens 	return (dsl_dataset_is_zapified(ds) &&
35069c3fd121SMatthew Ahrens 	    zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
35079c3fd121SMatthew Ahrens 	    ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
35089c3fd121SMatthew Ahrens }
3509