4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Martin Matuska. All rights reserved.
25 * Copyright (c) 2014 Joyent, Inc. All rights reserved.
29 #include <sys/dmu_objset.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dsl_dataset.h>
32 #include <sys/dsl_dir.h>
33 #include <sys/dsl_prop.h>
34 #include <sys/dsl_synctask.h>
35 #include <sys/dsl_deleg.h>
36 #include <sys/dmu_impl.h>
38 #include <sys/metaslab.h>
42 #include <sys/sunddi.h>
43 #include <sys/zfeature.h>
44 #include <sys/policy.h>
45 #include <sys/zfs_znode.h>
47 #include "zfs_namecheck.h"
51 * Filesystem and Snapshot Limits
52 * ------------------------------
54 * These limits are used to restrict the number of filesystems and/or snapshots
55 * that can be created at a given level in the tree or below. A typical
56 * use-case is with a delegated dataset where the administrator wants to ensure
57 * that a user within the zone is not creating too many additional filesystems
58 * or snapshots, even though they're not exceeding their space quota.
60 * The filesystem and snapshot counts are stored as extensible properties. This
61 * capability is controlled by a feature flag and must be enabled to be used.
62 * Once enabled, the feature is not active until the first limit is set. At
63 * that point, future operations to create/destroy filesystems or snapshots
64 * will validate and update the counts.
66 * Because the count properties will not exist before the feature is active,
67 * the counts are updated when a limit is first set on an uninitialized
68 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
69 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
70 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
71 * snapshot count properties on a node indicate uninitialized counts on that
72 * node.) When first setting a limit on an uninitialized node, the code starts
73 * at the filesystem with the new limit and descends into all sub-filesystems
74 * to add the count properties.
76 * In practice this is lightweight since a limit is typically set when the
77 * filesystem is created and thus has no children. Once valid, changing the
78 * limit value won't require a re-traversal since the counts are already valid.
79 * When recursively fixing the counts, if a node with a limit is encountered
80 * during the descent, the counts are known to be valid and there is no need to
81 * descend into that filesystem's children. The counts on filesystems above the
82 * one with the new limit will still be uninitialized, unless a limit is
83 * eventually set on one of those filesystems. The counts are always recursively
84 * updated when a limit is set on a dataset, unless there is already a limit.
85 * When a new limit value is set on a filesystem with an existing limit, it is
86 * possible for the new limit to be less than the current count at that level
87 * since a user who can change the limit is also allowed to exceed the limit.
89 * Once the feature is active, then whenever a filesystem or snapshot is
90 * created, the code recurses up the tree, validating the new count against the
91 * limit at each initialized level. In practice, most levels will not have a
92 * limit set. If there is a limit at any initialized level up the tree, the
93 * check must pass or the creation will fail. Likewise, when a filesystem or
94 * snapshot is destroyed, the counts are recursively adjusted all the way up
95 * the initizized nodes in the tree. Renaming a filesystem into different point
96 * in the tree will first validate, then update the counts on each branch up to
97 * the common ancestor. A receive will also validate the counts and then update
100 * An exception to the above behavior is that the limit is not enforced if the
101 * user has permission to modify the limit. This is primarily so that
102 * recursive snapshots in the global zone always work. We want to prevent a
103 * denial-of-service in which a lower level delegated dataset could max out its
104 * limit and thus block recursive snapshots from being taken in the global zone.
105 * Because of this, it is possible for the snapshot count to be over the limit
106 * and snapshots taken in the global zone could cause a lower level dataset to
107 * hit or exceed its limit. The administrator taking the global zone recursive
108 * snapshot should be aware of this side-effect and behave accordingly.
109 * For consistency, the filesystem limit is also not enforced if the user can
112 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
113 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
114 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
115 * dsl_dir_init_fs_ss_count().
117 * There is a special case when we receive a filesystem that already exists. In
118 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
119 * never update the filesystem counts for temporary clones.
121 * Likewise, we do not update the snapshot counts for temporary snapshots,
122 * such as those created by zfs diff.
125 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
127 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
131 dsl_dir_evict(dmu_buf_t *db, void *arg)
135 ASSERTV(dsl_pool_t *dp = dd->dd_pool);
137 for (t = 0; t < TXG_SIZE; t++) {
138 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
139 ASSERT(dd->dd_tempreserved[t] == 0);
140 ASSERT(dd->dd_space_towrite[t] == 0);
144 dsl_dir_rele(dd->dd_parent, dd);
146 spa_close(dd->dd_pool->dp_spa, dd);
149 * The props callback list should have been cleaned up by
152 list_destroy(&dd->dd_prop_cbs);
153 mutex_destroy(&dd->dd_lock);
154 kmem_free(dd, sizeof (dsl_dir_t));
158 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
159 const char *tail, void *tag, dsl_dir_t **ddp)
165 ASSERT(dsl_pool_config_held(dp));
167 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
170 dd = dmu_buf_get_user(dbuf);
173 dmu_object_info_t doi;
174 dmu_object_info_from_db(dbuf, &doi);
175 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
176 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
182 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
183 dd->dd_object = ddobj;
186 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
188 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
189 offsetof(dsl_prop_cb_record_t, cbr_node));
191 dsl_dir_snap_cmtime_update(dd);
193 if (dsl_dir_phys(dd)->dd_parent_obj) {
194 err = dsl_dir_hold_obj(dp,
195 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
203 err = zap_lookup(dp->dp_meta_objset,
204 dsl_dir_phys(dd->dd_parent)->
205 dd_child_dir_zapobj, tail,
206 sizeof (foundobj), 1, &foundobj);
207 ASSERT(err || foundobj == ddobj);
209 (void) strcpy(dd->dd_myname, tail);
211 err = zap_value_search(dp->dp_meta_objset,
212 dsl_dir_phys(dd->dd_parent)->
214 ddobj, 0, dd->dd_myname);
219 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
222 if (dsl_dir_is_clone(dd)) {
223 dmu_buf_t *origin_bonus;
224 dsl_dataset_phys_t *origin_phys;
227 * We can't open the origin dataset, because
228 * that would require opening this dsl_dir.
229 * Just look at its phys directly instead.
231 err = dmu_bonus_hold(dp->dp_meta_objset,
232 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
236 origin_phys = origin_bonus->db_data;
238 origin_phys->ds_creation_txg;
239 dmu_buf_rele(origin_bonus, FTAG);
242 winner = dmu_buf_set_user_ie(dbuf, dd, dsl_dir_evict);
245 dsl_dir_rele(dd->dd_parent, dd);
246 mutex_destroy(&dd->dd_lock);
247 kmem_free(dd, sizeof (dsl_dir_t));
250 spa_open_ref(dp->dp_spa, dd);
255 * The dsl_dir_t has both open-to-close and instantiate-to-evict
256 * holds on the spa. We need the open-to-close holds because
257 * otherwise the spa_refcnt wouldn't change when we open a
258 * dir which the spa also has open, so we could incorrectly
259 * think it was OK to unload/export/destroy the pool. We need
260 * the instantiate-to-evict hold because the dsl_dir_t has a
261 * pointer to the dd_pool, which has a pointer to the spa_t.
263 spa_open_ref(dp->dp_spa, tag);
264 ASSERT3P(dd->dd_pool, ==, dp);
265 ASSERT3U(dd->dd_object, ==, ddobj);
266 ASSERT3P(dd->dd_dbuf, ==, dbuf);
272 dsl_dir_rele(dd->dd_parent, dd);
273 mutex_destroy(&dd->dd_lock);
274 kmem_free(dd, sizeof (dsl_dir_t));
275 dmu_buf_rele(dbuf, tag);
280 dsl_dir_rele(dsl_dir_t *dd, void *tag)
282 dprintf_dd(dd, "%s\n", "");
283 spa_close(dd->dd_pool->dp_spa, tag);
284 dmu_buf_rele(dd->dd_dbuf, tag);
287 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
289 dsl_dir_name(dsl_dir_t *dd, char *buf)
292 dsl_dir_name(dd->dd_parent, buf);
293 (void) strcat(buf, "/");
297 if (!MUTEX_HELD(&dd->dd_lock)) {
299 * recursive mutex so that we can use
300 * dprintf_dd() with dd_lock held
302 mutex_enter(&dd->dd_lock);
303 (void) strcat(buf, dd->dd_myname);
304 mutex_exit(&dd->dd_lock);
306 (void) strcat(buf, dd->dd_myname);
310 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
312 dsl_dir_namelen(dsl_dir_t *dd)
317 /* parent's name + 1 for the "/" */
318 result = dsl_dir_namelen(dd->dd_parent) + 1;
321 if (!MUTEX_HELD(&dd->dd_lock)) {
322 /* see dsl_dir_name */
323 mutex_enter(&dd->dd_lock);
324 result += strlen(dd->dd_myname);
325 mutex_exit(&dd->dd_lock);
327 result += strlen(dd->dd_myname);
334 getcomponent(const char *path, char *component, const char **nextp)
338 if ((path == NULL) || (path[0] == '\0'))
339 return (SET_ERROR(ENOENT));
340 /* This would be a good place to reserve some namespace... */
341 p = strpbrk(path, "/@");
342 if (p && (p[1] == '/' || p[1] == '@')) {
343 /* two separators in a row */
344 return (SET_ERROR(EINVAL));
346 if (p == NULL || p == path) {
348 * if the first thing is an @ or /, it had better be an
349 * @ and it had better not have any more ats or slashes,
350 * and it had better have something after the @.
353 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
354 return (SET_ERROR(EINVAL));
355 if (strlen(path) >= MAXNAMELEN)
356 return (SET_ERROR(ENAMETOOLONG));
357 (void) strcpy(component, path);
359 } else if (p[0] == '/') {
360 if (p - path >= MAXNAMELEN)
361 return (SET_ERROR(ENAMETOOLONG));
362 (void) strncpy(component, path, p - path);
363 component[p - path] = '\0';
365 } else if (p[0] == '@') {
367 * if the next separator is an @, there better not be
370 if (strchr(path, '/'))
371 return (SET_ERROR(EINVAL));
372 if (p - path >= MAXNAMELEN)
373 return (SET_ERROR(ENAMETOOLONG));
374 (void) strncpy(component, path, p - path);
375 component[p - path] = '\0';
377 panic("invalid p=%p", (void *)p);
384 * Return the dsl_dir_t, and possibly the last component which couldn't
385 * be found in *tail. The name must be in the specified dsl_pool_t. This
386 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
387 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
388 * (*tail)[0] == '@' means that the last component is a snapshot.
391 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
392 dsl_dir_t **ddp, const char **tailp)
395 const char *spaname, *next, *nextnext = NULL;
400 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
401 err = getcomponent(name, buf, &next);
405 /* Make sure the name is in the specified pool. */
406 spaname = spa_name(dp->dp_spa);
407 if (strcmp(buf, spaname) != 0) {
408 err = SET_ERROR(EXDEV);
412 ASSERT(dsl_pool_config_held(dp));
414 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
419 while (next != NULL) {
421 err = getcomponent(next, buf, &nextnext);
424 ASSERT(next[0] != '\0');
427 dprintf("looking up %s in obj%lld\n",
428 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
430 err = zap_lookup(dp->dp_meta_objset,
431 dsl_dir_phys(dd)->dd_child_dir_zapobj,
432 buf, sizeof (ddobj), 1, &ddobj);
439 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_ds);
442 dsl_dir_rele(dd, tag);
448 dsl_dir_rele(dd, tag);
453 * It's an error if there's more than one component left, or
454 * tailp==NULL and there's any component left.
457 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
459 dsl_dir_rele(dd, tag);
460 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
461 err = SET_ERROR(ENOENT);
467 kmem_free(buf, MAXNAMELEN);
472 * If the counts are already initialized for this filesystem and its
473 * descendants then do nothing, otherwise initialize the counts.
475 * The counts on this filesystem, and those below, may be uninitialized due to
476 * either the use of a pre-existing pool which did not support the
477 * filesystem/snapshot limit feature, or one in which the feature had not yet
480 * Recursively descend the filesystem tree and update the filesystem/snapshot
481 * counts on each filesystem below, then update the cumulative count on the
482 * current filesystem. If the filesystem already has a count set on it,
483 * then we know that its counts, and the counts on the filesystems below it,
484 * are already correct, so we don't have to update this filesystem.
487 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
489 uint64_t my_fs_cnt = 0;
490 uint64_t my_ss_cnt = 0;
491 dsl_pool_t *dp = dd->dd_pool;
492 objset_t *os = dp->dp_meta_objset;
497 ASSERT(spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
498 ASSERT(dsl_pool_config_held(dp));
499 ASSERT(dmu_tx_is_syncing(tx));
501 dsl_dir_zapify(dd, tx);
504 * If the filesystem count has already been initialized then we
505 * don't need to recurse down any further.
507 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
510 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
511 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
513 /* Iterate my child dirs */
514 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
515 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
519 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
523 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
524 * temporary datasets.
526 if (chld_dd->dd_myname[0] == '$' ||
527 chld_dd->dd_myname[0] == '%') {
528 dsl_dir_rele(chld_dd, FTAG);
532 my_fs_cnt++; /* count this child */
534 dsl_dir_init_fs_ss_count(chld_dd, tx);
536 VERIFY0(zap_lookup(os, chld_dd->dd_object,
537 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
539 VERIFY0(zap_lookup(os, chld_dd->dd_object,
540 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
543 dsl_dir_rele(chld_dd, FTAG);
546 /* Count my snapshots (we counted children's snapshots above) */
547 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
548 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
550 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
551 zap_cursor_retrieve(zc, za) == 0;
552 zap_cursor_advance(zc)) {
553 /* Don't count temporary snapshots */
554 if (za->za_name[0] != '%')
559 dsl_dataset_rele(ds, FTAG);
561 kmem_free(zc, sizeof (zap_cursor_t));
562 kmem_free(za, sizeof (zap_attribute_t));
564 /* we're in a sync task, update counts */
565 dmu_buf_will_dirty(dd->dd_dbuf, tx);
566 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
567 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
568 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
569 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
573 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
575 char *ddname = (char *)arg;
576 dsl_pool_t *dp = dmu_tx_pool(tx);
581 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
585 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
586 dsl_dataset_rele(ds, FTAG);
587 return (SET_ERROR(ENOTSUP));
591 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
592 dsl_dir_is_zapified(dd) &&
593 zap_contains(dp->dp_meta_objset, dd->dd_object,
594 DD_FIELD_FILESYSTEM_COUNT) == 0) {
595 dsl_dataset_rele(ds, FTAG);
596 return (SET_ERROR(EALREADY));
599 dsl_dataset_rele(ds, FTAG);
604 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
606 char *ddname = (char *)arg;
607 dsl_pool_t *dp = dmu_tx_pool(tx);
611 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
613 spa = dsl_dataset_get_spa(ds);
615 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
617 * Since the feature was not active and we're now setting a
618 * limit, increment the feature-active counter so that the
619 * feature becomes active for the first time.
621 * We are already in a sync task so we can update the MOS.
623 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
627 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
628 * we need to ensure the counts are correct. Descend down the tree from
629 * this point and update all of the counts to be accurate.
631 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
633 dsl_dataset_rele(ds, FTAG);
637 * Make sure the feature is enabled and activate it if necessary.
638 * Since we're setting a limit, ensure the on-disk counts are valid.
639 * This is only called by the ioctl path when setting a limit value.
641 * We do not need to validate the new limit, since users who can change the
642 * limit are also allowed to exceed the limit.
645 dsl_dir_activate_fs_ss_limit(const char *ddname)
649 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
650 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0);
652 if (error == EALREADY)
659 * Used to determine if the filesystem_limit or snapshot_limit should be
660 * enforced. We allow the limit to be exceeded if the user has permission to
661 * write the property value. We pass in the creds that we got in the open
662 * context since we will always be the GZ root in syncing context. We also have
663 * to handle the case where we are allowed to change the limit on the current
664 * dataset, but there may be another limit in the tree above.
666 * We can never modify these two properties within a non-global zone. In
667 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
668 * can't use that function since we are already holding the dp_config_rwlock.
669 * In addition, we already have the dd and dealing with snapshots is simplified
680 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
682 enforce_res_t enforce = ENFORCE_ALWAYS;
687 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
688 prop == ZFS_PROP_SNAPSHOT_LIMIT);
691 if (crgetzoneid(cr) != GLOBAL_ZONEID)
692 return (ENFORCE_ALWAYS);
694 if (secpolicy_zfs(cr) == 0)
695 return (ENFORCE_NEVER);
698 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
699 return (ENFORCE_ALWAYS);
701 ASSERT(dsl_pool_config_held(dd->dd_pool));
703 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
704 return (ENFORCE_ALWAYS);
706 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
707 /* Only root can access zoned fs's from the GZ */
708 enforce = ENFORCE_ALWAYS;
710 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
711 enforce = ENFORCE_ABOVE;
714 dsl_dataset_rele(ds, FTAG);
719 * Check if adding additional child filesystem(s) would exceed any filesystem
720 * limits or adding additional snapshot(s) would exceed any snapshot limits.
721 * The prop argument indicates which limit to check.
723 * Note that all filesystem limits up to the root (or the highest
724 * initialized) filesystem or the given ancestor must be satisfied.
727 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
728 dsl_dir_t *ancestor, cred_t *cr)
730 objset_t *os = dd->dd_pool->dp_meta_objset;
731 uint64_t limit, count;
733 enforce_res_t enforce;
736 ASSERT(dsl_pool_config_held(dd->dd_pool));
737 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
738 prop == ZFS_PROP_SNAPSHOT_LIMIT);
741 * If we're allowed to change the limit, don't enforce the limit
742 * e.g. this can happen if a snapshot is taken by an administrative
743 * user in the global zone (i.e. a recursive snapshot by root).
744 * However, we must handle the case of delegated permissions where we
745 * are allowed to change the limit on the current dataset, but there
746 * is another limit in the tree above.
748 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
749 if (enforce == ENFORCE_NEVER)
753 * e.g. if renaming a dataset with no snapshots, count adjustment
759 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
761 * We don't enforce the limit for temporary snapshots. This is
762 * indicated by a NULL cred_t argument.
767 count_prop = DD_FIELD_SNAPSHOT_COUNT;
769 count_prop = DD_FIELD_FILESYSTEM_COUNT;
773 * If an ancestor has been provided, stop checking the limit once we
774 * hit that dir. We need this during rename so that we don't overcount
775 * the check once we recurse up to the common ancestor.
781 * If we hit an uninitialized node while recursing up the tree, we can
782 * stop since we know there is no limit here (or above). The counts are
783 * not valid on this node and we know we won't touch this node's counts.
785 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
786 count_prop, sizeof (count), 1, &count) == ENOENT)
789 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
794 /* Is there a limit which we've hit? */
795 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
796 return (SET_ERROR(EDQUOT));
798 if (dd->dd_parent != NULL)
799 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
806 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
807 * parents. When a new filesystem/snapshot is created, increment the count on
808 * all parents, and when a filesystem/snapshot is destroyed, decrement the
812 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
816 objset_t *os = dd->dd_pool->dp_meta_objset;
819 ASSERT(dsl_pool_config_held(dd->dd_pool));
820 ASSERT(dmu_tx_is_syncing(tx));
821 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
822 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
825 * When we receive an incremental stream into a filesystem that already
826 * exists, a temporary clone is created. We don't count this temporary
827 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
828 * $MOS & $ORIGIN) objsets.
830 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
831 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
835 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
841 * If we hit an uninitialized node while recursing up the tree, we can
842 * stop since we know the counts are not valid on this node and we
843 * know we shouldn't touch this node's counts. An uninitialized count
844 * on the node indicates that either the feature has not yet been
845 * activated or there are no limits on this part of the tree.
847 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
848 prop, sizeof (count), 1, &count)) == ENOENT)
853 /* Use a signed verify to make sure we're not neg. */
854 VERIFY3S(count, >=, 0);
856 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
859 /* Roll up this additional count into our ancestors */
860 if (dd->dd_parent != NULL)
861 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
865 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
868 objset_t *mos = dp->dp_meta_objset;
870 dsl_dir_phys_t *ddphys;
873 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
874 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
876 VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
877 name, sizeof (uint64_t), 1, &ddobj, tx));
879 /* it's the root dir */
880 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
881 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
883 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
884 dmu_buf_will_dirty(dbuf, tx);
885 ddphys = dbuf->db_data;
887 ddphys->dd_creation_time = gethrestime_sec();
889 ddphys->dd_parent_obj = pds->dd_object;
891 /* update the filesystem counts */
892 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
894 ddphys->dd_props_zapobj = zap_create(mos,
895 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
896 ddphys->dd_child_dir_zapobj = zap_create(mos,
897 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
898 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
899 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
900 dmu_buf_rele(dbuf, FTAG);
906 dsl_dir_is_clone(dsl_dir_t *dd)
908 return (dsl_dir_phys(dd)->dd_origin_obj &&
909 (dd->dd_pool->dp_origin_snap == NULL ||
910 dsl_dir_phys(dd)->dd_origin_obj !=
911 dd->dd_pool->dp_origin_snap->ds_object));
915 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
917 mutex_enter(&dd->dd_lock);
918 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
919 dsl_dir_phys(dd)->dd_used_bytes);
920 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
921 dsl_dir_phys(dd)->dd_quota);
922 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
923 dsl_dir_phys(dd)->dd_reserved);
924 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
925 dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
926 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
927 dsl_dir_phys(dd)->dd_compressed_bytes));
928 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
929 dsl_dir_phys(dd)->dd_uncompressed_bytes);
930 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
931 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
932 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
933 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
934 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
935 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
936 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
937 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
938 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
939 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
941 mutex_exit(&dd->dd_lock);
943 if (dsl_dir_is_zapified(dd)) {
945 objset_t *os = dd->dd_pool->dp_meta_objset;
947 if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
948 sizeof (count), 1, &count) == 0) {
949 dsl_prop_nvlist_add_uint64(nv,
950 ZFS_PROP_FILESYSTEM_COUNT, count);
952 if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
953 sizeof (count), 1, &count) == 0) {
954 dsl_prop_nvlist_add_uint64(nv,
955 ZFS_PROP_SNAPSHOT_COUNT, count);
959 if (dsl_dir_is_clone(dd)) {
961 char buf[MAXNAMELEN];
963 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
964 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
965 dsl_dataset_name(ds, buf);
966 dsl_dataset_rele(ds, FTAG);
967 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
972 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
974 dsl_pool_t *dp = dd->dd_pool;
976 ASSERT(dsl_dir_phys(dd));
978 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
979 /* up the hold count until we can be written out */
980 dmu_buf_add_ref(dd->dd_dbuf, dd);
985 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
987 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
988 uint64_t new_accounted =
989 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
990 return (new_accounted - old_accounted);
994 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
996 ASSERT(dmu_tx_is_syncing(tx));
998 mutex_enter(&dd->dd_lock);
999 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1000 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1001 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1002 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1003 mutex_exit(&dd->dd_lock);
1005 /* release the hold from dsl_dir_dirty */
1006 dmu_buf_rele(dd->dd_dbuf, dd);
1010 dsl_dir_space_towrite(dsl_dir_t *dd)
1015 ASSERT(MUTEX_HELD(&dd->dd_lock));
1017 for (i = 0; i < TXG_SIZE; i++) {
1018 space += dd->dd_space_towrite[i&TXG_MASK];
1019 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1025 * How much space would dd have available if ancestor had delta applied
1026 * to it? If ondiskonly is set, we're only interested in what's
1027 * on-disk, not estimated pending changes.
1030 dsl_dir_space_available(dsl_dir_t *dd,
1031 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1033 uint64_t parentspace, myspace, quota, used;
1036 * If there are no restrictions otherwise, assume we have
1037 * unlimited space available.
1040 parentspace = UINT64_MAX;
1042 if (dd->dd_parent != NULL) {
1043 parentspace = dsl_dir_space_available(dd->dd_parent,
1044 ancestor, delta, ondiskonly);
1047 mutex_enter(&dd->dd_lock);
1048 if (dsl_dir_phys(dd)->dd_quota != 0)
1049 quota = dsl_dir_phys(dd)->dd_quota;
1050 used = dsl_dir_phys(dd)->dd_used_bytes;
1052 used += dsl_dir_space_towrite(dd);
1054 if (dd->dd_parent == NULL) {
1055 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1056 quota = MIN(quota, poolsize);
1059 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1061 * We have some space reserved, in addition to what our
1064 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1067 if (dd == ancestor) {
1069 ASSERT(used >= -delta);
1071 if (parentspace != UINT64_MAX)
1072 parentspace -= delta;
1080 * the lesser of the space provided by our parent and
1081 * the space left in our quota
1083 myspace = MIN(parentspace, quota - used);
1086 mutex_exit(&dd->dd_lock);
1091 struct tempreserve {
1092 list_node_t tr_node;
1098 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1099 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
1100 dmu_tx_t *tx, boolean_t first)
1102 uint64_t txg = tx->tx_txg;
1103 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1104 uint64_t deferred = 0;
1105 struct tempreserve *tr;
1106 int retval = EDQUOT;
1107 int txgidx = txg & TXG_MASK;
1109 uint64_t ref_rsrv = 0;
1111 ASSERT3U(txg, !=, 0);
1112 ASSERT3S(asize, >, 0);
1114 mutex_enter(&dd->dd_lock);
1117 * Check against the dsl_dir's quota. We don't add in the delta
1118 * when checking for over-quota because they get one free hit.
1120 est_inflight = dsl_dir_space_towrite(dd);
1121 for (i = 0; i < TXG_SIZE; i++)
1122 est_inflight += dd->dd_tempreserved[i];
1123 used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1126 * On the first iteration, fetch the dataset's used-on-disk and
1127 * refreservation values. Also, if checkrefquota is set, test if
1128 * allocating this space would exceed the dataset's refquota.
1130 if (first && tx->tx_objset) {
1132 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1134 error = dsl_dataset_check_quota(ds, checkrefquota,
1135 asize, est_inflight, &used_on_disk, &ref_rsrv);
1137 mutex_exit(&dd->dd_lock);
1138 DMU_TX_STAT_BUMP(dmu_tx_quota);
1144 * If this transaction will result in a net free of space,
1145 * we want to let it through.
1147 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1150 quota = dsl_dir_phys(dd)->dd_quota;
1153 * Adjust the quota against the actual pool size at the root
1154 * minus any outstanding deferred frees.
1155 * To ensure that it's possible to remove files from a full
1156 * pool without inducing transient overcommits, we throttle
1157 * netfree transactions against a quota that is slightly larger,
1158 * but still within the pool's allocation slop. In cases where
1159 * we're very close to full, this will allow a steady trickle of
1160 * removes to get through.
1162 if (dd->dd_parent == NULL) {
1163 spa_t *spa = dd->dd_pool->dp_spa;
1164 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1165 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1166 if (poolsize - deferred < quota) {
1167 quota = poolsize - deferred;
1173 * If they are requesting more space, and our current estimate
1174 * is over quota, they get to try again unless the actual
1175 * on-disk is over quota and there are no pending changes (which
1176 * may free up space for us).
1178 if (used_on_disk + est_inflight >= quota) {
1179 if (est_inflight > 0 || used_on_disk < quota ||
1180 (retval == ENOSPC && used_on_disk < quota + deferred))
1182 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1183 "quota=%lluK tr=%lluK err=%d\n",
1184 used_on_disk>>10, est_inflight>>10,
1185 quota>>10, asize>>10, retval);
1186 mutex_exit(&dd->dd_lock);
1187 DMU_TX_STAT_BUMP(dmu_tx_quota);
1188 return (SET_ERROR(retval));
1191 /* We need to up our estimated delta before dropping dd_lock */
1192 dd->dd_tempreserved[txgidx] += asize;
1194 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1196 mutex_exit(&dd->dd_lock);
1198 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1200 tr->tr_size = asize;
1201 list_insert_tail(tr_list, tr);
1203 /* see if it's OK with our parent */
1204 if (dd->dd_parent && parent_rsrv) {
1205 boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1207 return (dsl_dir_tempreserve_impl(dd->dd_parent,
1208 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1215 * Reserve space in this dsl_dir, to be used in this tx's txg.
1216 * After the space has been dirtied (and dsl_dir_willuse_space()
1217 * has been called), the reservation should be canceled, using
1218 * dsl_dir_tempreserve_clear().
1221 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1222 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1232 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1233 list_create(tr_list, sizeof (struct tempreserve),
1234 offsetof(struct tempreserve, tr_node));
1235 ASSERT3S(asize, >, 0);
1236 ASSERT3S(fsize, >=, 0);
1238 err = arc_tempreserve_space(lsize, tx->tx_txg);
1240 struct tempreserve *tr;
1242 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1243 tr->tr_size = lsize;
1244 list_insert_tail(tr_list, tr);
1246 if (err == EAGAIN) {
1248 * If arc_memory_throttle() detected that pageout
1249 * is running and we are low on memory, we delay new
1250 * non-pageout transactions to give pageout an
1253 * It is unfortunate to be delaying while the caller's
1256 txg_delay(dd->dd_pool, tx->tx_txg,
1257 MSEC2NSEC(10), MSEC2NSEC(10));
1258 err = SET_ERROR(ERESTART);
1263 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
1264 FALSE, asize > usize, tr_list, tx, TRUE);
1268 dsl_dir_tempreserve_clear(tr_list, tx);
1270 *tr_cookiep = tr_list;
1276 * Clear a temporary reservation that we previously made with
1277 * dsl_dir_tempreserve_space().
1280 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1282 int txgidx = tx->tx_txg & TXG_MASK;
1283 list_t *tr_list = tr_cookie;
1284 struct tempreserve *tr;
1286 ASSERT3U(tx->tx_txg, !=, 0);
1288 if (tr_cookie == NULL)
1291 while ((tr = list_head(tr_list)) != NULL) {
1293 mutex_enter(&tr->tr_ds->dd_lock);
1294 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1296 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1297 mutex_exit(&tr->tr_ds->dd_lock);
1299 arc_tempreserve_clear(tr->tr_size);
1301 list_remove(tr_list, tr);
1302 kmem_free(tr, sizeof (struct tempreserve));
1305 kmem_free(tr_list, sizeof (list_t));
1309 * This should be called from open context when we think we're going to write
1310 * or free space, for example when dirtying data. Be conservative; it's okay
1311 * to write less space or free more, but we don't want to write more or free
1312 * less than the amount specified.
1314 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1315 * version however it has been adjusted to use an iterative rather then
1316 * recursive algorithm to minimize stack usage.
1319 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1321 int64_t parent_space;
1325 mutex_enter(&dd->dd_lock);
1327 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1329 est_used = dsl_dir_space_towrite(dd) +
1330 dsl_dir_phys(dd)->dd_used_bytes;
1331 parent_space = parent_delta(dd, est_used, space);
1332 mutex_exit(&dd->dd_lock);
1334 /* Make sure that we clean up dd_space_to* */
1335 dsl_dir_dirty(dd, tx);
1338 space = parent_space;
1339 } while (space && dd);
1342 /* call from syncing context when we actually write/free space for this dd */
1344 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1345 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1347 int64_t accounted_delta;
1350 * dsl_dataset_set_refreservation_sync_impl() calls this with
1351 * dd_lock held, so that it can atomically update
1352 * ds->ds_reserved and the dsl_dir accounting, so that
1353 * dsl_dataset_check_quota() can see dataset and dir accounting
1356 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1358 ASSERT(dmu_tx_is_syncing(tx));
1359 ASSERT(type < DD_USED_NUM);
1361 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1364 mutex_enter(&dd->dd_lock);
1366 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1367 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1368 ASSERT(compressed >= 0 ||
1369 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1370 ASSERT(uncompressed >= 0 ||
1371 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1372 dsl_dir_phys(dd)->dd_used_bytes += used;
1373 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1374 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1376 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1378 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1379 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1384 for (t = 0; t < DD_USED_NUM; t++)
1385 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1386 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1391 mutex_exit(&dd->dd_lock);
1393 if (dd->dd_parent != NULL) {
1394 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1395 accounted_delta, compressed, uncompressed, tx);
1396 dsl_dir_transfer_space(dd->dd_parent,
1397 used - accounted_delta,
1398 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1403 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1404 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1406 ASSERT(dmu_tx_is_syncing(tx));
1407 ASSERT(oldtype < DD_USED_NUM);
1408 ASSERT(newtype < DD_USED_NUM);
1411 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1414 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1415 mutex_enter(&dd->dd_lock);
1417 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1418 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1419 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1420 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1421 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1422 mutex_exit(&dd->dd_lock);
1425 typedef struct dsl_dir_set_qr_arg {
1426 const char *ddsqra_name;
1427 zprop_source_t ddsqra_source;
1428 uint64_t ddsqra_value;
1429 } dsl_dir_set_qr_arg_t;
1432 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1434 dsl_dir_set_qr_arg_t *ddsqra = arg;
1435 dsl_pool_t *dp = dmu_tx_pool(tx);
1438 uint64_t towrite, newval;
1440 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1444 error = dsl_prop_predict(ds->ds_dir, "quota",
1445 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1447 dsl_dataset_rele(ds, FTAG);
1452 dsl_dataset_rele(ds, FTAG);
1456 mutex_enter(&ds->ds_dir->dd_lock);
1458 * If we are doing the preliminary check in open context, and
1459 * there are pending changes, then don't fail it, since the
1460 * pending changes could under-estimate the amount of space to be
1463 towrite = dsl_dir_space_towrite(ds->ds_dir);
1464 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1465 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1466 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1467 error = SET_ERROR(ENOSPC);
1469 mutex_exit(&ds->ds_dir->dd_lock);
1470 dsl_dataset_rele(ds, FTAG);
1475 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1477 dsl_dir_set_qr_arg_t *ddsqra = arg;
1478 dsl_pool_t *dp = dmu_tx_pool(tx);
1482 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1484 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1485 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1486 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1487 &ddsqra->ddsqra_value, tx);
1489 VERIFY0(dsl_prop_get_int_ds(ds,
1490 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1492 newval = ddsqra->ddsqra_value;
1493 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1494 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1497 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1498 mutex_enter(&ds->ds_dir->dd_lock);
1499 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1500 mutex_exit(&ds->ds_dir->dd_lock);
1501 dsl_dataset_rele(ds, FTAG);
1505 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1507 dsl_dir_set_qr_arg_t ddsqra;
1509 ddsqra.ddsqra_name = ddname;
1510 ddsqra.ddsqra_source = source;
1511 ddsqra.ddsqra_value = quota;
1513 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1514 dsl_dir_set_quota_sync, &ddsqra, 0));
1518 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1520 dsl_dir_set_qr_arg_t *ddsqra = arg;
1521 dsl_pool_t *dp = dmu_tx_pool(tx);
1524 uint64_t newval, used, avail;
1527 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1533 * If we are doing the preliminary check in open context, the
1534 * space estimates may be inaccurate.
1536 if (!dmu_tx_is_syncing(tx)) {
1537 dsl_dataset_rele(ds, FTAG);
1541 error = dsl_prop_predict(ds->ds_dir,
1542 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1543 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1545 dsl_dataset_rele(ds, FTAG);
1549 mutex_enter(&dd->dd_lock);
1550 used = dsl_dir_phys(dd)->dd_used_bytes;
1551 mutex_exit(&dd->dd_lock);
1553 if (dd->dd_parent) {
1554 avail = dsl_dir_space_available(dd->dd_parent,
1557 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1560 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1561 uint64_t delta = MAX(used, newval) -
1562 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1564 if (delta > avail ||
1565 (dsl_dir_phys(dd)->dd_quota > 0 &&
1566 newval > dsl_dir_phys(dd)->dd_quota))
1567 error = SET_ERROR(ENOSPC);
1570 dsl_dataset_rele(ds, FTAG);
1575 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1580 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1582 mutex_enter(&dd->dd_lock);
1583 used = dsl_dir_phys(dd)->dd_used_bytes;
1584 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1585 dsl_dir_phys(dd)->dd_reserved = value;
1587 if (dd->dd_parent != NULL) {
1588 /* Roll up this additional usage into our ancestors */
1589 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1592 mutex_exit(&dd->dd_lock);
1596 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1598 dsl_dir_set_qr_arg_t *ddsqra = arg;
1599 dsl_pool_t *dp = dmu_tx_pool(tx);
1603 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1605 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1606 dsl_prop_set_sync_impl(ds,
1607 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1608 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1609 &ddsqra->ddsqra_value, tx);
1611 VERIFY0(dsl_prop_get_int_ds(ds,
1612 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1614 newval = ddsqra->ddsqra_value;
1615 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1616 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1617 (longlong_t)newval);
1620 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1621 dsl_dataset_rele(ds, FTAG);
1625 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1626 uint64_t reservation)
1628 dsl_dir_set_qr_arg_t ddsqra;
1630 ddsqra.ddsqra_name = ddname;
1631 ddsqra.ddsqra_source = source;
1632 ddsqra.ddsqra_value = reservation;
1634 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1635 dsl_dir_set_reservation_sync, &ddsqra, 0));
1639 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1641 for (; ds1; ds1 = ds1->dd_parent) {
1643 for (dd = ds2; dd; dd = dd->dd_parent) {
1652 * If delta is applied to dd, how much of that delta would be applied to
1653 * ancestor? Syncing context only.
1656 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1661 mutex_enter(&dd->dd_lock);
1662 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1663 mutex_exit(&dd->dd_lock);
1664 return (would_change(dd->dd_parent, delta, ancestor));
1667 typedef struct dsl_dir_rename_arg {
1668 const char *ddra_oldname;
1669 const char *ddra_newname;
1671 } dsl_dir_rename_arg_t;
1675 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1678 char namebuf[MAXNAMELEN];
1680 dsl_dataset_name(ds, namebuf);
1682 if (strlen(namebuf) + *deltap >= MAXNAMELEN)
1683 return (SET_ERROR(ENAMETOOLONG));
1688 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1690 dsl_dir_rename_arg_t *ddra = arg;
1691 dsl_pool_t *dp = dmu_tx_pool(tx);
1692 dsl_dir_t *dd, *newparent;
1693 const char *mynewname;
1695 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1697 /* target dir should exist */
1698 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1702 /* new parent should exist */
1703 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1704 &newparent, &mynewname);
1706 dsl_dir_rele(dd, FTAG);
1710 /* can't rename to different pool */
1711 if (dd->dd_pool != newparent->dd_pool) {
1712 dsl_dir_rele(newparent, FTAG);
1713 dsl_dir_rele(dd, FTAG);
1714 return (SET_ERROR(EXDEV));
1717 /* new name should not already exist */
1718 if (mynewname == NULL) {
1719 dsl_dir_rele(newparent, FTAG);
1720 dsl_dir_rele(dd, FTAG);
1721 return (SET_ERROR(EEXIST));
1724 /* if the name length is growing, validate child name lengths */
1726 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1727 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1729 dsl_dir_rele(newparent, FTAG);
1730 dsl_dir_rele(dd, FTAG);
1735 if (dmu_tx_is_syncing(tx)) {
1736 if (spa_feature_is_enabled(dp->dp_spa,
1737 SPA_FEATURE_FS_SS_LIMIT)) {
1739 * Although this is the check function and we don't
1740 * normally make on-disk changes in check functions,
1741 * we need to do that here.
1743 * Ensure this portion of the tree's counts have been
1744 * initialized in case the new parent has limits set.
1746 dsl_dir_init_fs_ss_count(dd, tx);
1750 if (newparent != dd->dd_parent) {
1751 /* is there enough space? */
1753 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1754 dsl_dir_phys(dd)->dd_reserved);
1755 objset_t *os = dd->dd_pool->dp_meta_objset;
1756 uint64_t fs_cnt = 0;
1757 uint64_t ss_cnt = 0;
1759 if (dsl_dir_is_zapified(dd)) {
1762 err = zap_lookup(os, dd->dd_object,
1763 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1765 if (err != ENOENT && err != 0)
1769 * have to add 1 for the filesystem itself that we're
1774 err = zap_lookup(os, dd->dd_object,
1775 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1777 if (err != ENOENT && err != 0)
1781 /* no rename into our descendant */
1782 if (closest_common_ancestor(dd, newparent) == dd) {
1783 dsl_dir_rele(newparent, FTAG);
1784 dsl_dir_rele(dd, FTAG);
1785 return (SET_ERROR(EINVAL));
1788 error = dsl_dir_transfer_possible(dd->dd_parent,
1789 newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
1791 dsl_dir_rele(newparent, FTAG);
1792 dsl_dir_rele(dd, FTAG);
1797 dsl_dir_rele(newparent, FTAG);
1798 dsl_dir_rele(dd, FTAG);
1803 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
1805 dsl_dir_rename_arg_t *ddra = arg;
1806 dsl_pool_t *dp = dmu_tx_pool(tx);
1807 dsl_dir_t *dd, *newparent;
1808 const char *mynewname;
1810 objset_t *mos = dp->dp_meta_objset;
1812 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
1813 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
1816 /* Log this before we change the name. */
1817 spa_history_log_internal_dd(dd, "rename", tx,
1818 "-> %s", ddra->ddra_newname);
1820 if (newparent != dd->dd_parent) {
1821 objset_t *os = dd->dd_pool->dp_meta_objset;
1822 uint64_t fs_cnt = 0;
1823 uint64_t ss_cnt = 0;
1826 * We already made sure the dd counts were initialized in the
1829 if (spa_feature_is_enabled(dp->dp_spa,
1830 SPA_FEATURE_FS_SS_LIMIT)) {
1831 VERIFY0(zap_lookup(os, dd->dd_object,
1832 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1834 /* add 1 for the filesystem itself that we're moving */
1837 VERIFY0(zap_lookup(os, dd->dd_object,
1838 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1842 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1843 DD_FIELD_FILESYSTEM_COUNT, tx);
1844 dsl_fs_ss_count_adjust(newparent, fs_cnt,
1845 DD_FIELD_FILESYSTEM_COUNT, tx);
1847 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1848 DD_FIELD_SNAPSHOT_COUNT, tx);
1849 dsl_fs_ss_count_adjust(newparent, ss_cnt,
1850 DD_FIELD_SNAPSHOT_COUNT, tx);
1852 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1853 -dsl_dir_phys(dd)->dd_used_bytes,
1854 -dsl_dir_phys(dd)->dd_compressed_bytes,
1855 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1856 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1857 dsl_dir_phys(dd)->dd_used_bytes,
1858 dsl_dir_phys(dd)->dd_compressed_bytes,
1859 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1861 if (dsl_dir_phys(dd)->dd_reserved >
1862 dsl_dir_phys(dd)->dd_used_bytes) {
1863 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1864 dsl_dir_phys(dd)->dd_used_bytes;
1866 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1867 -unused_rsrv, 0, 0, tx);
1868 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
1869 unused_rsrv, 0, 0, tx);
1873 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1875 /* remove from old parent zapobj */
1876 error = zap_remove(mos,
1877 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1881 (void) strcpy(dd->dd_myname, mynewname);
1882 dsl_dir_rele(dd->dd_parent, dd);
1883 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
1884 VERIFY0(dsl_dir_hold_obj(dp,
1885 newparent->dd_object, NULL, dd, &dd->dd_parent));
1887 /* add to new parent zapobj */
1888 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
1889 dd->dd_myname, 8, 1, &dd->dd_object, tx));
1892 zvol_rename_minors(ddra->ddra_oldname, ddra->ddra_newname);
1895 dsl_prop_notify_all(dd);
1897 dsl_dir_rele(newparent, FTAG);
1898 dsl_dir_rele(dd, FTAG);
1902 dsl_dir_rename(const char *oldname, const char *newname)
1904 dsl_dir_rename_arg_t ddra;
1906 ddra.ddra_oldname = oldname;
1907 ddra.ddra_newname = newname;
1908 ddra.ddra_cred = CRED();
1910 return (dsl_sync_task(oldname,
1911 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra, 3));
1915 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1916 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
1918 dsl_dir_t *ancestor;
1923 ancestor = closest_common_ancestor(sdd, tdd);
1924 adelta = would_change(sdd, -space, ancestor);
1925 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1927 return (SET_ERROR(ENOSPC));
1929 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1933 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1942 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1946 mutex_enter(&dd->dd_lock);
1947 t = dd->dd_snap_cmtime;
1948 mutex_exit(&dd->dd_lock);
1954 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1959 mutex_enter(&dd->dd_lock);
1960 dd->dd_snap_cmtime = t;
1961 mutex_exit(&dd->dd_lock);
1965 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
1967 objset_t *mos = dd->dd_pool->dp_meta_objset;
1968 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
1972 dsl_dir_is_zapified(dsl_dir_t *dd)
1974 dmu_object_info_t doi;
1976 dmu_object_info_from_db(dd->dd_dbuf, &doi);
1977 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
1980 #if defined(_KERNEL) && defined(HAVE_SPL)
1981 EXPORT_SYMBOL(dsl_dir_set_quota);
1982 EXPORT_SYMBOL(dsl_dir_set_reservation);