xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision 03b1c2971d24a9cd2c073d634f7e074fbd14e984)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237d46dc6cSMatthew Ahrens  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24013023d4SMartin Matuska  * Copyright (c) 2013 Martin Matuska. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2014 Joyent, Inc. All rights reserved.
26bc9014e6SJustin Gibbs  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27*03b1c297SAlexander Eremin  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28fa9e4066Sahrens  */
29fa9e4066Sahrens 
30fa9e4066Sahrens #include <sys/dmu.h>
31a9799022Sck #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 
126c1379625SJustin T. Gibbs extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
127c1379625SJustin T. Gibbs 
128a9799022Sck static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
129fa9e4066Sahrens 
130fa9e4066Sahrens static void
131bc9014e6SJustin Gibbs dsl_dir_evict(void *dbu)
132fa9e4066Sahrens {
133bc9014e6SJustin Gibbs 	dsl_dir_t *dd = dbu;
134fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
135fa9e4066Sahrens 	int t;
136fa9e4066Sahrens 
137bc9014e6SJustin Gibbs 	dd->dd_dbuf = NULL;
138bc9014e6SJustin Gibbs 
139fa9e4066Sahrens 	for (t = 0; t < TXG_SIZE; t++) {
140fa9e4066Sahrens 		ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
141fa9e4066Sahrens 		ASSERT(dd->dd_tempreserved[t] == 0);
142fa9e4066Sahrens 		ASSERT(dd->dd_space_towrite[t] == 0);
143fa9e4066Sahrens 	}
144fa9e4066Sahrens 
145fa9e4066Sahrens 	if (dd->dd_parent)
146bc9014e6SJustin Gibbs 		dsl_dir_async_rele(dd->dd_parent, dd);
147fa9e4066Sahrens 
148bc9014e6SJustin Gibbs 	spa_async_close(dd->dd_pool->dp_spa, dd);
149fa9e4066Sahrens 
150fa9e4066Sahrens 	/*
1513f9d6ad7SLin Ling 	 * The props callback list should have been cleaned up by
1523f9d6ad7SLin Ling 	 * objset_evict().
153fa9e4066Sahrens 	 */
154fa9e4066Sahrens 	list_destroy(&dd->dd_prop_cbs);
1555ad82045Snd 	mutex_destroy(&dd->dd_lock);
156fa9e4066Sahrens 	kmem_free(dd, sizeof (dsl_dir_t));
157fa9e4066Sahrens }
158fa9e4066Sahrens 
159ea8dc4b6Seschrock int
1603b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
161ea8dc4b6Seschrock     const char *tail, void *tag, dsl_dir_t **ddp)
162fa9e4066Sahrens {
163fa9e4066Sahrens 	dmu_buf_t *dbuf;
164fa9e4066Sahrens 	dsl_dir_t *dd;
165ea8dc4b6Seschrock 	int err;
166fa9e4066Sahrens 
1673b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
168fa9e4066Sahrens 
169ea8dc4b6Seschrock 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
1703b2aab18SMatthew Ahrens 	if (err != 0)
171ea8dc4b6Seschrock 		return (err);
172fa9e4066Sahrens 	dd = dmu_buf_get_user(dbuf);
173fa9e4066Sahrens #ifdef ZFS_DEBUG
174fa9e4066Sahrens 	{
175fa9e4066Sahrens 		dmu_object_info_t doi;
176fa9e4066Sahrens 		dmu_object_info_from_db(dbuf, &doi);
1772acef22dSMatthew Ahrens 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
17874e7dc98SMatthew Ahrens 		ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
179fa9e4066Sahrens 	}
180fa9e4066Sahrens #endif
181fa9e4066Sahrens 	if (dd == NULL) {
182fa9e4066Sahrens 		dsl_dir_t *winner;
183fa9e4066Sahrens 
184fa9e4066Sahrens 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
185fa9e4066Sahrens 		dd->dd_object = ddobj;
186fa9e4066Sahrens 		dd->dd_dbuf = dbuf;
187fa9e4066Sahrens 		dd->dd_pool = dp;
1885ad82045Snd 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
189fa9e4066Sahrens 
190fa9e4066Sahrens 		list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
191fa9e4066Sahrens 		    offsetof(dsl_prop_cb_record_t, cbr_node));
192fa9e4066Sahrens 
19371eb0538SChris Kirby 		dsl_dir_snap_cmtime_update(dd);
19471eb0538SChris Kirby 
195c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_parent_obj) {
196c1379625SJustin T. Gibbs 			err = dsl_dir_hold_obj(dp,
197c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
198c1379625SJustin T. Gibbs 			    &dd->dd_parent);
1993b2aab18SMatthew Ahrens 			if (err != 0)
20074e7dc98SMatthew Ahrens 				goto errout;
201fa9e4066Sahrens 			if (tail) {
202fa9e4066Sahrens #ifdef ZFS_DEBUG
203fa9e4066Sahrens 				uint64_t foundobj;
204fa9e4066Sahrens 
205fa9e4066Sahrens 				err = zap_lookup(dp->dp_meta_objset,
206c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
207c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj, tail,
208c1379625SJustin T. Gibbs 				    sizeof (foundobj), 1, &foundobj);
209ea8dc4b6Seschrock 				ASSERT(err || foundobj == ddobj);
210fa9e4066Sahrens #endif
211fa9e4066Sahrens 				(void) strcpy(dd->dd_myname, tail);
212fa9e4066Sahrens 			} else {
213fa9e4066Sahrens 				err = zap_value_search(dp->dp_meta_objset,
214c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
215c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj,
216e7437265Sahrens 				    ddobj, 0, dd->dd_myname);
217ea8dc4b6Seschrock 			}
2183b2aab18SMatthew Ahrens 			if (err != 0)
21974e7dc98SMatthew Ahrens 				goto errout;
220fa9e4066Sahrens 		} else {
221fa9e4066Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
222fa9e4066Sahrens 		}
223fa9e4066Sahrens 
2243f9d6ad7SLin Ling 		if (dsl_dir_is_clone(dd)) {
2253f9d6ad7SLin Ling 			dmu_buf_t *origin_bonus;
2263f9d6ad7SLin Ling 			dsl_dataset_phys_t *origin_phys;
2273f9d6ad7SLin Ling 
2283f9d6ad7SLin Ling 			/*
2293f9d6ad7SLin Ling 			 * We can't open the origin dataset, because
2303f9d6ad7SLin Ling 			 * that would require opening this dsl_dir.
2313f9d6ad7SLin Ling 			 * Just look at its phys directly instead.
2323f9d6ad7SLin Ling 			 */
2333f9d6ad7SLin Ling 			err = dmu_bonus_hold(dp->dp_meta_objset,
234c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_origin_obj, FTAG,
235c1379625SJustin T. Gibbs 			    &origin_bonus);
2363b2aab18SMatthew Ahrens 			if (err != 0)
2373f9d6ad7SLin Ling 				goto errout;
2383f9d6ad7SLin Ling 			origin_phys = origin_bonus->db_data;
2393f9d6ad7SLin Ling 			dd->dd_origin_txg =
2403f9d6ad7SLin Ling 			    origin_phys->ds_creation_txg;
2413f9d6ad7SLin Ling 			dmu_buf_rele(origin_bonus, FTAG);
2423f9d6ad7SLin Ling 		}
2433f9d6ad7SLin Ling 
244bc9014e6SJustin Gibbs 		dmu_buf_init_user(&dd->dd_dbu, dsl_dir_evict, &dd->dd_dbuf);
245bc9014e6SJustin Gibbs 		winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
246bc9014e6SJustin Gibbs 		if (winner != NULL) {
247fa9e4066Sahrens 			if (dd->dd_parent)
2483b2aab18SMatthew Ahrens 				dsl_dir_rele(dd->dd_parent, dd);
2495ad82045Snd 			mutex_destroy(&dd->dd_lock);
250fa9e4066Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
251fa9e4066Sahrens 			dd = winner;
252fa9e4066Sahrens 		} else {
253fa9e4066Sahrens 			spa_open_ref(dp->dp_spa, dd);
254fa9e4066Sahrens 		}
255fa9e4066Sahrens 	}
256fa9e4066Sahrens 
257fa9e4066Sahrens 	/*
258fa9e4066Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
259fa9e4066Sahrens 	 * holds on the spa.  We need the open-to-close holds because
260fa9e4066Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
261fa9e4066Sahrens 	 * dir which the spa also has open, so we could incorrectly
262fa9e4066Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
263fa9e4066Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
264fa9e4066Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
265fa9e4066Sahrens 	 */
266fa9e4066Sahrens 	spa_open_ref(dp->dp_spa, tag);
267fa9e4066Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
268fa9e4066Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
269fa9e4066Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
270ea8dc4b6Seschrock 	*ddp = dd;
271ea8dc4b6Seschrock 	return (0);
27274e7dc98SMatthew Ahrens 
27374e7dc98SMatthew Ahrens errout:
27474e7dc98SMatthew Ahrens 	if (dd->dd_parent)
2753b2aab18SMatthew Ahrens 		dsl_dir_rele(dd->dd_parent, dd);
27674e7dc98SMatthew Ahrens 	mutex_destroy(&dd->dd_lock);
27774e7dc98SMatthew Ahrens 	kmem_free(dd, sizeof (dsl_dir_t));
27874e7dc98SMatthew Ahrens 	dmu_buf_rele(dbuf, tag);
27974e7dc98SMatthew Ahrens 	return (err);
280fa9e4066Sahrens }
281fa9e4066Sahrens 
282fa9e4066Sahrens void
2833b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag)
284fa9e4066Sahrens {
285fa9e4066Sahrens 	dprintf_dd(dd, "%s\n", "");
286fa9e4066Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
287ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
288fa9e4066Sahrens }
289fa9e4066Sahrens 
290bc9014e6SJustin Gibbs /*
291bc9014e6SJustin Gibbs  * Remove a reference to the given dsl dir that is being asynchronously
292bc9014e6SJustin Gibbs  * released.  Async releases occur from a taskq performing eviction of
293bc9014e6SJustin Gibbs  * dsl datasets and dirs.  This process is identical to a normal release
294bc9014e6SJustin Gibbs  * with the exception of using the async API for releasing the reference on
295bc9014e6SJustin Gibbs  * the spa.
296bc9014e6SJustin Gibbs  */
297bc9014e6SJustin Gibbs void
298bc9014e6SJustin Gibbs dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
299bc9014e6SJustin Gibbs {
300bc9014e6SJustin Gibbs 	dprintf_dd(dd, "%s\n", "");
301bc9014e6SJustin Gibbs 	spa_async_close(dd->dd_pool->dp_spa, tag);
302bc9014e6SJustin Gibbs 	dmu_buf_rele(dd->dd_dbuf, tag);
303bc9014e6SJustin Gibbs }
304bc9014e6SJustin Gibbs 
30515f66a7fSek /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
306fa9e4066Sahrens void
307fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
308fa9e4066Sahrens {
309fa9e4066Sahrens 	if (dd->dd_parent) {
310fa9e4066Sahrens 		dsl_dir_name(dd->dd_parent, buf);
311fa9e4066Sahrens 		(void) strcat(buf, "/");
312fa9e4066Sahrens 	} else {
313fa9e4066Sahrens 		buf[0] = '\0';
314fa9e4066Sahrens 	}
315fa9e4066Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
316fa9e4066Sahrens 		/*
317fa9e4066Sahrens 		 * recursive mutex so that we can use
318fa9e4066Sahrens 		 * dprintf_dd() with dd_lock held
319fa9e4066Sahrens 		 */
320fa9e4066Sahrens 		mutex_enter(&dd->dd_lock);
321fa9e4066Sahrens 		(void) strcat(buf, dd->dd_myname);
322fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
323fa9e4066Sahrens 	} else {
324fa9e4066Sahrens 		(void) strcat(buf, dd->dd_myname);
325fa9e4066Sahrens 	}
326fa9e4066Sahrens }
327fa9e4066Sahrens 
328ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
329b7661cccSmmusante int
330b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd)
331b7661cccSmmusante {
332b7661cccSmmusante 	int result = 0;
333b7661cccSmmusante 
334b7661cccSmmusante 	if (dd->dd_parent) {
335b7661cccSmmusante 		/* parent's name + 1 for the "/" */
336b7661cccSmmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
337b7661cccSmmusante 	}
338b7661cccSmmusante 
339b7661cccSmmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
340b7661cccSmmusante 		/* see dsl_dir_name */
341b7661cccSmmusante 		mutex_enter(&dd->dd_lock);
342b7661cccSmmusante 		result += strlen(dd->dd_myname);
343b7661cccSmmusante 		mutex_exit(&dd->dd_lock);
344b7661cccSmmusante 	} else {
345b7661cccSmmusante 		result += strlen(dd->dd_myname);
346b7661cccSmmusante 	}
347b7661cccSmmusante 
348b7661cccSmmusante 	return (result);
349b7661cccSmmusante }
350b7661cccSmmusante 
351fa9e4066Sahrens static int
352fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp)
353fa9e4066Sahrens {
354fa9e4066Sahrens 	char *p;
3553b2aab18SMatthew Ahrens 
356ccba0801SRich Morris 	if ((path == NULL) || (path[0] == '\0'))
357be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
358fa9e4066Sahrens 	/* This would be a good place to reserve some namespace... */
359fa9e4066Sahrens 	p = strpbrk(path, "/@");
360fa9e4066Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
361fa9e4066Sahrens 		/* two separators in a row */
362be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
363fa9e4066Sahrens 	}
364fa9e4066Sahrens 	if (p == NULL || p == path) {
365fa9e4066Sahrens 		/*
366fa9e4066Sahrens 		 * if the first thing is an @ or /, it had better be an
367fa9e4066Sahrens 		 * @ and it had better not have any more ats or slashes,
368fa9e4066Sahrens 		 * and it had better have something after the @.
369fa9e4066Sahrens 		 */
370fa9e4066Sahrens 		if (p != NULL &&
371fa9e4066Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
372be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
373fa9e4066Sahrens 		if (strlen(path) >= MAXNAMELEN)
374be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
375fa9e4066Sahrens 		(void) strcpy(component, path);
376fa9e4066Sahrens 		p = NULL;
377fa9e4066Sahrens 	} else if (p[0] == '/') {
3783b2aab18SMatthew Ahrens 		if (p - path >= MAXNAMELEN)
379be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
380fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
3813b2aab18SMatthew Ahrens 		component[p - path] = '\0';
382fa9e4066Sahrens 		p++;
383fa9e4066Sahrens 	} else if (p[0] == '@') {
384fa9e4066Sahrens 		/*
385fa9e4066Sahrens 		 * if the next separator is an @, there better not be
386fa9e4066Sahrens 		 * any more slashes.
387fa9e4066Sahrens 		 */
388fa9e4066Sahrens 		if (strchr(path, '/'))
389be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
3903b2aab18SMatthew Ahrens 		if (p - path >= MAXNAMELEN)
391be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
392fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
3933b2aab18SMatthew Ahrens 		component[p - path] = '\0';
394fa9e4066Sahrens 	} else {
3953b2aab18SMatthew Ahrens 		panic("invalid p=%p", (void *)p);
396fa9e4066Sahrens 	}
397fa9e4066Sahrens 	*nextp = p;
398fa9e4066Sahrens 	return (0);
399fa9e4066Sahrens }
400fa9e4066Sahrens 
401fa9e4066Sahrens /*
4023b2aab18SMatthew Ahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
4033b2aab18SMatthew Ahrens  * be found in *tail.  The name must be in the specified dsl_pool_t.  This
4043b2aab18SMatthew Ahrens  * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
4053b2aab18SMatthew Ahrens  * path is bogus, or if tail==NULL and we couldn't parse the whole name.
4063b2aab18SMatthew Ahrens  * (*tail)[0] == '@' means that the last component is a snapshot.
407fa9e4066Sahrens  */
408ea8dc4b6Seschrock int
4093b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
410ea8dc4b6Seschrock     dsl_dir_t **ddp, const char **tailp)
411fa9e4066Sahrens {
412fa9e4066Sahrens 	char buf[MAXNAMELEN];
4133b2aab18SMatthew Ahrens 	const char *spaname, *next, *nextnext = NULL;
414fa9e4066Sahrens 	int err;
415fa9e4066Sahrens 	dsl_dir_t *dd;
416fa9e4066Sahrens 	uint64_t ddobj;
417fa9e4066Sahrens 
418fa9e4066Sahrens 	err = getcomponent(name, buf, &next);
4193b2aab18SMatthew Ahrens 	if (err != 0)
420ea8dc4b6Seschrock 		return (err);
421fa9e4066Sahrens 
4223b2aab18SMatthew Ahrens 	/* Make sure the name is in the specified pool. */
4233b2aab18SMatthew Ahrens 	spaname = spa_name(dp->dp_spa);
4243b2aab18SMatthew Ahrens 	if (strcmp(buf, spaname) != 0)
425*03b1c297SAlexander Eremin 		return (SET_ERROR(EXDEV));
426fa9e4066Sahrens 
4273b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
428fa9e4066Sahrens 
4293b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
4303b2aab18SMatthew Ahrens 	if (err != 0) {
431ea8dc4b6Seschrock 		return (err);
432ea8dc4b6Seschrock 	}
433ea8dc4b6Seschrock 
434fa9e4066Sahrens 	while (next != NULL) {
435bc9014e6SJustin Gibbs 		dsl_dir_t *child_dd;
436fa9e4066Sahrens 		err = getcomponent(next, buf, &nextnext);
4373b2aab18SMatthew Ahrens 		if (err != 0)
438ea8dc4b6Seschrock 			break;
439fa9e4066Sahrens 		ASSERT(next[0] != '\0');
440fa9e4066Sahrens 		if (next[0] == '@')
441fa9e4066Sahrens 			break;
442fa9e4066Sahrens 		dprintf("looking up %s in obj%lld\n",
443c1379625SJustin T. Gibbs 		    buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
444fa9e4066Sahrens 
445fa9e4066Sahrens 		err = zap_lookup(dp->dp_meta_objset,
446c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj,
447fa9e4066Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
4483b2aab18SMatthew Ahrens 		if (err != 0) {
449ea8dc4b6Seschrock 			if (err == ENOENT)
450ea8dc4b6Seschrock 				err = 0;
451fa9e4066Sahrens 			break;
452fa9e4066Sahrens 		}
453fa9e4066Sahrens 
454bc9014e6SJustin Gibbs 		err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
4553b2aab18SMatthew Ahrens 		if (err != 0)
456ea8dc4b6Seschrock 			break;
4573b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
458bc9014e6SJustin Gibbs 		dd = child_dd;
459fa9e4066Sahrens 		next = nextnext;
460fa9e4066Sahrens 	}
461fa9e4066Sahrens 
4623b2aab18SMatthew Ahrens 	if (err != 0) {
4633b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
464ea8dc4b6Seschrock 		return (err);
465ea8dc4b6Seschrock 	}
466ea8dc4b6Seschrock 
467fa9e4066Sahrens 	/*
468fa9e4066Sahrens 	 * It's an error if there's more than one component left, or
469fa9e4066Sahrens 	 * tailp==NULL and there's any component left.
470fa9e4066Sahrens 	 */
471fa9e4066Sahrens 	if (next != NULL &&
472fa9e4066Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
473fa9e4066Sahrens 		/* bad path name */
4743b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
475fa9e4066Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
476be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
477fa9e4066Sahrens 	}
4783b2aab18SMatthew Ahrens 	if (tailp != NULL)
479fa9e4066Sahrens 		*tailp = next;
480ea8dc4b6Seschrock 	*ddp = dd;
481ea8dc4b6Seschrock 	return (err);
482fa9e4066Sahrens }
483fa9e4066Sahrens 
484a2afb611SJerry Jelinek /*
485a2afb611SJerry Jelinek  * If the counts are already initialized for this filesystem and its
486a2afb611SJerry Jelinek  * descendants then do nothing, otherwise initialize the counts.
487a2afb611SJerry Jelinek  *
488a2afb611SJerry Jelinek  * The counts on this filesystem, and those below, may be uninitialized due to
489a2afb611SJerry Jelinek  * either the use of a pre-existing pool which did not support the
490a2afb611SJerry Jelinek  * filesystem/snapshot limit feature, or one in which the feature had not yet
491a2afb611SJerry Jelinek  * been enabled.
492a2afb611SJerry Jelinek  *
493a2afb611SJerry Jelinek  * Recursively descend the filesystem tree and update the filesystem/snapshot
494a2afb611SJerry Jelinek  * counts on each filesystem below, then update the cumulative count on the
495a2afb611SJerry Jelinek  * current filesystem. If the filesystem already has a count set on it,
496a2afb611SJerry Jelinek  * then we know that its counts, and the counts on the filesystems below it,
497a2afb611SJerry Jelinek  * are already correct, so we don't have to update this filesystem.
498a2afb611SJerry Jelinek  */
499a2afb611SJerry Jelinek static void
500a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
501a2afb611SJerry Jelinek {
502a2afb611SJerry Jelinek 	uint64_t my_fs_cnt = 0;
503a2afb611SJerry Jelinek 	uint64_t my_ss_cnt = 0;
504a2afb611SJerry Jelinek 	dsl_pool_t *dp = dd->dd_pool;
505a2afb611SJerry Jelinek 	objset_t *os = dp->dp_meta_objset;
506a2afb611SJerry Jelinek 	zap_cursor_t *zc;
507a2afb611SJerry Jelinek 	zap_attribute_t *za;
508a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
509a2afb611SJerry Jelinek 
510adf34077SJerry Jelinek 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
511a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dp));
512a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
513a2afb611SJerry Jelinek 
514a2afb611SJerry Jelinek 	dsl_dir_zapify(dd, tx);
515a2afb611SJerry Jelinek 
516a2afb611SJerry Jelinek 	/*
517a2afb611SJerry Jelinek 	 * If the filesystem count has already been initialized then we
518a2afb611SJerry Jelinek 	 * don't need to recurse down any further.
519a2afb611SJerry Jelinek 	 */
520a2afb611SJerry Jelinek 	if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
521a2afb611SJerry Jelinek 		return;
522a2afb611SJerry Jelinek 
523a2afb611SJerry Jelinek 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
524a2afb611SJerry Jelinek 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
525a2afb611SJerry Jelinek 
526a2afb611SJerry Jelinek 	/* Iterate my child dirs */
527c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
528a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
529a2afb611SJerry Jelinek 		dsl_dir_t *chld_dd;
530a2afb611SJerry Jelinek 		uint64_t count;
531a2afb611SJerry Jelinek 
532a2afb611SJerry Jelinek 		VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
533a2afb611SJerry Jelinek 		    &chld_dd));
534a2afb611SJerry Jelinek 
535a2afb611SJerry Jelinek 		/*
536a2afb611SJerry Jelinek 		 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
537a2afb611SJerry Jelinek 		 * temporary datasets.
538a2afb611SJerry Jelinek 		 */
539a2afb611SJerry Jelinek 		if (chld_dd->dd_myname[0] == '$' ||
540a2afb611SJerry Jelinek 		    chld_dd->dd_myname[0] == '%') {
541a2afb611SJerry Jelinek 			dsl_dir_rele(chld_dd, FTAG);
542a2afb611SJerry Jelinek 			continue;
543a2afb611SJerry Jelinek 		}
544a2afb611SJerry Jelinek 
545a2afb611SJerry Jelinek 		my_fs_cnt++;	/* count this child */
546a2afb611SJerry Jelinek 
547a2afb611SJerry Jelinek 		dsl_dir_init_fs_ss_count(chld_dd, tx);
548a2afb611SJerry Jelinek 
549a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
550a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
551a2afb611SJerry Jelinek 		my_fs_cnt += count;
552a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
553a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
554a2afb611SJerry Jelinek 		my_ss_cnt += count;
555a2afb611SJerry Jelinek 
556a2afb611SJerry Jelinek 		dsl_dir_rele(chld_dd, FTAG);
557a2afb611SJerry Jelinek 	}
558a2afb611SJerry Jelinek 	zap_cursor_fini(zc);
559a2afb611SJerry Jelinek 	/* Count my snapshots (we counted children's snapshots above) */
560a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
561c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
562a2afb611SJerry Jelinek 
563c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
564a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0;
565a2afb611SJerry Jelinek 	    zap_cursor_advance(zc)) {
566a2afb611SJerry Jelinek 		/* Don't count temporary snapshots */
567a2afb611SJerry Jelinek 		if (za->za_name[0] != '%')
568a2afb611SJerry Jelinek 			my_ss_cnt++;
569a2afb611SJerry Jelinek 	}
570fb7001f1SAlex Reece 	zap_cursor_fini(zc);
571a2afb611SJerry Jelinek 
572a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
573a2afb611SJerry Jelinek 
574a2afb611SJerry Jelinek 	kmem_free(zc, sizeof (zap_cursor_t));
575a2afb611SJerry Jelinek 	kmem_free(za, sizeof (zap_attribute_t));
576a2afb611SJerry Jelinek 
577a2afb611SJerry Jelinek 	/* we're in a sync task, update counts */
578a2afb611SJerry Jelinek 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
579a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
580a2afb611SJerry Jelinek 	    sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
581a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
582a2afb611SJerry Jelinek 	    sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
583a2afb611SJerry Jelinek }
584a2afb611SJerry Jelinek 
585a2afb611SJerry Jelinek static int
586a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
587a2afb611SJerry Jelinek {
588a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
589a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
590a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
591a2afb611SJerry Jelinek 	dsl_dir_t *dd;
592a2afb611SJerry Jelinek 	int error;
593a2afb611SJerry Jelinek 
594a2afb611SJerry Jelinek 	error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
595a2afb611SJerry Jelinek 	if (error != 0)
596a2afb611SJerry Jelinek 		return (error);
597a2afb611SJerry Jelinek 
598a2afb611SJerry Jelinek 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
599a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
600a2afb611SJerry Jelinek 		return (SET_ERROR(ENOTSUP));
601a2afb611SJerry Jelinek 	}
602a2afb611SJerry Jelinek 
603a2afb611SJerry Jelinek 	dd = ds->ds_dir;
604a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
605a2afb611SJerry Jelinek 	    dsl_dir_is_zapified(dd) &&
606a2afb611SJerry Jelinek 	    zap_contains(dp->dp_meta_objset, dd->dd_object,
607a2afb611SJerry Jelinek 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
608a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
609a2afb611SJerry Jelinek 		return (SET_ERROR(EALREADY));
610a2afb611SJerry Jelinek 	}
611a2afb611SJerry Jelinek 
612a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
613a2afb611SJerry Jelinek 	return (0);
614a2afb611SJerry Jelinek }
615a2afb611SJerry Jelinek 
616a2afb611SJerry Jelinek static void
617a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
618a2afb611SJerry Jelinek {
619a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
620a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
621a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
622a2afb611SJerry Jelinek 	spa_t *spa;
623a2afb611SJerry Jelinek 
624a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
625a2afb611SJerry Jelinek 
626a2afb611SJerry Jelinek 	spa = dsl_dataset_get_spa(ds);
627a2afb611SJerry Jelinek 
628a2afb611SJerry Jelinek 	if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
629a2afb611SJerry Jelinek 		/*
630a2afb611SJerry Jelinek 		 * Since the feature was not active and we're now setting a
631a2afb611SJerry Jelinek 		 * limit, increment the feature-active counter so that the
632a2afb611SJerry Jelinek 		 * feature becomes active for the first time.
633a2afb611SJerry Jelinek 		 *
634a2afb611SJerry Jelinek 		 * We are already in a sync task so we can update the MOS.
635a2afb611SJerry Jelinek 		 */
636a2afb611SJerry Jelinek 		spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
637a2afb611SJerry Jelinek 	}
638a2afb611SJerry Jelinek 
639a2afb611SJerry Jelinek 	/*
640a2afb611SJerry Jelinek 	 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
641a2afb611SJerry Jelinek 	 * we need to ensure the counts are correct. Descend down the tree from
642a2afb611SJerry Jelinek 	 * this point and update all of the counts to be accurate.
643a2afb611SJerry Jelinek 	 */
644a2afb611SJerry Jelinek 	dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
645a2afb611SJerry Jelinek 
646a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
647a2afb611SJerry Jelinek }
648a2afb611SJerry Jelinek 
649a2afb611SJerry Jelinek /*
650a2afb611SJerry Jelinek  * Make sure the feature is enabled and activate it if necessary.
651a2afb611SJerry Jelinek  * Since we're setting a limit, ensure the on-disk counts are valid.
652a2afb611SJerry Jelinek  * This is only called by the ioctl path when setting a limit value.
653a2afb611SJerry Jelinek  *
654a2afb611SJerry Jelinek  * We do not need to validate the new limit, since users who can change the
655a2afb611SJerry Jelinek  * limit are also allowed to exceed the limit.
656a2afb611SJerry Jelinek  */
657a2afb611SJerry Jelinek int
658a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname)
659a2afb611SJerry Jelinek {
660a2afb611SJerry Jelinek 	int error;
661a2afb611SJerry Jelinek 
662a2afb611SJerry Jelinek 	error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
6637d46dc6cSMatthew Ahrens 	    dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
6647d46dc6cSMatthew Ahrens 	    ZFS_SPACE_CHECK_RESERVED);
665a2afb611SJerry Jelinek 
666a2afb611SJerry Jelinek 	if (error == EALREADY)
667a2afb611SJerry Jelinek 		error = 0;
668a2afb611SJerry Jelinek 
669a2afb611SJerry Jelinek 	return (error);
670a2afb611SJerry Jelinek }
671a2afb611SJerry Jelinek 
672a2afb611SJerry Jelinek /*
673a2afb611SJerry Jelinek  * Used to determine if the filesystem_limit or snapshot_limit should be
674a2afb611SJerry Jelinek  * enforced. We allow the limit to be exceeded if the user has permission to
675a2afb611SJerry Jelinek  * write the property value. We pass in the creds that we got in the open
676a2afb611SJerry Jelinek  * context since we will always be the GZ root in syncing context. We also have
677a2afb611SJerry Jelinek  * to handle the case where we are allowed to change the limit on the current
678a2afb611SJerry Jelinek  * dataset, but there may be another limit in the tree above.
679a2afb611SJerry Jelinek  *
680a2afb611SJerry Jelinek  * We can never modify these two properties within a non-global zone. In
681a2afb611SJerry Jelinek  * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
682a2afb611SJerry Jelinek  * can't use that function since we are already holding the dp_config_rwlock.
683a2afb611SJerry Jelinek  * In addition, we already have the dd and dealing with snapshots is simplified
684a2afb611SJerry Jelinek  * in this code.
685a2afb611SJerry Jelinek  */
686a2afb611SJerry Jelinek 
687a2afb611SJerry Jelinek typedef enum {
688a2afb611SJerry Jelinek 	ENFORCE_ALWAYS,
689a2afb611SJerry Jelinek 	ENFORCE_NEVER,
690a2afb611SJerry Jelinek 	ENFORCE_ABOVE
691a2afb611SJerry Jelinek } enforce_res_t;
692a2afb611SJerry Jelinek 
693a2afb611SJerry Jelinek static enforce_res_t
694a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
695a2afb611SJerry Jelinek {
696a2afb611SJerry Jelinek 	enforce_res_t enforce = ENFORCE_ALWAYS;
697a2afb611SJerry Jelinek 	uint64_t obj;
698a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
699a2afb611SJerry Jelinek 	uint64_t zoned;
700a2afb611SJerry Jelinek 
701a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
702a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
703a2afb611SJerry Jelinek 
704a2afb611SJerry Jelinek #ifdef _KERNEL
705a2afb611SJerry Jelinek 	if (crgetzoneid(cr) != GLOBAL_ZONEID)
706a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
707a2afb611SJerry Jelinek 
708a2afb611SJerry Jelinek 	if (secpolicy_zfs(cr) == 0)
709a2afb611SJerry Jelinek 		return (ENFORCE_NEVER);
710a2afb611SJerry Jelinek #endif
711a2afb611SJerry Jelinek 
712c1379625SJustin T. Gibbs 	if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
713a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
714a2afb611SJerry Jelinek 
715a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
716a2afb611SJerry Jelinek 
717a2afb611SJerry Jelinek 	if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
718a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
719a2afb611SJerry Jelinek 
720a2afb611SJerry Jelinek 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
721a2afb611SJerry Jelinek 		/* Only root can access zoned fs's from the GZ */
722a2afb611SJerry Jelinek 		enforce = ENFORCE_ALWAYS;
723a2afb611SJerry Jelinek 	} else {
724a2afb611SJerry Jelinek 		if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
725a2afb611SJerry Jelinek 			enforce = ENFORCE_ABOVE;
726a2afb611SJerry Jelinek 	}
727a2afb611SJerry Jelinek 
728a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
729a2afb611SJerry Jelinek 	return (enforce);
730a2afb611SJerry Jelinek }
731a2afb611SJerry Jelinek 
732a2afb611SJerry Jelinek /*
733a2afb611SJerry Jelinek  * Check if adding additional child filesystem(s) would exceed any filesystem
734a2afb611SJerry Jelinek  * limits or adding additional snapshot(s) would exceed any snapshot limits.
735a2afb611SJerry Jelinek  * The prop argument indicates which limit to check.
736a2afb611SJerry Jelinek  *
737a2afb611SJerry Jelinek  * Note that all filesystem limits up to the root (or the highest
738a2afb611SJerry Jelinek  * initialized) filesystem or the given ancestor must be satisfied.
739a2afb611SJerry Jelinek  */
740a2afb611SJerry Jelinek int
741a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
742a2afb611SJerry Jelinek     dsl_dir_t *ancestor, cred_t *cr)
743a2afb611SJerry Jelinek {
744a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
745a2afb611SJerry Jelinek 	uint64_t limit, count;
746a2afb611SJerry Jelinek 	char *count_prop;
747a2afb611SJerry Jelinek 	enforce_res_t enforce;
748a2afb611SJerry Jelinek 	int err = 0;
749a2afb611SJerry Jelinek 
750a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
751a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
752a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
753a2afb611SJerry Jelinek 
754a2afb611SJerry Jelinek 	/*
755a2afb611SJerry Jelinek 	 * If we're allowed to change the limit, don't enforce the limit
756a2afb611SJerry Jelinek 	 * e.g. this can happen if a snapshot is taken by an administrative
757a2afb611SJerry Jelinek 	 * user in the global zone (i.e. a recursive snapshot by root).
758a2afb611SJerry Jelinek 	 * However, we must handle the case of delegated permissions where we
759a2afb611SJerry Jelinek 	 * are allowed to change the limit on the current dataset, but there
760a2afb611SJerry Jelinek 	 * is another limit in the tree above.
761a2afb611SJerry Jelinek 	 */
762a2afb611SJerry Jelinek 	enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
763a2afb611SJerry Jelinek 	if (enforce == ENFORCE_NEVER)
764a2afb611SJerry Jelinek 		return (0);
765a2afb611SJerry Jelinek 
766a2afb611SJerry Jelinek 	/*
767a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment
768a2afb611SJerry Jelinek 	 * is 0.
769a2afb611SJerry Jelinek 	 */
770a2afb611SJerry Jelinek 	if (delta == 0)
771a2afb611SJerry Jelinek 		return (0);
772a2afb611SJerry Jelinek 
773a2afb611SJerry Jelinek 	if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
774a2afb611SJerry Jelinek 		/*
775a2afb611SJerry Jelinek 		 * We don't enforce the limit for temporary snapshots. This is
776a2afb611SJerry Jelinek 		 * indicated by a NULL cred_t argument.
777a2afb611SJerry Jelinek 		 */
778a2afb611SJerry Jelinek 		if (cr == NULL)
779a2afb611SJerry Jelinek 			return (0);
780a2afb611SJerry Jelinek 
781a2afb611SJerry Jelinek 		count_prop = DD_FIELD_SNAPSHOT_COUNT;
782a2afb611SJerry Jelinek 	} else {
783a2afb611SJerry Jelinek 		count_prop = DD_FIELD_FILESYSTEM_COUNT;
784a2afb611SJerry Jelinek 	}
785a2afb611SJerry Jelinek 
786a2afb611SJerry Jelinek 	/*
787a2afb611SJerry Jelinek 	 * If an ancestor has been provided, stop checking the limit once we
788a2afb611SJerry Jelinek 	 * hit that dir. We need this during rename so that we don't overcount
789a2afb611SJerry Jelinek 	 * the check once we recurse up to the common ancestor.
790a2afb611SJerry Jelinek 	 */
791a2afb611SJerry Jelinek 	if (ancestor == dd)
792a2afb611SJerry Jelinek 		return (0);
793a2afb611SJerry Jelinek 
794a2afb611SJerry Jelinek 	/*
795a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
796a2afb611SJerry Jelinek 	 * stop since we know there is no limit here (or above). The counts are
797a2afb611SJerry Jelinek 	 * not valid on this node and we know we won't touch this node's counts.
798a2afb611SJerry Jelinek 	 */
799a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
800a2afb611SJerry Jelinek 	    count_prop, sizeof (count), 1, &count) == ENOENT)
801a2afb611SJerry Jelinek 		return (0);
802a2afb611SJerry Jelinek 
803a2afb611SJerry Jelinek 	err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
804a2afb611SJerry Jelinek 	    B_FALSE);
805a2afb611SJerry Jelinek 	if (err != 0)
806a2afb611SJerry Jelinek 		return (err);
807a2afb611SJerry Jelinek 
808a2afb611SJerry Jelinek 	/* Is there a limit which we've hit? */
809a2afb611SJerry Jelinek 	if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
810a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
811a2afb611SJerry Jelinek 
812a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
813a2afb611SJerry Jelinek 		err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
814a2afb611SJerry Jelinek 		    ancestor, cr);
815a2afb611SJerry Jelinek 
816a2afb611SJerry Jelinek 	return (err);
817a2afb611SJerry Jelinek }
818a2afb611SJerry Jelinek 
819a2afb611SJerry Jelinek /*
820a2afb611SJerry Jelinek  * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
821a2afb611SJerry Jelinek  * parents. When a new filesystem/snapshot is created, increment the count on
822a2afb611SJerry Jelinek  * all parents, and when a filesystem/snapshot is destroyed, decrement the
823a2afb611SJerry Jelinek  * count.
824a2afb611SJerry Jelinek  */
825a2afb611SJerry Jelinek void
826a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
827a2afb611SJerry Jelinek     dmu_tx_t *tx)
828a2afb611SJerry Jelinek {
829a2afb611SJerry Jelinek 	int err;
830a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
831a2afb611SJerry Jelinek 	uint64_t count;
832a2afb611SJerry Jelinek 
833a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
834a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
835a2afb611SJerry Jelinek 	ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
836a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
837a2afb611SJerry Jelinek 
838a2afb611SJerry Jelinek 	/*
839a2afb611SJerry Jelinek 	 * When we receive an incremental stream into a filesystem that already
840a2afb611SJerry Jelinek 	 * exists, a temporary clone is created.  We don't count this temporary
841a2afb611SJerry Jelinek 	 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
842a2afb611SJerry Jelinek 	 * $MOS & $ORIGIN) objsets.
843a2afb611SJerry Jelinek 	 */
844a2afb611SJerry Jelinek 	if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
845a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
846a2afb611SJerry Jelinek 		return;
847a2afb611SJerry Jelinek 
848a2afb611SJerry Jelinek 	/*
849a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
850a2afb611SJerry Jelinek 	 */
851a2afb611SJerry Jelinek 	if (delta == 0)
852a2afb611SJerry Jelinek 		return;
853a2afb611SJerry Jelinek 
854a2afb611SJerry Jelinek 	/*
855a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
856a2afb611SJerry Jelinek 	 * stop since we know the counts are not valid on this node and we
857a2afb611SJerry Jelinek 	 * know we shouldn't touch this node's counts. An uninitialized count
858a2afb611SJerry Jelinek 	 * on the node indicates that either the feature has not yet been
859a2afb611SJerry Jelinek 	 * activated or there are no limits on this part of the tree.
860a2afb611SJerry Jelinek 	 */
861a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
862a2afb611SJerry Jelinek 	    prop, sizeof (count), 1, &count)) == ENOENT)
863a2afb611SJerry Jelinek 		return;
864a2afb611SJerry Jelinek 	VERIFY0(err);
865a2afb611SJerry Jelinek 
866a2afb611SJerry Jelinek 	count += delta;
867a2afb611SJerry Jelinek 	/* Use a signed verify to make sure we're not neg. */
868a2afb611SJerry Jelinek 	VERIFY3S(count, >=, 0);
869a2afb611SJerry Jelinek 
870a2afb611SJerry Jelinek 	VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
871a2afb611SJerry Jelinek 	    tx));
872a2afb611SJerry Jelinek 
873a2afb611SJerry Jelinek 	/* Roll up this additional count into our ancestors */
874a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
875a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
876a2afb611SJerry Jelinek }
877a2afb611SJerry Jelinek 
8781d452cf5Sahrens uint64_t
879088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
880088f3894Sahrens     dmu_tx_t *tx)
881fa9e4066Sahrens {
882088f3894Sahrens 	objset_t *mos = dp->dp_meta_objset;
883fa9e4066Sahrens 	uint64_t ddobj;
884cde58dbcSMatthew Ahrens 	dsl_dir_phys_t *ddphys;
885fa9e4066Sahrens 	dmu_buf_t *dbuf;
886fa9e4066Sahrens 
8871649cd4bStabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
8881649cd4bStabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
889088f3894Sahrens 	if (pds) {
890c1379625SJustin T. Gibbs 		VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
891088f3894Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
892088f3894Sahrens 	} else {
893088f3894Sahrens 		/* it's the root dir */
894088f3894Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
895088f3894Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
896088f3894Sahrens 	}
897ea8dc4b6Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
898fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
899cde58dbcSMatthew Ahrens 	ddphys = dbuf->db_data;
900fa9e4066Sahrens 
901cde58dbcSMatthew Ahrens 	ddphys->dd_creation_time = gethrestime_sec();
902a2afb611SJerry Jelinek 	if (pds) {
903cde58dbcSMatthew Ahrens 		ddphys->dd_parent_obj = pds->dd_object;
904a2afb611SJerry Jelinek 
905a2afb611SJerry Jelinek 		/* update the filesystem counts */
906a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
907a2afb611SJerry Jelinek 	}
908cde58dbcSMatthew Ahrens 	ddphys->dd_props_zapobj = zap_create(mos,
909fa9e4066Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
910cde58dbcSMatthew Ahrens 	ddphys->dd_child_dir_zapobj = zap_create(mos,
91187e5029aSahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
91274e7dc98SMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
913cde58dbcSMatthew Ahrens 		ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
914ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
915fa9e4066Sahrens 
9161d452cf5Sahrens 	return (ddobj);
917fa9e4066Sahrens }
918fa9e4066Sahrens 
919088f3894Sahrens boolean_t
920088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
921fa9e4066Sahrens {
922c1379625SJustin T. Gibbs 	return (dsl_dir_phys(dd)->dd_origin_obj &&
923088f3894Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
924c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_origin_obj !=
925088f3894Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
926fa9e4066Sahrens }
927fa9e4066Sahrens 
928fa9e4066Sahrens void
929a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
930fa9e4066Sahrens {
931fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
93274e7dc98SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
933c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_bytes);
934c1379625SJustin T. Gibbs 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
935c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_quota);
936a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
937c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_reserved);
938a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
939c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
940c1379625SJustin T. Gibbs 	    (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
941c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes));
94277372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
943c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes);
944c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
94574e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
946c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
94774e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
948c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
94974e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
950c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
95174e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
952c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
953c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
95474e7dc98SMatthew Ahrens 	}
955fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
956fa9e4066Sahrens 
957a2afb611SJerry Jelinek 	if (dsl_dir_is_zapified(dd)) {
958a2afb611SJerry Jelinek 		uint64_t count;
959a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
960a2afb611SJerry Jelinek 
961a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
962a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
963a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
964a2afb611SJerry Jelinek 			    ZFS_PROP_FILESYSTEM_COUNT, count);
965a2afb611SJerry Jelinek 		}
966a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
967a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
968a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
969a2afb611SJerry Jelinek 			    ZFS_PROP_SNAPSHOT_COUNT, count);
970a2afb611SJerry Jelinek 		}
971a2afb611SJerry Jelinek 	}
972a2afb611SJerry Jelinek 
973088f3894Sahrens 	if (dsl_dir_is_clone(dd)) {
974fa9e4066Sahrens 		dsl_dataset_t *ds;
975a2eea2e1Sahrens 		char buf[MAXNAMELEN];
976fa9e4066Sahrens 
9773b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
978c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
979a2eea2e1Sahrens 		dsl_dataset_name(ds, buf);
980745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
981a2eea2e1Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
982fa9e4066Sahrens 	}
983fa9e4066Sahrens }
984fa9e4066Sahrens 
985fa9e4066Sahrens void
986fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
987fa9e4066Sahrens {
988fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
989fa9e4066Sahrens 
990c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd));
991fa9e4066Sahrens 
9923b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
993fa9e4066Sahrens 		/* up the hold count until we can be written out */
994fa9e4066Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
995fa9e4066Sahrens 	}
996fa9e4066Sahrens }
997fa9e4066Sahrens 
998fa9e4066Sahrens static int64_t
999fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1000fa9e4066Sahrens {
1001c1379625SJustin T. Gibbs 	uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1002c1379625SJustin T. Gibbs 	uint64_t new_accounted =
1003c1379625SJustin T. Gibbs 	    MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1004fa9e4066Sahrens 	return (new_accounted - old_accounted);
1005fa9e4066Sahrens }
1006fa9e4066Sahrens 
1007fa9e4066Sahrens void
1008fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1009fa9e4066Sahrens {
1010fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1011fa9e4066Sahrens 
1012fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1013fb09f5aaSMadhav Suresh 	ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1014fa9e4066Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1015fa9e4066Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1016fa9e4066Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1017fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1018fa9e4066Sahrens 
1019fa9e4066Sahrens 	/* release the hold from dsl_dir_dirty */
1020ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
1021fa9e4066Sahrens }
1022fa9e4066Sahrens 
1023fa9e4066Sahrens static uint64_t
1024a9799022Sck dsl_dir_space_towrite(dsl_dir_t *dd)
1025fa9e4066Sahrens {
1026a9799022Sck 	uint64_t space = 0;
1027fa9e4066Sahrens 	int i;
1028fa9e4066Sahrens 
1029fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
1030fa9e4066Sahrens 
1031fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1032fa9e4066Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
1033fa9e4066Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1034fa9e4066Sahrens 	}
1035fa9e4066Sahrens 	return (space);
1036fa9e4066Sahrens }
1037fa9e4066Sahrens 
1038fa9e4066Sahrens /*
1039fa9e4066Sahrens  * How much space would dd have available if ancestor had delta applied
1040fa9e4066Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
1041fa9e4066Sahrens  * on-disk, not estimated pending changes.
1042fa9e4066Sahrens  */
1043a2eea2e1Sahrens uint64_t
1044fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd,
1045fa9e4066Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1046fa9e4066Sahrens {
1047fa9e4066Sahrens 	uint64_t parentspace, myspace, quota, used;
1048fa9e4066Sahrens 
1049fa9e4066Sahrens 	/*
1050fa9e4066Sahrens 	 * If there are no restrictions otherwise, assume we have
1051fa9e4066Sahrens 	 * unlimited space available.
1052fa9e4066Sahrens 	 */
1053fa9e4066Sahrens 	quota = UINT64_MAX;
1054fa9e4066Sahrens 	parentspace = UINT64_MAX;
1055fa9e4066Sahrens 
1056fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1057fa9e4066Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
1058fa9e4066Sahrens 		    ancestor, delta, ondiskonly);
1059fa9e4066Sahrens 	}
1060fa9e4066Sahrens 
1061fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1062c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_quota != 0)
1063c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1064c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1065a9799022Sck 	if (!ondiskonly)
1066a9799022Sck 		used += dsl_dir_space_towrite(dd);
1067fa9e4066Sahrens 
1068fa9e4066Sahrens 	if (dd->dd_parent == NULL) {
106999653d4eSeschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1070fa9e4066Sahrens 		quota = MIN(quota, poolsize);
1071fa9e4066Sahrens 	}
1072fa9e4066Sahrens 
1073c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1074fa9e4066Sahrens 		/*
1075fa9e4066Sahrens 		 * We have some space reserved, in addition to what our
1076fa9e4066Sahrens 		 * parent gave us.
1077fa9e4066Sahrens 		 */
1078c1379625SJustin T. Gibbs 		parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1079fa9e4066Sahrens 	}
1080fa9e4066Sahrens 
108174e7dc98SMatthew Ahrens 	if (dd == ancestor) {
108274e7dc98SMatthew Ahrens 		ASSERT(delta <= 0);
108374e7dc98SMatthew Ahrens 		ASSERT(used >= -delta);
108474e7dc98SMatthew Ahrens 		used += delta;
108574e7dc98SMatthew Ahrens 		if (parentspace != UINT64_MAX)
108674e7dc98SMatthew Ahrens 			parentspace -= delta;
108774e7dc98SMatthew Ahrens 	}
108874e7dc98SMatthew Ahrens 
1089fa9e4066Sahrens 	if (used > quota) {
1090fa9e4066Sahrens 		/* over quota */
1091fa9e4066Sahrens 		myspace = 0;
1092fa9e4066Sahrens 	} else {
1093fa9e4066Sahrens 		/*
109499653d4eSeschrock 		 * the lesser of the space provided by our parent and
109599653d4eSeschrock 		 * the space left in our quota
1096fa9e4066Sahrens 		 */
1097fa9e4066Sahrens 		myspace = MIN(parentspace, quota - used);
1098fa9e4066Sahrens 	}
1099fa9e4066Sahrens 
1100fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1101fa9e4066Sahrens 
1102fa9e4066Sahrens 	return (myspace);
1103fa9e4066Sahrens }
1104fa9e4066Sahrens 
1105fa9e4066Sahrens struct tempreserve {
1106fa9e4066Sahrens 	list_node_t tr_node;
1107fa9e4066Sahrens 	dsl_dir_t *tr_ds;
1108fa9e4066Sahrens 	uint64_t tr_size;
1109fa9e4066Sahrens };
1110fa9e4066Sahrens 
1111fa9e4066Sahrens static int
1112a9799022Sck dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1113a9799022Sck     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
11146df6c3bcSck     dmu_tx_t *tx, boolean_t first)
1115fa9e4066Sahrens {
1116fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
1117a9799022Sck 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1118468c413aSTim Haley 	uint64_t deferred = 0;
1119a9799022Sck 	struct tempreserve *tr;
1120468c413aSTim Haley 	int retval = EDQUOT;
1121fa9e4066Sahrens 	int txgidx = txg & TXG_MASK;
1122fa9e4066Sahrens 	int i;
11239082849eSck 	uint64_t ref_rsrv = 0;
1124fa9e4066Sahrens 
1125fa9e4066Sahrens 	ASSERT3U(txg, !=, 0);
1126a9799022Sck 	ASSERT3S(asize, >, 0);
1127fa9e4066Sahrens 
1128fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1129a9799022Sck 
1130fa9e4066Sahrens 	/*
1131fa9e4066Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
1132fa9e4066Sahrens 	 * when checking for over-quota because they get one free hit.
1133fa9e4066Sahrens 	 */
1134a9799022Sck 	est_inflight = dsl_dir_space_towrite(dd);
1135fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++)
1136a9799022Sck 		est_inflight += dd->dd_tempreserved[i];
1137c1379625SJustin T. Gibbs 	used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1138fa9e4066Sahrens 
1139f4d2e9e6Smaybee 	/*
11406df6c3bcSck 	 * On the first iteration, fetch the dataset's used-on-disk and
11416df6c3bcSck 	 * refreservation values. Also, if checkrefquota is set, test if
11426df6c3bcSck 	 * allocating this space would exceed the dataset's refquota.
1143f4d2e9e6Smaybee 	 */
11446df6c3bcSck 	if (first && tx->tx_objset) {
1145c3fdb13aSmaybee 		int error;
1146503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
11479082849eSck 
1148a9799022Sck 		error = dsl_dataset_check_quota(ds, checkrefquota,
11499082849eSck 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
1150a9799022Sck 		if (error) {
1151a9799022Sck 			mutex_exit(&dd->dd_lock);
1152a9799022Sck 			return (error);
1153a9799022Sck 		}
1154a9799022Sck 	}
1155a9799022Sck 
1156a9799022Sck 	/*
1157a9799022Sck 	 * If this transaction will result in a net free of space,
1158a9799022Sck 	 * we want to let it through.
1159a9799022Sck 	 */
1160c1379625SJustin T. Gibbs 	if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1161f4d2e9e6Smaybee 		quota = UINT64_MAX;
1162f4d2e9e6Smaybee 	else
1163c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1164fa9e4066Sahrens 
1165fa9e4066Sahrens 	/*
1166468c413aSTim Haley 	 * Adjust the quota against the actual pool size at the root
1167468c413aSTim Haley 	 * minus any outstanding deferred frees.
1168f4d2e9e6Smaybee 	 * To ensure that it's possible to remove files from a full
1169f4d2e9e6Smaybee 	 * pool without inducing transient overcommits, we throttle
1170fa9e4066Sahrens 	 * netfree transactions against a quota that is slightly larger,
1171fa9e4066Sahrens 	 * but still within the pool's allocation slop.  In cases where
1172fa9e4066Sahrens 	 * we're very close to full, this will allow a steady trickle of
1173fa9e4066Sahrens 	 * removes to get through.
1174fa9e4066Sahrens 	 */
11751934e92fSmaybee 	if (dd->dd_parent == NULL) {
1176b24ab676SJeff Bonwick 		spa_t *spa = dd->dd_pool->dp_spa;
1177fa9e4066Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1178b24ab676SJeff Bonwick 		deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1179468c413aSTim Haley 		if (poolsize - deferred < quota) {
1180468c413aSTim Haley 			quota = poolsize - deferred;
1181468c413aSTim Haley 			retval = ENOSPC;
1182fa9e4066Sahrens 		}
1183fa9e4066Sahrens 	}
1184fa9e4066Sahrens 
1185fa9e4066Sahrens 	/*
1186fa9e4066Sahrens 	 * If they are requesting more space, and our current estimate
1187a9799022Sck 	 * is over quota, they get to try again unless the actual
1188ea8dc4b6Seschrock 	 * on-disk is over quota and there are no pending changes (which
1189ea8dc4b6Seschrock 	 * may free up space for us).
1190fa9e4066Sahrens 	 */
1191468c413aSTim Haley 	if (used_on_disk + est_inflight >= quota) {
1192468c413aSTim Haley 		if (est_inflight > 0 || used_on_disk < quota ||
1193468c413aSTim Haley 		    (retval == ENOSPC && used_on_disk < quota + deferred))
1194468c413aSTim Haley 			retval = ERESTART;
1195a9799022Sck 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1196fa9e4066Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
1197a9799022Sck 		    used_on_disk>>10, est_inflight>>10,
1198468c413aSTim Haley 		    quota>>10, asize>>10, retval);
1199fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
1200be6fd75aSMatthew Ahrens 		return (SET_ERROR(retval));
1201fa9e4066Sahrens 	}
1202fa9e4066Sahrens 
1203fa9e4066Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
1204fa9e4066Sahrens 	dd->dd_tempreserved[txgidx] += asize;
1205fa9e4066Sahrens 
12069082849eSck 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
12079082849eSck 	    asize - ref_rsrv);
1208fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1209fa9e4066Sahrens 
12101ab7f2deSmaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1211fa9e4066Sahrens 	tr->tr_ds = dd;
1212fa9e4066Sahrens 	tr->tr_size = asize;
1213fa9e4066Sahrens 	list_insert_tail(tr_list, tr);
1214fa9e4066Sahrens 
1215fa9e4066Sahrens 	/* see if it's OK with our parent */
12161934e92fSmaybee 	if (dd->dd_parent && parent_rsrv) {
1217c1379625SJustin T. Gibbs 		boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
12181934e92fSmaybee 
1219fa9e4066Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
12206df6c3bcSck 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1221fa9e4066Sahrens 	} else {
1222fa9e4066Sahrens 		return (0);
1223fa9e4066Sahrens 	}
1224fa9e4066Sahrens }
1225fa9e4066Sahrens 
1226fa9e4066Sahrens /*
1227fa9e4066Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
1228a9799022Sck  * After the space has been dirtied (and dsl_dir_willuse_space()
1229a9799022Sck  * has been called), the reservation should be canceled, using
1230a9799022Sck  * dsl_dir_tempreserve_clear().
1231fa9e4066Sahrens  */
1232fa9e4066Sahrens int
1233a9799022Sck dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1234a9799022Sck     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1235fa9e4066Sahrens {
12361ab7f2deSmaybee 	int err;
1237fa9e4066Sahrens 	list_t *tr_list;
1238fa9e4066Sahrens 
1239a9799022Sck 	if (asize == 0) {
1240a9799022Sck 		*tr_cookiep = NULL;
1241a9799022Sck 		return (0);
1242a9799022Sck 	}
1243a9799022Sck 
1244fa9e4066Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1245fa9e4066Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
1246fa9e4066Sahrens 	    offsetof(struct tempreserve, tr_node));
1247a9799022Sck 	ASSERT3S(asize, >, 0);
1248ea8dc4b6Seschrock 	ASSERT3S(fsize, >=, 0);
1249fa9e4066Sahrens 
12501ab7f2deSmaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
1251fa9e4066Sahrens 	if (err == 0) {
1252fa9e4066Sahrens 		struct tempreserve *tr;
1253fa9e4066Sahrens 
12541ab7f2deSmaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
12551ab7f2deSmaybee 		tr->tr_size = lsize;
12561ab7f2deSmaybee 		list_insert_tail(tr_list, tr);
12571ab7f2deSmaybee 	} else {
12581ab7f2deSmaybee 		if (err == EAGAIN) {
125969962b56SMatthew Ahrens 			/*
126069962b56SMatthew Ahrens 			 * If arc_memory_throttle() detected that pageout
126169962b56SMatthew Ahrens 			 * is running and we are low on memory, we delay new
126269962b56SMatthew Ahrens 			 * non-pageout transactions to give pageout an
126369962b56SMatthew Ahrens 			 * advantage.
126469962b56SMatthew Ahrens 			 *
126569962b56SMatthew Ahrens 			 * It is unfortunate to be delaying while the caller's
126669962b56SMatthew Ahrens 			 * locks are held.
126769962b56SMatthew Ahrens 			 */
12680689f76cSAdam Leventhal 			txg_delay(dd->dd_pool, tx->tx_txg,
12690689f76cSAdam Leventhal 			    MSEC2NSEC(10), MSEC2NSEC(10));
1270be6fd75aSMatthew Ahrens 			err = SET_ERROR(ERESTART);
1271fa9e4066Sahrens 		}
12721ab7f2deSmaybee 	}
12731ab7f2deSmaybee 
12741ab7f2deSmaybee 	if (err == 0) {
12751ab7f2deSmaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
12766df6c3bcSck 		    FALSE, asize > usize, tr_list, tx, TRUE);
1277fa9e4066Sahrens 	}
1278fa9e4066Sahrens 
12793b2aab18SMatthew Ahrens 	if (err != 0)
1280fa9e4066Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
1281fa9e4066Sahrens 	else
1282fa9e4066Sahrens 		*tr_cookiep = tr_list;
12831ab7f2deSmaybee 
1284fa9e4066Sahrens 	return (err);
1285fa9e4066Sahrens }
1286fa9e4066Sahrens 
1287fa9e4066Sahrens /*
1288fa9e4066Sahrens  * Clear a temporary reservation that we previously made with
1289fa9e4066Sahrens  * dsl_dir_tempreserve_space().
1290fa9e4066Sahrens  */
1291fa9e4066Sahrens void
1292fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1293fa9e4066Sahrens {
1294fa9e4066Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
1295fa9e4066Sahrens 	list_t *tr_list = tr_cookie;
1296fa9e4066Sahrens 	struct tempreserve *tr;
1297fa9e4066Sahrens 
1298fa9e4066Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
1299fa9e4066Sahrens 
1300a9799022Sck 	if (tr_cookie == NULL)
1301a9799022Sck 		return;
1302a9799022Sck 
130369962b56SMatthew Ahrens 	while ((tr = list_head(tr_list)) != NULL) {
130469962b56SMatthew Ahrens 		if (tr->tr_ds) {
1305fa9e4066Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
1306fa9e4066Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1307fa9e4066Sahrens 			    tr->tr_size);
1308fa9e4066Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1309fa9e4066Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
13101ab7f2deSmaybee 		} else {
13111ab7f2deSmaybee 			arc_tempreserve_clear(tr->tr_size);
1312fa9e4066Sahrens 		}
1313fa9e4066Sahrens 		list_remove(tr_list, tr);
1314fa9e4066Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
1315fa9e4066Sahrens 	}
1316fa9e4066Sahrens 
1317fa9e4066Sahrens 	kmem_free(tr_list, sizeof (list_t));
1318fa9e4066Sahrens }
1319fa9e4066Sahrens 
132069962b56SMatthew Ahrens /*
132169962b56SMatthew Ahrens  * This should be called from open context when we think we're going to write
132269962b56SMatthew Ahrens  * or free space, for example when dirtying data. Be conservative; it's okay
132369962b56SMatthew Ahrens  * to write less space or free more, but we don't want to write more or free
132469962b56SMatthew Ahrens  * less than the amount specified.
132569962b56SMatthew Ahrens  */
132669962b56SMatthew Ahrens void
132769962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1328fa9e4066Sahrens {
1329fa9e4066Sahrens 	int64_t parent_space;
1330fa9e4066Sahrens 	uint64_t est_used;
1331fa9e4066Sahrens 
1332fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1333fa9e4066Sahrens 	if (space > 0)
1334fa9e4066Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1335fa9e4066Sahrens 
1336c1379625SJustin T. Gibbs 	est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1337fa9e4066Sahrens 	parent_space = parent_delta(dd, est_used, space);
1338fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1339fa9e4066Sahrens 
1340fa9e4066Sahrens 	/* Make sure that we clean up dd_space_to* */
1341fa9e4066Sahrens 	dsl_dir_dirty(dd, tx);
1342fa9e4066Sahrens 
1343fa9e4066Sahrens 	/* XXX this is potentially expensive and unnecessary... */
1344fa9e4066Sahrens 	if (parent_space && dd->dd_parent)
134569962b56SMatthew Ahrens 		dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1346fa9e4066Sahrens }
1347fa9e4066Sahrens 
1348fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */
1349fa9e4066Sahrens void
135074e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1351fa9e4066Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1352fa9e4066Sahrens {
1353fa9e4066Sahrens 	int64_t accounted_delta;
1354b62969f8SMatthew Ahrens 
1355b62969f8SMatthew Ahrens 	/*
1356b62969f8SMatthew Ahrens 	 * dsl_dataset_set_refreservation_sync_impl() calls this with
1357b62969f8SMatthew Ahrens 	 * dd_lock held, so that it can atomically update
1358b62969f8SMatthew Ahrens 	 * ds->ds_reserved and the dsl_dir accounting, so that
1359b62969f8SMatthew Ahrens 	 * dsl_dataset_check_quota() can see dataset and dir accounting
1360b62969f8SMatthew Ahrens 	 * consistently.
1361b62969f8SMatthew Ahrens 	 */
136202c8f3f0SMatthew Ahrens 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1363fa9e4066Sahrens 
1364fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
136574e7dc98SMatthew Ahrens 	ASSERT(type < DD_USED_NUM);
1366fa9e4066Sahrens 
1367b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1368b62969f8SMatthew Ahrens 
136902c8f3f0SMatthew Ahrens 	if (needlock)
137002c8f3f0SMatthew Ahrens 		mutex_enter(&dd->dd_lock);
1371c1379625SJustin T. Gibbs 	accounted_delta =
1372c1379625SJustin T. Gibbs 	    parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1373c1379625SJustin T. Gibbs 	ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1374fa9e4066Sahrens 	ASSERT(compressed >= 0 ||
1375c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1376fa9e4066Sahrens 	ASSERT(uncompressed >= 0 ||
1377c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1378c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_bytes += used;
1379c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1380c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
138174e7dc98SMatthew Ahrens 
1382c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
138374e7dc98SMatthew Ahrens 		ASSERT(used > 0 ||
1384c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1385c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
138674e7dc98SMatthew Ahrens #ifdef DEBUG
138774e7dc98SMatthew Ahrens 		dd_used_t t;
138874e7dc98SMatthew Ahrens 		uint64_t u = 0;
138974e7dc98SMatthew Ahrens 		for (t = 0; t < DD_USED_NUM; t++)
1390c1379625SJustin T. Gibbs 			u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1391c1379625SJustin T. Gibbs 		ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
139274e7dc98SMatthew Ahrens #endif
139374e7dc98SMatthew Ahrens 	}
139402c8f3f0SMatthew Ahrens 	if (needlock)
139502c8f3f0SMatthew Ahrens 		mutex_exit(&dd->dd_lock);
1396fa9e4066Sahrens 
1397fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
139874e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1399fa9e4066Sahrens 		    accounted_delta, compressed, uncompressed, tx);
140074e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(dd->dd_parent,
140174e7dc98SMatthew Ahrens 		    used - accounted_delta,
140274e7dc98SMatthew Ahrens 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1403fa9e4066Sahrens 	}
1404fa9e4066Sahrens }
1405fa9e4066Sahrens 
140674e7dc98SMatthew Ahrens void
140774e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
140874e7dc98SMatthew Ahrens     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
140974e7dc98SMatthew Ahrens {
141074e7dc98SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
141174e7dc98SMatthew Ahrens 	ASSERT(oldtype < DD_USED_NUM);
141274e7dc98SMatthew Ahrens 	ASSERT(newtype < DD_USED_NUM);
141374e7dc98SMatthew Ahrens 
1414c1379625SJustin T. Gibbs 	if (delta == 0 ||
1415c1379625SJustin T. Gibbs 	    !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
141674e7dc98SMatthew Ahrens 		return;
141774e7dc98SMatthew Ahrens 
1418b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1419b62969f8SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
142074e7dc98SMatthew Ahrens 	ASSERT(delta > 0 ?
1421c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1422c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1423c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1424c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1425c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1426b62969f8SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
142774e7dc98SMatthew Ahrens }
142874e7dc98SMatthew Ahrens 
14293b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg {
14303b2aab18SMatthew Ahrens 	const char *ddsqra_name;
14313b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
14323b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
14333b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t;
14343b2aab18SMatthew Ahrens 
1435fa9e4066Sahrens static int
14363b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1437fa9e4066Sahrens {
14383b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14393b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14403b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14413b2aab18SMatthew Ahrens 	int error;
14423b2aab18SMatthew Ahrens 	uint64_t towrite, newval;
1443fa9e4066Sahrens 
14443b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
14453b2aab18SMatthew Ahrens 	if (error != 0)
14463b2aab18SMatthew Ahrens 		return (error);
14473b2aab18SMatthew Ahrens 
14483b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir, "quota",
14493b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
14503b2aab18SMatthew Ahrens 	if (error != 0) {
14513b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14523b2aab18SMatthew Ahrens 		return (error);
14533b2aab18SMatthew Ahrens 	}
145492241e0bSTom Erickson 
14553b2aab18SMatthew Ahrens 	if (newval == 0) {
14563b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14571d452cf5Sahrens 		return (0);
14583b2aab18SMatthew Ahrens 	}
1459fa9e4066Sahrens 
14603b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
14611d452cf5Sahrens 	/*
14621d452cf5Sahrens 	 * If we are doing the preliminary check in open context, and
14631d452cf5Sahrens 	 * there are pending changes, then don't fail it, since the
1464a9799022Sck 	 * pending changes could under-estimate the amount of space to be
14651d452cf5Sahrens 	 * freed up.
14661d452cf5Sahrens 	 */
14673b2aab18SMatthew Ahrens 	towrite = dsl_dir_space_towrite(ds->ds_dir);
14681d452cf5Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1469c1379625SJustin T. Gibbs 	    (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1470c1379625SJustin T. Gibbs 	    newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1471be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOSPC);
1472fa9e4066Sahrens 	}
14733b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
14743b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14753b2aab18SMatthew Ahrens 	return (error);
1476fa9e4066Sahrens }
1477fa9e4066Sahrens 
14781d452cf5Sahrens static void
14793b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
14801d452cf5Sahrens {
14813b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14823b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14833b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14843b2aab18SMatthew Ahrens 	uint64_t newval;
148592241e0bSTom Erickson 
14863b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
14871d452cf5Sahrens 
1488013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1489013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1490013023d4SMartin Matuska 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1491013023d4SMartin Matuska 		    &ddsqra->ddsqra_value, tx);
14921d452cf5Sahrens 
1493013023d4SMartin Matuska 		VERIFY0(dsl_prop_get_int_ds(ds,
1494013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1495013023d4SMartin Matuska 	} else {
1496013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1497013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1498013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1499013023d4SMartin Matuska 	}
15003b2aab18SMatthew Ahrens 
15013b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
15023b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
1503c1379625SJustin T. Gibbs 	dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
15043b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
15053b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15061d452cf5Sahrens }
15071d452cf5Sahrens 
1508fa9e4066Sahrens int
150992241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1510fa9e4066Sahrens {
15113b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
1512fa9e4066Sahrens 
15133b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
15143b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
15153b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = quota;
151692241e0bSTom Erickson 
15173b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
15187d46dc6cSMatthew Ahrens 	    dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1519fa9e4066Sahrens }
1520fa9e4066Sahrens 
1521a9799022Sck int
15223b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1523fa9e4066Sahrens {
15243b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
15253b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15263b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15273b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15283b2aab18SMatthew Ahrens 	uint64_t newval, used, avail;
15293b2aab18SMatthew Ahrens 	int error;
153092241e0bSTom Erickson 
15313b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
15323b2aab18SMatthew Ahrens 	if (error != 0)
15333b2aab18SMatthew Ahrens 		return (error);
15343b2aab18SMatthew Ahrens 	dd = ds->ds_dir;
1535fa9e4066Sahrens 
15361d452cf5Sahrens 	/*
15371d452cf5Sahrens 	 * If we are doing the preliminary check in open context, the
15381d452cf5Sahrens 	 * space estimates may be inaccurate.
15391d452cf5Sahrens 	 */
15403b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
15413b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15421d452cf5Sahrens 		return (0);
15433b2aab18SMatthew Ahrens 	}
15443b2aab18SMatthew Ahrens 
15453b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
15463b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_RESERVATION),
15473b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
15483b2aab18SMatthew Ahrens 	if (error != 0) {
15493b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15503b2aab18SMatthew Ahrens 		return (error);
15513b2aab18SMatthew Ahrens 	}
15521d452cf5Sahrens 
1553fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1554c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1555fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1556fa9e4066Sahrens 
1557fa9e4066Sahrens 	if (dd->dd_parent) {
1558fa9e4066Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1559fa9e4066Sahrens 		    NULL, 0, FALSE);
1560fa9e4066Sahrens 	} else {
1561fa9e4066Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1562fa9e4066Sahrens 	}
1563fa9e4066Sahrens 
1564c1379625SJustin T. Gibbs 	if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
15653b2aab18SMatthew Ahrens 		uint64_t delta = MAX(used, newval) -
1566c1379625SJustin T. Gibbs 		    MAX(used, dsl_dir_phys(dd)->dd_reserved);
1567379c004dSEric Schrock 
15683b2aab18SMatthew Ahrens 		if (delta > avail ||
1569c1379625SJustin T. Gibbs 		    (dsl_dir_phys(dd)->dd_quota > 0 &&
1570c1379625SJustin T. Gibbs 		    newval > dsl_dir_phys(dd)->dd_quota))
1571be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENOSPC);
1572379c004dSEric Schrock 	}
1573379c004dSEric Schrock 
15743b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15753b2aab18SMatthew Ahrens 	return (error);
15761d452cf5Sahrens }
15771d452cf5Sahrens 
15783b2aab18SMatthew Ahrens void
15794445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
15801d452cf5Sahrens {
15811d452cf5Sahrens 	uint64_t used;
15821d452cf5Sahrens 	int64_t delta;
15831d452cf5Sahrens 
1584a9799022Sck 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1585a9799022Sck 
15861d452cf5Sahrens 	mutex_enter(&dd->dd_lock);
1587c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1588c1379625SJustin T. Gibbs 	delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1589c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_reserved = value;
1590fa9e4066Sahrens 
1591fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1592fa9e4066Sahrens 		/* Roll up this additional usage into our ancestors */
159374e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
159474e7dc98SMatthew Ahrens 		    delta, 0, 0, tx);
1595fa9e4066Sahrens 	}
159602c8f3f0SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
15974445fffbSMatthew Ahrens }
15984445fffbSMatthew Ahrens 
15994445fffbSMatthew Ahrens 
16004445fffbSMatthew Ahrens static void
16013b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
16024445fffbSMatthew Ahrens {
16033b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
16043b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16053b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16063b2aab18SMatthew Ahrens 	uint64_t newval;
16073b2aab18SMatthew Ahrens 
16083b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
16093b2aab18SMatthew Ahrens 
1610013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1611013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds,
1612013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1613013023d4SMartin Matuska 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1614013023d4SMartin Matuska 		    &ddsqra->ddsqra_value, tx);
16154445fffbSMatthew Ahrens 
1616013023d4SMartin Matuska 		VERIFY0(dsl_prop_get_int_ds(ds,
1617013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1618013023d4SMartin Matuska 	} else {
1619013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1620013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1621013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1622013023d4SMartin Matuska 		    (longlong_t)newval);
1623013023d4SMartin Matuska 	}
1624ecd6cf80Smarks 
16253b2aab18SMatthew Ahrens 	dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
16263b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1627fa9e4066Sahrens }
1628fa9e4066Sahrens 
1629fa9e4066Sahrens int
163092241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
163192241e0bSTom Erickson     uint64_t reservation)
1632fa9e4066Sahrens {
16333b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
163492241e0bSTom Erickson 
16353b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
16363b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
16373b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = reservation;
163892241e0bSTom Erickson 
16393b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
16407d46dc6cSMatthew Ahrens 	    dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1641fa9e4066Sahrens }
1642fa9e4066Sahrens 
1643fa9e4066Sahrens static dsl_dir_t *
1644fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1645fa9e4066Sahrens {
1646fa9e4066Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1647fa9e4066Sahrens 		dsl_dir_t *dd;
1648fa9e4066Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1649fa9e4066Sahrens 			if (ds1 == dd)
1650fa9e4066Sahrens 				return (dd);
1651fa9e4066Sahrens 		}
1652fa9e4066Sahrens 	}
1653fa9e4066Sahrens 	return (NULL);
1654fa9e4066Sahrens }
1655fa9e4066Sahrens 
1656fa9e4066Sahrens /*
1657fa9e4066Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1658fa9e4066Sahrens  * ancestor?  Syncing context only.
1659fa9e4066Sahrens  */
1660fa9e4066Sahrens static int64_t
1661fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1662fa9e4066Sahrens {
1663fa9e4066Sahrens 	if (dd == ancestor)
1664fa9e4066Sahrens 		return (delta);
1665fa9e4066Sahrens 
1666fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1667c1379625SJustin T. Gibbs 	delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1668fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1669fa9e4066Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1670fa9e4066Sahrens }
1671fa9e4066Sahrens 
16723b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg {
16733b2aab18SMatthew Ahrens 	const char *ddra_oldname;
16743b2aab18SMatthew Ahrens 	const char *ddra_newname;
1675a2afb611SJerry Jelinek 	cred_t *ddra_cred;
16763b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t;
16771d452cf5Sahrens 
16783b2aab18SMatthew Ahrens /* ARGSUSED */
16791d452cf5Sahrens static int
16803b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1681fa9e4066Sahrens {
16823b2aab18SMatthew Ahrens 	int *deltap = arg;
16833b2aab18SMatthew Ahrens 	char namebuf[MAXNAMELEN];
1684fa9e4066Sahrens 
16853b2aab18SMatthew Ahrens 	dsl_dataset_name(ds, namebuf);
16863b2aab18SMatthew Ahrens 
16873b2aab18SMatthew Ahrens 	if (strlen(namebuf) + *deltap >= MAXNAMELEN)
1688be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
16893b2aab18SMatthew Ahrens 	return (0);
16903b2aab18SMatthew Ahrens }
16913b2aab18SMatthew Ahrens 
16923b2aab18SMatthew Ahrens static int
16933b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
16943b2aab18SMatthew Ahrens {
16953b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
16963b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16973b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
16983b2aab18SMatthew Ahrens 	const char *mynewname;
16993b2aab18SMatthew Ahrens 	int error;
17003b2aab18SMatthew Ahrens 	int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1701fa9e4066Sahrens 
17023b2aab18SMatthew Ahrens 	/* target dir should exist */
17033b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
17043b2aab18SMatthew Ahrens 	if (error != 0)
17053b2aab18SMatthew Ahrens 		return (error);
17063b2aab18SMatthew Ahrens 
17073b2aab18SMatthew Ahrens 	/* new parent should exist */
17083b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
17093b2aab18SMatthew Ahrens 	    &newparent, &mynewname);
17103b2aab18SMatthew Ahrens 	if (error != 0) {
17113b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
17123b2aab18SMatthew Ahrens 		return (error);
17133b2aab18SMatthew Ahrens 	}
17143b2aab18SMatthew Ahrens 
17153b2aab18SMatthew Ahrens 	/* can't rename to different pool */
17163b2aab18SMatthew Ahrens 	if (dd->dd_pool != newparent->dd_pool) {
17173b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17183b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1719be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
17203b2aab18SMatthew Ahrens 	}
17213b2aab18SMatthew Ahrens 
17223b2aab18SMatthew Ahrens 	/* new name should not already exist */
17233b2aab18SMatthew Ahrens 	if (mynewname == NULL) {
17243b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17253b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1726be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
17273b2aab18SMatthew Ahrens 	}
17283b2aab18SMatthew Ahrens 
17293b2aab18SMatthew Ahrens 	/* if the name length is growing, validate child name lengths */
17303b2aab18SMatthew Ahrens 	if (delta > 0) {
17313b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
17323b2aab18SMatthew Ahrens 		    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
17333b2aab18SMatthew Ahrens 		if (error != 0) {
17343b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17353b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17363b2aab18SMatthew Ahrens 			return (error);
17373b2aab18SMatthew Ahrens 		}
17383b2aab18SMatthew Ahrens 	}
17391d452cf5Sahrens 
1740a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1741adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1742a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1743a2afb611SJerry Jelinek 			/*
1744a2afb611SJerry Jelinek 			 * Although this is the check function and we don't
1745a2afb611SJerry Jelinek 			 * normally make on-disk changes in check functions,
1746a2afb611SJerry Jelinek 			 * we need to do that here.
1747a2afb611SJerry Jelinek 			 *
1748a2afb611SJerry Jelinek 			 * Ensure this portion of the tree's counts have been
1749a2afb611SJerry Jelinek 			 * initialized in case the new parent has limits set.
1750a2afb611SJerry Jelinek 			 */
1751a2afb611SJerry Jelinek 			dsl_dir_init_fs_ss_count(dd, tx);
1752a2afb611SJerry Jelinek 		}
1753a2afb611SJerry Jelinek 	}
1754a2afb611SJerry Jelinek 
17553b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
175699653d4eSeschrock 		/* is there enough space? */
175799653d4eSeschrock 		uint64_t myspace =
1758c1379625SJustin T. Gibbs 		    MAX(dsl_dir_phys(dd)->dd_used_bytes,
1759c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_reserved);
1760a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1761a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1762a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1763a2afb611SJerry Jelinek 
1764a2afb611SJerry Jelinek 		if (dsl_dir_is_zapified(dd)) {
1765a2afb611SJerry Jelinek 			int err;
1766a2afb611SJerry Jelinek 
1767a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1768a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1769a2afb611SJerry Jelinek 			    &fs_cnt);
1770adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1771adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1772adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1773a2afb611SJerry Jelinek 				return (err);
1774adf34077SJerry Jelinek 			}
1775a2afb611SJerry Jelinek 
1776a2afb611SJerry Jelinek 			/*
1777a2afb611SJerry Jelinek 			 * have to add 1 for the filesystem itself that we're
1778a2afb611SJerry Jelinek 			 * moving
1779a2afb611SJerry Jelinek 			 */
1780a2afb611SJerry Jelinek 			fs_cnt++;
1781a2afb611SJerry Jelinek 
1782a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1783a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1784a2afb611SJerry Jelinek 			    &ss_cnt);
1785adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1786adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1787adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1788a2afb611SJerry Jelinek 				return (err);
1789adf34077SJerry Jelinek 			}
1790a2afb611SJerry Jelinek 		}
1791fa9e4066Sahrens 
17921d452cf5Sahrens 		/* no rename into our descendant */
17933b2aab18SMatthew Ahrens 		if (closest_common_ancestor(dd, newparent) == dd) {
17943b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17953b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
1796be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
17973b2aab18SMatthew Ahrens 		}
1798fa9e4066Sahrens 
17993b2aab18SMatthew Ahrens 		error = dsl_dir_transfer_possible(dd->dd_parent,
1800a2afb611SJerry Jelinek 		    newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
18013b2aab18SMatthew Ahrens 		if (error != 0) {
18023b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
18033b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
18043b2aab18SMatthew Ahrens 			return (error);
18053b2aab18SMatthew Ahrens 		}
18061d452cf5Sahrens 	}
1807fa9e4066Sahrens 
18083b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
18093b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
18101d452cf5Sahrens 	return (0);
18111d452cf5Sahrens }
18121d452cf5Sahrens 
18131d452cf5Sahrens static void
18143b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
18151d452cf5Sahrens {
18163b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
18173b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18183b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
18193b2aab18SMatthew Ahrens 	const char *mynewname;
18203b2aab18SMatthew Ahrens 	int error;
18211d452cf5Sahrens 	objset_t *mos = dp->dp_meta_objset;
18221d452cf5Sahrens 
18233b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
18243b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
18253b2aab18SMatthew Ahrens 	    &mynewname));
18261d452cf5Sahrens 
18274445fffbSMatthew Ahrens 	/* Log this before we change the name. */
18284445fffbSMatthew Ahrens 	spa_history_log_internal_dd(dd, "rename", tx,
18293b2aab18SMatthew Ahrens 	    "-> %s", ddra->ddra_newname);
18304445fffbSMatthew Ahrens 
18313b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
1832a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1833a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1834a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1835a2afb611SJerry Jelinek 
1836a2afb611SJerry Jelinek 		/*
1837a2afb611SJerry Jelinek 		 * We already made sure the dd counts were initialized in the
1838a2afb611SJerry Jelinek 		 * check function.
1839a2afb611SJerry Jelinek 		 */
1840adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1841a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1842a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1843a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1844a2afb611SJerry Jelinek 			    &fs_cnt));
1845a2afb611SJerry Jelinek 			/* add 1 for the filesystem itself that we're moving */
1846a2afb611SJerry Jelinek 			fs_cnt++;
1847a2afb611SJerry Jelinek 
1848a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1849a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1850a2afb611SJerry Jelinek 			    &ss_cnt));
1851a2afb611SJerry Jelinek 		}
1852a2afb611SJerry Jelinek 
1853a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1854a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1855a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, fs_cnt,
1856a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1857a2afb611SJerry Jelinek 
1858a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1859a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1860a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, ss_cnt,
1861a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1862a2afb611SJerry Jelinek 
186374e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1864c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_used_bytes,
1865c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_compressed_bytes,
1866c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
18673b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1868c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes,
1869c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_compressed_bytes,
1870c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
187174e7dc98SMatthew Ahrens 
1872c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_reserved >
1873c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes) {
1874c1379625SJustin T. Gibbs 			uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1875c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_used_bytes;
187674e7dc98SMatthew Ahrens 
187774e7dc98SMatthew Ahrens 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
187874e7dc98SMatthew Ahrens 			    -unused_rsrv, 0, 0, tx);
18793b2aab18SMatthew Ahrens 			dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
188074e7dc98SMatthew Ahrens 			    unused_rsrv, 0, 0, tx);
188174e7dc98SMatthew Ahrens 		}
1882fa9e4066Sahrens 	}
1883fa9e4066Sahrens 
1884fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1885fa9e4066Sahrens 
1886fa9e4066Sahrens 	/* remove from old parent zapobj */
1887c1379625SJustin T. Gibbs 	error = zap_remove(mos,
1888c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1889fa9e4066Sahrens 	    dd->dd_myname, tx);
18903b2aab18SMatthew Ahrens 	ASSERT0(error);
1891fa9e4066Sahrens 
18923b2aab18SMatthew Ahrens 	(void) strcpy(dd->dd_myname, mynewname);
18933b2aab18SMatthew Ahrens 	dsl_dir_rele(dd->dd_parent, dd);
1894c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
18953b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp,
18963b2aab18SMatthew Ahrens 	    newparent->dd_object, NULL, dd, &dd->dd_parent));
1897fa9e4066Sahrens 
1898fa9e4066Sahrens 	/* add to new parent zapobj */
1899c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
19003b2aab18SMatthew Ahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx));
19013b2aab18SMatthew Ahrens 
19023b2aab18SMatthew Ahrens 	dsl_prop_notify_all(dd);
1903ecd6cf80Smarks 
19043b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
19053b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
19061d452cf5Sahrens }
1907fa9e4066Sahrens 
19081d452cf5Sahrens int
19093b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname)
19101d452cf5Sahrens {
19113b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t ddra;
19121d452cf5Sahrens 
19133b2aab18SMatthew Ahrens 	ddra.ddra_oldname = oldname;
19143b2aab18SMatthew Ahrens 	ddra.ddra_newname = newname;
1915a2afb611SJerry Jelinek 	ddra.ddra_cred = CRED();
19161d452cf5Sahrens 
19173b2aab18SMatthew Ahrens 	return (dsl_sync_task(oldname,
19187d46dc6cSMatthew Ahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
19197d46dc6cSMatthew Ahrens 	    3, ZFS_SPACE_CHECK_RESERVED));
1920fa9e4066Sahrens }
192199653d4eSeschrock 
192299653d4eSeschrock int
1923a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1924a2afb611SJerry Jelinek     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
192599653d4eSeschrock {
192699653d4eSeschrock 	dsl_dir_t *ancestor;
192799653d4eSeschrock 	int64_t adelta;
192899653d4eSeschrock 	uint64_t avail;
1929a2afb611SJerry Jelinek 	int err;
193099653d4eSeschrock 
193199653d4eSeschrock 	ancestor = closest_common_ancestor(sdd, tdd);
193299653d4eSeschrock 	adelta = would_change(sdd, -space, ancestor);
193399653d4eSeschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
193499653d4eSeschrock 	if (avail < space)
1935be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
193699653d4eSeschrock 
1937a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1938a2afb611SJerry Jelinek 	    ancestor, cr);
1939a2afb611SJerry Jelinek 	if (err != 0)
1940a2afb611SJerry Jelinek 		return (err);
1941a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1942a2afb611SJerry Jelinek 	    ancestor, cr);
1943a2afb611SJerry Jelinek 	if (err != 0)
1944a2afb611SJerry Jelinek 		return (err);
1945a2afb611SJerry Jelinek 
194699653d4eSeschrock 	return (0);
194799653d4eSeschrock }
194871eb0538SChris Kirby 
194971eb0538SChris Kirby timestruc_t
195071eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd)
195171eb0538SChris Kirby {
195271eb0538SChris Kirby 	timestruc_t t;
195371eb0538SChris Kirby 
195471eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
195571eb0538SChris Kirby 	t = dd->dd_snap_cmtime;
195671eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
195771eb0538SChris Kirby 
195871eb0538SChris Kirby 	return (t);
195971eb0538SChris Kirby }
196071eb0538SChris Kirby 
196171eb0538SChris Kirby void
196271eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
196371eb0538SChris Kirby {
196471eb0538SChris Kirby 	timestruc_t t;
196571eb0538SChris Kirby 
196671eb0538SChris Kirby 	gethrestime(&t);
196771eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
196871eb0538SChris Kirby 	dd->dd_snap_cmtime = t;
196971eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
197071eb0538SChris Kirby }
19712acef22dSMatthew Ahrens 
19722acef22dSMatthew Ahrens void
19732acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
19742acef22dSMatthew Ahrens {
19752acef22dSMatthew Ahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
19762acef22dSMatthew Ahrens 	dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
19772acef22dSMatthew Ahrens }
1978a2afb611SJerry Jelinek 
1979a2afb611SJerry Jelinek boolean_t
1980a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd)
1981a2afb611SJerry Jelinek {
1982a2afb611SJerry Jelinek 	dmu_object_info_t doi;
1983a2afb611SJerry Jelinek 
1984a2afb611SJerry Jelinek 	dmu_object_info_from_db(dd->dd_dbuf, &doi);
1985a2afb611SJerry Jelinek 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
1986a2afb611SJerry Jelinek }
1987