xref: /illumos-gate/usr/src/uts/common/fs/zfs/dsl_dir.c (revision 9adfa60d484ce2435f5af77cc99dcd4e692b6660)
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.
2703b1c297SAlexander 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 
15003bad06fSJustin Gibbs 	dsl_prop_fini(dd);
1515ad82045Snd 	mutex_destroy(&dd->dd_lock);
152fa9e4066Sahrens 	kmem_free(dd, sizeof (dsl_dir_t));
153fa9e4066Sahrens }
154fa9e4066Sahrens 
155ea8dc4b6Seschrock int
1563b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
157ea8dc4b6Seschrock     const char *tail, void *tag, dsl_dir_t **ddp)
158fa9e4066Sahrens {
159fa9e4066Sahrens 	dmu_buf_t *dbuf;
160fa9e4066Sahrens 	dsl_dir_t *dd;
161ea8dc4b6Seschrock 	int err;
162fa9e4066Sahrens 
1633b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
164fa9e4066Sahrens 
165ea8dc4b6Seschrock 	err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
1663b2aab18SMatthew Ahrens 	if (err != 0)
167ea8dc4b6Seschrock 		return (err);
168fa9e4066Sahrens 	dd = dmu_buf_get_user(dbuf);
169fa9e4066Sahrens #ifdef ZFS_DEBUG
170fa9e4066Sahrens 	{
171fa9e4066Sahrens 		dmu_object_info_t doi;
172fa9e4066Sahrens 		dmu_object_info_from_db(dbuf, &doi);
1732acef22dSMatthew Ahrens 		ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
17474e7dc98SMatthew Ahrens 		ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
175fa9e4066Sahrens 	}
176fa9e4066Sahrens #endif
177fa9e4066Sahrens 	if (dd == NULL) {
178fa9e4066Sahrens 		dsl_dir_t *winner;
179fa9e4066Sahrens 
180fa9e4066Sahrens 		dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
181fa9e4066Sahrens 		dd->dd_object = ddobj;
182fa9e4066Sahrens 		dd->dd_dbuf = dbuf;
183fa9e4066Sahrens 		dd->dd_pool = dp;
1845ad82045Snd 		mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
18503bad06fSJustin Gibbs 		dsl_prop_init(dd);
186fa9e4066Sahrens 
18771eb0538SChris Kirby 		dsl_dir_snap_cmtime_update(dd);
18871eb0538SChris Kirby 
189c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_parent_obj) {
190c1379625SJustin T. Gibbs 			err = dsl_dir_hold_obj(dp,
191c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
192c1379625SJustin T. Gibbs 			    &dd->dd_parent);
1933b2aab18SMatthew Ahrens 			if (err != 0)
19474e7dc98SMatthew Ahrens 				goto errout;
195fa9e4066Sahrens 			if (tail) {
196fa9e4066Sahrens #ifdef ZFS_DEBUG
197fa9e4066Sahrens 				uint64_t foundobj;
198fa9e4066Sahrens 
199fa9e4066Sahrens 				err = zap_lookup(dp->dp_meta_objset,
200c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
201c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj, tail,
202c1379625SJustin T. Gibbs 				    sizeof (foundobj), 1, &foundobj);
203ea8dc4b6Seschrock 				ASSERT(err || foundobj == ddobj);
204fa9e4066Sahrens #endif
205fa9e4066Sahrens 				(void) strcpy(dd->dd_myname, tail);
206fa9e4066Sahrens 			} else {
207fa9e4066Sahrens 				err = zap_value_search(dp->dp_meta_objset,
208c1379625SJustin T. Gibbs 				    dsl_dir_phys(dd->dd_parent)->
209c1379625SJustin T. Gibbs 				    dd_child_dir_zapobj,
210e7437265Sahrens 				    ddobj, 0, dd->dd_myname);
211ea8dc4b6Seschrock 			}
2123b2aab18SMatthew Ahrens 			if (err != 0)
21374e7dc98SMatthew Ahrens 				goto errout;
214fa9e4066Sahrens 		} else {
215fa9e4066Sahrens 			(void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
216fa9e4066Sahrens 		}
217fa9e4066Sahrens 
2183f9d6ad7SLin Ling 		if (dsl_dir_is_clone(dd)) {
2193f9d6ad7SLin Ling 			dmu_buf_t *origin_bonus;
2203f9d6ad7SLin Ling 			dsl_dataset_phys_t *origin_phys;
2213f9d6ad7SLin Ling 
2223f9d6ad7SLin Ling 			/*
2233f9d6ad7SLin Ling 			 * We can't open the origin dataset, because
2243f9d6ad7SLin Ling 			 * that would require opening this dsl_dir.
2253f9d6ad7SLin Ling 			 * Just look at its phys directly instead.
2263f9d6ad7SLin Ling 			 */
2273f9d6ad7SLin Ling 			err = dmu_bonus_hold(dp->dp_meta_objset,
228c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_origin_obj, FTAG,
229c1379625SJustin T. Gibbs 			    &origin_bonus);
2303b2aab18SMatthew Ahrens 			if (err != 0)
2313f9d6ad7SLin Ling 				goto errout;
2323f9d6ad7SLin Ling 			origin_phys = origin_bonus->db_data;
2333f9d6ad7SLin Ling 			dd->dd_origin_txg =
2343f9d6ad7SLin Ling 			    origin_phys->ds_creation_txg;
2353f9d6ad7SLin Ling 			dmu_buf_rele(origin_bonus, FTAG);
2363f9d6ad7SLin Ling 		}
2373f9d6ad7SLin Ling 
238bc9014e6SJustin Gibbs 		dmu_buf_init_user(&dd->dd_dbu, dsl_dir_evict, &dd->dd_dbuf);
239bc9014e6SJustin Gibbs 		winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
240bc9014e6SJustin Gibbs 		if (winner != NULL) {
241fa9e4066Sahrens 			if (dd->dd_parent)
2423b2aab18SMatthew Ahrens 				dsl_dir_rele(dd->dd_parent, dd);
24303bad06fSJustin Gibbs 			dsl_prop_fini(dd);
2445ad82045Snd 			mutex_destroy(&dd->dd_lock);
245fa9e4066Sahrens 			kmem_free(dd, sizeof (dsl_dir_t));
246fa9e4066Sahrens 			dd = winner;
247fa9e4066Sahrens 		} else {
248fa9e4066Sahrens 			spa_open_ref(dp->dp_spa, dd);
249fa9e4066Sahrens 		}
250fa9e4066Sahrens 	}
251fa9e4066Sahrens 
252fa9e4066Sahrens 	/*
253fa9e4066Sahrens 	 * The dsl_dir_t has both open-to-close and instantiate-to-evict
254fa9e4066Sahrens 	 * holds on the spa.  We need the open-to-close holds because
255fa9e4066Sahrens 	 * otherwise the spa_refcnt wouldn't change when we open a
256fa9e4066Sahrens 	 * dir which the spa also has open, so we could incorrectly
257fa9e4066Sahrens 	 * think it was OK to unload/export/destroy the pool.  We need
258fa9e4066Sahrens 	 * the instantiate-to-evict hold because the dsl_dir_t has a
259fa9e4066Sahrens 	 * pointer to the dd_pool, which has a pointer to the spa_t.
260fa9e4066Sahrens 	 */
261fa9e4066Sahrens 	spa_open_ref(dp->dp_spa, tag);
262fa9e4066Sahrens 	ASSERT3P(dd->dd_pool, ==, dp);
263fa9e4066Sahrens 	ASSERT3U(dd->dd_object, ==, ddobj);
264fa9e4066Sahrens 	ASSERT3P(dd->dd_dbuf, ==, dbuf);
265ea8dc4b6Seschrock 	*ddp = dd;
266ea8dc4b6Seschrock 	return (0);
26774e7dc98SMatthew Ahrens 
26874e7dc98SMatthew Ahrens errout:
26974e7dc98SMatthew Ahrens 	if (dd->dd_parent)
2703b2aab18SMatthew Ahrens 		dsl_dir_rele(dd->dd_parent, dd);
27103bad06fSJustin Gibbs 	dsl_prop_fini(dd);
27274e7dc98SMatthew Ahrens 	mutex_destroy(&dd->dd_lock);
27374e7dc98SMatthew Ahrens 	kmem_free(dd, sizeof (dsl_dir_t));
27474e7dc98SMatthew Ahrens 	dmu_buf_rele(dbuf, tag);
27574e7dc98SMatthew Ahrens 	return (err);
276fa9e4066Sahrens }
277fa9e4066Sahrens 
278fa9e4066Sahrens void
2793b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag)
280fa9e4066Sahrens {
281fa9e4066Sahrens 	dprintf_dd(dd, "%s\n", "");
282fa9e4066Sahrens 	spa_close(dd->dd_pool->dp_spa, tag);
283ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, tag);
284fa9e4066Sahrens }
285fa9e4066Sahrens 
286bc9014e6SJustin Gibbs /*
287bc9014e6SJustin Gibbs  * Remove a reference to the given dsl dir that is being asynchronously
288bc9014e6SJustin Gibbs  * released.  Async releases occur from a taskq performing eviction of
289bc9014e6SJustin Gibbs  * dsl datasets and dirs.  This process is identical to a normal release
290bc9014e6SJustin Gibbs  * with the exception of using the async API for releasing the reference on
291bc9014e6SJustin Gibbs  * the spa.
292bc9014e6SJustin Gibbs  */
293bc9014e6SJustin Gibbs void
294bc9014e6SJustin Gibbs dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
295bc9014e6SJustin Gibbs {
296bc9014e6SJustin Gibbs 	dprintf_dd(dd, "%s\n", "");
297bc9014e6SJustin Gibbs 	spa_async_close(dd->dd_pool->dp_spa, tag);
298bc9014e6SJustin Gibbs 	dmu_buf_rele(dd->dd_dbuf, tag);
299bc9014e6SJustin Gibbs }
300bc9014e6SJustin Gibbs 
301*9adfa60dSMatthew Ahrens /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
302fa9e4066Sahrens void
303fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
304fa9e4066Sahrens {
305fa9e4066Sahrens 	if (dd->dd_parent) {
306fa9e4066Sahrens 		dsl_dir_name(dd->dd_parent, buf);
307*9adfa60dSMatthew Ahrens 		VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
308*9adfa60dSMatthew Ahrens 		    ZFS_MAX_DATASET_NAME_LEN);
309fa9e4066Sahrens 	} else {
310fa9e4066Sahrens 		buf[0] = '\0';
311fa9e4066Sahrens 	}
312fa9e4066Sahrens 	if (!MUTEX_HELD(&dd->dd_lock)) {
313fa9e4066Sahrens 		/*
314fa9e4066Sahrens 		 * recursive mutex so that we can use
315fa9e4066Sahrens 		 * dprintf_dd() with dd_lock held
316fa9e4066Sahrens 		 */
317fa9e4066Sahrens 		mutex_enter(&dd->dd_lock);
318*9adfa60dSMatthew Ahrens 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
319*9adfa60dSMatthew Ahrens 		    <, ZFS_MAX_DATASET_NAME_LEN);
320fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
321fa9e4066Sahrens 	} else {
322*9adfa60dSMatthew Ahrens 		VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
323*9adfa60dSMatthew Ahrens 		    <, ZFS_MAX_DATASET_NAME_LEN);
324fa9e4066Sahrens 	}
325fa9e4066Sahrens }
326fa9e4066Sahrens 
327ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
328b7661cccSmmusante int
329b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd)
330b7661cccSmmusante {
331b7661cccSmmusante 	int result = 0;
332b7661cccSmmusante 
333b7661cccSmmusante 	if (dd->dd_parent) {
334b7661cccSmmusante 		/* parent's name + 1 for the "/" */
335b7661cccSmmusante 		result = dsl_dir_namelen(dd->dd_parent) + 1;
336b7661cccSmmusante 	}
337b7661cccSmmusante 
338b7661cccSmmusante 	if (!MUTEX_HELD(&dd->dd_lock)) {
339b7661cccSmmusante 		/* see dsl_dir_name */
340b7661cccSmmusante 		mutex_enter(&dd->dd_lock);
341b7661cccSmmusante 		result += strlen(dd->dd_myname);
342b7661cccSmmusante 		mutex_exit(&dd->dd_lock);
343b7661cccSmmusante 	} else {
344b7661cccSmmusante 		result += strlen(dd->dd_myname);
345b7661cccSmmusante 	}
346b7661cccSmmusante 
347b7661cccSmmusante 	return (result);
348b7661cccSmmusante }
349b7661cccSmmusante 
350fa9e4066Sahrens static int
351fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp)
352fa9e4066Sahrens {
353fa9e4066Sahrens 	char *p;
3543b2aab18SMatthew Ahrens 
355ccba0801SRich Morris 	if ((path == NULL) || (path[0] == '\0'))
356be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOENT));
357fa9e4066Sahrens 	/* This would be a good place to reserve some namespace... */
358fa9e4066Sahrens 	p = strpbrk(path, "/@");
359fa9e4066Sahrens 	if (p && (p[1] == '/' || p[1] == '@')) {
360fa9e4066Sahrens 		/* two separators in a row */
361be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
362fa9e4066Sahrens 	}
363fa9e4066Sahrens 	if (p == NULL || p == path) {
364fa9e4066Sahrens 		/*
365fa9e4066Sahrens 		 * if the first thing is an @ or /, it had better be an
366fa9e4066Sahrens 		 * @ and it had better not have any more ats or slashes,
367fa9e4066Sahrens 		 * and it had better have something after the @.
368fa9e4066Sahrens 		 */
369fa9e4066Sahrens 		if (p != NULL &&
370fa9e4066Sahrens 		    (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
371be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
372*9adfa60dSMatthew Ahrens 		if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
373be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
374fa9e4066Sahrens 		(void) strcpy(component, path);
375fa9e4066Sahrens 		p = NULL;
376fa9e4066Sahrens 	} else if (p[0] == '/') {
377*9adfa60dSMatthew Ahrens 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
378be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
379fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
3803b2aab18SMatthew Ahrens 		component[p - path] = '\0';
381fa9e4066Sahrens 		p++;
382fa9e4066Sahrens 	} else if (p[0] == '@') {
383fa9e4066Sahrens 		/*
384fa9e4066Sahrens 		 * if the next separator is an @, there better not be
385fa9e4066Sahrens 		 * any more slashes.
386fa9e4066Sahrens 		 */
387fa9e4066Sahrens 		if (strchr(path, '/'))
388be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
389*9adfa60dSMatthew Ahrens 		if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
390be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENAMETOOLONG));
391fa9e4066Sahrens 		(void) strncpy(component, path, p - path);
3923b2aab18SMatthew Ahrens 		component[p - path] = '\0';
393fa9e4066Sahrens 	} else {
3943b2aab18SMatthew Ahrens 		panic("invalid p=%p", (void *)p);
395fa9e4066Sahrens 	}
396fa9e4066Sahrens 	*nextp = p;
397fa9e4066Sahrens 	return (0);
398fa9e4066Sahrens }
399fa9e4066Sahrens 
400fa9e4066Sahrens /*
4013b2aab18SMatthew Ahrens  * Return the dsl_dir_t, and possibly the last component which couldn't
4023b2aab18SMatthew Ahrens  * be found in *tail.  The name must be in the specified dsl_pool_t.  This
4033b2aab18SMatthew Ahrens  * thread must hold the dp_config_rwlock for the pool.  Returns NULL if the
4043b2aab18SMatthew Ahrens  * path is bogus, or if tail==NULL and we couldn't parse the whole name.
4053b2aab18SMatthew Ahrens  * (*tail)[0] == '@' means that the last component is a snapshot.
406fa9e4066Sahrens  */
407ea8dc4b6Seschrock int
4083b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
409ea8dc4b6Seschrock     dsl_dir_t **ddp, const char **tailp)
410fa9e4066Sahrens {
411*9adfa60dSMatthew Ahrens 	char buf[ZFS_MAX_DATASET_NAME_LEN];
4123b2aab18SMatthew Ahrens 	const char *spaname, *next, *nextnext = NULL;
413fa9e4066Sahrens 	int err;
414fa9e4066Sahrens 	dsl_dir_t *dd;
415fa9e4066Sahrens 	uint64_t ddobj;
416fa9e4066Sahrens 
417fa9e4066Sahrens 	err = getcomponent(name, buf, &next);
4183b2aab18SMatthew Ahrens 	if (err != 0)
419ea8dc4b6Seschrock 		return (err);
420fa9e4066Sahrens 
4213b2aab18SMatthew Ahrens 	/* Make sure the name is in the specified pool. */
4223b2aab18SMatthew Ahrens 	spaname = spa_name(dp->dp_spa);
4233b2aab18SMatthew Ahrens 	if (strcmp(buf, spaname) != 0)
42403b1c297SAlexander Eremin 		return (SET_ERROR(EXDEV));
425fa9e4066Sahrens 
4263b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
427fa9e4066Sahrens 
4283b2aab18SMatthew Ahrens 	err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
4293b2aab18SMatthew Ahrens 	if (err != 0) {
430ea8dc4b6Seschrock 		return (err);
431ea8dc4b6Seschrock 	}
432ea8dc4b6Seschrock 
433fa9e4066Sahrens 	while (next != NULL) {
434bc9014e6SJustin Gibbs 		dsl_dir_t *child_dd;
435fa9e4066Sahrens 		err = getcomponent(next, buf, &nextnext);
4363b2aab18SMatthew Ahrens 		if (err != 0)
437ea8dc4b6Seschrock 			break;
438fa9e4066Sahrens 		ASSERT(next[0] != '\0');
439fa9e4066Sahrens 		if (next[0] == '@')
440fa9e4066Sahrens 			break;
441fa9e4066Sahrens 		dprintf("looking up %s in obj%lld\n",
442c1379625SJustin T. Gibbs 		    buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
443fa9e4066Sahrens 
444fa9e4066Sahrens 		err = zap_lookup(dp->dp_meta_objset,
445c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_child_dir_zapobj,
446fa9e4066Sahrens 		    buf, sizeof (ddobj), 1, &ddobj);
4473b2aab18SMatthew Ahrens 		if (err != 0) {
448ea8dc4b6Seschrock 			if (err == ENOENT)
449ea8dc4b6Seschrock 				err = 0;
450fa9e4066Sahrens 			break;
451fa9e4066Sahrens 		}
452fa9e4066Sahrens 
453bc9014e6SJustin Gibbs 		err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
4543b2aab18SMatthew Ahrens 		if (err != 0)
455ea8dc4b6Seschrock 			break;
4563b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
457bc9014e6SJustin Gibbs 		dd = child_dd;
458fa9e4066Sahrens 		next = nextnext;
459fa9e4066Sahrens 	}
460fa9e4066Sahrens 
4613b2aab18SMatthew Ahrens 	if (err != 0) {
4623b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
463ea8dc4b6Seschrock 		return (err);
464ea8dc4b6Seschrock 	}
465ea8dc4b6Seschrock 
466fa9e4066Sahrens 	/*
467fa9e4066Sahrens 	 * It's an error if there's more than one component left, or
468fa9e4066Sahrens 	 * tailp==NULL and there's any component left.
469fa9e4066Sahrens 	 */
470fa9e4066Sahrens 	if (next != NULL &&
471fa9e4066Sahrens 	    (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
472fa9e4066Sahrens 		/* bad path name */
4733b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, tag);
474fa9e4066Sahrens 		dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
475be6fd75aSMatthew Ahrens 		err = SET_ERROR(ENOENT);
476fa9e4066Sahrens 	}
4773b2aab18SMatthew Ahrens 	if (tailp != NULL)
478fa9e4066Sahrens 		*tailp = next;
479ea8dc4b6Seschrock 	*ddp = dd;
480ea8dc4b6Seschrock 	return (err);
481fa9e4066Sahrens }
482fa9e4066Sahrens 
483a2afb611SJerry Jelinek /*
484a2afb611SJerry Jelinek  * If the counts are already initialized for this filesystem and its
485a2afb611SJerry Jelinek  * descendants then do nothing, otherwise initialize the counts.
486a2afb611SJerry Jelinek  *
487a2afb611SJerry Jelinek  * The counts on this filesystem, and those below, may be uninitialized due to
488a2afb611SJerry Jelinek  * either the use of a pre-existing pool which did not support the
489a2afb611SJerry Jelinek  * filesystem/snapshot limit feature, or one in which the feature had not yet
490a2afb611SJerry Jelinek  * been enabled.
491a2afb611SJerry Jelinek  *
492a2afb611SJerry Jelinek  * Recursively descend the filesystem tree and update the filesystem/snapshot
493a2afb611SJerry Jelinek  * counts on each filesystem below, then update the cumulative count on the
494a2afb611SJerry Jelinek  * current filesystem. If the filesystem already has a count set on it,
495a2afb611SJerry Jelinek  * then we know that its counts, and the counts on the filesystems below it,
496a2afb611SJerry Jelinek  * are already correct, so we don't have to update this filesystem.
497a2afb611SJerry Jelinek  */
498a2afb611SJerry Jelinek static void
499a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
500a2afb611SJerry Jelinek {
501a2afb611SJerry Jelinek 	uint64_t my_fs_cnt = 0;
502a2afb611SJerry Jelinek 	uint64_t my_ss_cnt = 0;
503a2afb611SJerry Jelinek 	dsl_pool_t *dp = dd->dd_pool;
504a2afb611SJerry Jelinek 	objset_t *os = dp->dp_meta_objset;
505a2afb611SJerry Jelinek 	zap_cursor_t *zc;
506a2afb611SJerry Jelinek 	zap_attribute_t *za;
507a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
508a2afb611SJerry Jelinek 
509adf34077SJerry Jelinek 	ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
510a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dp));
511a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
512a2afb611SJerry Jelinek 
513a2afb611SJerry Jelinek 	dsl_dir_zapify(dd, tx);
514a2afb611SJerry Jelinek 
515a2afb611SJerry Jelinek 	/*
516a2afb611SJerry Jelinek 	 * If the filesystem count has already been initialized then we
517a2afb611SJerry Jelinek 	 * don't need to recurse down any further.
518a2afb611SJerry Jelinek 	 */
519a2afb611SJerry Jelinek 	if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
520a2afb611SJerry Jelinek 		return;
521a2afb611SJerry Jelinek 
522a2afb611SJerry Jelinek 	zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
523a2afb611SJerry Jelinek 	za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
524a2afb611SJerry Jelinek 
525a2afb611SJerry Jelinek 	/* Iterate my child dirs */
526c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
527a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
528a2afb611SJerry Jelinek 		dsl_dir_t *chld_dd;
529a2afb611SJerry Jelinek 		uint64_t count;
530a2afb611SJerry Jelinek 
531a2afb611SJerry Jelinek 		VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
532a2afb611SJerry Jelinek 		    &chld_dd));
533a2afb611SJerry Jelinek 
534a2afb611SJerry Jelinek 		/*
535a2afb611SJerry Jelinek 		 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
536a2afb611SJerry Jelinek 		 * temporary datasets.
537a2afb611SJerry Jelinek 		 */
538a2afb611SJerry Jelinek 		if (chld_dd->dd_myname[0] == '$' ||
539a2afb611SJerry Jelinek 		    chld_dd->dd_myname[0] == '%') {
540a2afb611SJerry Jelinek 			dsl_dir_rele(chld_dd, FTAG);
541a2afb611SJerry Jelinek 			continue;
542a2afb611SJerry Jelinek 		}
543a2afb611SJerry Jelinek 
544a2afb611SJerry Jelinek 		my_fs_cnt++;	/* count this child */
545a2afb611SJerry Jelinek 
546a2afb611SJerry Jelinek 		dsl_dir_init_fs_ss_count(chld_dd, tx);
547a2afb611SJerry Jelinek 
548a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
549a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
550a2afb611SJerry Jelinek 		my_fs_cnt += count;
551a2afb611SJerry Jelinek 		VERIFY0(zap_lookup(os, chld_dd->dd_object,
552a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
553a2afb611SJerry Jelinek 		my_ss_cnt += count;
554a2afb611SJerry Jelinek 
555a2afb611SJerry Jelinek 		dsl_dir_rele(chld_dd, FTAG);
556a2afb611SJerry Jelinek 	}
557a2afb611SJerry Jelinek 	zap_cursor_fini(zc);
558a2afb611SJerry Jelinek 	/* Count my snapshots (we counted children's snapshots above) */
559a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
560c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
561a2afb611SJerry Jelinek 
562c1379625SJustin T. Gibbs 	for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
563a2afb611SJerry Jelinek 	    zap_cursor_retrieve(zc, za) == 0;
564a2afb611SJerry Jelinek 	    zap_cursor_advance(zc)) {
565a2afb611SJerry Jelinek 		/* Don't count temporary snapshots */
566a2afb611SJerry Jelinek 		if (za->za_name[0] != '%')
567a2afb611SJerry Jelinek 			my_ss_cnt++;
568a2afb611SJerry Jelinek 	}
569fb7001f1SAlex Reece 	zap_cursor_fini(zc);
570a2afb611SJerry Jelinek 
571a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
572a2afb611SJerry Jelinek 
573a2afb611SJerry Jelinek 	kmem_free(zc, sizeof (zap_cursor_t));
574a2afb611SJerry Jelinek 	kmem_free(za, sizeof (zap_attribute_t));
575a2afb611SJerry Jelinek 
576a2afb611SJerry Jelinek 	/* we're in a sync task, update counts */
577a2afb611SJerry Jelinek 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
578a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
579a2afb611SJerry Jelinek 	    sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
580a2afb611SJerry Jelinek 	VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
581a2afb611SJerry Jelinek 	    sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
582a2afb611SJerry Jelinek }
583a2afb611SJerry Jelinek 
584a2afb611SJerry Jelinek static int
585a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
586a2afb611SJerry Jelinek {
587a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
588a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
589a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
590a2afb611SJerry Jelinek 	dsl_dir_t *dd;
591a2afb611SJerry Jelinek 	int error;
592a2afb611SJerry Jelinek 
593a2afb611SJerry Jelinek 	error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
594a2afb611SJerry Jelinek 	if (error != 0)
595a2afb611SJerry Jelinek 		return (error);
596a2afb611SJerry Jelinek 
597a2afb611SJerry Jelinek 	if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
598a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
599a2afb611SJerry Jelinek 		return (SET_ERROR(ENOTSUP));
600a2afb611SJerry Jelinek 	}
601a2afb611SJerry Jelinek 
602a2afb611SJerry Jelinek 	dd = ds->ds_dir;
603a2afb611SJerry Jelinek 	if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
604a2afb611SJerry Jelinek 	    dsl_dir_is_zapified(dd) &&
605a2afb611SJerry Jelinek 	    zap_contains(dp->dp_meta_objset, dd->dd_object,
606a2afb611SJerry Jelinek 	    DD_FIELD_FILESYSTEM_COUNT) == 0) {
607a2afb611SJerry Jelinek 		dsl_dataset_rele(ds, FTAG);
608a2afb611SJerry Jelinek 		return (SET_ERROR(EALREADY));
609a2afb611SJerry Jelinek 	}
610a2afb611SJerry Jelinek 
611a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
612a2afb611SJerry Jelinek 	return (0);
613a2afb611SJerry Jelinek }
614a2afb611SJerry Jelinek 
615a2afb611SJerry Jelinek static void
616a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
617a2afb611SJerry Jelinek {
618a2afb611SJerry Jelinek 	char *ddname = (char *)arg;
619a2afb611SJerry Jelinek 	dsl_pool_t *dp = dmu_tx_pool(tx);
620a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
621a2afb611SJerry Jelinek 	spa_t *spa;
622a2afb611SJerry Jelinek 
623a2afb611SJerry Jelinek 	VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
624a2afb611SJerry Jelinek 
625a2afb611SJerry Jelinek 	spa = dsl_dataset_get_spa(ds);
626a2afb611SJerry Jelinek 
627a2afb611SJerry Jelinek 	if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
628a2afb611SJerry Jelinek 		/*
629a2afb611SJerry Jelinek 		 * Since the feature was not active and we're now setting a
630a2afb611SJerry Jelinek 		 * limit, increment the feature-active counter so that the
631a2afb611SJerry Jelinek 		 * feature becomes active for the first time.
632a2afb611SJerry Jelinek 		 *
633a2afb611SJerry Jelinek 		 * We are already in a sync task so we can update the MOS.
634a2afb611SJerry Jelinek 		 */
635a2afb611SJerry Jelinek 		spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
636a2afb611SJerry Jelinek 	}
637a2afb611SJerry Jelinek 
638a2afb611SJerry Jelinek 	/*
639a2afb611SJerry Jelinek 	 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
640a2afb611SJerry Jelinek 	 * we need to ensure the counts are correct. Descend down the tree from
641a2afb611SJerry Jelinek 	 * this point and update all of the counts to be accurate.
642a2afb611SJerry Jelinek 	 */
643a2afb611SJerry Jelinek 	dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
644a2afb611SJerry Jelinek 
645a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
646a2afb611SJerry Jelinek }
647a2afb611SJerry Jelinek 
648a2afb611SJerry Jelinek /*
649a2afb611SJerry Jelinek  * Make sure the feature is enabled and activate it if necessary.
650a2afb611SJerry Jelinek  * Since we're setting a limit, ensure the on-disk counts are valid.
651a2afb611SJerry Jelinek  * This is only called by the ioctl path when setting a limit value.
652a2afb611SJerry Jelinek  *
653a2afb611SJerry Jelinek  * We do not need to validate the new limit, since users who can change the
654a2afb611SJerry Jelinek  * limit are also allowed to exceed the limit.
655a2afb611SJerry Jelinek  */
656a2afb611SJerry Jelinek int
657a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname)
658a2afb611SJerry Jelinek {
659a2afb611SJerry Jelinek 	int error;
660a2afb611SJerry Jelinek 
661a2afb611SJerry Jelinek 	error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
6627d46dc6cSMatthew Ahrens 	    dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
6637d46dc6cSMatthew Ahrens 	    ZFS_SPACE_CHECK_RESERVED);
664a2afb611SJerry Jelinek 
665a2afb611SJerry Jelinek 	if (error == EALREADY)
666a2afb611SJerry Jelinek 		error = 0;
667a2afb611SJerry Jelinek 
668a2afb611SJerry Jelinek 	return (error);
669a2afb611SJerry Jelinek }
670a2afb611SJerry Jelinek 
671a2afb611SJerry Jelinek /*
672a2afb611SJerry Jelinek  * Used to determine if the filesystem_limit or snapshot_limit should be
673a2afb611SJerry Jelinek  * enforced. We allow the limit to be exceeded if the user has permission to
674a2afb611SJerry Jelinek  * write the property value. We pass in the creds that we got in the open
675a2afb611SJerry Jelinek  * context since we will always be the GZ root in syncing context. We also have
676a2afb611SJerry Jelinek  * to handle the case where we are allowed to change the limit on the current
677a2afb611SJerry Jelinek  * dataset, but there may be another limit in the tree above.
678a2afb611SJerry Jelinek  *
679a2afb611SJerry Jelinek  * We can never modify these two properties within a non-global zone. In
680a2afb611SJerry Jelinek  * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
681a2afb611SJerry Jelinek  * can't use that function since we are already holding the dp_config_rwlock.
682a2afb611SJerry Jelinek  * In addition, we already have the dd and dealing with snapshots is simplified
683a2afb611SJerry Jelinek  * in this code.
684a2afb611SJerry Jelinek  */
685a2afb611SJerry Jelinek 
686a2afb611SJerry Jelinek typedef enum {
687a2afb611SJerry Jelinek 	ENFORCE_ALWAYS,
688a2afb611SJerry Jelinek 	ENFORCE_NEVER,
689a2afb611SJerry Jelinek 	ENFORCE_ABOVE
690a2afb611SJerry Jelinek } enforce_res_t;
691a2afb611SJerry Jelinek 
692a2afb611SJerry Jelinek static enforce_res_t
693a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
694a2afb611SJerry Jelinek {
695a2afb611SJerry Jelinek 	enforce_res_t enforce = ENFORCE_ALWAYS;
696a2afb611SJerry Jelinek 	uint64_t obj;
697a2afb611SJerry Jelinek 	dsl_dataset_t *ds;
698a2afb611SJerry Jelinek 	uint64_t zoned;
699a2afb611SJerry Jelinek 
700a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
701a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
702a2afb611SJerry Jelinek 
703a2afb611SJerry Jelinek #ifdef _KERNEL
704a2afb611SJerry Jelinek 	if (crgetzoneid(cr) != GLOBAL_ZONEID)
705a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
706a2afb611SJerry Jelinek 
707a2afb611SJerry Jelinek 	if (secpolicy_zfs(cr) == 0)
708a2afb611SJerry Jelinek 		return (ENFORCE_NEVER);
709a2afb611SJerry Jelinek #endif
710a2afb611SJerry Jelinek 
711c1379625SJustin T. Gibbs 	if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
712a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
713a2afb611SJerry Jelinek 
714a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
715a2afb611SJerry Jelinek 
716a2afb611SJerry Jelinek 	if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
717a2afb611SJerry Jelinek 		return (ENFORCE_ALWAYS);
718a2afb611SJerry Jelinek 
719a2afb611SJerry Jelinek 	if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
720a2afb611SJerry Jelinek 		/* Only root can access zoned fs's from the GZ */
721a2afb611SJerry Jelinek 		enforce = ENFORCE_ALWAYS;
722a2afb611SJerry Jelinek 	} else {
723a2afb611SJerry Jelinek 		if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
724a2afb611SJerry Jelinek 			enforce = ENFORCE_ABOVE;
725a2afb611SJerry Jelinek 	}
726a2afb611SJerry Jelinek 
727a2afb611SJerry Jelinek 	dsl_dataset_rele(ds, FTAG);
728a2afb611SJerry Jelinek 	return (enforce);
729a2afb611SJerry Jelinek }
730a2afb611SJerry Jelinek 
731a2afb611SJerry Jelinek /*
732a2afb611SJerry Jelinek  * Check if adding additional child filesystem(s) would exceed any filesystem
733a2afb611SJerry Jelinek  * limits or adding additional snapshot(s) would exceed any snapshot limits.
734a2afb611SJerry Jelinek  * The prop argument indicates which limit to check.
735a2afb611SJerry Jelinek  *
736a2afb611SJerry Jelinek  * Note that all filesystem limits up to the root (or the highest
737a2afb611SJerry Jelinek  * initialized) filesystem or the given ancestor must be satisfied.
738a2afb611SJerry Jelinek  */
739a2afb611SJerry Jelinek int
740a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
741a2afb611SJerry Jelinek     dsl_dir_t *ancestor, cred_t *cr)
742a2afb611SJerry Jelinek {
743a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
744a2afb611SJerry Jelinek 	uint64_t limit, count;
745a2afb611SJerry Jelinek 	char *count_prop;
746a2afb611SJerry Jelinek 	enforce_res_t enforce;
747a2afb611SJerry Jelinek 	int err = 0;
748a2afb611SJerry Jelinek 
749a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
750a2afb611SJerry Jelinek 	ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
751a2afb611SJerry Jelinek 	    prop == ZFS_PROP_SNAPSHOT_LIMIT);
752a2afb611SJerry Jelinek 
753a2afb611SJerry Jelinek 	/*
754a2afb611SJerry Jelinek 	 * If we're allowed to change the limit, don't enforce the limit
755a2afb611SJerry Jelinek 	 * e.g. this can happen if a snapshot is taken by an administrative
756a2afb611SJerry Jelinek 	 * user in the global zone (i.e. a recursive snapshot by root).
757a2afb611SJerry Jelinek 	 * However, we must handle the case of delegated permissions where we
758a2afb611SJerry Jelinek 	 * are allowed to change the limit on the current dataset, but there
759a2afb611SJerry Jelinek 	 * is another limit in the tree above.
760a2afb611SJerry Jelinek 	 */
761a2afb611SJerry Jelinek 	enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
762a2afb611SJerry Jelinek 	if (enforce == ENFORCE_NEVER)
763a2afb611SJerry Jelinek 		return (0);
764a2afb611SJerry Jelinek 
765a2afb611SJerry Jelinek 	/*
766a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment
767a2afb611SJerry Jelinek 	 * is 0.
768a2afb611SJerry Jelinek 	 */
769a2afb611SJerry Jelinek 	if (delta == 0)
770a2afb611SJerry Jelinek 		return (0);
771a2afb611SJerry Jelinek 
772a2afb611SJerry Jelinek 	if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
773a2afb611SJerry Jelinek 		/*
774a2afb611SJerry Jelinek 		 * We don't enforce the limit for temporary snapshots. This is
775a2afb611SJerry Jelinek 		 * indicated by a NULL cred_t argument.
776a2afb611SJerry Jelinek 		 */
777a2afb611SJerry Jelinek 		if (cr == NULL)
778a2afb611SJerry Jelinek 			return (0);
779a2afb611SJerry Jelinek 
780a2afb611SJerry Jelinek 		count_prop = DD_FIELD_SNAPSHOT_COUNT;
781a2afb611SJerry Jelinek 	} else {
782a2afb611SJerry Jelinek 		count_prop = DD_FIELD_FILESYSTEM_COUNT;
783a2afb611SJerry Jelinek 	}
784a2afb611SJerry Jelinek 
785a2afb611SJerry Jelinek 	/*
786a2afb611SJerry Jelinek 	 * If an ancestor has been provided, stop checking the limit once we
787a2afb611SJerry Jelinek 	 * hit that dir. We need this during rename so that we don't overcount
788a2afb611SJerry Jelinek 	 * the check once we recurse up to the common ancestor.
789a2afb611SJerry Jelinek 	 */
790a2afb611SJerry Jelinek 	if (ancestor == dd)
791a2afb611SJerry Jelinek 		return (0);
792a2afb611SJerry Jelinek 
793a2afb611SJerry Jelinek 	/*
794a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
795a2afb611SJerry Jelinek 	 * stop since we know there is no limit here (or above). The counts are
796a2afb611SJerry Jelinek 	 * not valid on this node and we know we won't touch this node's counts.
797a2afb611SJerry Jelinek 	 */
798a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
799a2afb611SJerry Jelinek 	    count_prop, sizeof (count), 1, &count) == ENOENT)
800a2afb611SJerry Jelinek 		return (0);
801a2afb611SJerry Jelinek 
802a2afb611SJerry Jelinek 	err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
803a2afb611SJerry Jelinek 	    B_FALSE);
804a2afb611SJerry Jelinek 	if (err != 0)
805a2afb611SJerry Jelinek 		return (err);
806a2afb611SJerry Jelinek 
807a2afb611SJerry Jelinek 	/* Is there a limit which we've hit? */
808a2afb611SJerry Jelinek 	if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
809a2afb611SJerry Jelinek 		return (SET_ERROR(EDQUOT));
810a2afb611SJerry Jelinek 
811a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
812a2afb611SJerry Jelinek 		err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
813a2afb611SJerry Jelinek 		    ancestor, cr);
814a2afb611SJerry Jelinek 
815a2afb611SJerry Jelinek 	return (err);
816a2afb611SJerry Jelinek }
817a2afb611SJerry Jelinek 
818a2afb611SJerry Jelinek /*
819a2afb611SJerry Jelinek  * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
820a2afb611SJerry Jelinek  * parents. When a new filesystem/snapshot is created, increment the count on
821a2afb611SJerry Jelinek  * all parents, and when a filesystem/snapshot is destroyed, decrement the
822a2afb611SJerry Jelinek  * count.
823a2afb611SJerry Jelinek  */
824a2afb611SJerry Jelinek void
825a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
826a2afb611SJerry Jelinek     dmu_tx_t *tx)
827a2afb611SJerry Jelinek {
828a2afb611SJerry Jelinek 	int err;
829a2afb611SJerry Jelinek 	objset_t *os = dd->dd_pool->dp_meta_objset;
830a2afb611SJerry Jelinek 	uint64_t count;
831a2afb611SJerry Jelinek 
832a2afb611SJerry Jelinek 	ASSERT(dsl_pool_config_held(dd->dd_pool));
833a2afb611SJerry Jelinek 	ASSERT(dmu_tx_is_syncing(tx));
834a2afb611SJerry Jelinek 	ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
835a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
836a2afb611SJerry Jelinek 
837a2afb611SJerry Jelinek 	/*
838a2afb611SJerry Jelinek 	 * When we receive an incremental stream into a filesystem that already
839a2afb611SJerry Jelinek 	 * exists, a temporary clone is created.  We don't count this temporary
840a2afb611SJerry Jelinek 	 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
841a2afb611SJerry Jelinek 	 * $MOS & $ORIGIN) objsets.
842a2afb611SJerry Jelinek 	 */
843a2afb611SJerry Jelinek 	if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
844a2afb611SJerry Jelinek 	    strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
845a2afb611SJerry Jelinek 		return;
846a2afb611SJerry Jelinek 
847a2afb611SJerry Jelinek 	/*
848a2afb611SJerry Jelinek 	 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
849a2afb611SJerry Jelinek 	 */
850a2afb611SJerry Jelinek 	if (delta == 0)
851a2afb611SJerry Jelinek 		return;
852a2afb611SJerry Jelinek 
853a2afb611SJerry Jelinek 	/*
854a2afb611SJerry Jelinek 	 * If we hit an uninitialized node while recursing up the tree, we can
855a2afb611SJerry Jelinek 	 * stop since we know the counts are not valid on this node and we
856a2afb611SJerry Jelinek 	 * know we shouldn't touch this node's counts. An uninitialized count
857a2afb611SJerry Jelinek 	 * on the node indicates that either the feature has not yet been
858a2afb611SJerry Jelinek 	 * activated or there are no limits on this part of the tree.
859a2afb611SJerry Jelinek 	 */
860a2afb611SJerry Jelinek 	if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
861a2afb611SJerry Jelinek 	    prop, sizeof (count), 1, &count)) == ENOENT)
862a2afb611SJerry Jelinek 		return;
863a2afb611SJerry Jelinek 	VERIFY0(err);
864a2afb611SJerry Jelinek 
865a2afb611SJerry Jelinek 	count += delta;
866a2afb611SJerry Jelinek 	/* Use a signed verify to make sure we're not neg. */
867a2afb611SJerry Jelinek 	VERIFY3S(count, >=, 0);
868a2afb611SJerry Jelinek 
869a2afb611SJerry Jelinek 	VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
870a2afb611SJerry Jelinek 	    tx));
871a2afb611SJerry Jelinek 
872a2afb611SJerry Jelinek 	/* Roll up this additional count into our ancestors */
873a2afb611SJerry Jelinek 	if (dd->dd_parent != NULL)
874a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
875a2afb611SJerry Jelinek }
876a2afb611SJerry Jelinek 
8771d452cf5Sahrens uint64_t
878088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
879088f3894Sahrens     dmu_tx_t *tx)
880fa9e4066Sahrens {
881088f3894Sahrens 	objset_t *mos = dp->dp_meta_objset;
882fa9e4066Sahrens 	uint64_t ddobj;
883cde58dbcSMatthew Ahrens 	dsl_dir_phys_t *ddphys;
884fa9e4066Sahrens 	dmu_buf_t *dbuf;
885fa9e4066Sahrens 
8861649cd4bStabriz 	ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
8871649cd4bStabriz 	    DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
888088f3894Sahrens 	if (pds) {
889c1379625SJustin T. Gibbs 		VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
890088f3894Sahrens 		    name, sizeof (uint64_t), 1, &ddobj, tx));
891088f3894Sahrens 	} else {
892088f3894Sahrens 		/* it's the root dir */
893088f3894Sahrens 		VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
894088f3894Sahrens 		    DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
895088f3894Sahrens 	}
896ea8dc4b6Seschrock 	VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
897fa9e4066Sahrens 	dmu_buf_will_dirty(dbuf, tx);
898cde58dbcSMatthew Ahrens 	ddphys = dbuf->db_data;
899fa9e4066Sahrens 
900cde58dbcSMatthew Ahrens 	ddphys->dd_creation_time = gethrestime_sec();
901a2afb611SJerry Jelinek 	if (pds) {
902cde58dbcSMatthew Ahrens 		ddphys->dd_parent_obj = pds->dd_object;
903a2afb611SJerry Jelinek 
904a2afb611SJerry Jelinek 		/* update the filesystem counts */
905a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
906a2afb611SJerry Jelinek 	}
907cde58dbcSMatthew Ahrens 	ddphys->dd_props_zapobj = zap_create(mos,
908fa9e4066Sahrens 	    DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
909cde58dbcSMatthew Ahrens 	ddphys->dd_child_dir_zapobj = zap_create(mos,
91087e5029aSahrens 	    DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
91174e7dc98SMatthew Ahrens 	if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
912cde58dbcSMatthew Ahrens 		ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
913ea8dc4b6Seschrock 	dmu_buf_rele(dbuf, FTAG);
914fa9e4066Sahrens 
9151d452cf5Sahrens 	return (ddobj);
916fa9e4066Sahrens }
917fa9e4066Sahrens 
918088f3894Sahrens boolean_t
919088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
920fa9e4066Sahrens {
921c1379625SJustin T. Gibbs 	return (dsl_dir_phys(dd)->dd_origin_obj &&
922088f3894Sahrens 	    (dd->dd_pool->dp_origin_snap == NULL ||
923c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_origin_obj !=
924088f3894Sahrens 	    dd->dd_pool->dp_origin_snap->ds_object));
925fa9e4066Sahrens }
926fa9e4066Sahrens 
927fa9e4066Sahrens void
928a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
929fa9e4066Sahrens {
930fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
93174e7dc98SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
932c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_bytes);
933c1379625SJustin T. Gibbs 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
934c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_quota);
935a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
936c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_reserved);
937a2eea2e1Sahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
938c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
939c1379625SJustin T. Gibbs 	    (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
940c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes));
94177372cb0SMatthew Ahrens 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
942c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes);
943c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
94474e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
945c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
94674e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
947c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
94874e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
949c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
95074e7dc98SMatthew Ahrens 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
951c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
952c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
95374e7dc98SMatthew Ahrens 	}
954fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
955fa9e4066Sahrens 
956a2afb611SJerry Jelinek 	if (dsl_dir_is_zapified(dd)) {
957a2afb611SJerry Jelinek 		uint64_t count;
958a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
959a2afb611SJerry Jelinek 
960a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
961a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
962a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
963a2afb611SJerry Jelinek 			    ZFS_PROP_FILESYSTEM_COUNT, count);
964a2afb611SJerry Jelinek 		}
965a2afb611SJerry Jelinek 		if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
966a2afb611SJerry Jelinek 		    sizeof (count), 1, &count) == 0) {
967a2afb611SJerry Jelinek 			dsl_prop_nvlist_add_uint64(nv,
968a2afb611SJerry Jelinek 			    ZFS_PROP_SNAPSHOT_COUNT, count);
969a2afb611SJerry Jelinek 		}
970a2afb611SJerry Jelinek 	}
971a2afb611SJerry Jelinek 
972088f3894Sahrens 	if (dsl_dir_is_clone(dd)) {
973fa9e4066Sahrens 		dsl_dataset_t *ds;
974*9adfa60dSMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
975fa9e4066Sahrens 
9763b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
977c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
978a2eea2e1Sahrens 		dsl_dataset_name(ds, buf);
979745cd3c5Smaybee 		dsl_dataset_rele(ds, FTAG);
980a2eea2e1Sahrens 		dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
981fa9e4066Sahrens 	}
982fa9e4066Sahrens }
983fa9e4066Sahrens 
984fa9e4066Sahrens void
985fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
986fa9e4066Sahrens {
987fa9e4066Sahrens 	dsl_pool_t *dp = dd->dd_pool;
988fa9e4066Sahrens 
989c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd));
990fa9e4066Sahrens 
9913b2aab18SMatthew Ahrens 	if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
992fa9e4066Sahrens 		/* up the hold count until we can be written out */
993fa9e4066Sahrens 		dmu_buf_add_ref(dd->dd_dbuf, dd);
994fa9e4066Sahrens 	}
995fa9e4066Sahrens }
996fa9e4066Sahrens 
997fa9e4066Sahrens static int64_t
998fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
999fa9e4066Sahrens {
1000c1379625SJustin T. Gibbs 	uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1001c1379625SJustin T. Gibbs 	uint64_t new_accounted =
1002c1379625SJustin T. Gibbs 	    MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1003fa9e4066Sahrens 	return (new_accounted - old_accounted);
1004fa9e4066Sahrens }
1005fa9e4066Sahrens 
1006fa9e4066Sahrens void
1007fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1008fa9e4066Sahrens {
1009fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
1010fa9e4066Sahrens 
1011fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1012fb09f5aaSMadhav Suresh 	ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1013fa9e4066Sahrens 	dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1014fa9e4066Sahrens 	    dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1015fa9e4066Sahrens 	dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1016fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1017fa9e4066Sahrens 
1018fa9e4066Sahrens 	/* release the hold from dsl_dir_dirty */
1019ea8dc4b6Seschrock 	dmu_buf_rele(dd->dd_dbuf, dd);
1020fa9e4066Sahrens }
1021fa9e4066Sahrens 
1022fa9e4066Sahrens static uint64_t
1023a9799022Sck dsl_dir_space_towrite(dsl_dir_t *dd)
1024fa9e4066Sahrens {
1025a9799022Sck 	uint64_t space = 0;
1026fa9e4066Sahrens 	int i;
1027fa9e4066Sahrens 
1028fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&dd->dd_lock));
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++) {
1031fa9e4066Sahrens 		space += dd->dd_space_towrite[i&TXG_MASK];
1032fa9e4066Sahrens 		ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1033fa9e4066Sahrens 	}
1034fa9e4066Sahrens 	return (space);
1035fa9e4066Sahrens }
1036fa9e4066Sahrens 
1037fa9e4066Sahrens /*
1038fa9e4066Sahrens  * How much space would dd have available if ancestor had delta applied
1039fa9e4066Sahrens  * to it?  If ondiskonly is set, we're only interested in what's
1040fa9e4066Sahrens  * on-disk, not estimated pending changes.
1041fa9e4066Sahrens  */
1042a2eea2e1Sahrens uint64_t
1043fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd,
1044fa9e4066Sahrens     dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1045fa9e4066Sahrens {
1046fa9e4066Sahrens 	uint64_t parentspace, myspace, quota, used;
1047fa9e4066Sahrens 
1048fa9e4066Sahrens 	/*
1049fa9e4066Sahrens 	 * If there are no restrictions otherwise, assume we have
1050fa9e4066Sahrens 	 * unlimited space available.
1051fa9e4066Sahrens 	 */
1052fa9e4066Sahrens 	quota = UINT64_MAX;
1053fa9e4066Sahrens 	parentspace = UINT64_MAX;
1054fa9e4066Sahrens 
1055fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1056fa9e4066Sahrens 		parentspace = dsl_dir_space_available(dd->dd_parent,
1057fa9e4066Sahrens 		    ancestor, delta, ondiskonly);
1058fa9e4066Sahrens 	}
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1061c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_quota != 0)
1062c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1063c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1064a9799022Sck 	if (!ondiskonly)
1065a9799022Sck 		used += dsl_dir_space_towrite(dd);
1066fa9e4066Sahrens 
1067fa9e4066Sahrens 	if (dd->dd_parent == NULL) {
106899653d4eSeschrock 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1069fa9e4066Sahrens 		quota = MIN(quota, poolsize);
1070fa9e4066Sahrens 	}
1071fa9e4066Sahrens 
1072c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1073fa9e4066Sahrens 		/*
1074fa9e4066Sahrens 		 * We have some space reserved, in addition to what our
1075fa9e4066Sahrens 		 * parent gave us.
1076fa9e4066Sahrens 		 */
1077c1379625SJustin T. Gibbs 		parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1078fa9e4066Sahrens 	}
1079fa9e4066Sahrens 
108074e7dc98SMatthew Ahrens 	if (dd == ancestor) {
108174e7dc98SMatthew Ahrens 		ASSERT(delta <= 0);
108274e7dc98SMatthew Ahrens 		ASSERT(used >= -delta);
108374e7dc98SMatthew Ahrens 		used += delta;
108474e7dc98SMatthew Ahrens 		if (parentspace != UINT64_MAX)
108574e7dc98SMatthew Ahrens 			parentspace -= delta;
108674e7dc98SMatthew Ahrens 	}
108774e7dc98SMatthew Ahrens 
1088fa9e4066Sahrens 	if (used > quota) {
1089fa9e4066Sahrens 		/* over quota */
1090fa9e4066Sahrens 		myspace = 0;
1091fa9e4066Sahrens 	} else {
1092fa9e4066Sahrens 		/*
109399653d4eSeschrock 		 * the lesser of the space provided by our parent and
109499653d4eSeschrock 		 * the space left in our quota
1095fa9e4066Sahrens 		 */
1096fa9e4066Sahrens 		myspace = MIN(parentspace, quota - used);
1097fa9e4066Sahrens 	}
1098fa9e4066Sahrens 
1099fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1100fa9e4066Sahrens 
1101fa9e4066Sahrens 	return (myspace);
1102fa9e4066Sahrens }
1103fa9e4066Sahrens 
1104fa9e4066Sahrens struct tempreserve {
1105fa9e4066Sahrens 	list_node_t tr_node;
1106fa9e4066Sahrens 	dsl_dir_t *tr_ds;
1107fa9e4066Sahrens 	uint64_t tr_size;
1108fa9e4066Sahrens };
1109fa9e4066Sahrens 
1110fa9e4066Sahrens static int
1111a9799022Sck dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1112a9799022Sck     boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
11136df6c3bcSck     dmu_tx_t *tx, boolean_t first)
1114fa9e4066Sahrens {
1115fa9e4066Sahrens 	uint64_t txg = tx->tx_txg;
1116a9799022Sck 	uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1117468c413aSTim Haley 	uint64_t deferred = 0;
1118a9799022Sck 	struct tempreserve *tr;
1119468c413aSTim Haley 	int retval = EDQUOT;
1120fa9e4066Sahrens 	int txgidx = txg & TXG_MASK;
1121fa9e4066Sahrens 	int i;
11229082849eSck 	uint64_t ref_rsrv = 0;
1123fa9e4066Sahrens 
1124fa9e4066Sahrens 	ASSERT3U(txg, !=, 0);
1125a9799022Sck 	ASSERT3S(asize, >, 0);
1126fa9e4066Sahrens 
1127fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1128a9799022Sck 
1129fa9e4066Sahrens 	/*
1130fa9e4066Sahrens 	 * Check against the dsl_dir's quota.  We don't add in the delta
1131fa9e4066Sahrens 	 * when checking for over-quota because they get one free hit.
1132fa9e4066Sahrens 	 */
1133a9799022Sck 	est_inflight = dsl_dir_space_towrite(dd);
1134fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++)
1135a9799022Sck 		est_inflight += dd->dd_tempreserved[i];
1136c1379625SJustin T. Gibbs 	used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1137fa9e4066Sahrens 
1138f4d2e9e6Smaybee 	/*
11396df6c3bcSck 	 * On the first iteration, fetch the dataset's used-on-disk and
11406df6c3bcSck 	 * refreservation values. Also, if checkrefquota is set, test if
11416df6c3bcSck 	 * allocating this space would exceed the dataset's refquota.
1142f4d2e9e6Smaybee 	 */
11436df6c3bcSck 	if (first && tx->tx_objset) {
1144c3fdb13aSmaybee 		int error;
1145503ad85cSMatthew Ahrens 		dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
11469082849eSck 
1147a9799022Sck 		error = dsl_dataset_check_quota(ds, checkrefquota,
11489082849eSck 		    asize, est_inflight, &used_on_disk, &ref_rsrv);
1149a9799022Sck 		if (error) {
1150a9799022Sck 			mutex_exit(&dd->dd_lock);
1151a9799022Sck 			return (error);
1152a9799022Sck 		}
1153a9799022Sck 	}
1154a9799022Sck 
1155a9799022Sck 	/*
1156a9799022Sck 	 * If this transaction will result in a net free of space,
1157a9799022Sck 	 * we want to let it through.
1158a9799022Sck 	 */
1159c1379625SJustin T. Gibbs 	if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1160f4d2e9e6Smaybee 		quota = UINT64_MAX;
1161f4d2e9e6Smaybee 	else
1162c1379625SJustin T. Gibbs 		quota = dsl_dir_phys(dd)->dd_quota;
1163fa9e4066Sahrens 
1164fa9e4066Sahrens 	/*
1165468c413aSTim Haley 	 * Adjust the quota against the actual pool size at the root
1166468c413aSTim Haley 	 * minus any outstanding deferred frees.
1167f4d2e9e6Smaybee 	 * To ensure that it's possible to remove files from a full
1168f4d2e9e6Smaybee 	 * pool without inducing transient overcommits, we throttle
1169fa9e4066Sahrens 	 * netfree transactions against a quota that is slightly larger,
1170fa9e4066Sahrens 	 * but still within the pool's allocation slop.  In cases where
1171fa9e4066Sahrens 	 * we're very close to full, this will allow a steady trickle of
1172fa9e4066Sahrens 	 * removes to get through.
1173fa9e4066Sahrens 	 */
11741934e92fSmaybee 	if (dd->dd_parent == NULL) {
1175b24ab676SJeff Bonwick 		spa_t *spa = dd->dd_pool->dp_spa;
1176fa9e4066Sahrens 		uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1177b24ab676SJeff Bonwick 		deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1178468c413aSTim Haley 		if (poolsize - deferred < quota) {
1179468c413aSTim Haley 			quota = poolsize - deferred;
1180468c413aSTim Haley 			retval = ENOSPC;
1181fa9e4066Sahrens 		}
1182fa9e4066Sahrens 	}
1183fa9e4066Sahrens 
1184fa9e4066Sahrens 	/*
1185fa9e4066Sahrens 	 * If they are requesting more space, and our current estimate
1186a9799022Sck 	 * is over quota, they get to try again unless the actual
1187ea8dc4b6Seschrock 	 * on-disk is over quota and there are no pending changes (which
1188ea8dc4b6Seschrock 	 * may free up space for us).
1189fa9e4066Sahrens 	 */
1190468c413aSTim Haley 	if (used_on_disk + est_inflight >= quota) {
1191468c413aSTim Haley 		if (est_inflight > 0 || used_on_disk < quota ||
1192468c413aSTim Haley 		    (retval == ENOSPC && used_on_disk < quota + deferred))
1193468c413aSTim Haley 			retval = ERESTART;
1194a9799022Sck 		dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1195fa9e4066Sahrens 		    "quota=%lluK tr=%lluK err=%d\n",
1196a9799022Sck 		    used_on_disk>>10, est_inflight>>10,
1197468c413aSTim Haley 		    quota>>10, asize>>10, retval);
1198fa9e4066Sahrens 		mutex_exit(&dd->dd_lock);
1199be6fd75aSMatthew Ahrens 		return (SET_ERROR(retval));
1200fa9e4066Sahrens 	}
1201fa9e4066Sahrens 
1202fa9e4066Sahrens 	/* We need to up our estimated delta before dropping dd_lock */
1203fa9e4066Sahrens 	dd->dd_tempreserved[txgidx] += asize;
1204fa9e4066Sahrens 
12059082849eSck 	parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
12069082849eSck 	    asize - ref_rsrv);
1207fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1208fa9e4066Sahrens 
12091ab7f2deSmaybee 	tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1210fa9e4066Sahrens 	tr->tr_ds = dd;
1211fa9e4066Sahrens 	tr->tr_size = asize;
1212fa9e4066Sahrens 	list_insert_tail(tr_list, tr);
1213fa9e4066Sahrens 
1214fa9e4066Sahrens 	/* see if it's OK with our parent */
12151934e92fSmaybee 	if (dd->dd_parent && parent_rsrv) {
1216c1379625SJustin T. Gibbs 		boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
12171934e92fSmaybee 
1218fa9e4066Sahrens 		return (dsl_dir_tempreserve_impl(dd->dd_parent,
12196df6c3bcSck 		    parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1220fa9e4066Sahrens 	} else {
1221fa9e4066Sahrens 		return (0);
1222fa9e4066Sahrens 	}
1223fa9e4066Sahrens }
1224fa9e4066Sahrens 
1225fa9e4066Sahrens /*
1226fa9e4066Sahrens  * Reserve space in this dsl_dir, to be used in this tx's txg.
1227a9799022Sck  * After the space has been dirtied (and dsl_dir_willuse_space()
1228a9799022Sck  * has been called), the reservation should be canceled, using
1229a9799022Sck  * dsl_dir_tempreserve_clear().
1230fa9e4066Sahrens  */
1231fa9e4066Sahrens int
1232a9799022Sck dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1233a9799022Sck     uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1234fa9e4066Sahrens {
12351ab7f2deSmaybee 	int err;
1236fa9e4066Sahrens 	list_t *tr_list;
1237fa9e4066Sahrens 
1238a9799022Sck 	if (asize == 0) {
1239a9799022Sck 		*tr_cookiep = NULL;
1240a9799022Sck 		return (0);
1241a9799022Sck 	}
1242a9799022Sck 
1243fa9e4066Sahrens 	tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1244fa9e4066Sahrens 	list_create(tr_list, sizeof (struct tempreserve),
1245fa9e4066Sahrens 	    offsetof(struct tempreserve, tr_node));
1246a9799022Sck 	ASSERT3S(asize, >, 0);
1247ea8dc4b6Seschrock 	ASSERT3S(fsize, >=, 0);
1248fa9e4066Sahrens 
12491ab7f2deSmaybee 	err = arc_tempreserve_space(lsize, tx->tx_txg);
1250fa9e4066Sahrens 	if (err == 0) {
1251fa9e4066Sahrens 		struct tempreserve *tr;
1252fa9e4066Sahrens 
12531ab7f2deSmaybee 		tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
12541ab7f2deSmaybee 		tr->tr_size = lsize;
12551ab7f2deSmaybee 		list_insert_tail(tr_list, tr);
12561ab7f2deSmaybee 	} else {
12571ab7f2deSmaybee 		if (err == EAGAIN) {
125869962b56SMatthew Ahrens 			/*
125969962b56SMatthew Ahrens 			 * If arc_memory_throttle() detected that pageout
126069962b56SMatthew Ahrens 			 * is running and we are low on memory, we delay new
126169962b56SMatthew Ahrens 			 * non-pageout transactions to give pageout an
126269962b56SMatthew Ahrens 			 * advantage.
126369962b56SMatthew Ahrens 			 *
126469962b56SMatthew Ahrens 			 * It is unfortunate to be delaying while the caller's
126569962b56SMatthew Ahrens 			 * locks are held.
126669962b56SMatthew Ahrens 			 */
12670689f76cSAdam Leventhal 			txg_delay(dd->dd_pool, tx->tx_txg,
12680689f76cSAdam Leventhal 			    MSEC2NSEC(10), MSEC2NSEC(10));
1269be6fd75aSMatthew Ahrens 			err = SET_ERROR(ERESTART);
1270fa9e4066Sahrens 		}
12711ab7f2deSmaybee 	}
12721ab7f2deSmaybee 
12731ab7f2deSmaybee 	if (err == 0) {
12741ab7f2deSmaybee 		err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
12756df6c3bcSck 		    FALSE, asize > usize, tr_list, tx, TRUE);
1276fa9e4066Sahrens 	}
1277fa9e4066Sahrens 
12783b2aab18SMatthew Ahrens 	if (err != 0)
1279fa9e4066Sahrens 		dsl_dir_tempreserve_clear(tr_list, tx);
1280fa9e4066Sahrens 	else
1281fa9e4066Sahrens 		*tr_cookiep = tr_list;
12821ab7f2deSmaybee 
1283fa9e4066Sahrens 	return (err);
1284fa9e4066Sahrens }
1285fa9e4066Sahrens 
1286fa9e4066Sahrens /*
1287fa9e4066Sahrens  * Clear a temporary reservation that we previously made with
1288fa9e4066Sahrens  * dsl_dir_tempreserve_space().
1289fa9e4066Sahrens  */
1290fa9e4066Sahrens void
1291fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1292fa9e4066Sahrens {
1293fa9e4066Sahrens 	int txgidx = tx->tx_txg & TXG_MASK;
1294fa9e4066Sahrens 	list_t *tr_list = tr_cookie;
1295fa9e4066Sahrens 	struct tempreserve *tr;
1296fa9e4066Sahrens 
1297fa9e4066Sahrens 	ASSERT3U(tx->tx_txg, !=, 0);
1298fa9e4066Sahrens 
1299a9799022Sck 	if (tr_cookie == NULL)
1300a9799022Sck 		return;
1301a9799022Sck 
130269962b56SMatthew Ahrens 	while ((tr = list_head(tr_list)) != NULL) {
130369962b56SMatthew Ahrens 		if (tr->tr_ds) {
1304fa9e4066Sahrens 			mutex_enter(&tr->tr_ds->dd_lock);
1305fa9e4066Sahrens 			ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1306fa9e4066Sahrens 			    tr->tr_size);
1307fa9e4066Sahrens 			tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1308fa9e4066Sahrens 			mutex_exit(&tr->tr_ds->dd_lock);
13091ab7f2deSmaybee 		} else {
13101ab7f2deSmaybee 			arc_tempreserve_clear(tr->tr_size);
1311fa9e4066Sahrens 		}
1312fa9e4066Sahrens 		list_remove(tr_list, tr);
1313fa9e4066Sahrens 		kmem_free(tr, sizeof (struct tempreserve));
1314fa9e4066Sahrens 	}
1315fa9e4066Sahrens 
1316fa9e4066Sahrens 	kmem_free(tr_list, sizeof (list_t));
1317fa9e4066Sahrens }
1318fa9e4066Sahrens 
131969962b56SMatthew Ahrens /*
132069962b56SMatthew Ahrens  * This should be called from open context when we think we're going to write
132169962b56SMatthew Ahrens  * or free space, for example when dirtying data. Be conservative; it's okay
132269962b56SMatthew Ahrens  * to write less space or free more, but we don't want to write more or free
132369962b56SMatthew Ahrens  * less than the amount specified.
132469962b56SMatthew Ahrens  */
132569962b56SMatthew Ahrens void
132669962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1327fa9e4066Sahrens {
1328fa9e4066Sahrens 	int64_t parent_space;
1329fa9e4066Sahrens 	uint64_t est_used;
1330fa9e4066Sahrens 
1331fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1332fa9e4066Sahrens 	if (space > 0)
1333fa9e4066Sahrens 		dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1334fa9e4066Sahrens 
1335c1379625SJustin T. Gibbs 	est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1336fa9e4066Sahrens 	parent_space = parent_delta(dd, est_used, space);
1337fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1338fa9e4066Sahrens 
1339fa9e4066Sahrens 	/* Make sure that we clean up dd_space_to* */
1340fa9e4066Sahrens 	dsl_dir_dirty(dd, tx);
1341fa9e4066Sahrens 
1342fa9e4066Sahrens 	/* XXX this is potentially expensive and unnecessary... */
1343fa9e4066Sahrens 	if (parent_space && dd->dd_parent)
134469962b56SMatthew Ahrens 		dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1345fa9e4066Sahrens }
1346fa9e4066Sahrens 
1347fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */
1348fa9e4066Sahrens void
134974e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1350fa9e4066Sahrens     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1351fa9e4066Sahrens {
1352fa9e4066Sahrens 	int64_t accounted_delta;
1353b62969f8SMatthew Ahrens 
1354b62969f8SMatthew Ahrens 	/*
1355b62969f8SMatthew Ahrens 	 * dsl_dataset_set_refreservation_sync_impl() calls this with
1356b62969f8SMatthew Ahrens 	 * dd_lock held, so that it can atomically update
1357b62969f8SMatthew Ahrens 	 * ds->ds_reserved and the dsl_dir accounting, so that
1358b62969f8SMatthew Ahrens 	 * dsl_dataset_check_quota() can see dataset and dir accounting
1359b62969f8SMatthew Ahrens 	 * consistently.
1360b62969f8SMatthew Ahrens 	 */
136102c8f3f0SMatthew Ahrens 	boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1362fa9e4066Sahrens 
1363fa9e4066Sahrens 	ASSERT(dmu_tx_is_syncing(tx));
136474e7dc98SMatthew Ahrens 	ASSERT(type < DD_USED_NUM);
1365fa9e4066Sahrens 
1366b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1367b62969f8SMatthew Ahrens 
136802c8f3f0SMatthew Ahrens 	if (needlock)
136902c8f3f0SMatthew Ahrens 		mutex_enter(&dd->dd_lock);
1370c1379625SJustin T. Gibbs 	accounted_delta =
1371c1379625SJustin T. Gibbs 	    parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1372c1379625SJustin T. Gibbs 	ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1373fa9e4066Sahrens 	ASSERT(compressed >= 0 ||
1374c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1375fa9e4066Sahrens 	ASSERT(uncompressed >= 0 ||
1376c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1377c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_bytes += used;
1378c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1379c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
138074e7dc98SMatthew Ahrens 
1381c1379625SJustin T. Gibbs 	if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
138274e7dc98SMatthew Ahrens 		ASSERT(used > 0 ||
1383c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1384c1379625SJustin T. Gibbs 		dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
138574e7dc98SMatthew Ahrens #ifdef DEBUG
138674e7dc98SMatthew Ahrens 		dd_used_t t;
138774e7dc98SMatthew Ahrens 		uint64_t u = 0;
138874e7dc98SMatthew Ahrens 		for (t = 0; t < DD_USED_NUM; t++)
1389c1379625SJustin T. Gibbs 			u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1390c1379625SJustin T. Gibbs 		ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
139174e7dc98SMatthew Ahrens #endif
139274e7dc98SMatthew Ahrens 	}
139302c8f3f0SMatthew Ahrens 	if (needlock)
139402c8f3f0SMatthew Ahrens 		mutex_exit(&dd->dd_lock);
1395fa9e4066Sahrens 
1396fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
139774e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1398fa9e4066Sahrens 		    accounted_delta, compressed, uncompressed, tx);
139974e7dc98SMatthew Ahrens 		dsl_dir_transfer_space(dd->dd_parent,
140074e7dc98SMatthew Ahrens 		    used - accounted_delta,
140174e7dc98SMatthew Ahrens 		    DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1402fa9e4066Sahrens 	}
1403fa9e4066Sahrens }
1404fa9e4066Sahrens 
140574e7dc98SMatthew Ahrens void
140674e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
140774e7dc98SMatthew Ahrens     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
140874e7dc98SMatthew Ahrens {
140974e7dc98SMatthew Ahrens 	ASSERT(dmu_tx_is_syncing(tx));
141074e7dc98SMatthew Ahrens 	ASSERT(oldtype < DD_USED_NUM);
141174e7dc98SMatthew Ahrens 	ASSERT(newtype < DD_USED_NUM);
141274e7dc98SMatthew Ahrens 
1413c1379625SJustin T. Gibbs 	if (delta == 0 ||
1414c1379625SJustin T. Gibbs 	    !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
141574e7dc98SMatthew Ahrens 		return;
141674e7dc98SMatthew Ahrens 
1417b62969f8SMatthew Ahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1418b62969f8SMatthew Ahrens 	mutex_enter(&dd->dd_lock);
141974e7dc98SMatthew Ahrens 	ASSERT(delta > 0 ?
1420c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1421c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1422c1379625SJustin T. Gibbs 	ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1423c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1424c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1425b62969f8SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
142674e7dc98SMatthew Ahrens }
142774e7dc98SMatthew Ahrens 
14283b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg {
14293b2aab18SMatthew Ahrens 	const char *ddsqra_name;
14303b2aab18SMatthew Ahrens 	zprop_source_t ddsqra_source;
14313b2aab18SMatthew Ahrens 	uint64_t ddsqra_value;
14323b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t;
14333b2aab18SMatthew Ahrens 
1434fa9e4066Sahrens static int
14353b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1436fa9e4066Sahrens {
14373b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14383b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14393b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14403b2aab18SMatthew Ahrens 	int error;
14413b2aab18SMatthew Ahrens 	uint64_t towrite, newval;
1442fa9e4066Sahrens 
14433b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
14443b2aab18SMatthew Ahrens 	if (error != 0)
14453b2aab18SMatthew Ahrens 		return (error);
14463b2aab18SMatthew Ahrens 
14473b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir, "quota",
14483b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
14493b2aab18SMatthew Ahrens 	if (error != 0) {
14503b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14513b2aab18SMatthew Ahrens 		return (error);
14523b2aab18SMatthew Ahrens 	}
145392241e0bSTom Erickson 
14543b2aab18SMatthew Ahrens 	if (newval == 0) {
14553b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14561d452cf5Sahrens 		return (0);
14573b2aab18SMatthew Ahrens 	}
1458fa9e4066Sahrens 
14593b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
14601d452cf5Sahrens 	/*
14611d452cf5Sahrens 	 * If we are doing the preliminary check in open context, and
14621d452cf5Sahrens 	 * there are pending changes, then don't fail it, since the
1463a9799022Sck 	 * pending changes could under-estimate the amount of space to be
14641d452cf5Sahrens 	 * freed up.
14651d452cf5Sahrens 	 */
14663b2aab18SMatthew Ahrens 	towrite = dsl_dir_space_towrite(ds->ds_dir);
14671d452cf5Sahrens 	if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1468c1379625SJustin T. Gibbs 	    (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1469c1379625SJustin T. Gibbs 	    newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1470be6fd75aSMatthew Ahrens 		error = SET_ERROR(ENOSPC);
1471fa9e4066Sahrens 	}
14723b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
14733b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
14743b2aab18SMatthew Ahrens 	return (error);
1475fa9e4066Sahrens }
1476fa9e4066Sahrens 
14771d452cf5Sahrens static void
14783b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
14791d452cf5Sahrens {
14803b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
14813b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14823b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
14833b2aab18SMatthew Ahrens 	uint64_t newval;
148492241e0bSTom Erickson 
14853b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
14861d452cf5Sahrens 
1487013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1488013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1489013023d4SMartin Matuska 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1490013023d4SMartin Matuska 		    &ddsqra->ddsqra_value, tx);
14911d452cf5Sahrens 
1492013023d4SMartin Matuska 		VERIFY0(dsl_prop_get_int_ds(ds,
1493013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1494013023d4SMartin Matuska 	} else {
1495013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1496013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1497013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1498013023d4SMartin Matuska 	}
14993b2aab18SMatthew Ahrens 
15003b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
15013b2aab18SMatthew Ahrens 	mutex_enter(&ds->ds_dir->dd_lock);
1502c1379625SJustin T. Gibbs 	dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
15033b2aab18SMatthew Ahrens 	mutex_exit(&ds->ds_dir->dd_lock);
15043b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15051d452cf5Sahrens }
15061d452cf5Sahrens 
1507fa9e4066Sahrens int
150892241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1509fa9e4066Sahrens {
15103b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
1511fa9e4066Sahrens 
15123b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
15133b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
15143b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = quota;
151592241e0bSTom Erickson 
15163b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
15177d46dc6cSMatthew Ahrens 	    dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1518fa9e4066Sahrens }
1519fa9e4066Sahrens 
1520a9799022Sck int
15213b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1522fa9e4066Sahrens {
15233b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
15243b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15253b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
15263b2aab18SMatthew Ahrens 	dsl_dir_t *dd;
15273b2aab18SMatthew Ahrens 	uint64_t newval, used, avail;
15283b2aab18SMatthew Ahrens 	int error;
152992241e0bSTom Erickson 
15303b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
15313b2aab18SMatthew Ahrens 	if (error != 0)
15323b2aab18SMatthew Ahrens 		return (error);
15333b2aab18SMatthew Ahrens 	dd = ds->ds_dir;
1534fa9e4066Sahrens 
15351d452cf5Sahrens 	/*
15361d452cf5Sahrens 	 * If we are doing the preliminary check in open context, the
15371d452cf5Sahrens 	 * space estimates may be inaccurate.
15381d452cf5Sahrens 	 */
15393b2aab18SMatthew Ahrens 	if (!dmu_tx_is_syncing(tx)) {
15403b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15411d452cf5Sahrens 		return (0);
15423b2aab18SMatthew Ahrens 	}
15433b2aab18SMatthew Ahrens 
15443b2aab18SMatthew Ahrens 	error = dsl_prop_predict(ds->ds_dir,
15453b2aab18SMatthew Ahrens 	    zfs_prop_to_name(ZFS_PROP_RESERVATION),
15463b2aab18SMatthew Ahrens 	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
15473b2aab18SMatthew Ahrens 	if (error != 0) {
15483b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15493b2aab18SMatthew Ahrens 		return (error);
15503b2aab18SMatthew Ahrens 	}
15511d452cf5Sahrens 
1552fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1553c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1554fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1555fa9e4066Sahrens 
1556fa9e4066Sahrens 	if (dd->dd_parent) {
1557fa9e4066Sahrens 		avail = dsl_dir_space_available(dd->dd_parent,
1558fa9e4066Sahrens 		    NULL, 0, FALSE);
1559fa9e4066Sahrens 	} else {
1560fa9e4066Sahrens 		avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1561fa9e4066Sahrens 	}
1562fa9e4066Sahrens 
1563c1379625SJustin T. Gibbs 	if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
15643b2aab18SMatthew Ahrens 		uint64_t delta = MAX(used, newval) -
1565c1379625SJustin T. Gibbs 		    MAX(used, dsl_dir_phys(dd)->dd_reserved);
1566379c004dSEric Schrock 
15673b2aab18SMatthew Ahrens 		if (delta > avail ||
1568c1379625SJustin T. Gibbs 		    (dsl_dir_phys(dd)->dd_quota > 0 &&
1569c1379625SJustin T. Gibbs 		    newval > dsl_dir_phys(dd)->dd_quota))
1570be6fd75aSMatthew Ahrens 			error = SET_ERROR(ENOSPC);
1571379c004dSEric Schrock 	}
1572379c004dSEric Schrock 
15733b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15743b2aab18SMatthew Ahrens 	return (error);
15751d452cf5Sahrens }
15761d452cf5Sahrens 
15773b2aab18SMatthew Ahrens void
15784445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
15791d452cf5Sahrens {
15801d452cf5Sahrens 	uint64_t used;
15811d452cf5Sahrens 	int64_t delta;
15821d452cf5Sahrens 
1583a9799022Sck 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1584a9799022Sck 
15851d452cf5Sahrens 	mutex_enter(&dd->dd_lock);
1586c1379625SJustin T. Gibbs 	used = dsl_dir_phys(dd)->dd_used_bytes;
1587c1379625SJustin T. Gibbs 	delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1588c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_reserved = value;
1589fa9e4066Sahrens 
1590fa9e4066Sahrens 	if (dd->dd_parent != NULL) {
1591fa9e4066Sahrens 		/* Roll up this additional usage into our ancestors */
159274e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
159374e7dc98SMatthew Ahrens 		    delta, 0, 0, tx);
1594fa9e4066Sahrens 	}
159502c8f3f0SMatthew Ahrens 	mutex_exit(&dd->dd_lock);
15964445fffbSMatthew Ahrens }
15974445fffbSMatthew Ahrens 
15984445fffbSMatthew Ahrens 
15994445fffbSMatthew Ahrens static void
16003b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
16014445fffbSMatthew Ahrens {
16023b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t *ddsqra = arg;
16033b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16043b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
16053b2aab18SMatthew Ahrens 	uint64_t newval;
16063b2aab18SMatthew Ahrens 
16073b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
16083b2aab18SMatthew Ahrens 
1609013023d4SMartin Matuska 	if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1610013023d4SMartin Matuska 		dsl_prop_set_sync_impl(ds,
1611013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1612013023d4SMartin Matuska 		    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1613013023d4SMartin Matuska 		    &ddsqra->ddsqra_value, tx);
16144445fffbSMatthew Ahrens 
1615013023d4SMartin Matuska 		VERIFY0(dsl_prop_get_int_ds(ds,
1616013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1617013023d4SMartin Matuska 	} else {
1618013023d4SMartin Matuska 		newval = ddsqra->ddsqra_value;
1619013023d4SMartin Matuska 		spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1620013023d4SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
1621013023d4SMartin Matuska 		    (longlong_t)newval);
1622013023d4SMartin Matuska 	}
1623ecd6cf80Smarks 
16243b2aab18SMatthew Ahrens 	dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
16253b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1626fa9e4066Sahrens }
1627fa9e4066Sahrens 
1628fa9e4066Sahrens int
162992241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
163092241e0bSTom Erickson     uint64_t reservation)
1631fa9e4066Sahrens {
16323b2aab18SMatthew Ahrens 	dsl_dir_set_qr_arg_t ddsqra;
163392241e0bSTom Erickson 
16343b2aab18SMatthew Ahrens 	ddsqra.ddsqra_name = ddname;
16353b2aab18SMatthew Ahrens 	ddsqra.ddsqra_source = source;
16363b2aab18SMatthew Ahrens 	ddsqra.ddsqra_value = reservation;
163792241e0bSTom Erickson 
16383b2aab18SMatthew Ahrens 	return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
16397d46dc6cSMatthew Ahrens 	    dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1640fa9e4066Sahrens }
1641fa9e4066Sahrens 
1642fa9e4066Sahrens static dsl_dir_t *
1643fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1644fa9e4066Sahrens {
1645fa9e4066Sahrens 	for (; ds1; ds1 = ds1->dd_parent) {
1646fa9e4066Sahrens 		dsl_dir_t *dd;
1647fa9e4066Sahrens 		for (dd = ds2; dd; dd = dd->dd_parent) {
1648fa9e4066Sahrens 			if (ds1 == dd)
1649fa9e4066Sahrens 				return (dd);
1650fa9e4066Sahrens 		}
1651fa9e4066Sahrens 	}
1652fa9e4066Sahrens 	return (NULL);
1653fa9e4066Sahrens }
1654fa9e4066Sahrens 
1655fa9e4066Sahrens /*
1656fa9e4066Sahrens  * If delta is applied to dd, how much of that delta would be applied to
1657fa9e4066Sahrens  * ancestor?  Syncing context only.
1658fa9e4066Sahrens  */
1659fa9e4066Sahrens static int64_t
1660fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1661fa9e4066Sahrens {
1662fa9e4066Sahrens 	if (dd == ancestor)
1663fa9e4066Sahrens 		return (delta);
1664fa9e4066Sahrens 
1665fa9e4066Sahrens 	mutex_enter(&dd->dd_lock);
1666c1379625SJustin T. Gibbs 	delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1667fa9e4066Sahrens 	mutex_exit(&dd->dd_lock);
1668fa9e4066Sahrens 	return (would_change(dd->dd_parent, delta, ancestor));
1669fa9e4066Sahrens }
1670fa9e4066Sahrens 
16713b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg {
16723b2aab18SMatthew Ahrens 	const char *ddra_oldname;
16733b2aab18SMatthew Ahrens 	const char *ddra_newname;
1674a2afb611SJerry Jelinek 	cred_t *ddra_cred;
16753b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t;
16761d452cf5Sahrens 
16773b2aab18SMatthew Ahrens /* ARGSUSED */
16781d452cf5Sahrens static int
16793b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1680fa9e4066Sahrens {
16813b2aab18SMatthew Ahrens 	int *deltap = arg;
1682*9adfa60dSMatthew Ahrens 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1683fa9e4066Sahrens 
16843b2aab18SMatthew Ahrens 	dsl_dataset_name(ds, namebuf);
16853b2aab18SMatthew Ahrens 
1686*9adfa60dSMatthew Ahrens 	if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN)
1687be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENAMETOOLONG));
16883b2aab18SMatthew Ahrens 	return (0);
16893b2aab18SMatthew Ahrens }
16903b2aab18SMatthew Ahrens 
16913b2aab18SMatthew Ahrens static int
16923b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
16933b2aab18SMatthew Ahrens {
16943b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
16953b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16963b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
16973b2aab18SMatthew Ahrens 	const char *mynewname;
16983b2aab18SMatthew Ahrens 	int error;
16993b2aab18SMatthew Ahrens 	int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1700fa9e4066Sahrens 
17013b2aab18SMatthew Ahrens 	/* target dir should exist */
17023b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
17033b2aab18SMatthew Ahrens 	if (error != 0)
17043b2aab18SMatthew Ahrens 		return (error);
17053b2aab18SMatthew Ahrens 
17063b2aab18SMatthew Ahrens 	/* new parent should exist */
17073b2aab18SMatthew Ahrens 	error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
17083b2aab18SMatthew Ahrens 	    &newparent, &mynewname);
17093b2aab18SMatthew Ahrens 	if (error != 0) {
17103b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
17113b2aab18SMatthew Ahrens 		return (error);
17123b2aab18SMatthew Ahrens 	}
17133b2aab18SMatthew Ahrens 
17143b2aab18SMatthew Ahrens 	/* can't rename to different pool */
17153b2aab18SMatthew Ahrens 	if (dd->dd_pool != newparent->dd_pool) {
17163b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17173b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1718be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENXIO));
17193b2aab18SMatthew Ahrens 	}
17203b2aab18SMatthew Ahrens 
17213b2aab18SMatthew Ahrens 	/* new name should not already exist */
17223b2aab18SMatthew Ahrens 	if (mynewname == NULL) {
17233b2aab18SMatthew Ahrens 		dsl_dir_rele(newparent, FTAG);
17243b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
1725be6fd75aSMatthew Ahrens 		return (SET_ERROR(EEXIST));
17263b2aab18SMatthew Ahrens 	}
17273b2aab18SMatthew Ahrens 
17283b2aab18SMatthew Ahrens 	/* if the name length is growing, validate child name lengths */
17293b2aab18SMatthew Ahrens 	if (delta > 0) {
17303b2aab18SMatthew Ahrens 		error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
17313b2aab18SMatthew Ahrens 		    &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
17323b2aab18SMatthew Ahrens 		if (error != 0) {
17333b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17343b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
17353b2aab18SMatthew Ahrens 			return (error);
17363b2aab18SMatthew Ahrens 		}
17373b2aab18SMatthew Ahrens 	}
17381d452cf5Sahrens 
1739a2afb611SJerry Jelinek 	if (dmu_tx_is_syncing(tx)) {
1740adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1741a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1742a2afb611SJerry Jelinek 			/*
1743a2afb611SJerry Jelinek 			 * Although this is the check function and we don't
1744a2afb611SJerry Jelinek 			 * normally make on-disk changes in check functions,
1745a2afb611SJerry Jelinek 			 * we need to do that here.
1746a2afb611SJerry Jelinek 			 *
1747a2afb611SJerry Jelinek 			 * Ensure this portion of the tree's counts have been
1748a2afb611SJerry Jelinek 			 * initialized in case the new parent has limits set.
1749a2afb611SJerry Jelinek 			 */
1750a2afb611SJerry Jelinek 			dsl_dir_init_fs_ss_count(dd, tx);
1751a2afb611SJerry Jelinek 		}
1752a2afb611SJerry Jelinek 	}
1753a2afb611SJerry Jelinek 
17543b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
175599653d4eSeschrock 		/* is there enough space? */
175699653d4eSeschrock 		uint64_t myspace =
1757c1379625SJustin T. Gibbs 		    MAX(dsl_dir_phys(dd)->dd_used_bytes,
1758c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_reserved);
1759a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1760a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1761a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1762a2afb611SJerry Jelinek 
1763a2afb611SJerry Jelinek 		if (dsl_dir_is_zapified(dd)) {
1764a2afb611SJerry Jelinek 			int err;
1765a2afb611SJerry Jelinek 
1766a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1767a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1768a2afb611SJerry Jelinek 			    &fs_cnt);
1769adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1770adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1771adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1772a2afb611SJerry Jelinek 				return (err);
1773adf34077SJerry Jelinek 			}
1774a2afb611SJerry Jelinek 
1775a2afb611SJerry Jelinek 			/*
1776a2afb611SJerry Jelinek 			 * have to add 1 for the filesystem itself that we're
1777a2afb611SJerry Jelinek 			 * moving
1778a2afb611SJerry Jelinek 			 */
1779a2afb611SJerry Jelinek 			fs_cnt++;
1780a2afb611SJerry Jelinek 
1781a2afb611SJerry Jelinek 			err = zap_lookup(os, dd->dd_object,
1782a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1783a2afb611SJerry Jelinek 			    &ss_cnt);
1784adf34077SJerry Jelinek 			if (err != ENOENT && err != 0) {
1785adf34077SJerry Jelinek 				dsl_dir_rele(newparent, FTAG);
1786adf34077SJerry Jelinek 				dsl_dir_rele(dd, FTAG);
1787a2afb611SJerry Jelinek 				return (err);
1788adf34077SJerry Jelinek 			}
1789a2afb611SJerry Jelinek 		}
1790fa9e4066Sahrens 
17911d452cf5Sahrens 		/* no rename into our descendant */
17923b2aab18SMatthew Ahrens 		if (closest_common_ancestor(dd, newparent) == dd) {
17933b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
17943b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
1795be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
17963b2aab18SMatthew Ahrens 		}
1797fa9e4066Sahrens 
17983b2aab18SMatthew Ahrens 		error = dsl_dir_transfer_possible(dd->dd_parent,
1799a2afb611SJerry Jelinek 		    newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
18003b2aab18SMatthew Ahrens 		if (error != 0) {
18013b2aab18SMatthew Ahrens 			dsl_dir_rele(newparent, FTAG);
18023b2aab18SMatthew Ahrens 			dsl_dir_rele(dd, FTAG);
18033b2aab18SMatthew Ahrens 			return (error);
18043b2aab18SMatthew Ahrens 		}
18051d452cf5Sahrens 	}
1806fa9e4066Sahrens 
18073b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
18083b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
18091d452cf5Sahrens 	return (0);
18101d452cf5Sahrens }
18111d452cf5Sahrens 
18121d452cf5Sahrens static void
18133b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
18141d452cf5Sahrens {
18153b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t *ddra = arg;
18163b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
18173b2aab18SMatthew Ahrens 	dsl_dir_t *dd, *newparent;
18183b2aab18SMatthew Ahrens 	const char *mynewname;
18193b2aab18SMatthew Ahrens 	int error;
18201d452cf5Sahrens 	objset_t *mos = dp->dp_meta_objset;
18211d452cf5Sahrens 
18223b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
18233b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
18243b2aab18SMatthew Ahrens 	    &mynewname));
18251d452cf5Sahrens 
18264445fffbSMatthew Ahrens 	/* Log this before we change the name. */
18274445fffbSMatthew Ahrens 	spa_history_log_internal_dd(dd, "rename", tx,
18283b2aab18SMatthew Ahrens 	    "-> %s", ddra->ddra_newname);
18294445fffbSMatthew Ahrens 
18303b2aab18SMatthew Ahrens 	if (newparent != dd->dd_parent) {
1831a2afb611SJerry Jelinek 		objset_t *os = dd->dd_pool->dp_meta_objset;
1832a2afb611SJerry Jelinek 		uint64_t fs_cnt = 0;
1833a2afb611SJerry Jelinek 		uint64_t ss_cnt = 0;
1834a2afb611SJerry Jelinek 
1835a2afb611SJerry Jelinek 		/*
1836a2afb611SJerry Jelinek 		 * We already made sure the dd counts were initialized in the
1837a2afb611SJerry Jelinek 		 * check function.
1838a2afb611SJerry Jelinek 		 */
1839adf34077SJerry Jelinek 		if (spa_feature_is_active(dp->dp_spa,
1840a2afb611SJerry Jelinek 		    SPA_FEATURE_FS_SS_LIMIT)) {
1841a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1842a2afb611SJerry Jelinek 			    DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1843a2afb611SJerry Jelinek 			    &fs_cnt));
1844a2afb611SJerry Jelinek 			/* add 1 for the filesystem itself that we're moving */
1845a2afb611SJerry Jelinek 			fs_cnt++;
1846a2afb611SJerry Jelinek 
1847a2afb611SJerry Jelinek 			VERIFY0(zap_lookup(os, dd->dd_object,
1848a2afb611SJerry Jelinek 			    DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1849a2afb611SJerry Jelinek 			    &ss_cnt));
1850a2afb611SJerry Jelinek 		}
1851a2afb611SJerry Jelinek 
1852a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1853a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1854a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, fs_cnt,
1855a2afb611SJerry Jelinek 		    DD_FIELD_FILESYSTEM_COUNT, tx);
1856a2afb611SJerry Jelinek 
1857a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1858a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1859a2afb611SJerry Jelinek 		dsl_fs_ss_count_adjust(newparent, ss_cnt,
1860a2afb611SJerry Jelinek 		    DD_FIELD_SNAPSHOT_COUNT, tx);
1861a2afb611SJerry Jelinek 
186274e7dc98SMatthew Ahrens 		dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1863c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_used_bytes,
1864c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_compressed_bytes,
1865c1379625SJustin T. Gibbs 		    -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
18663b2aab18SMatthew Ahrens 		dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1867c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes,
1868c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_compressed_bytes,
1869c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
187074e7dc98SMatthew Ahrens 
1871c1379625SJustin T. Gibbs 		if (dsl_dir_phys(dd)->dd_reserved >
1872c1379625SJustin T. Gibbs 		    dsl_dir_phys(dd)->dd_used_bytes) {
1873c1379625SJustin T. Gibbs 			uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1874c1379625SJustin T. Gibbs 			    dsl_dir_phys(dd)->dd_used_bytes;
187574e7dc98SMatthew Ahrens 
187674e7dc98SMatthew Ahrens 			dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
187774e7dc98SMatthew Ahrens 			    -unused_rsrv, 0, 0, tx);
18783b2aab18SMatthew Ahrens 			dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
187974e7dc98SMatthew Ahrens 			    unused_rsrv, 0, 0, tx);
188074e7dc98SMatthew Ahrens 		}
1881fa9e4066Sahrens 	}
1882fa9e4066Sahrens 
1883fa9e4066Sahrens 	dmu_buf_will_dirty(dd->dd_dbuf, tx);
1884fa9e4066Sahrens 
1885fa9e4066Sahrens 	/* remove from old parent zapobj */
1886c1379625SJustin T. Gibbs 	error = zap_remove(mos,
1887c1379625SJustin T. Gibbs 	    dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1888fa9e4066Sahrens 	    dd->dd_myname, tx);
18893b2aab18SMatthew Ahrens 	ASSERT0(error);
1890fa9e4066Sahrens 
18913b2aab18SMatthew Ahrens 	(void) strcpy(dd->dd_myname, mynewname);
18923b2aab18SMatthew Ahrens 	dsl_dir_rele(dd->dd_parent, dd);
1893c1379625SJustin T. Gibbs 	dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
18943b2aab18SMatthew Ahrens 	VERIFY0(dsl_dir_hold_obj(dp,
18953b2aab18SMatthew Ahrens 	    newparent->dd_object, NULL, dd, &dd->dd_parent));
1896fa9e4066Sahrens 
1897fa9e4066Sahrens 	/* add to new parent zapobj */
1898c1379625SJustin T. Gibbs 	VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
18993b2aab18SMatthew Ahrens 	    dd->dd_myname, 8, 1, &dd->dd_object, tx));
19003b2aab18SMatthew Ahrens 
19013b2aab18SMatthew Ahrens 	dsl_prop_notify_all(dd);
1902ecd6cf80Smarks 
19033b2aab18SMatthew Ahrens 	dsl_dir_rele(newparent, FTAG);
19043b2aab18SMatthew Ahrens 	dsl_dir_rele(dd, FTAG);
19051d452cf5Sahrens }
1906fa9e4066Sahrens 
19071d452cf5Sahrens int
19083b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname)
19091d452cf5Sahrens {
19103b2aab18SMatthew Ahrens 	dsl_dir_rename_arg_t ddra;
19111d452cf5Sahrens 
19123b2aab18SMatthew Ahrens 	ddra.ddra_oldname = oldname;
19133b2aab18SMatthew Ahrens 	ddra.ddra_newname = newname;
1914a2afb611SJerry Jelinek 	ddra.ddra_cred = CRED();
19151d452cf5Sahrens 
19163b2aab18SMatthew Ahrens 	return (dsl_sync_task(oldname,
19177d46dc6cSMatthew Ahrens 	    dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
19187d46dc6cSMatthew Ahrens 	    3, ZFS_SPACE_CHECK_RESERVED));
1919fa9e4066Sahrens }
192099653d4eSeschrock 
192199653d4eSeschrock int
1922a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1923a2afb611SJerry Jelinek     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
192499653d4eSeschrock {
192599653d4eSeschrock 	dsl_dir_t *ancestor;
192699653d4eSeschrock 	int64_t adelta;
192799653d4eSeschrock 	uint64_t avail;
1928a2afb611SJerry Jelinek 	int err;
192999653d4eSeschrock 
193099653d4eSeschrock 	ancestor = closest_common_ancestor(sdd, tdd);
193199653d4eSeschrock 	adelta = would_change(sdd, -space, ancestor);
193299653d4eSeschrock 	avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
193399653d4eSeschrock 	if (avail < space)
1934be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOSPC));
193599653d4eSeschrock 
1936a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1937a2afb611SJerry Jelinek 	    ancestor, cr);
1938a2afb611SJerry Jelinek 	if (err != 0)
1939a2afb611SJerry Jelinek 		return (err);
1940a2afb611SJerry Jelinek 	err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1941a2afb611SJerry Jelinek 	    ancestor, cr);
1942a2afb611SJerry Jelinek 	if (err != 0)
1943a2afb611SJerry Jelinek 		return (err);
1944a2afb611SJerry Jelinek 
194599653d4eSeschrock 	return (0);
194699653d4eSeschrock }
194771eb0538SChris Kirby 
194871eb0538SChris Kirby timestruc_t
194971eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd)
195071eb0538SChris Kirby {
195171eb0538SChris Kirby 	timestruc_t t;
195271eb0538SChris Kirby 
195371eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
195471eb0538SChris Kirby 	t = dd->dd_snap_cmtime;
195571eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
195671eb0538SChris Kirby 
195771eb0538SChris Kirby 	return (t);
195871eb0538SChris Kirby }
195971eb0538SChris Kirby 
196071eb0538SChris Kirby void
196171eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
196271eb0538SChris Kirby {
196371eb0538SChris Kirby 	timestruc_t t;
196471eb0538SChris Kirby 
196571eb0538SChris Kirby 	gethrestime(&t);
196671eb0538SChris Kirby 	mutex_enter(&dd->dd_lock);
196771eb0538SChris Kirby 	dd->dd_snap_cmtime = t;
196871eb0538SChris Kirby 	mutex_exit(&dd->dd_lock);
196971eb0538SChris Kirby }
19702acef22dSMatthew Ahrens 
19712acef22dSMatthew Ahrens void
19722acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
19732acef22dSMatthew Ahrens {
19742acef22dSMatthew Ahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
19752acef22dSMatthew Ahrens 	dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
19762acef22dSMatthew Ahrens }
1977a2afb611SJerry Jelinek 
1978a2afb611SJerry Jelinek boolean_t
1979a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd)
1980a2afb611SJerry Jelinek {
1981a2afb611SJerry Jelinek 	dmu_object_info_t doi;
1982a2afb611SJerry Jelinek 
1983a2afb611SJerry Jelinek 	dmu_object_info_from_db(dd->dd_dbuf, &doi);
1984a2afb611SJerry Jelinek 	return (doi.doi_type == DMU_OTN_ZAP_METADATA);
1985a2afb611SJerry Jelinek }
1986