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 (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_prop.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/dsl_deleg.h>
39 #include <sys/dmu_impl.h>
41 #include <sys/metaslab.h>
45 #include <sys/sunddi.h>
48 #include <sys/zfs_vfsops.h>
50 #include <sys/zfeature.h>
51 #include <sys/policy.h>
52 #include <sys/zfs_znode.h>
53 #include "zfs_namecheck.h"
57 * Filesystem and Snapshot Limits
58 * ------------------------------
60 * These limits are used to restrict the number of filesystems and/or snapshots
61 * that can be created at a given level in the tree or below. A typical
62 * use-case is with a delegated dataset where the administrator wants to ensure
63 * that a user within the zone is not creating too many additional filesystems
64 * or snapshots, even though they're not exceeding their space quota.
66 * The filesystem and snapshot counts are stored as extensible properties. This
67 * capability is controlled by a feature flag and must be enabled to be used.
68 * Once enabled, the feature is not active until the first limit is set. At
69 * that point, future operations to create/destroy filesystems or snapshots
70 * will validate and update the counts.
72 * Because the count properties will not exist before the feature is active,
73 * the counts are updated when a limit is first set on an uninitialized
74 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
75 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
76 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
77 * snapshot count properties on a node indicate uninitialized counts on that
78 * node.) When first setting a limit on an uninitialized node, the code starts
79 * at the filesystem with the new limit and descends into all sub-filesystems
80 * to add the count properties.
82 * In practice this is lightweight since a limit is typically set when the
83 * filesystem is created and thus has no children. Once valid, changing the
84 * limit value won't require a re-traversal since the counts are already valid.
85 * When recursively fixing the counts, if a node with a limit is encountered
86 * during the descent, the counts are known to be valid and there is no need to
87 * descend into that filesystem's children. The counts on filesystems above the
88 * one with the new limit will still be uninitialized, unless a limit is
89 * eventually set on one of those filesystems. The counts are always recursively
90 * updated when a limit is set on a dataset, unless there is already a limit.
91 * When a new limit value is set on a filesystem with an existing limit, it is
92 * possible for the new limit to be less than the current count at that level
93 * since a user who can change the limit is also allowed to exceed the limit.
95 * Once the feature is active, then whenever a filesystem or snapshot is
96 * created, the code recurses up the tree, validating the new count against the
97 * limit at each initialized level. In practice, most levels will not have a
98 * limit set. If there is a limit at any initialized level up the tree, the
99 * check must pass or the creation will fail. Likewise, when a filesystem or
100 * snapshot is destroyed, the counts are recursively adjusted all the way up
101 * the initizized nodes in the tree. Renaming a filesystem into different point
102 * in the tree will first validate, then update the counts on each branch up to
103 * the common ancestor. A receive will also validate the counts and then update
106 * An exception to the above behavior is that the limit is not enforced if the
107 * user has permission to modify the limit. This is primarily so that
108 * recursive snapshots in the global zone always work. We want to prevent a
109 * denial-of-service in which a lower level delegated dataset could max out its
110 * limit and thus block recursive snapshots from being taken in the global zone.
111 * Because of this, it is possible for the snapshot count to be over the limit
112 * and snapshots taken in the global zone could cause a lower level dataset to
113 * hit or exceed its limit. The administrator taking the global zone recursive
114 * snapshot should be aware of this side-effect and behave accordingly.
115 * For consistency, the filesystem limit is also not enforced if the user can
118 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
119 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
120 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
121 * dsl_dir_init_fs_ss_count().
123 * There is a special case when we receive a filesystem that already exists. In
124 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
125 * never update the filesystem counts for temporary clones.
127 * Likewise, we do not update the snapshot counts for temporary snapshots,
128 * such as those created by zfs diff.
131 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
133 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
136 dsl_dir_evict(void *dbu)
139 dsl_pool_t *dp = dd->dd_pool;
144 for (t = 0; t < TXG_SIZE; t++) {
145 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
146 ASSERT(dd->dd_tempreserved[t] == 0);
147 ASSERT(dd->dd_space_towrite[t] == 0);
151 dsl_dir_async_rele(dd->dd_parent, dd);
153 spa_async_close(dd->dd_pool->dp_spa, dd);
156 * The props callback list should have been cleaned up by
159 list_destroy(&dd->dd_prop_cbs);
160 mutex_destroy(&dd->dd_lock);
161 kmem_free(dd, sizeof (dsl_dir_t));
165 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
166 const char *tail, void *tag, dsl_dir_t **ddp)
172 ASSERT(dsl_pool_config_held(dp));
174 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
177 dd = dmu_buf_get_user(dbuf);
180 dmu_object_info_t doi;
181 dmu_object_info_from_db(dbuf, &doi);
182 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
183 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
189 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
190 dd->dd_object = ddobj;
193 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
195 list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t),
196 offsetof(dsl_prop_cb_record_t, cbr_node));
198 dsl_dir_snap_cmtime_update(dd);
200 if (dsl_dir_phys(dd)->dd_parent_obj) {
201 err = dsl_dir_hold_obj(dp,
202 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
210 err = zap_lookup(dp->dp_meta_objset,
211 dsl_dir_phys(dd->dd_parent)->
212 dd_child_dir_zapobj, tail,
213 sizeof (foundobj), 1, &foundobj);
214 ASSERT(err || foundobj == ddobj);
216 (void) strcpy(dd->dd_myname, tail);
218 err = zap_value_search(dp->dp_meta_objset,
219 dsl_dir_phys(dd->dd_parent)->
221 ddobj, 0, dd->dd_myname);
226 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
229 if (dsl_dir_is_clone(dd)) {
230 dmu_buf_t *origin_bonus;
231 dsl_dataset_phys_t *origin_phys;
234 * We can't open the origin dataset, because
235 * that would require opening this dsl_dir.
236 * Just look at its phys directly instead.
238 err = dmu_bonus_hold(dp->dp_meta_objset,
239 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
243 origin_phys = origin_bonus->db_data;
245 origin_phys->ds_creation_txg;
246 dmu_buf_rele(origin_bonus, FTAG);
249 dmu_buf_init_user(&dd->dd_dbu, dsl_dir_evict, &dd->dd_dbuf);
250 winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
251 if (winner != NULL) {
253 dsl_dir_rele(dd->dd_parent, dd);
254 mutex_destroy(&dd->dd_lock);
255 kmem_free(dd, sizeof (dsl_dir_t));
258 spa_open_ref(dp->dp_spa, dd);
263 * The dsl_dir_t has both open-to-close and instantiate-to-evict
264 * holds on the spa. We need the open-to-close holds because
265 * otherwise the spa_refcnt wouldn't change when we open a
266 * dir which the spa also has open, so we could incorrectly
267 * think it was OK to unload/export/destroy the pool. We need
268 * the instantiate-to-evict hold because the dsl_dir_t has a
269 * pointer to the dd_pool, which has a pointer to the spa_t.
271 spa_open_ref(dp->dp_spa, tag);
272 ASSERT3P(dd->dd_pool, ==, dp);
273 ASSERT3U(dd->dd_object, ==, ddobj);
274 ASSERT3P(dd->dd_dbuf, ==, dbuf);
280 dsl_dir_rele(dd->dd_parent, dd);
281 mutex_destroy(&dd->dd_lock);
282 kmem_free(dd, sizeof (dsl_dir_t));
283 dmu_buf_rele(dbuf, tag);
288 dsl_dir_rele(dsl_dir_t *dd, void *tag)
290 dprintf_dd(dd, "%s\n", "");
291 spa_close(dd->dd_pool->dp_spa, tag);
292 dmu_buf_rele(dd->dd_dbuf, tag);
296 * Remove a reference to the given dsl dir that is being asynchronously
297 * released. Async releases occur from a taskq performing eviction of
298 * dsl datasets and dirs. This process is identical to a normal release
299 * with the exception of using the async API for releasing the reference on
303 dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
305 dprintf_dd(dd, "%s\n", "");
306 spa_async_close(dd->dd_pool->dp_spa, tag);
307 dmu_buf_rele(dd->dd_dbuf, tag);
310 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
312 dsl_dir_name(dsl_dir_t *dd, char *buf)
315 dsl_dir_name(dd->dd_parent, buf);
316 (void) strcat(buf, "/");
320 if (!MUTEX_HELD(&dd->dd_lock)) {
322 * recursive mutex so that we can use
323 * dprintf_dd() with dd_lock held
325 mutex_enter(&dd->dd_lock);
326 (void) strcat(buf, dd->dd_myname);
327 mutex_exit(&dd->dd_lock);
329 (void) strcat(buf, dd->dd_myname);
333 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
335 dsl_dir_namelen(dsl_dir_t *dd)
340 /* parent's name + 1 for the "/" */
341 result = dsl_dir_namelen(dd->dd_parent) + 1;
344 if (!MUTEX_HELD(&dd->dd_lock)) {
345 /* see dsl_dir_name */
346 mutex_enter(&dd->dd_lock);
347 result += strlen(dd->dd_myname);
348 mutex_exit(&dd->dd_lock);
350 result += strlen(dd->dd_myname);
357 getcomponent(const char *path, char *component, const char **nextp)
361 if ((path == NULL) || (path[0] == '\0'))
362 return (SET_ERROR(ENOENT));
363 /* This would be a good place to reserve some namespace... */
364 p = strpbrk(path, "/@");
365 if (p && (p[1] == '/' || p[1] == '@')) {
366 /* two separators in a row */
367 return (SET_ERROR(EINVAL));
369 if (p == NULL || p == path) {
371 * if the first thing is an @ or /, it had better be an
372 * @ and it had better not have any more ats or slashes,
373 * and it had better have something after the @.
376 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
377 return (SET_ERROR(EINVAL));
378 if (strlen(path) >= MAXNAMELEN)
379 return (SET_ERROR(ENAMETOOLONG));
380 (void) strcpy(component, path);
382 } else if (p[0] == '/') {
383 if (p - path >= MAXNAMELEN)
384 return (SET_ERROR(ENAMETOOLONG));
385 (void) strncpy(component, path, p - path);
386 component[p - path] = '\0';
388 } else if (p[0] == '@') {
390 * if the next separator is an @, there better not be
393 if (strchr(path, '/'))
394 return (SET_ERROR(EINVAL));
395 if (p - path >= MAXNAMELEN)
396 return (SET_ERROR(ENAMETOOLONG));
397 (void) strncpy(component, path, p - path);
398 component[p - path] = '\0';
400 panic("invalid p=%p", (void *)p);
407 * Return the dsl_dir_t, and possibly the last component which couldn't
408 * be found in *tail. The name must be in the specified dsl_pool_t. This
409 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
410 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
411 * (*tail)[0] == '@' means that the last component is a snapshot.
414 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
415 dsl_dir_t **ddp, const char **tailp)
417 char buf[MAXNAMELEN];
418 const char *spaname, *next, *nextnext = NULL;
423 err = getcomponent(name, buf, &next);
427 /* Make sure the name is in the specified pool. */
428 spaname = spa_name(dp->dp_spa);
429 if (strcmp(buf, spaname) != 0)
430 return (SET_ERROR(EXDEV));
432 ASSERT(dsl_pool_config_held(dp));
434 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
439 while (next != NULL) {
441 err = getcomponent(next, buf, &nextnext);
444 ASSERT(next[0] != '\0');
447 dprintf("looking up %s in obj%lld\n",
448 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
450 err = zap_lookup(dp->dp_meta_objset,
451 dsl_dir_phys(dd)->dd_child_dir_zapobj,
452 buf, sizeof (ddobj), 1, &ddobj);
459 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
462 dsl_dir_rele(dd, tag);
468 dsl_dir_rele(dd, tag);
473 * It's an error if there's more than one component left, or
474 * tailp==NULL and there's any component left.
477 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
479 dsl_dir_rele(dd, tag);
480 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
481 err = SET_ERROR(ENOENT);
490 * If the counts are already initialized for this filesystem and its
491 * descendants then do nothing, otherwise initialize the counts.
493 * The counts on this filesystem, and those below, may be uninitialized due to
494 * either the use of a pre-existing pool which did not support the
495 * filesystem/snapshot limit feature, or one in which the feature had not yet
498 * Recursively descend the filesystem tree and update the filesystem/snapshot
499 * counts on each filesystem below, then update the cumulative count on the
500 * current filesystem. If the filesystem already has a count set on it,
501 * then we know that its counts, and the counts on the filesystems below it,
502 * are already correct, so we don't have to update this filesystem.
505 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
507 uint64_t my_fs_cnt = 0;
508 uint64_t my_ss_cnt = 0;
509 dsl_pool_t *dp = dd->dd_pool;
510 objset_t *os = dp->dp_meta_objset;
515 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
516 ASSERT(dsl_pool_config_held(dp));
517 ASSERT(dmu_tx_is_syncing(tx));
519 dsl_dir_zapify(dd, tx);
522 * If the filesystem count has already been initialized then we
523 * don't need to recurse down any further.
525 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
528 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
529 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
531 /* Iterate my child dirs */
532 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
533 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
537 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
541 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
542 * temporary datasets.
544 if (chld_dd->dd_myname[0] == '$' ||
545 chld_dd->dd_myname[0] == '%') {
546 dsl_dir_rele(chld_dd, FTAG);
550 my_fs_cnt++; /* count this child */
552 dsl_dir_init_fs_ss_count(chld_dd, tx);
554 VERIFY0(zap_lookup(os, chld_dd->dd_object,
555 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
557 VERIFY0(zap_lookup(os, chld_dd->dd_object,
558 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
561 dsl_dir_rele(chld_dd, FTAG);
564 /* Count my snapshots (we counted children's snapshots above) */
565 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
566 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
568 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
569 zap_cursor_retrieve(zc, za) == 0;
570 zap_cursor_advance(zc)) {
571 /* Don't count temporary snapshots */
572 if (za->za_name[0] != '%')
577 dsl_dataset_rele(ds, FTAG);
579 kmem_free(zc, sizeof (zap_cursor_t));
580 kmem_free(za, sizeof (zap_attribute_t));
582 /* we're in a sync task, update counts */
583 dmu_buf_will_dirty(dd->dd_dbuf, tx);
584 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
585 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
586 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
587 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
591 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
593 char *ddname = (char *)arg;
594 dsl_pool_t *dp = dmu_tx_pool(tx);
599 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
603 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
604 dsl_dataset_rele(ds, FTAG);
605 return (SET_ERROR(ENOTSUP));
609 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
610 dsl_dir_is_zapified(dd) &&
611 zap_contains(dp->dp_meta_objset, dd->dd_object,
612 DD_FIELD_FILESYSTEM_COUNT) == 0) {
613 dsl_dataset_rele(ds, FTAG);
614 return (SET_ERROR(EALREADY));
617 dsl_dataset_rele(ds, FTAG);
622 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
624 char *ddname = (char *)arg;
625 dsl_pool_t *dp = dmu_tx_pool(tx);
629 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
631 spa = dsl_dataset_get_spa(ds);
633 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
635 * Since the feature was not active and we're now setting a
636 * limit, increment the feature-active counter so that the
637 * feature becomes active for the first time.
639 * We are already in a sync task so we can update the MOS.
641 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
645 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
646 * we need to ensure the counts are correct. Descend down the tree from
647 * this point and update all of the counts to be accurate.
649 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
651 dsl_dataset_rele(ds, FTAG);
655 * Make sure the feature is enabled and activate it if necessary.
656 * Since we're setting a limit, ensure the on-disk counts are valid.
657 * This is only called by the ioctl path when setting a limit value.
659 * We do not need to validate the new limit, since users who can change the
660 * limit are also allowed to exceed the limit.
663 dsl_dir_activate_fs_ss_limit(const char *ddname)
667 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
668 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
669 ZFS_SPACE_CHECK_RESERVED);
671 if (error == EALREADY)
678 * Used to determine if the filesystem_limit or snapshot_limit should be
679 * enforced. We allow the limit to be exceeded if the user has permission to
680 * write the property value. We pass in the creds that we got in the open
681 * context since we will always be the GZ root in syncing context. We also have
682 * to handle the case where we are allowed to change the limit on the current
683 * dataset, but there may be another limit in the tree above.
685 * We can never modify these two properties within a non-global zone. In
686 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
687 * can't use that function since we are already holding the dp_config_rwlock.
688 * In addition, we already have the dd and dealing with snapshots is simplified
699 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
701 enforce_res_t enforce = ENFORCE_ALWAYS;
706 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
707 prop == ZFS_PROP_SNAPSHOT_LIMIT);
713 if (crgetzoneid(cr) != GLOBAL_ZONEID)
715 return (ENFORCE_ALWAYS);
717 if (secpolicy_zfs(cr) == 0)
718 return (ENFORCE_NEVER);
721 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
722 return (ENFORCE_ALWAYS);
724 ASSERT(dsl_pool_config_held(dd->dd_pool));
726 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
727 return (ENFORCE_ALWAYS);
729 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
730 /* Only root can access zoned fs's from the GZ */
731 enforce = ENFORCE_ALWAYS;
733 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
734 enforce = ENFORCE_ABOVE;
737 dsl_dataset_rele(ds, FTAG);
742 * Check if adding additional child filesystem(s) would exceed any filesystem
743 * limits or adding additional snapshot(s) would exceed any snapshot limits.
744 * The prop argument indicates which limit to check.
746 * Note that all filesystem limits up to the root (or the highest
747 * initialized) filesystem or the given ancestor must be satisfied.
750 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
751 dsl_dir_t *ancestor, cred_t *cr)
753 objset_t *os = dd->dd_pool->dp_meta_objset;
754 uint64_t limit, count;
756 enforce_res_t enforce;
759 ASSERT(dsl_pool_config_held(dd->dd_pool));
760 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
761 prop == ZFS_PROP_SNAPSHOT_LIMIT);
764 * If we're allowed to change the limit, don't enforce the limit
765 * e.g. this can happen if a snapshot is taken by an administrative
766 * user in the global zone (i.e. a recursive snapshot by root).
767 * However, we must handle the case of delegated permissions where we
768 * are allowed to change the limit on the current dataset, but there
769 * is another limit in the tree above.
771 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
772 if (enforce == ENFORCE_NEVER)
776 * e.g. if renaming a dataset with no snapshots, count adjustment
782 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
784 * We don't enforce the limit for temporary snapshots. This is
785 * indicated by a NULL cred_t argument.
790 count_prop = DD_FIELD_SNAPSHOT_COUNT;
792 count_prop = DD_FIELD_FILESYSTEM_COUNT;
796 * If an ancestor has been provided, stop checking the limit once we
797 * hit that dir. We need this during rename so that we don't overcount
798 * the check once we recurse up to the common ancestor.
804 * If we hit an uninitialized node while recursing up the tree, we can
805 * stop since we know there is no limit here (or above). The counts are
806 * not valid on this node and we know we won't touch this node's counts.
808 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
809 count_prop, sizeof (count), 1, &count) == ENOENT)
812 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
817 /* Is there a limit which we've hit? */
818 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
819 return (SET_ERROR(EDQUOT));
821 if (dd->dd_parent != NULL)
822 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
829 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
830 * parents. When a new filesystem/snapshot is created, increment the count on
831 * all parents, and when a filesystem/snapshot is destroyed, decrement the
835 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
839 objset_t *os = dd->dd_pool->dp_meta_objset;
842 ASSERT(dsl_pool_config_held(dd->dd_pool));
843 ASSERT(dmu_tx_is_syncing(tx));
844 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
845 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
848 * When we receive an incremental stream into a filesystem that already
849 * exists, a temporary clone is created. We don't count this temporary
850 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
851 * $MOS & $ORIGIN) objsets.
853 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
854 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
858 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
864 * If we hit an uninitialized node while recursing up the tree, we can
865 * stop since we know the counts are not valid on this node and we
866 * know we shouldn't touch this node's counts. An uninitialized count
867 * on the node indicates that either the feature has not yet been
868 * activated or there are no limits on this part of the tree.
870 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
871 prop, sizeof (count), 1, &count)) == ENOENT)
876 /* Use a signed verify to make sure we're not neg. */
877 VERIFY3S(count, >=, 0);
879 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
882 /* Roll up this additional count into our ancestors */
883 if (dd->dd_parent != NULL)
884 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
888 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
891 objset_t *mos = dp->dp_meta_objset;
893 dsl_dir_phys_t *ddphys;
896 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
897 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
899 VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
900 name, sizeof (uint64_t), 1, &ddobj, tx));
902 /* it's the root dir */
903 VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
904 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
906 VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
907 dmu_buf_will_dirty(dbuf, tx);
908 ddphys = dbuf->db_data;
910 ddphys->dd_creation_time = gethrestime_sec();
912 ddphys->dd_parent_obj = pds->dd_object;
914 /* update the filesystem counts */
915 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
917 ddphys->dd_props_zapobj = zap_create(mos,
918 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
919 ddphys->dd_child_dir_zapobj = zap_create(mos,
920 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
921 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
922 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
923 dmu_buf_rele(dbuf, FTAG);
929 dsl_dir_is_clone(dsl_dir_t *dd)
931 return (dsl_dir_phys(dd)->dd_origin_obj &&
932 (dd->dd_pool->dp_origin_snap == NULL ||
933 dsl_dir_phys(dd)->dd_origin_obj !=
934 dd->dd_pool->dp_origin_snap->ds_object));
938 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
940 mutex_enter(&dd->dd_lock);
941 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
942 dsl_dir_phys(dd)->dd_used_bytes);
943 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
944 dsl_dir_phys(dd)->dd_quota);
945 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
946 dsl_dir_phys(dd)->dd_reserved);
947 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO,
948 dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
949 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
950 dsl_dir_phys(dd)->dd_compressed_bytes));
951 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
952 dsl_dir_phys(dd)->dd_uncompressed_bytes);
953 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
954 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
955 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
956 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
957 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
958 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
959 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
960 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
961 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
962 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
964 mutex_exit(&dd->dd_lock);
966 if (dsl_dir_is_zapified(dd)) {
968 objset_t *os = dd->dd_pool->dp_meta_objset;
970 if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
971 sizeof (count), 1, &count) == 0) {
972 dsl_prop_nvlist_add_uint64(nv,
973 ZFS_PROP_FILESYSTEM_COUNT, count);
975 if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
976 sizeof (count), 1, &count) == 0) {
977 dsl_prop_nvlist_add_uint64(nv,
978 ZFS_PROP_SNAPSHOT_COUNT, count);
982 if (dsl_dir_is_clone(dd)) {
984 char buf[MAXNAMELEN];
986 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
987 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
988 dsl_dataset_name(ds, buf);
989 dsl_dataset_rele(ds, FTAG);
990 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
995 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
997 dsl_pool_t *dp = dd->dd_pool;
999 ASSERT(dsl_dir_phys(dd));
1001 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1002 /* up the hold count until we can be written out */
1003 dmu_buf_add_ref(dd->dd_dbuf, dd);
1008 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1010 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1011 uint64_t new_accounted =
1012 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1013 return (new_accounted - old_accounted);
1017 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1019 ASSERT(dmu_tx_is_syncing(tx));
1021 mutex_enter(&dd->dd_lock);
1022 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1023 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1024 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1025 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1026 mutex_exit(&dd->dd_lock);
1028 /* release the hold from dsl_dir_dirty */
1029 dmu_buf_rele(dd->dd_dbuf, dd);
1033 dsl_dir_space_towrite(dsl_dir_t *dd)
1038 ASSERT(MUTEX_HELD(&dd->dd_lock));
1040 for (i = 0; i < TXG_SIZE; i++) {
1041 space += dd->dd_space_towrite[i&TXG_MASK];
1042 ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0);
1048 * How much space would dd have available if ancestor had delta applied
1049 * to it? If ondiskonly is set, we're only interested in what's
1050 * on-disk, not estimated pending changes.
1053 dsl_dir_space_available(dsl_dir_t *dd,
1054 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1056 uint64_t parentspace, myspace, quota, used;
1059 * If there are no restrictions otherwise, assume we have
1060 * unlimited space available.
1063 parentspace = UINT64_MAX;
1065 if (dd->dd_parent != NULL) {
1066 parentspace = dsl_dir_space_available(dd->dd_parent,
1067 ancestor, delta, ondiskonly);
1070 mutex_enter(&dd->dd_lock);
1071 if (dsl_dir_phys(dd)->dd_quota != 0)
1072 quota = dsl_dir_phys(dd)->dd_quota;
1073 used = dsl_dir_phys(dd)->dd_used_bytes;
1075 used += dsl_dir_space_towrite(dd);
1077 if (dd->dd_parent == NULL) {
1078 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE);
1079 quota = MIN(quota, poolsize);
1082 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1084 * We have some space reserved, in addition to what our
1087 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1090 if (dd == ancestor) {
1092 ASSERT(used >= -delta);
1094 if (parentspace != UINT64_MAX)
1095 parentspace -= delta;
1103 * the lesser of the space provided by our parent and
1104 * the space left in our quota
1106 myspace = MIN(parentspace, quota - used);
1109 mutex_exit(&dd->dd_lock);
1114 struct tempreserve {
1115 list_node_t tr_node;
1121 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1122 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list,
1123 dmu_tx_t *tx, boolean_t first)
1125 uint64_t txg = tx->tx_txg;
1126 uint64_t est_inflight, used_on_disk, quota, parent_rsrv;
1127 uint64_t deferred = 0;
1128 struct tempreserve *tr;
1129 int retval = EDQUOT;
1130 int txgidx = txg & TXG_MASK;
1132 uint64_t ref_rsrv = 0;
1134 ASSERT3U(txg, !=, 0);
1135 ASSERT3S(asize, >, 0);
1137 mutex_enter(&dd->dd_lock);
1140 * Check against the dsl_dir's quota. We don't add in the delta
1141 * when checking for over-quota because they get one free hit.
1143 est_inflight = dsl_dir_space_towrite(dd);
1144 for (i = 0; i < TXG_SIZE; i++)
1145 est_inflight += dd->dd_tempreserved[i];
1146 used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1149 * On the first iteration, fetch the dataset's used-on-disk and
1150 * refreservation values. Also, if checkrefquota is set, test if
1151 * allocating this space would exceed the dataset's refquota.
1153 if (first && tx->tx_objset) {
1155 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1157 error = dsl_dataset_check_quota(ds, checkrefquota,
1158 asize, est_inflight, &used_on_disk, &ref_rsrv);
1160 mutex_exit(&dd->dd_lock);
1166 * If this transaction will result in a net free of space,
1167 * we want to let it through.
1169 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1172 quota = dsl_dir_phys(dd)->dd_quota;
1175 * Adjust the quota against the actual pool size at the root
1176 * minus any outstanding deferred frees.
1177 * To ensure that it's possible to remove files from a full
1178 * pool without inducing transient overcommits, we throttle
1179 * netfree transactions against a quota that is slightly larger,
1180 * but still within the pool's allocation slop. In cases where
1181 * we're very close to full, this will allow a steady trickle of
1182 * removes to get through.
1184 if (dd->dd_parent == NULL) {
1185 spa_t *spa = dd->dd_pool->dp_spa;
1186 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree);
1187 deferred = metaslab_class_get_deferred(spa_normal_class(spa));
1188 if (poolsize - deferred < quota) {
1189 quota = poolsize - deferred;
1195 * If they are requesting more space, and our current estimate
1196 * is over quota, they get to try again unless the actual
1197 * on-disk is over quota and there are no pending changes (which
1198 * may free up space for us).
1200 if (used_on_disk + est_inflight >= quota) {
1201 if (est_inflight > 0 || used_on_disk < quota ||
1202 (retval == ENOSPC && used_on_disk < quota + deferred))
1204 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1205 "quota=%lluK tr=%lluK err=%d\n",
1206 used_on_disk>>10, est_inflight>>10,
1207 quota>>10, asize>>10, retval);
1208 mutex_exit(&dd->dd_lock);
1209 return (SET_ERROR(retval));
1212 /* We need to up our estimated delta before dropping dd_lock */
1213 dd->dd_tempreserved[txgidx] += asize;
1215 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1217 mutex_exit(&dd->dd_lock);
1219 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1221 tr->tr_size = asize;
1222 list_insert_tail(tr_list, tr);
1224 /* see if it's OK with our parent */
1225 if (dd->dd_parent && parent_rsrv) {
1226 boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1228 return (dsl_dir_tempreserve_impl(dd->dd_parent,
1229 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE));
1236 * Reserve space in this dsl_dir, to be used in this tx's txg.
1237 * After the space has been dirtied (and dsl_dir_willuse_space()
1238 * has been called), the reservation should be canceled, using
1239 * dsl_dir_tempreserve_clear().
1242 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1243 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx)
1253 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1254 list_create(tr_list, sizeof (struct tempreserve),
1255 offsetof(struct tempreserve, tr_node));
1256 ASSERT3S(asize, >, 0);
1257 ASSERT3S(fsize, >=, 0);
1259 err = arc_tempreserve_space(lsize, tx->tx_txg);
1261 struct tempreserve *tr;
1263 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1264 tr->tr_size = lsize;
1265 list_insert_tail(tr_list, tr);
1267 if (err == EAGAIN) {
1269 * If arc_memory_throttle() detected that pageout
1270 * is running and we are low on memory, we delay new
1271 * non-pageout transactions to give pageout an
1274 * It is unfortunate to be delaying while the caller's
1277 txg_delay(dd->dd_pool, tx->tx_txg,
1278 MSEC2NSEC(10), MSEC2NSEC(10));
1279 err = SET_ERROR(ERESTART);
1284 err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize,
1285 FALSE, asize > usize, tr_list, tx, TRUE);
1289 dsl_dir_tempreserve_clear(tr_list, tx);
1291 *tr_cookiep = tr_list;
1297 * Clear a temporary reservation that we previously made with
1298 * dsl_dir_tempreserve_space().
1301 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1303 int txgidx = tx->tx_txg & TXG_MASK;
1304 list_t *tr_list = tr_cookie;
1305 struct tempreserve *tr;
1307 ASSERT3U(tx->tx_txg, !=, 0);
1309 if (tr_cookie == NULL)
1312 while ((tr = list_head(tr_list)) != NULL) {
1314 mutex_enter(&tr->tr_ds->dd_lock);
1315 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1317 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1318 mutex_exit(&tr->tr_ds->dd_lock);
1320 arc_tempreserve_clear(tr->tr_size);
1322 list_remove(tr_list, tr);
1323 kmem_free(tr, sizeof (struct tempreserve));
1326 kmem_free(tr_list, sizeof (list_t));
1330 * This should be called from open context when we think we're going to write
1331 * or free space, for example when dirtying data. Be conservative; it's okay
1332 * to write less space or free more, but we don't want to write more or free
1333 * less than the amount specified.
1336 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1338 int64_t parent_space;
1341 mutex_enter(&dd->dd_lock);
1343 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1345 est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes;
1346 parent_space = parent_delta(dd, est_used, space);
1347 mutex_exit(&dd->dd_lock);
1349 /* Make sure that we clean up dd_space_to* */
1350 dsl_dir_dirty(dd, tx);
1352 /* XXX this is potentially expensive and unnecessary... */
1353 if (parent_space && dd->dd_parent)
1354 dsl_dir_willuse_space(dd->dd_parent, parent_space, tx);
1357 /* call from syncing context when we actually write/free space for this dd */
1359 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1360 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1362 int64_t accounted_delta;
1365 * dsl_dataset_set_refreservation_sync_impl() calls this with
1366 * dd_lock held, so that it can atomically update
1367 * ds->ds_reserved and the dsl_dir accounting, so that
1368 * dsl_dataset_check_quota() can see dataset and dir accounting
1371 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1373 ASSERT(dmu_tx_is_syncing(tx));
1374 ASSERT(type < DD_USED_NUM);
1376 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1379 mutex_enter(&dd->dd_lock);
1381 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1382 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1383 ASSERT(compressed >= 0 ||
1384 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1385 ASSERT(uncompressed >= 0 ||
1386 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1387 dsl_dir_phys(dd)->dd_used_bytes += used;
1388 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1389 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1391 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1393 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1394 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1398 for (t = 0; t < DD_USED_NUM; t++)
1399 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1400 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1404 mutex_exit(&dd->dd_lock);
1406 if (dd->dd_parent != NULL) {
1407 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1408 accounted_delta, compressed, uncompressed, tx);
1409 dsl_dir_transfer_space(dd->dd_parent,
1410 used - accounted_delta,
1411 DD_USED_CHILD_RSRV, DD_USED_CHILD, NULL);
1416 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1417 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1419 ASSERT(tx == NULL || dmu_tx_is_syncing(tx));
1420 ASSERT(oldtype < DD_USED_NUM);
1421 ASSERT(newtype < DD_USED_NUM);
1424 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1428 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1429 mutex_enter(&dd->dd_lock);
1431 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1432 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1433 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1434 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1435 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1436 mutex_exit(&dd->dd_lock);
1439 typedef struct dsl_dir_set_qr_arg {
1440 const char *ddsqra_name;
1441 zprop_source_t ddsqra_source;
1442 uint64_t ddsqra_value;
1443 } dsl_dir_set_qr_arg_t;
1446 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1448 dsl_dir_set_qr_arg_t *ddsqra = arg;
1449 dsl_pool_t *dp = dmu_tx_pool(tx);
1452 uint64_t towrite, newval;
1454 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1458 error = dsl_prop_predict(ds->ds_dir, "quota",
1459 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1461 dsl_dataset_rele(ds, FTAG);
1466 dsl_dataset_rele(ds, FTAG);
1470 mutex_enter(&ds->ds_dir->dd_lock);
1472 * If we are doing the preliminary check in open context, and
1473 * there are pending changes, then don't fail it, since the
1474 * pending changes could under-estimate the amount of space to be
1477 towrite = dsl_dir_space_towrite(ds->ds_dir);
1478 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1479 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1480 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1481 error = SET_ERROR(ENOSPC);
1483 mutex_exit(&ds->ds_dir->dd_lock);
1484 dsl_dataset_rele(ds, FTAG);
1489 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1491 dsl_dir_set_qr_arg_t *ddsqra = arg;
1492 dsl_pool_t *dp = dmu_tx_pool(tx);
1496 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1498 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1499 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1500 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1501 &ddsqra->ddsqra_value, tx);
1503 VERIFY0(dsl_prop_get_int_ds(ds,
1504 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1506 newval = ddsqra->ddsqra_value;
1507 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1508 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1511 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1512 mutex_enter(&ds->ds_dir->dd_lock);
1513 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1514 mutex_exit(&ds->ds_dir->dd_lock);
1515 dsl_dataset_rele(ds, FTAG);
1519 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1521 dsl_dir_set_qr_arg_t ddsqra;
1523 ddsqra.ddsqra_name = ddname;
1524 ddsqra.ddsqra_source = source;
1525 ddsqra.ddsqra_value = quota;
1527 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1528 dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1532 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1534 dsl_dir_set_qr_arg_t *ddsqra = arg;
1535 dsl_pool_t *dp = dmu_tx_pool(tx);
1538 uint64_t newval, used, avail;
1541 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1547 * If we are doing the preliminary check in open context, the
1548 * space estimates may be inaccurate.
1550 if (!dmu_tx_is_syncing(tx)) {
1551 dsl_dataset_rele(ds, FTAG);
1555 error = dsl_prop_predict(ds->ds_dir,
1556 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1557 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1559 dsl_dataset_rele(ds, FTAG);
1563 mutex_enter(&dd->dd_lock);
1564 used = dsl_dir_phys(dd)->dd_used_bytes;
1565 mutex_exit(&dd->dd_lock);
1567 if (dd->dd_parent) {
1568 avail = dsl_dir_space_available(dd->dd_parent,
1571 avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used;
1574 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1575 uint64_t delta = MAX(used, newval) -
1576 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1578 if (delta > avail ||
1579 (dsl_dir_phys(dd)->dd_quota > 0 &&
1580 newval > dsl_dir_phys(dd)->dd_quota))
1581 error = SET_ERROR(ENOSPC);
1584 dsl_dataset_rele(ds, FTAG);
1589 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1594 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1596 mutex_enter(&dd->dd_lock);
1597 used = dsl_dir_phys(dd)->dd_used_bytes;
1598 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1599 dsl_dir_phys(dd)->dd_reserved = value;
1601 if (dd->dd_parent != NULL) {
1602 /* Roll up this additional usage into our ancestors */
1603 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1606 mutex_exit(&dd->dd_lock);
1610 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1612 dsl_dir_set_qr_arg_t *ddsqra = arg;
1613 dsl_pool_t *dp = dmu_tx_pool(tx);
1617 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1619 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1620 dsl_prop_set_sync_impl(ds,
1621 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1622 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1623 &ddsqra->ddsqra_value, tx);
1625 VERIFY0(dsl_prop_get_int_ds(ds,
1626 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1628 newval = ddsqra->ddsqra_value;
1629 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1630 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1631 (longlong_t)newval);
1634 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1635 dsl_dataset_rele(ds, FTAG);
1639 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1640 uint64_t reservation)
1642 dsl_dir_set_qr_arg_t ddsqra;
1644 ddsqra.ddsqra_name = ddname;
1645 ddsqra.ddsqra_source = source;
1646 ddsqra.ddsqra_value = reservation;
1648 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1649 dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE));
1653 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1655 for (; ds1; ds1 = ds1->dd_parent) {
1657 for (dd = ds2; dd; dd = dd->dd_parent) {
1666 * If delta is applied to dd, how much of that delta would be applied to
1667 * ancestor? Syncing context only.
1670 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1675 mutex_enter(&dd->dd_lock);
1676 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1677 mutex_exit(&dd->dd_lock);
1678 return (would_change(dd->dd_parent, delta, ancestor));
1681 typedef struct dsl_dir_rename_arg {
1682 const char *ddra_oldname;
1683 const char *ddra_newname;
1685 } dsl_dir_rename_arg_t;
1689 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1692 char namebuf[MAXNAMELEN];
1694 dsl_dataset_name(ds, namebuf);
1696 if (strlen(namebuf) + *deltap >= MAXNAMELEN)
1697 return (SET_ERROR(ENAMETOOLONG));
1702 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1704 dsl_dir_rename_arg_t *ddra = arg;
1705 dsl_pool_t *dp = dmu_tx_pool(tx);
1706 dsl_dir_t *dd, *newparent;
1707 const char *mynewname;
1709 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1711 /* target dir should exist */
1712 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1716 /* new parent should exist */
1717 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1718 &newparent, &mynewname);
1720 dsl_dir_rele(dd, FTAG);
1724 /* can't rename to different pool */
1725 if (dd->dd_pool != newparent->dd_pool) {
1726 dsl_dir_rele(newparent, FTAG);
1727 dsl_dir_rele(dd, FTAG);
1728 return (SET_ERROR(EXDEV));
1731 /* new name should not already exist */
1732 if (mynewname == NULL) {
1733 dsl_dir_rele(newparent, FTAG);
1734 dsl_dir_rele(dd, FTAG);
1735 return (SET_ERROR(EEXIST));
1738 /* if the name length is growing, validate child name lengths */
1740 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1741 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1743 dsl_dir_rele(newparent, FTAG);
1744 dsl_dir_rele(dd, FTAG);
1749 if (dmu_tx_is_syncing(tx)) {
1750 if (spa_feature_is_active(dp->dp_spa,
1751 SPA_FEATURE_FS_SS_LIMIT)) {
1753 * Although this is the check function and we don't
1754 * normally make on-disk changes in check functions,
1755 * we need to do that here.
1757 * Ensure this portion of the tree's counts have been
1758 * initialized in case the new parent has limits set.
1760 dsl_dir_init_fs_ss_count(dd, tx);
1764 if (newparent != dd->dd_parent) {
1765 /* is there enough space? */
1767 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1768 dsl_dir_phys(dd)->dd_reserved);
1769 objset_t *os = dd->dd_pool->dp_meta_objset;
1770 uint64_t fs_cnt = 0;
1771 uint64_t ss_cnt = 0;
1773 if (dsl_dir_is_zapified(dd)) {
1776 err = zap_lookup(os, dd->dd_object,
1777 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1779 if (err != ENOENT && err != 0) {
1780 dsl_dir_rele(newparent, FTAG);
1781 dsl_dir_rele(dd, FTAG);
1786 * have to add 1 for the filesystem itself that we're
1791 err = zap_lookup(os, dd->dd_object,
1792 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1794 if (err != ENOENT && err != 0) {
1795 dsl_dir_rele(newparent, FTAG);
1796 dsl_dir_rele(dd, FTAG);
1801 /* no rename into our descendant */
1802 if (closest_common_ancestor(dd, newparent) == dd) {
1803 dsl_dir_rele(newparent, FTAG);
1804 dsl_dir_rele(dd, FTAG);
1805 return (SET_ERROR(EINVAL));
1808 error = dsl_dir_transfer_possible(dd->dd_parent,
1809 newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
1811 dsl_dir_rele(newparent, FTAG);
1812 dsl_dir_rele(dd, FTAG);
1817 dsl_dir_rele(newparent, FTAG);
1818 dsl_dir_rele(dd, FTAG);
1823 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
1825 dsl_dir_rename_arg_t *ddra = arg;
1826 dsl_pool_t *dp = dmu_tx_pool(tx);
1827 dsl_dir_t *dd, *newparent;
1828 const char *mynewname;
1830 objset_t *mos = dp->dp_meta_objset;
1832 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
1833 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
1836 /* Log this before we change the name. */
1837 spa_history_log_internal_dd(dd, "rename", tx,
1838 "-> %s", ddra->ddra_newname);
1840 if (newparent != dd->dd_parent) {
1841 objset_t *os = dd->dd_pool->dp_meta_objset;
1842 uint64_t fs_cnt = 0;
1843 uint64_t ss_cnt = 0;
1846 * We already made sure the dd counts were initialized in the
1849 if (spa_feature_is_active(dp->dp_spa,
1850 SPA_FEATURE_FS_SS_LIMIT)) {
1851 VERIFY0(zap_lookup(os, dd->dd_object,
1852 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1854 /* add 1 for the filesystem itself that we're moving */
1857 VERIFY0(zap_lookup(os, dd->dd_object,
1858 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1862 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
1863 DD_FIELD_FILESYSTEM_COUNT, tx);
1864 dsl_fs_ss_count_adjust(newparent, fs_cnt,
1865 DD_FIELD_FILESYSTEM_COUNT, tx);
1867 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
1868 DD_FIELD_SNAPSHOT_COUNT, tx);
1869 dsl_fs_ss_count_adjust(newparent, ss_cnt,
1870 DD_FIELD_SNAPSHOT_COUNT, tx);
1872 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1873 -dsl_dir_phys(dd)->dd_used_bytes,
1874 -dsl_dir_phys(dd)->dd_compressed_bytes,
1875 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1876 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
1877 dsl_dir_phys(dd)->dd_used_bytes,
1878 dsl_dir_phys(dd)->dd_compressed_bytes,
1879 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
1881 if (dsl_dir_phys(dd)->dd_reserved >
1882 dsl_dir_phys(dd)->dd_used_bytes) {
1883 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
1884 dsl_dir_phys(dd)->dd_used_bytes;
1886 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1887 -unused_rsrv, 0, 0, tx);
1888 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
1889 unused_rsrv, 0, 0, tx);
1893 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1895 /* remove from old parent zapobj */
1896 error = zap_remove(mos,
1897 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
1901 (void) strcpy(dd->dd_myname, mynewname);
1902 dsl_dir_rele(dd->dd_parent, dd);
1903 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
1904 VERIFY0(dsl_dir_hold_obj(dp,
1905 newparent->dd_object, NULL, dd, &dd->dd_parent));
1907 /* add to new parent zapobj */
1908 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
1909 dd->dd_myname, 8, 1, &dd->dd_object, tx));
1913 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);
1914 zvol_rename_minors(ddra->ddra_oldname, ddra->ddra_newname);
1918 dsl_prop_notify_all(dd);
1920 dsl_dir_rele(newparent, FTAG);
1921 dsl_dir_rele(dd, FTAG);
1925 dsl_dir_rename(const char *oldname, const char *newname)
1927 dsl_dir_rename_arg_t ddra;
1929 ddra.ddra_oldname = oldname;
1930 ddra.ddra_newname = newname;
1931 ddra.ddra_cred = CRED();
1933 return (dsl_sync_task(oldname,
1934 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
1935 3, ZFS_SPACE_CHECK_RESERVED));
1939 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
1940 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
1942 dsl_dir_t *ancestor;
1947 ancestor = closest_common_ancestor(sdd, tdd);
1948 adelta = would_change(sdd, -space, ancestor);
1949 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
1951 return (SET_ERROR(ENOSPC));
1953 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
1957 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
1966 dsl_dir_snap_cmtime(dsl_dir_t *dd)
1970 mutex_enter(&dd->dd_lock);
1971 t = dd->dd_snap_cmtime;
1972 mutex_exit(&dd->dd_lock);
1978 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
1983 mutex_enter(&dd->dd_lock);
1984 dd->dd_snap_cmtime = t;
1985 mutex_exit(&dd->dd_lock);
1989 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
1991 objset_t *mos = dd->dd_pool->dp_meta_objset;
1992 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
1996 dsl_dir_is_zapified(dsl_dir_t *dd)
1998 dmu_object_info_t doi;
2000 dmu_object_info_from_db(dd->dd_dbuf, &doi);
2001 return (doi.doi_type == DMU_OTN_ZAP_METADATA);