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.
23e6546372SJosef 'Jeff' Sipek * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
24013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved.
25a2afb611SJerry Jelinek * Copyright (c) 2014 Joyent, Inc. All rights reserved.
26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
2703b1c297SAlexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28fa9e4066Sahrens */
29fa9e4066Sahrens
30fa9e4066Sahrens #include <sys/dmu.h>
31a9799022Sck153898 #include <sys/dmu_objset.h>
32fa9e4066Sahrens #include <sys/dmu_tx.h>
33fa9e4066Sahrens #include <sys/dsl_dataset.h>
34fa9e4066Sahrens #include <sys/dsl_dir.h>
35fa9e4066Sahrens #include <sys/dsl_prop.h>
361d452cf5Sahrens #include <sys/dsl_synctask.h>
37ecd6cf80Smarks #include <sys/dsl_deleg.h>
382acef22dSMatthew Ahrens #include <sys/dmu_impl.h>
39fa9e4066Sahrens #include <sys/spa.h>
40b24ab676SJeff Bonwick #include <sys/metaslab.h>
41fa9e4066Sahrens #include <sys/zap.h>
42fa9e4066Sahrens #include <sys/zio.h>
43fa9e4066Sahrens #include <sys/arc.h>
44ecd6cf80Smarks #include <sys/sunddi.h>
45a2afb611SJerry Jelinek #include <sys/zfeature.h>
46a2afb611SJerry Jelinek #include <sys/policy.h>
47a2afb611SJerry Jelinek #include <sys/zfs_znode.h>
48fa9e4066Sahrens #include "zfs_namecheck.h"
49a2afb611SJerry Jelinek #include "zfs_prop.h"
50a2afb611SJerry Jelinek
51a2afb611SJerry Jelinek /*
52a2afb611SJerry Jelinek * Filesystem and Snapshot Limits
53a2afb611SJerry Jelinek * ------------------------------
54a2afb611SJerry Jelinek *
55a2afb611SJerry Jelinek * These limits are used to restrict the number of filesystems and/or snapshots
56a2afb611SJerry Jelinek * that can be created at a given level in the tree or below. A typical
57a2afb611SJerry Jelinek * use-case is with a delegated dataset where the administrator wants to ensure
58a2afb611SJerry Jelinek * that a user within the zone is not creating too many additional filesystems
59a2afb611SJerry Jelinek * or snapshots, even though they're not exceeding their space quota.
60a2afb611SJerry Jelinek *
61a2afb611SJerry Jelinek * The filesystem and snapshot counts are stored as extensible properties. This
62a2afb611SJerry Jelinek * capability is controlled by a feature flag and must be enabled to be used.
63a2afb611SJerry Jelinek * Once enabled, the feature is not active until the first limit is set. At
64a2afb611SJerry Jelinek * that point, future operations to create/destroy filesystems or snapshots
65a2afb611SJerry Jelinek * will validate and update the counts.
66a2afb611SJerry Jelinek *
67a2afb611SJerry Jelinek * Because the count properties will not exist before the feature is active,
68a2afb611SJerry Jelinek * the counts are updated when a limit is first set on an uninitialized
69a2afb611SJerry Jelinek * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
70a2afb611SJerry Jelinek * all of the nested filesystems/snapshots. Thus, a new leaf node has a
71a2afb611SJerry Jelinek * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
72a2afb611SJerry Jelinek * snapshot count properties on a node indicate uninitialized counts on that
73a2afb611SJerry Jelinek * node.) When first setting a limit on an uninitialized node, the code starts
74a2afb611SJerry Jelinek * at the filesystem with the new limit and descends into all sub-filesystems
75a2afb611SJerry Jelinek * to add the count properties.
76a2afb611SJerry Jelinek *
77a2afb611SJerry Jelinek * In practice this is lightweight since a limit is typically set when the
78a2afb611SJerry Jelinek * filesystem is created and thus has no children. Once valid, changing the
79a2afb611SJerry Jelinek * limit value won't require a re-traversal since the counts are already valid.
80a2afb611SJerry Jelinek * When recursively fixing the counts, if a node with a limit is encountered
81a2afb611SJerry Jelinek * during the descent, the counts are known to be valid and there is no need to
82a2afb611SJerry Jelinek * descend into that filesystem's children. The counts on filesystems above the
83a2afb611SJerry Jelinek * one with the new limit will still be uninitialized, unless a limit is
84a2afb611SJerry Jelinek * eventually set on one of those filesystems. The counts are always recursively
85a2afb611SJerry Jelinek * updated when a limit is set on a dataset, unless there is already a limit.
86a2afb611SJerry Jelinek * When a new limit value is set on a filesystem with an existing limit, it is
87a2afb611SJerry Jelinek * possible for the new limit to be less than the current count at that level
88a2afb611SJerry Jelinek * since a user who can change the limit is also allowed to exceed the limit.
89a2afb611SJerry Jelinek *
90a2afb611SJerry Jelinek * Once the feature is active, then whenever a filesystem or snapshot is
91a2afb611SJerry Jelinek * created, the code recurses up the tree, validating the new count against the
92a2afb611SJerry Jelinek * limit at each initialized level. In practice, most levels will not have a
93a2afb611SJerry Jelinek * limit set. If there is a limit at any initialized level up the tree, the
94a2afb611SJerry Jelinek * check must pass or the creation will fail. Likewise, when a filesystem or
95a2afb611SJerry Jelinek * snapshot is destroyed, the counts are recursively adjusted all the way up
96a2afb611SJerry Jelinek * the initizized nodes in the tree. Renaming a filesystem into different point
97a2afb611SJerry Jelinek * in the tree will first validate, then update the counts on each branch up to
98a2afb611SJerry Jelinek * the common ancestor. A receive will also validate the counts and then update
99a2afb611SJerry Jelinek * them.
100a2afb611SJerry Jelinek *
101a2afb611SJerry Jelinek * An exception to the above behavior is that the limit is not enforced if the
102a2afb611SJerry Jelinek * user has permission to modify the limit. This is primarily so that
103a2afb611SJerry Jelinek * recursive snapshots in the global zone always work. We want to prevent a
104a2afb611SJerry Jelinek * denial-of-service in which a lower level delegated dataset could max out its
105a2afb611SJerry Jelinek * limit and thus block recursive snapshots from being taken in the global zone.
106a2afb611SJerry Jelinek * Because of this, it is possible for the snapshot count to be over the limit
107a2afb611SJerry Jelinek * and snapshots taken in the global zone could cause a lower level dataset to
108a2afb611SJerry Jelinek * hit or exceed its limit. The administrator taking the global zone recursive
109a2afb611SJerry Jelinek * snapshot should be aware of this side-effect and behave accordingly.
110a2afb611SJerry Jelinek * For consistency, the filesystem limit is also not enforced if the user can
111a2afb611SJerry Jelinek * modify the limit.
112a2afb611SJerry Jelinek *
113a2afb611SJerry Jelinek * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
114a2afb611SJerry Jelinek * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
115a2afb611SJerry Jelinek * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
116a2afb611SJerry Jelinek * dsl_dir_init_fs_ss_count().
117a2afb611SJerry Jelinek *
118a2afb611SJerry Jelinek * There is a special case when we receive a filesystem that already exists. In
119a2afb611SJerry Jelinek * this case a temporary clone name of %X is created (see dmu_recv_begin). We
120a2afb611SJerry Jelinek * never update the filesystem counts for temporary clones.
121a2afb611SJerry Jelinek *
122a2afb611SJerry Jelinek * Likewise, we do not update the snapshot counts for temporary snapshots,
123a2afb611SJerry Jelinek * such as those created by zfs diff.
124a2afb611SJerry Jelinek */
125fa9e4066Sahrens
126b9f08e63SArne Jansen /*
127b9f08e63SArne Jansen * Tunable to control EDQUOT behaviour. With this set to a value != 0, zfs
128b9f08e63SArne Jansen * doesn't always wait for a dirty txg to complete when an operation can't
129b9f08e63SArne Jansen * get through due to space exhaustion. Instead it fails early in a range
130b9f08e63SArne Jansen * of the tunable around the quota.
131b9f08e63SArne Jansen * This vastly helps to reduce the number of threads waiting for the txg
132b9f08e63SArne Jansen * to commit when a busy filesystem is near quota, especially in combination
133b9f08e63SArne Jansen * with NFS, where each waiter takes up a server thread.
134b9f08e63SArne Jansen */
135b9f08e63SArne Jansen uint64_t early_edquot_threshold = 32 * 1048576; /* tunable */
136b9f08e63SArne Jansen
137*a2f7b05aSArne Jansen /*
138*a2f7b05aSArne Jansen * tunable. when set, zfs allows all tx into the running txg as long as the recorded
139*a2f7b05aSArne Jansen * on-disk quota from the previous is below the quota. On busy filesystems it can be
140*a2f7b05aSArne Jansen * possible to go over quota for several GB. The intention here is to never let a
141*a2f7b05aSArne Jansen * request wait for the next transaction. It either fails or passes immediately. This
142*a2f7b05aSArne Jansen * prevents NFS threads to pile up on busy filesystems near quota.
143*a2f7b05aSArne Jansen * This tunable takes precedence over early_edquot_threshold.
144*a2f7b05aSArne Jansen */
145*a2f7b05aSArne Jansen boolean_t late_edquot = TRUE; /* tunable */
146*a2f7b05aSArne Jansen
147c1379625SJustin T. Gibbs extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
148c1379625SJustin T. Gibbs
149a9799022Sck153898 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
150fa9e4066Sahrens
151fa9e4066Sahrens static void
dsl_dir_evict_async(void * dbu)152e6546372SJosef 'Jeff' Sipek dsl_dir_evict_async(void *dbu)
153fa9e4066Sahrens {
154bc9014e6SJustin Gibbs dsl_dir_t *dd = dbu;
155fa9e4066Sahrens dsl_pool_t *dp = dd->dd_pool;
156fa9e4066Sahrens int t;
157fa9e4066Sahrens
158bc9014e6SJustin Gibbs dd->dd_dbuf = NULL;
159bc9014e6SJustin Gibbs
160fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) {
161fa9e4066Sahrens ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
162fa9e4066Sahrens ASSERT(dd->dd_tempreserved[t] == 0);
163fa9e4066Sahrens ASSERT(dd->dd_space_towrite[t] == 0);
164fa9e4066Sahrens }
165fa9e4066Sahrens
166fa9e4066Sahrens if (dd->dd_parent)
167bc9014e6SJustin Gibbs dsl_dir_async_rele(dd->dd_parent, dd);
168fa9e4066Sahrens
169bc9014e6SJustin Gibbs spa_async_close(dd->dd_pool->dp_spa, dd);
170fa9e4066Sahrens
17103bad06fSJustin Gibbs dsl_prop_fini(dd);
1725ad82045Snd150628 mutex_destroy(&dd->dd_lock);
173fa9e4066Sahrens kmem_free(dd, sizeof (dsl_dir_t));
174fa9e4066Sahrens }
175fa9e4066Sahrens
176ea8dc4b6Seschrock int
dsl_dir_hold_obj(dsl_pool_t * dp,uint64_t ddobj,const char * tail,void * tag,dsl_dir_t ** ddp)1773b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
178ea8dc4b6Seschrock const char *tail, void *tag, dsl_dir_t **ddp)
179fa9e4066Sahrens {
180fa9e4066Sahrens dmu_buf_t *dbuf;
181fa9e4066Sahrens dsl_dir_t *dd;
182ea8dc4b6Seschrock int err;
183fa9e4066Sahrens
1843b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp));
185fa9e4066Sahrens
186ea8dc4b6Seschrock err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
1873b2aab18SMatthew Ahrens if (err != 0)
188ea8dc4b6Seschrock return (err);
189fa9e4066Sahrens dd = dmu_buf_get_user(dbuf);
190fa9e4066Sahrens #ifdef ZFS_DEBUG
191fa9e4066Sahrens {
192fa9e4066Sahrens dmu_object_info_t doi;
193fa9e4066Sahrens dmu_object_info_from_db(dbuf, &doi);
1942acef22dSMatthew Ahrens ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
19574e7dc98SMatthew Ahrens ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
196fa9e4066Sahrens }
197fa9e4066Sahrens #endif
198fa9e4066Sahrens if (dd == NULL) {
199fa9e4066Sahrens dsl_dir_t *winner;
200fa9e4066Sahrens
201fa9e4066Sahrens dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
202fa9e4066Sahrens dd->dd_object = ddobj;
203fa9e4066Sahrens dd->dd_dbuf = dbuf;
204fa9e4066Sahrens dd->dd_pool = dp;
2055ad82045Snd150628 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
20603bad06fSJustin Gibbs dsl_prop_init(dd);
207fa9e4066Sahrens
20871eb0538SChris Kirby dsl_dir_snap_cmtime_update(dd);
20971eb0538SChris Kirby
210c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_parent_obj) {
211c1379625SJustin T. Gibbs err = dsl_dir_hold_obj(dp,
212c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
213c1379625SJustin T. Gibbs &dd->dd_parent);
2143b2aab18SMatthew Ahrens if (err != 0)
21574e7dc98SMatthew Ahrens goto errout;
216fa9e4066Sahrens if (tail) {
217fa9e4066Sahrens #ifdef ZFS_DEBUG
218fa9e4066Sahrens uint64_t foundobj;
219fa9e4066Sahrens
220fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset,
221c1379625SJustin T. Gibbs dsl_dir_phys(dd->dd_parent)->
222c1379625SJustin T. Gibbs dd_child_dir_zapobj, tail,
223c1379625SJustin T. Gibbs sizeof (foundobj), 1, &foundobj);
224ea8dc4b6Seschrock ASSERT(err || foundobj == ddobj);
225fa9e4066Sahrens #endif
226fa9e4066Sahrens (void) strcpy(dd->dd_myname, tail);
227fa9e4066Sahrens } else {
228fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset,
229c1379625SJustin T. Gibbs dsl_dir_phys(dd->dd_parent)->
230c1379625SJustin T. Gibbs dd_child_dir_zapobj,
231e7437265Sahrens ddobj, 0, dd->dd_myname);
232ea8dc4b6Seschrock }
2333b2aab18SMatthew Ahrens if (err != 0)
23474e7dc98SMatthew Ahrens goto errout;
235fa9e4066Sahrens } else {
236fa9e4066Sahrens (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
237fa9e4066Sahrens }
238fa9e4066Sahrens
2393f9d6ad7SLin Ling if (dsl_dir_is_clone(dd)) {
2403f9d6ad7SLin Ling dmu_buf_t *origin_bonus;
2413f9d6ad7SLin Ling dsl_dataset_phys_t *origin_phys;
2423f9d6ad7SLin Ling
2433f9d6ad7SLin Ling /*
2443f9d6ad7SLin Ling * We can't open the origin dataset, because
2453f9d6ad7SLin Ling * that would require opening this dsl_dir.
2463f9d6ad7SLin Ling * Just look at its phys directly instead.
2473f9d6ad7SLin Ling */
2483f9d6ad7SLin Ling err = dmu_bonus_hold(dp->dp_meta_objset,
249c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj, FTAG,
250c1379625SJustin T. Gibbs &origin_bonus);
2513b2aab18SMatthew Ahrens if (err != 0)
2523f9d6ad7SLin Ling goto errout;
2533f9d6ad7SLin Ling origin_phys = origin_bonus->db_data;
2543f9d6ad7SLin Ling dd->dd_origin_txg =
2553f9d6ad7SLin Ling origin_phys->ds_creation_txg;
2563f9d6ad7SLin Ling dmu_buf_rele(origin_bonus, FTAG);
2573f9d6ad7SLin Ling }
2583f9d6ad7SLin Ling
259e6546372SJosef 'Jeff' Sipek dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
260e6546372SJosef 'Jeff' Sipek &dd->dd_dbuf);
261bc9014e6SJustin Gibbs winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
262bc9014e6SJustin Gibbs if (winner != NULL) {
263fa9e4066Sahrens if (dd->dd_parent)
2643b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd);
26503bad06fSJustin Gibbs dsl_prop_fini(dd);
2665ad82045Snd150628 mutex_destroy(&dd->dd_lock);
267fa9e4066Sahrens kmem_free(dd, sizeof (dsl_dir_t));
268fa9e4066Sahrens dd = winner;
269fa9e4066Sahrens } else {
270fa9e4066Sahrens spa_open_ref(dp->dp_spa, dd);
271fa9e4066Sahrens }
272fa9e4066Sahrens }
273fa9e4066Sahrens
274fa9e4066Sahrens /*
275fa9e4066Sahrens * The dsl_dir_t has both open-to-close and instantiate-to-evict
276fa9e4066Sahrens * holds on the spa. We need the open-to-close holds because
277fa9e4066Sahrens * otherwise the spa_refcnt wouldn't change when we open a
278fa9e4066Sahrens * dir which the spa also has open, so we could incorrectly
279fa9e4066Sahrens * think it was OK to unload/export/destroy the pool. We need
280fa9e4066Sahrens * the instantiate-to-evict hold because the dsl_dir_t has a
281fa9e4066Sahrens * pointer to the dd_pool, which has a pointer to the spa_t.
282fa9e4066Sahrens */
283fa9e4066Sahrens spa_open_ref(dp->dp_spa, tag);
284fa9e4066Sahrens ASSERT3P(dd->dd_pool, ==, dp);
285fa9e4066Sahrens ASSERT3U(dd->dd_object, ==, ddobj);
286fa9e4066Sahrens ASSERT3P(dd->dd_dbuf, ==, dbuf);
287ea8dc4b6Seschrock *ddp = dd;
288ea8dc4b6Seschrock return (0);
28974e7dc98SMatthew Ahrens
29074e7dc98SMatthew Ahrens errout:
29174e7dc98SMatthew Ahrens if (dd->dd_parent)
2923b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd);
29303bad06fSJustin Gibbs dsl_prop_fini(dd);
29474e7dc98SMatthew Ahrens mutex_destroy(&dd->dd_lock);
29574e7dc98SMatthew Ahrens kmem_free(dd, sizeof (dsl_dir_t));
29674e7dc98SMatthew Ahrens dmu_buf_rele(dbuf, tag);
29774e7dc98SMatthew Ahrens return (err);
298fa9e4066Sahrens }
299fa9e4066Sahrens
300fa9e4066Sahrens void
dsl_dir_rele(dsl_dir_t * dd,void * tag)3013b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag)
302fa9e4066Sahrens {
303fa9e4066Sahrens dprintf_dd(dd, "%s\n", "");
304fa9e4066Sahrens spa_close(dd->dd_pool->dp_spa, tag);
305ea8dc4b6Seschrock dmu_buf_rele(dd->dd_dbuf, tag);
306fa9e4066Sahrens }
307fa9e4066Sahrens
308bc9014e6SJustin Gibbs /*
309bc9014e6SJustin Gibbs * Remove a reference to the given dsl dir that is being asynchronously
310bc9014e6SJustin Gibbs * released. Async releases occur from a taskq performing eviction of
311bc9014e6SJustin Gibbs * dsl datasets and dirs. This process is identical to a normal release
312bc9014e6SJustin Gibbs * with the exception of using the async API for releasing the reference on
313bc9014e6SJustin Gibbs * the spa.
314bc9014e6SJustin Gibbs */
315bc9014e6SJustin Gibbs void
dsl_dir_async_rele(dsl_dir_t * dd,void * tag)316bc9014e6SJustin Gibbs dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
317bc9014e6SJustin Gibbs {
318bc9014e6SJustin Gibbs dprintf_dd(dd, "%s\n", "");
319bc9014e6SJustin Gibbs spa_async_close(dd->dd_pool->dp_spa, tag);
320bc9014e6SJustin Gibbs dmu_buf_rele(dd->dd_dbuf, tag);
321bc9014e6SJustin Gibbs }
322bc9014e6SJustin Gibbs
32340a5c998SMatthew Ahrens /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
324fa9e4066Sahrens void
dsl_dir_name(dsl_dir_t * dd,char * buf)325fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf)
326fa9e4066Sahrens {
327fa9e4066Sahrens if (dd->dd_parent) {
328fa9e4066Sahrens dsl_dir_name(dd->dd_parent, buf);
32940a5c998SMatthew Ahrens VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
33040a5c998SMatthew Ahrens ZFS_MAX_DATASET_NAME_LEN);
331fa9e4066Sahrens } else {
332fa9e4066Sahrens buf[0] = '\0';
333fa9e4066Sahrens }
334fa9e4066Sahrens if (!MUTEX_HELD(&dd->dd_lock)) {
335fa9e4066Sahrens /*
336fa9e4066Sahrens * recursive mutex so that we can use
337fa9e4066Sahrens * dprintf_dd() with dd_lock held
338fa9e4066Sahrens */
339fa9e4066Sahrens mutex_enter(&dd->dd_lock);
34040a5c998SMatthew Ahrens VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
34140a5c998SMatthew Ahrens <, ZFS_MAX_DATASET_NAME_LEN);
342fa9e4066Sahrens mutex_exit(&dd->dd_lock);
343fa9e4066Sahrens } else {
34440a5c998SMatthew Ahrens VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
34540a5c998SMatthew Ahrens <, ZFS_MAX_DATASET_NAME_LEN);
346fa9e4066Sahrens }
347fa9e4066Sahrens }
348fa9e4066Sahrens
349ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
350b7661cccSmmusante int
dsl_dir_namelen(dsl_dir_t * dd)351b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd)
352b7661cccSmmusante {
353b7661cccSmmusante int result = 0;
354b7661cccSmmusante
355b7661cccSmmusante if (dd->dd_parent) {
356b7661cccSmmusante /* parent's name + 1 for the "/" */
357b7661cccSmmusante result = dsl_dir_namelen(dd->dd_parent) + 1;
358b7661cccSmmusante }
359b7661cccSmmusante
360b7661cccSmmusante if (!MUTEX_HELD(&dd->dd_lock)) {
361b7661cccSmmusante /* see dsl_dir_name */
362b7661cccSmmusante mutex_enter(&dd->dd_lock);
363b7661cccSmmusante result += strlen(dd->dd_myname);
364b7661cccSmmusante mutex_exit(&dd->dd_lock);
365b7661cccSmmusante } else {
366b7661cccSmmusante result += strlen(dd->dd_myname);
367b7661cccSmmusante }
368b7661cccSmmusante
369b7661cccSmmusante return (result);
370b7661cccSmmusante }
371b7661cccSmmusante
372fa9e4066Sahrens static int
getcomponent(const char * path,char * component,const char ** nextp)373fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp)
374fa9e4066Sahrens {
375fa9e4066Sahrens char *p;
3763b2aab18SMatthew Ahrens
377ccba0801SRich Morris if ((path == NULL) || (path[0] == '\0'))
378be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT));
379fa9e4066Sahrens /* This would be a good place to reserve some namespace... */
380fa9e4066Sahrens p = strpbrk(path, "/@");
381fa9e4066Sahrens if (p && (p[1] == '/' || p[1] == '@')) {
382fa9e4066Sahrens /* two separators in a row */
383be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
384fa9e4066Sahrens }
385fa9e4066Sahrens if (p == NULL || p == path) {
386fa9e4066Sahrens /*
387fa9e4066Sahrens * if the first thing is an @ or /, it had better be an
388fa9e4066Sahrens * @ and it had better not have any more ats or slashes,
389fa9e4066Sahrens * and it had better have something after the @.
390fa9e4066Sahrens */
391fa9e4066Sahrens if (p != NULL &&
392fa9e4066Sahrens (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
393be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
39440a5c998SMatthew Ahrens if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
395be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
396fa9e4066Sahrens (void) strcpy(component, path);
397fa9e4066Sahrens p = NULL;
398fa9e4066Sahrens } else if (p[0] == '/') {
39940a5c998SMatthew Ahrens if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
400be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
401fa9e4066Sahrens (void) strncpy(component, path, p - path);
402fa9e4066Sahrens component[p - path] = '\0';
403fa9e4066Sahrens p++;
404fa9e4066Sahrens } else if (p[0] == '@') {
405fa9e4066Sahrens /*
406fa9e4066Sahrens * if the next separator is an @, there better not be
407fa9e4066Sahrens * any more slashes.
408fa9e4066Sahrens */
409fa9e4066Sahrens if (strchr(path, '/'))
410be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
41140a5c998SMatthew Ahrens if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
412be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
413fa9e4066Sahrens (void) strncpy(component, path, p - path);
414fa9e4066Sahrens component[p - path] = '\0';
415fa9e4066Sahrens } else {
4163b2aab18SMatthew Ahrens panic("invalid p=%p", (void *)p);
417fa9e4066Sahrens }
418fa9e4066Sahrens *nextp = p;
419fa9e4066Sahrens return (0);
420fa9e4066Sahrens }
421fa9e4066Sahrens
422fa9e4066Sahrens /*
4233b2aab18SMatthew Ahrens * Return the dsl_dir_t, and possibly the last component which couldn't
4243b2aab18SMatthew Ahrens * be found in *tail. The name must be in the specified dsl_pool_t. This
4253b2aab18SMatthew Ahrens * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
4263b2aab18SMatthew Ahrens * path is bogus, or if tail==NULL and we couldn't parse the whole name.
4273b2aab18SMatthew Ahrens * (*tail)[0] == '@' means that the last component is a snapshot.
428fa9e4066Sahrens */
429ea8dc4b6Seschrock int
dsl_dir_hold(dsl_pool_t * dp,const char * name,void * tag,dsl_dir_t ** ddp,const char ** tailp)4303b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
431ea8dc4b6Seschrock dsl_dir_t **ddp, const char **tailp)
432fa9e4066Sahrens {
43340a5c998SMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN];
4343b2aab18SMatthew Ahrens const char *spaname, *next, *nextnext = NULL;
435fa9e4066Sahrens int err;
436fa9e4066Sahrens dsl_dir_t *dd;
437fa9e4066Sahrens uint64_t ddobj;
438fa9e4066Sahrens
439fa9e4066Sahrens err = getcomponent(name, buf, &next);
4403b2aab18SMatthew Ahrens if (err != 0)
441ea8dc4b6Seschrock return (err);
442fa9e4066Sahrens
4433b2aab18SMatthew Ahrens /* Make sure the name is in the specified pool. */
4443b2aab18SMatthew Ahrens spaname = spa_name(dp->dp_spa);
4453b2aab18SMatthew Ahrens if (strcmp(buf, spaname) != 0)
44603b1c297SAlexander Eremin return (SET_ERROR(EXDEV));
447fa9e4066Sahrens
4483b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp));
449fa9e4066Sahrens
4503b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
4513b2aab18SMatthew Ahrens if (err != 0) {
452ea8dc4b6Seschrock return (err);
453fa9e4066Sahrens }
454ea8dc4b6Seschrock
455ea8dc4b6Seschrock while (next != NULL) {
456bc9014e6SJustin Gibbs dsl_dir_t *child_dd;
457ea8dc4b6Seschrock err = getcomponent(next, buf, &nextnext);
4583b2aab18SMatthew Ahrens if (err != 0)
459ea8dc4b6Seschrock break;
460fa9e4066Sahrens ASSERT(next[0] != '\0');
461fa9e4066Sahrens if (next[0] == '@')
462fa9e4066Sahrens break;
463fa9e4066Sahrens dprintf("looking up %s in obj%lld\n",
464c1379625SJustin T. Gibbs buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
465fa9e4066Sahrens
466fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset,
467c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj,
468fa9e4066Sahrens buf, sizeof (ddobj), 1, &ddobj);
4693b2aab18SMatthew Ahrens if (err != 0) {
470ea8dc4b6Seschrock if (err == ENOENT)
471ea8dc4b6Seschrock err = 0;
472fa9e4066Sahrens break;
473fa9e4066Sahrens }
474fa9e4066Sahrens
475bc9014e6SJustin Gibbs err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
4763b2aab18SMatthew Ahrens if (err != 0)
477ea8dc4b6Seschrock break;
4783b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag);
479bc9014e6SJustin Gibbs dd = child_dd;
480fa9e4066Sahrens next = nextnext;
481fa9e4066Sahrens }
482fa9e4066Sahrens
4833b2aab18SMatthew Ahrens if (err != 0) {
4843b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag);
485ea8dc4b6Seschrock return (err);
486ea8dc4b6Seschrock }
487ea8dc4b6Seschrock
488fa9e4066Sahrens /*
489fa9e4066Sahrens * It's an error if there's more than one component left, or
490fa9e4066Sahrens * tailp==NULL and there's any component left.
491fa9e4066Sahrens */
492fa9e4066Sahrens if (next != NULL &&
493fa9e4066Sahrens (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
494fa9e4066Sahrens /* bad path name */
4953b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag);
496fa9e4066Sahrens dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
497be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT);
498fa9e4066Sahrens }
4993b2aab18SMatthew Ahrens if (tailp != NULL)
500fa9e4066Sahrens *tailp = next;
501ea8dc4b6Seschrock *ddp = dd;
502ea8dc4b6Seschrock return (err);
503fa9e4066Sahrens }
504fa9e4066Sahrens
505a2afb611SJerry Jelinek /*
506a2afb611SJerry Jelinek * If the counts are already initialized for this filesystem and its
507a2afb611SJerry Jelinek * descendants then do nothing, otherwise initialize the counts.
508a2afb611SJerry Jelinek *
509a2afb611SJerry Jelinek * The counts on this filesystem, and those below, may be uninitialized due to
510a2afb611SJerry Jelinek * either the use of a pre-existing pool which did not support the
511a2afb611SJerry Jelinek * filesystem/snapshot limit feature, or one in which the feature had not yet
512a2afb611SJerry Jelinek * been enabled.
513a2afb611SJerry Jelinek *
514a2afb611SJerry Jelinek * Recursively descend the filesystem tree and update the filesystem/snapshot
515a2afb611SJerry Jelinek * counts on each filesystem below, then update the cumulative count on the
516a2afb611SJerry Jelinek * current filesystem. If the filesystem already has a count set on it,
517a2afb611SJerry Jelinek * then we know that its counts, and the counts on the filesystems below it,
518a2afb611SJerry Jelinek * are already correct, so we don't have to update this filesystem.
519a2afb611SJerry Jelinek */
520a2afb611SJerry Jelinek static void
dsl_dir_init_fs_ss_count(dsl_dir_t * dd,dmu_tx_t * tx)521a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
522a2afb611SJerry Jelinek {
523a2afb611SJerry Jelinek uint64_t my_fs_cnt = 0;
524a2afb611SJerry Jelinek uint64_t my_ss_cnt = 0;
525a2afb611SJerry Jelinek dsl_pool_t *dp = dd->dd_pool;
526a2afb611SJerry Jelinek objset_t *os = dp->dp_meta_objset;
527a2afb611SJerry Jelinek zap_cursor_t *zc;
528a2afb611SJerry Jelinek zap_attribute_t *za;
529a2afb611SJerry Jelinek dsl_dataset_t *ds;
530a2afb611SJerry Jelinek
531adf34077SJerry Jelinek ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
532a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dp));
533a2afb611SJerry Jelinek ASSERT(dmu_tx_is_syncing(tx));
534a2afb611SJerry Jelinek
535a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx);
536a2afb611SJerry Jelinek
537a2afb611SJerry Jelinek /*
538a2afb611SJerry Jelinek * If the filesystem count has already been initialized then we
539a2afb611SJerry Jelinek * don't need to recurse down any further.
540a2afb611SJerry Jelinek */
541a2afb611SJerry Jelinek if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
542a2afb611SJerry Jelinek return;
543a2afb611SJerry Jelinek
544a2afb611SJerry Jelinek zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
545a2afb611SJerry Jelinek za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
546a2afb611SJerry Jelinek
547a2afb611SJerry Jelinek /* Iterate my child dirs */
548c1379625SJustin T. Gibbs for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
549a2afb611SJerry Jelinek zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
550a2afb611SJerry Jelinek dsl_dir_t *chld_dd;
551a2afb611SJerry Jelinek uint64_t count;
552a2afb611SJerry Jelinek
553a2afb611SJerry Jelinek VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
554a2afb611SJerry Jelinek &chld_dd));
555a2afb611SJerry Jelinek
556a2afb611SJerry Jelinek /*
557a2afb611SJerry Jelinek * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
558a2afb611SJerry Jelinek * temporary datasets.
559a2afb611SJerry Jelinek */
560a2afb611SJerry Jelinek if (chld_dd->dd_myname[0] == '$' ||
561a2afb611SJerry Jelinek chld_dd->dd_myname[0] == '%') {
562a2afb611SJerry Jelinek dsl_dir_rele(chld_dd, FTAG);
563a2afb611SJerry Jelinek continue;
564a2afb611SJerry Jelinek }
565a2afb611SJerry Jelinek
566a2afb611SJerry Jelinek my_fs_cnt++; /* count this child */
567a2afb611SJerry Jelinek
568a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(chld_dd, tx);
569a2afb611SJerry Jelinek
570a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, chld_dd->dd_object,
571a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
572a2afb611SJerry Jelinek my_fs_cnt += count;
573a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, chld_dd->dd_object,
574a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
575a2afb611SJerry Jelinek my_ss_cnt += count;
576a2afb611SJerry Jelinek
577a2afb611SJerry Jelinek dsl_dir_rele(chld_dd, FTAG);
578a2afb611SJerry Jelinek }
579a2afb611SJerry Jelinek zap_cursor_fini(zc);
580a2afb611SJerry Jelinek /* Count my snapshots (we counted children's snapshots above) */
581a2afb611SJerry Jelinek VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
582c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
583a2afb611SJerry Jelinek
584c1379625SJustin T. Gibbs for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
585a2afb611SJerry Jelinek zap_cursor_retrieve(zc, za) == 0;
586a2afb611SJerry Jelinek zap_cursor_advance(zc)) {
587a2afb611SJerry Jelinek /* Don't count temporary snapshots */
588a2afb611SJerry Jelinek if (za->za_name[0] != '%')
589a2afb611SJerry Jelinek my_ss_cnt++;
590a2afb611SJerry Jelinek }
591fb7001f1SAlex Reece zap_cursor_fini(zc);
592a2afb611SJerry Jelinek
593a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG);
594a2afb611SJerry Jelinek
595a2afb611SJerry Jelinek kmem_free(zc, sizeof (zap_cursor_t));
596a2afb611SJerry Jelinek kmem_free(za, sizeof (zap_attribute_t));
597a2afb611SJerry Jelinek
598a2afb611SJerry Jelinek /* we're in a sync task, update counts */
599a2afb611SJerry Jelinek dmu_buf_will_dirty(dd->dd_dbuf, tx);
600a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
601a2afb611SJerry Jelinek sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
602a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
603a2afb611SJerry Jelinek sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
604a2afb611SJerry Jelinek }
605a2afb611SJerry Jelinek
606a2afb611SJerry Jelinek static int
dsl_dir_actv_fs_ss_limit_check(void * arg,dmu_tx_t * tx)607a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
608a2afb611SJerry Jelinek {
609a2afb611SJerry Jelinek char *ddname = (char *)arg;
610a2afb611SJerry Jelinek dsl_pool_t *dp = dmu_tx_pool(tx);
611a2afb611SJerry Jelinek dsl_dataset_t *ds;
612a2afb611SJerry Jelinek dsl_dir_t *dd;
613a2afb611SJerry Jelinek int error;
614a2afb611SJerry Jelinek
615a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
616a2afb611SJerry Jelinek if (error != 0)
617a2afb611SJerry Jelinek return (error);
618a2afb611SJerry Jelinek
619a2afb611SJerry Jelinek if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
620a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG);
621a2afb611SJerry Jelinek return (SET_ERROR(ENOTSUP));
622a2afb611SJerry Jelinek }
623a2afb611SJerry Jelinek
624a2afb611SJerry Jelinek dd = ds->ds_dir;
625a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
626a2afb611SJerry Jelinek dsl_dir_is_zapified(dd) &&
627a2afb611SJerry Jelinek zap_contains(dp->dp_meta_objset, dd->dd_object,
628a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT) == 0) {
629a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG);
630a2afb611SJerry Jelinek return (SET_ERROR(EALREADY));
631a2afb611SJerry Jelinek }
632a2afb611SJerry Jelinek
633a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG);
634a2afb611SJerry Jelinek return (0);
635a2afb611SJerry Jelinek }
636a2afb611SJerry Jelinek
637a2afb611SJerry Jelinek static void
dsl_dir_actv_fs_ss_limit_sync(void * arg,dmu_tx_t * tx)638a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
639a2afb611SJerry Jelinek {
640a2afb611SJerry Jelinek char *ddname = (char *)arg;
641a2afb611SJerry Jelinek dsl_pool_t *dp = dmu_tx_pool(tx);
642a2afb611SJerry Jelinek dsl_dataset_t *ds;
643a2afb611SJerry Jelinek spa_t *spa;
644a2afb611SJerry Jelinek
645a2afb611SJerry Jelinek VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
646a2afb611SJerry Jelinek
647a2afb611SJerry Jelinek spa = dsl_dataset_get_spa(ds);
648a2afb611SJerry Jelinek
649a2afb611SJerry Jelinek if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
650a2afb611SJerry Jelinek /*
651a2afb611SJerry Jelinek * Since the feature was not active and we're now setting a
652a2afb611SJerry Jelinek * limit, increment the feature-active counter so that the
653a2afb611SJerry Jelinek * feature becomes active for the first time.
654a2afb611SJerry Jelinek *
655a2afb611SJerry Jelinek * We are already in a sync task so we can update the MOS.
656a2afb611SJerry Jelinek */
657a2afb611SJerry Jelinek spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
658a2afb611SJerry Jelinek }
659a2afb611SJerry Jelinek
660a2afb611SJerry Jelinek /*
661a2afb611SJerry Jelinek * Since we are now setting a non-UINT64_MAX limit on the filesystem,
662a2afb611SJerry Jelinek * we need to ensure the counts are correct. Descend down the tree from
663a2afb611SJerry Jelinek * this point and update all of the counts to be accurate.
664a2afb611SJerry Jelinek */
665a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
666a2afb611SJerry Jelinek
667a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG);
668a2afb611SJerry Jelinek }
669a2afb611SJerry Jelinek
670a2afb611SJerry Jelinek /*
671a2afb611SJerry Jelinek * Make sure the feature is enabled and activate it if necessary.
672a2afb611SJerry Jelinek * Since we're setting a limit, ensure the on-disk counts are valid.
673a2afb611SJerry Jelinek * This is only called by the ioctl path when setting a limit value.
674a2afb611SJerry Jelinek *
675a2afb611SJerry Jelinek * We do not need to validate the new limit, since users who can change the
676a2afb611SJerry Jelinek * limit are also allowed to exceed the limit.
677a2afb611SJerry Jelinek */
678a2afb611SJerry Jelinek int
dsl_dir_activate_fs_ss_limit(const char * ddname)679a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname)
680a2afb611SJerry Jelinek {
681a2afb611SJerry Jelinek int error;
682a2afb611SJerry Jelinek
683a2afb611SJerry Jelinek error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
6847d46dc6cSMatthew Ahrens dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
6857d46dc6cSMatthew Ahrens ZFS_SPACE_CHECK_RESERVED);
686a2afb611SJerry Jelinek
687a2afb611SJerry Jelinek if (error == EALREADY)
688a2afb611SJerry Jelinek error = 0;
689a2afb611SJerry Jelinek
690a2afb611SJerry Jelinek return (error);
691a2afb611SJerry Jelinek }
692a2afb611SJerry Jelinek
693a2afb611SJerry Jelinek /*
694a2afb611SJerry Jelinek * Used to determine if the filesystem_limit or snapshot_limit should be
695a2afb611SJerry Jelinek * enforced. We allow the limit to be exceeded if the user has permission to
696a2afb611SJerry Jelinek * write the property value. We pass in the creds that we got in the open
697a2afb611SJerry Jelinek * context since we will always be the GZ root in syncing context. We also have
698a2afb611SJerry Jelinek * to handle the case where we are allowed to change the limit on the current
699a2afb611SJerry Jelinek * dataset, but there may be another limit in the tree above.
700a2afb611SJerry Jelinek *
701a2afb611SJerry Jelinek * We can never modify these two properties within a non-global zone. In
702a2afb611SJerry Jelinek * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
703a2afb611SJerry Jelinek * can't use that function since we are already holding the dp_config_rwlock.
704a2afb611SJerry Jelinek * In addition, we already have the dd and dealing with snapshots is simplified
705a2afb611SJerry Jelinek * in this code.
706a2afb611SJerry Jelinek */
707a2afb611SJerry Jelinek
708a2afb611SJerry Jelinek typedef enum {
709a2afb611SJerry Jelinek ENFORCE_ALWAYS,
710a2afb611SJerry Jelinek ENFORCE_NEVER,
711a2afb611SJerry Jelinek ENFORCE_ABOVE
712a2afb611SJerry Jelinek } enforce_res_t;
713a2afb611SJerry Jelinek
714a2afb611SJerry Jelinek static enforce_res_t
dsl_enforce_ds_ss_limits(dsl_dir_t * dd,zfs_prop_t prop,cred_t * cr)715a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
716a2afb611SJerry Jelinek {
717a2afb611SJerry Jelinek enforce_res_t enforce = ENFORCE_ALWAYS;
718a2afb611SJerry Jelinek uint64_t obj;
719a2afb611SJerry Jelinek dsl_dataset_t *ds;
720a2afb611SJerry Jelinek uint64_t zoned;
721a2afb611SJerry Jelinek
722a2afb611SJerry Jelinek ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
723a2afb611SJerry Jelinek prop == ZFS_PROP_SNAPSHOT_LIMIT);
724a2afb611SJerry Jelinek
725a2afb611SJerry Jelinek #ifdef _KERNEL
726a2afb611SJerry Jelinek if (crgetzoneid(cr) != GLOBAL_ZONEID)
727a2afb611SJerry Jelinek return (ENFORCE_ALWAYS);
728a2afb611SJerry Jelinek
729a2afb611SJerry Jelinek if (secpolicy_zfs(cr) == 0)
730a2afb611SJerry Jelinek return (ENFORCE_NEVER);
731a2afb611SJerry Jelinek #endif
732a2afb611SJerry Jelinek
733c1379625SJustin T. Gibbs if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
734a2afb611SJerry Jelinek return (ENFORCE_ALWAYS);
735a2afb611SJerry Jelinek
736a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool));
737a2afb611SJerry Jelinek
738a2afb611SJerry Jelinek if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
739a2afb611SJerry Jelinek return (ENFORCE_ALWAYS);
740a2afb611SJerry Jelinek
741a2afb611SJerry Jelinek if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
742a2afb611SJerry Jelinek /* Only root can access zoned fs's from the GZ */
743a2afb611SJerry Jelinek enforce = ENFORCE_ALWAYS;
744a2afb611SJerry Jelinek } else {
745a2afb611SJerry Jelinek if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
746a2afb611SJerry Jelinek enforce = ENFORCE_ABOVE;
747a2afb611SJerry Jelinek }
748a2afb611SJerry Jelinek
749a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG);
750a2afb611SJerry Jelinek return (enforce);
751a2afb611SJerry Jelinek }
752a2afb611SJerry Jelinek
753a2afb611SJerry Jelinek /*
754a2afb611SJerry Jelinek * Check if adding additional child filesystem(s) would exceed any filesystem
755a2afb611SJerry Jelinek * limits or adding additional snapshot(s) would exceed any snapshot limits.
756a2afb611SJerry Jelinek * The prop argument indicates which limit to check.
757a2afb611SJerry Jelinek *
758a2afb611SJerry Jelinek * Note that all filesystem limits up to the root (or the highest
759a2afb611SJerry Jelinek * initialized) filesystem or the given ancestor must be satisfied.
760a2afb611SJerry Jelinek */
761a2afb611SJerry 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)762a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
763a2afb611SJerry Jelinek dsl_dir_t *ancestor, cred_t *cr)
764a2afb611SJerry Jelinek {
765a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset;
766a2afb611SJerry Jelinek uint64_t limit, count;
767a2afb611SJerry Jelinek char *count_prop;
768a2afb611SJerry Jelinek enforce_res_t enforce;
769a2afb611SJerry Jelinek int err = 0;
770a2afb611SJerry Jelinek
771a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool));
772a2afb611SJerry Jelinek ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
773a2afb611SJerry Jelinek prop == ZFS_PROP_SNAPSHOT_LIMIT);
774a2afb611SJerry Jelinek
775a2afb611SJerry Jelinek /*
776a2afb611SJerry Jelinek * If we're allowed to change the limit, don't enforce the limit
777a2afb611SJerry Jelinek * e.g. this can happen if a snapshot is taken by an administrative
778a2afb611SJerry Jelinek * user in the global zone (i.e. a recursive snapshot by root).
779a2afb611SJerry Jelinek * However, we must handle the case of delegated permissions where we
780a2afb611SJerry Jelinek * are allowed to change the limit on the current dataset, but there
781a2afb611SJerry Jelinek * is another limit in the tree above.
782a2afb611SJerry Jelinek */
783a2afb611SJerry Jelinek enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
784a2afb611SJerry Jelinek if (enforce == ENFORCE_NEVER)
785a2afb611SJerry Jelinek return (0);
786a2afb611SJerry Jelinek
787a2afb611SJerry Jelinek /*
788a2afb611SJerry Jelinek * e.g. if renaming a dataset with no snapshots, count adjustment
789a2afb611SJerry Jelinek * is 0.
790a2afb611SJerry Jelinek */
791a2afb611SJerry Jelinek if (delta == 0)
792a2afb611SJerry Jelinek return (0);
793a2afb611SJerry Jelinek
794a2afb611SJerry Jelinek if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
795a2afb611SJerry Jelinek /*
796a2afb611SJerry Jelinek * We don't enforce the limit for temporary snapshots. This is
797a2afb611SJerry Jelinek * indicated by a NULL cred_t argument.
798a2afb611SJerry Jelinek */
799a2afb611SJerry Jelinek if (cr == NULL)
800a2afb611SJerry Jelinek return (0);
801a2afb611SJerry Jelinek
802a2afb611SJerry Jelinek count_prop = DD_FIELD_SNAPSHOT_COUNT;
803a2afb611SJerry Jelinek } else {
804a2afb611SJerry Jelinek count_prop = DD_FIELD_FILESYSTEM_COUNT;
805a2afb611SJerry Jelinek }
806a2afb611SJerry Jelinek
807a2afb611SJerry Jelinek /*
808a2afb611SJerry Jelinek * If an ancestor has been provided, stop checking the limit once we
809a2afb611SJerry Jelinek * hit that dir. We need this during rename so that we don't overcount
810a2afb611SJerry Jelinek * the check once we recurse up to the common ancestor.
811a2afb611SJerry Jelinek */
812a2afb611SJerry Jelinek if (ancestor == dd)
813a2afb611SJerry Jelinek return (0);
814a2afb611SJerry Jelinek
815a2afb611SJerry Jelinek /*
816a2afb611SJerry Jelinek * If we hit an uninitialized node while recursing up the tree, we can
817a2afb611SJerry Jelinek * stop since we know there is no limit here (or above). The counts are
818a2afb611SJerry Jelinek * not valid on this node and we know we won't touch this node's counts.
819a2afb611SJerry Jelinek */
820a2afb611SJerry Jelinek if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
821a2afb611SJerry Jelinek count_prop, sizeof (count), 1, &count) == ENOENT)
822a2afb611SJerry Jelinek return (0);
823a2afb611SJerry Jelinek
824a2afb611SJerry Jelinek err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
825a2afb611SJerry Jelinek B_FALSE);
826a2afb611SJerry Jelinek if (err != 0)
827a2afb611SJerry Jelinek return (err);
828a2afb611SJerry Jelinek
829a2afb611SJerry Jelinek /* Is there a limit which we've hit? */
830a2afb611SJerry Jelinek if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
831a2afb611SJerry Jelinek return (SET_ERROR(EDQUOT));
832a2afb611SJerry Jelinek
833a2afb611SJerry Jelinek if (dd->dd_parent != NULL)
834a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
835a2afb611SJerry Jelinek ancestor, cr);
836a2afb611SJerry Jelinek
837a2afb611SJerry Jelinek return (err);
838a2afb611SJerry Jelinek }
839a2afb611SJerry Jelinek
840a2afb611SJerry Jelinek /*
841a2afb611SJerry Jelinek * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
842a2afb611SJerry Jelinek * parents. When a new filesystem/snapshot is created, increment the count on
843a2afb611SJerry Jelinek * all parents, and when a filesystem/snapshot is destroyed, decrement the
844a2afb611SJerry Jelinek * count.
845a2afb611SJerry Jelinek */
846a2afb611SJerry Jelinek void
dsl_fs_ss_count_adjust(dsl_dir_t * dd,int64_t delta,const char * prop,dmu_tx_t * tx)847a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
848a2afb611SJerry Jelinek dmu_tx_t *tx)
849a2afb611SJerry Jelinek {
850a2afb611SJerry Jelinek int err;
851a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset;
852a2afb611SJerry Jelinek uint64_t count;
853a2afb611SJerry Jelinek
854a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool));
855a2afb611SJerry Jelinek ASSERT(dmu_tx_is_syncing(tx));
856a2afb611SJerry Jelinek ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
857a2afb611SJerry Jelinek strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
858a2afb611SJerry Jelinek
859a2afb611SJerry Jelinek /*
860a2afb611SJerry Jelinek * When we receive an incremental stream into a filesystem that already
861a2afb611SJerry Jelinek * exists, a temporary clone is created. We don't count this temporary
862a2afb611SJerry Jelinek * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
863a2afb611SJerry Jelinek * $MOS & $ORIGIN) objsets.
864a2afb611SJerry Jelinek */
865a2afb611SJerry Jelinek if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
866a2afb611SJerry Jelinek strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
867a2afb611SJerry Jelinek return;
868a2afb611SJerry Jelinek
869a2afb611SJerry Jelinek /*
870a2afb611SJerry Jelinek * e.g. if renaming a dataset with no snapshots, count adjustment is 0
871a2afb611SJerry Jelinek */
872a2afb611SJerry Jelinek if (delta == 0)
873a2afb611SJerry Jelinek return;
874a2afb611SJerry Jelinek
875a2afb611SJerry Jelinek /*
876a2afb611SJerry Jelinek * If we hit an uninitialized node while recursing up the tree, we can
877a2afb611SJerry Jelinek * stop since we know the counts are not valid on this node and we
878a2afb611SJerry Jelinek * know we shouldn't touch this node's counts. An uninitialized count
879a2afb611SJerry Jelinek * on the node indicates that either the feature has not yet been
880a2afb611SJerry Jelinek * activated or there are no limits on this part of the tree.
881a2afb611SJerry Jelinek */
882a2afb611SJerry Jelinek if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
883a2afb611SJerry Jelinek prop, sizeof (count), 1, &count)) == ENOENT)
884a2afb611SJerry Jelinek return;
885a2afb611SJerry Jelinek VERIFY0(err);
886a2afb611SJerry Jelinek
887a2afb611SJerry Jelinek count += delta;
888a2afb611SJerry Jelinek /* Use a signed verify to make sure we're not neg. */
889a2afb611SJerry Jelinek VERIFY3S(count, >=, 0);
890a2afb611SJerry Jelinek
891a2afb611SJerry Jelinek VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
892a2afb611SJerry Jelinek tx));
893a2afb611SJerry Jelinek
894a2afb611SJerry Jelinek /* Roll up this additional count into our ancestors */
895a2afb611SJerry Jelinek if (dd->dd_parent != NULL)
896a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
897a2afb611SJerry Jelinek }
898a2afb611SJerry Jelinek
8991d452cf5Sahrens uint64_t
dsl_dir_create_sync(dsl_pool_t * dp,dsl_dir_t * pds,const char * name,dmu_tx_t * tx)900088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
901088f3894Sahrens dmu_tx_t *tx)
902fa9e4066Sahrens {
903088f3894Sahrens objset_t *mos = dp->dp_meta_objset;
904fa9e4066Sahrens uint64_t ddobj;
905cde58dbcSMatthew Ahrens dsl_dir_phys_t *ddphys;
906fa9e4066Sahrens dmu_buf_t *dbuf;
907fa9e4066Sahrens
9081649cd4bStabriz ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
9091649cd4bStabriz DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
910088f3894Sahrens if (pds) {
911c1379625SJustin T. Gibbs VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
9121d452cf5Sahrens name, sizeof (uint64_t), 1, &ddobj, tx));
913088f3894Sahrens } else {
914088f3894Sahrens /* it's the root dir */
915088f3894Sahrens VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
916088f3894Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
917088f3894Sahrens }
918ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
919fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx);
920cde58dbcSMatthew Ahrens ddphys = dbuf->db_data;
921fa9e4066Sahrens
922cde58dbcSMatthew Ahrens ddphys->dd_creation_time = gethrestime_sec();
923a2afb611SJerry Jelinek if (pds) {
924cde58dbcSMatthew Ahrens ddphys->dd_parent_obj = pds->dd_object;
925a2afb611SJerry Jelinek
926a2afb611SJerry Jelinek /* update the filesystem counts */
927a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
928a2afb611SJerry Jelinek }
929cde58dbcSMatthew Ahrens ddphys->dd_props_zapobj = zap_create(mos,
930fa9e4066Sahrens DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
931cde58dbcSMatthew Ahrens ddphys->dd_child_dir_zapobj = zap_create(mos,
93287e5029aSahrens DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
93374e7dc98SMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
934cde58dbcSMatthew Ahrens ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
935ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG);
936fa9e4066Sahrens
9371d452cf5Sahrens return (ddobj);
9381d452cf5Sahrens }
9391d452cf5Sahrens
940088f3894Sahrens boolean_t
dsl_dir_is_clone(dsl_dir_t * dd)941088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd)
942fa9e4066Sahrens {
943c1379625SJustin T. Gibbs return (dsl_dir_phys(dd)->dd_origin_obj &&
944088f3894Sahrens (dd->dd_pool->dp_origin_snap == NULL ||
945c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj !=
946088f3894Sahrens dd->dd_pool->dp_origin_snap->ds_object));
947fa9e4066Sahrens }
948fa9e4066Sahrens
949fa9e4066Sahrens void
dsl_dir_stats(dsl_dir_t * dd,nvlist_t * nv)950a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
951fa9e4066Sahrens {
952fa9e4066Sahrens mutex_enter(&dd->dd_lock);
95374e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
954c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes);
955c1379625SJustin T. Gibbs dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
956c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_quota);
957a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
958c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_reserved);
959a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
960c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
961c1379625SJustin T. Gibbs (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
962c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes));
96377372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
964c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes);
965c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
96674e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
967c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
96874e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
969c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
97074e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
971c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
97274e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
973c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
974c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
97574e7dc98SMatthew Ahrens }
976fa9e4066Sahrens mutex_exit(&dd->dd_lock);
977fa9e4066Sahrens
978a2afb611SJerry Jelinek if (dsl_dir_is_zapified(dd)) {
979a2afb611SJerry Jelinek uint64_t count;
980a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset;
981a2afb611SJerry Jelinek
982a2afb611SJerry Jelinek if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
983a2afb611SJerry Jelinek sizeof (count), 1, &count) == 0) {
984a2afb611SJerry Jelinek dsl_prop_nvlist_add_uint64(nv,
985a2afb611SJerry Jelinek ZFS_PROP_FILESYSTEM_COUNT, count);
986a2afb611SJerry Jelinek }
987a2afb611SJerry Jelinek if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
988a2afb611SJerry Jelinek sizeof (count), 1, &count) == 0) {
989a2afb611SJerry Jelinek dsl_prop_nvlist_add_uint64(nv,
990a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_COUNT, count);
991a2afb611SJerry Jelinek }
992a2afb611SJerry Jelinek }
993a2afb611SJerry Jelinek
994088f3894Sahrens if (dsl_dir_is_clone(dd)) {
995fa9e4066Sahrens dsl_dataset_t *ds;
99640a5c998SMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN];
997fa9e4066Sahrens
9983b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
999c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1000a2eea2e1Sahrens dsl_dataset_name(ds, buf);
1001745cd3c5Smaybee dsl_dataset_rele(ds, FTAG);
1002a2eea2e1Sahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1003fa9e4066Sahrens }
1004fa9e4066Sahrens }
1005fa9e4066Sahrens
1006fa9e4066Sahrens void
dsl_dir_dirty(dsl_dir_t * dd,dmu_tx_t * tx)1007fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1008fa9e4066Sahrens {
1009fa9e4066Sahrens dsl_pool_t *dp = dd->dd_pool;
1010fa9e4066Sahrens
1011c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd));
1012fa9e4066Sahrens
10133b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1014fa9e4066Sahrens /* up the hold count until we can be written out */
1015fa9e4066Sahrens dmu_buf_add_ref(dd->dd_dbuf, dd);
1016fa9e4066Sahrens }
1017fa9e4066Sahrens }
1018fa9e4066Sahrens
1019fa9e4066Sahrens static int64_t
parent_delta(dsl_dir_t * dd,uint64_t used,int64_t delta)1020fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1021fa9e4066Sahrens {
1022c1379625SJustin T. Gibbs uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1023c1379625SJustin T. Gibbs uint64_t new_accounted =
1024c1379625SJustin T. Gibbs MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1025fa9e4066Sahrens return (new_accounted - old_accounted);
1026fa9e4066Sahrens }
1027fa9e4066Sahrens
1028fa9e4066Sahrens void
dsl_dir_sync(dsl_dir_t * dd,dmu_tx_t * tx)1029fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1030fa9e4066Sahrens {
1031fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx));
1032fa9e4066Sahrens
1033fa9e4066Sahrens mutex_enter(&dd->dd_lock);
1034fb09f5aaSMadhav Suresh ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1035fa9e4066Sahrens dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1036fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1037fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1038fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1039fa9e4066Sahrens
1040fa9e4066Sahrens /* release the hold from dsl_dir_dirty */
1041ea8dc4b6Seschrock dmu_buf_rele(dd->dd_dbuf, dd);
1042fa9e4066Sahrens }
1043fa9e4066Sahrens
1044fa9e4066Sahrens static uint64_t
dsl_dir_space_towrite(dsl_dir_t * dd)1045a9799022Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd)
1046fa9e4066Sahrens {
1047a9799022Sck153898 uint64_t space = 0;
1048fa9e4066Sahrens int i;
1049fa9e4066Sahrens
1050fa9e4066Sahrens ASSERT(MUTEX_HELD(&dd->dd_lock));
1051fa9e4066Sahrens
1052fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) {
1053fa9e4066Sahrens space += dd->dd_space_towrite[i&TXG_MASK];
1054fa9e4066Sahrens ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1055fa9e4066Sahrens }
1056fa9e4066Sahrens return (space);
1057fa9e4066Sahrens }
1058fa9e4066Sahrens
1059fa9e4066Sahrens /*
1060fa9e4066Sahrens * How much space would dd have available if ancestor had delta applied
1061fa9e4066Sahrens * to it? If ondiskonly is set, we're only interested in what's
1062fa9e4066Sahrens * on-disk, not estimated pending changes.
1063fa9e4066Sahrens */
1064a2eea2e1Sahrens uint64_t
dsl_dir_space_available(dsl_dir_t * dd,dsl_dir_t * ancestor,int64_t delta,int ondiskonly)1065fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd,
1066fa9e4066Sahrens dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1067fa9e4066Sahrens {
1068fa9e4066Sahrens uint64_t parentspace, myspace, quota, used;
1069fa9e4066Sahrens
1070fa9e4066Sahrens /*
1071fa9e4066Sahrens * If there are no restrictions otherwise, assume we have
1072fa9e4066Sahrens * unlimited space available.
1073fa9e4066Sahrens */
1074fa9e4066Sahrens quota = UINT64_MAX;
1075fa9e4066Sahrens parentspace = UINT64_MAX;
1076fa9e4066Sahrens
1077fa9e4066Sahrens if (dd->dd_parent != NULL) {
1078fa9e4066Sahrens parentspace = dsl_dir_space_available(dd->dd_parent,
1079fa9e4066Sahrens ancestor, delta, ondiskonly);
1080fa9e4066Sahrens }
1081fa9e4066Sahrens
1082fa9e4066Sahrens mutex_enter(&dd->dd_lock);
1083c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_quota != 0)
1084c1379625SJustin T. Gibbs quota = dsl_dir_phys(dd)->dd_quota;
1085c1379625SJustin T. Gibbs used = dsl_dir_phys(dd)->dd_used_bytes;
1086a9799022Sck153898 if (!ondiskonly)
1087a9799022Sck153898 used += dsl_dir_space_towrite(dd);
1088fa9e4066Sahrens
1089fa9e4066Sahrens if (dd->dd_parent == NULL) {
109099653d4eSeschrock uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1091fa9e4066Sahrens quota = MIN(quota, poolsize);
1092fa9e4066Sahrens }
1093fa9e4066Sahrens
1094c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1095fa9e4066Sahrens /*
1096fa9e4066Sahrens * We have some space reserved, in addition to what our
1097fa9e4066Sahrens * parent gave us.
1098fa9e4066Sahrens */
1099c1379625SJustin T. Gibbs parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1100fa9e4066Sahrens }
1101fa9e4066Sahrens
110274e7dc98SMatthew Ahrens if (dd == ancestor) {
110374e7dc98SMatthew Ahrens ASSERT(delta <= 0);
110474e7dc98SMatthew Ahrens ASSERT(used >= -delta);
110574e7dc98SMatthew Ahrens used += delta;
110674e7dc98SMatthew Ahrens if (parentspace != UINT64_MAX)
110774e7dc98SMatthew Ahrens parentspace -= delta;
110874e7dc98SMatthew Ahrens }
110974e7dc98SMatthew Ahrens
1110fa9e4066Sahrens if (used > quota) {
1111fa9e4066Sahrens /* over quota */
1112fa9e4066Sahrens myspace = 0;
1113fa9e4066Sahrens } else {
1114fa9e4066Sahrens /*
111599653d4eSeschrock * the lesser of the space provided by our parent and
111699653d4eSeschrock * the space left in our quota
1117fa9e4066Sahrens */
1118fa9e4066Sahrens myspace = MIN(parentspace, quota - used);
1119fa9e4066Sahrens }
1120fa9e4066Sahrens
1121fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1122fa9e4066Sahrens
1123fa9e4066Sahrens return (myspace);
1124fa9e4066Sahrens }
1125fa9e4066Sahrens
1126fa9e4066Sahrens struct tempreserve {
1127fa9e4066Sahrens list_node_t tr_node;
1128fa9e4066Sahrens dsl_dir_t *tr_ds;
1129fa9e4066Sahrens uint64_t tr_size;
1130fa9e4066Sahrens };
1131fa9e4066Sahrens
1132fa9e4066Sahrens 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)1133a9799022Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1134a9799022Sck153898 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
11356df6c3bcSck153898 dmu_tx_t *tx, boolean_t first)
1136fa9e4066Sahrens {
1137fa9e4066Sahrens uint64_t txg = tx->tx_txg;
1138a9799022Sck153898 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1139468c413aSTim Haley uint64_t deferred = 0;
1140a9799022Sck153898 struct tempreserve *tr;
1141468c413aSTim Haley int retval = EDQUOT;
1142fa9e4066Sahrens int txgidx = txg & TXG_MASK;
1143fa9e4066Sahrens int i;
11449082849eSck153898 uint64_t ref_rsrv = 0;
1145fa9e4066Sahrens
1146fa9e4066Sahrens ASSERT3U(txg, !=, 0);
1147a9799022Sck153898 ASSERT3S(asize, >, 0);
1148fa9e4066Sahrens
1149fa9e4066Sahrens mutex_enter(&dd->dd_lock);
1150a9799022Sck153898
1151fa9e4066Sahrens /*
1152fa9e4066Sahrens * Check against the dsl_dir's quota. We don't add in the delta
1153fa9e4066Sahrens * when checking for over-quota because they get one free hit.
1154fa9e4066Sahrens */
1155a9799022Sck153898 est_inflight = dsl_dir_space_towrite(dd);
1156fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++)
1157a9799022Sck153898 est_inflight += dd->dd_tempreserved[i];
1158c1379625SJustin T. Gibbs used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1159fa9e4066Sahrens
1160f4d2e9e6Smaybee /*
11616df6c3bcSck153898 * On the first iteration, fetch the dataset's used-on-disk and
11626df6c3bcSck153898 * refreservation values. Also, if checkrefquota is set, test if
11636df6c3bcSck153898 * allocating this space would exceed the dataset's refquota.
1164f4d2e9e6Smaybee */
11656df6c3bcSck153898 if (first && tx->tx_objset) {
1166c3fdb13aSmaybee int error;
1167503ad85cSMatthew Ahrens dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
11689082849eSck153898
1169a9799022Sck153898 error = dsl_dataset_check_quota(ds, checkrefquota,
11709082849eSck153898 asize, est_inflight, &used_on_disk, &ref_rsrv);
1171a9799022Sck153898 if (error) {
1172a9799022Sck153898 mutex_exit(&dd->dd_lock);
1173a9799022Sck153898 return (error);
1174a9799022Sck153898 }
1175a9799022Sck153898 }
1176a9799022Sck153898
1177a9799022Sck153898 /*
1178a9799022Sck153898 * If this transaction will result in a net free of space,
1179a9799022Sck153898 * we want to let it through.
1180a9799022Sck153898 */
1181c1379625SJustin T. Gibbs if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1182fa9e4066Sahrens quota = UINT64_MAX;
1183f4d2e9e6Smaybee else
1184c1379625SJustin T. Gibbs quota = dsl_dir_phys(dd)->dd_quota;
1185fa9e4066Sahrens
1186fa9e4066Sahrens /*
1187468c413aSTim Haley * Adjust the quota against the actual pool size at the root
1188468c413aSTim Haley * minus any outstanding deferred frees.
1189f4d2e9e6Smaybee * To ensure that it's possible to remove files from a full
1190f4d2e9e6Smaybee * pool without inducing transient overcommits, we throttle
1191fa9e4066Sahrens * netfree transactions against a quota that is slightly larger,
1192fa9e4066Sahrens * but still within the pool's allocation slop. In cases where
1193fa9e4066Sahrens * we're very close to full, this will allow a steady trickle of
1194fa9e4066Sahrens * removes to get through.
1195fa9e4066Sahrens */
11961934e92fSmaybee if (dd->dd_parent == NULL) {
1197b24ab676SJeff Bonwick spa_t *spa = dd->dd_pool->dp_spa;
1198fa9e4066Sahrens uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1199b24ab676SJeff Bonwick deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1200468c413aSTim Haley if (poolsize - deferred < quota) {
1201468c413aSTim Haley quota = poolsize - deferred;
1202468c413aSTim Haley retval = ENOSPC;
1203fa9e4066Sahrens }
1204fa9e4066Sahrens }
1205fa9e4066Sahrens
1206fa9e4066Sahrens /*
1207fa9e4066Sahrens * If they are requesting more space, and our current estimate
1208a9799022Sck153898 * is over quota, they get to try again unless the actual
1209ea8dc4b6Seschrock * on-disk is over quota and there are no pending changes (which
1210ea8dc4b6Seschrock * may free up space for us).
1211fa9e4066Sahrens */
1212*a2f7b05aSArne Jansen if (used_on_disk + (late_edquot ? 0 : est_inflight) >= quota) {
1213*a2f7b05aSArne Jansen if ((late_edquot == 0 && est_inflight > early_edquot_threshold) ||
1214b9f08e63SArne Jansen used_on_disk + early_edquot_threshold < quota ||
1215468c413aSTim Haley (retval == ENOSPC && used_on_disk < quota + deferred))
1216468c413aSTim Haley retval = ERESTART;
1217a9799022Sck153898 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1218fa9e4066Sahrens "quota=%lluK tr=%lluK err=%d\n",
1219a9799022Sck153898 used_on_disk>>10, est_inflight>>10,
1220468c413aSTim Haley quota>>10, asize>>10, retval);
1221fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1222be6fd75aSMatthew Ahrens return (SET_ERROR(retval));
1223fa9e4066Sahrens }
1224fa9e4066Sahrens
1225fa9e4066Sahrens /* We need to up our estimated delta before dropping dd_lock */
1226fa9e4066Sahrens dd->dd_tempreserved[txgidx] += asize;
1227fa9e4066Sahrens
12289082849eSck153898 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
12299082849eSck153898 asize - ref_rsrv);
1230fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1231fa9e4066Sahrens
12321ab7f2deSmaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1233fa9e4066Sahrens tr->tr_ds = dd;
1234fa9e4066Sahrens tr->tr_size = asize;
1235fa9e4066Sahrens list_insert_tail(tr_list, tr);
1236fa9e4066Sahrens
1237fa9e4066Sahrens /* see if it's OK with our parent */
12381934e92fSmaybee if (dd->dd_parent && parent_rsrv) {
1239c1379625SJustin T. Gibbs boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
12401934e92fSmaybee
1241fa9e4066Sahrens return (dsl_dir_tempreserve_impl(dd->dd_parent,
12426df6c3bcSck153898 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1243fa9e4066Sahrens } else {
1244fa9e4066Sahrens return (0);
1245fa9e4066Sahrens }
1246fa9e4066Sahrens }
1247fa9e4066Sahrens
1248fa9e4066Sahrens /*
1249fa9e4066Sahrens * Reserve space in this dsl_dir, to be used in this tx's txg.
1250a9799022Sck153898 * After the space has been dirtied (and dsl_dir_willuse_space()
1251a9799022Sck153898 * has been called), the reservation should be canceled, using
1252a9799022Sck153898 * dsl_dir_tempreserve_clear().
1253fa9e4066Sahrens */
1254fa9e4066Sahrens 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)1255a9799022Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1256a9799022Sck153898 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1257fa9e4066Sahrens {
12581ab7f2deSmaybee int err;
1259fa9e4066Sahrens list_t *tr_list;
1260fa9e4066Sahrens
1261a9799022Sck153898 if (asize == 0) {
1262a9799022Sck153898 *tr_cookiep = NULL;
1263a9799022Sck153898 return (0);
1264a9799022Sck153898 }
1265a9799022Sck153898
1266fa9e4066Sahrens tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1267fa9e4066Sahrens list_create(tr_list, sizeof (struct tempreserve),
1268fa9e4066Sahrens offsetof(struct tempreserve, tr_node));
1269a9799022Sck153898 ASSERT3S(asize, >, 0);
1270ea8dc4b6Seschrock ASSERT3S(fsize, >=, 0);
1271fa9e4066Sahrens
12721ab7f2deSmaybee err = arc_tempreserve_space(lsize, tx->tx_txg);
12731ab7f2deSmaybee if (err == 0) {
12741ab7f2deSmaybee struct tempreserve *tr;
12751ab7f2deSmaybee
12761ab7f2deSmaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
12771ab7f2deSmaybee tr->tr_size = lsize;
12781ab7f2deSmaybee list_insert_tail(tr_list, tr);
12791ab7f2deSmaybee } else {
12801ab7f2deSmaybee if (err == EAGAIN) {
128169962b56SMatthew Ahrens /*
128269962b56SMatthew Ahrens * If arc_memory_throttle() detected that pageout
128369962b56SMatthew Ahrens * is running and we are low on memory, we delay new
128469962b56SMatthew Ahrens * non-pageout transactions to give pageout an
128569962b56SMatthew Ahrens * advantage.
128669962b56SMatthew Ahrens *
128769962b56SMatthew Ahrens * It is unfortunate to be delaying while the caller's
128869962b56SMatthew Ahrens * locks are held.
128969962b56SMatthew Ahrens */
12900689f76cSAdam Leventhal txg_delay(dd->dd_pool, tx->tx_txg,
12910689f76cSAdam Leventhal MSEC2NSEC(10), MSEC2NSEC(10));
1292be6fd75aSMatthew Ahrens err = SET_ERROR(ERESTART);
12931ab7f2deSmaybee }
12941ab7f2deSmaybee }
1295fa9e4066Sahrens
1296fa9e4066Sahrens if (err == 0) {
12971ab7f2deSmaybee err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
12986df6c3bcSck153898 FALSE, asize > usize, tr_list, tx, TRUE);
1299fa9e4066Sahrens }
1300fa9e4066Sahrens
13013b2aab18SMatthew Ahrens if (err != 0)
1302fa9e4066Sahrens dsl_dir_tempreserve_clear(tr_list, tx);
1303fa9e4066Sahrens else
1304fa9e4066Sahrens *tr_cookiep = tr_list;
13051ab7f2deSmaybee
1306fa9e4066Sahrens return (err);
1307fa9e4066Sahrens }
1308fa9e4066Sahrens
1309fa9e4066Sahrens /*
1310fa9e4066Sahrens * Clear a temporary reservation that we previously made with
1311fa9e4066Sahrens * dsl_dir_tempreserve_space().
1312fa9e4066Sahrens */
1313fa9e4066Sahrens void
dsl_dir_tempreserve_clear(void * tr_cookie,dmu_tx_t * tx)1314fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1315fa9e4066Sahrens {
1316fa9e4066Sahrens int txgidx = tx->tx_txg & TXG_MASK;
1317fa9e4066Sahrens list_t *tr_list = tr_cookie;
1318fa9e4066Sahrens struct tempreserve *tr;
1319fa9e4066Sahrens
1320fa9e4066Sahrens ASSERT3U(tx->tx_txg, !=, 0);
1321fa9e4066Sahrens
1322a9799022Sck153898 if (tr_cookie == NULL)
1323a9799022Sck153898 return;
1324a9799022Sck153898
132569962b56SMatthew Ahrens while ((tr = list_head(tr_list)) != NULL) {
132669962b56SMatthew Ahrens if (tr->tr_ds) {
1327fa9e4066Sahrens mutex_enter(&tr->tr_ds->dd_lock);
1328fa9e4066Sahrens ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1329fa9e4066Sahrens tr->tr_size);
1330fa9e4066Sahrens tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1331fa9e4066Sahrens mutex_exit(&tr->tr_ds->dd_lock);
13321ab7f2deSmaybee } else {
13331ab7f2deSmaybee arc_tempreserve_clear(tr->tr_size);
1334fa9e4066Sahrens }
1335fa9e4066Sahrens list_remove(tr_list, tr);
1336fa9e4066Sahrens kmem_free(tr, sizeof (struct tempreserve));
1337fa9e4066Sahrens }
1338fa9e4066Sahrens
1339fa9e4066Sahrens kmem_free(tr_list, sizeof (list_t));
1340fa9e4066Sahrens }
1341fa9e4066Sahrens
134269962b56SMatthew Ahrens /*
134369962b56SMatthew Ahrens * This should be called from open context when we think we're going to write
134469962b56SMatthew Ahrens * or free space, for example when dirtying data. Be conservative; it's okay
134569962b56SMatthew Ahrens * to write less space or free more, but we don't want to write more or free
134669962b56SMatthew Ahrens * less than the amount specified.
134769962b56SMatthew Ahrens */
134869962b56SMatthew Ahrens void
dsl_dir_willuse_space(dsl_dir_t * dd,int64_t space,dmu_tx_t * tx)134969962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1350fa9e4066Sahrens {
1351fa9e4066Sahrens int64_t parent_space;
1352fa9e4066Sahrens uint64_t est_used;
1353fa9e4066Sahrens
1354fa9e4066Sahrens mutex_enter(&dd->dd_lock);
1355fa9e4066Sahrens if (space > 0)
1356fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1357fa9e4066Sahrens
1358c1379625SJustin T. Gibbs est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1359fa9e4066Sahrens parent_space = parent_delta(dd, est_used, space);
1360fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1361fa9e4066Sahrens
1362fa9e4066Sahrens /* Make sure that we clean up dd_space_to* */
1363fa9e4066Sahrens dsl_dir_dirty(dd, tx);
1364fa9e4066Sahrens
1365fa9e4066Sahrens /* XXX this is potentially expensive and unnecessary... */
1366fa9e4066Sahrens if (parent_space && dd->dd_parent)
136769962b56SMatthew Ahrens dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1368fa9e4066Sahrens }
1369fa9e4066Sahrens
1370fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */
1371fa9e4066Sahrens 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)137274e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1373fa9e4066Sahrens int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1374fa9e4066Sahrens {
1375fa9e4066Sahrens int64_t accounted_delta;
1376b62969f8SMatthew Ahrens
1377b62969f8SMatthew Ahrens /*
1378b62969f8SMatthew Ahrens * dsl_dataset_set_refreservation_sync_impl() calls this with
1379b62969f8SMatthew Ahrens * dd_lock held, so that it can atomically update
1380b62969f8SMatthew Ahrens * ds->ds_reserved and the dsl_dir accounting, so that
1381b62969f8SMatthew Ahrens * dsl_dataset_check_quota() can see dataset and dir accounting
1382b62969f8SMatthew Ahrens * consistently.
1383b62969f8SMatthew Ahrens */
138402c8f3f0SMatthew Ahrens boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1385fa9e4066Sahrens
1386fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx));
138774e7dc98SMatthew Ahrens ASSERT(type < DD_USED_NUM);
1388fa9e4066Sahrens
1389b62969f8SMatthew Ahrens dmu_buf_will_dirty(dd->dd_dbuf, tx);
1390b62969f8SMatthew Ahrens
139102c8f3f0SMatthew Ahrens if (needlock)
1392fa9e4066Sahrens mutex_enter(&dd->dd_lock);
1393c1379625SJustin T. Gibbs accounted_delta =
1394c1379625SJustin T. Gibbs parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1395c1379625SJustin T. Gibbs ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1396fa9e4066Sahrens ASSERT(compressed >= 0 ||
1397c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1398fa9e4066Sahrens ASSERT(uncompressed >= 0 ||
1399c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1400c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes += used;
1401c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1402c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
140374e7dc98SMatthew Ahrens
1404c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
140574e7dc98SMatthew Ahrens ASSERT(used > 0 ||
1406c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1407c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
140874e7dc98SMatthew Ahrens #ifdef DEBUG
140974e7dc98SMatthew Ahrens dd_used_t t;
141074e7dc98SMatthew Ahrens uint64_t u = 0;
141174e7dc98SMatthew Ahrens for (t = 0; t < DD_USED_NUM; t++)
1412c1379625SJustin T. Gibbs u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1413c1379625SJustin T. Gibbs ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
141474e7dc98SMatthew Ahrens #endif
141574e7dc98SMatthew Ahrens }
141602c8f3f0SMatthew Ahrens if (needlock)
1417fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1418fa9e4066Sahrens
1419fa9e4066Sahrens if (dd->dd_parent != NULL) {
142074e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1421fa9e4066Sahrens accounted_delta, compressed, uncompressed, tx);
142274e7dc98SMatthew Ahrens dsl_dir_transfer_space(dd->dd_parent,
142374e7dc98SMatthew Ahrens used - accounted_delta,
142474e7dc98SMatthew Ahrens DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1425fa9e4066Sahrens }
1426fa9e4066Sahrens }
1427fa9e4066Sahrens
142874e7dc98SMatthew 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)142974e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
143074e7dc98SMatthew Ahrens dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
143174e7dc98SMatthew Ahrens {
143274e7dc98SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx));
143374e7dc98SMatthew Ahrens ASSERT(oldtype < DD_USED_NUM);
143474e7dc98SMatthew Ahrens ASSERT(newtype < DD_USED_NUM);
143574e7dc98SMatthew Ahrens
1436c1379625SJustin T. Gibbs if (delta == 0 ||
1437c1379625SJustin T. Gibbs !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
143874e7dc98SMatthew Ahrens return;
143974e7dc98SMatthew Ahrens
1440b62969f8SMatthew Ahrens dmu_buf_will_dirty(dd->dd_dbuf, tx);
144174e7dc98SMatthew Ahrens mutex_enter(&dd->dd_lock);
144274e7dc98SMatthew Ahrens ASSERT(delta > 0 ?
1443c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1444c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1445c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1446c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1447c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
144874e7dc98SMatthew Ahrens mutex_exit(&dd->dd_lock);
144974e7dc98SMatthew Ahrens }
145074e7dc98SMatthew Ahrens
14513b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg {
14523b2aab18SMatthew Ahrens const char *ddsqra_name;
14533b2aab18SMatthew Ahrens zprop_source_t ddsqra_source;
14543b2aab18SMatthew Ahrens uint64_t ddsqra_value;
14553b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t;
14563b2aab18SMatthew Ahrens
1457fa9e4066Sahrens static int
dsl_dir_set_quota_check(void * arg,dmu_tx_t * tx)14583b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1459fa9e4066Sahrens {
14603b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg;
14613b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
14623b2aab18SMatthew Ahrens dsl_dataset_t *ds;
14633b2aab18SMatthew Ahrens int error;
14643b2aab18SMatthew Ahrens uint64_t towrite, newval;
14651d452cf5Sahrens
14663b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
14673b2aab18SMatthew Ahrens if (error != 0)
14683b2aab18SMatthew Ahrens return (error);
146992241e0bSTom Erickson
14703b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, "quota",
14713b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
14723b2aab18SMatthew Ahrens if (error != 0) {
14733b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
14743b2aab18SMatthew Ahrens return (error);
14753b2aab18SMatthew Ahrens }
14763b2aab18SMatthew Ahrens
14773b2aab18SMatthew Ahrens if (newval == 0) {
14783b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
14791d452cf5Sahrens return (0);
14803b2aab18SMatthew Ahrens }
14811d452cf5Sahrens
14823b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock);
14831d452cf5Sahrens /*
14841d452cf5Sahrens * If we are doing the preliminary check in open context, and
14851d452cf5Sahrens * there are pending changes, then don't fail it, since the
1486a9799022Sck153898 * pending changes could under-estimate the amount of space to be
14871d452cf5Sahrens * freed up.
14881d452cf5Sahrens */
14893b2aab18SMatthew Ahrens towrite = dsl_dir_space_towrite(ds->ds_dir);
14901d452cf5Sahrens if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1491c1379625SJustin T. Gibbs (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1492c1379625SJustin T. Gibbs newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1493be6fd75aSMatthew Ahrens error = SET_ERROR(ENOSPC);
14941d452cf5Sahrens }
14953b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock);
14963b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
14973b2aab18SMatthew Ahrens return (error);
14981d452cf5Sahrens }
14991d452cf5Sahrens
15001d452cf5Sahrens static void
dsl_dir_set_quota_sync(void * arg,dmu_tx_t * tx)15013b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
15021d452cf5Sahrens {
15033b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg;
15043b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
15053b2aab18SMatthew Ahrens dsl_dataset_t *ds;
15063b2aab18SMatthew Ahrens uint64_t newval;
150792241e0bSTom Erickson
15083b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1509fa9e4066Sahrens
1510013023d4SMartin Matuska if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
15113b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
15123b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
15133b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx);
1514fa9e4066Sahrens
15153b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds,
15163b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1517013023d4SMartin Matuska } else {
1518013023d4SMartin Matuska newval = ddsqra->ddsqra_value;
1519013023d4SMartin Matuska spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1520013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1521013023d4SMartin Matuska }
15223b2aab18SMatthew Ahrens
15233b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
15243b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock);
1525c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
15263b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock);
15273b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
1528fa9e4066Sahrens }
1529fa9e4066Sahrens
1530fa9e4066Sahrens int
dsl_dir_set_quota(const char * ddname,zprop_source_t source,uint64_t quota)153192241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1532fa9e4066Sahrens {
15333b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t ddsqra;
1534fa9e4066Sahrens
15353b2aab18SMatthew Ahrens ddsqra.ddsqra_name = ddname;
15363b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source;
15373b2aab18SMatthew Ahrens ddsqra.ddsqra_value = quota;
153892241e0bSTom Erickson
15393b2aab18SMatthew Ahrens return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
15407d46dc6cSMatthew Ahrens dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1541fa9e4066Sahrens }
1542fa9e4066Sahrens
1543a9799022Sck153898 int
dsl_dir_set_reservation_check(void * arg,dmu_tx_t * tx)15443b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1545fa9e4066Sahrens {
15463b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg;
15473b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
15483b2aab18SMatthew Ahrens dsl_dataset_t *ds;
15493b2aab18SMatthew Ahrens dsl_dir_t *dd;
15503b2aab18SMatthew Ahrens uint64_t newval, used, avail;
15513b2aab18SMatthew Ahrens int error;
155292241e0bSTom Erickson
15533b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
15543b2aab18SMatthew Ahrens if (error != 0)
15553b2aab18SMatthew Ahrens return (error);
15563b2aab18SMatthew Ahrens dd = ds->ds_dir;
1557fa9e4066Sahrens
15581d452cf5Sahrens /*
15591d452cf5Sahrens * If we are doing the preliminary check in open context, the
15601d452cf5Sahrens * space estimates may be inaccurate.
15611d452cf5Sahrens */
15623b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) {
15633b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
15641d452cf5Sahrens return (0);
15653b2aab18SMatthew Ahrens }
15663b2aab18SMatthew Ahrens
15673b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir,
15683b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RESERVATION),
15693b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
15703b2aab18SMatthew Ahrens if (error != 0) {
15713b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
15723b2aab18SMatthew Ahrens return (error);
15733b2aab18SMatthew Ahrens }
15741d452cf5Sahrens
1575fa9e4066Sahrens mutex_enter(&dd->dd_lock);
1576c1379625SJustin T. Gibbs used = dsl_dir_phys(dd)->dd_used_bytes;
1577fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1578fa9e4066Sahrens
1579fa9e4066Sahrens if (dd->dd_parent) {
1580fa9e4066Sahrens avail = dsl_dir_space_available(dd->dd_parent,
1581fa9e4066Sahrens NULL, 0, FALSE);
1582fa9e4066Sahrens } else {
1583fa9e4066Sahrens avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1584fa9e4066Sahrens }
1585fa9e4066Sahrens
1586c1379625SJustin T. Gibbs if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
15873b2aab18SMatthew Ahrens uint64_t delta = MAX(used, newval) -
1588c1379625SJustin T. Gibbs MAX(used, dsl_dir_phys(dd)->dd_reserved);
1589379c004dSEric Schrock
15903b2aab18SMatthew Ahrens if (delta > avail ||
1591c1379625SJustin T. Gibbs (dsl_dir_phys(dd)->dd_quota > 0 &&
1592c1379625SJustin T. Gibbs newval > dsl_dir_phys(dd)->dd_quota))
1593be6fd75aSMatthew Ahrens error = SET_ERROR(ENOSPC);
1594379c004dSEric Schrock }
1595379c004dSEric Schrock
15963b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
15973b2aab18SMatthew Ahrens return (error);
15981d452cf5Sahrens }
15991d452cf5Sahrens
16003b2aab18SMatthew Ahrens void
dsl_dir_set_reservation_sync_impl(dsl_dir_t * dd,uint64_t value,dmu_tx_t * tx)16014445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
16021d452cf5Sahrens {
16031d452cf5Sahrens uint64_t used;
16041d452cf5Sahrens int64_t delta;
16051d452cf5Sahrens
1606a9799022Sck153898 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1607a9799022Sck153898
16081d452cf5Sahrens mutex_enter(&dd->dd_lock);
1609c1379625SJustin T. Gibbs used = dsl_dir_phys(dd)->dd_used_bytes;
1610c1379625SJustin T. Gibbs delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1611c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_reserved = value;
1612fa9e4066Sahrens
1613fa9e4066Sahrens if (dd->dd_parent != NULL) {
1614fa9e4066Sahrens /* Roll up this additional usage into our ancestors */
161574e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
161674e7dc98SMatthew Ahrens delta, 0, 0, tx);
1617fa9e4066Sahrens }
161802c8f3f0SMatthew Ahrens mutex_exit(&dd->dd_lock);
16194445fffbSMatthew Ahrens }
1620ecd6cf80Smarks
16214445fffbSMatthew Ahrens
16224445fffbSMatthew Ahrens static void
dsl_dir_set_reservation_sync(void * arg,dmu_tx_t * tx)16233b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
16244445fffbSMatthew Ahrens {
16253b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg;
16263b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
16273b2aab18SMatthew Ahrens dsl_dataset_t *ds;
16283b2aab18SMatthew Ahrens uint64_t newval;
16294445fffbSMatthew Ahrens
16303b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
16314445fffbSMatthew Ahrens
1632013023d4SMartin Matuska if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1633013023d4SMartin Matuska dsl_prop_set_sync_impl(ds,
1634013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION),
16353b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
16363b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx);
16373b2aab18SMatthew Ahrens
16383b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds,
16393b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1640013023d4SMartin Matuska } else {
1641013023d4SMartin Matuska newval = ddsqra->ddsqra_value;
1642013023d4SMartin Matuska spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1643013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION),
1644013023d4SMartin Matuska (longlong_t)newval);
1645013023d4SMartin Matuska }
16463b2aab18SMatthew Ahrens
16473b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
16483b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG);
1649fa9e4066Sahrens }
1650fa9e4066Sahrens
1651fa9e4066Sahrens int
dsl_dir_set_reservation(const char * ddname,zprop_source_t source,uint64_t reservation)165292241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
165392241e0bSTom Erickson uint64_t reservation)
1654fa9e4066Sahrens {
16553b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t ddsqra;
1656fa9e4066Sahrens
16573b2aab18SMatthew Ahrens ddsqra.ddsqra_name = ddname;
16583b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source;
16593b2aab18SMatthew Ahrens ddsqra.ddsqra_value = reservation;
166092241e0bSTom Erickson
16613b2aab18SMatthew Ahrens return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
16627d46dc6cSMatthew Ahrens dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1663fa9e4066Sahrens }
1664fa9e4066Sahrens
1665fa9e4066Sahrens static dsl_dir_t *
closest_common_ancestor(dsl_dir_t * ds1,dsl_dir_t * ds2)1666fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1667fa9e4066Sahrens {
1668fa9e4066Sahrens for (; ds1; ds1 = ds1->dd_parent) {
1669fa9e4066Sahrens dsl_dir_t *dd;
1670fa9e4066Sahrens for (dd = ds2; dd; dd = dd->dd_parent) {
1671fa9e4066Sahrens if (ds1 == dd)
1672fa9e4066Sahrens return (dd);
1673fa9e4066Sahrens }
1674fa9e4066Sahrens }
1675fa9e4066Sahrens return (NULL);
1676fa9e4066Sahrens }
1677fa9e4066Sahrens
1678fa9e4066Sahrens /*
1679fa9e4066Sahrens * If delta is applied to dd, how much of that delta would be applied to
1680fa9e4066Sahrens * ancestor? Syncing context only.
1681fa9e4066Sahrens */
1682fa9e4066Sahrens static int64_t
would_change(dsl_dir_t * dd,int64_t delta,dsl_dir_t * ancestor)1683fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1684fa9e4066Sahrens {
1685fa9e4066Sahrens if (dd == ancestor)
1686fa9e4066Sahrens return (delta);
1687fa9e4066Sahrens
1688fa9e4066Sahrens mutex_enter(&dd->dd_lock);
1689c1379625SJustin T. Gibbs delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1690fa9e4066Sahrens mutex_exit(&dd->dd_lock);
1691fa9e4066Sahrens return (would_change(dd->dd_parent, delta, ancestor));
1692fa9e4066Sahrens }
1693fa9e4066Sahrens
16943b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg {
16953b2aab18SMatthew Ahrens const char *ddra_oldname;
16963b2aab18SMatthew Ahrens const char *ddra_newname;
1697a2afb611SJerry Jelinek cred_t *ddra_cred;
16983b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t;
16993b2aab18SMatthew Ahrens
17003b2aab18SMatthew Ahrens /* ARGSUSED */
17013b2aab18SMatthew Ahrens static int
dsl_valid_rename(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)17023b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
17033b2aab18SMatthew Ahrens {
17043b2aab18SMatthew Ahrens int *deltap = arg;
170540a5c998SMatthew Ahrens char namebuf[ZFS_MAX_DATASET_NAME_LEN];
17063b2aab18SMatthew Ahrens
17073b2aab18SMatthew Ahrens dsl_dataset_name(ds, namebuf);
17083b2aab18SMatthew Ahrens
170940a5c998SMatthew Ahrens if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN)
1710be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG));
17113b2aab18SMatthew Ahrens return (0);
17123b2aab18SMatthew Ahrens }
17131d452cf5Sahrens
17141d452cf5Sahrens static int
dsl_dir_rename_check(void * arg,dmu_tx_t * tx)17153b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1716fa9e4066Sahrens {
17173b2aab18SMatthew Ahrens dsl_dir_rename_arg_t *ddra = arg;
17183b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
17193b2aab18SMatthew Ahrens dsl_dir_t *dd, *newparent;
17203b2aab18SMatthew Ahrens const char *mynewname;
17213b2aab18SMatthew Ahrens int error;
17223b2aab18SMatthew Ahrens int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1723fa9e4066Sahrens
17243b2aab18SMatthew Ahrens /* target dir should exist */
17253b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
17263b2aab18SMatthew Ahrens if (error != 0)
17273b2aab18SMatthew Ahrens return (error);
1728fa9e4066Sahrens
17293b2aab18SMatthew Ahrens /* new parent should exist */
17303b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
17313b2aab18SMatthew Ahrens &newparent, &mynewname);
17323b2aab18SMatthew Ahrens if (error != 0) {
17333b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
17343b2aab18SMatthew Ahrens return (error);
17353b2aab18SMatthew Ahrens }
17363b2aab18SMatthew Ahrens
17373b2aab18SMatthew Ahrens /* can't rename to different pool */
17383b2aab18SMatthew Ahrens if (dd->dd_pool != newparent->dd_pool) {
17393b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG);
17403b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
1741be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO));
17423b2aab18SMatthew Ahrens }
17433b2aab18SMatthew Ahrens
17443b2aab18SMatthew Ahrens /* new name should not already exist */
17453b2aab18SMatthew Ahrens if (mynewname == NULL) {
17463b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG);
17473b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
1748be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST));
17493b2aab18SMatthew Ahrens }
17501d452cf5Sahrens
17513b2aab18SMatthew Ahrens /* if the name length is growing, validate child name lengths */
17523b2aab18SMatthew Ahrens if (delta > 0) {
17533b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
17543b2aab18SMatthew Ahrens &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
17553b2aab18SMatthew Ahrens if (error != 0) {
17563b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG);
17573b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
17583b2aab18SMatthew Ahrens return (error);
17593b2aab18SMatthew Ahrens }
17603b2aab18SMatthew Ahrens }
17613b2aab18SMatthew Ahrens
1762a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) {
1763adf34077SJerry Jelinek if (spa_feature_is_active(dp->dp_spa,
1764a2afb611SJerry Jelinek SPA_FEATURE_FS_SS_LIMIT)) {
1765a2afb611SJerry Jelinek /*
1766a2afb611SJerry Jelinek * Although this is the check function and we don't
1767a2afb611SJerry Jelinek * normally make on-disk changes in check functions,
1768a2afb611SJerry Jelinek * we need to do that here.
1769a2afb611SJerry Jelinek *
1770a2afb611SJerry Jelinek * Ensure this portion of the tree's counts have been
1771a2afb611SJerry Jelinek * initialized in case the new parent has limits set.
1772a2afb611SJerry Jelinek */
1773a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dd, tx);
1774a2afb611SJerry Jelinek }
1775a2afb611SJerry Jelinek }
1776a2afb611SJerry Jelinek
17773b2aab18SMatthew Ahrens if (newparent != dd->dd_parent) {
177899653d4eSeschrock /* is there enough space? */
177999653d4eSeschrock uint64_t myspace =
1780c1379625SJustin T. Gibbs MAX(dsl_dir_phys(dd)->dd_used_bytes,
1781c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_reserved);
1782a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset;
1783a2afb611SJerry Jelinek uint64_t fs_cnt = 0;
1784a2afb611SJerry Jelinek uint64_t ss_cnt = 0;
1785a2afb611SJerry Jelinek
1786a2afb611SJerry Jelinek if (dsl_dir_is_zapified(dd)) {
1787a2afb611SJerry Jelinek int err;
1788a2afb611SJerry Jelinek
1789a2afb611SJerry Jelinek err = zap_lookup(os, dd->dd_object,
1790a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1791a2afb611SJerry Jelinek &fs_cnt);
1792adf34077SJerry Jelinek if (err != ENOENT && err != 0) {
1793adf34077SJerry Jelinek dsl_dir_rele(newparent, FTAG);
1794adf34077SJerry Jelinek dsl_dir_rele(dd, FTAG);
1795a2afb611SJerry Jelinek return (err);
1796adf34077SJerry Jelinek }
1797a2afb611SJerry Jelinek
1798a2afb611SJerry Jelinek /*
1799a2afb611SJerry Jelinek * have to add 1 for the filesystem itself that we're
1800a2afb611SJerry Jelinek * moving
1801a2afb611SJerry Jelinek */
1802a2afb611SJerry Jelinek fs_cnt++;
1803a2afb611SJerry Jelinek
1804a2afb611SJerry Jelinek err = zap_lookup(os, dd->dd_object,
1805a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1806a2afb611SJerry Jelinek &ss_cnt);
1807adf34077SJerry Jelinek if (err != ENOENT && err != 0) {
1808adf34077SJerry Jelinek dsl_dir_rele(newparent, FTAG);
1809adf34077SJerry Jelinek dsl_dir_rele(dd, FTAG);
1810a2afb611SJerry Jelinek return (err);
1811a2afb611SJerry Jelinek }
1812adf34077SJerry Jelinek }
1813fa9e4066Sahrens
18141d452cf5Sahrens /* no rename into our descendant */
18153b2aab18SMatthew Ahrens if (closest_common_ancestor(dd, newparent) == dd) {
18163b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG);
18173b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
1818be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
1819fa9e4066Sahrens }
1820fa9e4066Sahrens
18213b2aab18SMatthew Ahrens error = dsl_dir_transfer_possible(dd->dd_parent,
1822a2afb611SJerry Jelinek newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
18233b2aab18SMatthew Ahrens if (error != 0) {
18243b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG);
18253b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
18263b2aab18SMatthew Ahrens return (error);
18273b2aab18SMatthew Ahrens }
18283b2aab18SMatthew Ahrens }
18293b2aab18SMatthew Ahrens
18303b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG);
18313b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
18321d452cf5Sahrens return (0);
18331d452cf5Sahrens }
18341d452cf5Sahrens
18351d452cf5Sahrens static void
dsl_dir_rename_sync(void * arg,dmu_tx_t * tx)18363b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
18371d452cf5Sahrens {
18383b2aab18SMatthew Ahrens dsl_dir_rename_arg_t *ddra = arg;
18393b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
18403b2aab18SMatthew Ahrens dsl_dir_t *dd, *newparent;
18413b2aab18SMatthew Ahrens const char *mynewname;
18423b2aab18SMatthew Ahrens int error;
18431d452cf5Sahrens objset_t *mos = dp->dp_meta_objset;
18441d452cf5Sahrens
18453b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
18463b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
18473b2aab18SMatthew Ahrens &mynewname));
18481d452cf5Sahrens
18494445fffbSMatthew Ahrens /* Log this before we change the name. */
18504445fffbSMatthew Ahrens spa_history_log_internal_dd(dd, "rename", tx,
18513b2aab18SMatthew Ahrens "-> %s", ddra->ddra_newname);
18524445fffbSMatthew Ahrens
18533b2aab18SMatthew Ahrens if (newparent != dd->dd_parent) {
1854a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset;
1855a2afb611SJerry Jelinek uint64_t fs_cnt = 0;
1856a2afb611SJerry Jelinek uint64_t ss_cnt = 0;
1857a2afb611SJerry Jelinek
1858a2afb611SJerry Jelinek /*
1859a2afb611SJerry Jelinek * We already made sure the dd counts were initialized in the
1860a2afb611SJerry Jelinek * check function.
1861a2afb611SJerry Jelinek */
1862adf34077SJerry Jelinek if (spa_feature_is_active(dp->dp_spa,
1863a2afb611SJerry Jelinek SPA_FEATURE_FS_SS_LIMIT)) {
1864a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, dd->dd_object,
1865a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1866a2afb611SJerry Jelinek &fs_cnt));
1867a2afb611SJerry Jelinek /* add 1 for the filesystem itself that we're moving */
1868a2afb611SJerry Jelinek fs_cnt++;
1869a2afb611SJerry Jelinek
1870a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, dd->dd_object,
1871a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1872a2afb611SJerry Jelinek &ss_cnt));
1873a2afb611SJerry Jelinek }
1874a2afb611SJerry Jelinek
1875a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1876a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx);
1877a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(newparent, fs_cnt,
1878a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx);
1879a2afb611SJerry Jelinek
1880a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1881a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx);
1882a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(newparent, ss_cnt,
1883a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx);
1884a2afb611SJerry Jelinek
188574e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1886c1379625SJustin T. Gibbs -dsl_dir_phys(dd)->dd_used_bytes,
1887c1379625SJustin T. Gibbs -dsl_dir_phys(dd)->dd_compressed_bytes,
1888c1379625SJustin T. Gibbs -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
18893b2aab18SMatthew Ahrens dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1890c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes,
1891c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes,
1892c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
189374e7dc98SMatthew Ahrens
1894c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_reserved >
1895c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes) {
1896c1379625SJustin T. Gibbs uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1897c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes;
189874e7dc98SMatthew Ahrens
189974e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
190074e7dc98SMatthew Ahrens -unused_rsrv, 0, 0, tx);
19013b2aab18SMatthew Ahrens dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
190274e7dc98SMatthew Ahrens unused_rsrv, 0, 0, tx);
190374e7dc98SMatthew Ahrens }
1904fa9e4066Sahrens }
1905fa9e4066Sahrens
1906fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx);
1907fa9e4066Sahrens
1908fa9e4066Sahrens /* remove from old parent zapobj */
1909c1379625SJustin T. Gibbs error = zap_remove(mos,
1910c1379625SJustin T. Gibbs dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1911fa9e4066Sahrens dd->dd_myname, tx);
19123b2aab18SMatthew Ahrens ASSERT0(error);
1913fa9e4066Sahrens
19143b2aab18SMatthew Ahrens (void) strcpy(dd->dd_myname, mynewname);
19153b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd);
1916c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
19173b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp,
19183b2aab18SMatthew Ahrens newparent->dd_object, NULL, dd, &dd->dd_parent));
1919fa9e4066Sahrens
1920fa9e4066Sahrens /* add to new parent zapobj */
1921c1379625SJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
19223b2aab18SMatthew Ahrens dd->dd_myname, 8, 1, &dd->dd_object, tx));
1923ecd6cf80Smarks
19243b2aab18SMatthew Ahrens dsl_prop_notify_all(dd);
19253b2aab18SMatthew Ahrens
19263b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG);
19273b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG);
19281d452cf5Sahrens }
1929fa9e4066Sahrens
19301d452cf5Sahrens int
dsl_dir_rename(const char * oldname,const char * newname)19313b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname)
19321d452cf5Sahrens {
19333b2aab18SMatthew Ahrens dsl_dir_rename_arg_t ddra;
19341d452cf5Sahrens
19353b2aab18SMatthew Ahrens ddra.ddra_oldname = oldname;
19363b2aab18SMatthew Ahrens ddra.ddra_newname = newname;
1937a2afb611SJerry Jelinek ddra.ddra_cred = CRED();
19381d452cf5Sahrens
19393b2aab18SMatthew Ahrens return (dsl_sync_task(oldname,
19407d46dc6cSMatthew Ahrens dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
19417d46dc6cSMatthew Ahrens 3, ZFS_SPACE_CHECK_RESERVED));
1942fa9e4066Sahrens }
194399653d4eSeschrock
194499653d4eSeschrock 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)1945a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1946a2afb611SJerry Jelinek uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
194799653d4eSeschrock {
194899653d4eSeschrock dsl_dir_t *ancestor;
194999653d4eSeschrock int64_t adelta;
195099653d4eSeschrock uint64_t avail;
1951a2afb611SJerry Jelinek int err;
195299653d4eSeschrock
195399653d4eSeschrock ancestor = closest_common_ancestor(sdd, tdd);
195499653d4eSeschrock adelta = would_change(sdd, -space, ancestor);
195599653d4eSeschrock avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
195699653d4eSeschrock if (avail < space)
1957be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC));
195899653d4eSeschrock
1959a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1960a2afb611SJerry Jelinek ancestor, cr);
1961a2afb611SJerry Jelinek if (err != 0)
1962a2afb611SJerry Jelinek return (err);
1963a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1964a2afb611SJerry Jelinek ancestor, cr);
1965a2afb611SJerry Jelinek if (err != 0)
1966a2afb611SJerry Jelinek return (err);
1967a2afb611SJerry Jelinek
196899653d4eSeschrock return (0);
196999653d4eSeschrock }
197071eb0538SChris Kirby
197171eb0538SChris Kirby timestruc_t
dsl_dir_snap_cmtime(dsl_dir_t * dd)197271eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd)
197371eb0538SChris Kirby {
197471eb0538SChris Kirby timestruc_t t;
197571eb0538SChris Kirby
197671eb0538SChris Kirby mutex_enter(&dd->dd_lock);
197771eb0538SChris Kirby t = dd->dd_snap_cmtime;
197871eb0538SChris Kirby mutex_exit(&dd->dd_lock);
197971eb0538SChris Kirby
198071eb0538SChris Kirby return (t);
198171eb0538SChris Kirby }
198271eb0538SChris Kirby
198371eb0538SChris Kirby void
dsl_dir_snap_cmtime_update(dsl_dir_t * dd)198471eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
198571eb0538SChris Kirby {
198671eb0538SChris Kirby timestruc_t t;
198771eb0538SChris Kirby
198871eb0538SChris Kirby gethrestime(&t);
198971eb0538SChris Kirby mutex_enter(&dd->dd_lock);
199071eb0538SChris Kirby dd->dd_snap_cmtime = t;
199171eb0538SChris Kirby mutex_exit(&dd->dd_lock);
199271eb0538SChris Kirby }
19932acef22dSMatthew Ahrens
19942acef22dSMatthew Ahrens void
dsl_dir_zapify(dsl_dir_t * dd,dmu_tx_t * tx)19952acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
19962acef22dSMatthew Ahrens {
19972acef22dSMatthew Ahrens objset_t *mos = dd->dd_pool->dp_meta_objset;
19982acef22dSMatthew Ahrens dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
19992acef22dSMatthew Ahrens }
2000a2afb611SJerry Jelinek
2001a2afb611SJerry Jelinek boolean_t
dsl_dir_is_zapified(dsl_dir_t * dd)2002a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd)
2003a2afb611SJerry Jelinek {
2004a2afb611SJerry Jelinek dmu_object_info_t doi;
2005a2afb611SJerry Jelinek
2006a2afb611SJerry Jelinek dmu_object_info_from_db(dd->dd_dbuf, &doi);
2007a2afb611SJerry Jelinek return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2008a2afb611SJerry Jelinek }
2009