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