xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
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.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/dmu_objset.h>
30fa9e4066Sahrens #include <sys/dsl_dataset.h>
31fa9e4066Sahrens #include <sys/dsl_dir.h>
3299653d4eSeschrock #include <sys/dsl_prop.h>
331d452cf5Sahrens #include <sys/dsl_synctask.h>
34fa9e4066Sahrens #include <sys/dmu_traverse.h>
354e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
36fa9e4066Sahrens #include <sys/dmu_tx.h>
37fa9e4066Sahrens #include <sys/arc.h>
38fa9e4066Sahrens #include <sys/zio.h>
39fa9e4066Sahrens #include <sys/zap.h>
40ad135b5dSChristopher Siden #include <sys/zfeature.h>
41fa9e4066Sahrens #include <sys/unique.h>
42fa9e4066Sahrens #include <sys/zfs_context.h>
43cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
44ecd6cf80Smarks #include <sys/spa.h>
45088f3894Sahrens #include <sys/zfs_znode.h>
46c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
47842727c2SChris Kirby #include <sys/zvol.h>
483f9d6ad7SLin Ling #include <sys/dsl_scan.h>
49cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
503b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
513b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
5278f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
53*45818ee1SMatthew Ahrens #include <sys/dmu_send.h>
54*45818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
55e1930233Sbonwick 
56b5152584SMatthew Ahrens /*
57b5152584SMatthew Ahrens  * The SPA supports block sizes up to 16MB.  However, very large blocks
58b5152584SMatthew Ahrens  * can have an impact on i/o latency (e.g. tying up a spinning disk for
59b5152584SMatthew Ahrens  * ~300ms), and also potentially on the memory allocator.  Therefore,
60b5152584SMatthew Ahrens  * we do not allow the recordsize to be set larger than zfs_max_recordsize
61b5152584SMatthew Ahrens  * (default 1MB).  Larger blocks can be created by changing this tunable,
62b5152584SMatthew Ahrens  * and pools with larger blocks can always be imported and used, regardless
63b5152584SMatthew Ahrens  * of this setting.
64b5152584SMatthew Ahrens  */
65b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024;
66b5152584SMatthew Ahrens 
67cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
68cde58dbcSMatthew Ahrens 	{ \
69cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
70cde58dbcSMatthew Ahrens 		(x) = (y); \
71cde58dbcSMatthew Ahrens 		(y) = __tmp; \
72cde58dbcSMatthew Ahrens 	}
73cde58dbcSMatthew Ahrens 
7455434c77Sek #define	DS_REF_MAX	(1ULL << 62)
75fa9e4066Sahrens 
76c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
77c1379625SJustin T. Gibbs 
78a9799022Sck /*
79a9799022Sck  * Figure out how much of this delta should be propogated to the dsl_dir
80a9799022Sck  * layer.  If there's a refreservation, that space has already been
81a9799022Sck  * partially accounted for in our ancestors.
82a9799022Sck  */
83a9799022Sck static int64_t
84a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta)
85a9799022Sck {
86c1379625SJustin T. Gibbs 	dsl_dataset_phys_t *ds_phys;
87a9799022Sck 	uint64_t old_bytes, new_bytes;
88a9799022Sck 
89a9799022Sck 	if (ds->ds_reserved == 0)
90a9799022Sck 		return (delta);
91a9799022Sck 
92c1379625SJustin T. Gibbs 	ds_phys = dsl_dataset_phys(ds);
93c1379625SJustin T. Gibbs 	old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
94c1379625SJustin T. Gibbs 	new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
95a9799022Sck 
96a9799022Sck 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
97a9799022Sck 	return (new_bytes - old_bytes);
98a9799022Sck }
99fa9e4066Sahrens 
100fa9e4066Sahrens void
101b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
102fa9e4066Sahrens {
103b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
104fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
105fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
106a9799022Sck 	int64_t delta;
107fa9e4066Sahrens 
1083f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
109fa9e4066Sahrens 
110fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
111fa9e4066Sahrens 	/* It could have been compressed away to nothing */
112fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
113fa9e4066Sahrens 		return;
114fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
115ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
116fa9e4066Sahrens 	if (ds == NULL) {
117ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
118ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
119fa9e4066Sahrens 		return;
120fa9e4066Sahrens 	}
1213f9d6ad7SLin Ling 
122b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
123fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
124a9799022Sck 	delta = parent_delta(ds, used);
125c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes += used;
126c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
127c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
128c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes += used;
129*45818ee1SMatthew Ahrens 
130ca0cc391SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
131ca0cc391SMatthew Ahrens 		ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
132ca0cc391SMatthew Ahrens 		    B_TRUE;
133ca0cc391SMatthew Ahrens 	}
134*45818ee1SMatthew Ahrens 
135*45818ee1SMatthew Ahrens 	spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
136*45818ee1SMatthew Ahrens 	if (f != SPA_FEATURE_NONE)
137*45818ee1SMatthew Ahrens 		ds->ds_feature_activation_needed[f] = B_TRUE;
138*45818ee1SMatthew Ahrens 
139fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
14074e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
14174e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
14274e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
14374e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
144fa9e4066Sahrens }
145fa9e4066Sahrens 
146cdb0ab79Smaybee int
147b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
148b24ab676SJeff Bonwick     boolean_t async)
149fa9e4066Sahrens {
15043466aaeSMax Grossman 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
15143466aaeSMax Grossman 	int compressed = BP_GET_PSIZE(bp);
15243466aaeSMax Grossman 	int uncompressed = BP_GET_UCSIZE(bp);
15343466aaeSMax Grossman 
154fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
155cdb0ab79Smaybee 		return (0);
156fa9e4066Sahrens 
157b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
158b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
159b24ab676SJeff Bonwick 
160fa9e4066Sahrens 	if (ds == NULL) {
161b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
162ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
163ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
164cdb0ab79Smaybee 		return (used);
165fa9e4066Sahrens 	}
166fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
167fa9e4066Sahrens 
168bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
169fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
170fa9e4066Sahrens 
171c1379625SJustin T. Gibbs 	if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
172a9799022Sck 		int64_t delta;
173c717a561Smaybee 
1743f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
175b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
176fa9e4066Sahrens 
177fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
178c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
179a9799022Sck 		    !DS_UNIQUE_IS_ACCURATE(ds));
180a9799022Sck 		delta = parent_delta(ds, -used);
181c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_unique_bytes -= used;
182fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
18374e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
184a9799022Sck 		    delta, -compressed, -uncompressed, tx);
18574e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
18674e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
187fa9e4066Sahrens 	} else {
188fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
189b24ab676SJeff Bonwick 		if (async) {
190b24ab676SJeff Bonwick 			/*
191b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
192b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
193cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
194b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
195b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
196b24ab676SJeff Bonwick 			 */
197cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
198b24ab676SJeff Bonwick 		} else {
199cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
200b24ab676SJeff Bonwick 		}
201a4611edeSahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
202c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj);
203c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
204fa9e4066Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
205c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
206a4611edeSahrens 		    ds->ds_object && bp->blk_birth >
207c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
208a4611edeSahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
209a4611edeSahrens 			mutex_enter(&ds->ds_prev->ds_lock);
210c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
211a4611edeSahrens 			mutex_exit(&ds->ds_prev->ds_lock);
212fa9e4066Sahrens 		}
2133f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
21474e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
21574e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
21674e7dc98SMatthew Ahrens 		}
217fa9e4066Sahrens 	}
218fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
219c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
220c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
221c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
222c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
223c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
224c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
225fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
226cdb0ab79Smaybee 
227cdb0ab79Smaybee 	return (used);
228fa9e4066Sahrens }
229fa9e4066Sahrens 
230ea8dc4b6Seschrock uint64_t
231ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
232fa9e4066Sahrens {
233a2eea2e1Sahrens 	uint64_t trysnap = 0;
234a2eea2e1Sahrens 
235fa9e4066Sahrens 	if (ds == NULL)
236ea8dc4b6Seschrock 		return (0);
237fa9e4066Sahrens 	/*
238fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
239fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
240fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
241fa9e4066Sahrens 	 * consume, which is OK.
242fa9e4066Sahrens 	 *
243fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
244fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
245fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
246fa9e4066Sahrens 	 */
247a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
248a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
249a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
250c1379625SJustin T. Gibbs 	return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap));
251ea8dc4b6Seschrock }
252ea8dc4b6Seschrock 
2533d692628SSanjeev Bagewadi boolean_t
254c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
255c7cd2421SGeorge Wilson     uint64_t blk_birth)
256ea8dc4b6Seschrock {
25743466aaeSMax Grossman 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
25843466aaeSMax Grossman 	    (bp != NULL && BP_IS_HOLE(bp)))
259c7cd2421SGeorge Wilson 		return (B_FALSE);
260c7cd2421SGeorge Wilson 
261837b568bSGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
262c7cd2421SGeorge Wilson 
263c7cd2421SGeorge Wilson 	return (B_TRUE);
264fa9e4066Sahrens }
265fa9e4066Sahrens 
266fa9e4066Sahrens static void
267bc9014e6SJustin Gibbs dsl_dataset_evict(void *dbu)
268fa9e4066Sahrens {
269bc9014e6SJustin Gibbs 	dsl_dataset_t *ds = dbu;
270fa9e4066Sahrens 
2713b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
272fa9e4066Sahrens 
273bc9014e6SJustin Gibbs 	ds->ds_dbuf = NULL;
274bc9014e6SJustin Gibbs 
27591ebeef5Sahrens 	unique_remove(ds->ds_fsid_guid);
276fa9e4066Sahrens 
277503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
278503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
279fa9e4066Sahrens 
280fa9e4066Sahrens 	if (ds->ds_prev) {
2813b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
282fa9e4066Sahrens 		ds->ds_prev = NULL;
283fa9e4066Sahrens 	}
284fa9e4066Sahrens 
285cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
286bc9014e6SJustin Gibbs 	if (ds->ds_deadlist.dl_os != NULL)
287cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
288745cd3c5Smaybee 	if (ds->ds_dir)
289bc9014e6SJustin Gibbs 		dsl_dir_async_rele(ds->ds_dir, ds);
290fa9e4066Sahrens 
29191ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
292fa9e4066Sahrens 
2935ad82045Snd 	mutex_destroy(&ds->ds_lock);
29491ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
295d2b3cbbdSJorgen Lundman 	mutex_destroy(&ds->ds_sendstream_lock);
2963b2aab18SMatthew Ahrens 	refcount_destroy(&ds->ds_longholds);
2975ad82045Snd 
298fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
299fa9e4066Sahrens }
300fa9e4066Sahrens 
3013b2aab18SMatthew Ahrens int
302fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
305fa9e4066Sahrens 	int err;
306fa9e4066Sahrens 	dmu_buf_t *headdbuf;
307fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
308fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
309fa9e4066Sahrens 
310fa9e4066Sahrens 	if (ds->ds_snapname[0])
311ea8dc4b6Seschrock 		return (0);
312c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
313ea8dc4b6Seschrock 		return (0);
314fa9e4066Sahrens 
315c1379625SJustin T. Gibbs 	err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
316ea8dc4b6Seschrock 	    FTAG, &headdbuf);
3173b2aab18SMatthew Ahrens 	if (err != 0)
318ea8dc4b6Seschrock 		return (err);
319fa9e4066Sahrens 	headphys = headdbuf->db_data;
320fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
321e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
322ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
323ea8dc4b6Seschrock 	return (err);
324fa9e4066Sahrens }
325fa9e4066Sahrens 
3263b2aab18SMatthew Ahrens int
327745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
328ab04eb8eStimh {
329745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
330c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
331ab04eb8eStimh 	matchtype_t mt;
332ab04eb8eStimh 	int err;
333ab04eb8eStimh 
334c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
335ab04eb8eStimh 		mt = MT_FIRST;
336ab04eb8eStimh 	else
337ab04eb8eStimh 		mt = MT_EXACT;
338ab04eb8eStimh 
339745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
340ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
341ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
342745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
343ab04eb8eStimh 	return (err);
344ab04eb8eStimh }
345ab04eb8eStimh 
3463b2aab18SMatthew Ahrens int
347a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
348a2afb611SJerry Jelinek     boolean_t adj_cnt)
349ab04eb8eStimh {
350745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
351c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
352ab04eb8eStimh 	matchtype_t mt;
353ab04eb8eStimh 	int err;
354ab04eb8eStimh 
35571eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
35671eb0538SChris Kirby 
357c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
358ab04eb8eStimh 		mt = MT_FIRST;
359ab04eb8eStimh 	else
360ab04eb8eStimh 		mt = MT_EXACT;
361ab04eb8eStimh 
362745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
363ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
364745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
365a2afb611SJerry Jelinek 
366a2afb611SJerry Jelinek 	if (err == 0 && adj_cnt)
367a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
368a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
369a2afb611SJerry Jelinek 
370ab04eb8eStimh 	return (err);
371ab04eb8eStimh }
372ab04eb8eStimh 
373e57a022bSJustin T. Gibbs boolean_t
374e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
375e57a022bSJustin T. Gibbs {
3769d47dec0SJustin T. Gibbs 	dmu_buf_t *dbuf = ds->ds_dbuf;
3779d47dec0SJustin T. Gibbs 	boolean_t result = B_FALSE;
3789d47dec0SJustin T. Gibbs 
3799d47dec0SJustin T. Gibbs 	if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
3809d47dec0SJustin T. Gibbs 	    ds->ds_object, DMU_BONUS_BLKID, tag)) {
3819d47dec0SJustin T. Gibbs 
3829d47dec0SJustin T. Gibbs 		if (ds == dmu_buf_get_user(dbuf))
3839d47dec0SJustin T. Gibbs 			result = B_TRUE;
3849d47dec0SJustin T. Gibbs 		else
3859d47dec0SJustin T. Gibbs 			dmu_buf_rele(dbuf, tag);
3869d47dec0SJustin T. Gibbs 	}
3879d47dec0SJustin T. Gibbs 
3889d47dec0SJustin T. Gibbs 	return (result);
389e57a022bSJustin T. Gibbs }
390e57a022bSJustin T. Gibbs 
3913b2aab18SMatthew Ahrens int
3923b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
393745cd3c5Smaybee     dsl_dataset_t **dsp)
394fa9e4066Sahrens {
395fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
396fa9e4066Sahrens 	dmu_buf_t *dbuf;
397fa9e4066Sahrens 	dsl_dataset_t *ds;
398ea8dc4b6Seschrock 	int err;
399a7f53a56SChris Kirby 	dmu_object_info_t doi;
400fa9e4066Sahrens 
4013b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
402fa9e4066Sahrens 
403ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
4043b2aab18SMatthew Ahrens 	if (err != 0)
405ea8dc4b6Seschrock 		return (err);
406a7f53a56SChris Kirby 
407a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
408a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
4092acef22dSMatthew Ahrens 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
410b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
411be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
412b287be1bSWill Andrews 	}
413a7f53a56SChris Kirby 
414fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
415fa9e4066Sahrens 	if (ds == NULL) {
416d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
417fa9e4066Sahrens 
418fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
419fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
420fa9e4066Sahrens 		ds->ds_object = dsobj;
421bc9014e6SJustin Gibbs 		ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
422fa9e4066Sahrens 
4235ad82045Snd 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
42491ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
4254e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
4263b2aab18SMatthew Ahrens 		refcount_create(&ds->ds_longholds);
4275ad82045Snd 
428cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
429cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
430c1379625SJustin T. Gibbs 		    mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
431cde58dbcSMatthew Ahrens 
4324e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4334e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
4344e3c9f44SBill Pijewski 
435b5152584SMatthew Ahrens 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
436ca0cc391SMatthew Ahrens 			for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
437ca0cc391SMatthew Ahrens 				if (!(spa_feature_table[f].fi_flags &
438ca0cc391SMatthew Ahrens 				    ZFEATURE_FLAG_PER_DATASET))
439ca0cc391SMatthew Ahrens 					continue;
440ca0cc391SMatthew Ahrens 				err = zap_contains(mos, dsobj,
441ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_guid);
442ca0cc391SMatthew Ahrens 				if (err == 0) {
443ca0cc391SMatthew Ahrens 					ds->ds_feature_inuse[f] = B_TRUE;
444ca0cc391SMatthew Ahrens 				} else {
445ca0cc391SMatthew Ahrens 					ASSERT3U(err, ==, ENOENT);
446ca0cc391SMatthew Ahrens 					err = 0;
447ca0cc391SMatthew Ahrens 				}
448e1f3c208SJustin T. Gibbs 			}
449b5152584SMatthew Ahrens 		}
450b5152584SMatthew Ahrens 
451ca0cc391SMatthew Ahrens 		err = dsl_dir_hold_obj(dp,
452ca0cc391SMatthew Ahrens 		    dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
4533b2aab18SMatthew Ahrens 		if (err != 0) {
4545ad82045Snd 			mutex_destroy(&ds->ds_lock);
45591ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
456d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
4573b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
458cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
459cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
460ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
461ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
462ea8dc4b6Seschrock 			return (err);
463ea8dc4b6Seschrock 		}
464fa9e4066Sahrens 
465bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
466fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
467c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
4683b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
469c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_prev_snap_obj,
470745cd3c5Smaybee 				    ds, &ds->ds_prev);
471fa9e4066Sahrens 			}
47278f17100SMatthew Ahrens 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
47378f17100SMatthew Ahrens 				int zaperr = zap_lookup(mos, ds->ds_object,
47478f17100SMatthew Ahrens 				    DS_FIELD_BOOKMARK_NAMES,
47578f17100SMatthew Ahrens 				    sizeof (ds->ds_bookmarks), 1,
47678f17100SMatthew Ahrens 				    &ds->ds_bookmarks);
47778f17100SMatthew Ahrens 				if (zaperr != ENOENT)
47878f17100SMatthew Ahrens 					VERIFY0(zaperr);
47978f17100SMatthew Ahrens 			}
480842727c2SChris Kirby 		} else {
481842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
482842727c2SChris Kirby 				err = dsl_dataset_get_snapname(ds);
483c1379625SJustin T. Gibbs 			if (err == 0 &&
484c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
485842727c2SChris Kirby 				err = zap_count(
486842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
487c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_userrefs_obj,
488842727c2SChris Kirby 				    &ds->ds_userrefs);
489842727c2SChris Kirby 			}
490fa9e4066Sahrens 		}
491fa9e4066Sahrens 
492bc9014e6SJustin Gibbs 		if (err == 0 && !ds->ds_is_snapshot) {
4933b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
4943b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4953b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
496cb625fb5Sck 			if (err == 0) {
4973b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
4983b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4993b2aab18SMatthew Ahrens 				    &ds->ds_quota);
500cb625fb5Sck 			}
501cb625fb5Sck 		} else {
502cb625fb5Sck 			ds->ds_reserved = ds->ds_quota = 0;
503cb625fb5Sck 		}
504cb625fb5Sck 
505bc9014e6SJustin Gibbs 		dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict, &ds->ds_dbuf);
506bc9014e6SJustin Gibbs 		if (err == 0)
507bc9014e6SJustin Gibbs 			winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
508bc9014e6SJustin Gibbs 
509bc9014e6SJustin Gibbs 		if (err != 0 || winner != NULL) {
510cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
511cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
512745cd3c5Smaybee 			if (ds->ds_prev)
5133b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
5143b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
5155ad82045Snd 			mutex_destroy(&ds->ds_lock);
51691ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
517d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
5183b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
519fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
5203b2aab18SMatthew Ahrens 			if (err != 0) {
521ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
522ea8dc4b6Seschrock 				return (err);
523ea8dc4b6Seschrock 			}
524fa9e4066Sahrens 			ds = winner;
525fa9e4066Sahrens 		} else {
52691ebeef5Sahrens 			ds->ds_fsid_guid =
527c1379625SJustin T. Gibbs 			    unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
528fa9e4066Sahrens 		}
529fa9e4066Sahrens 	}
530fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
531c1379625SJustin T. Gibbs 	ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
532c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
533afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
53484db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
535ea8dc4b6Seschrock 	*dsp = ds;
536ea8dc4b6Seschrock 	return (0);
537fa9e4066Sahrens }
538fa9e4066Sahrens 
539745cd3c5Smaybee int
5403b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name,
541503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
542fa9e4066Sahrens {
543fa9e4066Sahrens 	dsl_dir_t *dd;
544745cd3c5Smaybee 	const char *snapname;
545fa9e4066Sahrens 	uint64_t obj;
546fa9e4066Sahrens 	int err = 0;
547a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds;
548fa9e4066Sahrens 
5493b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
5503b2aab18SMatthew Ahrens 	if (err != 0)
551ea8dc4b6Seschrock 		return (err);
552fa9e4066Sahrens 
5533b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
554c1379625SJustin T. Gibbs 	obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
5553b2aab18SMatthew Ahrens 	if (obj != 0)
556a2cdcdd2SPaul Dagnelie 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
557745cd3c5Smaybee 	else
558be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
559fa9e4066Sahrens 
560745cd3c5Smaybee 	/* we may be looking for a snapshot */
561745cd3c5Smaybee 	if (err == 0 && snapname != NULL) {
562a2cdcdd2SPaul Dagnelie 		dsl_dataset_t *snap_ds;
563fa9e4066Sahrens 
564745cd3c5Smaybee 		if (*snapname++ != '@') {
565a2cdcdd2SPaul Dagnelie 			dsl_dataset_rele(ds, tag);
5663b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
567be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
568fa9e4066Sahrens 		}
569fa9e4066Sahrens 
570745cd3c5Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
571a2cdcdd2SPaul Dagnelie 		err = dsl_dataset_snap_lookup(ds, snapname, &obj);
572745cd3c5Smaybee 		if (err == 0)
573a2cdcdd2SPaul Dagnelie 			err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds);
574a2cdcdd2SPaul Dagnelie 		dsl_dataset_rele(ds, tag);
575745cd3c5Smaybee 
5763b2aab18SMatthew Ahrens 		if (err == 0) {
577a2cdcdd2SPaul Dagnelie 			mutex_enter(&snap_ds->ds_lock);
578a2cdcdd2SPaul Dagnelie 			if (snap_ds->ds_snapname[0] == 0)
579a2cdcdd2SPaul Dagnelie 				(void) strlcpy(snap_ds->ds_snapname, snapname,
580a2cdcdd2SPaul Dagnelie 				    sizeof (snap_ds->ds_snapname));
581a2cdcdd2SPaul Dagnelie 			mutex_exit(&snap_ds->ds_lock);
582a2cdcdd2SPaul Dagnelie 			ds = snap_ds;
583fa9e4066Sahrens 		}
584fa9e4066Sahrens 	}
585a2cdcdd2SPaul Dagnelie 	if (err == 0)
586a2cdcdd2SPaul Dagnelie 		*dsp = ds;
5873b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
588fa9e4066Sahrens 	return (err);
589fa9e4066Sahrens }
590fa9e4066Sahrens 
591fa9e4066Sahrens int
5923b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
5933b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
5943b2aab18SMatthew Ahrens {
5953b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
5963b2aab18SMatthew Ahrens 	if (err != 0)
5973b2aab18SMatthew Ahrens 		return (err);
5983b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
5993b2aab18SMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
6003b2aab18SMatthew Ahrens 		*dsp = NULL;
601be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
6023b2aab18SMatthew Ahrens 	}
6033b2aab18SMatthew Ahrens 	return (0);
6043b2aab18SMatthew Ahrens }
6053b2aab18SMatthew Ahrens 
6063b2aab18SMatthew Ahrens int
6073b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name,
608503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
609fa9e4066Sahrens {
6103b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold(dp, name, tag, dsp);
6113b2aab18SMatthew Ahrens 	if (err != 0)
612745cd3c5Smaybee 		return (err);
6133b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
614503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
615be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
616745cd3c5Smaybee 	}
617745cd3c5Smaybee 	return (0);
618fa9e4066Sahrens }
619fa9e4066Sahrens 
6203b2aab18SMatthew Ahrens /*
6213b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
6223b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
6233b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
6243b2aab18SMatthew Ahrens  *
6253b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
6263b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
6273b2aab18SMatthew Ahrens  * and accessed.
6283b2aab18SMatthew Ahrens  */
6293b2aab18SMatthew Ahrens void
6303b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
6313b2aab18SMatthew Ahrens {
6323b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
6333b2aab18SMatthew Ahrens 	(void) refcount_add(&ds->ds_longholds, tag);
6343b2aab18SMatthew Ahrens }
6353b2aab18SMatthew Ahrens 
6363b2aab18SMatthew Ahrens void
6373b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
6383b2aab18SMatthew Ahrens {
6393b2aab18SMatthew Ahrens 	(void) refcount_remove(&ds->ds_longholds, tag);
6403b2aab18SMatthew Ahrens }
6413b2aab18SMatthew Ahrens 
6423b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
6433b2aab18SMatthew Ahrens boolean_t
6443b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
6453b2aab18SMatthew Ahrens {
6463b2aab18SMatthew Ahrens 	return (!refcount_is_zero(&ds->ds_longholds));
6473b2aab18SMatthew Ahrens }
6483b2aab18SMatthew Ahrens 
649fa9e4066Sahrens void
650fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
651fa9e4066Sahrens {
652fa9e4066Sahrens 	if (ds == NULL) {
653fa9e4066Sahrens 		(void) strcpy(name, "mos");
654fa9e4066Sahrens 	} else {
655fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
6563b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
657fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
658fa9e4066Sahrens 			(void) strcat(name, "@");
659745cd3c5Smaybee 			/*
660745cd3c5Smaybee 			 * We use a "recursive" mutex so that we
661745cd3c5Smaybee 			 * can call dprintf_ds() with ds_lock held.
662745cd3c5Smaybee 			 */
663fa9e4066Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
664fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
665fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
666fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
667fa9e4066Sahrens 			} else {
668fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
669fa9e4066Sahrens 			}
670fa9e4066Sahrens 		}
671fa9e4066Sahrens 	}
672fa9e4066Sahrens }
673fa9e4066Sahrens 
6743cb34c60Sahrens void
675745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6763cb34c60Sahrens {
6773b2aab18SMatthew Ahrens 	dmu_buf_rele(ds->ds_dbuf, tag);
678745cd3c5Smaybee }
679745cd3c5Smaybee 
680745cd3c5Smaybee void
681503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
682745cd3c5Smaybee {
683d808a4fcSJustin T. Gibbs 	ASSERT3P(ds->ds_owner, ==, tag);
684d808a4fcSJustin T. Gibbs 	ASSERT(ds->ds_dbuf != NULL);
685745cd3c5Smaybee 
6863cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
687745cd3c5Smaybee 	ds->ds_owner = NULL;
6883cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
6893b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
690d808a4fcSJustin T. Gibbs 	dsl_dataset_rele(ds, tag);
6913cb34c60Sahrens }
6923cb34c60Sahrens 
6933cb34c60Sahrens boolean_t
6943b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
6953cb34c60Sahrens {
696745cd3c5Smaybee 	boolean_t gotit = FALSE;
697745cd3c5Smaybee 
6983cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
6993b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
700503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
7013b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
702745cd3c5Smaybee 		gotit = TRUE;
7033cb34c60Sahrens 	}
7043cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
705745cd3c5Smaybee 	return (gotit);
706745cd3c5Smaybee }
707745cd3c5Smaybee 
708ca0cc391SMatthew Ahrens static void
709ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
710ca0cc391SMatthew Ahrens {
711ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
712ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
713ca0cc391SMatthew Ahrens 	uint64_t zero = 0;
714ca0cc391SMatthew Ahrens 
715ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
716ca0cc391SMatthew Ahrens 
717ca0cc391SMatthew Ahrens 	spa_feature_incr(spa, f, tx);
718ca0cc391SMatthew Ahrens 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
719ca0cc391SMatthew Ahrens 
720ca0cc391SMatthew Ahrens 	VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
721ca0cc391SMatthew Ahrens 	    sizeof (zero), 1, &zero, tx));
722ca0cc391SMatthew Ahrens }
723ca0cc391SMatthew Ahrens 
724ca0cc391SMatthew Ahrens void
725ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
726ca0cc391SMatthew Ahrens {
727ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
728ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
729ca0cc391SMatthew Ahrens 
730ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
731ca0cc391SMatthew Ahrens 
732ca0cc391SMatthew Ahrens 	VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
733ca0cc391SMatthew Ahrens 	spa_feature_decr(spa, f, tx);
734ca0cc391SMatthew Ahrens }
735ca0cc391SMatthew Ahrens 
7361d452cf5Sahrens uint64_t
737088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
738ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
739fa9e4066Sahrens {
7403cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
741fa9e4066Sahrens 	dmu_buf_t *dbuf;
742fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
7433cb34c60Sahrens 	uint64_t dsobj;
744fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
745fa9e4066Sahrens 
746088f3894Sahrens 	if (origin == NULL)
747088f3894Sahrens 		origin = dp->dp_origin_snap;
748088f3894Sahrens 
7493cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
750c1379625SJustin T. Gibbs 	ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
751fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
752c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
753fa9e4066Sahrens 
7541649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
7551649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7563b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
757fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
758fa9e4066Sahrens 	dsphys = dbuf->db_data;
759745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
760fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
761ab04eb8eStimh 	dsphys->ds_flags = flags;
762fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
763fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
764fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
765fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
766ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
767ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
768fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
769088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
770a9799022Sck 
771cde58dbcSMatthew Ahrens 	if (origin == NULL) {
772cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
773cde58dbcSMatthew Ahrens 	} else {
7743b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
775cde58dbcSMatthew Ahrens 
7763cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
777fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
778c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_creation_txg;
779ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
780c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_referenced_bytes;
781fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
782c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_compressed_bytes;
783fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
784c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_uncompressed_bytes;
785c1379625SJustin T. Gibbs 		dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
78642fcb65eSMatthew Ahrens 
78742fcb65eSMatthew Ahrens 		/*
78842fcb65eSMatthew Ahrens 		 * Inherit flags that describe the dataset's contents
78942fcb65eSMatthew Ahrens 		 * (INCONSISTENT) or properties (Case Insensitive).
79042fcb65eSMatthew Ahrens 		 */
791c1379625SJustin T. Gibbs 		dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
79242fcb65eSMatthew Ahrens 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
793fa9e4066Sahrens 
794ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
795ca0cc391SMatthew Ahrens 			if (origin->ds_feature_inuse[f])
796ca0cc391SMatthew Ahrens 				dsl_dataset_activate_feature(dsobj, f, tx);
797ca0cc391SMatthew Ahrens 		}
798b5152584SMatthew Ahrens 
7993cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
800c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin)->ds_num_children++;
801fa9e4066Sahrens 
8023b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
803c1379625SJustin T. Gibbs 		    dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
804c1379625SJustin T. Gibbs 		    FTAG, &ohds));
805cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
806cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
807cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
808cde58dbcSMatthew Ahrens 
809088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
810c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
811c1379625SJustin T. Gibbs 				dsl_dataset_phys(origin)->ds_next_clones_obj =
812088f3894Sahrens 				    zap_create(mos,
813088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
814088f3894Sahrens 			}
8153b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
816c1379625SJustin T. Gibbs 			    dsl_dataset_phys(origin)->ds_next_clones_obj,
817c1379625SJustin T. Gibbs 			    dsobj, tx));
818088f3894Sahrens 		}
819088f3894Sahrens 
820fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
821c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
822cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
823c1379625SJustin T. Gibbs 			if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
824cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
825c1379625SJustin T. Gibbs 				dsl_dir_phys(origin->ds_dir)->dd_clones =
826cde58dbcSMatthew Ahrens 				    zap_create(mos,
827cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
828cde58dbcSMatthew Ahrens 			}
8293b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
830c1379625SJustin T. Gibbs 			    dsl_dir_phys(origin->ds_dir)->dd_clones,
831c1379625SJustin T. Gibbs 			    dsobj, tx));
832cde58dbcSMatthew Ahrens 		}
833fa9e4066Sahrens 	}
834ab04eb8eStimh 
835ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
836ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
837ab04eb8eStimh 
838ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
839fa9e4066Sahrens 
840fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
841c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
8423cb34c60Sahrens 
8433cb34c60Sahrens 	return (dsobj);
8443cb34c60Sahrens }
8453cb34c60Sahrens 
8463b2aab18SMatthew Ahrens static void
8473b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
8483b2aab18SMatthew Ahrens {
8493b2aab18SMatthew Ahrens 	objset_t *os;
8503b2aab18SMatthew Ahrens 
8513b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
8523b2aab18SMatthew Ahrens 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
8533b2aab18SMatthew Ahrens 	dsl_dataset_dirty(ds, tx);
8543b2aab18SMatthew Ahrens }
8553b2aab18SMatthew Ahrens 
8563cb34c60Sahrens uint64_t
857ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
858ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8593cb34c60Sahrens {
8603cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8613cb34c60Sahrens 	uint64_t dsobj, ddobj;
8623cb34c60Sahrens 	dsl_dir_t *dd;
8633cb34c60Sahrens 
8643b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
8653cb34c60Sahrens 	ASSERT(lastname[0] != '@');
8663cb34c60Sahrens 
867088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8683b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
8693cb34c60Sahrens 
8703b2aab18SMatthew Ahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
8713b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
8723cb34c60Sahrens 
8733cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8743cb34c60Sahrens 
875a2afb611SJerry Jelinek 	/*
876a2afb611SJerry Jelinek 	 * Since we're creating a new node we know it's a leaf, so we can
877a2afb611SJerry Jelinek 	 * initialize the counts if the limit feature is active.
878a2afb611SJerry Jelinek 	 */
879a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
880a2afb611SJerry Jelinek 		uint64_t cnt = 0;
881a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
882a2afb611SJerry Jelinek 
883a2afb611SJerry Jelinek 		dsl_dir_zapify(dd, tx);
884a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
885a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
886a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
887a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
888a2afb611SJerry Jelinek 	}
889a2afb611SJerry Jelinek 
8903b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
891fa9e4066Sahrens 
892feaa74e4SMark Maybee 	/*
893feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
894feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
895feaa74e4SMark Maybee 	 */
8963b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
897feaa74e4SMark Maybee 		dsl_dataset_t *ds;
898feaa74e4SMark Maybee 
8993b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
9003b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
901feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
902feaa74e4SMark Maybee 	}
903feaa74e4SMark Maybee 
9041d452cf5Sahrens 	return (dsobj);
905fa9e4066Sahrens }
906fa9e4066Sahrens 
9071d452cf5Sahrens /*
9083b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
9093b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
9103b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
9113b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
9123b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
9133b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
9141d452cf5Sahrens  */
9153b2aab18SMatthew Ahrens void
9163b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
9171d452cf5Sahrens {
9183b2aab18SMatthew Ahrens 	uint64_t mrs_used;
9193b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
9201d452cf5Sahrens 
921bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
9221d452cf5Sahrens 
923c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
924c1379625SJustin T. Gibbs 		mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
9253b2aab18SMatthew Ahrens 	else
9263b2aab18SMatthew Ahrens 		mrs_used = 0;
927842727c2SChris Kirby 
9283b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
929fa9e4066Sahrens 
9303b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
931c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes =
932c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
93319b94df9SMatthew Ahrens 
9343b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
9353b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
936c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
937fa9e4066Sahrens }
938fa9e4066Sahrens 
9393b2aab18SMatthew Ahrens void
9403b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
9413b2aab18SMatthew Ahrens     dmu_tx_t *tx)
942842727c2SChris Kirby {
9433b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
9443b2aab18SMatthew Ahrens 	uint64_t count;
9453b2aab18SMatthew Ahrens 	int err;
9463b2aab18SMatthew Ahrens 
947c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
948c1379625SJustin T. Gibbs 	err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
949c1379625SJustin T. Gibbs 	    obj, tx);
9503b2aab18SMatthew Ahrens 	/*
9513b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
9523b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
9533b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
9543b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
9553b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
9563b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
9573b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
9583b2aab18SMatthew Ahrens 	 * remove this one.
9593b2aab18SMatthew Ahrens 	 */
9603b2aab18SMatthew Ahrens 	if (err != ENOENT)
9613b2aab18SMatthew Ahrens 		VERIFY0(err);
962c1379625SJustin T. Gibbs 	ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
9633b2aab18SMatthew Ahrens 	    &count));
964c1379625SJustin T. Gibbs 	ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
9653b2aab18SMatthew Ahrens }
966842727c2SChris Kirby 
967842727c2SChris Kirby 
9683b2aab18SMatthew Ahrens blkptr_t *
9693b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds)
9703b2aab18SMatthew Ahrens {
971c1379625SJustin T. Gibbs 	return (&dsl_dataset_phys(ds)->ds_bp);
972842727c2SChris Kirby }
973842727c2SChris Kirby 
9743b2aab18SMatthew Ahrens void
9753b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
976842727c2SChris Kirby {
9773b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
9783b2aab18SMatthew Ahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
9793b2aab18SMatthew Ahrens 	if (ds == NULL) {
9803b2aab18SMatthew Ahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
9813b2aab18SMatthew Ahrens 	} else {
9823b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
983c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_bp = *bp;
984842727c2SChris Kirby 	}
9853b2aab18SMatthew Ahrens }
986842727c2SChris Kirby 
9873b2aab18SMatthew Ahrens spa_t *
9883b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
9893b2aab18SMatthew Ahrens {
9903b2aab18SMatthew Ahrens 	return (ds->ds_dir->dd_pool->dp_spa);
991842727c2SChris Kirby }
992842727c2SChris Kirby 
9933b2aab18SMatthew Ahrens void
9943b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
995fa9e4066Sahrens {
9963b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
997842727c2SChris Kirby 
9983b2aab18SMatthew Ahrens 	if (ds == NULL) /* this is the meta-objset */
9993b2aab18SMatthew Ahrens 		return;
10001d452cf5Sahrens 
10013b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1002fa9e4066Sahrens 
1003c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
10043b2aab18SMatthew Ahrens 		panic("dirtying snapshot!");
1005fa9e4066Sahrens 
10063b2aab18SMatthew Ahrens 	dp = ds->ds_dir->dd_pool;
1007ce636f8bSMatthew Ahrens 
10083b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
10093b2aab18SMatthew Ahrens 		/* up the hold count until we can be written out */
10103b2aab18SMatthew Ahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
10113b2aab18SMatthew Ahrens 	}
10123b2aab18SMatthew Ahrens }
1013fa9e4066Sahrens 
10142e2c1355SMatthew Ahrens boolean_t
10152e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
10162e2c1355SMatthew Ahrens {
10172e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
10182e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
10192e2c1355SMatthew Ahrens 		    ds, t))
10202e2c1355SMatthew Ahrens 			return (B_TRUE);
10212e2c1355SMatthew Ahrens 	}
10222e2c1355SMatthew Ahrens 	return (B_FALSE);
10232e2c1355SMatthew Ahrens }
10242e2c1355SMatthew Ahrens 
1025fa9e4066Sahrens static int
10263b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1027fa9e4066Sahrens {
10283b2aab18SMatthew Ahrens 	uint64_t asize;
1029fa9e4066Sahrens 
10303b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
103188b7b0f2SMatthew Ahrens 		return (0);
1032fa9e4066Sahrens 
1033e1930233Sbonwick 	/*
10343b2aab18SMatthew Ahrens 	 * If there's an fs-only reservation, any blocks that might become
10353b2aab18SMatthew Ahrens 	 * owned by the snapshot dataset must be accommodated by space
10363b2aab18SMatthew Ahrens 	 * outside of the reservation.
1037e1930233Sbonwick 	 */
10383b2aab18SMatthew Ahrens 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1039c1379625SJustin T. Gibbs 	asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
10403b2aab18SMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1041be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
1042e1930233Sbonwick 
10433cb34c60Sahrens 	/*
10443b2aab18SMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
10453b2aab18SMatthew Ahrens 	 * snapshot checks in this sync group.
10463cb34c60Sahrens 	 */
10473b2aab18SMatthew Ahrens 	if (asize > 0)
10483b2aab18SMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
10493cb34c60Sahrens 
1050e1930233Sbonwick 	return (0);
1051e1930233Sbonwick }
1052e1930233Sbonwick 
10533b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg {
10543b2aab18SMatthew Ahrens 	nvlist_t *ddsa_snaps;
10553b2aab18SMatthew Ahrens 	nvlist_t *ddsa_props;
10563b2aab18SMatthew Ahrens 	nvlist_t *ddsa_errors;
1057a2afb611SJerry Jelinek 	cred_t *ddsa_cr;
10583b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t;
1059842727c2SChris Kirby 
10603cb34c60Sahrens int
10613b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1062a2afb611SJerry Jelinek     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
10631d452cf5Sahrens {
10643b2aab18SMatthew Ahrens 	int error;
10653b2aab18SMatthew Ahrens 	uint64_t value;
1066fa9e4066Sahrens 
10673b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1068745cd3c5Smaybee 
10693b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
1070842727c2SChris Kirby 		return (0);
1071fa9e4066Sahrens 
1072fa9e4066Sahrens 	/*
10733b2aab18SMatthew Ahrens 	 * We don't allow multiple snapshots of the same txg.  If there
10743b2aab18SMatthew Ahrens 	 * is already one, try again.
1075fa9e4066Sahrens 	 */
1076c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1077be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
1078fa9e4066Sahrens 
1079fa9e4066Sahrens 	/*
10803b2aab18SMatthew Ahrens 	 * Check for conflicting snapshot name.
1081fa9e4066Sahrens 	 */
10823b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
10833b2aab18SMatthew Ahrens 	if (error == 0)
1084be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
10853b2aab18SMatthew Ahrens 	if (error != ENOENT)
10863b2aab18SMatthew Ahrens 		return (error);
1087842727c2SChris Kirby 
1088ca48f36fSKeith M Wesolowski 	/*
1089ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
1090ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
1091ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
1092ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
1093ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1094ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
1095ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
1096ca48f36fSKeith M Wesolowski 	 */
1097ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
1098ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
1099ca48f36fSKeith M Wesolowski 
1100a2afb611SJerry Jelinek 	/*
1101a2afb611SJerry Jelinek 	 * Skip the check for temporary snapshots or if we have already checked
1102a2afb611SJerry Jelinek 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1103a2afb611SJerry Jelinek 	 * check the count here when we're receiving a stream.
1104a2afb611SJerry Jelinek 	 */
1105a2afb611SJerry Jelinek 	if (cnt != 0 && cr != NULL) {
1106a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1107a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1108a2afb611SJerry Jelinek 		if (error != 0)
1109a2afb611SJerry Jelinek 			return (error);
1110a2afb611SJerry Jelinek 	}
1111a2afb611SJerry Jelinek 
11123b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
11133b2aab18SMatthew Ahrens 	if (error != 0)
11143b2aab18SMatthew Ahrens 		return (error);
1115842727c2SChris Kirby 
11161d452cf5Sahrens 	return (0);
11171d452cf5Sahrens }
11181d452cf5Sahrens 
11193b2aab18SMatthew Ahrens static int
11203b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1121745cd3c5Smaybee {
11223b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
11233b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11243b2aab18SMatthew Ahrens 	nvpair_t *pair;
11253b2aab18SMatthew Ahrens 	int rv = 0;
11263b2aab18SMatthew Ahrens 
1127a2afb611SJerry Jelinek 	/*
1128a2afb611SJerry Jelinek 	 * Pre-compute how many total new snapshots will be created for each
1129a2afb611SJerry Jelinek 	 * level in the tree and below. This is needed for validating the
1130a2afb611SJerry Jelinek 	 * snapshot limit when either taking a recursive snapshot or when
1131a2afb611SJerry Jelinek 	 * taking multiple snapshots.
1132a2afb611SJerry Jelinek 	 *
1133a2afb611SJerry Jelinek 	 * The problem is that the counts are not actually adjusted when
1134a2afb611SJerry Jelinek 	 * we are checking, only when we finally sync. For a single snapshot,
1135a2afb611SJerry Jelinek 	 * this is easy, the count will increase by 1 at each node up the tree,
1136a2afb611SJerry Jelinek 	 * but its more complicated for the recursive/multiple snapshot case.
1137a2afb611SJerry Jelinek 	 *
1138a2afb611SJerry Jelinek 	 * The dsl_fs_ss_limit_check function does recursively check the count
1139a2afb611SJerry Jelinek 	 * at each level up the tree but since it is validating each snapshot
1140a2afb611SJerry Jelinek 	 * independently we need to be sure that we are validating the complete
1141a2afb611SJerry Jelinek 	 * count for the entire set of snapshots. We do this by rolling up the
1142a2afb611SJerry Jelinek 	 * counts for each component of the name into an nvlist and then
1143a2afb611SJerry Jelinek 	 * checking each of those cases with the aggregated count.
1144a2afb611SJerry Jelinek 	 *
1145a2afb611SJerry Jelinek 	 * This approach properly handles not only the recursive snapshot
1146a2afb611SJerry Jelinek 	 * case (where we get all of those on the ddsa_snaps list) but also
1147a2afb611SJerry Jelinek 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1148a2afb611SJerry Jelinek 	 * validate the limit on 'a' using a count of 2).
1149a2afb611SJerry Jelinek 	 *
1150a2afb611SJerry Jelinek 	 * We validate the snapshot names in the third loop and only report
1151a2afb611SJerry Jelinek 	 * name errors once.
1152a2afb611SJerry Jelinek 	 */
1153a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1154a2afb611SJerry Jelinek 		nvlist_t *cnt_track = NULL;
1155a2afb611SJerry Jelinek 		cnt_track = fnvlist_alloc();
1156a2afb611SJerry Jelinek 
1157a2afb611SJerry Jelinek 		/* Rollup aggregated counts into the cnt_track list */
1158a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1159a2afb611SJerry Jelinek 		    pair != NULL;
1160a2afb611SJerry Jelinek 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1161a2afb611SJerry Jelinek 			char *pdelim;
1162a2afb611SJerry Jelinek 			uint64_t val;
1163a2afb611SJerry Jelinek 			char nm[MAXPATHLEN];
1164a2afb611SJerry Jelinek 
1165a2afb611SJerry Jelinek 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1166a2afb611SJerry Jelinek 			pdelim = strchr(nm, '@');
1167a2afb611SJerry Jelinek 			if (pdelim == NULL)
1168a2afb611SJerry Jelinek 				continue;
1169a2afb611SJerry Jelinek 			*pdelim = '\0';
1170a2afb611SJerry Jelinek 
1171a2afb611SJerry Jelinek 			do {
1172a2afb611SJerry Jelinek 				if (nvlist_lookup_uint64(cnt_track, nm,
1173a2afb611SJerry Jelinek 				    &val) == 0) {
1174a2afb611SJerry Jelinek 					/* update existing entry */
1175a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm,
1176a2afb611SJerry Jelinek 					    val + 1);
1177a2afb611SJerry Jelinek 				} else {
1178a2afb611SJerry Jelinek 					/* add to list */
1179a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm, 1);
1180a2afb611SJerry Jelinek 				}
1181a2afb611SJerry Jelinek 
1182a2afb611SJerry Jelinek 				pdelim = strrchr(nm, '/');
1183a2afb611SJerry Jelinek 				if (pdelim != NULL)
1184a2afb611SJerry Jelinek 					*pdelim = '\0';
1185a2afb611SJerry Jelinek 			} while (pdelim != NULL);
1186a2afb611SJerry Jelinek 		}
1187a2afb611SJerry Jelinek 
1188a2afb611SJerry Jelinek 		/* Check aggregated counts at each level */
1189a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1190a2afb611SJerry Jelinek 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1191a2afb611SJerry Jelinek 			int error = 0;
1192a2afb611SJerry Jelinek 			char *name;
1193a2afb611SJerry Jelinek 			uint64_t cnt = 0;
1194a2afb611SJerry Jelinek 			dsl_dataset_t *ds;
1195a2afb611SJerry Jelinek 
1196a2afb611SJerry Jelinek 			name = nvpair_name(pair);
1197a2afb611SJerry Jelinek 			cnt = fnvpair_value_uint64(pair);
1198a2afb611SJerry Jelinek 			ASSERT(cnt > 0);
1199a2afb611SJerry Jelinek 
1200a2afb611SJerry Jelinek 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1201a2afb611SJerry Jelinek 			if (error == 0) {
1202a2afb611SJerry Jelinek 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1203a2afb611SJerry Jelinek 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1204a2afb611SJerry Jelinek 				    ddsa->ddsa_cr);
1205a2afb611SJerry Jelinek 				dsl_dataset_rele(ds, FTAG);
1206a2afb611SJerry Jelinek 			}
1207a2afb611SJerry Jelinek 
1208a2afb611SJerry Jelinek 			if (error != 0) {
1209a2afb611SJerry Jelinek 				if (ddsa->ddsa_errors != NULL)
1210a2afb611SJerry Jelinek 					fnvlist_add_int32(ddsa->ddsa_errors,
1211a2afb611SJerry Jelinek 					    name, error);
1212a2afb611SJerry Jelinek 				rv = error;
1213a2afb611SJerry Jelinek 				/* only report one error for this check */
1214a2afb611SJerry Jelinek 				break;
1215a2afb611SJerry Jelinek 			}
1216a2afb611SJerry Jelinek 		}
1217a2afb611SJerry Jelinek 		nvlist_free(cnt_track);
1218a2afb611SJerry Jelinek 	}
1219a2afb611SJerry Jelinek 
12203b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
12213b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
12223b2aab18SMatthew Ahrens 		int error = 0;
12233b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
12243b2aab18SMatthew Ahrens 		char *name, *atp;
12253b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
12263b2aab18SMatthew Ahrens 
12273b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
12283b2aab18SMatthew Ahrens 		if (strlen(name) >= MAXNAMELEN)
1229be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
12303b2aab18SMatthew Ahrens 		if (error == 0) {
12313b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
12323b2aab18SMatthew Ahrens 			if (atp == NULL)
1233be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
12343b2aab18SMatthew Ahrens 			if (error == 0)
12353b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
12363b2aab18SMatthew Ahrens 		}
12373b2aab18SMatthew Ahrens 		if (error == 0)
12383b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
12393b2aab18SMatthew Ahrens 		if (error == 0) {
1240a2afb611SJerry Jelinek 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
12413b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
1242a2afb611SJerry Jelinek 			    atp + 1, tx, B_FALSE, 0, NULL);
12433b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
12443b2aab18SMatthew Ahrens 		}
1245745cd3c5Smaybee 
12463b2aab18SMatthew Ahrens 		if (error != 0) {
12473b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
12483b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
12493b2aab18SMatthew Ahrens 				    name, error);
12503b2aab18SMatthew Ahrens 			}
12513b2aab18SMatthew Ahrens 			rv = error;
12523b2aab18SMatthew Ahrens 		}
12533b2aab18SMatthew Ahrens 	}
1254a2afb611SJerry Jelinek 
12553b2aab18SMatthew Ahrens 	return (rv);
1256745cd3c5Smaybee }
1257745cd3c5Smaybee 
12583b2aab18SMatthew Ahrens void
12593b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
12603b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1261745cd3c5Smaybee {
12623b2aab18SMatthew Ahrens 	static zil_header_t zero_zil;
1263745cd3c5Smaybee 
12643b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
12653b2aab18SMatthew Ahrens 	dmu_buf_t *dbuf;
12663b2aab18SMatthew Ahrens 	dsl_dataset_phys_t *dsphys;
12673b2aab18SMatthew Ahrens 	uint64_t dsobj, crtxg;
12683b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
12693b2aab18SMatthew Ahrens 	objset_t *os;
1270745cd3c5Smaybee 
12713b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1272c33e334fSMatthew Ahrens 
1273c33e334fSMatthew Ahrens 	/*
12743b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
12753b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1276c33e334fSMatthew Ahrens 	 */
12773b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
12783b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
12793b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
12803b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
1281c33e334fSMatthew Ahrens 
1282a2afb611SJerry Jelinek 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1283cde58dbcSMatthew Ahrens 
1284cde58dbcSMatthew Ahrens 	/*
12853b2aab18SMatthew Ahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1286088f3894Sahrens 	 */
1287088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1288088f3894Sahrens 		crtxg = 1;
1289088f3894Sahrens 	else
1290088f3894Sahrens 		crtxg = tx->tx_txg;
1291088f3894Sahrens 
12921649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
12931649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
12943b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1295fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1296fa9e4066Sahrens 	dsphys = dbuf->db_data;
1297745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
12981d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1299fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1300fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1301fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
1302c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1303c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1304fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1305fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1306fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1307088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
1308c1379625SJustin T. Gibbs 	dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1309c1379625SJustin T. Gibbs 	dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1310c1379625SJustin T. Gibbs 	dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1311c1379625SJustin T. Gibbs 	dsphys->ds_uncompressed_bytes =
1312c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1313c1379625SJustin T. Gibbs 	dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1314c1379625SJustin T. Gibbs 	dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1315ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1316fa9e4066Sahrens 
1317ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1318ca0cc391SMatthew Ahrens 		if (ds->ds_feature_inuse[f])
1319ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(dsobj, f, tx);
1320ca0cc391SMatthew Ahrens 	}
1321b5152584SMatthew Ahrens 
1322c1379625SJustin T. Gibbs 	ASSERT3U(ds->ds_prev != 0, ==,
1323c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
13241d452cf5Sahrens 	if (ds->ds_prev) {
1325088f3894Sahrens 		uint64_t next_clones_obj =
1326c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1327c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1328fa9e4066Sahrens 		    ds->ds_object ||
1329c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1330c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1331c1379625SJustin T. Gibbs 		    ds->ds_object) {
13321d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1333c1379625SJustin T. Gibbs 			ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1334c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1335c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1336088f3894Sahrens 		} else if (next_clones_obj != 0) {
13373b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1338c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
13393b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1340088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1341fa9e4066Sahrens 		}
1342fa9e4066Sahrens 	}
1343fa9e4066Sahrens 
1344a9799022Sck 	/*
1345a9799022Sck 	 * If we have a reference-reservation on this dataset, we will
1346a9799022Sck 	 * need to increase the amount of refreservation being charged
1347a9799022Sck 	 * since our unique space is going to zero.
1348a9799022Sck 	 */
1349a9799022Sck 	if (ds->ds_reserved) {
13503f9d6ad7SLin Ling 		int64_t delta;
13513f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1352c1379625SJustin T. Gibbs 		delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1353c1379625SJustin T. Gibbs 		    ds->ds_reserved);
135474e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
13553f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1356a9799022Sck 	}
1357a9799022Sck 
1358fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1359c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj =
1360c1379625SJustin T. Gibbs 	    dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1361c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1362cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1363c1379625SJustin T. Gibbs 	dsl_deadlist_open(&ds->ds_deadlist, mos,
1364c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1365cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
1366c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1367cde58dbcSMatthew Ahrens 
1368c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1369c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1370c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1371c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1372a9799022Sck 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1373c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1374fa9e4066Sahrens 
1375c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
13763b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1377fa9e4066Sahrens 
1378fa9e4066Sahrens 	if (ds->ds_prev)
13793b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
13803b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
1381c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1382ecd6cf80Smarks 
13833f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1384088f3894Sahrens 
138571eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
138671eb0538SChris Kirby 
13874445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1388fa9e4066Sahrens }
1389fa9e4066Sahrens 
13903b2aab18SMatthew Ahrens static void
13913b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1392fa9e4066Sahrens {
13933b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
13943b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
13953b2aab18SMatthew Ahrens 	nvpair_t *pair;
139691ebeef5Sahrens 
13973b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
13983b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
13993b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
14003b2aab18SMatthew Ahrens 		char *name, *atp;
14013b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
14023b2aab18SMatthew Ahrens 
14033b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
14043b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
14053b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
14063b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
14073b2aab18SMatthew Ahrens 
14083b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
14093b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
14103b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
14113b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
14123b2aab18SMatthew Ahrens 		}
14133b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14143b2aab18SMatthew Ahrens 	}
1415fa9e4066Sahrens }
1416fa9e4066Sahrens 
14173b2aab18SMatthew Ahrens /*
14183b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
14193b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
14203b2aab18SMatthew Ahrens  */
14213b2aab18SMatthew Ahrens int
14223b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
142319b94df9SMatthew Ahrens {
14243b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
14253b2aab18SMatthew Ahrens 	nvpair_t *pair;
14263b2aab18SMatthew Ahrens 	boolean_t needsuspend;
14273b2aab18SMatthew Ahrens 	int error;
14283b2aab18SMatthew Ahrens 	spa_t *spa;
14293b2aab18SMatthew Ahrens 	char *firstname;
14303b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
143119b94df9SMatthew Ahrens 
14323b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
14333b2aab18SMatthew Ahrens 	if (pair == NULL)
14343b2aab18SMatthew Ahrens 		return (0);
14353b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
14363b2aab18SMatthew Ahrens 
14373b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
14383b2aab18SMatthew Ahrens 	if (error != 0)
14393b2aab18SMatthew Ahrens 		return (error);
14403b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
14413b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
14423b2aab18SMatthew Ahrens 
14433b2aab18SMatthew Ahrens 	if (needsuspend) {
14443b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
14453b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
14463b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
14473b2aab18SMatthew Ahrens 			char fsname[MAXNAMELEN];
14483b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
14493b2aab18SMatthew Ahrens 			char *atp;
14503b2aab18SMatthew Ahrens 			void *cookie;
14513b2aab18SMatthew Ahrens 
14523b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
14533b2aab18SMatthew Ahrens 			if (atp == NULL) {
1454be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
14553b2aab18SMatthew Ahrens 				break;
14563b2aab18SMatthew Ahrens 			}
14573b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
14583b2aab18SMatthew Ahrens 
14593b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
14603b2aab18SMatthew Ahrens 			if (error != 0)
14613b2aab18SMatthew Ahrens 				break;
14623b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
14633b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
14643b2aab18SMatthew Ahrens 		}
14653b2aab18SMatthew Ahrens 	}
14663b2aab18SMatthew Ahrens 
14673b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
14683b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
14693b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
1470a2afb611SJerry Jelinek 	ddsa.ddsa_cr = CRED();
14713b2aab18SMatthew Ahrens 
14723b2aab18SMatthew Ahrens 	if (error == 0) {
14733b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
14743b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
14757d46dc6cSMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
14763b2aab18SMatthew Ahrens 	}
14773b2aab18SMatthew Ahrens 
14783b2aab18SMatthew Ahrens 	if (suspended != NULL) {
14793b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
14803b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
14813b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
14823b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
14833b2aab18SMatthew Ahrens 		}
14843b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
14853b2aab18SMatthew Ahrens 	}
14863b2aab18SMatthew Ahrens 
14873b2aab18SMatthew Ahrens 	return (error);
14883b2aab18SMatthew Ahrens }
14893b2aab18SMatthew Ahrens 
14903b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
14913b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
14923b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
14933b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
14943b2aab18SMatthew Ahrens 	const char *ddsta_htag;
14953b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
14963b2aab18SMatthew Ahrens 
14973b2aab18SMatthew Ahrens static int
14983b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
14993b2aab18SMatthew Ahrens {
15003b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15013b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15023b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15033b2aab18SMatthew Ahrens 	int error;
15043b2aab18SMatthew Ahrens 
15053b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
15063b2aab18SMatthew Ahrens 	if (error != 0)
15073b2aab18SMatthew Ahrens 		return (error);
15083b2aab18SMatthew Ahrens 
1509a2afb611SJerry Jelinek 	/* NULL cred means no limit check for tmp snapshot */
1510ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1511a2afb611SJerry Jelinek 	    tx, B_FALSE, 0, NULL);
15123b2aab18SMatthew Ahrens 	if (error != 0) {
15133b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15143b2aab18SMatthew Ahrens 		return (error);
15153b2aab18SMatthew Ahrens 	}
15163b2aab18SMatthew Ahrens 
15173b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
15183b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1519be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
15203b2aab18SMatthew Ahrens 	}
15213b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
15223b2aab18SMatthew Ahrens 	    B_TRUE, tx);
15233b2aab18SMatthew Ahrens 	if (error != 0) {
15243b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15253b2aab18SMatthew Ahrens 		return (error);
15263b2aab18SMatthew Ahrens 	}
15273b2aab18SMatthew Ahrens 
15283b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15293b2aab18SMatthew Ahrens 	return (0);
15303b2aab18SMatthew Ahrens }
15313b2aab18SMatthew Ahrens 
15323b2aab18SMatthew Ahrens static void
15333b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
15343b2aab18SMatthew Ahrens {
15353b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15363b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15373b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15383b2aab18SMatthew Ahrens 
15393b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
15403b2aab18SMatthew Ahrens 
15413b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
15423b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
15433b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
15443b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
15453b2aab18SMatthew Ahrens 
15463b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15473b2aab18SMatthew Ahrens }
15483b2aab18SMatthew Ahrens 
15493b2aab18SMatthew Ahrens int
15503b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
15513b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
15523b2aab18SMatthew Ahrens {
15533b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
15543b2aab18SMatthew Ahrens 	int error;
15553b2aab18SMatthew Ahrens 	spa_t *spa;
15563b2aab18SMatthew Ahrens 	boolean_t needsuspend;
15573b2aab18SMatthew Ahrens 	void *cookie;
15583b2aab18SMatthew Ahrens 
15593b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
15603b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
15613b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
15623b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
15633b2aab18SMatthew Ahrens 
15643b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
15653b2aab18SMatthew Ahrens 	if (error != 0)
15663b2aab18SMatthew Ahrens 		return (error);
15673b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
15683b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
15693b2aab18SMatthew Ahrens 
15703b2aab18SMatthew Ahrens 	if (needsuspend) {
15713b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
15723b2aab18SMatthew Ahrens 		if (error != 0)
15733b2aab18SMatthew Ahrens 			return (error);
15743b2aab18SMatthew Ahrens 	}
15753b2aab18SMatthew Ahrens 
15763b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
15777d46dc6cSMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
15783b2aab18SMatthew Ahrens 
15793b2aab18SMatthew Ahrens 	if (needsuspend)
15803b2aab18SMatthew Ahrens 		zil_resume(cookie);
15813b2aab18SMatthew Ahrens 	return (error);
15823b2aab18SMatthew Ahrens }
15833b2aab18SMatthew Ahrens 
15843b2aab18SMatthew Ahrens 
15853b2aab18SMatthew Ahrens void
15863b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
15873b2aab18SMatthew Ahrens {
15883b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
15893b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1590c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
15913b2aab18SMatthew Ahrens 
15923b2aab18SMatthew Ahrens 	/*
15933b2aab18SMatthew Ahrens 	 * in case we had to change ds_fsid_guid when we opened it,
15943b2aab18SMatthew Ahrens 	 * sync it out now.
15953b2aab18SMatthew Ahrens 	 */
15963b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1597c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
15983b2aab18SMatthew Ahrens 
15993b2aab18SMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
1600b5152584SMatthew Ahrens 
1601ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1602ca0cc391SMatthew Ahrens 		if (ds->ds_feature_activation_needed[f]) {
1603ca0cc391SMatthew Ahrens 			if (ds->ds_feature_inuse[f])
1604ca0cc391SMatthew Ahrens 				continue;
1605ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(ds->ds_object, f, tx);
1606ca0cc391SMatthew Ahrens 			ds->ds_feature_inuse[f] = B_TRUE;
1607ca0cc391SMatthew Ahrens 		}
1608b5152584SMatthew Ahrens 	}
16093b2aab18SMatthew Ahrens }
16103b2aab18SMatthew Ahrens 
16113b2aab18SMatthew Ahrens static void
16123b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
16133b2aab18SMatthew Ahrens {
16143b2aab18SMatthew Ahrens 	uint64_t count = 0;
16153b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
16163b2aab18SMatthew Ahrens 	zap_cursor_t zc;
16173b2aab18SMatthew Ahrens 	zap_attribute_t za;
16183b2aab18SMatthew Ahrens 	nvlist_t *propval = fnvlist_alloc();
16193b2aab18SMatthew Ahrens 	nvlist_t *val = fnvlist_alloc();
16203b2aab18SMatthew Ahrens 
16213b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
162219b94df9SMatthew Ahrens 
162319b94df9SMatthew Ahrens 	/*
16243b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
162519b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
162619b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
162719b94df9SMatthew Ahrens 	 */
1628c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1629c1379625SJustin T. Gibbs 		VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
163019b94df9SMatthew Ahrens 		    &count));
163119b94df9SMatthew Ahrens 	}
1632c1379625SJustin T. Gibbs 	if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
163319b94df9SMatthew Ahrens 		goto fail;
1634c1379625SJustin T. Gibbs 	for (zap_cursor_init(&zc, mos,
1635c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_next_clones_obj);
163619b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
163719b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
163819b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
163919b94df9SMatthew Ahrens 		char buf[ZFS_MAXNAMELEN];
16403b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
16413b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
164219b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
16433b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
164419b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
164519b94df9SMatthew Ahrens 	}
164619b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
16473b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
16483b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
164919b94df9SMatthew Ahrens fail:
165019b94df9SMatthew Ahrens 	nvlist_free(val);
165119b94df9SMatthew Ahrens 	nvlist_free(propval);
165219b94df9SMatthew Ahrens }
165319b94df9SMatthew Ahrens 
1654fa9e4066Sahrens void
1655a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1656fa9e4066Sahrens {
16573b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1658187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
1659a9799022Sck 
16603b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
16613b2aab18SMatthew Ahrens 
1662c1379625SJustin T. Gibbs 	ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
1663c1379625SJustin T. Gibbs 	    (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
1664c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_compressed_bytes);
16654445fffbSMatthew Ahrens 
16664445fffbSMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
166777372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1668c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes);
16694445fffbSMatthew Ahrens 
1670bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
16714445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
16724445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1673c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_unique_bytes);
16744445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
16754445fffbSMatthew Ahrens 	} else {
1676b461c746SMatthew Ahrens 		if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
1677b461c746SMatthew Ahrens 			char buf[MAXNAMELEN];
1678b461c746SMatthew Ahrens 			dsl_dataset_name(ds->ds_prev, buf);
1679b461c746SMatthew Ahrens 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1680b461c746SMatthew Ahrens 		}
1681b461c746SMatthew Ahrens 
16824445fffbSMatthew Ahrens 		dsl_dir_stats(ds->ds_dir, nv);
16834445fffbSMatthew Ahrens 	}
1684fa9e4066Sahrens 
1685a9799022Sck 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1686a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1687a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1688a9799022Sck 
1689a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1690c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_time);
1691a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1692c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_txg);
1693a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1694a9799022Sck 	    ds->ds_quota);
1695a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1696a9799022Sck 	    ds->ds_reserved);
1697c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1698c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_guid);
16991d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1700c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
17011d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
17021d713200SEric Schrock 	    ds->ds_object);
170392241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
170492241e0bSTom Erickson 	    ds->ds_userrefs);
1705842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1706842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1707fa9e4066Sahrens 
1708c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
170919b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
171019b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
171119b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
171219b94df9SMatthew Ahrens 
171319b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
1714c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
171519b94df9SMatthew Ahrens 		if (err == 0) {
171619b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
171719b94df9SMatthew Ahrens 			    &comp, &uncomp);
171819b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
171919b94df9SMatthew Ahrens 			if (err == 0) {
172019b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
172119b94df9SMatthew Ahrens 				    written);
172219b94df9SMatthew Ahrens 			}
172319b94df9SMatthew Ahrens 		}
172419b94df9SMatthew Ahrens 	}
1725fa9e4066Sahrens }
1726fa9e4066Sahrens 
1727a2eea2e1Sahrens void
1728a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1729a2eea2e1Sahrens {
17303b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
17313b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
17323b2aab18SMatthew Ahrens 
1733c1379625SJustin T. Gibbs 	stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
1734c1379625SJustin T. Gibbs 	stat->dds_inconsistent =
1735c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
1736c1379625SJustin T. Gibbs 	stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
17374445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
1738bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
1739a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
1740c1379625SJustin T. Gibbs 		stat->dds_num_clones =
1741c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_num_children - 1;
1742ebedde84SEric Taylor 	} else {
1743ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
1744ebedde84SEric Taylor 		stat->dds_num_clones = 0;
1745a2eea2e1Sahrens 
17464445fffbSMatthew Ahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
17474445fffbSMatthew Ahrens 			dsl_dataset_t *ods;
1748a2eea2e1Sahrens 
17493b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
1750c1379625SJustin T. Gibbs 			    dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
1751c1379625SJustin T. Gibbs 			    FTAG, &ods));
17524445fffbSMatthew Ahrens 			dsl_dataset_name(ods, stat->dds_origin);
17533b2aab18SMatthew Ahrens 			dsl_dataset_rele(ods, FTAG);
17544445fffbSMatthew Ahrens 		}
1755a2eea2e1Sahrens 	}
1756a2eea2e1Sahrens }
1757a2eea2e1Sahrens 
1758a2eea2e1Sahrens uint64_t
1759a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1760a2eea2e1Sahrens {
176191ebeef5Sahrens 	return (ds->ds_fsid_guid);
1762a2eea2e1Sahrens }
1763a2eea2e1Sahrens 
1764a2eea2e1Sahrens void
1765a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
1766a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
1767a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1768fa9e4066Sahrens {
1769c1379625SJustin T. Gibbs 	*refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
1770a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1771c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
1772c1379625SJustin T. Gibbs 		*availbytesp +=
1773c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
1774a9799022Sck 	if (ds->ds_quota != 0) {
1775a9799022Sck 		/*
1776a9799022Sck 		 * Adjust available bytes according to refquota
1777a9799022Sck 		 */
1778a9799022Sck 		if (*refdbytesp < ds->ds_quota)
1779a9799022Sck 			*availbytesp = MIN(*availbytesp,
1780a9799022Sck 			    ds->ds_quota - *refdbytesp);
1781a9799022Sck 		else
1782a9799022Sck 			*availbytesp = 0;
1783a9799022Sck 	}
1784c1379625SJustin T. Gibbs 	*usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
1785a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1786fa9e4066Sahrens }
1787fa9e4066Sahrens 
1788f18faf3fSek boolean_t
178934f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1790f18faf3fSek {
1791f18faf3fSek 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1792f18faf3fSek 
17933b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
179434f2f8cfSMatthew Ahrens 	if (snap == NULL)
1795f18faf3fSek 		return (B_FALSE);
1796c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_bp.blk_birth >
1797c1379625SJustin T. Gibbs 	    dsl_dataset_phys(snap)->ds_creation_txg) {
179834f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
17996e0cbcaaSMatthew Ahrens 		/*
18006e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
18016e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
18026e0cbcaaSMatthew Ahrens 		 * modified.
18036e0cbcaaSMatthew Ahrens 		 */
18046e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
18056e0cbcaaSMatthew Ahrens 			return (B_TRUE);
180634f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
18076e0cbcaaSMatthew Ahrens 			return (B_TRUE);
18086e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
180934f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
18106e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
18116e0cbcaaSMatthew Ahrens 	}
1812f18faf3fSek 	return (B_FALSE);
1813f18faf3fSek }
1814f18faf3fSek 
18153b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
18163b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
18173b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
18183b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
18193b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
18203b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
18213b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
18223b2aab18SMatthew Ahrens 
18231d452cf5Sahrens /* ARGSUSED */
1824fa9e4066Sahrens static int
18253b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
18263b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1827fa9e4066Sahrens {
18283b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
18293b2aab18SMatthew Ahrens 	int error;
1830fa9e4066Sahrens 	uint64_t val;
1831fa9e4066Sahrens 
18323b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
18333b2aab18SMatthew Ahrens 	if (error != 0) {
18343b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
18353b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
18363b2aab18SMatthew Ahrens 	}
18371d452cf5Sahrens 
18383b2aab18SMatthew Ahrens 	/* new name should not exist */
18393b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
18403b2aab18SMatthew Ahrens 	if (error == 0)
1841be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
18423b2aab18SMatthew Ahrens 	else if (error == ENOENT)
18433b2aab18SMatthew Ahrens 		error = 0;
1844cdf5b4caSmmusante 
1845cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
18463b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
18473b2aab18SMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1848be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
1849cdf5b4caSmmusante 
18503b2aab18SMatthew Ahrens 	return (error);
18511d452cf5Sahrens }
1852fa9e4066Sahrens 
18533b2aab18SMatthew Ahrens static int
18543b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
18551d452cf5Sahrens {
18563b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
18573b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18581d452cf5Sahrens 	dsl_dataset_t *hds;
18593b2aab18SMatthew Ahrens 	int error;
1860fa9e4066Sahrens 
18613b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
18623b2aab18SMatthew Ahrens 	if (error != 0)
18633b2aab18SMatthew Ahrens 		return (error);
1864fa9e4066Sahrens 
18653b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
18663b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
18673b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
18683b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
18693b2aab18SMatthew Ahrens 	} else {
18703b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
18713b2aab18SMatthew Ahrens 	}
1872745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
18733b2aab18SMatthew Ahrens 	return (error);
1874fa9e4066Sahrens }
1875fa9e4066Sahrens 
1876cdf5b4caSmmusante static int
18773b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
18783b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1879cdf5b4caSmmusante {
18803b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
18813b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
18823b2aab18SMatthew Ahrens 	uint64_t val;
18833b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
18843b2aab18SMatthew Ahrens 	int error;
1885ecd6cf80Smarks 
18863b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
18873b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
18883b2aab18SMatthew Ahrens 	if (error == ENOENT) {
18893b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
18903b2aab18SMatthew Ahrens 		return (0);
1891ecd6cf80Smarks 	}
1892ecd6cf80Smarks 
18933b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
18943b2aab18SMatthew Ahrens 
18953b2aab18SMatthew Ahrens 	/* log before we change the name */
18963b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
18973b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
1898cdf5b4caSmmusante 
1899a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
1900a2afb611SJerry Jelinek 	    B_FALSE));
19013b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
19023b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
19033b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
1904c1379625SJustin T. Gibbs 	VERIFY0(zap_add(dp->dp_meta_objset,
1905c1379625SJustin T. Gibbs 	    dsl_dataset_phys(hds)->ds_snapnames_zapobj,
19063b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1907cdf5b4caSmmusante 
19083b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1909cdf5b4caSmmusante 	return (0);
1910cdf5b4caSmmusante }
1911cdf5b4caSmmusante 
19123b2aab18SMatthew Ahrens static void
19133b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1914cdf5b4caSmmusante {
19153b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
19163b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
19173b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
1918cdf5b4caSmmusante 
19193b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
19203b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
19213b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
19223b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
19233b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
19243b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
19253b2aab18SMatthew Ahrens 	} else {
19263b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1927cdf5b4caSmmusante 	}
19283b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
1929cdf5b4caSmmusante }
1930cdf5b4caSmmusante 
19313b2aab18SMatthew Ahrens int
19323b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
19333b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
19343a5a36beSmmusante {
19353b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
19363a5a36beSmmusante 
19373b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
19383b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
19393b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
19403b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
19413a5a36beSmmusante 
19423b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
19437d46dc6cSMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
19447d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
19453a5a36beSmmusante }
19463a5a36beSmmusante 
194791948b51SKeith M Wesolowski /*
194891948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
194991948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
195091948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
195191948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
195291948b51SKeith M Wesolowski  * when we release the long hold.
195391948b51SKeith M Wesolowski  */
195491948b51SKeith M Wesolowski static int
195591948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
195691948b51SKeith M Wesolowski {
195791948b51SKeith M Wesolowski 	boolean_t held;
195891948b51SKeith M Wesolowski 
195991948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
196091948b51SKeith M Wesolowski 		return (0);
196191948b51SKeith M Wesolowski 
196291948b51SKeith M Wesolowski 	if (owner != NULL) {
196391948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
196491948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
196591948b51SKeith M Wesolowski 	}
196691948b51SKeith M Wesolowski 
196791948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
196891948b51SKeith M Wesolowski 
196991948b51SKeith M Wesolowski 	if (owner != NULL)
197091948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
197191948b51SKeith M Wesolowski 
197291948b51SKeith M Wesolowski 	if (held)
197391948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
197491948b51SKeith M Wesolowski 
197591948b51SKeith M Wesolowski 	return (0);
197691948b51SKeith M Wesolowski }
197791948b51SKeith M Wesolowski 
197891948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg {
197991948b51SKeith M Wesolowski 	const char *ddra_fsname;
198091948b51SKeith M Wesolowski 	void *ddra_owner;
1981a7027df1SMatthew Ahrens 	nvlist_t *ddra_result;
198291948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t;
198391948b51SKeith M Wesolowski 
19843b2aab18SMatthew Ahrens static int
19853b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1986fa9e4066Sahrens {
198791948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
19883b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
19891d452cf5Sahrens 	dsl_dataset_t *ds;
19903b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
19913b2aab18SMatthew Ahrens 	int error;
1992fa9e4066Sahrens 
199391948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
19943b2aab18SMatthew Ahrens 	if (error != 0)
19953b2aab18SMatthew Ahrens 		return (error);
1996370c1af0SSanjeev Bagewadi 
19973b2aab18SMatthew Ahrens 	/* must not be a snapshot */
1998bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
19993b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2000be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
20013b2aab18SMatthew Ahrens 	}
20023a5a36beSmmusante 
20033b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
2004c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
20053b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2006be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
20073b2aab18SMatthew Ahrens 	}
20083a5a36beSmmusante 
200978f17100SMatthew Ahrens 	/* must not have any bookmarks after the most recent snapshot */
201078f17100SMatthew Ahrens 	nvlist_t *proprequest = fnvlist_alloc();
201178f17100SMatthew Ahrens 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
201278f17100SMatthew Ahrens 	nvlist_t *bookmarks = fnvlist_alloc();
201378f17100SMatthew Ahrens 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
201478f17100SMatthew Ahrens 	fnvlist_free(proprequest);
201578f17100SMatthew Ahrens 	if (error != 0)
201678f17100SMatthew Ahrens 		return (error);
201778f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
201878f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
201978f17100SMatthew Ahrens 		nvlist_t *valuenv =
202078f17100SMatthew Ahrens 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
202178f17100SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
202278f17100SMatthew Ahrens 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
2023c1379625SJustin T. Gibbs 		if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
202478f17100SMatthew Ahrens 			fnvlist_free(bookmarks);
202578f17100SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
202678f17100SMatthew Ahrens 			return (SET_ERROR(EEXIST));
202778f17100SMatthew Ahrens 		}
202878f17100SMatthew Ahrens 	}
202978f17100SMatthew Ahrens 	fnvlist_free(bookmarks);
203078f17100SMatthew Ahrens 
203191948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
203291948b51SKeith M Wesolowski 	if (error != 0) {
20333b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
203491948b51SKeith M Wesolowski 		return (error);
20353b2aab18SMatthew Ahrens 	}
20363b2aab18SMatthew Ahrens 
20373b2aab18SMatthew Ahrens 	/*
20383b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
20393b2aab18SMatthew Ahrens 	 * the refquota.
20403b2aab18SMatthew Ahrens 	 */
20413b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
2042c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
20433b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2044be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2045fa9e4066Sahrens 	}
2046370c1af0SSanjeev Bagewadi 
20473b2aab18SMatthew Ahrens 	/*
20483b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
20493b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
20503b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
20513b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
20523b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
20533b2aab18SMatthew Ahrens 	 */
20543b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2055c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
20563b2aab18SMatthew Ahrens 
20573b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
20583b2aab18SMatthew Ahrens 	    unused_refres_delta >
20593b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
20603b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2061be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
2062fa9e4066Sahrens 	}
2063fa9e4066Sahrens 
20643b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
20653b2aab18SMatthew Ahrens 	return (0);
20663b2aab18SMatthew Ahrens }
20671d452cf5Sahrens 
20683b2aab18SMatthew Ahrens static void
20693b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
20703b2aab18SMatthew Ahrens {
207191948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
20723b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20733b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
20743b2aab18SMatthew Ahrens 	uint64_t cloneobj;
2075a7027df1SMatthew Ahrens 	char namebuf[ZFS_MAXNAMELEN];
20761d452cf5Sahrens 
207791948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
20781d452cf5Sahrens 
2079a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
2080a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2081a7027df1SMatthew Ahrens 
20823b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
20833b2aab18SMatthew Ahrens 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
20841d452cf5Sahrens 
20853b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
20861d452cf5Sahrens 
20873b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
20883b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
20893b2aab18SMatthew Ahrens 
20903b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
20913b2aab18SMatthew Ahrens 
20923b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
20933b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
20943b2aab18SMatthew Ahrens }
20953b2aab18SMatthew Ahrens 
209691948b51SKeith M Wesolowski /*
2097a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
2098a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
2099a7027df1SMatthew Ahrens  * in the result nvlist.
210091948b51SKeith M Wesolowski  *
2101a7027df1SMatthew Ahrens  * If owner != NULL:
210291948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
210391948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
210491948b51SKeith M Wesolowski  *   succeed or not.
210591948b51SKeith M Wesolowski  *
210691948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
210791948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
210891948b51SKeith M Wesolowski  */
21093b2aab18SMatthew Ahrens int
2110a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
21113b2aab18SMatthew Ahrens {
211291948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
211391948b51SKeith M Wesolowski 
211491948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
211591948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
2116a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
211791948b51SKeith M Wesolowski 
21183b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
21197d46dc6cSMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra,
21207d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
2121fa9e4066Sahrens }
212299653d4eSeschrock 
2123088f3894Sahrens struct promotenode {
2124745cd3c5Smaybee 	list_node_t link;
2125745cd3c5Smaybee 	dsl_dataset_t *ds;
2126745cd3c5Smaybee };
2127745cd3c5Smaybee 
21283b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg {
21293b2aab18SMatthew Ahrens 	const char *ddpa_clonename;
21303b2aab18SMatthew Ahrens 	dsl_dataset_t *ddpa_clone;
213174e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
21323b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_origin; /* origin of the origin */
213374e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2134681d9761SEric Taylor 	char *err_ds;
2135a2afb611SJerry Jelinek 	cred_t *cr;
21363b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t;
21371d452cf5Sahrens 
213874e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
21393b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
21403b2aab18SMatthew Ahrens     void *tag);
21413b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
214274e7dc98SMatthew Ahrens 
214399653d4eSeschrock static int
21443b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
214599653d4eSeschrock {
21463b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
21473b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
21483b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
21493b2aab18SMatthew Ahrens 	struct promotenode *snap;
21503b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
2151745cd3c5Smaybee 	int err;
2152cde58dbcSMatthew Ahrens 	uint64_t unused;
2153a2afb611SJerry Jelinek 	uint64_t ss_mv_cnt;
2154cb5842f8SAndriy Gapon 	size_t max_snap_len;
21551d452cf5Sahrens 
21563b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
21573b2aab18SMatthew Ahrens 	if (err != 0)
21583b2aab18SMatthew Ahrens 		return (err);
215999653d4eSeschrock 
21603b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
2161cb5842f8SAndriy Gapon 	max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
21621d452cf5Sahrens 
2163c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
21643b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
2165be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
21663b2aab18SMatthew Ahrens 	}
21673b2aab18SMatthew Ahrens 
21683b2aab18SMatthew Ahrens 	/*
21693b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
21703b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
21713b2aab18SMatthew Ahrens 	 */
21723b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
21733b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
21743b2aab18SMatthew Ahrens 		return (0);
21753b2aab18SMatthew Ahrens 	}
21763b2aab18SMatthew Ahrens 
21773b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
21783b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
217999653d4eSeschrock 
21803cb34c60Sahrens 	/* compute origin's new unique space */
21813b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2182c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2183c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2184cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2185c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
21863b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
218799653d4eSeschrock 
2188745cd3c5Smaybee 	/*
2189745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2190745cd3c5Smaybee 	 *
219174e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
21923b2aab18SMatthew Ahrens 	 * to used by each snapshot:
219374e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
219474e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
219574e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2196745cd3c5Smaybee 	 * So a sequence would look like:
219774e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2198745cd3c5Smaybee 	 * Which simplifies to:
219974e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2200745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
220174e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2202745cd3c5Smaybee 	 */
2203a2afb611SJerry Jelinek 	ss_mv_cnt = 0;
2204c1379625SJustin T. Gibbs 	ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2205c1379625SJustin T. Gibbs 	ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2206c1379625SJustin T. Gibbs 	ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
22073b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
22083b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
220999653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2210745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
221199653d4eSeschrock 
2212a2afb611SJerry Jelinek 		ss_mv_cnt++;
2213a2afb611SJerry Jelinek 
22143b2aab18SMatthew Ahrens 		/*
22153b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
22163b2aab18SMatthew Ahrens 		 * the objset.
22173b2aab18SMatthew Ahrens 		 */
22183b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
2219be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
22203b2aab18SMatthew Ahrens 			goto out;
22213b2aab18SMatthew Ahrens 		}
22223b2aab18SMatthew Ahrens 
222399653d4eSeschrock 		/* Check that the snapshot name does not conflict */
22243b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
2225cb5842f8SAndriy Gapon 		if (strlen(ds->ds_snapname) >= max_snap_len) {
2226cb5842f8SAndriy Gapon 			err = SET_ERROR(ENAMETOOLONG);
2227cb5842f8SAndriy Gapon 			goto out;
2228cb5842f8SAndriy Gapon 		}
2229745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2230681d9761SEric Taylor 		if (err == 0) {
22313b2aab18SMatthew Ahrens 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2232be6fd75aSMatthew Ahrens 			err = SET_ERROR(EEXIST);
2233681d9761SEric Taylor 			goto out;
2234681d9761SEric Taylor 		}
2235745cd3c5Smaybee 		if (err != ENOENT)
2236681d9761SEric Taylor 			goto out;
223799653d4eSeschrock 
2238745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
2239c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
224074e7dc98SMatthew Ahrens 			continue;
224174e7dc98SMatthew Ahrens 
2242cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2243cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
22443b2aab18SMatthew Ahrens 		ddpa->used += dlused;
22453b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
22463b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
224774e7dc98SMatthew Ahrens 	}
2248745cd3c5Smaybee 
2249745cd3c5Smaybee 	/*
2250745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
2251745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
2252745cd3c5Smaybee 	 */
22533b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
2254c1379625SJustin T. Gibbs 		ddpa->used -=
2255c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2256c1379625SJustin T. Gibbs 		ddpa->comp -=
2257c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
22583b2aab18SMatthew Ahrens 		ddpa->uncomp -=
2259c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->
2260c1379625SJustin T. Gibbs 		    ds_uncompressed_bytes;
226199653d4eSeschrock 	}
226299653d4eSeschrock 
2263a2afb611SJerry Jelinek 	/* Check that there is enough space and limit headroom here */
226474e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2265a2afb611SJerry Jelinek 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
22663b2aab18SMatthew Ahrens 	if (err != 0)
22673b2aab18SMatthew Ahrens 		goto out;
226874e7dc98SMatthew Ahrens 
226974e7dc98SMatthew Ahrens 	/*
227074e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
227174e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
227274e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
227374e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
227474e7dc98SMatthew Ahrens 	 */
2275c1379625SJustin T. Gibbs 	if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
227674e7dc98SMatthew Ahrens 		uint64_t space;
227774e7dc98SMatthew Ahrens 
227874e7dc98SMatthew Ahrens 		/*
227974e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
22803f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
2281cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
228274e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
228374e7dc98SMatthew Ahrens 		 * iterate over all bps.
228474e7dc98SMatthew Ahrens 		 */
22853b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
22863b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
22873b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
22883b2aab18SMatthew Ahrens 		if (err != 0)
22893b2aab18SMatthew Ahrens 			goto out;
229074e7dc98SMatthew Ahrens 
22913b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
22923f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
22933b2aab18SMatthew Ahrens 		if (err != 0)
22943b2aab18SMatthew Ahrens 			goto out;
22953b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
229674e7dc98SMatthew Ahrens 	}
2297c1379625SJustin T. Gibbs 	if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2298c1379625SJustin T. Gibbs 	    DD_FLAG_USED_BREAKDOWN) {
22993b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
2300c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_creation_txg,
2301c1379625SJustin T. Gibbs 		    &ddpa->originusedsnap);
23023b2aab18SMatthew Ahrens 		if (err != 0)
23033b2aab18SMatthew Ahrens 			goto out;
2304745cd3c5Smaybee 	}
23051d452cf5Sahrens 
2306681d9761SEric Taylor out:
23073b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2308681d9761SEric Taylor 	return (err);
23091d452cf5Sahrens }
231099653d4eSeschrock 
23111d452cf5Sahrens static void
23123b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
23131d452cf5Sahrens {
23143b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
23153b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
23163b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
23173b2aab18SMatthew Ahrens 	struct promotenode *snap;
23183b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
23193b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_head;
23203b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
23213cb34c60Sahrens 	dsl_dir_t *odd = NULL;
2322088f3894Sahrens 	uint64_t oldnext_obj;
232374e7dc98SMatthew Ahrens 	int64_t delta;
23241d452cf5Sahrens 
23253b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
23263b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
23273b2aab18SMatthew Ahrens 
2328c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
23291d452cf5Sahrens 
23303b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
23313b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
23323b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
23333b2aab18SMatthew Ahrens 
23343b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
233574e7dc98SMatthew Ahrens 	origin_head = snap->ds;
233674e7dc98SMatthew Ahrens 
23370b69c2f0Sahrens 	/*
23383cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
23390b69c2f0Sahrens 	 * changing.
23400b69c2f0Sahrens 	 */
23413b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
23423cb34c60Sahrens 	    NULL, FTAG, &odd));
234399653d4eSeschrock 
2344745cd3c5Smaybee 	/* change origin's next snap */
2345745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2346c1379625SJustin T. Gibbs 	oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
23473b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2348c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2349c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2350c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2351745cd3c5Smaybee 
2352088f3894Sahrens 	/* change the origin's next clone */
2353c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
23543b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
23553b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
23563b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2357c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2358088f3894Sahrens 		    oldnext_obj, tx));
2359088f3894Sahrens 	}
2360088f3894Sahrens 
2361745cd3c5Smaybee 	/* change origin */
2362745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2363c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2364c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
23653f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2366745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2367c1379625SJustin T. Gibbs 	dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
23683f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
2369c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
2370745cd3c5Smaybee 
2371cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2372cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
23733b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2374c1379625SJustin T. Gibbs 		    dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
23753b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2376c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2377cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2378cde58dbcSMatthew Ahrens 
23793b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2380c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2381cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
2382c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_clones == 0) {
2383c1379625SJustin T. Gibbs 			dsl_dir_phys(dd)->dd_clones =
2384c1379625SJustin T. Gibbs 			    zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2385c1379625SJustin T. Gibbs 			    DMU_OT_NONE, 0, tx);
2386cde58dbcSMatthew Ahrens 		}
23873b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2388c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2389cde58dbcSMatthew Ahrens 	}
2390cde58dbcSMatthew Ahrens 
239199653d4eSeschrock 	/* move snapshots to this dir */
23923b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
23933b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2394745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
239599653d4eSeschrock 
23963b2aab18SMatthew Ahrens 		/*
23973b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
23983b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
23993b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
24003b2aab18SMatthew Ahrens 		 */
2401503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2402503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2403503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
24043baa08fcSek 		}
24053b2aab18SMatthew Ahrens 
240699653d4eSeschrock 		/* move snap name entry */
24073b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
24083b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2409a2afb611SJerry Jelinek 		    ds->ds_snapname, tx, B_TRUE));
24103b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
2411c1379625SJustin T. Gibbs 		    dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
241299653d4eSeschrock 		    8, 1, &ds->ds_object, tx));
2413a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2414a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2415cde58dbcSMatthew Ahrens 
241699653d4eSeschrock 		/* change containing dsl_dir */
241799653d4eSeschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2418c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
2419c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
24203cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
24213b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
24223b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
242399653d4eSeschrock 		    NULL, ds, &ds->ds_dir));
242499653d4eSeschrock 
2425cde58dbcSMatthew Ahrens 		/* move any clone references */
2426c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
2427cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2428cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2429cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2430cde58dbcSMatthew Ahrens 
24313b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
2432c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
24333b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
24343b2aab18SMatthew Ahrens 			    zap_cursor_advance(&zc)) {
24353b2aab18SMatthew Ahrens 				dsl_dataset_t *cnds;
24363b2aab18SMatthew Ahrens 				uint64_t o;
2437a9799022Sck 
24383b2aab18SMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
24393b2aab18SMatthew Ahrens 					/*
24403b2aab18SMatthew Ahrens 					 * We've already moved the
24413b2aab18SMatthew Ahrens 					 * origin's reference.
24423b2aab18SMatthew Ahrens 					 */
24433b2aab18SMatthew Ahrens 					continue;
24443b2aab18SMatthew Ahrens 				}
2445a9799022Sck 
24463b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
24473b2aab18SMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
2448c1379625SJustin T. Gibbs 				o = dsl_dir_phys(cnds->ds_dir)->
2449c1379625SJustin T. Gibbs 				    dd_head_dataset_obj;
2450a9799022Sck 
24513b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
2452c1379625SJustin T. Gibbs 				    dsl_dir_phys(odd)->dd_clones, o, tx));
24533b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
2454c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd)->dd_clones, o, tx));
24553b2aab18SMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
24563b2aab18SMatthew Ahrens 			}
24573b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
24583b2aab18SMatthew Ahrens 		}
24599082849eSck 
24603b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
2461a9799022Sck 	}
2462a9799022Sck 
2463a9799022Sck 	/*
24643b2aab18SMatthew Ahrens 	 * Change space accounting.
24653b2aab18SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
24663b2aab18SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
24673b2aab18SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
2468a9799022Sck 	 */
2469a9799022Sck 
24703b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
2471c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
24723b2aab18SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
24733b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
24743b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
24753b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
24763b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
24773b2aab18SMatthew Ahrens 
24783b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
2479c1379625SJustin T. Gibbs 	    dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
24803b2aab18SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
24813b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
24823b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
24833b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
24843b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
24853b2aab18SMatthew Ahrens 
2486c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
24873b2aab18SMatthew Ahrens 
24883b2aab18SMatthew Ahrens 	/* log history record */
24893b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
24903b2aab18SMatthew Ahrens 
24913b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
24923b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2493a9799022Sck }
2494a9799022Sck 
24953b2aab18SMatthew Ahrens /*
24963b2aab18SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
24973b2aab18SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
24983b2aab18SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
24993b2aab18SMatthew Ahrens  * snapshots back to this dataset's origin.
25003b2aab18SMatthew Ahrens  */
2501a9799022Sck static int
25023b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
25033b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2504a9799022Sck {
25053b2aab18SMatthew Ahrens 	uint64_t obj = last_obj;
2506a9799022Sck 
25073b2aab18SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
25083b2aab18SMatthew Ahrens 	    offsetof(struct promotenode, link));
2509a9799022Sck 
25103b2aab18SMatthew Ahrens 	while (obj != first_obj) {
25113b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
25123b2aab18SMatthew Ahrens 		struct promotenode *snap;
25133b2aab18SMatthew Ahrens 		int err;
251492241e0bSTom Erickson 
25153b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
25163b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
25173b2aab18SMatthew Ahrens 		if (err != 0)
25183b2aab18SMatthew Ahrens 			return (err);
2519a9799022Sck 
25203b2aab18SMatthew Ahrens 		if (first_obj == 0)
2521c1379625SJustin T. Gibbs 			first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
25223b2aab18SMatthew Ahrens 
25233b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
25243b2aab18SMatthew Ahrens 		snap->ds = ds;
25253b2aab18SMatthew Ahrens 		list_insert_tail(l, snap);
2526c1379625SJustin T. Gibbs 		obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
25273b2aab18SMatthew Ahrens 	}
2528a9799022Sck 
2529a9799022Sck 	return (0);
2530a9799022Sck }
2531a9799022Sck 
25323b2aab18SMatthew Ahrens static int
25333b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2534a9799022Sck {
25353b2aab18SMatthew Ahrens 	struct promotenode *snap;
2536a9799022Sck 
25373b2aab18SMatthew Ahrens 	*spacep = 0;
25383b2aab18SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
25393b2aab18SMatthew Ahrens 		uint64_t used, comp, uncomp;
25403b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
25413b2aab18SMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
25423b2aab18SMatthew Ahrens 		*spacep += used;
254392241e0bSTom Erickson 	}
25443b2aab18SMatthew Ahrens 	return (0);
2545a9799022Sck }
2546a9799022Sck 
25473b2aab18SMatthew Ahrens static void
25483b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
2549a9799022Sck {
25503b2aab18SMatthew Ahrens 	struct promotenode *snap;
255192241e0bSTom Erickson 
25523b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
25533b2aab18SMatthew Ahrens 		return;
2554a9799022Sck 
25553b2aab18SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
25563b2aab18SMatthew Ahrens 		list_remove(l, snap);
25573b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
25583b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
25593b2aab18SMatthew Ahrens 	}
25603b2aab18SMatthew Ahrens 	list_destroy(l);
2561a9799022Sck }
2562a9799022Sck 
2563a9799022Sck static int
25643b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2565a9799022Sck {
25663b2aab18SMatthew Ahrens 	int error;
25673b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
25683b2aab18SMatthew Ahrens 	struct promotenode *snap;
2569a9799022Sck 
25703b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
25713b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
25723b2aab18SMatthew Ahrens 	if (error != 0)
25733b2aab18SMatthew Ahrens 		return (error);
25743b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
2575a9799022Sck 
2576bc9014e6SJustin Gibbs 	if (ddpa->ddpa_clone->ds_is_snapshot ||
25773b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
25783b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2579be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
25803b2aab18SMatthew Ahrens 	}
2581a9799022Sck 
2582c1379625SJustin T. Gibbs 	error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
25833b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
25843b2aab18SMatthew Ahrens 	if (error != 0)
25853b2aab18SMatthew Ahrens 		goto out;
2586a9799022Sck 
25873b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
25883b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
25893b2aab18SMatthew Ahrens 	if (error != 0)
25903b2aab18SMatthew Ahrens 		goto out;
2591a9799022Sck 
25923b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
2593c1379625SJustin T. Gibbs 	ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
2594c1379625SJustin T. Gibbs 	error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
2595c1379625SJustin T. Gibbs 	    dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
25963b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
25973b2aab18SMatthew Ahrens 	if (error != 0)
25983b2aab18SMatthew Ahrens 		goto out;
2599379c004dSEric Schrock 
2600c1379625SJustin T. Gibbs 	if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
26013b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
2602c1379625SJustin T. Gibbs 		    dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
26033b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
26043b2aab18SMatthew Ahrens 		if (error != 0)
26053b2aab18SMatthew Ahrens 			goto out;
2606379c004dSEric Schrock 	}
26073b2aab18SMatthew Ahrens out:
26083b2aab18SMatthew Ahrens 	if (error != 0)
26093b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
26103b2aab18SMatthew Ahrens 	return (error);
2611a9799022Sck }
2612a9799022Sck 
2613a9799022Sck static void
26143b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2615a9799022Sck {
26163b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
26173b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
26183b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
26193b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
26203b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
26213b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
26223b2aab18SMatthew Ahrens }
262302c8f3f0SMatthew Ahrens 
26243b2aab18SMatthew Ahrens /*
26253b2aab18SMatthew Ahrens  * Promote a clone.
26263b2aab18SMatthew Ahrens  *
26273b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
26283b2aab18SMatthew Ahrens  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
26293b2aab18SMatthew Ahrens  */
26303b2aab18SMatthew Ahrens int
26313b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap)
26323b2aab18SMatthew Ahrens {
26333b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
26343b2aab18SMatthew Ahrens 	uint64_t numsnaps;
26353b2aab18SMatthew Ahrens 	int error;
26363b2aab18SMatthew Ahrens 	objset_t *os;
263792241e0bSTom Erickson 
26383b2aab18SMatthew Ahrens 	/*
26393b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
26403b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
26413b2aab18SMatthew Ahrens 	 */
26423b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
26433b2aab18SMatthew Ahrens 	if (error != 0)
26443b2aab18SMatthew Ahrens 		return (error);
26453b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2646c1379625SJustin T. Gibbs 	    dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2647c1379625SJustin T. Gibbs 	    &numsnaps);
26483b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
26493b2aab18SMatthew Ahrens 	if (error != 0)
26503b2aab18SMatthew Ahrens 		return (error);
265102c8f3f0SMatthew Ahrens 
26523b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
26533b2aab18SMatthew Ahrens 	ddpa.err_ds = conflsnap;
2654a2afb611SJerry Jelinek 	ddpa.cr = CRED();
265502c8f3f0SMatthew Ahrens 
26563b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_dataset_promote_check,
26577d46dc6cSMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa,
26587d46dc6cSMatthew Ahrens 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2659a9799022Sck }
2660a9799022Sck 
2661a9799022Sck int
26623b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
266391948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2664a9799022Sck {
26653b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2666a9799022Sck 
26673b2aab18SMatthew Ahrens 	/* they should both be heads */
2668bc9014e6SJustin Gibbs 	if (clone->ds_is_snapshot ||
2669bc9014e6SJustin Gibbs 	    origin_head->ds_is_snapshot)
2670be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
267192241e0bSTom Erickson 
267234f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
267334f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
2674be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2675a9799022Sck 
26763b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
26773b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
26783b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
267934f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2680be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
268192241e0bSTom Erickson 
26823b2aab18SMatthew Ahrens 	/* the clone should be a child of the origin */
26833b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2684be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2685842727c2SChris Kirby 
26863b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
268734f2f8cfSMatthew Ahrens 	if (!force &&
268834f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2689be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
2690c99e4bdcSChris Kirby 
26913b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
269291948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2693be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
26943b2aab18SMatthew Ahrens 
26953b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
26963b2aab18SMatthew Ahrens 	unused_refres_delta =
26973b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2698c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
26993b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2700c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
27013b2aab18SMatthew Ahrens 
27023b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
27033b2aab18SMatthew Ahrens 	    unused_refres_delta >
27043b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2705be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
27063b2aab18SMatthew Ahrens 
27073b2aab18SMatthew Ahrens 	/* clone can't be over the head's refquota */
27083b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
2709c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes >
2710c1379625SJustin T. Gibbs 	    origin_head->ds_quota)
2711be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2712c99e4bdcSChris Kirby 
27133b2aab18SMatthew Ahrens 	return (0);
2714c99e4bdcSChris Kirby }
2715c99e4bdcSChris Kirby 
2716a7f53a56SChris Kirby void
27173b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
27183b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2719a7f53a56SChris Kirby {
27203b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
27213b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2722a7f53a56SChris Kirby 
27233b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
27243b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
2725c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota);
272634f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2727842727c2SChris Kirby 
2728ca0cc391SMatthew Ahrens 	/*
2729ca0cc391SMatthew Ahrens 	 * Swap per-dataset feature flags.
2730ca0cc391SMatthew Ahrens 	 */
2731ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2732ca0cc391SMatthew Ahrens 		if (!(spa_feature_table[f].fi_flags &
2733ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET)) {
2734ca0cc391SMatthew Ahrens 			ASSERT(!clone->ds_feature_inuse[f]);
2735ca0cc391SMatthew Ahrens 			ASSERT(!origin_head->ds_feature_inuse[f]);
2736ca0cc391SMatthew Ahrens 			continue;
2737ca0cc391SMatthew Ahrens 		}
2738ca0cc391SMatthew Ahrens 
2739ca0cc391SMatthew Ahrens 		boolean_t clone_inuse = clone->ds_feature_inuse[f];
2740ca0cc391SMatthew Ahrens 		boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
2741ca0cc391SMatthew Ahrens 
2742ca0cc391SMatthew Ahrens 		if (clone_inuse) {
2743ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
2744ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_FALSE;
2745ca0cc391SMatthew Ahrens 		}
2746ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
2747ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(origin_head->ds_object,
2748ca0cc391SMatthew Ahrens 			    f, tx);
2749ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_FALSE;
2750ca0cc391SMatthew Ahrens 		}
2751ca0cc391SMatthew Ahrens 		if (clone_inuse) {
2752ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(origin_head->ds_object,
2753ca0cc391SMatthew Ahrens 			    f, tx);
2754ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_TRUE;
2755ca0cc391SMatthew Ahrens 		}
2756ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
2757ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(clone->ds_object, f, tx);
2758ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_TRUE;
2759ca0cc391SMatthew Ahrens 		}
2760ca0cc391SMatthew Ahrens 	}
2761ca0cc391SMatthew Ahrens 
27623b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
27633b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2764842727c2SChris Kirby 
27653b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
27663b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
27673b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
27683b2aab18SMatthew Ahrens 	}
2769842727c2SChris Kirby 
27703b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
27713b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
27723b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
2773842727c2SChris Kirby 	}
2774842727c2SChris Kirby 
27753b2aab18SMatthew Ahrens 	unused_refres_delta =
27763b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2777c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
27783b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2779c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
27803b2aab18SMatthew Ahrens 
27813b2aab18SMatthew Ahrens 	/*
27823b2aab18SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
27833b2aab18SMatthew Ahrens 	 */
27843b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
27853b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
27863b2aab18SMatthew Ahrens 		uint64_t comp, uncomp;
27873b2aab18SMatthew Ahrens 
27883b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
27893b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
2790c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
2791c1379625SJustin T. Gibbs 		    &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
27923b2aab18SMatthew Ahrens 	}
27933b2aab18SMatthew Ahrens 
27943b2aab18SMatthew Ahrens 	/* swap blkptrs */
27953b2aab18SMatthew Ahrens 	{
27963b2aab18SMatthew Ahrens 		blkptr_t tmp;
2797c1379625SJustin T. Gibbs 		tmp = dsl_dataset_phys(origin_head)->ds_bp;
2798c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_bp =
2799c1379625SJustin T. Gibbs 		    dsl_dataset_phys(clone)->ds_bp;
2800c1379625SJustin T. Gibbs 		dsl_dataset_phys(clone)->ds_bp = tmp;
28013b2aab18SMatthew Ahrens 	}
28023b2aab18SMatthew Ahrens 
28033b2aab18SMatthew Ahrens 	/* set dd_*_bytes */
28043b2aab18SMatthew Ahrens 	{
28053b2aab18SMatthew Ahrens 		int64_t dused, dcomp, duncomp;
28063b2aab18SMatthew Ahrens 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
28073b2aab18SMatthew Ahrens 		uint64_t odl_used, odl_comp, odl_uncomp;
28083b2aab18SMatthew Ahrens 
2809c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dir_phys(clone->ds_dir)->
28103b2aab18SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
28113b2aab18SMatthew Ahrens 
28123b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
28133b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
28143b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
28153b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
281615508ac0SChris Kirby 
2817c1379625SJustin T. Gibbs 		dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
2818c1379625SJustin T. Gibbs 		    cdl_used -
2819c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
2820c1379625SJustin T. Gibbs 		    odl_used);
2821c1379625SJustin T. Gibbs 		dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
2822c1379625SJustin T. Gibbs 		    cdl_comp -
2823c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
2824c1379625SJustin T. Gibbs 		    odl_comp);
2825c1379625SJustin T. Gibbs 		duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
28263b2aab18SMatthew Ahrens 		    cdl_uncomp -
2827c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
2828c1379625SJustin T. Gibbs 		    odl_uncomp);
2829842727c2SChris Kirby 
28303b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
28313b2aab18SMatthew Ahrens 		    dused, dcomp, duncomp, tx);
28323b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
28333b2aab18SMatthew Ahrens 		    -dused, -dcomp, -duncomp, tx);
2834842727c2SChris Kirby 
2835842727c2SChris Kirby 		/*
28363b2aab18SMatthew Ahrens 		 * The difference in the space used by snapshots is the
28373b2aab18SMatthew Ahrens 		 * difference in snapshot space due to the head's
28383b2aab18SMatthew Ahrens 		 * deadlist (since that's the only thing that's
28393b2aab18SMatthew Ahrens 		 * changing that affects the snapused).
2840842727c2SChris Kirby 		 */
28413b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
28423b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
28433b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
28443b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
28453b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
28463b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
28473b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
28483b2aab18SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
2849842727c2SChris Kirby 	}
2850842727c2SChris Kirby 
28513b2aab18SMatthew Ahrens 	/* swap ds_*_bytes */
2852c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
2853c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes);
2854c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
2855c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_compressed_bytes);
2856c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
2857c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_uncompressed_bytes);
2858c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
2859c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
2860842727c2SChris Kirby 
28613b2aab18SMatthew Ahrens 	/* apply any parent delta for change in unconsumed refreservation */
28623b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
28633b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
2864ca45db41SChris Kirby 
28653b2aab18SMatthew Ahrens 	/*
28663b2aab18SMatthew Ahrens 	 * Swap deadlists.
28673b2aab18SMatthew Ahrens 	 */
28683b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
28693b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
2870c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
2871c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
28723b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2873c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
28743b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2875c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_deadlist_obj);
2876842727c2SChris Kirby 
28773b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2878842727c2SChris Kirby 
28793b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
28803b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
2881842727c2SChris Kirby }
2882842727c2SChris Kirby 
28833b2aab18SMatthew Ahrens /*
28843b2aab18SMatthew Ahrens  * Given a pool name and a dataset object number in that pool,
28853b2aab18SMatthew Ahrens  * return the name of that dataset.
28863b2aab18SMatthew Ahrens  */
2887a7f53a56SChris Kirby int
28883b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2889a7f53a56SChris Kirby {
28903b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
28913b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2892a7f53a56SChris Kirby 	int error;
2893a7f53a56SChris Kirby 
28943b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
28953b2aab18SMatthew Ahrens 	if (error != 0)
28963b2aab18SMatthew Ahrens 		return (error);
28973b2aab18SMatthew Ahrens 
28983b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
28993b2aab18SMatthew Ahrens 	if (error == 0) {
29003b2aab18SMatthew Ahrens 		dsl_dataset_name(ds, buf);
29013b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
29023b2aab18SMatthew Ahrens 	}
29033b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
2904a7f53a56SChris Kirby 
2905a7f53a56SChris Kirby 	return (error);
2906a7f53a56SChris Kirby }
2907a7f53a56SChris Kirby 
2908842727c2SChris Kirby int
29093b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
29103b2aab18SMatthew Ahrens     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2911842727c2SChris Kirby {
29123b2aab18SMatthew Ahrens 	int error = 0;
2913842727c2SChris Kirby 
29143b2aab18SMatthew Ahrens 	ASSERT3S(asize, >, 0);
2915842727c2SChris Kirby 
29163b2aab18SMatthew Ahrens 	/*
29173b2aab18SMatthew Ahrens 	 * *ref_rsrv is the portion of asize that will come from any
29183b2aab18SMatthew Ahrens 	 * unconsumed refreservation space.
29193b2aab18SMatthew Ahrens 	 */
29203b2aab18SMatthew Ahrens 	*ref_rsrv = 0;
2921842727c2SChris Kirby 
29223b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
29233b2aab18SMatthew Ahrens 	/*
29243b2aab18SMatthew Ahrens 	 * Make a space adjustment for reserved bytes.
29253b2aab18SMatthew Ahrens 	 */
2926c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
29273b2aab18SMatthew Ahrens 		ASSERT3U(*used, >=,
2928c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
2929c1379625SJustin T. Gibbs 		*used -=
2930c1379625SJustin T. Gibbs 		    (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
29313b2aab18SMatthew Ahrens 		*ref_rsrv =
29323b2aab18SMatthew Ahrens 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
2933842727c2SChris Kirby 	}
2934842727c2SChris Kirby 
29353b2aab18SMatthew Ahrens 	if (!check_quota || ds->ds_quota == 0) {
29363b2aab18SMatthew Ahrens 		mutex_exit(&ds->ds_lock);
29373b2aab18SMatthew Ahrens 		return (0);
2938842727c2SChris Kirby 	}
29393b2aab18SMatthew Ahrens 	/*
29403b2aab18SMatthew Ahrens 	 * If they are requesting more space, and our current estimate
29413b2aab18SMatthew Ahrens 	 * is over quota, they get to try again unless the actual
29423b2aab18SMatthew Ahrens 	 * on-disk is over quota and there are no pending changes (which
29433b2aab18SMatthew Ahrens 	 * may free up space for us).
29443b2aab18SMatthew Ahrens 	 */
2945c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
2946c1379625SJustin T. Gibbs 	    ds->ds_quota) {
29473b2aab18SMatthew Ahrens 		if (inflight > 0 ||
2948c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
2949be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
29503b2aab18SMatthew Ahrens 		else
2951be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
2952842727c2SChris Kirby 	}
29533b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2954842727c2SChris Kirby 
2955842727c2SChris Kirby 	return (error);
2956842727c2SChris Kirby }
2957842727c2SChris Kirby 
29583b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
29593b2aab18SMatthew Ahrens 	const char *ddsqra_name;
29603b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
29613b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
29623b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
2963842727c2SChris Kirby 
29643b2aab18SMatthew Ahrens 
29653b2aab18SMatthew Ahrens /* ARGSUSED */
2966842727c2SChris Kirby static int
29673b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2968842727c2SChris Kirby {
29693b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
29703b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29713b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2972842727c2SChris Kirby 	int error;
29733b2aab18SMatthew Ahrens 	uint64_t newval;
2974842727c2SChris Kirby 
29753b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2976be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
2977842727c2SChris Kirby 
29783b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
29793b2aab18SMatthew Ahrens 	if (error != 0)
29803b2aab18SMatthew Ahrens 		return (error);
29813b2aab18SMatthew Ahrens 
2982bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
29833b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2984be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2985842727c2SChris Kirby 	}
2986842727c2SChris Kirby 
29873b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
29883b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
29893b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
29903b2aab18SMatthew Ahrens 	if (error != 0) {
29913b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2992842727c2SChris Kirby 		return (error);
2993842727c2SChris Kirby 	}
2994842727c2SChris Kirby 
29953b2aab18SMatthew Ahrens 	if (newval == 0) {
29963b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
29973b2aab18SMatthew Ahrens 		return (0);
29983b2aab18SMatthew Ahrens 	}
2999842727c2SChris Kirby 
3000c1379625SJustin T. Gibbs 	if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
30013b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
30023b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3003be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
30043b2aab18SMatthew Ahrens 	}
30053b2aab18SMatthew Ahrens 
30063b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3007842727c2SChris Kirby 	return (0);
3008842727c2SChris Kirby }
3009842727c2SChris Kirby 
30103b2aab18SMatthew Ahrens static void
30113b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3012842727c2SChris Kirby {
30133b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
30143b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
30153b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
30163b2aab18SMatthew Ahrens 	uint64_t newval;
3017842727c2SChris Kirby 
30183b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3019842727c2SChris Kirby 
30203b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
30213b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
30223b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
30233b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
3024842727c2SChris Kirby 
30253b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
30263b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
3027842727c2SChris Kirby 
30283b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
30293b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
30303b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
3031842727c2SChris Kirby 	}
30323b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3033842727c2SChris Kirby }
3034842727c2SChris Kirby 
30353b2aab18SMatthew Ahrens int
30363b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
30373b2aab18SMatthew Ahrens     uint64_t refquota)
3038842727c2SChris Kirby {
30393b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3040842727c2SChris Kirby 
30413b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
30423b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
30433b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
30443b2aab18SMatthew Ahrens 
30453b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
30467d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
3047842727c2SChris Kirby }
3048842727c2SChris Kirby 
3049842727c2SChris Kirby static int
30503b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3051842727c2SChris Kirby {
30523b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
30533b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3054842727c2SChris Kirby 	dsl_dataset_t *ds;
3055842727c2SChris Kirby 	int error;
30563b2aab18SMatthew Ahrens 	uint64_t newval, unique;
3057d7747cbcSChris Kirby 
30583b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3059be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3060842727c2SChris Kirby 
30613b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
30623b2aab18SMatthew Ahrens 	if (error != 0)
3063842727c2SChris Kirby 		return (error);
3064842727c2SChris Kirby 
3065bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
30663b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3067be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3068842727c2SChris Kirby 	}
3069842727c2SChris Kirby 
30703b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
30713b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
30723b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
30733b2aab18SMatthew Ahrens 	if (error != 0) {
30743b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3075842727c2SChris Kirby 		return (error);
3076842727c2SChris Kirby 	}
3077842727c2SChris Kirby 
30783b2aab18SMatthew Ahrens 	/*
30793b2aab18SMatthew Ahrens 	 * If we are doing the preliminary check in open context, the
30803b2aab18SMatthew Ahrens 	 * space estimates may be inaccurate.
30813b2aab18SMatthew Ahrens 	 */
30823b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
30833b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
30843b2aab18SMatthew Ahrens 		return (0);
3085842727c2SChris Kirby 	}
3086842727c2SChris Kirby 
30873b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
30883b2aab18SMatthew Ahrens 	if (!DS_UNIQUE_IS_ACCURATE(ds))
30893b2aab18SMatthew Ahrens 		dsl_dataset_recalc_head_uniq(ds);
3090c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
30913b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3092842727c2SChris Kirby 
30933b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
30943b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
30953b2aab18SMatthew Ahrens 		    MAX(unique, ds->ds_reserved);
3096842727c2SChris Kirby 
30973b2aab18SMatthew Ahrens 		if (delta >
30983b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
30993b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
31003b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
3101be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
31023b2aab18SMatthew Ahrens 		}
3103842727c2SChris Kirby 	}
3104842727c2SChris Kirby 
31053b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
31063b2aab18SMatthew Ahrens 	return (0);
3107842727c2SChris Kirby }
3108842727c2SChris Kirby 
31093b2aab18SMatthew Ahrens void
31103b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
31113b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3112ca45db41SChris Kirby {
31133b2aab18SMatthew Ahrens 	uint64_t newval;
31143b2aab18SMatthew Ahrens 	uint64_t unique;
31153b2aab18SMatthew Ahrens 	int64_t delta;
3116ca45db41SChris Kirby 
31173b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
31183b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
3119ca45db41SChris Kirby 
31203b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
31213b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
3122a7f53a56SChris Kirby 
31233b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
31243b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
31253b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
31263b2aab18SMatthew Ahrens 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3127c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
31283b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
31293b2aab18SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
31303b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
31313b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3132a7f53a56SChris Kirby 
31333b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
31343b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
3135ca45db41SChris Kirby }
3136ca45db41SChris Kirby 
31373b2aab18SMatthew Ahrens static void
31383b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3139842727c2SChris Kirby {
31403b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
31413b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3142842727c2SChris Kirby 	dsl_dataset_t *ds;
3143842727c2SChris Kirby 
31443b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
31453b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
31463b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3147842727c2SChris Kirby 	dsl_dataset_rele(ds, FTAG);
3148842727c2SChris Kirby }
3149503ad85cSMatthew Ahrens 
3150503ad85cSMatthew Ahrens int
31513b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
31523b2aab18SMatthew Ahrens     uint64_t refreservation)
3153503ad85cSMatthew Ahrens {
31543b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3155503ad85cSMatthew Ahrens 
31563b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
31573b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
31583b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
31593b2aab18SMatthew Ahrens 
31603b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
31617d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refreservation_sync, &ddsqra,
31627d46dc6cSMatthew Ahrens 	    0, ZFS_SPACE_CHECK_NONE));
3163503ad85cSMatthew Ahrens }
316419b94df9SMatthew Ahrens 
316519b94df9SMatthew Ahrens /*
316619b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
316719b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
316819b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
316919b94df9SMatthew Ahrens  * fail and return EINVAL.
317019b94df9SMatthew Ahrens  *
317119b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
317219b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
317319b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
317419b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
317519b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
317619b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
317719b94df9SMatthew Ahrens  *
317819b94df9SMatthew Ahrens  * space freed                         [---------------------]
317919b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
318019b94df9SMatthew Ahrens  *                                         oldsnap            new
318119b94df9SMatthew Ahrens  */
318219b94df9SMatthew Ahrens int
318319b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
318419b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
318519b94df9SMatthew Ahrens {
318619b94df9SMatthew Ahrens 	int err = 0;
318719b94df9SMatthew Ahrens 	uint64_t snapobj;
318819b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
318919b94df9SMatthew Ahrens 
31903b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
31913b2aab18SMatthew Ahrens 
319219b94df9SMatthew Ahrens 	*usedp = 0;
3193c1379625SJustin T. Gibbs 	*usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3194c1379625SJustin T. Gibbs 	*usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
319519b94df9SMatthew Ahrens 
319619b94df9SMatthew Ahrens 	*compp = 0;
3197c1379625SJustin T. Gibbs 	*compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3198c1379625SJustin T. Gibbs 	*compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
319919b94df9SMatthew Ahrens 
320019b94df9SMatthew Ahrens 	*uncompp = 0;
3201c1379625SJustin T. Gibbs 	*uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3202c1379625SJustin T. Gibbs 	*uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
320319b94df9SMatthew Ahrens 
320419b94df9SMatthew Ahrens 	snapobj = new->ds_object;
320519b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
320619b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
320719b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
320819b94df9SMatthew Ahrens 
3209ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
3210ad135b5dSChristopher Siden 			snap = new;
3211ad135b5dSChristopher Siden 		} else {
3212ad135b5dSChristopher Siden 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3213ad135b5dSChristopher Siden 			if (err != 0)
3214ad135b5dSChristopher Siden 				break;
3215ad135b5dSChristopher Siden 		}
321619b94df9SMatthew Ahrens 
3217c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3218c1379625SJustin T. Gibbs 		    dsl_dataset_phys(oldsnap)->ds_creation_txg) {
321919b94df9SMatthew Ahrens 			/*
322019b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
322119b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
322219b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
322319b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
322419b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
322519b94df9SMatthew Ahrens 			 * optimization itself.
322619b94df9SMatthew Ahrens 			 */
322719b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
322819b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
322919b94df9SMatthew Ahrens 		} else {
323019b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
3231c1379625SJustin T. Gibbs 			    0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
323219b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
323319b94df9SMatthew Ahrens 		}
323419b94df9SMatthew Ahrens 		*usedp += used;
323519b94df9SMatthew Ahrens 		*compp += comp;
323619b94df9SMatthew Ahrens 		*uncompp += uncomp;
323719b94df9SMatthew Ahrens 
323819b94df9SMatthew Ahrens 		/*
323919b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
324019b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
324119b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
324219b94df9SMatthew Ahrens 		 */
3243c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3244ad135b5dSChristopher Siden 		if (snap != new)
3245ad135b5dSChristopher Siden 			dsl_dataset_rele(snap, FTAG);
324619b94df9SMatthew Ahrens 		if (snapobj == 0) {
3247be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
324819b94df9SMatthew Ahrens 			break;
324919b94df9SMatthew Ahrens 		}
325019b94df9SMatthew Ahrens 
325119b94df9SMatthew Ahrens 	}
325219b94df9SMatthew Ahrens 	return (err);
325319b94df9SMatthew Ahrens }
325419b94df9SMatthew Ahrens 
325519b94df9SMatthew Ahrens /*
325619b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
325719b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
325819b94df9SMatthew Ahrens  *
325919b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
326019b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
326119b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
326219b94df9SMatthew Ahrens  *
326319b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
326419b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
326519b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
326619b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
326719b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
326819b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
326919b94df9SMatthew Ahrens  */
327019b94df9SMatthew Ahrens int
327119b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
327219b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
327319b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
327419b94df9SMatthew Ahrens {
327519b94df9SMatthew Ahrens 	int err = 0;
327619b94df9SMatthew Ahrens 	uint64_t snapobj;
327719b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
327819b94df9SMatthew Ahrens 
3279bc9014e6SJustin Gibbs 	ASSERT(firstsnap->ds_is_snapshot);
3280bc9014e6SJustin Gibbs 	ASSERT(lastsnap->ds_is_snapshot);
328119b94df9SMatthew Ahrens 
328219b94df9SMatthew Ahrens 	/*
328319b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
328419b94df9SMatthew Ahrens 	 * is before lastsnap.
328519b94df9SMatthew Ahrens 	 */
328619b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
3287c1379625SJustin T. Gibbs 	    dsl_dataset_phys(firstsnap)->ds_creation_txg >
3288c1379625SJustin T. Gibbs 	    dsl_dataset_phys(lastsnap)->ds_creation_txg)
3289be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
329019b94df9SMatthew Ahrens 
329119b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
329219b94df9SMatthew Ahrens 
3293c1379625SJustin T. Gibbs 	snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
329419b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
329519b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
329619b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
329719b94df9SMatthew Ahrens 
329819b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
329919b94df9SMatthew Ahrens 		if (err != 0)
330019b94df9SMatthew Ahrens 			break;
330119b94df9SMatthew Ahrens 
330219b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
3303c1379625SJustin T. Gibbs 		    dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
330419b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
330519b94df9SMatthew Ahrens 		*usedp += used;
330619b94df9SMatthew Ahrens 		*compp += comp;
330719b94df9SMatthew Ahrens 		*uncompp += uncomp;
330819b94df9SMatthew Ahrens 
3309c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
331019b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
331119b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
331219b94df9SMatthew Ahrens 	}
331319b94df9SMatthew Ahrens 	return (err);
331419b94df9SMatthew Ahrens }
33153b2aab18SMatthew Ahrens 
33163b2aab18SMatthew Ahrens /*
33173b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
33183b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
33193b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
33203b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
33213b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
332278f17100SMatthew Ahrens  *
332378f17100SMatthew Ahrens  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
33243b2aab18SMatthew Ahrens  */
33253b2aab18SMatthew Ahrens boolean_t
332678f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
332778f17100SMatthew Ahrens 	uint64_t earlier_txg)
33283b2aab18SMatthew Ahrens {
33293b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
33303b2aab18SMatthew Ahrens 	int error;
33313b2aab18SMatthew Ahrens 	boolean_t ret;
33323b2aab18SMatthew Ahrens 
33333b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
3334bc9014e6SJustin Gibbs 	ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
333578f17100SMatthew Ahrens 
333678f17100SMatthew Ahrens 	if (earlier_txg == 0)
3337c1379625SJustin T. Gibbs 		earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
33383b2aab18SMatthew Ahrens 
3339bc9014e6SJustin Gibbs 	if (later->ds_is_snapshot &&
3340c1379625SJustin T. Gibbs 	    earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
33413b2aab18SMatthew Ahrens 		return (B_FALSE);
33423b2aab18SMatthew Ahrens 
33433b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
33443b2aab18SMatthew Ahrens 		return (B_TRUE);
33453b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
33463b2aab18SMatthew Ahrens 		return (B_FALSE);
33473b2aab18SMatthew Ahrens 
3348c1379625SJustin T. Gibbs 	if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
33493b2aab18SMatthew Ahrens 		return (B_TRUE);
33503b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
33513b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
3352c1379625SJustin T. Gibbs 	    dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
33533b2aab18SMatthew Ahrens 	if (error != 0)
33543b2aab18SMatthew Ahrens 		return (B_FALSE);
335578f17100SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
33563b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
33573b2aab18SMatthew Ahrens 	return (ret);
33583b2aab18SMatthew Ahrens }
33592acef22dSMatthew Ahrens 
33602acef22dSMatthew Ahrens void
33612acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
33622acef22dSMatthew Ahrens {
33632acef22dSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
33642acef22dSMatthew Ahrens 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
33652acef22dSMatthew Ahrens }
3366