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.
30 #include <sys/dmu_objset.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_prop.h>
35 #include <sys/dsl_synctask.h>
36 #include <sys/dsl_deleg.h>
37 #include <sys/dmu_impl.h>
39 #include <sys/metaslab.h>
43 #include <sys/sunddi.h>
46 #include <sys/zfs_vfsops.h>
48 #include <sys/zfeature.h>
49 #include <sys/policy.h>
50 #include <sys/zfs_znode.h>
51 #include "zfs_namecheck.h"
55 * Filesystem and Snapshot Limits
56 * ------------------------------
58 * These limits are used to restrict the number of filesystems and/or snapshots
59 * that can be created at a given level in the tree or below. A typical
60 * use-case is with a delegated dataset where the administrator wants to ensure
61 * that a user within the zone is not creating too many additional filesystems
62 * or snapshots, even though they're not exceeding their space quota.
64 * The filesystem and snapshot counts are stored as extensible properties. This
65 * capability is controlled by a feature flag and must be enabled to be used.
66 * Once enabled, the feature is not active until the first limit is set. At
67 * that point, future operations to create/destroy filesystems or snapshots
68 * will validate and update the counts.
70 * Because the count properties will not exist before the feature is active,
71 * the counts are updated when a limit is first set on an uninitialized
72 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
73 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
74 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
75 * snapshot count properties on a node indicate uninitialized counts on that
76 * node.) When first setting a limit on an uninitialized node, the code starts
77 * at the filesystem with the new limit and descends into all sub-filesystems
78 * to add the count properties.
80 * In practice this is lightweight since a limit is typically set when the
81 * filesystem is created and thus has no children. Once valid, changing the
82 * limit value won't require a re-traversal since the counts are already valid.
83 * When recursively fixing the counts, if a node with a limit is encountered
84 * during the descent, the counts are known to be valid and there is no need to
85 * descend into that filesystem's children. The counts on filesystems above the
86 * one with the new limit will still be uninitialized, unless a limit is
87 * eventually set on one of those filesystems. The counts are always recursively
88 * updated when a limit is set on a dataset, unless there is already a limit.
89 * When a new limit value is set on a filesystem with an existing limit, it is
90 * possible for the new limit to be less than the current count at that level
91 * since a user who can change the limit is also allowed to exceed the limit.
93 * Once the feature is active, then whenever a filesystem or snapshot is
94 * created, the code recurses up the tree, validating the new count against the
95 * limit at each initialized level. In practice, most levels will not have a
96 * limit set. If there is a limit at any initialized level up the tree, the
97 * check must pass or the creation will fail. Likewise, when a filesystem or
98 * snapshot is destroyed, the counts are recursively adjusted all the way up
99 * the initizized nodes in the tree. Renaming a filesystem into different point
100 * in the tree will first validate, then update the counts on each branch up to
101 * the common ancestor. A receive will also validate the counts and then update
104 * An exception to the above behavior is that the limit is not enforced if the
105 * user has permission to modify the limit. This is primarily so that
106 * recursive snapshots in the global zone always work. We want to prevent a
107 * denial-of-service in which a lower level delegated dataset could max out its
108 * limit and thus block recursive snapshots from being taken in the global zone.
109 * Because of this, it is possible for the snapshot count to be over the limit
110 * and snapshots taken in the global zone could cause a lower level dataset to
111 * hit or exceed its limit. The administrator taking the global zone recursive
112 * snapshot should be aware of this side-effect and behave accordingly.
113 * For consistency, the filesystem limit is also not enforced if the user can
116 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
117 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
118 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
119 * dsl_dir_init_fs_ss_count().
121 * There is a special case when we receive a filesystem that already exists. In
122 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
123 * never update the filesystem counts for temporary clones.
125 * Likewise, we do not update the snapshot counts for temporary snapshots,
126 * such as those created by zfs diff.
129 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
131 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
135 dsl_dir_evict(dmu_buf_t *db, void *arg)
138 dsl_pool_t *dp = dd->dd_pool;
141 for (t = 0; t < TXG_SIZE; t++) {
142 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
143 ASSERT(dd->dd_tempreserved[t] == 0);
144 ASSERT(dd->dd_space_towrite[t] == 0);
148 dsl_dir_rele(dd->dd_parent, dd);
150 spa_close(dd->dd_pool->dp_spa, dd);
153 * The props callback list should have been cleaned up by
156 list_destroy(&dd->dd_prop_cbs);
157 mutex_destroy(&dd->dd_lock);
158 kmem_free(dd, sizeof (dsl_dir_t));
162 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
163 const char *tail, void *tag, dsl_dir_t **ddp)
169 ASSERT(dsl_pool_config_held(dp));
171 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
174 dd = dmu_buf_get_user(dbuf);
177 dmu_object_info_t doi;
178 dmu_object_info_from_db(dbuf, &doi);
179 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
180 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
186 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
187 dd->dd_object = ddobj;
190 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
192 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
193 offsetof(dsl_prop_cb_record_t, cbr_node));
195 dsl_dir_snap_cmtime_update(dd);
197 if (dsl_dir_phys(dd)->dd_parent_obj) {
198 err = dsl_dir_hold_obj(dp,
199 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
207 err = zap_lookup(dp->dp_meta_objset,
208 dsl_dir_phys(dd->dd_parent)->
209 dd_child_dir_zapobj, tail,
210 sizeof (foundobj), 1, &foundobj);
211 ASSERT(err || foundobj == ddobj);
213 (void) strcpy(dd->dd_myname, tail);
215 err = zap_value_search(dp->dp_meta_objset,
216 dsl_dir_phys(dd->dd_parent)->
218 ddobj, 0, dd->dd_myname);
223 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
226 if (dsl_dir_is_clone(dd)) {
227 dmu_buf_t *origin_bonus;
228 dsl_dataset_phys_t *origin_phys;
231 * We can't open the origin dataset, because
232 * that would require opening this dsl_dir.
233 * Just look at its phys directly instead.
235 err = dmu_bonus_hold(dp->dp_meta_objset,
236 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
240 origin_phys = origin_bonus->db_data;
242 origin_phys->ds_creation_txg;
243 dmu_buf_rele(origin_bonus, FTAG);
246 winner = dmu_buf_set_user_ie(dbuf, dd, dsl_dir_evict);
249 dsl_dir_rele(dd->dd_parent, dd);
250 mutex_destroy(&dd->dd_lock);
251 kmem_free(dd, sizeof (dsl_dir_t));
254 spa_open_ref(dp->dp_spa, dd);
259 * The dsl_dir_t has both open-to-close and instantiate-to-evict
260 * holds on the spa. We need the open-to-close holds because
261 * otherwise the spa_refcnt wouldn't change when we open a
262 * dir which the spa also has open, so we could incorrectly
263 * think it was OK to unload/export/destroy the pool. We need
264 * the instantiate-to-evict hold because the dsl_dir_t has a
265 * pointer to the dd_pool, which has a pointer to the spa_t.
267 spa_open_ref(dp->dp_spa, tag);
268 ASSERT3P(dd->dd_pool, ==, dp);
269 ASSERT3U(dd->dd_object, ==, ddobj);
270 ASSERT3P(dd->dd_dbuf, ==, dbuf);
276 dsl_dir_rele(dd->dd_parent, dd);
277 mutex_destroy(&dd->dd_lock);
278 kmem_free(dd, sizeof (dsl_dir_t));
279 dmu_buf_rele(dbuf, tag);
284 dsl_dir_rele(dsl_dir_t *dd, void *tag)
286 dprintf_dd(dd, "%s\n", "");
287 spa_close(dd->dd_pool->dp_spa, tag);
288 dmu_buf_rele(dd->dd_dbuf, tag);
291 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
293 dsl_dir_name(dsl_dir_t *dd, char *buf)
296 dsl_dir_name(dd->dd_parent, buf);
297 (void) strcat(buf, "/");
301 if (!MUTEX_HELD(&dd->dd_lock)) {
303 * recursive mutex so that we can use
304 * dprintf_dd() with dd_lock held
306 mutex_enter(&dd->dd_lock);
307 (void) strcat(buf, dd->dd_myname);
308 mutex_exit(&dd->dd_lock);
310 (void) strcat(buf, dd->dd_myname);
314 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
316 dsl_dir_namelen(dsl_dir_t *dd)
321 /* parent's name + 1 for the "/" */
322 result = dsl_dir_namelen(dd->dd_parent) + 1;
325 if (!MUTEX_HELD(&dd->dd_lock)) {
326 /* see dsl_dir_name */
327 mutex_enter(&dd->dd_lock);
328 result += strlen(dd->dd_myname);
329 mutex_exit(&dd->dd_lock);
331 result += strlen(dd->dd_myname);
338 getcomponent(const char *path, char *component, const char **nextp)
342 if ((path == NULL) || (path[0] == '\0'))
343 return (SET_ERROR(ENOENT));
344 /* This would be a good place to reserve some namespace... */
345 p = strpbrk(path, "/@");
346 if (p && (p[1] == '/' || p[1] == '@')) {
347 /* two separators in a row */
348 return (SET_ERROR(EINVAL));
350 if (p == NULL || p == path) {
352 * if the first thing is an @ or /, it had better be an
353 * @ and it had better not have any more ats or slashes,
354 * and it had better have something after the @.
357 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
358 return (SET_ERROR(EINVAL));
359 if (strlen(path) >= MAXNAMELEN)
360 return (SET_ERROR(ENAMETOOLONG));
361 (void) strcpy(component, path);
363 } else if (p[0] == '/') {
364 if (p - path >= MAXNAMELEN)
365 return (SET_ERROR(ENAMETOOLONG));
366 (void) strncpy(component, path, p - path);
367 component[p - path] = '\0';
369 } else if (p[0] == '@') {
371 * if the next separator is an @, there better not be
374 if (strchr(path, '/'))
375 return (SET_ERROR(EINVAL));
376 if (p - path >= MAXNAMELEN)
377 return (SET_ERROR(ENAMETOOLONG));
378 (void) strncpy(component, path, p - path);
379 component[p - path] = '\0';
381 panic("invalid p=%p", (void *)p);
388 * Return the dsl_dir_t, and possibly the last component which couldn't
389 * be found in *tail. The name must be in the specified dsl_pool_t. This
390 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
391 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
392 * (*tail)[0] == '@' means that the last component is a snapshot.
395 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
396 dsl_dir_t **ddp, const char **tailp)
398 char buf[MAXNAMELEN];
399 const char *spaname, *next, *nextnext = NULL;
404 err = getcomponent(name, buf, &next);
408 /* Make sure the name is in the specified pool. */
409 spaname = spa_name(dp->dp_spa);
410 if (strcmp(buf, spaname) != 0)
411 return (SET_ERROR(EINVAL));
413 ASSERT(dsl_pool_config_held(dp));
415 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
420 while (next != NULL) {
422 err = getcomponent(next, buf, &nextnext);
425 ASSERT(next[0] != '\0');
428 dprintf("looking up %s in obj%lld\n",
429 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
431 err = zap_lookup(dp->dp_meta_objset,
432 dsl_dir_phys(dd)->dd_child_dir_zapobj,
433 buf, sizeof (ddobj), 1, &ddobj);
440 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_ds);
443 dsl_dir_rele(dd, tag);
449 dsl_dir_rele(dd, tag);
454 * It's an error if there's more than one component left, or
455 * tailp==NULL and there's any component left.
458 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
460 dsl_dir_rele(dd, tag);
461 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
462 err = SET_ERROR(ENOENT);
471 * If the counts are already initialized for this filesystem and its
472 * descendants then do nothing, otherwise initialize the counts.
474 * The counts on this filesystem, and those below, may be uninitialized due to
475 * either the use of a pre-existing pool which did not support the
476 * filesystem/snapshot limit feature, or one in which the feature had not yet
479 * Recursively descend the filesystem tree and update the filesystem/snapshot
480 * counts on each filesystem below, then update the cumulative count on the
481 * current filesystem. If the filesystem already has a count set on it,
482 * then we know that its counts, and the counts on the filesystems below it,
483 * are already correct, so we don't have to update this filesystem.
486 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
488 uint64_t my_fs_cnt = 0;
489 uint64_t my_ss_cnt = 0;
490 dsl_pool_t *dp = dd->dd_pool;
491 objset_t *os = dp->dp_meta_objset;
496 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
497 ASSERT(dsl_pool_config_held(dp));
498 ASSERT(dmu_tx_is_syncing(tx));
500 dsl_dir_zapify(dd, tx);
503 * If the filesystem count has already been initialized then we
504 * don't need to recurse down any further.
506 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
509 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
510 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
512 /* Iterate my child dirs */
513 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
514 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
518 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
522 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
523 * temporary datasets.
525 if (chld_dd->dd_myname[0] == '$' ||
526 chld_dd->dd_myname[0] == '%') {
527 dsl_dir_rele(chld_dd, FTAG);
531 my_fs_cnt++; /* count this child */
533 dsl_dir_init_fs_ss_count(chld_dd, tx);
535 VERIFY0(zap_lookup(os, chld_dd->dd_object,
536 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
538 VERIFY0(zap_lookup(os, chld_dd->dd_object,
539 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
542 dsl_dir_rele(chld_dd, FTAG);
545 /* Count my snapshots (we counted children's snapshots above) */
546 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
547 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
549 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
550 zap_cursor_retrieve(zc, za) == 0;
551 zap_cursor_advance(zc)) {
552 /* Don't count temporary snapshots */
553 if (za->za_name[0] != '%')
558 dsl_dataset_rele(ds, FTAG);
560 kmem_free(zc, sizeof (zap_cursor_t));
561 kmem_free(za, sizeof (zap_attribute_t));
563 /* we're in a sync task, update counts */
564 dmu_buf_will_dirty(dd->dd_dbuf, tx);
565 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
566 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
567 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
568 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
572 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
574 char *ddname = (char *)arg;
575 dsl_pool_t *dp = dmu_tx_pool(tx);
580 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
584 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
585 dsl_dataset_rele(ds, FTAG);
586 return (SET_ERROR(ENOTSUP));
590 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
591 dsl_dir_is_zapified(dd) &&
592 zap_contains(dp->dp_meta_objset, dd->dd_object,
593 DD_FIELD_FILESYSTEM_COUNT) == 0) {
594 dsl_dataset_rele(ds, FTAG);
595 return (SET_ERROR(EALREADY));
598 dsl_dataset_rele(ds, FTAG);
603 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
605 char *ddname = (char *)arg;
606 dsl_pool_t *dp = dmu_tx_pool(tx);
610 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
612 spa = dsl_dataset_get_spa(ds);
614 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
616 * Since the feature was not active and we're now setting a
617 * limit, increment the feature-active counter so that the
618 * feature becomes active for the first time.
620 * We are already in a sync task so we can update the MOS.
622 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
626 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
627 * we need to ensure the counts are correct. Descend down the tree from
628 * this point and update all of the counts to be accurate.
630 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
632 dsl_dataset_rele(ds, FTAG);
636 * Make sure the feature is enabled and activate it if necessary.
637 * Since we're setting a limit, ensure the on-disk counts are valid.
638 * This is only called by the ioctl path when setting a limit value.
640 * We do not need to validate the new limit, since users who can change the
641 * limit are also allowed to exceed the limit.
644 dsl_dir_activate_fs_ss_limit(const char *ddname)
648 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
649 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
650 ZFS_SPACE_CHECK_RESERVED);
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);
694 if (crgetzoneid(cr) != GLOBAL_ZONEID)
696 return (ENFORCE_ALWAYS);
698 if (secpolicy_zfs(cr) == 0)
699 return (ENFORCE_NEVER);
702 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
703 return (ENFORCE_ALWAYS);
705 ASSERT(dsl_pool_config_held(dd->dd_pool));
707 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
708 return (ENFORCE_ALWAYS);
710 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
711 /* Only root can access zoned fs's from the GZ */
712 enforce = ENFORCE_ALWAYS;
714 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
715 enforce = ENFORCE_ABOVE;
718 dsl_dataset_rele(ds, FTAG);
723 * Check if adding additional child filesystem(s) would exceed any filesystem
724 * limits or adding additional snapshot(s) would exceed any snapshot limits.
725 * The prop argument indicates which limit to check.
727 * Note that all filesystem limits up to the root (or the highest
728 * initialized) filesystem or the given ancestor must be satisfied.
731 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
732 dsl_dir_t *ancestor, cred_t *cr)
734 objset_t *os = dd->dd_pool->dp_meta_objset;
735 uint64_t limit, count;
737 enforce_res_t enforce;
740 ASSERT(dsl_pool_config_held(dd->dd_pool));
741 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
742 prop == ZFS_PROP_SNAPSHOT_LIMIT);
745 * If we're allowed to change the limit, don't enforce the limit
746 * e.g. this can happen if a snapshot is taken by an administrative
747 * user in the global zone (i.e. a recursive snapshot by root).
748 * However, we must handle the case of delegated permissions where we
749 * are allowed to change the limit on the current dataset, but there
750 * is another limit in the tree above.
752 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
753 if (enforce == ENFORCE_NEVER)
757 * e.g. if renaming a dataset with no snapshots, count adjustment
763 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
765 * We don't enforce the limit for temporary snapshots. This is
766 * indicated by a NULL cred_t argument.
771 count_prop = DD_FIELD_SNAPSHOT_COUNT;
773 count_prop = DD_FIELD_FILESYSTEM_COUNT;
777 * If an ancestor has been provided, stop checking the limit once we
778 * hit that dir. We need this during rename so that we don't overcount
779 * the check once we recurse up to the common ancestor.
785 * If we hit an uninitialized node while recursing up the tree, we can
786 * stop since we know there is no limit here (or above). The counts are
787 * not valid on this node and we know we won't touch this node's counts.
789 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
790 count_prop, sizeof (count), 1, &count) == ENOENT)
793 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
798 /* Is there a limit which we've hit? */
799 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
800 return (SET_ERROR(EDQUOT));
802 if (dd->dd_parent != NULL)
803 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
810 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
811 * parents. When a new filesystem/snapshot is created, increment the count on
812 * all parents, and when a filesystem/snapshot is destroyed, decrement the
816 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
820 objset_t *os = dd->dd_pool->dp_meta_objset;
823 ASSERT(dsl_pool_config_held(dd->dd_pool));
824 ASSERT(dmu_tx_is_syncing(tx));
825 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
826 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
829 * When we receive an incremental stream into a filesystem that already
830 * exists, a temporary clone is created. We don't count this temporary
831 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
832 * $MOS & $ORIGIN) objsets.
834 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
835 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
839 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
845 * If we hit an uninitialized node while recursing up the tree, we can
846 * stop since we know the counts are not valid on this node and we
847 * know we shouldn't touch this node's counts. An uninitialized count
848 * on the node indicates that either the feature has not yet been
849 * activated or there are no limits on this part of the tree.
851 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
852 prop, sizeof (count), 1, &count)) == ENOENT)
857 /* Use a signed verify to make sure we're not neg. */
858 VERIFY3S(count, >=, 0);
860 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
863 /* Roll up this additional count into our ancestors */
864 if (dd->dd_parent != NULL)
865 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
869 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
872 objset_t *mos = dp->dp_meta_objset;
874 dsl_dir_phys_t *ddphys;
877 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
878 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
880 VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
881 name, sizeof (uint64_t), 1, &ddobj, tx));
883 /* it's the root dir */
884 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
885 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
887 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
888 dmu_buf_will_dirty(dbuf, tx);
889 ddphys = dbuf->db_data;
891 ddphys->dd_creation_time = gethrestime_sec();
893 ddphys->dd_parent_obj = pds->dd_object;
895 /* update the filesystem counts */
896 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
898 ddphys->dd_props_zapobj = zap_create(mos,
899 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
900 ddphys->dd_child_dir_zapobj = zap_create(mos,
901 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
902 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
903 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
904 dmu_buf_rele(dbuf, FTAG);
910 dsl_dir_is_clone(dsl_dir_t *dd)
912 return (dsl_dir_phys(dd)->dd_origin_obj &&
913 (dd->dd_pool->dp_origin_snap == NULL ||
914 dsl_dir_phys(dd)->dd_origin_obj !=
915 dd->dd_pool->dp_origin_snap->ds_object));
919 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
921 mutex_enter(&dd->dd_lock);
922 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
923 dsl_dir_phys(dd)->dd_used_bytes);
924 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
925 dsl_dir_phys(dd)->dd_quota);
926 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
927 dsl_dir_phys(dd)->dd_reserved);
928 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
929 dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
930 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
931 dsl_dir_phys(dd)->dd_compressed_bytes));
932 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
933 dsl_dir_phys(dd)->dd_uncompressed_bytes);
934 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
935 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
936 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
937 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
938 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
939 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
940 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
941 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
942 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
943 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
945 mutex_exit(&dd->dd_lock);
947 if (dsl_dir_is_zapified(dd)) {
949 objset_t *os = dd->dd_pool->dp_meta_objset;
951 if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
952 sizeof (count), 1, &count) == 0) {
953 dsl_prop_nvlist_add_uint64(nv,
954 ZFS_PROP_FILESYSTEM_COUNT, count);
956 if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
957 sizeof (count), 1, &count) == 0) {
958 dsl_prop_nvlist_add_uint64(nv,
959 ZFS_PROP_SNAPSHOT_COUNT, count);
963 if (dsl_dir_is_clone(dd)) {
965 char buf[MAXNAMELEN];
967 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
968 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
969 dsl_dataset_name(ds, buf);
970 dsl_dataset_rele(ds, FTAG);
971 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
976 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
978 dsl_pool_t *dp = dd->dd_pool;
980 ASSERT(dsl_dir_phys(dd));
982 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
983 /* up the hold count until we can be written out */
984 dmu_buf_add_ref(dd->dd_dbuf, dd);
989 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
991 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
992 uint64_t new_accounted =
993 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
994 return (new_accounted - old_accounted);
998 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1000 ASSERT(dmu_tx_is_syncing(tx));
1002 mutex_enter(&dd->dd_lock);
1003 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1004 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1005 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1006 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1007 mutex_exit(&dd->dd_lock);
1009 /* release the hold from dsl_dir_dirty */
1010 dmu_buf_rele(dd->dd_dbuf, dd);
1014 dsl_dir_space_towrite(dsl_dir_t *dd)
1019 ASSERT(MUTEX_HELD(&dd->dd_lock));
1021 for (i = 0; i < TXG_SIZE; i++) {
1022 space += dd->dd_space_towrite[i&TXG_MASK];
1023 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1029 * How much space would dd have available if ancestor had delta applied
1030 * to it? If ondiskonly is set, we're only interested in what's
1031 * on-disk, not estimated pending changes.
1034 dsl_dir_space_available(dsl_dir_t *dd,
1035 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1037 uint64_t parentspace, myspace, quota, used;
1040 * If there are no restrictions otherwise, assume we have
1041 * unlimited space available.
1044 parentspace = UINT64_MAX;
1046 if (dd->dd_parent != NULL) {
1047 parentspace = dsl_dir_space_available(dd->dd_parent,
1048 ancestor, delta, ondiskonly);
1051 mutex_enter(&dd->dd_lock);
1052 if (dsl_dir_phys(dd)->dd_quota != 0)
1053 quota = dsl_dir_phys(dd)->dd_quota;
1054 used = dsl_dir_phys(dd)->dd_used_bytes;
1056 used += dsl_dir_space_towrite(dd);
1058 if (dd->dd_parent == NULL) {
1059 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1060 quota = MIN(quota, poolsize);
1063 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1065 * We have some space reserved, in addition to what our
1068 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1071 if (dd == ancestor) {
1073 ASSERT(used >= -delta);
1075 if (parentspace != UINT64_MAX)
1076 parentspace -= delta;
1084 * the lesser of the space provided by our parent and
1085 * the space left in our quota
1087 myspace = MIN(parentspace, quota - used);
1090 mutex_exit(&dd->dd_lock);
1095 struct tempreserve {
1096 list_node_t tr_node;
1102 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1103 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
1104 dmu_tx_t *tx, boolean_t first)
1106 uint64_t txg = tx->tx_txg;
1107 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1108 uint64_t deferred = 0;
1109 struct tempreserve *tr;
1110 int retval = EDQUOT;
1111 int txgidx = txg & TXG_MASK;
1113 uint64_t ref_rsrv = 0;
1115 ASSERT3U(txg, !=, 0);
1116 ASSERT3S(asize, >, 0);
1118 mutex_enter(&dd->dd_lock);
1121 * Check against the dsl_dir's quota. We don't add in the delta
1122 * when checking for over-quota because they get one free hit.
1124 est_inflight = dsl_dir_space_towrite(dd);
1125 for (i = 0; i < TXG_SIZE; i++)
1126 est_inflight += dd->dd_tempreserved[i];
1127 used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1130 * On the first iteration, fetch the dataset's used-on-disk and
1131 * refreservation values. Also, if checkrefquota is set, test if
1132 * allocating this space would exceed the dataset's refquota.
1134 if (first && tx->tx_objset) {
1136 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1138 error = dsl_dataset_check_quota(ds, checkrefquota,
1139 asize, est_inflight, &used_on_disk, &ref_rsrv);
1141 mutex_exit(&dd->dd_lock);
1147 * If this transaction will result in a net free of space,
1148 * we want to let it through.
1150 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1153 quota = dsl_dir_phys(dd)->dd_quota;
1156 * Adjust the quota against the actual pool size at the root
1157 * minus any outstanding deferred frees.
1158 * To ensure that it's possible to remove files from a full
1159 * pool without inducing transient overcommits, we throttle
1160 * netfree transactions against a quota that is slightly larger,
1161 * but still within the pool's allocation slop. In cases where
1162 * we're very close to full, this will allow a steady trickle of
1163 * removes to get through.
1165 if (dd->dd_parent == NULL) {
1166 spa_t *spa = dd->dd_pool->dp_spa;
1167 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1168 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1169 if (poolsize - deferred < quota) {
1170 quota = poolsize - deferred;
1176 * If they are requesting more space, and our current estimate
1177 * is over quota, they get to try again unless the actual
1178 * on-disk is over quota and there are no pending changes (which
1179 * may free up space for us).
1181 if (used_on_disk + est_inflight >= quota) {
1182 if (est_inflight > 0 || used_on_disk < quota ||
1183 (retval == ENOSPC && used_on_disk < quota + deferred))
1185 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1186 "quota=%lluK tr=%lluK err=%d\n",
1187 used_on_disk>>10, est_inflight>>10,
1188 quota>>10, asize>>10, retval);
1189 mutex_exit(&dd->dd_lock);
1190 return (SET_ERROR(retval));
1193 /* We need to up our estimated delta before dropping dd_lock */
1194 dd->dd_tempreserved[txgidx] += asize;
1196 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1198 mutex_exit(&dd->dd_lock);
1200 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1202 tr->tr_size = asize;
1203 list_insert_tail(tr_list, tr);
1205 /* see if it's OK with our parent */
1206 if (dd->dd_parent && parent_rsrv) {
1207 boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1209 return (dsl_dir_tempreserve_impl(dd->dd_parent,
1210 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1217 * Reserve space in this dsl_dir, to be used in this tx's txg.
1218 * After the space has been dirtied (and dsl_dir_willuse_space()
1219 * has been called), the reservation should be canceled, using
1220 * dsl_dir_tempreserve_clear().
1223 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1224 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1234 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1235 list_create(tr_list, sizeof (struct tempreserve),
1236 offsetof(struct tempreserve, tr_node));
1237 ASSERT3S(asize, >, 0);
1238 ASSERT3S(fsize, >=, 0);
1240 err = arc_tempreserve_space(lsize, tx->tx_txg);
1242 struct tempreserve *tr;
1244 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1245 tr->tr_size = lsize;
1246 list_insert_tail(tr_list, tr);
1248 if (err == EAGAIN) {
1250 * If arc_memory_throttle() detected that pageout
1251 * is running and we are low on memory, we delay new
1252 * non-pageout transactions to give pageout an
1255 * It is unfortunate to be delaying while the caller's
1258 txg_delay(dd->dd_pool, tx->tx_txg,
1259 MSEC2NSEC(10), MSEC2NSEC(10));
1260 err = SET_ERROR(ERESTART);
1265 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
1266 FALSE, asize > usize, tr_list, tx, TRUE);
1270 dsl_dir_tempreserve_clear(tr_list, tx);
1272 *tr_cookiep = tr_list;
1278 * Clear a temporary reservation that we previously made with
1279 * dsl_dir_tempreserve_space().
1282 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1284 int txgidx = tx->tx_txg & TXG_MASK;
1285 list_t *tr_list = tr_cookie;
1286 struct tempreserve *tr;
1288 ASSERT3U(tx->tx_txg, !=, 0);
1290 if (tr_cookie == NULL)
1293 while ((tr = list_head(tr_list)) != NULL) {
1295 mutex_enter(&tr->tr_ds->dd_lock);
1296 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1298 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1299 mutex_exit(&tr->tr_ds->dd_lock);
1301 arc_tempreserve_clear(tr->tr_size);
1303 list_remove(tr_list, tr);
1304 kmem_free(tr, sizeof (struct tempreserve));
1307 kmem_free(tr_list, sizeof (list_t));
1311 * This should be called from open context when we think we're going to write
1312 * or free space, for example when dirtying data. Be conservative; it's okay
1313 * to write less space or free more, but we don't want to write more or free
1314 * less than the amount specified.
1317 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1319 int64_t parent_space;
1322 mutex_enter(&dd->dd_lock);
1324 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1326 est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1327 parent_space = parent_delta(dd, est_used, space);
1328 mutex_exit(&dd->dd_lock);
1330 /* Make sure that we clean up dd_space_to* */
1331 dsl_dir_dirty(dd, tx);
1333 /* XXX this is potentially expensive and unnecessary... */
1334 if (parent_space && dd->dd_parent)
1335 dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1338 /* call from syncing context when we actually write/free space for this dd */
1340 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1341 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1343 int64_t accounted_delta;
1346 * dsl_dataset_set_refreservation_sync_impl() calls this with
1347 * dd_lock held, so that it can atomically update
1348 * ds->ds_reserved and the dsl_dir accounting, so that
1349 * dsl_dataset_check_quota() can see dataset and dir accounting
1352 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1354 ASSERT(dmu_tx_is_syncing(tx));
1355 ASSERT(type < DD_USED_NUM);
1357 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1360 mutex_enter(&dd->dd_lock);
1362 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1363 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1364 ASSERT(compressed >= 0 ||
1365 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1366 ASSERT(uncompressed >= 0 ||
1367 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1368 dsl_dir_phys(dd)->dd_used_bytes += used;
1369 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1370 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1372 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1374 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1375 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1379 for (t = 0; t < DD_USED_NUM; t++)
1380 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1381 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1385 mutex_exit(&dd->dd_lock);
1387 if (dd->dd_parent != NULL) {
1388 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1389 accounted_delta, compressed, uncompressed, tx);
1390 dsl_dir_transfer_space(dd->dd_parent,
1391 used - accounted_delta,
1392 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1397 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1398 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1400 ASSERT(dmu_tx_is_syncing(tx));
1401 ASSERT(oldtype < DD_USED_NUM);
1402 ASSERT(newtype < DD_USED_NUM);
1405 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1408 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1409 mutex_enter(&dd->dd_lock);
1411 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1412 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1413 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1414 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1415 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1416 mutex_exit(&dd->dd_lock);
1419 typedef struct dsl_dir_set_qr_arg {
1420 const char *ddsqra_name;
1421 zprop_source_t ddsqra_source;
1422 uint64_t ddsqra_value;
1423 } dsl_dir_set_qr_arg_t;
1426 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1428 dsl_dir_set_qr_arg_t *ddsqra = arg;
1429 dsl_pool_t *dp = dmu_tx_pool(tx);
1432 uint64_t towrite, newval;
1434 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1438 error = dsl_prop_predict(ds->ds_dir, "quota",
1439 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1441 dsl_dataset_rele(ds, FTAG);
1446 dsl_dataset_rele(ds, FTAG);
1450 mutex_enter(&ds->ds_dir->dd_lock);
1452 * If we are doing the preliminary check in open context, and
1453 * there are pending changes, then don't fail it, since the
1454 * pending changes could under-estimate the amount of space to be
1457 towrite = dsl_dir_space_towrite(ds->ds_dir);
1458 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1459 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1460 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1461 error = SET_ERROR(ENOSPC);
1463 mutex_exit(&ds->ds_dir->dd_lock);
1464 dsl_dataset_rele(ds, FTAG);
1469 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1471 dsl_dir_set_qr_arg_t *ddsqra = arg;
1472 dsl_pool_t *dp = dmu_tx_pool(tx);
1476 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1478 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1479 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1480 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1481 &ddsqra->ddsqra_value, tx);
1483 VERIFY0(dsl_prop_get_int_ds(ds,
1484 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1486 newval = ddsqra->ddsqra_value;
1487 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1488 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1491 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1492 mutex_enter(&ds->ds_dir->dd_lock);
1493 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1494 mutex_exit(&ds->ds_dir->dd_lock);
1495 dsl_dataset_rele(ds, FTAG);
1499 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1501 dsl_dir_set_qr_arg_t ddsqra;
1503 ddsqra.ddsqra_name = ddname;
1504 ddsqra.ddsqra_source = source;
1505 ddsqra.ddsqra_value = quota;
1507 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1508 dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1512 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1514 dsl_dir_set_qr_arg_t *ddsqra = arg;
1515 dsl_pool_t *dp = dmu_tx_pool(tx);
1518 uint64_t newval, used, avail;
1521 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1527 * If we are doing the preliminary check in open context, the
1528 * space estimates may be inaccurate.
1530 if (!dmu_tx_is_syncing(tx)) {
1531 dsl_dataset_rele(ds, FTAG);
1535 error = dsl_prop_predict(ds->ds_dir,
1536 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1537 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1539 dsl_dataset_rele(ds, FTAG);
1543 mutex_enter(&dd->dd_lock);
1544 used = dsl_dir_phys(dd)->dd_used_bytes;
1545 mutex_exit(&dd->dd_lock);
1547 if (dd->dd_parent) {
1548 avail = dsl_dir_space_available(dd->dd_parent,
1551 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1554 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1555 uint64_t delta = MAX(used, newval) -
1556 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1558 if (delta > avail ||
1559 (dsl_dir_phys(dd)->dd_quota > 0 &&
1560 newval > dsl_dir_phys(dd)->dd_quota))
1561 error = SET_ERROR(ENOSPC);
1564 dsl_dataset_rele(ds, FTAG);
1569 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1574 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1576 mutex_enter(&dd->dd_lock);
1577 used = dsl_dir_phys(dd)->dd_used_bytes;
1578 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1579 dsl_dir_phys(dd)->dd_reserved = value;
1581 if (dd->dd_parent != NULL) {
1582 /* Roll up this additional usage into our ancestors */
1583 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1586 mutex_exit(&dd->dd_lock);
1590 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1592 dsl_dir_set_qr_arg_t *ddsqra = arg;
1593 dsl_pool_t *dp = dmu_tx_pool(tx);
1597 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1599 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1600 dsl_prop_set_sync_impl(ds,
1601 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1602 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1603 &ddsqra->ddsqra_value, tx);
1605 VERIFY0(dsl_prop_get_int_ds(ds,
1606 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1608 newval = ddsqra->ddsqra_value;
1609 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1610 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1611 (longlong_t)newval);
1614 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1615 dsl_dataset_rele(ds, FTAG);
1619 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1620 uint64_t reservation)
1622 dsl_dir_set_qr_arg_t ddsqra;
1624 ddsqra.ddsqra_name = ddname;
1625 ddsqra.ddsqra_source = source;
1626 ddsqra.ddsqra_value = reservation;
1628 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1629 dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1633 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1635 for (; ds1; ds1 = ds1->dd_parent) {
1637 for (dd = ds2; dd; dd = dd->dd_parent) {
1646 * If delta is applied to dd, how much of that delta would be applied to
1647 * ancestor? Syncing context only.
1650 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1655 mutex_enter(&dd->dd_lock);
1656 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1657 mutex_exit(&dd->dd_lock);
1658 return (would_change(dd->dd_parent, delta, ancestor));
1661 typedef struct dsl_dir_rename_arg {
1662 const char *ddra_oldname;
1663 const char *ddra_newname;
1665 } dsl_dir_rename_arg_t;
1669 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1672 char namebuf[MAXNAMELEN];
1674 dsl_dataset_name(ds, namebuf);
1676 if (strlen(namebuf) + *deltap >= MAXNAMELEN)
1677 return (SET_ERROR(ENAMETOOLONG));
1682 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1684 dsl_dir_rename_arg_t *ddra = arg;
1685 dsl_pool_t *dp = dmu_tx_pool(tx);
1686 dsl_dir_t *dd, *newparent;
1687 const char *mynewname;
1689 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1691 /* target dir should exist */
1692 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1696 /* new parent should exist */
1697 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1698 &newparent, &mynewname);
1700 dsl_dir_rele(dd, FTAG);
1704 /* can't rename to different pool */
1705 if (dd->dd_pool != newparent->dd_pool) {
1706 dsl_dir_rele(newparent, FTAG);
1707 dsl_dir_rele(dd, FTAG);
1708 return (SET_ERROR(ENXIO));
1711 /* new name should not already exist */
1712 if (mynewname == NULL) {
1713 dsl_dir_rele(newparent, FTAG);
1714 dsl_dir_rele(dd, FTAG);
1715 return (SET_ERROR(EEXIST));
1718 /* if the name length is growing, validate child name lengths */
1720 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1721 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1723 dsl_dir_rele(newparent, FTAG);
1724 dsl_dir_rele(dd, FTAG);
1729 if (dmu_tx_is_syncing(tx)) {
1730 if (spa_feature_is_active(dp->dp_spa,
1731 SPA_FEATURE_FS_SS_LIMIT)) {
1733 * Although this is the check function and we don't
1734 * normally make on-disk changes in check functions,
1735 * we need to do that here.
1737 * Ensure this portion of the tree's counts have been
1738 * initialized in case the new parent has limits set.
1740 dsl_dir_init_fs_ss_count(dd, tx);
1744 if (newparent != dd->dd_parent) {
1745 /* is there enough space? */
1747 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1748 dsl_dir_phys(dd)->dd_reserved);
1749 objset_t *os = dd->dd_pool->dp_meta_objset;
1750 uint64_t fs_cnt = 0;
1751 uint64_t ss_cnt = 0;
1753 if (dsl_dir_is_zapified(dd)) {
1756 err = zap_lookup(os, dd->dd_object,
1757 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1759 if (err != ENOENT && err != 0) {
1760 dsl_dir_rele(newparent, FTAG);
1761 dsl_dir_rele(dd, FTAG);
1766 * have to add 1 for the filesystem itself that we're
1771 err = zap_lookup(os, dd->dd_object,
1772 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1774 if (err != ENOENT && err != 0) {
1775 dsl_dir_rele(newparent, FTAG);
1776 dsl_dir_rele(dd, FTAG);
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_active(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));
1893 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);
1894 zvol_rename_minors(ddra->ddra_oldname, ddra->ddra_newname);
1898 dsl_prop_notify_all(dd);
1900 dsl_dir_rele(newparent, FTAG);
1901 dsl_dir_rele(dd, FTAG);
1905 dsl_dir_rename(const char *oldname, const char *newname)
1907 dsl_dir_rename_arg_t ddra;
1909 ddra.ddra_oldname = oldname;
1910 ddra.ddra_newname = newname;
1911 ddra.ddra_cred = CRED();
1913 return (dsl_sync_task(oldname,
1914 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
1915 3, ZFS_SPACE_CHECK_RESERVED));
1919 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1920 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
1922 dsl_dir_t *ancestor;
1927 ancestor = closest_common_ancestor(sdd, tdd);
1928 adelta = would_change(sdd, -space, ancestor);
1929 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1931 return (SET_ERROR(ENOSPC));
1933 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1937 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1946 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1950 mutex_enter(&dd->dd_lock);
1951 t = dd->dd_snap_cmtime;
1952 mutex_exit(&dd->dd_lock);
1958 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1963 mutex_enter(&dd->dd_lock);
1964 dd->dd_snap_cmtime = t;
1965 mutex_exit(&dd->dd_lock);
1969 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
1971 objset_t *mos = dd->dd_pool->dp_meta_objset;
1972 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
1976 dsl_dir_is_zapified(dsl_dir_t *dd)
1978 dmu_object_info_t doi;
1980 dmu_object_info_from_db(dd->dd_dbuf, &doi);
1981 return (doi.doi_type == DMU_OTN_ZAP_METADATA);