xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision b62969f868a827f0823a084bc0af9c7d8b76c659)
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.
233b2aab18SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
244e3c9f44SBill Pijewski  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/dmu_objset.h>
28fa9e4066Sahrens #include <sys/dsl_dataset.h>
29fa9e4066Sahrens #include <sys/dsl_dir.h>
3099653d4eSeschrock #include <sys/dsl_prop.h>
311d452cf5Sahrens #include <sys/dsl_synctask.h>
32fa9e4066Sahrens #include <sys/dmu_traverse.h>
334e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
34fa9e4066Sahrens #include <sys/dmu_tx.h>
35fa9e4066Sahrens #include <sys/arc.h>
36fa9e4066Sahrens #include <sys/zio.h>
37fa9e4066Sahrens #include <sys/zap.h>
38ad135b5dSChristopher Siden #include <sys/zfeature.h>
39fa9e4066Sahrens #include <sys/unique.h>
40fa9e4066Sahrens #include <sys/zfs_context.h>
41cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
42ecd6cf80Smarks #include <sys/spa.h>
43088f3894Sahrens #include <sys/zfs_znode.h>
44c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
45842727c2SChris Kirby #include <sys/zvol.h>
463f9d6ad7SLin Ling #include <sys/dsl_scan.h>
47cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
483b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
493b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
50e1930233Sbonwick 
51cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
52cde58dbcSMatthew Ahrens 	{ \
53cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
54cde58dbcSMatthew Ahrens 		(x) = (y); \
55cde58dbcSMatthew Ahrens 		(y) = __tmp; \
56cde58dbcSMatthew Ahrens 	}
57cde58dbcSMatthew Ahrens 
5855434c77Sek #define	DS_REF_MAX	(1ULL << 62)
59fa9e4066Sahrens 
60fa9e4066Sahrens #define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
61fa9e4066Sahrens 
62a9799022Sck /*
63a9799022Sck  * Figure out how much of this delta should be propogated to the dsl_dir
64a9799022Sck  * layer.  If there's a refreservation, that space has already been
65a9799022Sck  * partially accounted for in our ancestors.
66a9799022Sck  */
67a9799022Sck static int64_t
68a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta)
69a9799022Sck {
70a9799022Sck 	uint64_t old_bytes, new_bytes;
71a9799022Sck 
72a9799022Sck 	if (ds->ds_reserved == 0)
73a9799022Sck 		return (delta);
74a9799022Sck 
75a9799022Sck 	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
76a9799022Sck 	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
77a9799022Sck 
78a9799022Sck 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
79a9799022Sck 	return (new_bytes - old_bytes);
80a9799022Sck }
81fa9e4066Sahrens 
82fa9e4066Sahrens void
83b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
84fa9e4066Sahrens {
85b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
86fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
87fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
88a9799022Sck 	int64_t delta;
89fa9e4066Sahrens 
903f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
91fa9e4066Sahrens 
92fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
93fa9e4066Sahrens 	/* It could have been compressed away to nothing */
94fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
95fa9e4066Sahrens 		return;
96fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
97ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
98fa9e4066Sahrens 	if (ds == NULL) {
99ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
100ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
101fa9e4066Sahrens 		return;
102fa9e4066Sahrens 	}
1033f9d6ad7SLin Ling 
104*b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
105fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
106a9799022Sck 	delta = parent_delta(ds, used);
107ad135b5dSChristopher Siden 	ds->ds_phys->ds_referenced_bytes += used;
108fa9e4066Sahrens 	ds->ds_phys->ds_compressed_bytes += compressed;
109fa9e4066Sahrens 	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
110fa9e4066Sahrens 	ds->ds_phys->ds_unique_bytes += used;
111fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
11274e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
11374e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
11474e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
11574e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
116fa9e4066Sahrens }
117fa9e4066Sahrens 
118cdb0ab79Smaybee int
119b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
120b24ab676SJeff Bonwick     boolean_t async)
121fa9e4066Sahrens {
122fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
123cdb0ab79Smaybee 		return (0);
124fa9e4066Sahrens 
125b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
126b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
127b24ab676SJeff Bonwick 
128b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
129b24ab676SJeff Bonwick 	int compressed = BP_GET_PSIZE(bp);
130b24ab676SJeff Bonwick 	int uncompressed = BP_GET_UCSIZE(bp);
131b24ab676SJeff Bonwick 
132fa9e4066Sahrens 	ASSERT(used > 0);
133fa9e4066Sahrens 	if (ds == NULL) {
134b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
135ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
136ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
137cdb0ab79Smaybee 		return (used);
138fa9e4066Sahrens 	}
139fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
140fa9e4066Sahrens 
14174e7dc98SMatthew Ahrens 	ASSERT(!dsl_dataset_is_snapshot(ds));
142fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
143fa9e4066Sahrens 
144fa9e4066Sahrens 	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
145a9799022Sck 		int64_t delta;
146c717a561Smaybee 
1473f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
148b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
149fa9e4066Sahrens 
150fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
151a9799022Sck 		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
152a9799022Sck 		    !DS_UNIQUE_IS_ACCURATE(ds));
153a9799022Sck 		delta = parent_delta(ds, -used);
154fa9e4066Sahrens 		ds->ds_phys->ds_unique_bytes -= used;
155fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
15674e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
157a9799022Sck 		    delta, -compressed, -uncompressed, tx);
15874e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
15974e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
160fa9e4066Sahrens 	} else {
161fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
162b24ab676SJeff Bonwick 		if (async) {
163b24ab676SJeff Bonwick 			/*
164b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
165b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
166cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
167b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
168b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
169b24ab676SJeff Bonwick 			 */
170cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
171b24ab676SJeff Bonwick 		} else {
172cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
173b24ab676SJeff Bonwick 		}
174a4611edeSahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
175a4611edeSahrens 		    ds->ds_phys->ds_prev_snap_obj);
176a4611edeSahrens 		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
177fa9e4066Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
178a4611edeSahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
179a4611edeSahrens 		    ds->ds_object && bp->blk_birth >
180a4611edeSahrens 		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
181a4611edeSahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
182a4611edeSahrens 			mutex_enter(&ds->ds_prev->ds_lock);
183a4611edeSahrens 			ds->ds_prev->ds_phys->ds_unique_bytes += used;
184a4611edeSahrens 			mutex_exit(&ds->ds_prev->ds_lock);
185fa9e4066Sahrens 		}
1863f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
18774e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
18874e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
18974e7dc98SMatthew Ahrens 		}
190fa9e4066Sahrens 	}
191fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
192ad135b5dSChristopher Siden 	ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
193ad135b5dSChristopher Siden 	ds->ds_phys->ds_referenced_bytes -= used;
194fa9e4066Sahrens 	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
195fa9e4066Sahrens 	ds->ds_phys->ds_compressed_bytes -= compressed;
196fa9e4066Sahrens 	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
197fa9e4066Sahrens 	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
198fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
199cdb0ab79Smaybee 
200cdb0ab79Smaybee 	return (used);
201fa9e4066Sahrens }
202fa9e4066Sahrens 
203ea8dc4b6Seschrock uint64_t
204ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
205fa9e4066Sahrens {
206a2eea2e1Sahrens 	uint64_t trysnap = 0;
207a2eea2e1Sahrens 
208fa9e4066Sahrens 	if (ds == NULL)
209ea8dc4b6Seschrock 		return (0);
210fa9e4066Sahrens 	/*
211fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
212fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
213fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
214fa9e4066Sahrens 	 * consume, which is OK.
215fa9e4066Sahrens 	 *
216fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
217fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
218fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
219fa9e4066Sahrens 	 */
220a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
221a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
222a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
223a2eea2e1Sahrens 	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
224ea8dc4b6Seschrock }
225ea8dc4b6Seschrock 
2263d692628SSanjeev Bagewadi boolean_t
227c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
228c7cd2421SGeorge Wilson     uint64_t blk_birth)
229ea8dc4b6Seschrock {
230c7cd2421SGeorge Wilson 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
231c7cd2421SGeorge Wilson 		return (B_FALSE);
232c7cd2421SGeorge Wilson 
233837b568bSGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
234c7cd2421SGeorge Wilson 
235c7cd2421SGeorge Wilson 	return (B_TRUE);
236fa9e4066Sahrens }
237fa9e4066Sahrens 
238fa9e4066Sahrens /* ARGSUSED */
239fa9e4066Sahrens static void
240fa9e4066Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv)
241fa9e4066Sahrens {
242fa9e4066Sahrens 	dsl_dataset_t *ds = dsv;
243fa9e4066Sahrens 
2443b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
245fa9e4066Sahrens 
24691ebeef5Sahrens 	unique_remove(ds->ds_fsid_guid);
247fa9e4066Sahrens 
248503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
249503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
250fa9e4066Sahrens 
251fa9e4066Sahrens 	if (ds->ds_prev) {
2523b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
253fa9e4066Sahrens 		ds->ds_prev = NULL;
254fa9e4066Sahrens 	}
255fa9e4066Sahrens 
256cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
2573b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_deadlist_obj != 0)
258cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
259745cd3c5Smaybee 	if (ds->ds_dir)
2603b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
261fa9e4066Sahrens 
26291ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
263fa9e4066Sahrens 
2645ad82045Snd 	mutex_destroy(&ds->ds_lock);
26591ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
2663b2aab18SMatthew Ahrens 	refcount_destroy(&ds->ds_longholds);
2675ad82045Snd 
268fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
269fa9e4066Sahrens }
270fa9e4066Sahrens 
2713b2aab18SMatthew Ahrens int
272fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
273fa9e4066Sahrens {
274fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
275fa9e4066Sahrens 	int err;
276fa9e4066Sahrens 	dmu_buf_t *headdbuf;
277fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
278fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
279fa9e4066Sahrens 
280fa9e4066Sahrens 	if (ds->ds_snapname[0])
281ea8dc4b6Seschrock 		return (0);
282fa9e4066Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
283ea8dc4b6Seschrock 		return (0);
284fa9e4066Sahrens 
285ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
286ea8dc4b6Seschrock 	    FTAG, &headdbuf);
2873b2aab18SMatthew Ahrens 	if (err != 0)
288ea8dc4b6Seschrock 		return (err);
289fa9e4066Sahrens 	headphys = headdbuf->db_data;
290fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
291e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
292ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
293ea8dc4b6Seschrock 	return (err);
294fa9e4066Sahrens }
295fa9e4066Sahrens 
2963b2aab18SMatthew Ahrens int
297745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
298ab04eb8eStimh {
299745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
300745cd3c5Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
301ab04eb8eStimh 	matchtype_t mt;
302ab04eb8eStimh 	int err;
303ab04eb8eStimh 
304745cd3c5Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
305ab04eb8eStimh 		mt = MT_FIRST;
306ab04eb8eStimh 	else
307ab04eb8eStimh 		mt = MT_EXACT;
308ab04eb8eStimh 
309745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
310ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
311ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
312745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
313ab04eb8eStimh 	return (err);
314ab04eb8eStimh }
315ab04eb8eStimh 
3163b2aab18SMatthew Ahrens int
3173b2aab18SMatthew Ahrens dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
318ab04eb8eStimh {
319745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
320745cd3c5Smaybee 	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
321ab04eb8eStimh 	matchtype_t mt;
322ab04eb8eStimh 	int err;
323ab04eb8eStimh 
32471eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
32571eb0538SChris Kirby 
326745cd3c5Smaybee 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
327ab04eb8eStimh 		mt = MT_FIRST;
328ab04eb8eStimh 	else
329ab04eb8eStimh 		mt = MT_EXACT;
330ab04eb8eStimh 
331745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
332ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
333745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
334ab04eb8eStimh 	return (err);
335ab04eb8eStimh }
336ab04eb8eStimh 
3373b2aab18SMatthew Ahrens int
3383b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
339745cd3c5Smaybee     dsl_dataset_t **dsp)
340fa9e4066Sahrens {
341fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
342fa9e4066Sahrens 	dmu_buf_t *dbuf;
343fa9e4066Sahrens 	dsl_dataset_t *ds;
344ea8dc4b6Seschrock 	int err;
345a7f53a56SChris Kirby 	dmu_object_info_t doi;
346fa9e4066Sahrens 
3473b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
348fa9e4066Sahrens 
349ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
3503b2aab18SMatthew Ahrens 	if (err != 0)
351ea8dc4b6Seschrock 		return (err);
352a7f53a56SChris Kirby 
353a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
354a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
355b287be1bSWill Andrews 	if (doi.doi_type != DMU_OT_DSL_DATASET) {
356b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
357be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
358b287be1bSWill Andrews 	}
359a7f53a56SChris Kirby 
360fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
361fa9e4066Sahrens 	if (ds == NULL) {
362d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
363fa9e4066Sahrens 
364fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
365fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
366fa9e4066Sahrens 		ds->ds_object = dsobj;
367fa9e4066Sahrens 		ds->ds_phys = dbuf->db_data;
368fa9e4066Sahrens 
3695ad82045Snd 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
37091ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
3714e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
3723b2aab18SMatthew Ahrens 		refcount_create(&ds->ds_longholds);
3735ad82045Snd 
374cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
375cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
376fa9e4066Sahrens 		    mos, ds->ds_phys->ds_deadlist_obj);
377cde58dbcSMatthew Ahrens 
3784e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
3794e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
3804e3c9f44SBill Pijewski 
381ea8dc4b6Seschrock 		if (err == 0) {
3823b2aab18SMatthew Ahrens 			err = dsl_dir_hold_obj(dp,
383ea8dc4b6Seschrock 			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
384ea8dc4b6Seschrock 		}
3853b2aab18SMatthew Ahrens 		if (err != 0) {
3865ad82045Snd 			mutex_destroy(&ds->ds_lock);
38791ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
3883b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
389cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
390cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
391ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
392ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
393ea8dc4b6Seschrock 			return (err);
394ea8dc4b6Seschrock 		}
395fa9e4066Sahrens 
39674e7dc98SMatthew Ahrens 		if (!dsl_dataset_is_snapshot(ds)) {
397fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
3983b2aab18SMatthew Ahrens 			if (ds->ds_phys->ds_prev_snap_obj != 0) {
3993b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
400745cd3c5Smaybee 				    ds->ds_phys->ds_prev_snap_obj,
401745cd3c5Smaybee 				    ds, &ds->ds_prev);
402fa9e4066Sahrens 			}
403842727c2SChris Kirby 		} else {
404842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
405842727c2SChris Kirby 				err = dsl_dataset_get_snapname(ds);
406842727c2SChris Kirby 			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
407842727c2SChris Kirby 				err = zap_count(
408842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
409842727c2SChris Kirby 				    ds->ds_phys->ds_userrefs_obj,
410842727c2SChris Kirby 				    &ds->ds_userrefs);
411842727c2SChris Kirby 			}
412fa9e4066Sahrens 		}
413fa9e4066Sahrens 
41474e7dc98SMatthew Ahrens 		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
4153b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
4163b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4173b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
418cb625fb5Sck 			if (err == 0) {
4193b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
4203b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4213b2aab18SMatthew Ahrens 				    &ds->ds_quota);
422cb625fb5Sck 			}
423cb625fb5Sck 		} else {
424cb625fb5Sck 			ds->ds_reserved = ds->ds_quota = 0;
425cb625fb5Sck 		}
426cb625fb5Sck 
427d5285caeSGeorge Wilson 		if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
428d5285caeSGeorge Wilson 		    &ds->ds_phys, dsl_dataset_evict)) != NULL) {
429cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
430cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
431745cd3c5Smaybee 			if (ds->ds_prev)
4323b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
4333b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
4345ad82045Snd 			mutex_destroy(&ds->ds_lock);
43591ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
4363b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
437fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
4383b2aab18SMatthew Ahrens 			if (err != 0) {
439ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
440ea8dc4b6Seschrock 				return (err);
441ea8dc4b6Seschrock 			}
442fa9e4066Sahrens 			ds = winner;
443fa9e4066Sahrens 		} else {
44491ebeef5Sahrens 			ds->ds_fsid_guid =
445fa9e4066Sahrens 			    unique_insert(ds->ds_phys->ds_fsid_guid);
446fa9e4066Sahrens 		}
447fa9e4066Sahrens 	}
448fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
449fa9e4066Sahrens 	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
450088f3894Sahrens 	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
451afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
45284db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
453ea8dc4b6Seschrock 	*dsp = ds;
454ea8dc4b6Seschrock 	return (0);
455fa9e4066Sahrens }
456fa9e4066Sahrens 
457745cd3c5Smaybee int
4583b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name,
459503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
460fa9e4066Sahrens {
461fa9e4066Sahrens 	dsl_dir_t *dd;
462745cd3c5Smaybee 	const char *snapname;
463fa9e4066Sahrens 	uint64_t obj;
464fa9e4066Sahrens 	int err = 0;
465fa9e4066Sahrens 
4663b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
4673b2aab18SMatthew Ahrens 	if (err != 0)
468ea8dc4b6Seschrock 		return (err);
469fa9e4066Sahrens 
4703b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
471fa9e4066Sahrens 	obj = dd->dd_phys->dd_head_dataset_obj;
4723b2aab18SMatthew Ahrens 	if (obj != 0)
4733b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
474745cd3c5Smaybee 	else
475be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
476fa9e4066Sahrens 
477745cd3c5Smaybee 	/* we may be looking for a snapshot */
478745cd3c5Smaybee 	if (err == 0 && snapname != NULL) {
4793b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
480fa9e4066Sahrens 
481745cd3c5Smaybee 		if (*snapname++ != '@') {
482745cd3c5Smaybee 			dsl_dataset_rele(*dsp, tag);
4833b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
484be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
485fa9e4066Sahrens 		}
486fa9e4066Sahrens 
487745cd3c5Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
488745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
489745cd3c5Smaybee 		if (err == 0)
4903b2aab18SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
491745cd3c5Smaybee 		dsl_dataset_rele(*dsp, tag);
492745cd3c5Smaybee 
4933b2aab18SMatthew Ahrens 		if (err == 0) {
494745cd3c5Smaybee 			mutex_enter(&ds->ds_lock);
495745cd3c5Smaybee 			if (ds->ds_snapname[0] == 0)
496745cd3c5Smaybee 				(void) strlcpy(ds->ds_snapname, snapname,
497745cd3c5Smaybee 				    sizeof (ds->ds_snapname));
498745cd3c5Smaybee 			mutex_exit(&ds->ds_lock);
4993b2aab18SMatthew Ahrens 			*dsp = ds;
500fa9e4066Sahrens 		}
501fa9e4066Sahrens 	}
5023b2aab18SMatthew Ahrens 
5033b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
504fa9e4066Sahrens 	return (err);
505fa9e4066Sahrens }
506fa9e4066Sahrens 
507fa9e4066Sahrens int
5083b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
5093b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
5103b2aab18SMatthew Ahrens {
5113b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
5123b2aab18SMatthew Ahrens 	if (err != 0)
5133b2aab18SMatthew Ahrens 		return (err);
5143b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
5153b2aab18SMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
5163b2aab18SMatthew Ahrens 		*dsp = NULL;
517be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
5183b2aab18SMatthew Ahrens 	}
5193b2aab18SMatthew Ahrens 	return (0);
5203b2aab18SMatthew Ahrens }
5213b2aab18SMatthew Ahrens 
5223b2aab18SMatthew Ahrens int
5233b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name,
524503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
525fa9e4066Sahrens {
5263b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold(dp, name, tag, dsp);
5273b2aab18SMatthew Ahrens 	if (err != 0)
528745cd3c5Smaybee 		return (err);
5293b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
530503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
531be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
532745cd3c5Smaybee 	}
533745cd3c5Smaybee 	return (0);
534fa9e4066Sahrens }
535fa9e4066Sahrens 
5363b2aab18SMatthew Ahrens /*
5373b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
5383b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
5393b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
5403b2aab18SMatthew Ahrens  *
5413b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
5423b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
5433b2aab18SMatthew Ahrens  * and accessed.
5443b2aab18SMatthew Ahrens  */
5453b2aab18SMatthew Ahrens void
5463b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
5473b2aab18SMatthew Ahrens {
5483b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
5493b2aab18SMatthew Ahrens 	(void) refcount_add(&ds->ds_longholds, tag);
5503b2aab18SMatthew Ahrens }
5513b2aab18SMatthew Ahrens 
5523b2aab18SMatthew Ahrens void
5533b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
5543b2aab18SMatthew Ahrens {
5553b2aab18SMatthew Ahrens 	(void) refcount_remove(&ds->ds_longholds, tag);
5563b2aab18SMatthew Ahrens }
5573b2aab18SMatthew Ahrens 
5583b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
5593b2aab18SMatthew Ahrens boolean_t
5603b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
5613b2aab18SMatthew Ahrens {
5623b2aab18SMatthew Ahrens 	return (!refcount_is_zero(&ds->ds_longholds));
5633b2aab18SMatthew Ahrens }
5643b2aab18SMatthew Ahrens 
565fa9e4066Sahrens void
566fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
567fa9e4066Sahrens {
568fa9e4066Sahrens 	if (ds == NULL) {
569fa9e4066Sahrens 		(void) strcpy(name, "mos");
570fa9e4066Sahrens 	} else {
571fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
5723b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
573fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
574fa9e4066Sahrens 			(void) strcat(name, "@");
575745cd3c5Smaybee 			/*
576745cd3c5Smaybee 			 * We use a "recursive" mutex so that we
577745cd3c5Smaybee 			 * can call dprintf_ds() with ds_lock held.
578745cd3c5Smaybee 			 */
579fa9e4066Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
580fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
581fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
582fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
583fa9e4066Sahrens 			} else {
584fa9e4066Sahrens 				(void) strcat(name, ds->ds_snapname);
585fa9e4066Sahrens 			}
586fa9e4066Sahrens 		}
587fa9e4066Sahrens 	}
588fa9e4066Sahrens }
589fa9e4066Sahrens 
5903cb34c60Sahrens void
591745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
5923cb34c60Sahrens {
5933b2aab18SMatthew Ahrens 	dmu_buf_rele(ds->ds_dbuf, tag);
594745cd3c5Smaybee }
595745cd3c5Smaybee 
596745cd3c5Smaybee void
597503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
598745cd3c5Smaybee {
5993b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
600745cd3c5Smaybee 
6013cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
602745cd3c5Smaybee 	ds->ds_owner = NULL;
6033cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
6043b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
6053b2aab18SMatthew Ahrens 	if (ds->ds_dbuf != NULL)
6063b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, tag);
607745cd3c5Smaybee 	else
608cde58dbcSMatthew Ahrens 		dsl_dataset_evict(NULL, ds);
6093cb34c60Sahrens }
6103cb34c60Sahrens 
6113cb34c60Sahrens boolean_t
6123b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
6133cb34c60Sahrens {
614745cd3c5Smaybee 	boolean_t gotit = FALSE;
615745cd3c5Smaybee 
6163cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
6173b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
618503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
6193b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
620745cd3c5Smaybee 		gotit = TRUE;
6213cb34c60Sahrens 	}
6223cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
623745cd3c5Smaybee 	return (gotit);
624745cd3c5Smaybee }
625745cd3c5Smaybee 
6261d452cf5Sahrens uint64_t
627088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
628ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
629fa9e4066Sahrens {
6303cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
631fa9e4066Sahrens 	dmu_buf_t *dbuf;
632fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
6333cb34c60Sahrens 	uint64_t dsobj;
634fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
635fa9e4066Sahrens 
636088f3894Sahrens 	if (origin == NULL)
637088f3894Sahrens 		origin = dp->dp_origin_snap;
638088f3894Sahrens 
6393cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
6403cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
641fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
6423cb34c60Sahrens 	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
643fa9e4066Sahrens 
6441649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
6451649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
6463b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
647fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
648fa9e4066Sahrens 	dsphys = dbuf->db_data;
649745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
650fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
651ab04eb8eStimh 	dsphys->ds_flags = flags;
652fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
653fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
654fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
655fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
656ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
657ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
658fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
659088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
660a9799022Sck 
661cde58dbcSMatthew Ahrens 	if (origin == NULL) {
662cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
663cde58dbcSMatthew Ahrens 	} else {
6643b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
665cde58dbcSMatthew Ahrens 
6663cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
667fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
6683cb34c60Sahrens 		    origin->ds_phys->ds_creation_txg;
669ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
670ad135b5dSChristopher Siden 		    origin->ds_phys->ds_referenced_bytes;
671fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
6723cb34c60Sahrens 		    origin->ds_phys->ds_compressed_bytes;
673fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
6743cb34c60Sahrens 		    origin->ds_phys->ds_uncompressed_bytes;
6753cb34c60Sahrens 		dsphys->ds_bp = origin->ds_phys->ds_bp;
676579ae4d5Stimh 		dsphys->ds_flags |= origin->ds_phys->ds_flags;
677fa9e4066Sahrens 
6783cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
6793cb34c60Sahrens 		origin->ds_phys->ds_num_children++;
680fa9e4066Sahrens 
6813b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
682cde58dbcSMatthew Ahrens 		    origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
683cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
684cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
685cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
686cde58dbcSMatthew Ahrens 
687088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
688088f3894Sahrens 			if (origin->ds_phys->ds_next_clones_obj == 0) {
689088f3894Sahrens 				origin->ds_phys->ds_next_clones_obj =
690088f3894Sahrens 				    zap_create(mos,
691088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
692088f3894Sahrens 			}
6933b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
6943b2aab18SMatthew Ahrens 			    origin->ds_phys->ds_next_clones_obj, dsobj, tx));
695088f3894Sahrens 		}
696088f3894Sahrens 
697fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
6983cb34c60Sahrens 		dd->dd_phys->dd_origin_obj = origin->ds_object;
699cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
700cde58dbcSMatthew Ahrens 			if (origin->ds_dir->dd_phys->dd_clones == 0) {
701cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
702cde58dbcSMatthew Ahrens 				origin->ds_dir->dd_phys->dd_clones =
703cde58dbcSMatthew Ahrens 				    zap_create(mos,
704cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
705cde58dbcSMatthew Ahrens 			}
7063b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
707cde58dbcSMatthew Ahrens 			    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
708cde58dbcSMatthew Ahrens 		}
709fa9e4066Sahrens 	}
710ab04eb8eStimh 
711ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
712ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
713ab04eb8eStimh 
714ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
715fa9e4066Sahrens 
716fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
717fa9e4066Sahrens 	dd->dd_phys->dd_head_dataset_obj = dsobj;
7183cb34c60Sahrens 
7193cb34c60Sahrens 	return (dsobj);
7203cb34c60Sahrens }
7213cb34c60Sahrens 
7223b2aab18SMatthew Ahrens static void
7233b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
7243b2aab18SMatthew Ahrens {
7253b2aab18SMatthew Ahrens 	objset_t *os;
7263b2aab18SMatthew Ahrens 
7273b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
7283b2aab18SMatthew Ahrens 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
7293b2aab18SMatthew Ahrens 	dsl_dataset_dirty(ds, tx);
7303b2aab18SMatthew Ahrens }
7313b2aab18SMatthew Ahrens 
7323cb34c60Sahrens uint64_t
733ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
734ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
7353cb34c60Sahrens {
7363cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
7373cb34c60Sahrens 	uint64_t dsobj, ddobj;
7383cb34c60Sahrens 	dsl_dir_t *dd;
7393cb34c60Sahrens 
7403b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
7413cb34c60Sahrens 	ASSERT(lastname[0] != '@');
7423cb34c60Sahrens 
743088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
7443b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
7453cb34c60Sahrens 
7463b2aab18SMatthew Ahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
7473b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
7483cb34c60Sahrens 
7493cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
7503cb34c60Sahrens 
7513b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
752fa9e4066Sahrens 
753feaa74e4SMark Maybee 	/*
754feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
755feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
756feaa74e4SMark Maybee 	 */
7573b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
758feaa74e4SMark Maybee 		dsl_dataset_t *ds;
759feaa74e4SMark Maybee 
7603b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
7613b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
762feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
763feaa74e4SMark Maybee 	}
764feaa74e4SMark Maybee 
7651d452cf5Sahrens 	return (dsobj);
766fa9e4066Sahrens }
767fa9e4066Sahrens 
7681d452cf5Sahrens /*
7693b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
7703b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
7713b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
7723b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
7733b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
7743b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
7751d452cf5Sahrens  */
7763b2aab18SMatthew Ahrens void
7773b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
7781d452cf5Sahrens {
7793b2aab18SMatthew Ahrens 	uint64_t mrs_used;
7803b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
7811d452cf5Sahrens 
7823b2aab18SMatthew Ahrens 	ASSERT(!dsl_dataset_is_snapshot(ds));
7831d452cf5Sahrens 
7843b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0)
7853b2aab18SMatthew Ahrens 		mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
7863b2aab18SMatthew Ahrens 	else
7873b2aab18SMatthew Ahrens 		mrs_used = 0;
788842727c2SChris Kirby 
7893b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
790fa9e4066Sahrens 
7913b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
7923b2aab18SMatthew Ahrens 	ds->ds_phys->ds_unique_bytes =
7933b2aab18SMatthew Ahrens 	    ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
79419b94df9SMatthew Ahrens 
7953b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
7963b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
7973b2aab18SMatthew Ahrens 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
798fa9e4066Sahrens }
799fa9e4066Sahrens 
8003b2aab18SMatthew Ahrens void
8013b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
8023b2aab18SMatthew Ahrens     dmu_tx_t *tx)
803842727c2SChris Kirby {
8043b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
8053b2aab18SMatthew Ahrens 	uint64_t count;
8063b2aab18SMatthew Ahrens 	int err;
8073b2aab18SMatthew Ahrens 
8083b2aab18SMatthew Ahrens 	ASSERT(ds->ds_phys->ds_num_children >= 2);
8093b2aab18SMatthew Ahrens 	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
8103b2aab18SMatthew Ahrens 	/*
8113b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
8123b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
8133b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
8143b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
8153b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
8163b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
8173b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
8183b2aab18SMatthew Ahrens 	 * remove this one.
8193b2aab18SMatthew Ahrens 	 */
8203b2aab18SMatthew Ahrens 	if (err != ENOENT)
8213b2aab18SMatthew Ahrens 		VERIFY0(err);
8223b2aab18SMatthew Ahrens 	ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
8233b2aab18SMatthew Ahrens 	    &count));
8243b2aab18SMatthew Ahrens 	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
8253b2aab18SMatthew Ahrens }
826842727c2SChris Kirby 
827842727c2SChris Kirby 
8283b2aab18SMatthew Ahrens blkptr_t *
8293b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds)
8303b2aab18SMatthew Ahrens {
8313b2aab18SMatthew Ahrens 	return (&ds->ds_phys->ds_bp);
832842727c2SChris Kirby }
833842727c2SChris Kirby 
8343b2aab18SMatthew Ahrens void
8353b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
836842727c2SChris Kirby {
8373b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
8383b2aab18SMatthew Ahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
8393b2aab18SMatthew Ahrens 	if (ds == NULL) {
8403b2aab18SMatthew Ahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
8413b2aab18SMatthew Ahrens 	} else {
8423b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
8433b2aab18SMatthew Ahrens 		ds->ds_phys->ds_bp = *bp;
844842727c2SChris Kirby 	}
8453b2aab18SMatthew Ahrens }
846842727c2SChris Kirby 
8473b2aab18SMatthew Ahrens spa_t *
8483b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
8493b2aab18SMatthew Ahrens {
8503b2aab18SMatthew Ahrens 	return (ds->ds_dir->dd_pool->dp_spa);
851842727c2SChris Kirby }
852842727c2SChris Kirby 
8533b2aab18SMatthew Ahrens void
8543b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
855fa9e4066Sahrens {
8563b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
857842727c2SChris Kirby 
8583b2aab18SMatthew Ahrens 	if (ds == NULL) /* this is the meta-objset */
8593b2aab18SMatthew Ahrens 		return;
8601d452cf5Sahrens 
8613b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
862fa9e4066Sahrens 
8633b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_next_snap_obj != 0)
8643b2aab18SMatthew Ahrens 		panic("dirtying snapshot!");
865fa9e4066Sahrens 
8663b2aab18SMatthew Ahrens 	dp = ds->ds_dir->dd_pool;
867ce636f8bSMatthew Ahrens 
8683b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
8693b2aab18SMatthew Ahrens 		/* up the hold count until we can be written out */
8703b2aab18SMatthew Ahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
8713b2aab18SMatthew Ahrens 	}
8723b2aab18SMatthew Ahrens }
873fa9e4066Sahrens 
8742e2c1355SMatthew Ahrens boolean_t
8752e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
8762e2c1355SMatthew Ahrens {
8772e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
8782e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
8792e2c1355SMatthew Ahrens 		    ds, t))
8802e2c1355SMatthew Ahrens 			return (B_TRUE);
8812e2c1355SMatthew Ahrens 	}
8822e2c1355SMatthew Ahrens 	return (B_FALSE);
8832e2c1355SMatthew Ahrens }
8842e2c1355SMatthew Ahrens 
885fa9e4066Sahrens static int
8863b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
887fa9e4066Sahrens {
8883b2aab18SMatthew Ahrens 	uint64_t asize;
889fa9e4066Sahrens 
8903b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
89188b7b0f2SMatthew Ahrens 		return (0);
892fa9e4066Sahrens 
893e1930233Sbonwick 	/*
8943b2aab18SMatthew Ahrens 	 * If there's an fs-only reservation, any blocks that might become
8953b2aab18SMatthew Ahrens 	 * owned by the snapshot dataset must be accommodated by space
8963b2aab18SMatthew Ahrens 	 * outside of the reservation.
897e1930233Sbonwick 	 */
8983b2aab18SMatthew Ahrens 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
8993b2aab18SMatthew Ahrens 	asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
9003b2aab18SMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
901be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
902e1930233Sbonwick 
9033cb34c60Sahrens 	/*
9043b2aab18SMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
9053b2aab18SMatthew Ahrens 	 * snapshot checks in this sync group.
9063cb34c60Sahrens 	 */
9073b2aab18SMatthew Ahrens 	if (asize > 0)
9083b2aab18SMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
9093cb34c60Sahrens 
910e1930233Sbonwick 	return (0);
911e1930233Sbonwick }
912e1930233Sbonwick 
9133b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg {
9143b2aab18SMatthew Ahrens 	nvlist_t *ddsa_snaps;
9153b2aab18SMatthew Ahrens 	nvlist_t *ddsa_props;
9163b2aab18SMatthew Ahrens 	nvlist_t *ddsa_errors;
9173b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t;
918842727c2SChris Kirby 
9193cb34c60Sahrens int
9203b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
921ca48f36fSKeith M Wesolowski     dmu_tx_t *tx, boolean_t recv)
9221d452cf5Sahrens {
9233b2aab18SMatthew Ahrens 	int error;
9243b2aab18SMatthew Ahrens 	uint64_t value;
925fa9e4066Sahrens 
9263b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
927745cd3c5Smaybee 
9283b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
929842727c2SChris Kirby 		return (0);
930fa9e4066Sahrens 
931fa9e4066Sahrens 	/*
9323b2aab18SMatthew Ahrens 	 * We don't allow multiple snapshots of the same txg.  If there
9333b2aab18SMatthew Ahrens 	 * is already one, try again.
934fa9e4066Sahrens 	 */
9353b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
936be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
937fa9e4066Sahrens 
938fa9e4066Sahrens 	/*
9393b2aab18SMatthew Ahrens 	 * Check for conflicting snapshot name.
940fa9e4066Sahrens 	 */
9413b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
9423b2aab18SMatthew Ahrens 	if (error == 0)
943be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
9443b2aab18SMatthew Ahrens 	if (error != ENOENT)
9453b2aab18SMatthew Ahrens 		return (error);
946842727c2SChris Kirby 
947ca48f36fSKeith M Wesolowski 	/*
948ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
949ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
950ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
951ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
952ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
953ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
954ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
955ca48f36fSKeith M Wesolowski 	 */
956ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
957ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
958ca48f36fSKeith M Wesolowski 
9593b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
9603b2aab18SMatthew Ahrens 	if (error != 0)
9613b2aab18SMatthew Ahrens 		return (error);
962842727c2SChris Kirby 
9631d452cf5Sahrens 	return (0);
9641d452cf5Sahrens }
9651d452cf5Sahrens 
9663b2aab18SMatthew Ahrens static int
9673b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
968745cd3c5Smaybee {
9693b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
9703b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9713b2aab18SMatthew Ahrens 	nvpair_t *pair;
9723b2aab18SMatthew Ahrens 	int rv = 0;
9733b2aab18SMatthew Ahrens 
9743b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
9753b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
9763b2aab18SMatthew Ahrens 		int error = 0;
9773b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
9783b2aab18SMatthew Ahrens 		char *name, *atp;
9793b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
9803b2aab18SMatthew Ahrens 
9813b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
9823b2aab18SMatthew Ahrens 		if (strlen(name) >= MAXNAMELEN)
983be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
9843b2aab18SMatthew Ahrens 		if (error == 0) {
9853b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
9863b2aab18SMatthew Ahrens 			if (atp == NULL)
987be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
9883b2aab18SMatthew Ahrens 			if (error == 0)
9893b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
9903b2aab18SMatthew Ahrens 		}
9913b2aab18SMatthew Ahrens 		if (error == 0)
9923b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
9933b2aab18SMatthew Ahrens 		if (error == 0) {
9943b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
995ca48f36fSKeith M Wesolowski 			    atp + 1, tx, B_FALSE);
9963b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
9973b2aab18SMatthew Ahrens 		}
998745cd3c5Smaybee 
9993b2aab18SMatthew Ahrens 		if (error != 0) {
10003b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
10013b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
10023b2aab18SMatthew Ahrens 				    name, error);
10033b2aab18SMatthew Ahrens 			}
10043b2aab18SMatthew Ahrens 			rv = error;
10053b2aab18SMatthew Ahrens 		}
10063b2aab18SMatthew Ahrens 	}
10073b2aab18SMatthew Ahrens 	return (rv);
1008745cd3c5Smaybee }
1009745cd3c5Smaybee 
10103b2aab18SMatthew Ahrens void
10113b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
10123b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1013745cd3c5Smaybee {
10143b2aab18SMatthew Ahrens 	static zil_header_t zero_zil;
1015745cd3c5Smaybee 
10163b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
10173b2aab18SMatthew Ahrens 	dmu_buf_t *dbuf;
10183b2aab18SMatthew Ahrens 	dsl_dataset_phys_t *dsphys;
10193b2aab18SMatthew Ahrens 	uint64_t dsobj, crtxg;
10203b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
10213b2aab18SMatthew Ahrens 	objset_t *os;
1022745cd3c5Smaybee 
10233b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1024c33e334fSMatthew Ahrens 
1025c33e334fSMatthew Ahrens 	/*
10263b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
10273b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1028c33e334fSMatthew Ahrens 	 */
10293b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
10303b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
10313b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
10323b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
1033c33e334fSMatthew Ahrens 
1034cde58dbcSMatthew Ahrens 
1035cde58dbcSMatthew Ahrens 	/*
10363b2aab18SMatthew Ahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1037088f3894Sahrens 	 */
1038088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1039088f3894Sahrens 		crtxg = 1;
1040088f3894Sahrens 	else
1041088f3894Sahrens 		crtxg = tx->tx_txg;
1042088f3894Sahrens 
10431649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
10441649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
10453b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1046fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1047fa9e4066Sahrens 	dsphys = dbuf->db_data;
1048745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
10491d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1050fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1051fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1052fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
1053fa9e4066Sahrens 	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
1054fa9e4066Sahrens 	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
1055fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1056fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1057fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1058088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
1059fa9e4066Sahrens 	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
1060ad135b5dSChristopher Siden 	dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
1061fa9e4066Sahrens 	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
1062fa9e4066Sahrens 	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
106399653d4eSeschrock 	dsphys->ds_flags = ds->ds_phys->ds_flags;
1064fa9e4066Sahrens 	dsphys->ds_bp = ds->ds_phys->ds_bp;
1065ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1066fa9e4066Sahrens 
10671d452cf5Sahrens 	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
10681d452cf5Sahrens 	if (ds->ds_prev) {
1069088f3894Sahrens 		uint64_t next_clones_obj =
1070088f3894Sahrens 		    ds->ds_prev->ds_phys->ds_next_clones_obj;
10711d452cf5Sahrens 		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
1072fa9e4066Sahrens 		    ds->ds_object ||
10731d452cf5Sahrens 		    ds->ds_prev->ds_phys->ds_num_children > 1);
10741d452cf5Sahrens 		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
10751d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1076fa9e4066Sahrens 			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
10771d452cf5Sahrens 			    ds->ds_prev->ds_phys->ds_creation_txg);
10781d452cf5Sahrens 			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
1079088f3894Sahrens 		} else if (next_clones_obj != 0) {
10803b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1081c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
10823b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1083088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1084fa9e4066Sahrens 		}
1085fa9e4066Sahrens 	}
1086fa9e4066Sahrens 
1087a9799022Sck 	/*
1088a9799022Sck 	 * If we have a reference-reservation on this dataset, we will
1089a9799022Sck 	 * need to increase the amount of refreservation being charged
1090a9799022Sck 	 * since our unique space is going to zero.
1091a9799022Sck 	 */
1092a9799022Sck 	if (ds->ds_reserved) {
10933f9d6ad7SLin Ling 		int64_t delta;
10943f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
10953f9d6ad7SLin Ling 		delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
109674e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
10973f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1098a9799022Sck 	}
1099a9799022Sck 
1100fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1101cde58dbcSMatthew Ahrens 	ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
1102cde58dbcSMatthew Ahrens 	    UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
1103cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1104cde58dbcSMatthew Ahrens 	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
1105cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
1106cde58dbcSMatthew Ahrens 	    ds->ds_phys->ds_prev_snap_txg, tx);
1107cde58dbcSMatthew Ahrens 
1108a4611edeSahrens 	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
1109fa9e4066Sahrens 	ds->ds_phys->ds_prev_snap_obj = dsobj;
1110088f3894Sahrens 	ds->ds_phys->ds_prev_snap_txg = crtxg;
1111fa9e4066Sahrens 	ds->ds_phys->ds_unique_bytes = 0;
1112a9799022Sck 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1113a9799022Sck 		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1114fa9e4066Sahrens 
11153b2aab18SMatthew Ahrens 	VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
11163b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1117fa9e4066Sahrens 
1118fa9e4066Sahrens 	if (ds->ds_prev)
11193b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
11203b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
1121745cd3c5Smaybee 	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
1122ecd6cf80Smarks 
11233f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1124088f3894Sahrens 
112571eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
112671eb0538SChris Kirby 
11274445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1128fa9e4066Sahrens }
1129fa9e4066Sahrens 
11303b2aab18SMatthew Ahrens static void
11313b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1132fa9e4066Sahrens {
11333b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
11343b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11353b2aab18SMatthew Ahrens 	nvpair_t *pair;
113691ebeef5Sahrens 
11373b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
11383b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
11393b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
11403b2aab18SMatthew Ahrens 		char *name, *atp;
11413b2aab18SMatthew Ahrens 		char dsname[MAXNAMELEN];
11423b2aab18SMatthew Ahrens 
11433b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
11443b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
11453b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
11463b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
11473b2aab18SMatthew Ahrens 
11483b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
11493b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
11503b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
11513b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
11523b2aab18SMatthew Ahrens 		}
11533b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
11543b2aab18SMatthew Ahrens 	}
1155fa9e4066Sahrens }
1156fa9e4066Sahrens 
11573b2aab18SMatthew Ahrens /*
11583b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
11593b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
11603b2aab18SMatthew Ahrens  */
11613b2aab18SMatthew Ahrens int
11623b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
116319b94df9SMatthew Ahrens {
11643b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
11653b2aab18SMatthew Ahrens 	nvpair_t *pair;
11663b2aab18SMatthew Ahrens 	boolean_t needsuspend;
11673b2aab18SMatthew Ahrens 	int error;
11683b2aab18SMatthew Ahrens 	spa_t *spa;
11693b2aab18SMatthew Ahrens 	char *firstname;
11703b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
117119b94df9SMatthew Ahrens 
11723b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
11733b2aab18SMatthew Ahrens 	if (pair == NULL)
11743b2aab18SMatthew Ahrens 		return (0);
11753b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
11763b2aab18SMatthew Ahrens 
11773b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
11783b2aab18SMatthew Ahrens 	if (error != 0)
11793b2aab18SMatthew Ahrens 		return (error);
11803b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
11813b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
11823b2aab18SMatthew Ahrens 
11833b2aab18SMatthew Ahrens 	if (needsuspend) {
11843b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
11853b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
11863b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
11873b2aab18SMatthew Ahrens 			char fsname[MAXNAMELEN];
11883b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
11893b2aab18SMatthew Ahrens 			char *atp;
11903b2aab18SMatthew Ahrens 			void *cookie;
11913b2aab18SMatthew Ahrens 
11923b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
11933b2aab18SMatthew Ahrens 			if (atp == NULL) {
1194be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
11953b2aab18SMatthew Ahrens 				break;
11963b2aab18SMatthew Ahrens 			}
11973b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
11983b2aab18SMatthew Ahrens 
11993b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
12003b2aab18SMatthew Ahrens 			if (error != 0)
12013b2aab18SMatthew Ahrens 				break;
12023b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
12033b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
12043b2aab18SMatthew Ahrens 		}
12053b2aab18SMatthew Ahrens 	}
12063b2aab18SMatthew Ahrens 
12073b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
12083b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
12093b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
12103b2aab18SMatthew Ahrens 
12113b2aab18SMatthew Ahrens 	if (error == 0) {
12123b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
12133b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
12143b2aab18SMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3);
12153b2aab18SMatthew Ahrens 	}
12163b2aab18SMatthew Ahrens 
12173b2aab18SMatthew Ahrens 	if (suspended != NULL) {
12183b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
12193b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
12203b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
12213b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
12223b2aab18SMatthew Ahrens 		}
12233b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
12243b2aab18SMatthew Ahrens 	}
12253b2aab18SMatthew Ahrens 
12263b2aab18SMatthew Ahrens 	return (error);
12273b2aab18SMatthew Ahrens }
12283b2aab18SMatthew Ahrens 
12293b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
12303b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
12313b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
12323b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
12333b2aab18SMatthew Ahrens 	const char *ddsta_htag;
12343b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
12353b2aab18SMatthew Ahrens 
12363b2aab18SMatthew Ahrens static int
12373b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
12383b2aab18SMatthew Ahrens {
12393b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
12403b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
12413b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
12423b2aab18SMatthew Ahrens 	int error;
12433b2aab18SMatthew Ahrens 
12443b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
12453b2aab18SMatthew Ahrens 	if (error != 0)
12463b2aab18SMatthew Ahrens 		return (error);
12473b2aab18SMatthew Ahrens 
1248ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1249ca48f36fSKeith M Wesolowski 	    tx, B_FALSE);
12503b2aab18SMatthew Ahrens 	if (error != 0) {
12513b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
12523b2aab18SMatthew Ahrens 		return (error);
12533b2aab18SMatthew Ahrens 	}
12543b2aab18SMatthew Ahrens 
12553b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
12563b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1257be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12583b2aab18SMatthew Ahrens 	}
12593b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
12603b2aab18SMatthew Ahrens 	    B_TRUE, tx);
12613b2aab18SMatthew Ahrens 	if (error != 0) {
12623b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
12633b2aab18SMatthew Ahrens 		return (error);
12643b2aab18SMatthew Ahrens 	}
12653b2aab18SMatthew Ahrens 
12663b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
12673b2aab18SMatthew Ahrens 	return (0);
12683b2aab18SMatthew Ahrens }
12693b2aab18SMatthew Ahrens 
12703b2aab18SMatthew Ahrens static void
12713b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
12723b2aab18SMatthew Ahrens {
12733b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
12743b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
12753b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
12763b2aab18SMatthew Ahrens 
12773b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
12783b2aab18SMatthew Ahrens 
12793b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
12803b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
12813b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
12823b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
12833b2aab18SMatthew Ahrens 
12843b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
12853b2aab18SMatthew Ahrens }
12863b2aab18SMatthew Ahrens 
12873b2aab18SMatthew Ahrens int
12883b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
12893b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
12903b2aab18SMatthew Ahrens {
12913b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
12923b2aab18SMatthew Ahrens 	int error;
12933b2aab18SMatthew Ahrens 	spa_t *spa;
12943b2aab18SMatthew Ahrens 	boolean_t needsuspend;
12953b2aab18SMatthew Ahrens 	void *cookie;
12963b2aab18SMatthew Ahrens 
12973b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
12983b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
12993b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
13003b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
13013b2aab18SMatthew Ahrens 
13023b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
13033b2aab18SMatthew Ahrens 	if (error != 0)
13043b2aab18SMatthew Ahrens 		return (error);
13053b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
13063b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
13073b2aab18SMatthew Ahrens 
13083b2aab18SMatthew Ahrens 	if (needsuspend) {
13093b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
13103b2aab18SMatthew Ahrens 		if (error != 0)
13113b2aab18SMatthew Ahrens 			return (error);
13123b2aab18SMatthew Ahrens 	}
13133b2aab18SMatthew Ahrens 
13143b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
13153b2aab18SMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
13163b2aab18SMatthew Ahrens 
13173b2aab18SMatthew Ahrens 	if (needsuspend)
13183b2aab18SMatthew Ahrens 		zil_resume(cookie);
13193b2aab18SMatthew Ahrens 	return (error);
13203b2aab18SMatthew Ahrens }
13213b2aab18SMatthew Ahrens 
13223b2aab18SMatthew Ahrens 
13233b2aab18SMatthew Ahrens void
13243b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
13253b2aab18SMatthew Ahrens {
13263b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
13273b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
13283b2aab18SMatthew Ahrens 	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
13293b2aab18SMatthew Ahrens 
13303b2aab18SMatthew Ahrens 	/*
13313b2aab18SMatthew Ahrens 	 * in case we had to change ds_fsid_guid when we opened it,
13323b2aab18SMatthew Ahrens 	 * sync it out now.
13333b2aab18SMatthew Ahrens 	 */
13343b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
13353b2aab18SMatthew Ahrens 	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
13363b2aab18SMatthew Ahrens 
13373b2aab18SMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
13383b2aab18SMatthew Ahrens }
13393b2aab18SMatthew Ahrens 
13403b2aab18SMatthew Ahrens static void
13413b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
13423b2aab18SMatthew Ahrens {
13433b2aab18SMatthew Ahrens 	uint64_t count = 0;
13443b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
13453b2aab18SMatthew Ahrens 	zap_cursor_t zc;
13463b2aab18SMatthew Ahrens 	zap_attribute_t za;
13473b2aab18SMatthew Ahrens 	nvlist_t *propval = fnvlist_alloc();
13483b2aab18SMatthew Ahrens 	nvlist_t *val = fnvlist_alloc();
13493b2aab18SMatthew Ahrens 
13503b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
135119b94df9SMatthew Ahrens 
135219b94df9SMatthew Ahrens 	/*
13533b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
135419b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
135519b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
135619b94df9SMatthew Ahrens 	 */
135719b94df9SMatthew Ahrens 	if (ds->ds_phys->ds_next_clones_obj != 0) {
13583b2aab18SMatthew Ahrens 		ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
135919b94df9SMatthew Ahrens 		    &count));
136019b94df9SMatthew Ahrens 	}
13613b2aab18SMatthew Ahrens 	if (count != ds->ds_phys->ds_num_children - 1)
136219b94df9SMatthew Ahrens 		goto fail;
136319b94df9SMatthew Ahrens 	for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
136419b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
136519b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
136619b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
136719b94df9SMatthew Ahrens 		char buf[ZFS_MAXNAMELEN];
13683b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
13693b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
137019b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
13713b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
137219b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
137319b94df9SMatthew Ahrens 	}
137419b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
13753b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
13763b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
137719b94df9SMatthew Ahrens fail:
137819b94df9SMatthew Ahrens 	nvlist_free(val);
137919b94df9SMatthew Ahrens 	nvlist_free(propval);
138019b94df9SMatthew Ahrens }
138119b94df9SMatthew Ahrens 
1382fa9e4066Sahrens void
1383a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1384fa9e4066Sahrens {
13853b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1386187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
1387a9799022Sck 
13883b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
13893b2aab18SMatthew Ahrens 
13904445fffbSMatthew Ahrens 	ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
13914445fffbSMatthew Ahrens 	    (ds->ds_phys->ds_uncompressed_bytes * 100 /
13924445fffbSMatthew Ahrens 	    ds->ds_phys->ds_compressed_bytes);
13934445fffbSMatthew Ahrens 
13944445fffbSMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
139577372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
139677372cb0SMatthew Ahrens 	    ds->ds_phys->ds_uncompressed_bytes);
13974445fffbSMatthew Ahrens 
13984445fffbSMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
13994445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
14004445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
14014445fffbSMatthew Ahrens 		    ds->ds_phys->ds_unique_bytes);
14024445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
14034445fffbSMatthew Ahrens 	} else {
14044445fffbSMatthew Ahrens 		dsl_dir_stats(ds->ds_dir, nv);
14054445fffbSMatthew Ahrens 	}
1406fa9e4066Sahrens 
1407a9799022Sck 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1408a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1409a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1410a9799022Sck 
1411a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
1412a2eea2e1Sahrens 	    ds->ds_phys->ds_creation_time);
1413a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
1414a2eea2e1Sahrens 	    ds->ds_phys->ds_creation_txg);
1415a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1416a9799022Sck 	    ds->ds_quota);
1417a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1418a9799022Sck 	    ds->ds_reserved);
1419c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
1420c5904d13Seschrock 	    ds->ds_phys->ds_guid);
14211d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
14223f9d6ad7SLin Ling 	    ds->ds_phys->ds_unique_bytes);
14231d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
14241d713200SEric Schrock 	    ds->ds_object);
142592241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
142692241e0bSTom Erickson 	    ds->ds_userrefs);
1427842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1428842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1429fa9e4066Sahrens 
143019b94df9SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_obj != 0) {
143119b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
143219b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
143319b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
143419b94df9SMatthew Ahrens 
143519b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
143619b94df9SMatthew Ahrens 		    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
143719b94df9SMatthew Ahrens 		if (err == 0) {
143819b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
143919b94df9SMatthew Ahrens 			    &comp, &uncomp);
144019b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
144119b94df9SMatthew Ahrens 			if (err == 0) {
144219b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
144319b94df9SMatthew Ahrens 				    written);
144419b94df9SMatthew Ahrens 			}
144519b94df9SMatthew Ahrens 		}
144619b94df9SMatthew Ahrens 	}
1447fa9e4066Sahrens }
1448fa9e4066Sahrens 
1449a2eea2e1Sahrens void
1450a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1451a2eea2e1Sahrens {
14523b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
14533b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
14543b2aab18SMatthew Ahrens 
1455a2eea2e1Sahrens 	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
1456a2eea2e1Sahrens 	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
14573cb34c60Sahrens 	stat->dds_guid = ds->ds_phys->ds_guid;
14584445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
14594445fffbSMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
1460a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
1461a2eea2e1Sahrens 		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
1462ebedde84SEric Taylor 	} else {
1463ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
1464ebedde84SEric Taylor 		stat->dds_num_clones = 0;
1465a2eea2e1Sahrens 
14664445fffbSMatthew Ahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
14674445fffbSMatthew Ahrens 			dsl_dataset_t *ods;
1468a2eea2e1Sahrens 
14693b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
14704445fffbSMatthew Ahrens 			    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
14714445fffbSMatthew Ahrens 			dsl_dataset_name(ods, stat->dds_origin);
14723b2aab18SMatthew Ahrens 			dsl_dataset_rele(ods, FTAG);
14734445fffbSMatthew Ahrens 		}
1474a2eea2e1Sahrens 	}
1475a2eea2e1Sahrens }
1476a2eea2e1Sahrens 
1477a2eea2e1Sahrens uint64_t
1478a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1479a2eea2e1Sahrens {
148091ebeef5Sahrens 	return (ds->ds_fsid_guid);
1481a2eea2e1Sahrens }
1482a2eea2e1Sahrens 
1483a2eea2e1Sahrens void
1484a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
1485a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
1486a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1487fa9e4066Sahrens {
1488ad135b5dSChristopher Siden 	*refdbytesp = ds->ds_phys->ds_referenced_bytes;
1489a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
1490a9799022Sck 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
1491a9799022Sck 		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
1492a9799022Sck 	if (ds->ds_quota != 0) {
1493a9799022Sck 		/*
1494a9799022Sck 		 * Adjust available bytes according to refquota
1495a9799022Sck 		 */
1496a9799022Sck 		if (*refdbytesp < ds->ds_quota)
1497a9799022Sck 			*availbytesp = MIN(*availbytesp,
1498a9799022Sck 			    ds->ds_quota - *refdbytesp);
1499a9799022Sck 		else
1500a9799022Sck 			*availbytesp = 0;
1501a9799022Sck 	}
1502a2eea2e1Sahrens 	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
1503a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1504fa9e4066Sahrens }
1505fa9e4066Sahrens 
1506f18faf3fSek boolean_t
150734f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1508f18faf3fSek {
1509f18faf3fSek 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1510f18faf3fSek 
15113b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
151234f2f8cfSMatthew Ahrens 	if (snap == NULL)
1513f18faf3fSek 		return (B_FALSE);
1514f18faf3fSek 	if (ds->ds_phys->ds_bp.blk_birth >
151534f2f8cfSMatthew Ahrens 	    snap->ds_phys->ds_creation_txg) {
151634f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
15176e0cbcaaSMatthew Ahrens 		/*
15186e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
15196e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
15206e0cbcaaSMatthew Ahrens 		 * modified.
15216e0cbcaaSMatthew Ahrens 		 */
15226e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
15236e0cbcaaSMatthew Ahrens 			return (B_TRUE);
152434f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
15256e0cbcaaSMatthew Ahrens 			return (B_TRUE);
15266e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
152734f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
15286e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
15296e0cbcaaSMatthew Ahrens 	}
1530f18faf3fSek 	return (B_FALSE);
1531f18faf3fSek }
1532f18faf3fSek 
15333b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
15343b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
15353b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
15363b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
15373b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
15383b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
15393b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
15403b2aab18SMatthew Ahrens 
15411d452cf5Sahrens /* ARGSUSED */
1542fa9e4066Sahrens static int
15433b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
15443b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1545fa9e4066Sahrens {
15463b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
15473b2aab18SMatthew Ahrens 	int error;
1548fa9e4066Sahrens 	uint64_t val;
1549fa9e4066Sahrens 
15503b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
15513b2aab18SMatthew Ahrens 	if (error != 0) {
15523b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
15533b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
15543b2aab18SMatthew Ahrens 	}
15551d452cf5Sahrens 
15563b2aab18SMatthew Ahrens 	/* new name should not exist */
15573b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
15583b2aab18SMatthew Ahrens 	if (error == 0)
1559be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
15603b2aab18SMatthew Ahrens 	else if (error == ENOENT)
15613b2aab18SMatthew Ahrens 		error = 0;
1562cdf5b4caSmmusante 
1563cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
15643b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
15653b2aab18SMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
1566be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
1567cdf5b4caSmmusante 
15683b2aab18SMatthew Ahrens 	return (error);
15691d452cf5Sahrens }
1570fa9e4066Sahrens 
15713b2aab18SMatthew Ahrens static int
15723b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
15731d452cf5Sahrens {
15743b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
15753b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15761d452cf5Sahrens 	dsl_dataset_t *hds;
15773b2aab18SMatthew Ahrens 	int error;
1578fa9e4066Sahrens 
15793b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
15803b2aab18SMatthew Ahrens 	if (error != 0)
15813b2aab18SMatthew Ahrens 		return (error);
1582fa9e4066Sahrens 
15833b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
15843b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
15853b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
15863b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
15873b2aab18SMatthew Ahrens 	} else {
15883b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
15893b2aab18SMatthew Ahrens 	}
1590745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
15913b2aab18SMatthew Ahrens 	return (error);
1592fa9e4066Sahrens }
1593fa9e4066Sahrens 
1594cdf5b4caSmmusante static int
15953b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
15963b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1597cdf5b4caSmmusante {
15983b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
15993b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16003b2aab18SMatthew Ahrens 	uint64_t val;
16013b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
16023b2aab18SMatthew Ahrens 	int error;
1603ecd6cf80Smarks 
16043b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
16053b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
16063b2aab18SMatthew Ahrens 	if (error == ENOENT) {
16073b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
16083b2aab18SMatthew Ahrens 		return (0);
1609ecd6cf80Smarks 	}
1610ecd6cf80Smarks 
16113b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
16123b2aab18SMatthew Ahrens 
16133b2aab18SMatthew Ahrens 	/* log before we change the name */
16143b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
16153b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
1616cdf5b4caSmmusante 
16173b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
16183b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
16193b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
16203b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
16213b2aab18SMatthew Ahrens 	VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
16223b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
1623cdf5b4caSmmusante 
16243b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1625cdf5b4caSmmusante 	return (0);
1626cdf5b4caSmmusante }
1627cdf5b4caSmmusante 
16283b2aab18SMatthew Ahrens static void
16293b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
1630cdf5b4caSmmusante {
16313b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
16323b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16333b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
1634cdf5b4caSmmusante 
16353b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
16363b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
16373b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
16383b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
16393b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
16403b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
16413b2aab18SMatthew Ahrens 	} else {
16423b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
1643cdf5b4caSmmusante 	}
16443b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
1645cdf5b4caSmmusante }
1646cdf5b4caSmmusante 
16473b2aab18SMatthew Ahrens int
16483b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
16493b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
16503a5a36beSmmusante {
16513b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
16523a5a36beSmmusante 
16533b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
16543b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
16553b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
16563b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
16573a5a36beSmmusante 
16583b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
16593b2aab18SMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
16603a5a36beSmmusante }
16613a5a36beSmmusante 
166291948b51SKeith M Wesolowski /*
166391948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
166491948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
166591948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
166691948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
166791948b51SKeith M Wesolowski  * when we release the long hold.
166891948b51SKeith M Wesolowski  */
166991948b51SKeith M Wesolowski static int
167091948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
167191948b51SKeith M Wesolowski {
167291948b51SKeith M Wesolowski 	boolean_t held;
167391948b51SKeith M Wesolowski 
167491948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
167591948b51SKeith M Wesolowski 		return (0);
167691948b51SKeith M Wesolowski 
167791948b51SKeith M Wesolowski 	if (owner != NULL) {
167891948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
167991948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
168091948b51SKeith M Wesolowski 	}
168191948b51SKeith M Wesolowski 
168291948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
168391948b51SKeith M Wesolowski 
168491948b51SKeith M Wesolowski 	if (owner != NULL)
168591948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
168691948b51SKeith M Wesolowski 
168791948b51SKeith M Wesolowski 	if (held)
168891948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
168991948b51SKeith M Wesolowski 
169091948b51SKeith M Wesolowski 	return (0);
169191948b51SKeith M Wesolowski }
169291948b51SKeith M Wesolowski 
169391948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg {
169491948b51SKeith M Wesolowski 	const char *ddra_fsname;
169591948b51SKeith M Wesolowski 	void *ddra_owner;
1696a7027df1SMatthew Ahrens 	nvlist_t *ddra_result;
169791948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t;
169891948b51SKeith M Wesolowski 
16993b2aab18SMatthew Ahrens static int
17003b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
1701fa9e4066Sahrens {
170291948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
17033b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17041d452cf5Sahrens 	dsl_dataset_t *ds;
17053b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
17063b2aab18SMatthew Ahrens 	int error;
1707fa9e4066Sahrens 
170891948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
17093b2aab18SMatthew Ahrens 	if (error != 0)
17103b2aab18SMatthew Ahrens 		return (error);
1711370c1af0SSanjeev Bagewadi 
17123b2aab18SMatthew Ahrens 	/* must not be a snapshot */
17133b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
17143b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1715be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
17163b2aab18SMatthew Ahrens 	}
17173a5a36beSmmusante 
17183b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
17193b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
17203b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1721be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
17223b2aab18SMatthew Ahrens 	}
17233a5a36beSmmusante 
172491948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
172591948b51SKeith M Wesolowski 	if (error != 0) {
17263b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
172791948b51SKeith M Wesolowski 		return (error);
17283b2aab18SMatthew Ahrens 	}
17293b2aab18SMatthew Ahrens 
17303b2aab18SMatthew Ahrens 	/*
17313b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
17323b2aab18SMatthew Ahrens 	 * the refquota.
17333b2aab18SMatthew Ahrens 	 */
17343b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
17353b2aab18SMatthew Ahrens 	    ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
17363b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1737be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
1738fa9e4066Sahrens 	}
1739370c1af0SSanjeev Bagewadi 
17403b2aab18SMatthew Ahrens 	/*
17413b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
17423b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
17433b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
17443b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
17453b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
17463b2aab18SMatthew Ahrens 	 */
17473b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
17483b2aab18SMatthew Ahrens 	    ds->ds_phys->ds_unique_bytes);
17493b2aab18SMatthew Ahrens 
17503b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
17513b2aab18SMatthew Ahrens 	    unused_refres_delta >
17523b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
17533b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1754be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
1755fa9e4066Sahrens 	}
1756fa9e4066Sahrens 
17573b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
17583b2aab18SMatthew Ahrens 	return (0);
17593b2aab18SMatthew Ahrens }
17601d452cf5Sahrens 
17613b2aab18SMatthew Ahrens static void
17623b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
17633b2aab18SMatthew Ahrens {
176491948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
17653b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17663b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
17673b2aab18SMatthew Ahrens 	uint64_t cloneobj;
1768a7027df1SMatthew Ahrens 	char namebuf[ZFS_MAXNAMELEN];
17691d452cf5Sahrens 
177091948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
17711d452cf5Sahrens 
1772a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
1773a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
1774a7027df1SMatthew Ahrens 
17753b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
17763b2aab18SMatthew Ahrens 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
17771d452cf5Sahrens 
17783b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
17791d452cf5Sahrens 
17803b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
17813b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
17823b2aab18SMatthew Ahrens 
17833b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
17843b2aab18SMatthew Ahrens 
17853b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
17863b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
17873b2aab18SMatthew Ahrens }
17883b2aab18SMatthew Ahrens 
178991948b51SKeith M Wesolowski /*
1790a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
1791a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
1792a7027df1SMatthew Ahrens  * in the result nvlist.
179391948b51SKeith M Wesolowski  *
1794a7027df1SMatthew Ahrens  * If owner != NULL:
179591948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
179691948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
179791948b51SKeith M Wesolowski  *   succeed or not.
179891948b51SKeith M Wesolowski  *
179991948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
180091948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
180191948b51SKeith M Wesolowski  */
18023b2aab18SMatthew Ahrens int
1803a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
18043b2aab18SMatthew Ahrens {
180591948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
180691948b51SKeith M Wesolowski 
180791948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
180891948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
1809a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
181091948b51SKeith M Wesolowski 
18113b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
1812a7027df1SMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra, 1));
1813fa9e4066Sahrens }
181499653d4eSeschrock 
1815088f3894Sahrens struct promotenode {
1816745cd3c5Smaybee 	list_node_t link;
1817745cd3c5Smaybee 	dsl_dataset_t *ds;
1818745cd3c5Smaybee };
1819745cd3c5Smaybee 
18203b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg {
18213b2aab18SMatthew Ahrens 	const char *ddpa_clonename;
18223b2aab18SMatthew Ahrens 	dsl_dataset_t *ddpa_clone;
182374e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
18243b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_origin; /* origin of the origin */
182574e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
1826681d9761SEric Taylor 	char *err_ds;
18273b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t;
18281d452cf5Sahrens 
182974e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
18303b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
18313b2aab18SMatthew Ahrens     void *tag);
18323b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
183374e7dc98SMatthew Ahrens 
183499653d4eSeschrock static int
18353b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
183699653d4eSeschrock {
18373b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
18383b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18393b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
18403b2aab18SMatthew Ahrens 	struct promotenode *snap;
18413b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
1842745cd3c5Smaybee 	int err;
1843cde58dbcSMatthew Ahrens 	uint64_t unused;
18441d452cf5Sahrens 
18453b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
18463b2aab18SMatthew Ahrens 	if (err != 0)
18473b2aab18SMatthew Ahrens 		return (err);
184899653d4eSeschrock 
18493b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
18501d452cf5Sahrens 
18513b2aab18SMatthew Ahrens 	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
18523b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
1853be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
18543b2aab18SMatthew Ahrens 	}
18553b2aab18SMatthew Ahrens 
18563b2aab18SMatthew Ahrens 	/*
18573b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
18583b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
18593b2aab18SMatthew Ahrens 	 */
18603b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
18613b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
18623b2aab18SMatthew Ahrens 		return (0);
18633b2aab18SMatthew Ahrens 	}
18643b2aab18SMatthew Ahrens 
18653b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
18663b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
186799653d4eSeschrock 
18683cb34c60Sahrens 	/* compute origin's new unique space */
18693b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
187074e7dc98SMatthew Ahrens 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
1871cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
1872cde58dbcSMatthew Ahrens 	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
18733b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
187499653d4eSeschrock 
1875745cd3c5Smaybee 	/*
1876745cd3c5Smaybee 	 * Walk the snapshots that we are moving
1877745cd3c5Smaybee 	 *
187874e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
18793b2aab18SMatthew Ahrens 	 * to used by each snapshot:
188074e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
188174e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
188274e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
1883745cd3c5Smaybee 	 * So a sequence would look like:
188474e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
1885745cd3c5Smaybee 	 * Which simplifies to:
188674e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
1887745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
188874e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
1889745cd3c5Smaybee 	 */
18903b2aab18SMatthew Ahrens 	ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
18913b2aab18SMatthew Ahrens 	ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
18923b2aab18SMatthew Ahrens 	ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
18933b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
18943b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
189599653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
1896745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
189799653d4eSeschrock 
18983b2aab18SMatthew Ahrens 		/*
18993b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
19003b2aab18SMatthew Ahrens 		 * the objset.
19013b2aab18SMatthew Ahrens 		 */
19023b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
1903be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
19043b2aab18SMatthew Ahrens 			goto out;
19053b2aab18SMatthew Ahrens 		}
19063b2aab18SMatthew Ahrens 
190799653d4eSeschrock 		/* Check that the snapshot name does not conflict */
19083b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
1909745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
1910681d9761SEric Taylor 		if (err == 0) {
19113b2aab18SMatthew Ahrens 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
1912be6fd75aSMatthew Ahrens 			err = SET_ERROR(EEXIST);
1913681d9761SEric Taylor 			goto out;
1914681d9761SEric Taylor 		}
1915745cd3c5Smaybee 		if (err != ENOENT)
1916681d9761SEric Taylor 			goto out;
191799653d4eSeschrock 
1918745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
191974e7dc98SMatthew Ahrens 		if (ds->ds_phys->ds_prev_snap_obj == 0)
192074e7dc98SMatthew Ahrens 			continue;
192174e7dc98SMatthew Ahrens 
1922cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
1923cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
19243b2aab18SMatthew Ahrens 		ddpa->used += dlused;
19253b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
19263b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
192774e7dc98SMatthew Ahrens 	}
1928745cd3c5Smaybee 
1929745cd3c5Smaybee 	/*
1930745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
1931745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
1932745cd3c5Smaybee 	 */
19333b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
19343b2aab18SMatthew Ahrens 		ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
19353b2aab18SMatthew Ahrens 		ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
19363b2aab18SMatthew Ahrens 		ddpa->uncomp -=
19373b2aab18SMatthew Ahrens 		    ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
193899653d4eSeschrock 	}
193999653d4eSeschrock 
194099653d4eSeschrock 	/* Check that there is enough space here */
194174e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
19423b2aab18SMatthew Ahrens 	    ddpa->used);
19433b2aab18SMatthew Ahrens 	if (err != 0)
19443b2aab18SMatthew Ahrens 		goto out;
194574e7dc98SMatthew Ahrens 
194674e7dc98SMatthew Ahrens 	/*
194774e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
194874e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
194974e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
195074e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
195174e7dc98SMatthew Ahrens 	 */
195274e7dc98SMatthew Ahrens 	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
195374e7dc98SMatthew Ahrens 		uint64_t space;
195474e7dc98SMatthew Ahrens 
195574e7dc98SMatthew Ahrens 		/*
195674e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
19573f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
1958cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
195974e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
196074e7dc98SMatthew Ahrens 		 * iterate over all bps.
196174e7dc98SMatthew Ahrens 		 */
19623b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
19633b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
19643b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
19653b2aab18SMatthew Ahrens 		if (err != 0)
19663b2aab18SMatthew Ahrens 			goto out;
196774e7dc98SMatthew Ahrens 
19683b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
19693f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
19703b2aab18SMatthew Ahrens 		if (err != 0)
19713b2aab18SMatthew Ahrens 			goto out;
19723b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
197374e7dc98SMatthew Ahrens 	}
197474e7dc98SMatthew Ahrens 	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
19753b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
19763b2aab18SMatthew Ahrens 		    origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
19773b2aab18SMatthew Ahrens 		if (err != 0)
19783b2aab18SMatthew Ahrens 			goto out;
1979745cd3c5Smaybee 	}
19801d452cf5Sahrens 
1981681d9761SEric Taylor out:
19823b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
1983681d9761SEric Taylor 	return (err);
19841d452cf5Sahrens }
198599653d4eSeschrock 
19861d452cf5Sahrens static void
19873b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
19881d452cf5Sahrens {
19893b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
19903b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
19913b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
19923b2aab18SMatthew Ahrens 	struct promotenode *snap;
19933b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
19943b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_head;
19953b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
19963cb34c60Sahrens 	dsl_dir_t *odd = NULL;
1997088f3894Sahrens 	uint64_t oldnext_obj;
199874e7dc98SMatthew Ahrens 	int64_t delta;
19991d452cf5Sahrens 
20003b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
20013b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
20023b2aab18SMatthew Ahrens 
20033b2aab18SMatthew Ahrens 	ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
20041d452cf5Sahrens 
20053b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
20063b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
20073b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
20083b2aab18SMatthew Ahrens 
20093b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
201074e7dc98SMatthew Ahrens 	origin_head = snap->ds;
201174e7dc98SMatthew Ahrens 
20120b69c2f0Sahrens 	/*
20133cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
20140b69c2f0Sahrens 	 * changing.
20150b69c2f0Sahrens 	 */
20163b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
20173cb34c60Sahrens 	    NULL, FTAG, &odd));
201899653d4eSeschrock 
2019745cd3c5Smaybee 	/* change origin's next snap */
2020745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
2021088f3894Sahrens 	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
20223b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
202374e7dc98SMatthew Ahrens 	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
202474e7dc98SMatthew Ahrens 	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
2025745cd3c5Smaybee 
2026088f3894Sahrens 	/* change the origin's next clone */
2027088f3894Sahrens 	if (origin_ds->ds_phys->ds_next_clones_obj) {
20283b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
20293b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
20303b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2031088f3894Sahrens 		    origin_ds->ds_phys->ds_next_clones_obj,
2032088f3894Sahrens 		    oldnext_obj, tx));
2033088f3894Sahrens 	}
2034088f3894Sahrens 
2035745cd3c5Smaybee 	/* change origin */
2036745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2037745cd3c5Smaybee 	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
2038745cd3c5Smaybee 	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
20393f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2040745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
2041745cd3c5Smaybee 	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
20423f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
20433f9d6ad7SLin Ling 	    origin_ds->ds_phys->ds_creation_txg;
2044745cd3c5Smaybee 
2045cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2046cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
20473b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
2048cde58dbcSMatthew Ahrens 		    odd->dd_phys->dd_clones, hds->ds_object, tx));
20493b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
20503b2aab18SMatthew Ahrens 		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2051cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2052cde58dbcSMatthew Ahrens 
20533b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
20543b2aab18SMatthew Ahrens 		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
2055cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
2056cde58dbcSMatthew Ahrens 		if (dd->dd_phys->dd_clones == 0) {
2057cde58dbcSMatthew Ahrens 			dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
2058cde58dbcSMatthew Ahrens 			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
2059cde58dbcSMatthew Ahrens 		}
20603b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
2061cde58dbcSMatthew Ahrens 		    dd->dd_phys->dd_clones, origin_head->ds_object, tx));
2062cde58dbcSMatthew Ahrens 	}
2063cde58dbcSMatthew Ahrens 
206499653d4eSeschrock 	/* move snapshots to this dir */
20653b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
20663b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2067745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
206899653d4eSeschrock 
20693b2aab18SMatthew Ahrens 		/*
20703b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
20713b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
20723b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
20733b2aab18SMatthew Ahrens 		 */
2074503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2075503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2076503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
20773baa08fcSek 		}
20783b2aab18SMatthew Ahrens 
207999653d4eSeschrock 		/* move snap name entry */
20803b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
20813b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2082745cd3c5Smaybee 		    ds->ds_snapname, tx));
20833b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
208499653d4eSeschrock 		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
208599653d4eSeschrock 		    8, 1, &ds->ds_object, tx));
2086cde58dbcSMatthew Ahrens 
208799653d4eSeschrock 		/* change containing dsl_dir */
208899653d4eSeschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
20893cb34c60Sahrens 		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
209099653d4eSeschrock 		ds->ds_phys->ds_dir_obj = dd->dd_object;
20913cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
20923b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
20933b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
209499653d4eSeschrock 		    NULL, ds, &ds->ds_dir));
209599653d4eSeschrock 
2096cde58dbcSMatthew Ahrens 		/* move any clone references */
2097cde58dbcSMatthew Ahrens 		if (ds->ds_phys->ds_next_clones_obj &&
2098cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2099cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2100cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2101cde58dbcSMatthew Ahrens 
21023b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
21033b2aab18SMatthew Ahrens 			    ds->ds_phys->ds_next_clones_obj);
21043b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
21053b2aab18SMatthew Ahrens 			    zap_cursor_advance(&zc)) {
21063b2aab18SMatthew Ahrens 				dsl_dataset_t *cnds;
21073b2aab18SMatthew Ahrens 				uint64_t o;
2108a9799022Sck 
21093b2aab18SMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
21103b2aab18SMatthew Ahrens 					/*
21113b2aab18SMatthew Ahrens 					 * We've already moved the
21123b2aab18SMatthew Ahrens 					 * origin's reference.
21133b2aab18SMatthew Ahrens 					 */
21143b2aab18SMatthew Ahrens 					continue;
21153b2aab18SMatthew Ahrens 				}
2116a9799022Sck 
21173b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
21183b2aab18SMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
21193b2aab18SMatthew Ahrens 				o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
2120a9799022Sck 
21213b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
21223b2aab18SMatthew Ahrens 				    odd->dd_phys->dd_clones, o, tx));
21233b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
21243b2aab18SMatthew Ahrens 				    dd->dd_phys->dd_clones, o, tx));
21253b2aab18SMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
21263b2aab18SMatthew Ahrens 			}
21273b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
21283b2aab18SMatthew Ahrens 		}
21299082849eSck 
21303b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
2131a9799022Sck 	}
2132a9799022Sck 
2133a9799022Sck 	/*
21343b2aab18SMatthew Ahrens 	 * Change space accounting.
21353b2aab18SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
21363b2aab18SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
21373b2aab18SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
2138a9799022Sck 	 */
2139a9799022Sck 
21403b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
21413b2aab18SMatthew Ahrens 	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
21423b2aab18SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
21433b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
21443b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
21453b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
21463b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
21473b2aab18SMatthew Ahrens 
21483b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
21493b2aab18SMatthew Ahrens 	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
21503b2aab18SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
21513b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
21523b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
21533b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
21543b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
21553b2aab18SMatthew Ahrens 
21563b2aab18SMatthew Ahrens 	origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
21573b2aab18SMatthew Ahrens 
21583b2aab18SMatthew Ahrens 	/* log history record */
21593b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
21603b2aab18SMatthew Ahrens 
21613b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
21623b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2163a9799022Sck }
2164a9799022Sck 
21653b2aab18SMatthew Ahrens /*
21663b2aab18SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
21673b2aab18SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
21683b2aab18SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
21693b2aab18SMatthew Ahrens  * snapshots back to this dataset's origin.
21703b2aab18SMatthew Ahrens  */
2171a9799022Sck static int
21723b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
21733b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
2174a9799022Sck {
21753b2aab18SMatthew Ahrens 	uint64_t obj = last_obj;
2176a9799022Sck 
21773b2aab18SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
21783b2aab18SMatthew Ahrens 	    offsetof(struct promotenode, link));
2179a9799022Sck 
21803b2aab18SMatthew Ahrens 	while (obj != first_obj) {
21813b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
21823b2aab18SMatthew Ahrens 		struct promotenode *snap;
21833b2aab18SMatthew Ahrens 		int err;
218492241e0bSTom Erickson 
21853b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
21863b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
21873b2aab18SMatthew Ahrens 		if (err != 0)
21883b2aab18SMatthew Ahrens 			return (err);
2189a9799022Sck 
21903b2aab18SMatthew Ahrens 		if (first_obj == 0)
21913b2aab18SMatthew Ahrens 			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
21923b2aab18SMatthew Ahrens 
21933b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
21943b2aab18SMatthew Ahrens 		snap->ds = ds;
21953b2aab18SMatthew Ahrens 		list_insert_tail(l, snap);
21963b2aab18SMatthew Ahrens 		obj = ds->ds_phys->ds_prev_snap_obj;
21973b2aab18SMatthew Ahrens 	}
2198a9799022Sck 
2199a9799022Sck 	return (0);
2200a9799022Sck }
2201a9799022Sck 
22023b2aab18SMatthew Ahrens static int
22033b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
2204a9799022Sck {
22053b2aab18SMatthew Ahrens 	struct promotenode *snap;
2206a9799022Sck 
22073b2aab18SMatthew Ahrens 	*spacep = 0;
22083b2aab18SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
22093b2aab18SMatthew Ahrens 		uint64_t used, comp, uncomp;
22103b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
22113b2aab18SMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
22123b2aab18SMatthew Ahrens 		*spacep += used;
221392241e0bSTom Erickson 	}
22143b2aab18SMatthew Ahrens 	return (0);
2215a9799022Sck }
2216a9799022Sck 
22173b2aab18SMatthew Ahrens static void
22183b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
2219a9799022Sck {
22203b2aab18SMatthew Ahrens 	struct promotenode *snap;
222192241e0bSTom Erickson 
22223b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
22233b2aab18SMatthew Ahrens 		return;
2224a9799022Sck 
22253b2aab18SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
22263b2aab18SMatthew Ahrens 		list_remove(l, snap);
22273b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
22283b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
22293b2aab18SMatthew Ahrens 	}
22303b2aab18SMatthew Ahrens 	list_destroy(l);
2231a9799022Sck }
2232a9799022Sck 
2233a9799022Sck static int
22343b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
2235a9799022Sck {
22363b2aab18SMatthew Ahrens 	int error;
22373b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
22383b2aab18SMatthew Ahrens 	struct promotenode *snap;
2239a9799022Sck 
22403b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
22413b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
22423b2aab18SMatthew Ahrens 	if (error != 0)
22433b2aab18SMatthew Ahrens 		return (error);
22443b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
2245a9799022Sck 
22463b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
22473b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
22483b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2249be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22503b2aab18SMatthew Ahrens 	}
2251a9799022Sck 
22523b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
22533b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
22543b2aab18SMatthew Ahrens 	if (error != 0)
22553b2aab18SMatthew Ahrens 		goto out;
2256a9799022Sck 
22573b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
22583b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
22593b2aab18SMatthew Ahrens 	if (error != 0)
22603b2aab18SMatthew Ahrens 		goto out;
2261a9799022Sck 
22623b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
22633b2aab18SMatthew Ahrens 	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
22643b2aab18SMatthew Ahrens 	error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
22653b2aab18SMatthew Ahrens 	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
22663b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
22673b2aab18SMatthew Ahrens 	if (error != 0)
22683b2aab18SMatthew Ahrens 		goto out;
2269379c004dSEric Schrock 
22703b2aab18SMatthew Ahrens 	if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
22713b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
22723b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
22733b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
22743b2aab18SMatthew Ahrens 		if (error != 0)
22753b2aab18SMatthew Ahrens 			goto out;
2276379c004dSEric Schrock 	}
22773b2aab18SMatthew Ahrens out:
22783b2aab18SMatthew Ahrens 	if (error != 0)
22793b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
22803b2aab18SMatthew Ahrens 	return (error);
2281a9799022Sck }
2282a9799022Sck 
2283a9799022Sck static void
22843b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
2285a9799022Sck {
22863b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
22873b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
22883b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
22893b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
22903b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
22913b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
22923b2aab18SMatthew Ahrens }
229302c8f3f0SMatthew Ahrens 
22943b2aab18SMatthew Ahrens /*
22953b2aab18SMatthew Ahrens  * Promote a clone.
22963b2aab18SMatthew Ahrens  *
22973b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
22983b2aab18SMatthew Ahrens  * in with the name.  (It must be at least MAXNAMELEN bytes long.)
22993b2aab18SMatthew Ahrens  */
23003b2aab18SMatthew Ahrens int
23013b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap)
23023b2aab18SMatthew Ahrens {
23033b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
23043b2aab18SMatthew Ahrens 	uint64_t numsnaps;
23053b2aab18SMatthew Ahrens 	int error;
23063b2aab18SMatthew Ahrens 	objset_t *os;
230792241e0bSTom Erickson 
23083b2aab18SMatthew Ahrens 	/*
23093b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
23103b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
23113b2aab18SMatthew Ahrens 	 */
23123b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
23133b2aab18SMatthew Ahrens 	if (error != 0)
23143b2aab18SMatthew Ahrens 		return (error);
23153b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
23163b2aab18SMatthew Ahrens 	    dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
23173b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
23183b2aab18SMatthew Ahrens 	if (error != 0)
23193b2aab18SMatthew Ahrens 		return (error);
232002c8f3f0SMatthew Ahrens 
23213b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
23223b2aab18SMatthew Ahrens 	ddpa.err_ds = conflsnap;
232302c8f3f0SMatthew Ahrens 
23243b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_dataset_promote_check,
23253b2aab18SMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2326a9799022Sck }
2327a9799022Sck 
2328a9799022Sck int
23293b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
233091948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2331a9799022Sck {
23323b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2333a9799022Sck 
23343b2aab18SMatthew Ahrens 	/* they should both be heads */
23353b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(clone) ||
23363b2aab18SMatthew Ahrens 	    dsl_dataset_is_snapshot(origin_head))
2337be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
233892241e0bSTom Erickson 
233934f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
234034f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
2341be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2342a9799022Sck 
23433b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
23443b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
23453b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
234634f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2347be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
234892241e0bSTom Erickson 
23493b2aab18SMatthew Ahrens 	/* the clone should be a child of the origin */
23503b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2351be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2352842727c2SChris Kirby 
23533b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
235434f2f8cfSMatthew Ahrens 	if (!force &&
235534f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2356be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
2357c99e4bdcSChris Kirby 
23583b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
235991948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2360be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
23613b2aab18SMatthew Ahrens 
23623b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
23633b2aab18SMatthew Ahrens 	unused_refres_delta =
23643b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
23653b2aab18SMatthew Ahrens 	    origin_head->ds_phys->ds_unique_bytes) -
23663b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
23673b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes);
23683b2aab18SMatthew Ahrens 
23693b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
23703b2aab18SMatthew Ahrens 	    unused_refres_delta >
23713b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2372be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
23733b2aab18SMatthew Ahrens 
23743b2aab18SMatthew Ahrens 	/* clone can't be over the head's refquota */
23753b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
23763b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
2377be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2378c99e4bdcSChris Kirby 
23793b2aab18SMatthew Ahrens 	return (0);
2380c99e4bdcSChris Kirby }
2381c99e4bdcSChris Kirby 
2382a7f53a56SChris Kirby void
23833b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
23843b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2385a7f53a56SChris Kirby {
23863b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
23873b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2388a7f53a56SChris Kirby 
23893b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
23903b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
23913b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
239234f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2393842727c2SChris Kirby 
23943b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
23953b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2396842727c2SChris Kirby 
23973b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
23983b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
23993b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
24003b2aab18SMatthew Ahrens 	}
2401842727c2SChris Kirby 
24023b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
24033b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
24043b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
2405842727c2SChris Kirby 	}
2406842727c2SChris Kirby 
24073b2aab18SMatthew Ahrens 	unused_refres_delta =
24083b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
24093b2aab18SMatthew Ahrens 	    origin_head->ds_phys->ds_unique_bytes) -
24103b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
24113b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes);
24123b2aab18SMatthew Ahrens 
24133b2aab18SMatthew Ahrens 	/*
24143b2aab18SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
24153b2aab18SMatthew Ahrens 	 */
24163b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
24173b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
24183b2aab18SMatthew Ahrens 		uint64_t comp, uncomp;
24193b2aab18SMatthew Ahrens 
24203b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
24213b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
24223b2aab18SMatthew Ahrens 		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
24233b2aab18SMatthew Ahrens 		    &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
24243b2aab18SMatthew Ahrens 	}
24253b2aab18SMatthew Ahrens 
24263b2aab18SMatthew Ahrens 	/* swap blkptrs */
24273b2aab18SMatthew Ahrens 	{
24283b2aab18SMatthew Ahrens 		blkptr_t tmp;
24293b2aab18SMatthew Ahrens 		tmp = origin_head->ds_phys->ds_bp;
24303b2aab18SMatthew Ahrens 		origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
24313b2aab18SMatthew Ahrens 		clone->ds_phys->ds_bp = tmp;
24323b2aab18SMatthew Ahrens 	}
24333b2aab18SMatthew Ahrens 
24343b2aab18SMatthew Ahrens 	/* set dd_*_bytes */
24353b2aab18SMatthew Ahrens 	{
24363b2aab18SMatthew Ahrens 		int64_t dused, dcomp, duncomp;
24373b2aab18SMatthew Ahrens 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
24383b2aab18SMatthew Ahrens 		uint64_t odl_used, odl_comp, odl_uncomp;
24393b2aab18SMatthew Ahrens 
24403b2aab18SMatthew Ahrens 		ASSERT3U(clone->ds_dir->dd_phys->
24413b2aab18SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
24423b2aab18SMatthew Ahrens 
24433b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
24443b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
24453b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
24463b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
244715508ac0SChris Kirby 
24483b2aab18SMatthew Ahrens 		dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
24493b2aab18SMatthew Ahrens 		    (origin_head->ds_phys->ds_referenced_bytes + odl_used);
24503b2aab18SMatthew Ahrens 		dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
24513b2aab18SMatthew Ahrens 		    (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
24523b2aab18SMatthew Ahrens 		duncomp = clone->ds_phys->ds_uncompressed_bytes +
24533b2aab18SMatthew Ahrens 		    cdl_uncomp -
24543b2aab18SMatthew Ahrens 		    (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
2455842727c2SChris Kirby 
24563b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
24573b2aab18SMatthew Ahrens 		    dused, dcomp, duncomp, tx);
24583b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
24593b2aab18SMatthew Ahrens 		    -dused, -dcomp, -duncomp, tx);
2460842727c2SChris Kirby 
2461842727c2SChris Kirby 		/*
24623b2aab18SMatthew Ahrens 		 * The difference in the space used by snapshots is the
24633b2aab18SMatthew Ahrens 		 * difference in snapshot space due to the head's
24643b2aab18SMatthew Ahrens 		 * deadlist (since that's the only thing that's
24653b2aab18SMatthew Ahrens 		 * changing that affects the snapused).
2466842727c2SChris Kirby 		 */
24673b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
24683b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
24693b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
24703b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
24713b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
24723b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
24733b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
24743b2aab18SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
2475842727c2SChris Kirby 	}
2476842727c2SChris Kirby 
24773b2aab18SMatthew Ahrens 	/* swap ds_*_bytes */
24783b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
24793b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_referenced_bytes);
24803b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
24813b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_compressed_bytes);
24823b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
24833b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_uncompressed_bytes);
24843b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_unique_bytes,
24853b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_unique_bytes);
2486842727c2SChris Kirby 
24873b2aab18SMatthew Ahrens 	/* apply any parent delta for change in unconsumed refreservation */
24883b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
24893b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
2490ca45db41SChris Kirby 
24913b2aab18SMatthew Ahrens 	/*
24923b2aab18SMatthew Ahrens 	 * Swap deadlists.
24933b2aab18SMatthew Ahrens 	 */
24943b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
24953b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
24963b2aab18SMatthew Ahrens 	SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
24973b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_deadlist_obj);
24983b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
24993b2aab18SMatthew Ahrens 	    clone->ds_phys->ds_deadlist_obj);
25003b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
25013b2aab18SMatthew Ahrens 	    origin_head->ds_phys->ds_deadlist_obj);
2502842727c2SChris Kirby 
25033b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
2504842727c2SChris Kirby 
25053b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
25063b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
2507842727c2SChris Kirby }
2508842727c2SChris Kirby 
25093b2aab18SMatthew Ahrens /*
25103b2aab18SMatthew Ahrens  * Given a pool name and a dataset object number in that pool,
25113b2aab18SMatthew Ahrens  * return the name of that dataset.
25123b2aab18SMatthew Ahrens  */
2513a7f53a56SChris Kirby int
25143b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
2515a7f53a56SChris Kirby {
25163b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
25173b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2518a7f53a56SChris Kirby 	int error;
2519a7f53a56SChris Kirby 
25203b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
25213b2aab18SMatthew Ahrens 	if (error != 0)
25223b2aab18SMatthew Ahrens 		return (error);
25233b2aab18SMatthew Ahrens 
25243b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
25253b2aab18SMatthew Ahrens 	if (error == 0) {
25263b2aab18SMatthew Ahrens 		dsl_dataset_name(ds, buf);
25273b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
25283b2aab18SMatthew Ahrens 	}
25293b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
2530a7f53a56SChris Kirby 
2531a7f53a56SChris Kirby 	return (error);
2532a7f53a56SChris Kirby }
2533a7f53a56SChris Kirby 
2534842727c2SChris Kirby int
25353b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
25363b2aab18SMatthew Ahrens     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
2537842727c2SChris Kirby {
25383b2aab18SMatthew Ahrens 	int error = 0;
2539842727c2SChris Kirby 
25403b2aab18SMatthew Ahrens 	ASSERT3S(asize, >, 0);
2541842727c2SChris Kirby 
25423b2aab18SMatthew Ahrens 	/*
25433b2aab18SMatthew Ahrens 	 * *ref_rsrv is the portion of asize that will come from any
25443b2aab18SMatthew Ahrens 	 * unconsumed refreservation space.
25453b2aab18SMatthew Ahrens 	 */
25463b2aab18SMatthew Ahrens 	*ref_rsrv = 0;
2547842727c2SChris Kirby 
25483b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
25493b2aab18SMatthew Ahrens 	/*
25503b2aab18SMatthew Ahrens 	 * Make a space adjustment for reserved bytes.
25513b2aab18SMatthew Ahrens 	 */
25523b2aab18SMatthew Ahrens 	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
25533b2aab18SMatthew Ahrens 		ASSERT3U(*used, >=,
25543b2aab18SMatthew Ahrens 		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
25553b2aab18SMatthew Ahrens 		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
25563b2aab18SMatthew Ahrens 		*ref_rsrv =
25573b2aab18SMatthew Ahrens 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
2558842727c2SChris Kirby 	}
2559842727c2SChris Kirby 
25603b2aab18SMatthew Ahrens 	if (!check_quota || ds->ds_quota == 0) {
25613b2aab18SMatthew Ahrens 		mutex_exit(&ds->ds_lock);
25623b2aab18SMatthew Ahrens 		return (0);
2563842727c2SChris Kirby 	}
25643b2aab18SMatthew Ahrens 	/*
25653b2aab18SMatthew Ahrens 	 * If they are requesting more space, and our current estimate
25663b2aab18SMatthew Ahrens 	 * is over quota, they get to try again unless the actual
25673b2aab18SMatthew Ahrens 	 * on-disk is over quota and there are no pending changes (which
25683b2aab18SMatthew Ahrens 	 * may free up space for us).
25693b2aab18SMatthew Ahrens 	 */
25703b2aab18SMatthew Ahrens 	if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
25713b2aab18SMatthew Ahrens 		if (inflight > 0 ||
25723b2aab18SMatthew Ahrens 		    ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
2573be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
25743b2aab18SMatthew Ahrens 		else
2575be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
2576842727c2SChris Kirby 	}
25773b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2578842727c2SChris Kirby 
2579842727c2SChris Kirby 	return (error);
2580842727c2SChris Kirby }
2581842727c2SChris Kirby 
25823b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
25833b2aab18SMatthew Ahrens 	const char *ddsqra_name;
25843b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
25853b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
25863b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
2587842727c2SChris Kirby 
25883b2aab18SMatthew Ahrens 
25893b2aab18SMatthew Ahrens /* ARGSUSED */
2590842727c2SChris Kirby static int
25913b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
2592842727c2SChris Kirby {
25933b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
25943b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
25953b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
2596842727c2SChris Kirby 	int error;
25973b2aab18SMatthew Ahrens 	uint64_t newval;
2598842727c2SChris Kirby 
25993b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
2600be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
2601842727c2SChris Kirby 
26023b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
26033b2aab18SMatthew Ahrens 	if (error != 0)
26043b2aab18SMatthew Ahrens 		return (error);
26053b2aab18SMatthew Ahrens 
26063b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
26073b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2608be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2609842727c2SChris Kirby 	}
2610842727c2SChris Kirby 
26113b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
26123b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
26133b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
26143b2aab18SMatthew Ahrens 	if (error != 0) {
26153b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2616842727c2SChris Kirby 		return (error);
2617842727c2SChris Kirby 	}
2618842727c2SChris Kirby 
26193b2aab18SMatthew Ahrens 	if (newval == 0) {
26203b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
26213b2aab18SMatthew Ahrens 		return (0);
26223b2aab18SMatthew Ahrens 	}
2623842727c2SChris Kirby 
26243b2aab18SMatthew Ahrens 	if (newval < ds->ds_phys->ds_referenced_bytes ||
26253b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
26263b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2627be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
26283b2aab18SMatthew Ahrens 	}
26293b2aab18SMatthew Ahrens 
26303b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2631842727c2SChris Kirby 	return (0);
2632842727c2SChris Kirby }
2633842727c2SChris Kirby 
26343b2aab18SMatthew Ahrens static void
26353b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
2636842727c2SChris Kirby {
26373b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
26383b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
26393b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
26403b2aab18SMatthew Ahrens 	uint64_t newval;
2641842727c2SChris Kirby 
26423b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
2643842727c2SChris Kirby 
26443b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
26453b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
26463b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
26473b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
2648842727c2SChris Kirby 
26493b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
26503b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
2651842727c2SChris Kirby 
26523b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
26533b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
26543b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
2655842727c2SChris Kirby 	}
26563b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2657842727c2SChris Kirby }
2658842727c2SChris Kirby 
26593b2aab18SMatthew Ahrens int
26603b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
26613b2aab18SMatthew Ahrens     uint64_t refquota)
2662842727c2SChris Kirby {
26633b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
2664842727c2SChris Kirby 
26653b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
26663b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
26673b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
26683b2aab18SMatthew Ahrens 
26693b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
26703b2aab18SMatthew Ahrens 	    dsl_dataset_set_refquota_sync, &ddsqra, 0));
2671842727c2SChris Kirby }
2672842727c2SChris Kirby 
2673842727c2SChris Kirby static int
26743b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
2675842727c2SChris Kirby {
26763b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
26773b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
2678842727c2SChris Kirby 	dsl_dataset_t *ds;
2679842727c2SChris Kirby 	int error;
26803b2aab18SMatthew Ahrens 	uint64_t newval, unique;
2681d7747cbcSChris Kirby 
26823b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
2683be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
2684842727c2SChris Kirby 
26853b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
26863b2aab18SMatthew Ahrens 	if (error != 0)
2687842727c2SChris Kirby 		return (error);
2688842727c2SChris Kirby 
26893b2aab18SMatthew Ahrens 	if (dsl_dataset_is_snapshot(ds)) {
26903b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2691be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2692842727c2SChris Kirby 	}
2693842727c2SChris Kirby 
26943b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
26953b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
26963b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
26973b2aab18SMatthew Ahrens 	if (error != 0) {
26983b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2699842727c2SChris Kirby 		return (error);
2700842727c2SChris Kirby 	}
2701842727c2SChris Kirby 
27023b2aab18SMatthew Ahrens 	/*
27033b2aab18SMatthew Ahrens 	 * If we are doing the preliminary check in open context, the
27043b2aab18SMatthew Ahrens 	 * space estimates may be inaccurate.
27053b2aab18SMatthew Ahrens 	 */
27063b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
27073b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
27083b2aab18SMatthew Ahrens 		return (0);
2709842727c2SChris Kirby 	}
2710842727c2SChris Kirby 
27113b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
27123b2aab18SMatthew Ahrens 	if (!DS_UNIQUE_IS_ACCURATE(ds))
27133b2aab18SMatthew Ahrens 		dsl_dataset_recalc_head_uniq(ds);
27143b2aab18SMatthew Ahrens 	unique = ds->ds_phys->ds_unique_bytes;
27153b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2716842727c2SChris Kirby 
27173b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
27183b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
27193b2aab18SMatthew Ahrens 		    MAX(unique, ds->ds_reserved);
2720842727c2SChris Kirby 
27213b2aab18SMatthew Ahrens 		if (delta >
27223b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
27233b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
27243b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
2725be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
27263b2aab18SMatthew Ahrens 		}
2727842727c2SChris Kirby 	}
2728842727c2SChris Kirby 
27293b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
27303b2aab18SMatthew Ahrens 	return (0);
2731842727c2SChris Kirby }
2732842727c2SChris Kirby 
27333b2aab18SMatthew Ahrens void
27343b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
27353b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
2736ca45db41SChris Kirby {
27373b2aab18SMatthew Ahrens 	uint64_t newval;
27383b2aab18SMatthew Ahrens 	uint64_t unique;
27393b2aab18SMatthew Ahrens 	int64_t delta;
2740ca45db41SChris Kirby 
27413b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
27423b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
2743ca45db41SChris Kirby 
27443b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
27453b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
2746a7f53a56SChris Kirby 
27473b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
27483b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
27493b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
27503b2aab18SMatthew Ahrens 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
27513b2aab18SMatthew Ahrens 	unique = ds->ds_phys->ds_unique_bytes;
27523b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
27533b2aab18SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
27543b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
27553b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2756a7f53a56SChris Kirby 
27573b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
27583b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
2759ca45db41SChris Kirby }
2760ca45db41SChris Kirby 
27613b2aab18SMatthew Ahrens static void
27623b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
2763842727c2SChris Kirby {
27643b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
27653b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
2766842727c2SChris Kirby 	dsl_dataset_t *ds;
2767842727c2SChris Kirby 
27683b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
27693b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
27703b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
2771842727c2SChris Kirby 	dsl_dataset_rele(ds, FTAG);
2772842727c2SChris Kirby }
2773503ad85cSMatthew Ahrens 
2774503ad85cSMatthew Ahrens int
27753b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
27763b2aab18SMatthew Ahrens     uint64_t refreservation)
2777503ad85cSMatthew Ahrens {
27783b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
2779503ad85cSMatthew Ahrens 
27803b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
27813b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
27823b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
27833b2aab18SMatthew Ahrens 
27843b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
27853b2aab18SMatthew Ahrens 	    dsl_dataset_set_refreservation_sync, &ddsqra, 0));
2786503ad85cSMatthew Ahrens }
278719b94df9SMatthew Ahrens 
278819b94df9SMatthew Ahrens /*
278919b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
279019b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
279119b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
279219b94df9SMatthew Ahrens  * fail and return EINVAL.
279319b94df9SMatthew Ahrens  *
279419b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
279519b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
279619b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
279719b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
279819b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
279919b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
280019b94df9SMatthew Ahrens  *
280119b94df9SMatthew Ahrens  * space freed                         [---------------------]
280219b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
280319b94df9SMatthew Ahrens  *                                         oldsnap            new
280419b94df9SMatthew Ahrens  */
280519b94df9SMatthew Ahrens int
280619b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
280719b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
280819b94df9SMatthew Ahrens {
280919b94df9SMatthew Ahrens 	int err = 0;
281019b94df9SMatthew Ahrens 	uint64_t snapobj;
281119b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
281219b94df9SMatthew Ahrens 
28133b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
28143b2aab18SMatthew Ahrens 
281519b94df9SMatthew Ahrens 	*usedp = 0;
2816ad135b5dSChristopher Siden 	*usedp += new->ds_phys->ds_referenced_bytes;
2817ad135b5dSChristopher Siden 	*usedp -= oldsnap->ds_phys->ds_referenced_bytes;
281819b94df9SMatthew Ahrens 
281919b94df9SMatthew Ahrens 	*compp = 0;
282019b94df9SMatthew Ahrens 	*compp += new->ds_phys->ds_compressed_bytes;
282119b94df9SMatthew Ahrens 	*compp -= oldsnap->ds_phys->ds_compressed_bytes;
282219b94df9SMatthew Ahrens 
282319b94df9SMatthew Ahrens 	*uncompp = 0;
282419b94df9SMatthew Ahrens 	*uncompp += new->ds_phys->ds_uncompressed_bytes;
282519b94df9SMatthew Ahrens 	*uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
282619b94df9SMatthew Ahrens 
282719b94df9SMatthew Ahrens 	snapobj = new->ds_object;
282819b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
282919b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
283019b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
283119b94df9SMatthew Ahrens 
2832ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
2833ad135b5dSChristopher Siden 			snap = new;
2834ad135b5dSChristopher Siden 		} else {
2835ad135b5dSChristopher Siden 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2836ad135b5dSChristopher Siden 			if (err != 0)
2837ad135b5dSChristopher Siden 				break;
2838ad135b5dSChristopher Siden 		}
283919b94df9SMatthew Ahrens 
284019b94df9SMatthew Ahrens 		if (snap->ds_phys->ds_prev_snap_txg ==
284119b94df9SMatthew Ahrens 		    oldsnap->ds_phys->ds_creation_txg) {
284219b94df9SMatthew Ahrens 			/*
284319b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
284419b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
284519b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
284619b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
284719b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
284819b94df9SMatthew Ahrens 			 * optimization itself.
284919b94df9SMatthew Ahrens 			 */
285019b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
285119b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
285219b94df9SMatthew Ahrens 		} else {
285319b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
285419b94df9SMatthew Ahrens 			    0, oldsnap->ds_phys->ds_creation_txg,
285519b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
285619b94df9SMatthew Ahrens 		}
285719b94df9SMatthew Ahrens 		*usedp += used;
285819b94df9SMatthew Ahrens 		*compp += comp;
285919b94df9SMatthew Ahrens 		*uncompp += uncomp;
286019b94df9SMatthew Ahrens 
286119b94df9SMatthew Ahrens 		/*
286219b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
286319b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
286419b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
286519b94df9SMatthew Ahrens 		 */
286619b94df9SMatthew Ahrens 		snapobj = snap->ds_phys->ds_prev_snap_obj;
2867ad135b5dSChristopher Siden 		if (snap != new)
2868ad135b5dSChristopher Siden 			dsl_dataset_rele(snap, FTAG);
286919b94df9SMatthew Ahrens 		if (snapobj == 0) {
2870be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
287119b94df9SMatthew Ahrens 			break;
287219b94df9SMatthew Ahrens 		}
287319b94df9SMatthew Ahrens 
287419b94df9SMatthew Ahrens 	}
287519b94df9SMatthew Ahrens 	return (err);
287619b94df9SMatthew Ahrens }
287719b94df9SMatthew Ahrens 
287819b94df9SMatthew Ahrens /*
287919b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
288019b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
288119b94df9SMatthew Ahrens  *
288219b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
288319b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
288419b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
288519b94df9SMatthew Ahrens  *
288619b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
288719b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
288819b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
288919b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
289019b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
289119b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
289219b94df9SMatthew Ahrens  */
289319b94df9SMatthew Ahrens int
289419b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
289519b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
289619b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
289719b94df9SMatthew Ahrens {
289819b94df9SMatthew Ahrens 	int err = 0;
289919b94df9SMatthew Ahrens 	uint64_t snapobj;
290019b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
290119b94df9SMatthew Ahrens 
290219b94df9SMatthew Ahrens 	ASSERT(dsl_dataset_is_snapshot(firstsnap));
290319b94df9SMatthew Ahrens 	ASSERT(dsl_dataset_is_snapshot(lastsnap));
290419b94df9SMatthew Ahrens 
290519b94df9SMatthew Ahrens 	/*
290619b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
290719b94df9SMatthew Ahrens 	 * is before lastsnap.
290819b94df9SMatthew Ahrens 	 */
290919b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
291019b94df9SMatthew Ahrens 	    firstsnap->ds_phys->ds_creation_txg >
291119b94df9SMatthew Ahrens 	    lastsnap->ds_phys->ds_creation_txg)
2912be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
291319b94df9SMatthew Ahrens 
291419b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
291519b94df9SMatthew Ahrens 
291619b94df9SMatthew Ahrens 	snapobj = lastsnap->ds_phys->ds_next_snap_obj;
291719b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
291819b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
291919b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
292019b94df9SMatthew Ahrens 
292119b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
292219b94df9SMatthew Ahrens 		if (err != 0)
292319b94df9SMatthew Ahrens 			break;
292419b94df9SMatthew Ahrens 
292519b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
292619b94df9SMatthew Ahrens 		    firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
292719b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
292819b94df9SMatthew Ahrens 		*usedp += used;
292919b94df9SMatthew Ahrens 		*compp += comp;
293019b94df9SMatthew Ahrens 		*uncompp += uncomp;
293119b94df9SMatthew Ahrens 
293219b94df9SMatthew Ahrens 		snapobj = ds->ds_phys->ds_prev_snap_obj;
293319b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
293419b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
293519b94df9SMatthew Ahrens 	}
293619b94df9SMatthew Ahrens 	return (err);
293719b94df9SMatthew Ahrens }
29383b2aab18SMatthew Ahrens 
29393b2aab18SMatthew Ahrens /*
29403b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
29413b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
29423b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
29433b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
29443b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
29453b2aab18SMatthew Ahrens  */
29463b2aab18SMatthew Ahrens boolean_t
29473b2aab18SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
29483b2aab18SMatthew Ahrens {
29493b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
29503b2aab18SMatthew Ahrens 	int error;
29513b2aab18SMatthew Ahrens 	boolean_t ret;
29523b2aab18SMatthew Ahrens 
29533b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
29543b2aab18SMatthew Ahrens 
29553b2aab18SMatthew Ahrens 	if (earlier->ds_phys->ds_creation_txg >=
29563b2aab18SMatthew Ahrens 	    later->ds_phys->ds_creation_txg)
29573b2aab18SMatthew Ahrens 		return (B_FALSE);
29583b2aab18SMatthew Ahrens 
29593b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
29603b2aab18SMatthew Ahrens 		return (B_TRUE);
29613b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
29623b2aab18SMatthew Ahrens 		return (B_FALSE);
29633b2aab18SMatthew Ahrens 
29643b2aab18SMatthew Ahrens 	if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
29653b2aab18SMatthew Ahrens 		return (B_TRUE);
29663b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
29673b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
29683b2aab18SMatthew Ahrens 	    later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
29693b2aab18SMatthew Ahrens 	if (error != 0)
29703b2aab18SMatthew Ahrens 		return (B_FALSE);
29713b2aab18SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier);
29723b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
29733b2aab18SMatthew Ahrens 	return (ret);
29743b2aab18SMatthew Ahrens }
2975