xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision c1379625401dfbe1c39b79136dd384a571d47fde)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237d46dc6cSMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2014 Joyent, Inc. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/dmu.h>
29a9799022Sck #include <sys/dmu_objset.h>
30fa9e4066Sahrens #include <sys/dmu_tx.h>
31fa9e4066Sahrens #include <sys/dsl_dataset.h>
32fa9e4066Sahrens #include <sys/dsl_dir.h>
33fa9e4066Sahrens #include <sys/dsl_prop.h>
341d452cf5Sahrens #include <sys/dsl_synctask.h>
35ecd6cf80Smarks #include <sys/dsl_deleg.h>
362acef22dSMatthew Ahrens #include <sys/dmu_impl.h>
37fa9e4066Sahrens #include <sys/spa.h>
38b24ab676SJeff Bonwick #include <sys/metaslab.h>
39fa9e4066Sahrens #include <sys/zap.h>
40fa9e4066Sahrens #include <sys/zio.h>
41fa9e4066Sahrens #include <sys/arc.h>
42ecd6cf80Smarks #include <sys/sunddi.h>
43a2afb611SJerry Jelinek #include <sys/zfeature.h>
44a2afb611SJerry Jelinek #include <sys/policy.h>
45a2afb611SJerry Jelinek #include <sys/zfs_znode.h>
46fa9e4066Sahrens #include "zfs_namecheck.h"
47a2afb611SJerry Jelinek #include "zfs_prop.h"
48a2afb611SJerry Jelinek 
49a2afb611SJerry Jelinek /*
50a2afb611SJerry Jelinek  * Filesystem and Snapshot Limits
51a2afb611SJerry Jelinek  * ------------------------------
52a2afb611SJerry Jelinek  *
53a2afb611SJerry Jelinek  * These limits are used to restrict the number of filesystems and/or snapshots
54a2afb611SJerry Jelinek  * that can be created at a given level in the tree or below. A typical
55a2afb611SJerry Jelinek  * use-case is with a delegated dataset where the administrator wants to ensure
56a2afb611SJerry Jelinek  * that a user within the zone is not creating too many additional filesystems
57a2afb611SJerry Jelinek  * or snapshots, even though they're not exceeding their space quota.
58a2afb611SJerry Jelinek  *
59a2afb611SJerry Jelinek  * The filesystem and snapshot counts are stored as extensible properties. This
60a2afb611SJerry Jelinek  * capability is controlled by a feature flag and must be enabled to be used.
61a2afb611SJerry Jelinek  * Once enabled, the feature is not active until the first limit is set. At
62a2afb611SJerry Jelinek  * that point, future operations to create/destroy filesystems or snapshots
63a2afb611SJerry Jelinek  * will validate and update the counts.
64a2afb611SJerry Jelinek  *
65a2afb611SJerry Jelinek  * Because the count properties will not exist before the feature is active,
66a2afb611SJerry Jelinek  * the counts are updated when a limit is first set on an uninitialized
67a2afb611SJerry Jelinek  * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
68a2afb611SJerry Jelinek  * all of the nested filesystems/snapshots. Thus, a new leaf node has a
69a2afb611SJerry Jelinek  * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
70a2afb611SJerry Jelinek  * snapshot count properties on a node indicate uninitialized counts on that
71a2afb611SJerry Jelinek  * node.) When first setting a limit on an uninitialized node, the code starts
72a2afb611SJerry Jelinek  * at the filesystem with the new limit and descends into all sub-filesystems
73a2afb611SJerry Jelinek  * to add the count properties.
74a2afb611SJerry Jelinek  *
75a2afb611SJerry Jelinek  * In practice this is lightweight since a limit is typically set when the
76a2afb611SJerry Jelinek  * filesystem is created and thus has no children. Once valid, changing the
77a2afb611SJerry Jelinek  * limit value won't require a re-traversal since the counts are already valid.
78a2afb611SJerry Jelinek  * When recursively fixing the counts, if a node with a limit is encountered
79a2afb611SJerry Jelinek  * during the descent, the counts are known to be valid and there is no need to
80a2afb611SJerry Jelinek  * descend into that filesystem's children. The counts on filesystems above the
81a2afb611SJerry Jelinek  * one with the new limit will still be uninitialized, unless a limit is
82a2afb611SJerry Jelinek  * eventually set on one of those filesystems. The counts are always recursively
83a2afb611SJerry Jelinek  * updated when a limit is set on a dataset, unless there is already a limit.
84a2afb611SJerry Jelinek  * When a new limit value is set on a filesystem with an existing limit, it is
85a2afb611SJerry Jelinek  * possible for the new limit to be less than the current count at that level
86a2afb611SJerry Jelinek  * since a user who can change the limit is also allowed to exceed the limit.
87a2afb611SJerry Jelinek  *
88a2afb611SJerry Jelinek  * Once the feature is active, then whenever a filesystem or snapshot is
89a2afb611SJerry Jelinek  * created, the code recurses up the tree, validating the new count against the
90a2afb611SJerry Jelinek  * limit at each initialized level. In practice, most levels will not have a
91a2afb611SJerry Jelinek  * limit set. If there is a limit at any initialized level up the tree, the
92a2afb611SJerry Jelinek  * check must pass or the creation will fail. Likewise, when a filesystem or
93a2afb611SJerry Jelinek  * snapshot is destroyed, the counts are recursively adjusted all the way up
94a2afb611SJerry Jelinek  * the initizized nodes in the tree. Renaming a filesystem into different point
95a2afb611SJerry Jelinek  * in the tree will first validate, then update the counts on each branch up to
96a2afb611SJerry Jelinek  * the common ancestor. A receive will also validate the counts and then update
97a2afb611SJerry Jelinek  * them.
98a2afb611SJerry Jelinek  *
99a2afb611SJerry Jelinek  * An exception to the above behavior is that the limit is not enforced if the
100a2afb611SJerry Jelinek  * user has permission to modify the limit. This is primarily so that
101a2afb611SJerry Jelinek  * recursive snapshots in the global zone always work. We want to prevent a
102a2afb611SJerry Jelinek  * denial-of-service in which a lower level delegated dataset could max out its
103a2afb611SJerry Jelinek  * limit and thus block recursive snapshots from being taken in the global zone.
104a2afb611SJerry Jelinek  * Because of this, it is possible for the snapshot count to be over the limit
105a2afb611SJerry Jelinek  * and snapshots taken in the global zone could cause a lower level dataset to
106a2afb611SJerry Jelinek  * hit or exceed its limit. The administrator taking the global zone recursive
107a2afb611SJerry Jelinek  * snapshot should be aware of this side-effect and behave accordingly.
108a2afb611SJerry Jelinek  * For consistency, the filesystem limit is also not enforced if the user can
109a2afb611SJerry Jelinek  * modify the limit.
110a2afb611SJerry Jelinek  *
111a2afb611SJerry Jelinek  * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
112a2afb611SJerry Jelinek  * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
113a2afb611SJerry Jelinek  * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
114a2afb611SJerry Jelinek  * dsl_dir_init_fs_ss_count().
115a2afb611SJerry Jelinek  *
116a2afb611SJerry Jelinek  * There is a special case when we receive a filesystem that already exists. In
117a2afb611SJerry Jelinek  * this case a temporary clone name of %X is created (see dmu_recv_begin). We
118a2afb611SJerry Jelinek  * never update the filesystem counts for temporary clones.
119a2afb611SJerry Jelinek  *
120a2afb611SJerry Jelinek  * Likewise, we do not update the snapshot counts for temporary snapshots,
121a2afb611SJerry Jelinek  * such as those created by zfs diff.
122a2afb611SJerry Jelinek  */
123fa9e4066Sahrens 
124*c1379625SJustin T. Gibbs extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
125*c1379625SJustin T. Gibbs 
126a9799022Sck static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
127fa9e4066Sahrens 
128fa9e4066Sahrens /* ARGSUSED */
129fa9e4066Sahrens static void
130fa9e4066Sahrens dsl_dir_evict(dmu_buf_t *db, void *arg)
131fa9e4066Sahrens {
132fa9e4066Sahrens 	dsl_dir_t *dd = arg;
133fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
134fa9e4066Sahrens 	int t;
135fa9e4066Sahrens 
136fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
137fa9e4066Sahrens 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
138fa9e4066Sahrens 		ASSERT(dd->dd_tempreserved[t] == 0);
139fa9e4066Sahrens 		ASSERT(dd->dd_space_towrite[t] == 0);
140fa9e4066Sahrens 	}
141fa9e4066Sahrens 
142fa9e4066Sahrens 	if (dd->dd_parent)
1433b2aab18SMatthew Ahrens 		dsl_dir_rele(dd->dd_parent, dd);
144fa9e4066Sahrens 
145fa9e4066Sahrens 	spa_close(dd->dd_pool->dp_spa, dd);
146fa9e4066Sahrens 
147fa9e4066Sahrens 	/*
1483f9d6ad7SLin Ling 	 * The props callback list should have been cleaned up by
1493f9d6ad7SLin Ling 	 * objset_evict().
150fa9e4066Sahrens 	 */
151fa9e4066Sahrens 	list_destroy(&dd->dd_prop_cbs);
1525ad82045Snd 	mutex_destroy(&dd->dd_lock);
153fa9e4066Sahrens 	kmem_free(dd, sizeof (dsl_dir_t));
154fa9e4066Sahrens }
155fa9e4066Sahrens 
156ea8dc4b6Seschrock int
1573b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
158ea8dc4b6Seschrock     const char *tail, void *tag, dsl_dir_t **ddp)
159fa9e4066Sahrens {
160fa9e4066Sahrens 	dmu_buf_t *dbuf;
161fa9e4066Sahrens 	dsl_dir_t *dd;
162ea8dc4b6Seschrock 	int err;
163fa9e4066Sahrens 
1643b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
165fa9e4066Sahrens 
166ea8dc4b6Seschrock 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
1673b2aab18SMatthew Ahrens 	if (err != 0)
168ea8dc4b6Seschrock 		return (err);
169fa9e4066Sahrens 	dd = dmu_buf_get_user(dbuf);
170fa9e4066Sahrens #ifdef ZFS_DEBUG
171fa9e4066Sahrens 	{
172fa9e4066Sahrens 		dmu_object_info_t doi;
173fa9e4066Sahrens 		dmu_object_info_from_db(dbuf, &doi);
1742acef22dSMatthew Ahrens 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
17574e7dc98SMatthew Ahrens 		ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
176fa9e4066Sahrens 	}
177fa9e4066Sahrens #endif
178fa9e4066Sahrens 	if (dd == NULL) {
179fa9e4066Sahrens 		dsl_dir_t *winner;
180fa9e4066Sahrens 
181fa9e4066Sahrens 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
182fa9e4066Sahrens 		dd->dd_object = ddobj;
183fa9e4066Sahrens 		dd->dd_dbuf = dbuf;
184fa9e4066Sahrens 		dd->dd_pool = dp;
1855ad82045Snd 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
186fa9e4066Sahrens 
187fa9e4066Sahrens 		list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
188fa9e4066Sahrens 		    offsetof(dsl_prop_cb_record_t, cbr_node));
189fa9e4066Sahrens 
19071eb0538SChris Kirby 		dsl_dir_snap_cmtime_update(dd);
19171eb0538SChris Kirby 
192*c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_parent_obj) {
193*c1379625SJustin T. Gibbs 			err = dsl_dir_hold_obj(dp,
194*c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
195*c1379625SJustin T. Gibbs 			    &dd->dd_parent);
1963b2aab18SMatthew Ahrens 			if (err != 0)
19774e7dc98SMatthew Ahrens 				goto errout;
198fa9e4066Sahrens 			if (tail) {
199fa9e4066Sahrens #ifdef ZFS_DEBUG
200fa9e4066Sahrens 				uint64_t foundobj;
201fa9e4066Sahrens 
202fa9e4066Sahrens 				err = zap_lookup(dp->dp_meta_objset,
203*c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
204*c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj, tail,
205*c1379625SJustin T. Gibbs 				    sizeof (foundobj), 1, &foundobj);
206ea8dc4b6Seschrock 				ASSERT(err || foundobj == ddobj);
207fa9e4066Sahrens #endif
208fa9e4066Sahrens 				(void) strcpy(dd->dd_myname, tail);
209fa9e4066Sahrens 			} else {
210fa9e4066Sahrens 				err = zap_value_search(dp->dp_meta_objset,
211*c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
212*c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj,
213e7437265Sahrens 				    ddobj, 0, dd->dd_myname);
214ea8dc4b6Seschrock 			}
2153b2aab18SMatthew Ahrens 			if (err != 0)
21674e7dc98SMatthew Ahrens 				goto errout;
217fa9e4066Sahrens 		} else {
218fa9e4066Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
219fa9e4066Sahrens 		}
220fa9e4066Sahrens 
2213f9d6ad7SLin Ling 		if (dsl_dir_is_clone(dd)) {
2223f9d6ad7SLin Ling 			dmu_buf_t *origin_bonus;
2233f9d6ad7SLin Ling 			dsl_dataset_phys_t *origin_phys;
2243f9d6ad7SLin Ling 
2253f9d6ad7SLin Ling 			/*
2263f9d6ad7SLin Ling 			 * We can't open the origin dataset, because
2273f9d6ad7SLin Ling 			 * that would require opening this dsl_dir.
2283f9d6ad7SLin Ling 			 * Just look at its phys directly instead.
2293f9d6ad7SLin Ling 			 */
2303f9d6ad7SLin Ling 			err = dmu_bonus_hold(dp->dp_meta_objset,
231*c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_origin_obj, FTAG,
232*c1379625SJustin T. Gibbs 			    &origin_bonus);
2333b2aab18SMatthew Ahrens 			if (err != 0)
2343f9d6ad7SLin Ling 				goto errout;
2353f9d6ad7SLin Ling 			origin_phys = origin_bonus->db_data;
2363f9d6ad7SLin Ling 			dd->dd_origin_txg =
2373f9d6ad7SLin Ling 			    origin_phys->ds_creation_txg;
2383f9d6ad7SLin Ling 			dmu_buf_rele(origin_bonus, FTAG);
2393f9d6ad7SLin Ling 		}
2403f9d6ad7SLin Ling 
241*c1379625SJustin T. Gibbs 		winner = dmu_buf_set_user_ie(dbuf, dd, dsl_dir_evict);
242fa9e4066Sahrens 		if (winner) {
243fa9e4066Sahrens 			if (dd->dd_parent)
2443b2aab18SMatthew Ahrens 				dsl_dir_rele(dd->dd_parent, dd);
2455ad82045Snd 			mutex_destroy(&dd->dd_lock);
246fa9e4066Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
247fa9e4066Sahrens 			dd = winner;
248fa9e4066Sahrens 		} else {
249fa9e4066Sahrens 			spa_open_ref(dp->dp_spa, dd);
250fa9e4066Sahrens 		}
251fa9e4066Sahrens 	}
252fa9e4066Sahrens 
253fa9e4066Sahrens 	/*
254fa9e4066Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
255fa9e4066Sahrens 	 * holds on the spa.  We need the open-to-close holds because
256fa9e4066Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
257fa9e4066Sahrens 	 * dir which the spa also has open, so we could incorrectly
258fa9e4066Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
259fa9e4066Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
260fa9e4066Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
261fa9e4066Sahrens 	 */
262fa9e4066Sahrens 	spa_open_ref(dp->dp_spa, tag);
263fa9e4066Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
264fa9e4066Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
265fa9e4066Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
266ea8dc4b6Seschrock 	*ddp = dd;
267ea8dc4b6Seschrock 	return (0);
26874e7dc98SMatthew Ahrens 
26974e7dc98SMatthew Ahrens errout:
27074e7dc98SMatthew Ahrens 	if (dd->dd_parent)
2713b2aab18SMatthew Ahrens 		dsl_dir_rele(dd->dd_parent, dd);
27274e7dc98SMatthew Ahrens 	mutex_destroy(&dd->dd_lock);
27374e7dc98SMatthew Ahrens 	kmem_free(dd, sizeof (dsl_dir_t));
27474e7dc98SMatthew Ahrens 	dmu_buf_rele(dbuf, tag);
27574e7dc98SMatthew Ahrens 	return (err);
276fa9e4066Sahrens }
277fa9e4066Sahrens 
278fa9e4066Sahrens void
2793b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag)
280fa9e4066Sahrens {
281fa9e4066Sahrens 	dprintf_dd(dd, "%s\n", "");
282fa9e4066Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
283ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
284fa9e4066Sahrens }
285fa9e4066Sahrens 
28615f66a7fSek /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
287fa9e4066Sahrens void
288fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
289fa9e4066Sahrens {
290fa9e4066Sahrens 	if (dd->dd_parent) {
291fa9e4066Sahrens 		dsl_dir_name(dd->dd_parent, buf);
292fa9e4066Sahrens 		(void) strcat(buf, "/");
293fa9e4066Sahrens 	} else {
294fa9e4066Sahrens 		buf[0] = '\0';
295fa9e4066Sahrens 	}
296fa9e4066Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
297fa9e4066Sahrens 		/*
298fa9e4066Sahrens 		 * recursive mutex so that we can use
299fa9e4066Sahrens 		 * dprintf_dd() with dd_lock held
300fa9e4066Sahrens 		 */
301fa9e4066Sahrens 		mutex_enter(&dd->dd_lock);
302fa9e4066Sahrens 		(void) strcat(buf, dd->dd_myname);
303fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
304fa9e4066Sahrens 	} else {
305fa9e4066Sahrens 		(void) strcat(buf, dd->dd_myname);
306fa9e4066Sahrens 	}
307fa9e4066Sahrens }
308fa9e4066Sahrens 
309ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
310b7661cccSmmusante int
311b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd)
312b7661cccSmmusante {
313b7661cccSmmusante 	int result = 0;
314b7661cccSmmusante 
315b7661cccSmmusante 	if (dd->dd_parent) {
316b7661cccSmmusante 		/* parent's name + 1 for the "/" */
317b7661cccSmmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
318b7661cccSmmusante 	}
319b7661cccSmmusante 
320b7661cccSmmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
321b7661cccSmmusante 		/* see dsl_dir_name */
322b7661cccSmmusante 		mutex_enter(&dd->dd_lock);
323b7661cccSmmusante 		result += strlen(dd->dd_myname);
324b7661cccSmmusante 		mutex_exit(&dd->dd_lock);
325b7661cccSmmusante 	} else {
326b7661cccSmmusante 		result += strlen(dd->dd_myname);
327b7661cccSmmusante 	}
328b7661cccSmmusante 
329b7661cccSmmusante 	return (result);
330b7661cccSmmusante }
331b7661cccSmmusante 
332fa9e4066Sahrens static int
333fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp)
334fa9e4066Sahrens {
335fa9e4066Sahrens 	char *p;
3363b2aab18SMatthew Ahrens 
337ccba0801SRich Morris 	if ((path == NULL) || (path[0] == '\0'))
338be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
339fa9e4066Sahrens 	/* This would be a good place to reserve some namespace... */
340fa9e4066Sahrens 	p = strpbrk(path, "/@");
341fa9e4066Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
342fa9e4066Sahrens 		/* two separators in a row */
343be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
344fa9e4066Sahrens 	}
345fa9e4066Sahrens 	if (p == NULL || p == path) {
346fa9e4066Sahrens 		/*
347fa9e4066Sahrens 		 * if the first thing is an @ or /, it had better be an
348fa9e4066Sahrens 		 * @ and it had better not have any more ats or slashes,
349fa9e4066Sahrens 		 * and it had better have something after the @.
350fa9e4066Sahrens 		 */
351fa9e4066Sahrens 		if (p != NULL &&
352fa9e4066Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
353be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
354fa9e4066Sahrens 		if (strlen(path) >= MAXNAMELEN)
355be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
356fa9e4066Sahrens 		(void) strcpy(component, path);
357fa9e4066Sahrens 		p = NULL;
358fa9e4066Sahrens 	} else if (p[0] == '/') {
3593b2aab18SMatthew Ahrens 		if (p - path >= MAXNAMELEN)
360be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
361fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
3623b2aab18SMatthew Ahrens 		component[p - path] = '\0';
363fa9e4066Sahrens 		p++;
364fa9e4066Sahrens 	} else if (p[0] == '@') {
365fa9e4066Sahrens 		/*
366fa9e4066Sahrens 		 * if the next separator is an @, there better not be
367fa9e4066Sahrens 		 * any more slashes.
368fa9e4066Sahrens 		 */
369fa9e4066Sahrens 		if (strchr(path, '/'))
370be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3713b2aab18SMatthew Ahrens 		if (p - path >= MAXNAMELEN)
372be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
373fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
3743b2aab18SMatthew Ahrens 		component[p - path] = '\0';
375fa9e4066Sahrens 	} else {
3763b2aab18SMatthew Ahrens 		panic("invalid p=%p", (void *)p);
377fa9e4066Sahrens 	}
378fa9e4066Sahrens 	*nextp = p;
379fa9e4066Sahrens 	return (0);
380fa9e4066Sahrens }
381fa9e4066Sahrens 
382fa9e4066Sahrens /*
3833b2aab18SMatthew Ahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
3843b2aab18SMatthew Ahrens  * be found in *tail.  The name must be in the specified dsl_pool_t.  This
3853b2aab18SMatthew Ahrens  * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
3863b2aab18SMatthew Ahrens  * path is bogus, or if tail==NULL and we couldn't parse the whole name.
3873b2aab18SMatthew Ahrens  * (*tail)[0] == '@' means that the last component is a snapshot.
388fa9e4066Sahrens  */
389ea8dc4b6Seschrock int
3903b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
391ea8dc4b6Seschrock     dsl_dir_t **ddp, const char **tailp)
392fa9e4066Sahrens {
393fa9e4066Sahrens 	char buf[MAXNAMELEN];
3943b2aab18SMatthew Ahrens 	const char *spaname, *next, *nextnext = NULL;
395fa9e4066Sahrens 	int err;
396fa9e4066Sahrens 	dsl_dir_t *dd;
397fa9e4066Sahrens 	uint64_t ddobj;
398fa9e4066Sahrens 
399fa9e4066Sahrens 	err = getcomponent(name, buf, &next);
4003b2aab18SMatthew Ahrens 	if (err != 0)
401ea8dc4b6Seschrock 		return (err);
402fa9e4066Sahrens 
4033b2aab18SMatthew Ahrens 	/* Make sure the name is in the specified pool. */
4043b2aab18SMatthew Ahrens 	spaname = spa_name(dp->dp_spa);
4053b2aab18SMatthew Ahrens 	if (strcmp(buf, spaname) != 0)
406be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
407fa9e4066Sahrens 
4083b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
409fa9e4066Sahrens 
4103b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
4113b2aab18SMatthew Ahrens 	if (err != 0) {
412ea8dc4b6Seschrock 		return (err);
413ea8dc4b6Seschrock 	}
414ea8dc4b6Seschrock 
415fa9e4066Sahrens 	while (next != NULL) {
416fa9e4066Sahrens 		dsl_dir_t *child_ds;
417fa9e4066Sahrens 		err = getcomponent(next, buf, &nextnext);
4183b2aab18SMatthew Ahrens 		if (err != 0)
419ea8dc4b6Seschrock 			break;
420fa9e4066Sahrens 		ASSERT(next[0] != '\0');
421fa9e4066Sahrens 		if (next[0] == '@')
422fa9e4066Sahrens 			break;
423fa9e4066Sahrens 		dprintf("looking up %s in obj%lld\n",
424*c1379625SJustin T. Gibbs 		    buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
425fa9e4066Sahrens 
426fa9e4066Sahrens 		err = zap_lookup(dp->dp_meta_objset,
427*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj,
428fa9e4066Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
4293b2aab18SMatthew Ahrens 		if (err != 0) {
430ea8dc4b6Seschrock 			if (err == ENOENT)
431ea8dc4b6Seschrock 				err = 0;
432fa9e4066Sahrens 			break;
433fa9e4066Sahrens 		}
434fa9e4066Sahrens 
4353b2aab18SMatthew Ahrens 		err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_ds);
4363b2aab18SMatthew Ahrens 		if (err != 0)
437ea8dc4b6Seschrock 			break;
4383b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
439fa9e4066Sahrens 		dd = child_ds;
440fa9e4066Sahrens 		next = nextnext;
441fa9e4066Sahrens 	}
442fa9e4066Sahrens 
4433b2aab18SMatthew Ahrens 	if (err != 0) {
4443b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
445ea8dc4b6Seschrock 		return (err);
446ea8dc4b6Seschrock 	}
447ea8dc4b6Seschrock 
448fa9e4066Sahrens 	/*
449fa9e4066Sahrens 	 * It's an error if there's more than one component left, or
450fa9e4066Sahrens 	 * tailp==NULL and there's any component left.
451fa9e4066Sahrens 	 */
452fa9e4066Sahrens 	if (next != NULL &&
453fa9e4066Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
454fa9e4066Sahrens 		/* bad path name */
4553b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
456fa9e4066Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
457be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
458fa9e4066Sahrens 	}
4593b2aab18SMatthew Ahrens 	if (tailp != NULL)
460fa9e4066Sahrens 		*tailp = next;
461ea8dc4b6Seschrock 	*ddp = dd;
462ea8dc4b6Seschrock 	return (err);
463fa9e4066Sahrens }
464fa9e4066Sahrens 
465a2afb611SJerry Jelinek /*
466a2afb611SJerry Jelinek  * If the counts are already initialized for this filesystem and its
467a2afb611SJerry Jelinek  * descendants then do nothing, otherwise initialize the counts.
468a2afb611SJerry Jelinek  *
469a2afb611SJerry Jelinek  * The counts on this filesystem, and those below, may be uninitialized due to
470a2afb611SJerry Jelinek  * either the use of a pre-existing pool which did not support the
471a2afb611SJerry Jelinek  * filesystem/snapshot limit feature, or one in which the feature had not yet
472a2afb611SJerry Jelinek  * been enabled.
473a2afb611SJerry Jelinek  *
474a2afb611SJerry Jelinek  * Recursively descend the filesystem tree and update the filesystem/snapshot
475a2afb611SJerry Jelinek  * counts on each filesystem below, then update the cumulative count on the
476a2afb611SJerry Jelinek  * current filesystem. If the filesystem already has a count set on it,
477a2afb611SJerry Jelinek  * then we know that its counts, and the counts on the filesystems below it,
478a2afb611SJerry Jelinek  * are already correct, so we don't have to update this filesystem.
479a2afb611SJerry Jelinek  */
480a2afb611SJerry Jelinek static void
481a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
482a2afb611SJerry Jelinek {
483a2afb611SJerry Jelinek 	uint64_t my_fs_cnt = 0;
484a2afb611SJerry Jelinek 	uint64_t my_ss_cnt = 0;
485a2afb611SJerry Jelinek 	dsl_pool_t *dp = dd->dd_pool;
486a2afb611SJerry Jelinek 	objset_t *os = dp->dp_meta_objset;
487a2afb611SJerry Jelinek 	zap_cursor_t *zc;
488a2afb611SJerry Jelinek 	zap_attribute_t *za;
489a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
490a2afb611SJerry Jelinek 
491adf34077SJerry Jelinek 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
492a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dp));
493a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
494a2afb611SJerry Jelinek 
495a2afb611SJerry Jelinek 	dsl_dir_zapify(dd, tx);
496a2afb611SJerry Jelinek 
497a2afb611SJerry Jelinek 	/*
498a2afb611SJerry Jelinek 	 * If the filesystem count has already been initialized then we
499a2afb611SJerry Jelinek 	 * don't need to recurse down any further.
500a2afb611SJerry Jelinek 	 */
501a2afb611SJerry Jelinek 	if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
502a2afb611SJerry Jelinek 		return;
503a2afb611SJerry Jelinek 
504a2afb611SJerry Jelinek 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
505a2afb611SJerry Jelinek 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
506a2afb611SJerry Jelinek 
507a2afb611SJerry Jelinek 	/* Iterate my child dirs */
508*c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
509a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
510a2afb611SJerry Jelinek 		dsl_dir_t *chld_dd;
511a2afb611SJerry Jelinek 		uint64_t count;
512a2afb611SJerry Jelinek 
513a2afb611SJerry Jelinek 		VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
514a2afb611SJerry Jelinek 		    &chld_dd));
515a2afb611SJerry Jelinek 
516a2afb611SJerry Jelinek 		/*
517a2afb611SJerry Jelinek 		 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
518a2afb611SJerry Jelinek 		 * temporary datasets.
519a2afb611SJerry Jelinek 		 */
520a2afb611SJerry Jelinek 		if (chld_dd->dd_myname[0] == '$' ||
521a2afb611SJerry Jelinek 		    chld_dd->dd_myname[0] == '%') {
522a2afb611SJerry Jelinek 			dsl_dir_rele(chld_dd, FTAG);
523a2afb611SJerry Jelinek 			continue;
524a2afb611SJerry Jelinek 		}
525a2afb611SJerry Jelinek 
526a2afb611SJerry Jelinek 		my_fs_cnt++;	/* count this child */
527a2afb611SJerry Jelinek 
528a2afb611SJerry Jelinek 		dsl_dir_init_fs_ss_count(chld_dd, tx);
529a2afb611SJerry Jelinek 
530a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
531a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
532a2afb611SJerry Jelinek 		my_fs_cnt += count;
533a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
534a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
535a2afb611SJerry Jelinek 		my_ss_cnt += count;
536a2afb611SJerry Jelinek 
537a2afb611SJerry Jelinek 		dsl_dir_rele(chld_dd, FTAG);
538a2afb611SJerry Jelinek 	}
539a2afb611SJerry Jelinek 	zap_cursor_fini(zc);
540a2afb611SJerry Jelinek 	/* Count my snapshots (we counted children's snapshots above) */
541a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
542*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
543a2afb611SJerry Jelinek 
544*c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
545a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0;
546a2afb611SJerry Jelinek 	    zap_cursor_advance(zc)) {
547a2afb611SJerry Jelinek 		/* Don't count temporary snapshots */
548a2afb611SJerry Jelinek 		if (za->za_name[0] != '%')
549a2afb611SJerry Jelinek 			my_ss_cnt++;
550a2afb611SJerry Jelinek 	}
551fb7001f1SAlex Reece 	zap_cursor_fini(zc);
552a2afb611SJerry Jelinek 
553a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
554a2afb611SJerry Jelinek 
555a2afb611SJerry Jelinek 	kmem_free(zc, sizeof (zap_cursor_t));
556a2afb611SJerry Jelinek 	kmem_free(za, sizeof (zap_attribute_t));
557a2afb611SJerry Jelinek 
558a2afb611SJerry Jelinek 	/* we're in a sync task, update counts */
559a2afb611SJerry Jelinek 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
560a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
561a2afb611SJerry Jelinek 	    sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
562a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
563a2afb611SJerry Jelinek 	    sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
564a2afb611SJerry Jelinek }
565a2afb611SJerry Jelinek 
566a2afb611SJerry Jelinek static int
567a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
568a2afb611SJerry Jelinek {
569a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
570a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
571a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
572a2afb611SJerry Jelinek 	dsl_dir_t *dd;
573a2afb611SJerry Jelinek 	int error;
574a2afb611SJerry Jelinek 
575a2afb611SJerry Jelinek 	error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
576a2afb611SJerry Jelinek 	if (error != 0)
577a2afb611SJerry Jelinek 		return (error);
578a2afb611SJerry Jelinek 
579a2afb611SJerry Jelinek 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
580a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
581a2afb611SJerry Jelinek 		return (SET_ERROR(ENOTSUP));
582a2afb611SJerry Jelinek 	}
583a2afb611SJerry Jelinek 
584a2afb611SJerry Jelinek 	dd = ds->ds_dir;
585a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
586a2afb611SJerry Jelinek 	    dsl_dir_is_zapified(dd) &&
587a2afb611SJerry Jelinek 	    zap_contains(dp->dp_meta_objset, dd->dd_object,
588a2afb611SJerry Jelinek 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
589a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
590a2afb611SJerry Jelinek 		return (SET_ERROR(EALREADY));
591a2afb611SJerry Jelinek 	}
592a2afb611SJerry Jelinek 
593a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
594a2afb611SJerry Jelinek 	return (0);
595a2afb611SJerry Jelinek }
596a2afb611SJerry Jelinek 
597a2afb611SJerry Jelinek static void
598a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
599a2afb611SJerry Jelinek {
600a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
601a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
602a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
603a2afb611SJerry Jelinek 	spa_t *spa;
604a2afb611SJerry Jelinek 
605a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
606a2afb611SJerry Jelinek 
607a2afb611SJerry Jelinek 	spa = dsl_dataset_get_spa(ds);
608a2afb611SJerry Jelinek 
609a2afb611SJerry Jelinek 	if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
610a2afb611SJerry Jelinek 		/*
611a2afb611SJerry Jelinek 		 * Since the feature was not active and we're now setting a
612a2afb611SJerry Jelinek 		 * limit, increment the feature-active counter so that the
613a2afb611SJerry Jelinek 		 * feature becomes active for the first time.
614a2afb611SJerry Jelinek 		 *
615a2afb611SJerry Jelinek 		 * We are already in a sync task so we can update the MOS.
616a2afb611SJerry Jelinek 		 */
617a2afb611SJerry Jelinek 		spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
618a2afb611SJerry Jelinek 	}
619a2afb611SJerry Jelinek 
620a2afb611SJerry Jelinek 	/*
621a2afb611SJerry Jelinek 	 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
622a2afb611SJerry Jelinek 	 * we need to ensure the counts are correct. Descend down the tree from
623a2afb611SJerry Jelinek 	 * this point and update all of the counts to be accurate.
624a2afb611SJerry Jelinek 	 */
625a2afb611SJerry Jelinek 	dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
626a2afb611SJerry Jelinek 
627a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
628a2afb611SJerry Jelinek }
629a2afb611SJerry Jelinek 
630a2afb611SJerry Jelinek /*
631a2afb611SJerry Jelinek  * Make sure the feature is enabled and activate it if necessary.
632a2afb611SJerry Jelinek  * Since we're setting a limit, ensure the on-disk counts are valid.
633a2afb611SJerry Jelinek  * This is only called by the ioctl path when setting a limit value.
634a2afb611SJerry Jelinek  *
635a2afb611SJerry Jelinek  * We do not need to validate the new limit, since users who can change the
636a2afb611SJerry Jelinek  * limit are also allowed to exceed the limit.
637a2afb611SJerry Jelinek  */
638a2afb611SJerry Jelinek int
639a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname)
640a2afb611SJerry Jelinek {
641a2afb611SJerry Jelinek 	int error;
642a2afb611SJerry Jelinek 
643a2afb611SJerry Jelinek 	error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
6447d46dc6cSMatthew Ahrens 	    dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
6457d46dc6cSMatthew Ahrens 	    ZFS_SPACE_CHECK_RESERVED);
646a2afb611SJerry Jelinek 
647a2afb611SJerry Jelinek 	if (error == EALREADY)
648a2afb611SJerry Jelinek 		error = 0;
649a2afb611SJerry Jelinek 
650a2afb611SJerry Jelinek 	return (error);
651a2afb611SJerry Jelinek }
652a2afb611SJerry Jelinek 
653a2afb611SJerry Jelinek /*
654a2afb611SJerry Jelinek  * Used to determine if the filesystem_limit or snapshot_limit should be
655a2afb611SJerry Jelinek  * enforced. We allow the limit to be exceeded if the user has permission to
656a2afb611SJerry Jelinek  * write the property value. We pass in the creds that we got in the open
657a2afb611SJerry Jelinek  * context since we will always be the GZ root in syncing context. We also have
658a2afb611SJerry Jelinek  * to handle the case where we are allowed to change the limit on the current
659a2afb611SJerry Jelinek  * dataset, but there may be another limit in the tree above.
660a2afb611SJerry Jelinek  *
661a2afb611SJerry Jelinek  * We can never modify these two properties within a non-global zone. In
662a2afb611SJerry Jelinek  * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
663a2afb611SJerry Jelinek  * can't use that function since we are already holding the dp_config_rwlock.
664a2afb611SJerry Jelinek  * In addition, we already have the dd and dealing with snapshots is simplified
665a2afb611SJerry Jelinek  * in this code.
666a2afb611SJerry Jelinek  */
667a2afb611SJerry Jelinek 
668a2afb611SJerry Jelinek typedef enum {
669a2afb611SJerry Jelinek 	ENFORCE_ALWAYS,
670a2afb611SJerry Jelinek 	ENFORCE_NEVER,
671a2afb611SJerry Jelinek 	ENFORCE_ABOVE
672a2afb611SJerry Jelinek } enforce_res_t;
673a2afb611SJerry Jelinek 
674a2afb611SJerry Jelinek static enforce_res_t
675a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
676a2afb611SJerry Jelinek {
677a2afb611SJerry Jelinek 	enforce_res_t enforce = ENFORCE_ALWAYS;
678a2afb611SJerry Jelinek 	uint64_t obj;
679a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
680a2afb611SJerry Jelinek 	uint64_t zoned;
681a2afb611SJerry Jelinek 
682a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
683a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
684a2afb611SJerry Jelinek 
685a2afb611SJerry Jelinek #ifdef _KERNEL
686a2afb611SJerry Jelinek 	if (crgetzoneid(cr) != GLOBAL_ZONEID)
687a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
688a2afb611SJerry Jelinek 
689a2afb611SJerry Jelinek 	if (secpolicy_zfs(cr) == 0)
690a2afb611SJerry Jelinek 		return (ENFORCE_NEVER);
691a2afb611SJerry Jelinek #endif
692a2afb611SJerry Jelinek 
693*c1379625SJustin T. Gibbs 	if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
694a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
695a2afb611SJerry Jelinek 
696a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
697a2afb611SJerry Jelinek 
698a2afb611SJerry Jelinek 	if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
699a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
700a2afb611SJerry Jelinek 
701a2afb611SJerry Jelinek 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
702a2afb611SJerry Jelinek 		/* Only root can access zoned fs's from the GZ */
703a2afb611SJerry Jelinek 		enforce = ENFORCE_ALWAYS;
704a2afb611SJerry Jelinek 	} else {
705a2afb611SJerry Jelinek 		if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
706a2afb611SJerry Jelinek 			enforce = ENFORCE_ABOVE;
707a2afb611SJerry Jelinek 	}
708a2afb611SJerry Jelinek 
709a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
710a2afb611SJerry Jelinek 	return (enforce);
711a2afb611SJerry Jelinek }
712a2afb611SJerry Jelinek 
713a2afb611SJerry Jelinek /*
714a2afb611SJerry Jelinek  * Check if adding additional child filesystem(s) would exceed any filesystem
715a2afb611SJerry Jelinek  * limits or adding additional snapshot(s) would exceed any snapshot limits.
716a2afb611SJerry Jelinek  * The prop argument indicates which limit to check.
717a2afb611SJerry Jelinek  *
718a2afb611SJerry Jelinek  * Note that all filesystem limits up to the root (or the highest
719a2afb611SJerry Jelinek  * initialized) filesystem or the given ancestor must be satisfied.
720a2afb611SJerry Jelinek  */
721a2afb611SJerry Jelinek int
722a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
723a2afb611SJerry Jelinek     dsl_dir_t *ancestor, cred_t *cr)
724a2afb611SJerry Jelinek {
725a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
726a2afb611SJerry Jelinek 	uint64_t limit, count;
727a2afb611SJerry Jelinek 	char *count_prop;
728a2afb611SJerry Jelinek 	enforce_res_t enforce;
729a2afb611SJerry Jelinek 	int err = 0;
730a2afb611SJerry Jelinek 
731a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
732a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
733a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
734a2afb611SJerry Jelinek 
735a2afb611SJerry Jelinek 	/*
736a2afb611SJerry Jelinek 	 * If we're allowed to change the limit, don't enforce the limit
737a2afb611SJerry Jelinek 	 * e.g. this can happen if a snapshot is taken by an administrative
738a2afb611SJerry Jelinek 	 * user in the global zone (i.e. a recursive snapshot by root).
739a2afb611SJerry Jelinek 	 * However, we must handle the case of delegated permissions where we
740a2afb611SJerry Jelinek 	 * are allowed to change the limit on the current dataset, but there
741a2afb611SJerry Jelinek 	 * is another limit in the tree above.
742a2afb611SJerry Jelinek 	 */
743a2afb611SJerry Jelinek 	enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
744a2afb611SJerry Jelinek 	if (enforce == ENFORCE_NEVER)
745a2afb611SJerry Jelinek 		return (0);
746a2afb611SJerry Jelinek 
747a2afb611SJerry Jelinek 	/*
748a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment
749a2afb611SJerry Jelinek 	 * is 0.
750a2afb611SJerry Jelinek 	 */
751a2afb611SJerry Jelinek 	if (delta == 0)
752a2afb611SJerry Jelinek 		return (0);
753a2afb611SJerry Jelinek 
754a2afb611SJerry Jelinek 	if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
755a2afb611SJerry Jelinek 		/*
756a2afb611SJerry Jelinek 		 * We don't enforce the limit for temporary snapshots. This is
757a2afb611SJerry Jelinek 		 * indicated by a NULL cred_t argument.
758a2afb611SJerry Jelinek 		 */
759a2afb611SJerry Jelinek 		if (cr == NULL)
760a2afb611SJerry Jelinek 			return (0);
761a2afb611SJerry Jelinek 
762a2afb611SJerry Jelinek 		count_prop = DD_FIELD_SNAPSHOT_COUNT;
763a2afb611SJerry Jelinek 	} else {
764a2afb611SJerry Jelinek 		count_prop = DD_FIELD_FILESYSTEM_COUNT;
765a2afb611SJerry Jelinek 	}
766a2afb611SJerry Jelinek 
767a2afb611SJerry Jelinek 	/*
768a2afb611SJerry Jelinek 	 * If an ancestor has been provided, stop checking the limit once we
769a2afb611SJerry Jelinek 	 * hit that dir. We need this during rename so that we don't overcount
770a2afb611SJerry Jelinek 	 * the check once we recurse up to the common ancestor.
771a2afb611SJerry Jelinek 	 */
772a2afb611SJerry Jelinek 	if (ancestor == dd)
773a2afb611SJerry Jelinek 		return (0);
774a2afb611SJerry Jelinek 
775a2afb611SJerry Jelinek 	/*
776a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
777a2afb611SJerry Jelinek 	 * stop since we know there is no limit here (or above). The counts are
778a2afb611SJerry Jelinek 	 * not valid on this node and we know we won't touch this node's counts.
779a2afb611SJerry Jelinek 	 */
780a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
781a2afb611SJerry Jelinek 	    count_prop, sizeof (count), 1, &count) == ENOENT)
782a2afb611SJerry Jelinek 		return (0);
783a2afb611SJerry Jelinek 
784a2afb611SJerry Jelinek 	err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
785a2afb611SJerry Jelinek 	    B_FALSE);
786a2afb611SJerry Jelinek 	if (err != 0)
787a2afb611SJerry Jelinek 		return (err);
788a2afb611SJerry Jelinek 
789a2afb611SJerry Jelinek 	/* Is there a limit which we've hit? */
790a2afb611SJerry Jelinek 	if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
791a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
792a2afb611SJerry Jelinek 
793a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
794a2afb611SJerry Jelinek 		err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
795a2afb611SJerry Jelinek 		    ancestor, cr);
796a2afb611SJerry Jelinek 
797a2afb611SJerry Jelinek 	return (err);
798a2afb611SJerry Jelinek }
799a2afb611SJerry Jelinek 
800a2afb611SJerry Jelinek /*
801a2afb611SJerry Jelinek  * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
802a2afb611SJerry Jelinek  * parents. When a new filesystem/snapshot is created, increment the count on
803a2afb611SJerry Jelinek  * all parents, and when a filesystem/snapshot is destroyed, decrement the
804a2afb611SJerry Jelinek  * count.
805a2afb611SJerry Jelinek  */
806a2afb611SJerry Jelinek void
807a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
808a2afb611SJerry Jelinek     dmu_tx_t *tx)
809a2afb611SJerry Jelinek {
810a2afb611SJerry Jelinek 	int err;
811a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
812a2afb611SJerry Jelinek 	uint64_t count;
813a2afb611SJerry Jelinek 
814a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
815a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
816a2afb611SJerry Jelinek 	ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
817a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
818a2afb611SJerry Jelinek 
819a2afb611SJerry Jelinek 	/*
820a2afb611SJerry Jelinek 	 * When we receive an incremental stream into a filesystem that already
821a2afb611SJerry Jelinek 	 * exists, a temporary clone is created.  We don't count this temporary
822a2afb611SJerry Jelinek 	 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
823a2afb611SJerry Jelinek 	 * $MOS & $ORIGIN) objsets.
824a2afb611SJerry Jelinek 	 */
825a2afb611SJerry Jelinek 	if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
826a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
827a2afb611SJerry Jelinek 		return;
828a2afb611SJerry Jelinek 
829a2afb611SJerry Jelinek 	/*
830a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
831a2afb611SJerry Jelinek 	 */
832a2afb611SJerry Jelinek 	if (delta == 0)
833a2afb611SJerry Jelinek 		return;
834a2afb611SJerry Jelinek 
835a2afb611SJerry Jelinek 	/*
836a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
837a2afb611SJerry Jelinek 	 * stop since we know the counts are not valid on this node and we
838a2afb611SJerry Jelinek 	 * know we shouldn't touch this node's counts. An uninitialized count
839a2afb611SJerry Jelinek 	 * on the node indicates that either the feature has not yet been
840a2afb611SJerry Jelinek 	 * activated or there are no limits on this part of the tree.
841a2afb611SJerry Jelinek 	 */
842a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
843a2afb611SJerry Jelinek 	    prop, sizeof (count), 1, &count)) == ENOENT)
844a2afb611SJerry Jelinek 		return;
845a2afb611SJerry Jelinek 	VERIFY0(err);
846a2afb611SJerry Jelinek 
847a2afb611SJerry Jelinek 	count += delta;
848a2afb611SJerry Jelinek 	/* Use a signed verify to make sure we're not neg. */
849a2afb611SJerry Jelinek 	VERIFY3S(count, >=, 0);
850a2afb611SJerry Jelinek 
851a2afb611SJerry Jelinek 	VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
852a2afb611SJerry Jelinek 	    tx));
853a2afb611SJerry Jelinek 
854a2afb611SJerry Jelinek 	/* Roll up this additional count into our ancestors */
855a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
856a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
857a2afb611SJerry Jelinek }
858a2afb611SJerry Jelinek 
8591d452cf5Sahrens uint64_t
860088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
861088f3894Sahrens     dmu_tx_t *tx)
862fa9e4066Sahrens {
863088f3894Sahrens 	objset_t *mos = dp->dp_meta_objset;
864fa9e4066Sahrens 	uint64_t ddobj;
865cde58dbcSMatthew Ahrens 	dsl_dir_phys_t *ddphys;
866fa9e4066Sahrens 	dmu_buf_t *dbuf;
867fa9e4066Sahrens 
8681649cd4bStabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
8691649cd4bStabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
870088f3894Sahrens 	if (pds) {
871*c1379625SJustin T. Gibbs 		VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
872088f3894Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
873088f3894Sahrens 	} else {
874088f3894Sahrens 		/* it's the root dir */
875088f3894Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
876088f3894Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
877088f3894Sahrens 	}
878ea8dc4b6Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
879fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
880cde58dbcSMatthew Ahrens 	ddphys = dbuf->db_data;
881fa9e4066Sahrens 
882cde58dbcSMatthew Ahrens 	ddphys->dd_creation_time = gethrestime_sec();
883a2afb611SJerry Jelinek 	if (pds) {
884cde58dbcSMatthew Ahrens 		ddphys->dd_parent_obj = pds->dd_object;
885a2afb611SJerry Jelinek 
886a2afb611SJerry Jelinek 		/* update the filesystem counts */
887a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
888a2afb611SJerry Jelinek 	}
889cde58dbcSMatthew Ahrens 	ddphys->dd_props_zapobj = zap_create(mos,
890fa9e4066Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
891cde58dbcSMatthew Ahrens 	ddphys->dd_child_dir_zapobj = zap_create(mos,
89287e5029aSahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
89374e7dc98SMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
894cde58dbcSMatthew Ahrens 		ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
895ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
896fa9e4066Sahrens 
8971d452cf5Sahrens 	return (ddobj);
898fa9e4066Sahrens }
899fa9e4066Sahrens 
900088f3894Sahrens boolean_t
901088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
902fa9e4066Sahrens {
903*c1379625SJustin T. Gibbs 	return (dsl_dir_phys(dd)->dd_origin_obj &&
904088f3894Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
905*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_origin_obj !=
906088f3894Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
907fa9e4066Sahrens }
908fa9e4066Sahrens 
909fa9e4066Sahrens void
910a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
911fa9e4066Sahrens {
912fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
91374e7dc98SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
914*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_bytes);
915*c1379625SJustin T. Gibbs 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
916*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_quota);
917a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
918*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_reserved);
919a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
920*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
921*c1379625SJustin T. Gibbs 	    (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
922*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes));
92377372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
924*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes);
925*c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
92674e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
927*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
92874e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
929*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
93074e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
931*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
93274e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
933*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
934*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
93574e7dc98SMatthew Ahrens 	}
936fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
937fa9e4066Sahrens 
938a2afb611SJerry Jelinek 	if (dsl_dir_is_zapified(dd)) {
939a2afb611SJerry Jelinek 		uint64_t count;
940a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
941a2afb611SJerry Jelinek 
942a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
943a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
944a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
945a2afb611SJerry Jelinek 			    ZFS_PROP_FILESYSTEM_COUNT, count);
946a2afb611SJerry Jelinek 		}
947a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
948a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
949a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
950a2afb611SJerry Jelinek 			    ZFS_PROP_SNAPSHOT_COUNT, count);
951a2afb611SJerry Jelinek 		}
952a2afb611SJerry Jelinek 	}
953a2afb611SJerry Jelinek 
954088f3894Sahrens 	if (dsl_dir_is_clone(dd)) {
955fa9e4066Sahrens 		dsl_dataset_t *ds;
956a2eea2e1Sahrens 		char buf[MAXNAMELEN];
957fa9e4066Sahrens 
9583b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
959*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
960a2eea2e1Sahrens 		dsl_dataset_name(ds, buf);
961745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
962a2eea2e1Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
963fa9e4066Sahrens 	}
964fa9e4066Sahrens }
965fa9e4066Sahrens 
966fa9e4066Sahrens void
967fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
968fa9e4066Sahrens {
969fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
970fa9e4066Sahrens 
971*c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd));
972fa9e4066Sahrens 
9733b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
974fa9e4066Sahrens 		/* up the hold count until we can be written out */
975fa9e4066Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
976fa9e4066Sahrens 	}
977fa9e4066Sahrens }
978fa9e4066Sahrens 
979fa9e4066Sahrens static int64_t
980fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
981fa9e4066Sahrens {
982*c1379625SJustin T. Gibbs 	uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
983*c1379625SJustin T. Gibbs 	uint64_t new_accounted =
984*c1379625SJustin T. Gibbs 	    MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
985fa9e4066Sahrens 	return (new_accounted - old_accounted);
986fa9e4066Sahrens }
987fa9e4066Sahrens 
988fa9e4066Sahrens void
989fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
990fa9e4066Sahrens {
991fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
992fa9e4066Sahrens 
993fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
994fb09f5aaSMadhav Suresh 	ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
995fa9e4066Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
996fa9e4066Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
997fa9e4066Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
998fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
999fa9e4066Sahrens 
1000fa9e4066Sahrens 	/* release the hold from dsl_dir_dirty */
1001ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
1002fa9e4066Sahrens }
1003fa9e4066Sahrens 
1004fa9e4066Sahrens static uint64_t
1005a9799022Sck dsl_dir_space_towrite(dsl_dir_t *dd)
1006fa9e4066Sahrens {
1007a9799022Sck 	uint64_t space = 0;
1008fa9e4066Sahrens 	int i;
1009fa9e4066Sahrens 
1010fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
1011fa9e4066Sahrens 
1012fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1013fa9e4066Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
1014fa9e4066Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1015fa9e4066Sahrens 	}
1016fa9e4066Sahrens 	return (space);
1017fa9e4066Sahrens }
1018fa9e4066Sahrens 
1019fa9e4066Sahrens /*
1020fa9e4066Sahrens  * How much space would dd have available if ancestor had delta applied
1021fa9e4066Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
1022fa9e4066Sahrens  * on-disk, not estimated pending changes.
1023fa9e4066Sahrens  */
1024a2eea2e1Sahrens uint64_t
1025fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd,
1026fa9e4066Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1027fa9e4066Sahrens {
1028fa9e4066Sahrens 	uint64_t parentspace, myspace, quota, used;
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	/*
1031fa9e4066Sahrens 	 * If there are no restrictions otherwise, assume we have
1032fa9e4066Sahrens 	 * unlimited space available.
1033fa9e4066Sahrens 	 */
1034fa9e4066Sahrens 	quota = UINT64_MAX;
1035fa9e4066Sahrens 	parentspace = UINT64_MAX;
1036fa9e4066Sahrens 
1037fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1038fa9e4066Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
1039fa9e4066Sahrens 		    ancestor, delta, ondiskonly);
1040fa9e4066Sahrens 	}
1041fa9e4066Sahrens 
1042fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1043*c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_quota != 0)
1044*c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1045*c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1046a9799022Sck 	if (!ondiskonly)
1047a9799022Sck 		used += dsl_dir_space_towrite(dd);
1048fa9e4066Sahrens 
1049fa9e4066Sahrens 	if (dd->dd_parent == NULL) {
105099653d4eSeschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1051fa9e4066Sahrens 		quota = MIN(quota, poolsize);
1052fa9e4066Sahrens 	}
1053fa9e4066Sahrens 
1054*c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1055fa9e4066Sahrens 		/*
1056fa9e4066Sahrens 		 * We have some space reserved, in addition to what our
1057fa9e4066Sahrens 		 * parent gave us.
1058fa9e4066Sahrens 		 */
1059*c1379625SJustin T. Gibbs 		parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1060fa9e4066Sahrens 	}
1061fa9e4066Sahrens 
106274e7dc98SMatthew Ahrens 	if (dd == ancestor) {
106374e7dc98SMatthew Ahrens 		ASSERT(delta <= 0);
106474e7dc98SMatthew Ahrens 		ASSERT(used >= -delta);
106574e7dc98SMatthew Ahrens 		used += delta;
106674e7dc98SMatthew Ahrens 		if (parentspace != UINT64_MAX)
106774e7dc98SMatthew Ahrens 			parentspace -= delta;
106874e7dc98SMatthew Ahrens 	}
106974e7dc98SMatthew Ahrens 
1070fa9e4066Sahrens 	if (used > quota) {
1071fa9e4066Sahrens 		/* over quota */
1072fa9e4066Sahrens 		myspace = 0;
1073fa9e4066Sahrens 	} else {
1074fa9e4066Sahrens 		/*
107599653d4eSeschrock 		 * the lesser of the space provided by our parent and
107699653d4eSeschrock 		 * the space left in our quota
1077fa9e4066Sahrens 		 */
1078fa9e4066Sahrens 		myspace = MIN(parentspace, quota - used);
1079fa9e4066Sahrens 	}
1080fa9e4066Sahrens 
1081fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1082fa9e4066Sahrens 
1083fa9e4066Sahrens 	return (myspace);
1084fa9e4066Sahrens }
1085fa9e4066Sahrens 
1086fa9e4066Sahrens struct tempreserve {
1087fa9e4066Sahrens 	list_node_t tr_node;
1088fa9e4066Sahrens 	dsl_dir_t *tr_ds;
1089fa9e4066Sahrens 	uint64_t tr_size;
1090fa9e4066Sahrens };
1091fa9e4066Sahrens 
1092fa9e4066Sahrens static int
1093a9799022Sck dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1094a9799022Sck     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
10956df6c3bcSck     dmu_tx_t *tx, boolean_t first)
1096fa9e4066Sahrens {
1097fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
1098a9799022Sck 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1099468c413aSTim Haley 	uint64_t deferred = 0;
1100a9799022Sck 	struct tempreserve *tr;
1101468c413aSTim Haley 	int retval = EDQUOT;
1102fa9e4066Sahrens 	int txgidx = txg & TXG_MASK;
1103fa9e4066Sahrens 	int i;
11049082849eSck 	uint64_t ref_rsrv = 0;
1105fa9e4066Sahrens 
1106fa9e4066Sahrens 	ASSERT3U(txg, !=, 0);
1107a9799022Sck 	ASSERT3S(asize, >, 0);
1108fa9e4066Sahrens 
1109fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1110a9799022Sck 
1111fa9e4066Sahrens 	/*
1112fa9e4066Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
1113fa9e4066Sahrens 	 * when checking for over-quota because they get one free hit.
1114fa9e4066Sahrens 	 */
1115a9799022Sck 	est_inflight = dsl_dir_space_towrite(dd);
1116fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++)
1117a9799022Sck 		est_inflight += dd->dd_tempreserved[i];
1118*c1379625SJustin T. Gibbs 	used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1119fa9e4066Sahrens 
1120f4d2e9e6Smaybee 	/*
11216df6c3bcSck 	 * On the first iteration, fetch the dataset's used-on-disk and
11226df6c3bcSck 	 * refreservation values. Also, if checkrefquota is set, test if
11236df6c3bcSck 	 * allocating this space would exceed the dataset's refquota.
1124f4d2e9e6Smaybee 	 */
11256df6c3bcSck 	if (first && tx->tx_objset) {
1126c3fdb13aSmaybee 		int error;
1127503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
11289082849eSck 
1129a9799022Sck 		error = dsl_dataset_check_quota(ds, checkrefquota,
11309082849eSck 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
1131a9799022Sck 		if (error) {
1132a9799022Sck 			mutex_exit(&dd->dd_lock);
1133a9799022Sck 			return (error);
1134a9799022Sck 		}
1135a9799022Sck 	}
1136a9799022Sck 
1137a9799022Sck 	/*
1138a9799022Sck 	 * If this transaction will result in a net free of space,
1139a9799022Sck 	 * we want to let it through.
1140a9799022Sck 	 */
1141*c1379625SJustin T. Gibbs 	if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1142f4d2e9e6Smaybee 		quota = UINT64_MAX;
1143f4d2e9e6Smaybee 	else
1144*c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1145fa9e4066Sahrens 
1146fa9e4066Sahrens 	/*
1147468c413aSTim Haley 	 * Adjust the quota against the actual pool size at the root
1148468c413aSTim Haley 	 * minus any outstanding deferred frees.
1149f4d2e9e6Smaybee 	 * To ensure that it's possible to remove files from a full
1150f4d2e9e6Smaybee 	 * pool without inducing transient overcommits, we throttle
1151fa9e4066Sahrens 	 * netfree transactions against a quota that is slightly larger,
1152fa9e4066Sahrens 	 * but still within the pool's allocation slop.  In cases where
1153fa9e4066Sahrens 	 * we're very close to full, this will allow a steady trickle of
1154fa9e4066Sahrens 	 * removes to get through.
1155fa9e4066Sahrens 	 */
11561934e92fSmaybee 	if (dd->dd_parent == NULL) {
1157b24ab676SJeff Bonwick 		spa_t *spa = dd->dd_pool->dp_spa;
1158fa9e4066Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1159b24ab676SJeff Bonwick 		deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1160468c413aSTim Haley 		if (poolsize - deferred < quota) {
1161468c413aSTim Haley 			quota = poolsize - deferred;
1162468c413aSTim Haley 			retval = ENOSPC;
1163fa9e4066Sahrens 		}
1164fa9e4066Sahrens 	}
1165fa9e4066Sahrens 
1166fa9e4066Sahrens 	/*
1167fa9e4066Sahrens 	 * If they are requesting more space, and our current estimate
1168a9799022Sck 	 * is over quota, they get to try again unless the actual
1169ea8dc4b6Seschrock 	 * on-disk is over quota and there are no pending changes (which
1170ea8dc4b6Seschrock 	 * may free up space for us).
1171fa9e4066Sahrens 	 */
1172468c413aSTim Haley 	if (used_on_disk + est_inflight >= quota) {
1173468c413aSTim Haley 		if (est_inflight > 0 || used_on_disk < quota ||
1174468c413aSTim Haley 		    (retval == ENOSPC && used_on_disk < quota + deferred))
1175468c413aSTim Haley 			retval = ERESTART;
1176a9799022Sck 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1177fa9e4066Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
1178a9799022Sck 		    used_on_disk>>10, est_inflight>>10,
1179468c413aSTim Haley 		    quota>>10, asize>>10, retval);
1180fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
1181be6fd75aSMatthew Ahrens 		return (SET_ERROR(retval));
1182fa9e4066Sahrens 	}
1183fa9e4066Sahrens 
1184fa9e4066Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
1185fa9e4066Sahrens 	dd->dd_tempreserved[txgidx] += asize;
1186fa9e4066Sahrens 
11879082849eSck 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
11889082849eSck 	    asize - ref_rsrv);
1189fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1190fa9e4066Sahrens 
11911ab7f2deSmaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1192fa9e4066Sahrens 	tr->tr_ds = dd;
1193fa9e4066Sahrens 	tr->tr_size = asize;
1194fa9e4066Sahrens 	list_insert_tail(tr_list, tr);
1195fa9e4066Sahrens 
1196fa9e4066Sahrens 	/* see if it's OK with our parent */
11971934e92fSmaybee 	if (dd->dd_parent && parent_rsrv) {
1198*c1379625SJustin T. Gibbs 		boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
11991934e92fSmaybee 
1200fa9e4066Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
12016df6c3bcSck 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1202fa9e4066Sahrens 	} else {
1203fa9e4066Sahrens 		return (0);
1204fa9e4066Sahrens 	}
1205fa9e4066Sahrens }
1206fa9e4066Sahrens 
1207fa9e4066Sahrens /*
1208fa9e4066Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
1209a9799022Sck  * After the space has been dirtied (and dsl_dir_willuse_space()
1210a9799022Sck  * has been called), the reservation should be canceled, using
1211a9799022Sck  * dsl_dir_tempreserve_clear().
1212fa9e4066Sahrens  */
1213fa9e4066Sahrens int
1214a9799022Sck dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1215a9799022Sck     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1216fa9e4066Sahrens {
12171ab7f2deSmaybee 	int err;
1218fa9e4066Sahrens 	list_t *tr_list;
1219fa9e4066Sahrens 
1220a9799022Sck 	if (asize == 0) {
1221a9799022Sck 		*tr_cookiep = NULL;
1222a9799022Sck 		return (0);
1223a9799022Sck 	}
1224a9799022Sck 
1225fa9e4066Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1226fa9e4066Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
1227fa9e4066Sahrens 	    offsetof(struct tempreserve, tr_node));
1228a9799022Sck 	ASSERT3S(asize, >, 0);
1229ea8dc4b6Seschrock 	ASSERT3S(fsize, >=, 0);
1230fa9e4066Sahrens 
12311ab7f2deSmaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
1232fa9e4066Sahrens 	if (err == 0) {
1233fa9e4066Sahrens 		struct tempreserve *tr;
1234fa9e4066Sahrens 
12351ab7f2deSmaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
12361ab7f2deSmaybee 		tr->tr_size = lsize;
12371ab7f2deSmaybee 		list_insert_tail(tr_list, tr);
12381ab7f2deSmaybee 	} else {
12391ab7f2deSmaybee 		if (err == EAGAIN) {
124069962b56SMatthew Ahrens 			/*
124169962b56SMatthew Ahrens 			 * If arc_memory_throttle() detected that pageout
124269962b56SMatthew Ahrens 			 * is running and we are low on memory, we delay new
124369962b56SMatthew Ahrens 			 * non-pageout transactions to give pageout an
124469962b56SMatthew Ahrens 			 * advantage.
124569962b56SMatthew Ahrens 			 *
124669962b56SMatthew Ahrens 			 * It is unfortunate to be delaying while the caller's
124769962b56SMatthew Ahrens 			 * locks are held.
124869962b56SMatthew Ahrens 			 */
12490689f76cSAdam Leventhal 			txg_delay(dd->dd_pool, tx->tx_txg,
12500689f76cSAdam Leventhal 			    MSEC2NSEC(10), MSEC2NSEC(10));
1251be6fd75aSMatthew Ahrens 			err = SET_ERROR(ERESTART);
1252fa9e4066Sahrens 		}
12531ab7f2deSmaybee 	}
12541ab7f2deSmaybee 
12551ab7f2deSmaybee 	if (err == 0) {
12561ab7f2deSmaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
12576df6c3bcSck 		    FALSE, asize > usize, tr_list, tx, TRUE);
1258fa9e4066Sahrens 	}
1259fa9e4066Sahrens 
12603b2aab18SMatthew Ahrens 	if (err != 0)
1261fa9e4066Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
1262fa9e4066Sahrens 	else
1263fa9e4066Sahrens 		*tr_cookiep = tr_list;
12641ab7f2deSmaybee 
1265fa9e4066Sahrens 	return (err);
1266fa9e4066Sahrens }
1267fa9e4066Sahrens 
1268fa9e4066Sahrens /*
1269fa9e4066Sahrens  * Clear a temporary reservation that we previously made with
1270fa9e4066Sahrens  * dsl_dir_tempreserve_space().
1271fa9e4066Sahrens  */
1272fa9e4066Sahrens void
1273fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1274fa9e4066Sahrens {
1275fa9e4066Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
1276fa9e4066Sahrens 	list_t *tr_list = tr_cookie;
1277fa9e4066Sahrens 	struct tempreserve *tr;
1278fa9e4066Sahrens 
1279fa9e4066Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
1280fa9e4066Sahrens 
1281a9799022Sck 	if (tr_cookie == NULL)
1282a9799022Sck 		return;
1283a9799022Sck 
128469962b56SMatthew Ahrens 	while ((tr = list_head(tr_list)) != NULL) {
128569962b56SMatthew Ahrens 		if (tr->tr_ds) {
1286fa9e4066Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
1287fa9e4066Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1288fa9e4066Sahrens 			    tr->tr_size);
1289fa9e4066Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1290fa9e4066Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
12911ab7f2deSmaybee 		} else {
12921ab7f2deSmaybee 			arc_tempreserve_clear(tr->tr_size);
1293fa9e4066Sahrens 		}
1294fa9e4066Sahrens 		list_remove(tr_list, tr);
1295fa9e4066Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
1296fa9e4066Sahrens 	}
1297fa9e4066Sahrens 
1298fa9e4066Sahrens 	kmem_free(tr_list, sizeof (list_t));
1299fa9e4066Sahrens }
1300fa9e4066Sahrens 
130169962b56SMatthew Ahrens /*
130269962b56SMatthew Ahrens  * This should be called from open context when we think we're going to write
130369962b56SMatthew Ahrens  * or free space, for example when dirtying data. Be conservative; it's okay
130469962b56SMatthew Ahrens  * to write less space or free more, but we don't want to write more or free
130569962b56SMatthew Ahrens  * less than the amount specified.
130669962b56SMatthew Ahrens  */
130769962b56SMatthew Ahrens void
130869962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1309fa9e4066Sahrens {
1310fa9e4066Sahrens 	int64_t parent_space;
1311fa9e4066Sahrens 	uint64_t est_used;
1312fa9e4066Sahrens 
1313fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1314fa9e4066Sahrens 	if (space > 0)
1315fa9e4066Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1316fa9e4066Sahrens 
1317*c1379625SJustin T. Gibbs 	est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1318fa9e4066Sahrens 	parent_space = parent_delta(dd, est_used, space);
1319fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1320fa9e4066Sahrens 
1321fa9e4066Sahrens 	/* Make sure that we clean up dd_space_to* */
1322fa9e4066Sahrens 	dsl_dir_dirty(dd, tx);
1323fa9e4066Sahrens 
1324fa9e4066Sahrens 	/* XXX this is potentially expensive and unnecessary... */
1325fa9e4066Sahrens 	if (parent_space && dd->dd_parent)
132669962b56SMatthew Ahrens 		dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1327fa9e4066Sahrens }
1328fa9e4066Sahrens 
1329fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */
1330fa9e4066Sahrens void
133174e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1332fa9e4066Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1333fa9e4066Sahrens {
1334fa9e4066Sahrens 	int64_t accounted_delta;
1335b62969f8SMatthew Ahrens 
1336b62969f8SMatthew Ahrens 	/*
1337b62969f8SMatthew Ahrens 	 * dsl_dataset_set_refreservation_sync_impl() calls this with
1338b62969f8SMatthew Ahrens 	 * dd_lock held, so that it can atomically update
1339b62969f8SMatthew Ahrens 	 * ds->ds_reserved and the dsl_dir accounting, so that
1340b62969f8SMatthew Ahrens 	 * dsl_dataset_check_quota() can see dataset and dir accounting
1341b62969f8SMatthew Ahrens 	 * consistently.
1342b62969f8SMatthew Ahrens 	 */
134302c8f3f0SMatthew Ahrens 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1344fa9e4066Sahrens 
1345fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
134674e7dc98SMatthew Ahrens 	ASSERT(type < DD_USED_NUM);
1347fa9e4066Sahrens 
1348b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1349b62969f8SMatthew Ahrens 
135002c8f3f0SMatthew Ahrens 	if (needlock)
135102c8f3f0SMatthew Ahrens 		mutex_enter(&dd->dd_lock);
1352*c1379625SJustin T. Gibbs 	accounted_delta =
1353*c1379625SJustin T. Gibbs 	    parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1354*c1379625SJustin T. Gibbs 	ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1355fa9e4066Sahrens 	ASSERT(compressed >= 0 ||
1356*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1357fa9e4066Sahrens 	ASSERT(uncompressed >= 0 ||
1358*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1359*c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_bytes += used;
1360*c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1361*c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
136274e7dc98SMatthew Ahrens 
1363*c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
136474e7dc98SMatthew Ahrens 		ASSERT(used > 0 ||
1365*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1366*c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
136774e7dc98SMatthew Ahrens #ifdef DEBUG
136874e7dc98SMatthew Ahrens 		dd_used_t t;
136974e7dc98SMatthew Ahrens 		uint64_t u = 0;
137074e7dc98SMatthew Ahrens 		for (t = 0; t < DD_USED_NUM; t++)
1371*c1379625SJustin T. Gibbs 			u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1372*c1379625SJustin T. Gibbs 		ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
137374e7dc98SMatthew Ahrens #endif
137474e7dc98SMatthew Ahrens 	}
137502c8f3f0SMatthew Ahrens 	if (needlock)
137602c8f3f0SMatthew Ahrens 		mutex_exit(&dd->dd_lock);
1377fa9e4066Sahrens 
1378fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
137974e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1380fa9e4066Sahrens 		    accounted_delta, compressed, uncompressed, tx);
138174e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(dd->dd_parent,
138274e7dc98SMatthew Ahrens 		    used - accounted_delta,
138374e7dc98SMatthew Ahrens 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1384fa9e4066Sahrens 	}
1385fa9e4066Sahrens }
1386fa9e4066Sahrens 
138774e7dc98SMatthew Ahrens void
138874e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
138974e7dc98SMatthew Ahrens     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
139074e7dc98SMatthew Ahrens {
139174e7dc98SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
139274e7dc98SMatthew Ahrens 	ASSERT(oldtype < DD_USED_NUM);
139374e7dc98SMatthew Ahrens 	ASSERT(newtype < DD_USED_NUM);
139474e7dc98SMatthew Ahrens 
1395*c1379625SJustin T. Gibbs 	if (delta == 0 ||
1396*c1379625SJustin T. Gibbs 	    !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
139774e7dc98SMatthew Ahrens 		return;
139874e7dc98SMatthew Ahrens 
1399b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1400b62969f8SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
140174e7dc98SMatthew Ahrens 	ASSERT(delta > 0 ?
1402*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1403*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1404*c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1405*c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1406*c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1407b62969f8SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
140874e7dc98SMatthew Ahrens }
140974e7dc98SMatthew Ahrens 
14103b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg {
14113b2aab18SMatthew Ahrens 	const char *ddsqra_name;
14123b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
14133b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
14143b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t;
14153b2aab18SMatthew Ahrens 
1416fa9e4066Sahrens static int
14173b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1418fa9e4066Sahrens {
14193b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14203b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14213b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14223b2aab18SMatthew Ahrens 	int error;
14233b2aab18SMatthew Ahrens 	uint64_t towrite, newval;
1424fa9e4066Sahrens 
14253b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
14263b2aab18SMatthew Ahrens 	if (error != 0)
14273b2aab18SMatthew Ahrens 		return (error);
14283b2aab18SMatthew Ahrens 
14293b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir, "quota",
14303b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
14313b2aab18SMatthew Ahrens 	if (error != 0) {
14323b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14333b2aab18SMatthew Ahrens 		return (error);
14343b2aab18SMatthew Ahrens 	}
143592241e0bSTom Erickson 
14363b2aab18SMatthew Ahrens 	if (newval == 0) {
14373b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14381d452cf5Sahrens 		return (0);
14393b2aab18SMatthew Ahrens 	}
1440fa9e4066Sahrens 
14413b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
14421d452cf5Sahrens 	/*
14431d452cf5Sahrens 	 * If we are doing the preliminary check in open context, and
14441d452cf5Sahrens 	 * there are pending changes, then don't fail it, since the
1445a9799022Sck 	 * pending changes could under-estimate the amount of space to be
14461d452cf5Sahrens 	 * freed up.
14471d452cf5Sahrens 	 */
14483b2aab18SMatthew Ahrens 	towrite = dsl_dir_space_towrite(ds->ds_dir);
14491d452cf5Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1450*c1379625SJustin T. Gibbs 	    (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1451*c1379625SJustin T. Gibbs 	    newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1452be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOSPC);
1453fa9e4066Sahrens 	}
14543b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
14553b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14563b2aab18SMatthew Ahrens 	return (error);
1457fa9e4066Sahrens }
1458fa9e4066Sahrens 
14591d452cf5Sahrens static void
14603b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
14611d452cf5Sahrens {
14623b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14633b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14643b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14653b2aab18SMatthew Ahrens 	uint64_t newval;
146692241e0bSTom Erickson 
14673b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
14681d452cf5Sahrens 
1469013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1470013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1471013023d4SMartin Matuska 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1472013023d4SMartin Matuska 		    &ddsqra->ddsqra_value, tx);
14731d452cf5Sahrens 
1474013023d4SMartin Matuska 		VERIFY0(dsl_prop_get_int_ds(ds,
1475013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1476013023d4SMartin Matuska 	} else {
1477013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1478013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1479013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1480013023d4SMartin Matuska 	}
14813b2aab18SMatthew Ahrens 
14823b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
14833b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
1484*c1379625SJustin T. Gibbs 	dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
14853b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
14863b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14871d452cf5Sahrens }
14881d452cf5Sahrens 
1489fa9e4066Sahrens int
149092241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1491fa9e4066Sahrens {
14923b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
1493fa9e4066Sahrens 
14943b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
14953b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
14963b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = quota;
149792241e0bSTom Erickson 
14983b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
14997d46dc6cSMatthew Ahrens 	    dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1500fa9e4066Sahrens }
1501fa9e4066Sahrens 
1502a9799022Sck int
15033b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1504fa9e4066Sahrens {
15053b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
15063b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15073b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15083b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15093b2aab18SMatthew Ahrens 	uint64_t newval, used, avail;
15103b2aab18SMatthew Ahrens 	int error;
151192241e0bSTom Erickson 
15123b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
15133b2aab18SMatthew Ahrens 	if (error != 0)
15143b2aab18SMatthew Ahrens 		return (error);
15153b2aab18SMatthew Ahrens 	dd = ds->ds_dir;
1516fa9e4066Sahrens 
15171d452cf5Sahrens 	/*
15181d452cf5Sahrens 	 * If we are doing the preliminary check in open context, the
15191d452cf5Sahrens 	 * space estimates may be inaccurate.
15201d452cf5Sahrens 	 */
15213b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
15223b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15231d452cf5Sahrens 		return (0);
15243b2aab18SMatthew Ahrens 	}
15253b2aab18SMatthew Ahrens 
15263b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
15273b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_RESERVATION),
15283b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
15293b2aab18SMatthew Ahrens 	if (error != 0) {
15303b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15313b2aab18SMatthew Ahrens 		return (error);
15323b2aab18SMatthew Ahrens 	}
15331d452cf5Sahrens 
1534fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1535*c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1536fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1537fa9e4066Sahrens 
1538fa9e4066Sahrens 	if (dd->dd_parent) {
1539fa9e4066Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1540fa9e4066Sahrens 		    NULL, 0, FALSE);
1541fa9e4066Sahrens 	} else {
1542fa9e4066Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1543fa9e4066Sahrens 	}
1544fa9e4066Sahrens 
1545*c1379625SJustin T. Gibbs 	if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
15463b2aab18SMatthew Ahrens 		uint64_t delta = MAX(used, newval) -
1547*c1379625SJustin T. Gibbs 		    MAX(used, dsl_dir_phys(dd)->dd_reserved);
1548379c004dSEric Schrock 
15493b2aab18SMatthew Ahrens 		if (delta > avail ||
1550*c1379625SJustin T. Gibbs 		    (dsl_dir_phys(dd)->dd_quota > 0 &&
1551*c1379625SJustin T. Gibbs 		    newval > dsl_dir_phys(dd)->dd_quota))
1552be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENOSPC);
1553379c004dSEric Schrock 	}
1554379c004dSEric Schrock 
15553b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15563b2aab18SMatthew Ahrens 	return (error);
15571d452cf5Sahrens }
15581d452cf5Sahrens 
15593b2aab18SMatthew Ahrens void
15604445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
15611d452cf5Sahrens {
15621d452cf5Sahrens 	uint64_t used;
15631d452cf5Sahrens 	int64_t delta;
15641d452cf5Sahrens 
1565a9799022Sck 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1566a9799022Sck 
15671d452cf5Sahrens 	mutex_enter(&dd->dd_lock);
1568*c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1569*c1379625SJustin T. Gibbs 	delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1570*c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_reserved = value;
1571fa9e4066Sahrens 
1572fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1573fa9e4066Sahrens 		/* Roll up this additional usage into our ancestors */
157474e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
157574e7dc98SMatthew Ahrens 		    delta, 0, 0, tx);
1576fa9e4066Sahrens 	}
157702c8f3f0SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
15784445fffbSMatthew Ahrens }
15794445fffbSMatthew Ahrens 
15804445fffbSMatthew Ahrens 
15814445fffbSMatthew Ahrens static void
15823b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
15834445fffbSMatthew Ahrens {
15843b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
15853b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15863b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15873b2aab18SMatthew Ahrens 	uint64_t newval;
15883b2aab18SMatthew Ahrens 
15893b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
15903b2aab18SMatthew Ahrens 
1591013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1592013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds,
1593013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1594013023d4SMartin Matuska 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1595013023d4SMartin Matuska 		    &ddsqra->ddsqra_value, tx);
15964445fffbSMatthew Ahrens 
1597013023d4SMartin Matuska 		VERIFY0(dsl_prop_get_int_ds(ds,
1598013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1599013023d4SMartin Matuska 	} else {
1600013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1601013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1602013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1603013023d4SMartin Matuska 		    (longlong_t)newval);
1604013023d4SMartin Matuska 	}
1605ecd6cf80Smarks 
16063b2aab18SMatthew Ahrens 	dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
16073b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1608fa9e4066Sahrens }
1609fa9e4066Sahrens 
1610fa9e4066Sahrens int
161192241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
161292241e0bSTom Erickson     uint64_t reservation)
1613fa9e4066Sahrens {
16143b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
161592241e0bSTom Erickson 
16163b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
16173b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
16183b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = reservation;
161992241e0bSTom Erickson 
16203b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
16217d46dc6cSMatthew Ahrens 	    dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1622fa9e4066Sahrens }
1623fa9e4066Sahrens 
1624fa9e4066Sahrens static dsl_dir_t *
1625fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1626fa9e4066Sahrens {
1627fa9e4066Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1628fa9e4066Sahrens 		dsl_dir_t *dd;
1629fa9e4066Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1630fa9e4066Sahrens 			if (ds1 == dd)
1631fa9e4066Sahrens 				return (dd);
1632fa9e4066Sahrens 		}
1633fa9e4066Sahrens 	}
1634fa9e4066Sahrens 	return (NULL);
1635fa9e4066Sahrens }
1636fa9e4066Sahrens 
1637fa9e4066Sahrens /*
1638fa9e4066Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1639fa9e4066Sahrens  * ancestor?  Syncing context only.
1640fa9e4066Sahrens  */
1641fa9e4066Sahrens static int64_t
1642fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1643fa9e4066Sahrens {
1644fa9e4066Sahrens 	if (dd == ancestor)
1645fa9e4066Sahrens 		return (delta);
1646fa9e4066Sahrens 
1647fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1648*c1379625SJustin T. Gibbs 	delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1649fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1650fa9e4066Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1651fa9e4066Sahrens }
1652fa9e4066Sahrens 
16533b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg {
16543b2aab18SMatthew Ahrens 	const char *ddra_oldname;
16553b2aab18SMatthew Ahrens 	const char *ddra_newname;
1656a2afb611SJerry Jelinek 	cred_t *ddra_cred;
16573b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t;
16581d452cf5Sahrens 
16593b2aab18SMatthew Ahrens /* ARGSUSED */
16601d452cf5Sahrens static int
16613b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1662fa9e4066Sahrens {
16633b2aab18SMatthew Ahrens 	int *deltap = arg;
16643b2aab18SMatthew Ahrens 	char namebuf[MAXNAMELEN];
1665fa9e4066Sahrens 
16663b2aab18SMatthew Ahrens 	dsl_dataset_name(ds, namebuf);
16673b2aab18SMatthew Ahrens 
16683b2aab18SMatthew Ahrens 	if (strlen(namebuf) + *deltap >= MAXNAMELEN)
1669be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
16703b2aab18SMatthew Ahrens 	return (0);
16713b2aab18SMatthew Ahrens }
16723b2aab18SMatthew Ahrens 
16733b2aab18SMatthew Ahrens static int
16743b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
16753b2aab18SMatthew Ahrens {
16763b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
16773b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16783b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
16793b2aab18SMatthew Ahrens 	const char *mynewname;
16803b2aab18SMatthew Ahrens 	int error;
16813b2aab18SMatthew Ahrens 	int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1682fa9e4066Sahrens 
16833b2aab18SMatthew Ahrens 	/* target dir should exist */
16843b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
16853b2aab18SMatthew Ahrens 	if (error != 0)
16863b2aab18SMatthew Ahrens 		return (error);
16873b2aab18SMatthew Ahrens 
16883b2aab18SMatthew Ahrens 	/* new parent should exist */
16893b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
16903b2aab18SMatthew Ahrens 	    &newparent, &mynewname);
16913b2aab18SMatthew Ahrens 	if (error != 0) {
16923b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
16933b2aab18SMatthew Ahrens 		return (error);
16943b2aab18SMatthew Ahrens 	}
16953b2aab18SMatthew Ahrens 
16963b2aab18SMatthew Ahrens 	/* can't rename to different pool */
16973b2aab18SMatthew Ahrens 	if (dd->dd_pool != newparent->dd_pool) {
16983b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
16993b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1700be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
17013b2aab18SMatthew Ahrens 	}
17023b2aab18SMatthew Ahrens 
17033b2aab18SMatthew Ahrens 	/* new name should not already exist */
17043b2aab18SMatthew Ahrens 	if (mynewname == NULL) {
17053b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17063b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1707be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
17083b2aab18SMatthew Ahrens 	}
17093b2aab18SMatthew Ahrens 
17103b2aab18SMatthew Ahrens 	/* if the name length is growing, validate child name lengths */
17113b2aab18SMatthew Ahrens 	if (delta > 0) {
17123b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
17133b2aab18SMatthew Ahrens 		    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
17143b2aab18SMatthew Ahrens 		if (error != 0) {
17153b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17163b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17173b2aab18SMatthew Ahrens 			return (error);
17183b2aab18SMatthew Ahrens 		}
17193b2aab18SMatthew Ahrens 	}
17201d452cf5Sahrens 
1721a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1722adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1723a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1724a2afb611SJerry Jelinek 			/*
1725a2afb611SJerry Jelinek 			 * Although this is the check function and we don't
1726a2afb611SJerry Jelinek 			 * normally make on-disk changes in check functions,
1727a2afb611SJerry Jelinek 			 * we need to do that here.
1728a2afb611SJerry Jelinek 			 *
1729a2afb611SJerry Jelinek 			 * Ensure this portion of the tree's counts have been
1730a2afb611SJerry Jelinek 			 * initialized in case the new parent has limits set.
1731a2afb611SJerry Jelinek 			 */
1732a2afb611SJerry Jelinek 			dsl_dir_init_fs_ss_count(dd, tx);
1733a2afb611SJerry Jelinek 		}
1734a2afb611SJerry Jelinek 	}
1735a2afb611SJerry Jelinek 
17363b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
173799653d4eSeschrock 		/* is there enough space? */
173899653d4eSeschrock 		uint64_t myspace =
1739*c1379625SJustin T. Gibbs 		    MAX(dsl_dir_phys(dd)->dd_used_bytes,
1740*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_reserved);
1741a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1742a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1743a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1744a2afb611SJerry Jelinek 
1745a2afb611SJerry Jelinek 		if (dsl_dir_is_zapified(dd)) {
1746a2afb611SJerry Jelinek 			int err;
1747a2afb611SJerry Jelinek 
1748a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1749a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1750a2afb611SJerry Jelinek 			    &fs_cnt);
1751adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1752adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1753adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1754a2afb611SJerry Jelinek 				return (err);
1755adf34077SJerry Jelinek 			}
1756a2afb611SJerry Jelinek 
1757a2afb611SJerry Jelinek 			/*
1758a2afb611SJerry Jelinek 			 * have to add 1 for the filesystem itself that we're
1759a2afb611SJerry Jelinek 			 * moving
1760a2afb611SJerry Jelinek 			 */
1761a2afb611SJerry Jelinek 			fs_cnt++;
1762a2afb611SJerry Jelinek 
1763a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1764a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1765a2afb611SJerry Jelinek 			    &ss_cnt);
1766adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1767adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1768adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1769a2afb611SJerry Jelinek 				return (err);
1770adf34077SJerry Jelinek 			}
1771a2afb611SJerry Jelinek 		}
1772fa9e4066Sahrens 
17731d452cf5Sahrens 		/* no rename into our descendant */
17743b2aab18SMatthew Ahrens 		if (closest_common_ancestor(dd, newparent) == dd) {
17753b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17763b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
1777be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
17783b2aab18SMatthew Ahrens 		}
1779fa9e4066Sahrens 
17803b2aab18SMatthew Ahrens 		error = dsl_dir_transfer_possible(dd->dd_parent,
1781a2afb611SJerry Jelinek 		    newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
17823b2aab18SMatthew Ahrens 		if (error != 0) {
17833b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17843b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17853b2aab18SMatthew Ahrens 			return (error);
17863b2aab18SMatthew Ahrens 		}
17871d452cf5Sahrens 	}
1788fa9e4066Sahrens 
17893b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
17903b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
17911d452cf5Sahrens 	return (0);
17921d452cf5Sahrens }
17931d452cf5Sahrens 
17941d452cf5Sahrens static void
17953b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
17961d452cf5Sahrens {
17973b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
17983b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17993b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
18003b2aab18SMatthew Ahrens 	const char *mynewname;
18013b2aab18SMatthew Ahrens 	int error;
18021d452cf5Sahrens 	objset_t *mos = dp->dp_meta_objset;
18031d452cf5Sahrens 
18043b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
18053b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
18063b2aab18SMatthew Ahrens 	    &mynewname));
18071d452cf5Sahrens 
18084445fffbSMatthew Ahrens 	/* Log this before we change the name. */
18094445fffbSMatthew Ahrens 	spa_history_log_internal_dd(dd, "rename", tx,
18103b2aab18SMatthew Ahrens 	    "-> %s", ddra->ddra_newname);
18114445fffbSMatthew Ahrens 
18123b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
1813a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1814a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1815a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1816a2afb611SJerry Jelinek 
1817a2afb611SJerry Jelinek 		/*
1818a2afb611SJerry Jelinek 		 * We already made sure the dd counts were initialized in the
1819a2afb611SJerry Jelinek 		 * check function.
1820a2afb611SJerry Jelinek 		 */
1821adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1822a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1823a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1824a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1825a2afb611SJerry Jelinek 			    &fs_cnt));
1826a2afb611SJerry Jelinek 			/* add 1 for the filesystem itself that we're moving */
1827a2afb611SJerry Jelinek 			fs_cnt++;
1828a2afb611SJerry Jelinek 
1829a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1830a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1831a2afb611SJerry Jelinek 			    &ss_cnt));
1832a2afb611SJerry Jelinek 		}
1833a2afb611SJerry Jelinek 
1834a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1835a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1836a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, fs_cnt,
1837a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1838a2afb611SJerry Jelinek 
1839a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1840a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1841a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, ss_cnt,
1842a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1843a2afb611SJerry Jelinek 
184474e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1845*c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_used_bytes,
1846*c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_compressed_bytes,
1847*c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
18483b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1849*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes,
1850*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_compressed_bytes,
1851*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
185274e7dc98SMatthew Ahrens 
1853*c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_reserved >
1854*c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes) {
1855*c1379625SJustin T. Gibbs 			uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1856*c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_used_bytes;
185774e7dc98SMatthew Ahrens 
185874e7dc98SMatthew Ahrens 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
185974e7dc98SMatthew Ahrens 			    -unused_rsrv, 0, 0, tx);
18603b2aab18SMatthew Ahrens 			dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
186174e7dc98SMatthew Ahrens 			    unused_rsrv, 0, 0, tx);
186274e7dc98SMatthew Ahrens 		}
1863fa9e4066Sahrens 	}
1864fa9e4066Sahrens 
1865fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1866fa9e4066Sahrens 
1867fa9e4066Sahrens 	/* remove from old parent zapobj */
1868*c1379625SJustin T. Gibbs 	error = zap_remove(mos,
1869*c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1870fa9e4066Sahrens 	    dd->dd_myname, tx);
18713b2aab18SMatthew Ahrens 	ASSERT0(error);
1872fa9e4066Sahrens 
18733b2aab18SMatthew Ahrens 	(void) strcpy(dd->dd_myname, mynewname);
18743b2aab18SMatthew Ahrens 	dsl_dir_rele(dd->dd_parent, dd);
1875*c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
18763b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp,
18773b2aab18SMatthew Ahrens 	    newparent->dd_object, NULL, dd, &dd->dd_parent));
1878fa9e4066Sahrens 
1879fa9e4066Sahrens 	/* add to new parent zapobj */
1880*c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
18813b2aab18SMatthew Ahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx));
18823b2aab18SMatthew Ahrens 
18833b2aab18SMatthew Ahrens 	dsl_prop_notify_all(dd);
1884ecd6cf80Smarks 
18853b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
18863b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
18871d452cf5Sahrens }
1888fa9e4066Sahrens 
18891d452cf5Sahrens int
18903b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname)
18911d452cf5Sahrens {
18923b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t ddra;
18931d452cf5Sahrens 
18943b2aab18SMatthew Ahrens 	ddra.ddra_oldname = oldname;
18953b2aab18SMatthew Ahrens 	ddra.ddra_newname = newname;
1896a2afb611SJerry Jelinek 	ddra.ddra_cred = CRED();
18971d452cf5Sahrens 
18983b2aab18SMatthew Ahrens 	return (dsl_sync_task(oldname,
18997d46dc6cSMatthew Ahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
19007d46dc6cSMatthew Ahrens 	    3, ZFS_SPACE_CHECK_RESERVED));
1901fa9e4066Sahrens }
190299653d4eSeschrock 
190399653d4eSeschrock int
1904a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1905a2afb611SJerry Jelinek     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
190699653d4eSeschrock {
190799653d4eSeschrock 	dsl_dir_t *ancestor;
190899653d4eSeschrock 	int64_t adelta;
190999653d4eSeschrock 	uint64_t avail;
1910a2afb611SJerry Jelinek 	int err;
191199653d4eSeschrock 
191299653d4eSeschrock 	ancestor = closest_common_ancestor(sdd, tdd);
191399653d4eSeschrock 	adelta = would_change(sdd, -space, ancestor);
191499653d4eSeschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
191599653d4eSeschrock 	if (avail < space)
1916be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
191799653d4eSeschrock 
1918a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1919a2afb611SJerry Jelinek 	    ancestor, cr);
1920a2afb611SJerry Jelinek 	if (err != 0)
1921a2afb611SJerry Jelinek 		return (err);
1922a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1923a2afb611SJerry Jelinek 	    ancestor, cr);
1924a2afb611SJerry Jelinek 	if (err != 0)
1925a2afb611SJerry Jelinek 		return (err);
1926a2afb611SJerry Jelinek 
192799653d4eSeschrock 	return (0);
192899653d4eSeschrock }
192971eb0538SChris Kirby 
193071eb0538SChris Kirby timestruc_t
193171eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd)
193271eb0538SChris Kirby {
193371eb0538SChris Kirby 	timestruc_t t;
193471eb0538SChris Kirby 
193571eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
193671eb0538SChris Kirby 	t = dd->dd_snap_cmtime;
193771eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
193871eb0538SChris Kirby 
193971eb0538SChris Kirby 	return (t);
194071eb0538SChris Kirby }
194171eb0538SChris Kirby 
194271eb0538SChris Kirby void
194371eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
194471eb0538SChris Kirby {
194571eb0538SChris Kirby 	timestruc_t t;
194671eb0538SChris Kirby 
194771eb0538SChris Kirby 	gethrestime(&t);
194871eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
194971eb0538SChris Kirby 	dd->dd_snap_cmtime = t;
195071eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
195171eb0538SChris Kirby }
19522acef22dSMatthew Ahrens 
19532acef22dSMatthew Ahrens void
19542acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
19552acef22dSMatthew Ahrens {
19562acef22dSMatthew Ahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
19572acef22dSMatthew Ahrens 	dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
19582acef22dSMatthew Ahrens }
1959a2afb611SJerry Jelinek 
1960a2afb611SJerry Jelinek boolean_t
1961a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd)
1962a2afb611SJerry Jelinek {
1963a2afb611SJerry Jelinek 	dmu_object_info_t doi;
1964a2afb611SJerry Jelinek 
1965a2afb611SJerry Jelinek 	dmu_object_info_from_db(dd->dd_dbuf, &doi);
1966a2afb611SJerry Jelinek 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
1967a2afb611SJerry Jelinek }
1968