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) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
24 * All rights reserved.
25 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
26 * Copyright (c) 2014 Joyent, Inc. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/dsl_deleg.h>
38 #include <sys/dmu_impl.h>
40 #include <sys/metaslab.h>
44 #include <sys/sunddi.h>
47 #include <sys/zfs_vfsops.h>
49 #include <sys/zfeature.h>
50 #include <sys/policy.h>
51 #include <sys/zfs_znode.h>
52 #include "zfs_namecheck.h"
56 * Filesystem and Snapshot Limits
57 * ------------------------------
59 * These limits are used to restrict the number of filesystems and/or snapshots
60 * that can be created at a given level in the tree or below. A typical
61 * use-case is with a delegated dataset where the administrator wants to ensure
62 * that a user within the zone is not creating too many additional filesystems
63 * or snapshots, even though they're not exceeding their space quota.
65 * The filesystem and snapshot counts are stored as extensible properties. This
66 * capability is controlled by a feature flag and must be enabled to be used.
67 * Once enabled, the feature is not active until the first limit is set. At
68 * that point, future operations to create/destroy filesystems or snapshots
69 * will validate and update the counts.
71 * Because the count properties will not exist before the feature is active,
72 * the counts are updated when a limit is first set on an uninitialized
73 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
74 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
75 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
76 * snapshot count properties on a node indicate uninitialized counts on that
77 * node.) When first setting a limit on an uninitialized node, the code starts
78 * at the filesystem with the new limit and descends into all sub-filesystems
79 * to add the count properties.
81 * In practice this is lightweight since a limit is typically set when the
82 * filesystem is created and thus has no children. Once valid, changing the
83 * limit value won't require a re-traversal since the counts are already valid.
84 * When recursively fixing the counts, if a node with a limit is encountered
85 * during the descent, the counts are known to be valid and there is no need to
86 * descend into that filesystem's children. The counts on filesystems above the
87 * one with the new limit will still be uninitialized, unless a limit is
88 * eventually set on one of those filesystems. The counts are always recursively
89 * updated when a limit is set on a dataset, unless there is already a limit.
90 * When a new limit value is set on a filesystem with an existing limit, it is
91 * possible for the new limit to be less than the current count at that level
92 * since a user who can change the limit is also allowed to exceed the limit.
94 * Once the feature is active, then whenever a filesystem or snapshot is
95 * created, the code recurses up the tree, validating the new count against the
96 * limit at each initialized level. In practice, most levels will not have a
97 * limit set. If there is a limit at any initialized level up the tree, the
98 * check must pass or the creation will fail. Likewise, when a filesystem or
99 * snapshot is destroyed, the counts are recursively adjusted all the way up
100 * the initizized nodes in the tree. Renaming a filesystem into different point
101 * in the tree will first validate, then update the counts on each branch up to
102 * the common ancestor. A receive will also validate the counts and then update
105 * An exception to the above behavior is that the limit is not enforced if the
106 * user has permission to modify the limit. This is primarily so that
107 * recursive snapshots in the global zone always work. We want to prevent a
108 * denial-of-service in which a lower level delegated dataset could max out its
109 * limit and thus block recursive snapshots from being taken in the global zone.
110 * Because of this, it is possible for the snapshot count to be over the limit
111 * and snapshots taken in the global zone could cause a lower level dataset to
112 * hit or exceed its limit. The administrator taking the global zone recursive
113 * snapshot should be aware of this side-effect and behave accordingly.
114 * For consistency, the filesystem limit is also not enforced if the user can
117 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
118 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
119 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
120 * dsl_dir_init_fs_ss_count().
122 * There is a special case when we receive a filesystem that already exists. In
123 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
124 * never update the filesystem counts for temporary clones.
126 * Likewise, we do not update the snapshot counts for temporary snapshots,
127 * such as those created by zfs diff.
130 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
132 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
136 dsl_dir_evict(dmu_buf_t *db, void *arg)
139 dsl_pool_t *dp = dd->dd_pool;
142 for (t = 0; t < TXG_SIZE; t++) {
143 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
144 ASSERT(dd->dd_tempreserved[t] == 0);
145 ASSERT(dd->dd_space_towrite[t] == 0);
149 dsl_dir_rele(dd->dd_parent, dd);
151 spa_close(dd->dd_pool->dp_spa, dd);
154 * The props callback list should have been cleaned up by
157 list_destroy(&dd->dd_prop_cbs);
158 mutex_destroy(&dd->dd_lock);
159 kmem_free(dd, sizeof (dsl_dir_t));
163 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
164 const char *tail, void *tag, dsl_dir_t **ddp)
170 ASSERT(dsl_pool_config_held(dp));
172 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
175 dd = dmu_buf_get_user(dbuf);
178 dmu_object_info_t doi;
179 dmu_object_info_from_db(dbuf, &doi);
180 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
181 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
187 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
188 dd->dd_object = ddobj;
191 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
193 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
194 offsetof(dsl_prop_cb_record_t, cbr_node));
196 dsl_dir_snap_cmtime_update(dd);
198 if (dsl_dir_phys(dd)->dd_parent_obj) {
199 err = dsl_dir_hold_obj(dp,
200 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
208 err = zap_lookup(dp->dp_meta_objset,
209 dsl_dir_phys(dd->dd_parent)->
210 dd_child_dir_zapobj, tail,
211 sizeof (foundobj), 1, &foundobj);
212 ASSERT(err || foundobj == ddobj);
214 (void) strcpy(dd->dd_myname, tail);
216 err = zap_value_search(dp->dp_meta_objset,
217 dsl_dir_phys(dd->dd_parent)->
219 ddobj, 0, dd->dd_myname);
224 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
227 if (dsl_dir_is_clone(dd)) {
228 dmu_buf_t *origin_bonus;
229 dsl_dataset_phys_t *origin_phys;
232 * We can't open the origin dataset, because
233 * that would require opening this dsl_dir.
234 * Just look at its phys directly instead.
236 err = dmu_bonus_hold(dp->dp_meta_objset,
237 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
241 origin_phys = origin_bonus->db_data;
243 origin_phys->ds_creation_txg;
244 dmu_buf_rele(origin_bonus, FTAG);
247 winner = dmu_buf_set_user_ie(dbuf, dd, dsl_dir_evict);
250 dsl_dir_rele(dd->dd_parent, dd);
251 mutex_destroy(&dd->dd_lock);
252 kmem_free(dd, sizeof (dsl_dir_t));
255 spa_open_ref(dp->dp_spa, dd);
260 * The dsl_dir_t has both open-to-close and instantiate-to-evict
261 * holds on the spa. We need the open-to-close holds because
262 * otherwise the spa_refcnt wouldn't change when we open a
263 * dir which the spa also has open, so we could incorrectly
264 * think it was OK to unload/export/destroy the pool. We need
265 * the instantiate-to-evict hold because the dsl_dir_t has a
266 * pointer to the dd_pool, which has a pointer to the spa_t.
268 spa_open_ref(dp->dp_spa, tag);
269 ASSERT3P(dd->dd_pool, ==, dp);
270 ASSERT3U(dd->dd_object, ==, ddobj);
271 ASSERT3P(dd->dd_dbuf, ==, dbuf);
277 dsl_dir_rele(dd->dd_parent, dd);
278 mutex_destroy(&dd->dd_lock);
279 kmem_free(dd, sizeof (dsl_dir_t));
280 dmu_buf_rele(dbuf, tag);
285 dsl_dir_rele(dsl_dir_t *dd, void *tag)
287 dprintf_dd(dd, "%s\n", "");
288 spa_close(dd->dd_pool->dp_spa, tag);
289 dmu_buf_rele(dd->dd_dbuf, tag);
292 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
294 dsl_dir_name(dsl_dir_t *dd, char *buf)
297 dsl_dir_name(dd->dd_parent, buf);
298 (void) strcat(buf, "/");
302 if (!MUTEX_HELD(&dd->dd_lock)) {
304 * recursive mutex so that we can use
305 * dprintf_dd() with dd_lock held
307 mutex_enter(&dd->dd_lock);
308 (void) strcat(buf, dd->dd_myname);
309 mutex_exit(&dd->dd_lock);
311 (void) strcat(buf, dd->dd_myname);
315 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
317 dsl_dir_namelen(dsl_dir_t *dd)
322 /* parent's name + 1 for the "/" */
323 result = dsl_dir_namelen(dd->dd_parent) + 1;
326 if (!MUTEX_HELD(&dd->dd_lock)) {
327 /* see dsl_dir_name */
328 mutex_enter(&dd->dd_lock);
329 result += strlen(dd->dd_myname);
330 mutex_exit(&dd->dd_lock);
332 result += strlen(dd->dd_myname);
339 getcomponent(const char *path, char *component, const char **nextp)
343 if ((path == NULL) || (path[0] == '\0'))
344 return (SET_ERROR(ENOENT));
345 /* This would be a good place to reserve some namespace... */
346 p = strpbrk(path, "/@");
347 if (p && (p[1] == '/' || p[1] == '@')) {
348 /* two separators in a row */
349 return (SET_ERROR(EINVAL));
351 if (p == NULL || p == path) {
353 * if the first thing is an @ or /, it had better be an
354 * @ and it had better not have any more ats or slashes,
355 * and it had better have something after the @.
358 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
359 return (SET_ERROR(EINVAL));
360 if (strlen(path) >= MAXNAMELEN)
361 return (SET_ERROR(ENAMETOOLONG));
362 (void) strcpy(component, path);
364 } else if (p[0] == '/') {
365 if (p - path >= MAXNAMELEN)
366 return (SET_ERROR(ENAMETOOLONG));
367 (void) strncpy(component, path, p - path);
368 component[p - path] = '\0';
370 } else if (p[0] == '@') {
372 * if the next separator is an @, there better not be
375 if (strchr(path, '/'))
376 return (SET_ERROR(EINVAL));
377 if (p - path >= MAXNAMELEN)
378 return (SET_ERROR(ENAMETOOLONG));
379 (void) strncpy(component, path, p - path);
380 component[p - path] = '\0';
382 panic("invalid p=%p", (void *)p);
389 * Return the dsl_dir_t, and possibly the last component which couldn't
390 * be found in *tail. The name must be in the specified dsl_pool_t. This
391 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
392 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
393 * (*tail)[0] == '@' means that the last component is a snapshot.
396 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
397 dsl_dir_t **ddp, const char **tailp)
399 char buf[MAXNAMELEN];
400 const char *spaname, *next, *nextnext = NULL;
405 err = getcomponent(name, buf, &next);
409 /* Make sure the name is in the specified pool. */
410 spaname = spa_name(dp->dp_spa);
411 if (strcmp(buf, spaname) != 0)
412 return (SET_ERROR(EXDEV));
414 ASSERT(dsl_pool_config_held(dp));
416 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
421 while (next != NULL) {
423 err = getcomponent(next, buf, &nextnext);
426 ASSERT(next[0] != '\0');
429 dprintf("looking up %s in obj%lld\n",
430 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
432 err = zap_lookup(dp->dp_meta_objset,
433 dsl_dir_phys(dd)->dd_child_dir_zapobj,
434 buf, sizeof (ddobj), 1, &ddobj);
441 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_ds);
444 dsl_dir_rele(dd, tag);
450 dsl_dir_rele(dd, tag);
455 * It's an error if there's more than one component left, or
456 * tailp==NULL and there's any component left.
459 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
461 dsl_dir_rele(dd, tag);
462 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
463 err = SET_ERROR(ENOENT);
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_active(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,
651 ZFS_SPACE_CHECK_RESERVED);
653 if (error == EALREADY)
660 * Used to determine if the filesystem_limit or snapshot_limit should be
661 * enforced. We allow the limit to be exceeded if the user has permission to
662 * write the property value. We pass in the creds that we got in the open
663 * context since we will always be the GZ root in syncing context. We also have
664 * to handle the case where we are allowed to change the limit on the current
665 * dataset, but there may be another limit in the tree above.
667 * We can never modify these two properties within a non-global zone. In
668 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
669 * can't use that function since we are already holding the dp_config_rwlock.
670 * In addition, we already have the dd and dealing with snapshots is simplified
681 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
683 enforce_res_t enforce = ENFORCE_ALWAYS;
688 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
689 prop == ZFS_PROP_SNAPSHOT_LIMIT);
695 if (crgetzoneid(cr) != GLOBAL_ZONEID)
697 return (ENFORCE_ALWAYS);
699 if (secpolicy_zfs(cr) == 0)
700 return (ENFORCE_NEVER);
703 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
704 return (ENFORCE_ALWAYS);
706 ASSERT(dsl_pool_config_held(dd->dd_pool));
708 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
709 return (ENFORCE_ALWAYS);
711 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
712 /* Only root can access zoned fs's from the GZ */
713 enforce = ENFORCE_ALWAYS;
715 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
716 enforce = ENFORCE_ABOVE;
719 dsl_dataset_rele(ds, FTAG);
724 * Check if adding additional child filesystem(s) would exceed any filesystem
725 * limits or adding additional snapshot(s) would exceed any snapshot limits.
726 * The prop argument indicates which limit to check.
728 * Note that all filesystem limits up to the root (or the highest
729 * initialized) filesystem or the given ancestor must be satisfied.
732 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
733 dsl_dir_t *ancestor, cred_t *cr)
735 objset_t *os = dd->dd_pool->dp_meta_objset;
736 uint64_t limit, count;
738 enforce_res_t enforce;
741 ASSERT(dsl_pool_config_held(dd->dd_pool));
742 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
743 prop == ZFS_PROP_SNAPSHOT_LIMIT);
746 * If we're allowed to change the limit, don't enforce the limit
747 * e.g. this can happen if a snapshot is taken by an administrative
748 * user in the global zone (i.e. a recursive snapshot by root).
749 * However, we must handle the case of delegated permissions where we
750 * are allowed to change the limit on the current dataset, but there
751 * is another limit in the tree above.
753 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
754 if (enforce == ENFORCE_NEVER)
758 * e.g. if renaming a dataset with no snapshots, count adjustment
764 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
766 * We don't enforce the limit for temporary snapshots. This is
767 * indicated by a NULL cred_t argument.
772 count_prop = DD_FIELD_SNAPSHOT_COUNT;
774 count_prop = DD_FIELD_FILESYSTEM_COUNT;
778 * If an ancestor has been provided, stop checking the limit once we
779 * hit that dir. We need this during rename so that we don't overcount
780 * the check once we recurse up to the common ancestor.
786 * If we hit an uninitialized node while recursing up the tree, we can
787 * stop since we know there is no limit here (or above). The counts are
788 * not valid on this node and we know we won't touch this node's counts.
790 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
791 count_prop, sizeof (count), 1, &count) == ENOENT)
794 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
799 /* Is there a limit which we've hit? */
800 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
801 return (SET_ERROR(EDQUOT));
803 if (dd->dd_parent != NULL)
804 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
811 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
812 * parents. When a new filesystem/snapshot is created, increment the count on
813 * all parents, and when a filesystem/snapshot is destroyed, decrement the
817 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
821 objset_t *os = dd->dd_pool->dp_meta_objset;
824 ASSERT(dsl_pool_config_held(dd->dd_pool));
825 ASSERT(dmu_tx_is_syncing(tx));
826 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
827 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
830 * When we receive an incremental stream into a filesystem that already
831 * exists, a temporary clone is created. We don't count this temporary
832 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
833 * $MOS & $ORIGIN) objsets.
835 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
836 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
840 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
846 * If we hit an uninitialized node while recursing up the tree, we can
847 * stop since we know the counts are not valid on this node and we
848 * know we shouldn't touch this node's counts. An uninitialized count
849 * on the node indicates that either the feature has not yet been
850 * activated or there are no limits on this part of the tree.
852 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
853 prop, sizeof (count), 1, &count)) == ENOENT)
858 /* Use a signed verify to make sure we're not neg. */
859 VERIFY3S(count, >=, 0);
861 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
864 /* Roll up this additional count into our ancestors */
865 if (dd->dd_parent != NULL)
866 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
870 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
873 objset_t *mos = dp->dp_meta_objset;
875 dsl_dir_phys_t *ddphys;
878 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
879 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
881 VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
882 name, sizeof (uint64_t), 1, &ddobj, tx));
884 /* it's the root dir */
885 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
886 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
888 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
889 dmu_buf_will_dirty(dbuf, tx);
890 ddphys = dbuf->db_data;
892 ddphys->dd_creation_time = gethrestime_sec();
894 ddphys->dd_parent_obj = pds->dd_object;
896 /* update the filesystem counts */
897 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
899 ddphys->dd_props_zapobj = zap_create(mos,
900 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
901 ddphys->dd_child_dir_zapobj = zap_create(mos,
902 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
903 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
904 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
905 dmu_buf_rele(dbuf, FTAG);
911 dsl_dir_is_clone(dsl_dir_t *dd)
913 return (dsl_dir_phys(dd)->dd_origin_obj &&
914 (dd->dd_pool->dp_origin_snap == NULL ||
915 dsl_dir_phys(dd)->dd_origin_obj !=
916 dd->dd_pool->dp_origin_snap->ds_object));
920 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
922 mutex_enter(&dd->dd_lock);
923 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
924 dsl_dir_phys(dd)->dd_used_bytes);
925 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
926 dsl_dir_phys(dd)->dd_quota);
927 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
928 dsl_dir_phys(dd)->dd_reserved);
929 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
930 dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
931 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
932 dsl_dir_phys(dd)->dd_compressed_bytes));
933 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
934 dsl_dir_phys(dd)->dd_uncompressed_bytes);
935 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
936 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
937 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
938 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
939 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
940 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
941 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
942 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
943 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
944 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
946 mutex_exit(&dd->dd_lock);
948 if (dsl_dir_is_zapified(dd)) {
950 objset_t *os = dd->dd_pool->dp_meta_objset;
952 if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
953 sizeof (count), 1, &count) == 0) {
954 dsl_prop_nvlist_add_uint64(nv,
955 ZFS_PROP_FILESYSTEM_COUNT, count);
957 if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
958 sizeof (count), 1, &count) == 0) {
959 dsl_prop_nvlist_add_uint64(nv,
960 ZFS_PROP_SNAPSHOT_COUNT, count);
964 if (dsl_dir_is_clone(dd)) {
966 char buf[MAXNAMELEN];
968 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
969 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
970 dsl_dataset_name(ds, buf);
971 dsl_dataset_rele(ds, FTAG);
972 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
977 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
979 dsl_pool_t *dp = dd->dd_pool;
981 ASSERT(dsl_dir_phys(dd));
983 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
984 /* up the hold count until we can be written out */
985 dmu_buf_add_ref(dd->dd_dbuf, dd);
990 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
992 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
993 uint64_t new_accounted =
994 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
995 return (new_accounted - old_accounted);
999 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1001 ASSERT(dmu_tx_is_syncing(tx));
1003 mutex_enter(&dd->dd_lock);
1004 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1005 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1006 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1007 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1008 mutex_exit(&dd->dd_lock);
1010 /* release the hold from dsl_dir_dirty */
1011 dmu_buf_rele(dd->dd_dbuf, dd);
1015 dsl_dir_space_towrite(dsl_dir_t *dd)
1020 ASSERT(MUTEX_HELD(&dd->dd_lock));
1022 for (i = 0; i < TXG_SIZE; i++) {
1023 space += dd->dd_space_towrite[i&TXG_MASK];
1024 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1030 * How much space would dd have available if ancestor had delta applied
1031 * to it? If ondiskonly is set, we're only interested in what's
1032 * on-disk, not estimated pending changes.
1035 dsl_dir_space_available(dsl_dir_t *dd,
1036 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1038 uint64_t parentspace, myspace, quota, used;
1041 * If there are no restrictions otherwise, assume we have
1042 * unlimited space available.
1045 parentspace = UINT64_MAX;
1047 if (dd->dd_parent != NULL) {
1048 parentspace = dsl_dir_space_available(dd->dd_parent,
1049 ancestor, delta, ondiskonly);
1052 mutex_enter(&dd->dd_lock);
1053 if (dsl_dir_phys(dd)->dd_quota != 0)
1054 quota = dsl_dir_phys(dd)->dd_quota;
1055 used = dsl_dir_phys(dd)->dd_used_bytes;
1057 used += dsl_dir_space_towrite(dd);
1059 if (dd->dd_parent == NULL) {
1060 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1061 quota = MIN(quota, poolsize);
1064 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1066 * We have some space reserved, in addition to what our
1069 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1072 if (dd == ancestor) {
1074 ASSERT(used >= -delta);
1076 if (parentspace != UINT64_MAX)
1077 parentspace -= delta;
1085 * the lesser of the space provided by our parent and
1086 * the space left in our quota
1088 myspace = MIN(parentspace, quota - used);
1091 mutex_exit(&dd->dd_lock);
1096 struct tempreserve {
1097 list_node_t tr_node;
1103 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1104 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
1105 dmu_tx_t *tx, boolean_t first)
1107 uint64_t txg = tx->tx_txg;
1108 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1109 uint64_t deferred = 0;
1110 struct tempreserve *tr;
1111 int retval = EDQUOT;
1112 int txgidx = txg & TXG_MASK;
1114 uint64_t ref_rsrv = 0;
1116 ASSERT3U(txg, !=, 0);
1117 ASSERT3S(asize, >, 0);
1119 mutex_enter(&dd->dd_lock);
1122 * Check against the dsl_dir's quota. We don't add in the delta
1123 * when checking for over-quota because they get one free hit.
1125 est_inflight = dsl_dir_space_towrite(dd);
1126 for (i = 0; i < TXG_SIZE; i++)
1127 est_inflight += dd->dd_tempreserved[i];
1128 used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1131 * On the first iteration, fetch the dataset's used-on-disk and
1132 * refreservation values. Also, if checkrefquota is set, test if
1133 * allocating this space would exceed the dataset's refquota.
1135 if (first && tx->tx_objset) {
1137 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1139 error = dsl_dataset_check_quota(ds, checkrefquota,
1140 asize, est_inflight, &used_on_disk, &ref_rsrv);
1142 mutex_exit(&dd->dd_lock);
1148 * If this transaction will result in a net free of space,
1149 * we want to let it through.
1151 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1154 quota = dsl_dir_phys(dd)->dd_quota;
1157 * Adjust the quota against the actual pool size at the root
1158 * minus any outstanding deferred frees.
1159 * To ensure that it's possible to remove files from a full
1160 * pool without inducing transient overcommits, we throttle
1161 * netfree transactions against a quota that is slightly larger,
1162 * but still within the pool's allocation slop. In cases where
1163 * we're very close to full, this will allow a steady trickle of
1164 * removes to get through.
1166 if (dd->dd_parent == NULL) {
1167 spa_t *spa = dd->dd_pool->dp_spa;
1168 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1169 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1170 if (poolsize - deferred < quota) {
1171 quota = poolsize - deferred;
1177 * If they are requesting more space, and our current estimate
1178 * is over quota, they get to try again unless the actual
1179 * on-disk is over quota and there are no pending changes (which
1180 * may free up space for us).
1182 if (used_on_disk + est_inflight >= quota) {
1183 if (est_inflight > 0 || used_on_disk < quota ||
1184 (retval == ENOSPC && used_on_disk < quota + deferred))
1186 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1187 "quota=%lluK tr=%lluK err=%d\n",
1188 used_on_disk>>10, est_inflight>>10,
1189 quota>>10, asize>>10, retval);
1190 mutex_exit(&dd->dd_lock);
1191 return (SET_ERROR(retval));
1194 /* We need to up our estimated delta before dropping dd_lock */
1195 dd->dd_tempreserved[txgidx] += asize;
1197 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1199 mutex_exit(&dd->dd_lock);
1201 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1203 tr->tr_size = asize;
1204 list_insert_tail(tr_list, tr);
1206 /* see if it's OK with our parent */
1207 if (dd->dd_parent && parent_rsrv) {
1208 boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1210 return (dsl_dir_tempreserve_impl(dd->dd_parent,
1211 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1218 * Reserve space in this dsl_dir, to be used in this tx's txg.
1219 * After the space has been dirtied (and dsl_dir_willuse_space()
1220 * has been called), the reservation should be canceled, using
1221 * dsl_dir_tempreserve_clear().
1224 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1225 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1235 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1236 list_create(tr_list, sizeof (struct tempreserve),
1237 offsetof(struct tempreserve, tr_node));
1238 ASSERT3S(asize, >, 0);
1239 ASSERT3S(fsize, >=, 0);
1241 err = arc_tempreserve_space(lsize, tx->tx_txg);
1243 struct tempreserve *tr;
1245 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1246 tr->tr_size = lsize;
1247 list_insert_tail(tr_list, tr);
1249 if (err == EAGAIN) {
1251 * If arc_memory_throttle() detected that pageout
1252 * is running and we are low on memory, we delay new
1253 * non-pageout transactions to give pageout an
1256 * It is unfortunate to be delaying while the caller's
1259 txg_delay(dd->dd_pool, tx->tx_txg,
1260 MSEC2NSEC(10), MSEC2NSEC(10));
1261 err = SET_ERROR(ERESTART);
1266 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
1267 FALSE, asize > usize, tr_list, tx, TRUE);
1271 dsl_dir_tempreserve_clear(tr_list, tx);
1273 *tr_cookiep = tr_list;
1279 * Clear a temporary reservation that we previously made with
1280 * dsl_dir_tempreserve_space().
1283 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1285 int txgidx = tx->tx_txg & TXG_MASK;
1286 list_t *tr_list = tr_cookie;
1287 struct tempreserve *tr;
1289 ASSERT3U(tx->tx_txg, !=, 0);
1291 if (tr_cookie == NULL)
1294 while ((tr = list_head(tr_list)) != NULL) {
1296 mutex_enter(&tr->tr_ds->dd_lock);
1297 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1299 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1300 mutex_exit(&tr->tr_ds->dd_lock);
1302 arc_tempreserve_clear(tr->tr_size);
1304 list_remove(tr_list, tr);
1305 kmem_free(tr, sizeof (struct tempreserve));
1308 kmem_free(tr_list, sizeof (list_t));
1312 * This should be called from open context when we think we're going to write
1313 * or free space, for example when dirtying data. Be conservative; it's okay
1314 * to write less space or free more, but we don't want to write more or free
1315 * less than the amount specified.
1318 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1320 int64_t parent_space;
1323 mutex_enter(&dd->dd_lock);
1325 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1327 est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1328 parent_space = parent_delta(dd, est_used, space);
1329 mutex_exit(&dd->dd_lock);
1331 /* Make sure that we clean up dd_space_to* */
1332 dsl_dir_dirty(dd, tx);
1334 /* XXX this is potentially expensive and unnecessary... */
1335 if (parent_space && dd->dd_parent)
1336 dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1339 /* call from syncing context when we actually write/free space for this dd */
1341 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1342 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1344 int64_t accounted_delta;
1347 * dsl_dataset_set_refreservation_sync_impl() calls this with
1348 * dd_lock held, so that it can atomically update
1349 * ds->ds_reserved and the dsl_dir accounting, so that
1350 * dsl_dataset_check_quota() can see dataset and dir accounting
1353 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1355 ASSERT(dmu_tx_is_syncing(tx));
1356 ASSERT(type < DD_USED_NUM);
1358 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1361 mutex_enter(&dd->dd_lock);
1363 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1364 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1365 ASSERT(compressed >= 0 ||
1366 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1367 ASSERT(uncompressed >= 0 ||
1368 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1369 dsl_dir_phys(dd)->dd_used_bytes += used;
1370 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1371 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1373 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1375 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1376 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1380 for (t = 0; t < DD_USED_NUM; t++)
1381 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1382 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1386 mutex_exit(&dd->dd_lock);
1388 if (dd->dd_parent != NULL) {
1389 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1390 accounted_delta, compressed, uncompressed, tx);
1391 dsl_dir_transfer_space(dd->dd_parent,
1392 used - accounted_delta,
1393 DD_USED_CHILD_RSRV, DD_USED_CHILD, NULL);
1398 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1399 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1401 ASSERT(tx == NULL || dmu_tx_is_syncing(tx));
1402 ASSERT(oldtype < DD_USED_NUM);
1403 ASSERT(newtype < DD_USED_NUM);
1406 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1410 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1411 mutex_enter(&dd->dd_lock);
1413 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1414 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1415 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1416 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1417 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1418 mutex_exit(&dd->dd_lock);
1421 typedef struct dsl_dir_set_qr_arg {
1422 const char *ddsqra_name;
1423 zprop_source_t ddsqra_source;
1424 uint64_t ddsqra_value;
1425 } dsl_dir_set_qr_arg_t;
1428 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1430 dsl_dir_set_qr_arg_t *ddsqra = arg;
1431 dsl_pool_t *dp = dmu_tx_pool(tx);
1434 uint64_t towrite, newval;
1436 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1440 error = dsl_prop_predict(ds->ds_dir, "quota",
1441 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1443 dsl_dataset_rele(ds, FTAG);
1448 dsl_dataset_rele(ds, FTAG);
1452 mutex_enter(&ds->ds_dir->dd_lock);
1454 * If we are doing the preliminary check in open context, and
1455 * there are pending changes, then don't fail it, since the
1456 * pending changes could under-estimate the amount of space to be
1459 towrite = dsl_dir_space_towrite(ds->ds_dir);
1460 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1461 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1462 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1463 error = SET_ERROR(ENOSPC);
1465 mutex_exit(&ds->ds_dir->dd_lock);
1466 dsl_dataset_rele(ds, FTAG);
1471 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1473 dsl_dir_set_qr_arg_t *ddsqra = arg;
1474 dsl_pool_t *dp = dmu_tx_pool(tx);
1478 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1480 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1481 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1482 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1483 &ddsqra->ddsqra_value, tx);
1485 VERIFY0(dsl_prop_get_int_ds(ds,
1486 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1488 newval = ddsqra->ddsqra_value;
1489 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1490 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1493 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1494 mutex_enter(&ds->ds_dir->dd_lock);
1495 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1496 mutex_exit(&ds->ds_dir->dd_lock);
1497 dsl_dataset_rele(ds, FTAG);
1501 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1503 dsl_dir_set_qr_arg_t ddsqra;
1505 ddsqra.ddsqra_name = ddname;
1506 ddsqra.ddsqra_source = source;
1507 ddsqra.ddsqra_value = quota;
1509 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1510 dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1514 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1516 dsl_dir_set_qr_arg_t *ddsqra = arg;
1517 dsl_pool_t *dp = dmu_tx_pool(tx);
1520 uint64_t newval, used, avail;
1523 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1529 * If we are doing the preliminary check in open context, the
1530 * space estimates may be inaccurate.
1532 if (!dmu_tx_is_syncing(tx)) {
1533 dsl_dataset_rele(ds, FTAG);
1537 error = dsl_prop_predict(ds->ds_dir,
1538 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1539 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1541 dsl_dataset_rele(ds, FTAG);
1545 mutex_enter(&dd->dd_lock);
1546 used = dsl_dir_phys(dd)->dd_used_bytes;
1547 mutex_exit(&dd->dd_lock);
1549 if (dd->dd_parent) {
1550 avail = dsl_dir_space_available(dd->dd_parent,
1553 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1556 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1557 uint64_t delta = MAX(used, newval) -
1558 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1560 if (delta > avail ||
1561 (dsl_dir_phys(dd)->dd_quota > 0 &&
1562 newval > dsl_dir_phys(dd)->dd_quota))
1563 error = SET_ERROR(ENOSPC);
1566 dsl_dataset_rele(ds, FTAG);
1571 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1576 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1578 mutex_enter(&dd->dd_lock);
1579 used = dsl_dir_phys(dd)->dd_used_bytes;
1580 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1581 dsl_dir_phys(dd)->dd_reserved = value;
1583 if (dd->dd_parent != NULL) {
1584 /* Roll up this additional usage into our ancestors */
1585 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1588 mutex_exit(&dd->dd_lock);
1592 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1594 dsl_dir_set_qr_arg_t *ddsqra = arg;
1595 dsl_pool_t *dp = dmu_tx_pool(tx);
1599 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1601 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1602 dsl_prop_set_sync_impl(ds,
1603 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1604 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1605 &ddsqra->ddsqra_value, tx);
1607 VERIFY0(dsl_prop_get_int_ds(ds,
1608 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1610 newval = ddsqra->ddsqra_value;
1611 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1612 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1613 (longlong_t)newval);
1616 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1617 dsl_dataset_rele(ds, FTAG);
1621 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1622 uint64_t reservation)
1624 dsl_dir_set_qr_arg_t ddsqra;
1626 ddsqra.ddsqra_name = ddname;
1627 ddsqra.ddsqra_source = source;
1628 ddsqra.ddsqra_value = reservation;
1630 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1631 dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1635 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1637 for (; ds1; ds1 = ds1->dd_parent) {
1639 for (dd = ds2; dd; dd = dd->dd_parent) {
1648 * If delta is applied to dd, how much of that delta would be applied to
1649 * ancestor? Syncing context only.
1652 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1657 mutex_enter(&dd->dd_lock);
1658 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1659 mutex_exit(&dd->dd_lock);
1660 return (would_change(dd->dd_parent, delta, ancestor));
1663 typedef struct dsl_dir_rename_arg {
1664 const char *ddra_oldname;
1665 const char *ddra_newname;
1667 } dsl_dir_rename_arg_t;
1671 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1674 char namebuf[MAXNAMELEN];
1676 dsl_dataset_name(ds, namebuf);
1678 if (strlen(namebuf) + *deltap >= MAXNAMELEN)
1679 return (SET_ERROR(ENAMETOOLONG));
1684 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1686 dsl_dir_rename_arg_t *ddra = arg;
1687 dsl_pool_t *dp = dmu_tx_pool(tx);
1688 dsl_dir_t *dd, *newparent;
1689 const char *mynewname;
1691 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1693 /* target dir should exist */
1694 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1698 /* new parent should exist */
1699 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1700 &newparent, &mynewname);
1702 dsl_dir_rele(dd, FTAG);
1706 /* can't rename to different pool */
1707 if (dd->dd_pool != newparent->dd_pool) {
1708 dsl_dir_rele(newparent, FTAG);
1709 dsl_dir_rele(dd, FTAG);
1710 return (SET_ERROR(EXDEV));
1713 /* new name should not already exist */
1714 if (mynewname == NULL) {
1715 dsl_dir_rele(newparent, FTAG);
1716 dsl_dir_rele(dd, FTAG);
1717 return (SET_ERROR(EEXIST));
1720 /* if the name length is growing, validate child name lengths */
1722 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1723 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1725 dsl_dir_rele(newparent, FTAG);
1726 dsl_dir_rele(dd, FTAG);
1731 if (dmu_tx_is_syncing(tx)) {
1732 if (spa_feature_is_active(dp->dp_spa,
1733 SPA_FEATURE_FS_SS_LIMIT)) {
1735 * Although this is the check function and we don't
1736 * normally make on-disk changes in check functions,
1737 * we need to do that here.
1739 * Ensure this portion of the tree's counts have been
1740 * initialized in case the new parent has limits set.
1742 dsl_dir_init_fs_ss_count(dd, tx);
1746 if (newparent != dd->dd_parent) {
1747 /* is there enough space? */
1749 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1750 dsl_dir_phys(dd)->dd_reserved);
1751 objset_t *os = dd->dd_pool->dp_meta_objset;
1752 uint64_t fs_cnt = 0;
1753 uint64_t ss_cnt = 0;
1755 if (dsl_dir_is_zapified(dd)) {
1758 err = zap_lookup(os, dd->dd_object,
1759 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1761 if (err != ENOENT && err != 0) {
1762 dsl_dir_rele(newparent, FTAG);
1763 dsl_dir_rele(dd, FTAG);
1768 * have to add 1 for the filesystem itself that we're
1773 err = zap_lookup(os, dd->dd_object,
1774 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1776 if (err != ENOENT && err != 0) {
1777 dsl_dir_rele(newparent, FTAG);
1778 dsl_dir_rele(dd, FTAG);
1783 /* no rename into our descendant */
1784 if (closest_common_ancestor(dd, newparent) == dd) {
1785 dsl_dir_rele(newparent, FTAG);
1786 dsl_dir_rele(dd, FTAG);
1787 return (SET_ERROR(EINVAL));
1790 error = dsl_dir_transfer_possible(dd->dd_parent,
1791 newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
1793 dsl_dir_rele(newparent, FTAG);
1794 dsl_dir_rele(dd, FTAG);
1799 dsl_dir_rele(newparent, FTAG);
1800 dsl_dir_rele(dd, FTAG);
1805 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
1807 dsl_dir_rename_arg_t *ddra = arg;
1808 dsl_pool_t *dp = dmu_tx_pool(tx);
1809 dsl_dir_t *dd, *newparent;
1810 const char *mynewname;
1812 objset_t *mos = dp->dp_meta_objset;
1814 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
1815 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
1818 /* Log this before we change the name. */
1819 spa_history_log_internal_dd(dd, "rename", tx,
1820 "-> %s", ddra->ddra_newname);
1822 if (newparent != dd->dd_parent) {
1823 objset_t *os = dd->dd_pool->dp_meta_objset;
1824 uint64_t fs_cnt = 0;
1825 uint64_t ss_cnt = 0;
1828 * We already made sure the dd counts were initialized in the
1831 if (spa_feature_is_active(dp->dp_spa,
1832 SPA_FEATURE_FS_SS_LIMIT)) {
1833 VERIFY0(zap_lookup(os, dd->dd_object,
1834 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1836 /* add 1 for the filesystem itself that we're moving */
1839 VERIFY0(zap_lookup(os, dd->dd_object,
1840 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1844 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1845 DD_FIELD_FILESYSTEM_COUNT, tx);
1846 dsl_fs_ss_count_adjust(newparent, fs_cnt,
1847 DD_FIELD_FILESYSTEM_COUNT, tx);
1849 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1850 DD_FIELD_SNAPSHOT_COUNT, tx);
1851 dsl_fs_ss_count_adjust(newparent, ss_cnt,
1852 DD_FIELD_SNAPSHOT_COUNT, tx);
1854 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1855 -dsl_dir_phys(dd)->dd_used_bytes,
1856 -dsl_dir_phys(dd)->dd_compressed_bytes,
1857 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1858 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1859 dsl_dir_phys(dd)->dd_used_bytes,
1860 dsl_dir_phys(dd)->dd_compressed_bytes,
1861 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1863 if (dsl_dir_phys(dd)->dd_reserved >
1864 dsl_dir_phys(dd)->dd_used_bytes) {
1865 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1866 dsl_dir_phys(dd)->dd_used_bytes;
1868 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1869 -unused_rsrv, 0, 0, tx);
1870 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
1871 unused_rsrv, 0, 0, tx);
1875 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1877 /* remove from old parent zapobj */
1878 error = zap_remove(mos,
1879 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1883 (void) strcpy(dd->dd_myname, mynewname);
1884 dsl_dir_rele(dd->dd_parent, dd);
1885 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
1886 VERIFY0(dsl_dir_hold_obj(dp,
1887 newparent->dd_object, NULL, dd, &dd->dd_parent));
1889 /* add to new parent zapobj */
1890 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
1891 dd->dd_myname, 8, 1, &dd->dd_object, tx));
1895 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);
1896 zvol_rename_minors(ddra->ddra_oldname, ddra->ddra_newname);
1900 dsl_prop_notify_all(dd);
1902 dsl_dir_rele(newparent, FTAG);
1903 dsl_dir_rele(dd, FTAG);
1907 dsl_dir_rename(const char *oldname, const char *newname)
1909 dsl_dir_rename_arg_t ddra;
1911 ddra.ddra_oldname = oldname;
1912 ddra.ddra_newname = newname;
1913 ddra.ddra_cred = CRED();
1915 return (dsl_sync_task(oldname,
1916 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
1917 3, ZFS_SPACE_CHECK_RESERVED));
1921 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1922 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
1924 dsl_dir_t *ancestor;
1929 ancestor = closest_common_ancestor(sdd, tdd);
1930 adelta = would_change(sdd, -space, ancestor);
1931 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1933 return (SET_ERROR(ENOSPC));
1935 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1939 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1948 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1952 mutex_enter(&dd->dd_lock);
1953 t = dd->dd_snap_cmtime;
1954 mutex_exit(&dd->dd_lock);
1960 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1965 mutex_enter(&dd->dd_lock);
1966 dd->dd_snap_cmtime = t;
1967 mutex_exit(&dd->dd_lock);
1971 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
1973 objset_t *mos = dd->dd_pool->dp_meta_objset;
1974 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
1978 dsl_dir_is_zapified(dsl_dir_t *dd)
1980 dmu_object_info_t doi;
1982 dmu_object_info_from_db(dd->dd_dbuf, &doi);
1983 return (doi.doi_type == DMU_OTN_ZAP_METADATA);