xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dataset.c (revision eb633035c80613ec93d62f90482837adaaf21a0a)
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  */
211c17160aSKevin Crowe 
22fa9e4066Sahrens /*
235afc78aaSChris Kirby  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24000cce6bSBrad Lewis  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
2603d1795fSAlexander Stetsenko  * Copyright (c) 2014 RackTop Systems.
27bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
295f7a8e6dSDan McDonald  * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
301c17160aSKevin Crowe  * Copyright 2017 Nexenta Systems, Inc.
31fa9e4066Sahrens  */
32fa9e4066Sahrens 
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens #include <sys/dsl_dataset.h>
35fa9e4066Sahrens #include <sys/dsl_dir.h>
3699653d4eSeschrock #include <sys/dsl_prop.h>
371d452cf5Sahrens #include <sys/dsl_synctask.h>
38fa9e4066Sahrens #include <sys/dmu_traverse.h>
394e3c9f44SBill Pijewski #include <sys/dmu_impl.h>
40fa9e4066Sahrens #include <sys/dmu_tx.h>
41fa9e4066Sahrens #include <sys/arc.h>
42fa9e4066Sahrens #include <sys/zio.h>
43fa9e4066Sahrens #include <sys/zap.h>
44ad135b5dSChristopher Siden #include <sys/zfeature.h>
45fa9e4066Sahrens #include <sys/unique.h>
46fa9e4066Sahrens #include <sys/zfs_context.h>
47cdf5b4caSmmusante #include <sys/zfs_ioctl.h>
48ecd6cf80Smarks #include <sys/spa.h>
4986714001SSerapheim Dimitropoulos #include <sys/spa_impl.h>
505cabbc6bSPrashanth Sreenivasa #include <sys/vdev.h>
51088f3894Sahrens #include <sys/zfs_znode.h>
52c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
53842727c2SChris Kirby #include <sys/zvol.h>
543f9d6ad7SLin Ling #include <sys/dsl_scan.h>
55cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
563b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
573b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h>
5878f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
590fa1b3ccSPaul Dagnelie #include <sys/dmu_recv.h>
6045818ee1SMatthew Ahrens #include <sys/zio_checksum.h>
619c3fd121SMatthew Ahrens #include <sys/zio_compress.h>
629c3fd121SMatthew Ahrens #include <zfs_fletcher.h>
63e1930233Sbonwick 
64b5152584SMatthew Ahrens /*
65b5152584SMatthew Ahrens  * The SPA supports block sizes up to 16MB.  However, very large blocks
66b5152584SMatthew Ahrens  * can have an impact on i/o latency (e.g. tying up a spinning disk for
67b5152584SMatthew Ahrens  * ~300ms), and also potentially on the memory allocator.  Therefore,
68b5152584SMatthew Ahrens  * we do not allow the recordsize to be set larger than zfs_max_recordsize
69b5152584SMatthew Ahrens  * (default 1MB).  Larger blocks can be created by changing this tunable,
70b5152584SMatthew Ahrens  * and pools with larger blocks can always be imported and used, regardless
71b5152584SMatthew Ahrens  * of this setting.
72b5152584SMatthew Ahrens  */
73b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024;
74b5152584SMatthew Ahrens 
75cde58dbcSMatthew Ahrens #define	SWITCH64(x, y) \
76cde58dbcSMatthew Ahrens 	{ \
77cde58dbcSMatthew Ahrens 		uint64_t __tmp = (x); \
78cde58dbcSMatthew Ahrens 		(x) = (y); \
79cde58dbcSMatthew Ahrens 		(y) = __tmp; \
80cde58dbcSMatthew Ahrens 	}
81cde58dbcSMatthew Ahrens 
8255434c77Sek #define	DS_REF_MAX	(1ULL << 62)
83fa9e4066Sahrens 
84c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
85c1379625SJustin T. Gibbs 
865cabbc6bSPrashanth Sreenivasa static void dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds,
875cabbc6bSPrashanth Sreenivasa     uint64_t obj, dmu_tx_t *tx);
885cabbc6bSPrashanth Sreenivasa static void dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds,
895cabbc6bSPrashanth Sreenivasa     dmu_tx_t *tx);
905cabbc6bSPrashanth Sreenivasa 
915f7a8e6dSDan McDonald extern int spa_asize_inflation;
925f7a8e6dSDan McDonald 
93bfaed0b9SAndriy Gapon static zil_header_t zero_zil;
94bfaed0b9SAndriy Gapon 
95a9799022Sck /*
96a9799022Sck  * Figure out how much of this delta should be propogated to the dsl_dir
97a9799022Sck  * layer.  If there's a refreservation, that space has already been
98a9799022Sck  * partially accounted for in our ancestors.
99a9799022Sck  */
100a9799022Sck static int64_t
101a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta)
102a9799022Sck {
103c1379625SJustin T. Gibbs 	dsl_dataset_phys_t *ds_phys;
104a9799022Sck 	uint64_t old_bytes, new_bytes;
105a9799022Sck 
106a9799022Sck 	if (ds->ds_reserved == 0)
107a9799022Sck 		return (delta);
108a9799022Sck 
109c1379625SJustin T. Gibbs 	ds_phys = dsl_dataset_phys(ds);
110c1379625SJustin T. Gibbs 	old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
111c1379625SJustin T. Gibbs 	new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
112a9799022Sck 
113a9799022Sck 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
114a9799022Sck 	return (new_bytes - old_bytes);
115a9799022Sck }
116fa9e4066Sahrens 
117fa9e4066Sahrens void
118b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
119fa9e4066Sahrens {
120b24ab676SJeff Bonwick 	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
121fa9e4066Sahrens 	int compressed = BP_GET_PSIZE(bp);
122fa9e4066Sahrens 	int uncompressed = BP_GET_UCSIZE(bp);
123a9799022Sck 	int64_t delta;
124fa9e4066Sahrens 
1253f9d6ad7SLin Ling 	dprintf_bp(bp, "ds=%p", ds);
126fa9e4066Sahrens 
127fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
128fa9e4066Sahrens 	/* It could have been compressed away to nothing */
129fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
130fa9e4066Sahrens 		return;
131fa9e4066Sahrens 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
132ad135b5dSChristopher Siden 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
133fa9e4066Sahrens 	if (ds == NULL) {
134ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
135ce636f8bSMatthew Ahrens 		    used, compressed, uncompressed);
136fa9e4066Sahrens 		return;
137fa9e4066Sahrens 	}
1383f9d6ad7SLin Ling 
139bfaed0b9SAndriy Gapon 	ASSERT3U(bp->blk_birth, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
140b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
141fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
142a9799022Sck 	delta = parent_delta(ds, used);
143c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes += used;
144c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
145c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
146c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes += used;
14745818ee1SMatthew Ahrens 
148ca0cc391SMatthew Ahrens 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
149ca0cc391SMatthew Ahrens 		ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] =
150ca0cc391SMatthew Ahrens 		    B_TRUE;
151ca0cc391SMatthew Ahrens 	}
15245818ee1SMatthew Ahrens 
15345818ee1SMatthew Ahrens 	spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
15445818ee1SMatthew Ahrens 	if (f != SPA_FEATURE_NONE)
15545818ee1SMatthew Ahrens 		ds->ds_feature_activation_needed[f] = B_TRUE;
15645818ee1SMatthew Ahrens 
157fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
15874e7dc98SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
15974e7dc98SMatthew Ahrens 	    compressed, uncompressed, tx);
16074e7dc98SMatthew Ahrens 	dsl_dir_transfer_space(ds->ds_dir, used - delta,
16174e7dc98SMatthew Ahrens 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
162fa9e4066Sahrens }
163fa9e4066Sahrens 
1645cabbc6bSPrashanth Sreenivasa /*
1655cabbc6bSPrashanth Sreenivasa  * Called when the specified segment has been remapped, and is thus no
1665cabbc6bSPrashanth Sreenivasa  * longer referenced in the head dataset.  The vdev must be indirect.
1675cabbc6bSPrashanth Sreenivasa  *
1685cabbc6bSPrashanth Sreenivasa  * If the segment is referenced by a snapshot, put it on the remap deadlist.
1695cabbc6bSPrashanth Sreenivasa  * Otherwise, add this segment to the obsolete spacemap.
1705cabbc6bSPrashanth Sreenivasa  */
1715cabbc6bSPrashanth Sreenivasa void
1725cabbc6bSPrashanth Sreenivasa dsl_dataset_block_remapped(dsl_dataset_t *ds, uint64_t vdev, uint64_t offset,
1735cabbc6bSPrashanth Sreenivasa     uint64_t size, uint64_t birth, dmu_tx_t *tx)
1745cabbc6bSPrashanth Sreenivasa {
1755cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
1765cabbc6bSPrashanth Sreenivasa 
1775cabbc6bSPrashanth Sreenivasa 	ASSERT(dmu_tx_is_syncing(tx));
1785cabbc6bSPrashanth Sreenivasa 	ASSERT(birth <= tx->tx_txg);
1795cabbc6bSPrashanth Sreenivasa 	ASSERT(!ds->ds_is_snapshot);
1805cabbc6bSPrashanth Sreenivasa 
1815cabbc6bSPrashanth Sreenivasa 	if (birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
1825cabbc6bSPrashanth Sreenivasa 		spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
1835cabbc6bSPrashanth Sreenivasa 	} else {
1845cabbc6bSPrashanth Sreenivasa 		blkptr_t fakebp;
1855cabbc6bSPrashanth Sreenivasa 		dva_t *dva = &fakebp.blk_dva[0];
1865cabbc6bSPrashanth Sreenivasa 
1875cabbc6bSPrashanth Sreenivasa 		ASSERT(ds != NULL);
1885cabbc6bSPrashanth Sreenivasa 
1895cabbc6bSPrashanth Sreenivasa 		mutex_enter(&ds->ds_remap_deadlist_lock);
1905cabbc6bSPrashanth Sreenivasa 		if (!dsl_dataset_remap_deadlist_exists(ds)) {
1915cabbc6bSPrashanth Sreenivasa 			dsl_dataset_create_remap_deadlist(ds, tx);
1925cabbc6bSPrashanth Sreenivasa 		}
1935cabbc6bSPrashanth Sreenivasa 		mutex_exit(&ds->ds_remap_deadlist_lock);
1945cabbc6bSPrashanth Sreenivasa 
1955cabbc6bSPrashanth Sreenivasa 		BP_ZERO(&fakebp);
1965cabbc6bSPrashanth Sreenivasa 		fakebp.blk_birth = birth;
1975cabbc6bSPrashanth Sreenivasa 		DVA_SET_VDEV(dva, vdev);
1985cabbc6bSPrashanth Sreenivasa 		DVA_SET_OFFSET(dva, offset);
1995cabbc6bSPrashanth Sreenivasa 		DVA_SET_ASIZE(dva, size);
2005cabbc6bSPrashanth Sreenivasa 
2015cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_insert(&ds->ds_remap_deadlist, &fakebp, tx);
2025cabbc6bSPrashanth Sreenivasa 	}
2035cabbc6bSPrashanth Sreenivasa }
2045cabbc6bSPrashanth Sreenivasa 
205cdb0ab79Smaybee int
206b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
207b24ab676SJeff Bonwick     boolean_t async)
208fa9e4066Sahrens {
20986714001SSerapheim Dimitropoulos 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
21086714001SSerapheim Dimitropoulos 
21186714001SSerapheim Dimitropoulos 	int used = bp_get_dsize_sync(spa, bp);
21243466aaeSMax Grossman 	int compressed = BP_GET_PSIZE(bp);
21343466aaeSMax Grossman 	int uncompressed = BP_GET_UCSIZE(bp);
21443466aaeSMax Grossman 
215fa9e4066Sahrens 	if (BP_IS_HOLE(bp))
216cdb0ab79Smaybee 		return (0);
217fa9e4066Sahrens 
218b24ab676SJeff Bonwick 	ASSERT(dmu_tx_is_syncing(tx));
219b24ab676SJeff Bonwick 	ASSERT(bp->blk_birth <= tx->tx_txg);
220b24ab676SJeff Bonwick 
221fa9e4066Sahrens 	if (ds == NULL) {
222b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
223ce636f8bSMatthew Ahrens 		dsl_pool_mos_diduse_space(tx->tx_pool,
224ce636f8bSMatthew Ahrens 		    -used, -compressed, -uncompressed);
225cdb0ab79Smaybee 		return (used);
226fa9e4066Sahrens 	}
227fa9e4066Sahrens 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
228fa9e4066Sahrens 
229bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
230fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
231fa9e4066Sahrens 
232c1379625SJustin T. Gibbs 	if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
233a9799022Sck 		int64_t delta;
234c717a561Smaybee 
2353f9d6ad7SLin Ling 		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
236b24ab676SJeff Bonwick 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
237fa9e4066Sahrens 
238fa9e4066Sahrens 		mutex_enter(&ds->ds_lock);
239c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
240a9799022Sck 		    !DS_UNIQUE_IS_ACCURATE(ds));
241a9799022Sck 		delta = parent_delta(ds, -used);
242c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_unique_bytes -= used;
243fa9e4066Sahrens 		mutex_exit(&ds->ds_lock);
24474e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
245a9799022Sck 		    delta, -compressed, -uncompressed, tx);
24674e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
24774e7dc98SMatthew Ahrens 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
248fa9e4066Sahrens 	} else {
249fa9e4066Sahrens 		dprintf_bp(bp, "putting on dead list: %s", "");
250b24ab676SJeff Bonwick 		if (async) {
251b24ab676SJeff Bonwick 			/*
252b24ab676SJeff Bonwick 			 * We are here as part of zio's write done callback,
253b24ab676SJeff Bonwick 			 * which means we're a zio interrupt thread.  We can't
254cde58dbcSMatthew Ahrens 			 * call dsl_deadlist_insert() now because it may block
255b24ab676SJeff Bonwick 			 * waiting for I/O.  Instead, put bp on the deferred
256b24ab676SJeff Bonwick 			 * queue and let dsl_pool_sync() finish the job.
257b24ab676SJeff Bonwick 			 */
258cde58dbcSMatthew Ahrens 			bplist_append(&ds->ds_pending_deadlist, bp);
259b24ab676SJeff Bonwick 		} else {
260cde58dbcSMatthew Ahrens 			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
261b24ab676SJeff Bonwick 		}
262a4611edeSahrens 		ASSERT3U(ds->ds_prev->ds_object, ==,
263c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_prev_snap_obj);
264c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
265fa9e4066Sahrens 		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
266c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
267a4611edeSahrens 		    ds->ds_object && bp->blk_birth >
268c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
269a4611edeSahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
270a4611edeSahrens 			mutex_enter(&ds->ds_prev->ds_lock);
271c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
272a4611edeSahrens 			mutex_exit(&ds->ds_prev->ds_lock);
273fa9e4066Sahrens 		}
2743f9d6ad7SLin Ling 		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
27574e7dc98SMatthew Ahrens 			dsl_dir_transfer_space(ds->ds_dir, used,
27674e7dc98SMatthew Ahrens 			    DD_USED_HEAD, DD_USED_SNAP, tx);
27774e7dc98SMatthew Ahrens 		}
278fa9e4066Sahrens 	}
279fa9e4066Sahrens 	mutex_enter(&ds->ds_lock);
280c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
281c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
282c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
283c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
284c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
285c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
286fa9e4066Sahrens 	mutex_exit(&ds->ds_lock);
287cdb0ab79Smaybee 
288cdb0ab79Smaybee 	return (used);
289fa9e4066Sahrens }
290fa9e4066Sahrens 
29140510e8eSJosef 'Jeff' Sipek /*
29240510e8eSJosef 'Jeff' Sipek  * We have to release the fsid syncronously or we risk that a subsequent
29340510e8eSJosef 'Jeff' Sipek  * mount of the same dataset will fail to unique_insert the fsid.  This
29440510e8eSJosef 'Jeff' Sipek  * failure would manifest itself as the fsid of this dataset changing
29540510e8eSJosef 'Jeff' Sipek  * between mounts which makes NFS clients quite unhappy.
29640510e8eSJosef 'Jeff' Sipek  */
297fa9e4066Sahrens static void
29840510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_sync(void *dbu)
299fa9e4066Sahrens {
300bc9014e6SJustin Gibbs 	dsl_dataset_t *ds = dbu;
301fa9e4066Sahrens 
3023b2aab18SMatthew Ahrens 	ASSERT(ds->ds_owner == NULL);
303fa9e4066Sahrens 
30491ebeef5Sahrens 	unique_remove(ds->ds_fsid_guid);
30540510e8eSJosef 'Jeff' Sipek }
30640510e8eSJosef 'Jeff' Sipek 
30740510e8eSJosef 'Jeff' Sipek static void
30840510e8eSJosef 'Jeff' Sipek dsl_dataset_evict_async(void *dbu)
30940510e8eSJosef 'Jeff' Sipek {
31040510e8eSJosef 'Jeff' Sipek 	dsl_dataset_t *ds = dbu;
31140510e8eSJosef 'Jeff' Sipek 
31240510e8eSJosef 'Jeff' Sipek 	ASSERT(ds->ds_owner == NULL);
31340510e8eSJosef 'Jeff' Sipek 
31440510e8eSJosef 'Jeff' Sipek 	ds->ds_dbuf = NULL;
315fa9e4066Sahrens 
316503ad85cSMatthew Ahrens 	if (ds->ds_objset != NULL)
317503ad85cSMatthew Ahrens 		dmu_objset_evict(ds->ds_objset);
318fa9e4066Sahrens 
319fa9e4066Sahrens 	if (ds->ds_prev) {
3203b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
321fa9e4066Sahrens 		ds->ds_prev = NULL;
322fa9e4066Sahrens 	}
323fa9e4066Sahrens 
324cde58dbcSMatthew Ahrens 	bplist_destroy(&ds->ds_pending_deadlist);
3255cabbc6bSPrashanth Sreenivasa 	if (dsl_deadlist_is_open(&ds->ds_deadlist))
326cde58dbcSMatthew Ahrens 		dsl_deadlist_close(&ds->ds_deadlist);
3275cabbc6bSPrashanth Sreenivasa 	if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
3285cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_close(&ds->ds_remap_deadlist);
329745cd3c5Smaybee 	if (ds->ds_dir)
330bc9014e6SJustin Gibbs 		dsl_dir_async_rele(ds->ds_dir, ds);
331fa9e4066Sahrens 
33291ebeef5Sahrens 	ASSERT(!list_link_active(&ds->ds_synced_link));
333fa9e4066Sahrens 
33403bad06fSJustin Gibbs 	list_destroy(&ds->ds_prop_cbs);
3355ad82045Snd 	mutex_destroy(&ds->ds_lock);
33691ebeef5Sahrens 	mutex_destroy(&ds->ds_opening_lock);
337d2b3cbbdSJorgen Lundman 	mutex_destroy(&ds->ds_sendstream_lock);
3385cabbc6bSPrashanth Sreenivasa 	mutex_destroy(&ds->ds_remap_deadlist_lock);
339e914ace2STim Schumacher 	zfs_refcount_destroy(&ds->ds_longholds);
340c166b69dSPaul Dagnelie 	rrw_destroy(&ds->ds_bp_rwlock);
3415ad82045Snd 
342fa9e4066Sahrens 	kmem_free(ds, sizeof (dsl_dataset_t));
343fa9e4066Sahrens }
344fa9e4066Sahrens 
3453b2aab18SMatthew Ahrens int
346fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds)
347fa9e4066Sahrens {
348fa9e4066Sahrens 	dsl_dataset_phys_t *headphys;
349fa9e4066Sahrens 	int err;
350fa9e4066Sahrens 	dmu_buf_t *headdbuf;
351fa9e4066Sahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
352fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
353fa9e4066Sahrens 
354fa9e4066Sahrens 	if (ds->ds_snapname[0])
355ea8dc4b6Seschrock 		return (0);
356c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
357ea8dc4b6Seschrock 		return (0);
358fa9e4066Sahrens 
359c1379625SJustin T. Gibbs 	err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
360ea8dc4b6Seschrock 	    FTAG, &headdbuf);
3613b2aab18SMatthew Ahrens 	if (err != 0)
362ea8dc4b6Seschrock 		return (err);
363fa9e4066Sahrens 	headphys = headdbuf->db_data;
364fa9e4066Sahrens 	err = zap_value_search(dp->dp_meta_objset,
365e7437265Sahrens 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
366ea8dc4b6Seschrock 	dmu_buf_rele(headdbuf, FTAG);
367ea8dc4b6Seschrock 	return (err);
368fa9e4066Sahrens }
369fa9e4066Sahrens 
3703b2aab18SMatthew Ahrens int
371745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
372ab04eb8eStimh {
373745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
374c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
3751c17160aSKevin Crowe 	matchtype_t mt = 0;
376ab04eb8eStimh 	int err;
377ab04eb8eStimh 
378c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
3791c17160aSKevin Crowe 		mt = MT_NORMALIZE;
380ab04eb8eStimh 
381745cd3c5Smaybee 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
382ab04eb8eStimh 	    value, mt, NULL, 0, NULL);
3831c17160aSKevin Crowe 	if (err == ENOTSUP && (mt & MT_NORMALIZE))
384745cd3c5Smaybee 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
385ab04eb8eStimh 	return (err);
386ab04eb8eStimh }
387ab04eb8eStimh 
3883b2aab18SMatthew Ahrens int
389a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
390a2afb611SJerry Jelinek     boolean_t adj_cnt)
391ab04eb8eStimh {
392745cd3c5Smaybee 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
393c1379625SJustin T. Gibbs 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
3941c17160aSKevin Crowe 	matchtype_t mt = 0;
395ab04eb8eStimh 	int err;
396ab04eb8eStimh 
39771eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
39871eb0538SChris Kirby 
399c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
4001c17160aSKevin Crowe 		mt = MT_NORMALIZE;
401ab04eb8eStimh 
402745cd3c5Smaybee 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
4031c17160aSKevin Crowe 	if (err == ENOTSUP && (mt & MT_NORMALIZE))
404745cd3c5Smaybee 		err = zap_remove(mos, snapobj, name, tx);
405a2afb611SJerry Jelinek 
406a2afb611SJerry Jelinek 	if (err == 0 && adj_cnt)
407a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
408a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
409a2afb611SJerry Jelinek 
410ab04eb8eStimh 	return (err);
411ab04eb8eStimh }
412ab04eb8eStimh 
413e57a022bSJustin T. Gibbs boolean_t
414e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
415e57a022bSJustin T. Gibbs {
4169d47dec0SJustin T. Gibbs 	dmu_buf_t *dbuf = ds->ds_dbuf;
4179d47dec0SJustin T. Gibbs 	boolean_t result = B_FALSE;
4189d47dec0SJustin T. Gibbs 
4199d47dec0SJustin T. Gibbs 	if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
4209d47dec0SJustin T. Gibbs 	    ds->ds_object, DMU_BONUS_BLKID, tag)) {
4219d47dec0SJustin T. Gibbs 
4229d47dec0SJustin T. Gibbs 		if (ds == dmu_buf_get_user(dbuf))
4239d47dec0SJustin T. Gibbs 			result = B_TRUE;
4249d47dec0SJustin T. Gibbs 		else
4259d47dec0SJustin T. Gibbs 			dmu_buf_rele(dbuf, tag);
4269d47dec0SJustin T. Gibbs 	}
4279d47dec0SJustin T. Gibbs 
4289d47dec0SJustin T. Gibbs 	return (result);
429e57a022bSJustin T. Gibbs }
430e57a022bSJustin T. Gibbs 
4313b2aab18SMatthew Ahrens int
4323b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
433745cd3c5Smaybee     dsl_dataset_t **dsp)
434fa9e4066Sahrens {
435fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
436fa9e4066Sahrens 	dmu_buf_t *dbuf;
437fa9e4066Sahrens 	dsl_dataset_t *ds;
438ea8dc4b6Seschrock 	int err;
439a7f53a56SChris Kirby 	dmu_object_info_t doi;
440fa9e4066Sahrens 
4413b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
442fa9e4066Sahrens 
443ea8dc4b6Seschrock 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
4443b2aab18SMatthew Ahrens 	if (err != 0)
445ea8dc4b6Seschrock 		return (err);
446a7f53a56SChris Kirby 
447a7f53a56SChris Kirby 	/* Make sure dsobj has the correct object type. */
448a7f53a56SChris Kirby 	dmu_object_info_from_db(dbuf, &doi);
4492acef22dSMatthew Ahrens 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
450b287be1bSWill Andrews 		dmu_buf_rele(dbuf, tag);
451be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
452b287be1bSWill Andrews 	}
453a7f53a56SChris Kirby 
454fa9e4066Sahrens 	ds = dmu_buf_get_user(dbuf);
455fa9e4066Sahrens 	if (ds == NULL) {
456d5285caeSGeorge Wilson 		dsl_dataset_t *winner = NULL;
457fa9e4066Sahrens 
458fa9e4066Sahrens 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
459fa9e4066Sahrens 		ds->ds_dbuf = dbuf;
460fa9e4066Sahrens 		ds->ds_object = dsobj;
461bc9014e6SJustin Gibbs 		ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
462fa9e4066Sahrens 
4635cabbc6bSPrashanth Sreenivasa 		err = dsl_dir_hold_obj(dp, dsl_dataset_phys(ds)->ds_dir_obj,
4645cabbc6bSPrashanth Sreenivasa 		    NULL, ds, &ds->ds_dir);
4655cabbc6bSPrashanth Sreenivasa 		if (err != 0) {
4665cabbc6bSPrashanth Sreenivasa 			kmem_free(ds, sizeof (dsl_dataset_t));
4675cabbc6bSPrashanth Sreenivasa 			dmu_buf_rele(dbuf, tag);
4685cabbc6bSPrashanth Sreenivasa 			return (err);
4695cabbc6bSPrashanth Sreenivasa 		}
4705cabbc6bSPrashanth Sreenivasa 
4715ad82045Snd 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
47291ebeef5Sahrens 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
4734e3c9f44SBill Pijewski 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
4745cabbc6bSPrashanth Sreenivasa 		mutex_init(&ds->ds_remap_deadlist_lock,
4755cabbc6bSPrashanth Sreenivasa 		    NULL, MUTEX_DEFAULT, NULL);
476c166b69dSPaul Dagnelie 		rrw_init(&ds->ds_bp_rwlock, B_FALSE);
477e914ace2STim Schumacher 		zfs_refcount_create(&ds->ds_longholds);
4785ad82045Snd 
479cde58dbcSMatthew Ahrens 		bplist_create(&ds->ds_pending_deadlist);
480cde58dbcSMatthew Ahrens 
4814e3c9f44SBill Pijewski 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4824e3c9f44SBill Pijewski 		    offsetof(dmu_sendarg_t, dsa_link));
4834e3c9f44SBill Pijewski 
48403bad06fSJustin Gibbs 		list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
48503bad06fSJustin Gibbs 		    offsetof(dsl_prop_cb_record_t, cbr_ds_node));
48603bad06fSJustin Gibbs 
487b5152584SMatthew Ahrens 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
488ca0cc391SMatthew Ahrens 			for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
489ca0cc391SMatthew Ahrens 				if (!(spa_feature_table[f].fi_flags &
490ca0cc391SMatthew Ahrens 				    ZFEATURE_FLAG_PER_DATASET))
491ca0cc391SMatthew Ahrens 					continue;
492ca0cc391SMatthew Ahrens 				err = zap_contains(mos, dsobj,
493ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_guid);
494ca0cc391SMatthew Ahrens 				if (err == 0) {
495ca0cc391SMatthew Ahrens 					ds->ds_feature_inuse[f] = B_TRUE;
496ca0cc391SMatthew Ahrens 				} else {
497ca0cc391SMatthew Ahrens 					ASSERT3U(err, ==, ENOENT);
498ca0cc391SMatthew Ahrens 					err = 0;
499ca0cc391SMatthew Ahrens 				}
500e1f3c208SJustin T. Gibbs 			}
501b5152584SMatthew Ahrens 		}
502b5152584SMatthew Ahrens 
503bc9014e6SJustin Gibbs 		if (!ds->ds_is_snapshot) {
504fa9e4066Sahrens 			ds->ds_snapname[0] = '\0';
505c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
5063b2aab18SMatthew Ahrens 				err = dsl_dataset_hold_obj(dp,
507c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_prev_snap_obj,
508745cd3c5Smaybee 				    ds, &ds->ds_prev);
509fa9e4066Sahrens 			}
51078f17100SMatthew Ahrens 			if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
51178f17100SMatthew Ahrens 				int zaperr = zap_lookup(mos, ds->ds_object,
51278f17100SMatthew Ahrens 				    DS_FIELD_BOOKMARK_NAMES,
51378f17100SMatthew Ahrens 				    sizeof (ds->ds_bookmarks), 1,
51478f17100SMatthew Ahrens 				    &ds->ds_bookmarks);
51578f17100SMatthew Ahrens 				if (zaperr != ENOENT)
51678f17100SMatthew Ahrens 					VERIFY0(zaperr);
51778f17100SMatthew Ahrens 			}
518842727c2SChris Kirby 		} else {
519842727c2SChris Kirby 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
520842727c2SChris Kirby 				err = dsl_dataset_get_snapname(ds);
521c1379625SJustin T. Gibbs 			if (err == 0 &&
522c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
523842727c2SChris Kirby 				err = zap_count(
524842727c2SChris Kirby 				    ds->ds_dir->dd_pool->dp_meta_objset,
525c1379625SJustin T. Gibbs 				    dsl_dataset_phys(ds)->ds_userrefs_obj,
526842727c2SChris Kirby 				    &ds->ds_userrefs);
527842727c2SChris Kirby 			}
528fa9e4066Sahrens 		}
529fa9e4066Sahrens 
530bc9014e6SJustin Gibbs 		if (err == 0 && !ds->ds_is_snapshot) {
5313b2aab18SMatthew Ahrens 			err = dsl_prop_get_int_ds(ds,
5323b2aab18SMatthew Ahrens 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
5333b2aab18SMatthew Ahrens 			    &ds->ds_reserved);
534cb625fb5Sck 			if (err == 0) {
5353b2aab18SMatthew Ahrens 				err = dsl_prop_get_int_ds(ds,
5363b2aab18SMatthew Ahrens 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
5373b2aab18SMatthew Ahrens 				    &ds->ds_quota);
538cb625fb5Sck 			}
539cb625fb5Sck 		} else {
540cb625fb5Sck 			ds->ds_reserved = ds->ds_quota = 0;
541cb625fb5Sck 		}
542cb625fb5Sck 
543*eb633035STom Caputi 		if (err == 0 && ds->ds_dir->dd_crypto_obj != 0 &&
544*eb633035STom Caputi 		    ds->ds_is_snapshot &&
545*eb633035STom Caputi 		    zap_contains(mos, dsobj, DS_FIELD_IVSET_GUID) != 0) {
546*eb633035STom Caputi 			dp->dp_spa->spa_errata =
547*eb633035STom Caputi 			    ZPOOL_ERRATA_ZOL_8308_ENCRYPTION;
548*eb633035STom Caputi 		}
549*eb633035STom Caputi 
5505cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_open(&ds->ds_deadlist,
5515cabbc6bSPrashanth Sreenivasa 		    mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
5525cabbc6bSPrashanth Sreenivasa 		uint64_t remap_deadlist_obj =
5535cabbc6bSPrashanth Sreenivasa 		    dsl_dataset_get_remap_deadlist_object(ds);
5545cabbc6bSPrashanth Sreenivasa 		if (remap_deadlist_obj != 0) {
5555cabbc6bSPrashanth Sreenivasa 			dsl_deadlist_open(&ds->ds_remap_deadlist, mos,
5565cabbc6bSPrashanth Sreenivasa 			    remap_deadlist_obj);
5575cabbc6bSPrashanth Sreenivasa 		}
5585cabbc6bSPrashanth Sreenivasa 
55940510e8eSJosef 'Jeff' Sipek 		dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
56040510e8eSJosef 'Jeff' Sipek 		    dsl_dataset_evict_async, &ds->ds_dbuf);
561bc9014e6SJustin Gibbs 		if (err == 0)
562bc9014e6SJustin Gibbs 			winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
563bc9014e6SJustin Gibbs 
564bc9014e6SJustin Gibbs 		if (err != 0 || winner != NULL) {
565cde58dbcSMatthew Ahrens 			bplist_destroy(&ds->ds_pending_deadlist);
566cde58dbcSMatthew Ahrens 			dsl_deadlist_close(&ds->ds_deadlist);
5675cabbc6bSPrashanth Sreenivasa 			if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
5685cabbc6bSPrashanth Sreenivasa 				dsl_deadlist_close(&ds->ds_remap_deadlist);
569745cd3c5Smaybee 			if (ds->ds_prev)
5703b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds->ds_prev, ds);
5713b2aab18SMatthew Ahrens 			dsl_dir_rele(ds->ds_dir, ds);
572ad027c0fSJorgen Lundman 			list_destroy(&ds->ds_prop_cbs);
573ad027c0fSJorgen Lundman 			list_destroy(&ds->ds_sendstreams);
5745ad82045Snd 			mutex_destroy(&ds->ds_lock);
57591ebeef5Sahrens 			mutex_destroy(&ds->ds_opening_lock);
576d2b3cbbdSJorgen Lundman 			mutex_destroy(&ds->ds_sendstream_lock);
577ad027c0fSJorgen Lundman 			mutex_destroy(&ds->ds_remap_deadlist_lock);
578e914ace2STim Schumacher 			zfs_refcount_destroy(&ds->ds_longholds);
579ad027c0fSJorgen Lundman 			rrw_destroy(&ds->ds_bp_rwlock);
580fa9e4066Sahrens 			kmem_free(ds, sizeof (dsl_dataset_t));
5813b2aab18SMatthew Ahrens 			if (err != 0) {
582ea8dc4b6Seschrock 				dmu_buf_rele(dbuf, tag);
583ea8dc4b6Seschrock 				return (err);
584ea8dc4b6Seschrock 			}
585fa9e4066Sahrens 			ds = winner;
586fa9e4066Sahrens 		} else {
58791ebeef5Sahrens 			ds->ds_fsid_guid =
588c1379625SJustin T. Gibbs 			    unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
58940510e8eSJosef 'Jeff' Sipek 			if (ds->ds_fsid_guid !=
59040510e8eSJosef 'Jeff' Sipek 			    dsl_dataset_phys(ds)->ds_fsid_guid) {
59140510e8eSJosef 'Jeff' Sipek 				zfs_dbgmsg("ds_fsid_guid changed from "
59240510e8eSJosef 'Jeff' Sipek 				    "%llx to %llx for pool %s dataset id %llu",
59340510e8eSJosef 'Jeff' Sipek 				    (long long)
59440510e8eSJosef 'Jeff' Sipek 				    dsl_dataset_phys(ds)->ds_fsid_guid,
59540510e8eSJosef 'Jeff' Sipek 				    (long long)ds->ds_fsid_guid,
59640510e8eSJosef 'Jeff' Sipek 				    spa_name(dp->dp_spa),
59740510e8eSJosef 'Jeff' Sipek 				    dsobj);
59840510e8eSJosef 'Jeff' Sipek 			}
599fa9e4066Sahrens 		}
600fa9e4066Sahrens 	}
601*eb633035STom Caputi 
602fa9e4066Sahrens 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
603c1379625SJustin T. Gibbs 	ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
604c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
605afc6333aSahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
60684db2a68Sahrens 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
607ea8dc4b6Seschrock 	*dsp = ds;
608*eb633035STom Caputi 
609ea8dc4b6Seschrock 	return (0);
610fa9e4066Sahrens }
611fa9e4066Sahrens 
612745cd3c5Smaybee int
613*eb633035STom Caputi dsl_dataset_create_key_mapping(dsl_dataset_t *ds)
614*eb633035STom Caputi {
615*eb633035STom Caputi 	dsl_dir_t *dd = ds->ds_dir;
616*eb633035STom Caputi 
617*eb633035STom Caputi 	if (dd->dd_crypto_obj == 0)
618*eb633035STom Caputi 		return (0);
619*eb633035STom Caputi 
620*eb633035STom Caputi 	return (spa_keystore_create_mapping(dd->dd_pool->dp_spa,
621*eb633035STom Caputi 	    ds, ds, &ds->ds_key_mapping));
622*eb633035STom Caputi }
623*eb633035STom Caputi 
624*eb633035STom Caputi int
625*eb633035STom Caputi dsl_dataset_hold_obj_flags(dsl_pool_t *dp, uint64_t dsobj,
626*eb633035STom Caputi     ds_hold_flags_t flags, void *tag, dsl_dataset_t **dsp)
627*eb633035STom Caputi {
628*eb633035STom Caputi 	int err;
629*eb633035STom Caputi 
630*eb633035STom Caputi 	err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
631*eb633035STom Caputi 	if (err != 0)
632*eb633035STom Caputi 		return (err);
633*eb633035STom Caputi 
634*eb633035STom Caputi 	ASSERT3P(*dsp, !=, NULL);
635*eb633035STom Caputi 
636*eb633035STom Caputi 	if (flags & DS_HOLD_FLAG_DECRYPT) {
637*eb633035STom Caputi 		err = dsl_dataset_create_key_mapping(*dsp);
638*eb633035STom Caputi 		if (err != 0)
639*eb633035STom Caputi 			dsl_dataset_rele(*dsp, tag);
640*eb633035STom Caputi 	}
641*eb633035STom Caputi 
642*eb633035STom Caputi 	return (err);
643*eb633035STom Caputi }
644*eb633035STom Caputi 
645*eb633035STom Caputi int
646*eb633035STom Caputi dsl_dataset_hold_flags(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
647503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
648fa9e4066Sahrens {
649fa9e4066Sahrens 	dsl_dir_t *dd;
650745cd3c5Smaybee 	const char *snapname;
651fa9e4066Sahrens 	uint64_t obj;
652fa9e4066Sahrens 	int err = 0;
653a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds;
654fa9e4066Sahrens 
6553b2aab18SMatthew Ahrens 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
6563b2aab18SMatthew Ahrens 	if (err != 0)
657ea8dc4b6Seschrock 		return (err);
658fa9e4066Sahrens 
6593b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
660c1379625SJustin T. Gibbs 	obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
6613b2aab18SMatthew Ahrens 	if (obj != 0)
662*eb633035STom Caputi 		err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag, &ds);
663745cd3c5Smaybee 	else
664be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
665fa9e4066Sahrens 
666745cd3c5Smaybee 	/* we may be looking for a snapshot */
667745cd3c5Smaybee 	if (err == 0 && snapname != NULL) {
668a2cdcdd2SPaul Dagnelie 		dsl_dataset_t *snap_ds;
669fa9e4066Sahrens 
670745cd3c5Smaybee 		if (*snapname++ != '@') {
671*eb633035STom Caputi 			dsl_dataset_rele_flags(ds, flags, tag);
6723b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
673be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
674fa9e4066Sahrens 		}
675fa9e4066Sahrens 
676745cd3c5Smaybee 		dprintf("looking for snapshot '%s'\n", snapname);
677a2cdcdd2SPaul Dagnelie 		err = dsl_dataset_snap_lookup(ds, snapname, &obj);
678*eb633035STom Caputi 		if (err == 0) {
679*eb633035STom Caputi 			err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag,
680*eb633035STom Caputi 			    &snap_ds);
681*eb633035STom Caputi 		}
682*eb633035STom Caputi 		dsl_dataset_rele_flags(ds, flags, tag);
683745cd3c5Smaybee 
6843b2aab18SMatthew Ahrens 		if (err == 0) {
685a2cdcdd2SPaul Dagnelie 			mutex_enter(&snap_ds->ds_lock);
686a2cdcdd2SPaul Dagnelie 			if (snap_ds->ds_snapname[0] == 0)
687a2cdcdd2SPaul Dagnelie 				(void) strlcpy(snap_ds->ds_snapname, snapname,
688a2cdcdd2SPaul Dagnelie 				    sizeof (snap_ds->ds_snapname));
689a2cdcdd2SPaul Dagnelie 			mutex_exit(&snap_ds->ds_lock);
690a2cdcdd2SPaul Dagnelie 			ds = snap_ds;
691fa9e4066Sahrens 		}
692fa9e4066Sahrens 	}
693a2cdcdd2SPaul Dagnelie 	if (err == 0)
694a2cdcdd2SPaul Dagnelie 		*dsp = ds;
6953b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
696fa9e4066Sahrens 	return (err);
697fa9e4066Sahrens }
698fa9e4066Sahrens 
699fa9e4066Sahrens int
700*eb633035STom Caputi dsl_dataset_hold(dsl_pool_t *dp, const char *name, void *tag,
701*eb633035STom Caputi     dsl_dataset_t **dsp)
702*eb633035STom Caputi {
703*eb633035STom Caputi 	return (dsl_dataset_hold_flags(dp, name, 0, tag, dsp));
704*eb633035STom Caputi }
705*eb633035STom Caputi 
706*eb633035STom Caputi int
707*eb633035STom Caputi dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags,
7083b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
7093b2aab18SMatthew Ahrens {
710*eb633035STom Caputi 	int err = dsl_dataset_hold_obj_flags(dp, dsobj, flags, tag, dsp);
7113b2aab18SMatthew Ahrens 	if (err != 0)
7123b2aab18SMatthew Ahrens 		return (err);
7133b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
714*eb633035STom Caputi 		dsl_dataset_rele_flags(*dsp, flags, tag);
7153b2aab18SMatthew Ahrens 		*dsp = NULL;
716be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
7173b2aab18SMatthew Ahrens 	}
7183b2aab18SMatthew Ahrens 	return (0);
7193b2aab18SMatthew Ahrens }
7203b2aab18SMatthew Ahrens 
7213b2aab18SMatthew Ahrens int
722*eb633035STom Caputi dsl_dataset_own(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
723503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp)
724fa9e4066Sahrens {
725*eb633035STom Caputi 	int err = dsl_dataset_hold_flags(dp, name, flags, tag, dsp);
7263b2aab18SMatthew Ahrens 	if (err != 0)
727745cd3c5Smaybee 		return (err);
7283b2aab18SMatthew Ahrens 	if (!dsl_dataset_tryown(*dsp, tag)) {
729*eb633035STom Caputi 		dsl_dataset_rele_flags(*dsp, flags, tag);
730be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
731745cd3c5Smaybee 	}
732745cd3c5Smaybee 	return (0);
733fa9e4066Sahrens }
734fa9e4066Sahrens 
7353b2aab18SMatthew Ahrens /*
7363b2aab18SMatthew Ahrens  * See the comment above dsl_pool_hold() for details.  In summary, a long
7373b2aab18SMatthew Ahrens  * hold is used to prevent destruction of a dataset while the pool hold
7383b2aab18SMatthew Ahrens  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
7393b2aab18SMatthew Ahrens  *
7403b2aab18SMatthew Ahrens  * The dataset and pool must be held when this function is called.  After it
7413b2aab18SMatthew Ahrens  * is called, the pool hold may be released while the dataset is still held
7423b2aab18SMatthew Ahrens  * and accessed.
7433b2aab18SMatthew Ahrens  */
7443b2aab18SMatthew Ahrens void
7453b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
7463b2aab18SMatthew Ahrens {
7473b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
748e914ace2STim Schumacher 	(void) zfs_refcount_add(&ds->ds_longholds, tag);
7493b2aab18SMatthew Ahrens }
7503b2aab18SMatthew Ahrens 
7513b2aab18SMatthew Ahrens void
7523b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
7533b2aab18SMatthew Ahrens {
754e914ace2STim Schumacher 	(void) zfs_refcount_remove(&ds->ds_longholds, tag);
7553b2aab18SMatthew Ahrens }
7563b2aab18SMatthew Ahrens 
7573b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */
7583b2aab18SMatthew Ahrens boolean_t
7593b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds)
7603b2aab18SMatthew Ahrens {
761e914ace2STim Schumacher 	return (!zfs_refcount_is_zero(&ds->ds_longholds));
7623b2aab18SMatthew Ahrens }
7633b2aab18SMatthew Ahrens 
764fa9e4066Sahrens void
765fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name)
766fa9e4066Sahrens {
767fa9e4066Sahrens 	if (ds == NULL) {
768fa9e4066Sahrens 		(void) strcpy(name, "mos");
769fa9e4066Sahrens 	} else {
770fa9e4066Sahrens 		dsl_dir_name(ds->ds_dir, name);
7713b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
772fa9e4066Sahrens 		if (ds->ds_snapname[0]) {
7739adfa60dSMatthew Ahrens 			VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
7749adfa60dSMatthew Ahrens 			    <, ZFS_MAX_DATASET_NAME_LEN);
775745cd3c5Smaybee 			/*
776745cd3c5Smaybee 			 * We use a "recursive" mutex so that we
777745cd3c5Smaybee 			 * can call dprintf_ds() with ds_lock held.
778745cd3c5Smaybee 			 */
779fa9e4066Sahrens 			if (!MUTEX_HELD(&ds->ds_lock)) {
780fa9e4066Sahrens 				mutex_enter(&ds->ds_lock);
7819adfa60dSMatthew Ahrens 				VERIFY3U(strlcat(name, ds->ds_snapname,
7829adfa60dSMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN), <,
7839adfa60dSMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN);
784fa9e4066Sahrens 				mutex_exit(&ds->ds_lock);
785fa9e4066Sahrens 			} else {
7869adfa60dSMatthew Ahrens 				VERIFY3U(strlcat(name, ds->ds_snapname,
7879adfa60dSMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN), <,
7889adfa60dSMatthew Ahrens 				    ZFS_MAX_DATASET_NAME_LEN);
789fa9e4066Sahrens 			}
790fa9e4066Sahrens 		}
791fa9e4066Sahrens 	}
792fa9e4066Sahrens }
793fa9e4066Sahrens 
7949adfa60dSMatthew Ahrens int
7959adfa60dSMatthew Ahrens dsl_dataset_namelen(dsl_dataset_t *ds)
7969adfa60dSMatthew Ahrens {
7979adfa60dSMatthew Ahrens 	VERIFY0(dsl_dataset_get_snapname(ds));
7989adfa60dSMatthew Ahrens 	mutex_enter(&ds->ds_lock);
7999adfa60dSMatthew Ahrens 	int len = dsl_dir_namelen(ds->ds_dir) + 1 + strlen(ds->ds_snapname);
8009adfa60dSMatthew Ahrens 	mutex_exit(&ds->ds_lock);
8019adfa60dSMatthew Ahrens 	return (len);
8029adfa60dSMatthew Ahrens }
8039adfa60dSMatthew Ahrens 
8043cb34c60Sahrens void
805745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
8063cb34c60Sahrens {
8073b2aab18SMatthew Ahrens 	dmu_buf_rele(ds->ds_dbuf, tag);
808745cd3c5Smaybee }
809745cd3c5Smaybee 
810745cd3c5Smaybee void
811*eb633035STom Caputi dsl_dataset_remove_key_mapping(dsl_dataset_t *ds)
812*eb633035STom Caputi {
813*eb633035STom Caputi 	dsl_dir_t *dd = ds->ds_dir;
814*eb633035STom Caputi 
815*eb633035STom Caputi 	if (dd == NULL || dd->dd_crypto_obj == 0)
816*eb633035STom Caputi 		return;
817*eb633035STom Caputi 
818*eb633035STom Caputi 	(void) spa_keystore_remove_mapping(dd->dd_pool->dp_spa,
819*eb633035STom Caputi 	    ds->ds_object, ds);
820*eb633035STom Caputi }
821*eb633035STom Caputi 
822*eb633035STom Caputi void
823*eb633035STom Caputi dsl_dataset_rele_flags(dsl_dataset_t *ds, ds_hold_flags_t flags, void *tag)
824*eb633035STom Caputi {
825*eb633035STom Caputi 	if (flags & DS_HOLD_FLAG_DECRYPT)
826*eb633035STom Caputi 		dsl_dataset_remove_key_mapping(ds);
827*eb633035STom Caputi 
828*eb633035STom Caputi 	dsl_dataset_rele(ds, tag);
829*eb633035STom Caputi }
830*eb633035STom Caputi 
831*eb633035STom Caputi void
832*eb633035STom Caputi dsl_dataset_disown(dsl_dataset_t *ds, ds_hold_flags_t flags, void *tag)
833745cd3c5Smaybee {
834d808a4fcSJustin T. Gibbs 	ASSERT3P(ds->ds_owner, ==, tag);
835d808a4fcSJustin T. Gibbs 	ASSERT(ds->ds_dbuf != NULL);
836745cd3c5Smaybee 
8373cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
838745cd3c5Smaybee 	ds->ds_owner = NULL;
8393cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
8403b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, tag);
841*eb633035STom Caputi 	dsl_dataset_rele_flags(ds, flags, tag);
8423cb34c60Sahrens }
8433cb34c60Sahrens 
8443cb34c60Sahrens boolean_t
8453b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
8463cb34c60Sahrens {
847745cd3c5Smaybee 	boolean_t gotit = FALSE;
848745cd3c5Smaybee 
8499c3fd121SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
8503cb34c60Sahrens 	mutex_enter(&ds->ds_lock);
8513b2aab18SMatthew Ahrens 	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
852503ad85cSMatthew Ahrens 		ds->ds_owner = tag;
8533b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(ds, tag);
854745cd3c5Smaybee 		gotit = TRUE;
8553cb34c60Sahrens 	}
8563cb34c60Sahrens 	mutex_exit(&ds->ds_lock);
857745cd3c5Smaybee 	return (gotit);
858745cd3c5Smaybee }
859745cd3c5Smaybee 
8609c3fd121SMatthew Ahrens boolean_t
8619c3fd121SMatthew Ahrens dsl_dataset_has_owner(dsl_dataset_t *ds)
8629c3fd121SMatthew Ahrens {
8639c3fd121SMatthew Ahrens 	boolean_t rv;
8649c3fd121SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
8659c3fd121SMatthew Ahrens 	rv = (ds->ds_owner != NULL);
8669c3fd121SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
8679c3fd121SMatthew Ahrens 	return (rv);
8689c3fd121SMatthew Ahrens }
8699c3fd121SMatthew Ahrens 
870*eb633035STom Caputi void
871ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
872ca0cc391SMatthew Ahrens {
873ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
874ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
875ca0cc391SMatthew Ahrens 	uint64_t zero = 0;
876ca0cc391SMatthew Ahrens 
877ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
878ca0cc391SMatthew Ahrens 
879ca0cc391SMatthew Ahrens 	spa_feature_incr(spa, f, tx);
880ca0cc391SMatthew Ahrens 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
881ca0cc391SMatthew Ahrens 
882ca0cc391SMatthew Ahrens 	VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
883ca0cc391SMatthew Ahrens 	    sizeof (zero), 1, &zero, tx));
884ca0cc391SMatthew Ahrens }
885ca0cc391SMatthew Ahrens 
886ca0cc391SMatthew Ahrens void
887ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx)
888ca0cc391SMatthew Ahrens {
889ca0cc391SMatthew Ahrens 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
890ca0cc391SMatthew Ahrens 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
891ca0cc391SMatthew Ahrens 
892ca0cc391SMatthew Ahrens 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
893ca0cc391SMatthew Ahrens 
894ca0cc391SMatthew Ahrens 	VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
895ca0cc391SMatthew Ahrens 	spa_feature_decr(spa, f, tx);
896ca0cc391SMatthew Ahrens }
897ca0cc391SMatthew Ahrens 
8981d452cf5Sahrens uint64_t
899088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
900*eb633035STom Caputi     dsl_crypto_params_t *dcp, uint64_t flags, dmu_tx_t *tx)
901fa9e4066Sahrens {
9023cb34c60Sahrens 	dsl_pool_t *dp = dd->dd_pool;
903fa9e4066Sahrens 	dmu_buf_t *dbuf;
904fa9e4066Sahrens 	dsl_dataset_phys_t *dsphys;
9053cb34c60Sahrens 	uint64_t dsobj;
906fa9e4066Sahrens 	objset_t *mos = dp->dp_meta_objset;
907fa9e4066Sahrens 
908088f3894Sahrens 	if (origin == NULL)
909088f3894Sahrens 		origin = dp->dp_origin_snap;
910088f3894Sahrens 
9113cb34c60Sahrens 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
912c1379625SJustin T. Gibbs 	ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
913fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
914c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
915fa9e4066Sahrens 
9161649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
9171649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
9183b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
919fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
920fa9e4066Sahrens 	dsphys = dbuf->db_data;
921745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
922fa9e4066Sahrens 	dsphys->ds_dir_obj = dd->dd_object;
923ab04eb8eStimh 	dsphys->ds_flags = flags;
924fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
925fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
926fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
927fa9e4066Sahrens 	dsphys->ds_snapnames_zapobj =
928ab04eb8eStimh 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
929ab04eb8eStimh 	    DMU_OT_NONE, 0, tx);
930fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
931088f3894Sahrens 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
932a9799022Sck 
933cde58dbcSMatthew Ahrens 	if (origin == NULL) {
934cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
935cde58dbcSMatthew Ahrens 	} else {
9363b2aab18SMatthew Ahrens 		dsl_dataset_t *ohds; /* head of the origin snapshot */
937cde58dbcSMatthew Ahrens 
9383cb34c60Sahrens 		dsphys->ds_prev_snap_obj = origin->ds_object;
939fa9e4066Sahrens 		dsphys->ds_prev_snap_txg =
940c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_creation_txg;
941ad135b5dSChristopher Siden 		dsphys->ds_referenced_bytes =
942c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_referenced_bytes;
943fa9e4066Sahrens 		dsphys->ds_compressed_bytes =
944c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_compressed_bytes;
945fa9e4066Sahrens 		dsphys->ds_uncompressed_bytes =
946c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_uncompressed_bytes;
947c166b69dSPaul Dagnelie 		rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
948c1379625SJustin T. Gibbs 		dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
949c166b69dSPaul Dagnelie 		rrw_exit(&origin->ds_bp_rwlock, FTAG);
95042fcb65eSMatthew Ahrens 
95142fcb65eSMatthew Ahrens 		/*
95242fcb65eSMatthew Ahrens 		 * Inherit flags that describe the dataset's contents
95342fcb65eSMatthew Ahrens 		 * (INCONSISTENT) or properties (Case Insensitive).
95442fcb65eSMatthew Ahrens 		 */
955c1379625SJustin T. Gibbs 		dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
95642fcb65eSMatthew Ahrens 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
957fa9e4066Sahrens 
958ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
959ca0cc391SMatthew Ahrens 			if (origin->ds_feature_inuse[f])
960ca0cc391SMatthew Ahrens 				dsl_dataset_activate_feature(dsobj, f, tx);
961ca0cc391SMatthew Ahrens 		}
962b5152584SMatthew Ahrens 
9633cb34c60Sahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
964c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin)->ds_num_children++;
965fa9e4066Sahrens 
9663b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
967c1379625SJustin T. Gibbs 		    dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
968c1379625SJustin T. Gibbs 		    FTAG, &ohds));
969cde58dbcSMatthew Ahrens 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
970cde58dbcSMatthew Ahrens 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
971cde58dbcSMatthew Ahrens 		dsl_dataset_rele(ohds, FTAG);
972cde58dbcSMatthew Ahrens 
973088f3894Sahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
974c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
975c1379625SJustin T. Gibbs 				dsl_dataset_phys(origin)->ds_next_clones_obj =
976088f3894Sahrens 				    zap_create(mos,
977088f3894Sahrens 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
978088f3894Sahrens 			}
9793b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
980c1379625SJustin T. Gibbs 			    dsl_dataset_phys(origin)->ds_next_clones_obj,
981c1379625SJustin T. Gibbs 			    dsobj, tx));
982088f3894Sahrens 		}
983088f3894Sahrens 
984fa9e4066Sahrens 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
985c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
986cde58dbcSMatthew Ahrens 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
987c1379625SJustin T. Gibbs 			if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
988cde58dbcSMatthew Ahrens 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
989c1379625SJustin T. Gibbs 				dsl_dir_phys(origin->ds_dir)->dd_clones =
990cde58dbcSMatthew Ahrens 				    zap_create(mos,
991cde58dbcSMatthew Ahrens 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
992cde58dbcSMatthew Ahrens 			}
9933b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
994c1379625SJustin T. Gibbs 			    dsl_dir_phys(origin->ds_dir)->dd_clones,
995c1379625SJustin T. Gibbs 			    dsobj, tx));
996cde58dbcSMatthew Ahrens 		}
997fa9e4066Sahrens 	}
998ab04eb8eStimh 
999*eb633035STom Caputi 	/* handle encryption */
1000*eb633035STom Caputi 	dsl_dataset_create_crypt_sync(dsobj, dd, origin, dcp, tx);
1001*eb633035STom Caputi 
1002ab04eb8eStimh 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1003ab04eb8eStimh 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1004ab04eb8eStimh 
1005ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1006fa9e4066Sahrens 
1007fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1008c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
10093cb34c60Sahrens 
10103cb34c60Sahrens 	return (dsobj);
10113cb34c60Sahrens }
10123cb34c60Sahrens 
10133b2aab18SMatthew Ahrens static void
10143b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
10153b2aab18SMatthew Ahrens {
10163b2aab18SMatthew Ahrens 	objset_t *os;
10173b2aab18SMatthew Ahrens 
10183b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(ds, &os));
1019bfaed0b9SAndriy Gapon 	if (bcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) {
1020bfaed0b9SAndriy Gapon 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
1021bfaed0b9SAndriy Gapon 		zio_t *zio;
1022bfaed0b9SAndriy Gapon 
1023bfaed0b9SAndriy Gapon 		bzero(&os->os_zil_header, sizeof (os->os_zil_header));
1024*eb633035STom Caputi 		if (os->os_encrypted)
1025*eb633035STom Caputi 			os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
1026bfaed0b9SAndriy Gapon 
1027bfaed0b9SAndriy Gapon 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1028bfaed0b9SAndriy Gapon 		dsl_dataset_sync(ds, zio, tx);
1029bfaed0b9SAndriy Gapon 		VERIFY0(zio_wait(zio));
1030bfaed0b9SAndriy Gapon 
1031bfaed0b9SAndriy Gapon 		/* dsl_dataset_sync_done will drop this reference. */
1032bfaed0b9SAndriy Gapon 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1033bfaed0b9SAndriy Gapon 		dsl_dataset_sync_done(ds, tx);
1034bfaed0b9SAndriy Gapon 	}
10353b2aab18SMatthew Ahrens }
10363b2aab18SMatthew Ahrens 
10373cb34c60Sahrens uint64_t
1038ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
1039*eb633035STom Caputi     dsl_dataset_t *origin, uint64_t flags, cred_t *cr,
1040*eb633035STom Caputi     dsl_crypto_params_t *dcp, dmu_tx_t *tx)
10413cb34c60Sahrens {
10423cb34c60Sahrens 	dsl_pool_t *dp = pdd->dd_pool;
10433cb34c60Sahrens 	uint64_t dsobj, ddobj;
10443cb34c60Sahrens 	dsl_dir_t *dd;
10453cb34c60Sahrens 
10463b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
10473cb34c60Sahrens 	ASSERT(lastname[0] != '@');
10483cb34c60Sahrens 
1049088f3894Sahrens 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
10503b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
10513cb34c60Sahrens 
1052*eb633035STom Caputi 	dsobj = dsl_dataset_create_sync_dd(dd, origin, dcp,
10533b2aab18SMatthew Ahrens 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
10543cb34c60Sahrens 
10553cb34c60Sahrens 	dsl_deleg_set_create_perms(dd, tx, cr);
10563cb34c60Sahrens 
1057a2afb611SJerry Jelinek 	/*
1058a2afb611SJerry Jelinek 	 * Since we're creating a new node we know it's a leaf, so we can
1059a2afb611SJerry Jelinek 	 * initialize the counts if the limit feature is active.
1060a2afb611SJerry Jelinek 	 */
1061a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
1062a2afb611SJerry Jelinek 		uint64_t cnt = 0;
1063a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1064a2afb611SJerry Jelinek 
1065a2afb611SJerry Jelinek 		dsl_dir_zapify(dd, tx);
1066a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1067a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
1068a2afb611SJerry Jelinek 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1069a2afb611SJerry Jelinek 		    sizeof (cnt), 1, &cnt, tx));
1070a2afb611SJerry Jelinek 	}
1071a2afb611SJerry Jelinek 
10723b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
1073fa9e4066Sahrens 
1074feaa74e4SMark Maybee 	/*
1075feaa74e4SMark Maybee 	 * If we are creating a clone, make sure we zero out any stale
1076feaa74e4SMark Maybee 	 * data from the origin snapshots zil header.
1077feaa74e4SMark Maybee 	 */
10783b2aab18SMatthew Ahrens 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
1079feaa74e4SMark Maybee 		dsl_dataset_t *ds;
1080feaa74e4SMark Maybee 
10813b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
10823b2aab18SMatthew Ahrens 		dsl_dataset_zero_zil(ds, tx);
1083feaa74e4SMark Maybee 		dsl_dataset_rele(ds, FTAG);
1084feaa74e4SMark Maybee 	}
1085feaa74e4SMark Maybee 
10861d452cf5Sahrens 	return (dsobj);
1087fa9e4066Sahrens }
1088fa9e4066Sahrens 
10891d452cf5Sahrens /*
10903b2aab18SMatthew Ahrens  * The unique space in the head dataset can be calculated by subtracting
10913b2aab18SMatthew Ahrens  * the space used in the most recent snapshot, that is still being used
10923b2aab18SMatthew Ahrens  * in this file system, from the space currently in use.  To figure out
10933b2aab18SMatthew Ahrens  * the space in the most recent snapshot still in use, we need to take
10943b2aab18SMatthew Ahrens  * the total space used in the snapshot and subtract out the space that
10953b2aab18SMatthew Ahrens  * has been freed up since the snapshot was taken.
10961d452cf5Sahrens  */
10973b2aab18SMatthew Ahrens void
10983b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
10991d452cf5Sahrens {
11003b2aab18SMatthew Ahrens 	uint64_t mrs_used;
11013b2aab18SMatthew Ahrens 	uint64_t dlused, dlcomp, dluncomp;
11021d452cf5Sahrens 
1103bc9014e6SJustin Gibbs 	ASSERT(!ds->ds_is_snapshot);
11041d452cf5Sahrens 
1105c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
1106c1379625SJustin T. Gibbs 		mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
11073b2aab18SMatthew Ahrens 	else
11083b2aab18SMatthew Ahrens 		mrs_used = 0;
1109842727c2SChris Kirby 
11103b2aab18SMatthew Ahrens 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1111fa9e4066Sahrens 
11123b2aab18SMatthew Ahrens 	ASSERT3U(dlused, <=, mrs_used);
1113c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes =
1114c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
111519b94df9SMatthew Ahrens 
11163b2aab18SMatthew Ahrens 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
11173b2aab18SMatthew Ahrens 	    SPA_VERSION_UNIQUE_ACCURATE)
1118c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1119fa9e4066Sahrens }
1120fa9e4066Sahrens 
11213b2aab18SMatthew Ahrens void
11223b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
11233b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1124842727c2SChris Kirby {
11253b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
11263b2aab18SMatthew Ahrens 	uint64_t count;
11273b2aab18SMatthew Ahrens 	int err;
11283b2aab18SMatthew Ahrens 
1129c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
1130c1379625SJustin T. Gibbs 	err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1131c1379625SJustin T. Gibbs 	    obj, tx);
11323b2aab18SMatthew Ahrens 	/*
11333b2aab18SMatthew Ahrens 	 * The err should not be ENOENT, but a bug in a previous version
11343b2aab18SMatthew Ahrens 	 * of the code could cause upgrade_clones_cb() to not set
11353b2aab18SMatthew Ahrens 	 * ds_next_snap_obj when it should, leading to a missing entry.
11363b2aab18SMatthew Ahrens 	 * If we knew that the pool was created after
11373b2aab18SMatthew Ahrens 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
11383b2aab18SMatthew Ahrens 	 * ENOENT.  However, at least we can check that we don't have
11393b2aab18SMatthew Ahrens 	 * too many entries in the next_clones_obj even after failing to
11403b2aab18SMatthew Ahrens 	 * remove this one.
11413b2aab18SMatthew Ahrens 	 */
11423b2aab18SMatthew Ahrens 	if (err != ENOENT)
11433b2aab18SMatthew Ahrens 		VERIFY0(err);
1144c1379625SJustin T. Gibbs 	ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
11453b2aab18SMatthew Ahrens 	    &count));
1146c1379625SJustin T. Gibbs 	ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
11473b2aab18SMatthew Ahrens }
1148842727c2SChris Kirby 
1149842727c2SChris Kirby 
11503b2aab18SMatthew Ahrens blkptr_t *
11513b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds)
11523b2aab18SMatthew Ahrens {
1153c1379625SJustin T. Gibbs 	return (&dsl_dataset_phys(ds)->ds_bp);
1154842727c2SChris Kirby }
1155842727c2SChris Kirby 
11563b2aab18SMatthew Ahrens spa_t *
11573b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds)
11583b2aab18SMatthew Ahrens {
11593b2aab18SMatthew Ahrens 	return (ds->ds_dir->dd_pool->dp_spa);
1160842727c2SChris Kirby }
1161842727c2SChris Kirby 
11623b2aab18SMatthew Ahrens void
11633b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1164fa9e4066Sahrens {
11653b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
1166842727c2SChris Kirby 
11673b2aab18SMatthew Ahrens 	if (ds == NULL) /* this is the meta-objset */
11683b2aab18SMatthew Ahrens 		return;
11691d452cf5Sahrens 
11703b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1171fa9e4066Sahrens 
1172c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
11733b2aab18SMatthew Ahrens 		panic("dirtying snapshot!");
1174fa9e4066Sahrens 
1175bfaed0b9SAndriy Gapon 	/* Must not dirty a dataset in the same txg where it got snapshotted. */
1176bfaed0b9SAndriy Gapon 	ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
1177ce636f8bSMatthew Ahrens 
1178bfaed0b9SAndriy Gapon 	dp = ds->ds_dir->dd_pool;
11793b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1180*eb633035STom Caputi 		objset_t *os = ds->ds_objset;
1181*eb633035STom Caputi 
11823b2aab18SMatthew Ahrens 		/* up the hold count until we can be written out */
11833b2aab18SMatthew Ahrens 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1184*eb633035STom Caputi 
1185*eb633035STom Caputi 		/* if this dataset is encrypted, grab a reference to the DCK */
1186*eb633035STom Caputi 		if (ds->ds_dir->dd_crypto_obj != 0 &&
1187*eb633035STom Caputi 		    !os->os_raw_receive &&
1188*eb633035STom Caputi 		    !os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
1189*eb633035STom Caputi 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
1190*eb633035STom Caputi 			key_mapping_add_ref(ds->ds_key_mapping, ds);
1191*eb633035STom Caputi 		}
11923b2aab18SMatthew Ahrens 	}
11933b2aab18SMatthew Ahrens }
1194fa9e4066Sahrens 
11952e2c1355SMatthew Ahrens boolean_t
11962e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds)
11972e2c1355SMatthew Ahrens {
11982e2c1355SMatthew Ahrens 	for (int t = 0; t < TXG_SIZE; t++) {
11992e2c1355SMatthew Ahrens 		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
12002e2c1355SMatthew Ahrens 		    ds, t))
12012e2c1355SMatthew Ahrens 			return (B_TRUE);
12022e2c1355SMatthew Ahrens 	}
12032e2c1355SMatthew Ahrens 	return (B_FALSE);
12042e2c1355SMatthew Ahrens }
12052e2c1355SMatthew Ahrens 
1206fa9e4066Sahrens static int
12073b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1208fa9e4066Sahrens {
12093b2aab18SMatthew Ahrens 	uint64_t asize;
1210fa9e4066Sahrens 
12113b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
121288b7b0f2SMatthew Ahrens 		return (0);
1213fa9e4066Sahrens 
1214e1930233Sbonwick 	/*
12153b2aab18SMatthew Ahrens 	 * If there's an fs-only reservation, any blocks that might become
12163b2aab18SMatthew Ahrens 	 * owned by the snapshot dataset must be accommodated by space
12173b2aab18SMatthew Ahrens 	 * outside of the reservation.
1218e1930233Sbonwick 	 */
12193b2aab18SMatthew Ahrens 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1220c1379625SJustin T. Gibbs 	asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
12213b2aab18SMatthew Ahrens 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1222be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
1223e1930233Sbonwick 
12243cb34c60Sahrens 	/*
12253b2aab18SMatthew Ahrens 	 * Propagate any reserved space for this snapshot to other
12263b2aab18SMatthew Ahrens 	 * snapshot checks in this sync group.
12273cb34c60Sahrens 	 */
12283b2aab18SMatthew Ahrens 	if (asize > 0)
12293b2aab18SMatthew Ahrens 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
12303cb34c60Sahrens 
1231e1930233Sbonwick 	return (0);
1232e1930233Sbonwick }
1233e1930233Sbonwick 
12343cb34c60Sahrens int
12353b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1236a2afb611SJerry Jelinek     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr)
12371d452cf5Sahrens {
12383b2aab18SMatthew Ahrens 	int error;
12393b2aab18SMatthew Ahrens 	uint64_t value;
1240fa9e4066Sahrens 
12413b2aab18SMatthew Ahrens 	ds->ds_trysnap_txg = tx->tx_txg;
1242745cd3c5Smaybee 
12433b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx))
1244842727c2SChris Kirby 		return (0);
1245fa9e4066Sahrens 
1246fa9e4066Sahrens 	/*
12473b2aab18SMatthew Ahrens 	 * We don't allow multiple snapshots of the same txg.  If there
12483b2aab18SMatthew Ahrens 	 * is already one, try again.
1249fa9e4066Sahrens 	 */
1250c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1251be6fd75aSMatthew Ahrens 		return (SET_ERROR(EAGAIN));
1252fa9e4066Sahrens 
1253fa9e4066Sahrens 	/*
12543b2aab18SMatthew Ahrens 	 * Check for conflicting snapshot name.
1255fa9e4066Sahrens 	 */
12563b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
12573b2aab18SMatthew Ahrens 	if (error == 0)
1258be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
12593b2aab18SMatthew Ahrens 	if (error != ENOENT)
12603b2aab18SMatthew Ahrens 		return (error);
1261842727c2SChris Kirby 
1262ca48f36fSKeith M Wesolowski 	/*
1263ca48f36fSKeith M Wesolowski 	 * We don't allow taking snapshots of inconsistent datasets, such as
1264ca48f36fSKeith M Wesolowski 	 * those into which we are currently receiving.  However, if we are
1265ca48f36fSKeith M Wesolowski 	 * creating this snapshot as part of a receive, this check will be
1266ca48f36fSKeith M Wesolowski 	 * executed atomically with respect to the completion of the receive
1267ca48f36fSKeith M Wesolowski 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1268ca48f36fSKeith M Wesolowski 	 * case we ignore this, knowing it will be fixed up for us shortly in
1269ca48f36fSKeith M Wesolowski 	 * dmu_recv_end_sync().
1270ca48f36fSKeith M Wesolowski 	 */
1271ca48f36fSKeith M Wesolowski 	if (!recv && DS_IS_INCONSISTENT(ds))
1272ca48f36fSKeith M Wesolowski 		return (SET_ERROR(EBUSY));
1273ca48f36fSKeith M Wesolowski 
1274a2afb611SJerry Jelinek 	/*
1275a2afb611SJerry Jelinek 	 * Skip the check for temporary snapshots or if we have already checked
1276a2afb611SJerry Jelinek 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1277a2afb611SJerry Jelinek 	 * check the count here when we're receiving a stream.
1278a2afb611SJerry Jelinek 	 */
1279a2afb611SJerry Jelinek 	if (cnt != 0 && cr != NULL) {
1280a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1281a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr);
1282a2afb611SJerry Jelinek 		if (error != 0)
1283a2afb611SJerry Jelinek 			return (error);
1284a2afb611SJerry Jelinek 	}
1285a2afb611SJerry Jelinek 
12863b2aab18SMatthew Ahrens 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
12873b2aab18SMatthew Ahrens 	if (error != 0)
12883b2aab18SMatthew Ahrens 		return (error);
1289842727c2SChris Kirby 
12901d452cf5Sahrens 	return (0);
12911d452cf5Sahrens }
12921d452cf5Sahrens 
12932840dce1SChris Williamson int
12943b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1295745cd3c5Smaybee {
12963b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
12973b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
12983b2aab18SMatthew Ahrens 	nvpair_t *pair;
12993b2aab18SMatthew Ahrens 	int rv = 0;
13003b2aab18SMatthew Ahrens 
1301a2afb611SJerry Jelinek 	/*
1302a2afb611SJerry Jelinek 	 * Pre-compute how many total new snapshots will be created for each
1303a2afb611SJerry Jelinek 	 * level in the tree and below. This is needed for validating the
1304a2afb611SJerry Jelinek 	 * snapshot limit when either taking a recursive snapshot or when
1305a2afb611SJerry Jelinek 	 * taking multiple snapshots.
1306a2afb611SJerry Jelinek 	 *
1307a2afb611SJerry Jelinek 	 * The problem is that the counts are not actually adjusted when
1308a2afb611SJerry Jelinek 	 * we are checking, only when we finally sync. For a single snapshot,
1309a2afb611SJerry Jelinek 	 * this is easy, the count will increase by 1 at each node up the tree,
1310a2afb611SJerry Jelinek 	 * but its more complicated for the recursive/multiple snapshot case.
1311a2afb611SJerry Jelinek 	 *
1312a2afb611SJerry Jelinek 	 * The dsl_fs_ss_limit_check function does recursively check the count
1313a2afb611SJerry Jelinek 	 * at each level up the tree but since it is validating each snapshot
1314a2afb611SJerry Jelinek 	 * independently we need to be sure that we are validating the complete
1315a2afb611SJerry Jelinek 	 * count for the entire set of snapshots. We do this by rolling up the
1316a2afb611SJerry Jelinek 	 * counts for each component of the name into an nvlist and then
1317a2afb611SJerry Jelinek 	 * checking each of those cases with the aggregated count.
1318a2afb611SJerry Jelinek 	 *
1319a2afb611SJerry Jelinek 	 * This approach properly handles not only the recursive snapshot
1320a2afb611SJerry Jelinek 	 * case (where we get all of those on the ddsa_snaps list) but also
1321a2afb611SJerry Jelinek 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1322a2afb611SJerry Jelinek 	 * validate the limit on 'a' using a count of 2).
1323a2afb611SJerry Jelinek 	 *
1324a2afb611SJerry Jelinek 	 * We validate the snapshot names in the third loop and only report
1325a2afb611SJerry Jelinek 	 * name errors once.
1326a2afb611SJerry Jelinek 	 */
1327a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1328a2afb611SJerry Jelinek 		nvlist_t *cnt_track = NULL;
1329a2afb611SJerry Jelinek 		cnt_track = fnvlist_alloc();
1330a2afb611SJerry Jelinek 
1331a2afb611SJerry Jelinek 		/* Rollup aggregated counts into the cnt_track list */
1332a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1333a2afb611SJerry Jelinek 		    pair != NULL;
1334a2afb611SJerry Jelinek 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1335a2afb611SJerry Jelinek 			char *pdelim;
1336a2afb611SJerry Jelinek 			uint64_t val;
1337a2afb611SJerry Jelinek 			char nm[MAXPATHLEN];
1338a2afb611SJerry Jelinek 
1339a2afb611SJerry Jelinek 			(void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1340a2afb611SJerry Jelinek 			pdelim = strchr(nm, '@');
1341a2afb611SJerry Jelinek 			if (pdelim == NULL)
1342a2afb611SJerry Jelinek 				continue;
1343a2afb611SJerry Jelinek 			*pdelim = '\0';
1344a2afb611SJerry Jelinek 
1345a2afb611SJerry Jelinek 			do {
1346a2afb611SJerry Jelinek 				if (nvlist_lookup_uint64(cnt_track, nm,
1347a2afb611SJerry Jelinek 				    &val) == 0) {
1348a2afb611SJerry Jelinek 					/* update existing entry */
1349a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm,
1350a2afb611SJerry Jelinek 					    val + 1);
1351a2afb611SJerry Jelinek 				} else {
1352a2afb611SJerry Jelinek 					/* add to list */
1353a2afb611SJerry Jelinek 					fnvlist_add_uint64(cnt_track, nm, 1);
1354a2afb611SJerry Jelinek 				}
1355a2afb611SJerry Jelinek 
1356a2afb611SJerry Jelinek 				pdelim = strrchr(nm, '/');
1357a2afb611SJerry Jelinek 				if (pdelim != NULL)
1358a2afb611SJerry Jelinek 					*pdelim = '\0';
1359a2afb611SJerry Jelinek 			} while (pdelim != NULL);
1360a2afb611SJerry Jelinek 		}
1361a2afb611SJerry Jelinek 
1362a2afb611SJerry Jelinek 		/* Check aggregated counts at each level */
1363a2afb611SJerry Jelinek 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1364a2afb611SJerry Jelinek 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1365a2afb611SJerry Jelinek 			int error = 0;
1366a2afb611SJerry Jelinek 			char *name;
1367a2afb611SJerry Jelinek 			uint64_t cnt = 0;
1368a2afb611SJerry Jelinek 			dsl_dataset_t *ds;
1369a2afb611SJerry Jelinek 
1370a2afb611SJerry Jelinek 			name = nvpair_name(pair);
1371a2afb611SJerry Jelinek 			cnt = fnvpair_value_uint64(pair);
1372a2afb611SJerry Jelinek 			ASSERT(cnt > 0);
1373a2afb611SJerry Jelinek 
1374a2afb611SJerry Jelinek 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1375a2afb611SJerry Jelinek 			if (error == 0) {
1376a2afb611SJerry Jelinek 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1377a2afb611SJerry Jelinek 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1378a2afb611SJerry Jelinek 				    ddsa->ddsa_cr);
1379a2afb611SJerry Jelinek 				dsl_dataset_rele(ds, FTAG);
1380a2afb611SJerry Jelinek 			}
1381a2afb611SJerry Jelinek 
1382a2afb611SJerry Jelinek 			if (error != 0) {
1383a2afb611SJerry Jelinek 				if (ddsa->ddsa_errors != NULL)
1384a2afb611SJerry Jelinek 					fnvlist_add_int32(ddsa->ddsa_errors,
1385a2afb611SJerry Jelinek 					    name, error);
1386a2afb611SJerry Jelinek 				rv = error;
1387a2afb611SJerry Jelinek 				/* only report one error for this check */
1388a2afb611SJerry Jelinek 				break;
1389a2afb611SJerry Jelinek 			}
1390a2afb611SJerry Jelinek 		}
1391a2afb611SJerry Jelinek 		nvlist_free(cnt_track);
1392a2afb611SJerry Jelinek 	}
1393a2afb611SJerry Jelinek 
13943b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
13953b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
13963b2aab18SMatthew Ahrens 		int error = 0;
13973b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
13983b2aab18SMatthew Ahrens 		char *name, *atp;
13999adfa60dSMatthew Ahrens 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
14003b2aab18SMatthew Ahrens 
14013b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
14029adfa60dSMatthew Ahrens 		if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1403be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENAMETOOLONG);
14043b2aab18SMatthew Ahrens 		if (error == 0) {
14053b2aab18SMatthew Ahrens 			atp = strchr(name, '@');
14063b2aab18SMatthew Ahrens 			if (atp == NULL)
1407be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
14083b2aab18SMatthew Ahrens 			if (error == 0)
14093b2aab18SMatthew Ahrens 				(void) strlcpy(dsname, name, atp - name + 1);
14103b2aab18SMatthew Ahrens 		}
14113b2aab18SMatthew Ahrens 		if (error == 0)
14123b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
14133b2aab18SMatthew Ahrens 		if (error == 0) {
1414a2afb611SJerry Jelinek 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
14153b2aab18SMatthew Ahrens 			error = dsl_dataset_snapshot_check_impl(ds,
1416a2afb611SJerry Jelinek 			    atp + 1, tx, B_FALSE, 0, NULL);
14173b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
14183b2aab18SMatthew Ahrens 		}
1419745cd3c5Smaybee 
14203b2aab18SMatthew Ahrens 		if (error != 0) {
14213b2aab18SMatthew Ahrens 			if (ddsa->ddsa_errors != NULL) {
14223b2aab18SMatthew Ahrens 				fnvlist_add_int32(ddsa->ddsa_errors,
14233b2aab18SMatthew Ahrens 				    name, error);
14243b2aab18SMatthew Ahrens 			}
14253b2aab18SMatthew Ahrens 			rv = error;
14263b2aab18SMatthew Ahrens 		}
14273b2aab18SMatthew Ahrens 	}
1428a2afb611SJerry Jelinek 
14293b2aab18SMatthew Ahrens 	return (rv);
1430745cd3c5Smaybee }
1431745cd3c5Smaybee 
14323b2aab18SMatthew Ahrens void
14333b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
14343b2aab18SMatthew Ahrens     dmu_tx_t *tx)
1435745cd3c5Smaybee {
14363b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
14373b2aab18SMatthew Ahrens 	dmu_buf_t *dbuf;
14383b2aab18SMatthew Ahrens 	dsl_dataset_phys_t *dsphys;
14393b2aab18SMatthew Ahrens 	uint64_t dsobj, crtxg;
14403b2aab18SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
14413b2aab18SMatthew Ahrens 	objset_t *os;
1442745cd3c5Smaybee 
14433b2aab18SMatthew Ahrens 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1444c33e334fSMatthew Ahrens 
1445c33e334fSMatthew Ahrens 	/*
14463b2aab18SMatthew Ahrens 	 * If we are on an old pool, the zil must not be active, in which
14473b2aab18SMatthew Ahrens 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1448c33e334fSMatthew Ahrens 	 */
14493b2aab18SMatthew Ahrens 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
14503b2aab18SMatthew Ahrens 	    dmu_objset_from_ds(ds, &os) != 0 ||
14513b2aab18SMatthew Ahrens 	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
14523b2aab18SMatthew Ahrens 	    sizeof (zero_zil)) == 0);
1453c33e334fSMatthew Ahrens 
1454bfaed0b9SAndriy Gapon 	/* Should not snapshot a dirty dataset. */
1455bfaed0b9SAndriy Gapon 	ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1456bfaed0b9SAndriy Gapon 	    ds, tx->tx_txg));
1457bfaed0b9SAndriy Gapon 
1458a2afb611SJerry Jelinek 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1459cde58dbcSMatthew Ahrens 
1460cde58dbcSMatthew Ahrens 	/*
14613b2aab18SMatthew Ahrens 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1462088f3894Sahrens 	 */
1463088f3894Sahrens 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1464088f3894Sahrens 		crtxg = 1;
1465088f3894Sahrens 	else
1466088f3894Sahrens 		crtxg = tx->tx_txg;
1467088f3894Sahrens 
14681649cd4bStabriz 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
14691649cd4bStabriz 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
14703b2aab18SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1471fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
1472fa9e4066Sahrens 	dsphys = dbuf->db_data;
1473745cd3c5Smaybee 	bzero(dsphys, sizeof (dsl_dataset_phys_t));
14741d452cf5Sahrens 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1475fa9e4066Sahrens 	dsphys->ds_fsid_guid = unique_create();
1476fa9e4066Sahrens 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1477fa9e4066Sahrens 	    sizeof (dsphys->ds_guid));
1478c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1479c1379625SJustin T. Gibbs 	dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1480fa9e4066Sahrens 	dsphys->ds_next_snap_obj = ds->ds_object;
1481fa9e4066Sahrens 	dsphys->ds_num_children = 1;
1482fa9e4066Sahrens 	dsphys->ds_creation_time = gethrestime_sec();
1483088f3894Sahrens 	dsphys->ds_creation_txg = crtxg;
1484c1379625SJustin T. Gibbs 	dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1485c1379625SJustin T. Gibbs 	dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1486c1379625SJustin T. Gibbs 	dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1487c1379625SJustin T. Gibbs 	dsphys->ds_uncompressed_bytes =
1488c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1489c1379625SJustin T. Gibbs 	dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1490c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1491c1379625SJustin T. Gibbs 	dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1492c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
1493ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
1494fa9e4066Sahrens 
1495ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1496ca0cc391SMatthew Ahrens 		if (ds->ds_feature_inuse[f])
1497ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(dsobj, f, tx);
1498ca0cc391SMatthew Ahrens 	}
1499b5152584SMatthew Ahrens 
1500c1379625SJustin T. Gibbs 	ASSERT3U(ds->ds_prev != 0, ==,
1501c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
15021d452cf5Sahrens 	if (ds->ds_prev) {
1503088f3894Sahrens 		uint64_t next_clones_obj =
1504c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1505c1379625SJustin T. Gibbs 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1506fa9e4066Sahrens 		    ds->ds_object ||
1507c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1508c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1509c1379625SJustin T. Gibbs 		    ds->ds_object) {
15101d452cf5Sahrens 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1511c1379625SJustin T. Gibbs 			ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1512c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1513c1379625SJustin T. Gibbs 			dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1514088f3894Sahrens 		} else if (next_clones_obj != 0) {
15153b2aab18SMatthew Ahrens 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1516c33e334fSMatthew Ahrens 			    dsphys->ds_next_snap_obj, tx);
15173b2aab18SMatthew Ahrens 			VERIFY0(zap_add_int(mos,
1518088f3894Sahrens 			    next_clones_obj, dsobj, tx));
1519fa9e4066Sahrens 		}
1520fa9e4066Sahrens 	}
1521fa9e4066Sahrens 
1522a9799022Sck 	/*
1523a9799022Sck 	 * If we have a reference-reservation on this dataset, we will
1524a9799022Sck 	 * need to increase the amount of refreservation being charged
1525a9799022Sck 	 * since our unique space is going to zero.
1526a9799022Sck 	 */
1527a9799022Sck 	if (ds->ds_reserved) {
15283f9d6ad7SLin Ling 		int64_t delta;
15293f9d6ad7SLin Ling 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1530c1379625SJustin T. Gibbs 		delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1531c1379625SJustin T. Gibbs 		    ds->ds_reserved);
153274e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
15333f9d6ad7SLin Ling 		    delta, 0, 0, tx);
1534a9799022Sck 	}
1535a9799022Sck 
1536fa9e4066Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1537c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_deadlist_obj =
1538c1379625SJustin T. Gibbs 	    dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1539c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1540cde58dbcSMatthew Ahrens 	dsl_deadlist_close(&ds->ds_deadlist);
1541c1379625SJustin T. Gibbs 	dsl_deadlist_open(&ds->ds_deadlist, mos,
1542c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1543cde58dbcSMatthew Ahrens 	dsl_deadlist_add_key(&ds->ds_deadlist,
1544c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1545cde58dbcSMatthew Ahrens 
15465cabbc6bSPrashanth Sreenivasa 	if (dsl_dataset_remap_deadlist_exists(ds)) {
15475cabbc6bSPrashanth Sreenivasa 		uint64_t remap_deadlist_obj =
15485cabbc6bSPrashanth Sreenivasa 		    dsl_dataset_get_remap_deadlist_object(ds);
15495cabbc6bSPrashanth Sreenivasa 		/*
15505cabbc6bSPrashanth Sreenivasa 		 * Move the remap_deadlist to the snapshot.  The head
15515cabbc6bSPrashanth Sreenivasa 		 * will create a new remap deadlist on demand, from
15525cabbc6bSPrashanth Sreenivasa 		 * dsl_dataset_block_remapped().
15535cabbc6bSPrashanth Sreenivasa 		 */
15545cabbc6bSPrashanth Sreenivasa 		dsl_dataset_unset_remap_deadlist_object(ds, tx);
15555cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_close(&ds->ds_remap_deadlist);
15565cabbc6bSPrashanth Sreenivasa 
15575cabbc6bSPrashanth Sreenivasa 		dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
15585cabbc6bSPrashanth Sreenivasa 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_REMAP_DEADLIST,
15595cabbc6bSPrashanth Sreenivasa 		    sizeof (remap_deadlist_obj), 1, &remap_deadlist_obj, tx));
15605cabbc6bSPrashanth Sreenivasa 	}
15615cabbc6bSPrashanth Sreenivasa 
1562*eb633035STom Caputi 	/*
1563*eb633035STom Caputi 	 * Create a ivset guid for this snapshot if the dataset is
1564*eb633035STom Caputi 	 * encrypted. This may be overridden by a raw receive. A
1565*eb633035STom Caputi 	 * previous implementation of this code did not have this
1566*eb633035STom Caputi 	 * field as part of the on-disk format for ZFS encryption
1567*eb633035STom Caputi 	 * (see errata #4). As part of the remediation for this
1568*eb633035STom Caputi 	 * issue, we ask the user to enable the bookmark_v2 feature
1569*eb633035STom Caputi 	 * which is now a dependency of the encryption feature. We
1570*eb633035STom Caputi 	 * use this as a heuristic to determine when the user has
1571*eb633035STom Caputi 	 * elected to correct any datasets created with the old code.
1572*eb633035STom Caputi 	 * As a result, we only do this step if the bookmark_v2
1573*eb633035STom Caputi 	 * feature is enabled, which limits the number of states a
1574*eb633035STom Caputi 	 * given pool / dataset can be in with regards to terms of
1575*eb633035STom Caputi 	 * correcting the issue.
1576*eb633035STom Caputi 	 */
1577*eb633035STom Caputi 	if (ds->ds_dir->dd_crypto_obj != 0 &&
1578*eb633035STom Caputi 	    spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2)) {
1579*eb633035STom Caputi 		uint64_t ivset_guid = unique_create();
1580*eb633035STom Caputi 
1581*eb633035STom Caputi 		dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1582*eb633035STom Caputi 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_IVSET_GUID,
1583*eb633035STom Caputi 		    sizeof (ivset_guid), 1, &ivset_guid, tx));
1584*eb633035STom Caputi 	}
1585*eb633035STom Caputi 
1586c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1587c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1588c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1589c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_unique_bytes = 0;
15905cabbc6bSPrashanth Sreenivasa 
1591a9799022Sck 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1592c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1593fa9e4066Sahrens 
1594c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
15953b2aab18SMatthew Ahrens 	    snapname, 8, 1, &dsobj, tx));
1596fa9e4066Sahrens 
1597fa9e4066Sahrens 	if (ds->ds_prev)
15983b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds->ds_prev, ds);
15993b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp,
1600c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1601ecd6cf80Smarks 
16023f9d6ad7SLin Ling 	dsl_scan_ds_snapshotted(ds, tx);
1603088f3894Sahrens 
160471eb0538SChris Kirby 	dsl_dir_snap_cmtime_update(ds->ds_dir);
160571eb0538SChris Kirby 
16064445fffbSMatthew Ahrens 	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
1607fa9e4066Sahrens }
1608fa9e4066Sahrens 
16092840dce1SChris Williamson void
16103b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1611fa9e4066Sahrens {
16123b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t *ddsa = arg;
16133b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16143b2aab18SMatthew Ahrens 	nvpair_t *pair;
161591ebeef5Sahrens 
16163b2aab18SMatthew Ahrens 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
16173b2aab18SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
16183b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
16193b2aab18SMatthew Ahrens 		char *name, *atp;
16209adfa60dSMatthew Ahrens 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
16213b2aab18SMatthew Ahrens 
16223b2aab18SMatthew Ahrens 		name = nvpair_name(pair);
16233b2aab18SMatthew Ahrens 		atp = strchr(name, '@');
16243b2aab18SMatthew Ahrens 		(void) strlcpy(dsname, name, atp - name + 1);
16253b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
16263b2aab18SMatthew Ahrens 
16273b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
16283b2aab18SMatthew Ahrens 		if (ddsa->ddsa_props != NULL) {
16293b2aab18SMatthew Ahrens 			dsl_props_set_sync_impl(ds->ds_prev,
16303b2aab18SMatthew Ahrens 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
16313b2aab18SMatthew Ahrens 		}
16323b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
16333b2aab18SMatthew Ahrens 	}
1634fa9e4066Sahrens }
1635fa9e4066Sahrens 
16363b2aab18SMatthew Ahrens /*
16373b2aab18SMatthew Ahrens  * The snapshots must all be in the same pool.
16383b2aab18SMatthew Ahrens  * All-or-nothing: if there are any failures, nothing will be modified.
16393b2aab18SMatthew Ahrens  */
16403b2aab18SMatthew Ahrens int
16413b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
164219b94df9SMatthew Ahrens {
16433b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_arg_t ddsa;
16443b2aab18SMatthew Ahrens 	nvpair_t *pair;
16453b2aab18SMatthew Ahrens 	boolean_t needsuspend;
16463b2aab18SMatthew Ahrens 	int error;
16473b2aab18SMatthew Ahrens 	spa_t *spa;
16483b2aab18SMatthew Ahrens 	char *firstname;
16493b2aab18SMatthew Ahrens 	nvlist_t *suspended = NULL;
165019b94df9SMatthew Ahrens 
16513b2aab18SMatthew Ahrens 	pair = nvlist_next_nvpair(snaps, NULL);
16523b2aab18SMatthew Ahrens 	if (pair == NULL)
16533b2aab18SMatthew Ahrens 		return (0);
16543b2aab18SMatthew Ahrens 	firstname = nvpair_name(pair);
16553b2aab18SMatthew Ahrens 
16563b2aab18SMatthew Ahrens 	error = spa_open(firstname, &spa, FTAG);
16573b2aab18SMatthew Ahrens 	if (error != 0)
16583b2aab18SMatthew Ahrens 		return (error);
16593b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
16603b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
16613b2aab18SMatthew Ahrens 
16623b2aab18SMatthew Ahrens 	if (needsuspend) {
16633b2aab18SMatthew Ahrens 		suspended = fnvlist_alloc();
16643b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
16653b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(snaps, pair)) {
16669adfa60dSMatthew Ahrens 			char fsname[ZFS_MAX_DATASET_NAME_LEN];
16673b2aab18SMatthew Ahrens 			char *snapname = nvpair_name(pair);
16683b2aab18SMatthew Ahrens 			char *atp;
16693b2aab18SMatthew Ahrens 			void *cookie;
16703b2aab18SMatthew Ahrens 
16713b2aab18SMatthew Ahrens 			atp = strchr(snapname, '@');
16723b2aab18SMatthew Ahrens 			if (atp == NULL) {
1673be6fd75aSMatthew Ahrens 				error = SET_ERROR(EINVAL);
16743b2aab18SMatthew Ahrens 				break;
16753b2aab18SMatthew Ahrens 			}
16763b2aab18SMatthew Ahrens 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
16773b2aab18SMatthew Ahrens 
16783b2aab18SMatthew Ahrens 			error = zil_suspend(fsname, &cookie);
16793b2aab18SMatthew Ahrens 			if (error != 0)
16803b2aab18SMatthew Ahrens 				break;
16813b2aab18SMatthew Ahrens 			fnvlist_add_uint64(suspended, fsname,
16823b2aab18SMatthew Ahrens 			    (uintptr_t)cookie);
16833b2aab18SMatthew Ahrens 		}
16843b2aab18SMatthew Ahrens 	}
16853b2aab18SMatthew Ahrens 
16863b2aab18SMatthew Ahrens 	ddsa.ddsa_snaps = snaps;
16873b2aab18SMatthew Ahrens 	ddsa.ddsa_props = props;
16883b2aab18SMatthew Ahrens 	ddsa.ddsa_errors = errors;
1689a2afb611SJerry Jelinek 	ddsa.ddsa_cr = CRED();
16903b2aab18SMatthew Ahrens 
16913b2aab18SMatthew Ahrens 	if (error == 0) {
16923b2aab18SMatthew Ahrens 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
16933b2aab18SMatthew Ahrens 		    dsl_dataset_snapshot_sync, &ddsa,
16947d46dc6cSMatthew Ahrens 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
16953b2aab18SMatthew Ahrens 	}
16963b2aab18SMatthew Ahrens 
16973b2aab18SMatthew Ahrens 	if (suspended != NULL) {
16983b2aab18SMatthew Ahrens 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
16993b2aab18SMatthew Ahrens 		    pair = nvlist_next_nvpair(suspended, pair)) {
17003b2aab18SMatthew Ahrens 			zil_resume((void *)(uintptr_t)
17013b2aab18SMatthew Ahrens 			    fnvpair_value_uint64(pair));
17023b2aab18SMatthew Ahrens 		}
17033b2aab18SMatthew Ahrens 		fnvlist_free(suspended);
17043b2aab18SMatthew Ahrens 	}
17053b2aab18SMatthew Ahrens 
17063b2aab18SMatthew Ahrens 	return (error);
17073b2aab18SMatthew Ahrens }
17083b2aab18SMatthew Ahrens 
17093b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg {
17103b2aab18SMatthew Ahrens 	const char *ddsta_fsname;
17113b2aab18SMatthew Ahrens 	const char *ddsta_snapname;
17123b2aab18SMatthew Ahrens 	minor_t ddsta_cleanup_minor;
17133b2aab18SMatthew Ahrens 	const char *ddsta_htag;
17143b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t;
17153b2aab18SMatthew Ahrens 
17163b2aab18SMatthew Ahrens static int
17173b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
17183b2aab18SMatthew Ahrens {
17193b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
17203b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17213b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
17223b2aab18SMatthew Ahrens 	int error;
17233b2aab18SMatthew Ahrens 
17243b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
17253b2aab18SMatthew Ahrens 	if (error != 0)
17263b2aab18SMatthew Ahrens 		return (error);
17273b2aab18SMatthew Ahrens 
1728a2afb611SJerry Jelinek 	/* NULL cred means no limit check for tmp snapshot */
1729ca48f36fSKeith M Wesolowski 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
1730a2afb611SJerry Jelinek 	    tx, B_FALSE, 0, NULL);
17313b2aab18SMatthew Ahrens 	if (error != 0) {
17323b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
17333b2aab18SMatthew Ahrens 		return (error);
17343b2aab18SMatthew Ahrens 	}
17353b2aab18SMatthew Ahrens 
17363b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
17373b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1738be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
17393b2aab18SMatthew Ahrens 	}
17403b2aab18SMatthew Ahrens 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
17413b2aab18SMatthew Ahrens 	    B_TRUE, tx);
17423b2aab18SMatthew Ahrens 	if (error != 0) {
17433b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
17443b2aab18SMatthew Ahrens 		return (error);
17453b2aab18SMatthew Ahrens 	}
17463b2aab18SMatthew Ahrens 
17473b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
17483b2aab18SMatthew Ahrens 	return (0);
17493b2aab18SMatthew Ahrens }
17503b2aab18SMatthew Ahrens 
17513b2aab18SMatthew Ahrens static void
17523b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
17533b2aab18SMatthew Ahrens {
17543b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
17553b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17563b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
17573b2aab18SMatthew Ahrens 
17583b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
17593b2aab18SMatthew Ahrens 
17603b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
17613b2aab18SMatthew Ahrens 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
17623b2aab18SMatthew Ahrens 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
17633b2aab18SMatthew Ahrens 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
17643b2aab18SMatthew Ahrens 
17653b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
17663b2aab18SMatthew Ahrens }
17673b2aab18SMatthew Ahrens 
17683b2aab18SMatthew Ahrens int
17693b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
17703b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag)
17713b2aab18SMatthew Ahrens {
17723b2aab18SMatthew Ahrens 	dsl_dataset_snapshot_tmp_arg_t ddsta;
17733b2aab18SMatthew Ahrens 	int error;
17743b2aab18SMatthew Ahrens 	spa_t *spa;
17753b2aab18SMatthew Ahrens 	boolean_t needsuspend;
17763b2aab18SMatthew Ahrens 	void *cookie;
17773b2aab18SMatthew Ahrens 
17783b2aab18SMatthew Ahrens 	ddsta.ddsta_fsname = fsname;
17793b2aab18SMatthew Ahrens 	ddsta.ddsta_snapname = snapname;
17803b2aab18SMatthew Ahrens 	ddsta.ddsta_cleanup_minor = cleanup_minor;
17813b2aab18SMatthew Ahrens 	ddsta.ddsta_htag = htag;
17823b2aab18SMatthew Ahrens 
17833b2aab18SMatthew Ahrens 	error = spa_open(fsname, &spa, FTAG);
17843b2aab18SMatthew Ahrens 	if (error != 0)
17853b2aab18SMatthew Ahrens 		return (error);
17863b2aab18SMatthew Ahrens 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
17873b2aab18SMatthew Ahrens 	spa_close(spa, FTAG);
17883b2aab18SMatthew Ahrens 
17893b2aab18SMatthew Ahrens 	if (needsuspend) {
17903b2aab18SMatthew Ahrens 		error = zil_suspend(fsname, &cookie);
17913b2aab18SMatthew Ahrens 		if (error != 0)
17923b2aab18SMatthew Ahrens 			return (error);
17933b2aab18SMatthew Ahrens 	}
17943b2aab18SMatthew Ahrens 
17953b2aab18SMatthew Ahrens 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
17967d46dc6cSMatthew Ahrens 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
17973b2aab18SMatthew Ahrens 
17983b2aab18SMatthew Ahrens 	if (needsuspend)
17993b2aab18SMatthew Ahrens 		zil_resume(cookie);
18003b2aab18SMatthew Ahrens 	return (error);
18013b2aab18SMatthew Ahrens }
18023b2aab18SMatthew Ahrens 
18033b2aab18SMatthew Ahrens void
18043b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
18053b2aab18SMatthew Ahrens {
18063b2aab18SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
18073b2aab18SMatthew Ahrens 	ASSERT(ds->ds_objset != NULL);
1808c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
18093b2aab18SMatthew Ahrens 
18103b2aab18SMatthew Ahrens 	/*
18113b2aab18SMatthew Ahrens 	 * in case we had to change ds_fsid_guid when we opened it,
18123b2aab18SMatthew Ahrens 	 * sync it out now.
18133b2aab18SMatthew Ahrens 	 */
18143b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1815c1379625SJustin T. Gibbs 	dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
18163b2aab18SMatthew Ahrens 
18179c3fd121SMatthew Ahrens 	if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
18189c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
18199c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
18209c3fd121SMatthew Ahrens 		    &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
18219c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
18229c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
18239c3fd121SMatthew Ahrens 		    &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
18249c3fd121SMatthew Ahrens 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
18259c3fd121SMatthew Ahrens 		    ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
18269c3fd121SMatthew Ahrens 		    &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
18279c3fd121SMatthew Ahrens 		ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
18289c3fd121SMatthew Ahrens 		ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
18299c3fd121SMatthew Ahrens 		ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
18309c3fd121SMatthew Ahrens 	}
18319c3fd121SMatthew Ahrens 
18323b2aab18SMatthew Ahrens 	dmu_objset_sync(ds->ds_objset, zio, tx);
1833b5152584SMatthew Ahrens 
1834ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1835ca0cc391SMatthew Ahrens 		if (ds->ds_feature_activation_needed[f]) {
1836ca0cc391SMatthew Ahrens 			if (ds->ds_feature_inuse[f])
1837ca0cc391SMatthew Ahrens 				continue;
1838ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(ds->ds_object, f, tx);
1839ca0cc391SMatthew Ahrens 			ds->ds_feature_inuse[f] = B_TRUE;
1840ca0cc391SMatthew Ahrens 		}
1841b5152584SMatthew Ahrens 	}
18423b2aab18SMatthew Ahrens }
18433b2aab18SMatthew Ahrens 
1844bfaed0b9SAndriy Gapon static int
1845bfaed0b9SAndriy Gapon deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1846bfaed0b9SAndriy Gapon {
1847bfaed0b9SAndriy Gapon 	dsl_deadlist_t *dl = arg;
1848bfaed0b9SAndriy Gapon 	dsl_deadlist_insert(dl, bp, tx);
1849bfaed0b9SAndriy Gapon 	return (0);
1850bfaed0b9SAndriy Gapon }
1851bfaed0b9SAndriy Gapon 
1852bfaed0b9SAndriy Gapon void
1853bfaed0b9SAndriy Gapon dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
1854bfaed0b9SAndriy Gapon {
1855bfaed0b9SAndriy Gapon 	objset_t *os = ds->ds_objset;
1856bfaed0b9SAndriy Gapon 
1857bfaed0b9SAndriy Gapon 	bplist_iterate(&ds->ds_pending_deadlist,
1858bfaed0b9SAndriy Gapon 	    deadlist_enqueue_cb, &ds->ds_deadlist, tx);
1859bfaed0b9SAndriy Gapon 
186094c2d0ebSMatthew Ahrens 	if (os->os_synced_dnodes != NULL) {
186194c2d0ebSMatthew Ahrens 		multilist_destroy(os->os_synced_dnodes);
186294c2d0ebSMatthew Ahrens 		os->os_synced_dnodes = NULL;
186394c2d0ebSMatthew Ahrens 	}
186494c2d0ebSMatthew Ahrens 
1865*eb633035STom Caputi 	if (os->os_encrypted)
1866*eb633035STom Caputi 		os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_FALSE;
1867*eb633035STom Caputi 	else
1868*eb633035STom Caputi 		ASSERT0(os->os_next_write_raw[tx->tx_txg & TXG_MASK]);
1869*eb633035STom Caputi 
1870bfaed0b9SAndriy Gapon 	ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx)));
1871bfaed0b9SAndriy Gapon 
1872bfaed0b9SAndriy Gapon 	dmu_buf_rele(ds->ds_dbuf, ds);
1873bfaed0b9SAndriy Gapon }
1874bfaed0b9SAndriy Gapon 
1875dfc11533SChris Williamson int
1876dfc11533SChris Williamson get_clones_stat_impl(dsl_dataset_t *ds, nvlist_t *val)
18773b2aab18SMatthew Ahrens {
18783b2aab18SMatthew Ahrens 	uint64_t count = 0;
18793b2aab18SMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
18803b2aab18SMatthew Ahrens 	zap_cursor_t zc;
18813b2aab18SMatthew Ahrens 	zap_attribute_t za;
18823b2aab18SMatthew Ahrens 
18833b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
188419b94df9SMatthew Ahrens 
188519b94df9SMatthew Ahrens 	/*
18863b2aab18SMatthew Ahrens 	 * There may be missing entries in ds_next_clones_obj
188719b94df9SMatthew Ahrens 	 * due to a bug in a previous version of the code.
188819b94df9SMatthew Ahrens 	 * Only trust it if it has the right number of entries.
188919b94df9SMatthew Ahrens 	 */
1890c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
1891c1379625SJustin T. Gibbs 		VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
189219b94df9SMatthew Ahrens 		    &count));
189319b94df9SMatthew Ahrens 	}
1894dfc11533SChris Williamson 	if (count != dsl_dataset_phys(ds)->ds_num_children - 1) {
1895dfc11533SChris Williamson 		return (ENOENT);
1896dfc11533SChris Williamson 	}
1897c1379625SJustin T. Gibbs 	for (zap_cursor_init(&zc, mos,
1898c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_next_clones_obj);
189919b94df9SMatthew Ahrens 	    zap_cursor_retrieve(&zc, &za) == 0;
190019b94df9SMatthew Ahrens 	    zap_cursor_advance(&zc)) {
190119b94df9SMatthew Ahrens 		dsl_dataset_t *clone;
19029adfa60dSMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
19033b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
19043b2aab18SMatthew Ahrens 		    za.za_first_integer, FTAG, &clone));
190519b94df9SMatthew Ahrens 		dsl_dir_name(clone->ds_dir, buf);
19063b2aab18SMatthew Ahrens 		fnvlist_add_boolean(val, buf);
190719b94df9SMatthew Ahrens 		dsl_dataset_rele(clone, FTAG);
190819b94df9SMatthew Ahrens 	}
190919b94df9SMatthew Ahrens 	zap_cursor_fini(&zc);
1910dfc11533SChris Williamson 	return (0);
191119b94df9SMatthew Ahrens }
191219b94df9SMatthew Ahrens 
1913dfc11533SChris Williamson void
1914dfc11533SChris Williamson get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
1915dfc11533SChris Williamson {
1916dfc11533SChris Williamson 	nvlist_t *propval = fnvlist_alloc();
1917dfc11533SChris Williamson 	nvlist_t *val;
1918dfc11533SChris Williamson 
1919dfc11533SChris Williamson 	/*
1920dfc11533SChris Williamson 	 * We use nvlist_alloc() instead of fnvlist_alloc() because the
1921dfc11533SChris Williamson 	 * latter would allocate the list with NV_UNIQUE_NAME flag.
1922dfc11533SChris Williamson 	 * As a result, every time a clone name is appended to the list
1923dfc11533SChris Williamson 	 * it would be (linearly) searched for for a duplicate name.
1924dfc11533SChris Williamson 	 * We already know that all clone names must be unique and we
1925dfc11533SChris Williamson 	 * want avoid the quadratic complexity of double-checking that
1926dfc11533SChris Williamson 	 * because we can have a large number of clones.
1927dfc11533SChris Williamson 	 */
1928dfc11533SChris Williamson 	VERIFY0(nvlist_alloc(&val, 0, KM_SLEEP));
1929dfc11533SChris Williamson 
1930dfc11533SChris Williamson 	if (get_clones_stat_impl(ds, val) == 0) {
1931dfc11533SChris Williamson 		fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
1932dfc11533SChris Williamson 		fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
1933dfc11533SChris Williamson 		    propval);
1934dfc11533SChris Williamson 	}
19357d3000f7SPavel Zakharov 
19367d3000f7SPavel Zakharov 	nvlist_free(val);
19377d3000f7SPavel Zakharov 	nvlist_free(propval);
1938dfc11533SChris Williamson }
1939dfc11533SChris Williamson 
1940dfc11533SChris Williamson /*
1941dfc11533SChris Williamson  * Returns a string that represents the receive resume stats token. It should
1942dfc11533SChris Williamson  * be freed with strfree().
1943dfc11533SChris Williamson  */
1944dfc11533SChris Williamson char *
1945dfc11533SChris Williamson get_receive_resume_stats_impl(dsl_dataset_t *ds)
19469c3fd121SMatthew Ahrens {
19479c3fd121SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
19489c3fd121SMatthew Ahrens 
19499c3fd121SMatthew Ahrens 	if (dsl_dataset_has_resume_receive_state(ds)) {
19509c3fd121SMatthew Ahrens 		char *str;
19519c3fd121SMatthew Ahrens 		void *packed;
19529c3fd121SMatthew Ahrens 		uint8_t *compressed;
19539c3fd121SMatthew Ahrens 		uint64_t val;
19549c3fd121SMatthew Ahrens 		nvlist_t *token_nv = fnvlist_alloc();
19559c3fd121SMatthew Ahrens 		size_t packed_size, compressed_size;
19569c3fd121SMatthew Ahrens 
19579c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
19589c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
19599c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "fromguid", val);
19609c3fd121SMatthew Ahrens 		}
19619c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
19629c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
19639c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "object", val);
19649c3fd121SMatthew Ahrens 		}
19659c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
19669c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
19679c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "offset", val);
19689c3fd121SMatthew Ahrens 		}
19699c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
19709c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
19719c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "bytes", val);
19729c3fd121SMatthew Ahrens 		}
19739c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
19749c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
19759c3fd121SMatthew Ahrens 			fnvlist_add_uint64(token_nv, "toguid", val);
19769c3fd121SMatthew Ahrens 		}
19779c3fd121SMatthew Ahrens 		char buf[256];
19789c3fd121SMatthew Ahrens 		if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
19799c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
19809c3fd121SMatthew Ahrens 			fnvlist_add_string(token_nv, "toname", buf);
19819c3fd121SMatthew Ahrens 		}
19825602294fSDan Kimmel 		if (zap_contains(dp->dp_meta_objset, ds->ds_object,
19835602294fSDan Kimmel 		    DS_FIELD_RESUME_LARGEBLOCK) == 0) {
19845602294fSDan Kimmel 			fnvlist_add_boolean(token_nv, "largeblockok");
19855602294fSDan Kimmel 		}
19869c3fd121SMatthew Ahrens 		if (zap_contains(dp->dp_meta_objset, ds->ds_object,
19879c3fd121SMatthew Ahrens 		    DS_FIELD_RESUME_EMBEDOK) == 0) {
19889c3fd121SMatthew Ahrens 			fnvlist_add_boolean(token_nv, "embedok");
19899c3fd121SMatthew Ahrens 		}
19905602294fSDan Kimmel 		if (zap_contains(dp->dp_meta_objset, ds->ds_object,
19915602294fSDan Kimmel 		    DS_FIELD_RESUME_COMPRESSOK) == 0) {
19925602294fSDan Kimmel 			fnvlist_add_boolean(token_nv, "compressok");
19935602294fSDan Kimmel 		}
1994*eb633035STom Caputi 		if (zap_contains(dp->dp_meta_objset, ds->ds_object,
1995*eb633035STom Caputi 		    DS_FIELD_RESUME_RAWOK) == 0) {
1996*eb633035STom Caputi 			fnvlist_add_boolean(token_nv, "rawok");
1997*eb633035STom Caputi 		}
19989c3fd121SMatthew Ahrens 		packed = fnvlist_pack(token_nv, &packed_size);
19999c3fd121SMatthew Ahrens 		fnvlist_free(token_nv);
20009c3fd121SMatthew Ahrens 		compressed = kmem_alloc(packed_size, KM_SLEEP);
20019c3fd121SMatthew Ahrens 
20029c3fd121SMatthew Ahrens 		compressed_size = gzip_compress(packed, compressed,
20039c3fd121SMatthew Ahrens 		    packed_size, packed_size, 6);
20049c3fd121SMatthew Ahrens 
20059c3fd121SMatthew Ahrens 		zio_cksum_t cksum;
20069c3fd121SMatthew Ahrens 		fletcher_4_native(compressed, compressed_size, NULL, &cksum);
20079c3fd121SMatthew Ahrens 
20089c3fd121SMatthew Ahrens 		str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP);
20099c3fd121SMatthew Ahrens 		for (int i = 0; i < compressed_size; i++) {
20109c3fd121SMatthew Ahrens 			(void) sprintf(str + i * 2, "%02x", compressed[i]);
20119c3fd121SMatthew Ahrens 		}
20129c3fd121SMatthew Ahrens 		str[compressed_size * 2] = '\0';
20139c3fd121SMatthew Ahrens 		char *propval = kmem_asprintf("%u-%llx-%llx-%s",
20149c3fd121SMatthew Ahrens 		    ZFS_SEND_RESUME_TOKEN_VERSION,
20159c3fd121SMatthew Ahrens 		    (longlong_t)cksum.zc_word[0],
20169c3fd121SMatthew Ahrens 		    (longlong_t)packed_size, str);
20179c3fd121SMatthew Ahrens 		kmem_free(packed, packed_size);
20189c3fd121SMatthew Ahrens 		kmem_free(str, compressed_size * 2 + 1);
20199c3fd121SMatthew Ahrens 		kmem_free(compressed, packed_size);
2020dfc11533SChris Williamson 		return (propval);
2021dfc11533SChris Williamson 	}
2022dfc11533SChris Williamson 	return (strdup(""));
2023dfc11533SChris Williamson }
2024dfc11533SChris Williamson 
2025dfc11533SChris Williamson /*
2026dfc11533SChris Williamson  * Returns a string that represents the receive resume stats token of the
2027dfc11533SChris Williamson  * dataset's child. It should be freed with strfree().
2028dfc11533SChris Williamson  */
2029dfc11533SChris Williamson char *
2030dfc11533SChris Williamson get_child_receive_stats(dsl_dataset_t *ds)
2031dfc11533SChris Williamson {
2032dfc11533SChris Williamson 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
2033dfc11533SChris Williamson 	dsl_dataset_t *recv_ds;
2034dfc11533SChris Williamson 	dsl_dataset_name(ds, recvname);
2035dfc11533SChris Williamson 	if (strlcat(recvname, "/", sizeof (recvname)) <
2036dfc11533SChris Williamson 	    sizeof (recvname) &&
2037dfc11533SChris Williamson 	    strlcat(recvname, recv_clone_name, sizeof (recvname)) <
2038dfc11533SChris Williamson 	    sizeof (recvname) &&
2039dfc11533SChris Williamson 	    dsl_dataset_hold(ds->ds_dir->dd_pool, recvname, FTAG,
2040dfc11533SChris Williamson 	    &recv_ds)  == 0) {
2041dfc11533SChris Williamson 		char *propval = get_receive_resume_stats_impl(recv_ds);
2042dfc11533SChris Williamson 		dsl_dataset_rele(recv_ds, FTAG);
2043dfc11533SChris Williamson 		return (propval);
2044dfc11533SChris Williamson 	}
2045dfc11533SChris Williamson 	return (strdup(""));
2046dfc11533SChris Williamson }
2047dfc11533SChris Williamson 
2048dfc11533SChris Williamson static void
2049dfc11533SChris Williamson get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv)
2050dfc11533SChris Williamson {
2051dfc11533SChris Williamson 	char *propval = get_receive_resume_stats_impl(ds);
2052dfc11533SChris Williamson 	if (strcmp(propval, "") != 0) {
2053dfc11533SChris Williamson 		dsl_prop_nvlist_add_string(nv,
2054dfc11533SChris Williamson 		    ZFS_PROP_RECEIVE_RESUME_TOKEN, propval);
2055dfc11533SChris Williamson 	} else {
2056dfc11533SChris Williamson 		char *childval = get_child_receive_stats(ds);
2057dfc11533SChris Williamson 		if (strcmp(childval, "") != 0) {
2058dfc11533SChris Williamson 			dsl_prop_nvlist_add_string(nv,
2059dfc11533SChris Williamson 			    ZFS_PROP_RECEIVE_RESUME_TOKEN, childval);
2060dfc11533SChris Williamson 		}
2061dfc11533SChris Williamson 		strfree(childval);
20629c3fd121SMatthew Ahrens 	}
2063dfc11533SChris Williamson 	strfree(propval);
2064dfc11533SChris Williamson }
2065dfc11533SChris Williamson 
2066dfc11533SChris Williamson uint64_t
2067dfc11533SChris Williamson dsl_get_refratio(dsl_dataset_t *ds)
2068dfc11533SChris Williamson {
2069dfc11533SChris Williamson 	uint64_t ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
2070dfc11533SChris Williamson 	    (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
2071dfc11533SChris Williamson 	    dsl_dataset_phys(ds)->ds_compressed_bytes);
2072dfc11533SChris Williamson 	return (ratio);
2073dfc11533SChris Williamson }
2074dfc11533SChris Williamson 
2075dfc11533SChris Williamson uint64_t
2076dfc11533SChris Williamson dsl_get_logicalreferenced(dsl_dataset_t *ds)
2077dfc11533SChris Williamson {
2078dfc11533SChris Williamson 	return (dsl_dataset_phys(ds)->ds_uncompressed_bytes);
2079dfc11533SChris Williamson }
2080dfc11533SChris Williamson 
2081dfc11533SChris Williamson uint64_t
2082dfc11533SChris Williamson dsl_get_compressratio(dsl_dataset_t *ds)
2083dfc11533SChris Williamson {
2084dfc11533SChris Williamson 	if (ds->ds_is_snapshot) {
2085dfc11533SChris Williamson 		return (dsl_get_refratio(ds));
2086dfc11533SChris Williamson 	} else {
2087dfc11533SChris Williamson 		dsl_dir_t *dd = ds->ds_dir;
2088dfc11533SChris Williamson 		mutex_enter(&dd->dd_lock);
2089dfc11533SChris Williamson 		uint64_t val = dsl_dir_get_compressratio(dd);
2090dfc11533SChris Williamson 		mutex_exit(&dd->dd_lock);
2091dfc11533SChris Williamson 		return (val);
2092dfc11533SChris Williamson 	}
2093dfc11533SChris Williamson }
2094dfc11533SChris Williamson 
2095dfc11533SChris Williamson uint64_t
2096dfc11533SChris Williamson dsl_get_used(dsl_dataset_t *ds)
2097dfc11533SChris Williamson {
2098dfc11533SChris Williamson 	if (ds->ds_is_snapshot) {
2099dfc11533SChris Williamson 		return (dsl_dataset_phys(ds)->ds_unique_bytes);
2100dfc11533SChris Williamson 	} else {
2101dfc11533SChris Williamson 		dsl_dir_t *dd = ds->ds_dir;
2102dfc11533SChris Williamson 		mutex_enter(&dd->dd_lock);
2103dfc11533SChris Williamson 		uint64_t val = dsl_dir_get_used(dd);
2104dfc11533SChris Williamson 		mutex_exit(&dd->dd_lock);
2105dfc11533SChris Williamson 		return (val);
2106dfc11533SChris Williamson 	}
2107dfc11533SChris Williamson }
2108dfc11533SChris Williamson 
2109dfc11533SChris Williamson uint64_t
2110dfc11533SChris Williamson dsl_get_creation(dsl_dataset_t *ds)
2111dfc11533SChris Williamson {
2112dfc11533SChris Williamson 	return (dsl_dataset_phys(ds)->ds_creation_time);
2113dfc11533SChris Williamson }
2114dfc11533SChris Williamson 
2115dfc11533SChris Williamson uint64_t
2116dfc11533SChris Williamson dsl_get_creationtxg(dsl_dataset_t *ds)
2117dfc11533SChris Williamson {
2118dfc11533SChris Williamson 	return (dsl_dataset_phys(ds)->ds_creation_txg);
2119dfc11533SChris Williamson }
2120dfc11533SChris Williamson 
2121dfc11533SChris Williamson uint64_t
2122dfc11533SChris Williamson dsl_get_refquota(dsl_dataset_t *ds)
2123dfc11533SChris Williamson {
2124dfc11533SChris Williamson 	return (ds->ds_quota);
2125dfc11533SChris Williamson }
2126dfc11533SChris Williamson 
2127dfc11533SChris Williamson uint64_t
2128dfc11533SChris Williamson dsl_get_refreservation(dsl_dataset_t *ds)
2129dfc11533SChris Williamson {
2130dfc11533SChris Williamson 	return (ds->ds_reserved);
2131dfc11533SChris Williamson }
2132dfc11533SChris Williamson 
2133dfc11533SChris Williamson uint64_t
2134dfc11533SChris Williamson dsl_get_guid(dsl_dataset_t *ds)
2135dfc11533SChris Williamson {
2136dfc11533SChris Williamson 	return (dsl_dataset_phys(ds)->ds_guid);
2137dfc11533SChris Williamson }
2138dfc11533SChris Williamson 
2139dfc11533SChris Williamson uint64_t
2140dfc11533SChris Williamson dsl_get_unique(dsl_dataset_t *ds)
2141dfc11533SChris Williamson {
2142dfc11533SChris Williamson 	return (dsl_dataset_phys(ds)->ds_unique_bytes);
2143dfc11533SChris Williamson }
2144dfc11533SChris Williamson 
2145dfc11533SChris Williamson uint64_t
2146dfc11533SChris Williamson dsl_get_objsetid(dsl_dataset_t *ds)
2147dfc11533SChris Williamson {
2148dfc11533SChris Williamson 	return (ds->ds_object);
2149dfc11533SChris Williamson }
2150dfc11533SChris Williamson 
2151dfc11533SChris Williamson uint64_t
2152dfc11533SChris Williamson dsl_get_userrefs(dsl_dataset_t *ds)
2153dfc11533SChris Williamson {
2154dfc11533SChris Williamson 	return (ds->ds_userrefs);
2155dfc11533SChris Williamson }
2156dfc11533SChris Williamson 
2157dfc11533SChris Williamson uint64_t
2158dfc11533SChris Williamson dsl_get_defer_destroy(dsl_dataset_t *ds)
2159dfc11533SChris Williamson {
2160dfc11533SChris Williamson 	return (DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2161dfc11533SChris Williamson }
2162dfc11533SChris Williamson 
2163dfc11533SChris Williamson uint64_t
2164dfc11533SChris Williamson dsl_get_referenced(dsl_dataset_t *ds)
2165dfc11533SChris Williamson {
2166dfc11533SChris Williamson 	return (dsl_dataset_phys(ds)->ds_referenced_bytes);
2167dfc11533SChris Williamson }
2168dfc11533SChris Williamson 
2169dfc11533SChris Williamson uint64_t
2170dfc11533SChris Williamson dsl_get_numclones(dsl_dataset_t *ds)
2171dfc11533SChris Williamson {
2172dfc11533SChris Williamson 	ASSERT(ds->ds_is_snapshot);
2173dfc11533SChris Williamson 	return (dsl_dataset_phys(ds)->ds_num_children - 1);
2174dfc11533SChris Williamson }
2175dfc11533SChris Williamson 
2176dfc11533SChris Williamson uint64_t
2177dfc11533SChris Williamson dsl_get_inconsistent(dsl_dataset_t *ds)
2178dfc11533SChris Williamson {
2179dfc11533SChris Williamson 	return ((dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT) ?
2180dfc11533SChris Williamson 	    1 : 0);
2181dfc11533SChris Williamson }
2182dfc11533SChris Williamson 
2183dfc11533SChris Williamson uint64_t
2184dfc11533SChris Williamson dsl_get_available(dsl_dataset_t *ds)
2185dfc11533SChris Williamson {
2186dfc11533SChris Williamson 	uint64_t refdbytes = dsl_get_referenced(ds);
2187dfc11533SChris Williamson 	uint64_t availbytes = dsl_dir_space_available(ds->ds_dir,
2188dfc11533SChris Williamson 	    NULL, 0, TRUE);
2189dfc11533SChris Williamson 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
2190dfc11533SChris Williamson 		availbytes +=
2191dfc11533SChris Williamson 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2192dfc11533SChris Williamson 	}
2193dfc11533SChris Williamson 	if (ds->ds_quota != 0) {
2194dfc11533SChris Williamson 		/*
2195dfc11533SChris Williamson 		 * Adjust available bytes according to refquota
2196dfc11533SChris Williamson 		 */
2197dfc11533SChris Williamson 		if (refdbytes < ds->ds_quota) {
2198dfc11533SChris Williamson 			availbytes = MIN(availbytes,
2199dfc11533SChris Williamson 			    ds->ds_quota - refdbytes);
2200dfc11533SChris Williamson 		} else {
2201dfc11533SChris Williamson 			availbytes = 0;
2202dfc11533SChris Williamson 		}
2203dfc11533SChris Williamson 	}
2204dfc11533SChris Williamson 	return (availbytes);
2205dfc11533SChris Williamson }
2206dfc11533SChris Williamson 
2207dfc11533SChris Williamson int
2208dfc11533SChris Williamson dsl_get_written(dsl_dataset_t *ds, uint64_t *written)
2209dfc11533SChris Williamson {
2210dfc11533SChris Williamson 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2211dfc11533SChris Williamson 	dsl_dataset_t *prev;
2212dfc11533SChris Williamson 	int err = dsl_dataset_hold_obj(dp,
2213dfc11533SChris Williamson 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
2214dfc11533SChris Williamson 	if (err == 0) {
2215dfc11533SChris Williamson 		uint64_t comp, uncomp;
2216dfc11533SChris Williamson 		err = dsl_dataset_space_written(prev, ds, written,
2217dfc11533SChris Williamson 		    &comp, &uncomp);
2218dfc11533SChris Williamson 		dsl_dataset_rele(prev, FTAG);
2219dfc11533SChris Williamson 	}
2220dfc11533SChris Williamson 	return (err);
2221dfc11533SChris Williamson }
2222dfc11533SChris Williamson 
2223dfc11533SChris Williamson /*
2224dfc11533SChris Williamson  * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN.
2225dfc11533SChris Williamson  */
2226dfc11533SChris Williamson int
2227dfc11533SChris Williamson dsl_get_prev_snap(dsl_dataset_t *ds, char *snap)
2228dfc11533SChris Williamson {
2229dfc11533SChris Williamson 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2230dfc11533SChris Williamson 	if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
2231dfc11533SChris Williamson 		dsl_dataset_name(ds->ds_prev, snap);
2232dfc11533SChris Williamson 		return (0);
2233dfc11533SChris Williamson 	} else {
2234dfc11533SChris Williamson 		return (ENOENT);
2235dfc11533SChris Williamson 	}
2236dfc11533SChris Williamson }
2237dfc11533SChris Williamson 
2238dfc11533SChris Williamson /*
2239dfc11533SChris Williamson  * Returns the mountpoint property and source for the given dataset in the value
2240dfc11533SChris Williamson  * and source buffers. The value buffer must be at least as large as MAXPATHLEN
2241dfc11533SChris Williamson  * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN.
2242dfc11533SChris Williamson  * Returns 0 on success and an error on failure.
2243dfc11533SChris Williamson  */
2244dfc11533SChris Williamson int
2245dfc11533SChris Williamson dsl_get_mountpoint(dsl_dataset_t *ds, const char *dsname, char *value,
2246dfc11533SChris Williamson     char *source)
2247dfc11533SChris Williamson {
2248dfc11533SChris Williamson 	int error;
2249dfc11533SChris Williamson 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2250dfc11533SChris Williamson 
2251dfc11533SChris Williamson 	/* Retrieve the mountpoint value stored in the zap opbject */
2252dfc11533SChris Williamson 	error = dsl_prop_get_ds(ds, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1,
2253dfc11533SChris Williamson 	    ZAP_MAXVALUELEN, value, source);
2254dfc11533SChris Williamson 	if (error != 0) {
2255dfc11533SChris Williamson 		return (error);
2256dfc11533SChris Williamson 	}
2257dfc11533SChris Williamson 
2258f91fcf59SToomas Soome 	/*
2259f91fcf59SToomas Soome 	 * Process the dsname and source to find the full mountpoint string.
2260f91fcf59SToomas Soome 	 * Can be skipped for 'legacy' or 'none'.
2261f91fcf59SToomas Soome 	 */
2262dfc11533SChris Williamson 	if (value[0] == '/') {
2263dfc11533SChris Williamson 		char *buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
2264dfc11533SChris Williamson 		char *root = buf;
2265dfc11533SChris Williamson 		const char *relpath;
2266dfc11533SChris Williamson 
2267dfc11533SChris Williamson 		/*
2268dfc11533SChris Williamson 		 * If we inherit the mountpoint, even from a dataset
2269dfc11533SChris Williamson 		 * with a received value, the source will be the path of
2270dfc11533SChris Williamson 		 * the dataset we inherit from. If source is
2271dfc11533SChris Williamson 		 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2272dfc11533SChris Williamson 		 * inherited.
2273dfc11533SChris Williamson 		 */
2274dfc11533SChris Williamson 		if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2275dfc11533SChris Williamson 			relpath = "";
2276dfc11533SChris Williamson 		} else {
2277dfc11533SChris Williamson 			ASSERT0(strncmp(dsname, source, strlen(source)));
2278dfc11533SChris Williamson 			relpath = dsname + strlen(source);
2279dfc11533SChris Williamson 			if (relpath[0] == '/')
2280dfc11533SChris Williamson 				relpath++;
2281dfc11533SChris Williamson 		}
2282dfc11533SChris Williamson 
2283dfc11533SChris Williamson 		spa_altroot(dp->dp_spa, root, ZAP_MAXVALUELEN);
2284dfc11533SChris Williamson 
2285dfc11533SChris Williamson 		/*
2286dfc11533SChris Williamson 		 * Special case an alternate root of '/'. This will
2287dfc11533SChris Williamson 		 * avoid having multiple leading slashes in the
2288dfc11533SChris Williamson 		 * mountpoint path.
2289dfc11533SChris Williamson 		 */
2290dfc11533SChris Williamson 		if (strcmp(root, "/") == 0)
2291dfc11533SChris Williamson 			root++;
2292dfc11533SChris Williamson 
2293dfc11533SChris Williamson 		/*
2294dfc11533SChris Williamson 		 * If the mountpoint is '/' then skip over this
2295dfc11533SChris Williamson 		 * if we are obtaining either an alternate root or
2296dfc11533SChris Williamson 		 * an inherited mountpoint.
2297dfc11533SChris Williamson 		 */
2298dfc11533SChris Williamson 		char *mnt = value;
2299dfc11533SChris Williamson 		if (value[1] == '\0' && (root[0] != '\0' ||
2300dfc11533SChris Williamson 		    relpath[0] != '\0'))
2301dfc11533SChris Williamson 			mnt = value + 1;
2302dfc11533SChris Williamson 
2303dfc11533SChris Williamson 		if (relpath[0] == '\0') {
2304dfc11533SChris Williamson 			(void) snprintf(value, ZAP_MAXVALUELEN, "%s%s",
2305dfc11533SChris Williamson 			    root, mnt);
2306dfc11533SChris Williamson 		} else {
2307dfc11533SChris Williamson 			(void) snprintf(value, ZAP_MAXVALUELEN, "%s%s%s%s",
2308dfc11533SChris Williamson 			    root, mnt, relpath[0] == '@' ? "" : "/",
2309dfc11533SChris Williamson 			    relpath);
2310dfc11533SChris Williamson 		}
2311dfc11533SChris Williamson 		kmem_free(buf, ZAP_MAXVALUELEN);
2312dfc11533SChris Williamson 	}
2313f91fcf59SToomas Soome 
2314dfc11533SChris Williamson 	return (0);
23159c3fd121SMatthew Ahrens }
23169c3fd121SMatthew Ahrens 
2317fa9e4066Sahrens void
2318a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2319fa9e4066Sahrens {
2320*eb633035STom Caputi 	int err;
23213b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2322a9799022Sck 
23233b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
23243b2aab18SMatthew Ahrens 
2325dfc11533SChris Williamson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO,
2326dfc11533SChris Williamson 	    dsl_get_refratio(ds));
232777372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
2328dfc11533SChris Williamson 	    dsl_get_logicalreferenced(ds));
2329dfc11533SChris Williamson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
2330dfc11533SChris Williamson 	    dsl_get_compressratio(ds));
2331dfc11533SChris Williamson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2332dfc11533SChris Williamson 	    dsl_get_used(ds));
23334445fffbSMatthew Ahrens 
2334bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
23354445fffbSMatthew Ahrens 		get_clones_stat(ds, nv);
23364445fffbSMatthew Ahrens 	} else {
2337dfc11533SChris Williamson 		char buf[ZFS_MAX_DATASET_NAME_LEN];
2338dfc11533SChris Williamson 		if (dsl_get_prev_snap(ds, buf) == 0)
2339dfc11533SChris Williamson 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP,
2340dfc11533SChris Williamson 			    buf);
23414445fffbSMatthew Ahrens 		dsl_dir_stats(ds->ds_dir, nv);
23424445fffbSMatthew Ahrens 	}
2343fa9e4066Sahrens 
2344dfc11533SChris Williamson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE,
2345dfc11533SChris Williamson 	    dsl_get_available(ds));
2346dfc11533SChris Williamson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED,
2347dfc11533SChris Williamson 	    dsl_get_referenced(ds));
2348a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2349dfc11533SChris Williamson 	    dsl_get_creation(ds));
2350a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2351dfc11533SChris Williamson 	    dsl_get_creationtxg(ds));
2352a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2353dfc11533SChris Williamson 	    dsl_get_refquota(ds));
2354a9799022Sck 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2355dfc11533SChris Williamson 	    dsl_get_refreservation(ds));
2356c5904d13Seschrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2357dfc11533SChris Williamson 	    dsl_get_guid(ds));
23581d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2359dfc11533SChris Williamson 	    dsl_get_unique(ds));
23601d713200SEric Schrock 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2361dfc11533SChris Williamson 	    dsl_get_objsetid(ds));
236292241e0bSTom Erickson 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
2363dfc11533SChris Williamson 	    dsl_get_userrefs(ds));
2364842727c2SChris Kirby 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2365*eb633035STom Caputi 	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2366*eb633035STom Caputi 	dsl_dataset_crypt_stats(ds, nv);
2367fa9e4066Sahrens 
2368c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2369*eb633035STom Caputi 		uint64_t written, comp, uncomp;
2370*eb633035STom Caputi 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
2371*eb633035STom Caputi 		dsl_dataset_t *prev;
2372*eb633035STom Caputi 
2373*eb633035STom Caputi 		err = dsl_dataset_hold_obj(dp,
2374*eb633035STom Caputi 		    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
2375*eb633035STom Caputi 		if (err == 0) {
2376*eb633035STom Caputi 			err = dsl_dataset_space_written(prev, ds, &written,
2377*eb633035STom Caputi 			    &comp, &uncomp);
2378*eb633035STom Caputi 			dsl_dataset_rele(prev, FTAG);
2379*eb633035STom Caputi 			if (err == 0) {
2380*eb633035STom Caputi 				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2381*eb633035STom Caputi 				    written);
2382*eb633035STom Caputi 			}
238319b94df9SMatthew Ahrens 		}
238419b94df9SMatthew Ahrens 	}
23859c3fd121SMatthew Ahrens 
23869c3fd121SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds)) {
23879c3fd121SMatthew Ahrens 		/*
23889c3fd121SMatthew Ahrens 		 * A failed "newfs" (e.g. full) resumable receive leaves
23899c3fd121SMatthew Ahrens 		 * the stats set on this dataset.  Check here for the prop.
23909c3fd121SMatthew Ahrens 		 */
23919c3fd121SMatthew Ahrens 		get_receive_resume_stats(ds, nv);
23929c3fd121SMatthew Ahrens 
23939c3fd121SMatthew Ahrens 		/*
23949c3fd121SMatthew Ahrens 		 * A failed incremental resumable receive leaves the
23959c3fd121SMatthew Ahrens 		 * stats set on our child named "%recv".  Check the child
23969c3fd121SMatthew Ahrens 		 * for the prop.
23979c3fd121SMatthew Ahrens 		 */
23989adfa60dSMatthew Ahrens 		/* 6 extra bytes for /%recv */
23999adfa60dSMatthew Ahrens 		char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
24009c3fd121SMatthew Ahrens 		dsl_dataset_t *recv_ds;
24019c3fd121SMatthew Ahrens 		dsl_dataset_name(ds, recvname);
24029adfa60dSMatthew Ahrens 		if (strlcat(recvname, "/", sizeof (recvname)) <
24039adfa60dSMatthew Ahrens 		    sizeof (recvname) &&
24049adfa60dSMatthew Ahrens 		    strlcat(recvname, recv_clone_name, sizeof (recvname)) <
24059adfa60dSMatthew Ahrens 		    sizeof (recvname) &&
24069adfa60dSMatthew Ahrens 		    dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) {
24079c3fd121SMatthew Ahrens 			get_receive_resume_stats(recv_ds, nv);
24089c3fd121SMatthew Ahrens 			dsl_dataset_rele(recv_ds, FTAG);
24099c3fd121SMatthew Ahrens 		}
24109c3fd121SMatthew Ahrens 	}
2411fa9e4066Sahrens }
2412fa9e4066Sahrens 
2413a2eea2e1Sahrens void
2414a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2415a2eea2e1Sahrens {
24163b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
24173b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
24183b2aab18SMatthew Ahrens 
2419dfc11533SChris Williamson 	stat->dds_creation_txg = dsl_get_creationtxg(ds);
2420dfc11533SChris Williamson 	stat->dds_inconsistent = dsl_get_inconsistent(ds);
2421dfc11533SChris Williamson 	stat->dds_guid = dsl_get_guid(ds);
24224445fffbSMatthew Ahrens 	stat->dds_origin[0] = '\0';
2423bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
2424a2eea2e1Sahrens 		stat->dds_is_snapshot = B_TRUE;
2425dfc11533SChris Williamson 		stat->dds_num_clones = dsl_get_numclones(ds);
2426ebedde84SEric Taylor 	} else {
2427ebedde84SEric Taylor 		stat->dds_is_snapshot = B_FALSE;
2428ebedde84SEric Taylor 		stat->dds_num_clones = 0;
2429a2eea2e1Sahrens 
24304445fffbSMatthew Ahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
2431dfc11533SChris Williamson 			dsl_dir_get_origin(ds->ds_dir, stat->dds_origin);
24324445fffbSMatthew Ahrens 		}
2433a2eea2e1Sahrens 	}
2434a2eea2e1Sahrens }
2435a2eea2e1Sahrens 
2436a2eea2e1Sahrens uint64_t
2437a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2438a2eea2e1Sahrens {
243991ebeef5Sahrens 	return (ds->ds_fsid_guid);
2440a2eea2e1Sahrens }
2441a2eea2e1Sahrens 
2442a2eea2e1Sahrens void
2443a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds,
2444a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
2445a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp)
2446fa9e4066Sahrens {
2447c1379625SJustin T. Gibbs 	*refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
2448a2eea2e1Sahrens 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2449c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
2450c1379625SJustin T. Gibbs 		*availbytesp +=
2451c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2452a9799022Sck 	if (ds->ds_quota != 0) {
2453a9799022Sck 		/*
2454a9799022Sck 		 * Adjust available bytes according to refquota
2455a9799022Sck 		 */
2456a9799022Sck 		if (*refdbytesp < ds->ds_quota)
2457a9799022Sck 			*availbytesp = MIN(*availbytesp,
2458a9799022Sck 			    ds->ds_quota - *refdbytesp);
2459a9799022Sck 		else
2460a9799022Sck 			*availbytesp = 0;
2461a9799022Sck 	}
2462c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2463c1379625SJustin T. Gibbs 	*usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
2464c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
2465a2eea2e1Sahrens 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
2466fa9e4066Sahrens }
2467fa9e4066Sahrens 
2468f18faf3fSek boolean_t
246934f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
2470f18faf3fSek {
2471f18faf3fSek 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2472c166b69dSPaul Dagnelie 	uint64_t birth;
2473f18faf3fSek 
24743b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
247534f2f8cfSMatthew Ahrens 	if (snap == NULL)
2476f18faf3fSek 		return (B_FALSE);
2477c166b69dSPaul Dagnelie 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2478c166b69dSPaul Dagnelie 	birth = dsl_dataset_get_blkptr(ds)->blk_birth;
2479c166b69dSPaul Dagnelie 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
2480c166b69dSPaul Dagnelie 	if (birth > dsl_dataset_phys(snap)->ds_creation_txg) {
248134f2f8cfSMatthew Ahrens 		objset_t *os, *os_snap;
24826e0cbcaaSMatthew Ahrens 		/*
24836e0cbcaaSMatthew Ahrens 		 * It may be that only the ZIL differs, because it was
24846e0cbcaaSMatthew Ahrens 		 * reset in the head.  Don't count that as being
24856e0cbcaaSMatthew Ahrens 		 * modified.
24866e0cbcaaSMatthew Ahrens 		 */
24876e0cbcaaSMatthew Ahrens 		if (dmu_objset_from_ds(ds, &os) != 0)
24886e0cbcaaSMatthew Ahrens 			return (B_TRUE);
248934f2f8cfSMatthew Ahrens 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
24906e0cbcaaSMatthew Ahrens 			return (B_TRUE);
24916e0cbcaaSMatthew Ahrens 		return (bcmp(&os->os_phys->os_meta_dnode,
249234f2f8cfSMatthew Ahrens 		    &os_snap->os_phys->os_meta_dnode,
24936e0cbcaaSMatthew Ahrens 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
24946e0cbcaaSMatthew Ahrens 	}
2495f18faf3fSek 	return (B_FALSE);
2496f18faf3fSek }
2497f18faf3fSek 
24983b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg {
24993b2aab18SMatthew Ahrens 	const char *ddrsa_fsname;
25003b2aab18SMatthew Ahrens 	const char *ddrsa_oldsnapname;
25013b2aab18SMatthew Ahrens 	const char *ddrsa_newsnapname;
25023b2aab18SMatthew Ahrens 	boolean_t ddrsa_recursive;
25033b2aab18SMatthew Ahrens 	dmu_tx_t *ddrsa_tx;
25043b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t;
25053b2aab18SMatthew Ahrens 
25061d452cf5Sahrens /* ARGSUSED */
2507fa9e4066Sahrens static int
25083b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
25093b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
2510fa9e4066Sahrens {
25113b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
25123b2aab18SMatthew Ahrens 	int error;
2513fa9e4066Sahrens 	uint64_t val;
2514fa9e4066Sahrens 
25153b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
25163b2aab18SMatthew Ahrens 	if (error != 0) {
25173b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
25183b2aab18SMatthew Ahrens 		return (error == ENOENT ? 0 : error);
25193b2aab18SMatthew Ahrens 	}
25201d452cf5Sahrens 
25213b2aab18SMatthew Ahrens 	/* new name should not exist */
25223b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
25233b2aab18SMatthew Ahrens 	if (error == 0)
2524be6fd75aSMatthew Ahrens 		error = SET_ERROR(EEXIST);
25253b2aab18SMatthew Ahrens 	else if (error == ENOENT)
25263b2aab18SMatthew Ahrens 		error = 0;
2527cdf5b4caSmmusante 
2528cdf5b4caSmmusante 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
25293b2aab18SMatthew Ahrens 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
25309adfa60dSMatthew Ahrens 	    strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2531be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENAMETOOLONG);
2532cdf5b4caSmmusante 
25333b2aab18SMatthew Ahrens 	return (error);
25341d452cf5Sahrens }
2535fa9e4066Sahrens 
25363b2aab18SMatthew Ahrens static int
25373b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
25381d452cf5Sahrens {
25393b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
25403b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
25411d452cf5Sahrens 	dsl_dataset_t *hds;
25423b2aab18SMatthew Ahrens 	int error;
2543fa9e4066Sahrens 
25443b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
25453b2aab18SMatthew Ahrens 	if (error != 0)
25463b2aab18SMatthew Ahrens 		return (error);
2547fa9e4066Sahrens 
25483b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
25493b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
25503b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
25513b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN);
25523b2aab18SMatthew Ahrens 	} else {
25533b2aab18SMatthew Ahrens 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
25543b2aab18SMatthew Ahrens 	}
2555745cd3c5Smaybee 	dsl_dataset_rele(hds, FTAG);
25563b2aab18SMatthew Ahrens 	return (error);
2557fa9e4066Sahrens }
2558fa9e4066Sahrens 
2559cdf5b4caSmmusante static int
25603b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
25613b2aab18SMatthew Ahrens     dsl_dataset_t *hds, void *arg)
2562cdf5b4caSmmusante {
25633b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
25643b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
25653b2aab18SMatthew Ahrens 	uint64_t val;
25663b2aab18SMatthew Ahrens 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
25673b2aab18SMatthew Ahrens 	int error;
2568ecd6cf80Smarks 
25693b2aab18SMatthew Ahrens 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
25703b2aab18SMatthew Ahrens 	ASSERT(error == 0 || error == ENOENT);
25713b2aab18SMatthew Ahrens 	if (error == ENOENT) {
25723b2aab18SMatthew Ahrens 		/* ignore nonexistent snapshots */
25733b2aab18SMatthew Ahrens 		return (0);
2574ecd6cf80Smarks 	}
2575ecd6cf80Smarks 
25763b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
25773b2aab18SMatthew Ahrens 
25783b2aab18SMatthew Ahrens 	/* log before we change the name */
25793b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(ds, "rename", tx,
25803b2aab18SMatthew Ahrens 	    "-> @%s", ddrsa->ddrsa_newsnapname);
2581cdf5b4caSmmusante 
2582a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
2583a2afb611SJerry Jelinek 	    B_FALSE));
25843b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
25853b2aab18SMatthew Ahrens 	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
25863b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
2587c1379625SJustin T. Gibbs 	VERIFY0(zap_add(dp->dp_meta_objset,
2588c1379625SJustin T. Gibbs 	    dsl_dataset_phys(hds)->ds_snapnames_zapobj,
25893b2aab18SMatthew Ahrens 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
2590cdf5b4caSmmusante 
25913b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
2592cdf5b4caSmmusante 	return (0);
2593cdf5b4caSmmusante }
2594cdf5b4caSmmusante 
25953b2aab18SMatthew Ahrens static void
25963b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
2597cdf5b4caSmmusante {
25983b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
25993b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
26003b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
2601cdf5b4caSmmusante 
26023b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
26033b2aab18SMatthew Ahrens 	ddrsa->ddrsa_tx = tx;
26043b2aab18SMatthew Ahrens 	if (ddrsa->ddrsa_recursive) {
26053b2aab18SMatthew Ahrens 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
26063b2aab18SMatthew Ahrens 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
26073b2aab18SMatthew Ahrens 		    DS_FIND_CHILDREN));
26083b2aab18SMatthew Ahrens 	} else {
26093b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
2610cdf5b4caSmmusante 	}
26113b2aab18SMatthew Ahrens 	dsl_dataset_rele(hds, FTAG);
2612cdf5b4caSmmusante }
2613cdf5b4caSmmusante 
26143b2aab18SMatthew Ahrens int
26153b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname,
26163b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
26173a5a36beSmmusante {
26183b2aab18SMatthew Ahrens 	dsl_dataset_rename_snapshot_arg_t ddrsa;
26193a5a36beSmmusante 
26203b2aab18SMatthew Ahrens 	ddrsa.ddrsa_fsname = fsname;
26213b2aab18SMatthew Ahrens 	ddrsa.ddrsa_oldsnapname = oldsnapname;
26223b2aab18SMatthew Ahrens 	ddrsa.ddrsa_newsnapname = newsnapname;
26233b2aab18SMatthew Ahrens 	ddrsa.ddrsa_recursive = recursive;
26243a5a36beSmmusante 
26253b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
26267d46dc6cSMatthew Ahrens 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
26277d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
26283a5a36beSmmusante }
26293a5a36beSmmusante 
263091948b51SKeith M Wesolowski /*
263191948b51SKeith M Wesolowski  * If we're doing an ownership handoff, we need to make sure that there is
263291948b51SKeith M Wesolowski  * only one long hold on the dataset.  We're not allowed to change anything here
263391948b51SKeith M Wesolowski  * so we don't permanently release the long hold or regular hold here.  We want
263491948b51SKeith M Wesolowski  * to do this only when syncing to avoid the dataset unexpectedly going away
263591948b51SKeith M Wesolowski  * when we release the long hold.
263691948b51SKeith M Wesolowski  */
263791948b51SKeith M Wesolowski static int
263891948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
263991948b51SKeith M Wesolowski {
264091948b51SKeith M Wesolowski 	boolean_t held;
264191948b51SKeith M Wesolowski 
264291948b51SKeith M Wesolowski 	if (!dmu_tx_is_syncing(tx))
264391948b51SKeith M Wesolowski 		return (0);
264491948b51SKeith M Wesolowski 
264591948b51SKeith M Wesolowski 	if (owner != NULL) {
264691948b51SKeith M Wesolowski 		VERIFY3P(ds->ds_owner, ==, owner);
264791948b51SKeith M Wesolowski 		dsl_dataset_long_rele(ds, owner);
264891948b51SKeith M Wesolowski 	}
264991948b51SKeith M Wesolowski 
265091948b51SKeith M Wesolowski 	held = dsl_dataset_long_held(ds);
265191948b51SKeith M Wesolowski 
265291948b51SKeith M Wesolowski 	if (owner != NULL)
265391948b51SKeith M Wesolowski 		dsl_dataset_long_hold(ds, owner);
265491948b51SKeith M Wesolowski 
265591948b51SKeith M Wesolowski 	if (held)
265691948b51SKeith M Wesolowski 		return (SET_ERROR(EBUSY));
265791948b51SKeith M Wesolowski 
265891948b51SKeith M Wesolowski 	return (0);
265991948b51SKeith M Wesolowski }
266091948b51SKeith M Wesolowski 
2661000cce6bSBrad Lewis int
26623b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
2663fa9e4066Sahrens {
266491948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
26653b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
26661d452cf5Sahrens 	dsl_dataset_t *ds;
26673b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
26683b2aab18SMatthew Ahrens 	int error;
2669fa9e4066Sahrens 
267091948b51SKeith M Wesolowski 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
26713b2aab18SMatthew Ahrens 	if (error != 0)
26723b2aab18SMatthew Ahrens 		return (error);
2673370c1af0SSanjeev Bagewadi 
26743b2aab18SMatthew Ahrens 	/* must not be a snapshot */
2675bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
26763b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2677be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
26783b2aab18SMatthew Ahrens 	}
26793a5a36beSmmusante 
26803b2aab18SMatthew Ahrens 	/* must have a most recent snapshot */
2681c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
26823b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
268395643f75SAndriy Gapon 		return (SET_ERROR(ESRCH));
26843b2aab18SMatthew Ahrens 	}
26853a5a36beSmmusante 
2686bfaed0b9SAndriy Gapon 	/*
2687bfaed0b9SAndriy Gapon 	 * No rollback to a snapshot created in the current txg, because
2688bfaed0b9SAndriy Gapon 	 * the rollback may dirty the dataset and create blocks that are
2689bfaed0b9SAndriy Gapon 	 * not reachable from the rootbp while having a birth txg that
2690bfaed0b9SAndriy Gapon 	 * falls into the snapshot's range.
2691bfaed0b9SAndriy Gapon 	 */
2692bfaed0b9SAndriy Gapon 	if (dmu_tx_is_syncing(tx) &&
2693bfaed0b9SAndriy Gapon 	    dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
2694bfaed0b9SAndriy Gapon 		dsl_dataset_rele(ds, FTAG);
2695bfaed0b9SAndriy Gapon 		return (SET_ERROR(EAGAIN));
2696bfaed0b9SAndriy Gapon 	}
2697bfaed0b9SAndriy Gapon 
269877b17137SAndriy Gapon 	/*
269977b17137SAndriy Gapon 	 * If the expected target snapshot is specified, then check that
270077b17137SAndriy Gapon 	 * the latest snapshot is it.
270177b17137SAndriy Gapon 	 */
270277b17137SAndriy Gapon 	if (ddra->ddra_tosnap != NULL) {
270395643f75SAndriy Gapon 		dsl_dataset_t *snapds;
270495643f75SAndriy Gapon 
270595643f75SAndriy Gapon 		/* Check if the target snapshot exists at all. */
270695643f75SAndriy Gapon 		error = dsl_dataset_hold(dp, ddra->ddra_tosnap, FTAG, &snapds);
270795643f75SAndriy Gapon 		if (error != 0) {
270895643f75SAndriy Gapon 			/*
270995643f75SAndriy Gapon 			 * ESRCH is used to signal that the target snapshot does
271095643f75SAndriy Gapon 			 * not exist, while ENOENT is used to report that
271195643f75SAndriy Gapon 			 * the rolled back dataset does not exist.
271295643f75SAndriy Gapon 			 * ESRCH is also used to cover other cases where the
271395643f75SAndriy Gapon 			 * target snapshot is not related to the dataset being
271495643f75SAndriy Gapon 			 * rolled back such as being in a different pool.
271595643f75SAndriy Gapon 			 */
271695643f75SAndriy Gapon 			if (error == ENOENT || error == EXDEV)
271795643f75SAndriy Gapon 				error = SET_ERROR(ESRCH);
271895643f75SAndriy Gapon 			dsl_dataset_rele(ds, FTAG);
271995643f75SAndriy Gapon 			return (error);
272095643f75SAndriy Gapon 		}
272195643f75SAndriy Gapon 		ASSERT(snapds->ds_is_snapshot);
272277b17137SAndriy Gapon 
272395643f75SAndriy Gapon 		/* Check if the snapshot is the latest snapshot indeed. */
272495643f75SAndriy Gapon 		if (snapds != ds->ds_prev) {
272595643f75SAndriy Gapon 			/*
272695643f75SAndriy Gapon 			 * Distinguish between the case where the only problem
272795643f75SAndriy Gapon 			 * is intervening snapshots (EEXIST) vs the snapshot
272895643f75SAndriy Gapon 			 * not being a valid target for rollback (ESRCH).
272995643f75SAndriy Gapon 			 */
273095643f75SAndriy Gapon 			if (snapds->ds_dir == ds->ds_dir ||
273195643f75SAndriy Gapon 			    (dsl_dir_is_clone(ds->ds_dir) &&
273295643f75SAndriy Gapon 			    dsl_dir_phys(ds->ds_dir)->dd_origin_obj ==
273395643f75SAndriy Gapon 			    snapds->ds_object)) {
273495643f75SAndriy Gapon 				error = SET_ERROR(EEXIST);
273595643f75SAndriy Gapon 			} else {
273695643f75SAndriy Gapon 				error = SET_ERROR(ESRCH);
273795643f75SAndriy Gapon 			}
273895643f75SAndriy Gapon 			dsl_dataset_rele(snapds, FTAG);
273995643f75SAndriy Gapon 			dsl_dataset_rele(ds, FTAG);
274095643f75SAndriy Gapon 			return (error);
274195643f75SAndriy Gapon 		}
274295643f75SAndriy Gapon 		dsl_dataset_rele(snapds, FTAG);
274377b17137SAndriy Gapon 	}
274477b17137SAndriy Gapon 
274578f17100SMatthew Ahrens 	/* must not have any bookmarks after the most recent snapshot */
274678f17100SMatthew Ahrens 	nvlist_t *proprequest = fnvlist_alloc();
274778f17100SMatthew Ahrens 	fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG));
274878f17100SMatthew Ahrens 	nvlist_t *bookmarks = fnvlist_alloc();
274978f17100SMatthew Ahrens 	error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks);
275078f17100SMatthew Ahrens 	fnvlist_free(proprequest);
275195643f75SAndriy Gapon 	if (error != 0) {
275295643f75SAndriy Gapon 		dsl_dataset_rele(ds, FTAG);
275378f17100SMatthew Ahrens 		return (error);
275495643f75SAndriy Gapon 	}
275578f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL);
275678f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) {
275778f17100SMatthew Ahrens 		nvlist_t *valuenv =
275878f17100SMatthew Ahrens 		    fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair),
275978f17100SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_CREATETXG));
276078f17100SMatthew Ahrens 		uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value");
2761c1379625SJustin T. Gibbs 		if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
276278f17100SMatthew Ahrens 			fnvlist_free(bookmarks);
276378f17100SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
276478f17100SMatthew Ahrens 			return (SET_ERROR(EEXIST));
276578f17100SMatthew Ahrens 		}
276678f17100SMatthew Ahrens 	}
276778f17100SMatthew Ahrens 	fnvlist_free(bookmarks);
276878f17100SMatthew Ahrens 
276991948b51SKeith M Wesolowski 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
277091948b51SKeith M Wesolowski 	if (error != 0) {
27713b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
277291948b51SKeith M Wesolowski 		return (error);
27733b2aab18SMatthew Ahrens 	}
27743b2aab18SMatthew Ahrens 
27753b2aab18SMatthew Ahrens 	/*
27763b2aab18SMatthew Ahrens 	 * Check if the snap we are rolling back to uses more than
27773b2aab18SMatthew Ahrens 	 * the refquota.
27783b2aab18SMatthew Ahrens 	 */
27793b2aab18SMatthew Ahrens 	if (ds->ds_quota != 0 &&
2780c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
27813b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2782be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
2783fa9e4066Sahrens 	}
2784370c1af0SSanjeev Bagewadi 
27853b2aab18SMatthew Ahrens 	/*
27863b2aab18SMatthew Ahrens 	 * When we do the clone swap, we will temporarily use more space
27873b2aab18SMatthew Ahrens 	 * due to the refreservation (the head will no longer have any
27883b2aab18SMatthew Ahrens 	 * unique space, so the entire amount of the refreservation will need
27893b2aab18SMatthew Ahrens 	 * to be free).  We will immediately destroy the clone, freeing
27903b2aab18SMatthew Ahrens 	 * this space, but the freeing happens over many txg's.
27913b2aab18SMatthew Ahrens 	 */
27923b2aab18SMatthew Ahrens 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
2793c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_unique_bytes);
27943b2aab18SMatthew Ahrens 
27953b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
27963b2aab18SMatthew Ahrens 	    unused_refres_delta >
27973b2aab18SMatthew Ahrens 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
27983b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
2799be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
2800fa9e4066Sahrens 	}
2801fa9e4066Sahrens 
28023b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
28033b2aab18SMatthew Ahrens 	return (0);
28043b2aab18SMatthew Ahrens }
28051d452cf5Sahrens 
2806000cce6bSBrad Lewis void
28073b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
28083b2aab18SMatthew Ahrens {
280991948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t *ddra = arg;
28103b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
28113b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *clone;
28123b2aab18SMatthew Ahrens 	uint64_t cloneobj;
28139adfa60dSMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
28141d452cf5Sahrens 
281591948b51SKeith M Wesolowski 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
28161d452cf5Sahrens 
2817a7027df1SMatthew Ahrens 	dsl_dataset_name(ds->ds_prev, namebuf);
2818a7027df1SMatthew Ahrens 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
2819a7027df1SMatthew Ahrens 
28203b2aab18SMatthew Ahrens 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
2821*eb633035STom Caputi 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, NULL, tx);
28221d452cf5Sahrens 
28233b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
28241d452cf5Sahrens 
28253b2aab18SMatthew Ahrens 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
28263b2aab18SMatthew Ahrens 	dsl_dataset_zero_zil(ds, tx);
28273b2aab18SMatthew Ahrens 
28283b2aab18SMatthew Ahrens 	dsl_destroy_head_sync_impl(clone, tx);
28293b2aab18SMatthew Ahrens 
28303b2aab18SMatthew Ahrens 	dsl_dataset_rele(clone, FTAG);
28313b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
28323b2aab18SMatthew Ahrens }
28333b2aab18SMatthew Ahrens 
283491948b51SKeith M Wesolowski /*
2835a7027df1SMatthew Ahrens  * Rolls back the given filesystem or volume to the most recent snapshot.
2836a7027df1SMatthew Ahrens  * The name of the most recent snapshot will be returned under key "target"
2837a7027df1SMatthew Ahrens  * in the result nvlist.
283891948b51SKeith M Wesolowski  *
2839a7027df1SMatthew Ahrens  * If owner != NULL:
284091948b51SKeith M Wesolowski  * - The existing dataset MUST be owned by the specified owner at entry
284191948b51SKeith M Wesolowski  * - Upon return, dataset will still be held by the same owner, whether we
284291948b51SKeith M Wesolowski  *   succeed or not.
284391948b51SKeith M Wesolowski  *
284491948b51SKeith M Wesolowski  * This mode is required any time the existing filesystem is mounted.  See
284591948b51SKeith M Wesolowski  * notes above zfs_suspend_fs() for further details.
284691948b51SKeith M Wesolowski  */
28473b2aab18SMatthew Ahrens int
284877b17137SAndriy Gapon dsl_dataset_rollback(const char *fsname, const char *tosnap, void *owner,
284977b17137SAndriy Gapon     nvlist_t *result)
28503b2aab18SMatthew Ahrens {
285191948b51SKeith M Wesolowski 	dsl_dataset_rollback_arg_t ddra;
285291948b51SKeith M Wesolowski 
285391948b51SKeith M Wesolowski 	ddra.ddra_fsname = fsname;
285477b17137SAndriy Gapon 	ddra.ddra_tosnap = tosnap;
285591948b51SKeith M Wesolowski 	ddra.ddra_owner = owner;
2856a7027df1SMatthew Ahrens 	ddra.ddra_result = result;
285791948b51SKeith M Wesolowski 
28583b2aab18SMatthew Ahrens 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
28597d46dc6cSMatthew Ahrens 	    dsl_dataset_rollback_sync, &ddra,
28607d46dc6cSMatthew Ahrens 	    1, ZFS_SPACE_CHECK_RESERVED));
2861fa9e4066Sahrens }
286299653d4eSeschrock 
2863088f3894Sahrens struct promotenode {
2864745cd3c5Smaybee 	list_node_t link;
2865745cd3c5Smaybee 	dsl_dataset_t *ds;
2866745cd3c5Smaybee };
2867745cd3c5Smaybee 
286874e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
28693b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
28703b2aab18SMatthew Ahrens     void *tag);
28713b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
287274e7dc98SMatthew Ahrens 
2873dfc11533SChris Williamson int
28743b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
287599653d4eSeschrock {
28763b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
28773b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
28783b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
28793b2aab18SMatthew Ahrens 	struct promotenode *snap;
28803b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
2881745cd3c5Smaybee 	int err;
2882cde58dbcSMatthew Ahrens 	uint64_t unused;
2883a2afb611SJerry Jelinek 	uint64_t ss_mv_cnt;
2884cb5842f8SAndriy Gapon 	size_t max_snap_len;
2885dfc11533SChris Williamson 	boolean_t conflicting_snaps;
28861d452cf5Sahrens 
28873b2aab18SMatthew Ahrens 	err = promote_hold(ddpa, dp, FTAG);
28883b2aab18SMatthew Ahrens 	if (err != 0)
28893b2aab18SMatthew Ahrens 		return (err);
289099653d4eSeschrock 
28913b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
2892dfc11533SChris Williamson 	snap = list_head(&ddpa->shared_snaps);
2893dfc11533SChris Williamson 	origin_ds = snap->ds;
2894cb5842f8SAndriy Gapon 	max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
28951d452cf5Sahrens 
2896dfc11533SChris Williamson 	snap = list_head(&ddpa->origin_snaps);
2897dfc11533SChris Williamson 
2898c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
28993b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
2900be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
29013b2aab18SMatthew Ahrens 	}
29023b2aab18SMatthew Ahrens 
2903*eb633035STom Caputi 	snap = list_head(&ddpa->shared_snaps);
2904*eb633035STom Caputi 	if (snap == NULL) {
2905*eb633035STom Caputi 		err = SET_ERROR(ENOENT);
2906*eb633035STom Caputi 		goto out;
2907*eb633035STom Caputi 	}
2908*eb633035STom Caputi 	origin_ds = snap->ds;
2909*eb633035STom Caputi 
2910*eb633035STom Caputi 	/*
2911*eb633035STom Caputi 	 * Encrypted clones share a DSL Crypto Key with their origin's dsl dir.
2912*eb633035STom Caputi 	 * When doing a promote we must make sure the encryption root for
2913*eb633035STom Caputi 	 * both the target and the target's origin does not change to avoid
2914*eb633035STom Caputi 	 * needing to rewrap encryption keys
2915*eb633035STom Caputi 	 */
2916*eb633035STom Caputi 	err = dsl_dataset_promote_crypt_check(hds->ds_dir, origin_ds->ds_dir);
2917*eb633035STom Caputi 	if (err != 0)
2918*eb633035STom Caputi 		goto out;
2919*eb633035STom Caputi 
29203b2aab18SMatthew Ahrens 	/*
29213b2aab18SMatthew Ahrens 	 * Compute and check the amount of space to transfer.  Since this is
29223b2aab18SMatthew Ahrens 	 * so expensive, don't do the preliminary check.
29233b2aab18SMatthew Ahrens 	 */
29243b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
29253b2aab18SMatthew Ahrens 		promote_rele(ddpa, FTAG);
29263b2aab18SMatthew Ahrens 		return (0);
29273b2aab18SMatthew Ahrens 	}
29283b2aab18SMatthew Ahrens 
29293cb34c60Sahrens 	/* compute origin's new unique space */
29303b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
2931c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
2932c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
2933cde58dbcSMatthew Ahrens 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
2934c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
29353b2aab18SMatthew Ahrens 	    &ddpa->unique, &unused, &unused);
293699653d4eSeschrock 
2937745cd3c5Smaybee 	/*
2938745cd3c5Smaybee 	 * Walk the snapshots that we are moving
2939745cd3c5Smaybee 	 *
294074e7dc98SMatthew Ahrens 	 * Compute space to transfer.  Consider the incremental changes
29413b2aab18SMatthew Ahrens 	 * to used by each snapshot:
294274e7dc98SMatthew Ahrens 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
294374e7dc98SMatthew Ahrens 	 * So each snapshot gave birth to:
294474e7dc98SMatthew Ahrens 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
2945745cd3c5Smaybee 	 * So a sequence would look like:
294674e7dc98SMatthew Ahrens 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
2947745cd3c5Smaybee 	 * Which simplifies to:
294874e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + k1 + k0
2949745cd3c5Smaybee 	 * Note however, if we stop before we reach the ORIGIN we get:
295074e7dc98SMatthew Ahrens 	 * uN + kN + kN-1 + ... + kM - uM-1
2951745cd3c5Smaybee 	 */
2952dfc11533SChris Williamson 	conflicting_snaps = B_FALSE;
2953a2afb611SJerry Jelinek 	ss_mv_cnt = 0;
2954c1379625SJustin T. Gibbs 	ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
2955c1379625SJustin T. Gibbs 	ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
2956c1379625SJustin T. Gibbs 	ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
29573b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
29583b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
295999653d4eSeschrock 		uint64_t val, dlused, dlcomp, dluncomp;
2960745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
296199653d4eSeschrock 
2962a2afb611SJerry Jelinek 		ss_mv_cnt++;
2963a2afb611SJerry Jelinek 
29643b2aab18SMatthew Ahrens 		/*
29653b2aab18SMatthew Ahrens 		 * If there are long holds, we won't be able to evict
29663b2aab18SMatthew Ahrens 		 * the objset.
29673b2aab18SMatthew Ahrens 		 */
29683b2aab18SMatthew Ahrens 		if (dsl_dataset_long_held(ds)) {
2969be6fd75aSMatthew Ahrens 			err = SET_ERROR(EBUSY);
29703b2aab18SMatthew Ahrens 			goto out;
29713b2aab18SMatthew Ahrens 		}
29723b2aab18SMatthew Ahrens 
297399653d4eSeschrock 		/* Check that the snapshot name does not conflict */
29743b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
2975cb5842f8SAndriy Gapon 		if (strlen(ds->ds_snapname) >= max_snap_len) {
2976cb5842f8SAndriy Gapon 			err = SET_ERROR(ENAMETOOLONG);
2977cb5842f8SAndriy Gapon 			goto out;
2978cb5842f8SAndriy Gapon 		}
2979745cd3c5Smaybee 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
2980681d9761SEric Taylor 		if (err == 0) {
2981dfc11533SChris Williamson 			fnvlist_add_boolean(ddpa->err_ds,
2982dfc11533SChris Williamson 			    snap->ds->ds_snapname);
2983dfc11533SChris Williamson 			conflicting_snaps = B_TRUE;
2984dfc11533SChris Williamson 		} else if (err != ENOENT) {
2985681d9761SEric Taylor 			goto out;
2986681d9761SEric Taylor 		}
298799653d4eSeschrock 
2988745cd3c5Smaybee 		/* The very first snapshot does not have a deadlist */
2989c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
299074e7dc98SMatthew Ahrens 			continue;
299174e7dc98SMatthew Ahrens 
2992cde58dbcSMatthew Ahrens 		dsl_deadlist_space(&ds->ds_deadlist,
2993cde58dbcSMatthew Ahrens 		    &dlused, &dlcomp, &dluncomp);
29943b2aab18SMatthew Ahrens 		ddpa->used += dlused;
29953b2aab18SMatthew Ahrens 		ddpa->comp += dlcomp;
29963b2aab18SMatthew Ahrens 		ddpa->uncomp += dluncomp;
299774e7dc98SMatthew Ahrens 	}
2998745cd3c5Smaybee 
2999dfc11533SChris Williamson 	/*
3000dfc11533SChris Williamson 	 * In order to return the full list of conflicting snapshots, we check
3001dfc11533SChris Williamson 	 * whether there was a conflict after traversing all of them.
3002dfc11533SChris Williamson 	 */
3003dfc11533SChris Williamson 	if (conflicting_snaps) {
3004dfc11533SChris Williamson 		err = SET_ERROR(EEXIST);
3005dfc11533SChris Williamson 		goto out;
3006dfc11533SChris Williamson 	}
3007dfc11533SChris Williamson 
3008745cd3c5Smaybee 	/*
3009745cd3c5Smaybee 	 * If we are a clone of a clone then we never reached ORIGIN,
3010745cd3c5Smaybee 	 * so we need to subtract out the clone origin's used space.
3011745cd3c5Smaybee 	 */
30123b2aab18SMatthew Ahrens 	if (ddpa->origin_origin) {
3013c1379625SJustin T. Gibbs 		ddpa->used -=
3014c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
3015c1379625SJustin T. Gibbs 		ddpa->comp -=
3016c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
30173b2aab18SMatthew Ahrens 		ddpa->uncomp -=
3018c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ddpa->origin_origin)->
3019c1379625SJustin T. Gibbs 		    ds_uncompressed_bytes;
302099653d4eSeschrock 	}
302199653d4eSeschrock 
3022a2afb611SJerry Jelinek 	/* Check that there is enough space and limit headroom here */
302374e7dc98SMatthew Ahrens 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
3024a2afb611SJerry Jelinek 	    0, ss_mv_cnt, ddpa->used, ddpa->cr);
30253b2aab18SMatthew Ahrens 	if (err != 0)
30263b2aab18SMatthew Ahrens 		goto out;
302774e7dc98SMatthew Ahrens 
302874e7dc98SMatthew Ahrens 	/*
302974e7dc98SMatthew Ahrens 	 * Compute the amounts of space that will be used by snapshots
303074e7dc98SMatthew Ahrens 	 * after the promotion (for both origin and clone).  For each,
303174e7dc98SMatthew Ahrens 	 * it is the amount of space that will be on all of their
303274e7dc98SMatthew Ahrens 	 * deadlists (that was not born before their new origin).
303374e7dc98SMatthew Ahrens 	 */
3034c1379625SJustin T. Gibbs 	if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
303574e7dc98SMatthew Ahrens 		uint64_t space;
303674e7dc98SMatthew Ahrens 
303774e7dc98SMatthew Ahrens 		/*
303874e7dc98SMatthew Ahrens 		 * Note, typically this will not be a clone of a clone,
30393f9d6ad7SLin Ling 		 * so dd_origin_txg will be < TXG_INITIAL, so
3040cde58dbcSMatthew Ahrens 		 * these snaplist_space() -> dsl_deadlist_space_range()
304174e7dc98SMatthew Ahrens 		 * calls will be fast because they do not have to
304274e7dc98SMatthew Ahrens 		 * iterate over all bps.
304374e7dc98SMatthew Ahrens 		 */
30443b2aab18SMatthew Ahrens 		snap = list_head(&ddpa->origin_snaps);
30453b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->shared_snaps,
30463b2aab18SMatthew Ahrens 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
30473b2aab18SMatthew Ahrens 		if (err != 0)
30483b2aab18SMatthew Ahrens 			goto out;
304974e7dc98SMatthew Ahrens 
30503b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->clone_snaps,
30513f9d6ad7SLin Ling 		    snap->ds->ds_dir->dd_origin_txg, &space);
30523b2aab18SMatthew Ahrens 		if (err != 0)
30533b2aab18SMatthew Ahrens 			goto out;
30543b2aab18SMatthew Ahrens 		ddpa->cloneusedsnap += space;
305574e7dc98SMatthew Ahrens 	}
3056c1379625SJustin T. Gibbs 	if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
3057c1379625SJustin T. Gibbs 	    DD_FLAG_USED_BREAKDOWN) {
30583b2aab18SMatthew Ahrens 		err = snaplist_space(&ddpa->origin_snaps,
3059c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_creation_txg,
3060c1379625SJustin T. Gibbs 		    &ddpa->originusedsnap);
30613b2aab18SMatthew Ahrens 		if (err != 0)
30623b2aab18SMatthew Ahrens 			goto out;
3063745cd3c5Smaybee 	}
30641d452cf5Sahrens 
3065681d9761SEric Taylor out:
30663b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
3067681d9761SEric Taylor 	return (err);
30681d452cf5Sahrens }
306999653d4eSeschrock 
3070dfc11533SChris Williamson void
30713b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
30721d452cf5Sahrens {
30733b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t *ddpa = arg;
30743b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
30753b2aab18SMatthew Ahrens 	dsl_dataset_t *hds;
30763b2aab18SMatthew Ahrens 	struct promotenode *snap;
30773b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_ds;
30783b2aab18SMatthew Ahrens 	dsl_dataset_t *origin_head;
30793b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
30803cb34c60Sahrens 	dsl_dir_t *odd = NULL;
3081088f3894Sahrens 	uint64_t oldnext_obj;
308274e7dc98SMatthew Ahrens 	int64_t delta;
30831d452cf5Sahrens 
30843b2aab18SMatthew Ahrens 	VERIFY0(promote_hold(ddpa, dp, FTAG));
30853b2aab18SMatthew Ahrens 	hds = ddpa->ddpa_clone;
30863b2aab18SMatthew Ahrens 
3087c1379625SJustin T. Gibbs 	ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
30881d452cf5Sahrens 
30893b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
30903b2aab18SMatthew Ahrens 	origin_ds = snap->ds;
30913b2aab18SMatthew Ahrens 	dd = hds->ds_dir;
30923b2aab18SMatthew Ahrens 
30933b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->origin_snaps);
309474e7dc98SMatthew Ahrens 	origin_head = snap->ds;
309574e7dc98SMatthew Ahrens 
30960b69c2f0Sahrens 	/*
30973cb34c60Sahrens 	 * We need to explicitly open odd, since origin_ds's dd will be
30980b69c2f0Sahrens 	 * changing.
30990b69c2f0Sahrens 	 */
31003b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
31013cb34c60Sahrens 	    NULL, FTAG, &odd));
310299653d4eSeschrock 
3103*eb633035STom Caputi 	dsl_dataset_promote_crypt_sync(hds->ds_dir, odd, tx);
3104*eb633035STom Caputi 
3105745cd3c5Smaybee 	/* change origin's next snap */
3106745cd3c5Smaybee 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
3107c1379625SJustin T. Gibbs 	oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
31083b2aab18SMatthew Ahrens 	snap = list_tail(&ddpa->clone_snaps);
3109c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
3110c1379625SJustin T. Gibbs 	    origin_ds->ds_object);
3111c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
3112745cd3c5Smaybee 
3113088f3894Sahrens 	/* change the origin's next clone */
3114c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
31153b2aab18SMatthew Ahrens 		dsl_dataset_remove_from_next_clones(origin_ds,
31163b2aab18SMatthew Ahrens 		    snap->ds->ds_object, tx);
31173b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
3118c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
3119088f3894Sahrens 		    oldnext_obj, tx));
3120088f3894Sahrens 	}
3121088f3894Sahrens 
3122745cd3c5Smaybee 	/* change origin */
3123745cd3c5Smaybee 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
3124c1379625SJustin T. Gibbs 	ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
3125c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
31263f9d6ad7SLin Ling 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
3127745cd3c5Smaybee 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
3128c1379625SJustin T. Gibbs 	dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
31293f9d6ad7SLin Ling 	origin_head->ds_dir->dd_origin_txg =
3130c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
3131745cd3c5Smaybee 
3132cde58dbcSMatthew Ahrens 	/* change dd_clone entries */
3133cde58dbcSMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
31343b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
3135c1379625SJustin T. Gibbs 		    dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
31363b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
3137c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3138cde58dbcSMatthew Ahrens 		    hds->ds_object, tx));
3139cde58dbcSMatthew Ahrens 
31403b2aab18SMatthew Ahrens 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
3141c1379625SJustin T. Gibbs 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3142cde58dbcSMatthew Ahrens 		    origin_head->ds_object, tx));
3143c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_clones == 0) {
3144c1379625SJustin T. Gibbs 			dsl_dir_phys(dd)->dd_clones =
3145c1379625SJustin T. Gibbs 			    zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
3146c1379625SJustin T. Gibbs 			    DMU_OT_NONE, 0, tx);
3147cde58dbcSMatthew Ahrens 		}
31483b2aab18SMatthew Ahrens 		VERIFY0(zap_add_int(dp->dp_meta_objset,
3149c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
3150cde58dbcSMatthew Ahrens 	}
3151cde58dbcSMatthew Ahrens 
315299653d4eSeschrock 	/* move snapshots to this dir */
31533b2aab18SMatthew Ahrens 	for (snap = list_head(&ddpa->shared_snaps); snap;
31543b2aab18SMatthew Ahrens 	    snap = list_next(&ddpa->shared_snaps, snap)) {
3155745cd3c5Smaybee 		dsl_dataset_t *ds = snap->ds;
315699653d4eSeschrock 
31573b2aab18SMatthew Ahrens 		/*
31583b2aab18SMatthew Ahrens 		 * Property callbacks are registered to a particular
31593b2aab18SMatthew Ahrens 		 * dsl_dir.  Since ours is changing, evict the objset
31603b2aab18SMatthew Ahrens 		 * so that they will be unregistered from the old dsl_dir.
31613b2aab18SMatthew Ahrens 		 */
3162503ad85cSMatthew Ahrens 		if (ds->ds_objset) {
3163503ad85cSMatthew Ahrens 			dmu_objset_evict(ds->ds_objset);
3164503ad85cSMatthew Ahrens 			ds->ds_objset = NULL;
31653baa08fcSek 		}
31663b2aab18SMatthew Ahrens 
316799653d4eSeschrock 		/* move snap name entry */
31683b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_get_snapname(ds));
31693b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_snap_remove(origin_head,
3170a2afb611SJerry Jelinek 		    ds->ds_snapname, tx, B_TRUE));
31713b2aab18SMatthew Ahrens 		VERIFY0(zap_add(dp->dp_meta_objset,
3172c1379625SJustin T. Gibbs 		    dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
317399653d4eSeschrock 		    8, 1, &ds->ds_object, tx));
3174a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
3175a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
3176cde58dbcSMatthew Ahrens 
317799653d4eSeschrock 		/* change containing dsl_dir */
317899653d4eSeschrock 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
3179c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
3180c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
31813cb34c60Sahrens 		ASSERT3P(ds->ds_dir, ==, odd);
31823b2aab18SMatthew Ahrens 		dsl_dir_rele(ds->ds_dir, ds);
31833b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
318499653d4eSeschrock 		    NULL, ds, &ds->ds_dir));
318599653d4eSeschrock 
3186cde58dbcSMatthew Ahrens 		/* move any clone references */
3187c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
3188cde58dbcSMatthew Ahrens 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3189cde58dbcSMatthew Ahrens 			zap_cursor_t zc;
3190cde58dbcSMatthew Ahrens 			zap_attribute_t za;
3191cde58dbcSMatthew Ahrens 
31923b2aab18SMatthew Ahrens 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
3193c1379625SJustin T. Gibbs 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
31943b2aab18SMatthew Ahrens 			    zap_cursor_retrieve(&zc, &za) == 0;
31953b2aab18SMatthew Ahrens 			    zap_cursor_advance(&zc)) {
31963b2aab18SMatthew Ahrens 				dsl_dataset_t *cnds;
31973b2aab18SMatthew Ahrens 				uint64_t o;
3198a9799022Sck 
31993b2aab18SMatthew Ahrens 				if (za.za_first_integer == oldnext_obj) {
32003b2aab18SMatthew Ahrens 					/*
32013b2aab18SMatthew Ahrens 					 * We've already moved the
32023b2aab18SMatthew Ahrens 					 * origin's reference.
32033b2aab18SMatthew Ahrens 					 */
32043b2aab18SMatthew Ahrens 					continue;
32053b2aab18SMatthew Ahrens 				}
3206a9799022Sck 
32073b2aab18SMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp,
32083b2aab18SMatthew Ahrens 				    za.za_first_integer, FTAG, &cnds));
3209c1379625SJustin T. Gibbs 				o = dsl_dir_phys(cnds->ds_dir)->
3210c1379625SJustin T. Gibbs 				    dd_head_dataset_obj;
3211a9799022Sck 
32123b2aab18SMatthew Ahrens 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
3213c1379625SJustin T. Gibbs 				    dsl_dir_phys(odd)->dd_clones, o, tx));
32143b2aab18SMatthew Ahrens 				VERIFY0(zap_add_int(dp->dp_meta_objset,
3215c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd)->dd_clones, o, tx));
32163b2aab18SMatthew Ahrens 				dsl_dataset_rele(cnds, FTAG);
32173b2aab18SMatthew Ahrens 			}
32183b2aab18SMatthew Ahrens 			zap_cursor_fini(&zc);
32193b2aab18SMatthew Ahrens 		}
32209082849eSck 
32213b2aab18SMatthew Ahrens 		ASSERT(!dsl_prop_hascb(ds));
3222a9799022Sck 	}
3223a9799022Sck 
3224a9799022Sck 	/*
32253b2aab18SMatthew Ahrens 	 * Change space accounting.
32263b2aab18SMatthew Ahrens 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
32273b2aab18SMatthew Ahrens 	 * both be valid, or both be 0 (resulting in delta == 0).  This
32283b2aab18SMatthew Ahrens 	 * is true for each of {clone,origin} independently.
3229a9799022Sck 	 */
3230a9799022Sck 
32313b2aab18SMatthew Ahrens 	delta = ddpa->cloneusedsnap -
3232c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
32333b2aab18SMatthew Ahrens 	ASSERT3S(delta, >=, 0);
32343b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, delta);
32353b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
32363b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
32373b2aab18SMatthew Ahrens 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
32383b2aab18SMatthew Ahrens 
32393b2aab18SMatthew Ahrens 	delta = ddpa->originusedsnap -
3240c1379625SJustin T. Gibbs 	    dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
32413b2aab18SMatthew Ahrens 	ASSERT3S(delta, <=, 0);
32423b2aab18SMatthew Ahrens 	ASSERT3U(ddpa->used, >=, -delta);
32433b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
32443b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
32453b2aab18SMatthew Ahrens 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
32463b2aab18SMatthew Ahrens 
3247c1379625SJustin T. Gibbs 	dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
32483b2aab18SMatthew Ahrens 
32493b2aab18SMatthew Ahrens 	/* log history record */
32503b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(hds, "promote", tx, "");
32513b2aab18SMatthew Ahrens 
32523b2aab18SMatthew Ahrens 	dsl_dir_rele(odd, FTAG);
32533b2aab18SMatthew Ahrens 	promote_rele(ddpa, FTAG);
3254a9799022Sck }
3255a9799022Sck 
32563b2aab18SMatthew Ahrens /*
32573b2aab18SMatthew Ahrens  * Make a list of dsl_dataset_t's for the snapshots between first_obj
32583b2aab18SMatthew Ahrens  * (exclusive) and last_obj (inclusive).  The list will be in reverse
32593b2aab18SMatthew Ahrens  * order (last_obj will be the list_head()).  If first_obj == 0, do all
32603b2aab18SMatthew Ahrens  * snapshots back to this dataset's origin.
32613b2aab18SMatthew Ahrens  */
3262a9799022Sck static int
32633b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp,
32643b2aab18SMatthew Ahrens     uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
3265a9799022Sck {
32663b2aab18SMatthew Ahrens 	uint64_t obj = last_obj;
3267a9799022Sck 
32683b2aab18SMatthew Ahrens 	list_create(l, sizeof (struct promotenode),
32693b2aab18SMatthew Ahrens 	    offsetof(struct promotenode, link));
3270a9799022Sck 
32713b2aab18SMatthew Ahrens 	while (obj != first_obj) {
32723b2aab18SMatthew Ahrens 		dsl_dataset_t *ds;
32733b2aab18SMatthew Ahrens 		struct promotenode *snap;
32743b2aab18SMatthew Ahrens 		int err;
327592241e0bSTom Erickson 
32763b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
32773b2aab18SMatthew Ahrens 		ASSERT(err != ENOENT);
32783b2aab18SMatthew Ahrens 		if (err != 0)
32793b2aab18SMatthew Ahrens 			return (err);
3280a9799022Sck 
32813b2aab18SMatthew Ahrens 		if (first_obj == 0)
3282c1379625SJustin T. Gibbs 			first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
32833b2aab18SMatthew Ahrens 
32843b2aab18SMatthew Ahrens 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
32853b2aab18SMatthew Ahrens 		snap->ds = ds;
32863b2aab18SMatthew Ahrens 		list_insert_tail(l, snap);
3287c1379625SJustin T. Gibbs 		obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
32883b2aab18SMatthew Ahrens 	}
3289a9799022Sck 
3290a9799022Sck 	return (0);
3291a9799022Sck }
3292a9799022Sck 
32933b2aab18SMatthew Ahrens static int
32943b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
3295a9799022Sck {
32963b2aab18SMatthew Ahrens 	struct promotenode *snap;
3297a9799022Sck 
32983b2aab18SMatthew Ahrens 	*spacep = 0;
32993b2aab18SMatthew Ahrens 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
33003b2aab18SMatthew Ahrens 		uint64_t used, comp, uncomp;
33013b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
33023b2aab18SMatthew Ahrens 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
33033b2aab18SMatthew Ahrens 		*spacep += used;
330492241e0bSTom Erickson 	}
33053b2aab18SMatthew Ahrens 	return (0);
3306a9799022Sck }
3307a9799022Sck 
33083b2aab18SMatthew Ahrens static void
33093b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag)
3310a9799022Sck {
33113b2aab18SMatthew Ahrens 	struct promotenode *snap;
331292241e0bSTom Erickson 
33133b2aab18SMatthew Ahrens 	if (l == NULL || !list_link_active(&l->list_head))
33143b2aab18SMatthew Ahrens 		return;
3315a9799022Sck 
33163b2aab18SMatthew Ahrens 	while ((snap = list_tail(l)) != NULL) {
33173b2aab18SMatthew Ahrens 		list_remove(l, snap);
33183b2aab18SMatthew Ahrens 		dsl_dataset_rele(snap->ds, tag);
33193b2aab18SMatthew Ahrens 		kmem_free(snap, sizeof (*snap));
33203b2aab18SMatthew Ahrens 	}
33213b2aab18SMatthew Ahrens 	list_destroy(l);
3322a9799022Sck }
3323a9799022Sck 
3324a9799022Sck static int
33253b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
3326a9799022Sck {
33273b2aab18SMatthew Ahrens 	int error;
33283b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
33293b2aab18SMatthew Ahrens 	struct promotenode *snap;
3330a9799022Sck 
33313b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
33323b2aab18SMatthew Ahrens 	    &ddpa->ddpa_clone);
33333b2aab18SMatthew Ahrens 	if (error != 0)
33343b2aab18SMatthew Ahrens 		return (error);
33353b2aab18SMatthew Ahrens 	dd = ddpa->ddpa_clone->ds_dir;
3336a9799022Sck 
3337bc9014e6SJustin Gibbs 	if (ddpa->ddpa_clone->ds_is_snapshot ||
33383b2aab18SMatthew Ahrens 	    !dsl_dir_is_clone(dd)) {
33393b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
3340be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
33413b2aab18SMatthew Ahrens 	}
3342a9799022Sck 
3343c1379625SJustin T. Gibbs 	error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
33443b2aab18SMatthew Ahrens 	    &ddpa->shared_snaps, tag);
33453b2aab18SMatthew Ahrens 	if (error != 0)
33463b2aab18SMatthew Ahrens 		goto out;
3347a9799022Sck 
33483b2aab18SMatthew Ahrens 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
33493b2aab18SMatthew Ahrens 	    &ddpa->clone_snaps, tag);
33503b2aab18SMatthew Ahrens 	if (error != 0)
33513b2aab18SMatthew Ahrens 		goto out;
3352a9799022Sck 
33533b2aab18SMatthew Ahrens 	snap = list_head(&ddpa->shared_snaps);
3354c1379625SJustin T. Gibbs 	ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
3355c1379625SJustin T. Gibbs 	error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
3356c1379625SJustin T. Gibbs 	    dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
33573b2aab18SMatthew Ahrens 	    &ddpa->origin_snaps, tag);
33583b2aab18SMatthew Ahrens 	if (error != 0)
33593b2aab18SMatthew Ahrens 		goto out;
3360379c004dSEric Schrock 
3361c1379625SJustin T. Gibbs 	if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
33623b2aab18SMatthew Ahrens 		error = dsl_dataset_hold_obj(dp,
3363c1379625SJustin T. Gibbs 		    dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
33643b2aab18SMatthew Ahrens 		    tag, &ddpa->origin_origin);
33653b2aab18SMatthew Ahrens 		if (error != 0)
33663b2aab18SMatthew Ahrens 			goto out;
3367379c004dSEric Schrock 	}
33683b2aab18SMatthew Ahrens out:
33693b2aab18SMatthew Ahrens 	if (error != 0)
33703b2aab18SMatthew Ahrens 		promote_rele(ddpa, tag);
33713b2aab18SMatthew Ahrens 	return (error);
3372a9799022Sck }
3373a9799022Sck 
3374a9799022Sck static void
33753b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
3376a9799022Sck {
33773b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->shared_snaps, tag);
33783b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->clone_snaps, tag);
33793b2aab18SMatthew Ahrens 	snaplist_destroy(&ddpa->origin_snaps, tag);
33803b2aab18SMatthew Ahrens 	if (ddpa->origin_origin != NULL)
33813b2aab18SMatthew Ahrens 		dsl_dataset_rele(ddpa->origin_origin, tag);
33823b2aab18SMatthew Ahrens 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
33833b2aab18SMatthew Ahrens }
338402c8f3f0SMatthew Ahrens 
33853b2aab18SMatthew Ahrens /*
33863b2aab18SMatthew Ahrens  * Promote a clone.
33873b2aab18SMatthew Ahrens  *
33883b2aab18SMatthew Ahrens  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
33899adfa60dSMatthew Ahrens  * in with the name.  (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
33903b2aab18SMatthew Ahrens  */
33913b2aab18SMatthew Ahrens int
33923b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap)
33933b2aab18SMatthew Ahrens {
33943b2aab18SMatthew Ahrens 	dsl_dataset_promote_arg_t ddpa = { 0 };
33953b2aab18SMatthew Ahrens 	uint64_t numsnaps;
33963b2aab18SMatthew Ahrens 	int error;
3397dfc11533SChris Williamson 	nvpair_t *snap_pair;
33983b2aab18SMatthew Ahrens 	objset_t *os;
339992241e0bSTom Erickson 
34003b2aab18SMatthew Ahrens 	/*
34013b2aab18SMatthew Ahrens 	 * We will modify space proportional to the number of
34023b2aab18SMatthew Ahrens 	 * snapshots.  Compute numsnaps.
34033b2aab18SMatthew Ahrens 	 */
34043b2aab18SMatthew Ahrens 	error = dmu_objset_hold(name, FTAG, &os);
34053b2aab18SMatthew Ahrens 	if (error != 0)
34063b2aab18SMatthew Ahrens 		return (error);
34073b2aab18SMatthew Ahrens 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
3408c1379625SJustin T. Gibbs 	    dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
3409c1379625SJustin T. Gibbs 	    &numsnaps);
34103b2aab18SMatthew Ahrens 	dmu_objset_rele(os, FTAG);
34113b2aab18SMatthew Ahrens 	if (error != 0)
34123b2aab18SMatthew Ahrens 		return (error);
341302c8f3f0SMatthew Ahrens 
34143b2aab18SMatthew Ahrens 	ddpa.ddpa_clonename = name;
3415dfc11533SChris Williamson 	ddpa.err_ds = fnvlist_alloc();
3416a2afb611SJerry Jelinek 	ddpa.cr = CRED();
341702c8f3f0SMatthew Ahrens 
3418dfc11533SChris Williamson 	error = dsl_sync_task(name, dsl_dataset_promote_check,
34197d46dc6cSMatthew Ahrens 	    dsl_dataset_promote_sync, &ddpa,
3420dfc11533SChris Williamson 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED);
3421dfc11533SChris Williamson 
3422dfc11533SChris Williamson 	/*
3423dfc11533SChris Williamson 	 * Return the first conflicting snapshot found.
3424dfc11533SChris Williamson 	 */
3425dfc11533SChris Williamson 	snap_pair = nvlist_next_nvpair(ddpa.err_ds, NULL);
3426dfc11533SChris Williamson 	if (snap_pair != NULL && conflsnap != NULL)
3427dfc11533SChris Williamson 		(void) strcpy(conflsnap, nvpair_name(snap_pair));
3428dfc11533SChris Williamson 
3429dfc11533SChris Williamson 	fnvlist_free(ddpa.err_ds);
3430dfc11533SChris Williamson 	return (error);
3431a9799022Sck }
3432a9799022Sck 
3433a9799022Sck int
34343b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
343591948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
3436a9799022Sck {
34375f7a8e6dSDan McDonald 	/*
34385f7a8e6dSDan McDonald 	 * "slack" factor for received datasets with refquota set on them.
34395f7a8e6dSDan McDonald 	 * See the bottom of this function for details on its use.
34405f7a8e6dSDan McDonald 	 */
34415f7a8e6dSDan McDonald 	uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation;
34423b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
3443a9799022Sck 
34443b2aab18SMatthew Ahrens 	/* they should both be heads */
3445bc9014e6SJustin Gibbs 	if (clone->ds_is_snapshot ||
3446bc9014e6SJustin Gibbs 	    origin_head->ds_is_snapshot)
3447be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
344892241e0bSTom Erickson 
344934f2f8cfSMatthew Ahrens 	/* if we are not forcing, the branch point should be just before them */
345034f2f8cfSMatthew Ahrens 	if (!force && clone->ds_prev != origin_head->ds_prev)
3451be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3452a9799022Sck 
34533b2aab18SMatthew Ahrens 	/* clone should be the clone (unless they are unrelated) */
34543b2aab18SMatthew Ahrens 	if (clone->ds_prev != NULL &&
34553b2aab18SMatthew Ahrens 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
345634f2f8cfSMatthew Ahrens 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
3457be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
345892241e0bSTom Erickson 
34593b2aab18SMatthew Ahrens 	/* the clone should be a child of the origin */
34603b2aab18SMatthew Ahrens 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
3461be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3462842727c2SChris Kirby 
34633b2aab18SMatthew Ahrens 	/* origin_head shouldn't be modified unless 'force' */
346434f2f8cfSMatthew Ahrens 	if (!force &&
346534f2f8cfSMatthew Ahrens 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
3466be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
3467c99e4bdcSChris Kirby 
34683b2aab18SMatthew Ahrens 	/* origin_head should have no long holds (e.g. is not mounted) */
346991948b51SKeith M Wesolowski 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
3470be6fd75aSMatthew Ahrens 		return (SET_ERROR(EBUSY));
34713b2aab18SMatthew Ahrens 
34723b2aab18SMatthew Ahrens 	/* check amount of any unconsumed refreservation */
34733b2aab18SMatthew Ahrens 	unused_refres_delta =
34743b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
3475c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
34763b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
3477c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
34783b2aab18SMatthew Ahrens 
34793b2aab18SMatthew Ahrens 	if (unused_refres_delta > 0 &&
34803b2aab18SMatthew Ahrens 	    unused_refres_delta >
34813b2aab18SMatthew Ahrens 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
3482be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
34833b2aab18SMatthew Ahrens 
34845f7a8e6dSDan McDonald 	/*
34855f7a8e6dSDan McDonald 	 * The clone can't be too much over the head's refquota.
34865f7a8e6dSDan McDonald 	 *
34875f7a8e6dSDan McDonald 	 * To ensure that the entire refquota can be used, we allow one
34885f7a8e6dSDan McDonald 	 * transaction to exceed the the refquota.  Therefore, this check
34895f7a8e6dSDan McDonald 	 * needs to also allow for the space referenced to be more than the
34905f7a8e6dSDan McDonald 	 * refquota.  The maximum amount of space that one transaction can use
34915f7a8e6dSDan McDonald 	 * on disk is DMU_MAX_ACCESS * spa_asize_inflation.  Allowing this
34925f7a8e6dSDan McDonald 	 * overage ensures that we are able to receive a filesystem that
34935f7a8e6dSDan McDonald 	 * exceeds the refquota on the source system.
34945f7a8e6dSDan McDonald 	 *
34955f7a8e6dSDan McDonald 	 * So that overage is the refquota_slack we use below.
34965f7a8e6dSDan McDonald 	 */
34973b2aab18SMatthew Ahrens 	if (origin_head->ds_quota != 0 &&
3498c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes >
34995f7a8e6dSDan McDonald 	    origin_head->ds_quota + refquota_slack)
3500be6fd75aSMatthew Ahrens 		return (SET_ERROR(EDQUOT));
3501c99e4bdcSChris Kirby 
35023b2aab18SMatthew Ahrens 	return (0);
3503c99e4bdcSChris Kirby }
3504c99e4bdcSChris Kirby 
35055cabbc6bSPrashanth Sreenivasa static void
35065cabbc6bSPrashanth Sreenivasa dsl_dataset_swap_remap_deadlists(dsl_dataset_t *clone,
35075cabbc6bSPrashanth Sreenivasa     dsl_dataset_t *origin, dmu_tx_t *tx)
35085cabbc6bSPrashanth Sreenivasa {
35095cabbc6bSPrashanth Sreenivasa 	uint64_t clone_remap_dl_obj, origin_remap_dl_obj;
35105cabbc6bSPrashanth Sreenivasa 	dsl_pool_t *dp = dmu_tx_pool(tx);
35115cabbc6bSPrashanth Sreenivasa 
35125cabbc6bSPrashanth Sreenivasa 	ASSERT(dsl_pool_sync_context(dp));
35135cabbc6bSPrashanth Sreenivasa 
35145cabbc6bSPrashanth Sreenivasa 	clone_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(clone);
35155cabbc6bSPrashanth Sreenivasa 	origin_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(origin);
35165cabbc6bSPrashanth Sreenivasa 
35175cabbc6bSPrashanth Sreenivasa 	if (clone_remap_dl_obj != 0) {
35185cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_close(&clone->ds_remap_deadlist);
35195cabbc6bSPrashanth Sreenivasa 		dsl_dataset_unset_remap_deadlist_object(clone, tx);
35205cabbc6bSPrashanth Sreenivasa 	}
35215cabbc6bSPrashanth Sreenivasa 	if (origin_remap_dl_obj != 0) {
35225cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_close(&origin->ds_remap_deadlist);
35235cabbc6bSPrashanth Sreenivasa 		dsl_dataset_unset_remap_deadlist_object(origin, tx);
35245cabbc6bSPrashanth Sreenivasa 	}
35255cabbc6bSPrashanth Sreenivasa 
35265cabbc6bSPrashanth Sreenivasa 	if (clone_remap_dl_obj != 0) {
35275cabbc6bSPrashanth Sreenivasa 		dsl_dataset_set_remap_deadlist_object(origin,
35285cabbc6bSPrashanth Sreenivasa 		    clone_remap_dl_obj, tx);
35295cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_open(&origin->ds_remap_deadlist,
35305cabbc6bSPrashanth Sreenivasa 		    dp->dp_meta_objset, clone_remap_dl_obj);
35315cabbc6bSPrashanth Sreenivasa 	}
35325cabbc6bSPrashanth Sreenivasa 	if (origin_remap_dl_obj != 0) {
35335cabbc6bSPrashanth Sreenivasa 		dsl_dataset_set_remap_deadlist_object(clone,
35345cabbc6bSPrashanth Sreenivasa 		    origin_remap_dl_obj, tx);
35355cabbc6bSPrashanth Sreenivasa 		dsl_deadlist_open(&clone->ds_remap_deadlist,
35365cabbc6bSPrashanth Sreenivasa 		    dp->dp_meta_objset, origin_remap_dl_obj);
35375cabbc6bSPrashanth Sreenivasa 	}
35385cabbc6bSPrashanth Sreenivasa }
35395cabbc6bSPrashanth Sreenivasa 
3540a7f53a56SChris Kirby void
35413b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
35423b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx)
3543a7f53a56SChris Kirby {
35443b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
35453b2aab18SMatthew Ahrens 	int64_t unused_refres_delta;
3546a7f53a56SChris Kirby 
35473b2aab18SMatthew Ahrens 	ASSERT(clone->ds_reserved == 0);
35485f7a8e6dSDan McDonald 	/*
35495f7a8e6dSDan McDonald 	 * NOTE: On DEBUG kernels there could be a race between this and
35505f7a8e6dSDan McDonald 	 * the check function if spa_asize_inflation is adjusted...
35515f7a8e6dSDan McDonald 	 */
35523b2aab18SMatthew Ahrens 	ASSERT(origin_head->ds_quota == 0 ||
35535f7a8e6dSDan McDonald 	    dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
35545f7a8e6dSDan McDonald 	    DMU_MAX_ACCESS * spa_asize_inflation);
355534f2f8cfSMatthew Ahrens 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
3556842727c2SChris Kirby 
3557ca0cc391SMatthew Ahrens 	/*
3558ca0cc391SMatthew Ahrens 	 * Swap per-dataset feature flags.
3559ca0cc391SMatthew Ahrens 	 */
3560ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3561ca0cc391SMatthew Ahrens 		if (!(spa_feature_table[f].fi_flags &
3562ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET)) {
3563ca0cc391SMatthew Ahrens 			ASSERT(!clone->ds_feature_inuse[f]);
3564ca0cc391SMatthew Ahrens 			ASSERT(!origin_head->ds_feature_inuse[f]);
3565ca0cc391SMatthew Ahrens 			continue;
3566ca0cc391SMatthew Ahrens 		}
3567ca0cc391SMatthew Ahrens 
3568ca0cc391SMatthew Ahrens 		boolean_t clone_inuse = clone->ds_feature_inuse[f];
3569ca0cc391SMatthew Ahrens 		boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
3570ca0cc391SMatthew Ahrens 
3571ca0cc391SMatthew Ahrens 		if (clone_inuse) {
3572ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
3573ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_FALSE;
3574ca0cc391SMatthew Ahrens 		}
3575ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
3576ca0cc391SMatthew Ahrens 			dsl_dataset_deactivate_feature(origin_head->ds_object,
3577ca0cc391SMatthew Ahrens 			    f, tx);
3578ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_FALSE;
3579ca0cc391SMatthew Ahrens 		}
3580ca0cc391SMatthew Ahrens 		if (clone_inuse) {
3581ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(origin_head->ds_object,
3582ca0cc391SMatthew Ahrens 			    f, tx);
3583ca0cc391SMatthew Ahrens 			origin_head->ds_feature_inuse[f] = B_TRUE;
3584ca0cc391SMatthew Ahrens 		}
3585ca0cc391SMatthew Ahrens 		if (origin_head_inuse) {
3586ca0cc391SMatthew Ahrens 			dsl_dataset_activate_feature(clone->ds_object, f, tx);
3587ca0cc391SMatthew Ahrens 			clone->ds_feature_inuse[f] = B_TRUE;
3588ca0cc391SMatthew Ahrens 		}
3589ca0cc391SMatthew Ahrens 	}
3590ca0cc391SMatthew Ahrens 
35913b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
35923b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3593842727c2SChris Kirby 
35943b2aab18SMatthew Ahrens 	if (clone->ds_objset != NULL) {
35953b2aab18SMatthew Ahrens 		dmu_objset_evict(clone->ds_objset);
35963b2aab18SMatthew Ahrens 		clone->ds_objset = NULL;
35973b2aab18SMatthew Ahrens 	}
3598842727c2SChris Kirby 
35993b2aab18SMatthew Ahrens 	if (origin_head->ds_objset != NULL) {
36003b2aab18SMatthew Ahrens 		dmu_objset_evict(origin_head->ds_objset);
36013b2aab18SMatthew Ahrens 		origin_head->ds_objset = NULL;
3602842727c2SChris Kirby 	}
3603842727c2SChris Kirby 
36043b2aab18SMatthew Ahrens 	unused_refres_delta =
36053b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
3606c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
36073b2aab18SMatthew Ahrens 	    (int64_t)MIN(origin_head->ds_reserved,
3608c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
36093b2aab18SMatthew Ahrens 
36103b2aab18SMatthew Ahrens 	/*
36113b2aab18SMatthew Ahrens 	 * Reset origin's unique bytes, if it exists.
36123b2aab18SMatthew Ahrens 	 */
36133b2aab18SMatthew Ahrens 	if (clone->ds_prev) {
36143b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = clone->ds_prev;
36153b2aab18SMatthew Ahrens 		uint64_t comp, uncomp;
36163b2aab18SMatthew Ahrens 
36173b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
36183b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
3619c1379625SJustin T. Gibbs 		    dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
3620c1379625SJustin T. Gibbs 		    &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
36213b2aab18SMatthew Ahrens 	}
36223b2aab18SMatthew Ahrens 
36233b2aab18SMatthew Ahrens 	/* swap blkptrs */
36243b2aab18SMatthew Ahrens 	{
3625c166b69dSPaul Dagnelie 		rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
3626c166b69dSPaul Dagnelie 		rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
36273b2aab18SMatthew Ahrens 		blkptr_t tmp;
3628c1379625SJustin T. Gibbs 		tmp = dsl_dataset_phys(origin_head)->ds_bp;
3629c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_bp =
3630c1379625SJustin T. Gibbs 		    dsl_dataset_phys(clone)->ds_bp;
3631c1379625SJustin T. Gibbs 		dsl_dataset_phys(clone)->ds_bp = tmp;
3632c166b69dSPaul Dagnelie 		rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
3633c166b69dSPaul Dagnelie 		rrw_exit(&clone->ds_bp_rwlock, FTAG);
36343b2aab18SMatthew Ahrens 	}
36353b2aab18SMatthew Ahrens 
36363b2aab18SMatthew Ahrens 	/* set dd_*_bytes */
36373b2aab18SMatthew Ahrens 	{
36383b2aab18SMatthew Ahrens 		int64_t dused, dcomp, duncomp;
36393b2aab18SMatthew Ahrens 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
36403b2aab18SMatthew Ahrens 		uint64_t odl_used, odl_comp, odl_uncomp;
36413b2aab18SMatthew Ahrens 
3642c1379625SJustin T. Gibbs 		ASSERT3U(dsl_dir_phys(clone->ds_dir)->
36433b2aab18SMatthew Ahrens 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
36443b2aab18SMatthew Ahrens 
36453b2aab18SMatthew Ahrens 		dsl_deadlist_space(&clone->ds_deadlist,
36463b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
36473b2aab18SMatthew Ahrens 		dsl_deadlist_space(&origin_head->ds_deadlist,
36483b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
364915508ac0SChris Kirby 
3650c1379625SJustin T. Gibbs 		dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
3651c1379625SJustin T. Gibbs 		    cdl_used -
3652c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
3653c1379625SJustin T. Gibbs 		    odl_used);
3654c1379625SJustin T. Gibbs 		dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
3655c1379625SJustin T. Gibbs 		    cdl_comp -
3656c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
3657c1379625SJustin T. Gibbs 		    odl_comp);
3658c1379625SJustin T. Gibbs 		duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
36593b2aab18SMatthew Ahrens 		    cdl_uncomp -
3660c1379625SJustin T. Gibbs 		    (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
3661c1379625SJustin T. Gibbs 		    odl_uncomp);
3662842727c2SChris Kirby 
36633b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
36643b2aab18SMatthew Ahrens 		    dused, dcomp, duncomp, tx);
36653b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
36663b2aab18SMatthew Ahrens 		    -dused, -dcomp, -duncomp, tx);
3667842727c2SChris Kirby 
3668842727c2SChris Kirby 		/*
36693b2aab18SMatthew Ahrens 		 * The difference in the space used by snapshots is the
36703b2aab18SMatthew Ahrens 		 * difference in snapshot space due to the head's
36713b2aab18SMatthew Ahrens 		 * deadlist (since that's the only thing that's
36723b2aab18SMatthew Ahrens 		 * changing that affects the snapused).
3673842727c2SChris Kirby 		 */
36743b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&clone->ds_deadlist,
36753b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
36763b2aab18SMatthew Ahrens 		    &cdl_used, &cdl_comp, &cdl_uncomp);
36773b2aab18SMatthew Ahrens 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
36783b2aab18SMatthew Ahrens 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
36793b2aab18SMatthew Ahrens 		    &odl_used, &odl_comp, &odl_uncomp);
36803b2aab18SMatthew Ahrens 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
36813b2aab18SMatthew Ahrens 		    DD_USED_HEAD, DD_USED_SNAP, tx);
3682842727c2SChris Kirby 	}
3683842727c2SChris Kirby 
36843b2aab18SMatthew Ahrens 	/* swap ds_*_bytes */
3685c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
3686c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_referenced_bytes);
3687c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
3688c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_compressed_bytes);
3689c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
3690c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_uncompressed_bytes);
3691c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
3692c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_unique_bytes);
3693842727c2SChris Kirby 
36943b2aab18SMatthew Ahrens 	/* apply any parent delta for change in unconsumed refreservation */
36953b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
36963b2aab18SMatthew Ahrens 	    unused_refres_delta, 0, 0, tx);
3697ca45db41SChris Kirby 
36983b2aab18SMatthew Ahrens 	/*
36993b2aab18SMatthew Ahrens 	 * Swap deadlists.
37003b2aab18SMatthew Ahrens 	 */
37013b2aab18SMatthew Ahrens 	dsl_deadlist_close(&clone->ds_deadlist);
37023b2aab18SMatthew Ahrens 	dsl_deadlist_close(&origin_head->ds_deadlist);
3703c1379625SJustin T. Gibbs 	SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
3704c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
37053b2aab18SMatthew Ahrens 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
3706c1379625SJustin T. Gibbs 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
37073b2aab18SMatthew Ahrens 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
3708c1379625SJustin T. Gibbs 	    dsl_dataset_phys(origin_head)->ds_deadlist_obj);
37095cabbc6bSPrashanth Sreenivasa 	dsl_dataset_swap_remap_deadlists(clone, origin_head, tx);
3710842727c2SChris Kirby 
37113b2aab18SMatthew Ahrens 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
3712842727c2SChris Kirby 
37133b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(clone, "clone swap", tx,
37143b2aab18SMatthew Ahrens 	    "parent=%s", origin_head->ds_dir->dd_myname);
3715842727c2SChris Kirby }
3716842727c2SChris Kirby 
37173b2aab18SMatthew Ahrens /*
37183b2aab18SMatthew Ahrens  * Given a pool name and a dataset object number in that pool,
37193b2aab18SMatthew Ahrens  * return the name of that dataset.
37203b2aab18SMatthew Ahrens  */
3721a7f53a56SChris Kirby int
37223b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
3723a7f53a56SChris Kirby {
37243b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
37253b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
3726a7f53a56SChris Kirby 	int error;
3727a7f53a56SChris Kirby 
37283b2aab18SMatthew Ahrens 	error = dsl_pool_hold(pname, FTAG, &dp);
37293b2aab18SMatthew Ahrens 	if (error != 0)
37303b2aab18SMatthew Ahrens 		return (error);
37313b2aab18SMatthew Ahrens 
37323b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
37333b2aab18SMatthew Ahrens 	if (error == 0) {
37343b2aab18SMatthew Ahrens 		dsl_dataset_name(ds, buf);
37353b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
37363b2aab18SMatthew Ahrens 	}
37373b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3738a7f53a56SChris Kirby 
3739a7f53a56SChris Kirby 	return (error);
3740a7f53a56SChris Kirby }
3741a7f53a56SChris Kirby 
3742842727c2SChris Kirby int
37433b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
37443b2aab18SMatthew Ahrens     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
3745842727c2SChris Kirby {
37463b2aab18SMatthew Ahrens 	int error = 0;
3747842727c2SChris Kirby 
37483b2aab18SMatthew Ahrens 	ASSERT3S(asize, >, 0);
3749842727c2SChris Kirby 
37503b2aab18SMatthew Ahrens 	/*
37513b2aab18SMatthew Ahrens 	 * *ref_rsrv is the portion of asize that will come from any
37523b2aab18SMatthew Ahrens 	 * unconsumed refreservation space.
37533b2aab18SMatthew Ahrens 	 */
37543b2aab18SMatthew Ahrens 	*ref_rsrv = 0;
3755842727c2SChris Kirby 
37563b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
37573b2aab18SMatthew Ahrens 	/*
37583b2aab18SMatthew Ahrens 	 * Make a space adjustment for reserved bytes.
37593b2aab18SMatthew Ahrens 	 */
3760c1379625SJustin T. Gibbs 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
37613b2aab18SMatthew Ahrens 		ASSERT3U(*used, >=,
3762c1379625SJustin T. Gibbs 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
3763c1379625SJustin T. Gibbs 		*used -=
3764c1379625SJustin T. Gibbs 		    (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
37653b2aab18SMatthew Ahrens 		*ref_rsrv =
37663b2aab18SMatthew Ahrens 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
3767842727c2SChris Kirby 	}
3768842727c2SChris Kirby 
37693b2aab18SMatthew Ahrens 	if (!check_quota || ds->ds_quota == 0) {
37703b2aab18SMatthew Ahrens 		mutex_exit(&ds->ds_lock);
37713b2aab18SMatthew Ahrens 		return (0);
3772842727c2SChris Kirby 	}
37733b2aab18SMatthew Ahrens 	/*
37743b2aab18SMatthew Ahrens 	 * If they are requesting more space, and our current estimate
37753b2aab18SMatthew Ahrens 	 * is over quota, they get to try again unless the actual
37763b2aab18SMatthew Ahrens 	 * on-disk is over quota and there are no pending changes (which
37773b2aab18SMatthew Ahrens 	 * may free up space for us).
37783b2aab18SMatthew Ahrens 	 */
3779c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
3780c1379625SJustin T. Gibbs 	    ds->ds_quota) {
37813b2aab18SMatthew Ahrens 		if (inflight > 0 ||
3782c1379625SJustin T. Gibbs 		    dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
3783be6fd75aSMatthew Ahrens 			error = SET_ERROR(ERESTART);
37843b2aab18SMatthew Ahrens 		else
3785be6fd75aSMatthew Ahrens 			error = SET_ERROR(EDQUOT);
3786842727c2SChris Kirby 	}
37873b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3788842727c2SChris Kirby 
3789842727c2SChris Kirby 	return (error);
3790842727c2SChris Kirby }
3791842727c2SChris Kirby 
37923b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg {
37933b2aab18SMatthew Ahrens 	const char *ddsqra_name;
37943b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
37953b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
37963b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t;
3797842727c2SChris Kirby 
37983b2aab18SMatthew Ahrens 
37993b2aab18SMatthew Ahrens /* ARGSUSED */
3800842727c2SChris Kirby static int
38013b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
3802842727c2SChris Kirby {
38033b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
38043b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
38053b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
3806842727c2SChris Kirby 	int error;
38073b2aab18SMatthew Ahrens 	uint64_t newval;
3808842727c2SChris Kirby 
38093b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
3810be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3811842727c2SChris Kirby 
38123b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
38133b2aab18SMatthew Ahrens 	if (error != 0)
38143b2aab18SMatthew Ahrens 		return (error);
38153b2aab18SMatthew Ahrens 
3816bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
38173b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3818be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3819842727c2SChris Kirby 	}
3820842727c2SChris Kirby 
38213b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
38223b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
38233b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
38243b2aab18SMatthew Ahrens 	if (error != 0) {
38253b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3826842727c2SChris Kirby 		return (error);
3827842727c2SChris Kirby 	}
3828842727c2SChris Kirby 
38293b2aab18SMatthew Ahrens 	if (newval == 0) {
38303b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
38313b2aab18SMatthew Ahrens 		return (0);
38323b2aab18SMatthew Ahrens 	}
3833842727c2SChris Kirby 
3834c1379625SJustin T. Gibbs 	if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
38353b2aab18SMatthew Ahrens 	    newval < ds->ds_reserved) {
38363b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3837be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
38383b2aab18SMatthew Ahrens 	}
38393b2aab18SMatthew Ahrens 
38403b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3841842727c2SChris Kirby 	return (0);
3842842727c2SChris Kirby }
3843842727c2SChris Kirby 
38443b2aab18SMatthew Ahrens static void
38453b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
3846842727c2SChris Kirby {
38473b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
38483b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
38493b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
38503b2aab18SMatthew Ahrens 	uint64_t newval;
3851842727c2SChris Kirby 
38523b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
3853842727c2SChris Kirby 
38543b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds,
38553b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
38563b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
38573b2aab18SMatthew Ahrens 	    &ddsqra->ddsqra_value, tx);
3858842727c2SChris Kirby 
38593b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
38603b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
3861842727c2SChris Kirby 
38623b2aab18SMatthew Ahrens 	if (ds->ds_quota != newval) {
38633b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
38643b2aab18SMatthew Ahrens 		ds->ds_quota = newval;
3865842727c2SChris Kirby 	}
38663b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
3867842727c2SChris Kirby }
3868842727c2SChris Kirby 
38693b2aab18SMatthew Ahrens int
38703b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
38713b2aab18SMatthew Ahrens     uint64_t refquota)
3872842727c2SChris Kirby {
38733b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3874842727c2SChris Kirby 
38753b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
38763b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
38773b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refquota;
38783b2aab18SMatthew Ahrens 
38793b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
388086714001SSerapheim Dimitropoulos 	    dsl_dataset_set_refquota_sync, &ddsqra, 0,
388186714001SSerapheim Dimitropoulos 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
3882842727c2SChris Kirby }
3883842727c2SChris Kirby 
3884842727c2SChris Kirby static int
38853b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
3886842727c2SChris Kirby {
38873b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
38883b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3889842727c2SChris Kirby 	dsl_dataset_t *ds;
3890842727c2SChris Kirby 	int error;
38913b2aab18SMatthew Ahrens 	uint64_t newval, unique;
3892d7747cbcSChris Kirby 
38933b2aab18SMatthew Ahrens 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
3894be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
3895842727c2SChris Kirby 
38963b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
38973b2aab18SMatthew Ahrens 	if (error != 0)
3898842727c2SChris Kirby 		return (error);
3899842727c2SChris Kirby 
3900bc9014e6SJustin Gibbs 	if (ds->ds_is_snapshot) {
39013b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3902be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
3903842727c2SChris Kirby 	}
3904842727c2SChris Kirby 
39053b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
39063b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
39073b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
39083b2aab18SMatthew Ahrens 	if (error != 0) {
39093b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
3910842727c2SChris Kirby 		return (error);
3911842727c2SChris Kirby 	}
3912842727c2SChris Kirby 
39133b2aab18SMatthew Ahrens 	/*
39143b2aab18SMatthew Ahrens 	 * If we are doing the preliminary check in open context, the
39153b2aab18SMatthew Ahrens 	 * space estimates may be inaccurate.
39163b2aab18SMatthew Ahrens 	 */
39173b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
39183b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
39193b2aab18SMatthew Ahrens 		return (0);
3920842727c2SChris Kirby 	}
3921842727c2SChris Kirby 
39223b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
39233b2aab18SMatthew Ahrens 	if (!DS_UNIQUE_IS_ACCURATE(ds))
39243b2aab18SMatthew Ahrens 		dsl_dataset_recalc_head_uniq(ds);
3925c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
39263b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3927842727c2SChris Kirby 
39283b2aab18SMatthew Ahrens 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
39293b2aab18SMatthew Ahrens 		uint64_t delta = MAX(unique, newval) -
39303b2aab18SMatthew Ahrens 		    MAX(unique, ds->ds_reserved);
3931842727c2SChris Kirby 
39323b2aab18SMatthew Ahrens 		if (delta >
39333b2aab18SMatthew Ahrens 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
39343b2aab18SMatthew Ahrens 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
39353b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
3936be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOSPC));
39373b2aab18SMatthew Ahrens 		}
3938842727c2SChris Kirby 	}
3939842727c2SChris Kirby 
39403b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
39413b2aab18SMatthew Ahrens 	return (0);
3942842727c2SChris Kirby }
3943842727c2SChris Kirby 
39443b2aab18SMatthew Ahrens void
39453b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
39463b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
3947ca45db41SChris Kirby {
39483b2aab18SMatthew Ahrens 	uint64_t newval;
39493b2aab18SMatthew Ahrens 	uint64_t unique;
39503b2aab18SMatthew Ahrens 	int64_t delta;
3951ca45db41SChris Kirby 
39523b2aab18SMatthew Ahrens 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
39533b2aab18SMatthew Ahrens 	    source, sizeof (value), 1, &value, tx);
3954ca45db41SChris Kirby 
39553b2aab18SMatthew Ahrens 	VERIFY0(dsl_prop_get_int_ds(ds,
39563b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
3957a7f53a56SChris Kirby 
39583b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
39593b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
39603b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_lock);
39613b2aab18SMatthew Ahrens 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
3962c1379625SJustin T. Gibbs 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
39633b2aab18SMatthew Ahrens 	delta = MAX(0, (int64_t)(newval - unique)) -
39643b2aab18SMatthew Ahrens 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
39653b2aab18SMatthew Ahrens 	ds->ds_reserved = newval;
39663b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_lock);
3967a7f53a56SChris Kirby 
39683b2aab18SMatthew Ahrens 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
39693b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
3970ca45db41SChris Kirby }
3971ca45db41SChris Kirby 
39723b2aab18SMatthew Ahrens static void
39733b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
3974842727c2SChris Kirby {
39753b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
39763b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
3977842727c2SChris Kirby 	dsl_dataset_t *ds;
3978842727c2SChris Kirby 
39793b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
39803b2aab18SMatthew Ahrens 	dsl_dataset_set_refreservation_sync_impl(ds,
39813b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
3982842727c2SChris Kirby 	dsl_dataset_rele(ds, FTAG);
3983842727c2SChris Kirby }
3984503ad85cSMatthew Ahrens 
3985503ad85cSMatthew Ahrens int
39863b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
39873b2aab18SMatthew Ahrens     uint64_t refreservation)
3988503ad85cSMatthew Ahrens {
39893b2aab18SMatthew Ahrens 	dsl_dataset_set_qr_arg_t ddsqra;
3990503ad85cSMatthew Ahrens 
39913b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = dsname;
39923b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
39933b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = refreservation;
39943b2aab18SMatthew Ahrens 
39953b2aab18SMatthew Ahrens 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
399686714001SSerapheim Dimitropoulos 	    dsl_dataset_set_refreservation_sync, &ddsqra, 0,
399786714001SSerapheim Dimitropoulos 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
3998503ad85cSMatthew Ahrens }
399919b94df9SMatthew Ahrens 
400019b94df9SMatthew Ahrens /*
400119b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space written in new that is not
400219b94df9SMatthew Ahrens  * present in oldsnap.  New may be a snapshot or the head.  Old must be
400319b94df9SMatthew Ahrens  * a snapshot before new, in new's filesystem (or its origin).  If not then
400419b94df9SMatthew Ahrens  * fail and return EINVAL.
400519b94df9SMatthew Ahrens  *
400619b94df9SMatthew Ahrens  * The written space is calculated by considering two components:  First, we
400719b94df9SMatthew Ahrens  * ignore any freed space, and calculate the written as new's used space
400819b94df9SMatthew Ahrens  * minus old's used space.  Next, we add in the amount of space that was freed
400919b94df9SMatthew Ahrens  * between the two snapshots, thus reducing new's used space relative to old's.
401019b94df9SMatthew Ahrens  * Specifically, this is the space that was born before old->ds_creation_txg,
401119b94df9SMatthew Ahrens  * and freed before new (ie. on new's deadlist or a previous deadlist).
401219b94df9SMatthew Ahrens  *
401319b94df9SMatthew Ahrens  * space freed                         [---------------------]
401419b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O------
401519b94df9SMatthew Ahrens  *                                         oldsnap            new
401619b94df9SMatthew Ahrens  */
401719b94df9SMatthew Ahrens int
401819b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
401919b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
402019b94df9SMatthew Ahrens {
402119b94df9SMatthew Ahrens 	int err = 0;
402219b94df9SMatthew Ahrens 	uint64_t snapobj;
402319b94df9SMatthew Ahrens 	dsl_pool_t *dp = new->ds_dir->dd_pool;
402419b94df9SMatthew Ahrens 
40253b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
40263b2aab18SMatthew Ahrens 
402719b94df9SMatthew Ahrens 	*usedp = 0;
4028c1379625SJustin T. Gibbs 	*usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
4029c1379625SJustin T. Gibbs 	*usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes;
403019b94df9SMatthew Ahrens 
403119b94df9SMatthew Ahrens 	*compp = 0;
4032c1379625SJustin T. Gibbs 	*compp += dsl_dataset_phys(new)->ds_compressed_bytes;
4033c1379625SJustin T. Gibbs 	*compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes;
403419b94df9SMatthew Ahrens 
403519b94df9SMatthew Ahrens 	*uncompp = 0;
4036c1379625SJustin T. Gibbs 	*uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
4037c1379625SJustin T. Gibbs 	*uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes;
403819b94df9SMatthew Ahrens 
403919b94df9SMatthew Ahrens 	snapobj = new->ds_object;
404019b94df9SMatthew Ahrens 	while (snapobj != oldsnap->ds_object) {
404119b94df9SMatthew Ahrens 		dsl_dataset_t *snap;
404219b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
404319b94df9SMatthew Ahrens 
4044ad135b5dSChristopher Siden 		if (snapobj == new->ds_object) {
4045ad135b5dSChristopher Siden 			snap = new;
4046ad135b5dSChristopher Siden 		} else {
4047ad135b5dSChristopher Siden 			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
4048ad135b5dSChristopher Siden 			if (err != 0)
4049ad135b5dSChristopher Siden 				break;
4050ad135b5dSChristopher Siden 		}
405119b94df9SMatthew Ahrens 
4052c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(snap)->ds_prev_snap_txg ==
4053c1379625SJustin T. Gibbs 		    dsl_dataset_phys(oldsnap)->ds_creation_txg) {
405419b94df9SMatthew Ahrens 			/*
405519b94df9SMatthew Ahrens 			 * The blocks in the deadlist can not be born after
405619b94df9SMatthew Ahrens 			 * ds_prev_snap_txg, so get the whole deadlist space,
405719b94df9SMatthew Ahrens 			 * which is more efficient (especially for old-format
405819b94df9SMatthew Ahrens 			 * deadlists).  Unfortunately the deadlist code
405919b94df9SMatthew Ahrens 			 * doesn't have enough information to make this
406019b94df9SMatthew Ahrens 			 * optimization itself.
406119b94df9SMatthew Ahrens 			 */
406219b94df9SMatthew Ahrens 			dsl_deadlist_space(&snap->ds_deadlist,
406319b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
406419b94df9SMatthew Ahrens 		} else {
406519b94df9SMatthew Ahrens 			dsl_deadlist_space_range(&snap->ds_deadlist,
4066c1379625SJustin T. Gibbs 			    0, dsl_dataset_phys(oldsnap)->ds_creation_txg,
406719b94df9SMatthew Ahrens 			    &used, &comp, &uncomp);
406819b94df9SMatthew Ahrens 		}
406919b94df9SMatthew Ahrens 		*usedp += used;
407019b94df9SMatthew Ahrens 		*compp += comp;
407119b94df9SMatthew Ahrens 		*uncompp += uncomp;
407219b94df9SMatthew Ahrens 
407319b94df9SMatthew Ahrens 		/*
407419b94df9SMatthew Ahrens 		 * If we get to the beginning of the chain of snapshots
407519b94df9SMatthew Ahrens 		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
407619b94df9SMatthew Ahrens 		 * was not a snapshot of/before new.
407719b94df9SMatthew Ahrens 		 */
4078c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
4079ad135b5dSChristopher Siden 		if (snap != new)
4080ad135b5dSChristopher Siden 			dsl_dataset_rele(snap, FTAG);
408119b94df9SMatthew Ahrens 		if (snapobj == 0) {
4082be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINVAL);
408319b94df9SMatthew Ahrens 			break;
408419b94df9SMatthew Ahrens 		}
408519b94df9SMatthew Ahrens 
408619b94df9SMatthew Ahrens 	}
408719b94df9SMatthew Ahrens 	return (err);
408819b94df9SMatthew Ahrens }
408919b94df9SMatthew Ahrens 
409019b94df9SMatthew Ahrens /*
409119b94df9SMatthew Ahrens  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
409219b94df9SMatthew Ahrens  * lastsnap, and all snapshots in between are deleted.
409319b94df9SMatthew Ahrens  *
409419b94df9SMatthew Ahrens  * blocks that would be freed            [---------------------------]
409519b94df9SMatthew Ahrens  * snapshots                       ---O-------O--------O-------O--------O
409619b94df9SMatthew Ahrens  *                                        firstsnap        lastsnap
409719b94df9SMatthew Ahrens  *
409819b94df9SMatthew Ahrens  * This is the set of blocks that were born after the snap before firstsnap,
409919b94df9SMatthew Ahrens  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
410019b94df9SMatthew Ahrens  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
410119b94df9SMatthew Ahrens  * We calculate this by iterating over the relevant deadlists (from the snap
410219b94df9SMatthew Ahrens  * after lastsnap, backward to the snap after firstsnap), summing up the
410319b94df9SMatthew Ahrens  * space on the deadlist that was born after the snap before firstsnap.
410419b94df9SMatthew Ahrens  */
410519b94df9SMatthew Ahrens int
410619b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
410719b94df9SMatthew Ahrens     dsl_dataset_t *lastsnap,
410819b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
410919b94df9SMatthew Ahrens {
411019b94df9SMatthew Ahrens 	int err = 0;
411119b94df9SMatthew Ahrens 	uint64_t snapobj;
411219b94df9SMatthew Ahrens 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
411319b94df9SMatthew Ahrens 
4114bc9014e6SJustin Gibbs 	ASSERT(firstsnap->ds_is_snapshot);
4115bc9014e6SJustin Gibbs 	ASSERT(lastsnap->ds_is_snapshot);
411619b94df9SMatthew Ahrens 
411719b94df9SMatthew Ahrens 	/*
411819b94df9SMatthew Ahrens 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
411919b94df9SMatthew Ahrens 	 * is before lastsnap.
412019b94df9SMatthew Ahrens 	 */
412119b94df9SMatthew Ahrens 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
4122c1379625SJustin T. Gibbs 	    dsl_dataset_phys(firstsnap)->ds_creation_txg >
4123c1379625SJustin T. Gibbs 	    dsl_dataset_phys(lastsnap)->ds_creation_txg)
4124be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
412519b94df9SMatthew Ahrens 
412619b94df9SMatthew Ahrens 	*usedp = *compp = *uncompp = 0;
412719b94df9SMatthew Ahrens 
4128c1379625SJustin T. Gibbs 	snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
412919b94df9SMatthew Ahrens 	while (snapobj != firstsnap->ds_object) {
413019b94df9SMatthew Ahrens 		dsl_dataset_t *ds;
413119b94df9SMatthew Ahrens 		uint64_t used, comp, uncomp;
413219b94df9SMatthew Ahrens 
413319b94df9SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
413419b94df9SMatthew Ahrens 		if (err != 0)
413519b94df9SMatthew Ahrens 			break;
413619b94df9SMatthew Ahrens 
413719b94df9SMatthew Ahrens 		dsl_deadlist_space_range(&ds->ds_deadlist,
4138c1379625SJustin T. Gibbs 		    dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
413919b94df9SMatthew Ahrens 		    &used, &comp, &uncomp);
414019b94df9SMatthew Ahrens 		*usedp += used;
414119b94df9SMatthew Ahrens 		*compp += comp;
414219b94df9SMatthew Ahrens 		*uncompp += uncomp;
414319b94df9SMatthew Ahrens 
4144c1379625SJustin T. Gibbs 		snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
414519b94df9SMatthew Ahrens 		ASSERT3U(snapobj, !=, 0);
414619b94df9SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
414719b94df9SMatthew Ahrens 	}
414819b94df9SMatthew Ahrens 	return (err);
414919b94df9SMatthew Ahrens }
41503b2aab18SMatthew Ahrens 
41513b2aab18SMatthew Ahrens /*
41523b2aab18SMatthew Ahrens  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
41533b2aab18SMatthew Ahrens  * For example, they could both be snapshots of the same filesystem, and
41543b2aab18SMatthew Ahrens  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
41553b2aab18SMatthew Ahrens  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
41563b2aab18SMatthew Ahrens  * filesystem.  Or 'earlier' could be the origin's origin.
415778f17100SMatthew Ahrens  *
415878f17100SMatthew Ahrens  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
41593b2aab18SMatthew Ahrens  */
41603b2aab18SMatthew Ahrens boolean_t
416178f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
41629a686fbcSPaul Dagnelie     uint64_t earlier_txg)
41633b2aab18SMatthew Ahrens {
41643b2aab18SMatthew Ahrens 	dsl_pool_t *dp = later->ds_dir->dd_pool;
41653b2aab18SMatthew Ahrens 	int error;
41663b2aab18SMatthew Ahrens 	boolean_t ret;
41673b2aab18SMatthew Ahrens 
41683b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
4169bc9014e6SJustin Gibbs 	ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
417078f17100SMatthew Ahrens 
417178f17100SMatthew Ahrens 	if (earlier_txg == 0)
4172c1379625SJustin T. Gibbs 		earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
41733b2aab18SMatthew Ahrens 
4174bc9014e6SJustin Gibbs 	if (later->ds_is_snapshot &&
4175c1379625SJustin T. Gibbs 	    earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
41763b2aab18SMatthew Ahrens 		return (B_FALSE);
41773b2aab18SMatthew Ahrens 
41783b2aab18SMatthew Ahrens 	if (later->ds_dir == earlier->ds_dir)
41793b2aab18SMatthew Ahrens 		return (B_TRUE);
41803b2aab18SMatthew Ahrens 	if (!dsl_dir_is_clone(later->ds_dir))
41813b2aab18SMatthew Ahrens 		return (B_FALSE);
41823b2aab18SMatthew Ahrens 
4183c1379625SJustin T. Gibbs 	if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object)
41843b2aab18SMatthew Ahrens 		return (B_TRUE);
41853b2aab18SMatthew Ahrens 	dsl_dataset_t *origin;
41863b2aab18SMatthew Ahrens 	error = dsl_dataset_hold_obj(dp,
4187c1379625SJustin T. Gibbs 	    dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
41883b2aab18SMatthew Ahrens 	if (error != 0)
41893b2aab18SMatthew Ahrens 		return (B_FALSE);
419078f17100SMatthew Ahrens 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
41913b2aab18SMatthew Ahrens 	dsl_dataset_rele(origin, FTAG);
41923b2aab18SMatthew Ahrens 	return (ret);
41933b2aab18SMatthew Ahrens }
41942acef22dSMatthew Ahrens 
41952acef22dSMatthew Ahrens void
41962acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
41972acef22dSMatthew Ahrens {
41982acef22dSMatthew Ahrens 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
41992acef22dSMatthew Ahrens 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
42002acef22dSMatthew Ahrens }
42019c3fd121SMatthew Ahrens 
42029c3fd121SMatthew Ahrens boolean_t
42039c3fd121SMatthew Ahrens dsl_dataset_is_zapified(dsl_dataset_t *ds)
42049c3fd121SMatthew Ahrens {
42059c3fd121SMatthew Ahrens 	dmu_object_info_t doi;
42069c3fd121SMatthew Ahrens 
42079c3fd121SMatthew Ahrens 	dmu_object_info_from_db(ds->ds_dbuf, &doi);
42089c3fd121SMatthew Ahrens 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
42099c3fd121SMatthew Ahrens }
42109c3fd121SMatthew Ahrens 
42119c3fd121SMatthew Ahrens boolean_t
42129c3fd121SMatthew Ahrens dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
42139c3fd121SMatthew Ahrens {
42149c3fd121SMatthew Ahrens 	return (dsl_dataset_is_zapified(ds) &&
42159c3fd121SMatthew Ahrens 	    zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
42169c3fd121SMatthew Ahrens 	    ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
42179c3fd121SMatthew Ahrens }
42185cabbc6bSPrashanth Sreenivasa 
42195cabbc6bSPrashanth Sreenivasa uint64_t
42205cabbc6bSPrashanth Sreenivasa dsl_dataset_get_remap_deadlist_object(dsl_dataset_t *ds)
42215cabbc6bSPrashanth Sreenivasa {
42225cabbc6bSPrashanth Sreenivasa 	uint64_t remap_deadlist_obj;
42235cabbc6bSPrashanth Sreenivasa 	int err;
42245cabbc6bSPrashanth Sreenivasa 
42255cabbc6bSPrashanth Sreenivasa 	if (!dsl_dataset_is_zapified(ds))
42265cabbc6bSPrashanth Sreenivasa 		return (0);
42275cabbc6bSPrashanth Sreenivasa 
42285cabbc6bSPrashanth Sreenivasa 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
42295cabbc6bSPrashanth Sreenivasa 	    DS_FIELD_REMAP_DEADLIST, sizeof (remap_deadlist_obj), 1,
42305cabbc6bSPrashanth Sreenivasa 	    &remap_deadlist_obj);
42315cabbc6bSPrashanth Sreenivasa 
42325cabbc6bSPrashanth Sreenivasa 	if (err != 0) {
42335cabbc6bSPrashanth Sreenivasa 		VERIFY3S(err, ==, ENOENT);
42345cabbc6bSPrashanth Sreenivasa 		return (0);
42355cabbc6bSPrashanth Sreenivasa 	}
42365cabbc6bSPrashanth Sreenivasa 
42375cabbc6bSPrashanth Sreenivasa 	ASSERT(remap_deadlist_obj != 0);
42385cabbc6bSPrashanth Sreenivasa 	return (remap_deadlist_obj);
42395cabbc6bSPrashanth Sreenivasa }
42405cabbc6bSPrashanth Sreenivasa 
42415cabbc6bSPrashanth Sreenivasa boolean_t
42425cabbc6bSPrashanth Sreenivasa dsl_dataset_remap_deadlist_exists(dsl_dataset_t *ds)
42435cabbc6bSPrashanth Sreenivasa {
42445cabbc6bSPrashanth Sreenivasa 	EQUIV(dsl_deadlist_is_open(&ds->ds_remap_deadlist),
42455cabbc6bSPrashanth Sreenivasa 	    dsl_dataset_get_remap_deadlist_object(ds) != 0);
42465cabbc6bSPrashanth Sreenivasa 	return (dsl_deadlist_is_open(&ds->ds_remap_deadlist));
42475cabbc6bSPrashanth Sreenivasa }
42485cabbc6bSPrashanth Sreenivasa 
42495cabbc6bSPrashanth Sreenivasa static void
42505cabbc6bSPrashanth Sreenivasa dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds, uint64_t obj,
42515cabbc6bSPrashanth Sreenivasa     dmu_tx_t *tx)
42525cabbc6bSPrashanth Sreenivasa {
42535cabbc6bSPrashanth Sreenivasa 	ASSERT(obj != 0);
42545cabbc6bSPrashanth Sreenivasa 	dsl_dataset_zapify(ds, tx);
42555cabbc6bSPrashanth Sreenivasa 	VERIFY0(zap_add(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
42565cabbc6bSPrashanth Sreenivasa 	    DS_FIELD_REMAP_DEADLIST, sizeof (obj), 1, &obj, tx));
42575cabbc6bSPrashanth Sreenivasa }
42585cabbc6bSPrashanth Sreenivasa 
42595cabbc6bSPrashanth Sreenivasa static void
42605cabbc6bSPrashanth Sreenivasa dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds, dmu_tx_t *tx)
42615cabbc6bSPrashanth Sreenivasa {
42625cabbc6bSPrashanth Sreenivasa 	VERIFY0(zap_remove(ds->ds_dir->dd_pool->dp_meta_objset,
42635cabbc6bSPrashanth Sreenivasa 	    ds->ds_object, DS_FIELD_REMAP_DEADLIST, tx));
42645cabbc6bSPrashanth Sreenivasa }
42655cabbc6bSPrashanth Sreenivasa 
42665cabbc6bSPrashanth Sreenivasa void
42675cabbc6bSPrashanth Sreenivasa dsl_dataset_destroy_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
42685cabbc6bSPrashanth Sreenivasa {
42695cabbc6bSPrashanth Sreenivasa 	uint64_t remap_deadlist_object;
42705cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
42715cabbc6bSPrashanth Sreenivasa 
42725cabbc6bSPrashanth Sreenivasa 	ASSERT(dmu_tx_is_syncing(tx));
42735cabbc6bSPrashanth Sreenivasa 	ASSERT(dsl_dataset_remap_deadlist_exists(ds));
42745cabbc6bSPrashanth Sreenivasa 
42755cabbc6bSPrashanth Sreenivasa 	remap_deadlist_object = ds->ds_remap_deadlist.dl_object;
42765cabbc6bSPrashanth Sreenivasa 	dsl_deadlist_close(&ds->ds_remap_deadlist);
42775cabbc6bSPrashanth Sreenivasa 	dsl_deadlist_free(spa_meta_objset(spa), remap_deadlist_object, tx);
42785cabbc6bSPrashanth Sreenivasa 	dsl_dataset_unset_remap_deadlist_object(ds, tx);
42795cabbc6bSPrashanth Sreenivasa 	spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
42805cabbc6bSPrashanth Sreenivasa }
42815cabbc6bSPrashanth Sreenivasa 
42825cabbc6bSPrashanth Sreenivasa void
42835cabbc6bSPrashanth Sreenivasa dsl_dataset_create_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
42845cabbc6bSPrashanth Sreenivasa {
42855cabbc6bSPrashanth Sreenivasa 	uint64_t remap_deadlist_obj;
42865cabbc6bSPrashanth Sreenivasa 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
42875cabbc6bSPrashanth Sreenivasa 
42885cabbc6bSPrashanth Sreenivasa 	ASSERT(dmu_tx_is_syncing(tx));
42895cabbc6bSPrashanth Sreenivasa 	ASSERT(MUTEX_HELD(&ds->ds_remap_deadlist_lock));
42905cabbc6bSPrashanth Sreenivasa 	/*
42915cabbc6bSPrashanth Sreenivasa 	 * Currently we only create remap deadlists when there are indirect
42925cabbc6bSPrashanth Sreenivasa 	 * vdevs with referenced mappings.
42935cabbc6bSPrashanth Sreenivasa 	 */
42945cabbc6bSPrashanth Sreenivasa 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
42955cabbc6bSPrashanth Sreenivasa 
42965cabbc6bSPrashanth Sreenivasa 	remap_deadlist_obj = dsl_deadlist_clone(
42975cabbc6bSPrashanth Sreenivasa 	    &ds->ds_deadlist, UINT64_MAX,
42985cabbc6bSPrashanth Sreenivasa 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
42995cabbc6bSPrashanth Sreenivasa 	dsl_dataset_set_remap_deadlist_object(ds,
43005cabbc6bSPrashanth Sreenivasa 	    remap_deadlist_obj, tx);
43015cabbc6bSPrashanth Sreenivasa 	dsl_deadlist_open(&ds->ds_remap_deadlist, spa_meta_objset(spa),
43025cabbc6bSPrashanth Sreenivasa 	    remap_deadlist_obj);
43035cabbc6bSPrashanth Sreenivasa 	spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
43045cabbc6bSPrashanth Sreenivasa }
4305