xref: /titanic_50/usr/src/uts/common/fs/zfs/dsl_dir.c (revision e6546372a86065934a1b311176ecd1b1c543a222)
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.
23*e6546372SJosef 'Jeff' Sipek  * Copyright (c) 2012, 2016 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.
26bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2703b1c297SAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/dmu.h>
31a9799022Sck153898 #include <sys/dmu_objset.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/dsl_dataset.h>
34fa9e4066Sahrens #include <sys/dsl_dir.h>
35fa9e4066Sahrens #include <sys/dsl_prop.h>
361d452cf5Sahrens #include <sys/dsl_synctask.h>
37ecd6cf80Smarks #include <sys/dsl_deleg.h>
382acef22dSMatthew Ahrens #include <sys/dmu_impl.h>
39fa9e4066Sahrens #include <sys/spa.h>
40b24ab676SJeff Bonwick #include <sys/metaslab.h>
41fa9e4066Sahrens #include <sys/zap.h>
42fa9e4066Sahrens #include <sys/zio.h>
43fa9e4066Sahrens #include <sys/arc.h>
44ecd6cf80Smarks #include <sys/sunddi.h>
45a2afb611SJerry Jelinek #include <sys/zfeature.h>
46a2afb611SJerry Jelinek #include <sys/policy.h>
47a2afb611SJerry Jelinek #include <sys/zfs_znode.h>
48fa9e4066Sahrens #include "zfs_namecheck.h"
49a2afb611SJerry Jelinek #include "zfs_prop.h"
50a2afb611SJerry Jelinek 
51a2afb611SJerry Jelinek /*
52a2afb611SJerry Jelinek  * Filesystem and Snapshot Limits
53a2afb611SJerry Jelinek  * ------------------------------
54a2afb611SJerry Jelinek  *
55a2afb611SJerry Jelinek  * These limits are used to restrict the number of filesystems and/or snapshots
56a2afb611SJerry Jelinek  * that can be created at a given level in the tree or below. A typical
57a2afb611SJerry Jelinek  * use-case is with a delegated dataset where the administrator wants to ensure
58a2afb611SJerry Jelinek  * that a user within the zone is not creating too many additional filesystems
59a2afb611SJerry Jelinek  * or snapshots, even though they're not exceeding their space quota.
60a2afb611SJerry Jelinek  *
61a2afb611SJerry Jelinek  * The filesystem and snapshot counts are stored as extensible properties. This
62a2afb611SJerry Jelinek  * capability is controlled by a feature flag and must be enabled to be used.
63a2afb611SJerry Jelinek  * Once enabled, the feature is not active until the first limit is set. At
64a2afb611SJerry Jelinek  * that point, future operations to create/destroy filesystems or snapshots
65a2afb611SJerry Jelinek  * will validate and update the counts.
66a2afb611SJerry Jelinek  *
67a2afb611SJerry Jelinek  * Because the count properties will not exist before the feature is active,
68a2afb611SJerry Jelinek  * the counts are updated when a limit is first set on an uninitialized
69a2afb611SJerry Jelinek  * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
70a2afb611SJerry Jelinek  * all of the nested filesystems/snapshots. Thus, a new leaf node has a
71a2afb611SJerry Jelinek  * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
72a2afb611SJerry Jelinek  * snapshot count properties on a node indicate uninitialized counts on that
73a2afb611SJerry Jelinek  * node.) When first setting a limit on an uninitialized node, the code starts
74a2afb611SJerry Jelinek  * at the filesystem with the new limit and descends into all sub-filesystems
75a2afb611SJerry Jelinek  * to add the count properties.
76a2afb611SJerry Jelinek  *
77a2afb611SJerry Jelinek  * In practice this is lightweight since a limit is typically set when the
78a2afb611SJerry Jelinek  * filesystem is created and thus has no children. Once valid, changing the
79a2afb611SJerry Jelinek  * limit value won't require a re-traversal since the counts are already valid.
80a2afb611SJerry Jelinek  * When recursively fixing the counts, if a node with a limit is encountered
81a2afb611SJerry Jelinek  * during the descent, the counts are known to be valid and there is no need to
82a2afb611SJerry Jelinek  * descend into that filesystem's children. The counts on filesystems above the
83a2afb611SJerry Jelinek  * one with the new limit will still be uninitialized, unless a limit is
84a2afb611SJerry Jelinek  * eventually set on one of those filesystems. The counts are always recursively
85a2afb611SJerry Jelinek  * updated when a limit is set on a dataset, unless there is already a limit.
86a2afb611SJerry Jelinek  * When a new limit value is set on a filesystem with an existing limit, it is
87a2afb611SJerry Jelinek  * possible for the new limit to be less than the current count at that level
88a2afb611SJerry Jelinek  * since a user who can change the limit is also allowed to exceed the limit.
89a2afb611SJerry Jelinek  *
90a2afb611SJerry Jelinek  * Once the feature is active, then whenever a filesystem or snapshot is
91a2afb611SJerry Jelinek  * created, the code recurses up the tree, validating the new count against the
92a2afb611SJerry Jelinek  * limit at each initialized level. In practice, most levels will not have a
93a2afb611SJerry Jelinek  * limit set. If there is a limit at any initialized level up the tree, the
94a2afb611SJerry Jelinek  * check must pass or the creation will fail. Likewise, when a filesystem or
95a2afb611SJerry Jelinek  * snapshot is destroyed, the counts are recursively adjusted all the way up
96a2afb611SJerry Jelinek  * the initizized nodes in the tree. Renaming a filesystem into different point
97a2afb611SJerry Jelinek  * in the tree will first validate, then update the counts on each branch up to
98a2afb611SJerry Jelinek  * the common ancestor. A receive will also validate the counts and then update
99a2afb611SJerry Jelinek  * them.
100a2afb611SJerry Jelinek  *
101a2afb611SJerry Jelinek  * An exception to the above behavior is that the limit is not enforced if the
102a2afb611SJerry Jelinek  * user has permission to modify the limit. This is primarily so that
103a2afb611SJerry Jelinek  * recursive snapshots in the global zone always work. We want to prevent a
104a2afb611SJerry Jelinek  * denial-of-service in which a lower level delegated dataset could max out its
105a2afb611SJerry Jelinek  * limit and thus block recursive snapshots from being taken in the global zone.
106a2afb611SJerry Jelinek  * Because of this, it is possible for the snapshot count to be over the limit
107a2afb611SJerry Jelinek  * and snapshots taken in the global zone could cause a lower level dataset to
108a2afb611SJerry Jelinek  * hit or exceed its limit. The administrator taking the global zone recursive
109a2afb611SJerry Jelinek  * snapshot should be aware of this side-effect and behave accordingly.
110a2afb611SJerry Jelinek  * For consistency, the filesystem limit is also not enforced if the user can
111a2afb611SJerry Jelinek  * modify the limit.
112a2afb611SJerry Jelinek  *
113a2afb611SJerry Jelinek  * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
114a2afb611SJerry Jelinek  * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
115a2afb611SJerry Jelinek  * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
116a2afb611SJerry Jelinek  * dsl_dir_init_fs_ss_count().
117a2afb611SJerry Jelinek  *
118a2afb611SJerry Jelinek  * There is a special case when we receive a filesystem that already exists. In
119a2afb611SJerry Jelinek  * this case a temporary clone name of %X is created (see dmu_recv_begin). We
120a2afb611SJerry Jelinek  * never update the filesystem counts for temporary clones.
121a2afb611SJerry Jelinek  *
122a2afb611SJerry Jelinek  * Likewise, we do not update the snapshot counts for temporary snapshots,
123a2afb611SJerry Jelinek  * such as those created by zfs diff.
124a2afb611SJerry Jelinek  */
125fa9e4066Sahrens 
126b9f08e63SArne Jansen /*
127b9f08e63SArne Jansen  * Tunable to control EDQUOT behaviour. With this set to a value != 0, zfs
128b9f08e63SArne Jansen  * doesn't always wait for a dirty txg to complete when an operation can't
129b9f08e63SArne Jansen  * get through due to space exhaustion. Instead it fails early in a range
130b9f08e63SArne Jansen  * of the tunable around the quota.
131b9f08e63SArne Jansen  * This vastly helps to reduce the number of threads waiting for the txg
132b9f08e63SArne Jansen  * to commit when a busy filesystem is near quota, especially in combination
133b9f08e63SArne Jansen  * with NFS, where each waiter takes up a server thread.
134b9f08e63SArne Jansen  */
135b9f08e63SArne Jansen uint64_t early_edquot_threshold = 32 * 1048576; /* tunable */
136b9f08e63SArne Jansen 
137c1379625SJustin T. Gibbs extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
138c1379625SJustin T. Gibbs 
139a9799022Sck153898 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
140fa9e4066Sahrens 
141fa9e4066Sahrens static void
dsl_dir_evict_async(void * dbu)142*e6546372SJosef 'Jeff' Sipek dsl_dir_evict_async(void *dbu)
143fa9e4066Sahrens {
144bc9014e6SJustin Gibbs 	dsl_dir_t *dd = dbu;
145fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
146fa9e4066Sahrens 	int t;
147fa9e4066Sahrens 
148bc9014e6SJustin Gibbs 	dd->dd_dbuf = NULL;
149bc9014e6SJustin Gibbs 
150fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
151fa9e4066Sahrens 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
152fa9e4066Sahrens 		ASSERT(dd->dd_tempreserved[t] == 0);
153fa9e4066Sahrens 		ASSERT(dd->dd_space_towrite[t] == 0);
154fa9e4066Sahrens 	}
155fa9e4066Sahrens 
156fa9e4066Sahrens 	if (dd->dd_parent)
157bc9014e6SJustin Gibbs 		dsl_dir_async_rele(dd->dd_parent, dd);
158fa9e4066Sahrens 
159bc9014e6SJustin Gibbs 	spa_async_close(dd->dd_pool->dp_spa, dd);
160fa9e4066Sahrens 
16103bad06fSJustin Gibbs 	dsl_prop_fini(dd);
1625ad82045Snd150628 	mutex_destroy(&dd->dd_lock);
163fa9e4066Sahrens 	kmem_free(dd, sizeof (dsl_dir_t));
164fa9e4066Sahrens }
165fa9e4066Sahrens 
166ea8dc4b6Seschrock int
dsl_dir_hold_obj(dsl_pool_t * dp,uint64_t ddobj,const char * tail,void * tag,dsl_dir_t ** ddp)1673b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
168ea8dc4b6Seschrock     const char *tail, void *tag, dsl_dir_t **ddp)
169fa9e4066Sahrens {
170fa9e4066Sahrens 	dmu_buf_t *dbuf;
171fa9e4066Sahrens 	dsl_dir_t *dd;
172ea8dc4b6Seschrock 	int err;
173fa9e4066Sahrens 
1743b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
175fa9e4066Sahrens 
176ea8dc4b6Seschrock 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
1773b2aab18SMatthew Ahrens 	if (err != 0)
178ea8dc4b6Seschrock 		return (err);
179fa9e4066Sahrens 	dd = dmu_buf_get_user(dbuf);
180fa9e4066Sahrens #ifdef ZFS_DEBUG
181fa9e4066Sahrens 	{
182fa9e4066Sahrens 		dmu_object_info_t doi;
183fa9e4066Sahrens 		dmu_object_info_from_db(dbuf, &doi);
1842acef22dSMatthew Ahrens 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
18574e7dc98SMatthew Ahrens 		ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
186fa9e4066Sahrens 	}
187fa9e4066Sahrens #endif
188fa9e4066Sahrens 	if (dd == NULL) {
189fa9e4066Sahrens 		dsl_dir_t *winner;
190fa9e4066Sahrens 
191fa9e4066Sahrens 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
192fa9e4066Sahrens 		dd->dd_object = ddobj;
193fa9e4066Sahrens 		dd->dd_dbuf = dbuf;
194fa9e4066Sahrens 		dd->dd_pool = dp;
1955ad82045Snd150628 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
19603bad06fSJustin Gibbs 		dsl_prop_init(dd);
197fa9e4066Sahrens 
19871eb0538SChris Kirby 		dsl_dir_snap_cmtime_update(dd);
19971eb0538SChris Kirby 
200c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_parent_obj) {
201c1379625SJustin T. Gibbs 			err = dsl_dir_hold_obj(dp,
202c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
203c1379625SJustin T. Gibbs 			    &dd->dd_parent);
2043b2aab18SMatthew Ahrens 			if (err != 0)
20574e7dc98SMatthew Ahrens 				goto errout;
206fa9e4066Sahrens 			if (tail) {
207fa9e4066Sahrens #ifdef ZFS_DEBUG
208fa9e4066Sahrens 				uint64_t foundobj;
209fa9e4066Sahrens 
210fa9e4066Sahrens 				err = zap_lookup(dp->dp_meta_objset,
211c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
212c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj, tail,
213c1379625SJustin T. Gibbs 				    sizeof (foundobj), 1, &foundobj);
214ea8dc4b6Seschrock 				ASSERT(err || foundobj == ddobj);
215fa9e4066Sahrens #endif
216fa9e4066Sahrens 				(void) strcpy(dd->dd_myname, tail);
217fa9e4066Sahrens 			} else {
218fa9e4066Sahrens 				err = zap_value_search(dp->dp_meta_objset,
219c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
220c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj,
221e7437265Sahrens 				    ddobj, 0, dd->dd_myname);
222ea8dc4b6Seschrock 			}
2233b2aab18SMatthew Ahrens 			if (err != 0)
22474e7dc98SMatthew Ahrens 				goto errout;
225fa9e4066Sahrens 		} else {
226fa9e4066Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
227fa9e4066Sahrens 		}
228fa9e4066Sahrens 
2293f9d6ad7SLin Ling 		if (dsl_dir_is_clone(dd)) {
2303f9d6ad7SLin Ling 			dmu_buf_t *origin_bonus;
2313f9d6ad7SLin Ling 			dsl_dataset_phys_t *origin_phys;
2323f9d6ad7SLin Ling 
2333f9d6ad7SLin Ling 			/*
2343f9d6ad7SLin Ling 			 * We can't open the origin dataset, because
2353f9d6ad7SLin Ling 			 * that would require opening this dsl_dir.
2363f9d6ad7SLin Ling 			 * Just look at its phys directly instead.
2373f9d6ad7SLin Ling 			 */
2383f9d6ad7SLin Ling 			err = dmu_bonus_hold(dp->dp_meta_objset,
239c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_origin_obj, FTAG,
240c1379625SJustin T. Gibbs 			    &origin_bonus);
2413b2aab18SMatthew Ahrens 			if (err != 0)
2423f9d6ad7SLin Ling 				goto errout;
2433f9d6ad7SLin Ling 			origin_phys = origin_bonus->db_data;
2443f9d6ad7SLin Ling 			dd->dd_origin_txg =
2453f9d6ad7SLin Ling 			    origin_phys->ds_creation_txg;
2463f9d6ad7SLin Ling 			dmu_buf_rele(origin_bonus, FTAG);
2473f9d6ad7SLin Ling 		}
2483f9d6ad7SLin Ling 
249*e6546372SJosef 'Jeff' Sipek 		dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
250*e6546372SJosef 'Jeff' Sipek 		    &dd->dd_dbuf);
251bc9014e6SJustin Gibbs 		winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
252bc9014e6SJustin Gibbs 		if (winner != NULL) {
253fa9e4066Sahrens 			if (dd->dd_parent)
2543b2aab18SMatthew Ahrens 				dsl_dir_rele(dd->dd_parent, dd);
25503bad06fSJustin Gibbs 			dsl_prop_fini(dd);
2565ad82045Snd150628 			mutex_destroy(&dd->dd_lock);
257fa9e4066Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
258fa9e4066Sahrens 			dd = winner;
259fa9e4066Sahrens 		} else {
260fa9e4066Sahrens 			spa_open_ref(dp->dp_spa, dd);
261fa9e4066Sahrens 		}
262fa9e4066Sahrens 	}
263fa9e4066Sahrens 
264fa9e4066Sahrens 	/*
265fa9e4066Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
266fa9e4066Sahrens 	 * holds on the spa.  We need the open-to-close holds because
267fa9e4066Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
268fa9e4066Sahrens 	 * dir which the spa also has open, so we could incorrectly
269fa9e4066Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
270fa9e4066Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
271fa9e4066Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
272fa9e4066Sahrens 	 */
273fa9e4066Sahrens 	spa_open_ref(dp->dp_spa, tag);
274fa9e4066Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
275fa9e4066Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
276fa9e4066Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
277ea8dc4b6Seschrock 	*ddp = dd;
278ea8dc4b6Seschrock 	return (0);
27974e7dc98SMatthew Ahrens 
28074e7dc98SMatthew Ahrens errout:
28174e7dc98SMatthew Ahrens 	if (dd->dd_parent)
2823b2aab18SMatthew Ahrens 		dsl_dir_rele(dd->dd_parent, dd);
28303bad06fSJustin Gibbs 	dsl_prop_fini(dd);
28474e7dc98SMatthew Ahrens 	mutex_destroy(&dd->dd_lock);
28574e7dc98SMatthew Ahrens 	kmem_free(dd, sizeof (dsl_dir_t));
28674e7dc98SMatthew Ahrens 	dmu_buf_rele(dbuf, tag);
28774e7dc98SMatthew Ahrens 	return (err);
288fa9e4066Sahrens }
289fa9e4066Sahrens 
290fa9e4066Sahrens void
dsl_dir_rele(dsl_dir_t * dd,void * tag)2913b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag)
292fa9e4066Sahrens {
293fa9e4066Sahrens 	dprintf_dd(dd, "%s\n", "");
294fa9e4066Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
295ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
296fa9e4066Sahrens }
297fa9e4066Sahrens 
298bc9014e6SJustin Gibbs /*
299bc9014e6SJustin Gibbs  * Remove a reference to the given dsl dir that is being asynchronously
300bc9014e6SJustin Gibbs  * released.  Async releases occur from a taskq performing eviction of
301bc9014e6SJustin Gibbs  * dsl datasets and dirs.  This process is identical to a normal release
302bc9014e6SJustin Gibbs  * with the exception of using the async API for releasing the reference on
303bc9014e6SJustin Gibbs  * the spa.
304bc9014e6SJustin Gibbs  */
305bc9014e6SJustin Gibbs void
dsl_dir_async_rele(dsl_dir_t * dd,void * tag)306bc9014e6SJustin Gibbs dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
307bc9014e6SJustin Gibbs {
308bc9014e6SJustin Gibbs 	dprintf_dd(dd, "%s\n", "");
309bc9014e6SJustin Gibbs 	spa_async_close(dd->dd_pool->dp_spa, tag);
310bc9014e6SJustin Gibbs 	dmu_buf_rele(dd->dd_dbuf, tag);
311bc9014e6SJustin Gibbs }
312bc9014e6SJustin Gibbs 
31340a5c998SMatthew Ahrens /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
314fa9e4066Sahrens void
dsl_dir_name(dsl_dir_t * dd,char * buf)315fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
316fa9e4066Sahrens {
317fa9e4066Sahrens 	if (dd->dd_parent) {
318fa9e4066Sahrens 		dsl_dir_name(dd->dd_parent, buf);
31940a5c998SMatthew Ahrens 		VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
32040a5c998SMatthew Ahrens 		    ZFS_MAX_DATASET_NAME_LEN);
321fa9e4066Sahrens 	} else {
322fa9e4066Sahrens 		buf[0] = '\0';
323fa9e4066Sahrens 	}
324fa9e4066Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
325fa9e4066Sahrens 		/*
326fa9e4066Sahrens 		 * recursive mutex so that we can use
327fa9e4066Sahrens 		 * dprintf_dd() with dd_lock held
328fa9e4066Sahrens 		 */
329fa9e4066Sahrens 		mutex_enter(&dd->dd_lock);
33040a5c998SMatthew Ahrens 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
33140a5c998SMatthew Ahrens 		    <, ZFS_MAX_DATASET_NAME_LEN);
332fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
333fa9e4066Sahrens 	} else {
33440a5c998SMatthew Ahrens 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
33540a5c998SMatthew Ahrens 		    <, ZFS_MAX_DATASET_NAME_LEN);
336fa9e4066Sahrens 	}
337fa9e4066Sahrens }
338fa9e4066Sahrens 
339ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
340b7661cccSmmusante int
dsl_dir_namelen(dsl_dir_t * dd)341b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd)
342b7661cccSmmusante {
343b7661cccSmmusante 	int result = 0;
344b7661cccSmmusante 
345b7661cccSmmusante 	if (dd->dd_parent) {
346b7661cccSmmusante 		/* parent's name + 1 for the "/" */
347b7661cccSmmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
348b7661cccSmmusante 	}
349b7661cccSmmusante 
350b7661cccSmmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
351b7661cccSmmusante 		/* see dsl_dir_name */
352b7661cccSmmusante 		mutex_enter(&dd->dd_lock);
353b7661cccSmmusante 		result += strlen(dd->dd_myname);
354b7661cccSmmusante 		mutex_exit(&dd->dd_lock);
355b7661cccSmmusante 	} else {
356b7661cccSmmusante 		result += strlen(dd->dd_myname);
357b7661cccSmmusante 	}
358b7661cccSmmusante 
359b7661cccSmmusante 	return (result);
360b7661cccSmmusante }
361b7661cccSmmusante 
362fa9e4066Sahrens static int
getcomponent(const char * path,char * component,const char ** nextp)363fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp)
364fa9e4066Sahrens {
365fa9e4066Sahrens 	char *p;
3663b2aab18SMatthew Ahrens 
367ccba0801SRich Morris 	if ((path == NULL) || (path[0] == '\0'))
368be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
369fa9e4066Sahrens 	/* This would be a good place to reserve some namespace... */
370fa9e4066Sahrens 	p = strpbrk(path, "/@");
371fa9e4066Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
372fa9e4066Sahrens 		/* two separators in a row */
373be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
374fa9e4066Sahrens 	}
375fa9e4066Sahrens 	if (p == NULL || p == path) {
376fa9e4066Sahrens 		/*
377fa9e4066Sahrens 		 * if the first thing is an @ or /, it had better be an
378fa9e4066Sahrens 		 * @ and it had better not have any more ats or slashes,
379fa9e4066Sahrens 		 * and it had better have something after the @.
380fa9e4066Sahrens 		 */
381fa9e4066Sahrens 		if (p != NULL &&
382fa9e4066Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
383be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
38440a5c998SMatthew Ahrens 		if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
385be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
386fa9e4066Sahrens 		(void) strcpy(component, path);
387fa9e4066Sahrens 		p = NULL;
388fa9e4066Sahrens 	} else if (p[0] == '/') {
38940a5c998SMatthew Ahrens 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
390be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
391fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
392fa9e4066Sahrens 		component[p - path] = '\0';
393fa9e4066Sahrens 		p++;
394fa9e4066Sahrens 	} else if (p[0] == '@') {
395fa9e4066Sahrens 		/*
396fa9e4066Sahrens 		 * if the next separator is an @, there better not be
397fa9e4066Sahrens 		 * any more slashes.
398fa9e4066Sahrens 		 */
399fa9e4066Sahrens 		if (strchr(path, '/'))
400be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
40140a5c998SMatthew Ahrens 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
402be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
403fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
404fa9e4066Sahrens 		component[p - path] = '\0';
405fa9e4066Sahrens 	} else {
4063b2aab18SMatthew Ahrens 		panic("invalid p=%p", (void *)p);
407fa9e4066Sahrens 	}
408fa9e4066Sahrens 	*nextp = p;
409fa9e4066Sahrens 	return (0);
410fa9e4066Sahrens }
411fa9e4066Sahrens 
412fa9e4066Sahrens /*
4133b2aab18SMatthew Ahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
4143b2aab18SMatthew Ahrens  * be found in *tail.  The name must be in the specified dsl_pool_t.  This
4153b2aab18SMatthew Ahrens  * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
4163b2aab18SMatthew Ahrens  * path is bogus, or if tail==NULL and we couldn't parse the whole name.
4173b2aab18SMatthew Ahrens  * (*tail)[0] == '@' means that the last component is a snapshot.
418fa9e4066Sahrens  */
419ea8dc4b6Seschrock int
dsl_dir_hold(dsl_pool_t * dp,const char * name,void * tag,dsl_dir_t ** ddp,const char ** tailp)4203b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
421ea8dc4b6Seschrock     dsl_dir_t **ddp, const char **tailp)
422fa9e4066Sahrens {
42340a5c998SMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
4243b2aab18SMatthew Ahrens 	const char *spaname, *next, *nextnext = NULL;
425fa9e4066Sahrens 	int err;
426fa9e4066Sahrens 	dsl_dir_t *dd;
427fa9e4066Sahrens 	uint64_t ddobj;
428fa9e4066Sahrens 
429fa9e4066Sahrens 	err = getcomponent(name, buf, &next);
4303b2aab18SMatthew Ahrens 	if (err != 0)
431ea8dc4b6Seschrock 		return (err);
432fa9e4066Sahrens 
4333b2aab18SMatthew Ahrens 	/* Make sure the name is in the specified pool. */
4343b2aab18SMatthew Ahrens 	spaname = spa_name(dp->dp_spa);
4353b2aab18SMatthew Ahrens 	if (strcmp(buf, spaname) != 0)
43603b1c297SAlexander Eremin 		return (SET_ERROR(EXDEV));
437fa9e4066Sahrens 
4383b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
439fa9e4066Sahrens 
4403b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
4413b2aab18SMatthew Ahrens 	if (err != 0) {
442ea8dc4b6Seschrock 		return (err);
443fa9e4066Sahrens 	}
444ea8dc4b6Seschrock 
445ea8dc4b6Seschrock 	while (next != NULL) {
446bc9014e6SJustin Gibbs 		dsl_dir_t *child_dd;
447ea8dc4b6Seschrock 		err = getcomponent(next, buf, &nextnext);
4483b2aab18SMatthew Ahrens 		if (err != 0)
449ea8dc4b6Seschrock 			break;
450fa9e4066Sahrens 		ASSERT(next[0] != '\0');
451fa9e4066Sahrens 		if (next[0] == '@')
452fa9e4066Sahrens 			break;
453fa9e4066Sahrens 		dprintf("looking up %s in obj%lld\n",
454c1379625SJustin T. Gibbs 		    buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
455fa9e4066Sahrens 
456fa9e4066Sahrens 		err = zap_lookup(dp->dp_meta_objset,
457c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj,
458fa9e4066Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
4593b2aab18SMatthew Ahrens 		if (err != 0) {
460ea8dc4b6Seschrock 			if (err == ENOENT)
461ea8dc4b6Seschrock 				err = 0;
462fa9e4066Sahrens 			break;
463fa9e4066Sahrens 		}
464fa9e4066Sahrens 
465bc9014e6SJustin Gibbs 		err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
4663b2aab18SMatthew Ahrens 		if (err != 0)
467ea8dc4b6Seschrock 			break;
4683b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
469bc9014e6SJustin Gibbs 		dd = child_dd;
470fa9e4066Sahrens 		next = nextnext;
471fa9e4066Sahrens 	}
472fa9e4066Sahrens 
4733b2aab18SMatthew Ahrens 	if (err != 0) {
4743b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
475ea8dc4b6Seschrock 		return (err);
476ea8dc4b6Seschrock 	}
477ea8dc4b6Seschrock 
478fa9e4066Sahrens 	/*
479fa9e4066Sahrens 	 * It's an error if there's more than one component left, or
480fa9e4066Sahrens 	 * tailp==NULL and there's any component left.
481fa9e4066Sahrens 	 */
482fa9e4066Sahrens 	if (next != NULL &&
483fa9e4066Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
484fa9e4066Sahrens 		/* bad path name */
4853b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
486fa9e4066Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
487be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
488fa9e4066Sahrens 	}
4893b2aab18SMatthew Ahrens 	if (tailp != NULL)
490fa9e4066Sahrens 		*tailp = next;
491ea8dc4b6Seschrock 	*ddp = dd;
492ea8dc4b6Seschrock 	return (err);
493fa9e4066Sahrens }
494fa9e4066Sahrens 
495a2afb611SJerry Jelinek /*
496a2afb611SJerry Jelinek  * If the counts are already initialized for this filesystem and its
497a2afb611SJerry Jelinek  * descendants then do nothing, otherwise initialize the counts.
498a2afb611SJerry Jelinek  *
499a2afb611SJerry Jelinek  * The counts on this filesystem, and those below, may be uninitialized due to
500a2afb611SJerry Jelinek  * either the use of a pre-existing pool which did not support the
501a2afb611SJerry Jelinek  * filesystem/snapshot limit feature, or one in which the feature had not yet
502a2afb611SJerry Jelinek  * been enabled.
503a2afb611SJerry Jelinek  *
504a2afb611SJerry Jelinek  * Recursively descend the filesystem tree and update the filesystem/snapshot
505a2afb611SJerry Jelinek  * counts on each filesystem below, then update the cumulative count on the
506a2afb611SJerry Jelinek  * current filesystem. If the filesystem already has a count set on it,
507a2afb611SJerry Jelinek  * then we know that its counts, and the counts on the filesystems below it,
508a2afb611SJerry Jelinek  * are already correct, so we don't have to update this filesystem.
509a2afb611SJerry Jelinek  */
510a2afb611SJerry Jelinek static void
dsl_dir_init_fs_ss_count(dsl_dir_t * dd,dmu_tx_t * tx)511a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
512a2afb611SJerry Jelinek {
513a2afb611SJerry Jelinek 	uint64_t my_fs_cnt = 0;
514a2afb611SJerry Jelinek 	uint64_t my_ss_cnt = 0;
515a2afb611SJerry Jelinek 	dsl_pool_t *dp = dd->dd_pool;
516a2afb611SJerry Jelinek 	objset_t *os = dp->dp_meta_objset;
517a2afb611SJerry Jelinek 	zap_cursor_t *zc;
518a2afb611SJerry Jelinek 	zap_attribute_t *za;
519a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
520a2afb611SJerry Jelinek 
521adf34077SJerry Jelinek 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
522a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dp));
523a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
524a2afb611SJerry Jelinek 
525a2afb611SJerry Jelinek 	dsl_dir_zapify(dd, tx);
526a2afb611SJerry Jelinek 
527a2afb611SJerry Jelinek 	/*
528a2afb611SJerry Jelinek 	 * If the filesystem count has already been initialized then we
529a2afb611SJerry Jelinek 	 * don't need to recurse down any further.
530a2afb611SJerry Jelinek 	 */
531a2afb611SJerry Jelinek 	if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
532a2afb611SJerry Jelinek 		return;
533a2afb611SJerry Jelinek 
534a2afb611SJerry Jelinek 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
535a2afb611SJerry Jelinek 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
536a2afb611SJerry Jelinek 
537a2afb611SJerry Jelinek 	/* Iterate my child dirs */
538c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
539a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
540a2afb611SJerry Jelinek 		dsl_dir_t *chld_dd;
541a2afb611SJerry Jelinek 		uint64_t count;
542a2afb611SJerry Jelinek 
543a2afb611SJerry Jelinek 		VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
544a2afb611SJerry Jelinek 		    &chld_dd));
545a2afb611SJerry Jelinek 
546a2afb611SJerry Jelinek 		/*
547a2afb611SJerry Jelinek 		 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
548a2afb611SJerry Jelinek 		 * temporary datasets.
549a2afb611SJerry Jelinek 		 */
550a2afb611SJerry Jelinek 		if (chld_dd->dd_myname[0] == '$' ||
551a2afb611SJerry Jelinek 		    chld_dd->dd_myname[0] == '%') {
552a2afb611SJerry Jelinek 			dsl_dir_rele(chld_dd, FTAG);
553a2afb611SJerry Jelinek 			continue;
554a2afb611SJerry Jelinek 		}
555a2afb611SJerry Jelinek 
556a2afb611SJerry Jelinek 		my_fs_cnt++;	/* count this child */
557a2afb611SJerry Jelinek 
558a2afb611SJerry Jelinek 		dsl_dir_init_fs_ss_count(chld_dd, tx);
559a2afb611SJerry Jelinek 
560a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
561a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
562a2afb611SJerry Jelinek 		my_fs_cnt += count;
563a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
564a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
565a2afb611SJerry Jelinek 		my_ss_cnt += count;
566a2afb611SJerry Jelinek 
567a2afb611SJerry Jelinek 		dsl_dir_rele(chld_dd, FTAG);
568a2afb611SJerry Jelinek 	}
569a2afb611SJerry Jelinek 	zap_cursor_fini(zc);
570a2afb611SJerry Jelinek 	/* Count my snapshots (we counted children's snapshots above) */
571a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
572c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
573a2afb611SJerry Jelinek 
574c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
575a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0;
576a2afb611SJerry Jelinek 	    zap_cursor_advance(zc)) {
577a2afb611SJerry Jelinek 		/* Don't count temporary snapshots */
578a2afb611SJerry Jelinek 		if (za->za_name[0] != '%')
579a2afb611SJerry Jelinek 			my_ss_cnt++;
580a2afb611SJerry Jelinek 	}
581fb7001f1SAlex Reece 	zap_cursor_fini(zc);
582a2afb611SJerry Jelinek 
583a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
584a2afb611SJerry Jelinek 
585a2afb611SJerry Jelinek 	kmem_free(zc, sizeof (zap_cursor_t));
586a2afb611SJerry Jelinek 	kmem_free(za, sizeof (zap_attribute_t));
587a2afb611SJerry Jelinek 
588a2afb611SJerry Jelinek 	/* we're in a sync task, update counts */
589a2afb611SJerry Jelinek 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
590a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
591a2afb611SJerry Jelinek 	    sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
592a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
593a2afb611SJerry Jelinek 	    sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
594a2afb611SJerry Jelinek }
595a2afb611SJerry Jelinek 
596a2afb611SJerry Jelinek static int
dsl_dir_actv_fs_ss_limit_check(void * arg,dmu_tx_t * tx)597a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
598a2afb611SJerry Jelinek {
599a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
600a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
601a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
602a2afb611SJerry Jelinek 	dsl_dir_t *dd;
603a2afb611SJerry Jelinek 	int error;
604a2afb611SJerry Jelinek 
605a2afb611SJerry Jelinek 	error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
606a2afb611SJerry Jelinek 	if (error != 0)
607a2afb611SJerry Jelinek 		return (error);
608a2afb611SJerry Jelinek 
609a2afb611SJerry Jelinek 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
610a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
611a2afb611SJerry Jelinek 		return (SET_ERROR(ENOTSUP));
612a2afb611SJerry Jelinek 	}
613a2afb611SJerry Jelinek 
614a2afb611SJerry Jelinek 	dd = ds->ds_dir;
615a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
616a2afb611SJerry Jelinek 	    dsl_dir_is_zapified(dd) &&
617a2afb611SJerry Jelinek 	    zap_contains(dp->dp_meta_objset, dd->dd_object,
618a2afb611SJerry Jelinek 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
619a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
620a2afb611SJerry Jelinek 		return (SET_ERROR(EALREADY));
621a2afb611SJerry Jelinek 	}
622a2afb611SJerry Jelinek 
623a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
624a2afb611SJerry Jelinek 	return (0);
625a2afb611SJerry Jelinek }
626a2afb611SJerry Jelinek 
627a2afb611SJerry Jelinek static void
dsl_dir_actv_fs_ss_limit_sync(void * arg,dmu_tx_t * tx)628a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
629a2afb611SJerry Jelinek {
630a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
631a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
632a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
633a2afb611SJerry Jelinek 	spa_t *spa;
634a2afb611SJerry Jelinek 
635a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
636a2afb611SJerry Jelinek 
637a2afb611SJerry Jelinek 	spa = dsl_dataset_get_spa(ds);
638a2afb611SJerry Jelinek 
639a2afb611SJerry Jelinek 	if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
640a2afb611SJerry Jelinek 		/*
641a2afb611SJerry Jelinek 		 * Since the feature was not active and we're now setting a
642a2afb611SJerry Jelinek 		 * limit, increment the feature-active counter so that the
643a2afb611SJerry Jelinek 		 * feature becomes active for the first time.
644a2afb611SJerry Jelinek 		 *
645a2afb611SJerry Jelinek 		 * We are already in a sync task so we can update the MOS.
646a2afb611SJerry Jelinek 		 */
647a2afb611SJerry Jelinek 		spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
648a2afb611SJerry Jelinek 	}
649a2afb611SJerry Jelinek 
650a2afb611SJerry Jelinek 	/*
651a2afb611SJerry Jelinek 	 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
652a2afb611SJerry Jelinek 	 * we need to ensure the counts are correct. Descend down the tree from
653a2afb611SJerry Jelinek 	 * this point and update all of the counts to be accurate.
654a2afb611SJerry Jelinek 	 */
655a2afb611SJerry Jelinek 	dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
656a2afb611SJerry Jelinek 
657a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
658a2afb611SJerry Jelinek }
659a2afb611SJerry Jelinek 
660a2afb611SJerry Jelinek /*
661a2afb611SJerry Jelinek  * Make sure the feature is enabled and activate it if necessary.
662a2afb611SJerry Jelinek  * Since we're setting a limit, ensure the on-disk counts are valid.
663a2afb611SJerry Jelinek  * This is only called by the ioctl path when setting a limit value.
664a2afb611SJerry Jelinek  *
665a2afb611SJerry Jelinek  * We do not need to validate the new limit, since users who can change the
666a2afb611SJerry Jelinek  * limit are also allowed to exceed the limit.
667a2afb611SJerry Jelinek  */
668a2afb611SJerry Jelinek int
dsl_dir_activate_fs_ss_limit(const char * ddname)669a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname)
670a2afb611SJerry Jelinek {
671a2afb611SJerry Jelinek 	int error;
672a2afb611SJerry Jelinek 
673a2afb611SJerry Jelinek 	error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
6747d46dc6cSMatthew Ahrens 	    dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
6757d46dc6cSMatthew Ahrens 	    ZFS_SPACE_CHECK_RESERVED);
676a2afb611SJerry Jelinek 
677a2afb611SJerry Jelinek 	if (error == EALREADY)
678a2afb611SJerry Jelinek 		error = 0;
679a2afb611SJerry Jelinek 
680a2afb611SJerry Jelinek 	return (error);
681a2afb611SJerry Jelinek }
682a2afb611SJerry Jelinek 
683a2afb611SJerry Jelinek /*
684a2afb611SJerry Jelinek  * Used to determine if the filesystem_limit or snapshot_limit should be
685a2afb611SJerry Jelinek  * enforced. We allow the limit to be exceeded if the user has permission to
686a2afb611SJerry Jelinek  * write the property value. We pass in the creds that we got in the open
687a2afb611SJerry Jelinek  * context since we will always be the GZ root in syncing context. We also have
688a2afb611SJerry Jelinek  * to handle the case where we are allowed to change the limit on the current
689a2afb611SJerry Jelinek  * dataset, but there may be another limit in the tree above.
690a2afb611SJerry Jelinek  *
691a2afb611SJerry Jelinek  * We can never modify these two properties within a non-global zone. In
692a2afb611SJerry Jelinek  * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
693a2afb611SJerry Jelinek  * can't use that function since we are already holding the dp_config_rwlock.
694a2afb611SJerry Jelinek  * In addition, we already have the dd and dealing with snapshots is simplified
695a2afb611SJerry Jelinek  * in this code.
696a2afb611SJerry Jelinek  */
697a2afb611SJerry Jelinek 
698a2afb611SJerry Jelinek typedef enum {
699a2afb611SJerry Jelinek 	ENFORCE_ALWAYS,
700a2afb611SJerry Jelinek 	ENFORCE_NEVER,
701a2afb611SJerry Jelinek 	ENFORCE_ABOVE
702a2afb611SJerry Jelinek } enforce_res_t;
703a2afb611SJerry Jelinek 
704a2afb611SJerry Jelinek static enforce_res_t
dsl_enforce_ds_ss_limits(dsl_dir_t * dd,zfs_prop_t prop,cred_t * cr)705a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
706a2afb611SJerry Jelinek {
707a2afb611SJerry Jelinek 	enforce_res_t enforce = ENFORCE_ALWAYS;
708a2afb611SJerry Jelinek 	uint64_t obj;
709a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
710a2afb611SJerry Jelinek 	uint64_t zoned;
711a2afb611SJerry Jelinek 
712a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
713a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
714a2afb611SJerry Jelinek 
715a2afb611SJerry Jelinek #ifdef _KERNEL
716a2afb611SJerry Jelinek 	if (crgetzoneid(cr) != GLOBAL_ZONEID)
717a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
718a2afb611SJerry Jelinek 
719a2afb611SJerry Jelinek 	if (secpolicy_zfs(cr) == 0)
720a2afb611SJerry Jelinek 		return (ENFORCE_NEVER);
721a2afb611SJerry Jelinek #endif
722a2afb611SJerry Jelinek 
723c1379625SJustin T. Gibbs 	if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
724a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
725a2afb611SJerry Jelinek 
726a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
727a2afb611SJerry Jelinek 
728a2afb611SJerry Jelinek 	if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
729a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
730a2afb611SJerry Jelinek 
731a2afb611SJerry Jelinek 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
732a2afb611SJerry Jelinek 		/* Only root can access zoned fs's from the GZ */
733a2afb611SJerry Jelinek 		enforce = ENFORCE_ALWAYS;
734a2afb611SJerry Jelinek 	} else {
735a2afb611SJerry Jelinek 		if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
736a2afb611SJerry Jelinek 			enforce = ENFORCE_ABOVE;
737a2afb611SJerry Jelinek 	}
738a2afb611SJerry Jelinek 
739a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
740a2afb611SJerry Jelinek 	return (enforce);
741a2afb611SJerry Jelinek }
742a2afb611SJerry Jelinek 
743a2afb611SJerry Jelinek /*
744a2afb611SJerry Jelinek  * Check if adding additional child filesystem(s) would exceed any filesystem
745a2afb611SJerry Jelinek  * limits or adding additional snapshot(s) would exceed any snapshot limits.
746a2afb611SJerry Jelinek  * The prop argument indicates which limit to check.
747a2afb611SJerry Jelinek  *
748a2afb611SJerry Jelinek  * Note that all filesystem limits up to the root (or the highest
749a2afb611SJerry Jelinek  * initialized) filesystem or the given ancestor must be satisfied.
750a2afb611SJerry Jelinek  */
751a2afb611SJerry Jelinek int
dsl_fs_ss_limit_check(dsl_dir_t * dd,uint64_t delta,zfs_prop_t prop,dsl_dir_t * ancestor,cred_t * cr)752a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
753a2afb611SJerry Jelinek     dsl_dir_t *ancestor, cred_t *cr)
754a2afb611SJerry Jelinek {
755a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
756a2afb611SJerry Jelinek 	uint64_t limit, count;
757a2afb611SJerry Jelinek 	char *count_prop;
758a2afb611SJerry Jelinek 	enforce_res_t enforce;
759a2afb611SJerry Jelinek 	int err = 0;
760a2afb611SJerry Jelinek 
761a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
762a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
763a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
764a2afb611SJerry Jelinek 
765a2afb611SJerry Jelinek 	/*
766a2afb611SJerry Jelinek 	 * If we're allowed to change the limit, don't enforce the limit
767a2afb611SJerry Jelinek 	 * e.g. this can happen if a snapshot is taken by an administrative
768a2afb611SJerry Jelinek 	 * user in the global zone (i.e. a recursive snapshot by root).
769a2afb611SJerry Jelinek 	 * However, we must handle the case of delegated permissions where we
770a2afb611SJerry Jelinek 	 * are allowed to change the limit on the current dataset, but there
771a2afb611SJerry Jelinek 	 * is another limit in the tree above.
772a2afb611SJerry Jelinek 	 */
773a2afb611SJerry Jelinek 	enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
774a2afb611SJerry Jelinek 	if (enforce == ENFORCE_NEVER)
775a2afb611SJerry Jelinek 		return (0);
776a2afb611SJerry Jelinek 
777a2afb611SJerry Jelinek 	/*
778a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment
779a2afb611SJerry Jelinek 	 * is 0.
780a2afb611SJerry Jelinek 	 */
781a2afb611SJerry Jelinek 	if (delta == 0)
782a2afb611SJerry Jelinek 		return (0);
783a2afb611SJerry Jelinek 
784a2afb611SJerry Jelinek 	if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
785a2afb611SJerry Jelinek 		/*
786a2afb611SJerry Jelinek 		 * We don't enforce the limit for temporary snapshots. This is
787a2afb611SJerry Jelinek 		 * indicated by a NULL cred_t argument.
788a2afb611SJerry Jelinek 		 */
789a2afb611SJerry Jelinek 		if (cr == NULL)
790a2afb611SJerry Jelinek 			return (0);
791a2afb611SJerry Jelinek 
792a2afb611SJerry Jelinek 		count_prop = DD_FIELD_SNAPSHOT_COUNT;
793a2afb611SJerry Jelinek 	} else {
794a2afb611SJerry Jelinek 		count_prop = DD_FIELD_FILESYSTEM_COUNT;
795a2afb611SJerry Jelinek 	}
796a2afb611SJerry Jelinek 
797a2afb611SJerry Jelinek 	/*
798a2afb611SJerry Jelinek 	 * If an ancestor has been provided, stop checking the limit once we
799a2afb611SJerry Jelinek 	 * hit that dir. We need this during rename so that we don't overcount
800a2afb611SJerry Jelinek 	 * the check once we recurse up to the common ancestor.
801a2afb611SJerry Jelinek 	 */
802a2afb611SJerry Jelinek 	if (ancestor == dd)
803a2afb611SJerry Jelinek 		return (0);
804a2afb611SJerry Jelinek 
805a2afb611SJerry Jelinek 	/*
806a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
807a2afb611SJerry Jelinek 	 * stop since we know there is no limit here (or above). The counts are
808a2afb611SJerry Jelinek 	 * not valid on this node and we know we won't touch this node's counts.
809a2afb611SJerry Jelinek 	 */
810a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
811a2afb611SJerry Jelinek 	    count_prop, sizeof (count), 1, &count) == ENOENT)
812a2afb611SJerry Jelinek 		return (0);
813a2afb611SJerry Jelinek 
814a2afb611SJerry Jelinek 	err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
815a2afb611SJerry Jelinek 	    B_FALSE);
816a2afb611SJerry Jelinek 	if (err != 0)
817a2afb611SJerry Jelinek 		return (err);
818a2afb611SJerry Jelinek 
819a2afb611SJerry Jelinek 	/* Is there a limit which we've hit? */
820a2afb611SJerry Jelinek 	if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
821a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
822a2afb611SJerry Jelinek 
823a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
824a2afb611SJerry Jelinek 		err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
825a2afb611SJerry Jelinek 		    ancestor, cr);
826a2afb611SJerry Jelinek 
827a2afb611SJerry Jelinek 	return (err);
828a2afb611SJerry Jelinek }
829a2afb611SJerry Jelinek 
830a2afb611SJerry Jelinek /*
831a2afb611SJerry Jelinek  * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
832a2afb611SJerry Jelinek  * parents. When a new filesystem/snapshot is created, increment the count on
833a2afb611SJerry Jelinek  * all parents, and when a filesystem/snapshot is destroyed, decrement the
834a2afb611SJerry Jelinek  * count.
835a2afb611SJerry Jelinek  */
836a2afb611SJerry Jelinek void
dsl_fs_ss_count_adjust(dsl_dir_t * dd,int64_t delta,const char * prop,dmu_tx_t * tx)837a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
838a2afb611SJerry Jelinek     dmu_tx_t *tx)
839a2afb611SJerry Jelinek {
840a2afb611SJerry Jelinek 	int err;
841a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
842a2afb611SJerry Jelinek 	uint64_t count;
843a2afb611SJerry Jelinek 
844a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
845a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
846a2afb611SJerry Jelinek 	ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
847a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
848a2afb611SJerry Jelinek 
849a2afb611SJerry Jelinek 	/*
850a2afb611SJerry Jelinek 	 * When we receive an incremental stream into a filesystem that already
851a2afb611SJerry Jelinek 	 * exists, a temporary clone is created.  We don't count this temporary
852a2afb611SJerry Jelinek 	 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
853a2afb611SJerry Jelinek 	 * $MOS & $ORIGIN) objsets.
854a2afb611SJerry Jelinek 	 */
855a2afb611SJerry Jelinek 	if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
856a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
857a2afb611SJerry Jelinek 		return;
858a2afb611SJerry Jelinek 
859a2afb611SJerry Jelinek 	/*
860a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
861a2afb611SJerry Jelinek 	 */
862a2afb611SJerry Jelinek 	if (delta == 0)
863a2afb611SJerry Jelinek 		return;
864a2afb611SJerry Jelinek 
865a2afb611SJerry Jelinek 	/*
866a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
867a2afb611SJerry Jelinek 	 * stop since we know the counts are not valid on this node and we
868a2afb611SJerry Jelinek 	 * know we shouldn't touch this node's counts. An uninitialized count
869a2afb611SJerry Jelinek 	 * on the node indicates that either the feature has not yet been
870a2afb611SJerry Jelinek 	 * activated or there are no limits on this part of the tree.
871a2afb611SJerry Jelinek 	 */
872a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
873a2afb611SJerry Jelinek 	    prop, sizeof (count), 1, &count)) == ENOENT)
874a2afb611SJerry Jelinek 		return;
875a2afb611SJerry Jelinek 	VERIFY0(err);
876a2afb611SJerry Jelinek 
877a2afb611SJerry Jelinek 	count += delta;
878a2afb611SJerry Jelinek 	/* Use a signed verify to make sure we're not neg. */
879a2afb611SJerry Jelinek 	VERIFY3S(count, >=, 0);
880a2afb611SJerry Jelinek 
881a2afb611SJerry Jelinek 	VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
882a2afb611SJerry Jelinek 	    tx));
883a2afb611SJerry Jelinek 
884a2afb611SJerry Jelinek 	/* Roll up this additional count into our ancestors */
885a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
886a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
887a2afb611SJerry Jelinek }
888a2afb611SJerry Jelinek 
8891d452cf5Sahrens uint64_t
dsl_dir_create_sync(dsl_pool_t * dp,dsl_dir_t * pds,const char * name,dmu_tx_t * tx)890088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
891088f3894Sahrens     dmu_tx_t *tx)
892fa9e4066Sahrens {
893088f3894Sahrens 	objset_t *mos = dp->dp_meta_objset;
894fa9e4066Sahrens 	uint64_t ddobj;
895cde58dbcSMatthew Ahrens 	dsl_dir_phys_t *ddphys;
896fa9e4066Sahrens 	dmu_buf_t *dbuf;
897fa9e4066Sahrens 
8981649cd4bStabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
8991649cd4bStabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
900088f3894Sahrens 	if (pds) {
901c1379625SJustin T. Gibbs 		VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
9021d452cf5Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
903088f3894Sahrens 	} else {
904088f3894Sahrens 		/* it's the root dir */
905088f3894Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
906088f3894Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
907088f3894Sahrens 	}
908ea8dc4b6Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
909fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
910cde58dbcSMatthew Ahrens 	ddphys = dbuf->db_data;
911fa9e4066Sahrens 
912cde58dbcSMatthew Ahrens 	ddphys->dd_creation_time = gethrestime_sec();
913a2afb611SJerry Jelinek 	if (pds) {
914cde58dbcSMatthew Ahrens 		ddphys->dd_parent_obj = pds->dd_object;
915a2afb611SJerry Jelinek 
916a2afb611SJerry Jelinek 		/* update the filesystem counts */
917a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
918a2afb611SJerry Jelinek 	}
919cde58dbcSMatthew Ahrens 	ddphys->dd_props_zapobj = zap_create(mos,
920fa9e4066Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
921cde58dbcSMatthew Ahrens 	ddphys->dd_child_dir_zapobj = zap_create(mos,
92287e5029aSahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
92374e7dc98SMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
924cde58dbcSMatthew Ahrens 		ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
925ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
926fa9e4066Sahrens 
9271d452cf5Sahrens 	return (ddobj);
9281d452cf5Sahrens }
9291d452cf5Sahrens 
930088f3894Sahrens boolean_t
dsl_dir_is_clone(dsl_dir_t * dd)931088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
932fa9e4066Sahrens {
933c1379625SJustin T. Gibbs 	return (dsl_dir_phys(dd)->dd_origin_obj &&
934088f3894Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
935c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_origin_obj !=
936088f3894Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
937fa9e4066Sahrens }
938fa9e4066Sahrens 
939fa9e4066Sahrens void
dsl_dir_stats(dsl_dir_t * dd,nvlist_t * nv)940a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
941fa9e4066Sahrens {
942fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
94374e7dc98SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
944c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_bytes);
945c1379625SJustin T. Gibbs 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
946c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_quota);
947a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
948c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_reserved);
949a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
950c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
951c1379625SJustin T. Gibbs 	    (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
952c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes));
95377372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
954c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes);
955c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
95674e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
957c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
95874e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
959c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
96074e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
961c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
96274e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
963c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
964c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
96574e7dc98SMatthew Ahrens 	}
966fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
967fa9e4066Sahrens 
968a2afb611SJerry Jelinek 	if (dsl_dir_is_zapified(dd)) {
969a2afb611SJerry Jelinek 		uint64_t count;
970a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
971a2afb611SJerry Jelinek 
972a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
973a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
974a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
975a2afb611SJerry Jelinek 			    ZFS_PROP_FILESYSTEM_COUNT, count);
976a2afb611SJerry Jelinek 		}
977a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
978a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
979a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
980a2afb611SJerry Jelinek 			    ZFS_PROP_SNAPSHOT_COUNT, count);
981a2afb611SJerry Jelinek 		}
982a2afb611SJerry Jelinek 	}
983a2afb611SJerry Jelinek 
984088f3894Sahrens 	if (dsl_dir_is_clone(dd)) {
985fa9e4066Sahrens 		dsl_dataset_t *ds;
98640a5c998SMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
987fa9e4066Sahrens 
9883b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
989c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
990a2eea2e1Sahrens 		dsl_dataset_name(ds, buf);
991745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
992a2eea2e1Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
993fa9e4066Sahrens 	}
994fa9e4066Sahrens }
995fa9e4066Sahrens 
996fa9e4066Sahrens void
dsl_dir_dirty(dsl_dir_t * dd,dmu_tx_t * tx)997fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
998fa9e4066Sahrens {
999fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
1000fa9e4066Sahrens 
1001c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd));
1002fa9e4066Sahrens 
10033b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1004fa9e4066Sahrens 		/* up the hold count until we can be written out */
1005fa9e4066Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
1006fa9e4066Sahrens 	}
1007fa9e4066Sahrens }
1008fa9e4066Sahrens 
1009fa9e4066Sahrens static int64_t
parent_delta(dsl_dir_t * dd,uint64_t used,int64_t delta)1010fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1011fa9e4066Sahrens {
1012c1379625SJustin T. Gibbs 	uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1013c1379625SJustin T. Gibbs 	uint64_t new_accounted =
1014c1379625SJustin T. Gibbs 	    MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1015fa9e4066Sahrens 	return (new_accounted - old_accounted);
1016fa9e4066Sahrens }
1017fa9e4066Sahrens 
1018fa9e4066Sahrens void
dsl_dir_sync(dsl_dir_t * dd,dmu_tx_t * tx)1019fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1020fa9e4066Sahrens {
1021fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1022fa9e4066Sahrens 
1023fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1024fb09f5aaSMadhav Suresh 	ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1025fa9e4066Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1026fa9e4066Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1027fa9e4066Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1028fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	/* release the hold from dsl_dir_dirty */
1031ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
1032fa9e4066Sahrens }
1033fa9e4066Sahrens 
1034fa9e4066Sahrens static uint64_t
dsl_dir_space_towrite(dsl_dir_t * dd)1035a9799022Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd)
1036fa9e4066Sahrens {
1037a9799022Sck153898 	uint64_t space = 0;
1038fa9e4066Sahrens 	int i;
1039fa9e4066Sahrens 
1040fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
1041fa9e4066Sahrens 
1042fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1043fa9e4066Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
1044fa9e4066Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1045fa9e4066Sahrens 	}
1046fa9e4066Sahrens 	return (space);
1047fa9e4066Sahrens }
1048fa9e4066Sahrens 
1049fa9e4066Sahrens /*
1050fa9e4066Sahrens  * How much space would dd have available if ancestor had delta applied
1051fa9e4066Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
1052fa9e4066Sahrens  * on-disk, not estimated pending changes.
1053fa9e4066Sahrens  */
1054a2eea2e1Sahrens uint64_t
dsl_dir_space_available(dsl_dir_t * dd,dsl_dir_t * ancestor,int64_t delta,int ondiskonly)1055fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd,
1056fa9e4066Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1057fa9e4066Sahrens {
1058fa9e4066Sahrens 	uint64_t parentspace, myspace, quota, used;
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 	/*
1061fa9e4066Sahrens 	 * If there are no restrictions otherwise, assume we have
1062fa9e4066Sahrens 	 * unlimited space available.
1063fa9e4066Sahrens 	 */
1064fa9e4066Sahrens 	quota = UINT64_MAX;
1065fa9e4066Sahrens 	parentspace = UINT64_MAX;
1066fa9e4066Sahrens 
1067fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1068fa9e4066Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
1069fa9e4066Sahrens 		    ancestor, delta, ondiskonly);
1070fa9e4066Sahrens 	}
1071fa9e4066Sahrens 
1072fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1073c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_quota != 0)
1074c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1075c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1076a9799022Sck153898 	if (!ondiskonly)
1077a9799022Sck153898 		used += dsl_dir_space_towrite(dd);
1078fa9e4066Sahrens 
1079fa9e4066Sahrens 	if (dd->dd_parent == NULL) {
108099653d4eSeschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1081fa9e4066Sahrens 		quota = MIN(quota, poolsize);
1082fa9e4066Sahrens 	}
1083fa9e4066Sahrens 
1084c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1085fa9e4066Sahrens 		/*
1086fa9e4066Sahrens 		 * We have some space reserved, in addition to what our
1087fa9e4066Sahrens 		 * parent gave us.
1088fa9e4066Sahrens 		 */
1089c1379625SJustin T. Gibbs 		parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1090fa9e4066Sahrens 	}
1091fa9e4066Sahrens 
109274e7dc98SMatthew Ahrens 	if (dd == ancestor) {
109374e7dc98SMatthew Ahrens 		ASSERT(delta <= 0);
109474e7dc98SMatthew Ahrens 		ASSERT(used >= -delta);
109574e7dc98SMatthew Ahrens 		used += delta;
109674e7dc98SMatthew Ahrens 		if (parentspace != UINT64_MAX)
109774e7dc98SMatthew Ahrens 			parentspace -= delta;
109874e7dc98SMatthew Ahrens 	}
109974e7dc98SMatthew Ahrens 
1100fa9e4066Sahrens 	if (used > quota) {
1101fa9e4066Sahrens 		/* over quota */
1102fa9e4066Sahrens 		myspace = 0;
1103fa9e4066Sahrens 	} else {
1104fa9e4066Sahrens 		/*
110599653d4eSeschrock 		 * the lesser of the space provided by our parent and
110699653d4eSeschrock 		 * the space left in our quota
1107fa9e4066Sahrens 		 */
1108fa9e4066Sahrens 		myspace = MIN(parentspace, quota - used);
1109fa9e4066Sahrens 	}
1110fa9e4066Sahrens 
1111fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1112fa9e4066Sahrens 
1113fa9e4066Sahrens 	return (myspace);
1114fa9e4066Sahrens }
1115fa9e4066Sahrens 
1116fa9e4066Sahrens struct tempreserve {
1117fa9e4066Sahrens 	list_node_t tr_node;
1118fa9e4066Sahrens 	dsl_dir_t *tr_ds;
1119fa9e4066Sahrens 	uint64_t tr_size;
1120fa9e4066Sahrens };
1121fa9e4066Sahrens 
1122fa9e4066Sahrens static int
dsl_dir_tempreserve_impl(dsl_dir_t * dd,uint64_t asize,boolean_t netfree,boolean_t ignorequota,boolean_t checkrefquota,list_t * tr_list,dmu_tx_t * tx,boolean_t first)1123a9799022Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1124a9799022Sck153898     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
11256df6c3bcSck153898     dmu_tx_t *tx, boolean_t first)
1126fa9e4066Sahrens {
1127fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
1128a9799022Sck153898 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1129468c413aSTim Haley 	uint64_t deferred = 0;
1130a9799022Sck153898 	struct tempreserve *tr;
1131468c413aSTim Haley 	int retval = EDQUOT;
1132fa9e4066Sahrens 	int txgidx = txg & TXG_MASK;
1133fa9e4066Sahrens 	int i;
11349082849eSck153898 	uint64_t ref_rsrv = 0;
1135fa9e4066Sahrens 
1136fa9e4066Sahrens 	ASSERT3U(txg, !=, 0);
1137a9799022Sck153898 	ASSERT3S(asize, >, 0);
1138fa9e4066Sahrens 
1139fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1140a9799022Sck153898 
1141fa9e4066Sahrens 	/*
1142fa9e4066Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
1143fa9e4066Sahrens 	 * when checking for over-quota because they get one free hit.
1144fa9e4066Sahrens 	 */
1145a9799022Sck153898 	est_inflight = dsl_dir_space_towrite(dd);
1146fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++)
1147a9799022Sck153898 		est_inflight += dd->dd_tempreserved[i];
1148c1379625SJustin T. Gibbs 	used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1149fa9e4066Sahrens 
1150f4d2e9e6Smaybee 	/*
11516df6c3bcSck153898 	 * On the first iteration, fetch the dataset's used-on-disk and
11526df6c3bcSck153898 	 * refreservation values. Also, if checkrefquota is set, test if
11536df6c3bcSck153898 	 * allocating this space would exceed the dataset's refquota.
1154f4d2e9e6Smaybee 	 */
11556df6c3bcSck153898 	if (first && tx->tx_objset) {
1156c3fdb13aSmaybee 		int error;
1157503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
11589082849eSck153898 
1159a9799022Sck153898 		error = dsl_dataset_check_quota(ds, checkrefquota,
11609082849eSck153898 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
1161a9799022Sck153898 		if (error) {
1162a9799022Sck153898 			mutex_exit(&dd->dd_lock);
1163a9799022Sck153898 			return (error);
1164a9799022Sck153898 		}
1165a9799022Sck153898 	}
1166a9799022Sck153898 
1167a9799022Sck153898 	/*
1168a9799022Sck153898 	 * If this transaction will result in a net free of space,
1169a9799022Sck153898 	 * we want to let it through.
1170a9799022Sck153898 	 */
1171c1379625SJustin T. Gibbs 	if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1172fa9e4066Sahrens 		quota = UINT64_MAX;
1173f4d2e9e6Smaybee 	else
1174c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1175fa9e4066Sahrens 
1176fa9e4066Sahrens 	/*
1177468c413aSTim Haley 	 * Adjust the quota against the actual pool size at the root
1178468c413aSTim Haley 	 * minus any outstanding deferred frees.
1179f4d2e9e6Smaybee 	 * To ensure that it's possible to remove files from a full
1180f4d2e9e6Smaybee 	 * pool without inducing transient overcommits, we throttle
1181fa9e4066Sahrens 	 * netfree transactions against a quota that is slightly larger,
1182fa9e4066Sahrens 	 * but still within the pool's allocation slop.  In cases where
1183fa9e4066Sahrens 	 * we're very close to full, this will allow a steady trickle of
1184fa9e4066Sahrens 	 * removes to get through.
1185fa9e4066Sahrens 	 */
11861934e92fSmaybee 	if (dd->dd_parent == NULL) {
1187b24ab676SJeff Bonwick 		spa_t *spa = dd->dd_pool->dp_spa;
1188fa9e4066Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1189b24ab676SJeff Bonwick 		deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1190468c413aSTim Haley 		if (poolsize - deferred < quota) {
1191468c413aSTim Haley 			quota = poolsize - deferred;
1192468c413aSTim Haley 			retval = ENOSPC;
1193fa9e4066Sahrens 		}
1194fa9e4066Sahrens 	}
1195fa9e4066Sahrens 
1196fa9e4066Sahrens 	/*
1197fa9e4066Sahrens 	 * If they are requesting more space, and our current estimate
1198a9799022Sck153898 	 * is over quota, they get to try again unless the actual
1199ea8dc4b6Seschrock 	 * on-disk is over quota and there are no pending changes (which
1200ea8dc4b6Seschrock 	 * may free up space for us).
1201fa9e4066Sahrens 	 */
1202468c413aSTim Haley 	if (used_on_disk + est_inflight >= quota) {
1203b9f08e63SArne Jansen 		if (est_inflight > early_edquot_threshold ||
1204b9f08e63SArne Jansen 		    used_on_disk + early_edquot_threshold < quota ||
1205468c413aSTim Haley 		    (retval == ENOSPC && used_on_disk < quota + deferred))
1206468c413aSTim Haley 			retval = ERESTART;
1207a9799022Sck153898 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1208fa9e4066Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
1209a9799022Sck153898 		    used_on_disk>>10, est_inflight>>10,
1210468c413aSTim Haley 		    quota>>10, asize>>10, retval);
1211fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
1212be6fd75aSMatthew Ahrens 		return (SET_ERROR(retval));
1213fa9e4066Sahrens 	}
1214fa9e4066Sahrens 
1215fa9e4066Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
1216fa9e4066Sahrens 	dd->dd_tempreserved[txgidx] += asize;
1217fa9e4066Sahrens 
12189082849eSck153898 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
12199082849eSck153898 	    asize - ref_rsrv);
1220fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1221fa9e4066Sahrens 
12221ab7f2deSmaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1223fa9e4066Sahrens 	tr->tr_ds = dd;
1224fa9e4066Sahrens 	tr->tr_size = asize;
1225fa9e4066Sahrens 	list_insert_tail(tr_list, tr);
1226fa9e4066Sahrens 
1227fa9e4066Sahrens 	/* see if it's OK with our parent */
12281934e92fSmaybee 	if (dd->dd_parent && parent_rsrv) {
1229c1379625SJustin T. Gibbs 		boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
12301934e92fSmaybee 
1231fa9e4066Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
12326df6c3bcSck153898 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1233fa9e4066Sahrens 	} else {
1234fa9e4066Sahrens 		return (0);
1235fa9e4066Sahrens 	}
1236fa9e4066Sahrens }
1237fa9e4066Sahrens 
1238fa9e4066Sahrens /*
1239fa9e4066Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
1240a9799022Sck153898  * After the space has been dirtied (and dsl_dir_willuse_space()
1241a9799022Sck153898  * has been called), the reservation should be canceled, using
1242a9799022Sck153898  * dsl_dir_tempreserve_clear().
1243fa9e4066Sahrens  */
1244fa9e4066Sahrens int
dsl_dir_tempreserve_space(dsl_dir_t * dd,uint64_t lsize,uint64_t asize,uint64_t fsize,uint64_t usize,void ** tr_cookiep,dmu_tx_t * tx)1245a9799022Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1246a9799022Sck153898     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1247fa9e4066Sahrens {
12481ab7f2deSmaybee 	int err;
1249fa9e4066Sahrens 	list_t *tr_list;
1250fa9e4066Sahrens 
1251a9799022Sck153898 	if (asize == 0) {
1252a9799022Sck153898 		*tr_cookiep = NULL;
1253a9799022Sck153898 		return (0);
1254a9799022Sck153898 	}
1255a9799022Sck153898 
1256fa9e4066Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1257fa9e4066Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
1258fa9e4066Sahrens 	    offsetof(struct tempreserve, tr_node));
1259a9799022Sck153898 	ASSERT3S(asize, >, 0);
1260ea8dc4b6Seschrock 	ASSERT3S(fsize, >=, 0);
1261fa9e4066Sahrens 
12621ab7f2deSmaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
12631ab7f2deSmaybee 	if (err == 0) {
12641ab7f2deSmaybee 		struct tempreserve *tr;
12651ab7f2deSmaybee 
12661ab7f2deSmaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
12671ab7f2deSmaybee 		tr->tr_size = lsize;
12681ab7f2deSmaybee 		list_insert_tail(tr_list, tr);
12691ab7f2deSmaybee 	} else {
12701ab7f2deSmaybee 		if (err == EAGAIN) {
127169962b56SMatthew Ahrens 			/*
127269962b56SMatthew Ahrens 			 * If arc_memory_throttle() detected that pageout
127369962b56SMatthew Ahrens 			 * is running and we are low on memory, we delay new
127469962b56SMatthew Ahrens 			 * non-pageout transactions to give pageout an
127569962b56SMatthew Ahrens 			 * advantage.
127669962b56SMatthew Ahrens 			 *
127769962b56SMatthew Ahrens 			 * It is unfortunate to be delaying while the caller's
127869962b56SMatthew Ahrens 			 * locks are held.
127969962b56SMatthew Ahrens 			 */
12800689f76cSAdam Leventhal 			txg_delay(dd->dd_pool, tx->tx_txg,
12810689f76cSAdam Leventhal 			    MSEC2NSEC(10), MSEC2NSEC(10));
1282be6fd75aSMatthew Ahrens 			err = SET_ERROR(ERESTART);
12831ab7f2deSmaybee 		}
12841ab7f2deSmaybee 	}
1285fa9e4066Sahrens 
1286fa9e4066Sahrens 	if (err == 0) {
12871ab7f2deSmaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
12886df6c3bcSck153898 		    FALSE, asize > usize, tr_list, tx, TRUE);
1289fa9e4066Sahrens 	}
1290fa9e4066Sahrens 
12913b2aab18SMatthew Ahrens 	if (err != 0)
1292fa9e4066Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
1293fa9e4066Sahrens 	else
1294fa9e4066Sahrens 		*tr_cookiep = tr_list;
12951ab7f2deSmaybee 
1296fa9e4066Sahrens 	return (err);
1297fa9e4066Sahrens }
1298fa9e4066Sahrens 
1299fa9e4066Sahrens /*
1300fa9e4066Sahrens  * Clear a temporary reservation that we previously made with
1301fa9e4066Sahrens  * dsl_dir_tempreserve_space().
1302fa9e4066Sahrens  */
1303fa9e4066Sahrens void
dsl_dir_tempreserve_clear(void * tr_cookie,dmu_tx_t * tx)1304fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1305fa9e4066Sahrens {
1306fa9e4066Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
1307fa9e4066Sahrens 	list_t *tr_list = tr_cookie;
1308fa9e4066Sahrens 	struct tempreserve *tr;
1309fa9e4066Sahrens 
1310fa9e4066Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
1311fa9e4066Sahrens 
1312a9799022Sck153898 	if (tr_cookie == NULL)
1313a9799022Sck153898 		return;
1314a9799022Sck153898 
131569962b56SMatthew Ahrens 	while ((tr = list_head(tr_list)) != NULL) {
131669962b56SMatthew Ahrens 		if (tr->tr_ds) {
1317fa9e4066Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
1318fa9e4066Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1319fa9e4066Sahrens 			    tr->tr_size);
1320fa9e4066Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1321fa9e4066Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
13221ab7f2deSmaybee 		} else {
13231ab7f2deSmaybee 			arc_tempreserve_clear(tr->tr_size);
1324fa9e4066Sahrens 		}
1325fa9e4066Sahrens 		list_remove(tr_list, tr);
1326fa9e4066Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
1327fa9e4066Sahrens 	}
1328fa9e4066Sahrens 
1329fa9e4066Sahrens 	kmem_free(tr_list, sizeof (list_t));
1330fa9e4066Sahrens }
1331fa9e4066Sahrens 
133269962b56SMatthew Ahrens /*
133369962b56SMatthew Ahrens  * This should be called from open context when we think we're going to write
133469962b56SMatthew Ahrens  * or free space, for example when dirtying data. Be conservative; it's okay
133569962b56SMatthew Ahrens  * to write less space or free more, but we don't want to write more or free
133669962b56SMatthew Ahrens  * less than the amount specified.
133769962b56SMatthew Ahrens  */
133869962b56SMatthew Ahrens void
dsl_dir_willuse_space(dsl_dir_t * dd,int64_t space,dmu_tx_t * tx)133969962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1340fa9e4066Sahrens {
1341fa9e4066Sahrens 	int64_t parent_space;
1342fa9e4066Sahrens 	uint64_t est_used;
1343fa9e4066Sahrens 
1344fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1345fa9e4066Sahrens 	if (space > 0)
1346fa9e4066Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1347fa9e4066Sahrens 
1348c1379625SJustin T. Gibbs 	est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1349fa9e4066Sahrens 	parent_space = parent_delta(dd, est_used, space);
1350fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	/* Make sure that we clean up dd_space_to* */
1353fa9e4066Sahrens 	dsl_dir_dirty(dd, tx);
1354fa9e4066Sahrens 
1355fa9e4066Sahrens 	/* XXX this is potentially expensive and unnecessary... */
1356fa9e4066Sahrens 	if (parent_space && dd->dd_parent)
135769962b56SMatthew Ahrens 		dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1358fa9e4066Sahrens }
1359fa9e4066Sahrens 
1360fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */
1361fa9e4066Sahrens void
dsl_dir_diduse_space(dsl_dir_t * dd,dd_used_t type,int64_t used,int64_t compressed,int64_t uncompressed,dmu_tx_t * tx)136274e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1363fa9e4066Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1364fa9e4066Sahrens {
1365fa9e4066Sahrens 	int64_t accounted_delta;
1366b62969f8SMatthew Ahrens 
1367b62969f8SMatthew Ahrens 	/*
1368b62969f8SMatthew Ahrens 	 * dsl_dataset_set_refreservation_sync_impl() calls this with
1369b62969f8SMatthew Ahrens 	 * dd_lock held, so that it can atomically update
1370b62969f8SMatthew Ahrens 	 * ds->ds_reserved and the dsl_dir accounting, so that
1371b62969f8SMatthew Ahrens 	 * dsl_dataset_check_quota() can see dataset and dir accounting
1372b62969f8SMatthew Ahrens 	 * consistently.
1373b62969f8SMatthew Ahrens 	 */
137402c8f3f0SMatthew Ahrens 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1375fa9e4066Sahrens 
1376fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
137774e7dc98SMatthew Ahrens 	ASSERT(type < DD_USED_NUM);
1378fa9e4066Sahrens 
1379b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1380b62969f8SMatthew Ahrens 
138102c8f3f0SMatthew Ahrens 	if (needlock)
1382fa9e4066Sahrens 		mutex_enter(&dd->dd_lock);
1383c1379625SJustin T. Gibbs 	accounted_delta =
1384c1379625SJustin T. Gibbs 	    parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1385c1379625SJustin T. Gibbs 	ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1386fa9e4066Sahrens 	ASSERT(compressed >= 0 ||
1387c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1388fa9e4066Sahrens 	ASSERT(uncompressed >= 0 ||
1389c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1390c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_bytes += used;
1391c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1392c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
139374e7dc98SMatthew Ahrens 
1394c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
139574e7dc98SMatthew Ahrens 		ASSERT(used > 0 ||
1396c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1397c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
139874e7dc98SMatthew Ahrens #ifdef DEBUG
139974e7dc98SMatthew Ahrens 		dd_used_t t;
140074e7dc98SMatthew Ahrens 		uint64_t u = 0;
140174e7dc98SMatthew Ahrens 		for (t = 0; t < DD_USED_NUM; t++)
1402c1379625SJustin T. Gibbs 			u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1403c1379625SJustin T. Gibbs 		ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
140474e7dc98SMatthew Ahrens #endif
140574e7dc98SMatthew Ahrens 	}
140602c8f3f0SMatthew Ahrens 	if (needlock)
1407fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
1408fa9e4066Sahrens 
1409fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
141074e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1411fa9e4066Sahrens 		    accounted_delta, compressed, uncompressed, tx);
141274e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(dd->dd_parent,
141374e7dc98SMatthew Ahrens 		    used - accounted_delta,
141474e7dc98SMatthew Ahrens 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1415fa9e4066Sahrens 	}
1416fa9e4066Sahrens }
1417fa9e4066Sahrens 
141874e7dc98SMatthew Ahrens void
dsl_dir_transfer_space(dsl_dir_t * dd,int64_t delta,dd_used_t oldtype,dd_used_t newtype,dmu_tx_t * tx)141974e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
142074e7dc98SMatthew Ahrens     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
142174e7dc98SMatthew Ahrens {
142274e7dc98SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
142374e7dc98SMatthew Ahrens 	ASSERT(oldtype < DD_USED_NUM);
142474e7dc98SMatthew Ahrens 	ASSERT(newtype < DD_USED_NUM);
142574e7dc98SMatthew Ahrens 
1426c1379625SJustin T. Gibbs 	if (delta == 0 ||
1427c1379625SJustin T. Gibbs 	    !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
142874e7dc98SMatthew Ahrens 		return;
142974e7dc98SMatthew Ahrens 
1430b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
143174e7dc98SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
143274e7dc98SMatthew Ahrens 	ASSERT(delta > 0 ?
1433c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1434c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1435c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1436c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1437c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
143874e7dc98SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
143974e7dc98SMatthew Ahrens }
144074e7dc98SMatthew Ahrens 
14413b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg {
14423b2aab18SMatthew Ahrens 	const char *ddsqra_name;
14433b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
14443b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
14453b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t;
14463b2aab18SMatthew Ahrens 
1447fa9e4066Sahrens static int
dsl_dir_set_quota_check(void * arg,dmu_tx_t * tx)14483b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1449fa9e4066Sahrens {
14503b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14513b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14523b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14533b2aab18SMatthew Ahrens 	int error;
14543b2aab18SMatthew Ahrens 	uint64_t towrite, newval;
14551d452cf5Sahrens 
14563b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
14573b2aab18SMatthew Ahrens 	if (error != 0)
14583b2aab18SMatthew Ahrens 		return (error);
145992241e0bSTom Erickson 
14603b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir, "quota",
14613b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
14623b2aab18SMatthew Ahrens 	if (error != 0) {
14633b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14643b2aab18SMatthew Ahrens 		return (error);
14653b2aab18SMatthew Ahrens 	}
14663b2aab18SMatthew Ahrens 
14673b2aab18SMatthew Ahrens 	if (newval == 0) {
14683b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14691d452cf5Sahrens 		return (0);
14703b2aab18SMatthew Ahrens 	}
14711d452cf5Sahrens 
14723b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
14731d452cf5Sahrens 	/*
14741d452cf5Sahrens 	 * If we are doing the preliminary check in open context, and
14751d452cf5Sahrens 	 * there are pending changes, then don't fail it, since the
1476a9799022Sck153898 	 * pending changes could under-estimate the amount of space to be
14771d452cf5Sahrens 	 * freed up.
14781d452cf5Sahrens 	 */
14793b2aab18SMatthew Ahrens 	towrite = dsl_dir_space_towrite(ds->ds_dir);
14801d452cf5Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1481c1379625SJustin T. Gibbs 	    (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1482c1379625SJustin T. Gibbs 	    newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1483be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOSPC);
14841d452cf5Sahrens 	}
14853b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
14863b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14873b2aab18SMatthew Ahrens 	return (error);
14881d452cf5Sahrens }
14891d452cf5Sahrens 
14901d452cf5Sahrens static void
dsl_dir_set_quota_sync(void * arg,dmu_tx_t * tx)14913b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
14921d452cf5Sahrens {
14933b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14943b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14953b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14963b2aab18SMatthew Ahrens 	uint64_t newval;
149792241e0bSTom Erickson 
14983b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1499fa9e4066Sahrens 
1500013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
15013b2aab18SMatthew Ahrens 		dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
15023b2aab18SMatthew Ahrens 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
15033b2aab18SMatthew Ahrens 		    &ddsqra->ddsqra_value, tx);
1504fa9e4066Sahrens 
15053b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_get_int_ds(ds,
15063b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1507013023d4SMartin Matuska 	} else {
1508013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1509013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1510013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1511013023d4SMartin Matuska 	}
15123b2aab18SMatthew Ahrens 
15133b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
15143b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
1515c1379625SJustin T. Gibbs 	dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
15163b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
15173b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1518fa9e4066Sahrens }
1519fa9e4066Sahrens 
1520fa9e4066Sahrens int
dsl_dir_set_quota(const char * ddname,zprop_source_t source,uint64_t quota)152192241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1522fa9e4066Sahrens {
15233b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
1524fa9e4066Sahrens 
15253b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
15263b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
15273b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = quota;
152892241e0bSTom Erickson 
15293b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
15307d46dc6cSMatthew Ahrens 	    dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1531fa9e4066Sahrens }
1532fa9e4066Sahrens 
1533a9799022Sck153898 int
dsl_dir_set_reservation_check(void * arg,dmu_tx_t * tx)15343b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1535fa9e4066Sahrens {
15363b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
15373b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15383b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15393b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15403b2aab18SMatthew Ahrens 	uint64_t newval, used, avail;
15413b2aab18SMatthew Ahrens 	int error;
154292241e0bSTom Erickson 
15433b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
15443b2aab18SMatthew Ahrens 	if (error != 0)
15453b2aab18SMatthew Ahrens 		return (error);
15463b2aab18SMatthew Ahrens 	dd = ds->ds_dir;
1547fa9e4066Sahrens 
15481d452cf5Sahrens 	/*
15491d452cf5Sahrens 	 * If we are doing the preliminary check in open context, the
15501d452cf5Sahrens 	 * space estimates may be inaccurate.
15511d452cf5Sahrens 	 */
15523b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
15533b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15541d452cf5Sahrens 		return (0);
15553b2aab18SMatthew Ahrens 	}
15563b2aab18SMatthew Ahrens 
15573b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
15583b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_RESERVATION),
15593b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
15603b2aab18SMatthew Ahrens 	if (error != 0) {
15613b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15623b2aab18SMatthew Ahrens 		return (error);
15633b2aab18SMatthew Ahrens 	}
15641d452cf5Sahrens 
1565fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1566c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1567fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1568fa9e4066Sahrens 
1569fa9e4066Sahrens 	if (dd->dd_parent) {
1570fa9e4066Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1571fa9e4066Sahrens 		    NULL, 0, FALSE);
1572fa9e4066Sahrens 	} else {
1573fa9e4066Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1574fa9e4066Sahrens 	}
1575fa9e4066Sahrens 
1576c1379625SJustin T. Gibbs 	if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
15773b2aab18SMatthew Ahrens 		uint64_t delta = MAX(used, newval) -
1578c1379625SJustin T. Gibbs 		    MAX(used, dsl_dir_phys(dd)->dd_reserved);
1579379c004dSEric Schrock 
15803b2aab18SMatthew Ahrens 		if (delta > avail ||
1581c1379625SJustin T. Gibbs 		    (dsl_dir_phys(dd)->dd_quota > 0 &&
1582c1379625SJustin T. Gibbs 		    newval > dsl_dir_phys(dd)->dd_quota))
1583be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENOSPC);
1584379c004dSEric Schrock 	}
1585379c004dSEric Schrock 
15863b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15873b2aab18SMatthew Ahrens 	return (error);
15881d452cf5Sahrens }
15891d452cf5Sahrens 
15903b2aab18SMatthew Ahrens void
dsl_dir_set_reservation_sync_impl(dsl_dir_t * dd,uint64_t value,dmu_tx_t * tx)15914445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
15921d452cf5Sahrens {
15931d452cf5Sahrens 	uint64_t used;
15941d452cf5Sahrens 	int64_t delta;
15951d452cf5Sahrens 
1596a9799022Sck153898 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1597a9799022Sck153898 
15981d452cf5Sahrens 	mutex_enter(&dd->dd_lock);
1599c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1600c1379625SJustin T. Gibbs 	delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1601c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_reserved = value;
1602fa9e4066Sahrens 
1603fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1604fa9e4066Sahrens 		/* Roll up this additional usage into our ancestors */
160574e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
160674e7dc98SMatthew Ahrens 		    delta, 0, 0, tx);
1607fa9e4066Sahrens 	}
160802c8f3f0SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
16094445fffbSMatthew Ahrens }
1610ecd6cf80Smarks 
16114445fffbSMatthew Ahrens 
16124445fffbSMatthew Ahrens static void
dsl_dir_set_reservation_sync(void * arg,dmu_tx_t * tx)16133b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
16144445fffbSMatthew Ahrens {
16153b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
16163b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16173b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16183b2aab18SMatthew Ahrens 	uint64_t newval;
16194445fffbSMatthew Ahrens 
16203b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
16214445fffbSMatthew Ahrens 
1622013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1623013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds,
1624013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
16253b2aab18SMatthew Ahrens 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
16263b2aab18SMatthew Ahrens 		    &ddsqra->ddsqra_value, tx);
16273b2aab18SMatthew Ahrens 
16283b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_get_int_ds(ds,
16293b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1630013023d4SMartin Matuska 	} else {
1631013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1632013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1633013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1634013023d4SMartin Matuska 		    (longlong_t)newval);
1635013023d4SMartin Matuska 	}
16363b2aab18SMatthew Ahrens 
16373b2aab18SMatthew Ahrens 	dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
16383b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1639fa9e4066Sahrens }
1640fa9e4066Sahrens 
1641fa9e4066Sahrens int
dsl_dir_set_reservation(const char * ddname,zprop_source_t source,uint64_t reservation)164292241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
164392241e0bSTom Erickson     uint64_t reservation)
1644fa9e4066Sahrens {
16453b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
1646fa9e4066Sahrens 
16473b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
16483b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
16493b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = reservation;
165092241e0bSTom Erickson 
16513b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
16527d46dc6cSMatthew Ahrens 	    dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1653fa9e4066Sahrens }
1654fa9e4066Sahrens 
1655fa9e4066Sahrens static dsl_dir_t *
closest_common_ancestor(dsl_dir_t * ds1,dsl_dir_t * ds2)1656fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1657fa9e4066Sahrens {
1658fa9e4066Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1659fa9e4066Sahrens 		dsl_dir_t *dd;
1660fa9e4066Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1661fa9e4066Sahrens 			if (ds1 == dd)
1662fa9e4066Sahrens 				return (dd);
1663fa9e4066Sahrens 		}
1664fa9e4066Sahrens 	}
1665fa9e4066Sahrens 	return (NULL);
1666fa9e4066Sahrens }
1667fa9e4066Sahrens 
1668fa9e4066Sahrens /*
1669fa9e4066Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1670fa9e4066Sahrens  * ancestor?  Syncing context only.
1671fa9e4066Sahrens  */
1672fa9e4066Sahrens static int64_t
would_change(dsl_dir_t * dd,int64_t delta,dsl_dir_t * ancestor)1673fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1674fa9e4066Sahrens {
1675fa9e4066Sahrens 	if (dd == ancestor)
1676fa9e4066Sahrens 		return (delta);
1677fa9e4066Sahrens 
1678fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1679c1379625SJustin T. Gibbs 	delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1680fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1681fa9e4066Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1682fa9e4066Sahrens }
1683fa9e4066Sahrens 
16843b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg {
16853b2aab18SMatthew Ahrens 	const char *ddra_oldname;
16863b2aab18SMatthew Ahrens 	const char *ddra_newname;
1687a2afb611SJerry Jelinek 	cred_t *ddra_cred;
16883b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t;
16893b2aab18SMatthew Ahrens 
16903b2aab18SMatthew Ahrens /* ARGSUSED */
16913b2aab18SMatthew Ahrens static int
dsl_valid_rename(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)16923b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
16933b2aab18SMatthew Ahrens {
16943b2aab18SMatthew Ahrens 	int *deltap = arg;
169540a5c998SMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
16963b2aab18SMatthew Ahrens 
16973b2aab18SMatthew Ahrens 	dsl_dataset_name(ds, namebuf);
16983b2aab18SMatthew Ahrens 
169940a5c998SMatthew Ahrens 	if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN)
1700be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
17013b2aab18SMatthew Ahrens 	return (0);
17023b2aab18SMatthew Ahrens }
17031d452cf5Sahrens 
17041d452cf5Sahrens static int
dsl_dir_rename_check(void * arg,dmu_tx_t * tx)17053b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1706fa9e4066Sahrens {
17073b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
17083b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17093b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
17103b2aab18SMatthew Ahrens 	const char *mynewname;
17113b2aab18SMatthew Ahrens 	int error;
17123b2aab18SMatthew Ahrens 	int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1713fa9e4066Sahrens 
17143b2aab18SMatthew Ahrens 	/* target dir should exist */
17153b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
17163b2aab18SMatthew Ahrens 	if (error != 0)
17173b2aab18SMatthew Ahrens 		return (error);
1718fa9e4066Sahrens 
17193b2aab18SMatthew Ahrens 	/* new parent should exist */
17203b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
17213b2aab18SMatthew Ahrens 	    &newparent, &mynewname);
17223b2aab18SMatthew Ahrens 	if (error != 0) {
17233b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
17243b2aab18SMatthew Ahrens 		return (error);
17253b2aab18SMatthew Ahrens 	}
17263b2aab18SMatthew Ahrens 
17273b2aab18SMatthew Ahrens 	/* can't rename to different pool */
17283b2aab18SMatthew Ahrens 	if (dd->dd_pool != newparent->dd_pool) {
17293b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17303b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1731be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
17323b2aab18SMatthew Ahrens 	}
17333b2aab18SMatthew Ahrens 
17343b2aab18SMatthew Ahrens 	/* new name should not already exist */
17353b2aab18SMatthew Ahrens 	if (mynewname == NULL) {
17363b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17373b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1738be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
17393b2aab18SMatthew Ahrens 	}
17401d452cf5Sahrens 
17413b2aab18SMatthew Ahrens 	/* if the name length is growing, validate child name lengths */
17423b2aab18SMatthew Ahrens 	if (delta > 0) {
17433b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
17443b2aab18SMatthew Ahrens 		    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
17453b2aab18SMatthew Ahrens 		if (error != 0) {
17463b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17473b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17483b2aab18SMatthew Ahrens 			return (error);
17493b2aab18SMatthew Ahrens 		}
17503b2aab18SMatthew Ahrens 	}
17513b2aab18SMatthew Ahrens 
1752a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1753adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1754a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1755a2afb611SJerry Jelinek 			/*
1756a2afb611SJerry Jelinek 			 * Although this is the check function and we don't
1757a2afb611SJerry Jelinek 			 * normally make on-disk changes in check functions,
1758a2afb611SJerry Jelinek 			 * we need to do that here.
1759a2afb611SJerry Jelinek 			 *
1760a2afb611SJerry Jelinek 			 * Ensure this portion of the tree's counts have been
1761a2afb611SJerry Jelinek 			 * initialized in case the new parent has limits set.
1762a2afb611SJerry Jelinek 			 */
1763a2afb611SJerry Jelinek 			dsl_dir_init_fs_ss_count(dd, tx);
1764a2afb611SJerry Jelinek 		}
1765a2afb611SJerry Jelinek 	}
1766a2afb611SJerry Jelinek 
17673b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
176899653d4eSeschrock 		/* is there enough space? */
176999653d4eSeschrock 		uint64_t myspace =
1770c1379625SJustin T. Gibbs 		    MAX(dsl_dir_phys(dd)->dd_used_bytes,
1771c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_reserved);
1772a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1773a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1774a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1775a2afb611SJerry Jelinek 
1776a2afb611SJerry Jelinek 		if (dsl_dir_is_zapified(dd)) {
1777a2afb611SJerry Jelinek 			int err;
1778a2afb611SJerry Jelinek 
1779a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1780a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1781a2afb611SJerry Jelinek 			    &fs_cnt);
1782adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1783adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1784adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1785a2afb611SJerry Jelinek 				return (err);
1786adf34077SJerry Jelinek 			}
1787a2afb611SJerry Jelinek 
1788a2afb611SJerry Jelinek 			/*
1789a2afb611SJerry Jelinek 			 * have to add 1 for the filesystem itself that we're
1790a2afb611SJerry Jelinek 			 * moving
1791a2afb611SJerry Jelinek 			 */
1792a2afb611SJerry Jelinek 			fs_cnt++;
1793a2afb611SJerry Jelinek 
1794a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1795a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1796a2afb611SJerry Jelinek 			    &ss_cnt);
1797adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1798adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1799adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1800a2afb611SJerry Jelinek 				return (err);
1801a2afb611SJerry Jelinek 			}
1802adf34077SJerry Jelinek 		}
1803fa9e4066Sahrens 
18041d452cf5Sahrens 		/* no rename into our descendant */
18053b2aab18SMatthew Ahrens 		if (closest_common_ancestor(dd, newparent) == dd) {
18063b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
18073b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
1808be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1809fa9e4066Sahrens 		}
1810fa9e4066Sahrens 
18113b2aab18SMatthew Ahrens 		error = dsl_dir_transfer_possible(dd->dd_parent,
1812a2afb611SJerry Jelinek 		    newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
18133b2aab18SMatthew Ahrens 		if (error != 0) {
18143b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
18153b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
18163b2aab18SMatthew Ahrens 			return (error);
18173b2aab18SMatthew Ahrens 		}
18183b2aab18SMatthew Ahrens 	}
18193b2aab18SMatthew Ahrens 
18203b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
18213b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
18221d452cf5Sahrens 	return (0);
18231d452cf5Sahrens }
18241d452cf5Sahrens 
18251d452cf5Sahrens static void
dsl_dir_rename_sync(void * arg,dmu_tx_t * tx)18263b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
18271d452cf5Sahrens {
18283b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
18293b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18303b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
18313b2aab18SMatthew Ahrens 	const char *mynewname;
18323b2aab18SMatthew Ahrens 	int error;
18331d452cf5Sahrens 	objset_t *mos = dp->dp_meta_objset;
18341d452cf5Sahrens 
18353b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
18363b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
18373b2aab18SMatthew Ahrens 	    &mynewname));
18381d452cf5Sahrens 
18394445fffbSMatthew Ahrens 	/* Log this before we change the name. */
18404445fffbSMatthew Ahrens 	spa_history_log_internal_dd(dd, "rename", tx,
18413b2aab18SMatthew Ahrens 	    "-> %s", ddra->ddra_newname);
18424445fffbSMatthew Ahrens 
18433b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
1844a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1845a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1846a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1847a2afb611SJerry Jelinek 
1848a2afb611SJerry Jelinek 		/*
1849a2afb611SJerry Jelinek 		 * We already made sure the dd counts were initialized in the
1850a2afb611SJerry Jelinek 		 * check function.
1851a2afb611SJerry Jelinek 		 */
1852adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1853a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1854a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1855a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1856a2afb611SJerry Jelinek 			    &fs_cnt));
1857a2afb611SJerry Jelinek 			/* add 1 for the filesystem itself that we're moving */
1858a2afb611SJerry Jelinek 			fs_cnt++;
1859a2afb611SJerry Jelinek 
1860a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1861a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1862a2afb611SJerry Jelinek 			    &ss_cnt));
1863a2afb611SJerry Jelinek 		}
1864a2afb611SJerry Jelinek 
1865a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1866a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1867a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, fs_cnt,
1868a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1869a2afb611SJerry Jelinek 
1870a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1871a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1872a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, ss_cnt,
1873a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1874a2afb611SJerry Jelinek 
187574e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1876c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_used_bytes,
1877c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_compressed_bytes,
1878c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
18793b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1880c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes,
1881c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_compressed_bytes,
1882c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
188374e7dc98SMatthew Ahrens 
1884c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_reserved >
1885c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes) {
1886c1379625SJustin T. Gibbs 			uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1887c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_used_bytes;
188874e7dc98SMatthew Ahrens 
188974e7dc98SMatthew Ahrens 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
189074e7dc98SMatthew Ahrens 			    -unused_rsrv, 0, 0, tx);
18913b2aab18SMatthew Ahrens 			dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
189274e7dc98SMatthew Ahrens 			    unused_rsrv, 0, 0, tx);
189374e7dc98SMatthew Ahrens 		}
1894fa9e4066Sahrens 	}
1895fa9e4066Sahrens 
1896fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1897fa9e4066Sahrens 
1898fa9e4066Sahrens 	/* remove from old parent zapobj */
1899c1379625SJustin T. Gibbs 	error = zap_remove(mos,
1900c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1901fa9e4066Sahrens 	    dd->dd_myname, tx);
19023b2aab18SMatthew Ahrens 	ASSERT0(error);
1903fa9e4066Sahrens 
19043b2aab18SMatthew Ahrens 	(void) strcpy(dd->dd_myname, mynewname);
19053b2aab18SMatthew Ahrens 	dsl_dir_rele(dd->dd_parent, dd);
1906c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
19073b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp,
19083b2aab18SMatthew Ahrens 	    newparent->dd_object, NULL, dd, &dd->dd_parent));
1909fa9e4066Sahrens 
1910fa9e4066Sahrens 	/* add to new parent zapobj */
1911c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
19123b2aab18SMatthew Ahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx));
1913ecd6cf80Smarks 
19143b2aab18SMatthew Ahrens 	dsl_prop_notify_all(dd);
19153b2aab18SMatthew Ahrens 
19163b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
19173b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
19181d452cf5Sahrens }
1919fa9e4066Sahrens 
19201d452cf5Sahrens int
dsl_dir_rename(const char * oldname,const char * newname)19213b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname)
19221d452cf5Sahrens {
19233b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t ddra;
19241d452cf5Sahrens 
19253b2aab18SMatthew Ahrens 	ddra.ddra_oldname = oldname;
19263b2aab18SMatthew Ahrens 	ddra.ddra_newname = newname;
1927a2afb611SJerry Jelinek 	ddra.ddra_cred = CRED();
19281d452cf5Sahrens 
19293b2aab18SMatthew Ahrens 	return (dsl_sync_task(oldname,
19307d46dc6cSMatthew Ahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
19317d46dc6cSMatthew Ahrens 	    3, ZFS_SPACE_CHECK_RESERVED));
1932fa9e4066Sahrens }
193399653d4eSeschrock 
193499653d4eSeschrock int
dsl_dir_transfer_possible(dsl_dir_t * sdd,dsl_dir_t * tdd,uint64_t fs_cnt,uint64_t ss_cnt,uint64_t space,cred_t * cr)1935a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1936a2afb611SJerry Jelinek     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
193799653d4eSeschrock {
193899653d4eSeschrock 	dsl_dir_t *ancestor;
193999653d4eSeschrock 	int64_t adelta;
194099653d4eSeschrock 	uint64_t avail;
1941a2afb611SJerry Jelinek 	int err;
194299653d4eSeschrock 
194399653d4eSeschrock 	ancestor = closest_common_ancestor(sdd, tdd);
194499653d4eSeschrock 	adelta = would_change(sdd, -space, ancestor);
194599653d4eSeschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
194699653d4eSeschrock 	if (avail < space)
1947be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
194899653d4eSeschrock 
1949a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1950a2afb611SJerry Jelinek 	    ancestor, cr);
1951a2afb611SJerry Jelinek 	if (err != 0)
1952a2afb611SJerry Jelinek 		return (err);
1953a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1954a2afb611SJerry Jelinek 	    ancestor, cr);
1955a2afb611SJerry Jelinek 	if (err != 0)
1956a2afb611SJerry Jelinek 		return (err);
1957a2afb611SJerry Jelinek 
195899653d4eSeschrock 	return (0);
195999653d4eSeschrock }
196071eb0538SChris Kirby 
196171eb0538SChris Kirby timestruc_t
dsl_dir_snap_cmtime(dsl_dir_t * dd)196271eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd)
196371eb0538SChris Kirby {
196471eb0538SChris Kirby 	timestruc_t t;
196571eb0538SChris Kirby 
196671eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
196771eb0538SChris Kirby 	t = dd->dd_snap_cmtime;
196871eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
196971eb0538SChris Kirby 
197071eb0538SChris Kirby 	return (t);
197171eb0538SChris Kirby }
197271eb0538SChris Kirby 
197371eb0538SChris Kirby void
dsl_dir_snap_cmtime_update(dsl_dir_t * dd)197471eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
197571eb0538SChris Kirby {
197671eb0538SChris Kirby 	timestruc_t t;
197771eb0538SChris Kirby 
197871eb0538SChris Kirby 	gethrestime(&t);
197971eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
198071eb0538SChris Kirby 	dd->dd_snap_cmtime = t;
198171eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
198271eb0538SChris Kirby }
19832acef22dSMatthew Ahrens 
19842acef22dSMatthew Ahrens void
dsl_dir_zapify(dsl_dir_t * dd,dmu_tx_t * tx)19852acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
19862acef22dSMatthew Ahrens {
19872acef22dSMatthew Ahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
19882acef22dSMatthew Ahrens 	dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
19892acef22dSMatthew Ahrens }
1990a2afb611SJerry Jelinek 
1991a2afb611SJerry Jelinek boolean_t
dsl_dir_is_zapified(dsl_dir_t * dd)1992a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd)
1993a2afb611SJerry Jelinek {
1994a2afb611SJerry Jelinek 	dmu_object_info_t doi;
1995a2afb611SJerry Jelinek 
1996a2afb611SJerry Jelinek 	dmu_object_info_from_db(dd->dd_dbuf, &doi);
1997a2afb611SJerry Jelinek 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
1998a2afb611SJerry Jelinek }
1999