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) 2012, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Martin Matuska. All rights reserved.
25 * Copyright (c) 2014 Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 #include <sys/dmu_objset.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_prop.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/dsl_deleg.h>
38 #include <sys/dmu_impl.h>
40 #include <sys/spa_impl.h>
41 #include <sys/metaslab.h>
45 #include <sys/sunddi.h>
46 #include <sys/zfeature.h>
47 #include <sys/policy.h>
48 #include <sys/zfs_znode.h>
50 #include "zfs_namecheck.h"
54 * Filesystem and Snapshot Limits
55 * ------------------------------
57 * These limits are used to restrict the number of filesystems and/or snapshots
58 * that can be created at a given level in the tree or below. A typical
59 * use-case is with a delegated dataset where the administrator wants to ensure
60 * that a user within the zone is not creating too many additional filesystems
61 * or snapshots, even though they're not exceeding their space quota.
63 * The filesystem and snapshot counts are stored as extensible properties. This
64 * capability is controlled by a feature flag and must be enabled to be used.
65 * Once enabled, the feature is not active until the first limit is set. At
66 * that point, future operations to create/destroy filesystems or snapshots
67 * will validate and update the counts.
69 * Because the count properties will not exist before the feature is active,
70 * the counts are updated when a limit is first set on an uninitialized
71 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
72 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
73 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
74 * snapshot count properties on a node indicate uninitialized counts on that
75 * node.) When first setting a limit on an uninitialized node, the code starts
76 * at the filesystem with the new limit and descends into all sub-filesystems
77 * to add the count properties.
79 * In practice this is lightweight since a limit is typically set when the
80 * filesystem is created and thus has no children. Once valid, changing the
81 * limit value won't require a re-traversal since the counts are already valid.
82 * When recursively fixing the counts, if a node with a limit is encountered
83 * during the descent, the counts are known to be valid and there is no need to
84 * descend into that filesystem's children. The counts on filesystems above the
85 * one with the new limit will still be uninitialized, unless a limit is
86 * eventually set on one of those filesystems. The counts are always recursively
87 * updated when a limit is set on a dataset, unless there is already a limit.
88 * When a new limit value is set on a filesystem with an existing limit, it is
89 * possible for the new limit to be less than the current count at that level
90 * since a user who can change the limit is also allowed to exceed the limit.
92 * Once the feature is active, then whenever a filesystem or snapshot is
93 * created, the code recurses up the tree, validating the new count against the
94 * limit at each initialized level. In practice, most levels will not have a
95 * limit set. If there is a limit at any initialized level up the tree, the
96 * check must pass or the creation will fail. Likewise, when a filesystem or
97 * snapshot is destroyed, the counts are recursively adjusted all the way up
98 * the initizized nodes in the tree. Renaming a filesystem into different point
99 * in the tree will first validate, then update the counts on each branch up to
100 * the common ancestor. A receive will also validate the counts and then update
103 * An exception to the above behavior is that the limit is not enforced if the
104 * user has permission to modify the limit. This is primarily so that
105 * recursive snapshots in the global zone always work. We want to prevent a
106 * denial-of-service in which a lower level delegated dataset could max out its
107 * limit and thus block recursive snapshots from being taken in the global zone.
108 * Because of this, it is possible for the snapshot count to be over the limit
109 * and snapshots taken in the global zone could cause a lower level dataset to
110 * hit or exceed its limit. The administrator taking the global zone recursive
111 * snapshot should be aware of this side-effect and behave accordingly.
112 * For consistency, the filesystem limit is also not enforced if the user can
115 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
116 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
117 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
118 * dsl_dir_init_fs_ss_count().
120 * There is a special case when we receive a filesystem that already exists. In
121 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
122 * never update the filesystem counts for temporary clones.
124 * Likewise, we do not update the snapshot counts for temporary snapshots,
125 * such as those created by zfs diff.
128 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
130 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
132 typedef struct ddulrt_arg {
133 dsl_dir_t *ddulrta_dd;
138 dsl_dir_evict_async(void *dbu)
142 ASSERTV(dsl_pool_t *dp = dd->dd_pool);
146 for (t = 0; t < TXG_SIZE; t++) {
147 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
148 ASSERT(dd->dd_tempreserved[t] == 0);
149 ASSERT(dd->dd_space_towrite[t] == 0);
153 dsl_dir_async_rele(dd->dd_parent, dd);
155 spa_async_close(dd->dd_pool->dp_spa, dd);
158 mutex_destroy(&dd->dd_lock);
159 kmem_free(dd, sizeof (dsl_dir_t));
163 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
164 const char *tail, void *tag, dsl_dir_t **ddp)
168 dmu_object_info_t doi;
171 ASSERT(dsl_pool_config_held(dp));
173 err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);
176 dd = dmu_buf_get_user(dbuf);
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));
185 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
186 dd->dd_object = ddobj;
190 if (dsl_dir_is_zapified(dd) &&
191 zap_contains(dp->dp_meta_objset, ddobj,
192 DD_FIELD_CRYPTO_KEY_OBJ) == 0) {
193 VERIFY0(zap_lookup(dp->dp_meta_objset,
194 ddobj, DD_FIELD_CRYPTO_KEY_OBJ,
195 sizeof (uint64_t), 1, &dd->dd_crypto_obj));
197 /* check for on-disk format errata */
198 if (dsl_dir_incompatible_encryption_version(dd)) {
199 dp->dp_spa->spa_errata =
200 ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
204 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
207 dsl_dir_snap_cmtime_update(dd);
209 if (dsl_dir_phys(dd)->dd_parent_obj) {
210 err = dsl_dir_hold_obj(dp,
211 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
219 err = zap_lookup(dp->dp_meta_objset,
220 dsl_dir_phys(dd->dd_parent)->
221 dd_child_dir_zapobj, tail,
222 sizeof (foundobj), 1, &foundobj);
223 ASSERT(err || foundobj == ddobj);
225 (void) strlcpy(dd->dd_myname, tail,
226 sizeof (dd->dd_myname));
228 err = zap_value_search(dp->dp_meta_objset,
229 dsl_dir_phys(dd->dd_parent)->
231 ddobj, 0, dd->dd_myname);
236 (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa));
239 if (dsl_dir_is_clone(dd)) {
240 dmu_buf_t *origin_bonus;
241 dsl_dataset_phys_t *origin_phys;
244 * We can't open the origin dataset, because
245 * that would require opening this dsl_dir.
246 * Just look at its phys directly instead.
248 err = dmu_bonus_hold(dp->dp_meta_objset,
249 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
253 origin_phys = origin_bonus->db_data;
255 origin_phys->ds_creation_txg;
256 dmu_buf_rele(origin_bonus, FTAG);
259 dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
261 winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
262 if (winner != NULL) {
264 dsl_dir_rele(dd->dd_parent, dd);
266 mutex_destroy(&dd->dd_lock);
267 kmem_free(dd, sizeof (dsl_dir_t));
270 spa_open_ref(dp->dp_spa, dd);
275 * The dsl_dir_t has both open-to-close and instantiate-to-evict
276 * holds on the spa. We need the open-to-close holds because
277 * otherwise the spa_refcnt wouldn't change when we open a
278 * dir which the spa also has open, so we could incorrectly
279 * think it was OK to unload/export/destroy the pool. We need
280 * the instantiate-to-evict hold because the dsl_dir_t has a
281 * pointer to the dd_pool, which has a pointer to the spa_t.
283 spa_open_ref(dp->dp_spa, tag);
284 ASSERT3P(dd->dd_pool, ==, dp);
285 ASSERT3U(dd->dd_object, ==, ddobj);
286 ASSERT3P(dd->dd_dbuf, ==, dbuf);
292 dsl_dir_rele(dd->dd_parent, dd);
294 mutex_destroy(&dd->dd_lock);
295 kmem_free(dd, sizeof (dsl_dir_t));
296 dmu_buf_rele(dbuf, tag);
301 dsl_dir_rele(dsl_dir_t *dd, void *tag)
303 dprintf_dd(dd, "%s\n", "");
304 spa_close(dd->dd_pool->dp_spa, tag);
305 dmu_buf_rele(dd->dd_dbuf, tag);
309 * Remove a reference to the given dsl dir that is being asynchronously
310 * released. Async releases occur from a taskq performing eviction of
311 * dsl datasets and dirs. This process is identical to a normal release
312 * with the exception of using the async API for releasing the reference on
316 dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
318 dprintf_dd(dd, "%s\n", "");
319 spa_async_close(dd->dd_pool->dp_spa, tag);
320 dmu_buf_rele(dd->dd_dbuf, tag);
323 /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
325 dsl_dir_name(dsl_dir_t *dd, char *buf)
328 dsl_dir_name(dd->dd_parent, buf);
329 VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
330 ZFS_MAX_DATASET_NAME_LEN);
334 if (!MUTEX_HELD(&dd->dd_lock)) {
336 * recursive mutex so that we can use
337 * dprintf_dd() with dd_lock held
339 mutex_enter(&dd->dd_lock);
340 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
341 <, ZFS_MAX_DATASET_NAME_LEN);
342 mutex_exit(&dd->dd_lock);
344 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
345 <, ZFS_MAX_DATASET_NAME_LEN);
349 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
351 dsl_dir_namelen(dsl_dir_t *dd)
356 /* parent's name + 1 for the "/" */
357 result = dsl_dir_namelen(dd->dd_parent) + 1;
360 if (!MUTEX_HELD(&dd->dd_lock)) {
361 /* see dsl_dir_name */
362 mutex_enter(&dd->dd_lock);
363 result += strlen(dd->dd_myname);
364 mutex_exit(&dd->dd_lock);
366 result += strlen(dd->dd_myname);
373 getcomponent(const char *path, char *component, const char **nextp)
377 if ((path == NULL) || (path[0] == '\0'))
378 return (SET_ERROR(ENOENT));
379 /* This would be a good place to reserve some namespace... */
380 p = strpbrk(path, "/@");
381 if (p && (p[1] == '/' || p[1] == '@')) {
382 /* two separators in a row */
383 return (SET_ERROR(EINVAL));
385 if (p == NULL || p == path) {
387 * if the first thing is an @ or /, it had better be an
388 * @ and it had better not have any more ats or slashes,
389 * and it had better have something after the @.
392 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
393 return (SET_ERROR(EINVAL));
394 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
395 return (SET_ERROR(ENAMETOOLONG));
396 (void) strcpy(component, path);
398 } else if (p[0] == '/') {
399 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
400 return (SET_ERROR(ENAMETOOLONG));
401 (void) strncpy(component, path, p - path);
402 component[p - path] = '\0';
404 } else if (p[0] == '@') {
406 * if the next separator is an @, there better not be
409 if (strchr(path, '/'))
410 return (SET_ERROR(EINVAL));
411 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
412 return (SET_ERROR(ENAMETOOLONG));
413 (void) strncpy(component, path, p - path);
414 component[p - path] = '\0';
416 panic("invalid p=%p", (void *)p);
423 * Return the dsl_dir_t, and possibly the last component which couldn't
424 * be found in *tail. The name must be in the specified dsl_pool_t. This
425 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
426 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
427 * (*tail)[0] == '@' means that the last component is a snapshot.
430 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
431 dsl_dir_t **ddp, const char **tailp)
434 const char *spaname, *next, *nextnext = NULL;
439 buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
440 err = getcomponent(name, buf, &next);
444 /* Make sure the name is in the specified pool. */
445 spaname = spa_name(dp->dp_spa);
446 if (strcmp(buf, spaname) != 0) {
447 err = SET_ERROR(EXDEV);
451 ASSERT(dsl_pool_config_held(dp));
453 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
458 while (next != NULL) {
460 err = getcomponent(next, buf, &nextnext);
463 ASSERT(next[0] != '\0');
466 dprintf("looking up %s in obj%lld\n",
467 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
469 err = zap_lookup(dp->dp_meta_objset,
470 dsl_dir_phys(dd)->dd_child_dir_zapobj,
471 buf, sizeof (ddobj), 1, &ddobj);
478 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
481 dsl_dir_rele(dd, tag);
487 dsl_dir_rele(dd, tag);
492 * It's an error if there's more than one component left, or
493 * tailp==NULL and there's any component left.
496 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
498 dsl_dir_rele(dd, tag);
499 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
500 err = SET_ERROR(ENOENT);
506 kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN);
511 * If the counts are already initialized for this filesystem and its
512 * descendants then do nothing, otherwise initialize the counts.
514 * The counts on this filesystem, and those below, may be uninitialized due to
515 * either the use of a pre-existing pool which did not support the
516 * filesystem/snapshot limit feature, or one in which the feature had not yet
519 * Recursively descend the filesystem tree and update the filesystem/snapshot
520 * counts on each filesystem below, then update the cumulative count on the
521 * current filesystem. If the filesystem already has a count set on it,
522 * then we know that its counts, and the counts on the filesystems below it,
523 * are already correct, so we don't have to update this filesystem.
526 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
528 uint64_t my_fs_cnt = 0;
529 uint64_t my_ss_cnt = 0;
530 dsl_pool_t *dp = dd->dd_pool;
531 objset_t *os = dp->dp_meta_objset;
536 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
537 ASSERT(dsl_pool_config_held(dp));
538 ASSERT(dmu_tx_is_syncing(tx));
540 dsl_dir_zapify(dd, tx);
543 * If the filesystem count has already been initialized then we
544 * don't need to recurse down any further.
546 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
549 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
550 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
552 /* Iterate my child dirs */
553 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
554 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
558 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
562 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
563 * temporary datasets.
565 if (chld_dd->dd_myname[0] == '$' ||
566 chld_dd->dd_myname[0] == '%') {
567 dsl_dir_rele(chld_dd, FTAG);
571 my_fs_cnt++; /* count this child */
573 dsl_dir_init_fs_ss_count(chld_dd, tx);
575 VERIFY0(zap_lookup(os, chld_dd->dd_object,
576 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
578 VERIFY0(zap_lookup(os, chld_dd->dd_object,
579 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
582 dsl_dir_rele(chld_dd, FTAG);
585 /* Count my snapshots (we counted children's snapshots above) */
586 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
587 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
589 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
590 zap_cursor_retrieve(zc, za) == 0;
591 zap_cursor_advance(zc)) {
592 /* Don't count temporary snapshots */
593 if (za->za_name[0] != '%')
598 dsl_dataset_rele(ds, FTAG);
600 kmem_free(zc, sizeof (zap_cursor_t));
601 kmem_free(za, sizeof (zap_attribute_t));
603 /* we're in a sync task, update counts */
604 dmu_buf_will_dirty(dd->dd_dbuf, tx);
605 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
606 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
607 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
608 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
612 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
614 char *ddname = (char *)arg;
615 dsl_pool_t *dp = dmu_tx_pool(tx);
620 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
624 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
625 dsl_dataset_rele(ds, FTAG);
626 return (SET_ERROR(ENOTSUP));
630 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
631 dsl_dir_is_zapified(dd) &&
632 zap_contains(dp->dp_meta_objset, dd->dd_object,
633 DD_FIELD_FILESYSTEM_COUNT) == 0) {
634 dsl_dataset_rele(ds, FTAG);
635 return (SET_ERROR(EALREADY));
638 dsl_dataset_rele(ds, FTAG);
643 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
645 char *ddname = (char *)arg;
646 dsl_pool_t *dp = dmu_tx_pool(tx);
650 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
652 spa = dsl_dataset_get_spa(ds);
654 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
656 * Since the feature was not active and we're now setting a
657 * limit, increment the feature-active counter so that the
658 * feature becomes active for the first time.
660 * We are already in a sync task so we can update the MOS.
662 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
666 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
667 * we need to ensure the counts are correct. Descend down the tree from
668 * this point and update all of the counts to be accurate.
670 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
672 dsl_dataset_rele(ds, FTAG);
676 * Make sure the feature is enabled and activate it if necessary.
677 * Since we're setting a limit, ensure the on-disk counts are valid.
678 * This is only called by the ioctl path when setting a limit value.
680 * We do not need to validate the new limit, since users who can change the
681 * limit are also allowed to exceed the limit.
684 dsl_dir_activate_fs_ss_limit(const char *ddname)
688 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
689 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
690 ZFS_SPACE_CHECK_RESERVED);
692 if (error == EALREADY)
699 * Used to determine if the filesystem_limit or snapshot_limit should be
700 * enforced. We allow the limit to be exceeded if the user has permission to
701 * write the property value. We pass in the creds that we got in the open
702 * context since we will always be the GZ root in syncing context. We also have
703 * to handle the case where we are allowed to change the limit on the current
704 * dataset, but there may be another limit in the tree above.
706 * We can never modify these two properties within a non-global zone. In
707 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
708 * can't use that function since we are already holding the dp_config_rwlock.
709 * In addition, we already have the dd and dealing with snapshots is simplified
720 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr)
722 enforce_res_t enforce = ENFORCE_ALWAYS;
727 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
728 prop == ZFS_PROP_SNAPSHOT_LIMIT);
731 if (crgetzoneid(cr) != GLOBAL_ZONEID)
732 return (ENFORCE_ALWAYS);
734 if (secpolicy_zfs(cr) == 0)
735 return (ENFORCE_NEVER);
738 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
739 return (ENFORCE_ALWAYS);
741 ASSERT(dsl_pool_config_held(dd->dd_pool));
743 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
744 return (ENFORCE_ALWAYS);
746 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) {
747 /* Only root can access zoned fs's from the GZ */
748 enforce = ENFORCE_ALWAYS;
750 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
751 enforce = ENFORCE_ABOVE;
754 dsl_dataset_rele(ds, FTAG);
759 dsl_dir_update_last_remap_txg_sync(void *varg, dmu_tx_t *tx)
761 ddulrt_arg_t *arg = varg;
762 uint64_t last_remap_txg;
763 dsl_dir_t *dd = arg->ddulrta_dd;
764 objset_t *mos = dd->dd_pool->dp_meta_objset;
766 dsl_dir_zapify(dd, tx);
767 if (zap_lookup(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
768 sizeof (last_remap_txg), 1, &last_remap_txg) != 0 ||
769 last_remap_txg < arg->ddlrta_txg) {
770 VERIFY0(zap_update(mos, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
771 sizeof (arg->ddlrta_txg), 1, &arg->ddlrta_txg, tx));
776 dsl_dir_update_last_remap_txg(dsl_dir_t *dd, uint64_t txg)
780 arg.ddlrta_txg = txg;
782 return (dsl_sync_task(spa_name(dd->dd_pool->dp_spa),
783 NULL, dsl_dir_update_last_remap_txg_sync, &arg,
784 1, ZFS_SPACE_CHECK_RESERVED));
788 * Check if adding additional child filesystem(s) would exceed any filesystem
789 * limits or adding additional snapshot(s) would exceed any snapshot limits.
790 * The prop argument indicates which limit to check.
792 * Note that all filesystem limits up to the root (or the highest
793 * initialized) filesystem or the given ancestor must be satisfied.
796 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
797 dsl_dir_t *ancestor, cred_t *cr)
799 objset_t *os = dd->dd_pool->dp_meta_objset;
800 uint64_t limit, count;
802 enforce_res_t enforce;
805 ASSERT(dsl_pool_config_held(dd->dd_pool));
806 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
807 prop == ZFS_PROP_SNAPSHOT_LIMIT);
810 * If we're allowed to change the limit, don't enforce the limit
811 * e.g. this can happen if a snapshot is taken by an administrative
812 * user in the global zone (i.e. a recursive snapshot by root).
813 * However, we must handle the case of delegated permissions where we
814 * are allowed to change the limit on the current dataset, but there
815 * is another limit in the tree above.
817 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);
818 if (enforce == ENFORCE_NEVER)
822 * e.g. if renaming a dataset with no snapshots, count adjustment
828 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
830 * We don't enforce the limit for temporary snapshots. This is
831 * indicated by a NULL cred_t argument.
836 count_prop = DD_FIELD_SNAPSHOT_COUNT;
838 count_prop = DD_FIELD_FILESYSTEM_COUNT;
842 * If an ancestor has been provided, stop checking the limit once we
843 * hit that dir. We need this during rename so that we don't overcount
844 * the check once we recurse up to the common ancestor.
850 * If we hit an uninitialized node while recursing up the tree, we can
851 * stop since we know there is no limit here (or above). The counts are
852 * not valid on this node and we know we won't touch this node's counts.
854 if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object,
855 count_prop, sizeof (count), 1, &count) == ENOENT)
858 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
863 /* Is there a limit which we've hit? */
864 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
865 return (SET_ERROR(EDQUOT));
867 if (dd->dd_parent != NULL)
868 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
875 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
876 * parents. When a new filesystem/snapshot is created, increment the count on
877 * all parents, and when a filesystem/snapshot is destroyed, decrement the
881 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
885 objset_t *os = dd->dd_pool->dp_meta_objset;
888 ASSERT(dsl_pool_config_held(dd->dd_pool));
889 ASSERT(dmu_tx_is_syncing(tx));
890 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
891 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
894 * When we receive an incremental stream into a filesystem that already
895 * exists, a temporary clone is created. We don't count this temporary
896 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
897 * $MOS & $ORIGIN) objsets.
899 if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') &&
900 strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0)
904 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
910 * If we hit an uninitialized node while recursing up the tree, we can
911 * stop since we know the counts are not valid on this node and we
912 * know we shouldn't touch this node's counts. An uninitialized count
913 * on the node indicates that either the feature has not yet been
914 * activated or there are no limits on this part of the tree.
916 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
917 prop, sizeof (count), 1, &count)) == ENOENT)
922 /* Use a signed verify to make sure we're not neg. */
923 VERIFY3S(count, >=, 0);
925 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
928 /* Roll up this additional count into our ancestors */
929 if (dd->dd_parent != NULL)
930 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
934 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
937 objset_t *mos = dp->dp_meta_objset;
939 dsl_dir_phys_t *ddphys;
942 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
943 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
945 VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
946 name, sizeof (uint64_t), 1, &ddobj, tx));
948 /* it's the root dir */
949 VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
950 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
952 VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
953 dmu_buf_will_dirty(dbuf, tx);
954 ddphys = dbuf->db_data;
956 ddphys->dd_creation_time = gethrestime_sec();
958 ddphys->dd_parent_obj = pds->dd_object;
960 /* update the filesystem counts */
961 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
963 ddphys->dd_props_zapobj = zap_create(mos,
964 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
965 ddphys->dd_child_dir_zapobj = zap_create(mos,
966 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
967 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
968 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
970 dmu_buf_rele(dbuf, FTAG);
976 dsl_dir_is_clone(dsl_dir_t *dd)
978 return (dsl_dir_phys(dd)->dd_origin_obj &&
979 (dd->dd_pool->dp_origin_snap == NULL ||
980 dsl_dir_phys(dd)->dd_origin_obj !=
981 dd->dd_pool->dp_origin_snap->ds_object));
985 dsl_dir_get_used(dsl_dir_t *dd)
987 return (dsl_dir_phys(dd)->dd_used_bytes);
991 dsl_dir_get_compressed(dsl_dir_t *dd)
993 return (dsl_dir_phys(dd)->dd_compressed_bytes);
997 dsl_dir_get_quota(dsl_dir_t *dd)
999 return (dsl_dir_phys(dd)->dd_quota);
1003 dsl_dir_get_reservation(dsl_dir_t *dd)
1005 return (dsl_dir_phys(dd)->dd_reserved);
1009 dsl_dir_get_compressratio(dsl_dir_t *dd)
1011 /* a fixed point number, 100x the ratio */
1012 return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
1013 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
1014 dsl_dir_phys(dd)->dd_compressed_bytes));
1018 dsl_dir_get_logicalused(dsl_dir_t *dd)
1020 return (dsl_dir_phys(dd)->dd_uncompressed_bytes);
1024 dsl_dir_get_usedsnap(dsl_dir_t *dd)
1026 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
1030 dsl_dir_get_usedds(dsl_dir_t *dd)
1032 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
1036 dsl_dir_get_usedrefreserv(dsl_dir_t *dd)
1038 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
1042 dsl_dir_get_usedchild(dsl_dir_t *dd)
1044 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
1045 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
1049 dsl_dir_get_origin(dsl_dir_t *dd, char *buf)
1052 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
1053 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1055 dsl_dataset_name(ds, buf);
1057 dsl_dataset_rele(ds, FTAG);
1061 dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)
1063 if (dsl_dir_is_zapified(dd)) {
1064 objset_t *os = dd->dd_pool->dp_meta_objset;
1065 return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1066 sizeof (*count), 1, count));
1073 dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)
1075 if (dsl_dir_is_zapified(dd)) {
1076 objset_t *os = dd->dd_pool->dp_meta_objset;
1077 return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1078 sizeof (*count), 1, count));
1085 dsl_dir_get_remaptxg(dsl_dir_t *dd, uint64_t *count)
1087 if (dsl_dir_is_zapified(dd)) {
1088 objset_t *os = dd->dd_pool->dp_meta_objset;
1089 return (zap_lookup(os, dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1090 sizeof (*count), 1, count));
1098 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
1100 mutex_enter(&dd->dd_lock);
1101 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
1102 dsl_dir_get_quota(dd));
1103 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
1104 dsl_dir_get_reservation(dd));
1105 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
1106 dsl_dir_get_logicalused(dd));
1107 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1108 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
1109 dsl_dir_get_usedsnap(dd));
1110 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
1111 dsl_dir_get_usedds(dd));
1112 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
1113 dsl_dir_get_usedrefreserv(dd));
1114 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
1115 dsl_dir_get_usedchild(dd));
1117 mutex_exit(&dd->dd_lock);
1120 if (dsl_dir_get_filesystem_count(dd, &count) == 0) {
1121 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,
1124 if (dsl_dir_get_snapshot_count(dd, &count) == 0) {
1125 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,
1128 if (dsl_dir_get_remaptxg(dd, &count) == 0) {
1129 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REMAPTXG,
1133 if (dsl_dir_is_clone(dd)) {
1134 char buf[ZFS_MAX_DATASET_NAME_LEN];
1135 dsl_dir_get_origin(dd, buf);
1136 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1142 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1144 dsl_pool_t *dp = dd->dd_pool;
1146 ASSERT(dsl_dir_phys(dd));
1148 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1149 /* up the hold count until we can be written out */
1150 dmu_buf_add_ref(dd->dd_dbuf, dd);
1155 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1157 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1158 uint64_t new_accounted =
1159 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1160 return (new_accounted - old_accounted);
1164 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1166 ASSERT(dmu_tx_is_syncing(tx));
1168 mutex_enter(&dd->dd_lock);
1169 ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]);
1170 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1171 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024);
1172 dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0;
1173 mutex_exit(&dd->dd_lock);
1175 /* release the hold from dsl_dir_dirty */
1176 dmu_buf_rele(dd->dd_dbuf, dd);
1180 dsl_dir_space_towrite(dsl_dir_t *dd)
1184 ASSERT(MUTEX_HELD(&dd->dd_lock));
1186 for (int i = 0; i < TXG_SIZE; i++) {
1187 space += dd->dd_space_towrite[i & TXG_MASK];
1188 ASSERT3U(dd->dd_space_towrite[i & TXG_MASK], >=, 0);
1194 * How much space would dd have available if ancestor had delta applied
1195 * to it? If ondiskonly is set, we're only interested in what's
1196 * on-disk, not estimated pending changes.
1199 dsl_dir_space_available(dsl_dir_t *dd,
1200 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1202 uint64_t parentspace, myspace, quota, used;
1205 * If there are no restrictions otherwise, assume we have
1206 * unlimited space available.
1209 parentspace = UINT64_MAX;
1211 if (dd->dd_parent != NULL) {
1212 parentspace = dsl_dir_space_available(dd->dd_parent,
1213 ancestor, delta, ondiskonly);
1216 mutex_enter(&dd->dd_lock);
1217 if (dsl_dir_phys(dd)->dd_quota != 0)
1218 quota = dsl_dir_phys(dd)->dd_quota;
1219 used = dsl_dir_phys(dd)->dd_used_bytes;
1221 used += dsl_dir_space_towrite(dd);
1223 if (dd->dd_parent == NULL) {
1224 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool,
1225 ZFS_SPACE_CHECK_NORMAL);
1226 quota = MIN(quota, poolsize);
1229 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1231 * We have some space reserved, in addition to what our
1234 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1237 if (dd == ancestor) {
1239 ASSERT(used >= -delta);
1241 if (parentspace != UINT64_MAX)
1242 parentspace -= delta;
1250 * the lesser of the space provided by our parent and
1251 * the space left in our quota
1253 myspace = MIN(parentspace, quota - used);
1256 mutex_exit(&dd->dd_lock);
1261 struct tempreserve {
1262 list_node_t tr_node;
1268 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1269 boolean_t ignorequota, list_t *tr_list,
1270 dmu_tx_t *tx, boolean_t first)
1274 struct tempreserve *tr;
1283 ASSERT3U(txg, !=, 0);
1284 ASSERT3S(asize, >, 0);
1286 mutex_enter(&dd->dd_lock);
1289 * Check against the dsl_dir's quota. We don't add in the delta
1290 * when checking for over-quota because they get one free hit.
1292 uint64_t est_inflight = dsl_dir_space_towrite(dd);
1293 for (int i = 0; i < TXG_SIZE; i++)
1294 est_inflight += dd->dd_tempreserved[i];
1295 uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1298 * On the first iteration, fetch the dataset's used-on-disk and
1299 * refreservation values. Also, if checkrefquota is set, test if
1300 * allocating this space would exceed the dataset's refquota.
1302 if (first && tx->tx_objset) {
1304 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1306 error = dsl_dataset_check_quota(ds, !netfree,
1307 asize, est_inflight, &used_on_disk, &ref_rsrv);
1309 mutex_exit(&dd->dd_lock);
1310 DMU_TX_STAT_BUMP(dmu_tx_quota);
1316 * If this transaction will result in a net free of space,
1317 * we want to let it through.
1319 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1322 quota = dsl_dir_phys(dd)->dd_quota;
1325 * Adjust the quota against the actual pool size at the root
1326 * minus any outstanding deferred frees.
1327 * To ensure that it's possible to remove files from a full
1328 * pool without inducing transient overcommits, we throttle
1329 * netfree transactions against a quota that is slightly larger,
1330 * but still within the pool's allocation slop. In cases where
1331 * we're very close to full, this will allow a steady trickle of
1332 * removes to get through.
1334 uint64_t deferred = 0;
1335 if (dd->dd_parent == NULL) {
1336 uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool,
1338 ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL);
1340 if (avail < quota) {
1347 * If they are requesting more space, and our current estimate
1348 * is over quota, they get to try again unless the actual
1349 * on-disk is over quota and there are no pending changes (which
1350 * may free up space for us).
1352 if (used_on_disk + est_inflight >= quota) {
1353 if (est_inflight > 0 || used_on_disk < quota ||
1354 (retval == ENOSPC && used_on_disk < quota + deferred))
1356 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1357 "quota=%lluK tr=%lluK err=%d\n",
1358 used_on_disk>>10, est_inflight>>10,
1359 quota>>10, asize>>10, retval);
1360 mutex_exit(&dd->dd_lock);
1361 DMU_TX_STAT_BUMP(dmu_tx_quota);
1362 return (SET_ERROR(retval));
1365 /* We need to up our estimated delta before dropping dd_lock */
1366 dd->dd_tempreserved[txg & TXG_MASK] += asize;
1368 uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1370 mutex_exit(&dd->dd_lock);
1372 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1374 tr->tr_size = asize;
1375 list_insert_tail(tr_list, tr);
1377 /* see if it's OK with our parent */
1378 if (dd->dd_parent != NULL && parent_rsrv != 0) {
1380 * Recurse on our parent without recursion. This has been
1381 * observed to be potentially large stack usage even within
1382 * the test suite. Largest seen stack was 7632 bytes on linux.
1386 asize = parent_rsrv;
1387 ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1389 goto top_of_function;
1397 * Reserve space in this dsl_dir, to be used in this tx's txg.
1398 * After the space has been dirtied (and dsl_dir_willuse_space()
1399 * has been called), the reservation should be canceled, using
1400 * dsl_dir_tempreserve_clear().
1403 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1404 boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)
1414 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1415 list_create(tr_list, sizeof (struct tempreserve),
1416 offsetof(struct tempreserve, tr_node));
1417 ASSERT3S(asize, >, 0);
1419 err = arc_tempreserve_space(lsize, tx->tx_txg);
1421 struct tempreserve *tr;
1423 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1424 tr->tr_size = lsize;
1425 list_insert_tail(tr_list, tr);
1427 if (err == EAGAIN) {
1429 * If arc_memory_throttle() detected that pageout
1430 * is running and we are low on memory, we delay new
1431 * non-pageout transactions to give pageout an
1434 * It is unfortunate to be delaying while the caller's
1437 txg_delay(dd->dd_pool, tx->tx_txg,
1438 MSEC2NSEC(10), MSEC2NSEC(10));
1439 err = SET_ERROR(ERESTART);
1444 err = dsl_dir_tempreserve_impl(dd, asize, netfree,
1445 B_FALSE, tr_list, tx, B_TRUE);
1449 dsl_dir_tempreserve_clear(tr_list, tx);
1451 *tr_cookiep = tr_list;
1457 * Clear a temporary reservation that we previously made with
1458 * dsl_dir_tempreserve_space().
1461 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1463 int txgidx = tx->tx_txg & TXG_MASK;
1464 list_t *tr_list = tr_cookie;
1465 struct tempreserve *tr;
1467 ASSERT3U(tx->tx_txg, !=, 0);
1469 if (tr_cookie == NULL)
1472 while ((tr = list_head(tr_list)) != NULL) {
1474 mutex_enter(&tr->tr_ds->dd_lock);
1475 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1477 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1478 mutex_exit(&tr->tr_ds->dd_lock);
1480 arc_tempreserve_clear(tr->tr_size);
1482 list_remove(tr_list, tr);
1483 kmem_free(tr, sizeof (struct tempreserve));
1486 kmem_free(tr_list, sizeof (list_t));
1490 * This should be called from open context when we think we're going to write
1491 * or free space, for example when dirtying data. Be conservative; it's okay
1492 * to write less space or free more, but we don't want to write more or free
1493 * less than the amount specified.
1495 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1496 * version however it has been adjusted to use an iterative rather then
1497 * recursive algorithm to minimize stack usage.
1500 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1502 int64_t parent_space;
1506 mutex_enter(&dd->dd_lock);
1508 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1510 est_used = dsl_dir_space_towrite(dd) +
1511 dsl_dir_phys(dd)->dd_used_bytes;
1512 parent_space = parent_delta(dd, est_used, space);
1513 mutex_exit(&dd->dd_lock);
1515 /* Make sure that we clean up dd_space_to* */
1516 dsl_dir_dirty(dd, tx);
1519 space = parent_space;
1520 } while (space && dd);
1523 /* call from syncing context when we actually write/free space for this dd */
1525 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1526 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1528 int64_t accounted_delta;
1531 * dsl_dataset_set_refreservation_sync_impl() calls this with
1532 * dd_lock held, so that it can atomically update
1533 * ds->ds_reserved and the dsl_dir accounting, so that
1534 * dsl_dataset_check_quota() can see dataset and dir accounting
1537 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1539 ASSERT(dmu_tx_is_syncing(tx));
1540 ASSERT(type < DD_USED_NUM);
1542 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1545 mutex_enter(&dd->dd_lock);
1547 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1548 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1549 ASSERT(compressed >= 0 ||
1550 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1551 ASSERT(uncompressed >= 0 ||
1552 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1553 dsl_dir_phys(dd)->dd_used_bytes += used;
1554 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1555 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1557 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1559 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1560 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1565 for (t = 0; t < DD_USED_NUM; t++)
1566 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1567 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1572 mutex_exit(&dd->dd_lock);
1574 if (dd->dd_parent != NULL) {
1575 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1576 accounted_delta, compressed, uncompressed, tx);
1577 dsl_dir_transfer_space(dd->dd_parent,
1578 used - accounted_delta,
1579 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1584 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1585 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1587 ASSERT(dmu_tx_is_syncing(tx));
1588 ASSERT(oldtype < DD_USED_NUM);
1589 ASSERT(newtype < DD_USED_NUM);
1592 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1595 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1596 mutex_enter(&dd->dd_lock);
1598 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1599 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1600 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1601 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1602 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1603 mutex_exit(&dd->dd_lock);
1606 typedef struct dsl_dir_set_qr_arg {
1607 const char *ddsqra_name;
1608 zprop_source_t ddsqra_source;
1609 uint64_t ddsqra_value;
1610 } dsl_dir_set_qr_arg_t;
1613 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1615 dsl_dir_set_qr_arg_t *ddsqra = arg;
1616 dsl_pool_t *dp = dmu_tx_pool(tx);
1619 uint64_t towrite, newval;
1621 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1625 error = dsl_prop_predict(ds->ds_dir, "quota",
1626 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1628 dsl_dataset_rele(ds, FTAG);
1633 dsl_dataset_rele(ds, FTAG);
1637 mutex_enter(&ds->ds_dir->dd_lock);
1639 * If we are doing the preliminary check in open context, and
1640 * there are pending changes, then don't fail it, since the
1641 * pending changes could under-estimate the amount of space to be
1644 towrite = dsl_dir_space_towrite(ds->ds_dir);
1645 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1646 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1647 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1648 error = SET_ERROR(ENOSPC);
1650 mutex_exit(&ds->ds_dir->dd_lock);
1651 dsl_dataset_rele(ds, FTAG);
1656 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1658 dsl_dir_set_qr_arg_t *ddsqra = arg;
1659 dsl_pool_t *dp = dmu_tx_pool(tx);
1663 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1665 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1666 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1667 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1668 &ddsqra->ddsqra_value, tx);
1670 VERIFY0(dsl_prop_get_int_ds(ds,
1671 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1673 newval = ddsqra->ddsqra_value;
1674 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1675 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1678 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1679 mutex_enter(&ds->ds_dir->dd_lock);
1680 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1681 mutex_exit(&ds->ds_dir->dd_lock);
1682 dsl_dataset_rele(ds, FTAG);
1686 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1688 dsl_dir_set_qr_arg_t ddsqra;
1690 ddsqra.ddsqra_name = ddname;
1691 ddsqra.ddsqra_source = source;
1692 ddsqra.ddsqra_value = quota;
1694 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1695 dsl_dir_set_quota_sync, &ddsqra, 0,
1696 ZFS_SPACE_CHECK_EXTRA_RESERVED));
1700 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1702 dsl_dir_set_qr_arg_t *ddsqra = arg;
1703 dsl_pool_t *dp = dmu_tx_pool(tx);
1706 uint64_t newval, used, avail;
1709 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1715 * If we are doing the preliminary check in open context, the
1716 * space estimates may be inaccurate.
1718 if (!dmu_tx_is_syncing(tx)) {
1719 dsl_dataset_rele(ds, FTAG);
1723 error = dsl_prop_predict(ds->ds_dir,
1724 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1725 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1727 dsl_dataset_rele(ds, FTAG);
1731 mutex_enter(&dd->dd_lock);
1732 used = dsl_dir_phys(dd)->dd_used_bytes;
1733 mutex_exit(&dd->dd_lock);
1735 if (dd->dd_parent) {
1736 avail = dsl_dir_space_available(dd->dd_parent,
1739 avail = dsl_pool_adjustedsize(dd->dd_pool,
1740 ZFS_SPACE_CHECK_NORMAL) - used;
1743 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1744 uint64_t delta = MAX(used, newval) -
1745 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1747 if (delta > avail ||
1748 (dsl_dir_phys(dd)->dd_quota > 0 &&
1749 newval > dsl_dir_phys(dd)->dd_quota))
1750 error = SET_ERROR(ENOSPC);
1753 dsl_dataset_rele(ds, FTAG);
1758 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1763 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1765 mutex_enter(&dd->dd_lock);
1766 used = dsl_dir_phys(dd)->dd_used_bytes;
1767 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1768 dsl_dir_phys(dd)->dd_reserved = value;
1770 if (dd->dd_parent != NULL) {
1771 /* Roll up this additional usage into our ancestors */
1772 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1775 mutex_exit(&dd->dd_lock);
1779 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1781 dsl_dir_set_qr_arg_t *ddsqra = arg;
1782 dsl_pool_t *dp = dmu_tx_pool(tx);
1786 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1788 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1789 dsl_prop_set_sync_impl(ds,
1790 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1791 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1792 &ddsqra->ddsqra_value, tx);
1794 VERIFY0(dsl_prop_get_int_ds(ds,
1795 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1797 newval = ddsqra->ddsqra_value;
1798 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1799 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1800 (longlong_t)newval);
1803 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1804 dsl_dataset_rele(ds, FTAG);
1808 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1809 uint64_t reservation)
1811 dsl_dir_set_qr_arg_t ddsqra;
1813 ddsqra.ddsqra_name = ddname;
1814 ddsqra.ddsqra_source = source;
1815 ddsqra.ddsqra_value = reservation;
1817 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1818 dsl_dir_set_reservation_sync, &ddsqra, 0,
1819 ZFS_SPACE_CHECK_EXTRA_RESERVED));
1823 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1825 for (; ds1; ds1 = ds1->dd_parent) {
1827 for (dd = ds2; dd; dd = dd->dd_parent) {
1836 * If delta is applied to dd, how much of that delta would be applied to
1837 * ancestor? Syncing context only.
1840 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1845 mutex_enter(&dd->dd_lock);
1846 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1847 mutex_exit(&dd->dd_lock);
1848 return (would_change(dd->dd_parent, delta, ancestor));
1851 typedef struct dsl_dir_rename_arg {
1852 const char *ddra_oldname;
1853 const char *ddra_newname;
1855 } dsl_dir_rename_arg_t;
1859 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1862 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1864 dsl_dataset_name(ds, namebuf);
1866 if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN)
1867 return (SET_ERROR(ENAMETOOLONG));
1872 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1874 dsl_dir_rename_arg_t *ddra = arg;
1875 dsl_pool_t *dp = dmu_tx_pool(tx);
1876 dsl_dir_t *dd, *newparent;
1877 const char *mynewname;
1879 int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname);
1881 /* target dir should exist */
1882 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1886 /* new parent should exist */
1887 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1888 &newparent, &mynewname);
1890 dsl_dir_rele(dd, FTAG);
1894 /* can't rename to different pool */
1895 if (dd->dd_pool != newparent->dd_pool) {
1896 dsl_dir_rele(newparent, FTAG);
1897 dsl_dir_rele(dd, FTAG);
1898 return (SET_ERROR(EXDEV));
1901 /* new name should not already exist */
1902 if (mynewname == NULL) {
1903 dsl_dir_rele(newparent, FTAG);
1904 dsl_dir_rele(dd, FTAG);
1905 return (SET_ERROR(EEXIST));
1908 /* if the name length is growing, validate child name lengths */
1910 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1911 &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1913 dsl_dir_rele(newparent, FTAG);
1914 dsl_dir_rele(dd, FTAG);
1919 if (dmu_tx_is_syncing(tx)) {
1920 if (spa_feature_is_active(dp->dp_spa,
1921 SPA_FEATURE_FS_SS_LIMIT)) {
1923 * Although this is the check function and we don't
1924 * normally make on-disk changes in check functions,
1925 * we need to do that here.
1927 * Ensure this portion of the tree's counts have been
1928 * initialized in case the new parent has limits set.
1930 dsl_dir_init_fs_ss_count(dd, tx);
1934 if (newparent != dd->dd_parent) {
1935 /* is there enough space? */
1937 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1938 dsl_dir_phys(dd)->dd_reserved);
1939 objset_t *os = dd->dd_pool->dp_meta_objset;
1940 uint64_t fs_cnt = 0;
1941 uint64_t ss_cnt = 0;
1943 if (dsl_dir_is_zapified(dd)) {
1946 err = zap_lookup(os, dd->dd_object,
1947 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1949 if (err != ENOENT && err != 0) {
1950 dsl_dir_rele(newparent, FTAG);
1951 dsl_dir_rele(dd, FTAG);
1956 * have to add 1 for the filesystem itself that we're
1961 err = zap_lookup(os, dd->dd_object,
1962 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
1964 if (err != ENOENT && err != 0) {
1965 dsl_dir_rele(newparent, FTAG);
1966 dsl_dir_rele(dd, FTAG);
1971 /* check for encryption errors */
1972 error = dsl_dir_rename_crypt_check(dd, newparent);
1974 dsl_dir_rele(newparent, FTAG);
1975 dsl_dir_rele(dd, FTAG);
1976 return (SET_ERROR(EACCES));
1979 /* no rename into our descendant */
1980 if (closest_common_ancestor(dd, newparent) == dd) {
1981 dsl_dir_rele(newparent, FTAG);
1982 dsl_dir_rele(dd, FTAG);
1983 return (SET_ERROR(EINVAL));
1986 error = dsl_dir_transfer_possible(dd->dd_parent,
1987 newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);
1989 dsl_dir_rele(newparent, FTAG);
1990 dsl_dir_rele(dd, FTAG);
1995 dsl_dir_rele(newparent, FTAG);
1996 dsl_dir_rele(dd, FTAG);
2001 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
2003 dsl_dir_rename_arg_t *ddra = arg;
2004 dsl_pool_t *dp = dmu_tx_pool(tx);
2005 dsl_dir_t *dd, *newparent;
2006 const char *mynewname;
2008 objset_t *mos = dp->dp_meta_objset;
2010 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
2011 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
2014 /* Log this before we change the name. */
2015 spa_history_log_internal_dd(dd, "rename", tx,
2016 "-> %s", ddra->ddra_newname);
2018 if (newparent != dd->dd_parent) {
2019 objset_t *os = dd->dd_pool->dp_meta_objset;
2020 uint64_t fs_cnt = 0;
2021 uint64_t ss_cnt = 0;
2024 * We already made sure the dd counts were initialized in the
2027 if (spa_feature_is_active(dp->dp_spa,
2028 SPA_FEATURE_FS_SS_LIMIT)) {
2029 VERIFY0(zap_lookup(os, dd->dd_object,
2030 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2032 /* add 1 for the filesystem itself that we're moving */
2035 VERIFY0(zap_lookup(os, dd->dd_object,
2036 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2040 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
2041 DD_FIELD_FILESYSTEM_COUNT, tx);
2042 dsl_fs_ss_count_adjust(newparent, fs_cnt,
2043 DD_FIELD_FILESYSTEM_COUNT, tx);
2045 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
2046 DD_FIELD_SNAPSHOT_COUNT, tx);
2047 dsl_fs_ss_count_adjust(newparent, ss_cnt,
2048 DD_FIELD_SNAPSHOT_COUNT, tx);
2050 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
2051 -dsl_dir_phys(dd)->dd_used_bytes,
2052 -dsl_dir_phys(dd)->dd_compressed_bytes,
2053 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2054 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
2055 dsl_dir_phys(dd)->dd_used_bytes,
2056 dsl_dir_phys(dd)->dd_compressed_bytes,
2057 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2059 if (dsl_dir_phys(dd)->dd_reserved >
2060 dsl_dir_phys(dd)->dd_used_bytes) {
2061 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
2062 dsl_dir_phys(dd)->dd_used_bytes;
2064 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
2065 -unused_rsrv, 0, 0, tx);
2066 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
2067 unused_rsrv, 0, 0, tx);
2071 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2073 /* remove from old parent zapobj */
2074 error = zap_remove(mos,
2075 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
2079 (void) strlcpy(dd->dd_myname, mynewname,
2080 sizeof (dd->dd_myname));
2081 dsl_dir_rele(dd->dd_parent, dd);
2082 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
2083 VERIFY0(dsl_dir_hold_obj(dp,
2084 newparent->dd_object, NULL, dd, &dd->dd_parent));
2086 /* add to new parent zapobj */
2087 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
2088 dd->dd_myname, 8, 1, &dd->dd_object, tx));
2090 zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname,
2091 ddra->ddra_newname, B_TRUE);
2093 dsl_prop_notify_all(dd);
2095 dsl_dir_rele(newparent, FTAG);
2096 dsl_dir_rele(dd, FTAG);
2100 dsl_dir_rename(const char *oldname, const char *newname)
2102 dsl_dir_rename_arg_t ddra;
2104 ddra.ddra_oldname = oldname;
2105 ddra.ddra_newname = newname;
2106 ddra.ddra_cred = CRED();
2108 return (dsl_sync_task(oldname,
2109 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
2110 3, ZFS_SPACE_CHECK_RESERVED));
2114 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
2115 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr)
2117 dsl_dir_t *ancestor;
2122 ancestor = closest_common_ancestor(sdd, tdd);
2123 adelta = would_change(sdd, -space, ancestor);
2124 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
2126 return (SET_ERROR(ENOSPC));
2128 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
2132 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
2141 dsl_dir_snap_cmtime(dsl_dir_t *dd)
2145 mutex_enter(&dd->dd_lock);
2146 t = dd->dd_snap_cmtime;
2147 mutex_exit(&dd->dd_lock);
2153 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
2158 mutex_enter(&dd->dd_lock);
2159 dd->dd_snap_cmtime = t;
2160 mutex_exit(&dd->dd_lock);
2164 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
2166 objset_t *mos = dd->dd_pool->dp_meta_objset;
2167 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
2171 dsl_dir_is_zapified(dsl_dir_t *dd)
2173 dmu_object_info_t doi;
2175 dmu_object_info_from_db(dd->dd_dbuf, &doi);
2176 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2179 #if defined(_KERNEL)
2180 EXPORT_SYMBOL(dsl_dir_set_quota);
2181 EXPORT_SYMBOL(dsl_dir_set_reservation);