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.
2342fcb65eSMatthew Ahrens  * Copyright (c) 2011, 2014 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>
53e1930233Sbonwick 
54b5152584SMatthew Ahrens /*
55b5152584SMatthew Ahrens  * The SPA supports block sizes up to 16MB.  However, very large blocks
56b5152584SMatthew Ahrens  * can have an impact on i/o latency (e.g. tying up a spinning disk for
57b5152584SMatthew Ahrens  * ~300ms), and also potentially on the memory allocator.  Therefore,
58b5152584SMatthew Ahrens  * we do not allow the recordsize to be set larger than zfs_max_recordsize
59b5152584SMatthew Ahrens  * (default 1MB).  Larger blocks can be created by changing this tunable,
60b5152584SMatthew Ahrens  * and pools with larger blocks can always be imported and used, regardless
61b5152584SMatthew Ahrens  * of this setting.
62b5152584SMatthew Ahrens  */
63b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024;
64b5152584SMatthew Ahrens 
65cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
66cde58dbcSMatthew Ahrens 	{ \
67cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
68cde58dbcSMatthew Ahrens 		(x) = (y); \
69cde58dbcSMatthew Ahrens 		(y) = __tmp; \
70cde58dbcSMatthew Ahrens 	}
71cde58dbcSMatthew Ahrens 
7255434c77Sek #define	DS_REF_MAX	(1ULL << 62)
73fa9e4066Sahrens 
74c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
75c1379625SJustin T. Gibbs 
76a9799022Sck /*
77a9799022Sck  * Figure out how much of this delta should be propogated to the dsl_dir
78a9799022Sck  * layer.  If there's a refreservation, that space has already been
79a9799022Sck  * partially accounted for in our ancestors.
80a9799022Sck  */
81a9799022Sck static int64_t
82a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta)
83a9799022Sck {
84c1379625SJustin T. Gibbs 	dsl_dataset_phys_t *ds_phys;
85a9799022Sck 	uint64_t old_bytes, new_bytes;
86a9799022Sck 
87a9799022Sck 	if (ds->ds_reserved == 0)
88a9799022Sck 		return (delta);
89a9799022Sck 
90c1379625SJustin T. Gibbs 	ds_phys = dsl_dataset_phys(ds);
91c1379625SJustin T. Gibbs 	old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
92c1379625SJustin T. Gibbs 	new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
93a9799022Sck 
94a9799022Sck 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
95a9799022Sck 	return (new_bytes - old_bytes);
96a9799022Sck }
97fa9e4066Sahrens 
98fa9e4066Sahrens void
99b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
100fa9e4066Sahrens {
101b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
102fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
103fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
104a9799022Sck 	int64_t delta;
105fa9e4066Sahrens 
1063f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
107fa9e4066Sahrens 
108fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
109fa9e4066Sahrens 	/* It could have been compressed away to nothing */
110fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
111fa9e4066Sahrens 		return;
112fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
113ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
114fa9e4066Sahrens 	if (ds == NULL) {
115ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
116ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
117fa9e4066Sahrens 		return;
118fa9e4066Sahrens 	}
1193f9d6ad7SLin Ling 
120b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
121fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
122a9799022Sck 	delta = parent_delta(ds, used);
123c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes += used;
124c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
125c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
126c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes += used;
127b5152584SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE)
128b5152584SMatthew Ahrens 		ds->ds_need_large_blocks = B_TRUE;
129fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
13074e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
13174e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
13274e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
13374e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
134fa9e4066Sahrens }
135fa9e4066Sahrens 
136cdb0ab79Smaybee int
137b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
138b24ab676SJeff Bonwick     boolean_t async)
139fa9e4066Sahrens {
14043466aaeSMax Grossman 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
14143466aaeSMax Grossman 	int compressed = BP_GET_PSIZE(bp);
14243466aaeSMax Grossman 	int uncompressed = BP_GET_UCSIZE(bp);
14343466aaeSMax Grossman 
144fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
145cdb0ab79Smaybee 		return (0);
146fa9e4066Sahrens 
147b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
148b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
149b24ab676SJeff Bonwick 
150fa9e4066Sahrens 	if (ds == NULL) {
151b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
152ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
153ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
154cdb0ab79Smaybee 		return (used);
155fa9e4066Sahrens 	}
156fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
157fa9e4066Sahrens 
158bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
159fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
160fa9e4066Sahrens 
161c1379625SJustin T. Gibbs 	if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
162a9799022Sck 		int64_t delta;
163c717a561Smaybee 
1643f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
165b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
166fa9e4066Sahrens 
167fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
168c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
169a9799022Sck 		    !DS_UNIQUE_IS_ACCURATE(ds));
170a9799022Sck 		delta = parent_delta(ds, -used);
171c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_unique_bytes -= used;
172fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
17374e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
174a9799022Sck 		    delta, -compressed, -uncompressed, tx);
17574e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
17674e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
177fa9e4066Sahrens 	} else {
178fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
179b24ab676SJeff Bonwick 		if (async) {
180b24ab676SJeff Bonwick 			/*
181b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
182b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
183cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
184b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
185b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
186b24ab676SJeff Bonwick 			 */
187cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
188b24ab676SJeff Bonwick 		} else {
189cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
190b24ab676SJeff Bonwick 		}
191a4611edeSahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
192c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj);
193c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
194fa9e4066Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
195c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
196a4611edeSahrens 		    ds->ds_object && bp->blk_birth >
197c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
198a4611edeSahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
199a4611edeSahrens 			mutex_enter(&ds->ds_prev->ds_lock);
200c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
201a4611edeSahrens 			mutex_exit(&ds->ds_prev->ds_lock);
202fa9e4066Sahrens 		}
2033f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
20474e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
20574e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
20674e7dc98SMatthew Ahrens 		}
207fa9e4066Sahrens 	}
208fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
209c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
210c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
211c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
212c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
213c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
214c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
215fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
216cdb0ab79Smaybee 
217cdb0ab79Smaybee 	return (used);
218fa9e4066Sahrens }
219fa9e4066Sahrens 
220ea8dc4b6Seschrock uint64_t
221ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
222fa9e4066Sahrens {
223a2eea2e1Sahrens 	uint64_t trysnap = 0;
224a2eea2e1Sahrens 
225fa9e4066Sahrens 	if (ds == NULL)
226ea8dc4b6Seschrock 		return (0);
227fa9e4066Sahrens 	/*
228fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
229fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
230fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
231fa9e4066Sahrens 	 * consume, which is OK.
232fa9e4066Sahrens 	 *
233fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
234fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
235fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
236fa9e4066Sahrens 	 */
237a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
238a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
239a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
240c1379625SJustin T. Gibbs 	return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap));
241ea8dc4b6Seschrock }
242ea8dc4b6Seschrock 
2433d692628SSanjeev Bagewadi boolean_t
244c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
245c7cd2421SGeorge Wilson     uint64_t blk_birth)
246ea8dc4b6Seschrock {
24743466aaeSMax Grossman 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
24843466aaeSMax Grossman 	    (bp != NULL && BP_IS_HOLE(bp)))
249c7cd2421SGeorge Wilson 		return (B_FALSE);
250c7cd2421SGeorge Wilson 
251837b568bSGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
252c7cd2421SGeorge Wilson 
253c7cd2421SGeorge Wilson 	return (B_TRUE);
254fa9e4066Sahrens }
255fa9e4066Sahrens 
256fa9e4066Sahrens static void
257bc9014e6SJustin Gibbs dsl_dataset_evict(void *dbu)
258fa9e4066Sahrens {
259bc9014e6SJustin Gibbs 	dsl_dataset_t *ds = dbu;
260fa9e4066Sahrens 
2613b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
262fa9e4066Sahrens 
263bc9014e6SJustin Gibbs 	ds->ds_dbuf = NULL;
264bc9014e6SJustin Gibbs 
26591ebeef5Sahrens 	unique_remove(ds->ds_fsid_guid);
266fa9e4066Sahrens 
267503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
268503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	if (ds->ds_prev) {
2713b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
272fa9e4066Sahrens 		ds->ds_prev = NULL;
273fa9e4066Sahrens 	}
274fa9e4066Sahrens 
275cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
276bc9014e6SJustin Gibbs 	if (ds->ds_deadlist.dl_os != NULL)
277cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
278745cd3c5Smaybee 	if (ds->ds_dir)
279bc9014e6SJustin Gibbs 		dsl_dir_async_rele(ds->ds_dir, ds);
280fa9e4066Sahrens 
28191ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
282fa9e4066Sahrens 
2835ad82045Snd 	mutex_destroy(&ds->ds_lock);
28491ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
285d2b3cbbdSJorgen Lundman 	mutex_destroy(&ds->ds_sendstream_lock);
2863b2aab18SMatthew Ahrens 	refcount_destroy(&ds->ds_longholds);
2875ad82045Snd 
288fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
289fa9e4066Sahrens }
290fa9e4066Sahrens 
2913b2aab18SMatthew Ahrens int
292fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
293fa9e4066Sahrens {
294fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
295fa9e4066Sahrens 	int err;
296fa9e4066Sahrens 	dmu_buf_t *headdbuf;
297fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
298fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
299fa9e4066Sahrens 
300fa9e4066Sahrens 	if (ds->ds_snapname[0])
301ea8dc4b6Seschrock 		return (0);
302c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
303ea8dc4b6Seschrock 		return (0);
304fa9e4066Sahrens 
305c1379625SJustin T. Gibbs 	err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
306ea8dc4b6Seschrock 	    FTAG, &headdbuf);
3073b2aab18SMatthew Ahrens 	if (err != 0)
308ea8dc4b6Seschrock 		return (err);
309fa9e4066Sahrens 	headphys = headdbuf->db_data;
310fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
311e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
312ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
313ea8dc4b6Seschrock 	return (err);
314fa9e4066Sahrens }
315fa9e4066Sahrens 
3163b2aab18SMatthew Ahrens int
317745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
318ab04eb8eStimh {
319745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
320c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
321ab04eb8eStimh 	matchtype_t mt;
322ab04eb8eStimh 	int err;
323ab04eb8eStimh 
324c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
325ab04eb8eStimh 		mt = MT_FIRST;
326ab04eb8eStimh 	else
327ab04eb8eStimh 		mt = MT_EXACT;
328ab04eb8eStimh 
329745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
330ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
331ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
332745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
333ab04eb8eStimh 	return (err);
334ab04eb8eStimh }
335ab04eb8eStimh 
3363b2aab18SMatthew Ahrens int
337a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
338a2afb611SJerry Jelinek     boolean_t adj_cnt)
339ab04eb8eStimh {
340745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
341c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
342ab04eb8eStimh 	matchtype_t mt;
343ab04eb8eStimh 	int err;
344ab04eb8eStimh 
34571eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
34671eb0538SChris Kirby 
347c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
348ab04eb8eStimh 		mt = MT_FIRST;
349ab04eb8eStimh 	else
350ab04eb8eStimh 		mt = MT_EXACT;
351ab04eb8eStimh 
352745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
353ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
354745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
355a2afb611SJerry Jelinek 
356a2afb611SJerry Jelinek 	if (err == 0 && adj_cnt)
357a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
358a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
359a2afb611SJerry Jelinek 
360ab04eb8eStimh 	return (err);
361ab04eb8eStimh }
362ab04eb8eStimh 
363e57a022bSJustin T. Gibbs boolean_t
364e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
365e57a022bSJustin T. Gibbs {
366*9d47dec0SJustin T. Gibbs 	dmu_buf_t *dbuf = ds->ds_dbuf;
367*9d47dec0SJustin T. Gibbs 	boolean_t result = B_FALSE;
368*9d47dec0SJustin T. Gibbs 
369*9d47dec0SJustin T. Gibbs 	if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
370*9d47dec0SJustin T. Gibbs 	    ds->ds_object, DMU_BONUS_BLKID, tag)) {
371*9d47dec0SJustin T. Gibbs 
372*9d47dec0SJustin T. Gibbs 		if (ds == dmu_buf_get_user(dbuf))
373*9d47dec0SJustin T. Gibbs 			result = B_TRUE;
374*9d47dec0SJustin T. Gibbs 		else
375*9d47dec0SJustin T. Gibbs 			dmu_buf_rele(dbuf, tag);
376*9d47dec0SJustin T. Gibbs 	}
377*9d47dec0SJustin T. Gibbs 
378*9d47dec0SJustin T. Gibbs 	return (result);
379e57a022bSJustin T. Gibbs }
380e57a022bSJustin T. Gibbs 
3813b2aab18SMatthew Ahrens int
3823b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
383745cd3c5Smaybee     dsl_dataset_t **dsp)
384fa9e4066Sahrens {
385fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
386fa9e4066Sahrens 	dmu_buf_t *dbuf;
387fa9e4066Sahrens 	dsl_dataset_t *ds;
388ea8dc4b6Seschrock 	int err;
389a7f53a56SChris Kirby 	dmu_object_info_t doi;
390fa9e4066Sahrens 
3913b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
392fa9e4066Sahrens 
393ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3943b2aab18SMatthew Ahrens 	if (err != 0)
395ea8dc4b6Seschrock 		return (err);
396a7f53a56SChris Kirby 
397a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
398a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
3992acef22dSMatthew Ahrens 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
400b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
401be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
402b287be1bSWill Andrews 	}
403a7f53a56SChris Kirby 
404fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
405fa9e4066Sahrens 	if (ds == NULL) {
406d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
407fa9e4066Sahrens 
408fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
409fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
410fa9e4066Sahrens 		ds->ds_object = dsobj;
411bc9014e6SJustin Gibbs 		ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
412fa9e4066Sahrens 
4135ad82045Snd 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
41491ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
4154e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
4163b2aab18SMatthew Ahrens 		refcount_create(&ds->ds_longholds);
4175ad82045Snd 
418cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
419cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
420c1379625SJustin T. Gibbs 		    mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
421cde58dbcSMatthew Ahrens 
4224e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4234e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
4244e3c9f44SBill Pijewski 
425b5152584SMatthew Ahrens 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
426b5152584SMatthew Ahrens 			err = zap_contains(mos, dsobj, DS_FIELD_LARGE_BLOCKS);
427b5152584SMatthew Ahrens 			if (err == 0)
428b5152584SMatthew Ahrens 				ds->ds_large_blocks = B_TRUE;
429b5152584SMatthew Ahrens 			else
430b5152584SMatthew Ahrens 				ASSERT3U(err, ==, ENOENT);
431b5152584SMatthew Ahrens 		}
432b5152584SMatthew Ahrens 
433ea8dc4b6Seschrock 		if (err == 0) {
4343b2aab18SMatthew Ahrens 			err = dsl_dir_hold_obj(dp,
435c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds,
436c1379625SJustin T. Gibbs 			    &ds->ds_dir);
437ea8dc4b6Seschrock 		}
4383b2aab18SMatthew Ahrens 		if (err != 0) {
4395ad82045Snd 			mutex_destroy(&ds->ds_lock);
44091ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
441d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
4423b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
443cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
444cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
445ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
446ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
447ea8dc4b6Seschrock 			return (err);
448ea8dc4b6Seschrock 		}
449fa9e4066Sahrens 
450bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
451fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
452c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
4533b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
454c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_prev_snap_obj,
455745cd3c5Smaybee 				    ds, &ds->ds_prev);
456fa9e4066Sahrens 			}
45778f17100SMatthew Ahrens 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
45878f17100SMatthew Ahrens 				int zaperr = zap_lookup(mos, ds->ds_object,
45978f17100SMatthew Ahrens 				    DS_FIELD_BOOKMARK_NAMES,
46078f17100SMatthew Ahrens 				    sizeof (ds->ds_bookmarks), 1,
46178f17100SMatthew Ahrens 				    &ds->ds_bookmarks);
46278f17100SMatthew Ahrens 				if (zaperr != ENOENT)
46378f17100SMatthew Ahrens 					VERIFY0(zaperr);
46478f17100SMatthew Ahrens 			}
465842727c2SChris Kirby 		} else {
466842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
467842727c2SChris Kirby 				err = dsl_dataset_get_snapname(ds);
468c1379625SJustin T. Gibbs 			if (err == 0 &&
469c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
470842727c2SChris Kirby 				err = zap_count(
471842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
472c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_userrefs_obj,
473842727c2SChris Kirby 				    &ds->ds_userrefs);
474842727c2SChris Kirby 			}
475fa9e4066Sahrens 		}
476fa9e4066Sahrens 
477bc9014e6SJustin Gibbs 		if (err == 0 && !ds->ds_is_snapshot) {
4783b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
4793b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4803b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
481cb625fb5Sck 			if (err == 0) {
4823b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
4833b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4843b2aab18SMatthew Ahrens 				    &ds->ds_quota);
485cb625fb5Sck 			}
486cb625fb5Sck 		} else {
487cb625fb5Sck 			ds->ds_reserved = ds->ds_quota = 0;
488cb625fb5Sck 		}
489cb625fb5Sck 
490bc9014e6SJustin Gibbs 		dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict, &ds->ds_dbuf);
491bc9014e6SJustin Gibbs 		if (err == 0)
492bc9014e6SJustin Gibbs 			winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
493bc9014e6SJustin Gibbs 
494bc9014e6SJustin Gibbs 		if (err != 0 || winner != NULL) {
495cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
496cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
497745cd3c5Smaybee 			if (ds->ds_prev)
4983b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
4993b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
5005ad82045Snd 			mutex_destroy(&ds->ds_lock);
50191ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
502d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
5033b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
504fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
5053b2aab18SMatthew Ahrens 			if (err != 0) {
506ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
507ea8dc4b6Seschrock 				return (err);
508ea8dc4b6Seschrock 			}
509fa9e4066Sahrens 			ds = winner;
510fa9e4066Sahrens 		} else {
51191ebeef5Sahrens 			ds->ds_fsid_guid =
512c1379625SJustin T. Gibbs 			    unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
513fa9e4066Sahrens 		}
514fa9e4066Sahrens 	}
515fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
516c1379625SJustin T. Gibbs 	ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
517c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
518afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
51984db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
520ea8dc4b6Seschrock 	*dsp = ds;
521ea8dc4b6Seschrock 	return (0);
522fa9e4066Sahrens }
523fa9e4066Sahrens 
524745cd3c5Smaybee int
5253b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name,
526503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
527fa9e4066Sahrens {
528fa9e4066Sahrens 	dsl_dir_t *dd;
529745cd3c5Smaybee 	const char *snapname;
530fa9e4066Sahrens 	uint64_t obj;
531fa9e4066Sahrens 	int err = 0;
532fa9e4066Sahrens 
5333b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
5343b2aab18SMatthew Ahrens 	if (err != 0)
535ea8dc4b6Seschrock 		return (err);
536fa9e4066Sahrens 
5373b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
538c1379625SJustin T. Gibbs 	obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
5393b2aab18SMatthew Ahrens 	if (obj != 0)
5403b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
541745cd3c5Smaybee 	else
542be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
543fa9e4066Sahrens 
544745cd3c5Smaybee 	/* we may be looking for a snapshot */
545745cd3c5Smaybee 	if (err == 0 && snapname != NULL) {
5463b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
547fa9e4066Sahrens 
548745cd3c5Smaybee 		if (*snapname++ != '@') {
549745cd3c5Smaybee 			dsl_dataset_rele(*dsp, tag);
5503b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
551be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
552fa9e4066Sahrens 		}
553fa9e4066Sahrens 
554745cd3c5Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
555745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
556745cd3c5Smaybee 		if (err == 0)
5573b2aab18SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
558745cd3c5Smaybee 		dsl_dataset_rele(*dsp, tag);
559745cd3c5Smaybee 
5603b2aab18SMatthew Ahrens 		if (err == 0) {
561745cd3c5Smaybee 			mutex_enter(&ds->ds_lock);
562745cd3c5Smaybee 			if (ds->ds_snapname[0] == 0)
563745cd3c5Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
564745cd3c5Smaybee 				    sizeof (ds->ds_snapname));
565745cd3c5Smaybee 			mutex_exit(&ds->ds_lock);
5663b2aab18SMatthew Ahrens 			*dsp = ds;
567fa9e4066Sahrens 		}
568fa9e4066Sahrens 	}
5693b2aab18SMatthew Ahrens 
5703b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
571fa9e4066Sahrens 	return (err);
572fa9e4066Sahrens }
573fa9e4066Sahrens 
574fa9e4066Sahrens int
5753b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
5763b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
5773b2aab18SMatthew Ahrens {
5783b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
5793b2aab18SMatthew Ahrens 	if (err != 0)
5803b2aab18SMatthew Ahrens 		return (err);
5813b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
5823b2aab18SMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
5833b2aab18SMatthew Ahrens 		*dsp = NULL;
584be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
5853b2aab18SMatthew Ahrens 	}
5863b2aab18SMatthew Ahrens 	return (0);
5873b2aab18SMatthew Ahrens }
5883b2aab18SMatthew Ahrens 
5893b2aab18SMatthew Ahrens int
5903b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name,
591503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
592fa9e4066Sahrens {
5933b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold(dp, name, tag, dsp);
5943b2aab18SMatthew Ahrens 	if (err != 0)
595745cd3c5Smaybee 		return (err);
5963b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
597503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
598be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
599745cd3c5Smaybee 	}
600745cd3c5Smaybee 	return (0);
601fa9e4066Sahrens }
602fa9e4066Sahrens 
6033b2aab18SMatthew Ahrens /*
6043b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
6053b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
6063b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
6073b2aab18SMatthew Ahrens  *
6083b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
6093b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
6103b2aab18SMatthew Ahrens  * and accessed.
6113b2aab18SMatthew Ahrens  */
6123b2aab18SMatthew Ahrens void
6133b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
6143b2aab18SMatthew Ahrens {
6153b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
6163b2aab18SMatthew Ahrens 	(void) refcount_add(&ds->ds_longholds, tag);
6173b2aab18SMatthew Ahrens }
6183b2aab18SMatthew Ahrens 
6193b2aab18SMatthew Ahrens void
6203b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
6213b2aab18SMatthew Ahrens {
6223b2aab18SMatthew Ahrens 	(void) refcount_remove(&ds->ds_longholds, tag);
6233b2aab18SMatthew Ahrens }
6243b2aab18SMatthew Ahrens 
6253b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
6263b2aab18SMatthew Ahrens boolean_t
6273b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
6283b2aab18SMatthew Ahrens {
6293b2aab18SMatthew Ahrens 	return (!refcount_is_zero(&ds->ds_longholds));
6303b2aab18SMatthew Ahrens }
6313b2aab18SMatthew Ahrens 
632fa9e4066Sahrens void
633fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
634fa9e4066Sahrens {
635fa9e4066Sahrens 	if (ds == NULL) {
636fa9e4066Sahrens 		(void) strcpy(name, "mos");
637fa9e4066Sahrens 	} else {
638fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
6393b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
640fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
641fa9e4066Sahrens 			(void) strcat(name, "@");
642745cd3c5Smaybee 			/*
643745cd3c5Smaybee 			 * We use a "recursive" mutex so that we
644745cd3c5Smaybee 			 * can call dprintf_ds() with ds_lock held.
645745cd3c5Smaybee 			 */
646fa9e4066Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
647fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
648fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
649fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
650fa9e4066Sahrens 			} else {
651fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
652fa9e4066Sahrens 			}
653fa9e4066Sahrens 		}
654fa9e4066Sahrens 	}
655fa9e4066Sahrens }
656fa9e4066Sahrens 
6573cb34c60Sahrens void
658745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
6593cb34c60Sahrens {
6603b2aab18SMatthew Ahrens 	dmu_buf_rele(ds->ds_dbuf, tag);
661745cd3c5Smaybee }
662745cd3c5Smaybee 
663745cd3c5Smaybee void
664503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
665745cd3c5Smaybee {
666d808a4fcSJustin T. Gibbs 	ASSERT3P(ds->ds_owner, ==, tag);
667d808a4fcSJustin T. Gibbs 	ASSERT(ds->ds_dbuf != NULL);
668745cd3c5Smaybee 
6693cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
670745cd3c5Smaybee 	ds->ds_owner = NULL;
6713cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
6723b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
673d808a4fcSJustin T. Gibbs 	dsl_dataset_rele(ds, tag);
6743cb34c60Sahrens }
6753cb34c60Sahrens 
6763cb34c60Sahrens boolean_t
6773b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
6783cb34c60Sahrens {
679745cd3c5Smaybee 	boolean_t gotit = FALSE;
680745cd3c5Smaybee 
6813cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
6823b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
683503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
6843b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
685745cd3c5Smaybee 		gotit = TRUE;
6863cb34c60Sahrens 	}
6873cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
688745cd3c5Smaybee 	return (gotit);
689745cd3c5Smaybee }
690745cd3c5Smaybee 
6911d452cf5Sahrens uint64_t
692088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
693ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
694fa9e4066Sahrens {
6953cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
696fa9e4066Sahrens 	dmu_buf_t *dbuf;
697fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
6983cb34c60Sahrens 	uint64_t dsobj;
699fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
700fa9e4066Sahrens 
701088f3894Sahrens 	if (origin == NULL)
702088f3894Sahrens 		origin = dp->dp_origin_snap;
703088f3894Sahrens 
7043cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
705c1379625SJustin T. Gibbs 	ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
706fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
707c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
708fa9e4066Sahrens 
7091649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
7101649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
7113b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
712fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
713fa9e4066Sahrens 	dsphys = dbuf->db_data;
714745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
715fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
716ab04eb8eStimh 	dsphys->ds_flags = flags;
717fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
718fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
719fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
720fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
721ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
722ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
723fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
724088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
725a9799022Sck 
726cde58dbcSMatthew Ahrens 	if (origin == NULL) {
727cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
728cde58dbcSMatthew Ahrens 	} else {
7293b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
730cde58dbcSMatthew Ahrens 
7313cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
732fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
733c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_creation_txg;
734ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
735c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_referenced_bytes;
736fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
737c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_compressed_bytes;
738fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
739c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_uncompressed_bytes;
740c1379625SJustin T. Gibbs 		dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
74142fcb65eSMatthew Ahrens 
74242fcb65eSMatthew Ahrens 		/*
74342fcb65eSMatthew Ahrens 		 * Inherit flags that describe the dataset's contents
74442fcb65eSMatthew Ahrens 		 * (INCONSISTENT) or properties (Case Insensitive).
74542fcb65eSMatthew Ahrens 		 */
746c1379625SJustin T. Gibbs 		dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
74742fcb65eSMatthew Ahrens 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
748fa9e4066Sahrens 
749b5152584SMatthew Ahrens 		if (origin->ds_large_blocks)
750b5152584SMatthew Ahrens 			dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx);
751b5152584SMatthew Ahrens 
7523cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
753c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin)->ds_num_children++;
754fa9e4066Sahrens 
7553b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
756c1379625SJustin T. Gibbs 		    dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
757c1379625SJustin T. Gibbs 		    FTAG, &ohds));
758cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
759cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
760cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
761cde58dbcSMatthew Ahrens 
762088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
763c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
764c1379625SJustin T. Gibbs 				dsl_dataset_phys(origin)->ds_next_clones_obj =
765088f3894Sahrens 				    zap_create(mos,
766088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
767088f3894Sahrens 			}
7683b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
769c1379625SJustin T. Gibbs 			    dsl_dataset_phys(origin)->ds_next_clones_obj,
770c1379625SJustin T. Gibbs 			    dsobj, tx));
771088f3894Sahrens 		}
772088f3894Sahrens 
773fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
774c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
775cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
776c1379625SJustin T. Gibbs 			if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
777cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
778c1379625SJustin T. Gibbs 				dsl_dir_phys(origin->ds_dir)->dd_clones =
779cde58dbcSMatthew Ahrens 				    zap_create(mos,
780cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
781cde58dbcSMatthew Ahrens 			}
7823b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
783c1379625SJustin T. Gibbs 			    dsl_dir_phys(origin->ds_dir)->dd_clones,
784c1379625SJustin T. Gibbs 			    dsobj, tx));
785cde58dbcSMatthew Ahrens 		}
786fa9e4066Sahrens 	}
787ab04eb8eStimh 
788ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
789ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
790ab04eb8eStimh 
791ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
792fa9e4066Sahrens 
793fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
794c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
7953cb34c60Sahrens 
7963cb34c60Sahrens 	return (dsobj);
7973cb34c60Sahrens }
7983cb34c60Sahrens 
7993b2aab18SMatthew Ahrens static void
8003b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
8013b2aab18SMatthew Ahrens {
8023b2aab18SMatthew Ahrens 	objset_t *os;
8033b2aab18SMatthew Ahrens 
8043b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
8053b2aab18SMatthew Ahrens 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
8063b2aab18SMatthew Ahrens 	dsl_dataset_dirty(ds, tx);
8073b2aab18SMatthew Ahrens }
8083b2aab18SMatthew Ahrens 
8093cb34c60Sahrens uint64_t
810ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
811ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
8123cb34c60Sahrens {
8133cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
8143cb34c60Sahrens 	uint64_t dsobj, ddobj;
8153cb34c60Sahrens 	dsl_dir_t *dd;
8163cb34c60Sahrens 
8173b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
8183cb34c60Sahrens 	ASSERT(lastname[0] != '@');
8193cb34c60Sahrens 
820088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
8213b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
8223cb34c60Sahrens 
8233b2aab18SMatthew Ahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
8243b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
8253cb34c60Sahrens 
8263cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
8273cb34c60Sahrens 
828a2afb611SJerry Jelinek 	/*
829a2afb611SJerry Jelinek 	 * Since we're creating a new node we know it's a leaf, so we can
830a2afb611SJerry Jelinek 	 * initialize the counts if the limit feature is active.
831a2afb611SJerry Jelinek 	 */
832a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
833a2afb611SJerry Jelinek 		uint64_t cnt = 0;
834a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
835a2afb611SJerry Jelinek 
836a2afb611SJerry Jelinek 		dsl_dir_zapify(dd, tx);
837a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
838a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
839a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
840a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
841a2afb611SJerry Jelinek 	}
842a2afb611SJerry Jelinek 
8433b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
844fa9e4066Sahrens 
845feaa74e4SMark Maybee 	/*
846feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
847feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
848feaa74e4SMark Maybee 	 */
8493b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
850feaa74e4SMark Maybee 		dsl_dataset_t *ds;
851feaa74e4SMark Maybee 
8523b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
8533b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
854feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
855feaa74e4SMark Maybee 	}
856feaa74e4SMark Maybee 
8571d452cf5Sahrens 	return (dsobj);
858fa9e4066Sahrens }
859fa9e4066Sahrens 
8601d452cf5Sahrens /*
8613b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
8623b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
8633b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
8643b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
8653b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
8663b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
8671d452cf5Sahrens  */
8683b2aab18SMatthew Ahrens void
8693b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
8701d452cf5Sahrens {
8713b2aab18SMatthew Ahrens 	uint64_t mrs_used;
8723b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
8731d452cf5Sahrens 
874bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
8751d452cf5Sahrens 
876c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
877c1379625SJustin T. Gibbs 		mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
8783b2aab18SMatthew Ahrens 	else
8793b2aab18SMatthew Ahrens 		mrs_used = 0;
880842727c2SChris Kirby 
8813b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
882fa9e4066Sahrens 
8833b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
884c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes =
885c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
88619b94df9SMatthew Ahrens 
8873b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
8883b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
889c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
890fa9e4066Sahrens }
891fa9e4066Sahrens 
8923b2aab18SMatthew Ahrens void
8933b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
8943b2aab18SMatthew Ahrens     dmu_tx_t *tx)
895842727c2SChris Kirby {
8963b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
8973b2aab18SMatthew Ahrens 	uint64_t count;
8983b2aab18SMatthew Ahrens 	int err;
8993b2aab18SMatthew Ahrens 
900c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
901c1379625SJustin T. Gibbs 	err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
902c1379625SJustin T. Gibbs 	    obj, tx);
9033b2aab18SMatthew Ahrens 	/*
9043b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
9053b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
9063b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
9073b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
9083b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
9093b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
9103b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
9113b2aab18SMatthew Ahrens 	 * remove this one.
9123b2aab18SMatthew Ahrens 	 */
9133b2aab18SMatthew Ahrens 	if (err != ENOENT)
9143b2aab18SMatthew Ahrens 		VERIFY0(err);
915c1379625SJustin T. Gibbs 	ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
9163b2aab18SMatthew Ahrens 	    &count));
917c1379625SJustin T. Gibbs 	ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
9183b2aab18SMatthew Ahrens }
919842727c2SChris Kirby 
920842727c2SChris Kirby 
9213b2aab18SMatthew Ahrens blkptr_t *
9223b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds)
9233b2aab18SMatthew Ahrens {
924c1379625SJustin T. Gibbs 	return (&dsl_dataset_phys(ds)->ds_bp);
925842727c2SChris Kirby }
926842727c2SChris Kirby 
9273b2aab18SMatthew Ahrens void
9283b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
929842727c2SChris Kirby {
9303b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
9313b2aab18SMatthew Ahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
9323b2aab18SMatthew Ahrens 	if (ds == NULL) {
9333b2aab18SMatthew Ahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
9343b2aab18SMatthew Ahrens 	} else {
9353b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
936c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_bp = *bp;
937842727c2SChris Kirby 	}
9383b2aab18SMatthew Ahrens }
939842727c2SChris Kirby 
9403b2aab18SMatthew Ahrens spa_t *
9413b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
9423b2aab18SMatthew Ahrens {
9433b2aab18SMatthew Ahrens 	return (ds->ds_dir->dd_pool->dp_spa);
944842727c2SChris Kirby }
945842727c2SChris Kirby 
9463b2aab18SMatthew Ahrens void
9473b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
948fa9e4066Sahrens {
9493b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
950842727c2SChris Kirby 
9513b2aab18SMatthew Ahrens 	if (ds == NULL) /* this is the meta-objset */
9523b2aab18SMatthew Ahrens 		return;
9531d452cf5Sahrens 
9543b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
955fa9e4066Sahrens 
956c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
9573b2aab18SMatthew Ahrens 		panic("dirtying snapshot!");
958fa9e4066Sahrens 
9593b2aab18SMatthew Ahrens 	dp = ds->ds_dir->dd_pool;
960ce636f8bSMatthew Ahrens 
9613b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
9623b2aab18SMatthew Ahrens 		/* up the hold count until we can be written out */
9633b2aab18SMatthew Ahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
9643b2aab18SMatthew Ahrens 	}
9653b2aab18SMatthew Ahrens }
966fa9e4066Sahrens 
9672e2c1355SMatthew Ahrens boolean_t
9682e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
9692e2c1355SMatthew Ahrens {
9702e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
9712e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
9722e2c1355SMatthew Ahrens 		    ds, t))
9732e2c1355SMatthew Ahrens 			return (B_TRUE);
9742e2c1355SMatthew Ahrens 	}
9752e2c1355SMatthew Ahrens 	return (B_FALSE);
9762e2c1355SMatthew Ahrens }
9772e2c1355SMatthew Ahrens 
978fa9e4066Sahrens static int
9793b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
980fa9e4066Sahrens {
9813b2aab18SMatthew Ahrens 	uint64_t asize;
982fa9e4066Sahrens 
9833b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
98488b7b0f2SMatthew Ahrens 		return (0);
985fa9e4066Sahrens 
986e1930233Sbonwick 	/*
9873b2aab18SMatthew Ahrens 	 * If there's an fs-only reservation, any blocks that might become
9883b2aab18SMatthew Ahrens 	 * owned by the snapshot dataset must be accommodated by space
9893b2aab18SMatthew Ahrens 	 * outside of the reservation.
990e1930233Sbonwick 	 */
9913b2aab18SMatthew Ahrens 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
992c1379625SJustin T. Gibbs 	asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
9933b2aab18SMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
994be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
995e1930233Sbonwick 
9963cb34c60Sahrens 	/*
9973b2aab18SMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
9983b2aab18SMatthew Ahrens 	 * snapshot checks in this sync group.
9993cb34c60Sahrens 	 */
10003b2aab18SMatthew Ahrens 	if (asize > 0)
10013b2aab18SMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
10023cb34c60Sahrens 
1003e1930233Sbonwick 	return (0);
1004e1930233Sbonwick }
1005e1930233Sbonwick 
10063b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg {
10073b2aab18SMatthew Ahrens 	nvlist_t *ddsa_snaps;
10083b2aab18SMatthew Ahrens 	nvlist_t *ddsa_props;
10093b2aab18SMatthew Ahrens 	nvlist_t *ddsa_errors;
1010a2afb611SJerry Jelinek 	cred_t *ddsa_cr;
10113b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t;
1012842727c2SChris Kirby 
10133cb34c60Sahrens int
10143b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1015a2afb611SJerry Jelinek     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
10161d452cf5Sahrens {
10173b2aab18SMatthew Ahrens 	int error;
10183b2aab18SMatthew Ahrens 	uint64_t value;
1019fa9e4066Sahrens 
10203b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1021745cd3c5Smaybee 
10223b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
1023842727c2SChris Kirby 		return (0);
1024fa9e4066Sahrens 
1025fa9e4066Sahrens 	/*
10263b2aab18SMatthew Ahrens 	 * We don't allow multiple snapshots of the same txg.  If there
10273b2aab18SMatthew Ahrens 	 * is already one, try again.
1028fa9e4066Sahrens 	 */
1029c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1030be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
1031fa9e4066Sahrens 
1032fa9e4066Sahrens 	/*
10333b2aab18SMatthew Ahrens 	 * Check for conflicting snapshot name.
1034fa9e4066Sahrens 	 */
10353b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
10363b2aab18SMatthew Ahrens 	if (error == 0)
1037be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
10383b2aab18SMatthew Ahrens 	if (error != ENOENT)
10393b2aab18SMatthew Ahrens 		return (error);
1040842727c2SChris Kirby 
1041ca48f36fSKeith M Wesolowski 	/*
1042ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
1043ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
1044ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
1045ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
1046ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1047ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
1048ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
1049ca48f36fSKeith M Wesolowski 	 */
1050ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
1051ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
1052ca48f36fSKeith M Wesolowski 
1053a2afb611SJerry Jelinek 	/*
1054a2afb611SJerry Jelinek 	 * Skip the check for temporary snapshots or if we have already checked
1055a2afb611SJerry Jelinek 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1056a2afb611SJerry Jelinek 	 * check the count here when we're receiving a stream.
1057a2afb611SJerry Jelinek 	 */
1058a2afb611SJerry Jelinek 	if (cnt != 0 && cr != NULL) {
1059a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1060a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1061a2afb611SJerry Jelinek 		if (error != 0)
1062a2afb611SJerry Jelinek 			return (error);
1063a2afb611SJerry Jelinek 	}
1064a2afb611SJerry Jelinek 
10653b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
10663b2aab18SMatthew Ahrens 	if (error != 0)
10673b2aab18SMatthew Ahrens 		return (error);
1068842727c2SChris Kirby 
10691d452cf5Sahrens 	return (0);
10701d452cf5Sahrens }
10711d452cf5Sahrens 
10723b2aab18SMatthew Ahrens static int
10733b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1074745cd3c5Smaybee {
10753b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
10763b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
10773b2aab18SMatthew Ahrens 	nvpair_t *pair;
10783b2aab18SMatthew Ahrens 	int rv = 0;
10793b2aab18SMatthew Ahrens 
1080a2afb611SJerry Jelinek 	/*
1081a2afb611SJerry Jelinek 	 * Pre-compute how many total new snapshots will be created for each
1082a2afb611SJerry Jelinek 	 * level in the tree and below. This is needed for validating the
1083a2afb611SJerry Jelinek 	 * snapshot limit when either taking a recursive snapshot or when
1084a2afb611SJerry Jelinek 	 * taking multiple snapshots.
1085a2afb611SJerry Jelinek 	 *
1086a2afb611SJerry Jelinek 	 * The problem is that the counts are not actually adjusted when
1087a2afb611SJerry Jelinek 	 * we are checking, only when we finally sync. For a single snapshot,
1088a2afb611SJerry Jelinek 	 * this is easy, the count will increase by 1 at each node up the tree,
1089a2afb611SJerry Jelinek 	 * but its more complicated for the recursive/multiple snapshot case.
1090a2afb611SJerry Jelinek 	 *
1091a2afb611SJerry Jelinek 	 * The dsl_fs_ss_limit_check function does recursively check the count
1092a2afb611SJerry Jelinek 	 * at each level up the tree but since it is validating each snapshot
1093a2afb611SJerry Jelinek 	 * independently we need to be sure that we are validating the complete
1094a2afb611SJerry Jelinek 	 * count for the entire set of snapshots. We do this by rolling up the
1095a2afb611SJerry Jelinek 	 * counts for each component of the name into an nvlist and then
1096a2afb611SJerry Jelinek 	 * checking each of those cases with the aggregated count.
1097a2afb611SJerry Jelinek 	 *
1098a2afb611SJerry Jelinek 	 * This approach properly handles not only the recursive snapshot
1099a2afb611SJerry Jelinek 	 * case (where we get all of those on the ddsa_snaps list) but also
1100a2afb611SJerry Jelinek 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1101a2afb611SJerry Jelinek 	 * validate the limit on 'a' using a count of 2).
1102a2afb611SJerry Jelinek 	 *
1103a2afb611SJerry Jelinek 	 * We validate the snapshot names in the third loop and only report
1104a2afb611SJerry Jelinek 	 * name errors once.
1105a2afb611SJerry Jelinek 	 */
1106a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1107a2afb611SJerry Jelinek 		nvlist_t *cnt_track = NULL;
1108a2afb611SJerry Jelinek 		cnt_track = fnvlist_alloc();
1109a2afb611SJerry Jelinek 
1110a2afb611SJerry Jelinek 		/* Rollup aggregated counts into the cnt_track list */
1111a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1112a2afb611SJerry Jelinek 		    pair != NULL;
1113a2afb611SJerry Jelinek 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1114a2afb611SJerry Jelinek 			char *pdelim;
1115a2afb611SJerry Jelinek 			uint64_t val;
1116a2afb611SJerry Jelinek 			char nm[MAXPATHLEN];
1117a2afb611SJerry Jelinek 
1118a2afb611SJerry Jelinek 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1119a2afb611SJerry Jelinek 			pdelim = strchr(nm, '@');
1120a2afb611SJerry Jelinek 			if (pdelim == NULL)
1121a2afb611SJerry Jelinek 				continue;
1122a2afb611SJerry Jelinek 			*pdelim = '\0';
1123a2afb611SJerry Jelinek 
1124a2afb611SJerry Jelinek 			do {
1125a2afb611SJerry Jelinek 				if (nvlist_lookup_uint64(cnt_track, nm,
1126a2afb611SJerry Jelinek 				    &val) == 0) {
1127a2afb611SJerry Jelinek 					/* update existing entry */
1128a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm,
1129a2afb611SJerry Jelinek 					    val + 1);
1130a2afb611SJerry Jelinek 				} else {
1131a2afb611SJerry Jelinek 					/* add to list */
1132a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm, 1);
1133a2afb611SJerry Jelinek 				}
1134a2afb611SJerry Jelinek 
1135a2afb611SJerry Jelinek 				pdelim = strrchr(nm, '/');
1136a2afb611SJerry Jelinek 				if (pdelim != NULL)
1137a2afb611SJerry Jelinek 					*pdelim = '\0';
1138a2afb611SJerry Jelinek 			} while (pdelim != NULL);
1139a2afb611SJerry Jelinek 		}
1140a2afb611SJerry Jelinek 
1141a2afb611SJerry Jelinek 		/* Check aggregated counts at each level */
1142a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1143a2afb611SJerry Jelinek 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1144a2afb611SJerry Jelinek 			int error = 0;
1145a2afb611SJerry Jelinek 			char *name;
1146a2afb611SJerry Jelinek 			uint64_t cnt = 0;
1147a2afb611SJerry Jelinek 			dsl_dataset_t *ds;
1148a2afb611SJerry Jelinek 
1149a2afb611SJerry Jelinek 			name = nvpair_name(pair);
1150a2afb611SJerry Jelinek 			cnt = fnvpair_value_uint64(pair);
1151a2afb611SJerry Jelinek 			ASSERT(cnt > 0);
1152a2afb611SJerry Jelinek 
1153a2afb611SJerry Jelinek 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1154a2afb611SJerry Jelinek 			if (error == 0) {
1155a2afb611SJerry Jelinek 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1156a2afb611SJerry Jelinek 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1157a2afb611SJerry Jelinek 				    ddsa->ddsa_cr);
1158a2afb611SJerry Jelinek 				dsl_dataset_rele(ds, FTAG);
1159a2afb611SJerry Jelinek 			}
1160a2afb611SJerry Jelinek 
1161a2afb611SJerry Jelinek 			if (error != 0) {
1162a2afb611SJerry Jelinek 				if (ddsa->ddsa_errors != NULL)
1163a2afb611SJerry Jelinek 					fnvlist_add_int32(ddsa->ddsa_errors,
1164a2afb611SJerry Jelinek 					    name, error);
1165a2afb611SJerry Jelinek 				rv = error;
1166a2afb611SJerry Jelinek 				/* only report one error for this check */
1167a2afb611SJerry Jelinek 				break;
1168a2afb611SJerry Jelinek 			}
1169a2afb611SJerry Jelinek 		}
1170a2afb611SJerry Jelinek 		nvlist_free(cnt_track);
1171a2afb611SJerry Jelinek 	}
1172a2afb611SJerry Jelinek 
11733b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
11743b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
11753b2aab18SMatthew Ahrens 		int error = 0;
11763b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
11773b2aab18SMatthew Ahrens 		char *name, *atp;
11783b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
11793b2aab18SMatthew Ahrens 
11803b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
11813b2aab18SMatthew Ahrens 		if (strlen(name) >= MAXNAMELEN)
1182be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
11833b2aab18SMatthew Ahrens 		if (error == 0) {
11843b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
11853b2aab18SMatthew Ahrens 			if (atp == NULL)
1186be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
11873b2aab18SMatthew Ahrens 			if (error == 0)
11883b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
11893b2aab18SMatthew Ahrens 		}
11903b2aab18SMatthew Ahrens 		if (error == 0)
11913b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
11923b2aab18SMatthew Ahrens 		if (error == 0) {
1193a2afb611SJerry Jelinek 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
11943b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
1195a2afb611SJerry Jelinek 			    atp + 1, tx, B_FALSE, 0, NULL);
11963b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
11973b2aab18SMatthew Ahrens 		}
1198745cd3c5Smaybee 
11993b2aab18SMatthew Ahrens 		if (error != 0) {
12003b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
12013b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
12023b2aab18SMatthew Ahrens 				    name, error);
12033b2aab18SMatthew Ahrens 			}
12043b2aab18SMatthew Ahrens 			rv = error;
12053b2aab18SMatthew Ahrens 		}
12063b2aab18SMatthew Ahrens 	}
1207a2afb611SJerry Jelinek 
12083b2aab18SMatthew Ahrens 	return (rv);
1209745cd3c5Smaybee }
1210745cd3c5Smaybee 
12113b2aab18SMatthew Ahrens void
12123b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
12133b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1214745cd3c5Smaybee {
12153b2aab18SMatthew Ahrens 	static zil_header_t zero_zil;
1216745cd3c5Smaybee 
12173b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
12183b2aab18SMatthew Ahrens 	dmu_buf_t *dbuf;
12193b2aab18SMatthew Ahrens 	dsl_dataset_phys_t *dsphys;
12203b2aab18SMatthew Ahrens 	uint64_t dsobj, crtxg;
12213b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
12223b2aab18SMatthew Ahrens 	objset_t *os;
1223745cd3c5Smaybee 
12243b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1225c33e334fSMatthew Ahrens 
1226c33e334fSMatthew Ahrens 	/*
12273b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
12283b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1229c33e334fSMatthew Ahrens 	 */
12303b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
12313b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
12323b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
12333b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
1234c33e334fSMatthew Ahrens 
1235a2afb611SJerry Jelinek 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1236cde58dbcSMatthew Ahrens 
1237cde58dbcSMatthew Ahrens 	/*
12383b2aab18SMatthew Ahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1239088f3894Sahrens 	 */
1240088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1241088f3894Sahrens 		crtxg = 1;
1242088f3894Sahrens 	else
1243088f3894Sahrens 		crtxg = tx->tx_txg;
1244088f3894Sahrens 
12451649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
12461649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
12473b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1248fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1249fa9e4066Sahrens 	dsphys = dbuf->db_data;
1250745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
12511d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1252fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1253fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1254fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
1255c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1256c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1257fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1258fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1259fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1260088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
1261c1379625SJustin T. Gibbs 	dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1262c1379625SJustin T. Gibbs 	dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1263c1379625SJustin T. Gibbs 	dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1264c1379625SJustin T. Gibbs 	dsphys->ds_uncompressed_bytes =
1265c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1266c1379625SJustin T. Gibbs 	dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1267c1379625SJustin T. Gibbs 	dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1268ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1269fa9e4066Sahrens 
1270b5152584SMatthew Ahrens 	if (ds->ds_large_blocks)
1271b5152584SMatthew Ahrens 		dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx);
1272b5152584SMatthew Ahrens 
1273c1379625SJustin T. Gibbs 	ASSERT3U(ds->ds_prev != 0, ==,
1274c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
12751d452cf5Sahrens 	if (ds->ds_prev) {
1276088f3894Sahrens 		uint64_t next_clones_obj =
1277c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1278c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1279fa9e4066Sahrens 		    ds->ds_object ||
1280c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1281c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1282c1379625SJustin T. Gibbs 		    ds->ds_object) {
12831d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1284c1379625SJustin T. Gibbs 			ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1285c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1286c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1287088f3894Sahrens 		} else if (next_clones_obj != 0) {
12883b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1289c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
12903b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1291088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1292fa9e4066Sahrens 		}
1293fa9e4066Sahrens 	}
1294fa9e4066Sahrens 
1295a9799022Sck 	/*
1296a9799022Sck 	 * If we have a reference-reservation on this dataset, we will
1297a9799022Sck 	 * need to increase the amount of refreservation being charged
1298a9799022Sck 	 * since our unique space is going to zero.
1299a9799022Sck 	 */
1300a9799022Sck 	if (ds->ds_reserved) {
13013f9d6ad7SLin Ling 		int64_t delta;
13023f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1303c1379625SJustin T. Gibbs 		delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1304c1379625SJustin T. Gibbs 		    ds->ds_reserved);
130574e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
13063f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1307a9799022Sck 	}
1308a9799022Sck 
1309fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1310c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj =
1311c1379625SJustin T. Gibbs 	    dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1312c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1313cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1314c1379625SJustin T. Gibbs 	dsl_deadlist_open(&ds->ds_deadlist, mos,
1315c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1316cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
1317c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1318cde58dbcSMatthew Ahrens 
1319c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1320c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1321c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1322c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1323a9799022Sck 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1324c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1325fa9e4066Sahrens 
1326c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
13273b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1328fa9e4066Sahrens 
1329fa9e4066Sahrens 	if (ds->ds_prev)
13303b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
13313b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
1332c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1333ecd6cf80Smarks 
13343f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1335088f3894Sahrens 
133671eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
133771eb0538SChris Kirby 
13384445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1339fa9e4066Sahrens }
1340fa9e4066Sahrens 
13413b2aab18SMatthew Ahrens static void
13423b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1343fa9e4066Sahrens {
13443b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
13453b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
13463b2aab18SMatthew Ahrens 	nvpair_t *pair;
134791ebeef5Sahrens 
13483b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
13493b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
13503b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
13513b2aab18SMatthew Ahrens 		char *name, *atp;
13523b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
13533b2aab18SMatthew Ahrens 
13543b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
13553b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
13563b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
13573b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
13583b2aab18SMatthew Ahrens 
13593b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
13603b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
13613b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
13623b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
13633b2aab18SMatthew Ahrens 		}
13643b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13653b2aab18SMatthew Ahrens 	}
1366fa9e4066Sahrens }
1367fa9e4066Sahrens 
13683b2aab18SMatthew Ahrens /*
13693b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
13703b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
13713b2aab18SMatthew Ahrens  */
13723b2aab18SMatthew Ahrens int
13733b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
137419b94df9SMatthew Ahrens {
13753b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
13763b2aab18SMatthew Ahrens 	nvpair_t *pair;
13773b2aab18SMatthew Ahrens 	boolean_t needsuspend;
13783b2aab18SMatthew Ahrens 	int error;
13793b2aab18SMatthew Ahrens 	spa_t *spa;
13803b2aab18SMatthew Ahrens 	char *firstname;
13813b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
138219b94df9SMatthew Ahrens 
13833b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
13843b2aab18SMatthew Ahrens 	if (pair == NULL)
13853b2aab18SMatthew Ahrens 		return (0);
13863b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
13873b2aab18SMatthew Ahrens 
13883b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
13893b2aab18SMatthew Ahrens 	if (error != 0)
13903b2aab18SMatthew Ahrens 		return (error);
13913b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
13923b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
13933b2aab18SMatthew Ahrens 
13943b2aab18SMatthew Ahrens 	if (needsuspend) {
13953b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
13963b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
13973b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
13983b2aab18SMatthew Ahrens 			char fsname[MAXNAMELEN];
13993b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
14003b2aab18SMatthew Ahrens 			char *atp;
14013b2aab18SMatthew Ahrens 			void *cookie;
14023b2aab18SMatthew Ahrens 
14033b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
14043b2aab18SMatthew Ahrens 			if (atp == NULL) {
1405be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
14063b2aab18SMatthew Ahrens 				break;
14073b2aab18SMatthew Ahrens 			}
14083b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
14093b2aab18SMatthew Ahrens 
14103b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
14113b2aab18SMatthew Ahrens 			if (error != 0)
14123b2aab18SMatthew Ahrens 				break;
14133b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
14143b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
14153b2aab18SMatthew Ahrens 		}
14163b2aab18SMatthew Ahrens 	}
14173b2aab18SMatthew Ahrens 
14183b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
14193b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
14203b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
1421a2afb611SJerry Jelinek 	ddsa.ddsa_cr = CRED();
14223b2aab18SMatthew Ahrens 
14233b2aab18SMatthew Ahrens 	if (error == 0) {
14243b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
14253b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
14267d46dc6cSMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
14273b2aab18SMatthew Ahrens 	}
14283b2aab18SMatthew Ahrens 
14293b2aab18SMatthew Ahrens 	if (suspended != NULL) {
14303b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
14313b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
14323b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
14333b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
14343b2aab18SMatthew Ahrens 		}
14353b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
14363b2aab18SMatthew Ahrens 	}
14373b2aab18SMatthew Ahrens 
14383b2aab18SMatthew Ahrens 	return (error);
14393b2aab18SMatthew Ahrens }
14403b2aab18SMatthew Ahrens 
14413b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
14423b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
14433b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
14443b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
14453b2aab18SMatthew Ahrens 	const char *ddsta_htag;
14463b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
14473b2aab18SMatthew Ahrens 
14483b2aab18SMatthew Ahrens static int
14493b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
14503b2aab18SMatthew Ahrens {
14513b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
14523b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14533b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14543b2aab18SMatthew Ahrens 	int error;
14553b2aab18SMatthew Ahrens 
14563b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
14573b2aab18SMatthew Ahrens 	if (error != 0)
14583b2aab18SMatthew Ahrens 		return (error);
14593b2aab18SMatthew Ahrens 
1460a2afb611SJerry Jelinek 	/* NULL cred means no limit check for tmp snapshot */
1461ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1462a2afb611SJerry Jelinek 	    tx, B_FALSE, 0, NULL);
14633b2aab18SMatthew Ahrens 	if (error != 0) {
14643b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14653b2aab18SMatthew Ahrens 		return (error);
14663b2aab18SMatthew Ahrens 	}
14673b2aab18SMatthew Ahrens 
14683b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
14693b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1470be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
14713b2aab18SMatthew Ahrens 	}
14723b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
14733b2aab18SMatthew Ahrens 	    B_TRUE, tx);
14743b2aab18SMatthew Ahrens 	if (error != 0) {
14753b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14763b2aab18SMatthew Ahrens 		return (error);
14773b2aab18SMatthew Ahrens 	}
14783b2aab18SMatthew Ahrens 
14793b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14803b2aab18SMatthew Ahrens 	return (0);
14813b2aab18SMatthew Ahrens }
14823b2aab18SMatthew Ahrens 
14833b2aab18SMatthew Ahrens static void
14843b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
14853b2aab18SMatthew Ahrens {
14863b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
14873b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14883b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14893b2aab18SMatthew Ahrens 
14903b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
14913b2aab18SMatthew Ahrens 
14923b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
14933b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
14943b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
14953b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
14963b2aab18SMatthew Ahrens 
14973b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14983b2aab18SMatthew Ahrens }
14993b2aab18SMatthew Ahrens 
15003b2aab18SMatthew Ahrens int
15013b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
15023b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
15033b2aab18SMatthew Ahrens {
15043b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
15053b2aab18SMatthew Ahrens 	int error;
15063b2aab18SMatthew Ahrens 	spa_t *spa;
15073b2aab18SMatthew Ahrens 	boolean_t needsuspend;
15083b2aab18SMatthew Ahrens 	void *cookie;
15093b2aab18SMatthew Ahrens 
15103b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
15113b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
15123b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
15133b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
15143b2aab18SMatthew Ahrens 
15153b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
15163b2aab18SMatthew Ahrens 	if (error != 0)
15173b2aab18SMatthew Ahrens 		return (error);
15183b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
15193b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
15203b2aab18SMatthew Ahrens 
15213b2aab18SMatthew Ahrens 	if (needsuspend) {
15223b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
15233b2aab18SMatthew Ahrens 		if (error != 0)
15243b2aab18SMatthew Ahrens 			return (error);
15253b2aab18SMatthew Ahrens 	}
15263b2aab18SMatthew Ahrens 
15273b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
15287d46dc6cSMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
15293b2aab18SMatthew Ahrens 
15303b2aab18SMatthew Ahrens 	if (needsuspend)
15313b2aab18SMatthew Ahrens 		zil_resume(cookie);
15323b2aab18SMatthew Ahrens 	return (error);
15333b2aab18SMatthew Ahrens }
15343b2aab18SMatthew Ahrens 
15353b2aab18SMatthew Ahrens 
15363b2aab18SMatthew Ahrens void
15373b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
15383b2aab18SMatthew Ahrens {
15393b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
15403b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1541c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
15423b2aab18SMatthew Ahrens 
15433b2aab18SMatthew Ahrens 	/*
15443b2aab18SMatthew Ahrens 	 * in case we had to change ds_fsid_guid when we opened it,
15453b2aab18SMatthew Ahrens 	 * sync it out now.
15463b2aab18SMatthew Ahrens 	 */
15473b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1548c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
15493b2aab18SMatthew Ahrens 
15503b2aab18SMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
1551b5152584SMatthew Ahrens 
1552b5152584SMatthew Ahrens 	if (ds->ds_need_large_blocks && !ds->ds_large_blocks) {
1553b5152584SMatthew Ahrens 		dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx);
1554b5152584SMatthew Ahrens 		ds->ds_large_blocks = B_TRUE;
1555b5152584SMatthew Ahrens 	}
15563b2aab18SMatthew Ahrens }
15573b2aab18SMatthew Ahrens 
15583b2aab18SMatthew Ahrens static void
15593b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
15603b2aab18SMatthew Ahrens {
15613b2aab18SMatthew Ahrens 	uint64_t count = 0;
15623b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
15633b2aab18SMatthew Ahrens 	zap_cursor_t zc;
15643b2aab18SMatthew Ahrens 	zap_attribute_t za;
15653b2aab18SMatthew Ahrens 	nvlist_t *propval = fnvlist_alloc();
15663b2aab18SMatthew Ahrens 	nvlist_t *val = fnvlist_alloc();
15673b2aab18SMatthew Ahrens 
15683b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
156919b94df9SMatthew Ahrens 
157019b94df9SMatthew Ahrens 	/*
15713b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
157219b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
157319b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
157419b94df9SMatthew Ahrens 	 */
1575c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1576c1379625SJustin T. Gibbs 		VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
157719b94df9SMatthew Ahrens 		    &count));
157819b94df9SMatthew Ahrens 	}
1579c1379625SJustin T. Gibbs 	if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
158019b94df9SMatthew Ahrens 		goto fail;
1581c1379625SJustin T. Gibbs 	for (zap_cursor_init(&zc, mos,
1582c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_next_clones_obj);
158319b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
158419b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
158519b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
158619b94df9SMatthew Ahrens 		char buf[ZFS_MAXNAMELEN];
15873b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
15883b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
158919b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
15903b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
159119b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
159219b94df9SMatthew Ahrens 	}
159319b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
15943b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
15953b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
159619b94df9SMatthew Ahrens fail:
159719b94df9SMatthew Ahrens 	nvlist_free(val);
159819b94df9SMatthew Ahrens 	nvlist_free(propval);
159919b94df9SMatthew Ahrens }
160019b94df9SMatthew Ahrens 
1601fa9e4066Sahrens void
1602a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1603fa9e4066Sahrens {
16043b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1605187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
1606a9799022Sck 
16073b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
16083b2aab18SMatthew Ahrens 
1609c1379625SJustin T. Gibbs 	ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
1610c1379625SJustin T. Gibbs 	    (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
1611c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_compressed_bytes);
16124445fffbSMatthew Ahrens 
16134445fffbSMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
161477372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
1615c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes);
16164445fffbSMatthew Ahrens 
1617bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
16184445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
16194445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
1620c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_unique_bytes);
16214445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
16224445fffbSMatthew Ahrens 	} else {
1623b461c746SMatthew Ahrens 		if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
1624b461c746SMatthew Ahrens 			char buf[MAXNAMELEN];
1625b461c746SMatthew Ahrens 			dsl_dataset_name(ds->ds_prev, buf);
1626b461c746SMatthew Ahrens 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1627b461c746SMatthew Ahrens 		}
1628b461c746SMatthew Ahrens 
16294445fffbSMatthew Ahrens 		dsl_dir_stats(ds->ds_dir, nv);
16304445fffbSMatthew Ahrens 	}
1631fa9e4066Sahrens 
1632a9799022Sck 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1633a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1634a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1635a9799022Sck 
1636a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1637c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_time);
1638a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1639c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_txg);
1640a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1641a9799022Sck 	    ds->ds_quota);
1642a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1643a9799022Sck 	    ds->ds_reserved);
1644c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1645c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_guid);
16461d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
1647c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
16481d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
16491d713200SEric Schrock 	    ds->ds_object);
165092241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
165192241e0bSTom Erickson 	    ds->ds_userrefs);
1652842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1653842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1654fa9e4066Sahrens 
1655c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
165619b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
165719b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
165819b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
165919b94df9SMatthew Ahrens 
166019b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
1661c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
166219b94df9SMatthew Ahrens 		if (err == 0) {
166319b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
166419b94df9SMatthew Ahrens 			    &comp, &uncomp);
166519b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
166619b94df9SMatthew Ahrens 			if (err == 0) {
166719b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
166819b94df9SMatthew Ahrens 				    written);
166919b94df9SMatthew Ahrens 			}
167019b94df9SMatthew Ahrens 		}
167119b94df9SMatthew Ahrens 	}
1672fa9e4066Sahrens }
1673fa9e4066Sahrens 
1674a2eea2e1Sahrens void
1675a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1676a2eea2e1Sahrens {
16773b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
16783b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
16793b2aab18SMatthew Ahrens 
1680c1379625SJustin T. Gibbs 	stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
1681c1379625SJustin T. Gibbs 	stat->dds_inconsistent =
1682c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
1683c1379625SJustin T. Gibbs 	stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
16844445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
1685bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
1686a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
1687c1379625SJustin T. Gibbs 		stat->dds_num_clones =
1688c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_num_children - 1;
1689ebedde84SEric Taylor 	} else {
1690ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
1691ebedde84SEric Taylor 		stat->dds_num_clones = 0;
1692a2eea2e1Sahrens 
16934445fffbSMatthew Ahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
16944445fffbSMatthew Ahrens 			dsl_dataset_t *ods;
1695a2eea2e1Sahrens 
16963b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
1697c1379625SJustin T. Gibbs 			    dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
1698c1379625SJustin T. Gibbs 			    FTAG, &ods));
16994445fffbSMatthew Ahrens 			dsl_dataset_name(ods, stat->dds_origin);
17003b2aab18SMatthew Ahrens 			dsl_dataset_rele(ods, FTAG);
17014445fffbSMatthew Ahrens 		}
1702a2eea2e1Sahrens 	}
1703a2eea2e1Sahrens }
1704a2eea2e1Sahrens 
1705a2eea2e1Sahrens uint64_t
1706a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1707a2eea2e1Sahrens {
170891ebeef5Sahrens 	return (ds->ds_fsid_guid);
1709a2eea2e1Sahrens }
1710a2eea2e1Sahrens 
1711a2eea2e1Sahrens void
1712a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
1713a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
1714a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1715fa9e4066Sahrens {
1716c1379625SJustin T. Gibbs 	*refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
1717a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1718c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
1719c1379625SJustin T. Gibbs 		*availbytesp +=
1720c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
1721a9799022Sck 	if (ds->ds_quota != 0) {
1722a9799022Sck 		/*
1723a9799022Sck 		 * Adjust available bytes according to refquota
1724a9799022Sck 		 */
1725a9799022Sck 		if (*refdbytesp < ds->ds_quota)
1726a9799022Sck 			*availbytesp = MIN(*availbytesp,
1727a9799022Sck 			    ds->ds_quota - *refdbytesp);
1728a9799022Sck 		else
1729a9799022Sck 			*availbytesp = 0;
1730a9799022Sck 	}
1731c1379625SJustin T. Gibbs 	*usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
1732a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1733fa9e4066Sahrens }
1734fa9e4066Sahrens 
1735f18faf3fSek boolean_t
173634f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1737f18faf3fSek {
1738f18faf3fSek 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1739f18faf3fSek 
17403b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
174134f2f8cfSMatthew Ahrens 	if (snap == NULL)
1742f18faf3fSek 		return (B_FALSE);
1743c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_bp.blk_birth >
1744c1379625SJustin T. Gibbs 	    dsl_dataset_phys(snap)->ds_creation_txg) {
174534f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
17466e0cbcaaSMatthew Ahrens 		/*
17476e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
17486e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
17496e0cbcaaSMatthew Ahrens 		 * modified.
17506e0cbcaaSMatthew Ahrens 		 */
17516e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
17526e0cbcaaSMatthew Ahrens 			return (B_TRUE);
175334f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
17546e0cbcaaSMatthew Ahrens 			return (B_TRUE);
17556e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
175634f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
17576e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
17586e0cbcaaSMatthew Ahrens 	}
1759f18faf3fSek 	return (B_FALSE);
1760f18faf3fSek }
1761f18faf3fSek 
17623b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
17633b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
17643b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
17653b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
17663b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
17673b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
17683b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
17693b2aab18SMatthew Ahrens 
17701d452cf5Sahrens /* ARGSUSED */
1771fa9e4066Sahrens static int
17723b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
17733b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1774fa9e4066Sahrens {
17753b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
17763b2aab18SMatthew Ahrens 	int error;
1777fa9e4066Sahrens 	uint64_t val;
1778fa9e4066Sahrens 
17793b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
17803b2aab18SMatthew Ahrens 	if (error != 0) {
17813b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
17823b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
17833b2aab18SMatthew Ahrens 	}
17841d452cf5Sahrens 
17853b2aab18SMatthew Ahrens 	/* new name should not exist */
17863b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
17873b2aab18SMatthew Ahrens 	if (error == 0)
1788be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
17893b2aab18SMatthew Ahrens 	else if (error == ENOENT)
17903b2aab18SMatthew Ahrens 		error = 0;
1791cdf5b4caSmmusante 
1792cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
17933b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
17943b2aab18SMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1795be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
1796cdf5b4caSmmusante 
17973b2aab18SMatthew Ahrens 	return (error);
17981d452cf5Sahrens }
1799fa9e4066Sahrens 
18003b2aab18SMatthew Ahrens static int
18013b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
18021d452cf5Sahrens {
18033b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
18043b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18051d452cf5Sahrens 	dsl_dataset_t *hds;
18063b2aab18SMatthew Ahrens 	int error;
1807fa9e4066Sahrens 
18083b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
18093b2aab18SMatthew Ahrens 	if (error != 0)
18103b2aab18SMatthew Ahrens 		return (error);
1811fa9e4066Sahrens 
18123b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
18133b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
18143b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
18153b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
18163b2aab18SMatthew Ahrens 	} else {
18173b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
18183b2aab18SMatthew Ahrens 	}
1819745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
18203b2aab18SMatthew Ahrens 	return (error);
1821fa9e4066Sahrens }
1822fa9e4066Sahrens 
1823cdf5b4caSmmusante static int
18243b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
18253b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1826cdf5b4caSmmusante {
18273b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
18283b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
18293b2aab18SMatthew Ahrens 	uint64_t val;
18303b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
18313b2aab18SMatthew Ahrens 	int error;
1832ecd6cf80Smarks 
18333b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
18343b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
18353b2aab18SMatthew Ahrens 	if (error == ENOENT) {
18363b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
18373b2aab18SMatthew Ahrens 		return (0);
1838ecd6cf80Smarks 	}
1839ecd6cf80Smarks 
18403b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
18413b2aab18SMatthew Ahrens 
18423b2aab18SMatthew Ahrens 	/* log before we change the name */
18433b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
18443b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
1845cdf5b4caSmmusante 
1846a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
1847a2afb611SJerry Jelinek 	    B_FALSE));
18483b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
18493b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
18503b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
1851c1379625SJustin T. Gibbs 	VERIFY0(zap_add(dp->dp_meta_objset,
1852c1379625SJustin T. Gibbs 	    dsl_dataset_phys(hds)->ds_snapnames_zapobj,
18533b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1854cdf5b4caSmmusante 
18553b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1856cdf5b4caSmmusante 	return (0);
1857cdf5b4caSmmusante }
1858cdf5b4caSmmusante 
18593b2aab18SMatthew Ahrens static void
18603b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1861cdf5b4caSmmusante {
18623b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
18633b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18643b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
1865cdf5b4caSmmusante 
18663b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
18673b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
18683b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
18693b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
18703b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
18713b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
18723b2aab18SMatthew Ahrens 	} else {
18733b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1874cdf5b4caSmmusante 	}
18753b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
1876cdf5b4caSmmusante }
1877cdf5b4caSmmusante 
18783b2aab18SMatthew Ahrens int
18793b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
18803b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
18813a5a36beSmmusante {
18823b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
18833a5a36beSmmusante 
18843b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
18853b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
18863b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
18873b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
18883a5a36beSmmusante 
18893b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
18907d46dc6cSMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
18917d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
18923a5a36beSmmusante }
18933a5a36beSmmusante 
189491948b51SKeith M Wesolowski /*
189591948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
189691948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
189791948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
189891948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
189991948b51SKeith M Wesolowski  * when we release the long hold.
190091948b51SKeith M Wesolowski  */
190191948b51SKeith M Wesolowski static int
190291948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
190391948b51SKeith M Wesolowski {
190491948b51SKeith M Wesolowski 	boolean_t held;
190591948b51SKeith M Wesolowski 
190691948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
190791948b51SKeith M Wesolowski 		return (0);
190891948b51SKeith M Wesolowski 
190991948b51SKeith M Wesolowski 	if (owner != NULL) {
191091948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
191191948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
191291948b51SKeith M Wesolowski 	}
191391948b51SKeith M Wesolowski 
191491948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
191591948b51SKeith M Wesolowski 
191691948b51SKeith M Wesolowski 	if (owner != NULL)
191791948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
191891948b51SKeith M Wesolowski 
191991948b51SKeith M Wesolowski 	if (held)
192091948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
192191948b51SKeith M Wesolowski 
192291948b51SKeith M Wesolowski 	return (0);
192391948b51SKeith M Wesolowski }
192491948b51SKeith M Wesolowski 
192591948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg {
192691948b51SKeith M Wesolowski 	const char *ddra_fsname;
192791948b51SKeith M Wesolowski 	void *ddra_owner;
1928a7027df1SMatthew Ahrens 	nvlist_t *ddra_result;
192991948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t;
193091948b51SKeith M Wesolowski 
19313b2aab18SMatthew Ahrens static int
19323b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1933fa9e4066Sahrens {
193491948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
19353b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
19361d452cf5Sahrens 	dsl_dataset_t *ds;
19373b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
19383b2aab18SMatthew Ahrens 	int error;
1939fa9e4066Sahrens 
194091948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
19413b2aab18SMatthew Ahrens 	if (error != 0)
19423b2aab18SMatthew Ahrens 		return (error);
1943370c1af0SSanjeev Bagewadi 
19443b2aab18SMatthew Ahrens 	/* must not be a snapshot */
1945bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
19463b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1947be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
19483b2aab18SMatthew Ahrens 	}
19493a5a36beSmmusante 
19503b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
1951c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
19523b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1953be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
19543b2aab18SMatthew Ahrens 	}
19553a5a36beSmmusante 
195678f17100SMatthew Ahrens 	/* must not have any bookmarks after the most recent snapshot */
195778f17100SMatthew Ahrens 	nvlist_t *proprequest = fnvlist_alloc();
195878f17100SMatthew Ahrens 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
195978f17100SMatthew Ahrens 	nvlist_t *bookmarks = fnvlist_alloc();
196078f17100SMatthew Ahrens 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
196178f17100SMatthew Ahrens 	fnvlist_free(proprequest);
196278f17100SMatthew Ahrens 	if (error != 0)
196378f17100SMatthew Ahrens 		return (error);
196478f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
196578f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
196678f17100SMatthew Ahrens 		nvlist_t *valuenv =
196778f17100SMatthew Ahrens 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
196878f17100SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
196978f17100SMatthew Ahrens 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
1970c1379625SJustin T. Gibbs 		if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
197178f17100SMatthew Ahrens 			fnvlist_free(bookmarks);
197278f17100SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
197378f17100SMatthew Ahrens 			return (SET_ERROR(EEXIST));
197478f17100SMatthew Ahrens 		}
197578f17100SMatthew Ahrens 	}
197678f17100SMatthew Ahrens 	fnvlist_free(bookmarks);
197778f17100SMatthew Ahrens 
197891948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
197991948b51SKeith M Wesolowski 	if (error != 0) {
19803b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
198191948b51SKeith M Wesolowski 		return (error);
19823b2aab18SMatthew Ahrens 	}
19833b2aab18SMatthew Ahrens 
19843b2aab18SMatthew Ahrens 	/*
19853b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
19863b2aab18SMatthew Ahrens 	 * the refquota.
19873b2aab18SMatthew Ahrens 	 */
19883b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
1989c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
19903b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1991be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
1992fa9e4066Sahrens 	}
1993370c1af0SSanjeev Bagewadi 
19943b2aab18SMatthew Ahrens 	/*
19953b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
19963b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
19973b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
19983b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
19993b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
20003b2aab18SMatthew Ahrens 	 */
20013b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2002c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
20033b2aab18SMatthew Ahrens 
20043b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
20053b2aab18SMatthew Ahrens 	    unused_refres_delta >
20063b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
20073b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2008be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
2009fa9e4066Sahrens 	}
2010fa9e4066Sahrens 
20113b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
20123b2aab18SMatthew Ahrens 	return (0);
20133b2aab18SMatthew Ahrens }
20141d452cf5Sahrens 
20153b2aab18SMatthew Ahrens static void
20163b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
20173b2aab18SMatthew Ahrens {
201891948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
20193b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20203b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
20213b2aab18SMatthew Ahrens 	uint64_t cloneobj;
2022a7027df1SMatthew Ahrens 	char namebuf[ZFS_MAXNAMELEN];
20231d452cf5Sahrens 
202491948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
20251d452cf5Sahrens 
2026a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
2027a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2028a7027df1SMatthew Ahrens 
20293b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
20303b2aab18SMatthew Ahrens 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
20311d452cf5Sahrens 
20323b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
20331d452cf5Sahrens 
20343b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
20353b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
20363b2aab18SMatthew Ahrens 
20373b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
20383b2aab18SMatthew Ahrens 
20393b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
20403b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
20413b2aab18SMatthew Ahrens }
20423b2aab18SMatthew Ahrens 
204391948b51SKeith M Wesolowski /*
2044a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
2045a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
2046a7027df1SMatthew Ahrens  * in the result nvlist.
204791948b51SKeith M Wesolowski  *
2048a7027df1SMatthew Ahrens  * If owner != NULL:
204991948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
205091948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
205191948b51SKeith M Wesolowski  *   succeed or not.
205291948b51SKeith M Wesolowski  *
205391948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
205491948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
205591948b51SKeith M Wesolowski  */
20563b2aab18SMatthew Ahrens int
2057a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
20583b2aab18SMatthew Ahrens {
205991948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
206091948b51SKeith M Wesolowski 
206191948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
206291948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
2063a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
206491948b51SKeith M Wesolowski 
20653b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
20667d46dc6cSMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra,
20677d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
2068fa9e4066Sahrens }
206999653d4eSeschrock 
2070088f3894Sahrens struct promotenode {
2071745cd3c5Smaybee 	list_node_t link;
2072745cd3c5Smaybee 	dsl_dataset_t *ds;
2073745cd3c5Smaybee };
2074745cd3c5Smaybee 
20753b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg {
20763b2aab18SMatthew Ahrens 	const char *ddpa_clonename;
20773b2aab18SMatthew Ahrens 	dsl_dataset_t *ddpa_clone;
207874e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
20793b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_origin; /* origin of the origin */
208074e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2081681d9761SEric Taylor 	char *err_ds;
2082a2afb611SJerry Jelinek 	cred_t *cr;
20833b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t;
20841d452cf5Sahrens 
208574e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
20863b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
20873b2aab18SMatthew Ahrens     void *tag);
20883b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
208974e7dc98SMatthew Ahrens 
209099653d4eSeschrock static int
20913b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
209299653d4eSeschrock {
20933b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
20943b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20953b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
20963b2aab18SMatthew Ahrens 	struct promotenode *snap;
20973b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
2098745cd3c5Smaybee 	int err;
2099cde58dbcSMatthew Ahrens 	uint64_t unused;
2100a2afb611SJerry Jelinek 	uint64_t ss_mv_cnt;
21011d452cf5Sahrens 
21023b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
21033b2aab18SMatthew Ahrens 	if (err != 0)
21043b2aab18SMatthew Ahrens 		return (err);
210599653d4eSeschrock 
21063b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
21071d452cf5Sahrens 
2108c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
21093b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
2110be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
21113b2aab18SMatthew Ahrens 	}
21123b2aab18SMatthew Ahrens 
21133b2aab18SMatthew Ahrens 	/*
21143b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
21153b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
21163b2aab18SMatthew Ahrens 	 */
21173b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
21183b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
21193b2aab18SMatthew Ahrens 		return (0);
21203b2aab18SMatthew Ahrens 	}
21213b2aab18SMatthew Ahrens 
21223b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
21233b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
212499653d4eSeschrock 
21253cb34c60Sahrens 	/* compute origin's new unique space */
21263b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2127c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2128c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2129cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2130c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
21313b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
213299653d4eSeschrock 
2133745cd3c5Smaybee 	/*
2134745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2135745cd3c5Smaybee 	 *
213674e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
21373b2aab18SMatthew Ahrens 	 * to used by each snapshot:
213874e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
213974e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
214074e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2141745cd3c5Smaybee 	 * So a sequence would look like:
214274e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2143745cd3c5Smaybee 	 * Which simplifies to:
214474e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2145745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
214674e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2147745cd3c5Smaybee 	 */
2148a2afb611SJerry Jelinek 	ss_mv_cnt = 0;
2149c1379625SJustin T. Gibbs 	ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2150c1379625SJustin T. Gibbs 	ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2151c1379625SJustin T. Gibbs 	ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
21523b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
21533b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
215499653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2155745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
215699653d4eSeschrock 
2157a2afb611SJerry Jelinek 		ss_mv_cnt++;
2158a2afb611SJerry Jelinek 
21593b2aab18SMatthew Ahrens 		/*
21603b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
21613b2aab18SMatthew Ahrens 		 * the objset.
21623b2aab18SMatthew Ahrens 		 */
21633b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
2164be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
21653b2aab18SMatthew Ahrens 			goto out;
21663b2aab18SMatthew Ahrens 		}
21673b2aab18SMatthew Ahrens 
216899653d4eSeschrock 		/* Check that the snapshot name does not conflict */
21693b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
2170745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2171681d9761SEric Taylor 		if (err == 0) {
21723b2aab18SMatthew Ahrens 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2173be6fd75aSMatthew Ahrens 			err = SET_ERROR(EEXIST);
2174681d9761SEric Taylor 			goto out;
2175681d9761SEric Taylor 		}
2176745cd3c5Smaybee 		if (err != ENOENT)
2177681d9761SEric Taylor 			goto out;
217899653d4eSeschrock 
2179745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
2180c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
218174e7dc98SMatthew Ahrens 			continue;
218274e7dc98SMatthew Ahrens 
2183cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2184cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
21853b2aab18SMatthew Ahrens 		ddpa->used += dlused;
21863b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
21873b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
218874e7dc98SMatthew Ahrens 	}
2189745cd3c5Smaybee 
2190745cd3c5Smaybee 	/*
2191745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
2192745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
2193745cd3c5Smaybee 	 */
21943b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
2195c1379625SJustin T. Gibbs 		ddpa->used -=
2196c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
2197c1379625SJustin T. Gibbs 		ddpa->comp -=
2198c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
21993b2aab18SMatthew Ahrens 		ddpa->uncomp -=
2200c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->
2201c1379625SJustin T. Gibbs 		    ds_uncompressed_bytes;
220299653d4eSeschrock 	}
220399653d4eSeschrock 
2204a2afb611SJerry Jelinek 	/* Check that there is enough space and limit headroom here */
220574e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2206a2afb611SJerry Jelinek 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
22073b2aab18SMatthew Ahrens 	if (err != 0)
22083b2aab18SMatthew Ahrens 		goto out;
220974e7dc98SMatthew Ahrens 
221074e7dc98SMatthew Ahrens 	/*
221174e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
221274e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
221374e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
221474e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
221574e7dc98SMatthew Ahrens 	 */
2216c1379625SJustin T. Gibbs 	if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
221774e7dc98SMatthew Ahrens 		uint64_t space;
221874e7dc98SMatthew Ahrens 
221974e7dc98SMatthew Ahrens 		/*
222074e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
22213f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
2222cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
222374e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
222474e7dc98SMatthew Ahrens 		 * iterate over all bps.
222574e7dc98SMatthew Ahrens 		 */
22263b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
22273b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
22283b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
22293b2aab18SMatthew Ahrens 		if (err != 0)
22303b2aab18SMatthew Ahrens 			goto out;
223174e7dc98SMatthew Ahrens 
22323b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
22333f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
22343b2aab18SMatthew Ahrens 		if (err != 0)
22353b2aab18SMatthew Ahrens 			goto out;
22363b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
223774e7dc98SMatthew Ahrens 	}
2238c1379625SJustin T. Gibbs 	if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
2239c1379625SJustin T. Gibbs 	    DD_FLAG_USED_BREAKDOWN) {
22403b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
2241c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_creation_txg,
2242c1379625SJustin T. Gibbs 		    &ddpa->originusedsnap);
22433b2aab18SMatthew Ahrens 		if (err != 0)
22443b2aab18SMatthew Ahrens 			goto out;
2245745cd3c5Smaybee 	}
22461d452cf5Sahrens 
2247681d9761SEric Taylor out:
22483b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2249681d9761SEric Taylor 	return (err);
22501d452cf5Sahrens }
225199653d4eSeschrock 
22521d452cf5Sahrens static void
22533b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
22541d452cf5Sahrens {
22553b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
22563b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
22573b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
22583b2aab18SMatthew Ahrens 	struct promotenode *snap;
22593b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
22603b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_head;
22613b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
22623cb34c60Sahrens 	dsl_dir_t *odd = NULL;
2263088f3894Sahrens 	uint64_t oldnext_obj;
226474e7dc98SMatthew Ahrens 	int64_t delta;
22651d452cf5Sahrens 
22663b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
22673b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
22683b2aab18SMatthew Ahrens 
2269c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
22701d452cf5Sahrens 
22713b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
22723b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
22733b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
22743b2aab18SMatthew Ahrens 
22753b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
227674e7dc98SMatthew Ahrens 	origin_head = snap->ds;
227774e7dc98SMatthew Ahrens 
22780b69c2f0Sahrens 	/*
22793cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
22800b69c2f0Sahrens 	 * changing.
22810b69c2f0Sahrens 	 */
22823b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
22833cb34c60Sahrens 	    NULL, FTAG, &odd));
228499653d4eSeschrock 
2285745cd3c5Smaybee 	/* change origin's next snap */
2286745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2287c1379625SJustin T. Gibbs 	oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
22883b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2289c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2290c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2291c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2292745cd3c5Smaybee 
2293088f3894Sahrens 	/* change the origin's next clone */
2294c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
22953b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
22963b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
22973b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2298c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2299088f3894Sahrens 		    oldnext_obj, tx));
2300088f3894Sahrens 	}
2301088f3894Sahrens 
2302745cd3c5Smaybee 	/* change origin */
2303745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2304c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
2305c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
23063f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2307745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2308c1379625SJustin T. Gibbs 	dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
23093f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
2310c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
2311745cd3c5Smaybee 
2312cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2313cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
23143b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2315c1379625SJustin T. Gibbs 		    dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
23163b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2317c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2318cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2319cde58dbcSMatthew Ahrens 
23203b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2321c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2322cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
2323c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_clones == 0) {
2324c1379625SJustin T. Gibbs 			dsl_dir_phys(dd)->dd_clones =
2325c1379625SJustin T. Gibbs 			    zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
2326c1379625SJustin T. Gibbs 			    DMU_OT_NONE, 0, tx);
2327cde58dbcSMatthew Ahrens 		}
23283b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2329c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2330cde58dbcSMatthew Ahrens 	}
2331cde58dbcSMatthew Ahrens 
233299653d4eSeschrock 	/* move snapshots to this dir */
23333b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
23343b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2335745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
233699653d4eSeschrock 
23373b2aab18SMatthew Ahrens 		/*
23383b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
23393b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
23403b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
23413b2aab18SMatthew Ahrens 		 */
2342503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2343503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2344503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
23453baa08fcSek 		}
23463b2aab18SMatthew Ahrens 
234799653d4eSeschrock 		/* move snap name entry */
23483b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
23493b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2350a2afb611SJerry Jelinek 		    ds->ds_snapname, tx, B_TRUE));
23513b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
2352c1379625SJustin T. Gibbs 		    dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
235399653d4eSeschrock 		    8, 1, &ds->ds_object, tx));
2354a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2355a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2356cde58dbcSMatthew Ahrens 
235799653d4eSeschrock 		/* change containing dsl_dir */
235899653d4eSeschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2359c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
2360c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
23613cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
23623b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
23633b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
236499653d4eSeschrock 		    NULL, ds, &ds->ds_dir));
236599653d4eSeschrock 
2366cde58dbcSMatthew Ahrens 		/* move any clone references */
2367c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
2368cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2369cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2370cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2371cde58dbcSMatthew Ahrens 
23723b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
2373c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
23743b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
23753b2aab18SMatthew Ahrens 			    zap_cursor_advance(&zc)) {
23763b2aab18SMatthew Ahrens 				dsl_dataset_t *cnds;
23773b2aab18SMatthew Ahrens 				uint64_t o;
2378a9799022Sck 
23793b2aab18SMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
23803b2aab18SMatthew Ahrens 					/*
23813b2aab18SMatthew Ahrens 					 * We've already moved the
23823b2aab18SMatthew Ahrens 					 * origin's reference.
23833b2aab18SMatthew Ahrens 					 */
23843b2aab18SMatthew Ahrens 					continue;
23853b2aab18SMatthew Ahrens 				}
2386a9799022Sck 
23873b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
23883b2aab18SMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
2389c1379625SJustin T. Gibbs 				o = dsl_dir_phys(cnds->ds_dir)->
2390c1379625SJustin T. Gibbs 				    dd_head_dataset_obj;
2391a9799022Sck 
23923b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
2393c1379625SJustin T. Gibbs 				    dsl_dir_phys(odd)->dd_clones, o, tx));
23943b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
2395c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd)->dd_clones, o, tx));
23963b2aab18SMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
23973b2aab18SMatthew Ahrens 			}
23983b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
23993b2aab18SMatthew Ahrens 		}
24009082849eSck 
24013b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
2402a9799022Sck 	}
2403a9799022Sck 
2404a9799022Sck 	/*
24053b2aab18SMatthew Ahrens 	 * Change space accounting.
24063b2aab18SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
24073b2aab18SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
24083b2aab18SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
2409a9799022Sck 	 */
2410a9799022Sck 
24113b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
2412c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
24133b2aab18SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
24143b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
24153b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
24163b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
24173b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
24183b2aab18SMatthew Ahrens 
24193b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
2420c1379625SJustin T. Gibbs 	    dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
24213b2aab18SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
24223b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
24233b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
24243b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
24253b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
24263b2aab18SMatthew Ahrens 
2427c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
24283b2aab18SMatthew Ahrens 
24293b2aab18SMatthew Ahrens 	/* log history record */
24303b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
24313b2aab18SMatthew Ahrens 
24323b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
24333b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2434a9799022Sck }
2435a9799022Sck 
24363b2aab18SMatthew Ahrens /*
24373b2aab18SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
24383b2aab18SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
24393b2aab18SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
24403b2aab18SMatthew Ahrens  * snapshots back to this dataset's origin.
24413b2aab18SMatthew Ahrens  */
2442a9799022Sck static int
24433b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
24443b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2445a9799022Sck {
24463b2aab18SMatthew Ahrens 	uint64_t obj = last_obj;
2447a9799022Sck 
24483b2aab18SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
24493b2aab18SMatthew Ahrens 	    offsetof(struct promotenode, link));
2450a9799022Sck 
24513b2aab18SMatthew Ahrens 	while (obj != first_obj) {
24523b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
24533b2aab18SMatthew Ahrens 		struct promotenode *snap;
24543b2aab18SMatthew Ahrens 		int err;
245592241e0bSTom Erickson 
24563b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
24573b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
24583b2aab18SMatthew Ahrens 		if (err != 0)
24593b2aab18SMatthew Ahrens 			return (err);
2460a9799022Sck 
24613b2aab18SMatthew Ahrens 		if (first_obj == 0)
2462c1379625SJustin T. Gibbs 			first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
24633b2aab18SMatthew Ahrens 
24643b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
24653b2aab18SMatthew Ahrens 		snap->ds = ds;
24663b2aab18SMatthew Ahrens 		list_insert_tail(l, snap);
2467c1379625SJustin T. Gibbs 		obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
24683b2aab18SMatthew Ahrens 	}
2469a9799022Sck 
2470a9799022Sck 	return (0);
2471a9799022Sck }
2472a9799022Sck 
24733b2aab18SMatthew Ahrens static int
24743b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2475a9799022Sck {
24763b2aab18SMatthew Ahrens 	struct promotenode *snap;
2477a9799022Sck 
24783b2aab18SMatthew Ahrens 	*spacep = 0;
24793b2aab18SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
24803b2aab18SMatthew Ahrens 		uint64_t used, comp, uncomp;
24813b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
24823b2aab18SMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
24833b2aab18SMatthew Ahrens 		*spacep += used;
248492241e0bSTom Erickson 	}
24853b2aab18SMatthew Ahrens 	return (0);
2486a9799022Sck }
2487a9799022Sck 
24883b2aab18SMatthew Ahrens static void
24893b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
2490a9799022Sck {
24913b2aab18SMatthew Ahrens 	struct promotenode *snap;
249292241e0bSTom Erickson 
24933b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
24943b2aab18SMatthew Ahrens 		return;
2495a9799022Sck 
24963b2aab18SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
24973b2aab18SMatthew Ahrens 		list_remove(l, snap);
24983b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
24993b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
25003b2aab18SMatthew Ahrens 	}
25013b2aab18SMatthew Ahrens 	list_destroy(l);
2502a9799022Sck }
2503a9799022Sck 
2504a9799022Sck static int
25053b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2506a9799022Sck {
25073b2aab18SMatthew Ahrens 	int error;
25083b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
25093b2aab18SMatthew Ahrens 	struct promotenode *snap;
2510a9799022Sck 
25113b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
25123b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
25133b2aab18SMatthew Ahrens 	if (error != 0)
25143b2aab18SMatthew Ahrens 		return (error);
25153b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
2516a9799022Sck 
2517bc9014e6SJustin Gibbs 	if (ddpa->ddpa_clone->ds_is_snapshot ||
25183b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
25193b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2520be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
25213b2aab18SMatthew Ahrens 	}
2522a9799022Sck 
2523c1379625SJustin T. Gibbs 	error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
25243b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
25253b2aab18SMatthew Ahrens 	if (error != 0)
25263b2aab18SMatthew Ahrens 		goto out;
2527a9799022Sck 
25283b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
25293b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
25303b2aab18SMatthew Ahrens 	if (error != 0)
25313b2aab18SMatthew Ahrens 		goto out;
2532a9799022Sck 
25333b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
2534c1379625SJustin T. Gibbs 	ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
2535c1379625SJustin T. Gibbs 	error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
2536c1379625SJustin T. Gibbs 	    dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
25373b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
25383b2aab18SMatthew Ahrens 	if (error != 0)
25393b2aab18SMatthew Ahrens 		goto out;
2540379c004dSEric Schrock 
2541c1379625SJustin T. Gibbs 	if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
25423b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
2543c1379625SJustin T. Gibbs 		    dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
25443b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
25453b2aab18SMatthew Ahrens 		if (error != 0)
25463b2aab18SMatthew Ahrens 			goto out;
2547379c004dSEric Schrock 	}
25483b2aab18SMatthew Ahrens out:
25493b2aab18SMatthew Ahrens 	if (error != 0)
25503b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
25513b2aab18SMatthew Ahrens 	return (error);
2552a9799022Sck }
2553a9799022Sck 
2554a9799022Sck static void
25553b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2556a9799022Sck {
25573b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
25583b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
25593b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
25603b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
25613b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
25623b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
25633b2aab18SMatthew Ahrens }
256402c8f3f0SMatthew Ahrens 
25653b2aab18SMatthew Ahrens /*
25663b2aab18SMatthew Ahrens  * Promote a clone.
25673b2aab18SMatthew Ahrens  *
25683b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
25693b2aab18SMatthew Ahrens  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
25703b2aab18SMatthew Ahrens  */
25713b2aab18SMatthew Ahrens int
25723b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap)
25733b2aab18SMatthew Ahrens {
25743b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
25753b2aab18SMatthew Ahrens 	uint64_t numsnaps;
25763b2aab18SMatthew Ahrens 	int error;
25773b2aab18SMatthew Ahrens 	objset_t *os;
257892241e0bSTom Erickson 
25793b2aab18SMatthew Ahrens 	/*
25803b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
25813b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
25823b2aab18SMatthew Ahrens 	 */
25833b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
25843b2aab18SMatthew Ahrens 	if (error != 0)
25853b2aab18SMatthew Ahrens 		return (error);
25863b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2587c1379625SJustin T. Gibbs 	    dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2588c1379625SJustin T. Gibbs 	    &numsnaps);
25893b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
25903b2aab18SMatthew Ahrens 	if (error != 0)
25913b2aab18SMatthew Ahrens 		return (error);
259202c8f3f0SMatthew Ahrens 
25933b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
25943b2aab18SMatthew Ahrens 	ddpa.err_ds = conflsnap;
2595a2afb611SJerry Jelinek 	ddpa.cr = CRED();
259602c8f3f0SMatthew Ahrens 
25973b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_dataset_promote_check,
25987d46dc6cSMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa,
25997d46dc6cSMatthew Ahrens 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2600a9799022Sck }
2601a9799022Sck 
2602a9799022Sck int
26033b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
260491948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2605a9799022Sck {
26063b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2607a9799022Sck 
26083b2aab18SMatthew Ahrens 	/* they should both be heads */
2609bc9014e6SJustin Gibbs 	if (clone->ds_is_snapshot ||
2610bc9014e6SJustin Gibbs 	    origin_head->ds_is_snapshot)
2611be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
261292241e0bSTom Erickson 
261334f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
261434f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
2615be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2616a9799022Sck 
26173b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
26183b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
26193b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
262034f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2621be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
262292241e0bSTom Erickson 
26233b2aab18SMatthew Ahrens 	/* the clone should be a child of the origin */
26243b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2625be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2626842727c2SChris Kirby 
26273b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
262834f2f8cfSMatthew Ahrens 	if (!force &&
262934f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2630be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
2631c99e4bdcSChris Kirby 
26323b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
263391948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2634be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
26353b2aab18SMatthew Ahrens 
26363b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
26373b2aab18SMatthew Ahrens 	unused_refres_delta =
26383b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2639c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
26403b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2641c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
26423b2aab18SMatthew Ahrens 
26433b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
26443b2aab18SMatthew Ahrens 	    unused_refres_delta >
26453b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2646be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
26473b2aab18SMatthew Ahrens 
26483b2aab18SMatthew Ahrens 	/* clone can't be over the head's refquota */
26493b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
2650c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes >
2651c1379625SJustin T. Gibbs 	    origin_head->ds_quota)
2652be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2653c99e4bdcSChris Kirby 
26543b2aab18SMatthew Ahrens 	return (0);
2655c99e4bdcSChris Kirby }
2656c99e4bdcSChris Kirby 
2657a7f53a56SChris Kirby void
26583b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
26593b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2660a7f53a56SChris Kirby {
26613b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
26623b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2663a7f53a56SChris Kirby 
26643b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
26653b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
2666c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota);
266734f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2668842727c2SChris Kirby 
26693b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
26703b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2671842727c2SChris Kirby 
26723b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
26733b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
26743b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
26753b2aab18SMatthew Ahrens 	}
2676842727c2SChris Kirby 
26773b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
26783b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
26793b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
2680842727c2SChris Kirby 	}
2681842727c2SChris Kirby 
26823b2aab18SMatthew Ahrens 	unused_refres_delta =
26833b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2684c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
26853b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
2686c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
26873b2aab18SMatthew Ahrens 
26883b2aab18SMatthew Ahrens 	/*
26893b2aab18SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
26903b2aab18SMatthew Ahrens 	 */
26913b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
26923b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
26933b2aab18SMatthew Ahrens 		uint64_t comp, uncomp;
26943b2aab18SMatthew Ahrens 
26953b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
26963b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
2697c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
2698c1379625SJustin T. Gibbs 		    &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
26993b2aab18SMatthew Ahrens 	}
27003b2aab18SMatthew Ahrens 
27013b2aab18SMatthew Ahrens 	/* swap blkptrs */
27023b2aab18SMatthew Ahrens 	{
27033b2aab18SMatthew Ahrens 		blkptr_t tmp;
2704c1379625SJustin T. Gibbs 		tmp = dsl_dataset_phys(origin_head)->ds_bp;
2705c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_bp =
2706c1379625SJustin T. Gibbs 		    dsl_dataset_phys(clone)->ds_bp;
2707c1379625SJustin T. Gibbs 		dsl_dataset_phys(clone)->ds_bp = tmp;
27083b2aab18SMatthew Ahrens 	}
27093b2aab18SMatthew Ahrens 
27103b2aab18SMatthew Ahrens 	/* set dd_*_bytes */
27113b2aab18SMatthew Ahrens 	{
27123b2aab18SMatthew Ahrens 		int64_t dused, dcomp, duncomp;
27133b2aab18SMatthew Ahrens 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
27143b2aab18SMatthew Ahrens 		uint64_t odl_used, odl_comp, odl_uncomp;
27153b2aab18SMatthew Ahrens 
2716c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dir_phys(clone->ds_dir)->
27173b2aab18SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
27183b2aab18SMatthew Ahrens 
27193b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
27203b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
27213b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
27223b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
272315508ac0SChris Kirby 
2724c1379625SJustin T. Gibbs 		dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
2725c1379625SJustin T. Gibbs 		    cdl_used -
2726c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
2727c1379625SJustin T. Gibbs 		    odl_used);
2728c1379625SJustin T. Gibbs 		dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
2729c1379625SJustin T. Gibbs 		    cdl_comp -
2730c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
2731c1379625SJustin T. Gibbs 		    odl_comp);
2732c1379625SJustin T. Gibbs 		duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
27333b2aab18SMatthew Ahrens 		    cdl_uncomp -
2734c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
2735c1379625SJustin T. Gibbs 		    odl_uncomp);
2736842727c2SChris Kirby 
27373b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
27383b2aab18SMatthew Ahrens 		    dused, dcomp, duncomp, tx);
27393b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
27403b2aab18SMatthew Ahrens 		    -dused, -dcomp, -duncomp, tx);
2741842727c2SChris Kirby 
2742842727c2SChris Kirby 		/*
27433b2aab18SMatthew Ahrens 		 * The difference in the space used by snapshots is the
27443b2aab18SMatthew Ahrens 		 * difference in snapshot space due to the head's
27453b2aab18SMatthew Ahrens 		 * deadlist (since that's the only thing that's
27463b2aab18SMatthew Ahrens 		 * changing that affects the snapused).
2747842727c2SChris Kirby 		 */
27483b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
27493b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
27503b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
27513b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
27523b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
27533b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
27543b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
27553b2aab18SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
2756842727c2SChris Kirby 	}
2757842727c2SChris Kirby 
27583b2aab18SMatthew Ahrens 	/* swap ds_*_bytes */
2759c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
2760c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes);
2761c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
2762c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_compressed_bytes);
2763c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
2764c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_uncompressed_bytes);
2765c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
2766c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
2767842727c2SChris Kirby 
27683b2aab18SMatthew Ahrens 	/* apply any parent delta for change in unconsumed refreservation */
27693b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
27703b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
2771ca45db41SChris Kirby 
27723b2aab18SMatthew Ahrens 	/*
27733b2aab18SMatthew Ahrens 	 * Swap deadlists.
27743b2aab18SMatthew Ahrens 	 */
27753b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
27763b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
2777c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
2778c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
27793b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
2780c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
27813b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
2782c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_deadlist_obj);
2783842727c2SChris Kirby 
27843b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2785842727c2SChris Kirby 
27863b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
27873b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
2788842727c2SChris Kirby }
2789842727c2SChris Kirby 
27903b2aab18SMatthew Ahrens /*
27913b2aab18SMatthew Ahrens  * Given a pool name and a dataset object number in that pool,
27923b2aab18SMatthew Ahrens  * return the name of that dataset.
27933b2aab18SMatthew Ahrens  */
2794a7f53a56SChris Kirby int
27953b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2796a7f53a56SChris Kirby {
27973b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
27983b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2799a7f53a56SChris Kirby 	int error;
2800a7f53a56SChris Kirby 
28013b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
28023b2aab18SMatthew Ahrens 	if (error != 0)
28033b2aab18SMatthew Ahrens 		return (error);
28043b2aab18SMatthew Ahrens 
28053b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
28063b2aab18SMatthew Ahrens 	if (error == 0) {
28073b2aab18SMatthew Ahrens 		dsl_dataset_name(ds, buf);
28083b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
28093b2aab18SMatthew Ahrens 	}
28103b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
2811a7f53a56SChris Kirby 
2812a7f53a56SChris Kirby 	return (error);
2813a7f53a56SChris Kirby }
2814a7f53a56SChris Kirby 
2815842727c2SChris Kirby int
28163b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
28173b2aab18SMatthew Ahrens     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2818842727c2SChris Kirby {
28193b2aab18SMatthew Ahrens 	int error = 0;
2820842727c2SChris Kirby 
28213b2aab18SMatthew Ahrens 	ASSERT3S(asize, >, 0);
2822842727c2SChris Kirby 
28233b2aab18SMatthew Ahrens 	/*
28243b2aab18SMatthew Ahrens 	 * *ref_rsrv is the portion of asize that will come from any
28253b2aab18SMatthew Ahrens 	 * unconsumed refreservation space.
28263b2aab18SMatthew Ahrens 	 */
28273b2aab18SMatthew Ahrens 	*ref_rsrv = 0;
2828842727c2SChris Kirby 
28293b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
28303b2aab18SMatthew Ahrens 	/*
28313b2aab18SMatthew Ahrens 	 * Make a space adjustment for reserved bytes.
28323b2aab18SMatthew Ahrens 	 */
2833c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
28343b2aab18SMatthew Ahrens 		ASSERT3U(*used, >=,
2835c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
2836c1379625SJustin T. Gibbs 		*used -=
2837c1379625SJustin T. Gibbs 		    (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
28383b2aab18SMatthew Ahrens 		*ref_rsrv =
28393b2aab18SMatthew Ahrens 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
2840842727c2SChris Kirby 	}
2841842727c2SChris Kirby 
28423b2aab18SMatthew Ahrens 	if (!check_quota || ds->ds_quota == 0) {
28433b2aab18SMatthew Ahrens 		mutex_exit(&ds->ds_lock);
28443b2aab18SMatthew Ahrens 		return (0);
2845842727c2SChris Kirby 	}
28463b2aab18SMatthew Ahrens 	/*
28473b2aab18SMatthew Ahrens 	 * If they are requesting more space, and our current estimate
28483b2aab18SMatthew Ahrens 	 * is over quota, they get to try again unless the actual
28493b2aab18SMatthew Ahrens 	 * on-disk is over quota and there are no pending changes (which
28503b2aab18SMatthew Ahrens 	 * may free up space for us).
28513b2aab18SMatthew Ahrens 	 */
2852c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
2853c1379625SJustin T. Gibbs 	    ds->ds_quota) {
28543b2aab18SMatthew Ahrens 		if (inflight > 0 ||
2855c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
2856be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
28573b2aab18SMatthew Ahrens 		else
2858be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
2859842727c2SChris Kirby 	}
28603b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2861842727c2SChris Kirby 
2862842727c2SChris Kirby 	return (error);
2863842727c2SChris Kirby }
2864842727c2SChris Kirby 
28653b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
28663b2aab18SMatthew Ahrens 	const char *ddsqra_name;
28673b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
28683b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
28693b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
2870842727c2SChris Kirby 
28713b2aab18SMatthew Ahrens 
28723b2aab18SMatthew Ahrens /* ARGSUSED */
2873842727c2SChris Kirby static int
28743b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2875842727c2SChris Kirby {
28763b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
28773b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
28783b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2879842727c2SChris Kirby 	int error;
28803b2aab18SMatthew Ahrens 	uint64_t newval;
2881842727c2SChris Kirby 
28823b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2883be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
2884842727c2SChris Kirby 
28853b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
28863b2aab18SMatthew Ahrens 	if (error != 0)
28873b2aab18SMatthew Ahrens 		return (error);
28883b2aab18SMatthew Ahrens 
2889bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
28903b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2891be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2892842727c2SChris Kirby 	}
2893842727c2SChris Kirby 
28943b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
28953b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
28963b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
28973b2aab18SMatthew Ahrens 	if (error != 0) {
28983b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2899842727c2SChris Kirby 		return (error);
2900842727c2SChris Kirby 	}
2901842727c2SChris Kirby 
29023b2aab18SMatthew Ahrens 	if (newval == 0) {
29033b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
29043b2aab18SMatthew Ahrens 		return (0);
29053b2aab18SMatthew Ahrens 	}
2906842727c2SChris Kirby 
2907c1379625SJustin T. Gibbs 	if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
29083b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
29093b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2910be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
29113b2aab18SMatthew Ahrens 	}
29123b2aab18SMatthew Ahrens 
29133b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2914842727c2SChris Kirby 	return (0);
2915842727c2SChris Kirby }
2916842727c2SChris Kirby 
29173b2aab18SMatthew Ahrens static void
29183b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2919842727c2SChris Kirby {
29203b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
29213b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29223b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
29233b2aab18SMatthew Ahrens 	uint64_t newval;
2924842727c2SChris Kirby 
29253b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2926842727c2SChris Kirby 
29273b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
29283b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
29293b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
29303b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
2931842727c2SChris Kirby 
29323b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
29333b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2934842727c2SChris Kirby 
29353b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
29363b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
29373b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
2938842727c2SChris Kirby 	}
29393b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2940842727c2SChris Kirby }
2941842727c2SChris Kirby 
29423b2aab18SMatthew Ahrens int
29433b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
29443b2aab18SMatthew Ahrens     uint64_t refquota)
2945842727c2SChris Kirby {
29463b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
2947842727c2SChris Kirby 
29483b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
29493b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
29503b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
29513b2aab18SMatthew Ahrens 
29523b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
29537d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
2954842727c2SChris Kirby }
2955842727c2SChris Kirby 
2956842727c2SChris Kirby static int
29573b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2958842727c2SChris Kirby {
29593b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
29603b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
2961842727c2SChris Kirby 	dsl_dataset_t *ds;
2962842727c2SChris Kirby 	int error;
29633b2aab18SMatthew Ahrens 	uint64_t newval, unique;
2964d7747cbcSChris Kirby 
29653b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2966be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
2967842727c2SChris Kirby 
29683b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
29693b2aab18SMatthew Ahrens 	if (error != 0)
2970842727c2SChris Kirby 		return (error);
2971842727c2SChris Kirby 
2972bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
29733b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2974be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2975842727c2SChris Kirby 	}
2976842727c2SChris Kirby 
29773b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
29783b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
29793b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
29803b2aab18SMatthew Ahrens 	if (error != 0) {
29813b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2982842727c2SChris Kirby 		return (error);
2983842727c2SChris Kirby 	}
2984842727c2SChris Kirby 
29853b2aab18SMatthew Ahrens 	/*
29863b2aab18SMatthew Ahrens 	 * If we are doing the preliminary check in open context, the
29873b2aab18SMatthew Ahrens 	 * space estimates may be inaccurate.
29883b2aab18SMatthew Ahrens 	 */
29893b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
29903b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
29913b2aab18SMatthew Ahrens 		return (0);
2992842727c2SChris Kirby 	}
2993842727c2SChris Kirby 
29943b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
29953b2aab18SMatthew Ahrens 	if (!DS_UNIQUE_IS_ACCURATE(ds))
29963b2aab18SMatthew Ahrens 		dsl_dataset_recalc_head_uniq(ds);
2997c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
29983b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2999842727c2SChris Kirby 
30003b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
30013b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
30023b2aab18SMatthew Ahrens 		    MAX(unique, ds->ds_reserved);
3003842727c2SChris Kirby 
30043b2aab18SMatthew Ahrens 		if (delta >
30053b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
30063b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
30073b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
3008be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
30093b2aab18SMatthew Ahrens 		}
3010842727c2SChris Kirby 	}
3011842727c2SChris Kirby 
30123b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
30133b2aab18SMatthew Ahrens 	return (0);
3014842727c2SChris Kirby }
3015842727c2SChris Kirby 
30163b2aab18SMatthew Ahrens void
30173b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
30183b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3019ca45db41SChris Kirby {
30203b2aab18SMatthew Ahrens 	uint64_t newval;
30213b2aab18SMatthew Ahrens 	uint64_t unique;
30223b2aab18SMatthew Ahrens 	int64_t delta;
3023ca45db41SChris Kirby 
30243b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
30253b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
3026ca45db41SChris Kirby 
30273b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
30283b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
3029a7f53a56SChris Kirby 
30303b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
30313b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
30323b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
30333b2aab18SMatthew Ahrens 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3034c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
30353b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
30363b2aab18SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
30373b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
30383b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3039a7f53a56SChris Kirby 
30403b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
30413b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
3042ca45db41SChris Kirby }
3043ca45db41SChris Kirby 
30443b2aab18SMatthew Ahrens static void
30453b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3046842727c2SChris Kirby {
30473b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
30483b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3049842727c2SChris Kirby 	dsl_dataset_t *ds;
3050842727c2SChris Kirby 
30513b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
30523b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
30533b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3054842727c2SChris Kirby 	dsl_dataset_rele(ds, FTAG);
3055842727c2SChris Kirby }
3056503ad85cSMatthew Ahrens 
3057503ad85cSMatthew Ahrens int
30583b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
30593b2aab18SMatthew Ahrens     uint64_t refreservation)
3060503ad85cSMatthew Ahrens {
30613b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3062503ad85cSMatthew Ahrens 
30633b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
30643b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
30653b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
30663b2aab18SMatthew Ahrens 
30673b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
30687d46dc6cSMatthew Ahrens 	    dsl_dataset_set_refreservation_sync, &ddsqra,
30697d46dc6cSMatthew Ahrens 	    0, ZFS_SPACE_CHECK_NONE));
3070503ad85cSMatthew Ahrens }
307119b94df9SMatthew Ahrens 
307219b94df9SMatthew Ahrens /*
307319b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
307419b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
307519b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
307619b94df9SMatthew Ahrens  * fail and return EINVAL.
307719b94df9SMatthew Ahrens  *
307819b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
307919b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
308019b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
308119b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
308219b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
308319b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
308419b94df9SMatthew Ahrens  *
308519b94df9SMatthew Ahrens  * space freed                         [---------------------]
308619b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
308719b94df9SMatthew Ahrens  *                                         oldsnap            new
308819b94df9SMatthew Ahrens  */
308919b94df9SMatthew Ahrens int
309019b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
309119b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
309219b94df9SMatthew Ahrens {
309319b94df9SMatthew Ahrens 	int err = 0;
309419b94df9SMatthew Ahrens 	uint64_t snapobj;
309519b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
309619b94df9SMatthew Ahrens 
30973b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
30983b2aab18SMatthew Ahrens 
309919b94df9SMatthew Ahrens 	*usedp = 0;
3100c1379625SJustin T. Gibbs 	*usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
3101c1379625SJustin T. Gibbs 	*usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
310219b94df9SMatthew Ahrens 
310319b94df9SMatthew Ahrens 	*compp = 0;
3104c1379625SJustin T. Gibbs 	*compp += dsl_dataset_phys(new)->ds_compressed_bytes;
3105c1379625SJustin T. Gibbs 	*compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
310619b94df9SMatthew Ahrens 
310719b94df9SMatthew Ahrens 	*uncompp = 0;
3108c1379625SJustin T. Gibbs 	*uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
3109c1379625SJustin T. Gibbs 	*uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
311019b94df9SMatthew Ahrens 
311119b94df9SMatthew Ahrens 	snapobj = new->ds_object;
311219b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
311319b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
311419b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
311519b94df9SMatthew Ahrens 
3116ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
3117ad135b5dSChristopher Siden 			snap = new;
3118ad135b5dSChristopher Siden 		} else {
3119ad135b5dSChristopher Siden 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
3120ad135b5dSChristopher Siden 			if (err != 0)
3121ad135b5dSChristopher Siden 				break;
3122ad135b5dSChristopher Siden 		}
312319b94df9SMatthew Ahrens 
3124c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
3125c1379625SJustin T. Gibbs 		    dsl_dataset_phys(oldsnap)->ds_creation_txg) {
312619b94df9SMatthew Ahrens 			/*
312719b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
312819b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
312919b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
313019b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
313119b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
313219b94df9SMatthew Ahrens 			 * optimization itself.
313319b94df9SMatthew Ahrens 			 */
313419b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
313519b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
313619b94df9SMatthew Ahrens 		} else {
313719b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
3138c1379625SJustin T. Gibbs 			    0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
313919b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
314019b94df9SMatthew Ahrens 		}
314119b94df9SMatthew Ahrens 		*usedp += used;
314219b94df9SMatthew Ahrens 		*compp += comp;
314319b94df9SMatthew Ahrens 		*uncompp += uncomp;
314419b94df9SMatthew Ahrens 
314519b94df9SMatthew Ahrens 		/*
314619b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
314719b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
314819b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
314919b94df9SMatthew Ahrens 		 */
3150c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3151ad135b5dSChristopher Siden 		if (snap != new)
3152ad135b5dSChristopher Siden 			dsl_dataset_rele(snap, FTAG);
315319b94df9SMatthew Ahrens 		if (snapobj == 0) {
3154be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
315519b94df9SMatthew Ahrens 			break;
315619b94df9SMatthew Ahrens 		}
315719b94df9SMatthew Ahrens 
315819b94df9SMatthew Ahrens 	}
315919b94df9SMatthew Ahrens 	return (err);
316019b94df9SMatthew Ahrens }
316119b94df9SMatthew Ahrens 
316219b94df9SMatthew Ahrens /*
316319b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
316419b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
316519b94df9SMatthew Ahrens  *
316619b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
316719b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
316819b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
316919b94df9SMatthew Ahrens  *
317019b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
317119b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
317219b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
317319b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
317419b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
317519b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
317619b94df9SMatthew Ahrens  */
317719b94df9SMatthew Ahrens int
317819b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
317919b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
318019b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
318119b94df9SMatthew Ahrens {
318219b94df9SMatthew Ahrens 	int err = 0;
318319b94df9SMatthew Ahrens 	uint64_t snapobj;
318419b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
318519b94df9SMatthew Ahrens 
3186bc9014e6SJustin Gibbs 	ASSERT(firstsnap->ds_is_snapshot);
3187bc9014e6SJustin Gibbs 	ASSERT(lastsnap->ds_is_snapshot);
318819b94df9SMatthew Ahrens 
318919b94df9SMatthew Ahrens 	/*
319019b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
319119b94df9SMatthew Ahrens 	 * is before lastsnap.
319219b94df9SMatthew Ahrens 	 */
319319b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
3194c1379625SJustin T. Gibbs 	    dsl_dataset_phys(firstsnap)->ds_creation_txg >
3195c1379625SJustin T. Gibbs 	    dsl_dataset_phys(lastsnap)->ds_creation_txg)
3196be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
319719b94df9SMatthew Ahrens 
319819b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
319919b94df9SMatthew Ahrens 
3200c1379625SJustin T. Gibbs 	snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
320119b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
320219b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
320319b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
320419b94df9SMatthew Ahrens 
320519b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
320619b94df9SMatthew Ahrens 		if (err != 0)
320719b94df9SMatthew Ahrens 			break;
320819b94df9SMatthew Ahrens 
320919b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
3210c1379625SJustin T. Gibbs 		    dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
321119b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
321219b94df9SMatthew Ahrens 		*usedp += used;
321319b94df9SMatthew Ahrens 		*compp += comp;
321419b94df9SMatthew Ahrens 		*uncompp += uncomp;
321519b94df9SMatthew Ahrens 
3216c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
321719b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
321819b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
321919b94df9SMatthew Ahrens 	}
322019b94df9SMatthew Ahrens 	return (err);
322119b94df9SMatthew Ahrens }
32223b2aab18SMatthew Ahrens 
3223b5152584SMatthew Ahrens static int
3224b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_check(void *arg, dmu_tx_t *tx)
3225b5152584SMatthew Ahrens {
3226b5152584SMatthew Ahrens 	const char *dsname = arg;
3227b5152584SMatthew Ahrens 	dsl_dataset_t *ds;
3228b5152584SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3229b5152584SMatthew Ahrens 	int error = 0;
3230b5152584SMatthew Ahrens 
3231b5152584SMatthew Ahrens 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
3232b5152584SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3233b5152584SMatthew Ahrens 
3234b5152584SMatthew Ahrens 	ASSERT(spa_feature_is_enabled(dp->dp_spa,
3235b5152584SMatthew Ahrens 	    SPA_FEATURE_EXTENSIBLE_DATASET));
3236b5152584SMatthew Ahrens 
3237b5152584SMatthew Ahrens 	error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
3238b5152584SMatthew Ahrens 	if (error != 0)
3239b5152584SMatthew Ahrens 		return (error);
3240b5152584SMatthew Ahrens 
3241b5152584SMatthew Ahrens 	if (ds->ds_large_blocks)
3242b5152584SMatthew Ahrens 		error = EALREADY;
3243b5152584SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3244b5152584SMatthew Ahrens 
3245b5152584SMatthew Ahrens 	return (error);
3246b5152584SMatthew Ahrens }
3247b5152584SMatthew Ahrens 
3248b5152584SMatthew Ahrens void
3249b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync_impl(uint64_t dsobj, dmu_tx_t *tx)
3250b5152584SMatthew Ahrens {
3251b5152584SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
3252b5152584SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
3253b5152584SMatthew Ahrens 	uint64_t zero = 0;
3254b5152584SMatthew Ahrens 
3255b5152584SMatthew Ahrens 	spa_feature_incr(spa, SPA_FEATURE_LARGE_BLOCKS, tx);
3256b5152584SMatthew Ahrens 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
3257b5152584SMatthew Ahrens 
3258b5152584SMatthew Ahrens 	VERIFY0(zap_add(mos, dsobj, DS_FIELD_LARGE_BLOCKS,
3259b5152584SMatthew Ahrens 	    sizeof (zero), 1, &zero, tx));
3260b5152584SMatthew Ahrens }
3261b5152584SMatthew Ahrens 
3262b5152584SMatthew Ahrens static void
3263b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync(void *arg, dmu_tx_t *tx)
3264b5152584SMatthew Ahrens {
3265b5152584SMatthew Ahrens 	const char *dsname = arg;
3266b5152584SMatthew Ahrens 	dsl_dataset_t *ds;
3267b5152584SMatthew Ahrens 
3268b5152584SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dmu_tx_pool(tx), dsname, FTAG, &ds));
3269b5152584SMatthew Ahrens 
3270b5152584SMatthew Ahrens 	dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx);
3271b5152584SMatthew Ahrens 	ASSERT(!ds->ds_large_blocks);
3272b5152584SMatthew Ahrens 	ds->ds_large_blocks = B_TRUE;
3273b5152584SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3274b5152584SMatthew Ahrens }
3275b5152584SMatthew Ahrens 
3276b5152584SMatthew Ahrens int
3277b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks(const char *dsname)
3278b5152584SMatthew Ahrens {
3279b5152584SMatthew Ahrens 	int error;
3280b5152584SMatthew Ahrens 
3281b5152584SMatthew Ahrens 	error = dsl_sync_task(dsname,
3282b5152584SMatthew Ahrens 	    dsl_dataset_activate_large_blocks_check,
3283b5152584SMatthew Ahrens 	    dsl_dataset_activate_large_blocks_sync, (void *)dsname,
3284b5152584SMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED);
3285b5152584SMatthew Ahrens 
3286b5152584SMatthew Ahrens 	/*
3287b5152584SMatthew Ahrens 	 * EALREADY indicates that this dataset already supports large blocks.
3288b5152584SMatthew Ahrens 	 */
3289b5152584SMatthew Ahrens 	if (error == EALREADY)
3290b5152584SMatthew Ahrens 		error = 0;
3291b5152584SMatthew Ahrens 	return (error);
3292b5152584SMatthew Ahrens }
3293b5152584SMatthew Ahrens 
32943b2aab18SMatthew Ahrens /*
32953b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
32963b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
32973b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
32983b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
32993b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
330078f17100SMatthew Ahrens  *
330178f17100SMatthew Ahrens  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
33023b2aab18SMatthew Ahrens  */
33033b2aab18SMatthew Ahrens boolean_t
330478f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
330578f17100SMatthew Ahrens 	uint64_t earlier_txg)
33063b2aab18SMatthew Ahrens {
33073b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
33083b2aab18SMatthew Ahrens 	int error;
33093b2aab18SMatthew Ahrens 	boolean_t ret;
33103b2aab18SMatthew Ahrens 
33113b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
3312bc9014e6SJustin Gibbs 	ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
331378f17100SMatthew Ahrens 
331478f17100SMatthew Ahrens 	if (earlier_txg == 0)
3315c1379625SJustin T. Gibbs 		earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
33163b2aab18SMatthew Ahrens 
3317bc9014e6SJustin Gibbs 	if (later->ds_is_snapshot &&
3318c1379625SJustin T. Gibbs 	    earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
33193b2aab18SMatthew Ahrens 		return (B_FALSE);
33203b2aab18SMatthew Ahrens 
33213b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
33223b2aab18SMatthew Ahrens 		return (B_TRUE);
33233b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
33243b2aab18SMatthew Ahrens 		return (B_FALSE);
33253b2aab18SMatthew Ahrens 
3326c1379625SJustin T. Gibbs 	if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
33273b2aab18SMatthew Ahrens 		return (B_TRUE);
33283b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
33293b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
3330c1379625SJustin T. Gibbs 	    dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
33313b2aab18SMatthew Ahrens 	if (error != 0)
33323b2aab18SMatthew Ahrens 		return (B_FALSE);
333378f17100SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
33343b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
33353b2aab18SMatthew Ahrens 	return (ret);
33363b2aab18SMatthew Ahrens }
33372acef22dSMatthew Ahrens 
33382acef22dSMatthew Ahrens 
33392acef22dSMatthew Ahrens void
33402acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
33412acef22dSMatthew Ahrens {
33422acef22dSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
33432acef22dSMatthew Ahrens 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
33442acef22dSMatthew Ahrens }
3345