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