xref: /freebsd/sys/contrib/openzfs/module/zfs/dsl_dir.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2013 Martin Matuska. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2014 Joyent, Inc. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27eda14cbcSMatt Macy  * Copyright (c) 2016 Actifio, Inc. All rights reserved.
28eda14cbcSMatt Macy  * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
29eda14cbcSMatt Macy  */
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy #include <sys/dmu.h>
32eda14cbcSMatt Macy #include <sys/dmu_objset.h>
33eda14cbcSMatt Macy #include <sys/dmu_tx.h>
34eda14cbcSMatt Macy #include <sys/dsl_dataset.h>
35eda14cbcSMatt Macy #include <sys/dsl_dir.h>
36eda14cbcSMatt Macy #include <sys/dsl_prop.h>
37eda14cbcSMatt Macy #include <sys/dsl_synctask.h>
38eda14cbcSMatt Macy #include <sys/dsl_deleg.h>
39eda14cbcSMatt Macy #include <sys/dmu_impl.h>
40eda14cbcSMatt Macy #include <sys/spa.h>
41eda14cbcSMatt Macy #include <sys/spa_impl.h>
42eda14cbcSMatt Macy #include <sys/metaslab.h>
43eda14cbcSMatt Macy #include <sys/zap.h>
44eda14cbcSMatt Macy #include <sys/zio.h>
45eda14cbcSMatt Macy #include <sys/arc.h>
46eda14cbcSMatt Macy #include <sys/sunddi.h>
47eda14cbcSMatt Macy #include <sys/zfeature.h>
48eda14cbcSMatt Macy #include <sys/policy.h>
49eac7052fSMatt Macy #include <sys/zfs_vfsops.h>
50eda14cbcSMatt Macy #include <sys/zfs_znode.h>
51eda14cbcSMatt Macy #include <sys/zvol.h>
52eda14cbcSMatt Macy #include <sys/zthr.h>
53eda14cbcSMatt Macy #include "zfs_namecheck.h"
54eda14cbcSMatt Macy #include "zfs_prop.h"
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy /*
57dbd5678dSMartin Matuska  * This controls if we verify the ZVOL quota or not.
58dbd5678dSMartin Matuska  * Currently, quotas are not implemented for ZVOLs.
59dbd5678dSMartin Matuska  * The quota size is the size of the ZVOL.
60dbd5678dSMartin Matuska  * The size of the volume already implies the ZVOL size quota.
61dbd5678dSMartin Matuska  * The quota mechanism can introduce a significant performance drop.
62dbd5678dSMartin Matuska  */
63dbd5678dSMartin Matuska static int zvol_enforce_quotas = B_TRUE;
64dbd5678dSMartin Matuska 
65dbd5678dSMartin Matuska /*
66eda14cbcSMatt Macy  * Filesystem and Snapshot Limits
67eda14cbcSMatt Macy  * ------------------------------
68eda14cbcSMatt Macy  *
69eda14cbcSMatt Macy  * These limits are used to restrict the number of filesystems and/or snapshots
70eda14cbcSMatt Macy  * that can be created at a given level in the tree or below. A typical
71eda14cbcSMatt Macy  * use-case is with a delegated dataset where the administrator wants to ensure
72eda14cbcSMatt Macy  * that a user within the zone is not creating too many additional filesystems
73eda14cbcSMatt Macy  * or snapshots, even though they're not exceeding their space quota.
74eda14cbcSMatt Macy  *
75eda14cbcSMatt Macy  * The filesystem and snapshot counts are stored as extensible properties. This
76eda14cbcSMatt Macy  * capability is controlled by a feature flag and must be enabled to be used.
77eda14cbcSMatt Macy  * Once enabled, the feature is not active until the first limit is set. At
78eda14cbcSMatt Macy  * that point, future operations to create/destroy filesystems or snapshots
79eda14cbcSMatt Macy  * will validate and update the counts.
80eda14cbcSMatt Macy  *
81eda14cbcSMatt Macy  * Because the count properties will not exist before the feature is active,
82eda14cbcSMatt Macy  * the counts are updated when a limit is first set on an uninitialized
83eda14cbcSMatt Macy  * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
84eda14cbcSMatt Macy  * all of the nested filesystems/snapshots. Thus, a new leaf node has a
85eda14cbcSMatt Macy  * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
86eda14cbcSMatt Macy  * snapshot count properties on a node indicate uninitialized counts on that
87eda14cbcSMatt Macy  * node.) When first setting a limit on an uninitialized node, the code starts
88eda14cbcSMatt Macy  * at the filesystem with the new limit and descends into all sub-filesystems
89eda14cbcSMatt Macy  * to add the count properties.
90eda14cbcSMatt Macy  *
91eda14cbcSMatt Macy  * In practice this is lightweight since a limit is typically set when the
92eda14cbcSMatt Macy  * filesystem is created and thus has no children. Once valid, changing the
93eda14cbcSMatt Macy  * limit value won't require a re-traversal since the counts are already valid.
94eda14cbcSMatt Macy  * When recursively fixing the counts, if a node with a limit is encountered
95eda14cbcSMatt Macy  * during the descent, the counts are known to be valid and there is no need to
96eda14cbcSMatt Macy  * descend into that filesystem's children. The counts on filesystems above the
97eda14cbcSMatt Macy  * one with the new limit will still be uninitialized, unless a limit is
98eda14cbcSMatt Macy  * eventually set on one of those filesystems. The counts are always recursively
99eda14cbcSMatt Macy  * updated when a limit is set on a dataset, unless there is already a limit.
100eda14cbcSMatt Macy  * When a new limit value is set on a filesystem with an existing limit, it is
101eda14cbcSMatt Macy  * possible for the new limit to be less than the current count at that level
102eda14cbcSMatt Macy  * since a user who can change the limit is also allowed to exceed the limit.
103eda14cbcSMatt Macy  *
104eda14cbcSMatt Macy  * Once the feature is active, then whenever a filesystem or snapshot is
105eda14cbcSMatt Macy  * created, the code recurses up the tree, validating the new count against the
106eda14cbcSMatt Macy  * limit at each initialized level. In practice, most levels will not have a
107eda14cbcSMatt Macy  * limit set. If there is a limit at any initialized level up the tree, the
108eda14cbcSMatt Macy  * check must pass or the creation will fail. Likewise, when a filesystem or
109eda14cbcSMatt Macy  * snapshot is destroyed, the counts are recursively adjusted all the way up
110eda14cbcSMatt Macy  * the initialized nodes in the tree. Renaming a filesystem into different point
111eda14cbcSMatt Macy  * in the tree will first validate, then update the counts on each branch up to
112eda14cbcSMatt Macy  * the common ancestor. A receive will also validate the counts and then update
113eda14cbcSMatt Macy  * them.
114eda14cbcSMatt Macy  *
115eda14cbcSMatt Macy  * An exception to the above behavior is that the limit is not enforced if the
116eda14cbcSMatt Macy  * user has permission to modify the limit. This is primarily so that
117eda14cbcSMatt Macy  * recursive snapshots in the global zone always work. We want to prevent a
118eda14cbcSMatt Macy  * denial-of-service in which a lower level delegated dataset could max out its
119eda14cbcSMatt Macy  * limit and thus block recursive snapshots from being taken in the global zone.
120eda14cbcSMatt Macy  * Because of this, it is possible for the snapshot count to be over the limit
121eda14cbcSMatt Macy  * and snapshots taken in the global zone could cause a lower level dataset to
122eda14cbcSMatt Macy  * hit or exceed its limit. The administrator taking the global zone recursive
123eda14cbcSMatt Macy  * snapshot should be aware of this side-effect and behave accordingly.
124eda14cbcSMatt Macy  * For consistency, the filesystem limit is also not enforced if the user can
125eda14cbcSMatt Macy  * modify the limit.
126eda14cbcSMatt Macy  *
127eda14cbcSMatt Macy  * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
128eda14cbcSMatt Macy  * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
129eda14cbcSMatt Macy  * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
130eda14cbcSMatt Macy  * dsl_dir_init_fs_ss_count().
131eda14cbcSMatt Macy  */
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy typedef struct ddulrt_arg {
136eda14cbcSMatt Macy 	dsl_dir_t	*ddulrta_dd;
137eda14cbcSMatt Macy 	uint64_t	ddlrta_txg;
138eda14cbcSMatt Macy } ddulrt_arg_t;
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy static void
141eda14cbcSMatt Macy dsl_dir_evict_async(void *dbu)
142eda14cbcSMatt Macy {
143eda14cbcSMatt Macy 	dsl_dir_t *dd = dbu;
144eda14cbcSMatt Macy 	int t;
145eda14cbcSMatt Macy 	dsl_pool_t *dp __maybe_unused = dd->dd_pool;
146eda14cbcSMatt Macy 
147eda14cbcSMatt Macy 	dd->dd_dbuf = NULL;
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 	for (t = 0; t < TXG_SIZE; t++) {
150eda14cbcSMatt Macy 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
151eda14cbcSMatt Macy 		ASSERT(dd->dd_tempreserved[t] == 0);
152eda14cbcSMatt Macy 		ASSERT(dd->dd_space_towrite[t] == 0);
153eda14cbcSMatt Macy 	}
154eda14cbcSMatt Macy 
155eda14cbcSMatt Macy 	if (dd->dd_parent)
156eda14cbcSMatt Macy 		dsl_dir_async_rele(dd->dd_parent, dd);
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 	spa_async_close(dd->dd_pool->dp_spa, dd);
159eda14cbcSMatt Macy 
160eda14cbcSMatt Macy 	if (dsl_deadlist_is_open(&dd->dd_livelist))
161eda14cbcSMatt Macy 		dsl_dir_livelist_close(dd);
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 	dsl_prop_fini(dd);
164eda14cbcSMatt Macy 	cv_destroy(&dd->dd_activity_cv);
165eda14cbcSMatt Macy 	mutex_destroy(&dd->dd_activity_lock);
166eda14cbcSMatt Macy 	mutex_destroy(&dd->dd_lock);
167eda14cbcSMatt Macy 	kmem_free(dd, sizeof (dsl_dir_t));
168eda14cbcSMatt Macy }
169eda14cbcSMatt Macy 
170eda14cbcSMatt Macy int
171eda14cbcSMatt Macy dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
172a0b956f5SMartin Matuska     const char *tail, const void *tag, dsl_dir_t **ddp)
173eda14cbcSMatt Macy {
174eda14cbcSMatt Macy 	dmu_buf_t *dbuf;
175eda14cbcSMatt Macy 	dsl_dir_t *dd;
176eda14cbcSMatt Macy 	dmu_object_info_t doi;
177eda14cbcSMatt Macy 	int err;
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
180eda14cbcSMatt Macy 
181eda14cbcSMatt Macy 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
182eda14cbcSMatt Macy 	if (err != 0)
183eda14cbcSMatt Macy 		return (err);
184eda14cbcSMatt Macy 	dd = dmu_buf_get_user(dbuf);
185eda14cbcSMatt Macy 
186eda14cbcSMatt Macy 	dmu_object_info_from_db(dbuf, &doi);
187eda14cbcSMatt Macy 	ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
188eda14cbcSMatt Macy 	ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy 	if (dd == NULL) {
191eda14cbcSMatt Macy 		dsl_dir_t *winner;
192eda14cbcSMatt Macy 
193eda14cbcSMatt Macy 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
194eda14cbcSMatt Macy 		dd->dd_object = ddobj;
195eda14cbcSMatt Macy 		dd->dd_dbuf = dbuf;
196eda14cbcSMatt Macy 		dd->dd_pool = dp;
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
199eda14cbcSMatt Macy 		mutex_init(&dd->dd_activity_lock, NULL, MUTEX_DEFAULT, NULL);
200eda14cbcSMatt Macy 		cv_init(&dd->dd_activity_cv, NULL, CV_DEFAULT, NULL);
201eda14cbcSMatt Macy 		dsl_prop_init(dd);
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy 		if (dsl_dir_is_zapified(dd)) {
204eda14cbcSMatt Macy 			err = zap_lookup(dp->dp_meta_objset,
205eda14cbcSMatt Macy 			    ddobj, DD_FIELD_CRYPTO_KEY_OBJ,
206eda14cbcSMatt Macy 			    sizeof (uint64_t), 1, &dd->dd_crypto_obj);
207eda14cbcSMatt Macy 			if (err == 0) {
208eda14cbcSMatt Macy 				/* check for on-disk format errata */
209eda14cbcSMatt Macy 				if (dsl_dir_incompatible_encryption_version(
210eda14cbcSMatt Macy 				    dd)) {
211eda14cbcSMatt Macy 					dp->dp_spa->spa_errata =
212eda14cbcSMatt Macy 					    ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
213eda14cbcSMatt Macy 				}
214eda14cbcSMatt Macy 			} else if (err != ENOENT) {
215eda14cbcSMatt Macy 				goto errout;
216eda14cbcSMatt Macy 			}
217eda14cbcSMatt Macy 		}
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 		if (dsl_dir_phys(dd)->dd_parent_obj) {
220eda14cbcSMatt Macy 			err = dsl_dir_hold_obj(dp,
221eda14cbcSMatt Macy 			    dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
222eda14cbcSMatt Macy 			    &dd->dd_parent);
223eda14cbcSMatt Macy 			if (err != 0)
224eda14cbcSMatt Macy 				goto errout;
225eda14cbcSMatt Macy 			if (tail) {
226eda14cbcSMatt Macy #ifdef ZFS_DEBUG
227eda14cbcSMatt Macy 				uint64_t foundobj;
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 				err = zap_lookup(dp->dp_meta_objset,
230eda14cbcSMatt Macy 				    dsl_dir_phys(dd->dd_parent)->
231eda14cbcSMatt Macy 				    dd_child_dir_zapobj, tail,
232eda14cbcSMatt Macy 				    sizeof (foundobj), 1, &foundobj);
233eda14cbcSMatt Macy 				ASSERT(err || foundobj == ddobj);
234eda14cbcSMatt Macy #endif
235eda14cbcSMatt Macy 				(void) strlcpy(dd->dd_myname, tail,
236eda14cbcSMatt Macy 				    sizeof (dd->dd_myname));
237eda14cbcSMatt Macy 			} else {
238eda14cbcSMatt Macy 				err = zap_value_search(dp->dp_meta_objset,
239eda14cbcSMatt Macy 				    dsl_dir_phys(dd->dd_parent)->
240eda14cbcSMatt Macy 				    dd_child_dir_zapobj,
241eda14cbcSMatt Macy 				    ddobj, 0, dd->dd_myname);
242eda14cbcSMatt Macy 			}
243eda14cbcSMatt Macy 			if (err != 0)
244eda14cbcSMatt Macy 				goto errout;
245eda14cbcSMatt Macy 		} else {
246eda14cbcSMatt Macy 			(void) strlcpy(dd->dd_myname, spa_name(dp->dp_spa),
247eda14cbcSMatt Macy 			    sizeof (dd->dd_myname));
248eda14cbcSMatt Macy 		}
249eda14cbcSMatt Macy 
250eda14cbcSMatt Macy 		if (dsl_dir_is_clone(dd)) {
251eda14cbcSMatt Macy 			dmu_buf_t *origin_bonus;
252eda14cbcSMatt Macy 			dsl_dataset_phys_t *origin_phys;
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy 			/*
255eda14cbcSMatt Macy 			 * We can't open the origin dataset, because
256eda14cbcSMatt Macy 			 * that would require opening this dsl_dir.
257eda14cbcSMatt Macy 			 * Just look at its phys directly instead.
258eda14cbcSMatt Macy 			 */
259eda14cbcSMatt Macy 			err = dmu_bonus_hold(dp->dp_meta_objset,
260eda14cbcSMatt Macy 			    dsl_dir_phys(dd)->dd_origin_obj, FTAG,
261eda14cbcSMatt Macy 			    &origin_bonus);
262eda14cbcSMatt Macy 			if (err != 0)
263eda14cbcSMatt Macy 				goto errout;
264eda14cbcSMatt Macy 			origin_phys = origin_bonus->db_data;
265eda14cbcSMatt Macy 			dd->dd_origin_txg =
266eda14cbcSMatt Macy 			    origin_phys->ds_creation_txg;
267eda14cbcSMatt Macy 			dmu_buf_rele(origin_bonus, FTAG);
268eda14cbcSMatt Macy 			if (dsl_dir_is_zapified(dd)) {
269eda14cbcSMatt Macy 				uint64_t obj;
270eda14cbcSMatt Macy 				err = zap_lookup(dp->dp_meta_objset,
271eda14cbcSMatt Macy 				    dd->dd_object, DD_FIELD_LIVELIST,
272eda14cbcSMatt Macy 				    sizeof (uint64_t), 1, &obj);
273eda14cbcSMatt Macy 				if (err == 0)
274eda14cbcSMatt Macy 					dsl_dir_livelist_open(dd, obj);
275eda14cbcSMatt Macy 				else if (err != ENOENT)
276eda14cbcSMatt Macy 					goto errout;
277eda14cbcSMatt Macy 			}
278eda14cbcSMatt Macy 		}
279eda14cbcSMatt Macy 
28008aba0aeSMartin Matuska 		if (dsl_dir_is_zapified(dd)) {
281271171e0SMartin Matuska 			inode_timespec_t t = {0};
282be181ee2SMartin Matuska 			(void) zap_lookup(dp->dp_meta_objset, ddobj,
283c7046f76SMartin Matuska 			    DD_FIELD_SNAPSHOTS_CHANGED,
284271171e0SMartin Matuska 			    sizeof (uint64_t),
28508aba0aeSMartin Matuska 			    sizeof (inode_timespec_t) / sizeof (uint64_t),
28608aba0aeSMartin Matuska 			    &t);
287271171e0SMartin Matuska 			dd->dd_snap_cmtime = t;
28808aba0aeSMartin Matuska 		}
289271171e0SMartin Matuska 
290eda14cbcSMatt Macy 		dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
291eda14cbcSMatt Macy 		    &dd->dd_dbuf);
292eda14cbcSMatt Macy 		winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
293eda14cbcSMatt Macy 		if (winner != NULL) {
294eda14cbcSMatt Macy 			if (dd->dd_parent)
295eda14cbcSMatt Macy 				dsl_dir_rele(dd->dd_parent, dd);
296eda14cbcSMatt Macy 			if (dsl_deadlist_is_open(&dd->dd_livelist))
297eda14cbcSMatt Macy 				dsl_dir_livelist_close(dd);
298eda14cbcSMatt Macy 			dsl_prop_fini(dd);
299eda14cbcSMatt Macy 			cv_destroy(&dd->dd_activity_cv);
300eda14cbcSMatt Macy 			mutex_destroy(&dd->dd_activity_lock);
301eda14cbcSMatt Macy 			mutex_destroy(&dd->dd_lock);
302eda14cbcSMatt Macy 			kmem_free(dd, sizeof (dsl_dir_t));
303eda14cbcSMatt Macy 			dd = winner;
304eda14cbcSMatt Macy 		} else {
305eda14cbcSMatt Macy 			spa_open_ref(dp->dp_spa, dd);
306eda14cbcSMatt Macy 		}
307eda14cbcSMatt Macy 	}
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 	/*
310eda14cbcSMatt Macy 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
311eda14cbcSMatt Macy 	 * holds on the spa.  We need the open-to-close holds because
312eda14cbcSMatt Macy 	 * otherwise the spa_refcnt wouldn't change when we open a
313eda14cbcSMatt Macy 	 * dir which the spa also has open, so we could incorrectly
314eda14cbcSMatt Macy 	 * think it was OK to unload/export/destroy the pool.  We need
315eda14cbcSMatt Macy 	 * the instantiate-to-evict hold because the dsl_dir_t has a
316eda14cbcSMatt Macy 	 * pointer to the dd_pool, which has a pointer to the spa_t.
317eda14cbcSMatt Macy 	 */
318eda14cbcSMatt Macy 	spa_open_ref(dp->dp_spa, tag);
319eda14cbcSMatt Macy 	ASSERT3P(dd->dd_pool, ==, dp);
320eda14cbcSMatt Macy 	ASSERT3U(dd->dd_object, ==, ddobj);
321eda14cbcSMatt Macy 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
322eda14cbcSMatt Macy 	*ddp = dd;
323eda14cbcSMatt Macy 	return (0);
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy errout:
326eda14cbcSMatt Macy 	if (dd->dd_parent)
327eda14cbcSMatt Macy 		dsl_dir_rele(dd->dd_parent, dd);
328eda14cbcSMatt Macy 	if (dsl_deadlist_is_open(&dd->dd_livelist))
329eda14cbcSMatt Macy 		dsl_dir_livelist_close(dd);
330eda14cbcSMatt Macy 	dsl_prop_fini(dd);
331eda14cbcSMatt Macy 	cv_destroy(&dd->dd_activity_cv);
332eda14cbcSMatt Macy 	mutex_destroy(&dd->dd_activity_lock);
333eda14cbcSMatt Macy 	mutex_destroy(&dd->dd_lock);
334eda14cbcSMatt Macy 	kmem_free(dd, sizeof (dsl_dir_t));
335eda14cbcSMatt Macy 	dmu_buf_rele(dbuf, tag);
336eda14cbcSMatt Macy 	return (err);
337eda14cbcSMatt Macy }
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy void
340a0b956f5SMartin Matuska dsl_dir_rele(dsl_dir_t *dd, const void *tag)
341eda14cbcSMatt Macy {
342eda14cbcSMatt Macy 	dprintf_dd(dd, "%s\n", "");
343eda14cbcSMatt Macy 	spa_close(dd->dd_pool->dp_spa, tag);
344eda14cbcSMatt Macy 	dmu_buf_rele(dd->dd_dbuf, tag);
345eda14cbcSMatt Macy }
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy /*
348eda14cbcSMatt Macy  * Remove a reference to the given dsl dir that is being asynchronously
349eda14cbcSMatt Macy  * released.  Async releases occur from a taskq performing eviction of
350eda14cbcSMatt Macy  * dsl datasets and dirs.  This process is identical to a normal release
351eda14cbcSMatt Macy  * with the exception of using the async API for releasing the reference on
352eda14cbcSMatt Macy  * the spa.
353eda14cbcSMatt Macy  */
354eda14cbcSMatt Macy void
355a0b956f5SMartin Matuska dsl_dir_async_rele(dsl_dir_t *dd, const void *tag)
356eda14cbcSMatt Macy {
357eda14cbcSMatt Macy 	dprintf_dd(dd, "%s\n", "");
358eda14cbcSMatt Macy 	spa_async_close(dd->dd_pool->dp_spa, tag);
359eda14cbcSMatt Macy 	dmu_buf_rele(dd->dd_dbuf, tag);
360eda14cbcSMatt Macy }
361eda14cbcSMatt Macy 
362eda14cbcSMatt Macy /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
363eda14cbcSMatt Macy void
364eda14cbcSMatt Macy dsl_dir_name(dsl_dir_t *dd, char *buf)
365eda14cbcSMatt Macy {
366eda14cbcSMatt Macy 	if (dd->dd_parent) {
367eda14cbcSMatt Macy 		dsl_dir_name(dd->dd_parent, buf);
368eda14cbcSMatt Macy 		VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
369eda14cbcSMatt Macy 		    ZFS_MAX_DATASET_NAME_LEN);
370eda14cbcSMatt Macy 	} else {
371eda14cbcSMatt Macy 		buf[0] = '\0';
372eda14cbcSMatt Macy 	}
373eda14cbcSMatt Macy 	if (!MUTEX_HELD(&dd->dd_lock)) {
374eda14cbcSMatt Macy 		/*
375eda14cbcSMatt Macy 		 * recursive mutex so that we can use
376eda14cbcSMatt Macy 		 * dprintf_dd() with dd_lock held
377eda14cbcSMatt Macy 		 */
378eda14cbcSMatt Macy 		mutex_enter(&dd->dd_lock);
379eda14cbcSMatt Macy 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
380eda14cbcSMatt Macy 		    <, ZFS_MAX_DATASET_NAME_LEN);
381eda14cbcSMatt Macy 		mutex_exit(&dd->dd_lock);
382eda14cbcSMatt Macy 	} else {
383eda14cbcSMatt Macy 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
384eda14cbcSMatt Macy 		    <, ZFS_MAX_DATASET_NAME_LEN);
385eda14cbcSMatt Macy 	}
386eda14cbcSMatt Macy }
387eda14cbcSMatt Macy 
388eda14cbcSMatt Macy /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
389eda14cbcSMatt Macy int
390eda14cbcSMatt Macy dsl_dir_namelen(dsl_dir_t *dd)
391eda14cbcSMatt Macy {
392eda14cbcSMatt Macy 	int result = 0;
393eda14cbcSMatt Macy 
394eda14cbcSMatt Macy 	if (dd->dd_parent) {
395eda14cbcSMatt Macy 		/* parent's name + 1 for the "/" */
396eda14cbcSMatt Macy 		result = dsl_dir_namelen(dd->dd_parent) + 1;
397eda14cbcSMatt Macy 	}
398eda14cbcSMatt Macy 
399eda14cbcSMatt Macy 	if (!MUTEX_HELD(&dd->dd_lock)) {
400eda14cbcSMatt Macy 		/* see dsl_dir_name */
401eda14cbcSMatt Macy 		mutex_enter(&dd->dd_lock);
402eda14cbcSMatt Macy 		result += strlen(dd->dd_myname);
403eda14cbcSMatt Macy 		mutex_exit(&dd->dd_lock);
404eda14cbcSMatt Macy 	} else {
405eda14cbcSMatt Macy 		result += strlen(dd->dd_myname);
406eda14cbcSMatt Macy 	}
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 	return (result);
409eda14cbcSMatt Macy }
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy static int
412eda14cbcSMatt Macy getcomponent(const char *path, char *component, const char **nextp)
413eda14cbcSMatt Macy {
414eda14cbcSMatt Macy 	char *p;
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy 	if ((path == NULL) || (path[0] == '\0'))
417eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
418eda14cbcSMatt Macy 	/* This would be a good place to reserve some namespace... */
419eda14cbcSMatt Macy 	p = strpbrk(path, "/@");
420eda14cbcSMatt Macy 	if (p && (p[1] == '/' || p[1] == '@')) {
421eda14cbcSMatt Macy 		/* two separators in a row */
422eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
423eda14cbcSMatt Macy 	}
424eda14cbcSMatt Macy 	if (p == NULL || p == path) {
425eda14cbcSMatt Macy 		/*
426eda14cbcSMatt Macy 		 * if the first thing is an @ or /, it had better be an
427eda14cbcSMatt Macy 		 * @ and it had better not have any more ats or slashes,
428eda14cbcSMatt Macy 		 * and it had better have something after the @.
429eda14cbcSMatt Macy 		 */
430eda14cbcSMatt Macy 		if (p != NULL &&
431eda14cbcSMatt Macy 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
432eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
433eda14cbcSMatt Macy 		if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
434eda14cbcSMatt Macy 			return (SET_ERROR(ENAMETOOLONG));
435eda14cbcSMatt Macy 		(void) strlcpy(component, path, ZFS_MAX_DATASET_NAME_LEN);
436eda14cbcSMatt Macy 		p = NULL;
437eda14cbcSMatt Macy 	} else if (p[0] == '/') {
438eda14cbcSMatt Macy 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
439eda14cbcSMatt Macy 			return (SET_ERROR(ENAMETOOLONG));
440be181ee2SMartin Matuska 		(void) strlcpy(component, path, p - path + 1);
441eda14cbcSMatt Macy 		p++;
442eda14cbcSMatt Macy 	} else if (p[0] == '@') {
443eda14cbcSMatt Macy 		/*
444eda14cbcSMatt Macy 		 * if the next separator is an @, there better not be
445eda14cbcSMatt Macy 		 * any more slashes.
446eda14cbcSMatt Macy 		 */
447eda14cbcSMatt Macy 		if (strchr(path, '/'))
448eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
449eda14cbcSMatt Macy 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
450eda14cbcSMatt Macy 			return (SET_ERROR(ENAMETOOLONG));
451be181ee2SMartin Matuska 		(void) strlcpy(component, path, p - path + 1);
452eda14cbcSMatt Macy 	} else {
453eda14cbcSMatt Macy 		panic("invalid p=%p", (void *)p);
454eda14cbcSMatt Macy 	}
455eda14cbcSMatt Macy 	*nextp = p;
456eda14cbcSMatt Macy 	return (0);
457eda14cbcSMatt Macy }
458eda14cbcSMatt Macy 
459eda14cbcSMatt Macy /*
460eda14cbcSMatt Macy  * Return the dsl_dir_t, and possibly the last component which couldn't
461eda14cbcSMatt Macy  * be found in *tail.  The name must be in the specified dsl_pool_t.  This
462eda14cbcSMatt Macy  * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
463eda14cbcSMatt Macy  * path is bogus, or if tail==NULL and we couldn't parse the whole name.
464eda14cbcSMatt Macy  * (*tail)[0] == '@' means that the last component is a snapshot.
465eda14cbcSMatt Macy  */
466eda14cbcSMatt Macy int
467a0b956f5SMartin Matuska dsl_dir_hold(dsl_pool_t *dp, const char *name, const void *tag,
468eda14cbcSMatt Macy     dsl_dir_t **ddp, const char **tailp)
469eda14cbcSMatt Macy {
470eda14cbcSMatt Macy 	char *buf;
471eda14cbcSMatt Macy 	const char *spaname, *next, *nextnext = NULL;
472eda14cbcSMatt Macy 	int err;
473eda14cbcSMatt Macy 	dsl_dir_t *dd;
474eda14cbcSMatt Macy 	uint64_t ddobj;
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
477eda14cbcSMatt Macy 	err = getcomponent(name, buf, &next);
478eda14cbcSMatt Macy 	if (err != 0)
479eda14cbcSMatt Macy 		goto error;
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	/* Make sure the name is in the specified pool. */
482eda14cbcSMatt Macy 	spaname = spa_name(dp->dp_spa);
483eda14cbcSMatt Macy 	if (strcmp(buf, spaname) != 0) {
484eda14cbcSMatt Macy 		err = SET_ERROR(EXDEV);
485eda14cbcSMatt Macy 		goto error;
486eda14cbcSMatt Macy 	}
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
489eda14cbcSMatt Macy 
490eda14cbcSMatt Macy 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
491eda14cbcSMatt Macy 	if (err != 0) {
492eda14cbcSMatt Macy 		goto error;
493eda14cbcSMatt Macy 	}
494eda14cbcSMatt Macy 
495eda14cbcSMatt Macy 	while (next != NULL) {
496eda14cbcSMatt Macy 		dsl_dir_t *child_dd;
497eda14cbcSMatt Macy 		err = getcomponent(next, buf, &nextnext);
498eda14cbcSMatt Macy 		if (err != 0)
499eda14cbcSMatt Macy 			break;
500eda14cbcSMatt Macy 		ASSERT(next[0] != '\0');
501eda14cbcSMatt Macy 		if (next[0] == '@')
502eda14cbcSMatt Macy 			break;
503eda14cbcSMatt Macy 		dprintf("looking up %s in obj%lld\n",
50433b8c039SMartin Matuska 		    buf, (longlong_t)dsl_dir_phys(dd)->dd_child_dir_zapobj);
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy 		err = zap_lookup(dp->dp_meta_objset,
507eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_child_dir_zapobj,
508eda14cbcSMatt Macy 		    buf, sizeof (ddobj), 1, &ddobj);
509eda14cbcSMatt Macy 		if (err != 0) {
510eda14cbcSMatt Macy 			if (err == ENOENT)
511eda14cbcSMatt Macy 				err = 0;
512eda14cbcSMatt Macy 			break;
513eda14cbcSMatt Macy 		}
514eda14cbcSMatt Macy 
515eda14cbcSMatt Macy 		err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
516eda14cbcSMatt Macy 		if (err != 0)
517eda14cbcSMatt Macy 			break;
518eda14cbcSMatt Macy 		dsl_dir_rele(dd, tag);
519eda14cbcSMatt Macy 		dd = child_dd;
520eda14cbcSMatt Macy 		next = nextnext;
521eda14cbcSMatt Macy 	}
522eda14cbcSMatt Macy 
523eda14cbcSMatt Macy 	if (err != 0) {
524eda14cbcSMatt Macy 		dsl_dir_rele(dd, tag);
525eda14cbcSMatt Macy 		goto error;
526eda14cbcSMatt Macy 	}
527eda14cbcSMatt Macy 
528eda14cbcSMatt Macy 	/*
529eda14cbcSMatt Macy 	 * It's an error if there's more than one component left, or
530eda14cbcSMatt Macy 	 * tailp==NULL and there's any component left.
531eda14cbcSMatt Macy 	 */
532eda14cbcSMatt Macy 	if (next != NULL &&
533eda14cbcSMatt Macy 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
534eda14cbcSMatt Macy 		/* bad path name */
535eda14cbcSMatt Macy 		dsl_dir_rele(dd, tag);
536eda14cbcSMatt Macy 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
537eda14cbcSMatt Macy 		err = SET_ERROR(ENOENT);
538eda14cbcSMatt Macy 	}
539eda14cbcSMatt Macy 	if (tailp != NULL)
540eda14cbcSMatt Macy 		*tailp = next;
541eda14cbcSMatt Macy 	if (err == 0)
542eda14cbcSMatt Macy 		*ddp = dd;
543eda14cbcSMatt Macy error:
544eda14cbcSMatt Macy 	kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN);
545eda14cbcSMatt Macy 	return (err);
546eda14cbcSMatt Macy }
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy /*
549eda14cbcSMatt Macy  * If the counts are already initialized for this filesystem and its
550eda14cbcSMatt Macy  * descendants then do nothing, otherwise initialize the counts.
551eda14cbcSMatt Macy  *
552eda14cbcSMatt Macy  * The counts on this filesystem, and those below, may be uninitialized due to
553eda14cbcSMatt Macy  * either the use of a pre-existing pool which did not support the
554eda14cbcSMatt Macy  * filesystem/snapshot limit feature, or one in which the feature had not yet
555eda14cbcSMatt Macy  * been enabled.
556eda14cbcSMatt Macy  *
557eda14cbcSMatt Macy  * Recursively descend the filesystem tree and update the filesystem/snapshot
558eda14cbcSMatt Macy  * counts on each filesystem below, then update the cumulative count on the
559eda14cbcSMatt Macy  * current filesystem. If the filesystem already has a count set on it,
560eda14cbcSMatt Macy  * then we know that its counts, and the counts on the filesystems below it,
561eda14cbcSMatt Macy  * are already correct, so we don't have to update this filesystem.
562eda14cbcSMatt Macy  */
563eda14cbcSMatt Macy static void
564eda14cbcSMatt Macy dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
565eda14cbcSMatt Macy {
566eda14cbcSMatt Macy 	uint64_t my_fs_cnt = 0;
567eda14cbcSMatt Macy 	uint64_t my_ss_cnt = 0;
568eda14cbcSMatt Macy 	dsl_pool_t *dp = dd->dd_pool;
569eda14cbcSMatt Macy 	objset_t *os = dp->dp_meta_objset;
570eda14cbcSMatt Macy 	zap_cursor_t *zc;
571eda14cbcSMatt Macy 	zap_attribute_t *za;
572eda14cbcSMatt Macy 	dsl_dataset_t *ds;
573eda14cbcSMatt Macy 
574eda14cbcSMatt Macy 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
575eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dp));
576eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
577eda14cbcSMatt Macy 
578eda14cbcSMatt Macy 	dsl_dir_zapify(dd, tx);
579eda14cbcSMatt Macy 
580eda14cbcSMatt Macy 	/*
581eda14cbcSMatt Macy 	 * If the filesystem count has already been initialized then we
582eda14cbcSMatt Macy 	 * don't need to recurse down any further.
583eda14cbcSMatt Macy 	 */
584eda14cbcSMatt Macy 	if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
585eda14cbcSMatt Macy 		return;
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
588eda14cbcSMatt Macy 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
589eda14cbcSMatt Macy 
590eda14cbcSMatt Macy 	/* Iterate my child dirs */
591eda14cbcSMatt Macy 	for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
592eda14cbcSMatt Macy 	    zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
593eda14cbcSMatt Macy 		dsl_dir_t *chld_dd;
594eda14cbcSMatt Macy 		uint64_t count;
595eda14cbcSMatt Macy 
596eda14cbcSMatt Macy 		VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
597eda14cbcSMatt Macy 		    &chld_dd));
598eda14cbcSMatt Macy 
599eda14cbcSMatt Macy 		/*
600ac0bf12eSMatt Macy 		 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets.
601eda14cbcSMatt Macy 		 */
602ac0bf12eSMatt Macy 		if (chld_dd->dd_myname[0] == '$') {
603eda14cbcSMatt Macy 			dsl_dir_rele(chld_dd, FTAG);
604eda14cbcSMatt Macy 			continue;
605eda14cbcSMatt Macy 		}
606eda14cbcSMatt Macy 
607eda14cbcSMatt Macy 		my_fs_cnt++;	/* count this child */
608eda14cbcSMatt Macy 
609eda14cbcSMatt Macy 		dsl_dir_init_fs_ss_count(chld_dd, tx);
610eda14cbcSMatt Macy 
611eda14cbcSMatt Macy 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
612eda14cbcSMatt Macy 		    DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
613eda14cbcSMatt Macy 		my_fs_cnt += count;
614eda14cbcSMatt Macy 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
615eda14cbcSMatt Macy 		    DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
616eda14cbcSMatt Macy 		my_ss_cnt += count;
617eda14cbcSMatt Macy 
618eda14cbcSMatt Macy 		dsl_dir_rele(chld_dd, FTAG);
619eda14cbcSMatt Macy 	}
620eda14cbcSMatt Macy 	zap_cursor_fini(zc);
621eda14cbcSMatt Macy 	/* Count my snapshots (we counted children's snapshots above) */
622eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
623eda14cbcSMatt Macy 	    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
624eda14cbcSMatt Macy 
625eda14cbcSMatt Macy 	for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
626eda14cbcSMatt Macy 	    zap_cursor_retrieve(zc, za) == 0;
627eda14cbcSMatt Macy 	    zap_cursor_advance(zc)) {
628eda14cbcSMatt Macy 		/* Don't count temporary snapshots */
629eda14cbcSMatt Macy 		if (za->za_name[0] != '%')
630eda14cbcSMatt Macy 			my_ss_cnt++;
631eda14cbcSMatt Macy 	}
632eda14cbcSMatt Macy 	zap_cursor_fini(zc);
633eda14cbcSMatt Macy 
634eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
635eda14cbcSMatt Macy 
636eda14cbcSMatt Macy 	kmem_free(zc, sizeof (zap_cursor_t));
637eda14cbcSMatt Macy 	kmem_free(za, sizeof (zap_attribute_t));
638eda14cbcSMatt Macy 
639eda14cbcSMatt Macy 	/* we're in a sync task, update counts */
640eda14cbcSMatt Macy 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
641eda14cbcSMatt Macy 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
642eda14cbcSMatt Macy 	    sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
643eda14cbcSMatt Macy 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
644eda14cbcSMatt Macy 	    sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
645eda14cbcSMatt Macy }
646eda14cbcSMatt Macy 
647eda14cbcSMatt Macy static int
648eda14cbcSMatt Macy dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
649eda14cbcSMatt Macy {
650eda14cbcSMatt Macy 	char *ddname = (char *)arg;
651eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
652eda14cbcSMatt Macy 	dsl_dataset_t *ds;
653eda14cbcSMatt Macy 	dsl_dir_t *dd;
654eda14cbcSMatt Macy 	int error;
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
657eda14cbcSMatt Macy 	if (error != 0)
658eda14cbcSMatt Macy 		return (error);
659eda14cbcSMatt Macy 
660eda14cbcSMatt Macy 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
661eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
662eda14cbcSMatt Macy 		return (SET_ERROR(ENOTSUP));
663eda14cbcSMatt Macy 	}
664eda14cbcSMatt Macy 
665eda14cbcSMatt Macy 	dd = ds->ds_dir;
666eda14cbcSMatt Macy 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
667eda14cbcSMatt Macy 	    dsl_dir_is_zapified(dd) &&
668eda14cbcSMatt Macy 	    zap_contains(dp->dp_meta_objset, dd->dd_object,
669eda14cbcSMatt Macy 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
670eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
671eda14cbcSMatt Macy 		return (SET_ERROR(EALREADY));
672eda14cbcSMatt Macy 	}
673eda14cbcSMatt Macy 
674eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
675eda14cbcSMatt Macy 	return (0);
676eda14cbcSMatt Macy }
677eda14cbcSMatt Macy 
678eda14cbcSMatt Macy static void
679eda14cbcSMatt Macy dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
680eda14cbcSMatt Macy {
681eda14cbcSMatt Macy 	char *ddname = (char *)arg;
682eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
683eda14cbcSMatt Macy 	dsl_dataset_t *ds;
684eda14cbcSMatt Macy 	spa_t *spa;
685eda14cbcSMatt Macy 
686eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
687eda14cbcSMatt Macy 
688eda14cbcSMatt Macy 	spa = dsl_dataset_get_spa(ds);
689eda14cbcSMatt Macy 
690eda14cbcSMatt Macy 	if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
691eda14cbcSMatt Macy 		/*
692eda14cbcSMatt Macy 		 * Since the feature was not active and we're now setting a
693eda14cbcSMatt Macy 		 * limit, increment the feature-active counter so that the
694eda14cbcSMatt Macy 		 * feature becomes active for the first time.
695eda14cbcSMatt Macy 		 *
696eda14cbcSMatt Macy 		 * We are already in a sync task so we can update the MOS.
697eda14cbcSMatt Macy 		 */
698eda14cbcSMatt Macy 		spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
699eda14cbcSMatt Macy 	}
700eda14cbcSMatt Macy 
701eda14cbcSMatt Macy 	/*
702eda14cbcSMatt Macy 	 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
703eda14cbcSMatt Macy 	 * we need to ensure the counts are correct. Descend down the tree from
704eda14cbcSMatt Macy 	 * this point and update all of the counts to be accurate.
705eda14cbcSMatt Macy 	 */
706eda14cbcSMatt Macy 	dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
707eda14cbcSMatt Macy 
708eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
709eda14cbcSMatt Macy }
710eda14cbcSMatt Macy 
711eda14cbcSMatt Macy /*
712eda14cbcSMatt Macy  * Make sure the feature is enabled and activate it if necessary.
713eda14cbcSMatt Macy  * Since we're setting a limit, ensure the on-disk counts are valid.
714eda14cbcSMatt Macy  * This is only called by the ioctl path when setting a limit value.
715eda14cbcSMatt Macy  *
716eda14cbcSMatt Macy  * We do not need to validate the new limit, since users who can change the
717eda14cbcSMatt Macy  * limit are also allowed to exceed the limit.
718eda14cbcSMatt Macy  */
719eda14cbcSMatt Macy int
720eda14cbcSMatt Macy dsl_dir_activate_fs_ss_limit(const char *ddname)
721eda14cbcSMatt Macy {
722eda14cbcSMatt Macy 	int error;
723eda14cbcSMatt Macy 
724eda14cbcSMatt Macy 	error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
725eda14cbcSMatt Macy 	    dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
726eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_RESERVED);
727eda14cbcSMatt Macy 
728eda14cbcSMatt Macy 	if (error == EALREADY)
729eda14cbcSMatt Macy 		error = 0;
730eda14cbcSMatt Macy 
731eda14cbcSMatt Macy 	return (error);
732eda14cbcSMatt Macy }
733eda14cbcSMatt Macy 
734eda14cbcSMatt Macy /*
735eda14cbcSMatt Macy  * Used to determine if the filesystem_limit or snapshot_limit should be
736eda14cbcSMatt Macy  * enforced. We allow the limit to be exceeded if the user has permission to
737eda14cbcSMatt Macy  * write the property value. We pass in the creds that we got in the open
738eda14cbcSMatt Macy  * context since we will always be the GZ root in syncing context. We also have
739eda14cbcSMatt Macy  * to handle the case where we are allowed to change the limit on the current
740eda14cbcSMatt Macy  * dataset, but there may be another limit in the tree above.
741eda14cbcSMatt Macy  *
742eda14cbcSMatt Macy  * We can never modify these two properties within a non-global zone. In
743eda14cbcSMatt Macy  * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
744eda14cbcSMatt Macy  * can't use that function since we are already holding the dp_config_rwlock.
745eda14cbcSMatt Macy  * In addition, we already have the dd and dealing with snapshots is simplified
746eda14cbcSMatt Macy  * in this code.
747eda14cbcSMatt Macy  */
748eda14cbcSMatt Macy 
749eda14cbcSMatt Macy typedef enum {
750eda14cbcSMatt Macy 	ENFORCE_ALWAYS,
751eda14cbcSMatt Macy 	ENFORCE_NEVER,
752eda14cbcSMatt Macy 	ENFORCE_ABOVE
753eda14cbcSMatt Macy } enforce_res_t;
754eda14cbcSMatt Macy 
755eda14cbcSMatt Macy static enforce_res_t
756eda14cbcSMatt Macy dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop,
757eda14cbcSMatt Macy     cred_t *cr, proc_t *proc)
758eda14cbcSMatt Macy {
759eda14cbcSMatt Macy 	enforce_res_t enforce = ENFORCE_ALWAYS;
760eda14cbcSMatt Macy 	uint64_t obj;
761eda14cbcSMatt Macy 	dsl_dataset_t *ds;
762eda14cbcSMatt Macy 	uint64_t zoned;
763eda14cbcSMatt Macy 	const char *zonedstr;
764eda14cbcSMatt Macy 
765eda14cbcSMatt Macy 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
766eda14cbcSMatt Macy 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
767eda14cbcSMatt Macy 
768eda14cbcSMatt Macy #ifdef _KERNEL
769eda14cbcSMatt Macy 	if (crgetzoneid(cr) != GLOBAL_ZONEID)
770eda14cbcSMatt Macy 		return (ENFORCE_ALWAYS);
771eda14cbcSMatt Macy 
772eda14cbcSMatt Macy 	/*
773eda14cbcSMatt Macy 	 * We are checking the saved credentials of the user process, which is
774eda14cbcSMatt Macy 	 * not the current process.  Note that we can't use secpolicy_zfs(),
775eda14cbcSMatt Macy 	 * because it only works if the cred is that of the current process (on
776eda14cbcSMatt Macy 	 * Linux).
777eda14cbcSMatt Macy 	 */
778eda14cbcSMatt Macy 	if (secpolicy_zfs_proc(cr, proc) == 0)
779eda14cbcSMatt Macy 		return (ENFORCE_NEVER);
780e92ffd9bSMartin Matuska #else
781e92ffd9bSMartin Matuska 	(void) proc;
782eda14cbcSMatt Macy #endif
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy 	if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
785eda14cbcSMatt Macy 		return (ENFORCE_ALWAYS);
786eda14cbcSMatt Macy 
787eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dd->dd_pool));
788eda14cbcSMatt Macy 
789eda14cbcSMatt Macy 	if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
790eda14cbcSMatt Macy 		return (ENFORCE_ALWAYS);
791eda14cbcSMatt Macy 
792eda14cbcSMatt Macy 	zonedstr = zfs_prop_to_name(ZFS_PROP_ZONED);
793eda14cbcSMatt Macy 	if (dsl_prop_get_ds(ds, zonedstr, 8, 1, &zoned, NULL) || zoned) {
794eda14cbcSMatt Macy 		/* Only root can access zoned fs's from the GZ */
795eda14cbcSMatt Macy 		enforce = ENFORCE_ALWAYS;
796eda14cbcSMatt Macy 	} else {
797eda14cbcSMatt Macy 		if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
798eda14cbcSMatt Macy 			enforce = ENFORCE_ABOVE;
799eda14cbcSMatt Macy 	}
800eda14cbcSMatt Macy 
801eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
802eda14cbcSMatt Macy 	return (enforce);
803eda14cbcSMatt Macy }
804eda14cbcSMatt Macy 
805eda14cbcSMatt Macy /*
806eda14cbcSMatt Macy  * Check if adding additional child filesystem(s) would exceed any filesystem
807eda14cbcSMatt Macy  * limits or adding additional snapshot(s) would exceed any snapshot limits.
808eda14cbcSMatt Macy  * The prop argument indicates which limit to check.
809eda14cbcSMatt Macy  *
810eda14cbcSMatt Macy  * Note that all filesystem limits up to the root (or the highest
811eda14cbcSMatt Macy  * initialized) filesystem or the given ancestor must be satisfied.
812eda14cbcSMatt Macy  */
813eda14cbcSMatt Macy int
814eda14cbcSMatt Macy dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
815eda14cbcSMatt Macy     dsl_dir_t *ancestor, cred_t *cr, proc_t *proc)
816eda14cbcSMatt Macy {
817eda14cbcSMatt Macy 	objset_t *os = dd->dd_pool->dp_meta_objset;
818eda14cbcSMatt Macy 	uint64_t limit, count;
819a0b956f5SMartin Matuska 	const char *count_prop;
820eda14cbcSMatt Macy 	enforce_res_t enforce;
821eda14cbcSMatt Macy 	int err = 0;
822eda14cbcSMatt Macy 
823eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dd->dd_pool));
824eda14cbcSMatt Macy 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
825eda14cbcSMatt Macy 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
826eda14cbcSMatt Macy 
827dbd5678dSMartin Matuska 	if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
828dbd5678dSMartin Matuska 		/*
829dbd5678dSMartin Matuska 		 * We don't enforce the limit for temporary snapshots. This is
830dbd5678dSMartin Matuska 		 * indicated by a NULL cred_t argument.
831dbd5678dSMartin Matuska 		 */
832dbd5678dSMartin Matuska 		if (cr == NULL)
833dbd5678dSMartin Matuska 			return (0);
834dbd5678dSMartin Matuska 
835dbd5678dSMartin Matuska 		count_prop = DD_FIELD_SNAPSHOT_COUNT;
836dbd5678dSMartin Matuska 	} else {
837dbd5678dSMartin Matuska 		count_prop = DD_FIELD_FILESYSTEM_COUNT;
838dbd5678dSMartin Matuska 	}
839eda14cbcSMatt Macy 	/*
840eda14cbcSMatt Macy 	 * If we're allowed to change the limit, don't enforce the limit
841eda14cbcSMatt Macy 	 * e.g. this can happen if a snapshot is taken by an administrative
842eda14cbcSMatt Macy 	 * user in the global zone (i.e. a recursive snapshot by root).
843eda14cbcSMatt Macy 	 * However, we must handle the case of delegated permissions where we
844eda14cbcSMatt Macy 	 * are allowed to change the limit on the current dataset, but there
845eda14cbcSMatt Macy 	 * is another limit in the tree above.
846eda14cbcSMatt Macy 	 */
847eda14cbcSMatt Macy 	enforce = dsl_enforce_ds_ss_limits(dd, prop, cr, proc);
848eda14cbcSMatt Macy 	if (enforce == ENFORCE_NEVER)
849eda14cbcSMatt Macy 		return (0);
850eda14cbcSMatt Macy 
851eda14cbcSMatt Macy 	/*
852eda14cbcSMatt Macy 	 * e.g. if renaming a dataset with no snapshots, count adjustment
853eda14cbcSMatt Macy 	 * is 0.
854eda14cbcSMatt Macy 	 */
855eda14cbcSMatt Macy 	if (delta == 0)
856eda14cbcSMatt Macy 		return (0);
857eda14cbcSMatt Macy 
858eda14cbcSMatt Macy 	/*
859eda14cbcSMatt Macy 	 * If an ancestor has been provided, stop checking the limit once we
860eda14cbcSMatt Macy 	 * hit that dir. We need this during rename so that we don't overcount
861eda14cbcSMatt Macy 	 * the check once we recurse up to the common ancestor.
862eda14cbcSMatt Macy 	 */
863eda14cbcSMatt Macy 	if (ancestor == dd)
864eda14cbcSMatt Macy 		return (0);
865eda14cbcSMatt Macy 
866eda14cbcSMatt Macy 	/*
867eda14cbcSMatt Macy 	 * If we hit an uninitialized node while recursing up the tree, we can
868eda14cbcSMatt Macy 	 * stop since we know there is no limit here (or above). The counts are
869eda14cbcSMatt Macy 	 * not valid on this node and we know we won't touch this node's counts.
870eda14cbcSMatt Macy 	 */
871eda14cbcSMatt Macy 	if (!dsl_dir_is_zapified(dd))
872eda14cbcSMatt Macy 		return (0);
873eda14cbcSMatt Macy 	err = zap_lookup(os, dd->dd_object,
874eda14cbcSMatt Macy 	    count_prop, sizeof (count), 1, &count);
875eda14cbcSMatt Macy 	if (err == ENOENT)
876eda14cbcSMatt Macy 		return (0);
877eda14cbcSMatt Macy 	if (err != 0)
878eda14cbcSMatt Macy 		return (err);
879eda14cbcSMatt Macy 
880eda14cbcSMatt Macy 	err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
881eda14cbcSMatt Macy 	    B_FALSE);
882eda14cbcSMatt Macy 	if (err != 0)
883eda14cbcSMatt Macy 		return (err);
884eda14cbcSMatt Macy 
885eda14cbcSMatt Macy 	/* Is there a limit which we've hit? */
886eda14cbcSMatt Macy 	if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
887eda14cbcSMatt Macy 		return (SET_ERROR(EDQUOT));
888eda14cbcSMatt Macy 
889eda14cbcSMatt Macy 	if (dd->dd_parent != NULL)
890eda14cbcSMatt Macy 		err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
891eda14cbcSMatt Macy 		    ancestor, cr, proc);
892eda14cbcSMatt Macy 
893eda14cbcSMatt Macy 	return (err);
894eda14cbcSMatt Macy }
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy /*
897eda14cbcSMatt Macy  * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
898eda14cbcSMatt Macy  * parents. When a new filesystem/snapshot is created, increment the count on
899eda14cbcSMatt Macy  * all parents, and when a filesystem/snapshot is destroyed, decrement the
900eda14cbcSMatt Macy  * count.
901eda14cbcSMatt Macy  */
902eda14cbcSMatt Macy void
903eda14cbcSMatt Macy dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
904eda14cbcSMatt Macy     dmu_tx_t *tx)
905eda14cbcSMatt Macy {
906eda14cbcSMatt Macy 	int err;
907eda14cbcSMatt Macy 	objset_t *os = dd->dd_pool->dp_meta_objset;
908eda14cbcSMatt Macy 	uint64_t count;
909eda14cbcSMatt Macy 
910eda14cbcSMatt Macy 	ASSERT(dsl_pool_config_held(dd->dd_pool));
911eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
912eda14cbcSMatt Macy 	ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
913eda14cbcSMatt Macy 	    strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
914eda14cbcSMatt Macy 
915eda14cbcSMatt Macy 	/*
916ac0bf12eSMatt Macy 	 * We don't do accounting for hidden ($FREE, $MOS & $ORIGIN) objsets.
917eda14cbcSMatt Macy 	 */
918ac0bf12eSMatt Macy 	if (dd->dd_myname[0] == '$' && strcmp(prop,
919ac0bf12eSMatt Macy 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
920eda14cbcSMatt Macy 		return;
921ac0bf12eSMatt Macy 	}
922eda14cbcSMatt Macy 
923eda14cbcSMatt Macy 	/*
924eda14cbcSMatt Macy 	 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
925eda14cbcSMatt Macy 	 */
926eda14cbcSMatt Macy 	if (delta == 0)
927eda14cbcSMatt Macy 		return;
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 	/*
930eda14cbcSMatt Macy 	 * If we hit an uninitialized node while recursing up the tree, we can
931eda14cbcSMatt Macy 	 * stop since we know the counts are not valid on this node and we
932eda14cbcSMatt Macy 	 * know we shouldn't touch this node's counts. An uninitialized count
933eda14cbcSMatt Macy 	 * on the node indicates that either the feature has not yet been
934eda14cbcSMatt Macy 	 * activated or there are no limits on this part of the tree.
935eda14cbcSMatt Macy 	 */
936eda14cbcSMatt Macy 	if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
937eda14cbcSMatt Macy 	    prop, sizeof (count), 1, &count)) == ENOENT)
938eda14cbcSMatt Macy 		return;
939eda14cbcSMatt Macy 	VERIFY0(err);
940eda14cbcSMatt Macy 
941eda14cbcSMatt Macy 	count += delta;
942eda14cbcSMatt Macy 	/* Use a signed verify to make sure we're not neg. */
943eda14cbcSMatt Macy 	VERIFY3S(count, >=, 0);
944eda14cbcSMatt Macy 
945eda14cbcSMatt Macy 	VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
946eda14cbcSMatt Macy 	    tx));
947eda14cbcSMatt Macy 
948eda14cbcSMatt Macy 	/* Roll up this additional count into our ancestors */
949eda14cbcSMatt Macy 	if (dd->dd_parent != NULL)
950eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
951eda14cbcSMatt Macy }
952eda14cbcSMatt Macy 
953eda14cbcSMatt Macy uint64_t
954eda14cbcSMatt Macy dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
955eda14cbcSMatt Macy     dmu_tx_t *tx)
956eda14cbcSMatt Macy {
957eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
958eda14cbcSMatt Macy 	uint64_t ddobj;
959eda14cbcSMatt Macy 	dsl_dir_phys_t *ddphys;
960eda14cbcSMatt Macy 	dmu_buf_t *dbuf;
961eda14cbcSMatt Macy 
962eda14cbcSMatt Macy 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
963eda14cbcSMatt Macy 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
964eda14cbcSMatt Macy 	if (pds) {
965eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
966eda14cbcSMatt Macy 		    name, sizeof (uint64_t), 1, &ddobj, tx));
967eda14cbcSMatt Macy 	} else {
968eda14cbcSMatt Macy 		/* it's the root dir */
969eda14cbcSMatt Macy 		VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
970eda14cbcSMatt Macy 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
971eda14cbcSMatt Macy 	}
972eda14cbcSMatt Macy 	VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
973eda14cbcSMatt Macy 	dmu_buf_will_dirty(dbuf, tx);
974eda14cbcSMatt Macy 	ddphys = dbuf->db_data;
975eda14cbcSMatt Macy 
976eda14cbcSMatt Macy 	ddphys->dd_creation_time = gethrestime_sec();
977eda14cbcSMatt Macy 	if (pds) {
978eda14cbcSMatt Macy 		ddphys->dd_parent_obj = pds->dd_object;
979eda14cbcSMatt Macy 
980eda14cbcSMatt Macy 		/* update the filesystem counts */
981eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
982eda14cbcSMatt Macy 	}
983eda14cbcSMatt Macy 	ddphys->dd_props_zapobj = zap_create(mos,
984eda14cbcSMatt Macy 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
985eda14cbcSMatt Macy 	ddphys->dd_child_dir_zapobj = zap_create(mos,
986eda14cbcSMatt Macy 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
987eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
988eda14cbcSMatt Macy 		ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
989eda14cbcSMatt Macy 
990eda14cbcSMatt Macy 	dmu_buf_rele(dbuf, FTAG);
991eda14cbcSMatt Macy 
992eda14cbcSMatt Macy 	return (ddobj);
993eda14cbcSMatt Macy }
994eda14cbcSMatt Macy 
995eda14cbcSMatt Macy boolean_t
996eda14cbcSMatt Macy dsl_dir_is_clone(dsl_dir_t *dd)
997eda14cbcSMatt Macy {
998eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_origin_obj &&
999eda14cbcSMatt Macy 	    (dd->dd_pool->dp_origin_snap == NULL ||
1000eda14cbcSMatt Macy 	    dsl_dir_phys(dd)->dd_origin_obj !=
1001eda14cbcSMatt Macy 	    dd->dd_pool->dp_origin_snap->ds_object));
1002eda14cbcSMatt Macy }
1003eda14cbcSMatt Macy 
1004eda14cbcSMatt Macy uint64_t
1005eda14cbcSMatt Macy dsl_dir_get_used(dsl_dir_t *dd)
1006eda14cbcSMatt Macy {
1007eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_used_bytes);
1008eda14cbcSMatt Macy }
1009eda14cbcSMatt Macy 
1010eda14cbcSMatt Macy uint64_t
1011eda14cbcSMatt Macy dsl_dir_get_compressed(dsl_dir_t *dd)
1012eda14cbcSMatt Macy {
1013eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_compressed_bytes);
1014eda14cbcSMatt Macy }
1015eda14cbcSMatt Macy 
1016eda14cbcSMatt Macy uint64_t
1017eda14cbcSMatt Macy dsl_dir_get_quota(dsl_dir_t *dd)
1018eda14cbcSMatt Macy {
1019eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_quota);
1020eda14cbcSMatt Macy }
1021eda14cbcSMatt Macy 
1022eda14cbcSMatt Macy uint64_t
1023eda14cbcSMatt Macy dsl_dir_get_reservation(dsl_dir_t *dd)
1024eda14cbcSMatt Macy {
1025eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_reserved);
1026eda14cbcSMatt Macy }
1027eda14cbcSMatt Macy 
1028eda14cbcSMatt Macy uint64_t
1029eda14cbcSMatt Macy dsl_dir_get_compressratio(dsl_dir_t *dd)
1030eda14cbcSMatt Macy {
1031eda14cbcSMatt Macy 	/* a fixed point number, 100x the ratio */
1032eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
1033eda14cbcSMatt Macy 	    (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
1034eda14cbcSMatt Macy 	    dsl_dir_phys(dd)->dd_compressed_bytes));
1035eda14cbcSMatt Macy }
1036eda14cbcSMatt Macy 
1037eda14cbcSMatt Macy uint64_t
1038eda14cbcSMatt Macy dsl_dir_get_logicalused(dsl_dir_t *dd)
1039eda14cbcSMatt Macy {
1040eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_uncompressed_bytes);
1041eda14cbcSMatt Macy }
1042eda14cbcSMatt Macy 
1043eda14cbcSMatt Macy uint64_t
1044eda14cbcSMatt Macy dsl_dir_get_usedsnap(dsl_dir_t *dd)
1045eda14cbcSMatt Macy {
1046eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
1047eda14cbcSMatt Macy }
1048eda14cbcSMatt Macy 
1049eda14cbcSMatt Macy uint64_t
1050eda14cbcSMatt Macy dsl_dir_get_usedds(dsl_dir_t *dd)
1051eda14cbcSMatt Macy {
1052eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
1053eda14cbcSMatt Macy }
1054eda14cbcSMatt Macy 
1055eda14cbcSMatt Macy uint64_t
1056eda14cbcSMatt Macy dsl_dir_get_usedrefreserv(dsl_dir_t *dd)
1057eda14cbcSMatt Macy {
1058eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
1059eda14cbcSMatt Macy }
1060eda14cbcSMatt Macy 
1061eda14cbcSMatt Macy uint64_t
1062eda14cbcSMatt Macy dsl_dir_get_usedchild(dsl_dir_t *dd)
1063eda14cbcSMatt Macy {
1064eda14cbcSMatt Macy 	return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
1065eda14cbcSMatt Macy 	    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
1066eda14cbcSMatt Macy }
1067eda14cbcSMatt Macy 
1068eda14cbcSMatt Macy void
1069eda14cbcSMatt Macy dsl_dir_get_origin(dsl_dir_t *dd, char *buf)
1070eda14cbcSMatt Macy {
1071eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1072eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
1073eda14cbcSMatt Macy 	    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1074eda14cbcSMatt Macy 
1075eda14cbcSMatt Macy 	dsl_dataset_name(ds, buf);
1076eda14cbcSMatt Macy 
1077eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1078eda14cbcSMatt Macy }
1079eda14cbcSMatt Macy 
1080eda14cbcSMatt Macy int
1081eda14cbcSMatt Macy dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)
1082eda14cbcSMatt Macy {
1083eda14cbcSMatt Macy 	if (dsl_dir_is_zapified(dd)) {
1084eda14cbcSMatt Macy 		objset_t *os = dd->dd_pool->dp_meta_objset;
1085eda14cbcSMatt Macy 		return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1086eda14cbcSMatt Macy 		    sizeof (*count), 1, count));
1087eda14cbcSMatt Macy 	} else {
1088eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
1089eda14cbcSMatt Macy 	}
1090eda14cbcSMatt Macy }
1091eda14cbcSMatt Macy 
1092eda14cbcSMatt Macy int
1093eda14cbcSMatt Macy dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)
1094eda14cbcSMatt Macy {
1095eda14cbcSMatt Macy 	if (dsl_dir_is_zapified(dd)) {
1096eda14cbcSMatt Macy 		objset_t *os = dd->dd_pool->dp_meta_objset;
1097eda14cbcSMatt Macy 		return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1098eda14cbcSMatt Macy 		    sizeof (*count), 1, count));
1099eda14cbcSMatt Macy 	} else {
1100eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
1101eda14cbcSMatt Macy 	}
1102eda14cbcSMatt Macy }
1103eda14cbcSMatt Macy 
1104eda14cbcSMatt Macy void
1105eda14cbcSMatt Macy dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
1106eda14cbcSMatt Macy {
1107eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1108eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
1109eda14cbcSMatt Macy 	    dsl_dir_get_quota(dd));
1110eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
1111eda14cbcSMatt Macy 	    dsl_dir_get_reservation(dd));
1112eda14cbcSMatt Macy 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
1113eda14cbcSMatt Macy 	    dsl_dir_get_logicalused(dd));
1114eda14cbcSMatt Macy 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1115eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
1116eda14cbcSMatt Macy 		    dsl_dir_get_usedsnap(dd));
1117eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
1118eda14cbcSMatt Macy 		    dsl_dir_get_usedds(dd));
1119eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
1120eda14cbcSMatt Macy 		    dsl_dir_get_usedrefreserv(dd));
1121eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
1122eda14cbcSMatt Macy 		    dsl_dir_get_usedchild(dd));
1123eda14cbcSMatt Macy 	}
1124eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1125eda14cbcSMatt Macy 
1126eda14cbcSMatt Macy 	uint64_t count;
1127eda14cbcSMatt Macy 	if (dsl_dir_get_filesystem_count(dd, &count) == 0) {
1128eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,
1129eda14cbcSMatt Macy 		    count);
1130eda14cbcSMatt Macy 	}
1131eda14cbcSMatt Macy 	if (dsl_dir_get_snapshot_count(dd, &count) == 0) {
1132eda14cbcSMatt Macy 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,
1133eda14cbcSMatt Macy 		    count);
1134eda14cbcSMatt Macy 	}
1135eda14cbcSMatt Macy 
1136eda14cbcSMatt Macy 	if (dsl_dir_is_clone(dd)) {
1137eda14cbcSMatt Macy 		char buf[ZFS_MAX_DATASET_NAME_LEN];
1138eda14cbcSMatt Macy 		dsl_dir_get_origin(dd, buf);
1139eda14cbcSMatt Macy 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1140eda14cbcSMatt Macy 	}
1141eda14cbcSMatt Macy 
1142eda14cbcSMatt Macy }
1143eda14cbcSMatt Macy 
1144eda14cbcSMatt Macy void
1145eda14cbcSMatt Macy dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1146eda14cbcSMatt Macy {
1147eda14cbcSMatt Macy 	dsl_pool_t *dp = dd->dd_pool;
1148eda14cbcSMatt Macy 
1149eda14cbcSMatt Macy 	ASSERT(dsl_dir_phys(dd));
1150eda14cbcSMatt Macy 
1151eda14cbcSMatt Macy 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1152eda14cbcSMatt Macy 		/* up the hold count until we can be written out */
1153eda14cbcSMatt Macy 		dmu_buf_add_ref(dd->dd_dbuf, dd);
1154eda14cbcSMatt Macy 	}
1155eda14cbcSMatt Macy }
1156eda14cbcSMatt Macy 
1157eda14cbcSMatt Macy static int64_t
1158eda14cbcSMatt Macy parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1159eda14cbcSMatt Macy {
1160eda14cbcSMatt Macy 	uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1161eda14cbcSMatt Macy 	uint64_t new_accounted =
1162eda14cbcSMatt Macy 	    MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1163eda14cbcSMatt Macy 	return (new_accounted - old_accounted);
1164eda14cbcSMatt Macy }
1165eda14cbcSMatt Macy 
1166eda14cbcSMatt Macy void
1167eda14cbcSMatt Macy dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1168eda14cbcSMatt Macy {
1169eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1170eda14cbcSMatt Macy 
1171eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1172eda14cbcSMatt Macy 	ASSERT0(dd->dd_tempreserved[tx->tx_txg & TXG_MASK]);
117333b8c039SMartin Matuska 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", (u_longlong_t)tx->tx_txg,
117433b8c039SMartin Matuska 	    (u_longlong_t)dd->dd_space_towrite[tx->tx_txg & TXG_MASK] / 1024);
1175eda14cbcSMatt Macy 	dd->dd_space_towrite[tx->tx_txg & TXG_MASK] = 0;
1176eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1177eda14cbcSMatt Macy 
1178eda14cbcSMatt Macy 	/* release the hold from dsl_dir_dirty */
1179eda14cbcSMatt Macy 	dmu_buf_rele(dd->dd_dbuf, dd);
1180eda14cbcSMatt Macy }
1181eda14cbcSMatt Macy 
1182eda14cbcSMatt Macy static uint64_t
1183eda14cbcSMatt Macy dsl_dir_space_towrite(dsl_dir_t *dd)
1184eda14cbcSMatt Macy {
1185eda14cbcSMatt Macy 	uint64_t space = 0;
1186eda14cbcSMatt Macy 
1187eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&dd->dd_lock));
1188eda14cbcSMatt Macy 
118915f0b8c3SMartin Matuska 	for (int i = 0; i < TXG_SIZE; i++)
1190eda14cbcSMatt Macy 		space += dd->dd_space_towrite[i & TXG_MASK];
119115f0b8c3SMartin Matuska 
1192eda14cbcSMatt Macy 	return (space);
1193eda14cbcSMatt Macy }
1194eda14cbcSMatt Macy 
1195eda14cbcSMatt Macy /*
1196eda14cbcSMatt Macy  * How much space would dd have available if ancestor had delta applied
1197eda14cbcSMatt Macy  * to it?  If ondiskonly is set, we're only interested in what's
1198eda14cbcSMatt Macy  * on-disk, not estimated pending changes.
1199eda14cbcSMatt Macy  */
1200eda14cbcSMatt Macy uint64_t
1201eda14cbcSMatt Macy dsl_dir_space_available(dsl_dir_t *dd,
1202eda14cbcSMatt Macy     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1203eda14cbcSMatt Macy {
1204eda14cbcSMatt Macy 	uint64_t parentspace, myspace, quota, used;
1205eda14cbcSMatt Macy 
1206eda14cbcSMatt Macy 	/*
1207eda14cbcSMatt Macy 	 * If there are no restrictions otherwise, assume we have
1208eda14cbcSMatt Macy 	 * unlimited space available.
1209eda14cbcSMatt Macy 	 */
1210eda14cbcSMatt Macy 	quota = UINT64_MAX;
1211eda14cbcSMatt Macy 	parentspace = UINT64_MAX;
1212eda14cbcSMatt Macy 
1213eda14cbcSMatt Macy 	if (dd->dd_parent != NULL) {
1214eda14cbcSMatt Macy 		parentspace = dsl_dir_space_available(dd->dd_parent,
1215eda14cbcSMatt Macy 		    ancestor, delta, ondiskonly);
1216eda14cbcSMatt Macy 	}
1217eda14cbcSMatt Macy 
1218eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1219eda14cbcSMatt Macy 	if (dsl_dir_phys(dd)->dd_quota != 0)
1220eda14cbcSMatt Macy 		quota = dsl_dir_phys(dd)->dd_quota;
1221eda14cbcSMatt Macy 	used = dsl_dir_phys(dd)->dd_used_bytes;
1222eda14cbcSMatt Macy 	if (!ondiskonly)
1223eda14cbcSMatt Macy 		used += dsl_dir_space_towrite(dd);
1224eda14cbcSMatt Macy 
1225eda14cbcSMatt Macy 	if (dd->dd_parent == NULL) {
1226eda14cbcSMatt Macy 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool,
1227eda14cbcSMatt Macy 		    ZFS_SPACE_CHECK_NORMAL);
1228eda14cbcSMatt Macy 		quota = MIN(quota, poolsize);
1229eda14cbcSMatt Macy 	}
1230eda14cbcSMatt Macy 
1231eda14cbcSMatt Macy 	if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1232eda14cbcSMatt Macy 		/*
1233eda14cbcSMatt Macy 		 * We have some space reserved, in addition to what our
1234eda14cbcSMatt Macy 		 * parent gave us.
1235eda14cbcSMatt Macy 		 */
1236eda14cbcSMatt Macy 		parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1237eda14cbcSMatt Macy 	}
1238eda14cbcSMatt Macy 
1239eda14cbcSMatt Macy 	if (dd == ancestor) {
1240eda14cbcSMatt Macy 		ASSERT(delta <= 0);
1241eda14cbcSMatt Macy 		ASSERT(used >= -delta);
1242eda14cbcSMatt Macy 		used += delta;
1243eda14cbcSMatt Macy 		if (parentspace != UINT64_MAX)
1244eda14cbcSMatt Macy 			parentspace -= delta;
1245eda14cbcSMatt Macy 	}
1246eda14cbcSMatt Macy 
1247eda14cbcSMatt Macy 	if (used > quota) {
1248eda14cbcSMatt Macy 		/* over quota */
1249eda14cbcSMatt Macy 		myspace = 0;
1250eda14cbcSMatt Macy 	} else {
1251eda14cbcSMatt Macy 		/*
1252eda14cbcSMatt Macy 		 * the lesser of the space provided by our parent and
1253eda14cbcSMatt Macy 		 * the space left in our quota
1254eda14cbcSMatt Macy 		 */
1255eda14cbcSMatt Macy 		myspace = MIN(parentspace, quota - used);
1256eda14cbcSMatt Macy 	}
1257eda14cbcSMatt Macy 
1258eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1259eda14cbcSMatt Macy 
1260eda14cbcSMatt Macy 	return (myspace);
1261eda14cbcSMatt Macy }
1262eda14cbcSMatt Macy 
1263eda14cbcSMatt Macy struct tempreserve {
1264eda14cbcSMatt Macy 	list_node_t tr_node;
1265eda14cbcSMatt Macy 	dsl_dir_t *tr_ds;
1266eda14cbcSMatt Macy 	uint64_t tr_size;
1267eda14cbcSMatt Macy };
1268eda14cbcSMatt Macy 
1269eda14cbcSMatt Macy static int
1270eda14cbcSMatt Macy dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1271eda14cbcSMatt Macy     boolean_t ignorequota, list_t *tr_list,
1272eda14cbcSMatt Macy     dmu_tx_t *tx, boolean_t first)
1273eda14cbcSMatt Macy {
1274eda14cbcSMatt Macy 	uint64_t txg;
1275eda14cbcSMatt Macy 	uint64_t quota;
1276eda14cbcSMatt Macy 	struct tempreserve *tr;
1277eda14cbcSMatt Macy 	int retval;
1278dbd5678dSMartin Matuska 	uint64_t ext_quota;
1279eda14cbcSMatt Macy 	uint64_t ref_rsrv;
1280eda14cbcSMatt Macy 
1281eda14cbcSMatt Macy top_of_function:
1282eda14cbcSMatt Macy 	txg = tx->tx_txg;
1283eda14cbcSMatt Macy 	retval = EDQUOT;
1284eda14cbcSMatt Macy 	ref_rsrv = 0;
1285eda14cbcSMatt Macy 
1286eda14cbcSMatt Macy 	ASSERT3U(txg, !=, 0);
1287eda14cbcSMatt Macy 	ASSERT3S(asize, >, 0);
1288eda14cbcSMatt Macy 
1289eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1290eda14cbcSMatt Macy 
1291eda14cbcSMatt Macy 	/*
1292eda14cbcSMatt Macy 	 * Check against the dsl_dir's quota.  We don't add in the delta
1293eda14cbcSMatt Macy 	 * when checking for over-quota because they get one free hit.
1294eda14cbcSMatt Macy 	 */
1295eda14cbcSMatt Macy 	uint64_t est_inflight = dsl_dir_space_towrite(dd);
1296eda14cbcSMatt Macy 	for (int i = 0; i < TXG_SIZE; i++)
1297eda14cbcSMatt Macy 		est_inflight += dd->dd_tempreserved[i];
1298eda14cbcSMatt Macy 	uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1299eda14cbcSMatt Macy 
1300eda14cbcSMatt Macy 	/*
1301eda14cbcSMatt Macy 	 * On the first iteration, fetch the dataset's used-on-disk and
1302eda14cbcSMatt Macy 	 * refreservation values. Also, if checkrefquota is set, test if
1303eda14cbcSMatt Macy 	 * allocating this space would exceed the dataset's refquota.
1304eda14cbcSMatt Macy 	 */
1305eda14cbcSMatt Macy 	if (first && tx->tx_objset) {
1306eda14cbcSMatt Macy 		int error;
1307eda14cbcSMatt Macy 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1308eda14cbcSMatt Macy 
1309eda14cbcSMatt Macy 		error = dsl_dataset_check_quota(ds, !netfree,
1310eda14cbcSMatt Macy 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
1311eda14cbcSMatt Macy 		if (error != 0) {
1312eda14cbcSMatt Macy 			mutex_exit(&dd->dd_lock);
1313eda14cbcSMatt Macy 			DMU_TX_STAT_BUMP(dmu_tx_quota);
1314eda14cbcSMatt Macy 			return (error);
1315eda14cbcSMatt Macy 		}
1316eda14cbcSMatt Macy 	}
1317eda14cbcSMatt Macy 
1318eda14cbcSMatt Macy 	/*
1319eda14cbcSMatt Macy 	 * If this transaction will result in a net free of space,
1320eda14cbcSMatt Macy 	 * we want to let it through.
1321eda14cbcSMatt Macy 	 */
1322dbd5678dSMartin Matuska 	if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0 ||
1323dbd5678dSMartin Matuska 	    (tx->tx_objset && dmu_objset_type(tx->tx_objset) == DMU_OST_ZVOL &&
1324dbd5678dSMartin Matuska 	    zvol_enforce_quotas == B_FALSE))
1325eda14cbcSMatt Macy 		quota = UINT64_MAX;
1326eda14cbcSMatt Macy 	else
1327eda14cbcSMatt Macy 		quota = dsl_dir_phys(dd)->dd_quota;
1328eda14cbcSMatt Macy 
1329eda14cbcSMatt Macy 	/*
1330eda14cbcSMatt Macy 	 * Adjust the quota against the actual pool size at the root
1331eda14cbcSMatt Macy 	 * minus any outstanding deferred frees.
1332eda14cbcSMatt Macy 	 * To ensure that it's possible to remove files from a full
1333eda14cbcSMatt Macy 	 * pool without inducing transient overcommits, we throttle
1334eda14cbcSMatt Macy 	 * netfree transactions against a quota that is slightly larger,
1335eda14cbcSMatt Macy 	 * but still within the pool's allocation slop.  In cases where
1336eda14cbcSMatt Macy 	 * we're very close to full, this will allow a steady trickle of
1337eda14cbcSMatt Macy 	 * removes to get through.
1338eda14cbcSMatt Macy 	 */
1339eda14cbcSMatt Macy 	if (dd->dd_parent == NULL) {
1340eda14cbcSMatt Macy 		uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool,
1341eda14cbcSMatt Macy 		    (netfree) ?
1342eda14cbcSMatt Macy 		    ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL);
1343eda14cbcSMatt Macy 
1344eda14cbcSMatt Macy 		if (avail < quota) {
1345eda14cbcSMatt Macy 			quota = avail;
1346eda14cbcSMatt Macy 			retval = SET_ERROR(ENOSPC);
1347eda14cbcSMatt Macy 		}
1348eda14cbcSMatt Macy 	}
1349eda14cbcSMatt Macy 
1350eda14cbcSMatt Macy 	/*
1351eda14cbcSMatt Macy 	 * If they are requesting more space, and our current estimate
1352eda14cbcSMatt Macy 	 * is over quota, they get to try again unless the actual
1353c03c5b1cSMartin Matuska 	 * on-disk is over quota and there are no pending changes
1354c03c5b1cSMartin Matuska 	 * or deferred frees (which may free up space for us).
1355eda14cbcSMatt Macy 	 */
1356dbd5678dSMartin Matuska 	ext_quota = quota >> 5;
1357dbd5678dSMartin Matuska 	if (quota == UINT64_MAX)
1358dbd5678dSMartin Matuska 		ext_quota = 0;
1359dbd5678dSMartin Matuska 
1360dbd5678dSMartin Matuska 	if (used_on_disk >= quota) {
1361dbd5678dSMartin Matuska 		/* Quota exceeded */
1362dbd5678dSMartin Matuska 		mutex_exit(&dd->dd_lock);
1363dbd5678dSMartin Matuska 		DMU_TX_STAT_BUMP(dmu_tx_quota);
1364dbd5678dSMartin Matuska 		return (retval);
1365dbd5678dSMartin Matuska 	} else if (used_on_disk + est_inflight >= quota + ext_quota) {
1366c03c5b1cSMartin Matuska 		if (est_inflight > 0 || used_on_disk < quota) {
1367c03c5b1cSMartin Matuska 			retval = SET_ERROR(ERESTART);
1368c03c5b1cSMartin Matuska 		} else {
1369c03c5b1cSMartin Matuska 			ASSERT3U(used_on_disk, >=, quota);
1370c03c5b1cSMartin Matuska 
1371c03c5b1cSMartin Matuska 			if (retval == ENOSPC && (used_on_disk - quota) <
1372c03c5b1cSMartin Matuska 			    dsl_pool_deferred_space(dd->dd_pool)) {
1373c03c5b1cSMartin Matuska 				retval = SET_ERROR(ERESTART);
1374c03c5b1cSMartin Matuska 			}
1375c03c5b1cSMartin Matuska 		}
1376c03c5b1cSMartin Matuska 
1377eda14cbcSMatt Macy 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1378eda14cbcSMatt Macy 		    "quota=%lluK tr=%lluK err=%d\n",
137933b8c039SMartin Matuska 		    (u_longlong_t)used_on_disk>>10,
138033b8c039SMartin Matuska 		    (u_longlong_t)est_inflight>>10,
138133b8c039SMartin Matuska 		    (u_longlong_t)quota>>10, (u_longlong_t)asize>>10, retval);
1382eda14cbcSMatt Macy 		mutex_exit(&dd->dd_lock);
1383eda14cbcSMatt Macy 		DMU_TX_STAT_BUMP(dmu_tx_quota);
1384c03c5b1cSMartin Matuska 		return (retval);
1385eda14cbcSMatt Macy 	}
1386eda14cbcSMatt Macy 
1387eda14cbcSMatt Macy 	/* We need to up our estimated delta before dropping dd_lock */
1388eda14cbcSMatt Macy 	dd->dd_tempreserved[txg & TXG_MASK] += asize;
1389eda14cbcSMatt Macy 
1390eda14cbcSMatt Macy 	uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1391eda14cbcSMatt Macy 	    asize - ref_rsrv);
1392eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1393eda14cbcSMatt Macy 
1394eda14cbcSMatt Macy 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1395eda14cbcSMatt Macy 	tr->tr_ds = dd;
1396eda14cbcSMatt Macy 	tr->tr_size = asize;
1397eda14cbcSMatt Macy 	list_insert_tail(tr_list, tr);
1398eda14cbcSMatt Macy 
1399eda14cbcSMatt Macy 	/* see if it's OK with our parent */
1400eda14cbcSMatt Macy 	if (dd->dd_parent != NULL && parent_rsrv != 0) {
1401eda14cbcSMatt Macy 		/*
1402eda14cbcSMatt Macy 		 * Recurse on our parent without recursion. This has been
1403eda14cbcSMatt Macy 		 * observed to be potentially large stack usage even within
1404eda14cbcSMatt Macy 		 * the test suite. Largest seen stack was 7632 bytes on linux.
1405eda14cbcSMatt Macy 		 */
1406eda14cbcSMatt Macy 
1407eda14cbcSMatt Macy 		dd = dd->dd_parent;
1408eda14cbcSMatt Macy 		asize = parent_rsrv;
1409eda14cbcSMatt Macy 		ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1410eda14cbcSMatt Macy 		first = B_FALSE;
1411eda14cbcSMatt Macy 		goto top_of_function;
1412eda14cbcSMatt Macy 	}
1413dbd5678dSMartin Matuska 
1414dbd5678dSMartin Matuska 	return (0);
1415eda14cbcSMatt Macy }
1416eda14cbcSMatt Macy 
1417eda14cbcSMatt Macy /*
1418eda14cbcSMatt Macy  * Reserve space in this dsl_dir, to be used in this tx's txg.
1419eda14cbcSMatt Macy  * After the space has been dirtied (and dsl_dir_willuse_space()
1420eda14cbcSMatt Macy  * has been called), the reservation should be canceled, using
1421eda14cbcSMatt Macy  * dsl_dir_tempreserve_clear().
1422eda14cbcSMatt Macy  */
1423eda14cbcSMatt Macy int
1424eda14cbcSMatt Macy dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1425eda14cbcSMatt Macy     boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)
1426eda14cbcSMatt Macy {
1427eda14cbcSMatt Macy 	int err;
1428eda14cbcSMatt Macy 	list_t *tr_list;
1429eda14cbcSMatt Macy 
1430eda14cbcSMatt Macy 	if (asize == 0) {
1431eda14cbcSMatt Macy 		*tr_cookiep = NULL;
1432eda14cbcSMatt Macy 		return (0);
1433eda14cbcSMatt Macy 	}
1434eda14cbcSMatt Macy 
1435eda14cbcSMatt Macy 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1436eda14cbcSMatt Macy 	list_create(tr_list, sizeof (struct tempreserve),
1437eda14cbcSMatt Macy 	    offsetof(struct tempreserve, tr_node));
1438eda14cbcSMatt Macy 	ASSERT3S(asize, >, 0);
1439eda14cbcSMatt Macy 
1440eda14cbcSMatt Macy 	err = arc_tempreserve_space(dd->dd_pool->dp_spa, lsize, tx->tx_txg);
1441eda14cbcSMatt Macy 	if (err == 0) {
1442eda14cbcSMatt Macy 		struct tempreserve *tr;
1443eda14cbcSMatt Macy 
1444eda14cbcSMatt Macy 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1445eda14cbcSMatt Macy 		tr->tr_size = lsize;
1446eda14cbcSMatt Macy 		list_insert_tail(tr_list, tr);
1447eda14cbcSMatt Macy 	} else {
1448eda14cbcSMatt Macy 		if (err == EAGAIN) {
1449eda14cbcSMatt Macy 			/*
1450eda14cbcSMatt Macy 			 * If arc_memory_throttle() detected that pageout
1451eda14cbcSMatt Macy 			 * is running and we are low on memory, we delay new
1452eda14cbcSMatt Macy 			 * non-pageout transactions to give pageout an
1453eda14cbcSMatt Macy 			 * advantage.
1454eda14cbcSMatt Macy 			 *
1455eda14cbcSMatt Macy 			 * It is unfortunate to be delaying while the caller's
1456eda14cbcSMatt Macy 			 * locks are held.
1457eda14cbcSMatt Macy 			 */
1458eda14cbcSMatt Macy 			txg_delay(dd->dd_pool, tx->tx_txg,
1459eda14cbcSMatt Macy 			    MSEC2NSEC(10), MSEC2NSEC(10));
1460eda14cbcSMatt Macy 			err = SET_ERROR(ERESTART);
1461eda14cbcSMatt Macy 		}
1462eda14cbcSMatt Macy 	}
1463eda14cbcSMatt Macy 
1464eda14cbcSMatt Macy 	if (err == 0) {
1465eda14cbcSMatt Macy 		err = dsl_dir_tempreserve_impl(dd, asize, netfree,
1466eda14cbcSMatt Macy 		    B_FALSE, tr_list, tx, B_TRUE);
1467eda14cbcSMatt Macy 	}
1468eda14cbcSMatt Macy 
1469eda14cbcSMatt Macy 	if (err != 0)
1470eda14cbcSMatt Macy 		dsl_dir_tempreserve_clear(tr_list, tx);
1471eda14cbcSMatt Macy 	else
1472eda14cbcSMatt Macy 		*tr_cookiep = tr_list;
1473eda14cbcSMatt Macy 
1474eda14cbcSMatt Macy 	return (err);
1475eda14cbcSMatt Macy }
1476eda14cbcSMatt Macy 
1477eda14cbcSMatt Macy /*
1478eda14cbcSMatt Macy  * Clear a temporary reservation that we previously made with
1479eda14cbcSMatt Macy  * dsl_dir_tempreserve_space().
1480eda14cbcSMatt Macy  */
1481eda14cbcSMatt Macy void
1482eda14cbcSMatt Macy dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1483eda14cbcSMatt Macy {
1484eda14cbcSMatt Macy 	int txgidx = tx->tx_txg & TXG_MASK;
1485eda14cbcSMatt Macy 	list_t *tr_list = tr_cookie;
1486eda14cbcSMatt Macy 	struct tempreserve *tr;
1487eda14cbcSMatt Macy 
1488eda14cbcSMatt Macy 	ASSERT3U(tx->tx_txg, !=, 0);
1489eda14cbcSMatt Macy 
1490eda14cbcSMatt Macy 	if (tr_cookie == NULL)
1491eda14cbcSMatt Macy 		return;
1492eda14cbcSMatt Macy 
1493eda14cbcSMatt Macy 	while ((tr = list_head(tr_list)) != NULL) {
1494eda14cbcSMatt Macy 		if (tr->tr_ds) {
1495eda14cbcSMatt Macy 			mutex_enter(&tr->tr_ds->dd_lock);
1496eda14cbcSMatt Macy 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1497eda14cbcSMatt Macy 			    tr->tr_size);
1498eda14cbcSMatt Macy 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1499eda14cbcSMatt Macy 			mutex_exit(&tr->tr_ds->dd_lock);
1500eda14cbcSMatt Macy 		} else {
1501eda14cbcSMatt Macy 			arc_tempreserve_clear(tr->tr_size);
1502eda14cbcSMatt Macy 		}
1503eda14cbcSMatt Macy 		list_remove(tr_list, tr);
1504eda14cbcSMatt Macy 		kmem_free(tr, sizeof (struct tempreserve));
1505eda14cbcSMatt Macy 	}
1506eda14cbcSMatt Macy 
1507eda14cbcSMatt Macy 	kmem_free(tr_list, sizeof (list_t));
1508eda14cbcSMatt Macy }
1509eda14cbcSMatt Macy 
1510eda14cbcSMatt Macy /*
1511eda14cbcSMatt Macy  * This should be called from open context when we think we're going to write
1512eda14cbcSMatt Macy  * or free space, for example when dirtying data. Be conservative; it's okay
1513eda14cbcSMatt Macy  * to write less space or free more, but we don't want to write more or free
1514eda14cbcSMatt Macy  * less than the amount specified.
1515eda14cbcSMatt Macy  *
1516eda14cbcSMatt Macy  * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1517eda14cbcSMatt Macy  * version however it has been adjusted to use an iterative rather than
1518eda14cbcSMatt Macy  * recursive algorithm to minimize stack usage.
1519eda14cbcSMatt Macy  */
1520eda14cbcSMatt Macy void
1521eda14cbcSMatt Macy dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1522eda14cbcSMatt Macy {
1523eda14cbcSMatt Macy 	int64_t parent_space;
1524eda14cbcSMatt Macy 	uint64_t est_used;
1525eda14cbcSMatt Macy 
1526eda14cbcSMatt Macy 	do {
1527eda14cbcSMatt Macy 		mutex_enter(&dd->dd_lock);
1528eda14cbcSMatt Macy 		if (space > 0)
1529eda14cbcSMatt Macy 			dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1530eda14cbcSMatt Macy 
1531eda14cbcSMatt Macy 		est_used = dsl_dir_space_towrite(dd) +
1532eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_used_bytes;
1533eda14cbcSMatt Macy 		parent_space = parent_delta(dd, est_used, space);
1534eda14cbcSMatt Macy 		mutex_exit(&dd->dd_lock);
1535eda14cbcSMatt Macy 
1536eda14cbcSMatt Macy 		/* Make sure that we clean up dd_space_to* */
1537eda14cbcSMatt Macy 		dsl_dir_dirty(dd, tx);
1538eda14cbcSMatt Macy 
1539eda14cbcSMatt Macy 		dd = dd->dd_parent;
1540eda14cbcSMatt Macy 		space = parent_space;
1541eda14cbcSMatt Macy 	} while (space && dd);
1542eda14cbcSMatt Macy }
1543eda14cbcSMatt Macy 
1544eda14cbcSMatt Macy /* call from syncing context when we actually write/free space for this dd */
1545eda14cbcSMatt Macy void
1546eda14cbcSMatt Macy dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1547eda14cbcSMatt Macy     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1548eda14cbcSMatt Macy {
1549eda14cbcSMatt Macy 	int64_t accounted_delta;
1550eda14cbcSMatt Macy 
15513f9d360cSMartin Matuska 	ASSERT(dmu_tx_is_syncing(tx));
15523f9d360cSMartin Matuska 	ASSERT(type < DD_USED_NUM);
15533f9d360cSMartin Matuska 
15543f9d360cSMartin Matuska 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
15553f9d360cSMartin Matuska 
1556eda14cbcSMatt Macy 	/*
1557eda14cbcSMatt Macy 	 * dsl_dataset_set_refreservation_sync_impl() calls this with
1558eda14cbcSMatt Macy 	 * dd_lock held, so that it can atomically update
1559eda14cbcSMatt Macy 	 * ds->ds_reserved and the dsl_dir accounting, so that
1560eda14cbcSMatt Macy 	 * dsl_dataset_check_quota() can see dataset and dir accounting
1561eda14cbcSMatt Macy 	 * consistently.
1562eda14cbcSMatt Macy 	 */
1563eda14cbcSMatt Macy 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1564eda14cbcSMatt Macy 	if (needlock)
1565eda14cbcSMatt Macy 		mutex_enter(&dd->dd_lock);
15663f9d360cSMartin Matuska 	dsl_dir_phys_t *ddp = dsl_dir_phys(dd);
15673f9d360cSMartin Matuska 	accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used);
15683f9d360cSMartin Matuska 	ASSERT(used >= 0 || ddp->dd_used_bytes >= -used);
15693f9d360cSMartin Matuska 	ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed);
1570eda14cbcSMatt Macy 	ASSERT(uncompressed >= 0 ||
15713f9d360cSMartin Matuska 	    ddp->dd_uncompressed_bytes >= -uncompressed);
15723f9d360cSMartin Matuska 	ddp->dd_used_bytes += used;
15733f9d360cSMartin Matuska 	ddp->dd_uncompressed_bytes += uncompressed;
15743f9d360cSMartin Matuska 	ddp->dd_compressed_bytes += compressed;
1575eda14cbcSMatt Macy 
15763f9d360cSMartin Matuska 	if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) {
15773f9d360cSMartin Matuska 		ASSERT(used >= 0 || ddp->dd_used_breakdown[type] >= -used);
15783f9d360cSMartin Matuska 		ddp->dd_used_breakdown[type] += used;
1579eda14cbcSMatt Macy #ifdef ZFS_DEBUG
1580eda14cbcSMatt Macy 		{
1581eda14cbcSMatt Macy 			dd_used_t t;
1582eda14cbcSMatt Macy 			uint64_t u = 0;
1583eda14cbcSMatt Macy 			for (t = 0; t < DD_USED_NUM; t++)
15843f9d360cSMartin Matuska 				u += ddp->dd_used_breakdown[t];
15853f9d360cSMartin Matuska 			ASSERT3U(u, ==, ddp->dd_used_bytes);
1586eda14cbcSMatt Macy 		}
1587eda14cbcSMatt Macy #endif
1588eda14cbcSMatt Macy 	}
1589eda14cbcSMatt Macy 	if (needlock)
1590eda14cbcSMatt Macy 		mutex_exit(&dd->dd_lock);
1591eda14cbcSMatt Macy 
1592eda14cbcSMatt Macy 	if (dd->dd_parent != NULL) {
15933f9d360cSMartin Matuska 		dsl_dir_diduse_transfer_space(dd->dd_parent,
15943f9d360cSMartin Matuska 		    accounted_delta, compressed, uncompressed,
15953f9d360cSMartin Matuska 		    used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1596eda14cbcSMatt Macy 	}
1597eda14cbcSMatt Macy }
1598eda14cbcSMatt Macy 
1599eda14cbcSMatt Macy void
1600eda14cbcSMatt Macy dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1601eda14cbcSMatt Macy     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1602eda14cbcSMatt Macy {
1603eda14cbcSMatt Macy 	ASSERT(dmu_tx_is_syncing(tx));
1604eda14cbcSMatt Macy 	ASSERT(oldtype < DD_USED_NUM);
1605eda14cbcSMatt Macy 	ASSERT(newtype < DD_USED_NUM);
1606eda14cbcSMatt Macy 
16073f9d360cSMartin Matuska 	dsl_dir_phys_t *ddp = dsl_dir_phys(dd);
1608eda14cbcSMatt Macy 	if (delta == 0 ||
16093f9d360cSMartin Matuska 	    !(ddp->dd_flags & DD_FLAG_USED_BREAKDOWN))
1610eda14cbcSMatt Macy 		return;
1611eda14cbcSMatt Macy 
1612eda14cbcSMatt Macy 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1613eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1614eda14cbcSMatt Macy 	ASSERT(delta > 0 ?
16153f9d360cSMartin Matuska 	    ddp->dd_used_breakdown[oldtype] >= delta :
16163f9d360cSMartin Matuska 	    ddp->dd_used_breakdown[newtype] >= -delta);
16173f9d360cSMartin Matuska 	ASSERT(ddp->dd_used_bytes >= ABS(delta));
16183f9d360cSMartin Matuska 	ddp->dd_used_breakdown[oldtype] -= delta;
16193f9d360cSMartin Matuska 	ddp->dd_used_breakdown[newtype] += delta;
1620eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1621eda14cbcSMatt Macy }
1622eda14cbcSMatt Macy 
16233f9d360cSMartin Matuska void
16243f9d360cSMartin Matuska dsl_dir_diduse_transfer_space(dsl_dir_t *dd, int64_t used,
16253f9d360cSMartin Matuska     int64_t compressed, int64_t uncompressed, int64_t tonew,
16263f9d360cSMartin Matuska     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
16273f9d360cSMartin Matuska {
16283f9d360cSMartin Matuska 	int64_t accounted_delta;
16293f9d360cSMartin Matuska 
16303f9d360cSMartin Matuska 	ASSERT(dmu_tx_is_syncing(tx));
16313f9d360cSMartin Matuska 	ASSERT(oldtype < DD_USED_NUM);
16323f9d360cSMartin Matuska 	ASSERT(newtype < DD_USED_NUM);
16333f9d360cSMartin Matuska 
16343f9d360cSMartin Matuska 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
16353f9d360cSMartin Matuska 
16363f9d360cSMartin Matuska 	mutex_enter(&dd->dd_lock);
16373f9d360cSMartin Matuska 	dsl_dir_phys_t *ddp = dsl_dir_phys(dd);
16383f9d360cSMartin Matuska 	accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used);
16393f9d360cSMartin Matuska 	ASSERT(used >= 0 || ddp->dd_used_bytes >= -used);
16403f9d360cSMartin Matuska 	ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed);
16413f9d360cSMartin Matuska 	ASSERT(uncompressed >= 0 ||
16423f9d360cSMartin Matuska 	    ddp->dd_uncompressed_bytes >= -uncompressed);
16433f9d360cSMartin Matuska 	ddp->dd_used_bytes += used;
16443f9d360cSMartin Matuska 	ddp->dd_uncompressed_bytes += uncompressed;
16453f9d360cSMartin Matuska 	ddp->dd_compressed_bytes += compressed;
16463f9d360cSMartin Matuska 
16473f9d360cSMartin Matuska 	if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) {
16483f9d360cSMartin Matuska 		ASSERT(tonew - used <= 0 ||
16493f9d360cSMartin Matuska 		    ddp->dd_used_breakdown[oldtype] >= tonew - used);
16503f9d360cSMartin Matuska 		ASSERT(tonew >= 0 ||
16513f9d360cSMartin Matuska 		    ddp->dd_used_breakdown[newtype] >= -tonew);
16523f9d360cSMartin Matuska 		ddp->dd_used_breakdown[oldtype] -= tonew - used;
16533f9d360cSMartin Matuska 		ddp->dd_used_breakdown[newtype] += tonew;
16543f9d360cSMartin Matuska #ifdef ZFS_DEBUG
16553f9d360cSMartin Matuska 		{
16563f9d360cSMartin Matuska 			dd_used_t t;
16573f9d360cSMartin Matuska 			uint64_t u = 0;
16583f9d360cSMartin Matuska 			for (t = 0; t < DD_USED_NUM; t++)
16593f9d360cSMartin Matuska 				u += ddp->dd_used_breakdown[t];
16603f9d360cSMartin Matuska 			ASSERT3U(u, ==, ddp->dd_used_bytes);
16613f9d360cSMartin Matuska 		}
16623f9d360cSMartin Matuska #endif
16633f9d360cSMartin Matuska 	}
16643f9d360cSMartin Matuska 	mutex_exit(&dd->dd_lock);
16653f9d360cSMartin Matuska 
16663f9d360cSMartin Matuska 	if (dd->dd_parent != NULL) {
16673f9d360cSMartin Matuska 		dsl_dir_diduse_transfer_space(dd->dd_parent,
16683f9d360cSMartin Matuska 		    accounted_delta, compressed, uncompressed,
16693f9d360cSMartin Matuska 		    used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
16703f9d360cSMartin Matuska 	}
16713f9d360cSMartin Matuska }
16723f9d360cSMartin Matuska 
1673eda14cbcSMatt Macy typedef struct dsl_dir_set_qr_arg {
1674eda14cbcSMatt Macy 	const char *ddsqra_name;
1675eda14cbcSMatt Macy 	zprop_source_t ddsqra_source;
1676eda14cbcSMatt Macy 	uint64_t ddsqra_value;
1677eda14cbcSMatt Macy } dsl_dir_set_qr_arg_t;
1678eda14cbcSMatt Macy 
1679eda14cbcSMatt Macy static int
1680eda14cbcSMatt Macy dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1681eda14cbcSMatt Macy {
1682eda14cbcSMatt Macy 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1683eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1684eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1685eda14cbcSMatt Macy 	int error;
1686eda14cbcSMatt Macy 	uint64_t towrite, newval;
1687eda14cbcSMatt Macy 
1688eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1689eda14cbcSMatt Macy 	if (error != 0)
1690eda14cbcSMatt Macy 		return (error);
1691eda14cbcSMatt Macy 
1692eda14cbcSMatt Macy 	error = dsl_prop_predict(ds->ds_dir, "quota",
1693eda14cbcSMatt Macy 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1694eda14cbcSMatt Macy 	if (error != 0) {
1695eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1696eda14cbcSMatt Macy 		return (error);
1697eda14cbcSMatt Macy 	}
1698eda14cbcSMatt Macy 
1699eda14cbcSMatt Macy 	if (newval == 0) {
1700eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1701eda14cbcSMatt Macy 		return (0);
1702eda14cbcSMatt Macy 	}
1703eda14cbcSMatt Macy 
1704eda14cbcSMatt Macy 	mutex_enter(&ds->ds_dir->dd_lock);
1705eda14cbcSMatt Macy 	/*
1706eda14cbcSMatt Macy 	 * If we are doing the preliminary check in open context, and
1707eda14cbcSMatt Macy 	 * there are pending changes, then don't fail it, since the
1708eda14cbcSMatt Macy 	 * pending changes could under-estimate the amount of space to be
1709eda14cbcSMatt Macy 	 * freed up.
1710eda14cbcSMatt Macy 	 */
1711eda14cbcSMatt Macy 	towrite = dsl_dir_space_towrite(ds->ds_dir);
1712eda14cbcSMatt Macy 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1713eda14cbcSMatt Macy 	    (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1714eda14cbcSMatt Macy 	    newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1715eda14cbcSMatt Macy 		error = SET_ERROR(ENOSPC);
1716eda14cbcSMatt Macy 	}
1717eda14cbcSMatt Macy 	mutex_exit(&ds->ds_dir->dd_lock);
1718eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1719eda14cbcSMatt Macy 	return (error);
1720eda14cbcSMatt Macy }
1721eda14cbcSMatt Macy 
1722eda14cbcSMatt Macy static void
1723eda14cbcSMatt Macy dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1724eda14cbcSMatt Macy {
1725eda14cbcSMatt Macy 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1726eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1727eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1728eda14cbcSMatt Macy 	uint64_t newval;
1729eda14cbcSMatt Macy 
1730eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1731eda14cbcSMatt Macy 
1732eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1733eda14cbcSMatt Macy 		dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1734eda14cbcSMatt Macy 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1735eda14cbcSMatt Macy 		    &ddsqra->ddsqra_value, tx);
1736eda14cbcSMatt Macy 
1737eda14cbcSMatt Macy 		VERIFY0(dsl_prop_get_int_ds(ds,
1738eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1739eda14cbcSMatt Macy 	} else {
1740eda14cbcSMatt Macy 		newval = ddsqra->ddsqra_value;
1741eda14cbcSMatt Macy 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1742eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1743eda14cbcSMatt Macy 	}
1744eda14cbcSMatt Macy 
1745eda14cbcSMatt Macy 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1746eda14cbcSMatt Macy 	mutex_enter(&ds->ds_dir->dd_lock);
1747eda14cbcSMatt Macy 	dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1748eda14cbcSMatt Macy 	mutex_exit(&ds->ds_dir->dd_lock);
1749eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1750eda14cbcSMatt Macy }
1751eda14cbcSMatt Macy 
1752eda14cbcSMatt Macy int
1753eda14cbcSMatt Macy dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1754eda14cbcSMatt Macy {
1755eda14cbcSMatt Macy 	dsl_dir_set_qr_arg_t ddsqra;
1756eda14cbcSMatt Macy 
1757eda14cbcSMatt Macy 	ddsqra.ddsqra_name = ddname;
1758eda14cbcSMatt Macy 	ddsqra.ddsqra_source = source;
1759eda14cbcSMatt Macy 	ddsqra.ddsqra_value = quota;
1760eda14cbcSMatt Macy 
1761eda14cbcSMatt Macy 	return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1762eda14cbcSMatt Macy 	    dsl_dir_set_quota_sync, &ddsqra, 0,
1763eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
1764eda14cbcSMatt Macy }
1765eda14cbcSMatt Macy 
1766eda14cbcSMatt Macy static int
1767eda14cbcSMatt Macy dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1768eda14cbcSMatt Macy {
1769eda14cbcSMatt Macy 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1770eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1771eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1772eda14cbcSMatt Macy 	dsl_dir_t *dd;
1773eda14cbcSMatt Macy 	uint64_t newval, used, avail;
1774eda14cbcSMatt Macy 	int error;
1775eda14cbcSMatt Macy 
1776eda14cbcSMatt Macy 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1777eda14cbcSMatt Macy 	if (error != 0)
1778eda14cbcSMatt Macy 		return (error);
1779eda14cbcSMatt Macy 	dd = ds->ds_dir;
1780eda14cbcSMatt Macy 
1781eda14cbcSMatt Macy 	/*
1782eda14cbcSMatt Macy 	 * If we are doing the preliminary check in open context, the
1783eda14cbcSMatt Macy 	 * space estimates may be inaccurate.
1784eda14cbcSMatt Macy 	 */
1785eda14cbcSMatt Macy 	if (!dmu_tx_is_syncing(tx)) {
1786eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1787eda14cbcSMatt Macy 		return (0);
1788eda14cbcSMatt Macy 	}
1789eda14cbcSMatt Macy 
1790eda14cbcSMatt Macy 	error = dsl_prop_predict(ds->ds_dir,
1791eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1792eda14cbcSMatt Macy 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1793eda14cbcSMatt Macy 	if (error != 0) {
1794eda14cbcSMatt Macy 		dsl_dataset_rele(ds, FTAG);
1795eda14cbcSMatt Macy 		return (error);
1796eda14cbcSMatt Macy 	}
1797eda14cbcSMatt Macy 
1798eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1799eda14cbcSMatt Macy 	used = dsl_dir_phys(dd)->dd_used_bytes;
1800eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1801eda14cbcSMatt Macy 
1802eda14cbcSMatt Macy 	if (dd->dd_parent) {
1803eda14cbcSMatt Macy 		avail = dsl_dir_space_available(dd->dd_parent,
1804eda14cbcSMatt Macy 		    NULL, 0, FALSE);
1805eda14cbcSMatt Macy 	} else {
1806eda14cbcSMatt Macy 		avail = dsl_pool_adjustedsize(dd->dd_pool,
1807eda14cbcSMatt Macy 		    ZFS_SPACE_CHECK_NORMAL) - used;
1808eda14cbcSMatt Macy 	}
1809eda14cbcSMatt Macy 
1810eda14cbcSMatt Macy 	if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1811eda14cbcSMatt Macy 		uint64_t delta = MAX(used, newval) -
1812eda14cbcSMatt Macy 		    MAX(used, dsl_dir_phys(dd)->dd_reserved);
1813eda14cbcSMatt Macy 
1814eda14cbcSMatt Macy 		if (delta > avail ||
1815eda14cbcSMatt Macy 		    (dsl_dir_phys(dd)->dd_quota > 0 &&
1816eda14cbcSMatt Macy 		    newval > dsl_dir_phys(dd)->dd_quota))
1817eda14cbcSMatt Macy 			error = SET_ERROR(ENOSPC);
1818eda14cbcSMatt Macy 	}
1819eda14cbcSMatt Macy 
1820eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1821eda14cbcSMatt Macy 	return (error);
1822eda14cbcSMatt Macy }
1823eda14cbcSMatt Macy 
1824eda14cbcSMatt Macy void
1825eda14cbcSMatt Macy dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1826eda14cbcSMatt Macy {
1827eda14cbcSMatt Macy 	uint64_t used;
1828eda14cbcSMatt Macy 	int64_t delta;
1829eda14cbcSMatt Macy 
1830eda14cbcSMatt Macy 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1831eda14cbcSMatt Macy 
1832eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1833eda14cbcSMatt Macy 	used = dsl_dir_phys(dd)->dd_used_bytes;
1834eda14cbcSMatt Macy 	delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1835eda14cbcSMatt Macy 	dsl_dir_phys(dd)->dd_reserved = value;
1836eda14cbcSMatt Macy 
1837eda14cbcSMatt Macy 	if (dd->dd_parent != NULL) {
1838eda14cbcSMatt Macy 		/* Roll up this additional usage into our ancestors */
1839eda14cbcSMatt Macy 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1840eda14cbcSMatt Macy 		    delta, 0, 0, tx);
1841eda14cbcSMatt Macy 	}
1842eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1843eda14cbcSMatt Macy }
1844eda14cbcSMatt Macy 
1845eda14cbcSMatt Macy static void
1846eda14cbcSMatt Macy dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1847eda14cbcSMatt Macy {
1848eda14cbcSMatt Macy 	dsl_dir_set_qr_arg_t *ddsqra = arg;
1849eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1850eda14cbcSMatt Macy 	dsl_dataset_t *ds;
1851eda14cbcSMatt Macy 	uint64_t newval;
1852eda14cbcSMatt Macy 
1853eda14cbcSMatt Macy 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1854eda14cbcSMatt Macy 
1855eda14cbcSMatt Macy 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1856eda14cbcSMatt Macy 		dsl_prop_set_sync_impl(ds,
1857eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1858eda14cbcSMatt Macy 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1859eda14cbcSMatt Macy 		    &ddsqra->ddsqra_value, tx);
1860eda14cbcSMatt Macy 
1861eda14cbcSMatt Macy 		VERIFY0(dsl_prop_get_int_ds(ds,
1862eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1863eda14cbcSMatt Macy 	} else {
1864eda14cbcSMatt Macy 		newval = ddsqra->ddsqra_value;
1865eda14cbcSMatt Macy 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1866eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1867eda14cbcSMatt Macy 		    (longlong_t)newval);
1868eda14cbcSMatt Macy 	}
1869eda14cbcSMatt Macy 
1870eda14cbcSMatt Macy 	dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1871eda14cbcSMatt Macy 	dsl_dataset_rele(ds, FTAG);
1872eda14cbcSMatt Macy }
1873eda14cbcSMatt Macy 
1874eda14cbcSMatt Macy int
1875eda14cbcSMatt Macy dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1876eda14cbcSMatt Macy     uint64_t reservation)
1877eda14cbcSMatt Macy {
1878eda14cbcSMatt Macy 	dsl_dir_set_qr_arg_t ddsqra;
1879eda14cbcSMatt Macy 
1880eda14cbcSMatt Macy 	ddsqra.ddsqra_name = ddname;
1881eda14cbcSMatt Macy 	ddsqra.ddsqra_source = source;
1882eda14cbcSMatt Macy 	ddsqra.ddsqra_value = reservation;
1883eda14cbcSMatt Macy 
1884eda14cbcSMatt Macy 	return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1885eda14cbcSMatt Macy 	    dsl_dir_set_reservation_sync, &ddsqra, 0,
1886eda14cbcSMatt Macy 	    ZFS_SPACE_CHECK_EXTRA_RESERVED));
1887eda14cbcSMatt Macy }
1888eda14cbcSMatt Macy 
1889eda14cbcSMatt Macy static dsl_dir_t *
1890eda14cbcSMatt Macy closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1891eda14cbcSMatt Macy {
1892eda14cbcSMatt Macy 	for (; ds1; ds1 = ds1->dd_parent) {
1893eda14cbcSMatt Macy 		dsl_dir_t *dd;
1894eda14cbcSMatt Macy 		for (dd = ds2; dd; dd = dd->dd_parent) {
1895eda14cbcSMatt Macy 			if (ds1 == dd)
1896eda14cbcSMatt Macy 				return (dd);
1897eda14cbcSMatt Macy 		}
1898eda14cbcSMatt Macy 	}
1899eda14cbcSMatt Macy 	return (NULL);
1900eda14cbcSMatt Macy }
1901eda14cbcSMatt Macy 
1902eda14cbcSMatt Macy /*
1903eda14cbcSMatt Macy  * If delta is applied to dd, how much of that delta would be applied to
1904eda14cbcSMatt Macy  * ancestor?  Syncing context only.
1905eda14cbcSMatt Macy  */
1906eda14cbcSMatt Macy static int64_t
1907eda14cbcSMatt Macy would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1908eda14cbcSMatt Macy {
1909eda14cbcSMatt Macy 	if (dd == ancestor)
1910eda14cbcSMatt Macy 		return (delta);
1911eda14cbcSMatt Macy 
1912eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
1913eda14cbcSMatt Macy 	delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1914eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
1915eda14cbcSMatt Macy 	return (would_change(dd->dd_parent, delta, ancestor));
1916eda14cbcSMatt Macy }
1917eda14cbcSMatt Macy 
1918eda14cbcSMatt Macy typedef struct dsl_dir_rename_arg {
1919eda14cbcSMatt Macy 	const char *ddra_oldname;
1920eda14cbcSMatt Macy 	const char *ddra_newname;
1921eda14cbcSMatt Macy 	cred_t *ddra_cred;
1922eda14cbcSMatt Macy 	proc_t *ddra_proc;
1923eda14cbcSMatt Macy } dsl_dir_rename_arg_t;
1924eda14cbcSMatt Macy 
1925eda14cbcSMatt Macy typedef struct dsl_valid_rename_arg {
1926eda14cbcSMatt Macy 	int char_delta;
1927eda14cbcSMatt Macy 	int nest_delta;
1928eda14cbcSMatt Macy } dsl_valid_rename_arg_t;
1929eda14cbcSMatt Macy 
1930eda14cbcSMatt Macy static int
1931eda14cbcSMatt Macy dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1932eda14cbcSMatt Macy {
1933e92ffd9bSMartin Matuska 	(void) dp;
1934eda14cbcSMatt Macy 	dsl_valid_rename_arg_t *dvra = arg;
1935eda14cbcSMatt Macy 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1936eda14cbcSMatt Macy 
1937eda14cbcSMatt Macy 	dsl_dataset_name(ds, namebuf);
1938eda14cbcSMatt Macy 
1939eda14cbcSMatt Macy 	ASSERT3U(strnlen(namebuf, ZFS_MAX_DATASET_NAME_LEN),
1940eda14cbcSMatt Macy 	    <, ZFS_MAX_DATASET_NAME_LEN);
1941eda14cbcSMatt Macy 	int namelen = strlen(namebuf) + dvra->char_delta;
1942eda14cbcSMatt Macy 	int depth = get_dataset_depth(namebuf) + dvra->nest_delta;
1943eda14cbcSMatt Macy 
1944eda14cbcSMatt Macy 	if (namelen >= ZFS_MAX_DATASET_NAME_LEN)
1945eda14cbcSMatt Macy 		return (SET_ERROR(ENAMETOOLONG));
1946eda14cbcSMatt Macy 	if (dvra->nest_delta > 0 && depth >= zfs_max_dataset_nesting)
1947eda14cbcSMatt Macy 		return (SET_ERROR(ENAMETOOLONG));
1948eda14cbcSMatt Macy 	return (0);
1949eda14cbcSMatt Macy }
1950eda14cbcSMatt Macy 
1951eda14cbcSMatt Macy static int
1952eda14cbcSMatt Macy dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1953eda14cbcSMatt Macy {
1954eda14cbcSMatt Macy 	dsl_dir_rename_arg_t *ddra = arg;
1955eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
1956eda14cbcSMatt Macy 	dsl_dir_t *dd, *newparent;
1957eda14cbcSMatt Macy 	dsl_valid_rename_arg_t dvra;
1958eda14cbcSMatt Macy 	dsl_dataset_t *parentds;
1959eda14cbcSMatt Macy 	objset_t *parentos;
1960eda14cbcSMatt Macy 	const char *mynewname;
1961eda14cbcSMatt Macy 	int error;
1962eda14cbcSMatt Macy 
1963eda14cbcSMatt Macy 	/* target dir should exist */
1964eda14cbcSMatt Macy 	error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1965eda14cbcSMatt Macy 	if (error != 0)
1966eda14cbcSMatt Macy 		return (error);
1967eda14cbcSMatt Macy 
1968eda14cbcSMatt Macy 	/* new parent should exist */
1969eda14cbcSMatt Macy 	error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1970eda14cbcSMatt Macy 	    &newparent, &mynewname);
1971eda14cbcSMatt Macy 	if (error != 0) {
1972eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
1973eda14cbcSMatt Macy 		return (error);
1974eda14cbcSMatt Macy 	}
1975eda14cbcSMatt Macy 
1976eda14cbcSMatt Macy 	/* can't rename to different pool */
1977eda14cbcSMatt Macy 	if (dd->dd_pool != newparent->dd_pool) {
1978eda14cbcSMatt Macy 		dsl_dir_rele(newparent, FTAG);
1979eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
1980eda14cbcSMatt Macy 		return (SET_ERROR(EXDEV));
1981eda14cbcSMatt Macy 	}
1982eda14cbcSMatt Macy 
1983eda14cbcSMatt Macy 	/* new name should not already exist */
1984eda14cbcSMatt Macy 	if (mynewname == NULL) {
1985eda14cbcSMatt Macy 		dsl_dir_rele(newparent, FTAG);
1986eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
1987eda14cbcSMatt Macy 		return (SET_ERROR(EEXIST));
1988eda14cbcSMatt Macy 	}
1989eda14cbcSMatt Macy 
1990eda14cbcSMatt Macy 	/* can't rename below anything but filesystems (eg. no ZVOLs) */
1991eda14cbcSMatt Macy 	error = dsl_dataset_hold_obj(newparent->dd_pool,
1992eda14cbcSMatt Macy 	    dsl_dir_phys(newparent)->dd_head_dataset_obj, FTAG, &parentds);
1993eda14cbcSMatt Macy 	if (error != 0) {
1994eda14cbcSMatt Macy 		dsl_dir_rele(newparent, FTAG);
1995eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
1996eda14cbcSMatt Macy 		return (error);
1997eda14cbcSMatt Macy 	}
1998eda14cbcSMatt Macy 	error = dmu_objset_from_ds(parentds, &parentos);
1999eda14cbcSMatt Macy 	if (error != 0) {
2000eda14cbcSMatt Macy 		dsl_dataset_rele(parentds, FTAG);
2001eda14cbcSMatt Macy 		dsl_dir_rele(newparent, FTAG);
2002eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
2003eda14cbcSMatt Macy 		return (error);
2004eda14cbcSMatt Macy 	}
2005eda14cbcSMatt Macy 	if (dmu_objset_type(parentos) != DMU_OST_ZFS) {
2006eda14cbcSMatt Macy 		dsl_dataset_rele(parentds, FTAG);
2007eda14cbcSMatt Macy 		dsl_dir_rele(newparent, FTAG);
2008eda14cbcSMatt Macy 		dsl_dir_rele(dd, FTAG);
2009eda14cbcSMatt Macy 		return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
2010eda14cbcSMatt Macy 	}
2011eda14cbcSMatt Macy 	dsl_dataset_rele(parentds, FTAG);
2012eda14cbcSMatt Macy 
2013eda14cbcSMatt Macy 	ASSERT3U(strnlen(ddra->ddra_newname, ZFS_MAX_DATASET_NAME_LEN),
2014eda14cbcSMatt Macy 	    <, ZFS_MAX_DATASET_NAME_LEN);
2015eda14cbcSMatt Macy 	ASSERT3U(strnlen(ddra->ddra_oldname, ZFS_MAX_DATASET_NAME_LEN),
2016eda14cbcSMatt Macy 	    <, ZFS_MAX_DATASET_NAME_LEN);
2017eda14cbcSMatt Macy 	dvra.char_delta = strlen(ddra->ddra_newname)
2018eda14cbcSMatt Macy 	    - strlen(ddra->ddra_oldname);
2019eda14cbcSMatt Macy 	dvra.nest_delta = get_dataset_depth(ddra->ddra_newname)
2020eda14cbcSMatt Macy 	    - get_dataset_depth(ddra->ddra_oldname);
2021eda14cbcSMatt Macy 
2022eda14cbcSMatt Macy 	/* if the name length is growing, validate child name lengths */
2023eda14cbcSMatt Macy 	if (dvra.char_delta > 0 || dvra.nest_delta > 0) {
2024eda14cbcSMatt Macy 		error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
2025eda14cbcSMatt Macy 		    &dvra, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
2026eda14cbcSMatt Macy 		if (error != 0) {
2027eda14cbcSMatt Macy 			dsl_dir_rele(newparent, FTAG);
2028eda14cbcSMatt Macy 			dsl_dir_rele(dd, FTAG);
2029eda14cbcSMatt Macy 			return (error);
2030eda14cbcSMatt Macy 		}
2031eda14cbcSMatt Macy 	}
2032eda14cbcSMatt Macy 
2033eda14cbcSMatt Macy 	if (dmu_tx_is_syncing(tx)) {
2034eda14cbcSMatt Macy 		if (spa_feature_is_active(dp->dp_spa,
2035eda14cbcSMatt Macy 		    SPA_FEATURE_FS_SS_LIMIT)) {
2036eda14cbcSMatt Macy 			/*
2037eda14cbcSMatt Macy 			 * Although this is the check function and we don't
2038eda14cbcSMatt Macy 			 * normally make on-disk changes in check functions,
2039eda14cbcSMatt Macy 			 * we need to do that here.
2040eda14cbcSMatt Macy 			 *
2041eda14cbcSMatt Macy 			 * Ensure this portion of the tree's counts have been
2042eda14cbcSMatt Macy 			 * initialized in case the new parent has limits set.
2043eda14cbcSMatt Macy 			 */
2044eda14cbcSMatt Macy 			dsl_dir_init_fs_ss_count(dd, tx);
2045eda14cbcSMatt Macy 		}
2046eda14cbcSMatt Macy 	}
2047eda14cbcSMatt Macy 
2048eda14cbcSMatt Macy 	if (newparent != dd->dd_parent) {
2049eda14cbcSMatt Macy 		/* is there enough space? */
2050eda14cbcSMatt Macy 		uint64_t myspace =
2051eda14cbcSMatt Macy 		    MAX(dsl_dir_phys(dd)->dd_used_bytes,
2052eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_reserved);
2053eda14cbcSMatt Macy 		objset_t *os = dd->dd_pool->dp_meta_objset;
2054eda14cbcSMatt Macy 		uint64_t fs_cnt = 0;
2055eda14cbcSMatt Macy 		uint64_t ss_cnt = 0;
2056eda14cbcSMatt Macy 
2057eda14cbcSMatt Macy 		if (dsl_dir_is_zapified(dd)) {
2058eda14cbcSMatt Macy 			int err;
2059eda14cbcSMatt Macy 
2060eda14cbcSMatt Macy 			err = zap_lookup(os, dd->dd_object,
2061eda14cbcSMatt Macy 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2062eda14cbcSMatt Macy 			    &fs_cnt);
2063eda14cbcSMatt Macy 			if (err != ENOENT && err != 0) {
2064eda14cbcSMatt Macy 				dsl_dir_rele(newparent, FTAG);
2065eda14cbcSMatt Macy 				dsl_dir_rele(dd, FTAG);
2066eda14cbcSMatt Macy 				return (err);
2067eda14cbcSMatt Macy 			}
2068eda14cbcSMatt Macy 
2069eda14cbcSMatt Macy 			/*
2070eda14cbcSMatt Macy 			 * have to add 1 for the filesystem itself that we're
2071eda14cbcSMatt Macy 			 * moving
2072eda14cbcSMatt Macy 			 */
2073eda14cbcSMatt Macy 			fs_cnt++;
2074eda14cbcSMatt Macy 
2075eda14cbcSMatt Macy 			err = zap_lookup(os, dd->dd_object,
2076eda14cbcSMatt Macy 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2077eda14cbcSMatt Macy 			    &ss_cnt);
2078eda14cbcSMatt Macy 			if (err != ENOENT && err != 0) {
2079eda14cbcSMatt Macy 				dsl_dir_rele(newparent, FTAG);
2080eda14cbcSMatt Macy 				dsl_dir_rele(dd, FTAG);
2081eda14cbcSMatt Macy 				return (err);
2082eda14cbcSMatt Macy 			}
2083eda14cbcSMatt Macy 		}
2084eda14cbcSMatt Macy 
2085eda14cbcSMatt Macy 		/* check for encryption errors */
2086eda14cbcSMatt Macy 		error = dsl_dir_rename_crypt_check(dd, newparent);
2087eda14cbcSMatt Macy 		if (error != 0) {
2088eda14cbcSMatt Macy 			dsl_dir_rele(newparent, FTAG);
2089eda14cbcSMatt Macy 			dsl_dir_rele(dd, FTAG);
2090eda14cbcSMatt Macy 			return (SET_ERROR(EACCES));
2091eda14cbcSMatt Macy 		}
2092eda14cbcSMatt Macy 
2093eda14cbcSMatt Macy 		/* no rename into our descendant */
2094eda14cbcSMatt Macy 		if (closest_common_ancestor(dd, newparent) == dd) {
2095eda14cbcSMatt Macy 			dsl_dir_rele(newparent, FTAG);
2096eda14cbcSMatt Macy 			dsl_dir_rele(dd, FTAG);
2097eda14cbcSMatt Macy 			return (SET_ERROR(EINVAL));
2098eda14cbcSMatt Macy 		}
2099eda14cbcSMatt Macy 
2100eda14cbcSMatt Macy 		error = dsl_dir_transfer_possible(dd->dd_parent,
2101eda14cbcSMatt Macy 		    newparent, fs_cnt, ss_cnt, myspace,
2102eda14cbcSMatt Macy 		    ddra->ddra_cred, ddra->ddra_proc);
2103eda14cbcSMatt Macy 		if (error != 0) {
2104eda14cbcSMatt Macy 			dsl_dir_rele(newparent, FTAG);
2105eda14cbcSMatt Macy 			dsl_dir_rele(dd, FTAG);
2106eda14cbcSMatt Macy 			return (error);
2107eda14cbcSMatt Macy 		}
2108eda14cbcSMatt Macy 	}
2109eda14cbcSMatt Macy 
2110eda14cbcSMatt Macy 	dsl_dir_rele(newparent, FTAG);
2111eda14cbcSMatt Macy 	dsl_dir_rele(dd, FTAG);
2112eda14cbcSMatt Macy 	return (0);
2113eda14cbcSMatt Macy }
2114eda14cbcSMatt Macy 
2115eda14cbcSMatt Macy static void
2116eda14cbcSMatt Macy dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
2117eda14cbcSMatt Macy {
2118eda14cbcSMatt Macy 	dsl_dir_rename_arg_t *ddra = arg;
2119eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
2120eda14cbcSMatt Macy 	dsl_dir_t *dd, *newparent;
2121eda14cbcSMatt Macy 	const char *mynewname;
2122eda14cbcSMatt Macy 	objset_t *mos = dp->dp_meta_objset;
2123eda14cbcSMatt Macy 
2124eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
2125eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
2126eda14cbcSMatt Macy 	    &mynewname));
2127eda14cbcSMatt Macy 
2128*2a58b312SMartin Matuska 	ASSERT3P(mynewname, !=, NULL);
2129*2a58b312SMartin Matuska 
2130eda14cbcSMatt Macy 	/* Log this before we change the name. */
2131eda14cbcSMatt Macy 	spa_history_log_internal_dd(dd, "rename", tx,
2132eda14cbcSMatt Macy 	    "-> %s", ddra->ddra_newname);
2133eda14cbcSMatt Macy 
2134eda14cbcSMatt Macy 	if (newparent != dd->dd_parent) {
2135eda14cbcSMatt Macy 		objset_t *os = dd->dd_pool->dp_meta_objset;
2136eda14cbcSMatt Macy 		uint64_t fs_cnt = 0;
2137eda14cbcSMatt Macy 		uint64_t ss_cnt = 0;
2138eda14cbcSMatt Macy 
2139eda14cbcSMatt Macy 		/*
2140eda14cbcSMatt Macy 		 * We already made sure the dd counts were initialized in the
2141eda14cbcSMatt Macy 		 * check function.
2142eda14cbcSMatt Macy 		 */
2143eda14cbcSMatt Macy 		if (spa_feature_is_active(dp->dp_spa,
2144eda14cbcSMatt Macy 		    SPA_FEATURE_FS_SS_LIMIT)) {
2145eda14cbcSMatt Macy 			VERIFY0(zap_lookup(os, dd->dd_object,
2146eda14cbcSMatt Macy 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2147eda14cbcSMatt Macy 			    &fs_cnt));
2148eda14cbcSMatt Macy 			/* add 1 for the filesystem itself that we're moving */
2149eda14cbcSMatt Macy 			fs_cnt++;
2150eda14cbcSMatt Macy 
2151eda14cbcSMatt Macy 			VERIFY0(zap_lookup(os, dd->dd_object,
2152eda14cbcSMatt Macy 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2153eda14cbcSMatt Macy 			    &ss_cnt));
2154eda14cbcSMatt Macy 		}
2155eda14cbcSMatt Macy 
2156eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
2157eda14cbcSMatt Macy 		    DD_FIELD_FILESYSTEM_COUNT, tx);
2158eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(newparent, fs_cnt,
2159eda14cbcSMatt Macy 		    DD_FIELD_FILESYSTEM_COUNT, tx);
2160eda14cbcSMatt Macy 
2161eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
2162eda14cbcSMatt Macy 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2163eda14cbcSMatt Macy 		dsl_fs_ss_count_adjust(newparent, ss_cnt,
2164eda14cbcSMatt Macy 		    DD_FIELD_SNAPSHOT_COUNT, tx);
2165eda14cbcSMatt Macy 
2166eda14cbcSMatt Macy 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
2167eda14cbcSMatt Macy 		    -dsl_dir_phys(dd)->dd_used_bytes,
2168eda14cbcSMatt Macy 		    -dsl_dir_phys(dd)->dd_compressed_bytes,
2169eda14cbcSMatt Macy 		    -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2170eda14cbcSMatt Macy 		dsl_dir_diduse_space(newparent, DD_USED_CHILD,
2171eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_used_bytes,
2172eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_compressed_bytes,
2173eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2174eda14cbcSMatt Macy 
2175eda14cbcSMatt Macy 		if (dsl_dir_phys(dd)->dd_reserved >
2176eda14cbcSMatt Macy 		    dsl_dir_phys(dd)->dd_used_bytes) {
2177eda14cbcSMatt Macy 			uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
2178eda14cbcSMatt Macy 			    dsl_dir_phys(dd)->dd_used_bytes;
2179eda14cbcSMatt Macy 
2180eda14cbcSMatt Macy 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
2181eda14cbcSMatt Macy 			    -unused_rsrv, 0, 0, tx);
2182eda14cbcSMatt Macy 			dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
2183eda14cbcSMatt Macy 			    unused_rsrv, 0, 0, tx);
2184eda14cbcSMatt Macy 		}
2185eda14cbcSMatt Macy 	}
2186eda14cbcSMatt Macy 
2187eda14cbcSMatt Macy 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
2188eda14cbcSMatt Macy 
2189eda14cbcSMatt Macy 	/* remove from old parent zapobj */
2190eda14cbcSMatt Macy 	VERIFY0(zap_remove(mos,
2191eda14cbcSMatt Macy 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
2192eda14cbcSMatt Macy 	    dd->dd_myname, tx));
2193eda14cbcSMatt Macy 
2194eda14cbcSMatt Macy 	(void) strlcpy(dd->dd_myname, mynewname,
2195eda14cbcSMatt Macy 	    sizeof (dd->dd_myname));
2196eda14cbcSMatt Macy 	dsl_dir_rele(dd->dd_parent, dd);
2197eda14cbcSMatt Macy 	dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
2198eda14cbcSMatt Macy 	VERIFY0(dsl_dir_hold_obj(dp,
2199eda14cbcSMatt Macy 	    newparent->dd_object, NULL, dd, &dd->dd_parent));
2200eda14cbcSMatt Macy 
2201eda14cbcSMatt Macy 	/* add to new parent zapobj */
2202eda14cbcSMatt Macy 	VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
2203eda14cbcSMatt Macy 	    dd->dd_myname, 8, 1, &dd->dd_object, tx));
2204eda14cbcSMatt Macy 
2205eac7052fSMatt Macy 	/* TODO: A rename callback to avoid these layering violations. */
2206eac7052fSMatt Macy 	zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);
2207eda14cbcSMatt Macy 	zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname,
2208eda14cbcSMatt Macy 	    ddra->ddra_newname, B_TRUE);
2209eda14cbcSMatt Macy 
2210eda14cbcSMatt Macy 	dsl_prop_notify_all(dd);
2211eda14cbcSMatt Macy 
2212eda14cbcSMatt Macy 	dsl_dir_rele(newparent, FTAG);
2213eda14cbcSMatt Macy 	dsl_dir_rele(dd, FTAG);
2214eda14cbcSMatt Macy }
2215eda14cbcSMatt Macy 
2216eda14cbcSMatt Macy int
2217eda14cbcSMatt Macy dsl_dir_rename(const char *oldname, const char *newname)
2218eda14cbcSMatt Macy {
2219eda14cbcSMatt Macy 	dsl_dir_rename_arg_t ddra;
2220eda14cbcSMatt Macy 
2221eda14cbcSMatt Macy 	ddra.ddra_oldname = oldname;
2222eda14cbcSMatt Macy 	ddra.ddra_newname = newname;
2223eda14cbcSMatt Macy 	ddra.ddra_cred = CRED();
2224eda14cbcSMatt Macy 	ddra.ddra_proc = curproc;
2225eda14cbcSMatt Macy 
2226eda14cbcSMatt Macy 	return (dsl_sync_task(oldname,
2227eda14cbcSMatt Macy 	    dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
2228eda14cbcSMatt Macy 	    3, ZFS_SPACE_CHECK_RESERVED));
2229eda14cbcSMatt Macy }
2230eda14cbcSMatt Macy 
2231eda14cbcSMatt Macy int
2232eda14cbcSMatt Macy dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
2233eda14cbcSMatt Macy     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space,
2234eda14cbcSMatt Macy     cred_t *cr, proc_t *proc)
2235eda14cbcSMatt Macy {
2236eda14cbcSMatt Macy 	dsl_dir_t *ancestor;
2237eda14cbcSMatt Macy 	int64_t adelta;
2238eda14cbcSMatt Macy 	uint64_t avail;
2239eda14cbcSMatt Macy 	int err;
2240eda14cbcSMatt Macy 
2241eda14cbcSMatt Macy 	ancestor = closest_common_ancestor(sdd, tdd);
2242eda14cbcSMatt Macy 	adelta = would_change(sdd, -space, ancestor);
2243eda14cbcSMatt Macy 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
2244eda14cbcSMatt Macy 	if (avail < space)
2245eda14cbcSMatt Macy 		return (SET_ERROR(ENOSPC));
2246eda14cbcSMatt Macy 
2247eda14cbcSMatt Macy 	err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
2248eda14cbcSMatt Macy 	    ancestor, cr, proc);
2249eda14cbcSMatt Macy 	if (err != 0)
2250eda14cbcSMatt Macy 		return (err);
2251eda14cbcSMatt Macy 	err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
2252eda14cbcSMatt Macy 	    ancestor, cr, proc);
2253eda14cbcSMatt Macy 	if (err != 0)
2254eda14cbcSMatt Macy 		return (err);
2255eda14cbcSMatt Macy 
2256eda14cbcSMatt Macy 	return (0);
2257eda14cbcSMatt Macy }
2258eda14cbcSMatt Macy 
2259eda14cbcSMatt Macy inode_timespec_t
2260eda14cbcSMatt Macy dsl_dir_snap_cmtime(dsl_dir_t *dd)
2261eda14cbcSMatt Macy {
2262eda14cbcSMatt Macy 	inode_timespec_t t;
2263eda14cbcSMatt Macy 
2264eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
2265eda14cbcSMatt Macy 	t = dd->dd_snap_cmtime;
2266eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
2267eda14cbcSMatt Macy 
2268eda14cbcSMatt Macy 	return (t);
2269eda14cbcSMatt Macy }
2270eda14cbcSMatt Macy 
2271eda14cbcSMatt Macy void
2272271171e0SMartin Matuska dsl_dir_snap_cmtime_update(dsl_dir_t *dd, dmu_tx_t *tx)
2273eda14cbcSMatt Macy {
227408aba0aeSMartin Matuska 	dsl_pool_t *dp = dmu_tx_pool(tx);
2275eda14cbcSMatt Macy 	inode_timespec_t t;
2276eda14cbcSMatt Macy 	gethrestime(&t);
227708aba0aeSMartin Matuska 
2278eda14cbcSMatt Macy 	mutex_enter(&dd->dd_lock);
2279eda14cbcSMatt Macy 	dd->dd_snap_cmtime = t;
228008aba0aeSMartin Matuska 	if (spa_feature_is_enabled(dp->dp_spa,
228108aba0aeSMartin Matuska 	    SPA_FEATURE_EXTENSIBLE_DATASET)) {
228208aba0aeSMartin Matuska 		objset_t *mos = dd->dd_pool->dp_meta_objset;
228308aba0aeSMartin Matuska 		uint64_t ddobj = dd->dd_object;
228408aba0aeSMartin Matuska 		dsl_dir_zapify(dd, tx);
228508aba0aeSMartin Matuska 		VERIFY0(zap_update(mos, ddobj,
2286c7046f76SMartin Matuska 		    DD_FIELD_SNAPSHOTS_CHANGED,
228708aba0aeSMartin Matuska 		    sizeof (uint64_t),
228808aba0aeSMartin Matuska 		    sizeof (inode_timespec_t) / sizeof (uint64_t),
228908aba0aeSMartin Matuska 		    &t, tx));
229008aba0aeSMartin Matuska 	}
2291eda14cbcSMatt Macy 	mutex_exit(&dd->dd_lock);
2292eda14cbcSMatt Macy }
2293eda14cbcSMatt Macy 
2294eda14cbcSMatt Macy void
2295eda14cbcSMatt Macy dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
2296eda14cbcSMatt Macy {
2297eda14cbcSMatt Macy 	objset_t *mos = dd->dd_pool->dp_meta_objset;
2298eda14cbcSMatt Macy 	dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
2299eda14cbcSMatt Macy }
2300eda14cbcSMatt Macy 
2301eda14cbcSMatt Macy boolean_t
2302eda14cbcSMatt Macy dsl_dir_is_zapified(dsl_dir_t *dd)
2303eda14cbcSMatt Macy {
2304eda14cbcSMatt Macy 	dmu_object_info_t doi;
2305eda14cbcSMatt Macy 
2306eda14cbcSMatt Macy 	dmu_object_info_from_db(dd->dd_dbuf, &doi);
2307eda14cbcSMatt Macy 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2308eda14cbcSMatt Macy }
2309eda14cbcSMatt Macy 
2310eda14cbcSMatt Macy void
2311eda14cbcSMatt Macy dsl_dir_livelist_open(dsl_dir_t *dd, uint64_t obj)
2312eda14cbcSMatt Macy {
2313eda14cbcSMatt Macy 	objset_t *mos = dd->dd_pool->dp_meta_objset;
2314eda14cbcSMatt Macy 	ASSERT(spa_feature_is_active(dd->dd_pool->dp_spa,
2315eda14cbcSMatt Macy 	    SPA_FEATURE_LIVELIST));
2316eda14cbcSMatt Macy 	dsl_deadlist_open(&dd->dd_livelist, mos, obj);
2317eda14cbcSMatt Macy 	bplist_create(&dd->dd_pending_allocs);
2318eda14cbcSMatt Macy 	bplist_create(&dd->dd_pending_frees);
2319eda14cbcSMatt Macy }
2320eda14cbcSMatt Macy 
2321eda14cbcSMatt Macy void
2322eda14cbcSMatt Macy dsl_dir_livelist_close(dsl_dir_t *dd)
2323eda14cbcSMatt Macy {
2324eda14cbcSMatt Macy 	dsl_deadlist_close(&dd->dd_livelist);
2325eda14cbcSMatt Macy 	bplist_destroy(&dd->dd_pending_allocs);
2326eda14cbcSMatt Macy 	bplist_destroy(&dd->dd_pending_frees);
2327eda14cbcSMatt Macy }
2328eda14cbcSMatt Macy 
2329eda14cbcSMatt Macy void
2330eda14cbcSMatt Macy dsl_dir_remove_livelist(dsl_dir_t *dd, dmu_tx_t *tx, boolean_t total)
2331eda14cbcSMatt Macy {
2332eda14cbcSMatt Macy 	uint64_t obj;
2333eda14cbcSMatt Macy 	dsl_pool_t *dp = dmu_tx_pool(tx);
2334eda14cbcSMatt Macy 	spa_t *spa = dp->dp_spa;
2335eda14cbcSMatt Macy 	livelist_condense_entry_t to_condense = spa->spa_to_condense;
2336eda14cbcSMatt Macy 
2337eda14cbcSMatt Macy 	if (!dsl_deadlist_is_open(&dd->dd_livelist))
2338eda14cbcSMatt Macy 		return;
2339eda14cbcSMatt Macy 
2340eda14cbcSMatt Macy 	/*
2341eda14cbcSMatt Macy 	 * If the livelist being removed is set to be condensed, stop the
2342eda14cbcSMatt Macy 	 * condense zthr and indicate the cancellation in the spa_to_condense
2343eda14cbcSMatt Macy 	 * struct in case the condense no-wait synctask has already started
2344eda14cbcSMatt Macy 	 */
2345eda14cbcSMatt Macy 	zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;
2346eda14cbcSMatt Macy 	if (ll_condense_thread != NULL &&
2347eda14cbcSMatt Macy 	    (to_condense.ds != NULL) && (to_condense.ds->ds_dir == dd)) {
2348eda14cbcSMatt Macy 		/*
2349eda14cbcSMatt Macy 		 * We use zthr_wait_cycle_done instead of zthr_cancel
2350eda14cbcSMatt Macy 		 * because we don't want to destroy the zthr, just have
2351eda14cbcSMatt Macy 		 * it skip its current task.
2352eda14cbcSMatt Macy 		 */
2353eda14cbcSMatt Macy 		spa->spa_to_condense.cancelled = B_TRUE;
2354eda14cbcSMatt Macy 		zthr_wait_cycle_done(ll_condense_thread);
2355eda14cbcSMatt Macy 		/*
2356eda14cbcSMatt Macy 		 * If we've returned from zthr_wait_cycle_done without
2357eda14cbcSMatt Macy 		 * clearing the to_condense data structure it's either
2358eda14cbcSMatt Macy 		 * because the no-wait synctask has started (which is
2359eda14cbcSMatt Macy 		 * indicated by 'syncing' field of to_condense) and we
2360eda14cbcSMatt Macy 		 * can expect it to clear to_condense on its own.
2361eda14cbcSMatt Macy 		 * Otherwise, we returned before the zthr ran. The
2362eda14cbcSMatt Macy 		 * checkfunc will now fail as cancelled == B_TRUE so we
2363eda14cbcSMatt Macy 		 * can safely NULL out ds, allowing a different dir's
2364eda14cbcSMatt Macy 		 * livelist to be condensed.
2365eda14cbcSMatt Macy 		 *
2366eda14cbcSMatt Macy 		 * We can be sure that the to_condense struct will not
2367eda14cbcSMatt Macy 		 * be repopulated at this stage because both this
2368eda14cbcSMatt Macy 		 * function and dsl_livelist_try_condense execute in
2369eda14cbcSMatt Macy 		 * syncing context.
2370eda14cbcSMatt Macy 		 */
2371eda14cbcSMatt Macy 		if ((spa->spa_to_condense.ds != NULL) &&
2372eda14cbcSMatt Macy 		    !spa->spa_to_condense.syncing) {
2373eda14cbcSMatt Macy 			dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf,
2374eda14cbcSMatt Macy 			    spa);
2375eda14cbcSMatt Macy 			spa->spa_to_condense.ds = NULL;
2376eda14cbcSMatt Macy 		}
2377eda14cbcSMatt Macy 	}
2378eda14cbcSMatt Macy 
2379eda14cbcSMatt Macy 	dsl_dir_livelist_close(dd);
2380eda14cbcSMatt Macy 	VERIFY0(zap_lookup(dp->dp_meta_objset, dd->dd_object,
2381eda14cbcSMatt Macy 	    DD_FIELD_LIVELIST, sizeof (uint64_t), 1, &obj));
2382eda14cbcSMatt Macy 	VERIFY0(zap_remove(dp->dp_meta_objset, dd->dd_object,
2383eda14cbcSMatt Macy 	    DD_FIELD_LIVELIST, tx));
2384eda14cbcSMatt Macy 	if (total) {
2385eda14cbcSMatt Macy 		dsl_deadlist_free(dp->dp_meta_objset, obj, tx);
2386eda14cbcSMatt Macy 		spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx);
2387eda14cbcSMatt Macy 	}
2388eda14cbcSMatt Macy }
2389eda14cbcSMatt Macy 
2390eda14cbcSMatt Macy static int
2391eda14cbcSMatt Macy dsl_dir_activity_in_progress(dsl_dir_t *dd, dsl_dataset_t *ds,
2392eda14cbcSMatt Macy     zfs_wait_activity_t activity, boolean_t *in_progress)
2393eda14cbcSMatt Macy {
2394eda14cbcSMatt Macy 	int error = 0;
2395eda14cbcSMatt Macy 
2396eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&dd->dd_activity_lock));
2397eda14cbcSMatt Macy 
2398eda14cbcSMatt Macy 	switch (activity) {
2399eda14cbcSMatt Macy 	case ZFS_WAIT_DELETEQ: {
2400eda14cbcSMatt Macy #ifdef _KERNEL
2401eda14cbcSMatt Macy 		objset_t *os;
2402eda14cbcSMatt Macy 		error = dmu_objset_from_ds(ds, &os);
2403eda14cbcSMatt Macy 		if (error != 0)
2404eda14cbcSMatt Macy 			break;
2405eda14cbcSMatt Macy 
2406eda14cbcSMatt Macy 		mutex_enter(&os->os_user_ptr_lock);
2407eda14cbcSMatt Macy 		void *user = dmu_objset_get_user(os);
2408eda14cbcSMatt Macy 		mutex_exit(&os->os_user_ptr_lock);
2409eda14cbcSMatt Macy 		if (dmu_objset_type(os) != DMU_OST_ZFS ||
2410eda14cbcSMatt Macy 		    user == NULL || zfs_get_vfs_flag_unmounted(os)) {
2411eda14cbcSMatt Macy 			*in_progress = B_FALSE;
2412eda14cbcSMatt Macy 			return (0);
2413eda14cbcSMatt Macy 		}
2414eda14cbcSMatt Macy 
2415eda14cbcSMatt Macy 		uint64_t readonly = B_FALSE;
2416eda14cbcSMatt Macy 		error = zfs_get_temporary_prop(ds, ZFS_PROP_READONLY, &readonly,
2417eda14cbcSMatt Macy 		    NULL);
2418eda14cbcSMatt Macy 
2419eda14cbcSMatt Macy 		if (error != 0)
2420eda14cbcSMatt Macy 			break;
2421eda14cbcSMatt Macy 
2422eda14cbcSMatt Macy 		if (readonly || !spa_writeable(dd->dd_pool->dp_spa)) {
2423eda14cbcSMatt Macy 			*in_progress = B_FALSE;
2424eda14cbcSMatt Macy 			return (0);
2425eda14cbcSMatt Macy 		}
2426eda14cbcSMatt Macy 
2427eda14cbcSMatt Macy 		uint64_t count, unlinked_obj;
2428eda14cbcSMatt Macy 		error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
2429eda14cbcSMatt Macy 		    &unlinked_obj);
2430eda14cbcSMatt Macy 		if (error != 0) {
2431eda14cbcSMatt Macy 			dsl_dataset_rele(ds, FTAG);
2432eda14cbcSMatt Macy 			break;
2433eda14cbcSMatt Macy 		}
2434eda14cbcSMatt Macy 		error = zap_count(os, unlinked_obj, &count);
2435eda14cbcSMatt Macy 
2436eda14cbcSMatt Macy 		if (error == 0)
2437eda14cbcSMatt Macy 			*in_progress = (count != 0);
2438eda14cbcSMatt Macy 		break;
2439eda14cbcSMatt Macy #else
2440eda14cbcSMatt Macy 		/*
2441eda14cbcSMatt Macy 		 * The delete queue is ZPL specific, and libzpool doesn't have
2442eda14cbcSMatt Macy 		 * it. It doesn't make sense to wait for it.
2443eda14cbcSMatt Macy 		 */
2444e92ffd9bSMartin Matuska 		(void) ds;
2445eda14cbcSMatt Macy 		*in_progress = B_FALSE;
2446eda14cbcSMatt Macy 		break;
2447eda14cbcSMatt Macy #endif
2448eda14cbcSMatt Macy 	}
2449eda14cbcSMatt Macy 	default:
2450eda14cbcSMatt Macy 		panic("unrecognized value for activity %d", activity);
2451eda14cbcSMatt Macy 	}
2452eda14cbcSMatt Macy 
2453eda14cbcSMatt Macy 	return (error);
2454eda14cbcSMatt Macy }
2455eda14cbcSMatt Macy 
2456eda14cbcSMatt Macy int
2457eda14cbcSMatt Macy dsl_dir_wait(dsl_dir_t *dd, dsl_dataset_t *ds, zfs_wait_activity_t activity,
2458eda14cbcSMatt Macy     boolean_t *waited)
2459eda14cbcSMatt Macy {
2460eda14cbcSMatt Macy 	int error = 0;
2461eda14cbcSMatt Macy 	boolean_t in_progress;
2462eda14cbcSMatt Macy 	dsl_pool_t *dp = dd->dd_pool;
2463eda14cbcSMatt Macy 	for (;;) {
2464eda14cbcSMatt Macy 		dsl_pool_config_enter(dp, FTAG);
2465eda14cbcSMatt Macy 		error = dsl_dir_activity_in_progress(dd, ds, activity,
2466eda14cbcSMatt Macy 		    &in_progress);
2467eda14cbcSMatt Macy 		dsl_pool_config_exit(dp, FTAG);
2468eda14cbcSMatt Macy 		if (error != 0 || !in_progress)
2469eda14cbcSMatt Macy 			break;
2470eda14cbcSMatt Macy 
2471eda14cbcSMatt Macy 		*waited = B_TRUE;
2472eda14cbcSMatt Macy 
2473eda14cbcSMatt Macy 		if (cv_wait_sig(&dd->dd_activity_cv, &dd->dd_activity_lock) ==
2474eda14cbcSMatt Macy 		    0 || dd->dd_activity_cancelled) {
2475eda14cbcSMatt Macy 			error = SET_ERROR(EINTR);
2476eda14cbcSMatt Macy 			break;
2477eda14cbcSMatt Macy 		}
2478eda14cbcSMatt Macy 	}
2479eda14cbcSMatt Macy 	return (error);
2480eda14cbcSMatt Macy }
2481eda14cbcSMatt Macy 
2482eda14cbcSMatt Macy void
2483eda14cbcSMatt Macy dsl_dir_cancel_waiters(dsl_dir_t *dd)
2484eda14cbcSMatt Macy {
2485eda14cbcSMatt Macy 	mutex_enter(&dd->dd_activity_lock);
2486eda14cbcSMatt Macy 	dd->dd_activity_cancelled = B_TRUE;
2487eda14cbcSMatt Macy 	cv_broadcast(&dd->dd_activity_cv);
2488eda14cbcSMatt Macy 	while (dd->dd_activity_waiters > 0)
2489eda14cbcSMatt Macy 		cv_wait(&dd->dd_activity_cv, &dd->dd_activity_lock);
2490eda14cbcSMatt Macy 	mutex_exit(&dd->dd_activity_lock);
2491eda14cbcSMatt Macy }
2492eda14cbcSMatt Macy 
2493eda14cbcSMatt Macy #if defined(_KERNEL)
2494eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dir_set_quota);
2495eda14cbcSMatt Macy EXPORT_SYMBOL(dsl_dir_set_reservation);
2496eda14cbcSMatt Macy #endif
2497dbd5678dSMartin Matuska 
2498dbd5678dSMartin Matuska /* CSTYLED */
2499dbd5678dSMartin Matuska ZFS_MODULE_PARAM(zfs, , zvol_enforce_quotas, INT, ZMOD_RW,
2500dbd5678dSMartin Matuska 	"Enable strict ZVOL quota enforcment");
2501