xref: /freebsd/sys/contrib/openzfs/module/zfs/dsl_dataset.c (revision e2df9bb44109577475aeb186e7186ac040f9bde1)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2014 RackTop Systems.
27eda14cbcSMatt Macy  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28eda14cbcSMatt Macy  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
29eda14cbcSMatt Macy  * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
30eda14cbcSMatt Macy  * Copyright 2017 Nexenta Systems, Inc.
31eda14cbcSMatt Macy  * Copyright (c) 2019, Klara Inc.
32eda14cbcSMatt Macy  * Copyright (c) 2019, Allan Jude
33eda14cbcSMatt Macy  * Copyright (c) 2020 The FreeBSD Foundation [1]
34eda14cbcSMatt Macy  *
35eda14cbcSMatt Macy  * [1] Portions of this software were developed by Allan Jude
36eda14cbcSMatt Macy  *     under sponsorship from the FreeBSD Foundation.
37eda14cbcSMatt Macy  */
38eda14cbcSMatt Macy 
39eda14cbcSMatt Macy #include <sys/dmu_objset.h>
40eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
41eda14cbcSMatt Macy #include <sys/dsl_dir.h>
42eda14cbcSMatt Macy #include <sys/dsl_prop.h>
43eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
44eda14cbcSMatt Macy #include <sys/dmu_traverse.h>
45eda14cbcSMatt Macy #include <sys/dmu_impl.h>
46eda14cbcSMatt Macy #include <sys/dmu_tx.h>
47eda14cbcSMatt Macy #include <sys/arc.h>
48eda14cbcSMatt Macy #include <sys/zio.h>
49eda14cbcSMatt Macy #include <sys/zap.h>
50eda14cbcSMatt Macy #include <sys/zfeature.h>
51eda14cbcSMatt Macy #include <sys/unique.h>
52eda14cbcSMatt Macy #include <sys/zfs_context.h>
53eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
54eda14cbcSMatt Macy #include <sys/spa.h>
55eda14cbcSMatt Macy #include <sys/spa_impl.h>
56eda14cbcSMatt Macy #include <sys/vdev.h>
57eda14cbcSMatt Macy #include <sys/zfs_znode.h>
58eda14cbcSMatt Macy #include <sys/zfs_onexit.h>
59eda14cbcSMatt Macy #include <sys/zvol.h>
60eda14cbcSMatt Macy #include <sys/dsl_scan.h>
61eda14cbcSMatt Macy #include <sys/dsl_deadlist.h>
62eda14cbcSMatt Macy #include <sys/dsl_destroy.h>
63eda14cbcSMatt Macy #include <sys/dsl_userhold.h>
64eda14cbcSMatt Macy #include <sys/dsl_bookmark.h>
65eda14cbcSMatt Macy #include <sys/policy.h>
66eda14cbcSMatt Macy #include <sys/dmu_send.h>
67eda14cbcSMatt Macy #include <sys/dmu_recv.h>
68eda14cbcSMatt Macy #include <sys/zio_compress.h>
69eda14cbcSMatt Macy #include <zfs_fletcher.h>
70eda14cbcSMatt Macy #include <sys/zio_checksum.h>
71eda14cbcSMatt Macy 
72eda14cbcSMatt Macy /*
73eda14cbcSMatt Macy  * The SPA supports block sizes up to 16MB.  However, very large blocks
74eda14cbcSMatt Macy  * can have an impact on i/o latency (e.g. tying up a spinning disk for
75eda14cbcSMatt Macy  * ~300ms), and also potentially on the memory allocator.  Therefore,
76716fd348SMartin Matuska  * we did not allow the recordsize to be set larger than zfs_max_recordsize
77716fd348SMartin Matuska  * (former default: 1MB).  Larger blocks could be created by changing this
78716fd348SMartin Matuska  * tunable, and pools with larger blocks could always be imported and used,
79716fd348SMartin Matuska  * regardless of this setting.
80716fd348SMartin Matuska  *
81716fd348SMartin Matuska  * We do, however, still limit it by default to 1M on x86_32, because Linux's
82716fd348SMartin Matuska  * 3/1 memory split doesn't leave much room for 16M chunks.
83eda14cbcSMatt Macy  */
84716fd348SMartin Matuska #ifdef _ILP32
85be181ee2SMartin Matuska uint_t zfs_max_recordsize =  1 * 1024 * 1024;
86716fd348SMartin Matuska #else
87be181ee2SMartin Matuska uint_t zfs_max_recordsize = 16 * 1024 * 1024;
88716fd348SMartin Matuska #endif
89e92ffd9bSMartin Matuska static int zfs_allow_redacted_dataset_mount = 0;
90eda14cbcSMatt Macy 
9108aba0aeSMartin Matuska int zfs_snapshot_history_enabled = 1;
9208aba0aeSMartin Matuska 
93eda14cbcSMatt Macy #define	SWITCH64(x, y) \
94eda14cbcSMatt Macy 	{ \
95eda14cbcSMatt Macy 		uint64_t __tmp = (x); \
96eda14cbcSMatt Macy 		(x) = (y); \
97eda14cbcSMatt Macy 		(y) = __tmp; \
98eda14cbcSMatt Macy 	}
99eda14cbcSMatt Macy 
100eda14cbcSMatt Macy #define	DS_REF_MAX	(1ULL << 62)
101eda14cbcSMatt Macy 
102eda14cbcSMatt Macy static void dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds,
103eda14cbcSMatt Macy     uint64_t obj, dmu_tx_t *tx);
104eda14cbcSMatt Macy static void dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds,
105eda14cbcSMatt Macy     dmu_tx_t *tx);
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy static void unload_zfeature(dsl_dataset_t *ds, spa_feature_t f);
108eda14cbcSMatt Macy 
109be181ee2SMartin Matuska extern uint_t spa_asize_inflation;
110eda14cbcSMatt Macy 
111eda14cbcSMatt Macy static zil_header_t zero_zil;
112eda14cbcSMatt Macy 
113eda14cbcSMatt Macy /*
114eda14cbcSMatt Macy  * Figure out how much of this delta should be propagated to the dsl_dir
115eda14cbcSMatt Macy  * layer.  If there's a refreservation, that space has already been
116eda14cbcSMatt Macy  * partially accounted for in our ancestors.
117eda14cbcSMatt Macy  */
118eda14cbcSMatt Macy static int64_t
119eda14cbcSMatt Macy parent_delta(dsl_dataset_t *ds, int64_t delta)
120eda14cbcSMatt Macy {
121eda14cbcSMatt Macy 	dsl_dataset_phys_t *ds_phys;
122eda14cbcSMatt Macy 	uint64_t old_bytes, new_bytes;
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy 	if (ds->ds_reserved == 0)
125eda14cbcSMatt Macy 		return (delta);
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy 	ds_phys = dsl_dataset_phys(ds);
128eda14cbcSMatt Macy 	old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
129eda14cbcSMatt Macy 	new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
130eda14cbcSMatt Macy 
131eda14cbcSMatt Macy 	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
132eda14cbcSMatt Macy 	return (new_bytes - old_bytes);
133eda14cbcSMatt Macy }
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy void
136eda14cbcSMatt Macy dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
137eda14cbcSMatt Macy {
138eda14cbcSMatt Macy 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
139eda14cbcSMatt Macy 	int used = bp_get_dsize_sync(spa, bp);
140eda14cbcSMatt Macy 	int compressed = BP_GET_PSIZE(bp);
141eda14cbcSMatt Macy 	int uncompressed = BP_GET_UCSIZE(bp);
142eda14cbcSMatt Macy 	int64_t delta;
143c7046f76SMartin Matuska 	spa_feature_t f;
144eda14cbcSMatt Macy 
145eda14cbcSMatt Macy 	dprintf_bp(bp, "ds=%p", ds);
146eda14cbcSMatt Macy 
147eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
148eda14cbcSMatt Macy 	/* It could have been compressed away to nothing */
149eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
150eda14cbcSMatt Macy 		return;
151eda14cbcSMatt Macy 	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
152eda14cbcSMatt Macy 	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
153eda14cbcSMatt Macy 	if (ds == NULL) {
154eda14cbcSMatt Macy 		dsl_pool_mos_diduse_space(tx->tx_pool,
155eda14cbcSMatt Macy 		    used, compressed, uncompressed);
156eda14cbcSMatt Macy 		return;
157eda14cbcSMatt Macy 	}
158eda14cbcSMatt Macy 
159783d3ff6SMartin Matuska 	ASSERT3U(BP_GET_LOGICAL_BIRTH(bp), >,
160783d3ff6SMartin Matuska 	    dsl_dataset_phys(ds)->ds_prev_snap_txg);
161eda14cbcSMatt Macy 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
162eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
163eda14cbcSMatt Macy 	delta = parent_delta(ds, used);
164eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_referenced_bytes += used;
165eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_compressed_bytes += compressed;
166eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed;
167eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_unique_bytes += used;
168eda14cbcSMatt Macy 
169c7046f76SMartin Matuska 	if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) {
170c7046f76SMartin Matuska 		ds->ds_feature_activation[SPA_FEATURE_LARGE_BLOCKS] =
171c7046f76SMartin Matuska 		    (void *)B_TRUE;
172c7046f76SMartin Matuska 	}
173c7046f76SMartin Matuska 
174c7046f76SMartin Matuska 
175c7046f76SMartin Matuska 	f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp));
176c7046f76SMartin Matuska 	if (f != SPA_FEATURE_NONE) {
177c7046f76SMartin Matuska 		ASSERT3S(spa_feature_table[f].fi_type, ==,
178c7046f76SMartin Matuska 		    ZFEATURE_TYPE_BOOLEAN);
179c7046f76SMartin Matuska 		ds->ds_feature_activation[f] = (void *)B_TRUE;
180c7046f76SMartin Matuska 	}
181c7046f76SMartin Matuska 
182c7046f76SMartin Matuska 	f = zio_compress_to_feature(BP_GET_COMPRESS(bp));
183c7046f76SMartin Matuska 	if (f != SPA_FEATURE_NONE) {
184c7046f76SMartin Matuska 		ASSERT3S(spa_feature_table[f].fi_type, ==,
185c7046f76SMartin Matuska 		    ZFEATURE_TYPE_BOOLEAN);
186c7046f76SMartin Matuska 		ds->ds_feature_activation[f] = (void *)B_TRUE;
187c7046f76SMartin Matuska 	}
188eda14cbcSMatt Macy 
189eda14cbcSMatt Macy 	/*
190eda14cbcSMatt Macy 	 * Track block for livelist, but ignore embedded blocks because
191eda14cbcSMatt Macy 	 * they do not need to be freed.
192eda14cbcSMatt Macy 	 */
193eda14cbcSMatt Macy 	if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
194783d3ff6SMartin Matuska 	    BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg &&
195eda14cbcSMatt Macy 	    !(BP_IS_EMBEDDED(bp))) {
196eda14cbcSMatt Macy 		ASSERT(dsl_dir_is_clone(ds->ds_dir));
197eda14cbcSMatt Macy 		ASSERT(spa_feature_is_enabled(spa,
198eda14cbcSMatt Macy 		    SPA_FEATURE_LIVELIST));
199eda14cbcSMatt Macy 		bplist_append(&ds->ds_dir->dd_pending_allocs, bp);
200eda14cbcSMatt Macy 	}
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
2033f9d360cSMartin Matuska 	dsl_dir_diduse_transfer_space(ds->ds_dir, delta,
2043f9d360cSMartin Matuska 	    compressed, uncompressed, used,
205eda14cbcSMatt Macy 	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
206eda14cbcSMatt Macy }
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy /*
209eda14cbcSMatt Macy  * Called when the specified segment has been remapped, and is thus no
210eda14cbcSMatt Macy  * longer referenced in the head dataset.  The vdev must be indirect.
211eda14cbcSMatt Macy  *
212eda14cbcSMatt Macy  * If the segment is referenced by a snapshot, put it on the remap deadlist.
213eda14cbcSMatt Macy  * Otherwise, add this segment to the obsolete spacemap.
214eda14cbcSMatt Macy  */
215eda14cbcSMatt Macy void
216eda14cbcSMatt Macy dsl_dataset_block_remapped(dsl_dataset_t *ds, uint64_t vdev, uint64_t offset,
217eda14cbcSMatt Macy     uint64_t size, uint64_t birth, dmu_tx_t *tx)
218eda14cbcSMatt Macy {
219eda14cbcSMatt Macy 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
222eda14cbcSMatt Macy 	ASSERT(birth <= tx->tx_txg);
223eda14cbcSMatt Macy 	ASSERT(!ds->ds_is_snapshot);
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 	if (birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
226eda14cbcSMatt Macy 		spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
227eda14cbcSMatt Macy 	} else {
228eda14cbcSMatt Macy 		blkptr_t fakebp;
229eda14cbcSMatt Macy 		dva_t *dva = &fakebp.blk_dva[0];
230eda14cbcSMatt Macy 
231eda14cbcSMatt Macy 		ASSERT(ds != NULL);
232eda14cbcSMatt Macy 
233eda14cbcSMatt Macy 		mutex_enter(&ds->ds_remap_deadlist_lock);
234eda14cbcSMatt Macy 		if (!dsl_dataset_remap_deadlist_exists(ds)) {
235eda14cbcSMatt Macy 			dsl_dataset_create_remap_deadlist(ds, tx);
236eda14cbcSMatt Macy 		}
237eda14cbcSMatt Macy 		mutex_exit(&ds->ds_remap_deadlist_lock);
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy 		BP_ZERO(&fakebp);
240783d3ff6SMartin Matuska 		BP_SET_LOGICAL_BIRTH(&fakebp, birth);
241eda14cbcSMatt Macy 		DVA_SET_VDEV(dva, vdev);
242eda14cbcSMatt Macy 		DVA_SET_OFFSET(dva, offset);
243eda14cbcSMatt Macy 		DVA_SET_ASIZE(dva, size);
244eda14cbcSMatt Macy 		dsl_deadlist_insert(&ds->ds_remap_deadlist, &fakebp, B_FALSE,
245eda14cbcSMatt Macy 		    tx);
246eda14cbcSMatt Macy 	}
247eda14cbcSMatt Macy }
248eda14cbcSMatt Macy 
249eda14cbcSMatt Macy int
250eda14cbcSMatt Macy dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
251eda14cbcSMatt Macy     boolean_t async)
252eda14cbcSMatt Macy {
253eda14cbcSMatt Macy 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
254eda14cbcSMatt Macy 
255eda14cbcSMatt Macy 	int used = bp_get_dsize_sync(spa, bp);
256eda14cbcSMatt Macy 	int compressed = BP_GET_PSIZE(bp);
257eda14cbcSMatt Macy 	int uncompressed = BP_GET_UCSIZE(bp);
258eda14cbcSMatt Macy 
259eda14cbcSMatt Macy 	if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp))
260eda14cbcSMatt Macy 		return (0);
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
263783d3ff6SMartin Matuska 	ASSERT(BP_GET_LOGICAL_BIRTH(bp) <= tx->tx_txg);
264eda14cbcSMatt Macy 
265eda14cbcSMatt Macy 	if (ds == NULL) {
266eda14cbcSMatt Macy 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
267eda14cbcSMatt Macy 		dsl_pool_mos_diduse_space(tx->tx_pool,
268eda14cbcSMatt Macy 		    -used, -compressed, -uncompressed);
269eda14cbcSMatt Macy 		return (used);
270eda14cbcSMatt Macy 	}
271eda14cbcSMatt Macy 	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy 	ASSERT(!ds->ds_is_snapshot);
274eda14cbcSMatt Macy 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy 	/*
277eda14cbcSMatt Macy 	 * Track block for livelist, but ignore embedded blocks because
278eda14cbcSMatt Macy 	 * they do not need to be freed.
279eda14cbcSMatt Macy 	 */
280eda14cbcSMatt Macy 	if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
281783d3ff6SMartin Matuska 	    BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg &&
282eda14cbcSMatt Macy 	    !(BP_IS_EMBEDDED(bp))) {
283eda14cbcSMatt Macy 		ASSERT(dsl_dir_is_clone(ds->ds_dir));
284eda14cbcSMatt Macy 		ASSERT(spa_feature_is_enabled(spa,
285eda14cbcSMatt Macy 		    SPA_FEATURE_LIVELIST));
286eda14cbcSMatt Macy 		bplist_append(&ds->ds_dir->dd_pending_frees, bp);
287eda14cbcSMatt Macy 	}
288eda14cbcSMatt Macy 
289783d3ff6SMartin Matuska 	if (BP_GET_LOGICAL_BIRTH(bp) > dsl_dataset_phys(ds)->ds_prev_snap_txg) {
290eda14cbcSMatt Macy 		int64_t delta;
291eda14cbcSMatt Macy 
29233b8c039SMartin Matuska 		dprintf_bp(bp, "freeing ds=%llu", (u_longlong_t)ds->ds_object);
293eda14cbcSMatt Macy 		dsl_free(tx->tx_pool, tx->tx_txg, bp);
294eda14cbcSMatt Macy 
295eda14cbcSMatt Macy 		mutex_enter(&ds->ds_lock);
296eda14cbcSMatt Macy 		ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used ||
297eda14cbcSMatt Macy 		    !DS_UNIQUE_IS_ACCURATE(ds));
298eda14cbcSMatt Macy 		delta = parent_delta(ds, -used);
299eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_unique_bytes -= used;
300eda14cbcSMatt Macy 		mutex_exit(&ds->ds_lock);
3013f9d360cSMartin Matuska 		dsl_dir_diduse_transfer_space(ds->ds_dir,
3023f9d360cSMartin Matuska 		    delta, -compressed, -uncompressed, -used,
303eda14cbcSMatt Macy 		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
304eda14cbcSMatt Macy 	} else {
305eda14cbcSMatt Macy 		dprintf_bp(bp, "putting on dead list: %s", "");
306eda14cbcSMatt Macy 		if (async) {
307eda14cbcSMatt Macy 			/*
308eda14cbcSMatt Macy 			 * We are here as part of zio's write done callback,
309eda14cbcSMatt Macy 			 * which means we're a zio interrupt thread.  We can't
310eda14cbcSMatt Macy 			 * call dsl_deadlist_insert() now because it may block
311eda14cbcSMatt Macy 			 * waiting for I/O.  Instead, put bp on the deferred
312eda14cbcSMatt Macy 			 * queue and let dsl_pool_sync() finish the job.
313eda14cbcSMatt Macy 			 */
314eda14cbcSMatt Macy 			bplist_append(&ds->ds_pending_deadlist, bp);
315eda14cbcSMatt Macy 		} else {
316eda14cbcSMatt Macy 			dsl_deadlist_insert(&ds->ds_deadlist, bp, B_FALSE, tx);
317eda14cbcSMatt Macy 		}
318eda14cbcSMatt Macy 		ASSERT3U(ds->ds_prev->ds_object, ==,
319eda14cbcSMatt Macy 		    dsl_dataset_phys(ds)->ds_prev_snap_obj);
320eda14cbcSMatt Macy 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0);
321783d3ff6SMartin Matuska 		/* if (logical birth > prev prev snap txg) prev unique += bs */
322eda14cbcSMatt Macy 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
323783d3ff6SMartin Matuska 		    ds->ds_object && BP_GET_LOGICAL_BIRTH(bp) >
324eda14cbcSMatt Macy 		    dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) {
325eda14cbcSMatt Macy 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
326eda14cbcSMatt Macy 			mutex_enter(&ds->ds_prev->ds_lock);
327eda14cbcSMatt Macy 			dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used;
328eda14cbcSMatt Macy 			mutex_exit(&ds->ds_prev->ds_lock);
329eda14cbcSMatt Macy 		}
330783d3ff6SMartin Matuska 		if (BP_GET_LOGICAL_BIRTH(bp) > ds->ds_dir->dd_origin_txg) {
331eda14cbcSMatt Macy 			dsl_dir_transfer_space(ds->ds_dir, used,
332eda14cbcSMatt Macy 			    DD_USED_HEAD, DD_USED_SNAP, tx);
333eda14cbcSMatt Macy 		}
334eda14cbcSMatt Macy 	}
335eda14cbcSMatt Macy 
336eda14cbcSMatt Macy 	dsl_bookmark_block_killed(ds, bp, tx);
337eda14cbcSMatt Macy 
338eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
339eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used);
340eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_referenced_bytes -= used;
341eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed);
342eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed;
343eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed);
344eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed;
345eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	return (used);
348eda14cbcSMatt Macy }
349eda14cbcSMatt Macy 
350eda14cbcSMatt Macy struct feature_type_uint64_array_arg {
351eda14cbcSMatt Macy 	uint64_t length;
352eda14cbcSMatt Macy 	uint64_t *array;
353eda14cbcSMatt Macy };
354eda14cbcSMatt Macy 
355eda14cbcSMatt Macy static void
356eda14cbcSMatt Macy unload_zfeature(dsl_dataset_t *ds, spa_feature_t f)
357eda14cbcSMatt Macy {
358eda14cbcSMatt Macy 	switch (spa_feature_table[f].fi_type) {
359eda14cbcSMatt Macy 	case ZFEATURE_TYPE_BOOLEAN:
360eda14cbcSMatt Macy 		break;
361eda14cbcSMatt Macy 	case ZFEATURE_TYPE_UINT64_ARRAY:
362eda14cbcSMatt Macy 	{
363eda14cbcSMatt Macy 		struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f];
364eda14cbcSMatt Macy 		kmem_free(ftuaa->array, ftuaa->length * sizeof (uint64_t));
365eda14cbcSMatt Macy 		kmem_free(ftuaa, sizeof (*ftuaa));
366eda14cbcSMatt Macy 		break;
367eda14cbcSMatt Macy 	}
368eda14cbcSMatt Macy 	default:
369eda14cbcSMatt Macy 		panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
370eda14cbcSMatt Macy 	}
371eda14cbcSMatt Macy }
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy static int
374eda14cbcSMatt Macy load_zfeature(objset_t *mos, dsl_dataset_t *ds, spa_feature_t f)
375eda14cbcSMatt Macy {
376eda14cbcSMatt Macy 	int err = 0;
377eda14cbcSMatt Macy 	switch (spa_feature_table[f].fi_type) {
378eda14cbcSMatt Macy 	case ZFEATURE_TYPE_BOOLEAN:
379eda14cbcSMatt Macy 		err = zap_contains(mos, ds->ds_object,
380eda14cbcSMatt Macy 		    spa_feature_table[f].fi_guid);
381eda14cbcSMatt Macy 		if (err == 0) {
382eda14cbcSMatt Macy 			ds->ds_feature[f] = (void *)B_TRUE;
383eda14cbcSMatt Macy 		} else {
384eda14cbcSMatt Macy 			ASSERT3U(err, ==, ENOENT);
385eda14cbcSMatt Macy 			err = 0;
386eda14cbcSMatt Macy 		}
387eda14cbcSMatt Macy 		break;
388eda14cbcSMatt Macy 	case ZFEATURE_TYPE_UINT64_ARRAY:
389eda14cbcSMatt Macy 	{
390eda14cbcSMatt Macy 		uint64_t int_size, num_int;
391eda14cbcSMatt Macy 		uint64_t *data;
392eda14cbcSMatt Macy 		err = zap_length(mos, ds->ds_object,
393eda14cbcSMatt Macy 		    spa_feature_table[f].fi_guid, &int_size, &num_int);
394eda14cbcSMatt Macy 		if (err != 0) {
395eda14cbcSMatt Macy 			ASSERT3U(err, ==, ENOENT);
396eda14cbcSMatt Macy 			err = 0;
397eda14cbcSMatt Macy 			break;
398eda14cbcSMatt Macy 		}
399eda14cbcSMatt Macy 		ASSERT3U(int_size, ==, sizeof (uint64_t));
400eda14cbcSMatt Macy 		data = kmem_alloc(int_size * num_int, KM_SLEEP);
401eda14cbcSMatt Macy 		VERIFY0(zap_lookup(mos, ds->ds_object,
402eda14cbcSMatt Macy 		    spa_feature_table[f].fi_guid, int_size, num_int, data));
403eda14cbcSMatt Macy 		struct feature_type_uint64_array_arg *ftuaa =
404eda14cbcSMatt Macy 		    kmem_alloc(sizeof (*ftuaa), KM_SLEEP);
405eda14cbcSMatt Macy 		ftuaa->length = num_int;
406eda14cbcSMatt Macy 		ftuaa->array = data;
407eda14cbcSMatt Macy 		ds->ds_feature[f] = ftuaa;
408eda14cbcSMatt Macy 		break;
409eda14cbcSMatt Macy 	}
410eda14cbcSMatt Macy 	default:
411eda14cbcSMatt Macy 		panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
412eda14cbcSMatt Macy 	}
413eda14cbcSMatt Macy 	return (err);
414eda14cbcSMatt Macy }
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy /*
417eda14cbcSMatt Macy  * We have to release the fsid synchronously or we risk that a subsequent
418eda14cbcSMatt Macy  * mount of the same dataset will fail to unique_insert the fsid.  This
419eda14cbcSMatt Macy  * failure would manifest itself as the fsid of this dataset changing
420eda14cbcSMatt Macy  * between mounts which makes NFS clients quite unhappy.
421eda14cbcSMatt Macy  */
422eda14cbcSMatt Macy static void
423eda14cbcSMatt Macy dsl_dataset_evict_sync(void *dbu)
424eda14cbcSMatt Macy {
425eda14cbcSMatt Macy 	dsl_dataset_t *ds = dbu;
426eda14cbcSMatt Macy 
427eda14cbcSMatt Macy 	ASSERT(ds->ds_owner == NULL);
428eda14cbcSMatt Macy 
429eda14cbcSMatt Macy 	unique_remove(ds->ds_fsid_guid);
430eda14cbcSMatt Macy }
431eda14cbcSMatt Macy 
432eda14cbcSMatt Macy static void
433eda14cbcSMatt Macy dsl_dataset_evict_async(void *dbu)
434eda14cbcSMatt Macy {
435eda14cbcSMatt Macy 	dsl_dataset_t *ds = dbu;
436eda14cbcSMatt Macy 
437eda14cbcSMatt Macy 	ASSERT(ds->ds_owner == NULL);
438eda14cbcSMatt Macy 
439eda14cbcSMatt Macy 	ds->ds_dbuf = NULL;
440eda14cbcSMatt Macy 
441eda14cbcSMatt Macy 	if (ds->ds_objset != NULL)
442eda14cbcSMatt Macy 		dmu_objset_evict(ds->ds_objset);
443eda14cbcSMatt Macy 
444eda14cbcSMatt Macy 	if (ds->ds_prev) {
445eda14cbcSMatt Macy 		dsl_dataset_rele(ds->ds_prev, ds);
446eda14cbcSMatt Macy 		ds->ds_prev = NULL;
447eda14cbcSMatt Macy 	}
448eda14cbcSMatt Macy 
449eda14cbcSMatt Macy 	dsl_bookmark_fini_ds(ds);
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	bplist_destroy(&ds->ds_pending_deadlist);
452eda14cbcSMatt Macy 	if (dsl_deadlist_is_open(&ds->ds_deadlist))
453eda14cbcSMatt Macy 		dsl_deadlist_close(&ds->ds_deadlist);
454eda14cbcSMatt Macy 	if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
455eda14cbcSMatt Macy 		dsl_deadlist_close(&ds->ds_remap_deadlist);
456eda14cbcSMatt Macy 	if (ds->ds_dir)
457eda14cbcSMatt Macy 		dsl_dir_async_rele(ds->ds_dir, ds);
458eda14cbcSMatt Macy 
459eda14cbcSMatt Macy 	ASSERT(!list_link_active(&ds->ds_synced_link));
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
462eda14cbcSMatt Macy 		if (dsl_dataset_feature_is_active(ds, f))
463eda14cbcSMatt Macy 			unload_zfeature(ds, f);
464eda14cbcSMatt Macy 	}
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy 	list_destroy(&ds->ds_prop_cbs);
467eda14cbcSMatt Macy 	mutex_destroy(&ds->ds_lock);
468eda14cbcSMatt Macy 	mutex_destroy(&ds->ds_opening_lock);
469eda14cbcSMatt Macy 	mutex_destroy(&ds->ds_sendstream_lock);
470eda14cbcSMatt Macy 	mutex_destroy(&ds->ds_remap_deadlist_lock);
471eda14cbcSMatt Macy 	zfs_refcount_destroy(&ds->ds_longholds);
472eda14cbcSMatt Macy 	rrw_destroy(&ds->ds_bp_rwlock);
473eda14cbcSMatt Macy 
474eda14cbcSMatt Macy 	kmem_free(ds, sizeof (dsl_dataset_t));
475eda14cbcSMatt Macy }
476eda14cbcSMatt Macy 
477eda14cbcSMatt Macy int
478eda14cbcSMatt Macy dsl_dataset_get_snapname(dsl_dataset_t *ds)
479eda14cbcSMatt Macy {
480eda14cbcSMatt Macy 	dsl_dataset_phys_t *headphys;
481eda14cbcSMatt Macy 	int err;
482eda14cbcSMatt Macy 	dmu_buf_t *headdbuf;
483eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
484eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy 	if (ds->ds_snapname[0])
487eda14cbcSMatt Macy 		return (0);
488eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0)
489eda14cbcSMatt Macy 		return (0);
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 	err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj,
492eda14cbcSMatt Macy 	    FTAG, &headdbuf);
493eda14cbcSMatt Macy 	if (err != 0)
494eda14cbcSMatt Macy 		return (err);
495eda14cbcSMatt Macy 	headphys = headdbuf->db_data;
496eda14cbcSMatt Macy 	err = zap_value_search(dp->dp_meta_objset,
497eda14cbcSMatt Macy 	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
498eda14cbcSMatt Macy 	if (err != 0 && zfs_recover == B_TRUE) {
499eda14cbcSMatt Macy 		err = 0;
500eda14cbcSMatt Macy 		(void) snprintf(ds->ds_snapname, sizeof (ds->ds_snapname),
501eda14cbcSMatt Macy 		    "SNAPOBJ=%llu-ERR=%d",
502eda14cbcSMatt Macy 		    (unsigned long long)ds->ds_object, err);
503eda14cbcSMatt Macy 	}
504eda14cbcSMatt Macy 	dmu_buf_rele(headdbuf, FTAG);
505eda14cbcSMatt Macy 	return (err);
506eda14cbcSMatt Macy }
507eda14cbcSMatt Macy 
508eda14cbcSMatt Macy int
509eda14cbcSMatt Macy dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
510eda14cbcSMatt Macy {
511eda14cbcSMatt Macy 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
512eda14cbcSMatt Macy 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
513eda14cbcSMatt Macy 	matchtype_t mt = 0;
514eda14cbcSMatt Macy 	int err;
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
517eda14cbcSMatt Macy 		mt = MT_NORMALIZE;
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy 	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
520eda14cbcSMatt Macy 	    value, mt, NULL, 0, NULL);
521eda14cbcSMatt Macy 	if (err == ENOTSUP && (mt & MT_NORMALIZE))
522eda14cbcSMatt Macy 		err = zap_lookup(mos, snapobj, name, 8, 1, value);
523eda14cbcSMatt Macy 	return (err);
524eda14cbcSMatt Macy }
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy int
527eda14cbcSMatt Macy dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
528eda14cbcSMatt Macy     boolean_t adj_cnt)
529eda14cbcSMatt Macy {
530eda14cbcSMatt Macy 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
531eda14cbcSMatt Macy 	uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
532eda14cbcSMatt Macy 	matchtype_t mt = 0;
533eda14cbcSMatt Macy 	int err;
534eda14cbcSMatt Macy 
535271171e0SMartin Matuska 	dsl_dir_snap_cmtime_update(ds->ds_dir, tx);
536eda14cbcSMatt Macy 
537eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
538eda14cbcSMatt Macy 		mt = MT_NORMALIZE;
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy 	err = zap_remove_norm(mos, snapobj, name, mt, tx);
541eda14cbcSMatt Macy 	if (err == ENOTSUP && (mt & MT_NORMALIZE))
542eda14cbcSMatt Macy 		err = zap_remove(mos, snapobj, name, tx);
543eda14cbcSMatt Macy 
544eda14cbcSMatt Macy 	if (err == 0 && adj_cnt)
545eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(ds->ds_dir, -1,
546eda14cbcSMatt Macy 		    DD_FIELD_SNAPSHOT_COUNT, tx);
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy 	return (err);
549eda14cbcSMatt Macy }
550eda14cbcSMatt Macy 
551eda14cbcSMatt Macy boolean_t
552a0b956f5SMartin Matuska dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, const void *tag)
553eda14cbcSMatt Macy {
554eda14cbcSMatt Macy 	dmu_buf_t *dbuf = ds->ds_dbuf;
555eda14cbcSMatt Macy 	boolean_t result = B_FALSE;
556eda14cbcSMatt Macy 
557eda14cbcSMatt Macy 	if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset,
558eda14cbcSMatt Macy 	    ds->ds_object, DMU_BONUS_BLKID, tag)) {
559eda14cbcSMatt Macy 
560eda14cbcSMatt Macy 		if (ds == dmu_buf_get_user(dbuf))
561eda14cbcSMatt Macy 			result = B_TRUE;
562eda14cbcSMatt Macy 		else
563eda14cbcSMatt Macy 			dmu_buf_rele(dbuf, tag);
564eda14cbcSMatt Macy 	}
565eda14cbcSMatt Macy 
566eda14cbcSMatt Macy 	return (result);
567eda14cbcSMatt Macy }
568eda14cbcSMatt Macy 
569eda14cbcSMatt Macy int
570a0b956f5SMartin Matuska dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, const void *tag,
571eda14cbcSMatt Macy     dsl_dataset_t **dsp)
572eda14cbcSMatt Macy {
573eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
574eda14cbcSMatt Macy 	dmu_buf_t *dbuf;
575eda14cbcSMatt Macy 	dsl_dataset_t *ds;
576eda14cbcSMatt Macy 	int err;
577eda14cbcSMatt Macy 	dmu_object_info_t doi;
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
580eda14cbcSMatt Macy 
581eda14cbcSMatt Macy 	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
582eda14cbcSMatt Macy 	if (err != 0)
583eda14cbcSMatt Macy 		return (err);
584eda14cbcSMatt Macy 
585eda14cbcSMatt Macy 	/* Make sure dsobj has the correct object type. */
586eda14cbcSMatt Macy 	dmu_object_info_from_db(dbuf, &doi);
587eda14cbcSMatt Macy 	if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) {
588eda14cbcSMatt Macy 		dmu_buf_rele(dbuf, tag);
589eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
590eda14cbcSMatt Macy 	}
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 	ds = dmu_buf_get_user(dbuf);
593eda14cbcSMatt Macy 	if (ds == NULL) {
594eda14cbcSMatt Macy 		dsl_dataset_t *winner = NULL;
595eda14cbcSMatt Macy 
596eda14cbcSMatt Macy 		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
597eda14cbcSMatt Macy 		ds->ds_dbuf = dbuf;
598eda14cbcSMatt Macy 		ds->ds_object = dsobj;
599eda14cbcSMatt Macy 		ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0;
600eda14cbcSMatt Macy 		list_link_init(&ds->ds_synced_link);
601eda14cbcSMatt Macy 
602eda14cbcSMatt Macy 		err = dsl_dir_hold_obj(dp, dsl_dataset_phys(ds)->ds_dir_obj,
603eda14cbcSMatt Macy 		    NULL, ds, &ds->ds_dir);
604eda14cbcSMatt Macy 		if (err != 0) {
605eda14cbcSMatt Macy 			kmem_free(ds, sizeof (dsl_dataset_t));
606eda14cbcSMatt Macy 			dmu_buf_rele(dbuf, tag);
607eda14cbcSMatt Macy 			return (err);
608eda14cbcSMatt Macy 		}
609eda14cbcSMatt Macy 
610eda14cbcSMatt Macy 		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
611eda14cbcSMatt Macy 		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
612eda14cbcSMatt Macy 		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
613eda14cbcSMatt Macy 		mutex_init(&ds->ds_remap_deadlist_lock,
614eda14cbcSMatt Macy 		    NULL, MUTEX_DEFAULT, NULL);
615eda14cbcSMatt Macy 		rrw_init(&ds->ds_bp_rwlock, B_FALSE);
616eda14cbcSMatt Macy 		zfs_refcount_create(&ds->ds_longholds);
617eda14cbcSMatt Macy 
618eda14cbcSMatt Macy 		bplist_create(&ds->ds_pending_deadlist);
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy 		list_create(&ds->ds_sendstreams, sizeof (dmu_sendstatus_t),
621eda14cbcSMatt Macy 		    offsetof(dmu_sendstatus_t, dss_link));
622eda14cbcSMatt Macy 
623eda14cbcSMatt Macy 		list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t),
624eda14cbcSMatt Macy 		    offsetof(dsl_prop_cb_record_t, cbr_ds_node));
625eda14cbcSMatt Macy 
626eda14cbcSMatt Macy 		if (doi.doi_type == DMU_OTN_ZAP_METADATA) {
627eda14cbcSMatt Macy 			spa_feature_t f;
628eda14cbcSMatt Macy 
629eda14cbcSMatt Macy 			for (f = 0; f < SPA_FEATURES; f++) {
630eda14cbcSMatt Macy 				if (!(spa_feature_table[f].fi_flags &
631eda14cbcSMatt Macy 				    ZFEATURE_FLAG_PER_DATASET))
632eda14cbcSMatt Macy 					continue;
633eda14cbcSMatt Macy 				err = load_zfeature(mos, ds, f);
634eda14cbcSMatt Macy 			}
635eda14cbcSMatt Macy 		}
636eda14cbcSMatt Macy 
637eda14cbcSMatt Macy 		if (!ds->ds_is_snapshot) {
638eda14cbcSMatt Macy 			ds->ds_snapname[0] = '\0';
639eda14cbcSMatt Macy 			if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
640eda14cbcSMatt Macy 				err = dsl_dataset_hold_obj(dp,
641eda14cbcSMatt Macy 				    dsl_dataset_phys(ds)->ds_prev_snap_obj,
642eda14cbcSMatt Macy 				    ds, &ds->ds_prev);
643eda14cbcSMatt Macy 			}
644be181ee2SMartin Matuska 			if (err != 0)
645be181ee2SMartin Matuska 				goto after_dsl_bookmark_fini;
646eda14cbcSMatt Macy 			err = dsl_bookmark_init_ds(ds);
647eda14cbcSMatt Macy 		} else {
648eda14cbcSMatt Macy 			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
649eda14cbcSMatt Macy 				err = dsl_dataset_get_snapname(ds);
650eda14cbcSMatt Macy 			if (err == 0 &&
651eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_userrefs_obj != 0) {
652eda14cbcSMatt Macy 				err = zap_count(
653eda14cbcSMatt Macy 				    ds->ds_dir->dd_pool->dp_meta_objset,
654eda14cbcSMatt Macy 				    dsl_dataset_phys(ds)->ds_userrefs_obj,
655eda14cbcSMatt Macy 				    &ds->ds_userrefs);
656eda14cbcSMatt Macy 			}
657eda14cbcSMatt Macy 		}
658eda14cbcSMatt Macy 
659eda14cbcSMatt Macy 		if (err == 0 && !ds->ds_is_snapshot) {
660eda14cbcSMatt Macy 			err = dsl_prop_get_int_ds(ds,
661eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
662eda14cbcSMatt Macy 			    &ds->ds_reserved);
663eda14cbcSMatt Macy 			if (err == 0) {
664eda14cbcSMatt Macy 				err = dsl_prop_get_int_ds(ds,
665eda14cbcSMatt Macy 				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
666eda14cbcSMatt Macy 				    &ds->ds_quota);
667eda14cbcSMatt Macy 			}
668eda14cbcSMatt Macy 		} else {
669eda14cbcSMatt Macy 			ds->ds_reserved = ds->ds_quota = 0;
670eda14cbcSMatt Macy 		}
671eda14cbcSMatt Macy 
672eda14cbcSMatt Macy 		if (err == 0 && ds->ds_dir->dd_crypto_obj != 0 &&
673eda14cbcSMatt Macy 		    ds->ds_is_snapshot &&
674eda14cbcSMatt Macy 		    zap_contains(mos, dsobj, DS_FIELD_IVSET_GUID) != 0) {
675eda14cbcSMatt Macy 			dp->dp_spa->spa_errata =
676eda14cbcSMatt Macy 			    ZPOOL_ERRATA_ZOL_8308_ENCRYPTION;
677eda14cbcSMatt Macy 		}
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy 		dsl_deadlist_open(&ds->ds_deadlist,
680eda14cbcSMatt Macy 		    mos, dsl_dataset_phys(ds)->ds_deadlist_obj);
681eda14cbcSMatt Macy 		uint64_t remap_deadlist_obj =
682eda14cbcSMatt Macy 		    dsl_dataset_get_remap_deadlist_object(ds);
683eda14cbcSMatt Macy 		if (remap_deadlist_obj != 0) {
684eda14cbcSMatt Macy 			dsl_deadlist_open(&ds->ds_remap_deadlist, mos,
685eda14cbcSMatt Macy 			    remap_deadlist_obj);
686eda14cbcSMatt Macy 		}
687eda14cbcSMatt Macy 
688eda14cbcSMatt Macy 		dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict_sync,
689eda14cbcSMatt Macy 		    dsl_dataset_evict_async, &ds->ds_dbuf);
690eda14cbcSMatt Macy 		if (err == 0)
691eda14cbcSMatt Macy 			winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu);
692eda14cbcSMatt Macy 
693eda14cbcSMatt Macy 		if (err != 0 || winner != NULL) {
694eda14cbcSMatt Macy 			dsl_deadlist_close(&ds->ds_deadlist);
695eda14cbcSMatt Macy 			if (dsl_deadlist_is_open(&ds->ds_remap_deadlist))
696eda14cbcSMatt Macy 				dsl_deadlist_close(&ds->ds_remap_deadlist);
697eda14cbcSMatt Macy 			dsl_bookmark_fini_ds(ds);
698be181ee2SMartin Matuska after_dsl_bookmark_fini:
699eda14cbcSMatt Macy 			if (ds->ds_prev)
700eda14cbcSMatt Macy 				dsl_dataset_rele(ds->ds_prev, ds);
701eda14cbcSMatt Macy 			dsl_dir_rele(ds->ds_dir, ds);
702eda14cbcSMatt Macy 			for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
703eda14cbcSMatt Macy 				if (dsl_dataset_feature_is_active(ds, f))
704eda14cbcSMatt Macy 					unload_zfeature(ds, f);
705eda14cbcSMatt Macy 			}
706eda14cbcSMatt Macy 
707eda14cbcSMatt Macy 			list_destroy(&ds->ds_prop_cbs);
708eda14cbcSMatt Macy 			list_destroy(&ds->ds_sendstreams);
709be181ee2SMartin Matuska 			bplist_destroy(&ds->ds_pending_deadlist);
710eda14cbcSMatt Macy 			mutex_destroy(&ds->ds_lock);
711eda14cbcSMatt Macy 			mutex_destroy(&ds->ds_opening_lock);
712eda14cbcSMatt Macy 			mutex_destroy(&ds->ds_sendstream_lock);
713eda14cbcSMatt Macy 			mutex_destroy(&ds->ds_remap_deadlist_lock);
714eda14cbcSMatt Macy 			zfs_refcount_destroy(&ds->ds_longholds);
715eda14cbcSMatt Macy 			rrw_destroy(&ds->ds_bp_rwlock);
716eda14cbcSMatt Macy 			kmem_free(ds, sizeof (dsl_dataset_t));
717eda14cbcSMatt Macy 			if (err != 0) {
718eda14cbcSMatt Macy 				dmu_buf_rele(dbuf, tag);
719eda14cbcSMatt Macy 				return (err);
720eda14cbcSMatt Macy 			}
721eda14cbcSMatt Macy 			ds = winner;
722eda14cbcSMatt Macy 		} else {
723eda14cbcSMatt Macy 			ds->ds_fsid_guid =
724eda14cbcSMatt Macy 			    unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid);
725eda14cbcSMatt Macy 			if (ds->ds_fsid_guid !=
726eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_fsid_guid) {
727eda14cbcSMatt Macy 				zfs_dbgmsg("ds_fsid_guid changed from "
728eda14cbcSMatt Macy 				    "%llx to %llx for pool %s dataset id %llu",
729eda14cbcSMatt Macy 				    (long long)
730eda14cbcSMatt Macy 				    dsl_dataset_phys(ds)->ds_fsid_guid,
731eda14cbcSMatt Macy 				    (long long)ds->ds_fsid_guid,
732eda14cbcSMatt Macy 				    spa_name(dp->dp_spa),
73333b8c039SMartin Matuska 				    (u_longlong_t)dsobj);
734eda14cbcSMatt Macy 			}
735eda14cbcSMatt Macy 		}
736eda14cbcSMatt Macy 	}
737eda14cbcSMatt Macy 
738eda14cbcSMatt Macy 	ASSERT3P(ds->ds_dbuf, ==, dbuf);
739eda14cbcSMatt Macy 	ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data);
740eda14cbcSMatt Macy 	ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 ||
741eda14cbcSMatt Macy 	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
742eda14cbcSMatt Macy 	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
743eda14cbcSMatt Macy 	*dsp = ds;
744eda14cbcSMatt Macy 
745eda14cbcSMatt Macy 	return (0);
746eda14cbcSMatt Macy }
747eda14cbcSMatt Macy 
748eda14cbcSMatt Macy int
749eda14cbcSMatt Macy dsl_dataset_create_key_mapping(dsl_dataset_t *ds)
750eda14cbcSMatt Macy {
751eda14cbcSMatt Macy 	dsl_dir_t *dd = ds->ds_dir;
752eda14cbcSMatt Macy 
753eda14cbcSMatt Macy 	if (dd->dd_crypto_obj == 0)
754eda14cbcSMatt Macy 		return (0);
755eda14cbcSMatt Macy 
756eda14cbcSMatt Macy 	return (spa_keystore_create_mapping(dd->dd_pool->dp_spa,
757eda14cbcSMatt Macy 	    ds, ds, &ds->ds_key_mapping));
758eda14cbcSMatt Macy }
759eda14cbcSMatt Macy 
760eda14cbcSMatt Macy int
761eda14cbcSMatt Macy dsl_dataset_hold_obj_flags(dsl_pool_t *dp, uint64_t dsobj,
762a0b956f5SMartin Matuska     ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp)
763eda14cbcSMatt Macy {
764eda14cbcSMatt Macy 	int err;
765eda14cbcSMatt Macy 
766eda14cbcSMatt Macy 	err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
767eda14cbcSMatt Macy 	if (err != 0)
768eda14cbcSMatt Macy 		return (err);
769eda14cbcSMatt Macy 
770eda14cbcSMatt Macy 	ASSERT3P(*dsp, !=, NULL);
771eda14cbcSMatt Macy 
772eda14cbcSMatt Macy 	if (flags & DS_HOLD_FLAG_DECRYPT) {
773eda14cbcSMatt Macy 		err = dsl_dataset_create_key_mapping(*dsp);
774eda14cbcSMatt Macy 		if (err != 0)
775eda14cbcSMatt Macy 			dsl_dataset_rele(*dsp, tag);
776eda14cbcSMatt Macy 	}
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy 	return (err);
779eda14cbcSMatt Macy }
780eda14cbcSMatt Macy 
781eda14cbcSMatt Macy int
782eda14cbcSMatt Macy dsl_dataset_hold_flags(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
783a0b956f5SMartin Matuska     const void *tag, dsl_dataset_t **dsp)
784eda14cbcSMatt Macy {
785eda14cbcSMatt Macy 	dsl_dir_t *dd;
786eda14cbcSMatt Macy 	const char *snapname;
787eda14cbcSMatt Macy 	uint64_t obj;
788eda14cbcSMatt Macy 	int err = 0;
789eda14cbcSMatt Macy 	dsl_dataset_t *ds;
790eda14cbcSMatt Macy 
791eda14cbcSMatt Macy 	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
792eda14cbcSMatt Macy 	if (err != 0)
793eda14cbcSMatt Macy 		return (err);
794eda14cbcSMatt Macy 
795eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
796eda14cbcSMatt Macy 	obj = dsl_dir_phys(dd)->dd_head_dataset_obj;
797eda14cbcSMatt Macy 	if (obj != 0)
798eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag, &ds);
799eda14cbcSMatt Macy 	else
800eda14cbcSMatt Macy 		err = SET_ERROR(ENOENT);
801eda14cbcSMatt Macy 
802eda14cbcSMatt Macy 	/* we may be looking for a snapshot */
803eda14cbcSMatt Macy 	if (err == 0 && snapname != NULL) {
804eda14cbcSMatt Macy 		dsl_dataset_t *snap_ds;
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy 		if (*snapname++ != '@') {
807eda14cbcSMatt Macy 			dsl_dataset_rele_flags(ds, flags, tag);
808eda14cbcSMatt Macy 			dsl_dir_rele(dd, FTAG);
809eda14cbcSMatt Macy 			return (SET_ERROR(ENOENT));
810eda14cbcSMatt Macy 		}
811eda14cbcSMatt Macy 
812eda14cbcSMatt Macy 		dprintf("looking for snapshot '%s'\n", snapname);
813eda14cbcSMatt Macy 		err = dsl_dataset_snap_lookup(ds, snapname, &obj);
814eda14cbcSMatt Macy 		if (err == 0) {
815eda14cbcSMatt Macy 			err = dsl_dataset_hold_obj_flags(dp, obj, flags, tag,
816eda14cbcSMatt Macy 			    &snap_ds);
817eda14cbcSMatt Macy 		}
818eda14cbcSMatt Macy 		dsl_dataset_rele_flags(ds, flags, tag);
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 		if (err == 0) {
821eda14cbcSMatt Macy 			mutex_enter(&snap_ds->ds_lock);
822eda14cbcSMatt Macy 			if (snap_ds->ds_snapname[0] == 0)
823eda14cbcSMatt Macy 				(void) strlcpy(snap_ds->ds_snapname, snapname,
824eda14cbcSMatt Macy 				    sizeof (snap_ds->ds_snapname));
825eda14cbcSMatt Macy 			mutex_exit(&snap_ds->ds_lock);
826eda14cbcSMatt Macy 			ds = snap_ds;
827eda14cbcSMatt Macy 		}
828eda14cbcSMatt Macy 	}
829eda14cbcSMatt Macy 	if (err == 0)
830eda14cbcSMatt Macy 		*dsp = ds;
831eda14cbcSMatt Macy 	dsl_dir_rele(dd, FTAG);
832eda14cbcSMatt Macy 	return (err);
833eda14cbcSMatt Macy }
834eda14cbcSMatt Macy 
835eda14cbcSMatt Macy int
836a0b956f5SMartin Matuska dsl_dataset_hold(dsl_pool_t *dp, const char *name, const void *tag,
837eda14cbcSMatt Macy     dsl_dataset_t **dsp)
838eda14cbcSMatt Macy {
839eda14cbcSMatt Macy 	return (dsl_dataset_hold_flags(dp, name, 0, tag, dsp));
840eda14cbcSMatt Macy }
841eda14cbcSMatt Macy 
842eda14cbcSMatt Macy static int
843eda14cbcSMatt Macy dsl_dataset_own_obj_impl(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags,
844a0b956f5SMartin Matuska     const void *tag, boolean_t override, dsl_dataset_t **dsp)
845eda14cbcSMatt Macy {
846eda14cbcSMatt Macy 	int err = dsl_dataset_hold_obj_flags(dp, dsobj, flags, tag, dsp);
847eda14cbcSMatt Macy 	if (err != 0)
848eda14cbcSMatt Macy 		return (err);
849eda14cbcSMatt Macy 	if (!dsl_dataset_tryown(*dsp, tag, override)) {
850eda14cbcSMatt Macy 		dsl_dataset_rele_flags(*dsp, flags, tag);
851eda14cbcSMatt Macy 		*dsp = NULL;
852eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
853eda14cbcSMatt Macy 	}
854eda14cbcSMatt Macy 	return (0);
855eda14cbcSMatt Macy }
856eda14cbcSMatt Macy 
857eda14cbcSMatt Macy 
858eda14cbcSMatt Macy int
859eda14cbcSMatt Macy dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, ds_hold_flags_t flags,
860a0b956f5SMartin Matuska     const void *tag, dsl_dataset_t **dsp)
861eda14cbcSMatt Macy {
862eda14cbcSMatt Macy 	return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_FALSE, dsp));
863eda14cbcSMatt Macy }
864eda14cbcSMatt Macy 
865eda14cbcSMatt Macy int
866eda14cbcSMatt Macy dsl_dataset_own_obj_force(dsl_pool_t *dp, uint64_t dsobj,
867a0b956f5SMartin Matuska     ds_hold_flags_t flags, const void *tag, dsl_dataset_t **dsp)
868eda14cbcSMatt Macy {
869eda14cbcSMatt Macy 	return (dsl_dataset_own_obj_impl(dp, dsobj, flags, tag, B_TRUE, dsp));
870eda14cbcSMatt Macy }
871eda14cbcSMatt Macy 
872eda14cbcSMatt Macy static int
873eda14cbcSMatt Macy dsl_dataset_own_impl(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
874a0b956f5SMartin Matuska     const void *tag, boolean_t override, dsl_dataset_t **dsp)
875eda14cbcSMatt Macy {
876eda14cbcSMatt Macy 	int err = dsl_dataset_hold_flags(dp, name, flags, tag, dsp);
877eda14cbcSMatt Macy 	if (err != 0)
878eda14cbcSMatt Macy 		return (err);
879eda14cbcSMatt Macy 	if (!dsl_dataset_tryown(*dsp, tag, override)) {
880eda14cbcSMatt Macy 		dsl_dataset_rele_flags(*dsp, flags, tag);
881eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
882eda14cbcSMatt Macy 	}
883eda14cbcSMatt Macy 	return (0);
884eda14cbcSMatt Macy }
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy int
887eda14cbcSMatt Macy dsl_dataset_own_force(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
888a0b956f5SMartin Matuska     const void *tag, dsl_dataset_t **dsp)
889eda14cbcSMatt Macy {
890eda14cbcSMatt Macy 	return (dsl_dataset_own_impl(dp, name, flags, tag, B_TRUE, dsp));
891eda14cbcSMatt Macy }
892eda14cbcSMatt Macy 
893eda14cbcSMatt Macy int
894eda14cbcSMatt Macy dsl_dataset_own(dsl_pool_t *dp, const char *name, ds_hold_flags_t flags,
895a0b956f5SMartin Matuska     const void *tag, dsl_dataset_t **dsp)
896eda14cbcSMatt Macy {
897eda14cbcSMatt Macy 	return (dsl_dataset_own_impl(dp, name, flags, tag, B_FALSE, dsp));
898eda14cbcSMatt Macy }
899eda14cbcSMatt Macy 
900eda14cbcSMatt Macy /*
901eda14cbcSMatt Macy  * See the comment above dsl_pool_hold() for details.  In summary, a long
902eda14cbcSMatt Macy  * hold is used to prevent destruction of a dataset while the pool hold
903eda14cbcSMatt Macy  * is dropped, allowing other concurrent operations (e.g. spa_sync()).
904eda14cbcSMatt Macy  *
905eda14cbcSMatt Macy  * The dataset and pool must be held when this function is called.  After it
906eda14cbcSMatt Macy  * is called, the pool hold may be released while the dataset is still held
907eda14cbcSMatt Macy  * and accessed.
908eda14cbcSMatt Macy  */
909eda14cbcSMatt Macy void
910e92ffd9bSMartin Matuska dsl_dataset_long_hold(dsl_dataset_t *ds, const void *tag)
911eda14cbcSMatt Macy {
912eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
913eda14cbcSMatt Macy 	(void) zfs_refcount_add(&ds->ds_longholds, tag);
914eda14cbcSMatt Macy }
915eda14cbcSMatt Macy 
916eda14cbcSMatt Macy void
917e92ffd9bSMartin Matuska dsl_dataset_long_rele(dsl_dataset_t *ds, const void *tag)
918eda14cbcSMatt Macy {
919eda14cbcSMatt Macy 	(void) zfs_refcount_remove(&ds->ds_longholds, tag);
920eda14cbcSMatt Macy }
921eda14cbcSMatt Macy 
922eda14cbcSMatt Macy /* Return B_TRUE if there are any long holds on this dataset. */
923eda14cbcSMatt Macy boolean_t
924eda14cbcSMatt Macy dsl_dataset_long_held(dsl_dataset_t *ds)
925eda14cbcSMatt Macy {
926eda14cbcSMatt Macy 	return (!zfs_refcount_is_zero(&ds->ds_longholds));
927eda14cbcSMatt Macy }
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy void
930eda14cbcSMatt Macy dsl_dataset_name(dsl_dataset_t *ds, char *name)
931eda14cbcSMatt Macy {
932eda14cbcSMatt Macy 	if (ds == NULL) {
933eda14cbcSMatt Macy 		(void) strlcpy(name, "mos", ZFS_MAX_DATASET_NAME_LEN);
934eda14cbcSMatt Macy 	} else {
935eda14cbcSMatt Macy 		dsl_dir_name(ds->ds_dir, name);
936eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_get_snapname(ds));
937eda14cbcSMatt Macy 		if (ds->ds_snapname[0]) {
938eda14cbcSMatt Macy 			VERIFY3U(strlcat(name, "@", ZFS_MAX_DATASET_NAME_LEN),
939eda14cbcSMatt Macy 			    <, ZFS_MAX_DATASET_NAME_LEN);
940eda14cbcSMatt Macy 			/*
941eda14cbcSMatt Macy 			 * We use a "recursive" mutex so that we
942eda14cbcSMatt Macy 			 * can call dprintf_ds() with ds_lock held.
943eda14cbcSMatt Macy 			 */
944eda14cbcSMatt Macy 			if (!MUTEX_HELD(&ds->ds_lock)) {
945eda14cbcSMatt Macy 				mutex_enter(&ds->ds_lock);
946eda14cbcSMatt Macy 				VERIFY3U(strlcat(name, ds->ds_snapname,
947eda14cbcSMatt Macy 				    ZFS_MAX_DATASET_NAME_LEN), <,
948eda14cbcSMatt Macy 				    ZFS_MAX_DATASET_NAME_LEN);
949eda14cbcSMatt Macy 				mutex_exit(&ds->ds_lock);
950eda14cbcSMatt Macy 			} else {
951eda14cbcSMatt Macy 				VERIFY3U(strlcat(name, ds->ds_snapname,
952eda14cbcSMatt Macy 				    ZFS_MAX_DATASET_NAME_LEN), <,
953eda14cbcSMatt Macy 				    ZFS_MAX_DATASET_NAME_LEN);
954eda14cbcSMatt Macy 			}
955eda14cbcSMatt Macy 		}
956eda14cbcSMatt Macy 	}
957eda14cbcSMatt Macy }
958eda14cbcSMatt Macy 
959eda14cbcSMatt Macy int
960eda14cbcSMatt Macy dsl_dataset_namelen(dsl_dataset_t *ds)
961eda14cbcSMatt Macy {
962eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_get_snapname(ds));
963eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
964eda14cbcSMatt Macy 	int len = strlen(ds->ds_snapname);
965eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
966eda14cbcSMatt Macy 	/* add '@' if ds is a snap */
967eda14cbcSMatt Macy 	if (len > 0)
968eda14cbcSMatt Macy 		len++;
969eda14cbcSMatt Macy 	len += dsl_dir_namelen(ds->ds_dir);
970eda14cbcSMatt Macy 	return (len);
971eda14cbcSMatt Macy }
972eda14cbcSMatt Macy 
973eda14cbcSMatt Macy void
974a0b956f5SMartin Matuska dsl_dataset_rele(dsl_dataset_t *ds, const void *tag)
975eda14cbcSMatt Macy {
976eda14cbcSMatt Macy 	dmu_buf_rele(ds->ds_dbuf, tag);
977eda14cbcSMatt Macy }
978eda14cbcSMatt Macy 
979eda14cbcSMatt Macy void
980eda14cbcSMatt Macy dsl_dataset_remove_key_mapping(dsl_dataset_t *ds)
981eda14cbcSMatt Macy {
982eda14cbcSMatt Macy 	dsl_dir_t *dd = ds->ds_dir;
983eda14cbcSMatt Macy 
984eda14cbcSMatt Macy 	if (dd == NULL || dd->dd_crypto_obj == 0)
985eda14cbcSMatt Macy 		return;
986eda14cbcSMatt Macy 
987eda14cbcSMatt Macy 	(void) spa_keystore_remove_mapping(dd->dd_pool->dp_spa,
988eda14cbcSMatt Macy 	    ds->ds_object, ds);
989eda14cbcSMatt Macy }
990eda14cbcSMatt Macy 
991eda14cbcSMatt Macy void
992a0b956f5SMartin Matuska dsl_dataset_rele_flags(dsl_dataset_t *ds, ds_hold_flags_t flags,
993a0b956f5SMartin Matuska     const void *tag)
994eda14cbcSMatt Macy {
995eda14cbcSMatt Macy 	if (flags & DS_HOLD_FLAG_DECRYPT)
996eda14cbcSMatt Macy 		dsl_dataset_remove_key_mapping(ds);
997eda14cbcSMatt Macy 
998eda14cbcSMatt Macy 	dsl_dataset_rele(ds, tag);
999eda14cbcSMatt Macy }
1000eda14cbcSMatt Macy 
1001eda14cbcSMatt Macy void
1002a0b956f5SMartin Matuska dsl_dataset_disown(dsl_dataset_t *ds, ds_hold_flags_t flags, const void *tag)
1003eda14cbcSMatt Macy {
1004eda14cbcSMatt Macy 	ASSERT3P(ds->ds_owner, ==, tag);
1005eda14cbcSMatt Macy 	ASSERT(ds->ds_dbuf != NULL);
1006eda14cbcSMatt Macy 
1007eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
1008eda14cbcSMatt Macy 	ds->ds_owner = NULL;
1009eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
1010eda14cbcSMatt Macy 	dsl_dataset_long_rele(ds, tag);
1011eda14cbcSMatt Macy 	dsl_dataset_rele_flags(ds, flags, tag);
1012eda14cbcSMatt Macy }
1013eda14cbcSMatt Macy 
1014eda14cbcSMatt Macy boolean_t
1015a0b956f5SMartin Matuska dsl_dataset_tryown(dsl_dataset_t *ds, const void *tag, boolean_t override)
1016eda14cbcSMatt Macy {
1017eda14cbcSMatt Macy 	boolean_t gotit = FALSE;
1018eda14cbcSMatt Macy 
1019eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1020eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
1021eda14cbcSMatt Macy 	if (ds->ds_owner == NULL && (override || !(DS_IS_INCONSISTENT(ds) ||
1022eda14cbcSMatt Macy 	    (dsl_dataset_feature_is_active(ds,
1023eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS) &&
1024eda14cbcSMatt Macy 	    !zfs_allow_redacted_dataset_mount)))) {
1025eda14cbcSMatt Macy 		ds->ds_owner = tag;
1026eda14cbcSMatt Macy 		dsl_dataset_long_hold(ds, tag);
1027eda14cbcSMatt Macy 		gotit = TRUE;
1028eda14cbcSMatt Macy 	}
1029eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
1030eda14cbcSMatt Macy 	return (gotit);
1031eda14cbcSMatt Macy }
1032eda14cbcSMatt Macy 
1033eda14cbcSMatt Macy boolean_t
1034eda14cbcSMatt Macy dsl_dataset_has_owner(dsl_dataset_t *ds)
1035eda14cbcSMatt Macy {
1036eda14cbcSMatt Macy 	boolean_t rv;
1037eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
1038eda14cbcSMatt Macy 	rv = (ds->ds_owner != NULL);
1039eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
1040eda14cbcSMatt Macy 	return (rv);
1041eda14cbcSMatt Macy }
1042eda14cbcSMatt Macy 
10432a58b312SMartin Matuska static boolean_t
1044eda14cbcSMatt Macy zfeature_active(spa_feature_t f, void *arg)
1045eda14cbcSMatt Macy {
1046eda14cbcSMatt Macy 	switch (spa_feature_table[f].fi_type) {
1047eda14cbcSMatt Macy 	case ZFEATURE_TYPE_BOOLEAN: {
1048eda14cbcSMatt Macy 		boolean_t val = (boolean_t)(uintptr_t)arg;
1049eda14cbcSMatt Macy 		ASSERT(val == B_FALSE || val == B_TRUE);
1050eda14cbcSMatt Macy 		return (val);
1051eda14cbcSMatt Macy 	}
1052eda14cbcSMatt Macy 	case ZFEATURE_TYPE_UINT64_ARRAY:
1053eda14cbcSMatt Macy 		/*
1054eda14cbcSMatt Macy 		 * In this case, arg is a uint64_t array.  The feature is active
1055eda14cbcSMatt Macy 		 * if the array is non-null.
1056eda14cbcSMatt Macy 		 */
1057eda14cbcSMatt Macy 		return (arg != NULL);
1058eda14cbcSMatt Macy 	default:
1059eda14cbcSMatt Macy 		panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
1060eda14cbcSMatt Macy 		return (B_FALSE);
1061eda14cbcSMatt Macy 	}
1062eda14cbcSMatt Macy }
1063eda14cbcSMatt Macy 
1064eda14cbcSMatt Macy boolean_t
1065eda14cbcSMatt Macy dsl_dataset_feature_is_active(dsl_dataset_t *ds, spa_feature_t f)
1066eda14cbcSMatt Macy {
1067eda14cbcSMatt Macy 	return (zfeature_active(f, ds->ds_feature[f]));
1068eda14cbcSMatt Macy }
1069eda14cbcSMatt Macy 
1070eda14cbcSMatt Macy /*
1071eda14cbcSMatt Macy  * The buffers passed out by this function are references to internal buffers;
1072eda14cbcSMatt Macy  * they should not be freed by callers of this function, and they should not be
1073eda14cbcSMatt Macy  * used after the dataset has been released.
1074eda14cbcSMatt Macy  */
1075eda14cbcSMatt Macy boolean_t
1076eda14cbcSMatt Macy dsl_dataset_get_uint64_array_feature(dsl_dataset_t *ds, spa_feature_t f,
1077eda14cbcSMatt Macy     uint64_t *outlength, uint64_t **outp)
1078eda14cbcSMatt Macy {
1079eda14cbcSMatt Macy 	VERIFY(spa_feature_table[f].fi_type & ZFEATURE_TYPE_UINT64_ARRAY);
1080eda14cbcSMatt Macy 	if (!dsl_dataset_feature_is_active(ds, f)) {
1081eda14cbcSMatt Macy 		return (B_FALSE);
1082eda14cbcSMatt Macy 	}
1083eda14cbcSMatt Macy 	struct feature_type_uint64_array_arg *ftuaa = ds->ds_feature[f];
1084eda14cbcSMatt Macy 	*outp = ftuaa->array;
1085eda14cbcSMatt Macy 	*outlength = ftuaa->length;
1086eda14cbcSMatt Macy 	return (B_TRUE);
1087eda14cbcSMatt Macy }
1088eda14cbcSMatt Macy 
1089eda14cbcSMatt Macy void
1090eda14cbcSMatt Macy dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, void *arg,
1091eda14cbcSMatt Macy     dmu_tx_t *tx)
1092eda14cbcSMatt Macy {
1093eda14cbcSMatt Macy 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1094eda14cbcSMatt Macy 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
1095eda14cbcSMatt Macy 	uint64_t zero = 0;
1096eda14cbcSMatt Macy 
1097eda14cbcSMatt Macy 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
1098eda14cbcSMatt Macy 
1099eda14cbcSMatt Macy 	spa_feature_incr(spa, f, tx);
1100eda14cbcSMatt Macy 	dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1101eda14cbcSMatt Macy 
1102eda14cbcSMatt Macy 	switch (spa_feature_table[f].fi_type) {
1103eda14cbcSMatt Macy 	case ZFEATURE_TYPE_BOOLEAN:
1104eda14cbcSMatt Macy 		ASSERT3S((boolean_t)(uintptr_t)arg, ==, B_TRUE);
1105eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
1106eda14cbcSMatt Macy 		    sizeof (zero), 1, &zero, tx));
1107eda14cbcSMatt Macy 		break;
1108eda14cbcSMatt Macy 	case ZFEATURE_TYPE_UINT64_ARRAY:
1109eda14cbcSMatt Macy 	{
1110eda14cbcSMatt Macy 		struct feature_type_uint64_array_arg *ftuaa = arg;
1111eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid,
1112eda14cbcSMatt Macy 		    sizeof (uint64_t), ftuaa->length, ftuaa->array, tx));
1113eda14cbcSMatt Macy 		break;
1114eda14cbcSMatt Macy 	}
1115eda14cbcSMatt Macy 	default:
1116eda14cbcSMatt Macy 		panic("Invalid zfeature type %d", spa_feature_table[f].fi_type);
1117eda14cbcSMatt Macy 	}
1118eda14cbcSMatt Macy }
1119eda14cbcSMatt Macy 
1120eda14cbcSMatt Macy static void
1121eda14cbcSMatt Macy dsl_dataset_deactivate_feature_impl(dsl_dataset_t *ds, spa_feature_t f,
1122eda14cbcSMatt Macy     dmu_tx_t *tx)
1123eda14cbcSMatt Macy {
1124eda14cbcSMatt Macy 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1125eda14cbcSMatt Macy 	objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset;
1126eda14cbcSMatt Macy 	uint64_t dsobj = ds->ds_object;
1127eda14cbcSMatt Macy 
1128eda14cbcSMatt Macy 	VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET);
1129eda14cbcSMatt Macy 
1130eda14cbcSMatt Macy 	VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx));
1131eda14cbcSMatt Macy 	spa_feature_decr(spa, f, tx);
1132eda14cbcSMatt Macy 	ds->ds_feature[f] = NULL;
1133eda14cbcSMatt Macy }
1134eda14cbcSMatt Macy 
1135eda14cbcSMatt Macy void
1136eda14cbcSMatt Macy dsl_dataset_deactivate_feature(dsl_dataset_t *ds, spa_feature_t f, dmu_tx_t *tx)
1137eda14cbcSMatt Macy {
1138eda14cbcSMatt Macy 	unload_zfeature(ds, f);
1139eda14cbcSMatt Macy 	dsl_dataset_deactivate_feature_impl(ds, f, tx);
1140eda14cbcSMatt Macy }
1141eda14cbcSMatt Macy 
1142eda14cbcSMatt Macy uint64_t
1143eda14cbcSMatt Macy dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
1144eda14cbcSMatt Macy     dsl_crypto_params_t *dcp, uint64_t flags, dmu_tx_t *tx)
1145eda14cbcSMatt Macy {
1146eda14cbcSMatt Macy 	dsl_pool_t *dp = dd->dd_pool;
1147eda14cbcSMatt Macy 	dmu_buf_t *dbuf;
1148eda14cbcSMatt Macy 	dsl_dataset_phys_t *dsphys;
1149eda14cbcSMatt Macy 	uint64_t dsobj;
1150eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1151eda14cbcSMatt Macy 
1152eda14cbcSMatt Macy 	if (origin == NULL)
1153eda14cbcSMatt Macy 		origin = dp->dp_origin_snap;
1154eda14cbcSMatt Macy 
1155eda14cbcSMatt Macy 	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
1156eda14cbcSMatt Macy 	ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0);
1157eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1158eda14cbcSMatt Macy 	ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1159eda14cbcSMatt Macy 
1160eda14cbcSMatt Macy 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1161eda14cbcSMatt Macy 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1162eda14cbcSMatt Macy 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1163eda14cbcSMatt Macy 	dmu_buf_will_dirty(dbuf, tx);
1164eda14cbcSMatt Macy 	dsphys = dbuf->db_data;
1165da5137abSMartin Matuska 	memset(dsphys, 0, sizeof (dsl_dataset_phys_t));
1166eda14cbcSMatt Macy 	dsphys->ds_dir_obj = dd->dd_object;
1167eda14cbcSMatt Macy 	dsphys->ds_flags = flags;
1168eda14cbcSMatt Macy 	dsphys->ds_fsid_guid = unique_create();
1169eda14cbcSMatt Macy 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1170eda14cbcSMatt Macy 	    sizeof (dsphys->ds_guid));
1171eda14cbcSMatt Macy 	dsphys->ds_snapnames_zapobj =
1172eda14cbcSMatt Macy 	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
1173eda14cbcSMatt Macy 	    DMU_OT_NONE, 0, tx);
1174eda14cbcSMatt Macy 	dsphys->ds_creation_time = gethrestime_sec();
1175eda14cbcSMatt Macy 	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
1176eda14cbcSMatt Macy 
1177eda14cbcSMatt Macy 	if (origin == NULL) {
1178eda14cbcSMatt Macy 		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
1179eda14cbcSMatt Macy 	} else {
1180eda14cbcSMatt Macy 		dsl_dataset_t *ohds; /* head of the origin snapshot */
1181eda14cbcSMatt Macy 
1182eda14cbcSMatt Macy 		dsphys->ds_prev_snap_obj = origin->ds_object;
1183eda14cbcSMatt Macy 		dsphys->ds_prev_snap_txg =
1184eda14cbcSMatt Macy 		    dsl_dataset_phys(origin)->ds_creation_txg;
1185eda14cbcSMatt Macy 		dsphys->ds_referenced_bytes =
1186eda14cbcSMatt Macy 		    dsl_dataset_phys(origin)->ds_referenced_bytes;
1187eda14cbcSMatt Macy 		dsphys->ds_compressed_bytes =
1188eda14cbcSMatt Macy 		    dsl_dataset_phys(origin)->ds_compressed_bytes;
1189eda14cbcSMatt Macy 		dsphys->ds_uncompressed_bytes =
1190eda14cbcSMatt Macy 		    dsl_dataset_phys(origin)->ds_uncompressed_bytes;
1191eda14cbcSMatt Macy 		rrw_enter(&origin->ds_bp_rwlock, RW_READER, FTAG);
1192eda14cbcSMatt Macy 		dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp;
1193eda14cbcSMatt Macy 		rrw_exit(&origin->ds_bp_rwlock, FTAG);
1194eda14cbcSMatt Macy 
1195eda14cbcSMatt Macy 		/*
1196eda14cbcSMatt Macy 		 * Inherit flags that describe the dataset's contents
1197eda14cbcSMatt Macy 		 * (INCONSISTENT) or properties (Case Insensitive).
1198eda14cbcSMatt Macy 		 */
1199eda14cbcSMatt Macy 		dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags &
1200eda14cbcSMatt Macy 		    (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET);
1201eda14cbcSMatt Macy 
1202eda14cbcSMatt Macy 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1203eda14cbcSMatt Macy 			if (zfeature_active(f, origin->ds_feature[f])) {
1204eda14cbcSMatt Macy 				dsl_dataset_activate_feature(dsobj, f,
1205eda14cbcSMatt Macy 				    origin->ds_feature[f], tx);
1206eda14cbcSMatt Macy 			}
1207eda14cbcSMatt Macy 		}
1208eda14cbcSMatt Macy 
1209eda14cbcSMatt Macy 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
1210eda14cbcSMatt Macy 		dsl_dataset_phys(origin)->ds_num_children++;
1211eda14cbcSMatt Macy 
1212eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold_obj(dp,
1213eda14cbcSMatt Macy 		    dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj,
1214eda14cbcSMatt Macy 		    FTAG, &ohds));
1215eda14cbcSMatt Macy 		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
1216eda14cbcSMatt Macy 		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
1217eda14cbcSMatt Macy 		dsl_dataset_rele(ohds, FTAG);
1218eda14cbcSMatt Macy 
1219eda14cbcSMatt Macy 		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
1220eda14cbcSMatt Macy 			if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) {
1221eda14cbcSMatt Macy 				dsl_dataset_phys(origin)->ds_next_clones_obj =
1222eda14cbcSMatt Macy 				    zap_create(mos,
1223eda14cbcSMatt Macy 				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
1224eda14cbcSMatt Macy 			}
1225eda14cbcSMatt Macy 			VERIFY0(zap_add_int(mos,
1226eda14cbcSMatt Macy 			    dsl_dataset_phys(origin)->ds_next_clones_obj,
1227eda14cbcSMatt Macy 			    dsobj, tx));
1228eda14cbcSMatt Macy 		}
1229eda14cbcSMatt Macy 
1230eda14cbcSMatt Macy 		dmu_buf_will_dirty(dd->dd_dbuf, tx);
1231eda14cbcSMatt Macy 		dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object;
1232eda14cbcSMatt Macy 		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
1233eda14cbcSMatt Macy 			if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) {
1234eda14cbcSMatt Macy 				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
1235eda14cbcSMatt Macy 				dsl_dir_phys(origin->ds_dir)->dd_clones =
1236eda14cbcSMatt Macy 				    zap_create(mos,
1237eda14cbcSMatt Macy 				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
1238eda14cbcSMatt Macy 			}
1239eda14cbcSMatt Macy 			VERIFY0(zap_add_int(mos,
1240eda14cbcSMatt Macy 			    dsl_dir_phys(origin->ds_dir)->dd_clones,
1241eda14cbcSMatt Macy 			    dsobj, tx));
1242eda14cbcSMatt Macy 		}
1243eda14cbcSMatt Macy 	}
1244eda14cbcSMatt Macy 
1245eda14cbcSMatt Macy 	/* handle encryption */
1246eda14cbcSMatt Macy 	dsl_dataset_create_crypt_sync(dsobj, dd, origin, dcp, tx);
1247eda14cbcSMatt Macy 
1248eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1249eda14cbcSMatt Macy 		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 	dmu_buf_rele(dbuf, FTAG);
1252eda14cbcSMatt Macy 
1253eda14cbcSMatt Macy 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1254eda14cbcSMatt Macy 	dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj;
1255eda14cbcSMatt Macy 
1256eda14cbcSMatt Macy 	return (dsobj);
1257eda14cbcSMatt Macy }
1258eda14cbcSMatt Macy 
1259eda14cbcSMatt Macy static void
1260eda14cbcSMatt Macy dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
1261eda14cbcSMatt Macy {
1262eda14cbcSMatt Macy 	objset_t *os;
1263eda14cbcSMatt Macy 
1264eda14cbcSMatt Macy 	VERIFY0(dmu_objset_from_ds(ds, &os));
1265da5137abSMartin Matuska 	if (memcmp(&os->os_zil_header, &zero_zil, sizeof (zero_zil)) != 0) {
1266eda14cbcSMatt Macy 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
1267eda14cbcSMatt Macy 		zio_t *zio;
1268eda14cbcSMatt Macy 
1269da5137abSMartin Matuska 		memset(&os->os_zil_header, 0, sizeof (os->os_zil_header));
1270eda14cbcSMatt Macy 		if (os->os_encrypted)
1271eda14cbcSMatt Macy 			os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
1272eda14cbcSMatt Macy 
1273eda14cbcSMatt Macy 		zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1274eda14cbcSMatt Macy 		dsl_dataset_sync(ds, zio, tx);
1275eda14cbcSMatt Macy 		VERIFY0(zio_wait(zio));
1276eda14cbcSMatt Macy 		dsl_dataset_sync_done(ds, tx);
1277eda14cbcSMatt Macy 	}
1278eda14cbcSMatt Macy }
1279eda14cbcSMatt Macy 
1280eda14cbcSMatt Macy uint64_t
1281eda14cbcSMatt Macy dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
1282eda14cbcSMatt Macy     dsl_dataset_t *origin, uint64_t flags, cred_t *cr,
1283eda14cbcSMatt Macy     dsl_crypto_params_t *dcp, dmu_tx_t *tx)
1284eda14cbcSMatt Macy {
1285eda14cbcSMatt Macy 	dsl_pool_t *dp = pdd->dd_pool;
1286eda14cbcSMatt Macy 	uint64_t dsobj, ddobj;
1287eda14cbcSMatt Macy 	dsl_dir_t *dd;
1288eda14cbcSMatt Macy 
1289eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1290eda14cbcSMatt Macy 	ASSERT(lastname[0] != '@');
1291eda14cbcSMatt Macy 	/*
1292eda14cbcSMatt Macy 	 * Filesystems will eventually have their origin set to dp_origin_snap,
1293eda14cbcSMatt Macy 	 * but that's taken care of in dsl_dataset_create_sync_dd. When
1294eda14cbcSMatt Macy 	 * creating a filesystem, this function is called with origin equal to
1295eda14cbcSMatt Macy 	 * NULL.
1296eda14cbcSMatt Macy 	 */
1297eda14cbcSMatt Macy 	if (origin != NULL)
1298eda14cbcSMatt Macy 		ASSERT3P(origin, !=, dp->dp_origin_snap);
1299eda14cbcSMatt Macy 
1300eda14cbcSMatt Macy 	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
1301eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
1302eda14cbcSMatt Macy 
1303eda14cbcSMatt Macy 	dsobj = dsl_dataset_create_sync_dd(dd, origin, dcp,
1304eda14cbcSMatt Macy 	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
1305eda14cbcSMatt Macy 
1306eda14cbcSMatt Macy 	dsl_deleg_set_create_perms(dd, tx, cr);
1307eda14cbcSMatt Macy 
1308eda14cbcSMatt Macy 	/*
1309eda14cbcSMatt Macy 	 * If we are creating a clone and the livelist feature is enabled,
1310eda14cbcSMatt Macy 	 * add the entry DD_FIELD_LIVELIST to ZAP.
1311eda14cbcSMatt Macy 	 */
1312eda14cbcSMatt Macy 	if (origin != NULL &&
1313eda14cbcSMatt Macy 	    spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LIVELIST)) {
1314eda14cbcSMatt Macy 		objset_t *mos = dd->dd_pool->dp_meta_objset;
1315eda14cbcSMatt Macy 		dsl_dir_zapify(dd, tx);
1316eda14cbcSMatt Macy 		uint64_t obj = dsl_deadlist_alloc(mos, tx);
1317eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_LIVELIST,
1318eda14cbcSMatt Macy 		    sizeof (uint64_t), 1, &obj, tx));
1319eda14cbcSMatt Macy 		spa_feature_incr(dp->dp_spa, SPA_FEATURE_LIVELIST, tx);
1320eda14cbcSMatt Macy 	}
1321eda14cbcSMatt Macy 
1322eda14cbcSMatt Macy 	/*
1323eda14cbcSMatt Macy 	 * Since we're creating a new node we know it's a leaf, so we can
1324eda14cbcSMatt Macy 	 * initialize the counts if the limit feature is active.
1325eda14cbcSMatt Macy 	 */
1326eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
1327eda14cbcSMatt Macy 		uint64_t cnt = 0;
1328eda14cbcSMatt Macy 		objset_t *os = dd->dd_pool->dp_meta_objset;
1329eda14cbcSMatt Macy 
1330eda14cbcSMatt Macy 		dsl_dir_zapify(dd, tx);
1331eda14cbcSMatt Macy 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1332eda14cbcSMatt Macy 		    sizeof (cnt), 1, &cnt, tx));
1333eda14cbcSMatt Macy 		VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1334eda14cbcSMatt Macy 		    sizeof (cnt), 1, &cnt, tx));
1335eda14cbcSMatt Macy 	}
1336eda14cbcSMatt Macy 
1337eda14cbcSMatt Macy 	dsl_dir_rele(dd, FTAG);
1338eda14cbcSMatt Macy 
1339eda14cbcSMatt Macy 	/*
1340eda14cbcSMatt Macy 	 * If we are creating a clone, make sure we zero out any stale
1341eda14cbcSMatt Macy 	 * data from the origin snapshots zil header.
1342eda14cbcSMatt Macy 	 */
1343eda14cbcSMatt Macy 	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
1344eda14cbcSMatt Macy 		dsl_dataset_t *ds;
1345eda14cbcSMatt Macy 
1346eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1347eda14cbcSMatt Macy 		dsl_dataset_zero_zil(ds, tx);
1348eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1349eda14cbcSMatt Macy 	}
1350eda14cbcSMatt Macy 
1351eda14cbcSMatt Macy 	return (dsobj);
1352eda14cbcSMatt Macy }
1353eda14cbcSMatt Macy 
1354eda14cbcSMatt Macy /*
1355eda14cbcSMatt Macy  * The unique space in the head dataset can be calculated by subtracting
1356eda14cbcSMatt Macy  * the space used in the most recent snapshot, that is still being used
1357eda14cbcSMatt Macy  * in this file system, from the space currently in use.  To figure out
1358eda14cbcSMatt Macy  * the space in the most recent snapshot still in use, we need to take
1359eda14cbcSMatt Macy  * the total space used in the snapshot and subtract out the space that
1360eda14cbcSMatt Macy  * has been freed up since the snapshot was taken.
1361eda14cbcSMatt Macy  */
1362eda14cbcSMatt Macy void
1363eda14cbcSMatt Macy dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
1364eda14cbcSMatt Macy {
1365eda14cbcSMatt Macy 	uint64_t mrs_used;
1366eda14cbcSMatt Macy 	uint64_t dlused, dlcomp, dluncomp;
1367eda14cbcSMatt Macy 
1368eda14cbcSMatt Macy 	ASSERT(!ds->ds_is_snapshot);
1369eda14cbcSMatt Macy 
1370eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0)
1371eda14cbcSMatt Macy 		mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes;
1372eda14cbcSMatt Macy 	else
1373eda14cbcSMatt Macy 		mrs_used = 0;
1374eda14cbcSMatt Macy 
1375eda14cbcSMatt Macy 	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
1376eda14cbcSMatt Macy 
1377eda14cbcSMatt Macy 	ASSERT3U(dlused, <=, mrs_used);
1378eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_unique_bytes =
1379eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused);
1380eda14cbcSMatt Macy 
1381eda14cbcSMatt Macy 	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
1382eda14cbcSMatt Macy 	    SPA_VERSION_UNIQUE_ACCURATE)
1383eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1384eda14cbcSMatt Macy }
1385eda14cbcSMatt Macy 
1386eda14cbcSMatt Macy void
1387eda14cbcSMatt Macy dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
1388eda14cbcSMatt Macy     dmu_tx_t *tx)
1389eda14cbcSMatt Macy {
1390eda14cbcSMatt Macy 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
1391eda14cbcSMatt Macy 	uint64_t count __maybe_unused;
1392eda14cbcSMatt Macy 	int err;
1393eda14cbcSMatt Macy 
1394eda14cbcSMatt Macy 	ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2);
1395eda14cbcSMatt Macy 	err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1396eda14cbcSMatt Macy 	    obj, tx);
1397eda14cbcSMatt Macy 	/*
1398eda14cbcSMatt Macy 	 * The err should not be ENOENT, but a bug in a previous version
1399eda14cbcSMatt Macy 	 * of the code could cause upgrade_clones_cb() to not set
1400eda14cbcSMatt Macy 	 * ds_next_snap_obj when it should, leading to a missing entry.
1401eda14cbcSMatt Macy 	 * If we knew that the pool was created after
1402eda14cbcSMatt Macy 	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
1403eda14cbcSMatt Macy 	 * ENOENT.  However, at least we can check that we don't have
1404eda14cbcSMatt Macy 	 * too many entries in the next_clones_obj even after failing to
1405eda14cbcSMatt Macy 	 * remove this one.
1406eda14cbcSMatt Macy 	 */
1407eda14cbcSMatt Macy 	if (err != ENOENT)
1408eda14cbcSMatt Macy 		VERIFY0(err);
1409eda14cbcSMatt Macy 	ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
1410eda14cbcSMatt Macy 	    &count));
1411eda14cbcSMatt Macy 	ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2);
1412eda14cbcSMatt Macy }
1413eda14cbcSMatt Macy 
1414eda14cbcSMatt Macy 
1415eda14cbcSMatt Macy blkptr_t *
1416eda14cbcSMatt Macy dsl_dataset_get_blkptr(dsl_dataset_t *ds)
1417eda14cbcSMatt Macy {
1418eda14cbcSMatt Macy 	return (&dsl_dataset_phys(ds)->ds_bp);
1419eda14cbcSMatt Macy }
1420eda14cbcSMatt Macy 
1421eda14cbcSMatt Macy spa_t *
1422eda14cbcSMatt Macy dsl_dataset_get_spa(dsl_dataset_t *ds)
1423eda14cbcSMatt Macy {
1424eda14cbcSMatt Macy 	return (ds->ds_dir->dd_pool->dp_spa);
1425eda14cbcSMatt Macy }
1426eda14cbcSMatt Macy 
1427eda14cbcSMatt Macy void
1428eda14cbcSMatt Macy dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
1429eda14cbcSMatt Macy {
1430eda14cbcSMatt Macy 	dsl_pool_t *dp;
1431eda14cbcSMatt Macy 
1432eda14cbcSMatt Macy 	if (ds == NULL) /* this is the meta-objset */
1433eda14cbcSMatt Macy 		return;
1434eda14cbcSMatt Macy 
1435eda14cbcSMatt Macy 	ASSERT(ds->ds_objset != NULL);
1436eda14cbcSMatt Macy 
1437eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0)
1438eda14cbcSMatt Macy 		panic("dirtying snapshot!");
1439eda14cbcSMatt Macy 
1440eda14cbcSMatt Macy 	/* Must not dirty a dataset in the same txg where it got snapshotted. */
1441eda14cbcSMatt Macy 	ASSERT3U(tx->tx_txg, >, dsl_dataset_phys(ds)->ds_prev_snap_txg);
1442eda14cbcSMatt Macy 
1443eda14cbcSMatt Macy 	dp = ds->ds_dir->dd_pool;
1444eda14cbcSMatt Macy 	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1445eda14cbcSMatt Macy 		objset_t *os = ds->ds_objset;
1446eda14cbcSMatt Macy 
1447eda14cbcSMatt Macy 		/* up the hold count until we can be written out */
1448eda14cbcSMatt Macy 		dmu_buf_add_ref(ds->ds_dbuf, ds);
1449eda14cbcSMatt Macy 
1450eda14cbcSMatt Macy 		/* if this dataset is encrypted, grab a reference to the DCK */
1451eda14cbcSMatt Macy 		if (ds->ds_dir->dd_crypto_obj != 0 &&
1452eda14cbcSMatt Macy 		    !os->os_raw_receive &&
1453eda14cbcSMatt Macy 		    !os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
1454eda14cbcSMatt Macy 			ASSERT3P(ds->ds_key_mapping, !=, NULL);
1455eda14cbcSMatt Macy 			key_mapping_add_ref(ds->ds_key_mapping, ds);
1456eda14cbcSMatt Macy 		}
1457eda14cbcSMatt Macy 	}
1458eda14cbcSMatt Macy }
1459eda14cbcSMatt Macy 
1460eda14cbcSMatt Macy static int
1461eda14cbcSMatt Macy dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
1462eda14cbcSMatt Macy {
1463eda14cbcSMatt Macy 	uint64_t asize;
1464eda14cbcSMatt Macy 
1465eda14cbcSMatt Macy 	if (!dmu_tx_is_syncing(tx))
1466eda14cbcSMatt Macy 		return (0);
1467eda14cbcSMatt Macy 
1468eda14cbcSMatt Macy 	/*
1469eda14cbcSMatt Macy 	 * If there's an fs-only reservation, any blocks that might become
1470eda14cbcSMatt Macy 	 * owned by the snapshot dataset must be accommodated by space
1471eda14cbcSMatt Macy 	 * outside of the reservation.
1472eda14cbcSMatt Macy 	 */
1473eda14cbcSMatt Macy 	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
1474eda14cbcSMatt Macy 	asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved);
1475eda14cbcSMatt Macy 	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
1476eda14cbcSMatt Macy 		return (SET_ERROR(ENOSPC));
1477eda14cbcSMatt Macy 
1478eda14cbcSMatt Macy 	/*
1479eda14cbcSMatt Macy 	 * Propagate any reserved space for this snapshot to other
1480eda14cbcSMatt Macy 	 * snapshot checks in this sync group.
1481eda14cbcSMatt Macy 	 */
1482eda14cbcSMatt Macy 	if (asize > 0)
1483eda14cbcSMatt Macy 		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
1484eda14cbcSMatt Macy 
1485eda14cbcSMatt Macy 	return (0);
1486eda14cbcSMatt Macy }
1487eda14cbcSMatt Macy 
1488eda14cbcSMatt Macy int
1489eda14cbcSMatt Macy dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
1490eda14cbcSMatt Macy     dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr, proc_t *proc)
1491eda14cbcSMatt Macy {
1492eda14cbcSMatt Macy 	int error;
1493eda14cbcSMatt Macy 	uint64_t value;
1494eda14cbcSMatt Macy 
1495eda14cbcSMatt Macy 	ds->ds_trysnap_txg = tx->tx_txg;
1496eda14cbcSMatt Macy 
1497eda14cbcSMatt Macy 	if (!dmu_tx_is_syncing(tx))
1498eda14cbcSMatt Macy 		return (0);
1499eda14cbcSMatt Macy 
1500eda14cbcSMatt Macy 	/*
1501eda14cbcSMatt Macy 	 * We don't allow multiple snapshots of the same txg.  If there
1502eda14cbcSMatt Macy 	 * is already one, try again.
1503eda14cbcSMatt Macy 	 */
1504eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg)
1505eda14cbcSMatt Macy 		return (SET_ERROR(EAGAIN));
1506eda14cbcSMatt Macy 
1507eda14cbcSMatt Macy 	/*
1508eda14cbcSMatt Macy 	 * Check for conflicting snapshot name.
1509eda14cbcSMatt Macy 	 */
1510eda14cbcSMatt Macy 	error = dsl_dataset_snap_lookup(ds, snapname, &value);
1511eda14cbcSMatt Macy 	if (error == 0)
1512eda14cbcSMatt Macy 		return (SET_ERROR(EEXIST));
1513eda14cbcSMatt Macy 	if (error != ENOENT)
1514eda14cbcSMatt Macy 		return (error);
1515eda14cbcSMatt Macy 
1516eda14cbcSMatt Macy 	/*
1517eda14cbcSMatt Macy 	 * We don't allow taking snapshots of inconsistent datasets, such as
1518eda14cbcSMatt Macy 	 * those into which we are currently receiving.  However, if we are
1519eda14cbcSMatt Macy 	 * creating this snapshot as part of a receive, this check will be
1520eda14cbcSMatt Macy 	 * executed atomically with respect to the completion of the receive
1521eda14cbcSMatt Macy 	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
1522eda14cbcSMatt Macy 	 * case we ignore this, knowing it will be fixed up for us shortly in
1523eda14cbcSMatt Macy 	 * dmu_recv_end_sync().
1524eda14cbcSMatt Macy 	 */
1525eda14cbcSMatt Macy 	if (!recv && DS_IS_INCONSISTENT(ds))
1526eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
1527eda14cbcSMatt Macy 
1528eda14cbcSMatt Macy 	/*
1529eda14cbcSMatt Macy 	 * Skip the check for temporary snapshots or if we have already checked
1530eda14cbcSMatt Macy 	 * the counts in dsl_dataset_snapshot_check. This means we really only
1531eda14cbcSMatt Macy 	 * check the count here when we're receiving a stream.
1532eda14cbcSMatt Macy 	 */
1533eda14cbcSMatt Macy 	if (cnt != 0 && cr != NULL) {
1534eda14cbcSMatt Macy 		error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1535eda14cbcSMatt Macy 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr, proc);
1536eda14cbcSMatt Macy 		if (error != 0)
1537eda14cbcSMatt Macy 			return (error);
1538eda14cbcSMatt Macy 	}
1539eda14cbcSMatt Macy 
1540eda14cbcSMatt Macy 	error = dsl_dataset_snapshot_reserve_space(ds, tx);
1541eda14cbcSMatt Macy 	if (error != 0)
1542eda14cbcSMatt Macy 		return (error);
1543eda14cbcSMatt Macy 
1544eda14cbcSMatt Macy 	return (0);
1545eda14cbcSMatt Macy }
1546eda14cbcSMatt Macy 
1547eda14cbcSMatt Macy int
1548eda14cbcSMatt Macy dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
1549eda14cbcSMatt Macy {
1550eda14cbcSMatt Macy 	dsl_dataset_snapshot_arg_t *ddsa = arg;
1551eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1552eda14cbcSMatt Macy 	nvpair_t *pair;
1553eda14cbcSMatt Macy 	int rv = 0;
1554eda14cbcSMatt Macy 
1555eda14cbcSMatt Macy 	/*
1556eda14cbcSMatt Macy 	 * Pre-compute how many total new snapshots will be created for each
1557eda14cbcSMatt Macy 	 * level in the tree and below. This is needed for validating the
1558eda14cbcSMatt Macy 	 * snapshot limit when either taking a recursive snapshot or when
1559eda14cbcSMatt Macy 	 * taking multiple snapshots.
1560eda14cbcSMatt Macy 	 *
1561eda14cbcSMatt Macy 	 * The problem is that the counts are not actually adjusted when
1562eda14cbcSMatt Macy 	 * we are checking, only when we finally sync. For a single snapshot,
1563eda14cbcSMatt Macy 	 * this is easy, the count will increase by 1 at each node up the tree,
1564eda14cbcSMatt Macy 	 * but its more complicated for the recursive/multiple snapshot case.
1565eda14cbcSMatt Macy 	 *
1566eda14cbcSMatt Macy 	 * The dsl_fs_ss_limit_check function does recursively check the count
1567eda14cbcSMatt Macy 	 * at each level up the tree but since it is validating each snapshot
1568eda14cbcSMatt Macy 	 * independently we need to be sure that we are validating the complete
1569eda14cbcSMatt Macy 	 * count for the entire set of snapshots. We do this by rolling up the
1570eda14cbcSMatt Macy 	 * counts for each component of the name into an nvlist and then
1571eda14cbcSMatt Macy 	 * checking each of those cases with the aggregated count.
1572eda14cbcSMatt Macy 	 *
1573eda14cbcSMatt Macy 	 * This approach properly handles not only the recursive snapshot
1574eda14cbcSMatt Macy 	 * case (where we get all of those on the ddsa_snaps list) but also
1575eda14cbcSMatt Macy 	 * the sibling case (e.g. snapshot a/b and a/c so that we will also
1576eda14cbcSMatt Macy 	 * validate the limit on 'a' using a count of 2).
1577eda14cbcSMatt Macy 	 *
1578eda14cbcSMatt Macy 	 * We validate the snapshot names in the third loop and only report
1579eda14cbcSMatt Macy 	 * name errors once.
1580eda14cbcSMatt Macy 	 */
1581eda14cbcSMatt Macy 	if (dmu_tx_is_syncing(tx)) {
1582eda14cbcSMatt Macy 		char *nm;
1583eda14cbcSMatt Macy 		nvlist_t *cnt_track = NULL;
1584eda14cbcSMatt Macy 		cnt_track = fnvlist_alloc();
1585eda14cbcSMatt Macy 
1586eda14cbcSMatt Macy 		nm = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1587eda14cbcSMatt Macy 
1588eda14cbcSMatt Macy 		/* Rollup aggregated counts into the cnt_track list */
1589eda14cbcSMatt Macy 		for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1590eda14cbcSMatt Macy 		    pair != NULL;
1591eda14cbcSMatt Macy 		    pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1592eda14cbcSMatt Macy 			char *pdelim;
1593eda14cbcSMatt Macy 			uint64_t val;
1594eda14cbcSMatt Macy 
1595eda14cbcSMatt Macy 			(void) strlcpy(nm, nvpair_name(pair), MAXPATHLEN);
1596eda14cbcSMatt Macy 			pdelim = strchr(nm, '@');
1597eda14cbcSMatt Macy 			if (pdelim == NULL)
1598eda14cbcSMatt Macy 				continue;
1599eda14cbcSMatt Macy 			*pdelim = '\0';
1600eda14cbcSMatt Macy 
1601eda14cbcSMatt Macy 			do {
1602eda14cbcSMatt Macy 				if (nvlist_lookup_uint64(cnt_track, nm,
1603eda14cbcSMatt Macy 				    &val) == 0) {
1604eda14cbcSMatt Macy 					/* update existing entry */
1605eda14cbcSMatt Macy 					fnvlist_add_uint64(cnt_track, nm,
1606eda14cbcSMatt Macy 					    val + 1);
1607eda14cbcSMatt Macy 				} else {
1608eda14cbcSMatt Macy 					/* add to list */
1609eda14cbcSMatt Macy 					fnvlist_add_uint64(cnt_track, nm, 1);
1610eda14cbcSMatt Macy 				}
1611eda14cbcSMatt Macy 
1612eda14cbcSMatt Macy 				pdelim = strrchr(nm, '/');
1613eda14cbcSMatt Macy 				if (pdelim != NULL)
1614eda14cbcSMatt Macy 					*pdelim = '\0';
1615eda14cbcSMatt Macy 			} while (pdelim != NULL);
1616eda14cbcSMatt Macy 		}
1617eda14cbcSMatt Macy 
1618eda14cbcSMatt Macy 		kmem_free(nm, MAXPATHLEN);
1619eda14cbcSMatt Macy 
1620eda14cbcSMatt Macy 		/* Check aggregated counts at each level */
1621eda14cbcSMatt Macy 		for (pair = nvlist_next_nvpair(cnt_track, NULL);
1622eda14cbcSMatt Macy 		    pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) {
1623eda14cbcSMatt Macy 			int error = 0;
16242a58b312SMartin Matuska 			const char *name;
1625eda14cbcSMatt Macy 			uint64_t cnt = 0;
1626eda14cbcSMatt Macy 			dsl_dataset_t *ds;
1627eda14cbcSMatt Macy 
1628eda14cbcSMatt Macy 			name = nvpair_name(pair);
1629eda14cbcSMatt Macy 			cnt = fnvpair_value_uint64(pair);
1630eda14cbcSMatt Macy 			ASSERT(cnt > 0);
1631eda14cbcSMatt Macy 
1632eda14cbcSMatt Macy 			error = dsl_dataset_hold(dp, name, FTAG, &ds);
1633eda14cbcSMatt Macy 			if (error == 0) {
1634eda14cbcSMatt Macy 				error = dsl_fs_ss_limit_check(ds->ds_dir, cnt,
1635eda14cbcSMatt Macy 				    ZFS_PROP_SNAPSHOT_LIMIT, NULL,
1636eda14cbcSMatt Macy 				    ddsa->ddsa_cr, ddsa->ddsa_proc);
1637eda14cbcSMatt Macy 				dsl_dataset_rele(ds, FTAG);
1638eda14cbcSMatt Macy 			}
1639eda14cbcSMatt Macy 
1640eda14cbcSMatt Macy 			if (error != 0) {
1641eda14cbcSMatt Macy 				if (ddsa->ddsa_errors != NULL)
1642eda14cbcSMatt Macy 					fnvlist_add_int32(ddsa->ddsa_errors,
1643eda14cbcSMatt Macy 					    name, error);
1644eda14cbcSMatt Macy 				rv = error;
1645eda14cbcSMatt Macy 				/* only report one error for this check */
1646eda14cbcSMatt Macy 				break;
1647eda14cbcSMatt Macy 			}
1648eda14cbcSMatt Macy 		}
1649eda14cbcSMatt Macy 		nvlist_free(cnt_track);
1650eda14cbcSMatt Macy 	}
1651eda14cbcSMatt Macy 
1652eda14cbcSMatt Macy 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1653eda14cbcSMatt Macy 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1654eda14cbcSMatt Macy 		int error = 0;
1655eda14cbcSMatt Macy 		dsl_dataset_t *ds;
16562a58b312SMartin Matuska 		const char *name, *atp = NULL;
1657eda14cbcSMatt Macy 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
1658eda14cbcSMatt Macy 
1659eda14cbcSMatt Macy 		name = nvpair_name(pair);
1660eda14cbcSMatt Macy 		if (strlen(name) >= ZFS_MAX_DATASET_NAME_LEN)
1661eda14cbcSMatt Macy 			error = SET_ERROR(ENAMETOOLONG);
1662eda14cbcSMatt Macy 		if (error == 0) {
1663eda14cbcSMatt Macy 			atp = strchr(name, '@');
1664eda14cbcSMatt Macy 			if (atp == NULL)
1665eda14cbcSMatt Macy 				error = SET_ERROR(EINVAL);
1666eda14cbcSMatt Macy 			if (error == 0)
1667eda14cbcSMatt Macy 				(void) strlcpy(dsname, name, atp - name + 1);
1668eda14cbcSMatt Macy 		}
1669eda14cbcSMatt Macy 		if (error == 0)
1670eda14cbcSMatt Macy 			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
1671eda14cbcSMatt Macy 		if (error == 0) {
1672eda14cbcSMatt Macy 			/* passing 0/NULL skips dsl_fs_ss_limit_check */
1673eda14cbcSMatt Macy 			error = dsl_dataset_snapshot_check_impl(ds,
1674eda14cbcSMatt Macy 			    atp + 1, tx, B_FALSE, 0, NULL, NULL);
1675eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
1676eda14cbcSMatt Macy 		}
1677eda14cbcSMatt Macy 
1678eda14cbcSMatt Macy 		if (error != 0) {
1679eda14cbcSMatt Macy 			if (ddsa->ddsa_errors != NULL) {
1680eda14cbcSMatt Macy 				fnvlist_add_int32(ddsa->ddsa_errors,
1681eda14cbcSMatt Macy 				    name, error);
1682eda14cbcSMatt Macy 			}
1683eda14cbcSMatt Macy 			rv = error;
1684eda14cbcSMatt Macy 		}
1685eda14cbcSMatt Macy 	}
1686eda14cbcSMatt Macy 
1687eda14cbcSMatt Macy 	return (rv);
1688eda14cbcSMatt Macy }
1689eda14cbcSMatt Macy 
1690eda14cbcSMatt Macy void
1691eda14cbcSMatt Macy dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
1692eda14cbcSMatt Macy     dmu_tx_t *tx)
1693eda14cbcSMatt Macy {
1694eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1695eda14cbcSMatt Macy 	dmu_buf_t *dbuf;
1696eda14cbcSMatt Macy 	dsl_dataset_phys_t *dsphys;
1697eda14cbcSMatt Macy 	uint64_t dsobj, crtxg;
1698eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
1699eda14cbcSMatt Macy 	objset_t *os __maybe_unused;
1700eda14cbcSMatt Macy 
1701eda14cbcSMatt Macy 	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
1702eda14cbcSMatt Macy 
1703eda14cbcSMatt Macy 	/*
1704eda14cbcSMatt Macy 	 * If we are on an old pool, the zil must not be active, in which
1705eda14cbcSMatt Macy 	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
1706eda14cbcSMatt Macy 	 */
1707eda14cbcSMatt Macy 	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
1708eda14cbcSMatt Macy 	    dmu_objset_from_ds(ds, &os) != 0 ||
1709da5137abSMartin Matuska 	    memcmp(&os->os_phys->os_zil_header, &zero_zil,
1710eda14cbcSMatt Macy 	    sizeof (zero_zil)) == 0);
1711eda14cbcSMatt Macy 
1712eda14cbcSMatt Macy 	/* Should not snapshot a dirty dataset. */
1713eda14cbcSMatt Macy 	ASSERT(!txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
1714eda14cbcSMatt Macy 	    ds, tx->tx_txg));
1715eda14cbcSMatt Macy 
1716eda14cbcSMatt Macy 	dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx);
1717eda14cbcSMatt Macy 
1718eda14cbcSMatt Macy 	/*
1719eda14cbcSMatt Macy 	 * The origin's ds_creation_txg has to be < TXG_INITIAL
1720eda14cbcSMatt Macy 	 */
1721eda14cbcSMatt Macy 	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
1722eda14cbcSMatt Macy 		crtxg = 1;
1723eda14cbcSMatt Macy 	else
1724eda14cbcSMatt Macy 		crtxg = tx->tx_txg;
1725eda14cbcSMatt Macy 
1726eda14cbcSMatt Macy 	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
1727eda14cbcSMatt Macy 	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
1728eda14cbcSMatt Macy 	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
1729eda14cbcSMatt Macy 	dmu_buf_will_dirty(dbuf, tx);
1730eda14cbcSMatt Macy 	dsphys = dbuf->db_data;
1731da5137abSMartin Matuska 	memset(dsphys, 0, sizeof (dsl_dataset_phys_t));
1732eda14cbcSMatt Macy 	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
1733eda14cbcSMatt Macy 	dsphys->ds_fsid_guid = unique_create();
1734eda14cbcSMatt Macy 	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
1735eda14cbcSMatt Macy 	    sizeof (dsphys->ds_guid));
1736eda14cbcSMatt Macy 	dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1737eda14cbcSMatt Macy 	dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
1738eda14cbcSMatt Macy 	dsphys->ds_next_snap_obj = ds->ds_object;
1739eda14cbcSMatt Macy 	dsphys->ds_num_children = 1;
1740eda14cbcSMatt Macy 	dsphys->ds_creation_time = gethrestime_sec();
1741eda14cbcSMatt Macy 	dsphys->ds_creation_txg = crtxg;
1742eda14cbcSMatt Macy 	dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj;
1743eda14cbcSMatt Macy 	dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes;
1744eda14cbcSMatt Macy 	dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes;
1745eda14cbcSMatt Macy 	dsphys->ds_uncompressed_bytes =
1746eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1747eda14cbcSMatt Macy 	dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags;
1748eda14cbcSMatt Macy 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1749eda14cbcSMatt Macy 	dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp;
1750eda14cbcSMatt Macy 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
1751eda14cbcSMatt Macy 	dmu_buf_rele(dbuf, FTAG);
1752eda14cbcSMatt Macy 
1753eda14cbcSMatt Macy 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
1754eda14cbcSMatt Macy 		if (zfeature_active(f, ds->ds_feature[f])) {
1755eda14cbcSMatt Macy 			dsl_dataset_activate_feature(dsobj, f,
1756eda14cbcSMatt Macy 			    ds->ds_feature[f], tx);
1757eda14cbcSMatt Macy 		}
1758eda14cbcSMatt Macy 	}
1759eda14cbcSMatt Macy 
1760eda14cbcSMatt Macy 	ASSERT3U(ds->ds_prev != 0, ==,
1761eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_obj != 0);
1762eda14cbcSMatt Macy 	if (ds->ds_prev) {
1763eda14cbcSMatt Macy 		uint64_t next_clones_obj =
1764eda14cbcSMatt Macy 		    dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj;
1765eda14cbcSMatt Macy 		ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1766eda14cbcSMatt Macy 		    ds->ds_object ||
1767eda14cbcSMatt Macy 		    dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1);
1768eda14cbcSMatt Macy 		if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj ==
1769eda14cbcSMatt Macy 		    ds->ds_object) {
1770eda14cbcSMatt Macy 			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1771eda14cbcSMatt Macy 			ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==,
1772eda14cbcSMatt Macy 			    dsl_dataset_phys(ds->ds_prev)->ds_creation_txg);
1773eda14cbcSMatt Macy 			dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj;
1774eda14cbcSMatt Macy 		} else if (next_clones_obj != 0) {
1775eda14cbcSMatt Macy 			dsl_dataset_remove_from_next_clones(ds->ds_prev,
1776eda14cbcSMatt Macy 			    dsphys->ds_next_snap_obj, tx);
1777eda14cbcSMatt Macy 			VERIFY0(zap_add_int(mos,
1778eda14cbcSMatt Macy 			    next_clones_obj, dsobj, tx));
1779eda14cbcSMatt Macy 		}
1780eda14cbcSMatt Macy 	}
1781eda14cbcSMatt Macy 
1782eda14cbcSMatt Macy 	/*
1783eda14cbcSMatt Macy 	 * If we have a reference-reservation on this dataset, we will
1784eda14cbcSMatt Macy 	 * need to increase the amount of refreservation being charged
1785eda14cbcSMatt Macy 	 * since our unique space is going to zero.
1786eda14cbcSMatt Macy 	 */
1787eda14cbcSMatt Macy 	if (ds->ds_reserved) {
1788eda14cbcSMatt Macy 		int64_t delta;
1789eda14cbcSMatt Macy 		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
1790eda14cbcSMatt Macy 		delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes,
1791eda14cbcSMatt Macy 		    ds->ds_reserved);
1792eda14cbcSMatt Macy 		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
1793eda14cbcSMatt Macy 		    delta, 0, 0, tx);
1794eda14cbcSMatt Macy 	}
1795eda14cbcSMatt Macy 
1796eda14cbcSMatt Macy 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1797eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_deadlist_obj =
1798eda14cbcSMatt Macy 	    dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX,
1799eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
1800eda14cbcSMatt Macy 	dsl_deadlist_close(&ds->ds_deadlist);
1801eda14cbcSMatt Macy 	dsl_deadlist_open(&ds->ds_deadlist, mos,
1802eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_deadlist_obj);
1803eda14cbcSMatt Macy 	dsl_deadlist_add_key(&ds->ds_deadlist,
1804eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
1805eda14cbcSMatt Macy 	dsl_bookmark_snapshotted(ds, tx);
1806eda14cbcSMatt Macy 
1807eda14cbcSMatt Macy 	if (dsl_dataset_remap_deadlist_exists(ds)) {
1808eda14cbcSMatt Macy 		uint64_t remap_deadlist_obj =
1809eda14cbcSMatt Macy 		    dsl_dataset_get_remap_deadlist_object(ds);
1810eda14cbcSMatt Macy 		/*
1811eda14cbcSMatt Macy 		 * Move the remap_deadlist to the snapshot.  The head
1812eda14cbcSMatt Macy 		 * will create a new remap deadlist on demand, from
1813eda14cbcSMatt Macy 		 * dsl_dataset_block_remapped().
1814eda14cbcSMatt Macy 		 */
1815eda14cbcSMatt Macy 		dsl_dataset_unset_remap_deadlist_object(ds, tx);
1816eda14cbcSMatt Macy 		dsl_deadlist_close(&ds->ds_remap_deadlist);
1817eda14cbcSMatt Macy 
1818eda14cbcSMatt Macy 		dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1819eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_REMAP_DEADLIST,
1820eda14cbcSMatt Macy 		    sizeof (remap_deadlist_obj), 1, &remap_deadlist_obj, tx));
1821eda14cbcSMatt Macy 	}
1822eda14cbcSMatt Macy 
1823eda14cbcSMatt Macy 	/*
1824eda14cbcSMatt Macy 	 * Create a ivset guid for this snapshot if the dataset is
1825eda14cbcSMatt Macy 	 * encrypted. This may be overridden by a raw receive. A
1826eda14cbcSMatt Macy 	 * previous implementation of this code did not have this
1827eda14cbcSMatt Macy 	 * field as part of the on-disk format for ZFS encryption
1828eda14cbcSMatt Macy 	 * (see errata #4). As part of the remediation for this
1829eda14cbcSMatt Macy 	 * issue, we ask the user to enable the bookmark_v2 feature
1830eda14cbcSMatt Macy 	 * which is now a dependency of the encryption feature. We
1831eda14cbcSMatt Macy 	 * use this as a heuristic to determine when the user has
1832eda14cbcSMatt Macy 	 * elected to correct any datasets created with the old code.
1833eda14cbcSMatt Macy 	 * As a result, we only do this step if the bookmark_v2
1834eda14cbcSMatt Macy 	 * feature is enabled, which limits the number of states a
1835eda14cbcSMatt Macy 	 * given pool / dataset can be in with regards to terms of
1836eda14cbcSMatt Macy 	 * correcting the issue.
1837eda14cbcSMatt Macy 	 */
1838eda14cbcSMatt Macy 	if (ds->ds_dir->dd_crypto_obj != 0 &&
1839eda14cbcSMatt Macy 	    spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_BOOKMARK_V2)) {
1840eda14cbcSMatt Macy 		uint64_t ivset_guid = unique_create();
1841eda14cbcSMatt Macy 
1842eda14cbcSMatt Macy 		dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx);
1843eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_IVSET_GUID,
1844eda14cbcSMatt Macy 		    sizeof (ivset_guid), 1, &ivset_guid, tx));
1845eda14cbcSMatt Macy 	}
1846eda14cbcSMatt Macy 
1847eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg);
1848eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj;
1849eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg;
1850eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_unique_bytes = 0;
1851eda14cbcSMatt Macy 
1852eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
1853eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
1854eda14cbcSMatt Macy 
1855eda14cbcSMatt Macy 	VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1856eda14cbcSMatt Macy 	    snapname, 8, 1, &dsobj, tx));
1857eda14cbcSMatt Macy 
1858eda14cbcSMatt Macy 	if (ds->ds_prev)
1859eda14cbcSMatt Macy 		dsl_dataset_rele(ds->ds_prev, ds);
1860eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp,
1861eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev));
1862eda14cbcSMatt Macy 
1863eda14cbcSMatt Macy 	dsl_scan_ds_snapshotted(ds, tx);
1864eda14cbcSMatt Macy 
1865271171e0SMartin Matuska 	dsl_dir_snap_cmtime_update(ds->ds_dir, tx);
1866eda14cbcSMatt Macy 
186708aba0aeSMartin Matuska 	if (zfs_snapshot_history_enabled)
1868eda14cbcSMatt Macy 		spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, " ");
1869eda14cbcSMatt Macy }
1870eda14cbcSMatt Macy 
1871eda14cbcSMatt Macy void
1872eda14cbcSMatt Macy dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
1873eda14cbcSMatt Macy {
1874eda14cbcSMatt Macy 	dsl_dataset_snapshot_arg_t *ddsa = arg;
1875eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1876eda14cbcSMatt Macy 	nvpair_t *pair;
1877eda14cbcSMatt Macy 
1878eda14cbcSMatt Macy 	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
1879eda14cbcSMatt Macy 	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
1880eda14cbcSMatt Macy 		dsl_dataset_t *ds;
18812a58b312SMartin Matuska 		const char *name, *atp;
1882eda14cbcSMatt Macy 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
1883eda14cbcSMatt Macy 
1884eda14cbcSMatt Macy 		name = nvpair_name(pair);
1885eda14cbcSMatt Macy 		atp = strchr(name, '@');
1886eda14cbcSMatt Macy 		(void) strlcpy(dsname, name, atp - name + 1);
1887eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
1888eda14cbcSMatt Macy 
1889eda14cbcSMatt Macy 		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
1890eda14cbcSMatt Macy 		if (ddsa->ddsa_props != NULL) {
1891eda14cbcSMatt Macy 			dsl_props_set_sync_impl(ds->ds_prev,
1892eda14cbcSMatt Macy 			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
1893eda14cbcSMatt Macy 		}
1894eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1895eda14cbcSMatt Macy 	}
1896eda14cbcSMatt Macy }
1897eda14cbcSMatt Macy 
1898eda14cbcSMatt Macy /*
1899eda14cbcSMatt Macy  * The snapshots must all be in the same pool.
1900eda14cbcSMatt Macy  * All-or-nothing: if there are any failures, nothing will be modified.
1901eda14cbcSMatt Macy  */
1902eda14cbcSMatt Macy int
1903eda14cbcSMatt Macy dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
1904eda14cbcSMatt Macy {
1905eda14cbcSMatt Macy 	dsl_dataset_snapshot_arg_t ddsa;
1906eda14cbcSMatt Macy 	nvpair_t *pair;
1907eda14cbcSMatt Macy 	boolean_t needsuspend;
1908eda14cbcSMatt Macy 	int error;
1909eda14cbcSMatt Macy 	spa_t *spa;
19102a58b312SMartin Matuska 	const char *firstname;
1911eda14cbcSMatt Macy 	nvlist_t *suspended = NULL;
1912eda14cbcSMatt Macy 
1913eda14cbcSMatt Macy 	pair = nvlist_next_nvpair(snaps, NULL);
1914eda14cbcSMatt Macy 	if (pair == NULL)
1915eda14cbcSMatt Macy 		return (0);
1916eda14cbcSMatt Macy 	firstname = nvpair_name(pair);
1917eda14cbcSMatt Macy 
1918eda14cbcSMatt Macy 	error = spa_open(firstname, &spa, FTAG);
1919eda14cbcSMatt Macy 	if (error != 0)
1920eda14cbcSMatt Macy 		return (error);
1921eda14cbcSMatt Macy 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1922eda14cbcSMatt Macy 	spa_close(spa, FTAG);
1923eda14cbcSMatt Macy 
1924eda14cbcSMatt Macy 	if (needsuspend) {
1925eda14cbcSMatt Macy 		suspended = fnvlist_alloc();
1926eda14cbcSMatt Macy 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1927eda14cbcSMatt Macy 		    pair = nvlist_next_nvpair(snaps, pair)) {
1928eda14cbcSMatt Macy 			char fsname[ZFS_MAX_DATASET_NAME_LEN];
19292a58b312SMartin Matuska 			const char *snapname = nvpair_name(pair);
19302a58b312SMartin Matuska 			const char *atp;
1931eda14cbcSMatt Macy 			void *cookie;
1932eda14cbcSMatt Macy 
1933eda14cbcSMatt Macy 			atp = strchr(snapname, '@');
1934eda14cbcSMatt Macy 			if (atp == NULL) {
1935eda14cbcSMatt Macy 				error = SET_ERROR(EINVAL);
1936eda14cbcSMatt Macy 				break;
1937eda14cbcSMatt Macy 			}
1938eda14cbcSMatt Macy 			(void) strlcpy(fsname, snapname, atp - snapname + 1);
1939eda14cbcSMatt Macy 
1940eda14cbcSMatt Macy 			error = zil_suspend(fsname, &cookie);
1941eda14cbcSMatt Macy 			if (error != 0)
1942eda14cbcSMatt Macy 				break;
1943eda14cbcSMatt Macy 			fnvlist_add_uint64(suspended, fsname,
1944eda14cbcSMatt Macy 			    (uintptr_t)cookie);
1945eda14cbcSMatt Macy 		}
1946eda14cbcSMatt Macy 	}
1947eda14cbcSMatt Macy 
1948eda14cbcSMatt Macy 	ddsa.ddsa_snaps = snaps;
1949eda14cbcSMatt Macy 	ddsa.ddsa_props = props;
1950eda14cbcSMatt Macy 	ddsa.ddsa_errors = errors;
1951eda14cbcSMatt Macy 	ddsa.ddsa_cr = CRED();
1952eda14cbcSMatt Macy 	ddsa.ddsa_proc = curproc;
1953eda14cbcSMatt Macy 
1954eda14cbcSMatt Macy 	if (error == 0) {
1955eda14cbcSMatt Macy 		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
1956eda14cbcSMatt Macy 		    dsl_dataset_snapshot_sync, &ddsa,
1957eda14cbcSMatt Macy 		    fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL);
1958eda14cbcSMatt Macy 	}
1959eda14cbcSMatt Macy 
1960eda14cbcSMatt Macy 	if (suspended != NULL) {
1961eda14cbcSMatt Macy 		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
1962eda14cbcSMatt Macy 		    pair = nvlist_next_nvpair(suspended, pair)) {
1963eda14cbcSMatt Macy 			zil_resume((void *)(uintptr_t)
1964eda14cbcSMatt Macy 			    fnvpair_value_uint64(pair));
1965eda14cbcSMatt Macy 		}
1966eda14cbcSMatt Macy 		fnvlist_free(suspended);
1967eda14cbcSMatt Macy 	}
1968eda14cbcSMatt Macy 
1969eda14cbcSMatt Macy 	if (error == 0) {
1970eda14cbcSMatt Macy 		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1971eda14cbcSMatt Macy 		    pair = nvlist_next_nvpair(snaps, pair)) {
1972eda14cbcSMatt Macy 			zvol_create_minor(nvpair_name(pair));
1973eda14cbcSMatt Macy 		}
1974eda14cbcSMatt Macy 	}
1975eda14cbcSMatt Macy 
1976eda14cbcSMatt Macy 	return (error);
1977eda14cbcSMatt Macy }
1978eda14cbcSMatt Macy 
1979eda14cbcSMatt Macy typedef struct dsl_dataset_snapshot_tmp_arg {
1980eda14cbcSMatt Macy 	const char *ddsta_fsname;
1981eda14cbcSMatt Macy 	const char *ddsta_snapname;
1982eda14cbcSMatt Macy 	minor_t ddsta_cleanup_minor;
1983eda14cbcSMatt Macy 	const char *ddsta_htag;
1984eda14cbcSMatt Macy } dsl_dataset_snapshot_tmp_arg_t;
1985eda14cbcSMatt Macy 
1986eda14cbcSMatt Macy static int
1987eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
1988eda14cbcSMatt Macy {
1989eda14cbcSMatt Macy 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
1990eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1991eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1992eda14cbcSMatt Macy 	int error;
1993eda14cbcSMatt Macy 
1994eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
1995eda14cbcSMatt Macy 	if (error != 0)
1996eda14cbcSMatt Macy 		return (error);
1997eda14cbcSMatt Macy 
1998eda14cbcSMatt Macy 	/* NULL cred means no limit check for tmp snapshot */
1999eda14cbcSMatt Macy 	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
2000eda14cbcSMatt Macy 	    tx, B_FALSE, 0, NULL, NULL);
2001eda14cbcSMatt Macy 	if (error != 0) {
2002eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
2003eda14cbcSMatt Macy 		return (error);
2004eda14cbcSMatt Macy 	}
2005eda14cbcSMatt Macy 
2006eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
2007eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
2008eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
2009eda14cbcSMatt Macy 	}
2010eda14cbcSMatt Macy 	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
2011eda14cbcSMatt Macy 	    B_TRUE, tx);
2012eda14cbcSMatt Macy 	if (error != 0) {
2013eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
2014eda14cbcSMatt Macy 		return (error);
2015eda14cbcSMatt Macy 	}
2016eda14cbcSMatt Macy 
2017eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
2018eda14cbcSMatt Macy 	return (0);
2019eda14cbcSMatt Macy }
2020eda14cbcSMatt Macy 
2021eda14cbcSMatt Macy static void
2022eda14cbcSMatt Macy dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
2023eda14cbcSMatt Macy {
2024eda14cbcSMatt Macy 	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
2025eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
2026eda14cbcSMatt Macy 	dsl_dataset_t *ds = NULL;
2027eda14cbcSMatt Macy 
2028eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
2029eda14cbcSMatt Macy 
2030eda14cbcSMatt Macy 	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
2031eda14cbcSMatt Macy 	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
2032eda14cbcSMatt Macy 	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
2033eda14cbcSMatt Macy 	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
2034eda14cbcSMatt Macy 
2035eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
2036eda14cbcSMatt Macy }
2037eda14cbcSMatt Macy 
2038eda14cbcSMatt Macy int
2039eda14cbcSMatt Macy dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
2040eda14cbcSMatt Macy     minor_t cleanup_minor, const char *htag)
2041eda14cbcSMatt Macy {
2042eda14cbcSMatt Macy 	dsl_dataset_snapshot_tmp_arg_t ddsta;
2043eda14cbcSMatt Macy 	int error;
2044eda14cbcSMatt Macy 	spa_t *spa;
2045eda14cbcSMatt Macy 	boolean_t needsuspend;
2046eda14cbcSMatt Macy 	void *cookie;
2047eda14cbcSMatt Macy 
2048eda14cbcSMatt Macy 	ddsta.ddsta_fsname = fsname;
2049eda14cbcSMatt Macy 	ddsta.ddsta_snapname = snapname;
2050eda14cbcSMatt Macy 	ddsta.ddsta_cleanup_minor = cleanup_minor;
2051eda14cbcSMatt Macy 	ddsta.ddsta_htag = htag;
2052eda14cbcSMatt Macy 
2053eda14cbcSMatt Macy 	error = spa_open(fsname, &spa, FTAG);
2054eda14cbcSMatt Macy 	if (error != 0)
2055eda14cbcSMatt Macy 		return (error);
2056eda14cbcSMatt Macy 	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
2057eda14cbcSMatt Macy 	spa_close(spa, FTAG);
2058eda14cbcSMatt Macy 
2059eda14cbcSMatt Macy 	if (needsuspend) {
2060eda14cbcSMatt Macy 		error = zil_suspend(fsname, &cookie);
2061eda14cbcSMatt Macy 		if (error != 0)
2062eda14cbcSMatt Macy 			return (error);
2063eda14cbcSMatt Macy 	}
2064eda14cbcSMatt Macy 
2065eda14cbcSMatt Macy 	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
2066eda14cbcSMatt Macy 	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED);
2067eda14cbcSMatt Macy 
2068eda14cbcSMatt Macy 	if (needsuspend)
2069eda14cbcSMatt Macy 		zil_resume(cookie);
2070eda14cbcSMatt Macy 	return (error);
2071eda14cbcSMatt Macy }
2072eda14cbcSMatt Macy 
207314c2e0a0SMartin Matuska /* Nonblocking dataset sync. Assumes dataset:objset is always 1:1 */
2074eda14cbcSMatt Macy void
207514c2e0a0SMartin Matuska dsl_dataset_sync(dsl_dataset_t *ds, zio_t *rio, dmu_tx_t *tx)
2076eda14cbcSMatt Macy {
2077eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
2078eda14cbcSMatt Macy 	ASSERT(ds->ds_objset != NULL);
2079eda14cbcSMatt Macy 	ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0);
2080eda14cbcSMatt Macy 
2081eda14cbcSMatt Macy 	/*
2082eda14cbcSMatt Macy 	 * in case we had to change ds_fsid_guid when we opened it,
2083eda14cbcSMatt Macy 	 * sync it out now.
2084eda14cbcSMatt Macy 	 */
2085eda14cbcSMatt Macy 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
2086eda14cbcSMatt Macy 	dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid;
2087eda14cbcSMatt Macy 
2088eda14cbcSMatt Macy 	if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) {
2089eda14cbcSMatt Macy 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2090eda14cbcSMatt Macy 		    ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1,
2091eda14cbcSMatt Macy 		    &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx));
2092eda14cbcSMatt Macy 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2093eda14cbcSMatt Macy 		    ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1,
2094eda14cbcSMatt Macy 		    &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx));
2095eda14cbcSMatt Macy 		VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2096eda14cbcSMatt Macy 		    ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1,
2097eda14cbcSMatt Macy 		    &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx));
2098eda14cbcSMatt Macy 		ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0;
2099eda14cbcSMatt Macy 		ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0;
2100eda14cbcSMatt Macy 		ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0;
2101eda14cbcSMatt Macy 	}
2102eda14cbcSMatt Macy 
210314c2e0a0SMartin Matuska 	dmu_objset_sync(ds->ds_objset, rio, tx);
2104eda14cbcSMatt Macy }
2105eda14cbcSMatt Macy 
2106eda14cbcSMatt Macy /*
2107eda14cbcSMatt Macy  * Check if the percentage of blocks shared between the clone and the
2108eda14cbcSMatt Macy  * snapshot (as opposed to those that are clone only) is below a certain
2109eda14cbcSMatt Macy  * threshold
2110eda14cbcSMatt Macy  */
2111eda14cbcSMatt Macy static boolean_t
2112eda14cbcSMatt Macy dsl_livelist_should_disable(dsl_dataset_t *ds)
2113eda14cbcSMatt Macy {
2114eda14cbcSMatt Macy 	uint64_t used, referenced;
2115eda14cbcSMatt Macy 	int percent_shared;
2116eda14cbcSMatt Macy 
2117eda14cbcSMatt Macy 	used = dsl_dir_get_usedds(ds->ds_dir);
2118eda14cbcSMatt Macy 	referenced = dsl_get_referenced(ds);
2119eda14cbcSMatt Macy 	if (referenced == 0)
2120eda14cbcSMatt Macy 		return (B_FALSE);
2121eda14cbcSMatt Macy 	percent_shared = (100 * (referenced - used)) / referenced;
2122eda14cbcSMatt Macy 	if (percent_shared <= zfs_livelist_min_percent_shared)
2123eda14cbcSMatt Macy 		return (B_TRUE);
2124eda14cbcSMatt Macy 	return (B_FALSE);
2125eda14cbcSMatt Macy }
2126eda14cbcSMatt Macy 
2127eda14cbcSMatt Macy /*
2128eda14cbcSMatt Macy  *  Check if it is possible to combine two livelist entries into one.
2129eda14cbcSMatt Macy  *  This is the case if the combined number of 'live' blkptrs (ALLOCs that
2130eda14cbcSMatt Macy  *  don't have a matching FREE) is under the maximum sublist size.
2131eda14cbcSMatt Macy  *  We check this by subtracting twice the total number of frees from the total
2132eda14cbcSMatt Macy  *  number of blkptrs. FREEs are counted twice because each FREE blkptr
2133eda14cbcSMatt Macy  *  will cancel out an ALLOC blkptr when the livelist is processed.
2134eda14cbcSMatt Macy  */
2135eda14cbcSMatt Macy static boolean_t
2136eda14cbcSMatt Macy dsl_livelist_should_condense(dsl_deadlist_entry_t *first,
2137eda14cbcSMatt Macy     dsl_deadlist_entry_t *next)
2138eda14cbcSMatt Macy {
2139eda14cbcSMatt Macy 	uint64_t total_free = first->dle_bpobj.bpo_phys->bpo_num_freed +
2140eda14cbcSMatt Macy 	    next->dle_bpobj.bpo_phys->bpo_num_freed;
2141eda14cbcSMatt Macy 	uint64_t total_entries = first->dle_bpobj.bpo_phys->bpo_num_blkptrs +
2142eda14cbcSMatt Macy 	    next->dle_bpobj.bpo_phys->bpo_num_blkptrs;
2143eda14cbcSMatt Macy 	if ((total_entries - (2 * total_free)) < zfs_livelist_max_entries)
2144eda14cbcSMatt Macy 		return (B_TRUE);
2145eda14cbcSMatt Macy 	return (B_FALSE);
2146eda14cbcSMatt Macy }
2147eda14cbcSMatt Macy 
2148eda14cbcSMatt Macy typedef struct try_condense_arg {
2149eda14cbcSMatt Macy 	spa_t *spa;
2150eda14cbcSMatt Macy 	dsl_dataset_t *ds;
2151eda14cbcSMatt Macy } try_condense_arg_t;
2152eda14cbcSMatt Macy 
2153eda14cbcSMatt Macy /*
2154eda14cbcSMatt Macy  * Iterate over the livelist entries, searching for a pair to condense.
2155eda14cbcSMatt Macy  * A nonzero return value means stop, 0 means keep looking.
2156eda14cbcSMatt Macy  */
2157eda14cbcSMatt Macy static int
2158eda14cbcSMatt Macy dsl_livelist_try_condense(void *arg, dsl_deadlist_entry_t *first)
2159eda14cbcSMatt Macy {
2160eda14cbcSMatt Macy 	try_condense_arg_t *tca = arg;
2161eda14cbcSMatt Macy 	spa_t *spa = tca->spa;
2162eda14cbcSMatt Macy 	dsl_dataset_t *ds = tca->ds;
2163eda14cbcSMatt Macy 	dsl_deadlist_t *ll = &ds->ds_dir->dd_livelist;
2164eda14cbcSMatt Macy 	dsl_deadlist_entry_t *next;
2165eda14cbcSMatt Macy 
2166eda14cbcSMatt Macy 	/* The condense thread has not yet been created at import */
2167eda14cbcSMatt Macy 	if (spa->spa_livelist_condense_zthr == NULL)
2168eda14cbcSMatt Macy 		return (1);
2169eda14cbcSMatt Macy 
2170eda14cbcSMatt Macy 	/* A condense is already in progress */
2171eda14cbcSMatt Macy 	if (spa->spa_to_condense.ds != NULL)
2172eda14cbcSMatt Macy 		return (1);
2173eda14cbcSMatt Macy 
2174eda14cbcSMatt Macy 	next = AVL_NEXT(&ll->dl_tree, &first->dle_node);
2175eda14cbcSMatt Macy 	/* The livelist has only one entry - don't condense it */
2176eda14cbcSMatt Macy 	if (next == NULL)
2177eda14cbcSMatt Macy 		return (1);
2178eda14cbcSMatt Macy 
2179eda14cbcSMatt Macy 	/* Next is the newest entry - don't condense it */
2180eda14cbcSMatt Macy 	if (AVL_NEXT(&ll->dl_tree, &next->dle_node) == NULL)
2181eda14cbcSMatt Macy 		return (1);
2182eda14cbcSMatt Macy 
2183eda14cbcSMatt Macy 	/* This pair is not ready to condense but keep looking */
2184eda14cbcSMatt Macy 	if (!dsl_livelist_should_condense(first, next))
2185eda14cbcSMatt Macy 		return (0);
2186eda14cbcSMatt Macy 
2187eda14cbcSMatt Macy 	/*
2188eda14cbcSMatt Macy 	 * Add a ref to prevent the dataset from being evicted while
2189eda14cbcSMatt Macy 	 * the condense zthr or synctask are running. Ref will be
2190eda14cbcSMatt Macy 	 * released at the end of the condense synctask
2191eda14cbcSMatt Macy 	 */
2192eda14cbcSMatt Macy 	dmu_buf_add_ref(ds->ds_dbuf, spa);
2193eda14cbcSMatt Macy 
2194eda14cbcSMatt Macy 	spa->spa_to_condense.ds = ds;
2195eda14cbcSMatt Macy 	spa->spa_to_condense.first = first;
2196eda14cbcSMatt Macy 	spa->spa_to_condense.next = next;
2197eda14cbcSMatt Macy 	spa->spa_to_condense.syncing = B_FALSE;
2198eda14cbcSMatt Macy 	spa->spa_to_condense.cancelled = B_FALSE;
2199eda14cbcSMatt Macy 
2200eda14cbcSMatt Macy 	zthr_wakeup(spa->spa_livelist_condense_zthr);
2201eda14cbcSMatt Macy 	return (1);
2202eda14cbcSMatt Macy }
2203eda14cbcSMatt Macy 
2204eda14cbcSMatt Macy static void
2205eda14cbcSMatt Macy dsl_flush_pending_livelist(dsl_dataset_t *ds, dmu_tx_t *tx)
2206eda14cbcSMatt Macy {
2207eda14cbcSMatt Macy 	dsl_dir_t *dd = ds->ds_dir;
2208eda14cbcSMatt Macy 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
2209eda14cbcSMatt Macy 	dsl_deadlist_entry_t *last = dsl_deadlist_last(&dd->dd_livelist);
2210eda14cbcSMatt Macy 
2211eda14cbcSMatt Macy 	/* Check if we need to add a new sub-livelist */
2212eda14cbcSMatt Macy 	if (last == NULL) {
2213eda14cbcSMatt Macy 		/* The livelist is empty */
2214eda14cbcSMatt Macy 		dsl_deadlist_add_key(&dd->dd_livelist,
2215eda14cbcSMatt Macy 		    tx->tx_txg - 1, tx);
2216eda14cbcSMatt Macy 	} else if (spa_sync_pass(spa) == 1) {
2217eda14cbcSMatt Macy 		/*
2218eda14cbcSMatt Macy 		 * Check if the newest entry is full. If it is, make a new one.
2219eda14cbcSMatt Macy 		 * We only do this once per sync because we could overfill a
2220eda14cbcSMatt Macy 		 * sublist in one sync pass and don't want to add another entry
2221eda14cbcSMatt Macy 		 * for a txg that is already represented. This ensures that
2222eda14cbcSMatt Macy 		 * blkptrs born in the same txg are stored in the same sublist.
2223eda14cbcSMatt Macy 		 */
2224eda14cbcSMatt Macy 		bpobj_t bpobj = last->dle_bpobj;
2225eda14cbcSMatt Macy 		uint64_t all = bpobj.bpo_phys->bpo_num_blkptrs;
2226eda14cbcSMatt Macy 		uint64_t free = bpobj.bpo_phys->bpo_num_freed;
2227eda14cbcSMatt Macy 		uint64_t alloc = all - free;
2228eda14cbcSMatt Macy 		if (alloc > zfs_livelist_max_entries) {
2229eda14cbcSMatt Macy 			dsl_deadlist_add_key(&dd->dd_livelist,
2230eda14cbcSMatt Macy 			    tx->tx_txg - 1, tx);
2231eda14cbcSMatt Macy 		}
2232eda14cbcSMatt Macy 	}
2233eda14cbcSMatt Macy 
2234eda14cbcSMatt Macy 	/* Insert each entry into the on-disk livelist */
2235eda14cbcSMatt Macy 	bplist_iterate(&dd->dd_pending_allocs,
2236eda14cbcSMatt Macy 	    dsl_deadlist_insert_alloc_cb, &dd->dd_livelist, tx);
2237eda14cbcSMatt Macy 	bplist_iterate(&dd->dd_pending_frees,
2238eda14cbcSMatt Macy 	    dsl_deadlist_insert_free_cb, &dd->dd_livelist, tx);
2239eda14cbcSMatt Macy 
2240eda14cbcSMatt Macy 	/* Attempt to condense every pair of adjacent entries */
2241eda14cbcSMatt Macy 	try_condense_arg_t arg = {
2242eda14cbcSMatt Macy 	    .spa = spa,
2243eda14cbcSMatt Macy 	    .ds = ds
2244eda14cbcSMatt Macy 	};
2245eda14cbcSMatt Macy 	dsl_deadlist_iterate(&dd->dd_livelist, dsl_livelist_try_condense,
2246eda14cbcSMatt Macy 	    &arg);
2247eda14cbcSMatt Macy }
2248eda14cbcSMatt Macy 
2249eda14cbcSMatt Macy void
2250eda14cbcSMatt Macy dsl_dataset_sync_done(dsl_dataset_t *ds, dmu_tx_t *tx)
2251eda14cbcSMatt Macy {
2252eda14cbcSMatt Macy 	objset_t *os = ds->ds_objset;
2253eda14cbcSMatt Macy 
2254eda14cbcSMatt Macy 	bplist_iterate(&ds->ds_pending_deadlist,
2255eda14cbcSMatt Macy 	    dsl_deadlist_insert_alloc_cb, &ds->ds_deadlist, tx);
2256eda14cbcSMatt Macy 
2257eda14cbcSMatt Macy 	if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist)) {
2258eda14cbcSMatt Macy 		dsl_flush_pending_livelist(ds, tx);
2259eda14cbcSMatt Macy 		if (dsl_livelist_should_disable(ds)) {
2260eda14cbcSMatt Macy 			dsl_dir_remove_livelist(ds->ds_dir, tx, B_TRUE);
2261eda14cbcSMatt Macy 		}
2262eda14cbcSMatt Macy 	}
2263eda14cbcSMatt Macy 
2264eda14cbcSMatt Macy 	dsl_bookmark_sync_done(ds, tx);
2265eda14cbcSMatt Macy 
22663ff01b23SMartin Matuska 	multilist_destroy(&os->os_synced_dnodes);
2267eda14cbcSMatt Macy 
2268eda14cbcSMatt Macy 	if (os->os_encrypted)
2269eda14cbcSMatt Macy 		os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_FALSE;
2270eda14cbcSMatt Macy 	else
2271eda14cbcSMatt Macy 		ASSERT0(os->os_next_write_raw[tx->tx_txg & TXG_MASK]);
2272eda14cbcSMatt Macy 
2273c9539b89SMartin Matuska 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2274c9539b89SMartin Matuska 		if (zfeature_active(f,
2275c9539b89SMartin Matuska 		    ds->ds_feature_activation[f])) {
2276c9539b89SMartin Matuska 			if (zfeature_active(f, ds->ds_feature[f]))
2277c9539b89SMartin Matuska 				continue;
2278c9539b89SMartin Matuska 			dsl_dataset_activate_feature(ds->ds_object, f,
2279c9539b89SMartin Matuska 			    ds->ds_feature_activation[f], tx);
2280c9539b89SMartin Matuska 			ds->ds_feature[f] = ds->ds_feature_activation[f];
2281c9539b89SMartin Matuska 		}
2282c9539b89SMartin Matuska 	}
22832a58b312SMartin Matuska 
22842a58b312SMartin Matuska 	ASSERT(!dmu_objset_is_dirty(os, dmu_tx_get_txg(tx)));
2285eda14cbcSMatt Macy }
2286eda14cbcSMatt Macy 
2287eda14cbcSMatt Macy int
2288eda14cbcSMatt Macy get_clones_stat_impl(dsl_dataset_t *ds, nvlist_t *val)
2289eda14cbcSMatt Macy {
2290eda14cbcSMatt Macy 	uint64_t count = 0;
2291eda14cbcSMatt Macy 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
2292eda14cbcSMatt Macy 	zap_cursor_t zc;
2293eda14cbcSMatt Macy 	zap_attribute_t za;
2294eda14cbcSMatt Macy 
2295eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
2296eda14cbcSMatt Macy 
2297eda14cbcSMatt Macy 	/*
2298eda14cbcSMatt Macy 	 * There may be missing entries in ds_next_clones_obj
2299eda14cbcSMatt Macy 	 * due to a bug in a previous version of the code.
2300eda14cbcSMatt Macy 	 * Only trust it if it has the right number of entries.
2301eda14cbcSMatt Macy 	 */
2302eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) {
2303eda14cbcSMatt Macy 		VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj,
2304eda14cbcSMatt Macy 		    &count));
2305eda14cbcSMatt Macy 	}
2306eda14cbcSMatt Macy 	if (count != dsl_dataset_phys(ds)->ds_num_children - 1) {
2307eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
2308eda14cbcSMatt Macy 	}
2309eda14cbcSMatt Macy 	for (zap_cursor_init(&zc, mos,
2310eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_next_clones_obj);
2311eda14cbcSMatt Macy 	    zap_cursor_retrieve(&zc, &za) == 0;
2312eda14cbcSMatt Macy 	    zap_cursor_advance(&zc)) {
2313eda14cbcSMatt Macy 		dsl_dataset_t *clone;
2314eda14cbcSMatt Macy 		char buf[ZFS_MAX_DATASET_NAME_LEN];
2315eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
2316eda14cbcSMatt Macy 		    za.za_first_integer, FTAG, &clone));
2317eda14cbcSMatt Macy 		dsl_dir_name(clone->ds_dir, buf);
2318eda14cbcSMatt Macy 		fnvlist_add_boolean(val, buf);
2319eda14cbcSMatt Macy 		dsl_dataset_rele(clone, FTAG);
2320eda14cbcSMatt Macy 	}
2321eda14cbcSMatt Macy 	zap_cursor_fini(&zc);
2322eda14cbcSMatt Macy 	return (0);
2323eda14cbcSMatt Macy }
2324eda14cbcSMatt Macy 
2325eda14cbcSMatt Macy void
2326eda14cbcSMatt Macy get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
2327eda14cbcSMatt Macy {
2328eda14cbcSMatt Macy 	nvlist_t *propval = fnvlist_alloc();
2329184c1b94SMartin Matuska 	nvlist_t *val = fnvlist_alloc();
2330eda14cbcSMatt Macy 
2331eda14cbcSMatt Macy 	if (get_clones_stat_impl(ds, val) == 0) {
2332eda14cbcSMatt Macy 		fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
2333eda14cbcSMatt Macy 		fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES),
2334eda14cbcSMatt Macy 		    propval);
2335eda14cbcSMatt Macy 	}
2336eda14cbcSMatt Macy 
2337eda14cbcSMatt Macy 	nvlist_free(val);
2338eda14cbcSMatt Macy 	nvlist_free(propval);
2339eda14cbcSMatt Macy }
2340eda14cbcSMatt Macy 
2341c03c5b1cSMartin Matuska static char *
2342c03c5b1cSMartin Matuska get_receive_resume_token_impl(dsl_dataset_t *ds)
2343eda14cbcSMatt Macy {
2344c03c5b1cSMartin Matuska 	if (!dsl_dataset_has_resume_receive_state(ds))
2345c03c5b1cSMartin Matuska 		return (NULL);
2346eda14cbcSMatt Macy 
2347c03c5b1cSMartin Matuska 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2348eda14cbcSMatt Macy 	char *str;
2349eda14cbcSMatt Macy 	void *packed;
2350eda14cbcSMatt Macy 	uint8_t *compressed;
2351eda14cbcSMatt Macy 	uint64_t val;
2352eda14cbcSMatt Macy 	nvlist_t *token_nv = fnvlist_alloc();
2353eda14cbcSMatt Macy 	size_t packed_size, compressed_size;
2354eda14cbcSMatt Macy 
2355eda14cbcSMatt Macy 	if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2356eda14cbcSMatt Macy 	    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) {
2357eda14cbcSMatt Macy 		fnvlist_add_uint64(token_nv, "fromguid", val);
2358eda14cbcSMatt Macy 	}
2359eda14cbcSMatt Macy 	if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2360eda14cbcSMatt Macy 	    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) {
2361eda14cbcSMatt Macy 		fnvlist_add_uint64(token_nv, "object", val);
2362eda14cbcSMatt Macy 	}
2363eda14cbcSMatt Macy 	if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2364eda14cbcSMatt Macy 	    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) {
2365eda14cbcSMatt Macy 		fnvlist_add_uint64(token_nv, "offset", val);
2366eda14cbcSMatt Macy 	}
2367eda14cbcSMatt Macy 	if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2368eda14cbcSMatt Macy 	    DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) {
2369eda14cbcSMatt Macy 		fnvlist_add_uint64(token_nv, "bytes", val);
2370eda14cbcSMatt Macy 	}
2371eda14cbcSMatt Macy 	if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2372eda14cbcSMatt Macy 	    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) {
2373eda14cbcSMatt Macy 		fnvlist_add_uint64(token_nv, "toguid", val);
2374eda14cbcSMatt Macy 	}
2375eda14cbcSMatt Macy 	char buf[MAXNAMELEN];
2376eda14cbcSMatt Macy 	if (zap_lookup(dp->dp_meta_objset, ds->ds_object,
2377eda14cbcSMatt Macy 	    DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) {
2378eda14cbcSMatt Macy 		fnvlist_add_string(token_nv, "toname", buf);
2379eda14cbcSMatt Macy 	}
2380eda14cbcSMatt Macy 	if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2381eda14cbcSMatt Macy 	    DS_FIELD_RESUME_LARGEBLOCK) == 0) {
2382eda14cbcSMatt Macy 		fnvlist_add_boolean(token_nv, "largeblockok");
2383eda14cbcSMatt Macy 	}
2384eda14cbcSMatt Macy 	if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2385eda14cbcSMatt Macy 	    DS_FIELD_RESUME_EMBEDOK) == 0) {
2386eda14cbcSMatt Macy 		fnvlist_add_boolean(token_nv, "embedok");
2387eda14cbcSMatt Macy 	}
2388eda14cbcSMatt Macy 	if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2389eda14cbcSMatt Macy 	    DS_FIELD_RESUME_COMPRESSOK) == 0) {
2390eda14cbcSMatt Macy 		fnvlist_add_boolean(token_nv, "compressok");
2391eda14cbcSMatt Macy 	}
2392eda14cbcSMatt Macy 	if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2393eda14cbcSMatt Macy 	    DS_FIELD_RESUME_RAWOK) == 0) {
2394eda14cbcSMatt Macy 		fnvlist_add_boolean(token_nv, "rawok");
2395eda14cbcSMatt Macy 	}
2396eda14cbcSMatt Macy 	if (dsl_dataset_feature_is_active(ds,
2397eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS)) {
2398c03c5b1cSMartin Matuska 		uint64_t num_redact_snaps = 0;
2399c03c5b1cSMartin Matuska 		uint64_t *redact_snaps = NULL;
2400c03c5b1cSMartin Matuska 		VERIFY3B(dsl_dataset_get_uint64_array_feature(ds,
2401eda14cbcSMatt Macy 		    SPA_FEATURE_REDACTED_DATASETS, &num_redact_snaps,
2402c03c5b1cSMartin Matuska 		    &redact_snaps), ==, B_TRUE);
2403eda14cbcSMatt Macy 		fnvlist_add_uint64_array(token_nv, "redact_snaps",
2404eda14cbcSMatt Macy 		    redact_snaps, num_redact_snaps);
2405eda14cbcSMatt Macy 	}
2406eda14cbcSMatt Macy 	if (zap_contains(dp->dp_meta_objset, ds->ds_object,
2407eda14cbcSMatt Macy 	    DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS) == 0) {
2408c03c5b1cSMartin Matuska 		uint64_t num_redact_snaps = 0, int_size = 0;
2409c03c5b1cSMartin Matuska 		uint64_t *redact_snaps = NULL;
2410eda14cbcSMatt Macy 		VERIFY0(zap_length(dp->dp_meta_objset, ds->ds_object,
2411eda14cbcSMatt Macy 		    DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, &int_size,
2412eda14cbcSMatt Macy 		    &num_redact_snaps));
2413eda14cbcSMatt Macy 		ASSERT3U(int_size, ==, sizeof (uint64_t));
2414eda14cbcSMatt Macy 
2415eda14cbcSMatt Macy 		redact_snaps = kmem_alloc(int_size * num_redact_snaps,
2416eda14cbcSMatt Macy 		    KM_SLEEP);
2417eda14cbcSMatt Macy 		VERIFY0(zap_lookup(dp->dp_meta_objset, ds->ds_object,
2418eda14cbcSMatt Macy 		    DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, int_size,
2419eda14cbcSMatt Macy 		    num_redact_snaps, redact_snaps));
2420eda14cbcSMatt Macy 		fnvlist_add_uint64_array(token_nv, "book_redact_snaps",
2421eda14cbcSMatt Macy 		    redact_snaps, num_redact_snaps);
2422eda14cbcSMatt Macy 		kmem_free(redact_snaps, int_size * num_redact_snaps);
2423eda14cbcSMatt Macy 	}
2424eda14cbcSMatt Macy 	packed = fnvlist_pack(token_nv, &packed_size);
2425eda14cbcSMatt Macy 	fnvlist_free(token_nv);
2426eda14cbcSMatt Macy 	compressed = kmem_alloc(packed_size, KM_SLEEP);
2427eda14cbcSMatt Macy 
2428*e2df9bb4SMartin Matuska 	/* Call compress function directly to avoid hole detection. */
2429*e2df9bb4SMartin Matuska 	abd_t pabd, cabd;
2430*e2df9bb4SMartin Matuska 	abd_get_from_buf_struct(&pabd, packed, packed_size);
2431*e2df9bb4SMartin Matuska 	abd_get_from_buf_struct(&cabd, compressed, packed_size);
2432*e2df9bb4SMartin Matuska 	compressed_size = zfs_gzip_compress(&pabd, &cabd,
2433eda14cbcSMatt Macy 	    packed_size, packed_size, 6);
2434*e2df9bb4SMartin Matuska 	abd_free(&cabd);
2435*e2df9bb4SMartin Matuska 	abd_free(&pabd);
2436eda14cbcSMatt Macy 
2437eda14cbcSMatt Macy 	zio_cksum_t cksum;
2438eda14cbcSMatt Macy 	fletcher_4_native_varsize(compressed, compressed_size, &cksum);
2439eda14cbcSMatt Macy 
2440eda14cbcSMatt Macy 	size_t alloc_size = compressed_size * 2 + 1;
2441eda14cbcSMatt Macy 	str = kmem_alloc(alloc_size, KM_SLEEP);
2442eda14cbcSMatt Macy 	for (int i = 0; i < compressed_size; i++) {
2443eda14cbcSMatt Macy 		size_t offset = i * 2;
2444eda14cbcSMatt Macy 		(void) snprintf(str + offset, alloc_size - offset,
2445eda14cbcSMatt Macy 	    "%02x", compressed[i]);
2446eda14cbcSMatt Macy 	}
2447eda14cbcSMatt Macy 	str[compressed_size * 2] = '\0';
2448eda14cbcSMatt Macy 	char *propval = kmem_asprintf("%u-%llx-%llx-%s",
2449eda14cbcSMatt Macy 	    ZFS_SEND_RESUME_TOKEN_VERSION,
2450eda14cbcSMatt Macy 	    (longlong_t)cksum.zc_word[0],
2451eda14cbcSMatt Macy 	    (longlong_t)packed_size, str);
2452eda14cbcSMatt Macy 	kmem_free(packed, packed_size);
2453eda14cbcSMatt Macy 	kmem_free(str, alloc_size);
2454eda14cbcSMatt Macy 	kmem_free(compressed, packed_size);
2455eda14cbcSMatt Macy 	return (propval);
2456eda14cbcSMatt Macy }
2457eda14cbcSMatt Macy 
2458eda14cbcSMatt Macy /*
2459c03c5b1cSMartin Matuska  * Returns a string that represents the receive resume state token. It should
2460c03c5b1cSMartin Matuska  * be freed with strfree(). NULL is returned if no resume state is present.
2461eda14cbcSMatt Macy  */
2462eda14cbcSMatt Macy char *
2463c03c5b1cSMartin Matuska get_receive_resume_token(dsl_dataset_t *ds)
2464eda14cbcSMatt Macy {
2465c03c5b1cSMartin Matuska 	/*
2466c03c5b1cSMartin Matuska 	 * A failed "newfs" (e.g. full) resumable receive leaves
2467c03c5b1cSMartin Matuska 	 * the stats set on this dataset.  Check here for the prop.
2468c03c5b1cSMartin Matuska 	 */
2469c03c5b1cSMartin Matuska 	char *token = get_receive_resume_token_impl(ds);
2470c03c5b1cSMartin Matuska 	if (token != NULL)
2471c03c5b1cSMartin Matuska 		return (token);
2472c03c5b1cSMartin Matuska 	/*
2473c03c5b1cSMartin Matuska 	 * A failed incremental resumable receive leaves the
2474c03c5b1cSMartin Matuska 	 * stats set on our child named "%recv".  Check the child
2475c03c5b1cSMartin Matuska 	 * for the prop.
2476c03c5b1cSMartin Matuska 	 */
2477c03c5b1cSMartin Matuska 	/* 6 extra bytes for /%recv */
2478c03c5b1cSMartin Matuska 	char name[ZFS_MAX_DATASET_NAME_LEN + 6];
2479eda14cbcSMatt Macy 	dsl_dataset_t *recv_ds;
2480c03c5b1cSMartin Matuska 	dsl_dataset_name(ds, name);
2481c03c5b1cSMartin Matuska 	if (strlcat(name, "/", sizeof (name)) < sizeof (name) &&
2482c03c5b1cSMartin Matuska 	    strlcat(name, recv_clone_name, sizeof (name)) < sizeof (name) &&
2483c03c5b1cSMartin Matuska 	    dsl_dataset_hold(ds->ds_dir->dd_pool, name, FTAG, &recv_ds) == 0) {
2484c03c5b1cSMartin Matuska 		token = get_receive_resume_token_impl(recv_ds);
2485eda14cbcSMatt Macy 		dsl_dataset_rele(recv_ds, FTAG);
2486eda14cbcSMatt Macy 	}
2487c03c5b1cSMartin Matuska 	return (token);
2488eda14cbcSMatt Macy }
2489eda14cbcSMatt Macy 
2490eda14cbcSMatt Macy uint64_t
2491eda14cbcSMatt Macy dsl_get_refratio(dsl_dataset_t *ds)
2492eda14cbcSMatt Macy {
2493eda14cbcSMatt Macy 	uint64_t ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 :
2494eda14cbcSMatt Macy 	    (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 /
2495eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_compressed_bytes);
2496eda14cbcSMatt Macy 	return (ratio);
2497eda14cbcSMatt Macy }
2498eda14cbcSMatt Macy 
2499eda14cbcSMatt Macy uint64_t
2500eda14cbcSMatt Macy dsl_get_logicalreferenced(dsl_dataset_t *ds)
2501eda14cbcSMatt Macy {
2502eda14cbcSMatt Macy 	return (dsl_dataset_phys(ds)->ds_uncompressed_bytes);
2503eda14cbcSMatt Macy }
2504eda14cbcSMatt Macy 
2505eda14cbcSMatt Macy uint64_t
2506eda14cbcSMatt Macy dsl_get_compressratio(dsl_dataset_t *ds)
2507eda14cbcSMatt Macy {
2508eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
2509eda14cbcSMatt Macy 		return (dsl_get_refratio(ds));
2510eda14cbcSMatt Macy 	} else {
2511eda14cbcSMatt Macy 		dsl_dir_t *dd = ds->ds_dir;
2512eda14cbcSMatt Macy 		mutex_enter(&dd->dd_lock);
2513eda14cbcSMatt Macy 		uint64_t val = dsl_dir_get_compressratio(dd);
2514eda14cbcSMatt Macy 		mutex_exit(&dd->dd_lock);
2515eda14cbcSMatt Macy 		return (val);
2516eda14cbcSMatt Macy 	}
2517eda14cbcSMatt Macy }
2518eda14cbcSMatt Macy 
2519eda14cbcSMatt Macy uint64_t
2520eda14cbcSMatt Macy dsl_get_used(dsl_dataset_t *ds)
2521eda14cbcSMatt Macy {
2522eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
2523eda14cbcSMatt Macy 		return (dsl_dataset_phys(ds)->ds_unique_bytes);
2524eda14cbcSMatt Macy 	} else {
2525eda14cbcSMatt Macy 		dsl_dir_t *dd = ds->ds_dir;
2526eda14cbcSMatt Macy 		mutex_enter(&dd->dd_lock);
2527eda14cbcSMatt Macy 		uint64_t val = dsl_dir_get_used(dd);
2528eda14cbcSMatt Macy 		mutex_exit(&dd->dd_lock);
2529eda14cbcSMatt Macy 		return (val);
2530eda14cbcSMatt Macy 	}
2531eda14cbcSMatt Macy }
2532eda14cbcSMatt Macy 
2533eda14cbcSMatt Macy uint64_t
2534eda14cbcSMatt Macy dsl_get_creation(dsl_dataset_t *ds)
2535eda14cbcSMatt Macy {
2536eda14cbcSMatt Macy 	return (dsl_dataset_phys(ds)->ds_creation_time);
2537eda14cbcSMatt Macy }
2538eda14cbcSMatt Macy 
2539eda14cbcSMatt Macy uint64_t
2540eda14cbcSMatt Macy dsl_get_creationtxg(dsl_dataset_t *ds)
2541eda14cbcSMatt Macy {
2542eda14cbcSMatt Macy 	return (dsl_dataset_phys(ds)->ds_creation_txg);
2543eda14cbcSMatt Macy }
2544eda14cbcSMatt Macy 
2545eda14cbcSMatt Macy uint64_t
2546eda14cbcSMatt Macy dsl_get_refquota(dsl_dataset_t *ds)
2547eda14cbcSMatt Macy {
2548eda14cbcSMatt Macy 	return (ds->ds_quota);
2549eda14cbcSMatt Macy }
2550eda14cbcSMatt Macy 
2551eda14cbcSMatt Macy uint64_t
2552eda14cbcSMatt Macy dsl_get_refreservation(dsl_dataset_t *ds)
2553eda14cbcSMatt Macy {
2554eda14cbcSMatt Macy 	return (ds->ds_reserved);
2555eda14cbcSMatt Macy }
2556eda14cbcSMatt Macy 
2557eda14cbcSMatt Macy uint64_t
2558eda14cbcSMatt Macy dsl_get_guid(dsl_dataset_t *ds)
2559eda14cbcSMatt Macy {
2560eda14cbcSMatt Macy 	return (dsl_dataset_phys(ds)->ds_guid);
2561eda14cbcSMatt Macy }
2562eda14cbcSMatt Macy 
2563eda14cbcSMatt Macy uint64_t
2564eda14cbcSMatt Macy dsl_get_unique(dsl_dataset_t *ds)
2565eda14cbcSMatt Macy {
2566eda14cbcSMatt Macy 	return (dsl_dataset_phys(ds)->ds_unique_bytes);
2567eda14cbcSMatt Macy }
2568eda14cbcSMatt Macy 
2569eda14cbcSMatt Macy uint64_t
2570eda14cbcSMatt Macy dsl_get_objsetid(dsl_dataset_t *ds)
2571eda14cbcSMatt Macy {
2572eda14cbcSMatt Macy 	return (ds->ds_object);
2573eda14cbcSMatt Macy }
2574eda14cbcSMatt Macy 
2575eda14cbcSMatt Macy uint64_t
2576eda14cbcSMatt Macy dsl_get_userrefs(dsl_dataset_t *ds)
2577eda14cbcSMatt Macy {
2578eda14cbcSMatt Macy 	return (ds->ds_userrefs);
2579eda14cbcSMatt Macy }
2580eda14cbcSMatt Macy 
2581eda14cbcSMatt Macy uint64_t
2582eda14cbcSMatt Macy dsl_get_defer_destroy(dsl_dataset_t *ds)
2583eda14cbcSMatt Macy {
2584eda14cbcSMatt Macy 	return (DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
2585eda14cbcSMatt Macy }
2586eda14cbcSMatt Macy 
2587eda14cbcSMatt Macy uint64_t
2588eda14cbcSMatt Macy dsl_get_referenced(dsl_dataset_t *ds)
2589eda14cbcSMatt Macy {
2590eda14cbcSMatt Macy 	return (dsl_dataset_phys(ds)->ds_referenced_bytes);
2591eda14cbcSMatt Macy }
2592eda14cbcSMatt Macy 
2593eda14cbcSMatt Macy uint64_t
2594eda14cbcSMatt Macy dsl_get_numclones(dsl_dataset_t *ds)
2595eda14cbcSMatt Macy {
2596eda14cbcSMatt Macy 	ASSERT(ds->ds_is_snapshot);
2597eda14cbcSMatt Macy 	return (dsl_dataset_phys(ds)->ds_num_children - 1);
2598eda14cbcSMatt Macy }
2599eda14cbcSMatt Macy 
2600eda14cbcSMatt Macy uint64_t
2601eda14cbcSMatt Macy dsl_get_inconsistent(dsl_dataset_t *ds)
2602eda14cbcSMatt Macy {
2603eda14cbcSMatt Macy 	return ((dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT) ?
2604eda14cbcSMatt Macy 	    1 : 0);
2605eda14cbcSMatt Macy }
2606eda14cbcSMatt Macy 
2607eda14cbcSMatt Macy uint64_t
2608eda14cbcSMatt Macy dsl_get_redacted(dsl_dataset_t *ds)
2609eda14cbcSMatt Macy {
2610eda14cbcSMatt Macy 	return (dsl_dataset_feature_is_active(ds,
2611eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS));
2612eda14cbcSMatt Macy }
2613eda14cbcSMatt Macy 
2614eda14cbcSMatt Macy uint64_t
2615eda14cbcSMatt Macy dsl_get_available(dsl_dataset_t *ds)
2616eda14cbcSMatt Macy {
2617eda14cbcSMatt Macy 	uint64_t refdbytes = dsl_get_referenced(ds);
2618eda14cbcSMatt Macy 	uint64_t availbytes = dsl_dir_space_available(ds->ds_dir,
2619eda14cbcSMatt Macy 	    NULL, 0, TRUE);
2620eda14cbcSMatt Macy 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
2621eda14cbcSMatt Macy 		availbytes +=
2622eda14cbcSMatt Macy 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2623eda14cbcSMatt Macy 	}
2624eda14cbcSMatt Macy 	if (ds->ds_quota != 0) {
2625eda14cbcSMatt Macy 		/*
2626eda14cbcSMatt Macy 		 * Adjust available bytes according to refquota
2627eda14cbcSMatt Macy 		 */
2628eda14cbcSMatt Macy 		if (refdbytes < ds->ds_quota) {
2629eda14cbcSMatt Macy 			availbytes = MIN(availbytes,
2630eda14cbcSMatt Macy 			    ds->ds_quota - refdbytes);
2631eda14cbcSMatt Macy 		} else {
2632eda14cbcSMatt Macy 			availbytes = 0;
2633eda14cbcSMatt Macy 		}
2634eda14cbcSMatt Macy 	}
2635eda14cbcSMatt Macy 	return (availbytes);
2636eda14cbcSMatt Macy }
2637eda14cbcSMatt Macy 
2638eda14cbcSMatt Macy int
2639eda14cbcSMatt Macy dsl_get_written(dsl_dataset_t *ds, uint64_t *written)
2640eda14cbcSMatt Macy {
2641eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2642eda14cbcSMatt Macy 	dsl_dataset_t *prev;
2643eda14cbcSMatt Macy 	int err = dsl_dataset_hold_obj(dp,
2644eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev);
2645eda14cbcSMatt Macy 	if (err == 0) {
2646eda14cbcSMatt Macy 		uint64_t comp, uncomp;
2647eda14cbcSMatt Macy 		err = dsl_dataset_space_written(prev, ds, written,
2648eda14cbcSMatt Macy 		    &comp, &uncomp);
2649eda14cbcSMatt Macy 		dsl_dataset_rele(prev, FTAG);
2650eda14cbcSMatt Macy 	}
2651eda14cbcSMatt Macy 	return (err);
2652eda14cbcSMatt Macy }
2653eda14cbcSMatt Macy 
2654eda14cbcSMatt Macy /*
2655eda14cbcSMatt Macy  * 'snap' should be a buffer of size ZFS_MAX_DATASET_NAME_LEN.
2656eda14cbcSMatt Macy  */
2657eda14cbcSMatt Macy int
2658eda14cbcSMatt Macy dsl_get_prev_snap(dsl_dataset_t *ds, char *snap)
2659eda14cbcSMatt Macy {
2660eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2661eda14cbcSMatt Macy 	if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) {
2662eda14cbcSMatt Macy 		dsl_dataset_name(ds->ds_prev, snap);
2663eda14cbcSMatt Macy 		return (0);
2664eda14cbcSMatt Macy 	} else {
2665eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
2666eda14cbcSMatt Macy 	}
2667eda14cbcSMatt Macy }
2668eda14cbcSMatt Macy 
2669eda14cbcSMatt Macy void
2670eda14cbcSMatt Macy dsl_get_redact_snaps(dsl_dataset_t *ds, nvlist_t *propval)
2671eda14cbcSMatt Macy {
2672eda14cbcSMatt Macy 	uint64_t nsnaps;
2673eda14cbcSMatt Macy 	uint64_t *snaps;
2674eda14cbcSMatt Macy 	if (dsl_dataset_get_uint64_array_feature(ds,
2675eda14cbcSMatt Macy 	    SPA_FEATURE_REDACTED_DATASETS, &nsnaps, &snaps)) {
2676eda14cbcSMatt Macy 		fnvlist_add_uint64_array(propval, ZPROP_VALUE, snaps,
2677eda14cbcSMatt Macy 		    nsnaps);
2678eda14cbcSMatt Macy 	}
2679eda14cbcSMatt Macy }
2680eda14cbcSMatt Macy 
2681eda14cbcSMatt Macy /*
2682eda14cbcSMatt Macy  * Returns the mountpoint property and source for the given dataset in the value
2683eda14cbcSMatt Macy  * and source buffers. The value buffer must be at least as large as MAXPATHLEN
2684eda14cbcSMatt Macy  * and the source buffer as least as large a ZFS_MAX_DATASET_NAME_LEN.
2685eda14cbcSMatt Macy  * Returns 0 on success and an error on failure.
2686eda14cbcSMatt Macy  */
2687eda14cbcSMatt Macy int
2688eda14cbcSMatt Macy dsl_get_mountpoint(dsl_dataset_t *ds, const char *dsname, char *value,
2689eda14cbcSMatt Macy     char *source)
2690eda14cbcSMatt Macy {
2691eda14cbcSMatt Macy 	int error;
2692eda14cbcSMatt Macy 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
2693eda14cbcSMatt Macy 
2694eda14cbcSMatt Macy 	/* Retrieve the mountpoint value stored in the zap object */
2695eda14cbcSMatt Macy 	error = dsl_prop_get_ds(ds, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), 1,
2696eda14cbcSMatt Macy 	    ZAP_MAXVALUELEN, value, source);
2697eda14cbcSMatt Macy 	if (error != 0) {
2698eda14cbcSMatt Macy 		return (error);
2699eda14cbcSMatt Macy 	}
2700eda14cbcSMatt Macy 
2701eda14cbcSMatt Macy 	/*
2702eda14cbcSMatt Macy 	 * Process the dsname and source to find the full mountpoint string.
2703eda14cbcSMatt Macy 	 * Can be skipped for 'legacy' or 'none'.
2704eda14cbcSMatt Macy 	 */
2705eda14cbcSMatt Macy 	if (value[0] == '/') {
2706eda14cbcSMatt Macy 		char *buf = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
2707eda14cbcSMatt Macy 		char *root = buf;
2708eda14cbcSMatt Macy 		const char *relpath;
2709eda14cbcSMatt Macy 
2710eda14cbcSMatt Macy 		/*
2711eda14cbcSMatt Macy 		 * If we inherit the mountpoint, even from a dataset
2712eda14cbcSMatt Macy 		 * with a received value, the source will be the path of
2713eda14cbcSMatt Macy 		 * the dataset we inherit from. If source is
2714eda14cbcSMatt Macy 		 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2715eda14cbcSMatt Macy 		 * inherited.
2716eda14cbcSMatt Macy 		 */
2717eda14cbcSMatt Macy 		if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2718eda14cbcSMatt Macy 			relpath = "";
2719eda14cbcSMatt Macy 		} else {
2720eda14cbcSMatt Macy 			ASSERT0(strncmp(dsname, source, strlen(source)));
2721eda14cbcSMatt Macy 			relpath = dsname + strlen(source);
2722eda14cbcSMatt Macy 			if (relpath[0] == '/')
2723eda14cbcSMatt Macy 				relpath++;
2724eda14cbcSMatt Macy 		}
2725eda14cbcSMatt Macy 
2726eda14cbcSMatt Macy 		spa_altroot(dp->dp_spa, root, ZAP_MAXVALUELEN);
2727eda14cbcSMatt Macy 
2728eda14cbcSMatt Macy 		/*
2729eda14cbcSMatt Macy 		 * Special case an alternate root of '/'. This will
2730eda14cbcSMatt Macy 		 * avoid having multiple leading slashes in the
2731eda14cbcSMatt Macy 		 * mountpoint path.
2732eda14cbcSMatt Macy 		 */
2733eda14cbcSMatt Macy 		if (strcmp(root, "/") == 0)
2734eda14cbcSMatt Macy 			root++;
2735eda14cbcSMatt Macy 
2736eda14cbcSMatt Macy 		/*
2737eda14cbcSMatt Macy 		 * If the mountpoint is '/' then skip over this
2738eda14cbcSMatt Macy 		 * if we are obtaining either an alternate root or
2739eda14cbcSMatt Macy 		 * an inherited mountpoint.
2740eda14cbcSMatt Macy 		 */
2741eda14cbcSMatt Macy 		char *mnt = value;
2742eda14cbcSMatt Macy 		if (value[1] == '\0' && (root[0] != '\0' ||
2743eda14cbcSMatt Macy 		    relpath[0] != '\0'))
2744eda14cbcSMatt Macy 			mnt = value + 1;
2745eda14cbcSMatt Macy 
2746be181ee2SMartin Matuska 		mnt = kmem_strdup(mnt);
2747be181ee2SMartin Matuska 
2748eda14cbcSMatt Macy 		if (relpath[0] == '\0') {
2749eda14cbcSMatt Macy 			(void) snprintf(value, ZAP_MAXVALUELEN, "%s%s",
2750eda14cbcSMatt Macy 			    root, mnt);
2751eda14cbcSMatt Macy 		} else {
2752eda14cbcSMatt Macy 			(void) snprintf(value, ZAP_MAXVALUELEN, "%s%s%s%s",
2753eda14cbcSMatt Macy 			    root, mnt, relpath[0] == '@' ? "" : "/",
2754eda14cbcSMatt Macy 			    relpath);
2755eda14cbcSMatt Macy 		}
2756eda14cbcSMatt Macy 		kmem_free(buf, ZAP_MAXVALUELEN);
2757be181ee2SMartin Matuska 		kmem_strfree(mnt);
2758eda14cbcSMatt Macy 	}
2759eda14cbcSMatt Macy 
2760eda14cbcSMatt Macy 	return (0);
2761eda14cbcSMatt Macy }
2762eda14cbcSMatt Macy 
2763eda14cbcSMatt Macy void
2764eda14cbcSMatt Macy dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
2765eda14cbcSMatt Macy {
2766c03c5b1cSMartin Matuska 	dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2767eda14cbcSMatt Macy 
2768eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
2769eda14cbcSMatt Macy 
2770eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO,
2771eda14cbcSMatt Macy 	    dsl_get_refratio(ds));
2772eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
2773eda14cbcSMatt Macy 	    dsl_get_logicalreferenced(ds));
2774eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
2775eda14cbcSMatt Macy 	    dsl_get_compressratio(ds));
2776eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
2777eda14cbcSMatt Macy 	    dsl_get_used(ds));
2778eda14cbcSMatt Macy 
2779eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
2780eda14cbcSMatt Macy 		get_clones_stat(ds, nv);
2781eda14cbcSMatt Macy 	} else {
2782eda14cbcSMatt Macy 		char buf[ZFS_MAX_DATASET_NAME_LEN];
2783eda14cbcSMatt Macy 		if (dsl_get_prev_snap(ds, buf) == 0)
2784eda14cbcSMatt Macy 			dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP,
2785eda14cbcSMatt Macy 			    buf);
2786eda14cbcSMatt Macy 		dsl_dir_stats(ds->ds_dir, nv);
2787eda14cbcSMatt Macy 	}
2788eda14cbcSMatt Macy 
2789eda14cbcSMatt Macy 	nvlist_t *propval = fnvlist_alloc();
2790eda14cbcSMatt Macy 	dsl_get_redact_snaps(ds, propval);
2791eda14cbcSMatt Macy 	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2792eda14cbcSMatt Macy 	    propval);
2793eda14cbcSMatt Macy 	nvlist_free(propval);
2794eda14cbcSMatt Macy 
2795eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE,
2796eda14cbcSMatt Macy 	    dsl_get_available(ds));
2797eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED,
2798eda14cbcSMatt Macy 	    dsl_get_referenced(ds));
2799eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
2800eda14cbcSMatt Macy 	    dsl_get_creation(ds));
2801eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
2802eda14cbcSMatt Macy 	    dsl_get_creationtxg(ds));
2803eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
2804eda14cbcSMatt Macy 	    dsl_get_refquota(ds));
2805eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
2806eda14cbcSMatt Macy 	    dsl_get_refreservation(ds));
2807eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
2808eda14cbcSMatt Macy 	    dsl_get_guid(ds));
2809eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
2810eda14cbcSMatt Macy 	    dsl_get_unique(ds));
2811eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2812eda14cbcSMatt Macy 	    dsl_get_objsetid(ds));
2813eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
2814eda14cbcSMatt Macy 	    dsl_get_userrefs(ds));
2815eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
2816eda14cbcSMatt Macy 	    dsl_get_defer_destroy(ds));
2817271171e0SMartin Matuska 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOTS_CHANGED,
2818271171e0SMartin Matuska 	    dsl_dir_snap_cmtime(ds->ds_dir).tv_sec);
2819eda14cbcSMatt Macy 	dsl_dataset_crypt_stats(ds, nv);
2820eda14cbcSMatt Macy 
2821eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) {
2822eda14cbcSMatt Macy 		uint64_t written;
2823eda14cbcSMatt Macy 		if (dsl_get_written(ds, &written) == 0) {
2824eda14cbcSMatt Macy 			dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
2825eda14cbcSMatt Macy 			    written);
2826eda14cbcSMatt Macy 		}
2827eda14cbcSMatt Macy 	}
2828eda14cbcSMatt Macy 
2829eda14cbcSMatt Macy 	if (!dsl_dataset_is_snapshot(ds)) {
2830c03c5b1cSMartin Matuska 		char *token = get_receive_resume_token(ds);
2831c03c5b1cSMartin Matuska 		if (token != NULL) {
2832c03c5b1cSMartin Matuska 			dsl_prop_nvlist_add_string(nv,
2833c03c5b1cSMartin Matuska 			    ZFS_PROP_RECEIVE_RESUME_TOKEN, token);
2834c03c5b1cSMartin Matuska 			kmem_strfree(token);
2835eda14cbcSMatt Macy 		}
2836eda14cbcSMatt Macy 	}
2837eda14cbcSMatt Macy }
2838eda14cbcSMatt Macy 
2839eda14cbcSMatt Macy void
2840eda14cbcSMatt Macy dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
2841eda14cbcSMatt Macy {
2842eda14cbcSMatt Macy 	dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2843eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
2844eda14cbcSMatt Macy 
2845eda14cbcSMatt Macy 	stat->dds_creation_txg = dsl_get_creationtxg(ds);
2846eda14cbcSMatt Macy 	stat->dds_inconsistent = dsl_get_inconsistent(ds);
2847eda14cbcSMatt Macy 	stat->dds_guid = dsl_get_guid(ds);
2848eda14cbcSMatt Macy 	stat->dds_redacted = dsl_get_redacted(ds);
2849eda14cbcSMatt Macy 	stat->dds_origin[0] = '\0';
2850eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
2851eda14cbcSMatt Macy 		stat->dds_is_snapshot = B_TRUE;
2852eda14cbcSMatt Macy 		stat->dds_num_clones = dsl_get_numclones(ds);
2853eda14cbcSMatt Macy 	} else {
2854eda14cbcSMatt Macy 		stat->dds_is_snapshot = B_FALSE;
2855eda14cbcSMatt Macy 		stat->dds_num_clones = 0;
2856eda14cbcSMatt Macy 
2857eda14cbcSMatt Macy 		if (dsl_dir_is_clone(ds->ds_dir)) {
2858eda14cbcSMatt Macy 			dsl_dir_get_origin(ds->ds_dir, stat->dds_origin);
2859eda14cbcSMatt Macy 		}
2860eda14cbcSMatt Macy 	}
2861eda14cbcSMatt Macy }
2862eda14cbcSMatt Macy 
2863eda14cbcSMatt Macy uint64_t
2864eda14cbcSMatt Macy dsl_dataset_fsid_guid(dsl_dataset_t *ds)
2865eda14cbcSMatt Macy {
2866eda14cbcSMatt Macy 	return (ds->ds_fsid_guid);
2867eda14cbcSMatt Macy }
2868eda14cbcSMatt Macy 
2869eda14cbcSMatt Macy void
2870eda14cbcSMatt Macy dsl_dataset_space(dsl_dataset_t *ds,
2871eda14cbcSMatt Macy     uint64_t *refdbytesp, uint64_t *availbytesp,
2872eda14cbcSMatt Macy     uint64_t *usedobjsp, uint64_t *availobjsp)
2873eda14cbcSMatt Macy {
2874eda14cbcSMatt Macy 	*refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes;
2875eda14cbcSMatt Macy 	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
2876eda14cbcSMatt Macy 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes)
2877eda14cbcSMatt Macy 		*availbytesp +=
2878eda14cbcSMatt Macy 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes;
2879eda14cbcSMatt Macy 	if (ds->ds_quota != 0) {
2880eda14cbcSMatt Macy 		/*
2881eda14cbcSMatt Macy 		 * Adjust available bytes according to refquota
2882eda14cbcSMatt Macy 		 */
2883eda14cbcSMatt Macy 		if (*refdbytesp < ds->ds_quota)
2884eda14cbcSMatt Macy 			*availbytesp = MIN(*availbytesp,
2885eda14cbcSMatt Macy 			    ds->ds_quota - *refdbytesp);
2886eda14cbcSMatt Macy 		else
2887eda14cbcSMatt Macy 			*availbytesp = 0;
2888eda14cbcSMatt Macy 	}
2889eda14cbcSMatt Macy 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2890eda14cbcSMatt Macy 	*usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp);
2891eda14cbcSMatt Macy 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
2892eda14cbcSMatt Macy 	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
2893eda14cbcSMatt Macy }
2894eda14cbcSMatt Macy 
2895eda14cbcSMatt Macy boolean_t
2896eda14cbcSMatt Macy dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
2897eda14cbcSMatt Macy {
2898eda14cbcSMatt Macy 	dsl_pool_t *dp __maybe_unused = ds->ds_dir->dd_pool;
2899eda14cbcSMatt Macy 	uint64_t birth;
2900eda14cbcSMatt Macy 
2901eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
2902eda14cbcSMatt Macy 	if (snap == NULL)
2903eda14cbcSMatt Macy 		return (B_FALSE);
2904eda14cbcSMatt Macy 	rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2905783d3ff6SMartin Matuska 	birth = BP_GET_LOGICAL_BIRTH(dsl_dataset_get_blkptr(ds));
2906eda14cbcSMatt Macy 	rrw_exit(&ds->ds_bp_rwlock, FTAG);
2907eda14cbcSMatt Macy 	if (birth > dsl_dataset_phys(snap)->ds_creation_txg) {
2908eda14cbcSMatt Macy 		objset_t *os, *os_snap;
2909eda14cbcSMatt Macy 		/*
2910eda14cbcSMatt Macy 		 * It may be that only the ZIL differs, because it was
2911eda14cbcSMatt Macy 		 * reset in the head.  Don't count that as being
2912eda14cbcSMatt Macy 		 * modified.
2913eda14cbcSMatt Macy 		 */
2914eda14cbcSMatt Macy 		if (dmu_objset_from_ds(ds, &os) != 0)
2915eda14cbcSMatt Macy 			return (B_TRUE);
2916eda14cbcSMatt Macy 		if (dmu_objset_from_ds(snap, &os_snap) != 0)
2917eda14cbcSMatt Macy 			return (B_TRUE);
2918da5137abSMartin Matuska 		return (memcmp(&os->os_phys->os_meta_dnode,
2919eda14cbcSMatt Macy 		    &os_snap->os_phys->os_meta_dnode,
2920eda14cbcSMatt Macy 		    sizeof (os->os_phys->os_meta_dnode)) != 0);
2921eda14cbcSMatt Macy 	}
2922eda14cbcSMatt Macy 	return (B_FALSE);
2923eda14cbcSMatt Macy }
2924eda14cbcSMatt Macy 
2925eda14cbcSMatt Macy static int
2926eda14cbcSMatt Macy dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
2927eda14cbcSMatt Macy     dsl_dataset_t *hds, void *arg)
2928eda14cbcSMatt Macy {
2929e92ffd9bSMartin Matuska 	(void) dp;
2930eda14cbcSMatt Macy 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2931eda14cbcSMatt Macy 	int error;
2932eda14cbcSMatt Macy 	uint64_t val;
2933eda14cbcSMatt Macy 
2934eda14cbcSMatt Macy 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2935eda14cbcSMatt Macy 	if (error != 0) {
2936eda14cbcSMatt Macy 		/* ignore nonexistent snapshots */
2937eda14cbcSMatt Macy 		return (error == ENOENT ? 0 : error);
2938eda14cbcSMatt Macy 	}
2939eda14cbcSMatt Macy 
2940eda14cbcSMatt Macy 	/* new name should not exist */
2941eda14cbcSMatt Macy 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
2942eda14cbcSMatt Macy 	if (error == 0)
2943eda14cbcSMatt Macy 		error = SET_ERROR(EEXIST);
2944eda14cbcSMatt Macy 	else if (error == ENOENT)
2945eda14cbcSMatt Macy 		error = 0;
2946eda14cbcSMatt Macy 
2947eda14cbcSMatt Macy 	/* dataset name + 1 for the "@" + the new snapshot name must fit */
2948eda14cbcSMatt Macy 	if (dsl_dir_namelen(hds->ds_dir) + 1 +
2949eda14cbcSMatt Macy 	    strlen(ddrsa->ddrsa_newsnapname) >= ZFS_MAX_DATASET_NAME_LEN)
2950eda14cbcSMatt Macy 		error = SET_ERROR(ENAMETOOLONG);
2951eda14cbcSMatt Macy 
2952eda14cbcSMatt Macy 	return (error);
2953eda14cbcSMatt Macy }
2954eda14cbcSMatt Macy 
2955c7046f76SMartin Matuska int
2956eda14cbcSMatt Macy dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
2957eda14cbcSMatt Macy {
2958eda14cbcSMatt Macy 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2959eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
2960eda14cbcSMatt Macy 	dsl_dataset_t *hds;
2961eda14cbcSMatt Macy 	int error;
2962eda14cbcSMatt Macy 
2963eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
2964eda14cbcSMatt Macy 	if (error != 0)
2965eda14cbcSMatt Macy 		return (error);
2966eda14cbcSMatt Macy 
2967eda14cbcSMatt Macy 	if (ddrsa->ddrsa_recursive) {
2968eda14cbcSMatt Macy 		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
2969eda14cbcSMatt Macy 		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
2970eda14cbcSMatt Macy 		    DS_FIND_CHILDREN);
2971eda14cbcSMatt Macy 	} else {
2972eda14cbcSMatt Macy 		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
2973eda14cbcSMatt Macy 	}
2974eda14cbcSMatt Macy 	dsl_dataset_rele(hds, FTAG);
2975eda14cbcSMatt Macy 	return (error);
2976eda14cbcSMatt Macy }
2977eda14cbcSMatt Macy 
2978eda14cbcSMatt Macy static int
2979eda14cbcSMatt Macy dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
2980eda14cbcSMatt Macy     dsl_dataset_t *hds, void *arg)
2981eda14cbcSMatt Macy {
2982eda14cbcSMatt Macy 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
2983eda14cbcSMatt Macy 	dsl_dataset_t *ds;
2984eda14cbcSMatt Macy 	uint64_t val;
2985eda14cbcSMatt Macy 	dmu_tx_t *tx = ddrsa->ddrsa_tx;
2986eda14cbcSMatt Macy 	int error;
2987eda14cbcSMatt Macy 
2988eda14cbcSMatt Macy 	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
2989eda14cbcSMatt Macy 	ASSERT(error == 0 || error == ENOENT);
2990eda14cbcSMatt Macy 	if (error == ENOENT) {
2991eda14cbcSMatt Macy 		/* ignore nonexistent snapshots */
2992eda14cbcSMatt Macy 		return (0);
2993eda14cbcSMatt Macy 	}
2994eda14cbcSMatt Macy 
2995eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
2996eda14cbcSMatt Macy 
2997eda14cbcSMatt Macy 	/* log before we change the name */
2998eda14cbcSMatt Macy 	spa_history_log_internal_ds(ds, "rename", tx,
2999eda14cbcSMatt Macy 	    "-> @%s", ddrsa->ddrsa_newsnapname);
3000eda14cbcSMatt Macy 
3001eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx,
3002eda14cbcSMatt Macy 	    B_FALSE));
3003eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
3004eda14cbcSMatt Macy 	(void) strlcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname,
3005eda14cbcSMatt Macy 	    sizeof (ds->ds_snapname));
3006eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
3007eda14cbcSMatt Macy 	VERIFY0(zap_add(dp->dp_meta_objset,
3008eda14cbcSMatt Macy 	    dsl_dataset_phys(hds)->ds_snapnames_zapobj,
3009eda14cbcSMatt Macy 	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
3010eda14cbcSMatt Macy 	zvol_rename_minors(dp->dp_spa, ddrsa->ddrsa_oldsnapname,
3011eda14cbcSMatt Macy 	    ddrsa->ddrsa_newsnapname, B_TRUE);
3012eda14cbcSMatt Macy 
3013eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
3014eda14cbcSMatt Macy 	return (0);
3015eda14cbcSMatt Macy }
3016eda14cbcSMatt Macy 
3017c7046f76SMartin Matuska void
3018eda14cbcSMatt Macy dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
3019eda14cbcSMatt Macy {
3020eda14cbcSMatt Macy 	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
3021eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3022eda14cbcSMatt Macy 	dsl_dataset_t *hds = NULL;
3023eda14cbcSMatt Macy 
3024eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
3025eda14cbcSMatt Macy 	ddrsa->ddrsa_tx = tx;
3026eda14cbcSMatt Macy 	if (ddrsa->ddrsa_recursive) {
3027eda14cbcSMatt Macy 		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
3028eda14cbcSMatt Macy 		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
3029eda14cbcSMatt Macy 		    DS_FIND_CHILDREN));
3030eda14cbcSMatt Macy 	} else {
3031eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
3032eda14cbcSMatt Macy 	}
3033eda14cbcSMatt Macy 	dsl_dataset_rele(hds, FTAG);
3034eda14cbcSMatt Macy }
3035eda14cbcSMatt Macy 
3036eda14cbcSMatt Macy int
3037eda14cbcSMatt Macy dsl_dataset_rename_snapshot(const char *fsname,
3038eda14cbcSMatt Macy     const char *oldsnapname, const char *newsnapname, boolean_t recursive)
3039eda14cbcSMatt Macy {
3040eda14cbcSMatt Macy 	dsl_dataset_rename_snapshot_arg_t ddrsa;
3041eda14cbcSMatt Macy 
3042eda14cbcSMatt Macy 	ddrsa.ddrsa_fsname = fsname;
3043eda14cbcSMatt Macy 	ddrsa.ddrsa_oldsnapname = oldsnapname;
3044eda14cbcSMatt Macy 	ddrsa.ddrsa_newsnapname = newsnapname;
3045eda14cbcSMatt Macy 	ddrsa.ddrsa_recursive = recursive;
3046eda14cbcSMatt Macy 
3047eda14cbcSMatt Macy 	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
3048eda14cbcSMatt Macy 	    dsl_dataset_rename_snapshot_sync, &ddrsa,
3049eda14cbcSMatt Macy 	    1, ZFS_SPACE_CHECK_RESERVED));
3050eda14cbcSMatt Macy }
3051eda14cbcSMatt Macy 
3052eda14cbcSMatt Macy /*
3053eda14cbcSMatt Macy  * If we're doing an ownership handoff, we need to make sure that there is
3054eda14cbcSMatt Macy  * only one long hold on the dataset.  We're not allowed to change anything here
3055eda14cbcSMatt Macy  * so we don't permanently release the long hold or regular hold here.  We want
3056eda14cbcSMatt Macy  * to do this only when syncing to avoid the dataset unexpectedly going away
3057eda14cbcSMatt Macy  * when we release the long hold.
3058eda14cbcSMatt Macy  */
3059eda14cbcSMatt Macy static int
3060eda14cbcSMatt Macy dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
3061eda14cbcSMatt Macy {
3062eda14cbcSMatt Macy 	boolean_t held = B_FALSE;
3063eda14cbcSMatt Macy 
3064eda14cbcSMatt Macy 	if (!dmu_tx_is_syncing(tx))
3065eda14cbcSMatt Macy 		return (0);
3066eda14cbcSMatt Macy 
3067eda14cbcSMatt Macy 	dsl_dir_t *dd = ds->ds_dir;
3068eda14cbcSMatt Macy 	mutex_enter(&dd->dd_activity_lock);
3069eda14cbcSMatt Macy 	uint64_t holds = zfs_refcount_count(&ds->ds_longholds) -
3070eda14cbcSMatt Macy 	    (owner != NULL ? 1 : 0);
3071eda14cbcSMatt Macy 	/*
3072eda14cbcSMatt Macy 	 * The value of dd_activity_waiters can chance as soon as we drop the
3073eda14cbcSMatt Macy 	 * lock, but we're fine with that; new waiters coming in or old
3074eda14cbcSMatt Macy 	 * waiters leaving doesn't cause problems, since we're going to cancel
3075eda14cbcSMatt Macy 	 * waiters later anyway. The goal of this check is to verify that no
3076eda14cbcSMatt Macy 	 * non-waiters have long-holds, and all new long-holds will be
3077eda14cbcSMatt Macy 	 * prevented because we're holding the pool config as writer.
3078eda14cbcSMatt Macy 	 */
3079eda14cbcSMatt Macy 	if (holds != dd->dd_activity_waiters)
3080eda14cbcSMatt Macy 		held = B_TRUE;
3081eda14cbcSMatt Macy 	mutex_exit(&dd->dd_activity_lock);
3082eda14cbcSMatt Macy 
3083eda14cbcSMatt Macy 	if (held)
3084eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
3085eda14cbcSMatt Macy 
3086eda14cbcSMatt Macy 	return (0);
3087eda14cbcSMatt Macy }
3088eda14cbcSMatt Macy 
3089eda14cbcSMatt Macy int
3090eda14cbcSMatt Macy dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
3091eda14cbcSMatt Macy {
3092eda14cbcSMatt Macy 	dsl_dataset_rollback_arg_t *ddra = arg;
3093eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3094eda14cbcSMatt Macy 	dsl_dataset_t *ds;
3095eda14cbcSMatt Macy 	int64_t unused_refres_delta;
3096eda14cbcSMatt Macy 	int error;
3097eda14cbcSMatt Macy 
3098eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
3099eda14cbcSMatt Macy 	if (error != 0)
3100eda14cbcSMatt Macy 		return (error);
3101eda14cbcSMatt Macy 
3102eda14cbcSMatt Macy 	/* must not be a snapshot */
3103eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
3104eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3105eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3106eda14cbcSMatt Macy 	}
3107eda14cbcSMatt Macy 
3108eda14cbcSMatt Macy 	/* must have a most recent snapshot */
3109eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) {
3110eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3111eda14cbcSMatt Macy 		return (SET_ERROR(ESRCH));
3112eda14cbcSMatt Macy 	}
3113eda14cbcSMatt Macy 
3114eda14cbcSMatt Macy 	/*
3115eda14cbcSMatt Macy 	 * No rollback to a snapshot created in the current txg, because
3116eda14cbcSMatt Macy 	 * the rollback may dirty the dataset and create blocks that are
3117eda14cbcSMatt Macy 	 * not reachable from the rootbp while having a birth txg that
3118eda14cbcSMatt Macy 	 * falls into the snapshot's range.
3119eda14cbcSMatt Macy 	 */
3120eda14cbcSMatt Macy 	if (dmu_tx_is_syncing(tx) &&
3121eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) {
3122eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3123eda14cbcSMatt Macy 		return (SET_ERROR(EAGAIN));
3124eda14cbcSMatt Macy 	}
3125eda14cbcSMatt Macy 
3126eda14cbcSMatt Macy 	/*
3127eda14cbcSMatt Macy 	 * If the expected target snapshot is specified, then check that
3128eda14cbcSMatt Macy 	 * the latest snapshot is it.
3129eda14cbcSMatt Macy 	 */
3130eda14cbcSMatt Macy 	if (ddra->ddra_tosnap != NULL) {
3131eda14cbcSMatt Macy 		dsl_dataset_t *snapds;
3132eda14cbcSMatt Macy 
3133eda14cbcSMatt Macy 		/* Check if the target snapshot exists at all. */
3134eda14cbcSMatt Macy 		error = dsl_dataset_hold(dp, ddra->ddra_tosnap, FTAG, &snapds);
3135eda14cbcSMatt Macy 		if (error != 0) {
3136eda14cbcSMatt Macy 			/*
3137eda14cbcSMatt Macy 			 * ESRCH is used to signal that the target snapshot does
3138eda14cbcSMatt Macy 			 * not exist, while ENOENT is used to report that
3139eda14cbcSMatt Macy 			 * the rolled back dataset does not exist.
3140eda14cbcSMatt Macy 			 * ESRCH is also used to cover other cases where the
3141eda14cbcSMatt Macy 			 * target snapshot is not related to the dataset being
3142eda14cbcSMatt Macy 			 * rolled back such as being in a different pool.
3143eda14cbcSMatt Macy 			 */
3144eda14cbcSMatt Macy 			if (error == ENOENT || error == EXDEV)
3145eda14cbcSMatt Macy 				error = SET_ERROR(ESRCH);
3146eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
3147eda14cbcSMatt Macy 			return (error);
3148eda14cbcSMatt Macy 		}
3149eda14cbcSMatt Macy 		ASSERT(snapds->ds_is_snapshot);
3150eda14cbcSMatt Macy 
3151eda14cbcSMatt Macy 		/* Check if the snapshot is the latest snapshot indeed. */
3152eda14cbcSMatt Macy 		if (snapds != ds->ds_prev) {
3153eda14cbcSMatt Macy 			/*
3154eda14cbcSMatt Macy 			 * Distinguish between the case where the only problem
3155eda14cbcSMatt Macy 			 * is intervening snapshots (EEXIST) vs the snapshot
3156eda14cbcSMatt Macy 			 * not being a valid target for rollback (ESRCH).
3157eda14cbcSMatt Macy 			 */
3158eda14cbcSMatt Macy 			if (snapds->ds_dir == ds->ds_dir ||
3159eda14cbcSMatt Macy 			    (dsl_dir_is_clone(ds->ds_dir) &&
3160eda14cbcSMatt Macy 			    dsl_dir_phys(ds->ds_dir)->dd_origin_obj ==
3161eda14cbcSMatt Macy 			    snapds->ds_object)) {
3162eda14cbcSMatt Macy 				error = SET_ERROR(EEXIST);
3163eda14cbcSMatt Macy 			} else {
3164eda14cbcSMatt Macy 				error = SET_ERROR(ESRCH);
3165eda14cbcSMatt Macy 			}
3166eda14cbcSMatt Macy 			dsl_dataset_rele(snapds, FTAG);
3167eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
3168eda14cbcSMatt Macy 			return (error);
3169eda14cbcSMatt Macy 		}
3170eda14cbcSMatt Macy 		dsl_dataset_rele(snapds, FTAG);
3171eda14cbcSMatt Macy 	}
3172eda14cbcSMatt Macy 
3173eda14cbcSMatt Macy 	/* must not have any bookmarks after the most recent snapshot */
3174eda14cbcSMatt Macy 	if (dsl_bookmark_latest_txg(ds) >
3175eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_txg) {
3176eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3177eda14cbcSMatt Macy 		return (SET_ERROR(EEXIST));
3178eda14cbcSMatt Macy 	}
3179eda14cbcSMatt Macy 
3180eda14cbcSMatt Macy 	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
3181eda14cbcSMatt Macy 	if (error != 0) {
3182eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3183eda14cbcSMatt Macy 		return (error);
3184eda14cbcSMatt Macy 	}
3185eda14cbcSMatt Macy 
3186eda14cbcSMatt Macy 	/*
3187eda14cbcSMatt Macy 	 * Check if the snap we are rolling back to uses more than
3188eda14cbcSMatt Macy 	 * the refquota.
3189eda14cbcSMatt Macy 	 */
3190eda14cbcSMatt Macy 	if (ds->ds_quota != 0 &&
3191eda14cbcSMatt Macy 	    dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) {
3192eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3193eda14cbcSMatt Macy 		return (SET_ERROR(EDQUOT));
3194eda14cbcSMatt Macy 	}
3195eda14cbcSMatt Macy 
3196eda14cbcSMatt Macy 	/*
3197eda14cbcSMatt Macy 	 * When we do the clone swap, we will temporarily use more space
3198eda14cbcSMatt Macy 	 * due to the refreservation (the head will no longer have any
3199eda14cbcSMatt Macy 	 * unique space, so the entire amount of the refreservation will need
3200eda14cbcSMatt Macy 	 * to be free).  We will immediately destroy the clone, freeing
3201eda14cbcSMatt Macy 	 * this space, but the freeing happens over many txg's.
3202eda14cbcSMatt Macy 	 */
3203eda14cbcSMatt Macy 	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
3204eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_unique_bytes);
3205eda14cbcSMatt Macy 
3206eda14cbcSMatt Macy 	if (unused_refres_delta > 0 &&
3207eda14cbcSMatt Macy 	    unused_refres_delta >
3208eda14cbcSMatt Macy 	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
3209eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
3210eda14cbcSMatt Macy 		return (SET_ERROR(ENOSPC));
3211eda14cbcSMatt Macy 	}
3212eda14cbcSMatt Macy 
3213eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
3214eda14cbcSMatt Macy 	return (0);
3215eda14cbcSMatt Macy }
3216eda14cbcSMatt Macy 
3217eda14cbcSMatt Macy void
3218eda14cbcSMatt Macy dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
3219eda14cbcSMatt Macy {
3220eda14cbcSMatt Macy 	dsl_dataset_rollback_arg_t *ddra = arg;
3221eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3222eda14cbcSMatt Macy 	dsl_dataset_t *ds, *clone;
3223eda14cbcSMatt Macy 	uint64_t cloneobj;
3224eda14cbcSMatt Macy 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3225eda14cbcSMatt Macy 
3226eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
3227eda14cbcSMatt Macy 
3228eda14cbcSMatt Macy 	dsl_dataset_name(ds->ds_prev, namebuf);
3229eda14cbcSMatt Macy 	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
3230eda14cbcSMatt Macy 
3231eda14cbcSMatt Macy 	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
3232eda14cbcSMatt Macy 	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, NULL, tx);
3233eda14cbcSMatt Macy 
3234eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
3235eda14cbcSMatt Macy 
3236eda14cbcSMatt Macy 	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
3237eda14cbcSMatt Macy 	dsl_dataset_zero_zil(ds, tx);
3238eda14cbcSMatt Macy 
3239eda14cbcSMatt Macy 	dsl_destroy_head_sync_impl(clone, tx);
3240eda14cbcSMatt Macy 
3241eda14cbcSMatt Macy 	dsl_dataset_rele(clone, FTAG);
3242eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
3243eda14cbcSMatt Macy }
3244eda14cbcSMatt Macy 
3245eda14cbcSMatt Macy /*
3246eda14cbcSMatt Macy  * Rolls back the given filesystem or volume to the most recent snapshot.
3247eda14cbcSMatt Macy  * The name of the most recent snapshot will be returned under key "target"
3248eda14cbcSMatt Macy  * in the result nvlist.
3249eda14cbcSMatt Macy  *
3250eda14cbcSMatt Macy  * If owner != NULL:
3251eda14cbcSMatt Macy  * - The existing dataset MUST be owned by the specified owner at entry
3252eda14cbcSMatt Macy  * - Upon return, dataset will still be held by the same owner, whether we
3253eda14cbcSMatt Macy  *   succeed or not.
3254eda14cbcSMatt Macy  *
3255eda14cbcSMatt Macy  * This mode is required any time the existing filesystem is mounted.  See
3256eda14cbcSMatt Macy  * notes above zfs_suspend_fs() for further details.
3257eda14cbcSMatt Macy  */
3258eda14cbcSMatt Macy int
3259eda14cbcSMatt Macy dsl_dataset_rollback(const char *fsname, const char *tosnap, void *owner,
3260eda14cbcSMatt Macy     nvlist_t *result)
3261eda14cbcSMatt Macy {
3262eda14cbcSMatt Macy 	dsl_dataset_rollback_arg_t ddra;
3263eda14cbcSMatt Macy 
3264eda14cbcSMatt Macy 	ddra.ddra_fsname = fsname;
3265eda14cbcSMatt Macy 	ddra.ddra_tosnap = tosnap;
3266eda14cbcSMatt Macy 	ddra.ddra_owner = owner;
3267eda14cbcSMatt Macy 	ddra.ddra_result = result;
3268eda14cbcSMatt Macy 
3269eda14cbcSMatt Macy 	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
3270eda14cbcSMatt Macy 	    dsl_dataset_rollback_sync, &ddra,
3271eda14cbcSMatt Macy 	    1, ZFS_SPACE_CHECK_RESERVED));
3272eda14cbcSMatt Macy }
3273eda14cbcSMatt Macy 
3274eda14cbcSMatt Macy struct promotenode {
3275eda14cbcSMatt Macy 	list_node_t link;
3276eda14cbcSMatt Macy 	dsl_dataset_t *ds;
3277eda14cbcSMatt Macy };
3278eda14cbcSMatt Macy 
3279eda14cbcSMatt Macy static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
3280eda14cbcSMatt Macy static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
3281a0b956f5SMartin Matuska     const void *tag);
3282a0b956f5SMartin Matuska static void promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag);
3283eda14cbcSMatt Macy 
3284eda14cbcSMatt Macy int
3285eda14cbcSMatt Macy dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
3286eda14cbcSMatt Macy {
3287eda14cbcSMatt Macy 	dsl_dataset_promote_arg_t *ddpa = arg;
3288eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3289eda14cbcSMatt Macy 	dsl_dataset_t *hds;
3290eda14cbcSMatt Macy 	struct promotenode *snap;
3291eda14cbcSMatt Macy 	int err;
3292eda14cbcSMatt Macy 	uint64_t unused;
3293eda14cbcSMatt Macy 	uint64_t ss_mv_cnt;
3294eda14cbcSMatt Macy 	size_t max_snap_len;
3295eda14cbcSMatt Macy 	boolean_t conflicting_snaps;
3296eda14cbcSMatt Macy 
3297eda14cbcSMatt Macy 	err = promote_hold(ddpa, dp, FTAG);
3298eda14cbcSMatt Macy 	if (err != 0)
3299eda14cbcSMatt Macy 		return (err);
3300eda14cbcSMatt Macy 
3301eda14cbcSMatt Macy 	hds = ddpa->ddpa_clone;
3302eda14cbcSMatt Macy 	max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1;
3303eda14cbcSMatt Macy 
3304eda14cbcSMatt Macy 	if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) {
3305eda14cbcSMatt Macy 		promote_rele(ddpa, FTAG);
3306eda14cbcSMatt Macy 		return (SET_ERROR(EXDEV));
3307eda14cbcSMatt Macy 	}
3308eda14cbcSMatt Macy 
3309eda14cbcSMatt Macy 	snap = list_head(&ddpa->shared_snaps);
3310eda14cbcSMatt Macy 	if (snap == NULL) {
3311eda14cbcSMatt Macy 		err = SET_ERROR(ENOENT);
3312eda14cbcSMatt Macy 		goto out;
3313eda14cbcSMatt Macy 	}
3314be181ee2SMartin Matuska 	dsl_dataset_t *const origin_ds = snap->ds;
3315eda14cbcSMatt Macy 
3316eda14cbcSMatt Macy 	/*
3317eda14cbcSMatt Macy 	 * Encrypted clones share a DSL Crypto Key with their origin's dsl dir.
3318eda14cbcSMatt Macy 	 * When doing a promote we must make sure the encryption root for
3319eda14cbcSMatt Macy 	 * both the target and the target's origin does not change to avoid
3320eda14cbcSMatt Macy 	 * needing to rewrap encryption keys
3321eda14cbcSMatt Macy 	 */
3322eda14cbcSMatt Macy 	err = dsl_dataset_promote_crypt_check(hds->ds_dir, origin_ds->ds_dir);
3323eda14cbcSMatt Macy 	if (err != 0)
3324eda14cbcSMatt Macy 		goto out;
3325eda14cbcSMatt Macy 
3326eda14cbcSMatt Macy 	/*
3327eda14cbcSMatt Macy 	 * Compute and check the amount of space to transfer.  Since this is
3328eda14cbcSMatt Macy 	 * so expensive, don't do the preliminary check.
3329eda14cbcSMatt Macy 	 */
3330eda14cbcSMatt Macy 	if (!dmu_tx_is_syncing(tx)) {
3331eda14cbcSMatt Macy 		promote_rele(ddpa, FTAG);
3332eda14cbcSMatt Macy 		return (0);
3333eda14cbcSMatt Macy 	}
3334eda14cbcSMatt Macy 
3335eda14cbcSMatt Macy 	/* compute origin's new unique space */
3336eda14cbcSMatt Macy 	snap = list_tail(&ddpa->clone_snaps);
3337eda14cbcSMatt Macy 	ASSERT(snap != NULL);
3338eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
3339eda14cbcSMatt Macy 	    origin_ds->ds_object);
3340eda14cbcSMatt Macy 	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3341eda14cbcSMatt Macy 	    dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX,
3342eda14cbcSMatt Macy 	    &ddpa->unique, &unused, &unused);
3343eda14cbcSMatt Macy 
3344eda14cbcSMatt Macy 	/*
3345eda14cbcSMatt Macy 	 * Walk the snapshots that we are moving
3346eda14cbcSMatt Macy 	 *
3347eda14cbcSMatt Macy 	 * Compute space to transfer.  Consider the incremental changes
3348eda14cbcSMatt Macy 	 * to used by each snapshot:
3349eda14cbcSMatt Macy 	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
3350eda14cbcSMatt Macy 	 * So each snapshot gave birth to:
3351eda14cbcSMatt Macy 	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
3352eda14cbcSMatt Macy 	 * So a sequence would look like:
3353eda14cbcSMatt Macy 	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
3354eda14cbcSMatt Macy 	 * Which simplifies to:
3355eda14cbcSMatt Macy 	 * uN + kN + kN-1 + ... + k1 + k0
3356eda14cbcSMatt Macy 	 * Note however, if we stop before we reach the ORIGIN we get:
3357eda14cbcSMatt Macy 	 * uN + kN + kN-1 + ... + kM - uM-1
3358eda14cbcSMatt Macy 	 */
3359eda14cbcSMatt Macy 	conflicting_snaps = B_FALSE;
3360eda14cbcSMatt Macy 	ss_mv_cnt = 0;
3361eda14cbcSMatt Macy 	ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes;
3362eda14cbcSMatt Macy 	ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes;
3363eda14cbcSMatt Macy 	ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes;
3364eda14cbcSMatt Macy 	for (snap = list_head(&ddpa->shared_snaps); snap;
3365eda14cbcSMatt Macy 	    snap = list_next(&ddpa->shared_snaps, snap)) {
3366eda14cbcSMatt Macy 		uint64_t val, dlused, dlcomp, dluncomp;
3367eda14cbcSMatt Macy 		dsl_dataset_t *ds = snap->ds;
3368eda14cbcSMatt Macy 
3369eda14cbcSMatt Macy 		ss_mv_cnt++;
3370eda14cbcSMatt Macy 
3371eda14cbcSMatt Macy 		/*
3372eda14cbcSMatt Macy 		 * If there are long holds, we won't be able to evict
3373eda14cbcSMatt Macy 		 * the objset.
3374eda14cbcSMatt Macy 		 */
3375eda14cbcSMatt Macy 		if (dsl_dataset_long_held(ds)) {
3376eda14cbcSMatt Macy 			err = SET_ERROR(EBUSY);
3377eda14cbcSMatt Macy 			goto out;
3378eda14cbcSMatt Macy 		}
3379eda14cbcSMatt Macy 
3380eda14cbcSMatt Macy 		/* Check that the snapshot name does not conflict */
3381eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_get_snapname(ds));
3382eda14cbcSMatt Macy 		if (strlen(ds->ds_snapname) >= max_snap_len) {
3383eda14cbcSMatt Macy 			err = SET_ERROR(ENAMETOOLONG);
3384eda14cbcSMatt Macy 			goto out;
3385eda14cbcSMatt Macy 		}
3386eda14cbcSMatt Macy 		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
3387eda14cbcSMatt Macy 		if (err == 0) {
3388eda14cbcSMatt Macy 			fnvlist_add_boolean(ddpa->err_ds,
3389eda14cbcSMatt Macy 			    snap->ds->ds_snapname);
3390eda14cbcSMatt Macy 			conflicting_snaps = B_TRUE;
3391eda14cbcSMatt Macy 		} else if (err != ENOENT) {
3392eda14cbcSMatt Macy 			goto out;
3393eda14cbcSMatt Macy 		}
3394eda14cbcSMatt Macy 
3395eda14cbcSMatt Macy 		/* The very first snapshot does not have a deadlist */
3396eda14cbcSMatt Macy 		if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0)
3397eda14cbcSMatt Macy 			continue;
3398eda14cbcSMatt Macy 
3399eda14cbcSMatt Macy 		dsl_deadlist_space(&ds->ds_deadlist,
3400eda14cbcSMatt Macy 		    &dlused, &dlcomp, &dluncomp);
3401eda14cbcSMatt Macy 		ddpa->used += dlused;
3402eda14cbcSMatt Macy 		ddpa->comp += dlcomp;
3403eda14cbcSMatt Macy 		ddpa->uncomp += dluncomp;
3404eda14cbcSMatt Macy 	}
3405eda14cbcSMatt Macy 
3406eda14cbcSMatt Macy 	/*
3407eda14cbcSMatt Macy 	 * Check that bookmarks that are being transferred don't have
3408eda14cbcSMatt Macy 	 * name conflicts.
3409eda14cbcSMatt Macy 	 */
3410be181ee2SMartin Matuska 	for (dsl_bookmark_node_t *dbn = avl_first(&origin_ds->ds_bookmarks);
3411eda14cbcSMatt Macy 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg <=
3412eda14cbcSMatt Macy 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
3413be181ee2SMartin Matuska 	    dbn = AVL_NEXT(&origin_ds->ds_bookmarks, dbn)) {
3414eda14cbcSMatt Macy 		if (strlen(dbn->dbn_name) >= max_snap_len) {
3415eda14cbcSMatt Macy 			err = SET_ERROR(ENAMETOOLONG);
3416eda14cbcSMatt Macy 			goto out;
3417eda14cbcSMatt Macy 		}
3418eda14cbcSMatt Macy 		zfs_bookmark_phys_t bm;
3419eda14cbcSMatt Macy 		err = dsl_bookmark_lookup_impl(ddpa->ddpa_clone,
3420eda14cbcSMatt Macy 		    dbn->dbn_name, &bm);
3421eda14cbcSMatt Macy 
3422eda14cbcSMatt Macy 		if (err == 0) {
3423eda14cbcSMatt Macy 			fnvlist_add_boolean(ddpa->err_ds, dbn->dbn_name);
3424eda14cbcSMatt Macy 			conflicting_snaps = B_TRUE;
3425eda14cbcSMatt Macy 		} else if (err == ESRCH) {
3426eda14cbcSMatt Macy 			err = 0;
3427dbd5678dSMartin Matuska 		}
3428dbd5678dSMartin Matuska 		if (err != 0) {
3429eda14cbcSMatt Macy 			goto out;
3430eda14cbcSMatt Macy 		}
3431eda14cbcSMatt Macy 	}
3432eda14cbcSMatt Macy 
3433eda14cbcSMatt Macy 	/*
3434eda14cbcSMatt Macy 	 * In order to return the full list of conflicting snapshots, we check
3435eda14cbcSMatt Macy 	 * whether there was a conflict after traversing all of them.
3436eda14cbcSMatt Macy 	 */
3437eda14cbcSMatt Macy 	if (conflicting_snaps) {
3438eda14cbcSMatt Macy 		err = SET_ERROR(EEXIST);
3439eda14cbcSMatt Macy 		goto out;
3440eda14cbcSMatt Macy 	}
3441eda14cbcSMatt Macy 
3442eda14cbcSMatt Macy 	/*
3443eda14cbcSMatt Macy 	 * If we are a clone of a clone then we never reached ORIGIN,
3444eda14cbcSMatt Macy 	 * so we need to subtract out the clone origin's used space.
3445eda14cbcSMatt Macy 	 */
3446eda14cbcSMatt Macy 	if (ddpa->origin_origin) {
3447eda14cbcSMatt Macy 		ddpa->used -=
3448eda14cbcSMatt Macy 		    dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes;
3449eda14cbcSMatt Macy 		ddpa->comp -=
3450eda14cbcSMatt Macy 		    dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes;
3451eda14cbcSMatt Macy 		ddpa->uncomp -=
3452eda14cbcSMatt Macy 		    dsl_dataset_phys(ddpa->origin_origin)->
3453eda14cbcSMatt Macy 		    ds_uncompressed_bytes;
3454eda14cbcSMatt Macy 	}
3455eda14cbcSMatt Macy 
3456eda14cbcSMatt Macy 	/* Check that there is enough space and limit headroom here */
3457eda14cbcSMatt Macy 	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
3458eda14cbcSMatt Macy 	    0, ss_mv_cnt, ddpa->used, ddpa->cr, ddpa->proc);
3459eda14cbcSMatt Macy 	if (err != 0)
3460eda14cbcSMatt Macy 		goto out;
3461eda14cbcSMatt Macy 
3462eda14cbcSMatt Macy 	/*
3463eda14cbcSMatt Macy 	 * Compute the amounts of space that will be used by snapshots
3464eda14cbcSMatt Macy 	 * after the promotion (for both origin and clone).  For each,
3465eda14cbcSMatt Macy 	 * it is the amount of space that will be on all of their
3466eda14cbcSMatt Macy 	 * deadlists (that was not born before their new origin).
3467eda14cbcSMatt Macy 	 */
3468eda14cbcSMatt Macy 	if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
3469eda14cbcSMatt Macy 		uint64_t space;
3470eda14cbcSMatt Macy 
3471eda14cbcSMatt Macy 		/*
3472eda14cbcSMatt Macy 		 * Note, typically this will not be a clone of a clone,
3473eda14cbcSMatt Macy 		 * so dd_origin_txg will be < TXG_INITIAL, so
3474eda14cbcSMatt Macy 		 * these snaplist_space() -> dsl_deadlist_space_range()
3475eda14cbcSMatt Macy 		 * calls will be fast because they do not have to
3476eda14cbcSMatt Macy 		 * iterate over all bps.
3477eda14cbcSMatt Macy 		 */
3478eda14cbcSMatt Macy 		snap = list_head(&ddpa->origin_snaps);
3479eda14cbcSMatt Macy 		if (snap == NULL) {
3480eda14cbcSMatt Macy 			err = SET_ERROR(ENOENT);
3481eda14cbcSMatt Macy 			goto out;
3482eda14cbcSMatt Macy 		}
3483eda14cbcSMatt Macy 		err = snaplist_space(&ddpa->shared_snaps,
3484eda14cbcSMatt Macy 		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
3485eda14cbcSMatt Macy 		if (err != 0)
3486eda14cbcSMatt Macy 			goto out;
3487eda14cbcSMatt Macy 
3488eda14cbcSMatt Macy 		err = snaplist_space(&ddpa->clone_snaps,
3489eda14cbcSMatt Macy 		    snap->ds->ds_dir->dd_origin_txg, &space);
3490eda14cbcSMatt Macy 		if (err != 0)
3491eda14cbcSMatt Macy 			goto out;
3492eda14cbcSMatt Macy 		ddpa->cloneusedsnap += space;
3493eda14cbcSMatt Macy 	}
3494eda14cbcSMatt Macy 	if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags &
3495eda14cbcSMatt Macy 	    DD_FLAG_USED_BREAKDOWN) {
3496eda14cbcSMatt Macy 		err = snaplist_space(&ddpa->origin_snaps,
3497eda14cbcSMatt Macy 		    dsl_dataset_phys(origin_ds)->ds_creation_txg,
3498eda14cbcSMatt Macy 		    &ddpa->originusedsnap);
3499eda14cbcSMatt Macy 		if (err != 0)
3500eda14cbcSMatt Macy 			goto out;
3501eda14cbcSMatt Macy 	}
3502eda14cbcSMatt Macy 
3503eda14cbcSMatt Macy out:
3504eda14cbcSMatt Macy 	promote_rele(ddpa, FTAG);
3505eda14cbcSMatt Macy 	return (err);
3506eda14cbcSMatt Macy }
3507eda14cbcSMatt Macy 
3508eda14cbcSMatt Macy void
3509eda14cbcSMatt Macy dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
3510eda14cbcSMatt Macy {
3511eda14cbcSMatt Macy 	dsl_dataset_promote_arg_t *ddpa = arg;
3512eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3513eda14cbcSMatt Macy 	dsl_dataset_t *hds;
3514eda14cbcSMatt Macy 	struct promotenode *snap;
3515eda14cbcSMatt Macy 	dsl_dataset_t *origin_ds;
3516eda14cbcSMatt Macy 	dsl_dataset_t *origin_head;
3517eda14cbcSMatt Macy 	dsl_dir_t *dd;
3518eda14cbcSMatt Macy 	dsl_dir_t *odd = NULL;
3519eda14cbcSMatt Macy 	uint64_t oldnext_obj;
3520eda14cbcSMatt Macy 	int64_t delta;
3521eda14cbcSMatt Macy 
3522eda14cbcSMatt Macy 	ASSERT(nvlist_empty(ddpa->err_ds));
3523eda14cbcSMatt Macy 
3524eda14cbcSMatt Macy 	VERIFY0(promote_hold(ddpa, dp, FTAG));
3525eda14cbcSMatt Macy 	hds = ddpa->ddpa_clone;
3526eda14cbcSMatt Macy 
3527eda14cbcSMatt Macy 	ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE);
3528eda14cbcSMatt Macy 
3529eda14cbcSMatt Macy 	snap = list_head(&ddpa->shared_snaps);
3530eda14cbcSMatt Macy 	origin_ds = snap->ds;
3531eda14cbcSMatt Macy 	dd = hds->ds_dir;
3532eda14cbcSMatt Macy 
3533eda14cbcSMatt Macy 	snap = list_head(&ddpa->origin_snaps);
3534eda14cbcSMatt Macy 	origin_head = snap->ds;
3535eda14cbcSMatt Macy 
3536eda14cbcSMatt Macy 	/*
3537eda14cbcSMatt Macy 	 * We need to explicitly open odd, since origin_ds's dd will be
3538eda14cbcSMatt Macy 	 * changing.
3539eda14cbcSMatt Macy 	 */
3540eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
3541eda14cbcSMatt Macy 	    NULL, FTAG, &odd));
3542eda14cbcSMatt Macy 
3543eda14cbcSMatt Macy 	dsl_dataset_promote_crypt_sync(hds->ds_dir, odd, tx);
3544eda14cbcSMatt Macy 
3545eda14cbcSMatt Macy 	/* change origin's next snap */
3546eda14cbcSMatt Macy 	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
3547eda14cbcSMatt Macy 	oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj;
3548eda14cbcSMatt Macy 	snap = list_tail(&ddpa->clone_snaps);
3549eda14cbcSMatt Macy 	ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==,
3550eda14cbcSMatt Macy 	    origin_ds->ds_object);
3551eda14cbcSMatt Macy 	dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object;
3552eda14cbcSMatt Macy 
3553eda14cbcSMatt Macy 	/* change the origin's next clone */
3554eda14cbcSMatt Macy 	if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) {
3555eda14cbcSMatt Macy 		dsl_dataset_remove_from_next_clones(origin_ds,
3556eda14cbcSMatt Macy 		    snap->ds->ds_object, tx);
3557eda14cbcSMatt Macy 		VERIFY0(zap_add_int(dp->dp_meta_objset,
3558eda14cbcSMatt Macy 		    dsl_dataset_phys(origin_ds)->ds_next_clones_obj,
3559eda14cbcSMatt Macy 		    oldnext_obj, tx));
3560eda14cbcSMatt Macy 	}
3561eda14cbcSMatt Macy 
3562eda14cbcSMatt Macy 	/* change origin */
3563eda14cbcSMatt Macy 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
3564eda14cbcSMatt Macy 	ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object);
3565eda14cbcSMatt Macy 	dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj;
3566eda14cbcSMatt Macy 	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
3567eda14cbcSMatt Macy 	dmu_buf_will_dirty(odd->dd_dbuf, tx);
3568eda14cbcSMatt Macy 	dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object;
3569eda14cbcSMatt Macy 	origin_head->ds_dir->dd_origin_txg =
3570eda14cbcSMatt Macy 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
3571eda14cbcSMatt Macy 
3572eda14cbcSMatt Macy 	/* change dd_clone entries */
3573eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3574eda14cbcSMatt Macy 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
3575eda14cbcSMatt Macy 		    dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx));
3576eda14cbcSMatt Macy 		VERIFY0(zap_add_int(dp->dp_meta_objset,
3577eda14cbcSMatt Macy 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3578eda14cbcSMatt Macy 		    hds->ds_object, tx));
3579eda14cbcSMatt Macy 
3580eda14cbcSMatt Macy 		VERIFY0(zap_remove_int(dp->dp_meta_objset,
3581eda14cbcSMatt Macy 		    dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones,
3582eda14cbcSMatt Macy 		    origin_head->ds_object, tx));
3583eda14cbcSMatt Macy 		if (dsl_dir_phys(dd)->dd_clones == 0) {
3584eda14cbcSMatt Macy 			dsl_dir_phys(dd)->dd_clones =
3585eda14cbcSMatt Macy 			    zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES,
3586eda14cbcSMatt Macy 			    DMU_OT_NONE, 0, tx);
3587eda14cbcSMatt Macy 		}
3588eda14cbcSMatt Macy 		VERIFY0(zap_add_int(dp->dp_meta_objset,
3589eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx));
3590eda14cbcSMatt Macy 	}
3591eda14cbcSMatt Macy 
3592eda14cbcSMatt Macy 	/*
3593eda14cbcSMatt Macy 	 * Move bookmarks to this dir.
3594eda14cbcSMatt Macy 	 */
3595eda14cbcSMatt Macy 	dsl_bookmark_node_t *dbn_next;
3596eda14cbcSMatt Macy 	for (dsl_bookmark_node_t *dbn = avl_first(&origin_head->ds_bookmarks);
3597eda14cbcSMatt Macy 	    dbn != NULL && dbn->dbn_phys.zbm_creation_txg <=
3598eda14cbcSMatt Macy 	    dsl_dataset_phys(origin_ds)->ds_creation_txg;
3599eda14cbcSMatt Macy 	    dbn = dbn_next) {
3600eda14cbcSMatt Macy 		dbn_next = AVL_NEXT(&origin_head->ds_bookmarks, dbn);
3601eda14cbcSMatt Macy 
3602eda14cbcSMatt Macy 		avl_remove(&origin_head->ds_bookmarks, dbn);
3603eda14cbcSMatt Macy 		VERIFY0(zap_remove(dp->dp_meta_objset,
3604eda14cbcSMatt Macy 		    origin_head->ds_bookmarks_obj, dbn->dbn_name, tx));
3605eda14cbcSMatt Macy 
3606eda14cbcSMatt Macy 		dsl_bookmark_node_add(hds, dbn, tx);
3607eda14cbcSMatt Macy 	}
3608eda14cbcSMatt Macy 
3609eda14cbcSMatt Macy 	dsl_bookmark_next_changed(hds, origin_ds, tx);
3610eda14cbcSMatt Macy 
3611eda14cbcSMatt Macy 	/* move snapshots to this dir */
3612eda14cbcSMatt Macy 	for (snap = list_head(&ddpa->shared_snaps); snap;
3613eda14cbcSMatt Macy 	    snap = list_next(&ddpa->shared_snaps, snap)) {
3614eda14cbcSMatt Macy 		dsl_dataset_t *ds = snap->ds;
3615eda14cbcSMatt Macy 
3616eda14cbcSMatt Macy 		/*
3617eda14cbcSMatt Macy 		 * Property callbacks are registered to a particular
3618eda14cbcSMatt Macy 		 * dsl_dir.  Since ours is changing, evict the objset
3619eda14cbcSMatt Macy 		 * so that they will be unregistered from the old dsl_dir.
3620eda14cbcSMatt Macy 		 */
3621eda14cbcSMatt Macy 		if (ds->ds_objset) {
3622eda14cbcSMatt Macy 			dmu_objset_evict(ds->ds_objset);
3623eda14cbcSMatt Macy 			ds->ds_objset = NULL;
3624eda14cbcSMatt Macy 		}
3625eda14cbcSMatt Macy 
3626eda14cbcSMatt Macy 		/* move snap name entry */
3627eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_get_snapname(ds));
3628eda14cbcSMatt Macy 		VERIFY0(dsl_dataset_snap_remove(origin_head,
3629eda14cbcSMatt Macy 		    ds->ds_snapname, tx, B_TRUE));
3630eda14cbcSMatt Macy 		VERIFY0(zap_add(dp->dp_meta_objset,
3631eda14cbcSMatt Macy 		    dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname,
3632eda14cbcSMatt Macy 		    8, 1, &ds->ds_object, tx));
3633eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(hds->ds_dir, 1,
3634eda14cbcSMatt Macy 		    DD_FIELD_SNAPSHOT_COUNT, tx);
3635eda14cbcSMatt Macy 
3636eda14cbcSMatt Macy 		/* change containing dsl_dir */
3637eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
3638eda14cbcSMatt Macy 		ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object);
3639eda14cbcSMatt Macy 		dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object;
3640eda14cbcSMatt Macy 		ASSERT3P(ds->ds_dir, ==, odd);
3641eda14cbcSMatt Macy 		dsl_dir_rele(ds->ds_dir, ds);
3642eda14cbcSMatt Macy 		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
3643eda14cbcSMatt Macy 		    NULL, ds, &ds->ds_dir));
3644eda14cbcSMatt Macy 
3645eda14cbcSMatt Macy 		/* move any clone references */
3646eda14cbcSMatt Macy 		if (dsl_dataset_phys(ds)->ds_next_clones_obj &&
3647eda14cbcSMatt Macy 		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
3648eda14cbcSMatt Macy 			zap_cursor_t zc;
3649eda14cbcSMatt Macy 			zap_attribute_t za;
3650eda14cbcSMatt Macy 
3651eda14cbcSMatt Macy 			for (zap_cursor_init(&zc, dp->dp_meta_objset,
3652eda14cbcSMatt Macy 			    dsl_dataset_phys(ds)->ds_next_clones_obj);
3653eda14cbcSMatt Macy 			    zap_cursor_retrieve(&zc, &za) == 0;
3654eda14cbcSMatt Macy 			    zap_cursor_advance(&zc)) {
3655eda14cbcSMatt Macy 				dsl_dataset_t *cnds;
3656eda14cbcSMatt Macy 				uint64_t o;
3657eda14cbcSMatt Macy 
3658eda14cbcSMatt Macy 				if (za.za_first_integer == oldnext_obj) {
3659eda14cbcSMatt Macy 					/*
3660eda14cbcSMatt Macy 					 * We've already moved the
3661eda14cbcSMatt Macy 					 * origin's reference.
3662eda14cbcSMatt Macy 					 */
3663eda14cbcSMatt Macy 					continue;
3664eda14cbcSMatt Macy 				}
3665eda14cbcSMatt Macy 
3666eda14cbcSMatt Macy 				VERIFY0(dsl_dataset_hold_obj(dp,
3667eda14cbcSMatt Macy 				    za.za_first_integer, FTAG, &cnds));
3668eda14cbcSMatt Macy 				o = dsl_dir_phys(cnds->ds_dir)->
3669eda14cbcSMatt Macy 				    dd_head_dataset_obj;
3670eda14cbcSMatt Macy 
3671eda14cbcSMatt Macy 				VERIFY0(zap_remove_int(dp->dp_meta_objset,
3672eda14cbcSMatt Macy 				    dsl_dir_phys(odd)->dd_clones, o, tx));
3673eda14cbcSMatt Macy 				VERIFY0(zap_add_int(dp->dp_meta_objset,
3674eda14cbcSMatt Macy 				    dsl_dir_phys(dd)->dd_clones, o, tx));
3675eda14cbcSMatt Macy 				dsl_dataset_rele(cnds, FTAG);
3676eda14cbcSMatt Macy 			}
3677eda14cbcSMatt Macy 			zap_cursor_fini(&zc);
3678eda14cbcSMatt Macy 		}
3679eda14cbcSMatt Macy 
3680eda14cbcSMatt Macy 		ASSERT(!dsl_prop_hascb(ds));
3681eda14cbcSMatt Macy 	}
3682eda14cbcSMatt Macy 
3683eda14cbcSMatt Macy 	/*
3684eda14cbcSMatt Macy 	 * Change space accounting.
3685eda14cbcSMatt Macy 	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
3686eda14cbcSMatt Macy 	 * both be valid, or both be 0 (resulting in delta == 0).  This
3687eda14cbcSMatt Macy 	 * is true for each of {clone,origin} independently.
3688eda14cbcSMatt Macy 	 */
3689eda14cbcSMatt Macy 
3690eda14cbcSMatt Macy 	delta = ddpa->cloneusedsnap -
3691eda14cbcSMatt Macy 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP];
3692eda14cbcSMatt Macy 	ASSERT3S(delta, >=, 0);
3693eda14cbcSMatt Macy 	ASSERT3U(ddpa->used, >=, delta);
3694eda14cbcSMatt Macy 	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
3695eda14cbcSMatt Macy 	dsl_dir_diduse_space(dd, DD_USED_HEAD,
3696eda14cbcSMatt Macy 	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
3697eda14cbcSMatt Macy 
3698eda14cbcSMatt Macy 	delta = ddpa->originusedsnap -
3699eda14cbcSMatt Macy 	    dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP];
3700eda14cbcSMatt Macy 	ASSERT3S(delta, <=, 0);
3701eda14cbcSMatt Macy 	ASSERT3U(ddpa->used, >=, -delta);
3702eda14cbcSMatt Macy 	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
3703eda14cbcSMatt Macy 	dsl_dir_diduse_space(odd, DD_USED_HEAD,
3704eda14cbcSMatt Macy 	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
3705eda14cbcSMatt Macy 
3706eda14cbcSMatt Macy 	dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique;
3707eda14cbcSMatt Macy 
3708eda14cbcSMatt Macy 	/*
3709eda14cbcSMatt Macy 	 * Since livelists are specific to a clone's origin txg, they
3710eda14cbcSMatt Macy 	 * are no longer accurate. Destroy the livelist from the clone being
3711eda14cbcSMatt Macy 	 * promoted. If the origin dataset is a clone, destroy its livelist
3712eda14cbcSMatt Macy 	 * as well.
3713eda14cbcSMatt Macy 	 */
3714eda14cbcSMatt Macy 	dsl_dir_remove_livelist(dd, tx, B_TRUE);
3715eda14cbcSMatt Macy 	dsl_dir_remove_livelist(odd, tx, B_TRUE);
3716eda14cbcSMatt Macy 
3717eda14cbcSMatt Macy 	/* log history record */
3718eda14cbcSMatt Macy 	spa_history_log_internal_ds(hds, "promote", tx, " ");
3719eda14cbcSMatt Macy 
3720eda14cbcSMatt Macy 	dsl_dir_rele(odd, FTAG);
3721716fd348SMartin Matuska 
3722716fd348SMartin Matuska 	/*
372375e1fea6SMartin Matuska 	 * Transfer common error blocks from old head to new head, before
372475e1fea6SMartin Matuska 	 * calling promote_rele() on ddpa since we need to dereference
372575e1fea6SMartin Matuska 	 * origin_head and hds.
3726716fd348SMartin Matuska 	 */
3727716fd348SMartin Matuska 	if (spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_HEAD_ERRLOG)) {
3728716fd348SMartin Matuska 		uint64_t old_head = origin_head->ds_object;
3729716fd348SMartin Matuska 		uint64_t new_head = hds->ds_object;
3730716fd348SMartin Matuska 		spa_swap_errlog(dp->dp_spa, new_head, old_head, tx);
3731716fd348SMartin Matuska 	}
373275e1fea6SMartin Matuska 
373375e1fea6SMartin Matuska 	promote_rele(ddpa, FTAG);
3734eda14cbcSMatt Macy }
3735eda14cbcSMatt Macy 
3736eda14cbcSMatt Macy /*
3737eda14cbcSMatt Macy  * Make a list of dsl_dataset_t's for the snapshots between first_obj
3738eda14cbcSMatt Macy  * (exclusive) and last_obj (inclusive).  The list will be in reverse
3739eda14cbcSMatt Macy  * order (last_obj will be the list_head()).  If first_obj == 0, do all
3740eda14cbcSMatt Macy  * snapshots back to this dataset's origin.
3741eda14cbcSMatt Macy  */
3742eda14cbcSMatt Macy static int
3743eda14cbcSMatt Macy snaplist_make(dsl_pool_t *dp,
3744a0b956f5SMartin Matuska     uint64_t first_obj, uint64_t last_obj, list_t *l, const void *tag)
3745eda14cbcSMatt Macy {
3746eda14cbcSMatt Macy 	uint64_t obj = last_obj;
3747eda14cbcSMatt Macy 
3748eda14cbcSMatt Macy 	list_create(l, sizeof (struct promotenode),
3749eda14cbcSMatt Macy 	    offsetof(struct promotenode, link));
3750eda14cbcSMatt Macy 
3751eda14cbcSMatt Macy 	while (obj != first_obj) {
3752eda14cbcSMatt Macy 		dsl_dataset_t *ds;
3753eda14cbcSMatt Macy 		struct promotenode *snap;
3754eda14cbcSMatt Macy 		int err;
3755eda14cbcSMatt Macy 
3756eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
3757eda14cbcSMatt Macy 		ASSERT(err != ENOENT);
3758eda14cbcSMatt Macy 		if (err != 0)
3759eda14cbcSMatt Macy 			return (err);
3760eda14cbcSMatt Macy 
3761eda14cbcSMatt Macy 		if (first_obj == 0)
3762eda14cbcSMatt Macy 			first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj;
3763eda14cbcSMatt Macy 
3764eda14cbcSMatt Macy 		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
3765eda14cbcSMatt Macy 		snap->ds = ds;
3766eda14cbcSMatt Macy 		list_insert_tail(l, snap);
3767eda14cbcSMatt Macy 		obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
3768eda14cbcSMatt Macy 	}
3769eda14cbcSMatt Macy 
3770eda14cbcSMatt Macy 	return (0);
3771eda14cbcSMatt Macy }
3772eda14cbcSMatt Macy 
3773eda14cbcSMatt Macy static int
3774eda14cbcSMatt Macy snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
3775eda14cbcSMatt Macy {
3776eda14cbcSMatt Macy 	struct promotenode *snap;
3777eda14cbcSMatt Macy 
3778eda14cbcSMatt Macy 	*spacep = 0;
3779eda14cbcSMatt Macy 	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
3780eda14cbcSMatt Macy 		uint64_t used, comp, uncomp;
3781eda14cbcSMatt Macy 		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
3782eda14cbcSMatt Macy 		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
3783eda14cbcSMatt Macy 		*spacep += used;
3784eda14cbcSMatt Macy 	}
3785eda14cbcSMatt Macy 	return (0);
3786eda14cbcSMatt Macy }
3787eda14cbcSMatt Macy 
3788eda14cbcSMatt Macy static void
3789a0b956f5SMartin Matuska snaplist_destroy(list_t *l, const void *tag)
3790eda14cbcSMatt Macy {
3791eda14cbcSMatt Macy 	struct promotenode *snap;
3792eda14cbcSMatt Macy 
3793eda14cbcSMatt Macy 	if (l == NULL || !list_link_active(&l->list_head))
3794eda14cbcSMatt Macy 		return;
3795eda14cbcSMatt Macy 
37964e8d558cSMartin Matuska 	while ((snap = list_remove_tail(l)) != NULL) {
3797eda14cbcSMatt Macy 		dsl_dataset_rele(snap->ds, tag);
3798eda14cbcSMatt Macy 		kmem_free(snap, sizeof (*snap));
3799eda14cbcSMatt Macy 	}
3800eda14cbcSMatt Macy 	list_destroy(l);
3801eda14cbcSMatt Macy }
3802eda14cbcSMatt Macy 
3803eda14cbcSMatt Macy static int
3804a0b956f5SMartin Matuska promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, const void *tag)
3805eda14cbcSMatt Macy {
3806eda14cbcSMatt Macy 	int error;
3807eda14cbcSMatt Macy 	dsl_dir_t *dd;
3808eda14cbcSMatt Macy 	struct promotenode *snap;
3809eda14cbcSMatt Macy 
3810eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
3811eda14cbcSMatt Macy 	    &ddpa->ddpa_clone);
3812eda14cbcSMatt Macy 	if (error != 0)
3813eda14cbcSMatt Macy 		return (error);
3814eda14cbcSMatt Macy 	dd = ddpa->ddpa_clone->ds_dir;
3815eda14cbcSMatt Macy 
3816eda14cbcSMatt Macy 	if (ddpa->ddpa_clone->ds_is_snapshot ||
3817eda14cbcSMatt Macy 	    !dsl_dir_is_clone(dd)) {
3818eda14cbcSMatt Macy 		dsl_dataset_rele(ddpa->ddpa_clone, tag);
3819eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3820eda14cbcSMatt Macy 	}
3821eda14cbcSMatt Macy 
3822eda14cbcSMatt Macy 	error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj,
3823eda14cbcSMatt Macy 	    &ddpa->shared_snaps, tag);
3824eda14cbcSMatt Macy 	if (error != 0)
3825eda14cbcSMatt Macy 		goto out;
3826eda14cbcSMatt Macy 
3827eda14cbcSMatt Macy 	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
3828eda14cbcSMatt Macy 	    &ddpa->clone_snaps, tag);
3829eda14cbcSMatt Macy 	if (error != 0)
3830eda14cbcSMatt Macy 		goto out;
3831eda14cbcSMatt Macy 
3832eda14cbcSMatt Macy 	snap = list_head(&ddpa->shared_snaps);
3833eda14cbcSMatt Macy 	ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj);
3834eda14cbcSMatt Macy 	error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj,
3835eda14cbcSMatt Macy 	    dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj,
3836eda14cbcSMatt Macy 	    &ddpa->origin_snaps, tag);
3837eda14cbcSMatt Macy 	if (error != 0)
3838eda14cbcSMatt Macy 		goto out;
3839eda14cbcSMatt Macy 
3840eda14cbcSMatt Macy 	if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) {
3841eda14cbcSMatt Macy 		error = dsl_dataset_hold_obj(dp,
3842eda14cbcSMatt Macy 		    dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj,
3843eda14cbcSMatt Macy 		    tag, &ddpa->origin_origin);
3844eda14cbcSMatt Macy 		if (error != 0)
3845eda14cbcSMatt Macy 			goto out;
3846eda14cbcSMatt Macy 	}
3847eda14cbcSMatt Macy out:
3848eda14cbcSMatt Macy 	if (error != 0)
3849eda14cbcSMatt Macy 		promote_rele(ddpa, tag);
3850eda14cbcSMatt Macy 	return (error);
3851eda14cbcSMatt Macy }
3852eda14cbcSMatt Macy 
3853eda14cbcSMatt Macy static void
3854a0b956f5SMartin Matuska promote_rele(dsl_dataset_promote_arg_t *ddpa, const void *tag)
3855eda14cbcSMatt Macy {
3856eda14cbcSMatt Macy 	snaplist_destroy(&ddpa->shared_snaps, tag);
3857eda14cbcSMatt Macy 	snaplist_destroy(&ddpa->clone_snaps, tag);
3858eda14cbcSMatt Macy 	snaplist_destroy(&ddpa->origin_snaps, tag);
3859eda14cbcSMatt Macy 	if (ddpa->origin_origin != NULL)
3860eda14cbcSMatt Macy 		dsl_dataset_rele(ddpa->origin_origin, tag);
3861eda14cbcSMatt Macy 	dsl_dataset_rele(ddpa->ddpa_clone, tag);
3862eda14cbcSMatt Macy }
3863eda14cbcSMatt Macy 
3864eda14cbcSMatt Macy /*
3865eda14cbcSMatt Macy  * Promote a clone.
3866eda14cbcSMatt Macy  *
3867eda14cbcSMatt Macy  * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
3868eda14cbcSMatt Macy  * in with the name.  (It must be at least ZFS_MAX_DATASET_NAME_LEN bytes long.)
3869eda14cbcSMatt Macy  */
3870eda14cbcSMatt Macy int
3871eda14cbcSMatt Macy dsl_dataset_promote(const char *name, char *conflsnap)
3872eda14cbcSMatt Macy {
3873eda14cbcSMatt Macy 	dsl_dataset_promote_arg_t ddpa = { 0 };
3874eda14cbcSMatt Macy 	uint64_t numsnaps;
3875eda14cbcSMatt Macy 	int error;
3876eda14cbcSMatt Macy 	nvpair_t *snap_pair;
3877eda14cbcSMatt Macy 	objset_t *os;
3878eda14cbcSMatt Macy 
3879eda14cbcSMatt Macy 	/*
3880eda14cbcSMatt Macy 	 * We will modify space proportional to the number of
3881eda14cbcSMatt Macy 	 * snapshots.  Compute numsnaps.
3882eda14cbcSMatt Macy 	 */
3883eda14cbcSMatt Macy 	error = dmu_objset_hold(name, FTAG, &os);
3884eda14cbcSMatt Macy 	if (error != 0)
3885eda14cbcSMatt Macy 		return (error);
3886eda14cbcSMatt Macy 	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
3887eda14cbcSMatt Macy 	    dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
3888eda14cbcSMatt Macy 	    &numsnaps);
3889eda14cbcSMatt Macy 	dmu_objset_rele(os, FTAG);
3890eda14cbcSMatt Macy 	if (error != 0)
3891eda14cbcSMatt Macy 		return (error);
3892eda14cbcSMatt Macy 
3893eda14cbcSMatt Macy 	ddpa.ddpa_clonename = name;
3894eda14cbcSMatt Macy 	ddpa.err_ds = fnvlist_alloc();
3895eda14cbcSMatt Macy 	ddpa.cr = CRED();
3896eda14cbcSMatt Macy 	ddpa.proc = curproc;
3897eda14cbcSMatt Macy 
3898eda14cbcSMatt Macy 	error = dsl_sync_task(name, dsl_dataset_promote_check,
3899eda14cbcSMatt Macy 	    dsl_dataset_promote_sync, &ddpa,
3900eda14cbcSMatt Macy 	    2 + numsnaps, ZFS_SPACE_CHECK_RESERVED);
3901eda14cbcSMatt Macy 
3902eda14cbcSMatt Macy 	/*
3903eda14cbcSMatt Macy 	 * Return the first conflicting snapshot found.
3904eda14cbcSMatt Macy 	 */
3905eda14cbcSMatt Macy 	snap_pair = nvlist_next_nvpair(ddpa.err_ds, NULL);
3906eda14cbcSMatt Macy 	if (snap_pair != NULL && conflsnap != NULL)
3907eda14cbcSMatt Macy 		(void) strlcpy(conflsnap, nvpair_name(snap_pair),
3908eda14cbcSMatt Macy 		    ZFS_MAX_DATASET_NAME_LEN);
3909eda14cbcSMatt Macy 
3910eda14cbcSMatt Macy 	fnvlist_free(ddpa.err_ds);
3911eda14cbcSMatt Macy 	return (error);
3912eda14cbcSMatt Macy }
3913eda14cbcSMatt Macy 
3914eda14cbcSMatt Macy int
3915eda14cbcSMatt Macy dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
3916eda14cbcSMatt Macy     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
3917eda14cbcSMatt Macy {
3918eda14cbcSMatt Macy 	/*
3919eda14cbcSMatt Macy 	 * "slack" factor for received datasets with refquota set on them.
3920eda14cbcSMatt Macy 	 * See the bottom of this function for details on its use.
3921eda14cbcSMatt Macy 	 */
3922eda14cbcSMatt Macy 	uint64_t refquota_slack = (uint64_t)DMU_MAX_ACCESS *
3923eda14cbcSMatt Macy 	    spa_asize_inflation;
3924eda14cbcSMatt Macy 	int64_t unused_refres_delta;
3925eda14cbcSMatt Macy 
3926eda14cbcSMatt Macy 	/* they should both be heads */
3927eda14cbcSMatt Macy 	if (clone->ds_is_snapshot ||
3928eda14cbcSMatt Macy 	    origin_head->ds_is_snapshot)
3929eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3930eda14cbcSMatt Macy 
3931eda14cbcSMatt Macy 	/* if we are not forcing, the branch point should be just before them */
3932eda14cbcSMatt Macy 	if (!force && clone->ds_prev != origin_head->ds_prev)
3933eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3934eda14cbcSMatt Macy 
3935eda14cbcSMatt Macy 	/* clone should be the clone (unless they are unrelated) */
3936eda14cbcSMatt Macy 	if (clone->ds_prev != NULL &&
3937eda14cbcSMatt Macy 	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
3938eda14cbcSMatt Macy 	    origin_head->ds_dir != clone->ds_prev->ds_dir)
3939eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3940eda14cbcSMatt Macy 
3941eda14cbcSMatt Macy 	/* the clone should be a child of the origin */
3942eda14cbcSMatt Macy 	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
3943eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
3944eda14cbcSMatt Macy 
3945eda14cbcSMatt Macy 	/* origin_head shouldn't be modified unless 'force' */
3946eda14cbcSMatt Macy 	if (!force &&
3947eda14cbcSMatt Macy 	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
3948eda14cbcSMatt Macy 		return (SET_ERROR(ETXTBSY));
3949eda14cbcSMatt Macy 
3950eda14cbcSMatt Macy 	/* origin_head should have no long holds (e.g. is not mounted) */
3951eda14cbcSMatt Macy 	if (dsl_dataset_handoff_check(origin_head, owner, tx))
3952eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
3953eda14cbcSMatt Macy 
3954eda14cbcSMatt Macy 	/* check amount of any unconsumed refreservation */
3955eda14cbcSMatt Macy 	unused_refres_delta =
3956eda14cbcSMatt Macy 	    (int64_t)MIN(origin_head->ds_reserved,
3957eda14cbcSMatt Macy 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
3958eda14cbcSMatt Macy 	    (int64_t)MIN(origin_head->ds_reserved,
3959eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_unique_bytes);
3960eda14cbcSMatt Macy 
3961eda14cbcSMatt Macy 	if (unused_refres_delta > 0 &&
3962eda14cbcSMatt Macy 	    unused_refres_delta >
3963eda14cbcSMatt Macy 	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
3964eda14cbcSMatt Macy 		return (SET_ERROR(ENOSPC));
3965eda14cbcSMatt Macy 
3966eda14cbcSMatt Macy 	/*
3967eda14cbcSMatt Macy 	 * The clone can't be too much over the head's refquota.
3968eda14cbcSMatt Macy 	 *
3969eda14cbcSMatt Macy 	 * To ensure that the entire refquota can be used, we allow one
3970eda14cbcSMatt Macy 	 * transaction to exceed the refquota.  Therefore, this check
3971eda14cbcSMatt Macy 	 * needs to also allow for the space referenced to be more than the
3972eda14cbcSMatt Macy 	 * refquota.  The maximum amount of space that one transaction can use
3973eda14cbcSMatt Macy 	 * on disk is DMU_MAX_ACCESS * spa_asize_inflation.  Allowing this
3974eda14cbcSMatt Macy 	 * overage ensures that we are able to receive a filesystem that
3975eda14cbcSMatt Macy 	 * exceeds the refquota on the source system.
3976eda14cbcSMatt Macy 	 *
3977eda14cbcSMatt Macy 	 * So that overage is the refquota_slack we use below.
3978eda14cbcSMatt Macy 	 */
3979eda14cbcSMatt Macy 	if (origin_head->ds_quota != 0 &&
3980eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_referenced_bytes >
3981eda14cbcSMatt Macy 	    origin_head->ds_quota + refquota_slack)
3982eda14cbcSMatt Macy 		return (SET_ERROR(EDQUOT));
3983eda14cbcSMatt Macy 
3984eda14cbcSMatt Macy 	return (0);
3985eda14cbcSMatt Macy }
3986eda14cbcSMatt Macy 
3987eda14cbcSMatt Macy static void
3988eda14cbcSMatt Macy dsl_dataset_swap_remap_deadlists(dsl_dataset_t *clone,
3989eda14cbcSMatt Macy     dsl_dataset_t *origin, dmu_tx_t *tx)
3990eda14cbcSMatt Macy {
3991eda14cbcSMatt Macy 	uint64_t clone_remap_dl_obj, origin_remap_dl_obj;
3992eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
3993eda14cbcSMatt Macy 
3994eda14cbcSMatt Macy 	ASSERT(dsl_pool_sync_context(dp));
3995eda14cbcSMatt Macy 
3996eda14cbcSMatt Macy 	clone_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(clone);
3997eda14cbcSMatt Macy 	origin_remap_dl_obj = dsl_dataset_get_remap_deadlist_object(origin);
3998eda14cbcSMatt Macy 
3999eda14cbcSMatt Macy 	if (clone_remap_dl_obj != 0) {
4000eda14cbcSMatt Macy 		dsl_deadlist_close(&clone->ds_remap_deadlist);
4001eda14cbcSMatt Macy 		dsl_dataset_unset_remap_deadlist_object(clone, tx);
4002eda14cbcSMatt Macy 	}
4003eda14cbcSMatt Macy 	if (origin_remap_dl_obj != 0) {
4004eda14cbcSMatt Macy 		dsl_deadlist_close(&origin->ds_remap_deadlist);
4005eda14cbcSMatt Macy 		dsl_dataset_unset_remap_deadlist_object(origin, tx);
4006eda14cbcSMatt Macy 	}
4007eda14cbcSMatt Macy 
4008eda14cbcSMatt Macy 	if (clone_remap_dl_obj != 0) {
4009eda14cbcSMatt Macy 		dsl_dataset_set_remap_deadlist_object(origin,
4010eda14cbcSMatt Macy 		    clone_remap_dl_obj, tx);
4011eda14cbcSMatt Macy 		dsl_deadlist_open(&origin->ds_remap_deadlist,
4012eda14cbcSMatt Macy 		    dp->dp_meta_objset, clone_remap_dl_obj);
4013eda14cbcSMatt Macy 	}
4014eda14cbcSMatt Macy 	if (origin_remap_dl_obj != 0) {
4015eda14cbcSMatt Macy 		dsl_dataset_set_remap_deadlist_object(clone,
4016eda14cbcSMatt Macy 		    origin_remap_dl_obj, tx);
4017eda14cbcSMatt Macy 		dsl_deadlist_open(&clone->ds_remap_deadlist,
4018eda14cbcSMatt Macy 		    dp->dp_meta_objset, origin_remap_dl_obj);
4019eda14cbcSMatt Macy 	}
4020eda14cbcSMatt Macy }
4021eda14cbcSMatt Macy 
4022eda14cbcSMatt Macy void
4023eda14cbcSMatt Macy dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
4024eda14cbcSMatt Macy     dsl_dataset_t *origin_head, dmu_tx_t *tx)
4025eda14cbcSMatt Macy {
4026eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
4027eda14cbcSMatt Macy 	int64_t unused_refres_delta;
4028eda14cbcSMatt Macy 
4029eda14cbcSMatt Macy 	ASSERT(clone->ds_reserved == 0);
4030eda14cbcSMatt Macy 	/*
4031eda14cbcSMatt Macy 	 * NOTE: On DEBUG kernels there could be a race between this and
4032eda14cbcSMatt Macy 	 * the check function if spa_asize_inflation is adjusted...
4033eda14cbcSMatt Macy 	 */
4034eda14cbcSMatt Macy 	ASSERT(origin_head->ds_quota == 0 ||
4035eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
4036eda14cbcSMatt Macy 	    DMU_MAX_ACCESS * spa_asize_inflation);
4037eda14cbcSMatt Macy 	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
4038eda14cbcSMatt Macy 
4039eda14cbcSMatt Macy 	dsl_dir_cancel_waiters(origin_head->ds_dir);
4040eda14cbcSMatt Macy 
4041eda14cbcSMatt Macy 	/*
4042eda14cbcSMatt Macy 	 * Swap per-dataset feature flags.
4043eda14cbcSMatt Macy 	 */
4044eda14cbcSMatt Macy 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
4045eda14cbcSMatt Macy 		if (!(spa_feature_table[f].fi_flags &
4046eda14cbcSMatt Macy 		    ZFEATURE_FLAG_PER_DATASET)) {
4047eda14cbcSMatt Macy 			ASSERT(!dsl_dataset_feature_is_active(clone, f));
4048eda14cbcSMatt Macy 			ASSERT(!dsl_dataset_feature_is_active(origin_head, f));
4049eda14cbcSMatt Macy 			continue;
4050eda14cbcSMatt Macy 		}
4051eda14cbcSMatt Macy 
4052eda14cbcSMatt Macy 		boolean_t clone_inuse = dsl_dataset_feature_is_active(clone, f);
4053eda14cbcSMatt Macy 		void *clone_feature = clone->ds_feature[f];
4054eda14cbcSMatt Macy 		boolean_t origin_head_inuse =
4055eda14cbcSMatt Macy 		    dsl_dataset_feature_is_active(origin_head, f);
4056eda14cbcSMatt Macy 		void *origin_head_feature = origin_head->ds_feature[f];
4057eda14cbcSMatt Macy 
4058eda14cbcSMatt Macy 		if (clone_inuse)
4059eda14cbcSMatt Macy 			dsl_dataset_deactivate_feature_impl(clone, f, tx);
4060eda14cbcSMatt Macy 		if (origin_head_inuse)
4061eda14cbcSMatt Macy 			dsl_dataset_deactivate_feature_impl(origin_head, f, tx);
4062eda14cbcSMatt Macy 
4063eda14cbcSMatt Macy 		if (clone_inuse) {
4064eda14cbcSMatt Macy 			dsl_dataset_activate_feature(origin_head->ds_object, f,
4065eda14cbcSMatt Macy 			    clone_feature, tx);
4066eda14cbcSMatt Macy 			origin_head->ds_feature[f] = clone_feature;
4067eda14cbcSMatt Macy 		}
4068eda14cbcSMatt Macy 		if (origin_head_inuse) {
4069eda14cbcSMatt Macy 			dsl_dataset_activate_feature(clone->ds_object, f,
4070eda14cbcSMatt Macy 			    origin_head_feature, tx);
4071eda14cbcSMatt Macy 			clone->ds_feature[f] = origin_head_feature;
4072eda14cbcSMatt Macy 		}
4073eda14cbcSMatt Macy 	}
4074eda14cbcSMatt Macy 
4075eda14cbcSMatt Macy 	dmu_buf_will_dirty(clone->ds_dbuf, tx);
4076eda14cbcSMatt Macy 	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
4077eda14cbcSMatt Macy 
4078eda14cbcSMatt Macy 	if (clone->ds_objset != NULL) {
4079eda14cbcSMatt Macy 		dmu_objset_evict(clone->ds_objset);
4080eda14cbcSMatt Macy 		clone->ds_objset = NULL;
4081eda14cbcSMatt Macy 	}
4082eda14cbcSMatt Macy 
4083eda14cbcSMatt Macy 	if (origin_head->ds_objset != NULL) {
4084eda14cbcSMatt Macy 		dmu_objset_evict(origin_head->ds_objset);
4085eda14cbcSMatt Macy 		origin_head->ds_objset = NULL;
4086eda14cbcSMatt Macy 	}
4087eda14cbcSMatt Macy 
4088eda14cbcSMatt Macy 	unused_refres_delta =
4089eda14cbcSMatt Macy 	    (int64_t)MIN(origin_head->ds_reserved,
4090eda14cbcSMatt Macy 	    dsl_dataset_phys(origin_head)->ds_unique_bytes) -
4091eda14cbcSMatt Macy 	    (int64_t)MIN(origin_head->ds_reserved,
4092eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_unique_bytes);
4093eda14cbcSMatt Macy 
4094eda14cbcSMatt Macy 	/*
4095eda14cbcSMatt Macy 	 * Reset origin's unique bytes.
4096eda14cbcSMatt Macy 	 */
4097eda14cbcSMatt Macy 	{
4098eda14cbcSMatt Macy 		dsl_dataset_t *origin = clone->ds_prev;
4099eda14cbcSMatt Macy 		uint64_t comp, uncomp;
4100eda14cbcSMatt Macy 
4101eda14cbcSMatt Macy 		dmu_buf_will_dirty(origin->ds_dbuf, tx);
4102eda14cbcSMatt Macy 		dsl_deadlist_space_range(&clone->ds_deadlist,
4103eda14cbcSMatt Macy 		    dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX,
4104eda14cbcSMatt Macy 		    &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp);
4105eda14cbcSMatt Macy 	}
4106eda14cbcSMatt Macy 
4107eda14cbcSMatt Macy 	/* swap blkptrs */
4108eda14cbcSMatt Macy 	{
4109eda14cbcSMatt Macy 		rrw_enter(&clone->ds_bp_rwlock, RW_WRITER, FTAG);
4110eda14cbcSMatt Macy 		rrw_enter(&origin_head->ds_bp_rwlock, RW_WRITER, FTAG);
4111eda14cbcSMatt Macy 		blkptr_t tmp;
4112eda14cbcSMatt Macy 		tmp = dsl_dataset_phys(origin_head)->ds_bp;
4113eda14cbcSMatt Macy 		dsl_dataset_phys(origin_head)->ds_bp =
4114eda14cbcSMatt Macy 		    dsl_dataset_phys(clone)->ds_bp;
4115eda14cbcSMatt Macy 		dsl_dataset_phys(clone)->ds_bp = tmp;
4116eda14cbcSMatt Macy 		rrw_exit(&origin_head->ds_bp_rwlock, FTAG);
4117eda14cbcSMatt Macy 		rrw_exit(&clone->ds_bp_rwlock, FTAG);
4118eda14cbcSMatt Macy 	}
4119eda14cbcSMatt Macy 
4120eda14cbcSMatt Macy 	/* set dd_*_bytes */
4121eda14cbcSMatt Macy 	{
4122eda14cbcSMatt Macy 		int64_t dused, dcomp, duncomp;
4123eda14cbcSMatt Macy 		uint64_t cdl_used, cdl_comp, cdl_uncomp;
4124eda14cbcSMatt Macy 		uint64_t odl_used, odl_comp, odl_uncomp;
4125eda14cbcSMatt Macy 
4126eda14cbcSMatt Macy 		ASSERT3U(dsl_dir_phys(clone->ds_dir)->
4127eda14cbcSMatt Macy 		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
4128eda14cbcSMatt Macy 
4129eda14cbcSMatt Macy 		dsl_deadlist_space(&clone->ds_deadlist,
4130eda14cbcSMatt Macy 		    &cdl_used, &cdl_comp, &cdl_uncomp);
4131eda14cbcSMatt Macy 		dsl_deadlist_space(&origin_head->ds_deadlist,
4132eda14cbcSMatt Macy 		    &odl_used, &odl_comp, &odl_uncomp);
4133eda14cbcSMatt Macy 
4134eda14cbcSMatt Macy 		dused = dsl_dataset_phys(clone)->ds_referenced_bytes +
4135eda14cbcSMatt Macy 		    cdl_used -
4136eda14cbcSMatt Macy 		    (dsl_dataset_phys(origin_head)->ds_referenced_bytes +
4137eda14cbcSMatt Macy 		    odl_used);
4138eda14cbcSMatt Macy 		dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes +
4139eda14cbcSMatt Macy 		    cdl_comp -
4140eda14cbcSMatt Macy 		    (dsl_dataset_phys(origin_head)->ds_compressed_bytes +
4141eda14cbcSMatt Macy 		    odl_comp);
4142eda14cbcSMatt Macy 		duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes +
4143eda14cbcSMatt Macy 		    cdl_uncomp -
4144eda14cbcSMatt Macy 		    (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes +
4145eda14cbcSMatt Macy 		    odl_uncomp);
4146eda14cbcSMatt Macy 
4147eda14cbcSMatt Macy 		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
4148eda14cbcSMatt Macy 		    dused, dcomp, duncomp, tx);
4149eda14cbcSMatt Macy 		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
4150eda14cbcSMatt Macy 		    -dused, -dcomp, -duncomp, tx);
4151eda14cbcSMatt Macy 
4152eda14cbcSMatt Macy 		/*
4153eda14cbcSMatt Macy 		 * The difference in the space used by snapshots is the
4154eda14cbcSMatt Macy 		 * difference in snapshot space due to the head's
4155eda14cbcSMatt Macy 		 * deadlist (since that's the only thing that's
4156eda14cbcSMatt Macy 		 * changing that affects the snapused).
4157eda14cbcSMatt Macy 		 */
4158eda14cbcSMatt Macy 		dsl_deadlist_space_range(&clone->ds_deadlist,
4159eda14cbcSMatt Macy 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
4160eda14cbcSMatt Macy 		    &cdl_used, &cdl_comp, &cdl_uncomp);
4161eda14cbcSMatt Macy 		dsl_deadlist_space_range(&origin_head->ds_deadlist,
4162eda14cbcSMatt Macy 		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
4163eda14cbcSMatt Macy 		    &odl_used, &odl_comp, &odl_uncomp);
4164eda14cbcSMatt Macy 		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
4165eda14cbcSMatt Macy 		    DD_USED_HEAD, DD_USED_SNAP, tx);
4166eda14cbcSMatt Macy 	}
4167eda14cbcSMatt Macy 
4168eda14cbcSMatt Macy 	/* swap ds_*_bytes */
4169eda14cbcSMatt Macy 	SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes,
4170eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_referenced_bytes);
4171eda14cbcSMatt Macy 	SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes,
4172eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_compressed_bytes);
4173eda14cbcSMatt Macy 	SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes,
4174eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_uncompressed_bytes);
4175eda14cbcSMatt Macy 	SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes,
4176eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_unique_bytes);
4177eda14cbcSMatt Macy 
4178eda14cbcSMatt Macy 	/* apply any parent delta for change in unconsumed refreservation */
4179eda14cbcSMatt Macy 	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
4180eda14cbcSMatt Macy 	    unused_refres_delta, 0, 0, tx);
4181eda14cbcSMatt Macy 
4182eda14cbcSMatt Macy 	/*
4183eda14cbcSMatt Macy 	 * Swap deadlists.
4184eda14cbcSMatt Macy 	 */
4185eda14cbcSMatt Macy 	dsl_deadlist_close(&clone->ds_deadlist);
4186eda14cbcSMatt Macy 	dsl_deadlist_close(&origin_head->ds_deadlist);
4187eda14cbcSMatt Macy 	SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj,
4188eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
4189eda14cbcSMatt Macy 	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
4190eda14cbcSMatt Macy 	    dsl_dataset_phys(clone)->ds_deadlist_obj);
4191eda14cbcSMatt Macy 	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
4192eda14cbcSMatt Macy 	    dsl_dataset_phys(origin_head)->ds_deadlist_obj);
4193eda14cbcSMatt Macy 	dsl_dataset_swap_remap_deadlists(clone, origin_head, tx);
4194eda14cbcSMatt Macy 
4195eda14cbcSMatt Macy 	/*
4196eda14cbcSMatt Macy 	 * If there is a bookmark at the origin, its "next dataset" is
4197eda14cbcSMatt Macy 	 * changing, so we need to reset its FBN.
4198eda14cbcSMatt Macy 	 */
4199eda14cbcSMatt Macy 	dsl_bookmark_next_changed(origin_head, origin_head->ds_prev, tx);
4200eda14cbcSMatt Macy 
4201eda14cbcSMatt Macy 	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
4202eda14cbcSMatt Macy 
4203eda14cbcSMatt Macy 	/*
4204eda14cbcSMatt Macy 	 * Destroy any livelists associated with the clone or the origin,
4205eda14cbcSMatt Macy 	 * since after the swap the corresponding livelists are no longer
4206eda14cbcSMatt Macy 	 * valid.
4207eda14cbcSMatt Macy 	 */
4208eda14cbcSMatt Macy 	dsl_dir_remove_livelist(clone->ds_dir, tx, B_TRUE);
4209eda14cbcSMatt Macy 	dsl_dir_remove_livelist(origin_head->ds_dir, tx, B_TRUE);
4210eda14cbcSMatt Macy 
4211eda14cbcSMatt Macy 	spa_history_log_internal_ds(clone, "clone swap", tx,
4212eda14cbcSMatt Macy 	    "parent=%s", origin_head->ds_dir->dd_myname);
4213eda14cbcSMatt Macy }
4214eda14cbcSMatt Macy 
4215eda14cbcSMatt Macy /*
4216eda14cbcSMatt Macy  * Given a pool name and a dataset object number in that pool,
4217eda14cbcSMatt Macy  * return the name of that dataset.
4218eda14cbcSMatt Macy  */
4219eda14cbcSMatt Macy int
4220eda14cbcSMatt Macy dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
4221eda14cbcSMatt Macy {
4222eda14cbcSMatt Macy 	dsl_pool_t *dp;
4223eda14cbcSMatt Macy 	dsl_dataset_t *ds;
4224eda14cbcSMatt Macy 	int error;
4225eda14cbcSMatt Macy 
4226eda14cbcSMatt Macy 	error = dsl_pool_hold(pname, FTAG, &dp);
4227eda14cbcSMatt Macy 	if (error != 0)
4228eda14cbcSMatt Macy 		return (error);
4229eda14cbcSMatt Macy 
4230eda14cbcSMatt Macy 	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
4231eda14cbcSMatt Macy 	if (error == 0) {
4232eda14cbcSMatt Macy 		dsl_dataset_name(ds, buf);
4233eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4234eda14cbcSMatt Macy 	}
4235eda14cbcSMatt Macy 	dsl_pool_rele(dp, FTAG);
4236eda14cbcSMatt Macy 
4237eda14cbcSMatt Macy 	return (error);
4238eda14cbcSMatt Macy }
4239eda14cbcSMatt Macy 
4240eda14cbcSMatt Macy int
4241eda14cbcSMatt Macy dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
4242eda14cbcSMatt Macy     uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
4243eda14cbcSMatt Macy {
4244eda14cbcSMatt Macy 	int error = 0;
4245eda14cbcSMatt Macy 
4246eda14cbcSMatt Macy 	ASSERT3S(asize, >, 0);
4247eda14cbcSMatt Macy 
4248eda14cbcSMatt Macy 	/*
4249eda14cbcSMatt Macy 	 * *ref_rsrv is the portion of asize that will come from any
4250eda14cbcSMatt Macy 	 * unconsumed refreservation space.
4251eda14cbcSMatt Macy 	 */
4252eda14cbcSMatt Macy 	*ref_rsrv = 0;
4253eda14cbcSMatt Macy 
4254eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
4255eda14cbcSMatt Macy 	/*
4256eda14cbcSMatt Macy 	 * Make a space adjustment for reserved bytes.
4257eda14cbcSMatt Macy 	 */
4258eda14cbcSMatt Macy 	if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) {
4259eda14cbcSMatt Macy 		ASSERT3U(*used, >=,
4260eda14cbcSMatt Macy 		    ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
4261eda14cbcSMatt Macy 		*used -=
4262eda14cbcSMatt Macy 		    (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes);
4263eda14cbcSMatt Macy 		*ref_rsrv =
4264eda14cbcSMatt Macy 		    asize - MIN(asize, parent_delta(ds, asize + inflight));
4265eda14cbcSMatt Macy 	}
4266eda14cbcSMatt Macy 
4267eda14cbcSMatt Macy 	if (!check_quota || ds->ds_quota == 0) {
4268eda14cbcSMatt Macy 		mutex_exit(&ds->ds_lock);
4269eda14cbcSMatt Macy 		return (0);
4270eda14cbcSMatt Macy 	}
4271eda14cbcSMatt Macy 	/*
4272eda14cbcSMatt Macy 	 * If they are requesting more space, and our current estimate
4273eda14cbcSMatt Macy 	 * is over quota, they get to try again unless the actual
4274eda14cbcSMatt Macy 	 * on-disk is over quota and there are no pending changes (which
4275eda14cbcSMatt Macy 	 * may free up space for us).
4276eda14cbcSMatt Macy 	 */
4277eda14cbcSMatt Macy 	if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >=
4278eda14cbcSMatt Macy 	    ds->ds_quota) {
4279eda14cbcSMatt Macy 		if (inflight > 0 ||
4280eda14cbcSMatt Macy 		    dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota)
4281eda14cbcSMatt Macy 			error = SET_ERROR(ERESTART);
4282eda14cbcSMatt Macy 		else
4283eda14cbcSMatt Macy 			error = SET_ERROR(EDQUOT);
4284eda14cbcSMatt Macy 	}
4285eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
4286eda14cbcSMatt Macy 
4287eda14cbcSMatt Macy 	return (error);
4288eda14cbcSMatt Macy }
4289eda14cbcSMatt Macy 
4290eda14cbcSMatt Macy typedef struct dsl_dataset_set_qr_arg {
4291eda14cbcSMatt Macy 	const char *ddsqra_name;
4292eda14cbcSMatt Macy 	zprop_source_t ddsqra_source;
4293eda14cbcSMatt Macy 	uint64_t ddsqra_value;
4294eda14cbcSMatt Macy } dsl_dataset_set_qr_arg_t;
4295eda14cbcSMatt Macy 
4296eda14cbcSMatt Macy 
4297eda14cbcSMatt Macy static int
4298eda14cbcSMatt Macy dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
4299eda14cbcSMatt Macy {
4300eda14cbcSMatt Macy 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4301eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
4302eda14cbcSMatt Macy 	dsl_dataset_t *ds;
4303eda14cbcSMatt Macy 	int error;
4304eda14cbcSMatt Macy 	uint64_t newval;
4305eda14cbcSMatt Macy 
4306eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
4307eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
4308eda14cbcSMatt Macy 
4309eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4310eda14cbcSMatt Macy 	if (error != 0)
4311eda14cbcSMatt Macy 		return (error);
4312eda14cbcSMatt Macy 
4313eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
4314eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4315eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
4316eda14cbcSMatt Macy 	}
4317eda14cbcSMatt Macy 
4318eda14cbcSMatt Macy 	error = dsl_prop_predict(ds->ds_dir,
4319eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4320eda14cbcSMatt Macy 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4321eda14cbcSMatt Macy 	if (error != 0) {
4322eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4323eda14cbcSMatt Macy 		return (error);
4324eda14cbcSMatt Macy 	}
4325eda14cbcSMatt Macy 
4326eda14cbcSMatt Macy 	if (newval == 0) {
4327eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4328eda14cbcSMatt Macy 		return (0);
4329eda14cbcSMatt Macy 	}
4330eda14cbcSMatt Macy 
4331eda14cbcSMatt Macy 	if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes ||
4332eda14cbcSMatt Macy 	    newval < ds->ds_reserved) {
4333eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4334eda14cbcSMatt Macy 		return (SET_ERROR(ENOSPC));
4335eda14cbcSMatt Macy 	}
4336eda14cbcSMatt Macy 
4337eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
4338eda14cbcSMatt Macy 	return (0);
4339eda14cbcSMatt Macy }
4340eda14cbcSMatt Macy 
4341eda14cbcSMatt Macy static void
4342eda14cbcSMatt Macy dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
4343eda14cbcSMatt Macy {
4344eda14cbcSMatt Macy 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4345eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
4346eda14cbcSMatt Macy 	dsl_dataset_t *ds = NULL;
4347eda14cbcSMatt Macy 	uint64_t newval;
4348eda14cbcSMatt Macy 
4349eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4350eda14cbcSMatt Macy 
4351eda14cbcSMatt Macy 	dsl_prop_set_sync_impl(ds,
4352eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4353eda14cbcSMatt Macy 	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
4354eda14cbcSMatt Macy 	    &ddsqra->ddsqra_value, tx);
4355eda14cbcSMatt Macy 
4356eda14cbcSMatt Macy 	VERIFY0(dsl_prop_get_int_ds(ds,
4357eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
4358eda14cbcSMatt Macy 
4359eda14cbcSMatt Macy 	if (ds->ds_quota != newval) {
4360eda14cbcSMatt Macy 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
4361eda14cbcSMatt Macy 		ds->ds_quota = newval;
4362eda14cbcSMatt Macy 	}
4363eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
4364eda14cbcSMatt Macy }
4365eda14cbcSMatt Macy 
4366eda14cbcSMatt Macy int
4367eda14cbcSMatt Macy dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
4368eda14cbcSMatt Macy     uint64_t refquota)
4369eda14cbcSMatt Macy {
4370eda14cbcSMatt Macy 	dsl_dataset_set_qr_arg_t ddsqra;
4371eda14cbcSMatt Macy 
4372eda14cbcSMatt Macy 	ddsqra.ddsqra_name = dsname;
4373eda14cbcSMatt Macy 	ddsqra.ddsqra_source = source;
4374eda14cbcSMatt Macy 	ddsqra.ddsqra_value = refquota;
4375eda14cbcSMatt Macy 
4376eda14cbcSMatt Macy 	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
4377eda14cbcSMatt Macy 	    dsl_dataset_set_refquota_sync, &ddsqra, 0,
4378eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
4379eda14cbcSMatt Macy }
4380eda14cbcSMatt Macy 
4381eda14cbcSMatt Macy static int
4382eda14cbcSMatt Macy dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
4383eda14cbcSMatt Macy {
4384eda14cbcSMatt Macy 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4385eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
4386eda14cbcSMatt Macy 	dsl_dataset_t *ds;
4387eda14cbcSMatt Macy 	int error;
4388eda14cbcSMatt Macy 	uint64_t newval, unique;
4389eda14cbcSMatt Macy 
4390eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
4391eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
4392eda14cbcSMatt Macy 
4393eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4394eda14cbcSMatt Macy 	if (error != 0)
4395eda14cbcSMatt Macy 		return (error);
4396eda14cbcSMatt Macy 
4397eda14cbcSMatt Macy 	if (ds->ds_is_snapshot) {
4398eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4399eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
4400eda14cbcSMatt Macy 	}
4401eda14cbcSMatt Macy 
4402eda14cbcSMatt Macy 	error = dsl_prop_predict(ds->ds_dir,
4403eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4404eda14cbcSMatt Macy 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4405eda14cbcSMatt Macy 	if (error != 0) {
4406eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4407eda14cbcSMatt Macy 		return (error);
4408eda14cbcSMatt Macy 	}
4409eda14cbcSMatt Macy 
4410eda14cbcSMatt Macy 	/*
4411eda14cbcSMatt Macy 	 * If we are doing the preliminary check in open context, the
4412eda14cbcSMatt Macy 	 * space estimates may be inaccurate.
4413eda14cbcSMatt Macy 	 */
4414eda14cbcSMatt Macy 	if (!dmu_tx_is_syncing(tx)) {
4415eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4416eda14cbcSMatt Macy 		return (0);
4417eda14cbcSMatt Macy 	}
4418eda14cbcSMatt Macy 
4419eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
4420eda14cbcSMatt Macy 	if (!DS_UNIQUE_IS_ACCURATE(ds))
4421eda14cbcSMatt Macy 		dsl_dataset_recalc_head_uniq(ds);
4422eda14cbcSMatt Macy 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
4423eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
4424eda14cbcSMatt Macy 
4425eda14cbcSMatt Macy 	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
4426eda14cbcSMatt Macy 		uint64_t delta = MAX(unique, newval) -
4427eda14cbcSMatt Macy 		    MAX(unique, ds->ds_reserved);
4428eda14cbcSMatt Macy 
4429eda14cbcSMatt Macy 		if (delta >
4430eda14cbcSMatt Macy 		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
4431eda14cbcSMatt Macy 		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
4432eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
4433eda14cbcSMatt Macy 			return (SET_ERROR(ENOSPC));
4434eda14cbcSMatt Macy 		}
4435eda14cbcSMatt Macy 	}
4436eda14cbcSMatt Macy 
4437eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
4438eda14cbcSMatt Macy 	return (0);
4439eda14cbcSMatt Macy }
4440eda14cbcSMatt Macy 
4441eda14cbcSMatt Macy void
4442eda14cbcSMatt Macy dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
4443eda14cbcSMatt Macy     zprop_source_t source, uint64_t value, dmu_tx_t *tx)
4444eda14cbcSMatt Macy {
4445eda14cbcSMatt Macy 	uint64_t newval;
4446eda14cbcSMatt Macy 	uint64_t unique;
4447eda14cbcSMatt Macy 	int64_t delta;
4448eda14cbcSMatt Macy 
4449eda14cbcSMatt Macy 	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4450eda14cbcSMatt Macy 	    source, sizeof (value), 1, &value, tx);
4451eda14cbcSMatt Macy 
4452eda14cbcSMatt Macy 	VERIFY0(dsl_prop_get_int_ds(ds,
4453eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
4454eda14cbcSMatt Macy 
4455eda14cbcSMatt Macy 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
4456eda14cbcSMatt Macy 	mutex_enter(&ds->ds_dir->dd_lock);
4457eda14cbcSMatt Macy 	mutex_enter(&ds->ds_lock);
4458eda14cbcSMatt Macy 	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
4459eda14cbcSMatt Macy 	unique = dsl_dataset_phys(ds)->ds_unique_bytes;
4460eda14cbcSMatt Macy 	delta = MAX(0, (int64_t)(newval - unique)) -
4461eda14cbcSMatt Macy 	    MAX(0, (int64_t)(ds->ds_reserved - unique));
4462eda14cbcSMatt Macy 	ds->ds_reserved = newval;
4463eda14cbcSMatt Macy 	mutex_exit(&ds->ds_lock);
4464eda14cbcSMatt Macy 
4465eda14cbcSMatt Macy 	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
4466eda14cbcSMatt Macy 	mutex_exit(&ds->ds_dir->dd_lock);
4467eda14cbcSMatt Macy }
4468eda14cbcSMatt Macy 
4469eda14cbcSMatt Macy static void
4470eda14cbcSMatt Macy dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
4471eda14cbcSMatt Macy {
4472eda14cbcSMatt Macy 	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4473eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
4474eda14cbcSMatt Macy 	dsl_dataset_t *ds = NULL;
4475eda14cbcSMatt Macy 
4476eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4477eda14cbcSMatt Macy 	dsl_dataset_set_refreservation_sync_impl(ds,
4478eda14cbcSMatt Macy 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
4479eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
4480eda14cbcSMatt Macy }
4481eda14cbcSMatt Macy 
4482eda14cbcSMatt Macy int
4483eda14cbcSMatt Macy dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
4484eda14cbcSMatt Macy     uint64_t refreservation)
4485eda14cbcSMatt Macy {
4486eda14cbcSMatt Macy 	dsl_dataset_set_qr_arg_t ddsqra;
4487eda14cbcSMatt Macy 
4488eda14cbcSMatt Macy 	ddsqra.ddsqra_name = dsname;
4489eda14cbcSMatt Macy 	ddsqra.ddsqra_source = source;
4490eda14cbcSMatt Macy 	ddsqra.ddsqra_value = refreservation;
4491eda14cbcSMatt Macy 
4492eda14cbcSMatt Macy 	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
4493eda14cbcSMatt Macy 	    dsl_dataset_set_refreservation_sync, &ddsqra, 0,
4494eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
4495eda14cbcSMatt Macy }
4496eda14cbcSMatt Macy 
4497eda14cbcSMatt Macy typedef struct dsl_dataset_set_compression_arg {
4498eda14cbcSMatt Macy 	const char *ddsca_name;
4499eda14cbcSMatt Macy 	zprop_source_t ddsca_source;
4500eda14cbcSMatt Macy 	uint64_t ddsca_value;
4501eda14cbcSMatt Macy } dsl_dataset_set_compression_arg_t;
4502eda14cbcSMatt Macy 
4503eda14cbcSMatt Macy static int
4504eda14cbcSMatt Macy dsl_dataset_set_compression_check(void *arg, dmu_tx_t *tx)
4505eda14cbcSMatt Macy {
4506eda14cbcSMatt Macy 	dsl_dataset_set_compression_arg_t *ddsca = arg;
4507eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
4508eda14cbcSMatt Macy 
4509eda14cbcSMatt Macy 	uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value);
4510eda14cbcSMatt Macy 	spa_feature_t f = zio_compress_to_feature(compval);
4511eda14cbcSMatt Macy 
4512eda14cbcSMatt Macy 	if (f == SPA_FEATURE_NONE)
4513eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
4514eda14cbcSMatt Macy 
4515eda14cbcSMatt Macy 	if (!spa_feature_is_enabled(dp->dp_spa, f))
4516eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
4517eda14cbcSMatt Macy 
4518eda14cbcSMatt Macy 	return (0);
4519eda14cbcSMatt Macy }
4520eda14cbcSMatt Macy 
4521eda14cbcSMatt Macy static void
4522eda14cbcSMatt Macy dsl_dataset_set_compression_sync(void *arg, dmu_tx_t *tx)
4523eda14cbcSMatt Macy {
4524eda14cbcSMatt Macy 	dsl_dataset_set_compression_arg_t *ddsca = arg;
4525eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
4526eda14cbcSMatt Macy 	dsl_dataset_t *ds = NULL;
4527eda14cbcSMatt Macy 
4528eda14cbcSMatt Macy 	uint64_t compval = ZIO_COMPRESS_ALGO(ddsca->ddsca_value);
4529eda14cbcSMatt Macy 	spa_feature_t f = zio_compress_to_feature(compval);
4530c7046f76SMartin Matuska 	ASSERT3S(f, !=, SPA_FEATURE_NONE);
4531eda14cbcSMatt Macy 	ASSERT3S(spa_feature_table[f].fi_type, ==, ZFEATURE_TYPE_BOOLEAN);
4532eda14cbcSMatt Macy 
4533eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddsca->ddsca_name, FTAG, &ds));
4534eda14cbcSMatt Macy 	if (zfeature_active(f, ds->ds_feature[f]) != B_TRUE) {
4535eda14cbcSMatt Macy 		ds->ds_feature_activation[f] = (void *)B_TRUE;
4536eda14cbcSMatt Macy 		dsl_dataset_activate_feature(ds->ds_object, f,
4537eda14cbcSMatt Macy 		    ds->ds_feature_activation[f], tx);
4538eda14cbcSMatt Macy 		ds->ds_feature[f] = ds->ds_feature_activation[f];
4539eda14cbcSMatt Macy 	}
4540eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
4541eda14cbcSMatt Macy }
4542eda14cbcSMatt Macy 
4543eda14cbcSMatt Macy int
4544eda14cbcSMatt Macy dsl_dataset_set_compression(const char *dsname, zprop_source_t source,
4545eda14cbcSMatt Macy     uint64_t compression)
4546eda14cbcSMatt Macy {
4547eda14cbcSMatt Macy 	dsl_dataset_set_compression_arg_t ddsca;
4548eda14cbcSMatt Macy 
4549eda14cbcSMatt Macy 	/*
4550eda14cbcSMatt Macy 	 * The sync task is only required for zstd in order to activate
4551eda14cbcSMatt Macy 	 * the feature flag when the property is first set.
4552eda14cbcSMatt Macy 	 */
4553eda14cbcSMatt Macy 	if (ZIO_COMPRESS_ALGO(compression) != ZIO_COMPRESS_ZSTD)
4554eda14cbcSMatt Macy 		return (0);
4555eda14cbcSMatt Macy 
4556eda14cbcSMatt Macy 	ddsca.ddsca_name = dsname;
4557eda14cbcSMatt Macy 	ddsca.ddsca_source = source;
4558eda14cbcSMatt Macy 	ddsca.ddsca_value = compression;
4559eda14cbcSMatt Macy 
4560eda14cbcSMatt Macy 	return (dsl_sync_task(dsname, dsl_dataset_set_compression_check,
4561eda14cbcSMatt Macy 	    dsl_dataset_set_compression_sync, &ddsca, 0,
4562eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
4563eda14cbcSMatt Macy }
4564eda14cbcSMatt Macy 
4565eda14cbcSMatt Macy /*
4566eda14cbcSMatt Macy  * Return (in *usedp) the amount of space referenced by "new" that was not
4567eda14cbcSMatt Macy  * referenced at the time the bookmark corresponds to.  "New" may be a
4568eda14cbcSMatt Macy  * snapshot or a head.  The bookmark must be before new, in
4569eda14cbcSMatt Macy  * new's filesystem (or its origin) -- caller verifies this.
4570eda14cbcSMatt Macy  *
4571eda14cbcSMatt Macy  * The written space is calculated by considering two components:  First, we
4572eda14cbcSMatt Macy  * ignore any freed space, and calculate the written as new's used space
4573eda14cbcSMatt Macy  * minus old's used space.  Next, we add in the amount of space that was freed
4574eda14cbcSMatt Macy  * between the two time points, thus reducing new's used space relative to
4575eda14cbcSMatt Macy  * old's. Specifically, this is the space that was born before
4576eda14cbcSMatt Macy  * zbm_creation_txg, and freed before new (ie. on new's deadlist or a
4577eda14cbcSMatt Macy  * previous deadlist).
4578eda14cbcSMatt Macy  *
4579eda14cbcSMatt Macy  * space freed                         [---------------------]
4580eda14cbcSMatt Macy  * snapshots                       ---O-------O--------O-------O------
4581eda14cbcSMatt Macy  *                                         bookmark           new
4582eda14cbcSMatt Macy  *
4583eda14cbcSMatt Macy  * Note, the bookmark's zbm_*_bytes_refd must be valid, but if the HAS_FBN
4584eda14cbcSMatt Macy  * flag is not set, we will calculate the freed_before_next based on the
4585eda14cbcSMatt Macy  * next snapshot's deadlist, rather than using zbm_*_freed_before_next_snap.
4586eda14cbcSMatt Macy  */
4587eda14cbcSMatt Macy static int
4588eda14cbcSMatt Macy dsl_dataset_space_written_impl(zfs_bookmark_phys_t *bmp,
4589eda14cbcSMatt Macy     dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4590eda14cbcSMatt Macy {
4591eda14cbcSMatt Macy 	int err = 0;
4592eda14cbcSMatt Macy 	dsl_pool_t *dp = new->ds_dir->dd_pool;
4593eda14cbcSMatt Macy 
4594eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
4595eda14cbcSMatt Macy 	if (dsl_dataset_is_snapshot(new)) {
4596eda14cbcSMatt Macy 		ASSERT3U(bmp->zbm_creation_txg, <,
4597eda14cbcSMatt Macy 		    dsl_dataset_phys(new)->ds_creation_txg);
4598eda14cbcSMatt Macy 	}
4599eda14cbcSMatt Macy 
4600eda14cbcSMatt Macy 	*usedp = 0;
4601eda14cbcSMatt Macy 	*usedp += dsl_dataset_phys(new)->ds_referenced_bytes;
4602eda14cbcSMatt Macy 	*usedp -= bmp->zbm_referenced_bytes_refd;
4603eda14cbcSMatt Macy 
4604eda14cbcSMatt Macy 	*compp = 0;
4605eda14cbcSMatt Macy 	*compp += dsl_dataset_phys(new)->ds_compressed_bytes;
4606eda14cbcSMatt Macy 	*compp -= bmp->zbm_compressed_bytes_refd;
4607eda14cbcSMatt Macy 
4608eda14cbcSMatt Macy 	*uncompp = 0;
4609eda14cbcSMatt Macy 	*uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes;
4610eda14cbcSMatt Macy 	*uncompp -= bmp->zbm_uncompressed_bytes_refd;
4611eda14cbcSMatt Macy 
4612eda14cbcSMatt Macy 	dsl_dataset_t *snap = new;
4613eda14cbcSMatt Macy 
4614eda14cbcSMatt Macy 	while (dsl_dataset_phys(snap)->ds_prev_snap_txg >
4615eda14cbcSMatt Macy 	    bmp->zbm_creation_txg) {
4616eda14cbcSMatt Macy 		uint64_t used, comp, uncomp;
4617eda14cbcSMatt Macy 
4618eda14cbcSMatt Macy 		dsl_deadlist_space_range(&snap->ds_deadlist,
4619eda14cbcSMatt Macy 		    0, bmp->zbm_creation_txg,
4620eda14cbcSMatt Macy 		    &used, &comp, &uncomp);
4621eda14cbcSMatt Macy 		*usedp += used;
4622eda14cbcSMatt Macy 		*compp += comp;
4623eda14cbcSMatt Macy 		*uncompp += uncomp;
4624eda14cbcSMatt Macy 
4625eda14cbcSMatt Macy 		uint64_t snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
4626eda14cbcSMatt Macy 		if (snap != new)
4627eda14cbcSMatt Macy 			dsl_dataset_rele(snap, FTAG);
4628eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
4629eda14cbcSMatt Macy 		if (err != 0)
4630eda14cbcSMatt Macy 			break;
4631eda14cbcSMatt Macy 	}
4632eda14cbcSMatt Macy 
4633eda14cbcSMatt Macy 	/*
4634eda14cbcSMatt Macy 	 * We might not have the FBN if we are calculating written from
4635eda14cbcSMatt Macy 	 * a snapshot (because we didn't know the correct "next" snapshot
4636eda14cbcSMatt Macy 	 * until now).
4637eda14cbcSMatt Macy 	 */
4638eda14cbcSMatt Macy 	if (bmp->zbm_flags & ZBM_FLAG_HAS_FBN) {
4639eda14cbcSMatt Macy 		*usedp += bmp->zbm_referenced_freed_before_next_snap;
4640eda14cbcSMatt Macy 		*compp += bmp->zbm_compressed_freed_before_next_snap;
4641eda14cbcSMatt Macy 		*uncompp += bmp->zbm_uncompressed_freed_before_next_snap;
4642eda14cbcSMatt Macy 	} else {
4643eda14cbcSMatt Macy 		ASSERT3U(dsl_dataset_phys(snap)->ds_prev_snap_txg, ==,
4644eda14cbcSMatt Macy 		    bmp->zbm_creation_txg);
4645eda14cbcSMatt Macy 		uint64_t used, comp, uncomp;
4646eda14cbcSMatt Macy 		dsl_deadlist_space(&snap->ds_deadlist, &used, &comp, &uncomp);
4647eda14cbcSMatt Macy 		*usedp += used;
4648eda14cbcSMatt Macy 		*compp += comp;
4649eda14cbcSMatt Macy 		*uncompp += uncomp;
4650eda14cbcSMatt Macy 	}
4651eda14cbcSMatt Macy 	if (snap != new)
4652eda14cbcSMatt Macy 		dsl_dataset_rele(snap, FTAG);
4653eda14cbcSMatt Macy 	return (err);
4654eda14cbcSMatt Macy }
4655eda14cbcSMatt Macy 
4656eda14cbcSMatt Macy /*
4657eda14cbcSMatt Macy  * Return (in *usedp) the amount of space written in new that was not
4658eda14cbcSMatt Macy  * present at the time the bookmark corresponds to.  New may be a
4659eda14cbcSMatt Macy  * snapshot or the head.  Old must be a bookmark before new, in
4660eda14cbcSMatt Macy  * new's filesystem (or its origin) -- caller verifies this.
4661eda14cbcSMatt Macy  */
4662eda14cbcSMatt Macy int
4663eda14cbcSMatt Macy dsl_dataset_space_written_bookmark(zfs_bookmark_phys_t *bmp,
4664eda14cbcSMatt Macy     dsl_dataset_t *new, uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4665eda14cbcSMatt Macy {
4666eda14cbcSMatt Macy 	if (!(bmp->zbm_flags & ZBM_FLAG_HAS_FBN))
4667eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
4668eda14cbcSMatt Macy 	return (dsl_dataset_space_written_impl(bmp, new,
4669eda14cbcSMatt Macy 	    usedp, compp, uncompp));
4670eda14cbcSMatt Macy }
4671eda14cbcSMatt Macy 
4672eda14cbcSMatt Macy /*
4673eda14cbcSMatt Macy  * Return (in *usedp) the amount of space written in new that is not
4674eda14cbcSMatt Macy  * present in oldsnap.  New may be a snapshot or the head.  Old must be
4675eda14cbcSMatt Macy  * a snapshot before new, in new's filesystem (or its origin).  If not then
4676eda14cbcSMatt Macy  * fail and return EINVAL.
4677eda14cbcSMatt Macy  */
4678eda14cbcSMatt Macy int
4679eda14cbcSMatt Macy dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
4680eda14cbcSMatt Macy     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4681eda14cbcSMatt Macy {
4682eda14cbcSMatt Macy 	if (!dsl_dataset_is_before(new, oldsnap, 0))
4683eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
4684eda14cbcSMatt Macy 
4685eda14cbcSMatt Macy 	zfs_bookmark_phys_t zbm = { 0 };
4686eda14cbcSMatt Macy 	dsl_dataset_phys_t *dsp = dsl_dataset_phys(oldsnap);
4687eda14cbcSMatt Macy 	zbm.zbm_guid = dsp->ds_guid;
4688eda14cbcSMatt Macy 	zbm.zbm_creation_txg = dsp->ds_creation_txg;
4689eda14cbcSMatt Macy 	zbm.zbm_creation_time = dsp->ds_creation_time;
4690eda14cbcSMatt Macy 	zbm.zbm_referenced_bytes_refd = dsp->ds_referenced_bytes;
4691eda14cbcSMatt Macy 	zbm.zbm_compressed_bytes_refd = dsp->ds_compressed_bytes;
4692eda14cbcSMatt Macy 	zbm.zbm_uncompressed_bytes_refd = dsp->ds_uncompressed_bytes;
4693eda14cbcSMatt Macy 
4694eda14cbcSMatt Macy 	/*
4695eda14cbcSMatt Macy 	 * If oldsnap is the origin (or origin's origin, ...) of new,
4696eda14cbcSMatt Macy 	 * we can't easily calculate the effective FBN.  Therefore,
4697eda14cbcSMatt Macy 	 * we do not set ZBM_FLAG_HAS_FBN, so that the _impl will calculate
4698eda14cbcSMatt Macy 	 * it relative to the correct "next": the next snapshot towards "new",
4699eda14cbcSMatt Macy 	 * rather than the next snapshot in oldsnap's dsl_dir.
4700eda14cbcSMatt Macy 	 */
4701eda14cbcSMatt Macy 	return (dsl_dataset_space_written_impl(&zbm, new,
4702eda14cbcSMatt Macy 	    usedp, compp, uncompp));
4703eda14cbcSMatt Macy }
4704eda14cbcSMatt Macy 
4705eda14cbcSMatt Macy /*
4706eda14cbcSMatt Macy  * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
4707eda14cbcSMatt Macy  * lastsnap, and all snapshots in between are deleted.
4708eda14cbcSMatt Macy  *
4709eda14cbcSMatt Macy  * blocks that would be freed            [---------------------------]
4710eda14cbcSMatt Macy  * snapshots                       ---O-------O--------O-------O--------O
4711eda14cbcSMatt Macy  *                                        firstsnap        lastsnap
4712eda14cbcSMatt Macy  *
4713eda14cbcSMatt Macy  * This is the set of blocks that were born after the snap before firstsnap,
4714eda14cbcSMatt Macy  * (birth > firstsnap->prev_snap_txg) and died before the snap after the
4715eda14cbcSMatt Macy  * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
4716eda14cbcSMatt Macy  * We calculate this by iterating over the relevant deadlists (from the snap
4717eda14cbcSMatt Macy  * after lastsnap, backward to the snap after firstsnap), summing up the
4718eda14cbcSMatt Macy  * space on the deadlist that was born after the snap before firstsnap.
4719eda14cbcSMatt Macy  */
4720eda14cbcSMatt Macy int
4721eda14cbcSMatt Macy dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
4722eda14cbcSMatt Macy     dsl_dataset_t *lastsnap,
4723eda14cbcSMatt Macy     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
4724eda14cbcSMatt Macy {
4725eda14cbcSMatt Macy 	int err = 0;
4726eda14cbcSMatt Macy 	uint64_t snapobj;
4727eda14cbcSMatt Macy 	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
4728eda14cbcSMatt Macy 
4729eda14cbcSMatt Macy 	ASSERT(firstsnap->ds_is_snapshot);
4730eda14cbcSMatt Macy 	ASSERT(lastsnap->ds_is_snapshot);
4731eda14cbcSMatt Macy 
4732eda14cbcSMatt Macy 	/*
4733eda14cbcSMatt Macy 	 * Check that the snapshots are in the same dsl_dir, and firstsnap
4734eda14cbcSMatt Macy 	 * is before lastsnap.
4735eda14cbcSMatt Macy 	 */
4736eda14cbcSMatt Macy 	if (firstsnap->ds_dir != lastsnap->ds_dir ||
4737eda14cbcSMatt Macy 	    dsl_dataset_phys(firstsnap)->ds_creation_txg >
4738eda14cbcSMatt Macy 	    dsl_dataset_phys(lastsnap)->ds_creation_txg)
4739eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
4740eda14cbcSMatt Macy 
4741eda14cbcSMatt Macy 	*usedp = *compp = *uncompp = 0;
4742eda14cbcSMatt Macy 
4743eda14cbcSMatt Macy 	snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj;
4744eda14cbcSMatt Macy 	while (snapobj != firstsnap->ds_object) {
4745eda14cbcSMatt Macy 		dsl_dataset_t *ds;
4746eda14cbcSMatt Macy 		uint64_t used, comp, uncomp;
4747eda14cbcSMatt Macy 
4748eda14cbcSMatt Macy 		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
4749eda14cbcSMatt Macy 		if (err != 0)
4750eda14cbcSMatt Macy 			break;
4751eda14cbcSMatt Macy 
4752eda14cbcSMatt Macy 		dsl_deadlist_space_range(&ds->ds_deadlist,
4753eda14cbcSMatt Macy 		    dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX,
4754eda14cbcSMatt Macy 		    &used, &comp, &uncomp);
4755eda14cbcSMatt Macy 		*usedp += used;
4756eda14cbcSMatt Macy 		*compp += comp;
4757eda14cbcSMatt Macy 		*uncompp += uncomp;
4758eda14cbcSMatt Macy 
4759eda14cbcSMatt Macy 		snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4760eda14cbcSMatt Macy 		ASSERT3U(snapobj, !=, 0);
4761eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
4762eda14cbcSMatt Macy 	}
4763eda14cbcSMatt Macy 	return (err);
4764eda14cbcSMatt Macy }
4765eda14cbcSMatt Macy 
4766eda14cbcSMatt Macy /*
4767eda14cbcSMatt Macy  * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
4768eda14cbcSMatt Macy  * For example, they could both be snapshots of the same filesystem, and
4769eda14cbcSMatt Macy  * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
4770eda14cbcSMatt Macy  * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
4771eda14cbcSMatt Macy  * filesystem.  Or 'earlier' could be the origin's origin.
4772eda14cbcSMatt Macy  *
4773eda14cbcSMatt Macy  * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg.
4774eda14cbcSMatt Macy  */
4775eda14cbcSMatt Macy boolean_t
4776eda14cbcSMatt Macy dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier,
4777eda14cbcSMatt Macy     uint64_t earlier_txg)
4778eda14cbcSMatt Macy {
4779eda14cbcSMatt Macy 	dsl_pool_t *dp = later->ds_dir->dd_pool;
4780eda14cbcSMatt Macy 	int error;
4781eda14cbcSMatt Macy 	boolean_t ret;
4782eda14cbcSMatt Macy 
4783eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
4784eda14cbcSMatt Macy 	ASSERT(earlier->ds_is_snapshot || earlier_txg != 0);
4785eda14cbcSMatt Macy 
4786eda14cbcSMatt Macy 	if (earlier_txg == 0)
4787eda14cbcSMatt Macy 		earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg;
4788eda14cbcSMatt Macy 
4789eda14cbcSMatt Macy 	if (later->ds_is_snapshot &&
4790eda14cbcSMatt Macy 	    earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg)
4791eda14cbcSMatt Macy 		return (B_FALSE);
4792eda14cbcSMatt Macy 
4793eda14cbcSMatt Macy 	if (later->ds_dir == earlier->ds_dir)
4794eda14cbcSMatt Macy 		return (B_TRUE);
4795eda14cbcSMatt Macy 
4796eda14cbcSMatt Macy 	/*
4797eda14cbcSMatt Macy 	 * We check dd_origin_obj explicitly here rather than using
4798eda14cbcSMatt Macy 	 * dsl_dir_is_clone() so that we will return TRUE if "earlier"
4799eda14cbcSMatt Macy 	 * is $ORIGIN@$ORIGIN.  dsl_dataset_space_written() depends on
4800eda14cbcSMatt Macy 	 * this behavior.
4801eda14cbcSMatt Macy 	 */
4802eda14cbcSMatt Macy 	if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == 0)
4803eda14cbcSMatt Macy 		return (B_FALSE);
4804eda14cbcSMatt Macy 
4805eda14cbcSMatt Macy 	dsl_dataset_t *origin;
4806eda14cbcSMatt Macy 	error = dsl_dataset_hold_obj(dp,
4807eda14cbcSMatt Macy 	    dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin);
4808eda14cbcSMatt Macy 	if (error != 0)
4809eda14cbcSMatt Macy 		return (B_FALSE);
4810eda14cbcSMatt Macy 	if (dsl_dataset_phys(origin)->ds_creation_txg == earlier_txg &&
4811eda14cbcSMatt Macy 	    origin->ds_dir == earlier->ds_dir) {
4812eda14cbcSMatt Macy 		dsl_dataset_rele(origin, FTAG);
4813eda14cbcSMatt Macy 		return (B_TRUE);
4814eda14cbcSMatt Macy 	}
4815eda14cbcSMatt Macy 	ret = dsl_dataset_is_before(origin, earlier, earlier_txg);
4816eda14cbcSMatt Macy 	dsl_dataset_rele(origin, FTAG);
4817eda14cbcSMatt Macy 	return (ret);
4818eda14cbcSMatt Macy }
4819eda14cbcSMatt Macy 
4820eda14cbcSMatt Macy void
4821eda14cbcSMatt Macy dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx)
4822eda14cbcSMatt Macy {
4823eda14cbcSMatt Macy 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
4824eda14cbcSMatt Macy 	dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx);
4825eda14cbcSMatt Macy }
4826eda14cbcSMatt Macy 
4827eda14cbcSMatt Macy boolean_t
4828eda14cbcSMatt Macy dsl_dataset_is_zapified(dsl_dataset_t *ds)
4829eda14cbcSMatt Macy {
4830eda14cbcSMatt Macy 	dmu_object_info_t doi;
4831eda14cbcSMatt Macy 
4832eda14cbcSMatt Macy 	dmu_object_info_from_db(ds->ds_dbuf, &doi);
4833eda14cbcSMatt Macy 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
4834eda14cbcSMatt Macy }
4835eda14cbcSMatt Macy 
4836eda14cbcSMatt Macy boolean_t
4837eda14cbcSMatt Macy dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds)
4838eda14cbcSMatt Macy {
4839eda14cbcSMatt Macy 	return (dsl_dataset_is_zapified(ds) &&
4840eda14cbcSMatt Macy 	    zap_contains(ds->ds_dir->dd_pool->dp_meta_objset,
4841eda14cbcSMatt Macy 	    ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0);
4842eda14cbcSMatt Macy }
4843eda14cbcSMatt Macy 
4844eda14cbcSMatt Macy uint64_t
4845eda14cbcSMatt Macy dsl_dataset_get_remap_deadlist_object(dsl_dataset_t *ds)
4846eda14cbcSMatt Macy {
4847eda14cbcSMatt Macy 	uint64_t remap_deadlist_obj;
4848eda14cbcSMatt Macy 	int err;
4849eda14cbcSMatt Macy 
4850eda14cbcSMatt Macy 	if (!dsl_dataset_is_zapified(ds))
4851eda14cbcSMatt Macy 		return (0);
4852eda14cbcSMatt Macy 
4853eda14cbcSMatt Macy 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
4854eda14cbcSMatt Macy 	    DS_FIELD_REMAP_DEADLIST, sizeof (remap_deadlist_obj), 1,
4855eda14cbcSMatt Macy 	    &remap_deadlist_obj);
4856eda14cbcSMatt Macy 
4857eda14cbcSMatt Macy 	if (err != 0) {
4858eda14cbcSMatt Macy 		VERIFY3S(err, ==, ENOENT);
4859eda14cbcSMatt Macy 		return (0);
4860eda14cbcSMatt Macy 	}
4861eda14cbcSMatt Macy 
4862eda14cbcSMatt Macy 	ASSERT(remap_deadlist_obj != 0);
4863eda14cbcSMatt Macy 	return (remap_deadlist_obj);
4864eda14cbcSMatt Macy }
4865eda14cbcSMatt Macy 
4866eda14cbcSMatt Macy boolean_t
4867eda14cbcSMatt Macy dsl_dataset_remap_deadlist_exists(dsl_dataset_t *ds)
4868eda14cbcSMatt Macy {
4869eda14cbcSMatt Macy 	EQUIV(dsl_deadlist_is_open(&ds->ds_remap_deadlist),
4870eda14cbcSMatt Macy 	    dsl_dataset_get_remap_deadlist_object(ds) != 0);
4871eda14cbcSMatt Macy 	return (dsl_deadlist_is_open(&ds->ds_remap_deadlist));
4872eda14cbcSMatt Macy }
4873eda14cbcSMatt Macy 
4874eda14cbcSMatt Macy static void
4875eda14cbcSMatt Macy dsl_dataset_set_remap_deadlist_object(dsl_dataset_t *ds, uint64_t obj,
4876eda14cbcSMatt Macy     dmu_tx_t *tx)
4877eda14cbcSMatt Macy {
4878eda14cbcSMatt Macy 	ASSERT(obj != 0);
4879eda14cbcSMatt Macy 	dsl_dataset_zapify(ds, tx);
4880eda14cbcSMatt Macy 	VERIFY0(zap_add(ds->ds_dir->dd_pool->dp_meta_objset, ds->ds_object,
4881eda14cbcSMatt Macy 	    DS_FIELD_REMAP_DEADLIST, sizeof (obj), 1, &obj, tx));
4882eda14cbcSMatt Macy }
4883eda14cbcSMatt Macy 
4884eda14cbcSMatt Macy static void
4885eda14cbcSMatt Macy dsl_dataset_unset_remap_deadlist_object(dsl_dataset_t *ds, dmu_tx_t *tx)
4886eda14cbcSMatt Macy {
4887eda14cbcSMatt Macy 	VERIFY0(zap_remove(ds->ds_dir->dd_pool->dp_meta_objset,
4888eda14cbcSMatt Macy 	    ds->ds_object, DS_FIELD_REMAP_DEADLIST, tx));
4889eda14cbcSMatt Macy }
4890eda14cbcSMatt Macy 
4891eda14cbcSMatt Macy void
4892eda14cbcSMatt Macy dsl_dataset_destroy_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
4893eda14cbcSMatt Macy {
4894eda14cbcSMatt Macy 	uint64_t remap_deadlist_object;
4895eda14cbcSMatt Macy 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
4896eda14cbcSMatt Macy 
4897eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
4898eda14cbcSMatt Macy 	ASSERT(dsl_dataset_remap_deadlist_exists(ds));
4899eda14cbcSMatt Macy 
4900eda14cbcSMatt Macy 	remap_deadlist_object = ds->ds_remap_deadlist.dl_object;
4901eda14cbcSMatt Macy 	dsl_deadlist_close(&ds->ds_remap_deadlist);
4902eda14cbcSMatt Macy 	dsl_deadlist_free(spa_meta_objset(spa), remap_deadlist_object, tx);
4903eda14cbcSMatt Macy 	dsl_dataset_unset_remap_deadlist_object(ds, tx);
4904eda14cbcSMatt Macy 	spa_feature_decr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4905eda14cbcSMatt Macy }
4906eda14cbcSMatt Macy 
4907eda14cbcSMatt Macy void
4908eda14cbcSMatt Macy dsl_dataset_create_remap_deadlist(dsl_dataset_t *ds, dmu_tx_t *tx)
4909eda14cbcSMatt Macy {
4910eda14cbcSMatt Macy 	uint64_t remap_deadlist_obj;
4911eda14cbcSMatt Macy 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
4912eda14cbcSMatt Macy 
4913eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
4914eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&ds->ds_remap_deadlist_lock));
4915eda14cbcSMatt Macy 	/*
4916eda14cbcSMatt Macy 	 * Currently we only create remap deadlists when there are indirect
4917eda14cbcSMatt Macy 	 * vdevs with referenced mappings.
4918eda14cbcSMatt Macy 	 */
4919eda14cbcSMatt Macy 	ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL));
4920eda14cbcSMatt Macy 
4921eda14cbcSMatt Macy 	remap_deadlist_obj = dsl_deadlist_clone(
4922eda14cbcSMatt Macy 	    &ds->ds_deadlist, UINT64_MAX,
4923eda14cbcSMatt Macy 	    dsl_dataset_phys(ds)->ds_prev_snap_obj, tx);
4924eda14cbcSMatt Macy 	dsl_dataset_set_remap_deadlist_object(ds,
4925eda14cbcSMatt Macy 	    remap_deadlist_obj, tx);
4926eda14cbcSMatt Macy 	dsl_deadlist_open(&ds->ds_remap_deadlist, spa_meta_objset(spa),
4927eda14cbcSMatt Macy 	    remap_deadlist_obj);
4928eda14cbcSMatt Macy 	spa_feature_incr(spa, SPA_FEATURE_OBSOLETE_COUNTS, tx);
4929eda14cbcSMatt Macy }
4930eda14cbcSMatt Macy 
4931eda14cbcSMatt Macy void
4932eda14cbcSMatt Macy dsl_dataset_activate_redaction(dsl_dataset_t *ds, uint64_t *redact_snaps,
4933eda14cbcSMatt Macy     uint64_t num_redact_snaps, dmu_tx_t *tx)
4934eda14cbcSMatt Macy {
4935eda14cbcSMatt Macy 	uint64_t dsobj = ds->ds_object;
4936eda14cbcSMatt Macy 	struct feature_type_uint64_array_arg *ftuaa =
4937eda14cbcSMatt Macy 	    kmem_zalloc(sizeof (*ftuaa), KM_SLEEP);
4938eda14cbcSMatt Macy 	ftuaa->length = (int64_t)num_redact_snaps;
4939eda14cbcSMatt Macy 	if (num_redact_snaps > 0) {
4940eda14cbcSMatt Macy 		ftuaa->array = kmem_alloc(num_redact_snaps * sizeof (uint64_t),
4941eda14cbcSMatt Macy 		    KM_SLEEP);
4942da5137abSMartin Matuska 		memcpy(ftuaa->array, redact_snaps, num_redact_snaps *
4943eda14cbcSMatt Macy 		    sizeof (uint64_t));
4944eda14cbcSMatt Macy 	}
4945eda14cbcSMatt Macy 	dsl_dataset_activate_feature(dsobj, SPA_FEATURE_REDACTED_DATASETS,
4946eda14cbcSMatt Macy 	    ftuaa, tx);
4947eda14cbcSMatt Macy 	ds->ds_feature[SPA_FEATURE_REDACTED_DATASETS] = ftuaa;
4948eda14cbcSMatt Macy }
4949eda14cbcSMatt Macy 
4950716fd348SMartin Matuska /*
4951716fd348SMartin Matuska  * Find and return (in *oldest_dsobj) the oldest snapshot of the dsobj
4952716fd348SMartin Matuska  * dataset whose birth time is >= min_txg.
4953716fd348SMartin Matuska  */
4954716fd348SMartin Matuska int
4955716fd348SMartin Matuska dsl_dataset_oldest_snapshot(spa_t *spa, uint64_t head_ds, uint64_t min_txg,
4956716fd348SMartin Matuska     uint64_t *oldest_dsobj)
4957716fd348SMartin Matuska {
4958716fd348SMartin Matuska 	dsl_dataset_t *ds;
4959716fd348SMartin Matuska 	dsl_pool_t *dp = spa->spa_dsl_pool;
4960716fd348SMartin Matuska 
4961716fd348SMartin Matuska 	int error = dsl_dataset_hold_obj(dp, head_ds, FTAG, &ds);
4962716fd348SMartin Matuska 	if (error != 0)
4963716fd348SMartin Matuska 		return (error);
4964716fd348SMartin Matuska 
4965716fd348SMartin Matuska 	uint64_t prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4966716fd348SMartin Matuska 	uint64_t prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
4967716fd348SMartin Matuska 
4968716fd348SMartin Matuska 	while (prev_obj != 0 && min_txg < prev_obj_txg) {
4969716fd348SMartin Matuska 		dsl_dataset_rele(ds, FTAG);
4970716fd348SMartin Matuska 		if ((error = dsl_dataset_hold_obj(dp, prev_obj,
4971716fd348SMartin Matuska 		    FTAG, &ds)) != 0)
4972716fd348SMartin Matuska 			return (error);
4973716fd348SMartin Matuska 		prev_obj_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg;
4974716fd348SMartin Matuska 		prev_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
4975716fd348SMartin Matuska 	}
4976716fd348SMartin Matuska 	*oldest_dsobj = ds->ds_object;
4977716fd348SMartin Matuska 	dsl_dataset_rele(ds, FTAG);
4978716fd348SMartin Matuska 	return (0);
4979716fd348SMartin Matuska }
4980716fd348SMartin Matuska 
4981be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, max_recordsize, UINT, ZMOD_RW,
4982eda14cbcSMatt Macy 	"Max allowed record size");
4983eda14cbcSMatt Macy 
4984eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, allow_redacted_dataset_mount, INT, ZMOD_RW,
4985eda14cbcSMatt Macy 	"Allow mounting of redacted datasets");
4986eda14cbcSMatt Macy 
498708aba0aeSMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, snapshot_history_enabled, INT, ZMOD_RW,
498808aba0aeSMartin Matuska 	"Include snapshot events in pool history/events");
498908aba0aeSMartin Matuska 
4990eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold);
4991eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold_flags);
4992eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold_obj);
4993eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_hold_obj_flags);
4994eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_own);
4995eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_own_obj);
4996eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_name);
4997eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_rele);
4998eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_rele_flags);
4999eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_disown);
5000eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_tryown);
5001eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_create_sync);
5002eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_create_sync_dd);
5003eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_snapshot_check);
5004eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_snapshot_sync);
5005eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_promote);
5006eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_user_hold);
5007eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_user_release);
5008eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_get_holds);
5009eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_get_blkptr);
5010eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_get_spa);
5011eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_modified_since_snap);
5012eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_space_written);
5013eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_space_wouldfree);
5014eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_sync);
5015eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_block_born);
5016eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_block_kill);
5017eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_dirty);
5018eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_stats);
5019eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_fast_stat);
5020eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_space);
5021eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_fsid_guid);
5022eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dsobj_to_dsname);
5023eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_check_quota);
5024eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_clone_swap_check_impl);
5025eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dataset_clone_swap_sync_impl);
5026