xref: /titanic_41/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision c4cb95767ae6931f185dc5c02edb165c2ff75b10)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
225afc78aaSChris Kirby  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23*c4cb9576SJosef 'Jeff' Sipek  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24bc93cffdSJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
256857e7a9SAlexander Stetsenko  * Copyright (c) 2014 RackTop Systems.
260521c5ebSJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/dmu_objset.h>
30fa9e4066Sahrens #include <sys/dsl_dataset.h>
31fa9e4066Sahrens #include <sys/dsl_dir.h>
3299653d4eSeschrock #include <sys/dsl_prop.h>
331d452cf5Sahrens #include <sys/dsl_synctask.h>
34fa9e4066Sahrens #include <sys/dmu_traverse.h>
354e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
36fa9e4066Sahrens #include <sys/dmu_tx.h>
37fa9e4066Sahrens #include <sys/arc.h>
38fa9e4066Sahrens #include <sys/zio.h>
39fa9e4066Sahrens #include <sys/zap.h>
40ad135b5dSChristopher Siden #include <sys/zfeature.h>
41fa9e4066Sahrens #include <sys/unique.h>
42fa9e4066Sahrens #include <sys/zfs_context.h>
43cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
44ecd6cf80Smarks #include <sys/spa.h>
45088f3894Sahrens #include <sys/zfs_znode.h>
46c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
47842727c2SChris Kirby #include <sys/zvol.h>
483f9d6ad7SLin Ling #include <sys/dsl_scan.h>
49cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
503b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
513b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
52e69d9941SMatthew Ahrens #include <sys/dsl_bookmark.h>
538429b235SMatthew Ahrens #include <sys/dmu_send.h>
548429b235SMatthew Ahrens #include <sys/zio_checksum.h>
558429b235SMatthew Ahrens #include <sys/zio_compress.h>
568429b235SMatthew Ahrens #include <zfs_fletcher.h>
57e1930233Sbonwick 
58d1a98260SMatthew Ahrens /*
59d1a98260SMatthew Ahrens  * The SPA supports block sizes up to 16MB.  However, very large blocks
60d1a98260SMatthew Ahrens  * can have an impact on i/o latency (e.g. tying up a spinning disk for
61d1a98260SMatthew Ahrens  * ~300ms), and also potentially on the memory allocator.  Therefore,
62d1a98260SMatthew Ahrens  * we do not allow the recordsize to be set larger than zfs_max_recordsize
63d1a98260SMatthew Ahrens  * (default 1MB).  Larger blocks can be created by changing this tunable,
64d1a98260SMatthew Ahrens  * and pools with larger blocks can always be imported and used, regardless
65d1a98260SMatthew Ahrens  * of this setting.
66d1a98260SMatthew Ahrens  */
67d1a98260SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024;
68d1a98260SMatthew Ahrens 
69cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
70cde58dbcSMatthew Ahrens 	{ \
71cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
72cde58dbcSMatthew Ahrens 		(x) = (y); \
73cde58dbcSMatthew Ahrens 		(y) = __tmp; \
74cde58dbcSMatthew Ahrens 	}
75cde58dbcSMatthew Ahrens 
7655434c77Sek110237 #define	DS_REF_MAX	(1ULL << 62)
77fa9e4066Sahrens 
788d62b223SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
798d62b223SJustin T. Gibbs 
80a9799022Sck153898 /*
81a9799022Sck153898  * Figure out how much of this delta should be propogated to the dsl_dir
82a9799022Sck153898  * layer.  If there's a refreservation, that space has already been
83a9799022Sck153898  * partially accounted for in our ancestors.
84a9799022Sck153898  */
85a9799022Sck153898 static int64_t
parent_delta(dsl_dataset_t * ds,int64_t delta)86a9799022Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta)
87a9799022Sck153898 {
888d62b223SJustin T. Gibbs 	dsl_dataset_phys_t *ds_phys;
89a9799022Sck153898 	uint64_t old_bytes, new_bytes;
90a9799022Sck153898 
91a9799022Sck153898 	if (ds->ds_reserved == 0)
92a9799022Sck153898 		return (delta);
93a9799022Sck153898 
948d62b223SJustin T. Gibbs 	ds_phys = dsl_dataset_phys(ds);
958d62b223SJustin T. Gibbs 	old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
968d62b223SJustin T. Gibbs 	new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
97a9799022Sck153898 
98a9799022Sck153898 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
99a9799022Sck153898 	return (new_bytes - old_bytes);
100a9799022Sck153898 }
101fa9e4066Sahrens 
102fa9e4066Sahrens void
dsl_dataset_block_born(dsl_dataset_t * ds,const blkptr_t * bp,dmu_tx_t * tx)103b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
104fa9e4066Sahrens {
105b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
106fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
107fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
108a9799022Sck153898 	int64_t delta;
109fa9e4066Sahrens 
1103f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
111fa9e4066Sahrens 
112fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
113fa9e4066Sahrens 	/* It could have been compressed away to nothing */
114fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
115fa9e4066Sahrens 		return;
116fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
117ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
118fa9e4066Sahrens 	if (ds == NULL) {
119ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
120ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
121fa9e4066Sahrens 		return;
122fa9e4066Sahrens 	}
1233f9d6ad7SLin Ling 
124b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
125fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
126a9799022Sck153898 	delta = parent_delta(ds, used);
1278d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes += used;
1288d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
1298d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
1308d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes += used;
1310cc589a4SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
1320cc589a4SMatthew Ahrens 		ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
1330cc589a4SMatthew Ahrens 		    B_TRUE;
1340cc589a4SMatthew Ahrens 	}
135fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
13674e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
13774e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
13874e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
13974e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
140fa9e4066Sahrens }
141fa9e4066Sahrens 
142cdb0ab79Smaybee int
dsl_dataset_block_kill(dsl_dataset_t * ds,const blkptr_t * bp,dmu_tx_t * tx,boolean_t async)143b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
144b24ab676SJeff Bonwick     boolean_t async)
145fa9e4066Sahrens {
14649064978SMax Grossman 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
14749064978SMax Grossman 	int compressed = BP_GET_PSIZE(bp);
14849064978SMax Grossman 	int uncompressed = BP_GET_UCSIZE(bp);
14949064978SMax Grossman 
150fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
151cdb0ab79Smaybee 		return (0);
152fa9e4066Sahrens 
153b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
154b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
155b24ab676SJeff Bonwick 
156fa9e4066Sahrens 	if (ds == NULL) {
157b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
158ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
159ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
160cdb0ab79Smaybee 		return (used);
161fa9e4066Sahrens 	}
162fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
163fa9e4066Sahrens 
1640521c5ebSJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
165fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
166fa9e4066Sahrens 
1678d62b223SJustin T. Gibbs 	if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
168a9799022Sck153898 		int64_t delta;
169c717a561Smaybee 
1703f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
171b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
172fa9e4066Sahrens 
173fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
1748d62b223SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
175a9799022Sck153898 		    !DS_UNIQUE_IS_ACCURATE(ds));
176a9799022Sck153898 		delta = parent_delta(ds, -used);
1778d62b223SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_unique_bytes -= used;
178fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
17974e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
180a9799022Sck153898 		    delta, -compressed, -uncompressed, tx);
18174e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
18274e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
183fa9e4066Sahrens 	} else {
184fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
185b24ab676SJeff Bonwick 		if (async) {
186b24ab676SJeff Bonwick 			/*
187b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
188b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
189cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
190b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
191b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
192b24ab676SJeff Bonwick 			 */
193cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
194b24ab676SJeff Bonwick 		} else {
195cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
196b24ab676SJeff Bonwick 		}
197fa9e4066Sahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
1988d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj);
1998d62b223SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
200a4611edeSahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
2018d62b223SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
20299653d4eSeschrock 		    ds->ds_object && bp->blk_birth >
2038d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
204fa9e4066Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
205fa9e4066Sahrens 			mutex_enter(&ds->ds_prev->ds_lock);
2068d62b223SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
207fa9e4066Sahrens 			mutex_exit(&ds->ds_prev->ds_lock);
208fa9e4066Sahrens 		}
2093f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
21074e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
21174e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
21274e7dc98SMatthew Ahrens 		}
213fa9e4066Sahrens 	}
214fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
2158d62b223SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
2168d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
2178d62b223SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
2188d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
2198d62b223SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
2208d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
221fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
222cdb0ab79Smaybee 
223cdb0ab79Smaybee 	return (used);
224fa9e4066Sahrens }
225fa9e4066Sahrens 
226ea8dc4b6Seschrock uint64_t
dsl_dataset_prev_snap_txg(dsl_dataset_t * ds)227ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
228fa9e4066Sahrens {
229a2eea2e1Sahrens 	uint64_t trysnap = 0;
230a2eea2e1Sahrens 
231fa9e4066Sahrens 	if (ds == NULL)
232ea8dc4b6Seschrock 		return (0);
233fa9e4066Sahrens 	/*
234fa9e4066Sahrens 	 * The snapshot creation could fail, but that would cause an
235fa9e4066Sahrens 	 * incorrect FALSE return, which would only result in an
236fa9e4066Sahrens 	 * overestimation of the amount of space that an operation would
237fa9e4066Sahrens 	 * consume, which is OK.
238fa9e4066Sahrens 	 *
239fa9e4066Sahrens 	 * There's also a small window where we could miss a pending
240fa9e4066Sahrens 	 * snapshot, because we could set the sync task in the quiescing
241fa9e4066Sahrens 	 * phase.  So this should only be used as a guess.
242fa9e4066Sahrens 	 */
243a2eea2e1Sahrens 	if (ds->ds_trysnap_txg >
244a2eea2e1Sahrens 	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
245a2eea2e1Sahrens 		trysnap = ds->ds_trysnap_txg;
2468d62b223SJustin T. Gibbs 	return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap));
247ea8dc4b6Seschrock }
248ea8dc4b6Seschrock 
2493d692628SSanjeev Bagewadi boolean_t
dsl_dataset_block_freeable(dsl_dataset_t * ds,const blkptr_t * bp,uint64_t blk_birth)250c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
251c7cd2421SGeorge Wilson     uint64_t blk_birth)
252ea8dc4b6Seschrock {
25349064978SMax Grossman 	if (blk_birth <= dsl_dataset_prev_snap_txg(ds) ||
25449064978SMax Grossman 	    (bp != NULL && BP_IS_HOLE(bp)))
255c7cd2421SGeorge Wilson 		return (B_FALSE);
256c7cd2421SGeorge Wilson 
257c7cd2421SGeorge Wilson 	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
258c7cd2421SGeorge Wilson 
259c7cd2421SGeorge Wilson 	return (B_TRUE);
260fa9e4066Sahrens }
261fa9e4066Sahrens 
262*c4cb9576SJosef 'Jeff' Sipek /*
263*c4cb9576SJosef 'Jeff' Sipek  * We have to release the fsid syncronously or we risk that a subsequent
264*c4cb9576SJosef 'Jeff' Sipek  * mount of the same dataset will fail to unique_insert the fsid.  This
265*c4cb9576SJosef 'Jeff' Sipek  * failure would manifest itself as the fsid of this dataset changing
266*c4cb9576SJosef 'Jeff' Sipek  * between mounts which makes NFS clients quite unhappy.
267*c4cb9576SJosef 'Jeff' Sipek  */
268fa9e4066Sahrens static void
dsl_dataset_evict_sync(void * dbu)269*c4cb9576SJosef 'Jeff' Sipek dsl_dataset_evict_sync(void *dbu)
270*c4cb9576SJosef 'Jeff' Sipek {
271*c4cb9576SJosef 'Jeff' Sipek 	dsl_dataset_t *ds = dbu;
272*c4cb9576SJosef 'Jeff' Sipek 
273*c4cb9576SJosef 'Jeff' Sipek 	ASSERT(ds->ds_owner == NULL);
274*c4cb9576SJosef 'Jeff' Sipek 
275*c4cb9576SJosef 'Jeff' Sipek 	unique_remove(ds->ds_fsid_guid);
276*c4cb9576SJosef 'Jeff' Sipek }
277*c4cb9576SJosef 'Jeff' Sipek 
278*c4cb9576SJosef 'Jeff' Sipek static void
dsl_dataset_evict_async(void * dbu)279*c4cb9576SJosef 'Jeff' Sipek dsl_dataset_evict_async(void *dbu)
280fa9e4066Sahrens {
2810521c5ebSJustin Gibbs 	dsl_dataset_t *ds = dbu;
282fa9e4066Sahrens 
2833b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
284fa9e4066Sahrens 
2850521c5ebSJustin Gibbs 	ds->ds_dbuf = NULL;
2860521c5ebSJustin Gibbs 
287503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
288503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
289fa9e4066Sahrens 
290fa9e4066Sahrens 	if (ds->ds_prev) {
2913b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
292fa9e4066Sahrens 		ds->ds_prev = NULL;
293fa9e4066Sahrens 	}
294fa9e4066Sahrens 
295cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
2960521c5ebSJustin Gibbs 	if (ds->ds_deadlist.dl_os != NULL)
297cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
298745cd3c5Smaybee 	if (ds->ds_dir)
2990521c5ebSJustin Gibbs 		dsl_dir_async_rele(ds->ds_dir, ds);
300fa9e4066Sahrens 
30191ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
302fa9e4066Sahrens 
3035ad82045Snd150628 	mutex_destroy(&ds->ds_lock);
30491ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
305fa565b39SJorgen Lundman 	mutex_destroy(&ds->ds_sendstream_lock);
3063b2aab18SMatthew Ahrens 	refcount_destroy(&ds->ds_longholds);
3075ad82045Snd150628 
308fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
309fa9e4066Sahrens }
310fa9e4066Sahrens 
3113b2aab18SMatthew Ahrens int
dsl_dataset_get_snapname(dsl_dataset_t * ds)312fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
313fa9e4066Sahrens {
314fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
315fa9e4066Sahrens 	int err;
316fa9e4066Sahrens 	dmu_buf_t *headdbuf;
317fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
318fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
319fa9e4066Sahrens 
320fa9e4066Sahrens 	if (ds->ds_snapname[0])
321ea8dc4b6Seschrock 		return (0);
3228d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
323ea8dc4b6Seschrock 		return (0);
324fa9e4066Sahrens 
3258d62b223SJustin T. Gibbs 	err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
326ea8dc4b6Seschrock 	    FTAG, &headdbuf);
3273b2aab18SMatthew Ahrens 	if (err != 0)
328ea8dc4b6Seschrock 		return (err);
329fa9e4066Sahrens 	headphys = headdbuf->db_data;
330fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
331e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
332ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
333ea8dc4b6Seschrock 	return (err);
334fa9e4066Sahrens }
335fa9e4066Sahrens 
3363b2aab18SMatthew Ahrens int
dsl_dataset_snap_lookup(dsl_dataset_t * ds,const char * name,uint64_t * value)337745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
338ab04eb8eStimh {
339745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3408d62b223SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
341ab04eb8eStimh 	matchtype_t mt;
342ab04eb8eStimh 	int err;
343ab04eb8eStimh 
3448d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
345ab04eb8eStimh 		mt = MT_FIRST;
346ab04eb8eStimh 	else
347ab04eb8eStimh 		mt = MT_EXACT;
348ab04eb8eStimh 
349745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
350ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
351ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
352745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
353ab04eb8eStimh 	return (err);
354ab04eb8eStimh }
355ab04eb8eStimh 
3563b2aab18SMatthew Ahrens int
dsl_dataset_snap_remove(dsl_dataset_t * ds,const char * name,dmu_tx_t * tx,boolean_t adj_cnt)357bc93cffdSJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
358bc93cffdSJerry Jelinek     boolean_t adj_cnt)
359ab04eb8eStimh {
360745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3618d62b223SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
362ab04eb8eStimh 	matchtype_t mt;
363ab04eb8eStimh 	int err;
364ab04eb8eStimh 
36571eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
36671eb0538SChris Kirby 
3678d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
368ab04eb8eStimh 		mt = MT_FIRST;
369ab04eb8eStimh 	else
370ab04eb8eStimh 		mt = MT_EXACT;
371ab04eb8eStimh 
372745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
373ab04eb8eStimh 	if (err == ENOTSUP && mt == MT_FIRST)
374745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
375bc93cffdSJerry Jelinek 
376bc93cffdSJerry Jelinek 	if (err == 0 && adj_cnt)
377bc93cffdSJerry Jelinek 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
378bc93cffdSJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
379bc93cffdSJerry Jelinek 
380ab04eb8eStimh 	return (err);
381ab04eb8eStimh }
382ab04eb8eStimh 
383ab64b4bbSJustin T. Gibbs boolean_t
dsl_dataset_try_add_ref(dsl_pool_t * dp,dsl_dataset_t * ds,void * tag)384ab64b4bbSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
385ab64b4bbSJustin T. Gibbs {
3862c41dd15SJustin T. Gibbs 	dmu_buf_t *dbuf = ds->ds_dbuf;
3872c41dd15SJustin T. Gibbs 	boolean_t result = B_FALSE;
3882c41dd15SJustin T. Gibbs 
3892c41dd15SJustin T. Gibbs 	if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
3902c41dd15SJustin T. Gibbs 	    ds->ds_object, DMU_BONUS_BLKID, tag)) {
3912c41dd15SJustin T. Gibbs 
3922c41dd15SJustin T. Gibbs 		if (ds == dmu_buf_get_user(dbuf))
3932c41dd15SJustin T. Gibbs 			result = B_TRUE;
3942c41dd15SJustin T. Gibbs 		else
3952c41dd15SJustin T. Gibbs 			dmu_buf_rele(dbuf, tag);
3962c41dd15SJustin T. Gibbs 	}
3972c41dd15SJustin T. Gibbs 
3982c41dd15SJustin T. Gibbs 	return (result);
399ab64b4bbSJustin T. Gibbs }
400ab64b4bbSJustin T. Gibbs 
4013b2aab18SMatthew Ahrens int
dsl_dataset_hold_obj(dsl_pool_t * dp,uint64_t dsobj,void * tag,dsl_dataset_t ** dsp)4023b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
403745cd3c5Smaybee     dsl_dataset_t **dsp)
404fa9e4066Sahrens {
405fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
406fa9e4066Sahrens 	dmu_buf_t *dbuf;
407fa9e4066Sahrens 	dsl_dataset_t *ds;
408ea8dc4b6Seschrock 	int err;
409a7f53a56SChris Kirby 	dmu_object_info_t doi;
410fa9e4066Sahrens 
4113b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
412fa9e4066Sahrens 
413ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
4143b2aab18SMatthew Ahrens 	if (err != 0)
415ea8dc4b6Seschrock 		return (err);
416a7f53a56SChris Kirby 
417a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
418a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
419dbc43af2SMatthew Ahrens 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
420b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
421be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
422b287be1bSWill Andrews 	}
423a7f53a56SChris Kirby 
424fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
425fa9e4066Sahrens 	if (ds == NULL) {
426d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
427fa9e4066Sahrens 
428fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
429fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
430fa9e4066Sahrens 		ds->ds_object = dsobj;
4310521c5ebSJustin Gibbs 		ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
432fa9e4066Sahrens 
4335ad82045Snd150628 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
43491ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
4354e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
4363b2aab18SMatthew Ahrens 		refcount_create(&ds->ds_longholds);
4375ad82045Snd150628 
438cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
439cde58dbcSMatthew Ahrens 		dsl_deadlist_open(&ds->ds_deadlist,
4408d62b223SJustin T. Gibbs 		    mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
441cde58dbcSMatthew Ahrens 
4424e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4434e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
4444e3c9f44SBill Pijewski 
445d1a98260SMatthew Ahrens 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
4460cc589a4SMatthew Ahrens 			for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4470cc589a4SMatthew Ahrens 				if (!(spa_feature_table[f].fi_flags &
4480cc589a4SMatthew Ahrens 				    ZFEATURE_FLAG_PER_DATASET))
4490cc589a4SMatthew Ahrens 					continue;
4500cc589a4SMatthew Ahrens 				err = zap_contains(mos, dsobj,
4510cc589a4SMatthew Ahrens 				    spa_feature_table[f].fi_guid);
4520cc589a4SMatthew Ahrens 				if (err == 0) {
4530cc589a4SMatthew Ahrens 					ds->ds_feature_inuse[f] = B_TRUE;
4540cc589a4SMatthew Ahrens 				} else {
4550cc589a4SMatthew Ahrens 					ASSERT3U(err, ==, ENOENT);
4560cc589a4SMatthew Ahrens 					err = 0;
4570cc589a4SMatthew Ahrens 				}
458ea9b25c1SJustin T. Gibbs 			}
459d1a98260SMatthew Ahrens 		}
460d1a98260SMatthew Ahrens 
4613b2aab18SMatthew Ahrens 		err = dsl_dir_hold_obj(dp,
4620cc589a4SMatthew Ahrens 		    dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir);
4633b2aab18SMatthew Ahrens 		if (err != 0) {
4645ad82045Snd150628 			mutex_destroy(&ds->ds_lock);
46591ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
466fa565b39SJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
4673b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
468cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
469cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
470ea8dc4b6Seschrock 			kmem_free(ds, sizeof (dsl_dataset_t));
471ea8dc4b6Seschrock 			dmu_buf_rele(dbuf, tag);
472ea8dc4b6Seschrock 			return (err);
473ea8dc4b6Seschrock 		}
474fa9e4066Sahrens 
4750521c5ebSJustin Gibbs 		if (!ds->ds_is_snapshot) {
476fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
4778d62b223SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
4783b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
4798d62b223SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_prev_snap_obj,
480745cd3c5Smaybee 				    ds, &ds->ds_prev);
481fa9e4066Sahrens 			}
482e69d9941SMatthew Ahrens 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
483e69d9941SMatthew Ahrens 				int zaperr = zap_lookup(mos, ds->ds_object,
484e69d9941SMatthew Ahrens 				    DS_FIELD_BOOKMARK_NAMES,
485e69d9941SMatthew Ahrens 				    sizeof (ds->ds_bookmarks), 1,
486e69d9941SMatthew Ahrens 				    &ds->ds_bookmarks);
487e69d9941SMatthew Ahrens 				if (zaperr != ENOENT)
488e69d9941SMatthew Ahrens 					VERIFY0(zaperr);
489e69d9941SMatthew Ahrens 			}
490842727c2SChris Kirby 		} else {
491842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
492ea8dc4b6Seschrock 				err = dsl_dataset_get_snapname(ds);
4938d62b223SJustin T. Gibbs 			if (err == 0 &&
4948d62b223SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
495842727c2SChris Kirby 				err = zap_count(
496842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
4978d62b223SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_userrefs_obj,
498842727c2SChris Kirby 				    &ds->ds_userrefs);
499842727c2SChris Kirby 			}
500fa9e4066Sahrens 		}
501fa9e4066Sahrens 
5020521c5ebSJustin Gibbs 		if (err == 0 && !ds->ds_is_snapshot) {
5033b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
5043b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
5053b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
506cb625fb5Sck153898 			if (err == 0) {
5073b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
5083b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
5093b2aab18SMatthew Ahrens 				    &ds->ds_quota);
510cb625fb5Sck153898 			}
511cb625fb5Sck153898 		} else {
512cb625fb5Sck153898 			ds->ds_reserved = ds->ds_quota = 0;
513cb625fb5Sck153898 		}
514cb625fb5Sck153898 
515*c4cb9576SJosef 'Jeff' Sipek 		dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
516*c4cb9576SJosef 'Jeff' Sipek 		    dsl_dataset_evict_async, &ds->ds_dbuf);
5170521c5ebSJustin Gibbs 		if (err == 0)
5180521c5ebSJustin Gibbs 			winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
5190521c5ebSJustin Gibbs 
5200521c5ebSJustin Gibbs 		if (err != 0 || winner != NULL) {
521cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
522cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
523745cd3c5Smaybee 			if (ds->ds_prev)
5243b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
5253b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
5265ad82045Snd150628 			mutex_destroy(&ds->ds_lock);
52791ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
528fa565b39SJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
5293b2aab18SMatthew Ahrens 			refcount_destroy(&ds->ds_longholds);
530fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
5313b2aab18SMatthew Ahrens 			if (err != 0) {
532ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
533ea8dc4b6Seschrock 				return (err);
534ea8dc4b6Seschrock 			}
535fa9e4066Sahrens 			ds = winner;
536fa9e4066Sahrens 		} else {
53791ebeef5Sahrens 			ds->ds_fsid_guid =
5388d62b223SJustin T. Gibbs 			    unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
539*c4cb9576SJosef 'Jeff' Sipek 			if (ds->ds_fsid_guid !=
540*c4cb9576SJosef 'Jeff' Sipek 			    dsl_dataset_phys(ds)->ds_fsid_guid) {
541*c4cb9576SJosef 'Jeff' Sipek 				zfs_dbgmsg("ds_fsid_guid changed from "
542*c4cb9576SJosef 'Jeff' Sipek 				    "%llx to %llx for pool %s dataset id %llu",
543*c4cb9576SJosef 'Jeff' Sipek 				    (long long)
544*c4cb9576SJosef 'Jeff' Sipek 				    dsl_dataset_phys(ds)->ds_fsid_guid,
545*c4cb9576SJosef 'Jeff' Sipek 				    (long long)ds->ds_fsid_guid,
546*c4cb9576SJosef 'Jeff' Sipek 				    spa_name(dp->dp_spa),
547*c4cb9576SJosef 'Jeff' Sipek 				    dsobj);
548*c4cb9576SJosef 'Jeff' Sipek 			}
549fa9e4066Sahrens 		}
550fa9e4066Sahrens 	}
551fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
5528d62b223SJustin T. Gibbs 	ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
5538d62b223SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
554afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
55584db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
556ea8dc4b6Seschrock 	*dsp = ds;
557ea8dc4b6Seschrock 	return (0);
558fa9e4066Sahrens }
559fa9e4066Sahrens 
5603b2aab18SMatthew Ahrens int
dsl_dataset_hold(dsl_pool_t * dp,const char * name,void * tag,dsl_dataset_t ** dsp)5613b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name,
5623b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
563745cd3c5Smaybee {
5643b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
5653b2aab18SMatthew Ahrens 	const char *snapname;
5663b2aab18SMatthew Ahrens 	uint64_t obj;
5673b2aab18SMatthew Ahrens 	int err = 0;
5685f9bb2f3SPaul Dagnelie 	dsl_dataset_t *ds;
569745cd3c5Smaybee 
5703b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
5713b2aab18SMatthew Ahrens 	if (err != 0)
5723b2aab18SMatthew Ahrens 		return (err);
573745cd3c5Smaybee 
5743b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
5758d62b223SJustin T. Gibbs 	obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
5763b2aab18SMatthew Ahrens 	if (obj != 0)
5775f9bb2f3SPaul Dagnelie 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
5783b2aab18SMatthew Ahrens 	else
579be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
5803b2aab18SMatthew Ahrens 
5813b2aab18SMatthew Ahrens 	/* we may be looking for a snapshot */
5823b2aab18SMatthew Ahrens 	if (err == 0 && snapname != NULL) {
5835f9bb2f3SPaul Dagnelie 		dsl_dataset_t *snap_ds;
5843b2aab18SMatthew Ahrens 
5853b2aab18SMatthew Ahrens 		if (*snapname++ != '@') {
5865f9bb2f3SPaul Dagnelie 			dsl_dataset_rele(ds, tag);
5873b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
588be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
589745cd3c5Smaybee 		}
5903b2aab18SMatthew Ahrens 
5913b2aab18SMatthew Ahrens 		dprintf("looking for snapshot '%s'\n", snapname);
5925f9bb2f3SPaul Dagnelie 		err = dsl_dataset_snap_lookup(ds, snapname, &obj);
5933b2aab18SMatthew Ahrens 		if (err == 0)
5945f9bb2f3SPaul Dagnelie 			err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds);
5955f9bb2f3SPaul Dagnelie 		dsl_dataset_rele(ds, tag);
5963b2aab18SMatthew Ahrens 
5973b2aab18SMatthew Ahrens 		if (err == 0) {
5985f9bb2f3SPaul Dagnelie 			mutex_enter(&snap_ds->ds_lock);
5995f9bb2f3SPaul Dagnelie 			if (snap_ds->ds_snapname[0] == 0)
6005f9bb2f3SPaul Dagnelie 				(void) strlcpy(snap_ds->ds_snapname, snapname,
6015f9bb2f3SPaul Dagnelie 				    sizeof (snap_ds->ds_snapname));
6025f9bb2f3SPaul Dagnelie 			mutex_exit(&snap_ds->ds_lock);
6035f9bb2f3SPaul Dagnelie 			ds = snap_ds;
6045f9bb2f3SPaul Dagnelie 		}
6055f9bb2f3SPaul Dagnelie 	}
6065f9bb2f3SPaul Dagnelie 	if (err == 0)
6073b2aab18SMatthew Ahrens 		*dsp = ds;
6083b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
609745cd3c5Smaybee 	return (err);
610745cd3c5Smaybee }
611745cd3c5Smaybee 
612745cd3c5Smaybee int
dsl_dataset_own_obj(dsl_pool_t * dp,uint64_t dsobj,void * tag,dsl_dataset_t ** dsp)6133b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
614503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
615745cd3c5Smaybee {
616503ad85cSMatthew Ahrens 	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
6173b2aab18SMatthew Ahrens 	if (err != 0)
618745cd3c5Smaybee 		return (err);
6193b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
620503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
6214f5064b7SMark J Musante 		*dsp = NULL;
622be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
623745cd3c5Smaybee 	}
624745cd3c5Smaybee 	return (0);
625745cd3c5Smaybee }
626745cd3c5Smaybee 
627745cd3c5Smaybee int
dsl_dataset_own(dsl_pool_t * dp,const char * name,void * tag,dsl_dataset_t ** dsp)6283b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name,
629503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
630fa9e4066Sahrens {
6313b2aab18SMatthew Ahrens 	int err = dsl_dataset_hold(dp, name, tag, dsp);
6323b2aab18SMatthew Ahrens 	if (err != 0)
633745cd3c5Smaybee 		return (err);
6343b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
635503ad85cSMatthew Ahrens 		dsl_dataset_rele(*dsp, tag);
636be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
637745cd3c5Smaybee 	}
638745cd3c5Smaybee 	return (0);
639fa9e4066Sahrens }
640fa9e4066Sahrens 
6413b2aab18SMatthew Ahrens /*
6423b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
6433b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
6443b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
6453b2aab18SMatthew Ahrens  *
6463b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
6473b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
6483b2aab18SMatthew Ahrens  * and accessed.
6493b2aab18SMatthew Ahrens  */
6503b2aab18SMatthew Ahrens void
dsl_dataset_long_hold(dsl_dataset_t * ds,void * tag)6513b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
6523b2aab18SMatthew Ahrens {
6533b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
6543b2aab18SMatthew Ahrens 	(void) refcount_add(&ds->ds_longholds, tag);
6553b2aab18SMatthew Ahrens }
6563b2aab18SMatthew Ahrens 
6573b2aab18SMatthew Ahrens void
dsl_dataset_long_rele(dsl_dataset_t * ds,void * tag)6583b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
6593b2aab18SMatthew Ahrens {
6603b2aab18SMatthew Ahrens 	(void) refcount_remove(&ds->ds_longholds, tag);
6613b2aab18SMatthew Ahrens }
6623b2aab18SMatthew Ahrens 
6633b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
6643b2aab18SMatthew Ahrens boolean_t
dsl_dataset_long_held(dsl_dataset_t * ds)6653b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
6663b2aab18SMatthew Ahrens {
6673b2aab18SMatthew Ahrens 	return (!refcount_is_zero(&ds->ds_longholds));
6683b2aab18SMatthew Ahrens }
6693b2aab18SMatthew Ahrens 
670fa9e4066Sahrens void
dsl_dataset_name(dsl_dataset_t * ds,char * name)671fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
672fa9e4066Sahrens {
673fa9e4066Sahrens 	if (ds == NULL) {
674fa9e4066Sahrens 		(void) strcpy(name, "mos");
675fa9e4066Sahrens 	} else {
676fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
6773b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
678fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
679675fc291SMatthew Ahrens 			VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
680675fc291SMatthew Ahrens 			    <, ZFS_MAX_DATASET_NAME_LEN);
681fa9e4066Sahrens 			/*
682fa9e4066Sahrens 			 * We use a "recursive" mutex so that we
683fa9e4066Sahrens 			 * can call dprintf_ds() with ds_lock held.
684fa9e4066Sahrens 			 */
685745cd3c5Smaybee 			if (!MUTEX_HELD(&ds->ds_lock)) {
686fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
687675fc291SMatthew Ahrens 				VERIFY3U(strlcat(name, ds->ds_snapname,
688675fc291SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN), <,
689675fc291SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN);
690fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
691fa9e4066Sahrens 			} else {
692675fc291SMatthew Ahrens 				VERIFY3U(strlcat(name, ds->ds_snapname,
693675fc291SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN), <,
694675fc291SMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN);
695fa9e4066Sahrens 			}
696fa9e4066Sahrens 		}
697fa9e4066Sahrens 	}
698fa9e4066Sahrens }
699fa9e4066Sahrens 
700675fc291SMatthew Ahrens int
dsl_dataset_namelen(dsl_dataset_t * ds)701675fc291SMatthew Ahrens dsl_dataset_namelen(dsl_dataset_t *ds)
702675fc291SMatthew Ahrens {
703675fc291SMatthew Ahrens 	VERIFY0(dsl_dataset_get_snapname(ds));
704675fc291SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
705675fc291SMatthew Ahrens 	int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname);
706675fc291SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
707675fc291SMatthew Ahrens 	return (len);
708675fc291SMatthew Ahrens }
709675fc291SMatthew Ahrens 
710088f3894Sahrens void
dsl_dataset_rele(dsl_dataset_t * ds,void * tag)7113b2aab18SMatthew Ahrens dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
712fa9e4066Sahrens {
713ea8dc4b6Seschrock 	dmu_buf_rele(ds->ds_dbuf, tag);
714fa9e4066Sahrens }
715fa9e4066Sahrens 
716fa9e4066Sahrens void
dsl_dataset_disown(dsl_dataset_t * ds,void * tag)717503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
718745cd3c5Smaybee {
71940afee89SJustin T. Gibbs 	ASSERT3P(ds->ds_owner, ==, tag);
72040afee89SJustin T. Gibbs 	ASSERT(ds->ds_dbuf != NULL);
721745cd3c5Smaybee 
7223cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
723745cd3c5Smaybee 	ds->ds_owner = NULL;
7243cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
7253b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
7263b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, tag);
7273cb34c60Sahrens }
7283cb34c60Sahrens 
7293cb34c60Sahrens boolean_t
dsl_dataset_tryown(dsl_dataset_t * ds,void * tag)7303b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
7313cb34c60Sahrens {
732745cd3c5Smaybee 	boolean_t gotit = FALSE;
733745cd3c5Smaybee 
7348429b235SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
7353cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
7363b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
737503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
7383b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
739745cd3c5Smaybee 		gotit = TRUE;
7403cb34c60Sahrens 	}
7413cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
742745cd3c5Smaybee 	return (gotit);
743745cd3c5Smaybee }
744745cd3c5Smaybee 
7458429b235SMatthew Ahrens boolean_t
dsl_dataset_has_owner(dsl_dataset_t * ds)7468429b235SMatthew Ahrens dsl_dataset_has_owner(dsl_dataset_t *ds)
7478429b235SMatthew Ahrens {
7488429b235SMatthew Ahrens 	boolean_t rv;
7498429b235SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
7508429b235SMatthew Ahrens 	rv = (ds->ds_owner != NULL);
7518429b235SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
7528429b235SMatthew Ahrens 	return (rv);
7538429b235SMatthew Ahrens }
7548429b235SMatthew Ahrens 
7550cc589a4SMatthew Ahrens static void
dsl_dataset_activate_feature(uint64_t dsobj,spa_feature_t f,dmu_tx_t * tx)7560cc589a4SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
7570cc589a4SMatthew Ahrens {
7580cc589a4SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7590cc589a4SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
7600cc589a4SMatthew Ahrens 	uint64_t zero = 0;
7610cc589a4SMatthew Ahrens 
7620cc589a4SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
7630cc589a4SMatthew Ahrens 
7640cc589a4SMatthew Ahrens 	spa_feature_incr(spa, f, tx);
7650cc589a4SMatthew Ahrens 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
7660cc589a4SMatthew Ahrens 
7670cc589a4SMatthew Ahrens 	VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
7680cc589a4SMatthew Ahrens 	    sizeof (zero), 1, &zero, tx));
7690cc589a4SMatthew Ahrens }
7700cc589a4SMatthew Ahrens 
7710cc589a4SMatthew Ahrens void
dsl_dataset_deactivate_feature(uint64_t dsobj,spa_feature_t f,dmu_tx_t * tx)7720cc589a4SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
7730cc589a4SMatthew Ahrens {
7740cc589a4SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
7750cc589a4SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
7760cc589a4SMatthew Ahrens 
7770cc589a4SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
7780cc589a4SMatthew Ahrens 
7790cc589a4SMatthew Ahrens 	VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
7800cc589a4SMatthew Ahrens 	spa_feature_decr(spa, f, tx);
7810cc589a4SMatthew Ahrens }
7820cc589a4SMatthew Ahrens 
7831d452cf5Sahrens uint64_t
dsl_dataset_create_sync_dd(dsl_dir_t * dd,dsl_dataset_t * origin,uint64_t flags,dmu_tx_t * tx)784088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
785ab04eb8eStimh     uint64_t flags, dmu_tx_t *tx)
786fa9e4066Sahrens {
7873cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
788fa9e4066Sahrens 	dmu_buf_t *dbuf;
789fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
7903cb34c60Sahrens 	uint64_t dsobj;
791fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
792fa9e4066Sahrens 
793088f3894Sahrens 	if (origin == NULL)
794088f3894Sahrens 		origin = dp->dp_origin_snap;
795088f3894Sahrens 
7963cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
7978d62b223SJustin T. Gibbs 	ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
798fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
7998d62b223SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
800fa9e4066Sahrens 
8011649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
8021649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
8033b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
804fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
805fa9e4066Sahrens 	dsphys = dbuf->db_data;
806745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
807fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
808ab04eb8eStimh 	dsphys->ds_flags = flags;
809fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
810fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
811fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
812fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
813ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
814ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
815fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
816088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
817a9799022Sck153898 
818cde58dbcSMatthew Ahrens 	if (origin == NULL) {
819cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
820cde58dbcSMatthew Ahrens 	} else {
8213b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
822cde58dbcSMatthew Ahrens 
8233cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
824fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
8258d62b223SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_creation_txg;
826ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
8278d62b223SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_referenced_bytes;
828fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
8298d62b223SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_compressed_bytes;
830fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
8318d62b223SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_uncompressed_bytes;
8328d62b223SJustin T. Gibbs 		dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
8331d925dedSMatthew Ahrens 
8341d925dedSMatthew Ahrens 		/*
8351d925dedSMatthew Ahrens 		 * Inherit flags that describe the dataset's contents
8361d925dedSMatthew Ahrens 		 * (INCONSISTENT) or properties (Case Insensitive).
8371d925dedSMatthew Ahrens 		 */
8388d62b223SJustin T. Gibbs 		dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
8391d925dedSMatthew Ahrens 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
840fa9e4066Sahrens 
8410cc589a4SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
8420cc589a4SMatthew Ahrens 			if (origin->ds_feature_inuse[f])
8430cc589a4SMatthew Ahrens 				dsl_dataset_activate_feature(dsobj, f, tx);
8440cc589a4SMatthew Ahrens 		}
845d1a98260SMatthew Ahrens 
8463cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
8478d62b223SJustin T. Gibbs 		dsl_dataset_phys(origin)->ds_num_children++;
848fa9e4066Sahrens 
8493b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
8508d62b223SJustin T. Gibbs 		    dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
8518d62b223SJustin T. Gibbs 		    FTAG, &ohds));
852cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
853cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
854cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
855cde58dbcSMatthew Ahrens 
856088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
8578d62b223SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
8588d62b223SJustin T. Gibbs 				dsl_dataset_phys(origin)->ds_next_clones_obj =
859088f3894Sahrens 				    zap_create(mos,
860088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
861088f3894Sahrens 			}
8623b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
8638d62b223SJustin T. Gibbs 			    dsl_dataset_phys(origin)->ds_next_clones_obj,
8648d62b223SJustin T. Gibbs 			    dsobj, tx));
865088f3894Sahrens 		}
866088f3894Sahrens 
867fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
8688d62b223SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
869cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
8708d62b223SJustin T. Gibbs 			if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
871cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
8728d62b223SJustin T. Gibbs 				dsl_dir_phys(origin->ds_dir)->dd_clones =
873cde58dbcSMatthew Ahrens 				    zap_create(mos,
874cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
875cde58dbcSMatthew Ahrens 			}
8763b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
8778d62b223SJustin T. Gibbs 			    dsl_dir_phys(origin->ds_dir)->dd_clones,
8788d62b223SJustin T. Gibbs 			    dsobj, tx));
879cde58dbcSMatthew Ahrens 		}
880fa9e4066Sahrens 	}
881ab04eb8eStimh 
882ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
883ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
884ab04eb8eStimh 
885ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
886fa9e4066Sahrens 
887fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
8888d62b223SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
8893cb34c60Sahrens 
8903cb34c60Sahrens 	return (dsobj);
8913cb34c60Sahrens }
8923cb34c60Sahrens 
8933b2aab18SMatthew Ahrens static void
dsl_dataset_zero_zil(dsl_dataset_t * ds,dmu_tx_t * tx)8943b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
8953b2aab18SMatthew Ahrens {
8963b2aab18SMatthew Ahrens 	objset_t *os;
8973b2aab18SMatthew Ahrens 
8983b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
8993b2aab18SMatthew Ahrens 	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
9003b2aab18SMatthew Ahrens 	dsl_dataset_dirty(ds, tx);
9013b2aab18SMatthew Ahrens }
9023b2aab18SMatthew Ahrens 
9033cb34c60Sahrens uint64_t
dsl_dataset_create_sync(dsl_dir_t * pdd,const char * lastname,dsl_dataset_t * origin,uint64_t flags,cred_t * cr,dmu_tx_t * tx)904ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
905ab04eb8eStimh     dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
9063cb34c60Sahrens {
9073cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
9083cb34c60Sahrens 	uint64_t dsobj, ddobj;
9093cb34c60Sahrens 	dsl_dir_t *dd;
9103cb34c60Sahrens 
9113b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
9123cb34c60Sahrens 	ASSERT(lastname[0] != '@');
9133cb34c60Sahrens 
914088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
9153b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
9163cb34c60Sahrens 
9173b2aab18SMatthew Ahrens 	dsobj = dsl_dataset_create_sync_dd(dd, origin,
9183b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
9193cb34c60Sahrens 
9203cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
9213cb34c60Sahrens 
922bc93cffdSJerry Jelinek 	/*
923bc93cffdSJerry Jelinek 	 * Since we're creating a new node we know it's a leaf, so we can
924bc93cffdSJerry Jelinek 	 * initialize the counts if the limit feature is active.
925bc93cffdSJerry Jelinek 	 */
926bc93cffdSJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
927bc93cffdSJerry Jelinek 		uint64_t cnt = 0;
928bc93cffdSJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
929bc93cffdSJerry Jelinek 
930bc93cffdSJerry Jelinek 		dsl_dir_zapify(dd, tx);
931bc93cffdSJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
932bc93cffdSJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
933bc93cffdSJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
934bc93cffdSJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
935bc93cffdSJerry Jelinek 	}
936bc93cffdSJerry Jelinek 
9373b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
938fa9e4066Sahrens 
939feaa74e4SMark Maybee 	/*
940feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
941feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
942feaa74e4SMark Maybee 	 */
9433b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
944feaa74e4SMark Maybee 		dsl_dataset_t *ds;
945feaa74e4SMark Maybee 
9463b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
9473b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
948feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
949feaa74e4SMark Maybee 	}
950feaa74e4SMark Maybee 
9511d452cf5Sahrens 	return (dsobj);
9521d452cf5Sahrens }
9531d452cf5Sahrens 
95419b94df9SMatthew Ahrens /*
9553b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
9563b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
9573b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
9583b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
9593b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
9603b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
96119b94df9SMatthew Ahrens  */
9623b2aab18SMatthew Ahrens void
dsl_dataset_recalc_head_uniq(dsl_dataset_t * ds)9633b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
9648ac09fceSRichard Lowe {
9653b2aab18SMatthew Ahrens 	uint64_t mrs_used;
9663b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
96719b94df9SMatthew Ahrens 
9680521c5ebSJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
96919b94df9SMatthew Ahrens 
9708d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
9718d62b223SJustin T. Gibbs 		mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
9723b2aab18SMatthew Ahrens 	else
9733b2aab18SMatthew Ahrens 		mrs_used = 0;
97419b94df9SMatthew Ahrens 
9753b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
9761d452cf5Sahrens 
9773b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
9788d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes =
9798d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
980842727c2SChris Kirby 
9813b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
9823b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
9838d62b223SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
9841d452cf5Sahrens }
9851d452cf5Sahrens 
9863b2aab18SMatthew Ahrens void
dsl_dataset_remove_from_next_clones(dsl_dataset_t * ds,uint64_t obj,dmu_tx_t * tx)9873b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
9883b2aab18SMatthew Ahrens     dmu_tx_t *tx)
989842727c2SChris Kirby {
9903b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
99114843421SMatthew Ahrens 	uint64_t count;
9923b2aab18SMatthew Ahrens 	int err;
99314843421SMatthew Ahrens 
9948d62b223SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
9958d62b223SJustin T. Gibbs 	err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
9968d62b223SJustin T. Gibbs 	    obj, tx);
997fa9e4066Sahrens 	/*
9983b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
9993b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
10003b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
10013b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
10023b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
10033b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
10043b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
10053b2aab18SMatthew Ahrens 	 * remove this one.
1006fa9e4066Sahrens 	 */
10073b2aab18SMatthew Ahrens 	if (err != ENOENT)
10083b2aab18SMatthew Ahrens 		VERIFY0(err);
10098d62b223SJustin T. Gibbs 	ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
10103b2aab18SMatthew Ahrens 	    &count));
10118d62b223SJustin T. Gibbs 	ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
1012842727c2SChris Kirby }
1013842727c2SChris Kirby 
1014fa9e4066Sahrens 
1015c717a561Smaybee blkptr_t *
dsl_dataset_get_blkptr(dsl_dataset_t * ds)1016c717a561Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1017fa9e4066Sahrens {
10188d62b223SJustin T. Gibbs 	return (&dsl_dataset_phys(ds)->ds_bp);
1019fa9e4066Sahrens }
1020fa9e4066Sahrens 
1021fa9e4066Sahrens void
dsl_dataset_set_blkptr(dsl_dataset_t * ds,blkptr_t * bp,dmu_tx_t * tx)1022fa9e4066Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
1023fa9e4066Sahrens {
1024fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1025fa9e4066Sahrens 	/* If it's the meta-objset, set dp_meta_rootbp */
1026fa9e4066Sahrens 	if (ds == NULL) {
1027fa9e4066Sahrens 		tx->tx_pool->dp_meta_rootbp = *bp;
1028fa9e4066Sahrens 	} else {
1029fa9e4066Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
10308d62b223SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_bp = *bp;
1031fa9e4066Sahrens 	}
1032fa9e4066Sahrens }
1033fa9e4066Sahrens 
1034fa9e4066Sahrens spa_t *
dsl_dataset_get_spa(dsl_dataset_t * ds)1035fa9e4066Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
1036fa9e4066Sahrens {
1037fa9e4066Sahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1038fa9e4066Sahrens }
1039fa9e4066Sahrens 
1040fa9e4066Sahrens void
dsl_dataset_dirty(dsl_dataset_t * ds,dmu_tx_t * tx)1041fa9e4066Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1042fa9e4066Sahrens {
1043fa9e4066Sahrens 	dsl_pool_t *dp;
1044fa9e4066Sahrens 
1045fa9e4066Sahrens 	if (ds == NULL) /* this is the meta-objset */
1046fa9e4066Sahrens 		return;
1047fa9e4066Sahrens 
1048503ad85cSMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1049a2eea2e1Sahrens 
10508d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
1051a2eea2e1Sahrens 		panic("dirtying snapshot!");
1052fa9e4066Sahrens 
1053fa9e4066Sahrens 	dp = ds->ds_dir->dd_pool;
1054fa9e4066Sahrens 
10553b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1056fa9e4066Sahrens 		/* up the hold count until we can be written out */
1057fa9e4066Sahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1058fa9e4066Sahrens 	}
1059fa9e4066Sahrens }
1060fa9e4066Sahrens 
10612e2c1355SMatthew Ahrens boolean_t
dsl_dataset_is_dirty(dsl_dataset_t * ds)10622e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
10632e2c1355SMatthew Ahrens {
10642e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
10652e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
10662e2c1355SMatthew Ahrens 		    ds, t))
10672e2c1355SMatthew Ahrens 			return (B_TRUE);
10682e2c1355SMatthew Ahrens 	}
10692e2c1355SMatthew Ahrens 	return (B_FALSE);
10702e2c1355SMatthew Ahrens }
10712e2c1355SMatthew Ahrens 
1072a9799022Sck153898 static int
dsl_dataset_snapshot_reserve_space(dsl_dataset_t * ds,dmu_tx_t * tx)1073a9799022Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1074a9799022Sck153898 {
1075a9799022Sck153898 	uint64_t asize;
1076a9799022Sck153898 
1077a9799022Sck153898 	if (!dmu_tx_is_syncing(tx))
1078a9799022Sck153898 		return (0);
1079a9799022Sck153898 
1080a9799022Sck153898 	/*
1081a9799022Sck153898 	 * If there's an fs-only reservation, any blocks that might become
1082a9799022Sck153898 	 * owned by the snapshot dataset must be accommodated by space
1083a9799022Sck153898 	 * outside of the reservation.
1084a9799022Sck153898 	 */
10853f9d6ad7SLin Ling 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
10868d62b223SJustin T. Gibbs 	asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
10876e0cbcaaSMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1088be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
1089a9799022Sck153898 
1090a9799022Sck153898 	/*
10914445fffbSMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
1092a9799022Sck153898 	 * snapshot checks in this sync group.
1093a9799022Sck153898 	 */
1094a9799022Sck153898 	if (asize > 0)
1095a9799022Sck153898 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1096a9799022Sck153898 
1097a9799022Sck153898 	return (0);
1098a9799022Sck153898 }
1099a9799022Sck153898 
11003b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg {
11013b2aab18SMatthew Ahrens 	nvlist_t *ddsa_snaps;
11023b2aab18SMatthew Ahrens 	nvlist_t *ddsa_props;
11033b2aab18SMatthew Ahrens 	nvlist_t *ddsa_errors;
1104bc93cffdSJerry Jelinek 	cred_t *ddsa_cr;
11053b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t;
11063b2aab18SMatthew Ahrens 
11071d452cf5Sahrens int
dsl_dataset_snapshot_check_impl(dsl_dataset_t * ds,const char * snapname,dmu_tx_t * tx,boolean_t recv,uint64_t cnt,cred_t * cr)11083b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1109bc93cffdSJerry Jelinek     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
11101d452cf5Sahrens {
11113b2aab18SMatthew Ahrens 	int error;
11121d452cf5Sahrens 	uint64_t value;
1113fa9e4066Sahrens 
11143b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
11153b2aab18SMatthew Ahrens 
11163b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
11173b2aab18SMatthew Ahrens 		return (0);
11183b2aab18SMatthew Ahrens 
1119fa9e4066Sahrens 	/*
11201d452cf5Sahrens 	 * We don't allow multiple snapshots of the same txg.  If there
11211d452cf5Sahrens 	 * is already one, try again.
1122fa9e4066Sahrens 	 */
11238d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1124be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
1125fa9e4066Sahrens 
11261d452cf5Sahrens 	/*
11274445fffbSMatthew Ahrens 	 * Check for conflicting snapshot name.
11281d452cf5Sahrens 	 */
11293b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
11303b2aab18SMatthew Ahrens 	if (error == 0)
1131be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
11323b2aab18SMatthew Ahrens 	if (error != ENOENT)
11333b2aab18SMatthew Ahrens 		return (error);
11341d452cf5Sahrens 
1135ca48f36fSKeith M Wesolowski 	/*
1136ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
1137ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
1138ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
1139ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
1140ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1141ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
1142ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
1143ca48f36fSKeith M Wesolowski 	 */
1144ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
1145ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
1146ca48f36fSKeith M Wesolowski 
1147bc93cffdSJerry Jelinek 	/*
1148bc93cffdSJerry Jelinek 	 * Skip the check for temporary snapshots or if we have already checked
1149bc93cffdSJerry Jelinek 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1150bc93cffdSJerry Jelinek 	 * check the count here when we're receiving a stream.
1151bc93cffdSJerry Jelinek 	 */
1152bc93cffdSJerry Jelinek 	if (cnt != 0 && cr != NULL) {
1153bc93cffdSJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1154bc93cffdSJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1155bc93cffdSJerry Jelinek 		if (error != 0)
1156bc93cffdSJerry Jelinek 			return (error);
1157bc93cffdSJerry Jelinek 	}
1158bc93cffdSJerry Jelinek 
11593b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
11603b2aab18SMatthew Ahrens 	if (error != 0)
11613b2aab18SMatthew Ahrens 		return (error);
1162b7661cccSmmusante 
1163fa9e4066Sahrens 	return (0);
1164fa9e4066Sahrens }
1165fa9e4066Sahrens 
11663b2aab18SMatthew Ahrens static int
dsl_dataset_snapshot_check(void * arg,dmu_tx_t * tx)11673b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
11683b2aab18SMatthew Ahrens {
11693b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
11703b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11713b2aab18SMatthew Ahrens 	nvpair_t *pair;
11723b2aab18SMatthew Ahrens 	int rv = 0;
11733b2aab18SMatthew Ahrens 
1174bc93cffdSJerry Jelinek 	/*
1175bc93cffdSJerry Jelinek 	 * Pre-compute how many total new snapshots will be created for each
1176bc93cffdSJerry Jelinek 	 * level in the tree and below. This is needed for validating the
1177bc93cffdSJerry Jelinek 	 * snapshot limit when either taking a recursive snapshot or when
1178bc93cffdSJerry Jelinek 	 * taking multiple snapshots.
1179bc93cffdSJerry Jelinek 	 *
1180bc93cffdSJerry Jelinek 	 * The problem is that the counts are not actually adjusted when
1181bc93cffdSJerry Jelinek 	 * we are checking, only when we finally sync. For a single snapshot,
1182bc93cffdSJerry Jelinek 	 * this is easy, the count will increase by 1 at each node up the tree,
1183bc93cffdSJerry Jelinek 	 * but its more complicated for the recursive/multiple snapshot case.
1184bc93cffdSJerry Jelinek 	 *
1185bc93cffdSJerry Jelinek 	 * The dsl_fs_ss_limit_check function does recursively check the count
1186bc93cffdSJerry Jelinek 	 * at each level up the tree but since it is validating each snapshot
1187bc93cffdSJerry Jelinek 	 * independently we need to be sure that we are validating the complete
1188bc93cffdSJerry Jelinek 	 * count for the entire set of snapshots. We do this by rolling up the
1189bc93cffdSJerry Jelinek 	 * counts for each component of the name into an nvlist and then
1190bc93cffdSJerry Jelinek 	 * checking each of those cases with the aggregated count.
1191bc93cffdSJerry Jelinek 	 *
1192bc93cffdSJerry Jelinek 	 * This approach properly handles not only the recursive snapshot
1193bc93cffdSJerry Jelinek 	 * case (where we get all of those on the ddsa_snaps list) but also
1194bc93cffdSJerry Jelinek 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1195bc93cffdSJerry Jelinek 	 * validate the limit on 'a' using a count of 2).
1196bc93cffdSJerry Jelinek 	 *
1197bc93cffdSJerry Jelinek 	 * We validate the snapshot names in the third loop and only report
1198bc93cffdSJerry Jelinek 	 * name errors once.
1199bc93cffdSJerry Jelinek 	 */
1200bc93cffdSJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1201bc93cffdSJerry Jelinek 		nvlist_t *cnt_track = NULL;
1202bc93cffdSJerry Jelinek 		cnt_track = fnvlist_alloc();
1203bc93cffdSJerry Jelinek 
1204bc93cffdSJerry Jelinek 		/* Rollup aggregated counts into the cnt_track list */
1205bc93cffdSJerry Jelinek 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1206bc93cffdSJerry Jelinek 		    pair != NULL;
1207bc93cffdSJerry Jelinek 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1208bc93cffdSJerry Jelinek 			char *pdelim;
1209bc93cffdSJerry Jelinek 			uint64_t val;
1210bc93cffdSJerry Jelinek 			char nm[MAXPATHLEN];
1211bc93cffdSJerry Jelinek 
1212bc93cffdSJerry Jelinek 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1213bc93cffdSJerry Jelinek 			pdelim = strchr(nm, '@');
1214bc93cffdSJerry Jelinek 			if (pdelim == NULL)
1215bc93cffdSJerry Jelinek 				continue;
1216bc93cffdSJerry Jelinek 			*pdelim = '\0';
1217bc93cffdSJerry Jelinek 
1218bc93cffdSJerry Jelinek 			do {
1219bc93cffdSJerry Jelinek 				if (nvlist_lookup_uint64(cnt_track, nm,
1220bc93cffdSJerry Jelinek 				    &val) == 0) {
1221bc93cffdSJerry Jelinek 					/* update existing entry */
1222bc93cffdSJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm,
1223bc93cffdSJerry Jelinek 					    val + 1);
1224bc93cffdSJerry Jelinek 				} else {
1225bc93cffdSJerry Jelinek 					/* add to list */
1226bc93cffdSJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm, 1);
1227bc93cffdSJerry Jelinek 				}
1228bc93cffdSJerry Jelinek 
1229bc93cffdSJerry Jelinek 				pdelim = strrchr(nm, '/');
1230bc93cffdSJerry Jelinek 				if (pdelim != NULL)
1231bc93cffdSJerry Jelinek 					*pdelim = '\0';
1232bc93cffdSJerry Jelinek 			} while (pdelim != NULL);
1233bc93cffdSJerry Jelinek 		}
1234bc93cffdSJerry Jelinek 
1235bc93cffdSJerry Jelinek 		/* Check aggregated counts at each level */
1236bc93cffdSJerry Jelinek 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1237bc93cffdSJerry Jelinek 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1238bc93cffdSJerry Jelinek 			int error = 0;
1239bc93cffdSJerry Jelinek 			char *name;
1240bc93cffdSJerry Jelinek 			uint64_t cnt = 0;
1241bc93cffdSJerry Jelinek 			dsl_dataset_t *ds;
1242bc93cffdSJerry Jelinek 
1243bc93cffdSJerry Jelinek 			name = nvpair_name(pair);
1244bc93cffdSJerry Jelinek 			cnt = fnvpair_value_uint64(pair);
1245bc93cffdSJerry Jelinek 			ASSERT(cnt > 0);
1246bc93cffdSJerry Jelinek 
1247bc93cffdSJerry Jelinek 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1248bc93cffdSJerry Jelinek 			if (error == 0) {
1249bc93cffdSJerry Jelinek 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1250bc93cffdSJerry Jelinek 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1251bc93cffdSJerry Jelinek 				    ddsa->ddsa_cr);
1252bc93cffdSJerry Jelinek 				dsl_dataset_rele(ds, FTAG);
1253bc93cffdSJerry Jelinek 			}
1254bc93cffdSJerry Jelinek 
1255bc93cffdSJerry Jelinek 			if (error != 0) {
1256bc93cffdSJerry Jelinek 				if (ddsa->ddsa_errors != NULL)
1257bc93cffdSJerry Jelinek 					fnvlist_add_int32(ddsa->ddsa_errors,
1258bc93cffdSJerry Jelinek 					    name, error);
1259bc93cffdSJerry Jelinek 				rv = error;
1260bc93cffdSJerry Jelinek 				/* only report one error for this check */
1261bc93cffdSJerry Jelinek 				break;
1262bc93cffdSJerry Jelinek 			}
1263bc93cffdSJerry Jelinek 		}
1264bc93cffdSJerry Jelinek 		nvlist_free(cnt_track);
1265bc93cffdSJerry Jelinek 	}
1266bc93cffdSJerry Jelinek 
12673b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
12683b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
12693b2aab18SMatthew Ahrens 		int error = 0;
12703b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
12713b2aab18SMatthew Ahrens 		char *name, *atp;
1272675fc291SMatthew Ahrens 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
12733b2aab18SMatthew Ahrens 
12743b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
1275675fc291SMatthew Ahrens 		if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1276be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
12773b2aab18SMatthew Ahrens 		if (error == 0) {
12783b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
12793b2aab18SMatthew Ahrens 			if (atp == NULL)
1280be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
12813b2aab18SMatthew Ahrens 			if (error == 0)
12823b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
12833b2aab18SMatthew Ahrens 		}
12843b2aab18SMatthew Ahrens 		if (error == 0)
12853b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
12863b2aab18SMatthew Ahrens 		if (error == 0) {
1287bc93cffdSJerry Jelinek 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
12883b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
1289bc93cffdSJerry Jelinek 			    atp + 1, tx, B_FALSE, 0, NULL);
12903b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
12913b2aab18SMatthew Ahrens 		}
12923b2aab18SMatthew Ahrens 
12933b2aab18SMatthew Ahrens 		if (error != 0) {
12943b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
12953b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
12963b2aab18SMatthew Ahrens 				    name, error);
12973b2aab18SMatthew Ahrens 			}
12983b2aab18SMatthew Ahrens 			rv = error;
12993b2aab18SMatthew Ahrens 		}
13003b2aab18SMatthew Ahrens 	}
1301bc93cffdSJerry Jelinek 
13023b2aab18SMatthew Ahrens 	return (rv);
13033b2aab18SMatthew Ahrens }
13043b2aab18SMatthew Ahrens 
13051d452cf5Sahrens void
dsl_dataset_snapshot_sync_impl(dsl_dataset_t * ds,const char * snapname,dmu_tx_t * tx)13063b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
13074445fffbSMatthew Ahrens     dmu_tx_t *tx)
1308fa9e4066Sahrens {
13093b2aab18SMatthew Ahrens 	static zil_header_t zero_zil;
13103b2aab18SMatthew Ahrens 
13111d452cf5Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1312fa9e4066Sahrens 	dmu_buf_t *dbuf;
1313fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
1314088f3894Sahrens 	uint64_t dsobj, crtxg;
1315fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
13163b2aab18SMatthew Ahrens 	objset_t *os;
1317fa9e4066Sahrens 
13183b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
13193b2aab18SMatthew Ahrens 
13203b2aab18SMatthew Ahrens 	/*
13213b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
13223b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
13233b2aab18SMatthew Ahrens 	 */
13243b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
13253b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
13263b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
13273b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
13283b2aab18SMatthew Ahrens 
1329bc93cffdSJerry Jelinek 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1330fa9e4066Sahrens 
1331088f3894Sahrens 	/*
1332088f3894Sahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1333088f3894Sahrens 	 */
1334088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1335088f3894Sahrens 		crtxg = 1;
1336088f3894Sahrens 	else
1337088f3894Sahrens 		crtxg = tx->tx_txg;
1338088f3894Sahrens 
13391649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
13401649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13413b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1342fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1343fa9e4066Sahrens 	dsphys = dbuf->db_data;
1344745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
13451d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1346fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1347fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1348fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
13498d62b223SJustin T. Gibbs 	dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
13508d62b223SJustin T. Gibbs 	dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1351fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1352fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1353fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1354088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
13558d62b223SJustin T. Gibbs 	dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
13568d62b223SJustin T. Gibbs 	dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
13578d62b223SJustin T. Gibbs 	dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
13588d62b223SJustin T. Gibbs 	dsphys->ds_uncompressed_bytes =
13598d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes;
13608d62b223SJustin T. Gibbs 	dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
13618d62b223SJustin T. Gibbs 	dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1362ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1363fa9e4066Sahrens 
13640cc589a4SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
13650cc589a4SMatthew Ahrens 		if (ds->ds_feature_inuse[f])
13660cc589a4SMatthew Ahrens 			dsl_dataset_activate_feature(dsobj, f, tx);
13670cc589a4SMatthew Ahrens 	}
1368d1a98260SMatthew Ahrens 
13698d62b223SJustin T. Gibbs 	ASSERT3U(ds->ds_prev != 0, ==,
13708d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
13711d452cf5Sahrens 	if (ds->ds_prev) {
1372088f3894Sahrens 		uint64_t next_clones_obj =
13738d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
13748d62b223SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1375fa9e4066Sahrens 		    ds->ds_object ||
13768d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
13778d62b223SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
13788d62b223SJustin T. Gibbs 		    ds->ds_object) {
13791d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
13808d62b223SJustin T. Gibbs 			ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
13818d62b223SJustin T. Gibbs 			    dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
13828d62b223SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1383088f3894Sahrens 		} else if (next_clones_obj != 0) {
13843b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1385c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
13863b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1387088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1388fa9e4066Sahrens 		}
1389fa9e4066Sahrens 	}
1390fa9e4066Sahrens 
1391a9799022Sck153898 	/*
1392a9799022Sck153898 	 * If we have a reference-reservation on this dataset, we will
1393a9799022Sck153898 	 * need to increase the amount of refreservation being charged
1394a9799022Sck153898 	 * since our unique space is going to zero.
1395a9799022Sck153898 	 */
1396a9799022Sck153898 	if (ds->ds_reserved) {
13973f9d6ad7SLin Ling 		int64_t delta;
13983f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
13998d62b223SJustin T. Gibbs 		delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
14008d62b223SJustin T. Gibbs 		    ds->ds_reserved);
140174e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
14023f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1403a9799022Sck153898 	}
1404a9799022Sck153898 
1405fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
14068d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj =
14078d62b223SJustin T. Gibbs 	    dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
14088d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1409cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
14108d62b223SJustin T. Gibbs 	dsl_deadlist_open(&ds->ds_deadlist, mos,
14118d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1412cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
14138d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1414cde58dbcSMatthew Ahrens 
14158d62b223SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
14168d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
14178d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
14188d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1419a9799022Sck153898 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
14208d62b223SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1421fa9e4066Sahrens 
14228d62b223SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
14233b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1424fa9e4066Sahrens 
1425fa9e4066Sahrens 	if (ds->ds_prev)
14263b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
14273b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
14288d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1429ecd6cf80Smarks 
14303f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1431088f3894Sahrens 
143271eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
143371eb0538SChris Kirby 
14344445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1435fa9e4066Sahrens }
1436fa9e4066Sahrens 
14373b2aab18SMatthew Ahrens static void
dsl_dataset_snapshot_sync(void * arg,dmu_tx_t * tx)14383b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
14393b2aab18SMatthew Ahrens {
14403b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
14413b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14423b2aab18SMatthew Ahrens 	nvpair_t *pair;
14433b2aab18SMatthew Ahrens 
14443b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
14453b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
14463b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
14473b2aab18SMatthew Ahrens 		char *name, *atp;
1448675fc291SMatthew Ahrens 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
14493b2aab18SMatthew Ahrens 
14503b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
14513b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
14523b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
14533b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
14543b2aab18SMatthew Ahrens 
14553b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
14563b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
14573b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
14583b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
14593b2aab18SMatthew Ahrens 		}
14603b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14613b2aab18SMatthew Ahrens 	}
14623b2aab18SMatthew Ahrens }
14633b2aab18SMatthew Ahrens 
14643b2aab18SMatthew Ahrens /*
14653b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
14663b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
14673b2aab18SMatthew Ahrens  */
14683b2aab18SMatthew Ahrens int
dsl_dataset_snapshot(nvlist_t * snaps,nvlist_t * props,nvlist_t * errors)14693b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
14703b2aab18SMatthew Ahrens {
14713b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
14723b2aab18SMatthew Ahrens 	nvpair_t *pair;
14733b2aab18SMatthew Ahrens 	boolean_t needsuspend;
14743b2aab18SMatthew Ahrens 	int error;
14753b2aab18SMatthew Ahrens 	spa_t *spa;
14763b2aab18SMatthew Ahrens 	char *firstname;
14773b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
14783b2aab18SMatthew Ahrens 
14793b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
14803b2aab18SMatthew Ahrens 	if (pair == NULL)
14813b2aab18SMatthew Ahrens 		return (0);
14823b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
14833b2aab18SMatthew Ahrens 
14843b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
14853b2aab18SMatthew Ahrens 	if (error != 0)
14863b2aab18SMatthew Ahrens 		return (error);
14873b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
14883b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
14893b2aab18SMatthew Ahrens 
14903b2aab18SMatthew Ahrens 	if (needsuspend) {
14913b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
14923b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
14933b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
1494675fc291SMatthew Ahrens 			char fsname[ZFS_MAX_DATASET_NAME_LEN];
14953b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
14963b2aab18SMatthew Ahrens 			char *atp;
14973b2aab18SMatthew Ahrens 			void *cookie;
14983b2aab18SMatthew Ahrens 
14993b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
15003b2aab18SMatthew Ahrens 			if (atp == NULL) {
1501be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
15023b2aab18SMatthew Ahrens 				break;
15033b2aab18SMatthew Ahrens 			}
15043b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
15053b2aab18SMatthew Ahrens 
15063b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
15073b2aab18SMatthew Ahrens 			if (error != 0)
15083b2aab18SMatthew Ahrens 				break;
15093b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
15103b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
15113b2aab18SMatthew Ahrens 		}
15123b2aab18SMatthew Ahrens 	}
15133b2aab18SMatthew Ahrens 
15143b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
15153b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
15163b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
1517bc93cffdSJerry Jelinek 	ddsa.ddsa_cr = CRED();
15183b2aab18SMatthew Ahrens 
15193b2aab18SMatthew Ahrens 	if (error == 0) {
15203b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
15213b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
152243d5e60eSMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
15233b2aab18SMatthew Ahrens 	}
15243b2aab18SMatthew Ahrens 
15253b2aab18SMatthew Ahrens 	if (suspended != NULL) {
15263b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
15273b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
15283b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
15293b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
15303b2aab18SMatthew Ahrens 		}
15313b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
15323b2aab18SMatthew Ahrens 	}
15333b2aab18SMatthew Ahrens 
15343b2aab18SMatthew Ahrens 	return (error);
15353b2aab18SMatthew Ahrens }
15363b2aab18SMatthew Ahrens 
15373b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
15383b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
15393b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
15403b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
15413b2aab18SMatthew Ahrens 	const char *ddsta_htag;
15423b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
15433b2aab18SMatthew Ahrens 
15443b2aab18SMatthew Ahrens static int
dsl_dataset_snapshot_tmp_check(void * arg,dmu_tx_t * tx)15453b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
15463b2aab18SMatthew Ahrens {
15473b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15483b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15493b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15503b2aab18SMatthew Ahrens 	int error;
15513b2aab18SMatthew Ahrens 
15523b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
15533b2aab18SMatthew Ahrens 	if (error != 0)
15543b2aab18SMatthew Ahrens 		return (error);
15553b2aab18SMatthew Ahrens 
1556bc93cffdSJerry Jelinek 	/* NULL cred means no limit check for tmp snapshot */
1557ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1558bc93cffdSJerry Jelinek 	    tx, B_FALSE, 0, NULL);
15593b2aab18SMatthew Ahrens 	if (error != 0) {
15603b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15613b2aab18SMatthew Ahrens 		return (error);
15623b2aab18SMatthew Ahrens 	}
15633b2aab18SMatthew Ahrens 
15643b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
15653b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1566be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
15673b2aab18SMatthew Ahrens 	}
15683b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
15693b2aab18SMatthew Ahrens 	    B_TRUE, tx);
15703b2aab18SMatthew Ahrens 	if (error != 0) {
15713b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15723b2aab18SMatthew Ahrens 		return (error);
15733b2aab18SMatthew Ahrens 	}
15743b2aab18SMatthew Ahrens 
15753b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15763b2aab18SMatthew Ahrens 	return (0);
15773b2aab18SMatthew Ahrens }
15783b2aab18SMatthew Ahrens 
15793b2aab18SMatthew Ahrens static void
dsl_dataset_snapshot_tmp_sync(void * arg,dmu_tx_t * tx)15803b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
15813b2aab18SMatthew Ahrens {
15823b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
15833b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15843b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15853b2aab18SMatthew Ahrens 
15863b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
15873b2aab18SMatthew Ahrens 
15883b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
15893b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
15903b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
15913b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
15923b2aab18SMatthew Ahrens 
15933b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15943b2aab18SMatthew Ahrens }
15953b2aab18SMatthew Ahrens 
15963b2aab18SMatthew Ahrens int
dsl_dataset_snapshot_tmp(const char * fsname,const char * snapname,minor_t cleanup_minor,const char * htag)15973b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
15983b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
15993b2aab18SMatthew Ahrens {
16003b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
16013b2aab18SMatthew Ahrens 	int error;
16023b2aab18SMatthew Ahrens 	spa_t *spa;
16033b2aab18SMatthew Ahrens 	boolean_t needsuspend;
16043b2aab18SMatthew Ahrens 	void *cookie;
16053b2aab18SMatthew Ahrens 
16063b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
16073b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
16083b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
16093b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
16103b2aab18SMatthew Ahrens 
16113b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
16123b2aab18SMatthew Ahrens 	if (error != 0)
16133b2aab18SMatthew Ahrens 		return (error);
16143b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
16153b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
16163b2aab18SMatthew Ahrens 
16173b2aab18SMatthew Ahrens 	if (needsuspend) {
16183b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
16193b2aab18SMatthew Ahrens 		if (error != 0)
16203b2aab18SMatthew Ahrens 			return (error);
16213b2aab18SMatthew Ahrens 	}
16223b2aab18SMatthew Ahrens 
16233b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
162443d5e60eSMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
16253b2aab18SMatthew Ahrens 
16263b2aab18SMatthew Ahrens 	if (needsuspend)
16273b2aab18SMatthew Ahrens 		zil_resume(cookie);
16283b2aab18SMatthew Ahrens 	return (error);
16293b2aab18SMatthew Ahrens }
16303b2aab18SMatthew Ahrens 
16313b2aab18SMatthew Ahrens 
1632fa9e4066Sahrens void
dsl_dataset_sync(dsl_dataset_t * ds,zio_t * zio,dmu_tx_t * tx)1633c717a561Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
1634fa9e4066Sahrens {
1635fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1636503ad85cSMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
16378d62b223SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
1638fa9e4066Sahrens 
163991ebeef5Sahrens 	/*
164091ebeef5Sahrens 	 * in case we had to change ds_fsid_guid when we opened it,
164191ebeef5Sahrens 	 * sync it out now.
164291ebeef5Sahrens 	 */
164391ebeef5Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
16448d62b223SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
164591ebeef5Sahrens 
16468429b235SMatthew Ahrens 	if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
16478429b235SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16488429b235SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
16498429b235SMatthew Ahrens 		    &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
16508429b235SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16518429b235SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
16528429b235SMatthew Ahrens 		    &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
16538429b235SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
16548429b235SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
16558429b235SMatthew Ahrens 		    &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
16568429b235SMatthew Ahrens 		ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
16578429b235SMatthew Ahrens 		ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
16588429b235SMatthew Ahrens 		ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
16598429b235SMatthew Ahrens 	}
16608429b235SMatthew Ahrens 
1661503ad85cSMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
1662d1a98260SMatthew Ahrens 
16630cc589a4SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
16640cc589a4SMatthew Ahrens 		if (ds->ds_feature_activation_needed[f]) {
16650cc589a4SMatthew Ahrens 			if (ds->ds_feature_inuse[f])
16660cc589a4SMatthew Ahrens 				continue;
16670cc589a4SMatthew Ahrens 			dsl_dataset_activate_feature(ds->ds_object, f, tx);
16680cc589a4SMatthew Ahrens 			ds->ds_feature_inuse[f] = B_TRUE;
16690cc589a4SMatthew Ahrens 		}
1670d1a98260SMatthew Ahrens 	}
1671fa9e4066Sahrens }
1672fa9e4066Sahrens 
167319b94df9SMatthew Ahrens static void
get_clones_stat(dsl_dataset_t * ds,nvlist_t * nv)167419b94df9SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
167519b94df9SMatthew Ahrens {
167619b94df9SMatthew Ahrens 	uint64_t count = 0;
167719b94df9SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
167819b94df9SMatthew Ahrens 	zap_cursor_t zc;
167919b94df9SMatthew Ahrens 	zap_attribute_t za;
16803b2aab18SMatthew Ahrens 	nvlist_t *propval = fnvlist_alloc();
16813b2aab18SMatthew Ahrens 	nvlist_t *val = fnvlist_alloc();
168219b94df9SMatthew Ahrens 
16833b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
168419b94df9SMatthew Ahrens 
168519b94df9SMatthew Ahrens 	/*
16863b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
168719b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
168819b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
168919b94df9SMatthew Ahrens 	 */
16908d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
16918d62b223SJustin T. Gibbs 		VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
169219b94df9SMatthew Ahrens 		    &count));
169319b94df9SMatthew Ahrens 	}
16948d62b223SJustin T. Gibbs 	if (count != dsl_dataset_phys(ds)->ds_num_children - 1)
169519b94df9SMatthew Ahrens 		goto fail;
16968d62b223SJustin T. Gibbs 	for (zap_cursor_init(&zc, mos,
16978d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_next_clones_obj);
169819b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
169919b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
170019b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
1701675fc291SMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
17023b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
17033b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
170419b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
17053b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
170619b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
170719b94df9SMatthew Ahrens 	}
170819b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
17093b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
17103b2aab18SMatthew Ahrens 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
171119b94df9SMatthew Ahrens fail:
171219b94df9SMatthew Ahrens 	nvlist_free(val);
171319b94df9SMatthew Ahrens 	nvlist_free(propval);
171419b94df9SMatthew Ahrens }
171519b94df9SMatthew Ahrens 
17168429b235SMatthew Ahrens static void
get_receive_resume_stats(dsl_dataset_t * ds,nvlist_t * nv)17178429b235SMatthew Ahrens get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv)
17188429b235SMatthew Ahrens {
17198429b235SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
17208429b235SMatthew Ahrens 
17218429b235SMatthew Ahrens 	if (dsl_dataset_has_resume_receive_state(ds)) {
17228429b235SMatthew Ahrens 		char *str;
17238429b235SMatthew Ahrens 		void *packed;
17248429b235SMatthew Ahrens 		uint8_t *compressed;
17258429b235SMatthew Ahrens 		uint64_t val;
17268429b235SMatthew Ahrens 		nvlist_t *token_nv = fnvlist_alloc();
17278429b235SMatthew Ahrens 		size_t packed_size, compressed_size;
17288429b235SMatthew Ahrens 
17298429b235SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17308429b235SMatthew Ahrens 		    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
17318429b235SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "fromguid", val);
17328429b235SMatthew Ahrens 		}
17338429b235SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17348429b235SMatthew Ahrens 		    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
17358429b235SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "object", val);
17368429b235SMatthew Ahrens 		}
17378429b235SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17388429b235SMatthew Ahrens 		    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
17398429b235SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "offset", val);
17408429b235SMatthew Ahrens 		}
17418429b235SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17428429b235SMatthew Ahrens 		    DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
17438429b235SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "bytes", val);
17448429b235SMatthew Ahrens 		}
17458429b235SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17468429b235SMatthew Ahrens 		    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
17478429b235SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "toguid", val);
17488429b235SMatthew Ahrens 		}
17498429b235SMatthew Ahrens 		char buf[256];
17508429b235SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
17518429b235SMatthew Ahrens 		    DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
17528429b235SMatthew Ahrens 			fnvlist_add_string(token_nv, "toname", buf);
17538429b235SMatthew Ahrens 		}
17548429b235SMatthew Ahrens 		if (zap_contains(dp->dp_meta_objset, ds->ds_object,
17558429b235SMatthew Ahrens 		    DS_FIELD_RESUME_EMBEDOK) == 0) {
17568429b235SMatthew Ahrens 			fnvlist_add_boolean(token_nv, "embedok");
17578429b235SMatthew Ahrens 		}
17588429b235SMatthew Ahrens 		packed = fnvlist_pack(token_nv, &packed_size);
17598429b235SMatthew Ahrens 		fnvlist_free(token_nv);
17608429b235SMatthew Ahrens 		compressed = kmem_alloc(packed_size, KM_SLEEP);
17618429b235SMatthew Ahrens 
17628429b235SMatthew Ahrens 		compressed_size = gzip_compress(packed, compressed,
17638429b235SMatthew Ahrens 		    packed_size, packed_size, 6);
17648429b235SMatthew Ahrens 
17658429b235SMatthew Ahrens 		zio_cksum_t cksum;
17668429b235SMatthew Ahrens 		fletcher_4_native(compressed, compressed_size, &cksum);
17678429b235SMatthew Ahrens 
17688429b235SMatthew Ahrens 		str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP);
17698429b235SMatthew Ahrens 		for (int i = 0; i < compressed_size; i++) {
17708429b235SMatthew Ahrens 			(void) sprintf(str + i * 2, "%02x", compressed[i]);
17718429b235SMatthew Ahrens 		}
17728429b235SMatthew Ahrens 		str[compressed_size * 2] = '\0';
17738429b235SMatthew Ahrens 		char *propval = kmem_asprintf("%u-%llx-%llx-%s",
17748429b235SMatthew Ahrens 		    ZFS_SEND_RESUME_TOKEN_VERSION,
17758429b235SMatthew Ahrens 		    (longlong_t)cksum.zc_word[0],
17768429b235SMatthew Ahrens 		    (longlong_t)packed_size, str);
17778429b235SMatthew Ahrens 		dsl_prop_nvlist_add_string(nv,
17788429b235SMatthew Ahrens 		    ZFS_PROP_RECEIVE_RESUME_TOKEN, propval);
17798429b235SMatthew Ahrens 		kmem_free(packed, packed_size);
17808429b235SMatthew Ahrens 		kmem_free(str, compressed_size * 2 + 1);
17818429b235SMatthew Ahrens 		kmem_free(compressed, packed_size);
17828429b235SMatthew Ahrens 		strfree(propval);
17838429b235SMatthew Ahrens 	}
17848429b235SMatthew Ahrens }
17858429b235SMatthew Ahrens 
1786fa9e4066Sahrens void
dsl_dataset_stats(dsl_dataset_t * ds,nvlist_t * nv)1787a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
1788fa9e4066Sahrens {
17893b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1790187d6ac0SMatt Ahrens 	uint64_t refd, avail, uobjs, aobjs, ratio;
1791a9799022Sck153898 
17923b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
17933b2aab18SMatthew Ahrens 
17948d62b223SJustin T. Gibbs 	ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
17958d62b223SJustin T. Gibbs 	    (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
17968d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_compressed_bytes);
17974445fffbSMatthew Ahrens 
17984445fffbSMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
179977372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
18008d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes);
18014445fffbSMatthew Ahrens 
18020521c5ebSJustin Gibbs 	if (ds->ds_is_snapshot) {
18034445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
18044445fffbSMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
18058d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_unique_bytes);
18064445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
18074445fffbSMatthew Ahrens 	} else {
1808ee69a30fSMatthew Ahrens 		if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
1809675fc291SMatthew Ahrens 			char buf[ZFS_MAX_DATASET_NAME_LEN];
1810ee69a30fSMatthew Ahrens 			dsl_dataset_name(ds->ds_prev, buf);
1811ee69a30fSMatthew Ahrens 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf);
1812ee69a30fSMatthew Ahrens 		}
1813ee69a30fSMatthew Ahrens 
1814a2eea2e1Sahrens 		dsl_dir_stats(ds->ds_dir, nv);
18154445fffbSMatthew Ahrens 	}
1816fa9e4066Sahrens 
1817a9799022Sck153898 	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
1818a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
1819a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
1820a9799022Sck153898 
1821a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
18228d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_time);
1823a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
18248d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_txg);
1825a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
1826a9799022Sck153898 	    ds->ds_quota);
1827a9799022Sck153898 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
1828a9799022Sck153898 	    ds->ds_reserved);
1829c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
18308d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_guid);
18311d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
18328d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
18331d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
18341d713200SEric Schrock 	    ds->ds_object);
183592241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
183692241e0bSTom Erickson 	    ds->ds_userrefs);
1837842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
1838842727c2SChris Kirby 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
1839fa9e4066Sahrens 
18408d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
184119b94df9SMatthew Ahrens 		uint64_t written, comp, uncomp;
184219b94df9SMatthew Ahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
184319b94df9SMatthew Ahrens 		dsl_dataset_t *prev;
184419b94df9SMatthew Ahrens 
184519b94df9SMatthew Ahrens 		int err = dsl_dataset_hold_obj(dp,
18468d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
184719b94df9SMatthew Ahrens 		if (err == 0) {
184819b94df9SMatthew Ahrens 			err = dsl_dataset_space_written(prev, ds, &written,
184919b94df9SMatthew Ahrens 			    &comp, &uncomp);
185019b94df9SMatthew Ahrens 			dsl_dataset_rele(prev, FTAG);
185119b94df9SMatthew Ahrens 			if (err == 0) {
185219b94df9SMatthew Ahrens 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
185319b94df9SMatthew Ahrens 				    written);
185419b94df9SMatthew Ahrens 			}
185519b94df9SMatthew Ahrens 		}
185619b94df9SMatthew Ahrens 	}
18578429b235SMatthew Ahrens 
18588429b235SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds)) {
18598429b235SMatthew Ahrens 		/*
18608429b235SMatthew Ahrens 		 * A failed "newfs" (e.g. full) resumable receive leaves
18618429b235SMatthew Ahrens 		 * the stats set on this dataset.  Check here for the prop.
18628429b235SMatthew Ahrens 		 */
18638429b235SMatthew Ahrens 		get_receive_resume_stats(ds, nv);
18648429b235SMatthew Ahrens 
18658429b235SMatthew Ahrens 		/*
18668429b235SMatthew Ahrens 		 * A failed incremental resumable receive leaves the
18678429b235SMatthew Ahrens 		 * stats set on our child named "%recv".  Check the child
18688429b235SMatthew Ahrens 		 * for the prop.
18698429b235SMatthew Ahrens 		 */
1870675fc291SMatthew Ahrens 		/* 6 extra bytes for /%recv */
1871675fc291SMatthew Ahrens 		char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
18728429b235SMatthew Ahrens 		dsl_dataset_t *recv_ds;
18738429b235SMatthew Ahrens 		dsl_dataset_name(ds, recvname);
1874675fc291SMatthew Ahrens 		if (strlcat(recvname, "/", sizeof (recvname)) <
1875675fc291SMatthew Ahrens 		    sizeof (recvname) &&
1876675fc291SMatthew Ahrens 		    strlcat(recvname, recv_clone_name, sizeof (recvname)) <
1877675fc291SMatthew Ahrens 		    sizeof (recvname) &&
1878675fc291SMatthew Ahrens 		    dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) {
18798429b235SMatthew Ahrens 			get_receive_resume_stats(recv_ds, nv);
18808429b235SMatthew Ahrens 			dsl_dataset_rele(recv_ds, FTAG);
18818429b235SMatthew Ahrens 		}
18828429b235SMatthew Ahrens 	}
1883fa9e4066Sahrens }
1884fa9e4066Sahrens 
1885a2eea2e1Sahrens void
dsl_dataset_fast_stat(dsl_dataset_t * ds,dmu_objset_stats_t * stat)1886a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
1887fa9e4066Sahrens {
18883b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
18893b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
18903b2aab18SMatthew Ahrens 
18918d62b223SJustin T. Gibbs 	stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg;
18928d62b223SJustin T. Gibbs 	stat->dds_inconsistent =
18938d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT;
18948d62b223SJustin T. Gibbs 	stat->dds_guid = dsl_dataset_phys(ds)->ds_guid;
18954445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
18960521c5ebSJustin Gibbs 	if (ds->ds_is_snapshot) {
1897a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
18988d62b223SJustin T. Gibbs 		stat->dds_num_clones =
18998d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_num_children - 1;
1900ebedde84SEric Taylor 	} else {
1901ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
1902ebedde84SEric Taylor 		stat->dds_num_clones = 0;
1903a2eea2e1Sahrens 
1904088f3894Sahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
1905a2eea2e1Sahrens 			dsl_dataset_t *ods;
1906a2eea2e1Sahrens 
19073b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
19088d62b223SJustin T. Gibbs 			    dsl_dir_phys(ds->ds_dir)->dd_origin_obj,
19098d62b223SJustin T. Gibbs 			    FTAG, &ods));
19103cb34c60Sahrens 			dsl_dataset_name(ods, stat->dds_origin);
19113b2aab18SMatthew Ahrens 			dsl_dataset_rele(ods, FTAG);
1912a2eea2e1Sahrens 		}
1913a2eea2e1Sahrens 	}
19144445fffbSMatthew Ahrens }
1915a2eea2e1Sahrens 
1916a2eea2e1Sahrens uint64_t
dsl_dataset_fsid_guid(dsl_dataset_t * ds)1917a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
1918a2eea2e1Sahrens {
191991ebeef5Sahrens 	return (ds->ds_fsid_guid);
1920a2eea2e1Sahrens }
1921a2eea2e1Sahrens 
1922a2eea2e1Sahrens void
dsl_dataset_space(dsl_dataset_t * ds,uint64_t * refdbytesp,uint64_t * availbytesp,uint64_t * usedobjsp,uint64_t * availobjsp)1923a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
1924a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
1925a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
1926a2eea2e1Sahrens {
19278d62b223SJustin T. Gibbs 	*refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
1928a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
19298d62b223SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
19308d62b223SJustin T. Gibbs 		*availbytesp +=
19318d62b223SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
1932a9799022Sck153898 	if (ds->ds_quota != 0) {
1933a9799022Sck153898 		/*
1934a9799022Sck153898 		 * Adjust available bytes according to refquota
1935a9799022Sck153898 		 */
1936a9799022Sck153898 		if (*refdbytesp < ds->ds_quota)
1937a9799022Sck153898 			*availbytesp = MIN(*availbytesp,
1938a9799022Sck153898 			    ds->ds_quota - *refdbytesp);
1939a9799022Sck153898 		else
1940a9799022Sck153898 			*availbytesp = 0;
1941a9799022Sck153898 	}
19428d62b223SJustin T. Gibbs 	*usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
1943a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
1944fa9e4066Sahrens }
1945fa9e4066Sahrens 
1946f18faf3fSek110237 boolean_t
dsl_dataset_modified_since_snap(dsl_dataset_t * ds,dsl_dataset_t * snap)194734f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
1948f18faf3fSek110237 {
1949f18faf3fSek110237 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1950f18faf3fSek110237 
19513b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
195234f2f8cfSMatthew Ahrens 	if (snap == NULL)
1953f18faf3fSek110237 		return (B_FALSE);
19548d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_bp.blk_birth >
19558d62b223SJustin T. Gibbs 	    dsl_dataset_phys(snap)->ds_creation_txg) {
195634f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
19576e0cbcaaSMatthew Ahrens 		/*
19586e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
19596e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
19606e0cbcaaSMatthew Ahrens 		 * modified.
19616e0cbcaaSMatthew Ahrens 		 */
19626e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
1963f18faf3fSek110237 			return (B_TRUE);
196434f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
19656e0cbcaaSMatthew Ahrens 			return (B_TRUE);
19666e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
196734f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
19686e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
19696e0cbcaaSMatthew Ahrens 	}
1970f18faf3fSek110237 	return (B_FALSE);
1971f18faf3fSek110237 }
1972f18faf3fSek110237 
19733b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
19743b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
19753b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
19763b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
19773b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
19783b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
19793b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
19803b2aab18SMatthew Ahrens 
19811d452cf5Sahrens /* ARGSUSED */
1982fa9e4066Sahrens static int
dsl_dataset_rename_snapshot_check_impl(dsl_pool_t * dp,dsl_dataset_t * hds,void * arg)19833b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
19843b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
1985fa9e4066Sahrens {
19863b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
19873b2aab18SMatthew Ahrens 	int error;
1988fa9e4066Sahrens 	uint64_t val;
1989fa9e4066Sahrens 
19903b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
19913b2aab18SMatthew Ahrens 	if (error != 0) {
19923b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
19933b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
19943b2aab18SMatthew Ahrens 	}
1995fa9e4066Sahrens 
19963b2aab18SMatthew Ahrens 	/* new name should not exist */
19973b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
19983b2aab18SMatthew Ahrens 	if (error == 0)
1999be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
20003b2aab18SMatthew Ahrens 	else if (error == ENOENT)
20013b2aab18SMatthew Ahrens 		error = 0;
2002cdf5b4caSmmusante 
2003cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
20043b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
2005675fc291SMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2006be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
2007cdf5b4caSmmusante 
20083b2aab18SMatthew Ahrens 	return (error);
20093b2aab18SMatthew Ahrens }
20103b2aab18SMatthew Ahrens 
20113b2aab18SMatthew Ahrens static int
dsl_dataset_rename_snapshot_check(void * arg,dmu_tx_t * tx)20123b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
20133b2aab18SMatthew Ahrens {
20143b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20153b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20163b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
20173b2aab18SMatthew Ahrens 	int error;
20183b2aab18SMatthew Ahrens 
20193b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
20203b2aab18SMatthew Ahrens 	if (error != 0)
20213b2aab18SMatthew Ahrens 		return (error);
20223b2aab18SMatthew Ahrens 
20233b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
20243b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
20253b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
20263b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
20273b2aab18SMatthew Ahrens 	} else {
20283b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
20293b2aab18SMatthew Ahrens 	}
20303b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
20313b2aab18SMatthew Ahrens 	return (error);
20323b2aab18SMatthew Ahrens }
20333b2aab18SMatthew Ahrens 
20343b2aab18SMatthew Ahrens static int
dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t * dp,dsl_dataset_t * hds,void * arg)20353b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
20363b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
20373b2aab18SMatthew Ahrens {
20383b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20393b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
20403b2aab18SMatthew Ahrens 	uint64_t val;
20413b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
20423b2aab18SMatthew Ahrens 	int error;
20433b2aab18SMatthew Ahrens 
20443b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
20453b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
20463b2aab18SMatthew Ahrens 	if (error == ENOENT) {
20473b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
20483b2aab18SMatthew Ahrens 		return (0);
20493b2aab18SMatthew Ahrens 	}
20503b2aab18SMatthew Ahrens 
20513b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
20523b2aab18SMatthew Ahrens 
20533b2aab18SMatthew Ahrens 	/* log before we change the name */
20543b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
20553b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
20563b2aab18SMatthew Ahrens 
2057bc93cffdSJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
2058bc93cffdSJerry Jelinek 	    B_FALSE));
20593b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
20603b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
20613b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
20628d62b223SJustin T. Gibbs 	VERIFY0(zap_add(dp->dp_meta_objset,
20638d62b223SJustin T. Gibbs 	    dsl_dataset_phys(hds)->ds_snapnames_zapobj,
20643b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
20653b2aab18SMatthew Ahrens 
20663b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
20673b2aab18SMatthew Ahrens 	return (0);
2068fa9e4066Sahrens }
2069fa9e4066Sahrens 
20701d452cf5Sahrens static void
dsl_dataset_rename_snapshot_sync(void * arg,dmu_tx_t * tx)20713b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
20721d452cf5Sahrens {
20733b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
20743b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20751d452cf5Sahrens 	dsl_dataset_t *hds;
2076fa9e4066Sahrens 
20773b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
20783b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
20793b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
20803b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
20813b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
20823b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
20833b2aab18SMatthew Ahrens 	} else {
20843b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
20853b2aab18SMatthew Ahrens 	}
2086745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
2087fa9e4066Sahrens }
2088fa9e4066Sahrens 
2089fa9e4066Sahrens int
dsl_dataset_rename_snapshot(const char * fsname,const char * oldsnapname,const char * newsnapname,boolean_t recursive)20903b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
20913b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
2092fa9e4066Sahrens {
20933b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
20943b2aab18SMatthew Ahrens 
20953b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
20963b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
20973b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
20983b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
20993b2aab18SMatthew Ahrens 
21003b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
210143d5e60eSMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
210243d5e60eSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
21033b2aab18SMatthew Ahrens }
21043b2aab18SMatthew Ahrens 
210591948b51SKeith M Wesolowski /*
210691948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
210791948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
210891948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
210991948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
211091948b51SKeith M Wesolowski  * when we release the long hold.
211191948b51SKeith M Wesolowski  */
211291948b51SKeith M Wesolowski static int
dsl_dataset_handoff_check(dsl_dataset_t * ds,void * owner,dmu_tx_t * tx)211391948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
211491948b51SKeith M Wesolowski {
211591948b51SKeith M Wesolowski 	boolean_t held;
211691948b51SKeith M Wesolowski 
211791948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
211891948b51SKeith M Wesolowski 		return (0);
211991948b51SKeith M Wesolowski 
212091948b51SKeith M Wesolowski 	if (owner != NULL) {
212191948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
212291948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
212391948b51SKeith M Wesolowski 	}
212491948b51SKeith M Wesolowski 
212591948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
212691948b51SKeith M Wesolowski 
212791948b51SKeith M Wesolowski 	if (owner != NULL)
212891948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
212991948b51SKeith M Wesolowski 
213091948b51SKeith M Wesolowski 	if (held)
213191948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
213291948b51SKeith M Wesolowski 
213391948b51SKeith M Wesolowski 	return (0);
213491948b51SKeith M Wesolowski }
213591948b51SKeith M Wesolowski 
213691948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg {
213791948b51SKeith M Wesolowski 	const char *ddra_fsname;
213891948b51SKeith M Wesolowski 	void *ddra_owner;
2139a7027df1SMatthew Ahrens 	nvlist_t *ddra_result;
214091948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t;
214191948b51SKeith M Wesolowski 
21423b2aab18SMatthew Ahrens static int
dsl_dataset_rollback_check(void * arg,dmu_tx_t * tx)21433b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
21443b2aab18SMatthew Ahrens {
214591948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
21463b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
21471d452cf5Sahrens 	dsl_dataset_t *ds;
21483b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
21493b2aab18SMatthew Ahrens 	int error;
2150fa9e4066Sahrens 
215191948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
21523b2aab18SMatthew Ahrens 	if (error != 0)
21533b2aab18SMatthew Ahrens 		return (error);
2154370c1af0SSanjeev Bagewadi 
21553b2aab18SMatthew Ahrens 	/* must not be a snapshot */
21560521c5ebSJustin Gibbs 	if (ds->ds_is_snapshot) {
21573b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2158be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21593b2aab18SMatthew Ahrens 	}
21601d452cf5Sahrens 
21613b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
21628d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
21633b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2164be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21653b2aab18SMatthew Ahrens 	}
21661d452cf5Sahrens 
2167e69d9941SMatthew Ahrens 	/* must not have any bookmarks after the most recent snapshot */
2168e69d9941SMatthew Ahrens 	nvlist_t *proprequest = fnvlist_alloc();
2169e69d9941SMatthew Ahrens 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
2170e69d9941SMatthew Ahrens 	nvlist_t *bookmarks = fnvlist_alloc();
2171e69d9941SMatthew Ahrens 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
2172e69d9941SMatthew Ahrens 	fnvlist_free(proprequest);
2173e69d9941SMatthew Ahrens 	if (error != 0)
2174e69d9941SMatthew Ahrens 		return (error);
2175e69d9941SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
2176e69d9941SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
2177e69d9941SMatthew Ahrens 		nvlist_t *valuenv =
2178e69d9941SMatthew Ahrens 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
2179e69d9941SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
2180e69d9941SMatthew Ahrens 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
21818d62b223SJustin T. Gibbs 		if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
2182e69d9941SMatthew Ahrens 			fnvlist_free(bookmarks);
2183e69d9941SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
2184e69d9941SMatthew Ahrens 			return (SET_ERROR(EEXIST));
2185e69d9941SMatthew Ahrens 		}
2186e69d9941SMatthew Ahrens 	}
2187e69d9941SMatthew Ahrens 	fnvlist_free(bookmarks);
2188e69d9941SMatthew Ahrens 
218991948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
219091948b51SKeith M Wesolowski 	if (error != 0) {
21913b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
219291948b51SKeith M Wesolowski 		return (error);
21933b2aab18SMatthew Ahrens 	}
21941d452cf5Sahrens 
21953b2aab18SMatthew Ahrens 	/*
21963b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
21973b2aab18SMatthew Ahrens 	 * the refquota.
21983b2aab18SMatthew Ahrens 	 */
21993b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
22008d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
22013b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2202be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
22033b2aab18SMatthew Ahrens 	}
22043b2aab18SMatthew Ahrens 
22053b2aab18SMatthew Ahrens 	/*
22063b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
22073b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
22083b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
22093b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
22103b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
22113b2aab18SMatthew Ahrens 	 */
22123b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
22138d62b223SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
22143b2aab18SMatthew Ahrens 
22153b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
22163b2aab18SMatthew Ahrens 	    unused_refres_delta >
22173b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
22183b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2219be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
22203b2aab18SMatthew Ahrens 	}
22213b2aab18SMatthew Ahrens 
22223b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
22233b2aab18SMatthew Ahrens 	return (0);
22243b2aab18SMatthew Ahrens }
22253b2aab18SMatthew Ahrens 
22263b2aab18SMatthew Ahrens static void
dsl_dataset_rollback_sync(void * arg,dmu_tx_t * tx)22273b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
22283b2aab18SMatthew Ahrens {
222991948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
22303b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
22313b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
22323b2aab18SMatthew Ahrens 	uint64_t cloneobj;
2233675fc291SMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
22343b2aab18SMatthew Ahrens 
223591948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
22363b2aab18SMatthew Ahrens 
2237a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
2238a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2239a7027df1SMatthew Ahrens 
22403b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
22413b2aab18SMatthew Ahrens 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
22423b2aab18SMatthew Ahrens 
22433b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
22443b2aab18SMatthew Ahrens 
22453b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
22463b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
22473b2aab18SMatthew Ahrens 
22483b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
22493b2aab18SMatthew Ahrens 
22503b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
2251745cd3c5Smaybee 	dsl_dataset_rele(ds, FTAG);
2252cdf5b4caSmmusante }
22531d452cf5Sahrens 
225491948b51SKeith M Wesolowski /*
2255a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
2256a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
2257a7027df1SMatthew Ahrens  * in the result nvlist.
225891948b51SKeith M Wesolowski  *
2259a7027df1SMatthew Ahrens  * If owner != NULL:
226091948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
226191948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
226291948b51SKeith M Wesolowski  *   succeed or not.
226391948b51SKeith M Wesolowski  *
226491948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
226591948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
226691948b51SKeith M Wesolowski  */
22673b2aab18SMatthew Ahrens int
dsl_dataset_rollback(const char * fsname,void * owner,nvlist_t * result)2268a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
22693b2aab18SMatthew Ahrens {
227091948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
227191948b51SKeith M Wesolowski 
227291948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
227391948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
2274a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
227591948b51SKeith M Wesolowski 
22763b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
227743d5e60eSMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra,
227843d5e60eSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
2279fa9e4066Sahrens }
228099653d4eSeschrock 
2281088f3894Sahrens struct promotenode {
2282745cd3c5Smaybee 	list_node_t link;
2283745cd3c5Smaybee 	dsl_dataset_t *ds;
2284745cd3c5Smaybee };
2285745cd3c5Smaybee 
22863b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg {
22873b2aab18SMatthew Ahrens 	const char *ddpa_clonename;
22883b2aab18SMatthew Ahrens 	dsl_dataset_t *ddpa_clone;
228974e7dc98SMatthew Ahrens 	list_t shared_snaps, origin_snaps, clone_snaps;
22903b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_origin; /* origin of the origin */
229174e7dc98SMatthew Ahrens 	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
2292681d9761SEric Taylor 	char *err_ds;
2293bc93cffdSJerry Jelinek 	cred_t *cr;
22943b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t;
22951d452cf5Sahrens 
229674e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
22973b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
22983b2aab18SMatthew Ahrens     void *tag);
22993b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
230074e7dc98SMatthew Ahrens 
230199653d4eSeschrock static int
dsl_dataset_promote_check(void * arg,dmu_tx_t * tx)23023b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
230399653d4eSeschrock {
23043b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
23053b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
23063b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
23073b2aab18SMatthew Ahrens 	struct promotenode *snap;
23083b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
2309745cd3c5Smaybee 	int err;
2310cde58dbcSMatthew Ahrens 	uint64_t unused;
2311bc93cffdSJerry Jelinek 	uint64_t ss_mv_cnt;
2312aab44f33SAndriy Gapon 	size_t max_snap_len;
23131d452cf5Sahrens 
23143b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
23153b2aab18SMatthew Ahrens 	if (err != 0)
23163b2aab18SMatthew Ahrens 		return (err);
231799653d4eSeschrock 
23183b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
2319aab44f33SAndriy Gapon 	max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
23201d452cf5Sahrens 
23218d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
23223b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
2323be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
23243b2aab18SMatthew Ahrens 	}
23253b2aab18SMatthew Ahrens 
23263b2aab18SMatthew Ahrens 	/*
23273b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
23283b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
23293b2aab18SMatthew Ahrens 	 */
23303b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
23313b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
23323b2aab18SMatthew Ahrens 		return (0);
23333b2aab18SMatthew Ahrens 	}
23343b2aab18SMatthew Ahrens 
23353b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
23363b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
233799653d4eSeschrock 
233874e7dc98SMatthew Ahrens 	/* compute origin's new unique space */
23393b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
23408d62b223SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
23418d62b223SJustin T. Gibbs 	    origin_ds->ds_object);
2342cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
23438d62b223SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
23443b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
2345745cd3c5Smaybee 
2346745cd3c5Smaybee 	/*
2347745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2348745cd3c5Smaybee 	 *
234974e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
23503b2aab18SMatthew Ahrens 	 * to used by each snapshot:
235174e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
235274e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
235374e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2354745cd3c5Smaybee 	 * So a sequence would look like:
235574e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2356745cd3c5Smaybee 	 * Which simplifies to:
235774e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2358745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
235974e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2360745cd3c5Smaybee 	 */
2361bc93cffdSJerry Jelinek 	ss_mv_cnt = 0;
23628d62b223SJustin T. Gibbs 	ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
23638d62b223SJustin T. Gibbs 	ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
23648d62b223SJustin T. Gibbs 	ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
23653b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
23663b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
236799653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2368745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
236999653d4eSeschrock 
2370bc93cffdSJerry Jelinek 		ss_mv_cnt++;
2371bc93cffdSJerry Jelinek 
23723b2aab18SMatthew Ahrens 		/*
23733b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
23743b2aab18SMatthew Ahrens 		 * the objset.
23753b2aab18SMatthew Ahrens 		 */
23763b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
2377be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
23783b2aab18SMatthew Ahrens 			goto out;
23793b2aab18SMatthew Ahrens 		}
23803b2aab18SMatthew Ahrens 
238199653d4eSeschrock 		/* Check that the snapshot name does not conflict */
23823b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
2383aab44f33SAndriy Gapon 		if (strlen(ds->ds_snapname) >= max_snap_len) {
2384aab44f33SAndriy Gapon 			err = SET_ERROR(ENAMETOOLONG);
2385aab44f33SAndriy Gapon 			goto out;
2386aab44f33SAndriy Gapon 		}
2387745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2388681d9761SEric Taylor 		if (err == 0) {
23893b2aab18SMatthew Ahrens 			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
2390be6fd75aSMatthew Ahrens 			err = SET_ERROR(EEXIST);
2391681d9761SEric Taylor 			goto out;
2392681d9761SEric Taylor 		}
2393745cd3c5Smaybee 		if (err != ENOENT)
2394681d9761SEric Taylor 			goto out;
239599653d4eSeschrock 
2396745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
23978d62b223SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
239874e7dc98SMatthew Ahrens 			continue;
239974e7dc98SMatthew Ahrens 
2400cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2401cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
24023b2aab18SMatthew Ahrens 		ddpa->used += dlused;
24033b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
24043b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
2405745cd3c5Smaybee 	}
240699653d4eSeschrock 
240799653d4eSeschrock 	/*
2408745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
2409745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
241099653d4eSeschrock 	 */
24113b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
24128d62b223SJustin T. Gibbs 		ddpa->used -=
24138d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
24148d62b223SJustin T. Gibbs 		ddpa->comp -=
24158d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
24163b2aab18SMatthew Ahrens 		ddpa->uncomp -=
24178d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->
24188d62b223SJustin T. Gibbs 		    ds_uncompressed_bytes;
241999653d4eSeschrock 	}
2420745cd3c5Smaybee 
2421bc93cffdSJerry Jelinek 	/* Check that there is enough space and limit headroom here */
242274e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
2423bc93cffdSJerry Jelinek 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
24243b2aab18SMatthew Ahrens 	if (err != 0)
24253b2aab18SMatthew Ahrens 		goto out;
242674e7dc98SMatthew Ahrens 
242774e7dc98SMatthew Ahrens 	/*
242874e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
242974e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
243074e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
243174e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
243274e7dc98SMatthew Ahrens 	 */
24338d62b223SJustin T. Gibbs 	if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
243474e7dc98SMatthew Ahrens 		uint64_t space;
243574e7dc98SMatthew Ahrens 
243674e7dc98SMatthew Ahrens 		/*
243774e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
24383f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
2439cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
244074e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
244174e7dc98SMatthew Ahrens 		 * iterate over all bps.
244274e7dc98SMatthew Ahrens 		 */
24433b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
24443b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
24453b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
24463b2aab18SMatthew Ahrens 		if (err != 0)
24473b2aab18SMatthew Ahrens 			goto out;
244874e7dc98SMatthew Ahrens 
24493b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
24503f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
24513b2aab18SMatthew Ahrens 		if (err != 0)
24523b2aab18SMatthew Ahrens 			goto out;
24533b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
245474e7dc98SMatthew Ahrens 	}
24558d62b223SJustin T. Gibbs 	if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
24568d62b223SJustin T. Gibbs 	    DD_FLAG_USED_BREAKDOWN) {
24573b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
24588d62b223SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_creation_txg,
24598d62b223SJustin T. Gibbs 		    &ddpa->originusedsnap);
24603b2aab18SMatthew Ahrens 		if (err != 0)
24613b2aab18SMatthew Ahrens 			goto out;
2462745cd3c5Smaybee 	}
246399653d4eSeschrock 
2464681d9761SEric Taylor out:
24653b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
2466681d9761SEric Taylor 	return (err);
246799653d4eSeschrock }
246899653d4eSeschrock 
24691d452cf5Sahrens static void
dsl_dataset_promote_sync(void * arg,dmu_tx_t * tx)24703b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
24711d452cf5Sahrens {
24723b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
24733b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
24743b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
24753b2aab18SMatthew Ahrens 	struct promotenode *snap;
24763b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
247774e7dc98SMatthew Ahrens 	dsl_dataset_t *origin_head;
24783b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
24793cb34c60Sahrens 	dsl_dir_t *odd = NULL;
2480088f3894Sahrens 	uint64_t oldnext_obj;
248174e7dc98SMatthew Ahrens 	int64_t delta;
24821d452cf5Sahrens 
24833b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
24843b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
24851d452cf5Sahrens 
24868d62b223SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
24873b2aab18SMatthew Ahrens 
24883b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
24893b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
24903b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
24913b2aab18SMatthew Ahrens 
24923b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
249374e7dc98SMatthew Ahrens 	origin_head = snap->ds;
249474e7dc98SMatthew Ahrens 
24950b69c2f0Sahrens 	/*
24963cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
24970b69c2f0Sahrens 	 * changing.
24980b69c2f0Sahrens 	 */
24993b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
25003cb34c60Sahrens 	    NULL, FTAG, &odd));
25011d452cf5Sahrens 
2502745cd3c5Smaybee 	/* change origin's next snap */
2503745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
25048d62b223SJustin T. Gibbs 	oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
25053b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
25068d62b223SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
25078d62b223SJustin T. Gibbs 	    origin_ds->ds_object);
25088d62b223SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
2509745cd3c5Smaybee 
2510088f3894Sahrens 	/* change the origin's next clone */
25118d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
25123b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
25133b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
25143b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
25158d62b223SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
2516088f3894Sahrens 		    oldnext_obj, tx));
2517088f3894Sahrens 	}
2518088f3894Sahrens 
2519745cd3c5Smaybee 	/* change origin */
2520745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
25218d62b223SJustin T. Gibbs 	ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
25228d62b223SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
25233f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
2524745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
25258d62b223SJustin T. Gibbs 	dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
25263f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
25278d62b223SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
2528745cd3c5Smaybee 
2529cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
2530cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
25313b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
25328d62b223SJustin T. Gibbs 		    dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
25333b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
25348d62b223SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2535cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
2536cde58dbcSMatthew Ahrens 
25373b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
25388d62b223SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
2539cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
25408d62b223SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_clones == 0) {
25418d62b223SJustin T. Gibbs 			dsl_dir_phys(dd)->dd_clones =
25428d62b223SJustin T. Gibbs 			    zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
25438d62b223SJustin T. Gibbs 			    DMU_OT_NONE, 0, tx);
2544cde58dbcSMatthew Ahrens 		}
25453b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
25468d62b223SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
2547cde58dbcSMatthew Ahrens 	}
2548cde58dbcSMatthew Ahrens 
25491d452cf5Sahrens 	/* move snapshots to this dir */
25503b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
25513b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
2552745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
25531d452cf5Sahrens 
25543b2aab18SMatthew Ahrens 		/*
25553b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
25563b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
25573b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
25583b2aab18SMatthew Ahrens 		 */
2559503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
2560503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
2561503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
25623baa08fcSek110237 		}
25633b2aab18SMatthew Ahrens 
25641d452cf5Sahrens 		/* move snap name entry */
25653b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
25663b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
2567bc93cffdSJerry Jelinek 		    ds->ds_snapname, tx, B_TRUE));
25683b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
25698d62b223SJustin T. Gibbs 		    dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
25701d452cf5Sahrens 		    8, 1, &ds->ds_object, tx));
2571bc93cffdSJerry Jelinek 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
2572bc93cffdSJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2573cde58dbcSMatthew Ahrens 
25741d452cf5Sahrens 		/* change containing dsl_dir */
25751d452cf5Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
25768d62b223SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
25778d62b223SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
25783cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
25793b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
25803b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
25811d452cf5Sahrens 		    NULL, ds, &ds->ds_dir));
25821d452cf5Sahrens 
2583cde58dbcSMatthew Ahrens 		/* move any clone references */
25848d62b223SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
2585cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
2586cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
2587cde58dbcSMatthew Ahrens 			zap_attribute_t za;
2588cde58dbcSMatthew Ahrens 
2589cde58dbcSMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
25908d62b223SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
2591cde58dbcSMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
2592cde58dbcSMatthew Ahrens 			    zap_cursor_advance(&zc)) {
2593cde58dbcSMatthew Ahrens 				dsl_dataset_t *cnds;
2594cde58dbcSMatthew Ahrens 				uint64_t o;
2595cde58dbcSMatthew Ahrens 
2596cde58dbcSMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
2597cde58dbcSMatthew Ahrens 					/*
2598cde58dbcSMatthew Ahrens 					 * We've already moved the
2599cde58dbcSMatthew Ahrens 					 * origin's reference.
2600cde58dbcSMatthew Ahrens 					 */
2601cde58dbcSMatthew Ahrens 					continue;
2602cde58dbcSMatthew Ahrens 				}
2603cde58dbcSMatthew Ahrens 
26043b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
2605cde58dbcSMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
26068d62b223SJustin T. Gibbs 				o = dsl_dir_phys(cnds->ds_dir)->
26078d62b223SJustin T. Gibbs 				    dd_head_dataset_obj;
2608cde58dbcSMatthew Ahrens 
26093b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
26108d62b223SJustin T. Gibbs 				    dsl_dir_phys(odd)->dd_clones, o, tx));
26113b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
26128d62b223SJustin T. Gibbs 				    dsl_dir_phys(dd)->dd_clones, o, tx));
2613cde58dbcSMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
2614cde58dbcSMatthew Ahrens 			}
2615cde58dbcSMatthew Ahrens 			zap_cursor_fini(&zc);
2616cde58dbcSMatthew Ahrens 		}
2617cde58dbcSMatthew Ahrens 
26183b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
261974e7dc98SMatthew Ahrens 	}
26201d452cf5Sahrens 
262174e7dc98SMatthew Ahrens 	/*
262274e7dc98SMatthew Ahrens 	 * Change space accounting.
262374e7dc98SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
262474e7dc98SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
262574e7dc98SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
262674e7dc98SMatthew Ahrens 	 */
262774e7dc98SMatthew Ahrens 
26283b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
26298d62b223SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
263074e7dc98SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
26313b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
263274e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
263374e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
26343b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
263574e7dc98SMatthew Ahrens 
26363b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
26378d62b223SJustin T. Gibbs 	    dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
263874e7dc98SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
26393b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
264074e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
264174e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
26423b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
264374e7dc98SMatthew Ahrens 
26448d62b223SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
26451d452cf5Sahrens 
2646ecd6cf80Smarks 	/* log history record */
26474445fffbSMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
2648ecd6cf80Smarks 
26493b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
26503b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
26511d452cf5Sahrens }
26521d452cf5Sahrens 
265374e7dc98SMatthew Ahrens /*
265474e7dc98SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
265574e7dc98SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
265674e7dc98SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
265774e7dc98SMatthew Ahrens  * snapshots back to this dataset's origin.
265874e7dc98SMatthew Ahrens  */
265974e7dc98SMatthew Ahrens static int
snaplist_make(dsl_pool_t * dp,uint64_t first_obj,uint64_t last_obj,list_t * l,void * tag)26603b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
26613b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
266274e7dc98SMatthew Ahrens {
266374e7dc98SMatthew Ahrens 	uint64_t obj = last_obj;
266474e7dc98SMatthew Ahrens 
266574e7dc98SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
266674e7dc98SMatthew Ahrens 	    offsetof(struct promotenode, link));
266774e7dc98SMatthew Ahrens 
266874e7dc98SMatthew Ahrens 	while (obj != first_obj) {
266974e7dc98SMatthew Ahrens 		dsl_dataset_t *ds;
267074e7dc98SMatthew Ahrens 		struct promotenode *snap;
267174e7dc98SMatthew Ahrens 		int err;
267274e7dc98SMatthew Ahrens 
26733b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
26743b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
26753b2aab18SMatthew Ahrens 		if (err != 0)
267674e7dc98SMatthew Ahrens 			return (err);
267774e7dc98SMatthew Ahrens 
267874e7dc98SMatthew Ahrens 		if (first_obj == 0)
26798d62b223SJustin T. Gibbs 			first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
268074e7dc98SMatthew Ahrens 
26813b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
268274e7dc98SMatthew Ahrens 		snap->ds = ds;
268374e7dc98SMatthew Ahrens 		list_insert_tail(l, snap);
26848d62b223SJustin T. Gibbs 		obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
268574e7dc98SMatthew Ahrens 	}
268674e7dc98SMatthew Ahrens 
268774e7dc98SMatthew Ahrens 	return (0);
268874e7dc98SMatthew Ahrens }
268974e7dc98SMatthew Ahrens 
269074e7dc98SMatthew Ahrens static int
snaplist_space(list_t * l,uint64_t mintxg,uint64_t * spacep)269174e7dc98SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
269274e7dc98SMatthew Ahrens {
269374e7dc98SMatthew Ahrens 	struct promotenode *snap;
269474e7dc98SMatthew Ahrens 
269574e7dc98SMatthew Ahrens 	*spacep = 0;
269674e7dc98SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
2697cde58dbcSMatthew Ahrens 		uint64_t used, comp, uncomp;
2698cde58dbcSMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2699cde58dbcSMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
270074e7dc98SMatthew Ahrens 		*spacep += used;
270174e7dc98SMatthew Ahrens 	}
270274e7dc98SMatthew Ahrens 	return (0);
270374e7dc98SMatthew Ahrens }
270474e7dc98SMatthew Ahrens 
270574e7dc98SMatthew Ahrens static void
snaplist_destroy(list_t * l,void * tag)27063b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
270774e7dc98SMatthew Ahrens {
270874e7dc98SMatthew Ahrens 	struct promotenode *snap;
270974e7dc98SMatthew Ahrens 
27103b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
271174e7dc98SMatthew Ahrens 		return;
271274e7dc98SMatthew Ahrens 
271374e7dc98SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
271474e7dc98SMatthew Ahrens 		list_remove(l, snap);
27153b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
27163b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
271774e7dc98SMatthew Ahrens 	}
271874e7dc98SMatthew Ahrens 	list_destroy(l);
271974e7dc98SMatthew Ahrens }
272074e7dc98SMatthew Ahrens 
27213b2aab18SMatthew Ahrens static int
promote_hold(dsl_dataset_promote_arg_t * ddpa,dsl_pool_t * dp,void * tag)27223b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
27233b2aab18SMatthew Ahrens {
27243b2aab18SMatthew Ahrens 	int error;
27253b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
27263b2aab18SMatthew Ahrens 	struct promotenode *snap;
27273b2aab18SMatthew Ahrens 
27283b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
27293b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
27303b2aab18SMatthew Ahrens 	if (error != 0)
27313b2aab18SMatthew Ahrens 		return (error);
27323b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
27333b2aab18SMatthew Ahrens 
27340521c5ebSJustin Gibbs 	if (ddpa->ddpa_clone->ds_is_snapshot ||
27353b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
27363b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
2737be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
27383b2aab18SMatthew Ahrens 	}
27393b2aab18SMatthew Ahrens 
27408d62b223SJustin T. Gibbs 	error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
27413b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
27423b2aab18SMatthew Ahrens 	if (error != 0)
27433b2aab18SMatthew Ahrens 		goto out;
27443b2aab18SMatthew Ahrens 
27453b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
27463b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
27473b2aab18SMatthew Ahrens 	if (error != 0)
27483b2aab18SMatthew Ahrens 		goto out;
27493b2aab18SMatthew Ahrens 
27503b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
27518d62b223SJustin T. Gibbs 	ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
27528d62b223SJustin T. Gibbs 	error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
27538d62b223SJustin T. Gibbs 	    dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
27543b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
27553b2aab18SMatthew Ahrens 	if (error != 0)
27563b2aab18SMatthew Ahrens 		goto out;
27573b2aab18SMatthew Ahrens 
27588d62b223SJustin T. Gibbs 	if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
27593b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
27608d62b223SJustin T. Gibbs 		    dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
27613b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
27623b2aab18SMatthew Ahrens 		if (error != 0)
27633b2aab18SMatthew Ahrens 			goto out;
27643b2aab18SMatthew Ahrens 	}
27653b2aab18SMatthew Ahrens out:
27663b2aab18SMatthew Ahrens 	if (error != 0)
27673b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
27683b2aab18SMatthew Ahrens 	return (error);
27693b2aab18SMatthew Ahrens }
27703b2aab18SMatthew Ahrens 
27713b2aab18SMatthew Ahrens static void
promote_rele(dsl_dataset_promote_arg_t * ddpa,void * tag)27723b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
27733b2aab18SMatthew Ahrens {
27743b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
27753b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
27763b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
27773b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
27783b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
27793b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
27803b2aab18SMatthew Ahrens }
27813b2aab18SMatthew Ahrens 
278274e7dc98SMatthew Ahrens /*
27833b2aab18SMatthew Ahrens  * Promote a clone.
27843b2aab18SMatthew Ahrens  *
27853b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
2786675fc291SMatthew Ahrens  * in with the name.  (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
278774e7dc98SMatthew Ahrens  */
278899653d4eSeschrock int
dsl_dataset_promote(const char * name,char * conflsnap)2789681d9761SEric Taylor dsl_dataset_promote(const char *name, char *conflsnap)
279099653d4eSeschrock {
27913b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
27923b2aab18SMatthew Ahrens 	uint64_t numsnaps;
27933b2aab18SMatthew Ahrens 	int error;
27943b2aab18SMatthew Ahrens 	objset_t *os;
279574e7dc98SMatthew Ahrens 
279699653d4eSeschrock 	/*
27973b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
27983b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
2799745cd3c5Smaybee 	 */
28003b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
28013b2aab18SMatthew Ahrens 	if (error != 0)
28023b2aab18SMatthew Ahrens 		return (error);
28033b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
28048d62b223SJustin T. Gibbs 	    dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
28058d62b223SJustin T. Gibbs 	    &numsnaps);
28063b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
28073b2aab18SMatthew Ahrens 	if (error != 0)
28083b2aab18SMatthew Ahrens 		return (error);
2809088f3894Sahrens 
28103b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
28113b2aab18SMatthew Ahrens 	ddpa.err_ds = conflsnap;
2812bc93cffdSJerry Jelinek 	ddpa.cr = CRED();
2813088f3894Sahrens 
28143b2aab18SMatthew Ahrens 	return (dsl_sync_task(name, dsl_dataset_promote_check,
281543d5e60eSMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa,
281643d5e60eSMatthew Ahrens 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2817745cd3c5Smaybee }
2818088f3894Sahrens 
28193b2aab18SMatthew Ahrens int
dsl_dataset_clone_swap_check_impl(dsl_dataset_t * clone,dsl_dataset_t * origin_head,boolean_t force,void * owner,dmu_tx_t * tx)28203b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
282191948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2822f18faf3fSek110237 {
28233b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2824f18faf3fSek110237 
28253cb34c60Sahrens 	/* they should both be heads */
28260521c5ebSJustin Gibbs 	if (clone->ds_is_snapshot ||
28270521c5ebSJustin Gibbs 	    origin_head->ds_is_snapshot)
2828be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2829f18faf3fSek110237 
283034f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
283134f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
2832be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2833f18faf3fSek110237 
28343b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
28353b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
28363b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
283734f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
2838be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2839f18faf3fSek110237 
28403cb34c60Sahrens 	/* the clone should be a child of the origin */
28413b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2842be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2843f18faf3fSek110237 
28443b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
284534f2f8cfSMatthew Ahrens 	if (!force &&
284634f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2847be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
2848a9b821a0Sck153898 
28493b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
285091948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
2851be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
2852a9b821a0Sck153898 
28533b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
28543b2aab18SMatthew Ahrens 	unused_refres_delta =
28553b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
28568d62b223SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
28573b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
28588d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
28593b2aab18SMatthew Ahrens 
28603b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
28613b2aab18SMatthew Ahrens 	    unused_refres_delta >
28623b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2863be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
2864a9b821a0Sck153898 
28653b2aab18SMatthew Ahrens 	/* clone can't be over the head's refquota */
28663b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
28678d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes >
28688d62b223SJustin T. Gibbs 	    origin_head->ds_quota)
2869be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2870c4cbca4fSChris Kirby 
28713cb34c60Sahrens 	return (0);
2872f18faf3fSek110237 }
2873f18faf3fSek110237 
28743b2aab18SMatthew Ahrens void
dsl_dataset_clone_swap_sync_impl(dsl_dataset_t * clone,dsl_dataset_t * origin_head,dmu_tx_t * tx)28753b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
28763b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2877f18faf3fSek110237 {
28783b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
28793b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
2880f18faf3fSek110237 
28813b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
28823b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
28838d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota);
288434f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2885a9b821a0Sck153898 
28860cc589a4SMatthew Ahrens 	/*
28870cc589a4SMatthew Ahrens 	 * Swap per-dataset feature flags.
28880cc589a4SMatthew Ahrens 	 */
28890cc589a4SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
28900cc589a4SMatthew Ahrens 		if (!(spa_feature_table[f].fi_flags &
28910cc589a4SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET)) {
28920cc589a4SMatthew Ahrens 			ASSERT(!clone->ds_feature_inuse[f]);
28930cc589a4SMatthew Ahrens 			ASSERT(!origin_head->ds_feature_inuse[f]);
28940cc589a4SMatthew Ahrens 			continue;
28950cc589a4SMatthew Ahrens 		}
28960cc589a4SMatthew Ahrens 
28970cc589a4SMatthew Ahrens 		boolean_t clone_inuse = clone->ds_feature_inuse[f];
28980cc589a4SMatthew Ahrens 		boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
28990cc589a4SMatthew Ahrens 
29000cc589a4SMatthew Ahrens 		if (clone_inuse) {
29010cc589a4SMatthew Ahrens 			dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
29020cc589a4SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_FALSE;
29030cc589a4SMatthew Ahrens 		}
29040cc589a4SMatthew Ahrens 		if (origin_head_inuse) {
29050cc589a4SMatthew Ahrens 			dsl_dataset_deactivate_feature(origin_head->ds_object,
29060cc589a4SMatthew Ahrens 			    f, tx);
29070cc589a4SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_FALSE;
29080cc589a4SMatthew Ahrens 		}
29090cc589a4SMatthew Ahrens 		if (clone_inuse) {
29100cc589a4SMatthew Ahrens 			dsl_dataset_activate_feature(origin_head->ds_object,
29110cc589a4SMatthew Ahrens 			    f, tx);
29120cc589a4SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_TRUE;
29130cc589a4SMatthew Ahrens 		}
29140cc589a4SMatthew Ahrens 		if (origin_head_inuse) {
29150cc589a4SMatthew Ahrens 			dsl_dataset_activate_feature(clone->ds_object, f, tx);
29160cc589a4SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_TRUE;
29170cc589a4SMatthew Ahrens 		}
29180cc589a4SMatthew Ahrens 	}
29190cc589a4SMatthew Ahrens 
29203b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
29213b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2922f18faf3fSek110237 
29233b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
29243b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
29253b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
29263cb34c60Sahrens 	}
2927f18faf3fSek110237 
29283b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
29293b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
29303b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
29313cb34c60Sahrens 	}
2932f18faf3fSek110237 
29333b2aab18SMatthew Ahrens 	unused_refres_delta =
29343b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
29358d62b223SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
29363b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
29378d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
29383b2aab18SMatthew Ahrens 
2939ae46e4c7SMatthew Ahrens 	/*
2940ae46e4c7SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
2941ae46e4c7SMatthew Ahrens 	 */
29423b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
29433b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
2944cde58dbcSMatthew Ahrens 		uint64_t comp, uncomp;
2945cde58dbcSMatthew Ahrens 
2946ae46e4c7SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
29473b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
29488d62b223SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
29498d62b223SJustin T. Gibbs 		    &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
2950ae46e4c7SMatthew Ahrens 	}
2951f18faf3fSek110237 
2952f18faf3fSek110237 	/* swap blkptrs */
2953f18faf3fSek110237 	{
2954f18faf3fSek110237 		blkptr_t tmp;
29558d62b223SJustin T. Gibbs 		tmp = dsl_dataset_phys(origin_head)->ds_bp;
29568d62b223SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_bp =
29578d62b223SJustin T. Gibbs 		    dsl_dataset_phys(clone)->ds_bp;
29588d62b223SJustin T. Gibbs 		dsl_dataset_phys(clone)->ds_bp = tmp;
2959f18faf3fSek110237 	}
2960f18faf3fSek110237 
2961f18faf3fSek110237 	/* set dd_*_bytes */
2962f18faf3fSek110237 	{
2963f18faf3fSek110237 		int64_t dused, dcomp, duncomp;
2964f18faf3fSek110237 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
2965f18faf3fSek110237 		uint64_t odl_used, odl_comp, odl_uncomp;
2966f18faf3fSek110237 
29678d62b223SJustin T. Gibbs 		ASSERT3U(dsl_dir_phys(clone->ds_dir)->
296874e7dc98SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
296974e7dc98SMatthew Ahrens 
29703b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
2971cde58dbcSMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
29723b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
2973cde58dbcSMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
297474e7dc98SMatthew Ahrens 
29758d62b223SJustin T. Gibbs 		dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
29768d62b223SJustin T. Gibbs 		    cdl_used -
29778d62b223SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
29788d62b223SJustin T. Gibbs 		    odl_used);
29798d62b223SJustin T. Gibbs 		dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
29808d62b223SJustin T. Gibbs 		    cdl_comp -
29818d62b223SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
29828d62b223SJustin T. Gibbs 		    odl_comp);
29838d62b223SJustin T. Gibbs 		duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
29843cb34c60Sahrens 		    cdl_uncomp -
29858d62b223SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
29868d62b223SJustin T. Gibbs 		    odl_uncomp);
2987f18faf3fSek110237 
29883b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
29893cb34c60Sahrens 		    dused, dcomp, duncomp, tx);
29903b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
29913cb34c60Sahrens 		    -dused, -dcomp, -duncomp, tx);
299274e7dc98SMatthew Ahrens 
299374e7dc98SMatthew Ahrens 		/*
299474e7dc98SMatthew Ahrens 		 * The difference in the space used by snapshots is the
299574e7dc98SMatthew Ahrens 		 * difference in snapshot space due to the head's
299674e7dc98SMatthew Ahrens 		 * deadlist (since that's the only thing that's
299774e7dc98SMatthew Ahrens 		 * changing that affects the snapused).
299874e7dc98SMatthew Ahrens 		 */
29993b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
30003b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3001cde58dbcSMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
30023b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
30033b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
3004cde58dbcSMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
30053b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
300674e7dc98SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
30073cb34c60Sahrens 	}
30083cb34c60Sahrens 
3009f18faf3fSek110237 	/* swap ds_*_bytes */
30108d62b223SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
30118d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes);
30128d62b223SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
30138d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_compressed_bytes);
30148d62b223SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
30158d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_uncompressed_bytes);
30168d62b223SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
30178d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
3018a9b821a0Sck153898 
3019a9b821a0Sck153898 	/* apply any parent delta for change in unconsumed refreservation */
30203b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
30213b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
3022f18faf3fSek110237 
3023cde58dbcSMatthew Ahrens 	/*
3024cde58dbcSMatthew Ahrens 	 * Swap deadlists.
3025cde58dbcSMatthew Ahrens 	 */
30263b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
30273b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
30288d62b223SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
30298d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
30303b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
30318d62b223SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
30323b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
30338d62b223SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_deadlist_obj);
303488b7b0f2SMatthew Ahrens 
30353b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
30364445fffbSMatthew Ahrens 
30373b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
30383b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
3039f18faf3fSek110237 }
3040f18faf3fSek110237 
3041b1b8ab34Slling /*
3042b1b8ab34Slling  * Given a pool name and a dataset object number in that pool,
3043b1b8ab34Slling  * return the name of that dataset.
3044b1b8ab34Slling  */
3045b1b8ab34Slling int
dsl_dsobj_to_dsname(char * pname,uint64_t obj,char * buf)3046b1b8ab34Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3047b1b8ab34Slling {
3048b1b8ab34Slling 	dsl_pool_t *dp;
3049745cd3c5Smaybee 	dsl_dataset_t *ds;
3050b1b8ab34Slling 	int error;
3051b1b8ab34Slling 
30523b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
30533b2aab18SMatthew Ahrens 	if (error != 0)
3054b1b8ab34Slling 		return (error);
30553b2aab18SMatthew Ahrens 
30563b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
30573b2aab18SMatthew Ahrens 	if (error == 0) {
3058b1b8ab34Slling 		dsl_dataset_name(ds, buf);
3059745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
3060745cd3c5Smaybee 	}
30613b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3062b1b8ab34Slling 
3063745cd3c5Smaybee 	return (error);
3064b1b8ab34Slling }
3065a9799022Sck153898 
3066a9799022Sck153898 int
dsl_dataset_check_quota(dsl_dataset_t * ds,boolean_t check_quota,uint64_t asize,uint64_t inflight,uint64_t * used,uint64_t * ref_rsrv)3067a9799022Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
3068745cd3c5Smaybee     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3069a9799022Sck153898 {
3070a9799022Sck153898 	int error = 0;
3071a9799022Sck153898 
3072a9799022Sck153898 	ASSERT3S(asize, >, 0);
3073a9799022Sck153898 
30749082849eSck153898 	/*
30759082849eSck153898 	 * *ref_rsrv is the portion of asize that will come from any
30769082849eSck153898 	 * unconsumed refreservation space.
30779082849eSck153898 	 */
30789082849eSck153898 	*ref_rsrv = 0;
30799082849eSck153898 
3080a9799022Sck153898 	mutex_enter(&ds->ds_lock);
3081a9799022Sck153898 	/*
3082a9799022Sck153898 	 * Make a space adjustment for reserved bytes.
3083a9799022Sck153898 	 */
30848d62b223SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
3085a9799022Sck153898 		ASSERT3U(*used, >=,
30868d62b223SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
30878d62b223SJustin T. Gibbs 		*used -=
30888d62b223SJustin T. Gibbs 		    (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
30899082849eSck153898 		*ref_rsrv =
30909082849eSck153898 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
3091a9799022Sck153898 	}
3092a9799022Sck153898 
3093a9799022Sck153898 	if (!check_quota || ds->ds_quota == 0) {
3094a9799022Sck153898 		mutex_exit(&ds->ds_lock);
3095a9799022Sck153898 		return (0);
3096a9799022Sck153898 	}
3097a9799022Sck153898 	/*
3098a9799022Sck153898 	 * If they are requesting more space, and our current estimate
3099a9799022Sck153898 	 * is over quota, they get to try again unless the actual
3100a9799022Sck153898 	 * on-disk is over quota and there are no pending changes (which
3101a9799022Sck153898 	 * may free up space for us).
3102a9799022Sck153898 	 */
31038d62b223SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
31048d62b223SJustin T. Gibbs 	    ds->ds_quota) {
3105ad135b5dSChristopher Siden 		if (inflight > 0 ||
31068d62b223SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
3107be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
3108a9799022Sck153898 		else
3109be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
3110a9799022Sck153898 	}
3111a9799022Sck153898 	mutex_exit(&ds->ds_lock);
3112a9799022Sck153898 
3113a9799022Sck153898 	return (error);
3114a9799022Sck153898 }
3115a9799022Sck153898 
31163b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
31173b2aab18SMatthew Ahrens 	const char *ddsqra_name;
31183b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
31193b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
31203b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
31213b2aab18SMatthew Ahrens 
31223b2aab18SMatthew Ahrens 
3123a9799022Sck153898 /* ARGSUSED */
3124a9799022Sck153898 static int
dsl_dataset_set_refquota_check(void * arg,dmu_tx_t * tx)31253b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
3126a9799022Sck153898 {
31273b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
31283b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
31293b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
31303b2aab18SMatthew Ahrens 	int error;
31313b2aab18SMatthew Ahrens 	uint64_t newval;
3132a9799022Sck153898 
31333b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
3134be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3135a9799022Sck153898 
31363b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
31373b2aab18SMatthew Ahrens 	if (error != 0)
31383b2aab18SMatthew Ahrens 		return (error);
313992241e0bSTom Erickson 
31400521c5ebSJustin Gibbs 	if (ds->ds_is_snapshot) {
31413b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3142be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
31433b2aab18SMatthew Ahrens 	}
31443b2aab18SMatthew Ahrens 
31453b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
31463b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
31473b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
31483b2aab18SMatthew Ahrens 	if (error != 0) {
31493b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
31503b2aab18SMatthew Ahrens 		return (error);
31513b2aab18SMatthew Ahrens 	}
31523b2aab18SMatthew Ahrens 
31533b2aab18SMatthew Ahrens 	if (newval == 0) {
31543b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3155a9799022Sck153898 		return (0);
31563b2aab18SMatthew Ahrens 	}
3157a9799022Sck153898 
31588d62b223SJustin T. Gibbs 	if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
31593b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
31603b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3161be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
31623b2aab18SMatthew Ahrens 	}
3163a9799022Sck153898 
31643b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3165a9799022Sck153898 	return (0);
3166a9799022Sck153898 }
3167a9799022Sck153898 
31683b2aab18SMatthew Ahrens static void
dsl_dataset_set_refquota_sync(void * arg,dmu_tx_t * tx)31693b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3170a9799022Sck153898 {
31713b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
31723b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
31733b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
31743b2aab18SMatthew Ahrens 	uint64_t newval;
3175a9799022Sck153898 
31763b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
317792241e0bSTom Erickson 
31783b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
31793b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
31803b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
31813b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
31823b2aab18SMatthew Ahrens 
31833b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
31843b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
31853b2aab18SMatthew Ahrens 
31863b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
3187a9799022Sck153898 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
31883b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
318992241e0bSTom Erickson 	}
31903b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3191a9799022Sck153898 }
3192a9799022Sck153898 
3193a9799022Sck153898 int
dsl_dataset_set_refquota(const char * dsname,zprop_source_t source,uint64_t refquota)31943b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
31953b2aab18SMatthew Ahrens     uint64_t refquota)
3196a9799022Sck153898 {
31973b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3198a9799022Sck153898 
31993b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
32003b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
32013b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
320292241e0bSTom Erickson 
32033b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
320443d5e60eSMatthew Ahrens 	    dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
3205a9799022Sck153898 }
3206a9799022Sck153898 
3207a9799022Sck153898 static int
dsl_dataset_set_refreservation_check(void * arg,dmu_tx_t * tx)32083b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3209a9799022Sck153898 {
32103b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
32113b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
32123b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
32133b2aab18SMatthew Ahrens 	int error;
32143b2aab18SMatthew Ahrens 	uint64_t newval, unique;
3215a9799022Sck153898 
32163b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3217be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3218a9799022Sck153898 
32193b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
32203b2aab18SMatthew Ahrens 	if (error != 0)
32213b2aab18SMatthew Ahrens 		return (error);
32223b2aab18SMatthew Ahrens 
32230521c5ebSJustin Gibbs 	if (ds->ds_is_snapshot) {
32243b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3225be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
32263b2aab18SMatthew Ahrens 	}
3227a9799022Sck153898 
32283b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
32293b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
32303b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
32313b2aab18SMatthew Ahrens 	if (error != 0) {
32323b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
32333b2aab18SMatthew Ahrens 		return (error);
32343b2aab18SMatthew Ahrens 	}
323592241e0bSTom Erickson 
3236a9799022Sck153898 	/*
3237a9799022Sck153898 	 * If we are doing the preliminary check in open context, the
3238a9799022Sck153898 	 * space estimates may be inaccurate.
3239a9799022Sck153898 	 */
32403b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
32413b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3242a9799022Sck153898 		return (0);
32433b2aab18SMatthew Ahrens 	}
3244a9799022Sck153898 
3245a9799022Sck153898 	mutex_enter(&ds->ds_lock);
32463f9d6ad7SLin Ling 	if (!DS_UNIQUE_IS_ACCURATE(ds))
32473f9d6ad7SLin Ling 		dsl_dataset_recalc_head_uniq(ds);
32488d62b223SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
3249a9799022Sck153898 	mutex_exit(&ds->ds_lock);
3250a9799022Sck153898 
32513b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
32523b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
3253379c004dSEric Schrock 		    MAX(unique, ds->ds_reserved);
3254379c004dSEric Schrock 
32553b2aab18SMatthew Ahrens 		if (delta >
32563b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
32573b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
32583b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
3259be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
3260379c004dSEric Schrock 		}
32613b2aab18SMatthew Ahrens 	}
3262a9799022Sck153898 
32633b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3264a9799022Sck153898 	return (0);
3265a9799022Sck153898 }
3266a9799022Sck153898 
32673b2aab18SMatthew Ahrens void
dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t * ds,zprop_source_t source,uint64_t value,dmu_tx_t * tx)32683b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
32693b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3270a9799022Sck153898 {
32713b2aab18SMatthew Ahrens 	uint64_t newval;
327202c8f3f0SMatthew Ahrens 	uint64_t unique;
327302c8f3f0SMatthew Ahrens 	int64_t delta;
3274a9799022Sck153898 
32753b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
32763b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
32773b2aab18SMatthew Ahrens 
32783b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
32793b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
328092241e0bSTom Erickson 
328102c8f3f0SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
328202c8f3f0SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
328302c8f3f0SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
32843f9d6ad7SLin Ling 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
32858d62b223SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
32863b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
328702c8f3f0SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
32883b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
328902c8f3f0SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
329002c8f3f0SMatthew Ahrens 
329102c8f3f0SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
329202c8f3f0SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
3293a9799022Sck153898 }
3294a9799022Sck153898 
3295c99e4bdcSChris Kirby static void
dsl_dataset_set_refreservation_sync(void * arg,dmu_tx_t * tx)32963b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3297c99e4bdcSChris Kirby {
32983b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
32993b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3300842727c2SChris Kirby 	dsl_dataset_t *ds;
3301842727c2SChris Kirby 
33023b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
33033b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
33043b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3305ca45db41SChris Kirby 	dsl_dataset_rele(ds, FTAG);
3306ca45db41SChris Kirby }
3307ca45db41SChris Kirby 
3308842727c2SChris Kirby int
dsl_dataset_set_refreservation(const char * dsname,zprop_source_t source,uint64_t refreservation)33093b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
33103b2aab18SMatthew Ahrens     uint64_t refreservation)
3311842727c2SChris Kirby {
33123b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3313842727c2SChris Kirby 
33143b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
33153b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
33163b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
3317842727c2SChris Kirby 
33183b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
331943d5e60eSMatthew Ahrens 	    dsl_dataset_set_refreservation_sync, &ddsqra,
332043d5e60eSMatthew Ahrens 	    0, ZFS_SPACE_CHECK_NONE));
3321503ad85cSMatthew Ahrens }
332219b94df9SMatthew Ahrens 
332319b94df9SMatthew Ahrens /*
332419b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
332519b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
332619b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
332719b94df9SMatthew Ahrens  * fail and return EINVAL.
332819b94df9SMatthew Ahrens  *
332919b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
333019b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
333119b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
333219b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
333319b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
333419b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
333519b94df9SMatthew Ahrens  *
333619b94df9SMatthew Ahrens  * space freed                         [---------------------]
333719b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
333819b94df9SMatthew Ahrens  *                                         oldsnap            new
333919b94df9SMatthew Ahrens  */
334019b94df9SMatthew Ahrens int
dsl_dataset_space_written(dsl_dataset_t * oldsnap,dsl_dataset_t * new,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)334119b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
334219b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
334319b94df9SMatthew Ahrens {
334419b94df9SMatthew Ahrens 	int err = 0;
334519b94df9SMatthew Ahrens 	uint64_t snapobj;
334619b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
334719b94df9SMatthew Ahrens 
33483b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
33493b2aab18SMatthew Ahrens 
335019b94df9SMatthew Ahrens 	*usedp = 0;
33518d62b223SJustin T. Gibbs 	*usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
33528d62b223SJustin T. Gibbs 	*usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
335319b94df9SMatthew Ahrens 
335419b94df9SMatthew Ahrens 	*compp = 0;
33558d62b223SJustin T. Gibbs 	*compp += dsl_dataset_phys(new)->ds_compressed_bytes;
33568d62b223SJustin T. Gibbs 	*compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
335719b94df9SMatthew Ahrens 
335819b94df9SMatthew Ahrens 	*uncompp = 0;
33598d62b223SJustin T. Gibbs 	*uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
33608d62b223SJustin T. Gibbs 	*uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
336119b94df9SMatthew Ahrens 
336219b94df9SMatthew Ahrens 	snapobj = new->ds_object;
336319b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
336419b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
336519b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
336619b94df9SMatthew Ahrens 
3367ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
3368ad135b5dSChristopher Siden 			snap = new;
3369ad135b5dSChristopher Siden 		} else {
337019b94df9SMatthew Ahrens 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
337119b94df9SMatthew Ahrens 			if (err != 0)
337219b94df9SMatthew Ahrens 				break;
3373ad135b5dSChristopher Siden 		}
337419b94df9SMatthew Ahrens 
33758d62b223SJustin T. Gibbs 		if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
33768d62b223SJustin T. Gibbs 		    dsl_dataset_phys(oldsnap)->ds_creation_txg) {
337719b94df9SMatthew Ahrens 			/*
337819b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
337919b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
338019b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
338119b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
338219b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
338319b94df9SMatthew Ahrens 			 * optimization itself.
338419b94df9SMatthew Ahrens 			 */
338519b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
338619b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
338719b94df9SMatthew Ahrens 		} else {
338819b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
33898d62b223SJustin T. Gibbs 			    0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
339019b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
339119b94df9SMatthew Ahrens 		}
339219b94df9SMatthew Ahrens 		*usedp += used;
339319b94df9SMatthew Ahrens 		*compp += comp;
339419b94df9SMatthew Ahrens 		*uncompp += uncomp;
339519b94df9SMatthew Ahrens 
339619b94df9SMatthew Ahrens 		/*
339719b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
339819b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
339919b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
340019b94df9SMatthew Ahrens 		 */
34018d62b223SJustin T. Gibbs 		snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3402ad135b5dSChristopher Siden 		if (snap != new)
340319b94df9SMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
340419b94df9SMatthew Ahrens 		if (snapobj == 0) {
3405be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
340619b94df9SMatthew Ahrens 			break;
340719b94df9SMatthew Ahrens 		}
340819b94df9SMatthew Ahrens 
340919b94df9SMatthew Ahrens 	}
341019b94df9SMatthew Ahrens 	return (err);
341119b94df9SMatthew Ahrens }
341219b94df9SMatthew Ahrens 
341319b94df9SMatthew Ahrens /*
341419b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
341519b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
341619b94df9SMatthew Ahrens  *
341719b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
341819b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
341919b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
342019b94df9SMatthew Ahrens  *
342119b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
342219b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
342319b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
342419b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
342519b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
342619b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
342719b94df9SMatthew Ahrens  */
342819b94df9SMatthew Ahrens int
dsl_dataset_space_wouldfree(dsl_dataset_t * firstsnap,dsl_dataset_t * lastsnap,uint64_t * usedp,uint64_t * compp,uint64_t * uncompp)342919b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
343019b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
343119b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
343219b94df9SMatthew Ahrens {
343319b94df9SMatthew Ahrens 	int err = 0;
343419b94df9SMatthew Ahrens 	uint64_t snapobj;
343519b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
343619b94df9SMatthew Ahrens 
34370521c5ebSJustin Gibbs 	ASSERT(firstsnap->ds_is_snapshot);
34380521c5ebSJustin Gibbs 	ASSERT(lastsnap->ds_is_snapshot);
343919b94df9SMatthew Ahrens 
344019b94df9SMatthew Ahrens 	/*
344119b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
344219b94df9SMatthew Ahrens 	 * is before lastsnap.
344319b94df9SMatthew Ahrens 	 */
344419b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
34458d62b223SJustin T. Gibbs 	    dsl_dataset_phys(firstsnap)->ds_creation_txg >
34468d62b223SJustin T. Gibbs 	    dsl_dataset_phys(lastsnap)->ds_creation_txg)
3447be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
344819b94df9SMatthew Ahrens 
344919b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
345019b94df9SMatthew Ahrens 
34518d62b223SJustin T. Gibbs 	snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
345219b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
345319b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
345419b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
345519b94df9SMatthew Ahrens 
345619b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
345719b94df9SMatthew Ahrens 		if (err != 0)
345819b94df9SMatthew Ahrens 			break;
345919b94df9SMatthew Ahrens 
346019b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
34618d62b223SJustin T. Gibbs 		    dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
346219b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
346319b94df9SMatthew Ahrens 		*usedp += used;
346419b94df9SMatthew Ahrens 		*compp += comp;
346519b94df9SMatthew Ahrens 		*uncompp += uncomp;
346619b94df9SMatthew Ahrens 
34678d62b223SJustin T. Gibbs 		snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
346819b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
346919b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
347019b94df9SMatthew Ahrens 	}
347119b94df9SMatthew Ahrens 	return (err);
347219b94df9SMatthew Ahrens }
34733b2aab18SMatthew Ahrens 
34743b2aab18SMatthew Ahrens /*
34753b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
34763b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
34773b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
34783b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
34793b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
3480e69d9941SMatthew Ahrens  *
3481e69d9941SMatthew Ahrens  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
34823b2aab18SMatthew Ahrens  */
34833b2aab18SMatthew Ahrens boolean_t
dsl_dataset_is_before(dsl_dataset_t * later,dsl_dataset_t * earlier,uint64_t earlier_txg)3484e69d9941SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
3485e69d9941SMatthew Ahrens 	uint64_t earlier_txg)
34863b2aab18SMatthew Ahrens {
34873b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
34883b2aab18SMatthew Ahrens 	int error;
34893b2aab18SMatthew Ahrens 	boolean_t ret;
34903b2aab18SMatthew Ahrens 
34913b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
34920521c5ebSJustin Gibbs 	ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
34933b2aab18SMatthew Ahrens 
3494e69d9941SMatthew Ahrens 	if (earlier_txg == 0)
34958d62b223SJustin T. Gibbs 		earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
3496e69d9941SMatthew Ahrens 
34970521c5ebSJustin Gibbs 	if (later->ds_is_snapshot &&
34988d62b223SJustin T. Gibbs 	    earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
34993b2aab18SMatthew Ahrens 		return (B_FALSE);
35003b2aab18SMatthew Ahrens 
35013b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
35023b2aab18SMatthew Ahrens 		return (B_TRUE);
35033b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
35043b2aab18SMatthew Ahrens 		return (B_FALSE);
35053b2aab18SMatthew Ahrens 
35068d62b223SJustin T. Gibbs 	if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
35073b2aab18SMatthew Ahrens 		return (B_TRUE);
35083b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
35093b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
35108d62b223SJustin T. Gibbs 	    dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
35113b2aab18SMatthew Ahrens 	if (error != 0)
35123b2aab18SMatthew Ahrens 		return (B_FALSE);
3513e69d9941SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
35143b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
35153b2aab18SMatthew Ahrens 	return (ret);
35163b2aab18SMatthew Ahrens }
3517dbc43af2SMatthew Ahrens 
3518dbc43af2SMatthew Ahrens void
dsl_dataset_zapify(dsl_dataset_t * ds,dmu_tx_t * tx)3519dbc43af2SMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
3520dbc43af2SMatthew Ahrens {
3521dbc43af2SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
3522dbc43af2SMatthew Ahrens 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
3523dbc43af2SMatthew Ahrens }
35248429b235SMatthew Ahrens 
35258429b235SMatthew Ahrens boolean_t
dsl_dataset_is_zapified(dsl_dataset_t * ds)35268429b235SMatthew Ahrens dsl_dataset_is_zapified(dsl_dataset_t *ds)
35278429b235SMatthew Ahrens {
35288429b235SMatthew Ahrens 	dmu_object_info_t doi;
35298429b235SMatthew Ahrens 
35308429b235SMatthew Ahrens 	dmu_object_info_from_db(ds->ds_dbuf, &doi);
35318429b235SMatthew Ahrens 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
35328429b235SMatthew Ahrens }
35338429b235SMatthew Ahrens 
35348429b235SMatthew Ahrens boolean_t
dsl_dataset_has_resume_receive_state(dsl_dataset_t * ds)35358429b235SMatthew Ahrens dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
35368429b235SMatthew Ahrens {
35378429b235SMatthew Ahrens 	return (dsl_dataset_is_zapified(ds) &&
35388429b235SMatthew Ahrens 	    zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
35398429b235SMatthew Ahrens 	    ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
35408429b235SMatthew Ahrens }
3541