xref: /titanic_44/usr/src/uts/common/fs/zfs/dsl_dir.c (revision 897e74ee430b2feb87fe53494f7cab864ae0458c)
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*897e74eeSJosef '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 
126d174dbf1SArne Jansen /*
127d174dbf1SArne Jansen  * Tunable to control EDQUOT behaviour. With this set to a value != 0, zfs
128d174dbf1SArne Jansen  * doesn't always wait for a dirty txg to complete when an operation can't
129d174dbf1SArne Jansen  * get through due to space exhaustion. Instead it fails early in a range
130d174dbf1SArne Jansen  * of the tunable around the quota.
131d174dbf1SArne Jansen  * This vastly helps to reduce the number of threads waiting for the txg
132d174dbf1SArne Jansen  * to commit when a busy filesystem is near quota, especially in combination
133d174dbf1SArne Jansen  * with NFS, where each waiter takes up a server thread.
134d174dbf1SArne Jansen  */
135d174dbf1SArne Jansen uint64_t early_edquot_threshold = 32 * 1048576; /* tunable */
136d174dbf1SArne 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*897e74eeSJosef '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 
161fa9e4066Sahrens 	/*
1623f9d6ad7SLin Ling 	 * The props callback list should have been cleaned up by
1633f9d6ad7SLin Ling 	 * objset_evict().
164fa9e4066Sahrens 	 */
165fa9e4066Sahrens 	list_destroy(&dd->dd_prop_cbs);
1665ad82045Snd150628 	mutex_destroy(&dd->dd_lock);
167fa9e4066Sahrens 	kmem_free(dd, sizeof (dsl_dir_t));
168fa9e4066Sahrens }
169fa9e4066Sahrens 
170ea8dc4b6Seschrock int
dsl_dir_hold_obj(dsl_pool_t * dp,uint64_t ddobj,const char * tail,void * tag,dsl_dir_t ** ddp)1713b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
172ea8dc4b6Seschrock     const char *tail, void *tag, dsl_dir_t **ddp)
173fa9e4066Sahrens {
174fa9e4066Sahrens 	dmu_buf_t *dbuf;
175fa9e4066Sahrens 	dsl_dir_t *dd;
176ea8dc4b6Seschrock 	int err;
177fa9e4066Sahrens 
1783b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
179fa9e4066Sahrens 
180ea8dc4b6Seschrock 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
1813b2aab18SMatthew Ahrens 	if (err != 0)
182ea8dc4b6Seschrock 		return (err);
183fa9e4066Sahrens 	dd = dmu_buf_get_user(dbuf);
184fa9e4066Sahrens #ifdef ZFS_DEBUG
185fa9e4066Sahrens 	{
186fa9e4066Sahrens 		dmu_object_info_t doi;
187fa9e4066Sahrens 		dmu_object_info_from_db(dbuf, &doi);
1882acef22dSMatthew Ahrens 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
18974e7dc98SMatthew Ahrens 		ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
190fa9e4066Sahrens 	}
191fa9e4066Sahrens #endif
192fa9e4066Sahrens 	if (dd == NULL) {
193fa9e4066Sahrens 		dsl_dir_t *winner;
194fa9e4066Sahrens 
195fa9e4066Sahrens 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
196fa9e4066Sahrens 		dd->dd_object = ddobj;
197fa9e4066Sahrens 		dd->dd_dbuf = dbuf;
198fa9e4066Sahrens 		dd->dd_pool = dp;
1995ad82045Snd150628 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
200fa9e4066Sahrens 
201fa9e4066Sahrens 		list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
202fa9e4066Sahrens 		    offsetof(dsl_prop_cb_record_t, cbr_node));
203fa9e4066Sahrens 
20471eb0538SChris Kirby 		dsl_dir_snap_cmtime_update(dd);
20571eb0538SChris Kirby 
206c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_parent_obj) {
207c1379625SJustin T. Gibbs 			err = dsl_dir_hold_obj(dp,
208c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
209c1379625SJustin T. Gibbs 			    &dd->dd_parent);
2103b2aab18SMatthew Ahrens 			if (err != 0)
21174e7dc98SMatthew Ahrens 				goto errout;
212fa9e4066Sahrens 			if (tail) {
213fa9e4066Sahrens #ifdef ZFS_DEBUG
214fa9e4066Sahrens 				uint64_t foundobj;
215fa9e4066Sahrens 
216fa9e4066Sahrens 				err = zap_lookup(dp->dp_meta_objset,
217c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
218c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj, tail,
219c1379625SJustin T. Gibbs 				    sizeof (foundobj), 1, &foundobj);
220ea8dc4b6Seschrock 				ASSERT(err || foundobj == ddobj);
221fa9e4066Sahrens #endif
222fa9e4066Sahrens 				(void) strcpy(dd->dd_myname, tail);
223fa9e4066Sahrens 			} else {
224fa9e4066Sahrens 				err = zap_value_search(dp->dp_meta_objset,
225c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
226c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj,
227e7437265Sahrens 				    ddobj, 0, dd->dd_myname);
228ea8dc4b6Seschrock 			}
2293b2aab18SMatthew Ahrens 			if (err != 0)
23074e7dc98SMatthew Ahrens 				goto errout;
231fa9e4066Sahrens 		} else {
232fa9e4066Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
233fa9e4066Sahrens 		}
234fa9e4066Sahrens 
2353f9d6ad7SLin Ling 		if (dsl_dir_is_clone(dd)) {
2363f9d6ad7SLin Ling 			dmu_buf_t *origin_bonus;
2373f9d6ad7SLin Ling 			dsl_dataset_phys_t *origin_phys;
2383f9d6ad7SLin Ling 
2393f9d6ad7SLin Ling 			/*
2403f9d6ad7SLin Ling 			 * We can't open the origin dataset, because
2413f9d6ad7SLin Ling 			 * that would require opening this dsl_dir.
2423f9d6ad7SLin Ling 			 * Just look at its phys directly instead.
2433f9d6ad7SLin Ling 			 */
2443f9d6ad7SLin Ling 			err = dmu_bonus_hold(dp->dp_meta_objset,
245c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_origin_obj, FTAG,
246c1379625SJustin T. Gibbs 			    &origin_bonus);
2473b2aab18SMatthew Ahrens 			if (err != 0)
2483f9d6ad7SLin Ling 				goto errout;
2493f9d6ad7SLin Ling 			origin_phys = origin_bonus->db_data;
2503f9d6ad7SLin Ling 			dd->dd_origin_txg =
2513f9d6ad7SLin Ling 			    origin_phys->ds_creation_txg;
2523f9d6ad7SLin Ling 			dmu_buf_rele(origin_bonus, FTAG);
2533f9d6ad7SLin Ling 		}
2543f9d6ad7SLin Ling 
255*897e74eeSJosef 'Jeff' Sipek 		dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
256*897e74eeSJosef 'Jeff' Sipek 		    &dd->dd_dbuf);
257bc9014e6SJustin Gibbs 		winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
258bc9014e6SJustin Gibbs 		if (winner != NULL) {
259fa9e4066Sahrens 			if (dd->dd_parent)
2603b2aab18SMatthew Ahrens 				dsl_dir_rele(dd->dd_parent, dd);
2615ad82045Snd150628 			mutex_destroy(&dd->dd_lock);
262fa9e4066Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
263fa9e4066Sahrens 			dd = winner;
264fa9e4066Sahrens 		} else {
265fa9e4066Sahrens 			spa_open_ref(dp->dp_spa, dd);
266fa9e4066Sahrens 		}
267fa9e4066Sahrens 	}
268fa9e4066Sahrens 
269fa9e4066Sahrens 	/*
270fa9e4066Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
271fa9e4066Sahrens 	 * holds on the spa.  We need the open-to-close holds because
272fa9e4066Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
273fa9e4066Sahrens 	 * dir which the spa also has open, so we could incorrectly
274fa9e4066Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
275fa9e4066Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
276fa9e4066Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
277fa9e4066Sahrens 	 */
278fa9e4066Sahrens 	spa_open_ref(dp->dp_spa, tag);
279fa9e4066Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
280fa9e4066Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
281fa9e4066Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
282ea8dc4b6Seschrock 	*ddp = dd;
283ea8dc4b6Seschrock 	return (0);
28474e7dc98SMatthew Ahrens 
28574e7dc98SMatthew Ahrens errout:
28674e7dc98SMatthew Ahrens 	if (dd->dd_parent)
2873b2aab18SMatthew Ahrens 		dsl_dir_rele(dd->dd_parent, dd);
28874e7dc98SMatthew Ahrens 	mutex_destroy(&dd->dd_lock);
28974e7dc98SMatthew Ahrens 	kmem_free(dd, sizeof (dsl_dir_t));
29074e7dc98SMatthew Ahrens 	dmu_buf_rele(dbuf, tag);
29174e7dc98SMatthew Ahrens 	return (err);
292fa9e4066Sahrens }
293fa9e4066Sahrens 
294fa9e4066Sahrens void
dsl_dir_rele(dsl_dir_t * dd,void * tag)2953b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag)
296fa9e4066Sahrens {
297fa9e4066Sahrens 	dprintf_dd(dd, "%s\n", "");
298fa9e4066Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
299ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
300fa9e4066Sahrens }
301fa9e4066Sahrens 
302bc9014e6SJustin Gibbs /*
303bc9014e6SJustin Gibbs  * Remove a reference to the given dsl dir that is being asynchronously
304bc9014e6SJustin Gibbs  * released.  Async releases occur from a taskq performing eviction of
305bc9014e6SJustin Gibbs  * dsl datasets and dirs.  This process is identical to a normal release
306bc9014e6SJustin Gibbs  * with the exception of using the async API for releasing the reference on
307bc9014e6SJustin Gibbs  * the spa.
308bc9014e6SJustin Gibbs  */
309bc9014e6SJustin Gibbs void
dsl_dir_async_rele(dsl_dir_t * dd,void * tag)310bc9014e6SJustin Gibbs dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
311bc9014e6SJustin Gibbs {
312bc9014e6SJustin Gibbs 	dprintf_dd(dd, "%s\n", "");
313bc9014e6SJustin Gibbs 	spa_async_close(dd->dd_pool->dp_spa, tag);
314bc9014e6SJustin Gibbs 	dmu_buf_rele(dd->dd_dbuf, tag);
315bc9014e6SJustin Gibbs }
316bc9014e6SJustin Gibbs 
317a1988827SMatthew Ahrens /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
318fa9e4066Sahrens void
dsl_dir_name(dsl_dir_t * dd,char * buf)319fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
320fa9e4066Sahrens {
321fa9e4066Sahrens 	if (dd->dd_parent) {
322fa9e4066Sahrens 		dsl_dir_name(dd->dd_parent, buf);
323a1988827SMatthew Ahrens 		VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
324a1988827SMatthew Ahrens 		    ZFS_MAX_DATASET_NAME_LEN);
325fa9e4066Sahrens 	} else {
326fa9e4066Sahrens 		buf[0] = '\0';
327fa9e4066Sahrens 	}
328fa9e4066Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
329fa9e4066Sahrens 		/*
330fa9e4066Sahrens 		 * recursive mutex so that we can use
331fa9e4066Sahrens 		 * dprintf_dd() with dd_lock held
332fa9e4066Sahrens 		 */
333fa9e4066Sahrens 		mutex_enter(&dd->dd_lock);
334a1988827SMatthew Ahrens 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
335a1988827SMatthew Ahrens 		    <, ZFS_MAX_DATASET_NAME_LEN);
336fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
337fa9e4066Sahrens 	} else {
338a1988827SMatthew Ahrens 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
339a1988827SMatthew Ahrens 		    <, ZFS_MAX_DATASET_NAME_LEN);
340fa9e4066Sahrens 	}
341fa9e4066Sahrens }
342fa9e4066Sahrens 
343ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
344b7661cccSmmusante int
dsl_dir_namelen(dsl_dir_t * dd)345b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd)
346b7661cccSmmusante {
347b7661cccSmmusante 	int result = 0;
348b7661cccSmmusante 
349b7661cccSmmusante 	if (dd->dd_parent) {
350b7661cccSmmusante 		/* parent's name + 1 for the "/" */
351b7661cccSmmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
352b7661cccSmmusante 	}
353b7661cccSmmusante 
354b7661cccSmmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
355b7661cccSmmusante 		/* see dsl_dir_name */
356b7661cccSmmusante 		mutex_enter(&dd->dd_lock);
357b7661cccSmmusante 		result += strlen(dd->dd_myname);
358b7661cccSmmusante 		mutex_exit(&dd->dd_lock);
359b7661cccSmmusante 	} else {
360b7661cccSmmusante 		result += strlen(dd->dd_myname);
361b7661cccSmmusante 	}
362b7661cccSmmusante 
363b7661cccSmmusante 	return (result);
364b7661cccSmmusante }
365b7661cccSmmusante 
366fa9e4066Sahrens static int
getcomponent(const char * path,char * component,const char ** nextp)367fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp)
368fa9e4066Sahrens {
369fa9e4066Sahrens 	char *p;
3703b2aab18SMatthew Ahrens 
371ccba0801SRich Morris 	if ((path == NULL) || (path[0] == '\0'))
372be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
373fa9e4066Sahrens 	/* This would be a good place to reserve some namespace... */
374fa9e4066Sahrens 	p = strpbrk(path, "/@");
375fa9e4066Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
376fa9e4066Sahrens 		/* two separators in a row */
377be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
378fa9e4066Sahrens 	}
379fa9e4066Sahrens 	if (p == NULL || p == path) {
380fa9e4066Sahrens 		/*
381fa9e4066Sahrens 		 * if the first thing is an @ or /, it had better be an
382fa9e4066Sahrens 		 * @ and it had better not have any more ats or slashes,
383fa9e4066Sahrens 		 * and it had better have something after the @.
384fa9e4066Sahrens 		 */
385fa9e4066Sahrens 		if (p != NULL &&
386fa9e4066Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
387be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
388a1988827SMatthew Ahrens 		if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
389be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
390fa9e4066Sahrens 		(void) strcpy(component, path);
391fa9e4066Sahrens 		p = NULL;
392fa9e4066Sahrens 	} else if (p[0] == '/') {
393a1988827SMatthew Ahrens 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
394be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
395fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
396fa9e4066Sahrens 		component[p - path] = '\0';
397fa9e4066Sahrens 		p++;
398fa9e4066Sahrens 	} else if (p[0] == '@') {
399fa9e4066Sahrens 		/*
400fa9e4066Sahrens 		 * if the next separator is an @, there better not be
401fa9e4066Sahrens 		 * any more slashes.
402fa9e4066Sahrens 		 */
403fa9e4066Sahrens 		if (strchr(path, '/'))
404be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
405a1988827SMatthew Ahrens 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
406be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
407fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
408fa9e4066Sahrens 		component[p - path] = '\0';
409fa9e4066Sahrens 	} else {
4103b2aab18SMatthew Ahrens 		panic("invalid p=%p", (void *)p);
411fa9e4066Sahrens 	}
412fa9e4066Sahrens 	*nextp = p;
413fa9e4066Sahrens 	return (0);
414fa9e4066Sahrens }
415fa9e4066Sahrens 
416fa9e4066Sahrens /*
4173b2aab18SMatthew Ahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
4183b2aab18SMatthew Ahrens  * be found in *tail.  The name must be in the specified dsl_pool_t.  This
4193b2aab18SMatthew Ahrens  * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
4203b2aab18SMatthew Ahrens  * path is bogus, or if tail==NULL and we couldn't parse the whole name.
4213b2aab18SMatthew Ahrens  * (*tail)[0] == '@' means that the last component is a snapshot.
422fa9e4066Sahrens  */
423ea8dc4b6Seschrock int
dsl_dir_hold(dsl_pool_t * dp,const char * name,void * tag,dsl_dir_t ** ddp,const char ** tailp)4243b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
425ea8dc4b6Seschrock     dsl_dir_t **ddp, const char **tailp)
426fa9e4066Sahrens {
427a1988827SMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
4283b2aab18SMatthew Ahrens 	const char *spaname, *next, *nextnext = NULL;
429fa9e4066Sahrens 	int err;
430fa9e4066Sahrens 	dsl_dir_t *dd;
431fa9e4066Sahrens 	uint64_t ddobj;
432fa9e4066Sahrens 
433fa9e4066Sahrens 	err = getcomponent(name, buf, &next);
4343b2aab18SMatthew Ahrens 	if (err != 0)
435ea8dc4b6Seschrock 		return (err);
436fa9e4066Sahrens 
4373b2aab18SMatthew Ahrens 	/* Make sure the name is in the specified pool. */
4383b2aab18SMatthew Ahrens 	spaname = spa_name(dp->dp_spa);
4393b2aab18SMatthew Ahrens 	if (strcmp(buf, spaname) != 0)
44003b1c297SAlexander Eremin 		return (SET_ERROR(EXDEV));
441fa9e4066Sahrens 
4423b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
443fa9e4066Sahrens 
4443b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
4453b2aab18SMatthew Ahrens 	if (err != 0) {
446ea8dc4b6Seschrock 		return (err);
447fa9e4066Sahrens 	}
448ea8dc4b6Seschrock 
449ea8dc4b6Seschrock 	while (next != NULL) {
450bc9014e6SJustin Gibbs 		dsl_dir_t *child_dd;
451ea8dc4b6Seschrock 		err = getcomponent(next, buf, &nextnext);
4523b2aab18SMatthew Ahrens 		if (err != 0)
453ea8dc4b6Seschrock 			break;
454fa9e4066Sahrens 		ASSERT(next[0] != '\0');
455fa9e4066Sahrens 		if (next[0] == '@')
456fa9e4066Sahrens 			break;
457fa9e4066Sahrens 		dprintf("looking up %s in obj%lld\n",
458c1379625SJustin T. Gibbs 		    buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
459fa9e4066Sahrens 
460fa9e4066Sahrens 		err = zap_lookup(dp->dp_meta_objset,
461c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj,
462fa9e4066Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
4633b2aab18SMatthew Ahrens 		if (err != 0) {
464ea8dc4b6Seschrock 			if (err == ENOENT)
465ea8dc4b6Seschrock 				err = 0;
466fa9e4066Sahrens 			break;
467fa9e4066Sahrens 		}
468fa9e4066Sahrens 
469bc9014e6SJustin Gibbs 		err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
4703b2aab18SMatthew Ahrens 		if (err != 0)
471ea8dc4b6Seschrock 			break;
4723b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
473bc9014e6SJustin Gibbs 		dd = child_dd;
474fa9e4066Sahrens 		next = nextnext;
475fa9e4066Sahrens 	}
476fa9e4066Sahrens 
4773b2aab18SMatthew Ahrens 	if (err != 0) {
4783b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
479ea8dc4b6Seschrock 		return (err);
480ea8dc4b6Seschrock 	}
481ea8dc4b6Seschrock 
482fa9e4066Sahrens 	/*
483fa9e4066Sahrens 	 * It's an error if there's more than one component left, or
484fa9e4066Sahrens 	 * tailp==NULL and there's any component left.
485fa9e4066Sahrens 	 */
486fa9e4066Sahrens 	if (next != NULL &&
487fa9e4066Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
488fa9e4066Sahrens 		/* bad path name */
4893b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
490fa9e4066Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
491be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
492fa9e4066Sahrens 	}
4933b2aab18SMatthew Ahrens 	if (tailp != NULL)
494fa9e4066Sahrens 		*tailp = next;
495ea8dc4b6Seschrock 	*ddp = dd;
496ea8dc4b6Seschrock 	return (err);
497fa9e4066Sahrens }
498fa9e4066Sahrens 
499a2afb611SJerry Jelinek /*
500a2afb611SJerry Jelinek  * If the counts are already initialized for this filesystem and its
501a2afb611SJerry Jelinek  * descendants then do nothing, otherwise initialize the counts.
502a2afb611SJerry Jelinek  *
503a2afb611SJerry Jelinek  * The counts on this filesystem, and those below, may be uninitialized due to
504a2afb611SJerry Jelinek  * either the use of a pre-existing pool which did not support the
505a2afb611SJerry Jelinek  * filesystem/snapshot limit feature, or one in which the feature had not yet
506a2afb611SJerry Jelinek  * been enabled.
507a2afb611SJerry Jelinek  *
508a2afb611SJerry Jelinek  * Recursively descend the filesystem tree and update the filesystem/snapshot
509a2afb611SJerry Jelinek  * counts on each filesystem below, then update the cumulative count on the
510a2afb611SJerry Jelinek  * current filesystem. If the filesystem already has a count set on it,
511a2afb611SJerry Jelinek  * then we know that its counts, and the counts on the filesystems below it,
512a2afb611SJerry Jelinek  * are already correct, so we don't have to update this filesystem.
513a2afb611SJerry Jelinek  */
514a2afb611SJerry Jelinek static void
dsl_dir_init_fs_ss_count(dsl_dir_t * dd,dmu_tx_t * tx)515a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
516a2afb611SJerry Jelinek {
517a2afb611SJerry Jelinek 	uint64_t my_fs_cnt = 0;
518a2afb611SJerry Jelinek 	uint64_t my_ss_cnt = 0;
519a2afb611SJerry Jelinek 	dsl_pool_t *dp = dd->dd_pool;
520a2afb611SJerry Jelinek 	objset_t *os = dp->dp_meta_objset;
521a2afb611SJerry Jelinek 	zap_cursor_t *zc;
522a2afb611SJerry Jelinek 	zap_attribute_t *za;
523a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
524a2afb611SJerry Jelinek 
525adf34077SJerry Jelinek 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
526a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dp));
527a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
528a2afb611SJerry Jelinek 
529a2afb611SJerry Jelinek 	dsl_dir_zapify(dd, tx);
530a2afb611SJerry Jelinek 
531a2afb611SJerry Jelinek 	/*
532a2afb611SJerry Jelinek 	 * If the filesystem count has already been initialized then we
533a2afb611SJerry Jelinek 	 * don't need to recurse down any further.
534a2afb611SJerry Jelinek 	 */
535a2afb611SJerry Jelinek 	if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
536a2afb611SJerry Jelinek 		return;
537a2afb611SJerry Jelinek 
538a2afb611SJerry Jelinek 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
539a2afb611SJerry Jelinek 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
540a2afb611SJerry Jelinek 
541a2afb611SJerry Jelinek 	/* Iterate my child dirs */
542c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
543a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
544a2afb611SJerry Jelinek 		dsl_dir_t *chld_dd;
545a2afb611SJerry Jelinek 		uint64_t count;
546a2afb611SJerry Jelinek 
547a2afb611SJerry Jelinek 		VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
548a2afb611SJerry Jelinek 		    &chld_dd));
549a2afb611SJerry Jelinek 
550a2afb611SJerry Jelinek 		/*
551a2afb611SJerry Jelinek 		 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
552a2afb611SJerry Jelinek 		 * temporary datasets.
553a2afb611SJerry Jelinek 		 */
554a2afb611SJerry Jelinek 		if (chld_dd->dd_myname[0] == '$' ||
555a2afb611SJerry Jelinek 		    chld_dd->dd_myname[0] == '%') {
556a2afb611SJerry Jelinek 			dsl_dir_rele(chld_dd, FTAG);
557a2afb611SJerry Jelinek 			continue;
558a2afb611SJerry Jelinek 		}
559a2afb611SJerry Jelinek 
560a2afb611SJerry Jelinek 		my_fs_cnt++;	/* count this child */
561a2afb611SJerry Jelinek 
562a2afb611SJerry Jelinek 		dsl_dir_init_fs_ss_count(chld_dd, tx);
563a2afb611SJerry Jelinek 
564a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
565a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
566a2afb611SJerry Jelinek 		my_fs_cnt += count;
567a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
568a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
569a2afb611SJerry Jelinek 		my_ss_cnt += count;
570a2afb611SJerry Jelinek 
571a2afb611SJerry Jelinek 		dsl_dir_rele(chld_dd, FTAG);
572a2afb611SJerry Jelinek 	}
573a2afb611SJerry Jelinek 	zap_cursor_fini(zc);
574a2afb611SJerry Jelinek 	/* Count my snapshots (we counted children's snapshots above) */
575a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
576c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
577a2afb611SJerry Jelinek 
578c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
579a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0;
580a2afb611SJerry Jelinek 	    zap_cursor_advance(zc)) {
581a2afb611SJerry Jelinek 		/* Don't count temporary snapshots */
582a2afb611SJerry Jelinek 		if (za->za_name[0] != '%')
583a2afb611SJerry Jelinek 			my_ss_cnt++;
584a2afb611SJerry Jelinek 	}
585fb7001f1SAlex Reece 	zap_cursor_fini(zc);
586a2afb611SJerry Jelinek 
587a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
588a2afb611SJerry Jelinek 
589a2afb611SJerry Jelinek 	kmem_free(zc, sizeof (zap_cursor_t));
590a2afb611SJerry Jelinek 	kmem_free(za, sizeof (zap_attribute_t));
591a2afb611SJerry Jelinek 
592a2afb611SJerry Jelinek 	/* we're in a sync task, update counts */
593a2afb611SJerry Jelinek 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
594a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
595a2afb611SJerry Jelinek 	    sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
596a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
597a2afb611SJerry Jelinek 	    sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
598a2afb611SJerry Jelinek }
599a2afb611SJerry Jelinek 
600a2afb611SJerry Jelinek static int
dsl_dir_actv_fs_ss_limit_check(void * arg,dmu_tx_t * tx)601a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
602a2afb611SJerry Jelinek {
603a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
604a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
605a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
606a2afb611SJerry Jelinek 	dsl_dir_t *dd;
607a2afb611SJerry Jelinek 	int error;
608a2afb611SJerry Jelinek 
609a2afb611SJerry Jelinek 	error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
610a2afb611SJerry Jelinek 	if (error != 0)
611a2afb611SJerry Jelinek 		return (error);
612a2afb611SJerry Jelinek 
613a2afb611SJerry Jelinek 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
614a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
615a2afb611SJerry Jelinek 		return (SET_ERROR(ENOTSUP));
616a2afb611SJerry Jelinek 	}
617a2afb611SJerry Jelinek 
618a2afb611SJerry Jelinek 	dd = ds->ds_dir;
619a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
620a2afb611SJerry Jelinek 	    dsl_dir_is_zapified(dd) &&
621a2afb611SJerry Jelinek 	    zap_contains(dp->dp_meta_objset, dd->dd_object,
622a2afb611SJerry Jelinek 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
623a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
624a2afb611SJerry Jelinek 		return (SET_ERROR(EALREADY));
625a2afb611SJerry Jelinek 	}
626a2afb611SJerry Jelinek 
627a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
628a2afb611SJerry Jelinek 	return (0);
629a2afb611SJerry Jelinek }
630a2afb611SJerry Jelinek 
631a2afb611SJerry Jelinek static void
dsl_dir_actv_fs_ss_limit_sync(void * arg,dmu_tx_t * tx)632a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
633a2afb611SJerry Jelinek {
634a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
635a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
636a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
637a2afb611SJerry Jelinek 	spa_t *spa;
638a2afb611SJerry Jelinek 
639a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
640a2afb611SJerry Jelinek 
641a2afb611SJerry Jelinek 	spa = dsl_dataset_get_spa(ds);
642a2afb611SJerry Jelinek 
643a2afb611SJerry Jelinek 	if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
644a2afb611SJerry Jelinek 		/*
645a2afb611SJerry Jelinek 		 * Since the feature was not active and we're now setting a
646a2afb611SJerry Jelinek 		 * limit, increment the feature-active counter so that the
647a2afb611SJerry Jelinek 		 * feature becomes active for the first time.
648a2afb611SJerry Jelinek 		 *
649a2afb611SJerry Jelinek 		 * We are already in a sync task so we can update the MOS.
650a2afb611SJerry Jelinek 		 */
651a2afb611SJerry Jelinek 		spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
652a2afb611SJerry Jelinek 	}
653a2afb611SJerry Jelinek 
654a2afb611SJerry Jelinek 	/*
655a2afb611SJerry Jelinek 	 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
656a2afb611SJerry Jelinek 	 * we need to ensure the counts are correct. Descend down the tree from
657a2afb611SJerry Jelinek 	 * this point and update all of the counts to be accurate.
658a2afb611SJerry Jelinek 	 */
659a2afb611SJerry Jelinek 	dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
660a2afb611SJerry Jelinek 
661a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
662a2afb611SJerry Jelinek }
663a2afb611SJerry Jelinek 
664a2afb611SJerry Jelinek /*
665a2afb611SJerry Jelinek  * Make sure the feature is enabled and activate it if necessary.
666a2afb611SJerry Jelinek  * Since we're setting a limit, ensure the on-disk counts are valid.
667a2afb611SJerry Jelinek  * This is only called by the ioctl path when setting a limit value.
668a2afb611SJerry Jelinek  *
669a2afb611SJerry Jelinek  * We do not need to validate the new limit, since users who can change the
670a2afb611SJerry Jelinek  * limit are also allowed to exceed the limit.
671a2afb611SJerry Jelinek  */
672a2afb611SJerry Jelinek int
dsl_dir_activate_fs_ss_limit(const char * ddname)673a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname)
674a2afb611SJerry Jelinek {
675a2afb611SJerry Jelinek 	int error;
676a2afb611SJerry Jelinek 
677a2afb611SJerry Jelinek 	error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
6787d46dc6cSMatthew Ahrens 	    dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
6797d46dc6cSMatthew Ahrens 	    ZFS_SPACE_CHECK_RESERVED);
680a2afb611SJerry Jelinek 
681a2afb611SJerry Jelinek 	if (error == EALREADY)
682a2afb611SJerry Jelinek 		error = 0;
683a2afb611SJerry Jelinek 
684a2afb611SJerry Jelinek 	return (error);
685a2afb611SJerry Jelinek }
686a2afb611SJerry Jelinek 
687a2afb611SJerry Jelinek /*
688a2afb611SJerry Jelinek  * Used to determine if the filesystem_limit or snapshot_limit should be
689a2afb611SJerry Jelinek  * enforced. We allow the limit to be exceeded if the user has permission to
690a2afb611SJerry Jelinek  * write the property value. We pass in the creds that we got in the open
691a2afb611SJerry Jelinek  * context since we will always be the GZ root in syncing context. We also have
692a2afb611SJerry Jelinek  * to handle the case where we are allowed to change the limit on the current
693a2afb611SJerry Jelinek  * dataset, but there may be another limit in the tree above.
694a2afb611SJerry Jelinek  *
695a2afb611SJerry Jelinek  * We can never modify these two properties within a non-global zone. In
696a2afb611SJerry Jelinek  * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
697a2afb611SJerry Jelinek  * can't use that function since we are already holding the dp_config_rwlock.
698a2afb611SJerry Jelinek  * In addition, we already have the dd and dealing with snapshots is simplified
699a2afb611SJerry Jelinek  * in this code.
700a2afb611SJerry Jelinek  */
701a2afb611SJerry Jelinek 
702a2afb611SJerry Jelinek typedef enum {
703a2afb611SJerry Jelinek 	ENFORCE_ALWAYS,
704a2afb611SJerry Jelinek 	ENFORCE_NEVER,
705a2afb611SJerry Jelinek 	ENFORCE_ABOVE
706a2afb611SJerry Jelinek } enforce_res_t;
707a2afb611SJerry Jelinek 
708a2afb611SJerry Jelinek static enforce_res_t
dsl_enforce_ds_ss_limits(dsl_dir_t * dd,zfs_prop_t prop,cred_t * cr)709a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
710a2afb611SJerry Jelinek {
711a2afb611SJerry Jelinek 	enforce_res_t enforce = ENFORCE_ALWAYS;
712a2afb611SJerry Jelinek 	uint64_t obj;
713a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
714a2afb611SJerry Jelinek 	uint64_t zoned;
715a2afb611SJerry Jelinek 
716a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
717a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
718a2afb611SJerry Jelinek 
719a2afb611SJerry Jelinek #ifdef _KERNEL
720a2afb611SJerry Jelinek 	if (crgetzoneid(cr) != GLOBAL_ZONEID)
721a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
722a2afb611SJerry Jelinek 
723a2afb611SJerry Jelinek 	if (secpolicy_zfs(cr) == 0)
724a2afb611SJerry Jelinek 		return (ENFORCE_NEVER);
725a2afb611SJerry Jelinek #endif
726a2afb611SJerry Jelinek 
727c1379625SJustin T. Gibbs 	if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
728a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
729a2afb611SJerry Jelinek 
730a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
731a2afb611SJerry Jelinek 
732a2afb611SJerry Jelinek 	if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
733a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
734a2afb611SJerry Jelinek 
735a2afb611SJerry Jelinek 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
736a2afb611SJerry Jelinek 		/* Only root can access zoned fs's from the GZ */
737a2afb611SJerry Jelinek 		enforce = ENFORCE_ALWAYS;
738a2afb611SJerry Jelinek 	} else {
739a2afb611SJerry Jelinek 		if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
740a2afb611SJerry Jelinek 			enforce = ENFORCE_ABOVE;
741a2afb611SJerry Jelinek 	}
742a2afb611SJerry Jelinek 
743a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
744a2afb611SJerry Jelinek 	return (enforce);
745a2afb611SJerry Jelinek }
746a2afb611SJerry Jelinek 
747a2afb611SJerry Jelinek /*
748a2afb611SJerry Jelinek  * Check if adding additional child filesystem(s) would exceed any filesystem
749a2afb611SJerry Jelinek  * limits or adding additional snapshot(s) would exceed any snapshot limits.
750a2afb611SJerry Jelinek  * The prop argument indicates which limit to check.
751a2afb611SJerry Jelinek  *
752a2afb611SJerry Jelinek  * Note that all filesystem limits up to the root (or the highest
753a2afb611SJerry Jelinek  * initialized) filesystem or the given ancestor must be satisfied.
754a2afb611SJerry Jelinek  */
755a2afb611SJerry 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)756a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
757a2afb611SJerry Jelinek     dsl_dir_t *ancestor, cred_t *cr)
758a2afb611SJerry Jelinek {
759a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
760a2afb611SJerry Jelinek 	uint64_t limit, count;
761a2afb611SJerry Jelinek 	char *count_prop;
762a2afb611SJerry Jelinek 	enforce_res_t enforce;
763a2afb611SJerry Jelinek 	int err = 0;
764a2afb611SJerry Jelinek 
765a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
766a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
767a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
768a2afb611SJerry Jelinek 
769a2afb611SJerry Jelinek 	/*
770a2afb611SJerry Jelinek 	 * If we're allowed to change the limit, don't enforce the limit
771a2afb611SJerry Jelinek 	 * e.g. this can happen if a snapshot is taken by an administrative
772a2afb611SJerry Jelinek 	 * user in the global zone (i.e. a recursive snapshot by root).
773a2afb611SJerry Jelinek 	 * However, we must handle the case of delegated permissions where we
774a2afb611SJerry Jelinek 	 * are allowed to change the limit on the current dataset, but there
775a2afb611SJerry Jelinek 	 * is another limit in the tree above.
776a2afb611SJerry Jelinek 	 */
777a2afb611SJerry Jelinek 	enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
778a2afb611SJerry Jelinek 	if (enforce == ENFORCE_NEVER)
779a2afb611SJerry Jelinek 		return (0);
780a2afb611SJerry Jelinek 
781a2afb611SJerry Jelinek 	/*
782a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment
783a2afb611SJerry Jelinek 	 * is 0.
784a2afb611SJerry Jelinek 	 */
785a2afb611SJerry Jelinek 	if (delta == 0)
786a2afb611SJerry Jelinek 		return (0);
787a2afb611SJerry Jelinek 
788a2afb611SJerry Jelinek 	if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
789a2afb611SJerry Jelinek 		/*
790a2afb611SJerry Jelinek 		 * We don't enforce the limit for temporary snapshots. This is
791a2afb611SJerry Jelinek 		 * indicated by a NULL cred_t argument.
792a2afb611SJerry Jelinek 		 */
793a2afb611SJerry Jelinek 		if (cr == NULL)
794a2afb611SJerry Jelinek 			return (0);
795a2afb611SJerry Jelinek 
796a2afb611SJerry Jelinek 		count_prop = DD_FIELD_SNAPSHOT_COUNT;
797a2afb611SJerry Jelinek 	} else {
798a2afb611SJerry Jelinek 		count_prop = DD_FIELD_FILESYSTEM_COUNT;
799a2afb611SJerry Jelinek 	}
800a2afb611SJerry Jelinek 
801a2afb611SJerry Jelinek 	/*
802a2afb611SJerry Jelinek 	 * If an ancestor has been provided, stop checking the limit once we
803a2afb611SJerry Jelinek 	 * hit that dir. We need this during rename so that we don't overcount
804a2afb611SJerry Jelinek 	 * the check once we recurse up to the common ancestor.
805a2afb611SJerry Jelinek 	 */
806a2afb611SJerry Jelinek 	if (ancestor == dd)
807a2afb611SJerry Jelinek 		return (0);
808a2afb611SJerry Jelinek 
809a2afb611SJerry Jelinek 	/*
810a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
811a2afb611SJerry Jelinek 	 * stop since we know there is no limit here (or above). The counts are
812a2afb611SJerry Jelinek 	 * not valid on this node and we know we won't touch this node's counts.
813a2afb611SJerry Jelinek 	 */
814a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
815a2afb611SJerry Jelinek 	    count_prop, sizeof (count), 1, &count) == ENOENT)
816a2afb611SJerry Jelinek 		return (0);
817a2afb611SJerry Jelinek 
818a2afb611SJerry Jelinek 	err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
819a2afb611SJerry Jelinek 	    B_FALSE);
820a2afb611SJerry Jelinek 	if (err != 0)
821a2afb611SJerry Jelinek 		return (err);
822a2afb611SJerry Jelinek 
823a2afb611SJerry Jelinek 	/* Is there a limit which we've hit? */
824a2afb611SJerry Jelinek 	if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
825a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
826a2afb611SJerry Jelinek 
827a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
828a2afb611SJerry Jelinek 		err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
829a2afb611SJerry Jelinek 		    ancestor, cr);
830a2afb611SJerry Jelinek 
831a2afb611SJerry Jelinek 	return (err);
832a2afb611SJerry Jelinek }
833a2afb611SJerry Jelinek 
834a2afb611SJerry Jelinek /*
835a2afb611SJerry Jelinek  * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
836a2afb611SJerry Jelinek  * parents. When a new filesystem/snapshot is created, increment the count on
837a2afb611SJerry Jelinek  * all parents, and when a filesystem/snapshot is destroyed, decrement the
838a2afb611SJerry Jelinek  * count.
839a2afb611SJerry Jelinek  */
840a2afb611SJerry Jelinek void
dsl_fs_ss_count_adjust(dsl_dir_t * dd,int64_t delta,const char * prop,dmu_tx_t * tx)841a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
842a2afb611SJerry Jelinek     dmu_tx_t *tx)
843a2afb611SJerry Jelinek {
844a2afb611SJerry Jelinek 	int err;
845a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
846a2afb611SJerry Jelinek 	uint64_t count;
847a2afb611SJerry Jelinek 
848a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
849a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
850a2afb611SJerry Jelinek 	ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
851a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
852a2afb611SJerry Jelinek 
853a2afb611SJerry Jelinek 	/*
854a2afb611SJerry Jelinek 	 * When we receive an incremental stream into a filesystem that already
855a2afb611SJerry Jelinek 	 * exists, a temporary clone is created.  We don't count this temporary
856a2afb611SJerry Jelinek 	 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
857a2afb611SJerry Jelinek 	 * $MOS & $ORIGIN) objsets.
858a2afb611SJerry Jelinek 	 */
859a2afb611SJerry Jelinek 	if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
860a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
861a2afb611SJerry Jelinek 		return;
862a2afb611SJerry Jelinek 
863a2afb611SJerry Jelinek 	/*
864a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
865a2afb611SJerry Jelinek 	 */
866a2afb611SJerry Jelinek 	if (delta == 0)
867a2afb611SJerry Jelinek 		return;
868a2afb611SJerry Jelinek 
869a2afb611SJerry Jelinek 	/*
870a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
871a2afb611SJerry Jelinek 	 * stop since we know the counts are not valid on this node and we
872a2afb611SJerry Jelinek 	 * know we shouldn't touch this node's counts. An uninitialized count
873a2afb611SJerry Jelinek 	 * on the node indicates that either the feature has not yet been
874a2afb611SJerry Jelinek 	 * activated or there are no limits on this part of the tree.
875a2afb611SJerry Jelinek 	 */
876a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
877a2afb611SJerry Jelinek 	    prop, sizeof (count), 1, &count)) == ENOENT)
878a2afb611SJerry Jelinek 		return;
879a2afb611SJerry Jelinek 	VERIFY0(err);
880a2afb611SJerry Jelinek 
881a2afb611SJerry Jelinek 	count += delta;
882a2afb611SJerry Jelinek 	/* Use a signed verify to make sure we're not neg. */
883a2afb611SJerry Jelinek 	VERIFY3S(count, >=, 0);
884a2afb611SJerry Jelinek 
885a2afb611SJerry Jelinek 	VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
886a2afb611SJerry Jelinek 	    tx));
887a2afb611SJerry Jelinek 
888a2afb611SJerry Jelinek 	/* Roll up this additional count into our ancestors */
889a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
890a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
891a2afb611SJerry Jelinek }
892a2afb611SJerry Jelinek 
8931d452cf5Sahrens uint64_t
dsl_dir_create_sync(dsl_pool_t * dp,dsl_dir_t * pds,const char * name,dmu_tx_t * tx)894088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
895088f3894Sahrens     dmu_tx_t *tx)
896fa9e4066Sahrens {
897088f3894Sahrens 	objset_t *mos = dp->dp_meta_objset;
898fa9e4066Sahrens 	uint64_t ddobj;
899cde58dbcSMatthew Ahrens 	dsl_dir_phys_t *ddphys;
900fa9e4066Sahrens 	dmu_buf_t *dbuf;
901fa9e4066Sahrens 
9021649cd4bStabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
9031649cd4bStabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
904088f3894Sahrens 	if (pds) {
905c1379625SJustin T. Gibbs 		VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
9061d452cf5Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
907088f3894Sahrens 	} else {
908088f3894Sahrens 		/* it's the root dir */
909088f3894Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
910088f3894Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
911088f3894Sahrens 	}
912ea8dc4b6Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
913fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
914cde58dbcSMatthew Ahrens 	ddphys = dbuf->db_data;
915fa9e4066Sahrens 
916cde58dbcSMatthew Ahrens 	ddphys->dd_creation_time = gethrestime_sec();
917a2afb611SJerry Jelinek 	if (pds) {
918cde58dbcSMatthew Ahrens 		ddphys->dd_parent_obj = pds->dd_object;
919a2afb611SJerry Jelinek 
920a2afb611SJerry Jelinek 		/* update the filesystem counts */
921a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
922a2afb611SJerry Jelinek 	}
923cde58dbcSMatthew Ahrens 	ddphys->dd_props_zapobj = zap_create(mos,
924fa9e4066Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
925cde58dbcSMatthew Ahrens 	ddphys->dd_child_dir_zapobj = zap_create(mos,
92687e5029aSahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
92774e7dc98SMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
928cde58dbcSMatthew Ahrens 		ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
929ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
930fa9e4066Sahrens 
9311d452cf5Sahrens 	return (ddobj);
9321d452cf5Sahrens }
9331d452cf5Sahrens 
934088f3894Sahrens boolean_t
dsl_dir_is_clone(dsl_dir_t * dd)935088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
936fa9e4066Sahrens {
937c1379625SJustin T. Gibbs 	return (dsl_dir_phys(dd)->dd_origin_obj &&
938088f3894Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
939c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_origin_obj !=
940088f3894Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
941fa9e4066Sahrens }
942fa9e4066Sahrens 
943fa9e4066Sahrens void
dsl_dir_stats(dsl_dir_t * dd,nvlist_t * nv)944a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
945fa9e4066Sahrens {
946fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
94774e7dc98SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
948c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_bytes);
949c1379625SJustin T. Gibbs 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
950c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_quota);
951a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
952c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_reserved);
953a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
954c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
955c1379625SJustin T. Gibbs 	    (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
956c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes));
95777372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
958c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes);
959c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
96074e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
961c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
96274e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
963c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
96474e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
965c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
96674e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
967c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
968c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
96974e7dc98SMatthew Ahrens 	}
970fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
971fa9e4066Sahrens 
972a2afb611SJerry Jelinek 	if (dsl_dir_is_zapified(dd)) {
973a2afb611SJerry Jelinek 		uint64_t count;
974a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
975a2afb611SJerry Jelinek 
976a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
977a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
978a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
979a2afb611SJerry Jelinek 			    ZFS_PROP_FILESYSTEM_COUNT, count);
980a2afb611SJerry Jelinek 		}
981a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
982a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
983a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
984a2afb611SJerry Jelinek 			    ZFS_PROP_SNAPSHOT_COUNT, count);
985a2afb611SJerry Jelinek 		}
986a2afb611SJerry Jelinek 	}
987a2afb611SJerry Jelinek 
988088f3894Sahrens 	if (dsl_dir_is_clone(dd)) {
989fa9e4066Sahrens 		dsl_dataset_t *ds;
990a1988827SMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
991fa9e4066Sahrens 
9923b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
993c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
994a2eea2e1Sahrens 		dsl_dataset_name(ds, buf);
995745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
996a2eea2e1Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
997fa9e4066Sahrens 	}
998fa9e4066Sahrens }
999fa9e4066Sahrens 
1000fa9e4066Sahrens void
dsl_dir_dirty(dsl_dir_t * dd,dmu_tx_t * tx)1001fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1002fa9e4066Sahrens {
1003fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
1004fa9e4066Sahrens 
1005c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd));
1006fa9e4066Sahrens 
10073b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1008fa9e4066Sahrens 		/* up the hold count until we can be written out */
1009fa9e4066Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
1010fa9e4066Sahrens 	}
1011fa9e4066Sahrens }
1012fa9e4066Sahrens 
1013fa9e4066Sahrens static int64_t
parent_delta(dsl_dir_t * dd,uint64_t used,int64_t delta)1014fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1015fa9e4066Sahrens {
1016c1379625SJustin T. Gibbs 	uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1017c1379625SJustin T. Gibbs 	uint64_t new_accounted =
1018c1379625SJustin T. Gibbs 	    MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1019fa9e4066Sahrens 	return (new_accounted - old_accounted);
1020fa9e4066Sahrens }
1021fa9e4066Sahrens 
1022fa9e4066Sahrens void
dsl_dir_sync(dsl_dir_t * dd,dmu_tx_t * tx)1023fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1024fa9e4066Sahrens {
1025fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1028fb09f5aaSMadhav Suresh 	ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1029fa9e4066Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1030fa9e4066Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1031fa9e4066Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1032fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1033fa9e4066Sahrens 
1034fa9e4066Sahrens 	/* release the hold from dsl_dir_dirty */
1035ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
1036fa9e4066Sahrens }
1037fa9e4066Sahrens 
1038fa9e4066Sahrens static uint64_t
dsl_dir_space_towrite(dsl_dir_t * dd)1039a9799022Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd)
1040fa9e4066Sahrens {
1041a9799022Sck153898 	uint64_t space = 0;
1042fa9e4066Sahrens 	int i;
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
1045fa9e4066Sahrens 
1046fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1047fa9e4066Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
1048fa9e4066Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1049fa9e4066Sahrens 	}
1050fa9e4066Sahrens 	return (space);
1051fa9e4066Sahrens }
1052fa9e4066Sahrens 
1053fa9e4066Sahrens /*
1054fa9e4066Sahrens  * How much space would dd have available if ancestor had delta applied
1055fa9e4066Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
1056fa9e4066Sahrens  * on-disk, not estimated pending changes.
1057fa9e4066Sahrens  */
1058a2eea2e1Sahrens uint64_t
dsl_dir_space_available(dsl_dir_t * dd,dsl_dir_t * ancestor,int64_t delta,int ondiskonly)1059fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd,
1060fa9e4066Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1061fa9e4066Sahrens {
1062fa9e4066Sahrens 	uint64_t parentspace, myspace, quota, used;
1063fa9e4066Sahrens 
1064fa9e4066Sahrens 	/*
1065fa9e4066Sahrens 	 * If there are no restrictions otherwise, assume we have
1066fa9e4066Sahrens 	 * unlimited space available.
1067fa9e4066Sahrens 	 */
1068fa9e4066Sahrens 	quota = UINT64_MAX;
1069fa9e4066Sahrens 	parentspace = UINT64_MAX;
1070fa9e4066Sahrens 
1071fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1072fa9e4066Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
1073fa9e4066Sahrens 		    ancestor, delta, ondiskonly);
1074fa9e4066Sahrens 	}
1075fa9e4066Sahrens 
1076fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1077c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_quota != 0)
1078c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1079c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1080a9799022Sck153898 	if (!ondiskonly)
1081a9799022Sck153898 		used += dsl_dir_space_towrite(dd);
1082fa9e4066Sahrens 
1083fa9e4066Sahrens 	if (dd->dd_parent == NULL) {
108499653d4eSeschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1085fa9e4066Sahrens 		quota = MIN(quota, poolsize);
1086fa9e4066Sahrens 	}
1087fa9e4066Sahrens 
1088c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1089fa9e4066Sahrens 		/*
1090fa9e4066Sahrens 		 * We have some space reserved, in addition to what our
1091fa9e4066Sahrens 		 * parent gave us.
1092fa9e4066Sahrens 		 */
1093c1379625SJustin T. Gibbs 		parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1094fa9e4066Sahrens 	}
1095fa9e4066Sahrens 
109674e7dc98SMatthew Ahrens 	if (dd == ancestor) {
109774e7dc98SMatthew Ahrens 		ASSERT(delta <= 0);
109874e7dc98SMatthew Ahrens 		ASSERT(used >= -delta);
109974e7dc98SMatthew Ahrens 		used += delta;
110074e7dc98SMatthew Ahrens 		if (parentspace != UINT64_MAX)
110174e7dc98SMatthew Ahrens 			parentspace -= delta;
110274e7dc98SMatthew Ahrens 	}
110374e7dc98SMatthew Ahrens 
1104fa9e4066Sahrens 	if (used > quota) {
1105fa9e4066Sahrens 		/* over quota */
1106fa9e4066Sahrens 		myspace = 0;
1107fa9e4066Sahrens 	} else {
1108fa9e4066Sahrens 		/*
110999653d4eSeschrock 		 * the lesser of the space provided by our parent and
111099653d4eSeschrock 		 * the space left in our quota
1111fa9e4066Sahrens 		 */
1112fa9e4066Sahrens 		myspace = MIN(parentspace, quota - used);
1113fa9e4066Sahrens 	}
1114fa9e4066Sahrens 
1115fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1116fa9e4066Sahrens 
1117fa9e4066Sahrens 	return (myspace);
1118fa9e4066Sahrens }
1119fa9e4066Sahrens 
1120fa9e4066Sahrens struct tempreserve {
1121fa9e4066Sahrens 	list_node_t tr_node;
1122fa9e4066Sahrens 	dsl_dir_t *tr_ds;
1123fa9e4066Sahrens 	uint64_t tr_size;
1124fa9e4066Sahrens };
1125fa9e4066Sahrens 
1126fa9e4066Sahrens 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)1127a9799022Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1128a9799022Sck153898     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
11296df6c3bcSck153898     dmu_tx_t *tx, boolean_t first)
1130fa9e4066Sahrens {
1131fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
1132a9799022Sck153898 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1133468c413aSTim Haley 	uint64_t deferred = 0;
1134a9799022Sck153898 	struct tempreserve *tr;
1135468c413aSTim Haley 	int retval = EDQUOT;
1136fa9e4066Sahrens 	int txgidx = txg & TXG_MASK;
1137fa9e4066Sahrens 	int i;
11389082849eSck153898 	uint64_t ref_rsrv = 0;
1139fa9e4066Sahrens 
1140fa9e4066Sahrens 	ASSERT3U(txg, !=, 0);
1141a9799022Sck153898 	ASSERT3S(asize, >, 0);
1142fa9e4066Sahrens 
1143fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1144a9799022Sck153898 
1145fa9e4066Sahrens 	/*
1146fa9e4066Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
1147fa9e4066Sahrens 	 * when checking for over-quota because they get one free hit.
1148fa9e4066Sahrens 	 */
1149a9799022Sck153898 	est_inflight = dsl_dir_space_towrite(dd);
1150fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++)
1151a9799022Sck153898 		est_inflight += dd->dd_tempreserved[i];
1152c1379625SJustin T. Gibbs 	used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1153fa9e4066Sahrens 
1154f4d2e9e6Smaybee 	/*
11556df6c3bcSck153898 	 * On the first iteration, fetch the dataset's used-on-disk and
11566df6c3bcSck153898 	 * refreservation values. Also, if checkrefquota is set, test if
11576df6c3bcSck153898 	 * allocating this space would exceed the dataset's refquota.
1158f4d2e9e6Smaybee 	 */
11596df6c3bcSck153898 	if (first && tx->tx_objset) {
1160c3fdb13aSmaybee 		int error;
1161503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
11629082849eSck153898 
1163a9799022Sck153898 		error = dsl_dataset_check_quota(ds, checkrefquota,
11649082849eSck153898 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
1165a9799022Sck153898 		if (error) {
1166a9799022Sck153898 			mutex_exit(&dd->dd_lock);
1167a9799022Sck153898 			return (error);
1168a9799022Sck153898 		}
1169a9799022Sck153898 	}
1170a9799022Sck153898 
1171a9799022Sck153898 	/*
1172a9799022Sck153898 	 * If this transaction will result in a net free of space,
1173a9799022Sck153898 	 * we want to let it through.
1174a9799022Sck153898 	 */
1175c1379625SJustin T. Gibbs 	if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1176fa9e4066Sahrens 		quota = UINT64_MAX;
1177f4d2e9e6Smaybee 	else
1178c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1179fa9e4066Sahrens 
1180fa9e4066Sahrens 	/*
1181468c413aSTim Haley 	 * Adjust the quota against the actual pool size at the root
1182468c413aSTim Haley 	 * minus any outstanding deferred frees.
1183f4d2e9e6Smaybee 	 * To ensure that it's possible to remove files from a full
1184f4d2e9e6Smaybee 	 * pool without inducing transient overcommits, we throttle
1185fa9e4066Sahrens 	 * netfree transactions against a quota that is slightly larger,
1186fa9e4066Sahrens 	 * but still within the pool's allocation slop.  In cases where
1187fa9e4066Sahrens 	 * we're very close to full, this will allow a steady trickle of
1188fa9e4066Sahrens 	 * removes to get through.
1189fa9e4066Sahrens 	 */
11901934e92fSmaybee 	if (dd->dd_parent == NULL) {
1191b24ab676SJeff Bonwick 		spa_t *spa = dd->dd_pool->dp_spa;
1192fa9e4066Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1193b24ab676SJeff Bonwick 		deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1194468c413aSTim Haley 		if (poolsize - deferred < quota) {
1195468c413aSTim Haley 			quota = poolsize - deferred;
1196468c413aSTim Haley 			retval = ENOSPC;
1197fa9e4066Sahrens 		}
1198fa9e4066Sahrens 	}
1199fa9e4066Sahrens 
1200fa9e4066Sahrens 	/*
1201fa9e4066Sahrens 	 * If they are requesting more space, and our current estimate
1202a9799022Sck153898 	 * is over quota, they get to try again unless the actual
1203ea8dc4b6Seschrock 	 * on-disk is over quota and there are no pending changes (which
1204ea8dc4b6Seschrock 	 * may free up space for us).
1205fa9e4066Sahrens 	 */
1206468c413aSTim Haley 	if (used_on_disk + est_inflight >= quota) {
1207d174dbf1SArne Jansen 		if (est_inflight > early_edquot_threshold ||
1208d174dbf1SArne Jansen 		    used_on_disk + early_edquot_threshold < quota ||
1209468c413aSTim Haley 		    (retval == ENOSPC && used_on_disk < quota + deferred))
1210468c413aSTim Haley 			retval = ERESTART;
1211a9799022Sck153898 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1212fa9e4066Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
1213a9799022Sck153898 		    used_on_disk>>10, est_inflight>>10,
1214468c413aSTim Haley 		    quota>>10, asize>>10, retval);
1215fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
1216be6fd75aSMatthew Ahrens 		return (SET_ERROR(retval));
1217fa9e4066Sahrens 	}
1218fa9e4066Sahrens 
1219fa9e4066Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
1220fa9e4066Sahrens 	dd->dd_tempreserved[txgidx] += asize;
1221fa9e4066Sahrens 
12229082849eSck153898 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
12239082849eSck153898 	    asize - ref_rsrv);
1224fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1225fa9e4066Sahrens 
12261ab7f2deSmaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1227fa9e4066Sahrens 	tr->tr_ds = dd;
1228fa9e4066Sahrens 	tr->tr_size = asize;
1229fa9e4066Sahrens 	list_insert_tail(tr_list, tr);
1230fa9e4066Sahrens 
1231fa9e4066Sahrens 	/* see if it's OK with our parent */
12321934e92fSmaybee 	if (dd->dd_parent && parent_rsrv) {
1233c1379625SJustin T. Gibbs 		boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
12341934e92fSmaybee 
1235fa9e4066Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
12366df6c3bcSck153898 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1237fa9e4066Sahrens 	} else {
1238fa9e4066Sahrens 		return (0);
1239fa9e4066Sahrens 	}
1240fa9e4066Sahrens }
1241fa9e4066Sahrens 
1242fa9e4066Sahrens /*
1243fa9e4066Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
1244a9799022Sck153898  * After the space has been dirtied (and dsl_dir_willuse_space()
1245a9799022Sck153898  * has been called), the reservation should be canceled, using
1246a9799022Sck153898  * dsl_dir_tempreserve_clear().
1247fa9e4066Sahrens  */
1248fa9e4066Sahrens 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)1249a9799022Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1250a9799022Sck153898     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1251fa9e4066Sahrens {
12521ab7f2deSmaybee 	int err;
1253fa9e4066Sahrens 	list_t *tr_list;
1254fa9e4066Sahrens 
1255a9799022Sck153898 	if (asize == 0) {
1256a9799022Sck153898 		*tr_cookiep = NULL;
1257a9799022Sck153898 		return (0);
1258a9799022Sck153898 	}
1259a9799022Sck153898 
1260fa9e4066Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1261fa9e4066Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
1262fa9e4066Sahrens 	    offsetof(struct tempreserve, tr_node));
1263a9799022Sck153898 	ASSERT3S(asize, >, 0);
1264ea8dc4b6Seschrock 	ASSERT3S(fsize, >=, 0);
1265fa9e4066Sahrens 
12661ab7f2deSmaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
12671ab7f2deSmaybee 	if (err == 0) {
12681ab7f2deSmaybee 		struct tempreserve *tr;
12691ab7f2deSmaybee 
12701ab7f2deSmaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
12711ab7f2deSmaybee 		tr->tr_size = lsize;
12721ab7f2deSmaybee 		list_insert_tail(tr_list, tr);
12731ab7f2deSmaybee 	} else {
12741ab7f2deSmaybee 		if (err == EAGAIN) {
127569962b56SMatthew Ahrens 			/*
127669962b56SMatthew Ahrens 			 * If arc_memory_throttle() detected that pageout
127769962b56SMatthew Ahrens 			 * is running and we are low on memory, we delay new
127869962b56SMatthew Ahrens 			 * non-pageout transactions to give pageout an
127969962b56SMatthew Ahrens 			 * advantage.
128069962b56SMatthew Ahrens 			 *
128169962b56SMatthew Ahrens 			 * It is unfortunate to be delaying while the caller's
128269962b56SMatthew Ahrens 			 * locks are held.
128369962b56SMatthew Ahrens 			 */
12840689f76cSAdam Leventhal 			txg_delay(dd->dd_pool, tx->tx_txg,
12850689f76cSAdam Leventhal 			    MSEC2NSEC(10), MSEC2NSEC(10));
1286be6fd75aSMatthew Ahrens 			err = SET_ERROR(ERESTART);
12871ab7f2deSmaybee 		}
12881ab7f2deSmaybee 	}
1289fa9e4066Sahrens 
1290fa9e4066Sahrens 	if (err == 0) {
12911ab7f2deSmaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
12926df6c3bcSck153898 		    FALSE, asize > usize, tr_list, tx, TRUE);
1293fa9e4066Sahrens 	}
1294fa9e4066Sahrens 
12953b2aab18SMatthew Ahrens 	if (err != 0)
1296fa9e4066Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
1297fa9e4066Sahrens 	else
1298fa9e4066Sahrens 		*tr_cookiep = tr_list;
12991ab7f2deSmaybee 
1300fa9e4066Sahrens 	return (err);
1301fa9e4066Sahrens }
1302fa9e4066Sahrens 
1303fa9e4066Sahrens /*
1304fa9e4066Sahrens  * Clear a temporary reservation that we previously made with
1305fa9e4066Sahrens  * dsl_dir_tempreserve_space().
1306fa9e4066Sahrens  */
1307fa9e4066Sahrens void
dsl_dir_tempreserve_clear(void * tr_cookie,dmu_tx_t * tx)1308fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1309fa9e4066Sahrens {
1310fa9e4066Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
1311fa9e4066Sahrens 	list_t *tr_list = tr_cookie;
1312fa9e4066Sahrens 	struct tempreserve *tr;
1313fa9e4066Sahrens 
1314fa9e4066Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
1315fa9e4066Sahrens 
1316a9799022Sck153898 	if (tr_cookie == NULL)
1317a9799022Sck153898 		return;
1318a9799022Sck153898 
131969962b56SMatthew Ahrens 	while ((tr = list_head(tr_list)) != NULL) {
132069962b56SMatthew Ahrens 		if (tr->tr_ds) {
1321fa9e4066Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
1322fa9e4066Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1323fa9e4066Sahrens 			    tr->tr_size);
1324fa9e4066Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1325fa9e4066Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
13261ab7f2deSmaybee 		} else {
13271ab7f2deSmaybee 			arc_tempreserve_clear(tr->tr_size);
1328fa9e4066Sahrens 		}
1329fa9e4066Sahrens 		list_remove(tr_list, tr);
1330fa9e4066Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
1331fa9e4066Sahrens 	}
1332fa9e4066Sahrens 
1333fa9e4066Sahrens 	kmem_free(tr_list, sizeof (list_t));
1334fa9e4066Sahrens }
1335fa9e4066Sahrens 
133669962b56SMatthew Ahrens /*
133769962b56SMatthew Ahrens  * This should be called from open context when we think we're going to write
133869962b56SMatthew Ahrens  * or free space, for example when dirtying data. Be conservative; it's okay
133969962b56SMatthew Ahrens  * to write less space or free more, but we don't want to write more or free
134069962b56SMatthew Ahrens  * less than the amount specified.
134169962b56SMatthew Ahrens  */
134269962b56SMatthew Ahrens void
dsl_dir_willuse_space(dsl_dir_t * dd,int64_t space,dmu_tx_t * tx)134369962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1344fa9e4066Sahrens {
1345fa9e4066Sahrens 	int64_t parent_space;
1346fa9e4066Sahrens 	uint64_t est_used;
1347fa9e4066Sahrens 
1348fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1349fa9e4066Sahrens 	if (space > 0)
1350fa9e4066Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1351fa9e4066Sahrens 
1352c1379625SJustin T. Gibbs 	est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1353fa9e4066Sahrens 	parent_space = parent_delta(dd, est_used, space);
1354fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1355fa9e4066Sahrens 
1356fa9e4066Sahrens 	/* Make sure that we clean up dd_space_to* */
1357fa9e4066Sahrens 	dsl_dir_dirty(dd, tx);
1358fa9e4066Sahrens 
1359fa9e4066Sahrens 	/* XXX this is potentially expensive and unnecessary... */
1360fa9e4066Sahrens 	if (parent_space && dd->dd_parent)
136169962b56SMatthew Ahrens 		dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1362fa9e4066Sahrens }
1363fa9e4066Sahrens 
1364fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */
1365fa9e4066Sahrens 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)136674e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1367fa9e4066Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1368fa9e4066Sahrens {
1369fa9e4066Sahrens 	int64_t accounted_delta;
1370b62969f8SMatthew Ahrens 
1371b62969f8SMatthew Ahrens 	/*
1372b62969f8SMatthew Ahrens 	 * dsl_dataset_set_refreservation_sync_impl() calls this with
1373b62969f8SMatthew Ahrens 	 * dd_lock held, so that it can atomically update
1374b62969f8SMatthew Ahrens 	 * ds->ds_reserved and the dsl_dir accounting, so that
1375b62969f8SMatthew Ahrens 	 * dsl_dataset_check_quota() can see dataset and dir accounting
1376b62969f8SMatthew Ahrens 	 * consistently.
1377b62969f8SMatthew Ahrens 	 */
137802c8f3f0SMatthew Ahrens 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1379fa9e4066Sahrens 
1380fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
138174e7dc98SMatthew Ahrens 	ASSERT(type < DD_USED_NUM);
1382fa9e4066Sahrens 
1383b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1384b62969f8SMatthew Ahrens 
138502c8f3f0SMatthew Ahrens 	if (needlock)
1386fa9e4066Sahrens 		mutex_enter(&dd->dd_lock);
1387c1379625SJustin T. Gibbs 	accounted_delta =
1388c1379625SJustin T. Gibbs 	    parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1389c1379625SJustin T. Gibbs 	ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1390fa9e4066Sahrens 	ASSERT(compressed >= 0 ||
1391c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1392fa9e4066Sahrens 	ASSERT(uncompressed >= 0 ||
1393c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1394c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_bytes += used;
1395c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1396c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
139774e7dc98SMatthew Ahrens 
1398c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
139974e7dc98SMatthew Ahrens 		ASSERT(used > 0 ||
1400c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1401c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
140274e7dc98SMatthew Ahrens #ifdef DEBUG
140374e7dc98SMatthew Ahrens 		dd_used_t t;
140474e7dc98SMatthew Ahrens 		uint64_t u = 0;
140574e7dc98SMatthew Ahrens 		for (t = 0; t < DD_USED_NUM; t++)
1406c1379625SJustin T. Gibbs 			u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1407c1379625SJustin T. Gibbs 		ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
140874e7dc98SMatthew Ahrens #endif
140974e7dc98SMatthew Ahrens 	}
141002c8f3f0SMatthew Ahrens 	if (needlock)
1411fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
1412fa9e4066Sahrens 
1413fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
141474e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1415fa9e4066Sahrens 		    accounted_delta, compressed, uncompressed, tx);
141674e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(dd->dd_parent,
141774e7dc98SMatthew Ahrens 		    used - accounted_delta,
141874e7dc98SMatthew Ahrens 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1419fa9e4066Sahrens 	}
1420fa9e4066Sahrens }
1421fa9e4066Sahrens 
142274e7dc98SMatthew 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)142374e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
142474e7dc98SMatthew Ahrens     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
142574e7dc98SMatthew Ahrens {
142674e7dc98SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
142774e7dc98SMatthew Ahrens 	ASSERT(oldtype < DD_USED_NUM);
142874e7dc98SMatthew Ahrens 	ASSERT(newtype < DD_USED_NUM);
142974e7dc98SMatthew Ahrens 
1430c1379625SJustin T. Gibbs 	if (delta == 0 ||
1431c1379625SJustin T. Gibbs 	    !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
143274e7dc98SMatthew Ahrens 		return;
143374e7dc98SMatthew Ahrens 
1434b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
143574e7dc98SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
143674e7dc98SMatthew Ahrens 	ASSERT(delta > 0 ?
1437c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1438c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1439c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1440c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1441c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
144274e7dc98SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
144374e7dc98SMatthew Ahrens }
144474e7dc98SMatthew Ahrens 
14453b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg {
14463b2aab18SMatthew Ahrens 	const char *ddsqra_name;
14473b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
14483b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
14493b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t;
14503b2aab18SMatthew Ahrens 
1451fa9e4066Sahrens static int
dsl_dir_set_quota_check(void * arg,dmu_tx_t * tx)14523b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1453fa9e4066Sahrens {
14543b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14553b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14563b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14573b2aab18SMatthew Ahrens 	int error;
14583b2aab18SMatthew Ahrens 	uint64_t towrite, newval;
14591d452cf5Sahrens 
14603b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
14613b2aab18SMatthew Ahrens 	if (error != 0)
14623b2aab18SMatthew Ahrens 		return (error);
146392241e0bSTom Erickson 
14643b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir, "quota",
14653b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
14663b2aab18SMatthew Ahrens 	if (error != 0) {
14673b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14683b2aab18SMatthew Ahrens 		return (error);
14693b2aab18SMatthew Ahrens 	}
14703b2aab18SMatthew Ahrens 
14713b2aab18SMatthew Ahrens 	if (newval == 0) {
14723b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14731d452cf5Sahrens 		return (0);
14743b2aab18SMatthew Ahrens 	}
14751d452cf5Sahrens 
14763b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
14771d452cf5Sahrens 	/*
14781d452cf5Sahrens 	 * If we are doing the preliminary check in open context, and
14791d452cf5Sahrens 	 * there are pending changes, then don't fail it, since the
1480a9799022Sck153898 	 * pending changes could under-estimate the amount of space to be
14811d452cf5Sahrens 	 * freed up.
14821d452cf5Sahrens 	 */
14833b2aab18SMatthew Ahrens 	towrite = dsl_dir_space_towrite(ds->ds_dir);
14841d452cf5Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1485c1379625SJustin T. Gibbs 	    (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1486c1379625SJustin T. Gibbs 	    newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1487be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOSPC);
14881d452cf5Sahrens 	}
14893b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
14903b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14913b2aab18SMatthew Ahrens 	return (error);
14921d452cf5Sahrens }
14931d452cf5Sahrens 
14941d452cf5Sahrens static void
dsl_dir_set_quota_sync(void * arg,dmu_tx_t * tx)14953b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
14961d452cf5Sahrens {
14973b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14983b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14993b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15003b2aab18SMatthew Ahrens 	uint64_t newval;
150192241e0bSTom Erickson 
15023b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1503fa9e4066Sahrens 
1504013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
15053b2aab18SMatthew Ahrens 		dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
15063b2aab18SMatthew Ahrens 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
15073b2aab18SMatthew Ahrens 		    &ddsqra->ddsqra_value, tx);
1508fa9e4066Sahrens 
15093b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_get_int_ds(ds,
15103b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1511013023d4SMartin Matuska 	} else {
1512013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1513013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1514013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1515013023d4SMartin Matuska 	}
15163b2aab18SMatthew Ahrens 
15173b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
15183b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
1519c1379625SJustin T. Gibbs 	dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
15203b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
15213b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1522fa9e4066Sahrens }
1523fa9e4066Sahrens 
1524fa9e4066Sahrens int
dsl_dir_set_quota(const char * ddname,zprop_source_t source,uint64_t quota)152592241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1526fa9e4066Sahrens {
15273b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
1528fa9e4066Sahrens 
15293b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
15303b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
15313b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = quota;
153292241e0bSTom Erickson 
15333b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
15347d46dc6cSMatthew Ahrens 	    dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1535fa9e4066Sahrens }
1536fa9e4066Sahrens 
1537a9799022Sck153898 int
dsl_dir_set_reservation_check(void * arg,dmu_tx_t * tx)15383b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1539fa9e4066Sahrens {
15403b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
15413b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15423b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15433b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15443b2aab18SMatthew Ahrens 	uint64_t newval, used, avail;
15453b2aab18SMatthew Ahrens 	int error;
154692241e0bSTom Erickson 
15473b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
15483b2aab18SMatthew Ahrens 	if (error != 0)
15493b2aab18SMatthew Ahrens 		return (error);
15503b2aab18SMatthew Ahrens 	dd = ds->ds_dir;
1551fa9e4066Sahrens 
15521d452cf5Sahrens 	/*
15531d452cf5Sahrens 	 * If we are doing the preliminary check in open context, the
15541d452cf5Sahrens 	 * space estimates may be inaccurate.
15551d452cf5Sahrens 	 */
15563b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
15573b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15581d452cf5Sahrens 		return (0);
15593b2aab18SMatthew Ahrens 	}
15603b2aab18SMatthew Ahrens 
15613b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
15623b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_RESERVATION),
15633b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
15643b2aab18SMatthew Ahrens 	if (error != 0) {
15653b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15663b2aab18SMatthew Ahrens 		return (error);
15673b2aab18SMatthew Ahrens 	}
15681d452cf5Sahrens 
1569fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1570c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1571fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1572fa9e4066Sahrens 
1573fa9e4066Sahrens 	if (dd->dd_parent) {
1574fa9e4066Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1575fa9e4066Sahrens 		    NULL, 0, FALSE);
1576fa9e4066Sahrens 	} else {
1577fa9e4066Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1578fa9e4066Sahrens 	}
1579fa9e4066Sahrens 
1580c1379625SJustin T. Gibbs 	if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
15813b2aab18SMatthew Ahrens 		uint64_t delta = MAX(used, newval) -
1582c1379625SJustin T. Gibbs 		    MAX(used, dsl_dir_phys(dd)->dd_reserved);
1583379c004dSEric Schrock 
15843b2aab18SMatthew Ahrens 		if (delta > avail ||
1585c1379625SJustin T. Gibbs 		    (dsl_dir_phys(dd)->dd_quota > 0 &&
1586c1379625SJustin T. Gibbs 		    newval > dsl_dir_phys(dd)->dd_quota))
1587be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENOSPC);
1588379c004dSEric Schrock 	}
1589379c004dSEric Schrock 
15903b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15913b2aab18SMatthew Ahrens 	return (error);
15921d452cf5Sahrens }
15931d452cf5Sahrens 
15943b2aab18SMatthew Ahrens void
dsl_dir_set_reservation_sync_impl(dsl_dir_t * dd,uint64_t value,dmu_tx_t * tx)15954445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
15961d452cf5Sahrens {
15971d452cf5Sahrens 	uint64_t used;
15981d452cf5Sahrens 	int64_t delta;
15991d452cf5Sahrens 
1600a9799022Sck153898 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1601a9799022Sck153898 
16021d452cf5Sahrens 	mutex_enter(&dd->dd_lock);
1603c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1604c1379625SJustin T. Gibbs 	delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1605c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_reserved = value;
1606fa9e4066Sahrens 
1607fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1608fa9e4066Sahrens 		/* Roll up this additional usage into our ancestors */
160974e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
161074e7dc98SMatthew Ahrens 		    delta, 0, 0, tx);
1611fa9e4066Sahrens 	}
161202c8f3f0SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
16134445fffbSMatthew Ahrens }
1614ecd6cf80Smarks 
16154445fffbSMatthew Ahrens 
16164445fffbSMatthew Ahrens static void
dsl_dir_set_reservation_sync(void * arg,dmu_tx_t * tx)16173b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
16184445fffbSMatthew Ahrens {
16193b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
16203b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16213b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16223b2aab18SMatthew Ahrens 	uint64_t newval;
16234445fffbSMatthew Ahrens 
16243b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
16254445fffbSMatthew Ahrens 
1626013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1627013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds,
1628013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
16293b2aab18SMatthew Ahrens 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
16303b2aab18SMatthew Ahrens 		    &ddsqra->ddsqra_value, tx);
16313b2aab18SMatthew Ahrens 
16323b2aab18SMatthew Ahrens 		VERIFY0(dsl_prop_get_int_ds(ds,
16333b2aab18SMatthew Ahrens 		    zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1634013023d4SMartin Matuska 	} else {
1635013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1636013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1637013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1638013023d4SMartin Matuska 		    (longlong_t)newval);
1639013023d4SMartin Matuska 	}
16403b2aab18SMatthew Ahrens 
16413b2aab18SMatthew Ahrens 	dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
16423b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1643fa9e4066Sahrens }
1644fa9e4066Sahrens 
1645fa9e4066Sahrens int
dsl_dir_set_reservation(const char * ddname,zprop_source_t source,uint64_t reservation)164692241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
164792241e0bSTom Erickson     uint64_t reservation)
1648fa9e4066Sahrens {
16493b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
1650fa9e4066Sahrens 
16513b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
16523b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
16533b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = reservation;
165492241e0bSTom Erickson 
16553b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
16567d46dc6cSMatthew Ahrens 	    dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1657fa9e4066Sahrens }
1658fa9e4066Sahrens 
1659fa9e4066Sahrens static dsl_dir_t *
closest_common_ancestor(dsl_dir_t * ds1,dsl_dir_t * ds2)1660fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1661fa9e4066Sahrens {
1662fa9e4066Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1663fa9e4066Sahrens 		dsl_dir_t *dd;
1664fa9e4066Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1665fa9e4066Sahrens 			if (ds1 == dd)
1666fa9e4066Sahrens 				return (dd);
1667fa9e4066Sahrens 		}
1668fa9e4066Sahrens 	}
1669fa9e4066Sahrens 	return (NULL);
1670fa9e4066Sahrens }
1671fa9e4066Sahrens 
1672fa9e4066Sahrens /*
1673fa9e4066Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1674fa9e4066Sahrens  * ancestor?  Syncing context only.
1675fa9e4066Sahrens  */
1676fa9e4066Sahrens static int64_t
would_change(dsl_dir_t * dd,int64_t delta,dsl_dir_t * ancestor)1677fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1678fa9e4066Sahrens {
1679fa9e4066Sahrens 	if (dd == ancestor)
1680fa9e4066Sahrens 		return (delta);
1681fa9e4066Sahrens 
1682fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1683c1379625SJustin T. Gibbs 	delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1684fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1685fa9e4066Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1686fa9e4066Sahrens }
1687fa9e4066Sahrens 
16883b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg {
16893b2aab18SMatthew Ahrens 	const char *ddra_oldname;
16903b2aab18SMatthew Ahrens 	const char *ddra_newname;
1691a2afb611SJerry Jelinek 	cred_t *ddra_cred;
16923b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t;
16933b2aab18SMatthew Ahrens 
16943b2aab18SMatthew Ahrens /* ARGSUSED */
16953b2aab18SMatthew Ahrens static int
dsl_valid_rename(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)16963b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
16973b2aab18SMatthew Ahrens {
16983b2aab18SMatthew Ahrens 	int *deltap = arg;
1699a1988827SMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
17003b2aab18SMatthew Ahrens 
17013b2aab18SMatthew Ahrens 	dsl_dataset_name(ds, namebuf);
17023b2aab18SMatthew Ahrens 
1703a1988827SMatthew Ahrens 	if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN)
1704be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
17053b2aab18SMatthew Ahrens 	return (0);
17063b2aab18SMatthew Ahrens }
17071d452cf5Sahrens 
17081d452cf5Sahrens static int
dsl_dir_rename_check(void * arg,dmu_tx_t * tx)17093b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1710fa9e4066Sahrens {
17113b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
17123b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17133b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
17143b2aab18SMatthew Ahrens 	const char *mynewname;
17153b2aab18SMatthew Ahrens 	int error;
17163b2aab18SMatthew Ahrens 	int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1717fa9e4066Sahrens 
17183b2aab18SMatthew Ahrens 	/* target dir should exist */
17193b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
17203b2aab18SMatthew Ahrens 	if (error != 0)
17213b2aab18SMatthew Ahrens 		return (error);
1722fa9e4066Sahrens 
17233b2aab18SMatthew Ahrens 	/* new parent should exist */
17243b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
17253b2aab18SMatthew Ahrens 	    &newparent, &mynewname);
17263b2aab18SMatthew Ahrens 	if (error != 0) {
17273b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
17283b2aab18SMatthew Ahrens 		return (error);
17293b2aab18SMatthew Ahrens 	}
17303b2aab18SMatthew Ahrens 
17313b2aab18SMatthew Ahrens 	/* can't rename to different pool */
17323b2aab18SMatthew Ahrens 	if (dd->dd_pool != newparent->dd_pool) {
17333b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17343b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1735be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
17363b2aab18SMatthew Ahrens 	}
17373b2aab18SMatthew Ahrens 
17383b2aab18SMatthew Ahrens 	/* new name should not already exist */
17393b2aab18SMatthew Ahrens 	if (mynewname == NULL) {
17403b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17413b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1742be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
17433b2aab18SMatthew Ahrens 	}
17441d452cf5Sahrens 
17453b2aab18SMatthew Ahrens 	/* if the name length is growing, validate child name lengths */
17463b2aab18SMatthew Ahrens 	if (delta > 0) {
17473b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
17483b2aab18SMatthew Ahrens 		    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
17493b2aab18SMatthew Ahrens 		if (error != 0) {
17503b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17513b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17523b2aab18SMatthew Ahrens 			return (error);
17533b2aab18SMatthew Ahrens 		}
17543b2aab18SMatthew Ahrens 	}
17553b2aab18SMatthew Ahrens 
1756a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1757adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1758a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1759a2afb611SJerry Jelinek 			/*
1760a2afb611SJerry Jelinek 			 * Although this is the check function and we don't
1761a2afb611SJerry Jelinek 			 * normally make on-disk changes in check functions,
1762a2afb611SJerry Jelinek 			 * we need to do that here.
1763a2afb611SJerry Jelinek 			 *
1764a2afb611SJerry Jelinek 			 * Ensure this portion of the tree's counts have been
1765a2afb611SJerry Jelinek 			 * initialized in case the new parent has limits set.
1766a2afb611SJerry Jelinek 			 */
1767a2afb611SJerry Jelinek 			dsl_dir_init_fs_ss_count(dd, tx);
1768a2afb611SJerry Jelinek 		}
1769a2afb611SJerry Jelinek 	}
1770a2afb611SJerry Jelinek 
17713b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
177299653d4eSeschrock 		/* is there enough space? */
177399653d4eSeschrock 		uint64_t myspace =
1774c1379625SJustin T. Gibbs 		    MAX(dsl_dir_phys(dd)->dd_used_bytes,
1775c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_reserved);
1776a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1777a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1778a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1779a2afb611SJerry Jelinek 
1780a2afb611SJerry Jelinek 		if (dsl_dir_is_zapified(dd)) {
1781a2afb611SJerry Jelinek 			int err;
1782a2afb611SJerry Jelinek 
1783a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1784a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1785a2afb611SJerry Jelinek 			    &fs_cnt);
1786adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1787adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1788adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1789a2afb611SJerry Jelinek 				return (err);
1790adf34077SJerry Jelinek 			}
1791a2afb611SJerry Jelinek 
1792a2afb611SJerry Jelinek 			/*
1793a2afb611SJerry Jelinek 			 * have to add 1 for the filesystem itself that we're
1794a2afb611SJerry Jelinek 			 * moving
1795a2afb611SJerry Jelinek 			 */
1796a2afb611SJerry Jelinek 			fs_cnt++;
1797a2afb611SJerry Jelinek 
1798a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1799a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1800a2afb611SJerry Jelinek 			    &ss_cnt);
1801adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1802adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1803adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1804a2afb611SJerry Jelinek 				return (err);
1805a2afb611SJerry Jelinek 			}
1806adf34077SJerry Jelinek 		}
1807fa9e4066Sahrens 
18081d452cf5Sahrens 		/* no rename into our descendant */
18093b2aab18SMatthew Ahrens 		if (closest_common_ancestor(dd, newparent) == dd) {
18103b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
18113b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
1812be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
1813fa9e4066Sahrens 		}
1814fa9e4066Sahrens 
18153b2aab18SMatthew Ahrens 		error = dsl_dir_transfer_possible(dd->dd_parent,
1816a2afb611SJerry Jelinek 		    newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
18173b2aab18SMatthew Ahrens 		if (error != 0) {
18183b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
18193b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
18203b2aab18SMatthew Ahrens 			return (error);
18213b2aab18SMatthew Ahrens 		}
18223b2aab18SMatthew Ahrens 	}
18233b2aab18SMatthew Ahrens 
18243b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
18253b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
18261d452cf5Sahrens 	return (0);
18271d452cf5Sahrens }
18281d452cf5Sahrens 
18291d452cf5Sahrens static void
dsl_dir_rename_sync(void * arg,dmu_tx_t * tx)18303b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
18311d452cf5Sahrens {
18323b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
18333b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18343b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
18353b2aab18SMatthew Ahrens 	const char *mynewname;
18363b2aab18SMatthew Ahrens 	int error;
18371d452cf5Sahrens 	objset_t *mos = dp->dp_meta_objset;
18381d452cf5Sahrens 
18393b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
18403b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
18413b2aab18SMatthew Ahrens 	    &mynewname));
18421d452cf5Sahrens 
18434445fffbSMatthew Ahrens 	/* Log this before we change the name. */
18444445fffbSMatthew Ahrens 	spa_history_log_internal_dd(dd, "rename", tx,
18453b2aab18SMatthew Ahrens 	    "-> %s", ddra->ddra_newname);
18464445fffbSMatthew Ahrens 
18473b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
1848a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1849a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1850a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1851a2afb611SJerry Jelinek 
1852a2afb611SJerry Jelinek 		/*
1853a2afb611SJerry Jelinek 		 * We already made sure the dd counts were initialized in the
1854a2afb611SJerry Jelinek 		 * check function.
1855a2afb611SJerry Jelinek 		 */
1856adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1857a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1858a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1859a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1860a2afb611SJerry Jelinek 			    &fs_cnt));
1861a2afb611SJerry Jelinek 			/* add 1 for the filesystem itself that we're moving */
1862a2afb611SJerry Jelinek 			fs_cnt++;
1863a2afb611SJerry Jelinek 
1864a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1865a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1866a2afb611SJerry Jelinek 			    &ss_cnt));
1867a2afb611SJerry Jelinek 		}
1868a2afb611SJerry Jelinek 
1869a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1870a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1871a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, fs_cnt,
1872a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1873a2afb611SJerry Jelinek 
1874a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1875a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1876a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, ss_cnt,
1877a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1878a2afb611SJerry Jelinek 
187974e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, 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);
18833b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1884c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes,
1885c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_compressed_bytes,
1886c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
188774e7dc98SMatthew Ahrens 
1888c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_reserved >
1889c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes) {
1890c1379625SJustin T. Gibbs 			uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1891c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_used_bytes;
189274e7dc98SMatthew Ahrens 
189374e7dc98SMatthew Ahrens 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
189474e7dc98SMatthew Ahrens 			    -unused_rsrv, 0, 0, tx);
18953b2aab18SMatthew Ahrens 			dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
189674e7dc98SMatthew Ahrens 			    unused_rsrv, 0, 0, tx);
189774e7dc98SMatthew Ahrens 		}
1898fa9e4066Sahrens 	}
1899fa9e4066Sahrens 
1900fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1901fa9e4066Sahrens 
1902fa9e4066Sahrens 	/* remove from old parent zapobj */
1903c1379625SJustin T. Gibbs 	error = zap_remove(mos,
1904c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1905fa9e4066Sahrens 	    dd->dd_myname, tx);
19063b2aab18SMatthew Ahrens 	ASSERT0(error);
1907fa9e4066Sahrens 
19083b2aab18SMatthew Ahrens 	(void) strcpy(dd->dd_myname, mynewname);
19093b2aab18SMatthew Ahrens 	dsl_dir_rele(dd->dd_parent, dd);
1910c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
19113b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp,
19123b2aab18SMatthew Ahrens 	    newparent->dd_object, NULL, dd, &dd->dd_parent));
1913fa9e4066Sahrens 
1914fa9e4066Sahrens 	/* add to new parent zapobj */
1915c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
19163b2aab18SMatthew Ahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx));
1917ecd6cf80Smarks 
19183b2aab18SMatthew Ahrens 	dsl_prop_notify_all(dd);
19193b2aab18SMatthew Ahrens 
19203b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
19213b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
19221d452cf5Sahrens }
1923fa9e4066Sahrens 
19241d452cf5Sahrens int
dsl_dir_rename(const char * oldname,const char * newname)19253b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname)
19261d452cf5Sahrens {
19273b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t ddra;
19281d452cf5Sahrens 
19293b2aab18SMatthew Ahrens 	ddra.ddra_oldname = oldname;
19303b2aab18SMatthew Ahrens 	ddra.ddra_newname = newname;
1931a2afb611SJerry Jelinek 	ddra.ddra_cred = CRED();
19321d452cf5Sahrens 
19333b2aab18SMatthew Ahrens 	return (dsl_sync_task(oldname,
19347d46dc6cSMatthew Ahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
19357d46dc6cSMatthew Ahrens 	    3, ZFS_SPACE_CHECK_RESERVED));
1936fa9e4066Sahrens }
193799653d4eSeschrock 
193899653d4eSeschrock 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)1939a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1940a2afb611SJerry Jelinek     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
194199653d4eSeschrock {
194299653d4eSeschrock 	dsl_dir_t *ancestor;
194399653d4eSeschrock 	int64_t adelta;
194499653d4eSeschrock 	uint64_t avail;
1945a2afb611SJerry Jelinek 	int err;
194699653d4eSeschrock 
194799653d4eSeschrock 	ancestor = closest_common_ancestor(sdd, tdd);
194899653d4eSeschrock 	adelta = would_change(sdd, -space, ancestor);
194999653d4eSeschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
195099653d4eSeschrock 	if (avail < space)
1951be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
195299653d4eSeschrock 
1953a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1954a2afb611SJerry Jelinek 	    ancestor, cr);
1955a2afb611SJerry Jelinek 	if (err != 0)
1956a2afb611SJerry Jelinek 		return (err);
1957a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1958a2afb611SJerry Jelinek 	    ancestor, cr);
1959a2afb611SJerry Jelinek 	if (err != 0)
1960a2afb611SJerry Jelinek 		return (err);
1961a2afb611SJerry Jelinek 
196299653d4eSeschrock 	return (0);
196399653d4eSeschrock }
196471eb0538SChris Kirby 
196571eb0538SChris Kirby timestruc_t
dsl_dir_snap_cmtime(dsl_dir_t * dd)196671eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd)
196771eb0538SChris Kirby {
196871eb0538SChris Kirby 	timestruc_t t;
196971eb0538SChris Kirby 
197071eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
197171eb0538SChris Kirby 	t = dd->dd_snap_cmtime;
197271eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
197371eb0538SChris Kirby 
197471eb0538SChris Kirby 	return (t);
197571eb0538SChris Kirby }
197671eb0538SChris Kirby 
197771eb0538SChris Kirby void
dsl_dir_snap_cmtime_update(dsl_dir_t * dd)197871eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
197971eb0538SChris Kirby {
198071eb0538SChris Kirby 	timestruc_t t;
198171eb0538SChris Kirby 
198271eb0538SChris Kirby 	gethrestime(&t);
198371eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
198471eb0538SChris Kirby 	dd->dd_snap_cmtime = t;
198571eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
198671eb0538SChris Kirby }
19872acef22dSMatthew Ahrens 
19882acef22dSMatthew Ahrens void
dsl_dir_zapify(dsl_dir_t * dd,dmu_tx_t * tx)19892acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
19902acef22dSMatthew Ahrens {
19912acef22dSMatthew Ahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
19922acef22dSMatthew Ahrens 	dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
19932acef22dSMatthew Ahrens }
1994a2afb611SJerry Jelinek 
1995a2afb611SJerry Jelinek boolean_t
dsl_dir_is_zapified(dsl_dir_t * dd)1996a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd)
1997a2afb611SJerry Jelinek {
1998a2afb611SJerry Jelinek 	dmu_object_info_t doi;
1999a2afb611SJerry Jelinek 
2000a2afb611SJerry Jelinek 	dmu_object_info_from_db(dd->dd_dbuf, &doi);
2001a2afb611SJerry Jelinek 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2002a2afb611SJerry Jelinek }
2003