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, 2018 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.
28 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. 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/spa_impl.h>
42 #include <sys/metaslab.h>
46 #include <sys/sunddi.h>
47 #include <sys/zfeature.h>
48 #include <sys/policy.h>
49 #include <sys/zfs_vfsops.h>
50 #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 initialized 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().
124 extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd);
126 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);
128 typedef struct ddulrt_arg {
129 dsl_dir_t *ddulrta_dd;
134 dsl_dir_evict_async(void *dbu)
138 dsl_pool_t *dp __maybe_unused = dd->dd_pool;
142 for (t = 0; t < TXG_SIZE; t++) {
143 ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));
144 ASSERT(dd->dd_tempreserved[t] == 0);
145 ASSERT(dd->dd_space_towrite[t] == 0);
149 dsl_dir_async_rele(dd->dd_parent, dd);
151 spa_async_close(dd->dd_pool->dp_spa, dd);
153 if (dsl_deadlist_is_open(&dd->dd_livelist))
154 dsl_dir_livelist_close(dd);
157 cv_destroy(&dd->dd_activity_cv);
158 mutex_destroy(&dd->dd_activity_lock);
159 mutex_destroy(&dd->dd_lock);
160 kmem_free(dd, sizeof (dsl_dir_t));
164 dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
165 const char *tail, void *tag, dsl_dir_t **ddp)
169 dmu_object_info_t doi;
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);
179 dmu_object_info_from_db(dbuf, &doi);
180 ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);
181 ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));
186 dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);
187 dd->dd_object = ddobj;
191 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);
192 mutex_init(&dd->dd_activity_lock, NULL, MUTEX_DEFAULT, NULL);
193 cv_init(&dd->dd_activity_cv, NULL, CV_DEFAULT, NULL);
196 if (dsl_dir_is_zapified(dd)) {
197 err = zap_lookup(dp->dp_meta_objset,
198 ddobj, DD_FIELD_CRYPTO_KEY_OBJ,
199 sizeof (uint64_t), 1, &dd->dd_crypto_obj);
201 /* check for on-disk format errata */
202 if (dsl_dir_incompatible_encryption_version(
204 dp->dp_spa->spa_errata =
205 ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
207 } else if (err != ENOENT) {
212 dsl_dir_snap_cmtime_update(dd);
214 if (dsl_dir_phys(dd)->dd_parent_obj) {
215 err = dsl_dir_hold_obj(dp,
216 dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,
224 err = zap_lookup(dp->dp_meta_objset,
225 dsl_dir_phys(dd->dd_parent)->
226 dd_child_dir_zapobj, tail,
227 sizeof (foundobj), 1, &foundobj);
228 ASSERT(err || foundobj == ddobj);
230 (void) strlcpy(dd->dd_myname, tail,
231 sizeof (dd->dd_myname));
233 err = zap_value_search(dp->dp_meta_objset,
234 dsl_dir_phys(dd->dd_parent)->
236 ddobj, 0, dd->dd_myname);
241 (void) strlcpy(dd->dd_myname, spa_name(dp->dp_spa),
242 sizeof (dd->dd_myname));
245 if (dsl_dir_is_clone(dd)) {
246 dmu_buf_t *origin_bonus;
247 dsl_dataset_phys_t *origin_phys;
250 * We can't open the origin dataset, because
251 * that would require opening this dsl_dir.
252 * Just look at its phys directly instead.
254 err = dmu_bonus_hold(dp->dp_meta_objset,
255 dsl_dir_phys(dd)->dd_origin_obj, FTAG,
259 origin_phys = origin_bonus->db_data;
261 origin_phys->ds_creation_txg;
262 dmu_buf_rele(origin_bonus, FTAG);
263 if (dsl_dir_is_zapified(dd)) {
265 err = zap_lookup(dp->dp_meta_objset,
266 dd->dd_object, DD_FIELD_LIVELIST,
267 sizeof (uint64_t), 1, &obj);
269 dsl_dir_livelist_open(dd, obj);
270 else if (err != ENOENT)
275 dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,
277 winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);
278 if (winner != NULL) {
280 dsl_dir_rele(dd->dd_parent, dd);
281 if (dsl_deadlist_is_open(&dd->dd_livelist))
282 dsl_dir_livelist_close(dd);
284 cv_destroy(&dd->dd_activity_cv);
285 mutex_destroy(&dd->dd_activity_lock);
286 mutex_destroy(&dd->dd_lock);
287 kmem_free(dd, sizeof (dsl_dir_t));
290 spa_open_ref(dp->dp_spa, dd);
295 * The dsl_dir_t has both open-to-close and instantiate-to-evict
296 * holds on the spa. We need the open-to-close holds because
297 * otherwise the spa_refcnt wouldn't change when we open a
298 * dir which the spa also has open, so we could incorrectly
299 * think it was OK to unload/export/destroy the pool. We need
300 * the instantiate-to-evict hold because the dsl_dir_t has a
301 * pointer to the dd_pool, which has a pointer to the spa_t.
303 spa_open_ref(dp->dp_spa, tag);
304 ASSERT3P(dd->dd_pool, ==, dp);
305 ASSERT3U(dd->dd_object, ==, ddobj);
306 ASSERT3P(dd->dd_dbuf, ==, dbuf);
312 dsl_dir_rele(dd->dd_parent, dd);
313 if (dsl_deadlist_is_open(&dd->dd_livelist))
314 dsl_dir_livelist_close(dd);
316 cv_destroy(&dd->dd_activity_cv);
317 mutex_destroy(&dd->dd_activity_lock);
318 mutex_destroy(&dd->dd_lock);
319 kmem_free(dd, sizeof (dsl_dir_t));
320 dmu_buf_rele(dbuf, tag);
325 dsl_dir_rele(dsl_dir_t *dd, void *tag)
327 dprintf_dd(dd, "%s\n", "");
328 spa_close(dd->dd_pool->dp_spa, tag);
329 dmu_buf_rele(dd->dd_dbuf, tag);
333 * Remove a reference to the given dsl dir that is being asynchronously
334 * released. Async releases occur from a taskq performing eviction of
335 * dsl datasets and dirs. This process is identical to a normal release
336 * with the exception of using the async API for releasing the reference on
340 dsl_dir_async_rele(dsl_dir_t *dd, void *tag)
342 dprintf_dd(dd, "%s\n", "");
343 spa_async_close(dd->dd_pool->dp_spa, tag);
344 dmu_buf_rele(dd->dd_dbuf, tag);
347 /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */
349 dsl_dir_name(dsl_dir_t *dd, char *buf)
352 dsl_dir_name(dd->dd_parent, buf);
353 VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,
354 ZFS_MAX_DATASET_NAME_LEN);
358 if (!MUTEX_HELD(&dd->dd_lock)) {
360 * recursive mutex so that we can use
361 * dprintf_dd() with dd_lock held
363 mutex_enter(&dd->dd_lock);
364 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
365 <, ZFS_MAX_DATASET_NAME_LEN);
366 mutex_exit(&dd->dd_lock);
368 VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),
369 <, ZFS_MAX_DATASET_NAME_LEN);
373 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
375 dsl_dir_namelen(dsl_dir_t *dd)
380 /* parent's name + 1 for the "/" */
381 result = dsl_dir_namelen(dd->dd_parent) + 1;
384 if (!MUTEX_HELD(&dd->dd_lock)) {
385 /* see dsl_dir_name */
386 mutex_enter(&dd->dd_lock);
387 result += strlen(dd->dd_myname);
388 mutex_exit(&dd->dd_lock);
390 result += strlen(dd->dd_myname);
397 getcomponent(const char *path, char *component, const char **nextp)
401 if ((path == NULL) || (path[0] == '\0'))
402 return (SET_ERROR(ENOENT));
403 /* This would be a good place to reserve some namespace... */
404 p = strpbrk(path, "/@");
405 if (p && (p[1] == '/' || p[1] == '@')) {
406 /* two separators in a row */
407 return (SET_ERROR(EINVAL));
409 if (p == NULL || p == path) {
411 * if the first thing is an @ or /, it had better be an
412 * @ and it had better not have any more ats or slashes,
413 * and it had better have something after the @.
416 (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))
417 return (SET_ERROR(EINVAL));
418 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)
419 return (SET_ERROR(ENAMETOOLONG));
420 (void) strlcpy(component, path, ZFS_MAX_DATASET_NAME_LEN);
422 } else if (p[0] == '/') {
423 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
424 return (SET_ERROR(ENAMETOOLONG));
425 (void) strncpy(component, path, p - path);
426 component[p - path] = '\0';
428 } else if (p[0] == '@') {
430 * if the next separator is an @, there better not be
433 if (strchr(path, '/'))
434 return (SET_ERROR(EINVAL));
435 if (p - path >= ZFS_MAX_DATASET_NAME_LEN)
436 return (SET_ERROR(ENAMETOOLONG));
437 (void) strncpy(component, path, p - path);
438 component[p - path] = '\0';
440 panic("invalid p=%p", (void *)p);
447 * Return the dsl_dir_t, and possibly the last component which couldn't
448 * be found in *tail. The name must be in the specified dsl_pool_t. This
449 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
450 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
451 * (*tail)[0] == '@' means that the last component is a snapshot.
454 dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
455 dsl_dir_t **ddp, const char **tailp)
458 const char *spaname, *next, *nextnext = NULL;
463 buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);
464 err = getcomponent(name, buf, &next);
468 /* Make sure the name is in the specified pool. */
469 spaname = spa_name(dp->dp_spa);
470 if (strcmp(buf, spaname) != 0) {
471 err = SET_ERROR(EXDEV);
475 ASSERT(dsl_pool_config_held(dp));
477 err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);
482 while (next != NULL) {
484 err = getcomponent(next, buf, &nextnext);
487 ASSERT(next[0] != '\0');
490 dprintf("looking up %s in obj%lld\n",
491 buf, dsl_dir_phys(dd)->dd_child_dir_zapobj);
493 err = zap_lookup(dp->dp_meta_objset,
494 dsl_dir_phys(dd)->dd_child_dir_zapobj,
495 buf, sizeof (ddobj), 1, &ddobj);
502 err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);
505 dsl_dir_rele(dd, tag);
511 dsl_dir_rele(dd, tag);
516 * It's an error if there's more than one component left, or
517 * tailp==NULL and there's any component left.
520 (tailp == NULL || (nextnext && nextnext[0] != '\0'))) {
522 dsl_dir_rele(dd, tag);
523 dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);
524 err = SET_ERROR(ENOENT);
531 kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN);
536 * If the counts are already initialized for this filesystem and its
537 * descendants then do nothing, otherwise initialize the counts.
539 * The counts on this filesystem, and those below, may be uninitialized due to
540 * either the use of a pre-existing pool which did not support the
541 * filesystem/snapshot limit feature, or one in which the feature had not yet
544 * Recursively descend the filesystem tree and update the filesystem/snapshot
545 * counts on each filesystem below, then update the cumulative count on the
546 * current filesystem. If the filesystem already has a count set on it,
547 * then we know that its counts, and the counts on the filesystems below it,
548 * are already correct, so we don't have to update this filesystem.
551 dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)
553 uint64_t my_fs_cnt = 0;
554 uint64_t my_ss_cnt = 0;
555 dsl_pool_t *dp = dd->dd_pool;
556 objset_t *os = dp->dp_meta_objset;
561 ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));
562 ASSERT(dsl_pool_config_held(dp));
563 ASSERT(dmu_tx_is_syncing(tx));
565 dsl_dir_zapify(dd, tx);
568 * If the filesystem count has already been initialized then we
569 * don't need to recurse down any further.
571 if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)
574 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
575 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
577 /* Iterate my child dirs */
578 for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);
579 zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {
583 VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,
587 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets.
589 if (chld_dd->dd_myname[0] == '$') {
590 dsl_dir_rele(chld_dd, FTAG);
594 my_fs_cnt++; /* count this child */
596 dsl_dir_init_fs_ss_count(chld_dd, tx);
598 VERIFY0(zap_lookup(os, chld_dd->dd_object,
599 DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));
601 VERIFY0(zap_lookup(os, chld_dd->dd_object,
602 DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));
605 dsl_dir_rele(chld_dd, FTAG);
608 /* Count my snapshots (we counted children's snapshots above) */
609 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
610 dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));
612 for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);
613 zap_cursor_retrieve(zc, za) == 0;
614 zap_cursor_advance(zc)) {
615 /* Don't count temporary snapshots */
616 if (za->za_name[0] != '%')
621 dsl_dataset_rele(ds, FTAG);
623 kmem_free(zc, sizeof (zap_cursor_t));
624 kmem_free(za, sizeof (zap_attribute_t));
626 /* we're in a sync task, update counts */
627 dmu_buf_will_dirty(dd->dd_dbuf, tx);
628 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
629 sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));
630 VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
631 sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));
635 dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)
637 char *ddname = (char *)arg;
638 dsl_pool_t *dp = dmu_tx_pool(tx);
643 error = dsl_dataset_hold(dp, ddname, FTAG, &ds);
647 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {
648 dsl_dataset_rele(ds, FTAG);
649 return (SET_ERROR(ENOTSUP));
653 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&
654 dsl_dir_is_zapified(dd) &&
655 zap_contains(dp->dp_meta_objset, dd->dd_object,
656 DD_FIELD_FILESYSTEM_COUNT) == 0) {
657 dsl_dataset_rele(ds, FTAG);
658 return (SET_ERROR(EALREADY));
661 dsl_dataset_rele(ds, FTAG);
666 dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)
668 char *ddname = (char *)arg;
669 dsl_pool_t *dp = dmu_tx_pool(tx);
673 VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));
675 spa = dsl_dataset_get_spa(ds);
677 if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {
679 * Since the feature was not active and we're now setting a
680 * limit, increment the feature-active counter so that the
681 * feature becomes active for the first time.
683 * We are already in a sync task so we can update the MOS.
685 spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);
689 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
690 * we need to ensure the counts are correct. Descend down the tree from
691 * this point and update all of the counts to be accurate.
693 dsl_dir_init_fs_ss_count(ds->ds_dir, tx);
695 dsl_dataset_rele(ds, FTAG);
699 * Make sure the feature is enabled and activate it if necessary.
700 * Since we're setting a limit, ensure the on-disk counts are valid.
701 * This is only called by the ioctl path when setting a limit value.
703 * We do not need to validate the new limit, since users who can change the
704 * limit are also allowed to exceed the limit.
707 dsl_dir_activate_fs_ss_limit(const char *ddname)
711 error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,
712 dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,
713 ZFS_SPACE_CHECK_RESERVED);
715 if (error == EALREADY)
722 * Used to determine if the filesystem_limit or snapshot_limit should be
723 * enforced. We allow the limit to be exceeded if the user has permission to
724 * write the property value. We pass in the creds that we got in the open
725 * context since we will always be the GZ root in syncing context. We also have
726 * to handle the case where we are allowed to change the limit on the current
727 * dataset, but there may be another limit in the tree above.
729 * We can never modify these two properties within a non-global zone. In
730 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
731 * can't use that function since we are already holding the dp_config_rwlock.
732 * In addition, we already have the dd and dealing with snapshots is simplified
743 dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop,
744 cred_t *cr, proc_t *proc)
746 enforce_res_t enforce = ENFORCE_ALWAYS;
750 const char *zonedstr;
752 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
753 prop == ZFS_PROP_SNAPSHOT_LIMIT);
756 if (crgetzoneid(cr) != GLOBAL_ZONEID)
757 return (ENFORCE_ALWAYS);
760 * We are checking the saved credentials of the user process, which is
761 * not the current process. Note that we can't use secpolicy_zfs(),
762 * because it only works if the cred is that of the current process (on
765 if (secpolicy_zfs_proc(cr, proc) == 0)
766 return (ENFORCE_NEVER);
769 if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)
770 return (ENFORCE_ALWAYS);
772 ASSERT(dsl_pool_config_held(dd->dd_pool));
774 if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)
775 return (ENFORCE_ALWAYS);
777 zonedstr = zfs_prop_to_name(ZFS_PROP_ZONED);
778 if (dsl_prop_get_ds(ds, zonedstr, 8, 1, &zoned, NULL) || zoned) {
779 /* Only root can access zoned fs's from the GZ */
780 enforce = ENFORCE_ALWAYS;
782 if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)
783 enforce = ENFORCE_ABOVE;
786 dsl_dataset_rele(ds, FTAG);
791 * Check if adding additional child filesystem(s) would exceed any filesystem
792 * limits or adding additional snapshot(s) would exceed any snapshot limits.
793 * The prop argument indicates which limit to check.
795 * Note that all filesystem limits up to the root (or the highest
796 * initialized) filesystem or the given ancestor must be satisfied.
799 dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,
800 dsl_dir_t *ancestor, cred_t *cr, proc_t *proc)
802 objset_t *os = dd->dd_pool->dp_meta_objset;
803 uint64_t limit, count;
805 enforce_res_t enforce;
808 ASSERT(dsl_pool_config_held(dd->dd_pool));
809 ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||
810 prop == ZFS_PROP_SNAPSHOT_LIMIT);
813 * If we're allowed to change the limit, don't enforce the limit
814 * e.g. this can happen if a snapshot is taken by an administrative
815 * user in the global zone (i.e. a recursive snapshot by root).
816 * However, we must handle the case of delegated permissions where we
817 * are allowed to change the limit on the current dataset, but there
818 * is another limit in the tree above.
820 enforce = dsl_enforce_ds_ss_limits(dd, prop, cr, proc);
821 if (enforce == ENFORCE_NEVER)
825 * e.g. if renaming a dataset with no snapshots, count adjustment
831 if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {
833 * We don't enforce the limit for temporary snapshots. This is
834 * indicated by a NULL cred_t argument.
839 count_prop = DD_FIELD_SNAPSHOT_COUNT;
841 count_prop = DD_FIELD_FILESYSTEM_COUNT;
845 * If an ancestor has been provided, stop checking the limit once we
846 * hit that dir. We need this during rename so that we don't overcount
847 * the check once we recurse up to the common ancestor.
853 * If we hit an uninitialized node while recursing up the tree, we can
854 * stop since we know there is no limit here (or above). The counts are
855 * not valid on this node and we know we won't touch this node's counts.
857 if (!dsl_dir_is_zapified(dd))
859 err = zap_lookup(os, dd->dd_object,
860 count_prop, sizeof (count), 1, &count);
866 err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,
871 /* Is there a limit which we've hit? */
872 if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)
873 return (SET_ERROR(EDQUOT));
875 if (dd->dd_parent != NULL)
876 err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,
883 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
884 * parents. When a new filesystem/snapshot is created, increment the count on
885 * all parents, and when a filesystem/snapshot is destroyed, decrement the
889 dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,
893 objset_t *os = dd->dd_pool->dp_meta_objset;
896 ASSERT(dsl_pool_config_held(dd->dd_pool));
897 ASSERT(dmu_tx_is_syncing(tx));
898 ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||
899 strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);
902 * We don't do accounting for hidden ($FREE, $MOS & $ORIGIN) objsets.
904 if (dd->dd_myname[0] == '$' && strcmp(prop,
905 DD_FIELD_FILESYSTEM_COUNT) == 0) {
910 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
916 * If we hit an uninitialized node while recursing up the tree, we can
917 * stop since we know the counts are not valid on this node and we
918 * know we shouldn't touch this node's counts. An uninitialized count
919 * on the node indicates that either the feature has not yet been
920 * activated or there are no limits on this part of the tree.
922 if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,
923 prop, sizeof (count), 1, &count)) == ENOENT)
928 /* Use a signed verify to make sure we're not neg. */
929 VERIFY3S(count, >=, 0);
931 VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,
934 /* Roll up this additional count into our ancestors */
935 if (dd->dd_parent != NULL)
936 dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);
940 dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,
943 objset_t *mos = dp->dp_meta_objset;
945 dsl_dir_phys_t *ddphys;
948 ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,
949 DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);
951 VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,
952 name, sizeof (uint64_t), 1, &ddobj, tx));
954 /* it's the root dir */
955 VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,
956 DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));
958 VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));
959 dmu_buf_will_dirty(dbuf, tx);
960 ddphys = dbuf->db_data;
962 ddphys->dd_creation_time = gethrestime_sec();
964 ddphys->dd_parent_obj = pds->dd_object;
966 /* update the filesystem counts */
967 dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);
969 ddphys->dd_props_zapobj = zap_create(mos,
970 DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);
971 ddphys->dd_child_dir_zapobj = zap_create(mos,
972 DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);
973 if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)
974 ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;
976 dmu_buf_rele(dbuf, FTAG);
982 dsl_dir_is_clone(dsl_dir_t *dd)
984 return (dsl_dir_phys(dd)->dd_origin_obj &&
985 (dd->dd_pool->dp_origin_snap == NULL ||
986 dsl_dir_phys(dd)->dd_origin_obj !=
987 dd->dd_pool->dp_origin_snap->ds_object));
991 dsl_dir_get_used(dsl_dir_t *dd)
993 return (dsl_dir_phys(dd)->dd_used_bytes);
997 dsl_dir_get_compressed(dsl_dir_t *dd)
999 return (dsl_dir_phys(dd)->dd_compressed_bytes);
1003 dsl_dir_get_quota(dsl_dir_t *dd)
1005 return (dsl_dir_phys(dd)->dd_quota);
1009 dsl_dir_get_reservation(dsl_dir_t *dd)
1011 return (dsl_dir_phys(dd)->dd_reserved);
1015 dsl_dir_get_compressratio(dsl_dir_t *dd)
1017 /* a fixed point number, 100x the ratio */
1018 return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :
1019 (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /
1020 dsl_dir_phys(dd)->dd_compressed_bytes));
1024 dsl_dir_get_logicalused(dsl_dir_t *dd)
1026 return (dsl_dir_phys(dd)->dd_uncompressed_bytes);
1030 dsl_dir_get_usedsnap(dsl_dir_t *dd)
1032 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);
1036 dsl_dir_get_usedds(dsl_dir_t *dd)
1038 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);
1042 dsl_dir_get_usedrefreserv(dsl_dir_t *dd)
1044 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);
1048 dsl_dir_get_usedchild(dsl_dir_t *dd)
1050 return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +
1051 dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);
1055 dsl_dir_get_origin(dsl_dir_t *dd, char *buf)
1058 VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,
1059 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));
1061 dsl_dataset_name(ds, buf);
1063 dsl_dataset_rele(ds, FTAG);
1067 dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)
1069 if (dsl_dir_is_zapified(dd)) {
1070 objset_t *os = dd->dd_pool->dp_meta_objset;
1071 return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,
1072 sizeof (*count), 1, count));
1074 return (SET_ERROR(ENOENT));
1079 dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)
1081 if (dsl_dir_is_zapified(dd)) {
1082 objset_t *os = dd->dd_pool->dp_meta_objset;
1083 return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,
1084 sizeof (*count), 1, count));
1086 return (SET_ERROR(ENOENT));
1091 dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)
1093 mutex_enter(&dd->dd_lock);
1094 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,
1095 dsl_dir_get_quota(dd));
1096 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,
1097 dsl_dir_get_reservation(dd));
1098 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,
1099 dsl_dir_get_logicalused(dd));
1100 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1101 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,
1102 dsl_dir_get_usedsnap(dd));
1103 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,
1104 dsl_dir_get_usedds(dd));
1105 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,
1106 dsl_dir_get_usedrefreserv(dd));
1107 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,
1108 dsl_dir_get_usedchild(dd));
1110 mutex_exit(&dd->dd_lock);
1113 if (dsl_dir_get_filesystem_count(dd, &count) == 0) {
1114 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,
1117 if (dsl_dir_get_snapshot_count(dd, &count) == 0) {
1118 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,
1122 if (dsl_dir_is_clone(dd)) {
1123 char buf[ZFS_MAX_DATASET_NAME_LEN];
1124 dsl_dir_get_origin(dd, buf);
1125 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);
1131 dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)
1133 dsl_pool_t *dp = dd->dd_pool;
1135 ASSERT(dsl_dir_phys(dd));
1137 if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {
1138 /* up the hold count until we can be written out */
1139 dmu_buf_add_ref(dd->dd_dbuf, dd);
1144 parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)
1146 uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);
1147 uint64_t new_accounted =
1148 MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);
1149 return (new_accounted - old_accounted);
1153 dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)
1155 ASSERT(dmu_tx_is_syncing(tx));
1157 mutex_enter(&dd->dd_lock);
1158 ASSERT0(dd->dd_tempreserved[tx->tx_txg & TXG_MASK]);
1159 dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg,
1160 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] / 1024);
1161 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] = 0;
1162 mutex_exit(&dd->dd_lock);
1164 /* release the hold from dsl_dir_dirty */
1165 dmu_buf_rele(dd->dd_dbuf, dd);
1169 dsl_dir_space_towrite(dsl_dir_t *dd)
1173 ASSERT(MUTEX_HELD(&dd->dd_lock));
1175 for (int i = 0; i < TXG_SIZE; i++) {
1176 space += dd->dd_space_towrite[i & TXG_MASK];
1177 ASSERT3U(dd->dd_space_towrite[i & TXG_MASK], >=, 0);
1183 * How much space would dd have available if ancestor had delta applied
1184 * to it? If ondiskonly is set, we're only interested in what's
1185 * on-disk, not estimated pending changes.
1188 dsl_dir_space_available(dsl_dir_t *dd,
1189 dsl_dir_t *ancestor, int64_t delta, int ondiskonly)
1191 uint64_t parentspace, myspace, quota, used;
1194 * If there are no restrictions otherwise, assume we have
1195 * unlimited space available.
1198 parentspace = UINT64_MAX;
1200 if (dd->dd_parent != NULL) {
1201 parentspace = dsl_dir_space_available(dd->dd_parent,
1202 ancestor, delta, ondiskonly);
1205 mutex_enter(&dd->dd_lock);
1206 if (dsl_dir_phys(dd)->dd_quota != 0)
1207 quota = dsl_dir_phys(dd)->dd_quota;
1208 used = dsl_dir_phys(dd)->dd_used_bytes;
1210 used += dsl_dir_space_towrite(dd);
1212 if (dd->dd_parent == NULL) {
1213 uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool,
1214 ZFS_SPACE_CHECK_NORMAL);
1215 quota = MIN(quota, poolsize);
1218 if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {
1220 * We have some space reserved, in addition to what our
1223 parentspace += dsl_dir_phys(dd)->dd_reserved - used;
1226 if (dd == ancestor) {
1228 ASSERT(used >= -delta);
1230 if (parentspace != UINT64_MAX)
1231 parentspace -= delta;
1239 * the lesser of the space provided by our parent and
1240 * the space left in our quota
1242 myspace = MIN(parentspace, quota - used);
1245 mutex_exit(&dd->dd_lock);
1250 struct tempreserve {
1251 list_node_t tr_node;
1257 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,
1258 boolean_t ignorequota, list_t *tr_list,
1259 dmu_tx_t *tx, boolean_t first)
1263 struct tempreserve *tr;
1272 ASSERT3U(txg, !=, 0);
1273 ASSERT3S(asize, >, 0);
1275 mutex_enter(&dd->dd_lock);
1278 * Check against the dsl_dir's quota. We don't add in the delta
1279 * when checking for over-quota because they get one free hit.
1281 uint64_t est_inflight = dsl_dir_space_towrite(dd);
1282 for (int i = 0; i < TXG_SIZE; i++)
1283 est_inflight += dd->dd_tempreserved[i];
1284 uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;
1287 * On the first iteration, fetch the dataset's used-on-disk and
1288 * refreservation values. Also, if checkrefquota is set, test if
1289 * allocating this space would exceed the dataset's refquota.
1291 if (first && tx->tx_objset) {
1293 dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;
1295 error = dsl_dataset_check_quota(ds, !netfree,
1296 asize, est_inflight, &used_on_disk, &ref_rsrv);
1298 mutex_exit(&dd->dd_lock);
1299 DMU_TX_STAT_BUMP(dmu_tx_quota);
1305 * If this transaction will result in a net free of space,
1306 * we want to let it through.
1308 if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0)
1311 quota = dsl_dir_phys(dd)->dd_quota;
1314 * Adjust the quota against the actual pool size at the root
1315 * minus any outstanding deferred frees.
1316 * To ensure that it's possible to remove files from a full
1317 * pool without inducing transient overcommits, we throttle
1318 * netfree transactions against a quota that is slightly larger,
1319 * but still within the pool's allocation slop. In cases where
1320 * we're very close to full, this will allow a steady trickle of
1321 * removes to get through.
1323 uint64_t deferred = 0;
1324 if (dd->dd_parent == NULL) {
1325 uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool,
1327 ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL);
1329 if (avail < quota) {
1331 retval = SET_ERROR(ENOSPC);
1336 * If they are requesting more space, and our current estimate
1337 * is over quota, they get to try again unless the actual
1338 * on-disk is over quota and there are no pending changes (which
1339 * may free up space for us).
1341 if (used_on_disk + est_inflight >= quota) {
1342 if (est_inflight > 0 || used_on_disk < quota ||
1343 (retval == ENOSPC && used_on_disk < quota + deferred))
1345 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "
1346 "quota=%lluK tr=%lluK err=%d\n",
1347 used_on_disk>>10, est_inflight>>10,
1348 quota>>10, asize>>10, retval);
1349 mutex_exit(&dd->dd_lock);
1350 DMU_TX_STAT_BUMP(dmu_tx_quota);
1351 return (SET_ERROR(retval));
1354 /* We need to up our estimated delta before dropping dd_lock */
1355 dd->dd_tempreserved[txg & TXG_MASK] += asize;
1357 uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,
1359 mutex_exit(&dd->dd_lock);
1361 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1363 tr->tr_size = asize;
1364 list_insert_tail(tr_list, tr);
1366 /* see if it's OK with our parent */
1367 if (dd->dd_parent != NULL && parent_rsrv != 0) {
1369 * Recurse on our parent without recursion. This has been
1370 * observed to be potentially large stack usage even within
1371 * the test suite. Largest seen stack was 7632 bytes on linux.
1375 asize = parent_rsrv;
1376 ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);
1378 goto top_of_function;
1386 * Reserve space in this dsl_dir, to be used in this tx's txg.
1387 * After the space has been dirtied (and dsl_dir_willuse_space()
1388 * has been called), the reservation should be canceled, using
1389 * dsl_dir_tempreserve_clear().
1392 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,
1393 boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)
1403 tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
1404 list_create(tr_list, sizeof (struct tempreserve),
1405 offsetof(struct tempreserve, tr_node));
1406 ASSERT3S(asize, >, 0);
1408 err = arc_tempreserve_space(dd->dd_pool->dp_spa, lsize, tx->tx_txg);
1410 struct tempreserve *tr;
1412 tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);
1413 tr->tr_size = lsize;
1414 list_insert_tail(tr_list, tr);
1416 if (err == EAGAIN) {
1418 * If arc_memory_throttle() detected that pageout
1419 * is running and we are low on memory, we delay new
1420 * non-pageout transactions to give pageout an
1423 * It is unfortunate to be delaying while the caller's
1426 txg_delay(dd->dd_pool, tx->tx_txg,
1427 MSEC2NSEC(10), MSEC2NSEC(10));
1428 err = SET_ERROR(ERESTART);
1433 err = dsl_dir_tempreserve_impl(dd, asize, netfree,
1434 B_FALSE, tr_list, tx, B_TRUE);
1438 dsl_dir_tempreserve_clear(tr_list, tx);
1440 *tr_cookiep = tr_list;
1446 * Clear a temporary reservation that we previously made with
1447 * dsl_dir_tempreserve_space().
1450 dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)
1452 int txgidx = tx->tx_txg & TXG_MASK;
1453 list_t *tr_list = tr_cookie;
1454 struct tempreserve *tr;
1456 ASSERT3U(tx->tx_txg, !=, 0);
1458 if (tr_cookie == NULL)
1461 while ((tr = list_head(tr_list)) != NULL) {
1463 mutex_enter(&tr->tr_ds->dd_lock);
1464 ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,
1466 tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;
1467 mutex_exit(&tr->tr_ds->dd_lock);
1469 arc_tempreserve_clear(tr->tr_size);
1471 list_remove(tr_list, tr);
1472 kmem_free(tr, sizeof (struct tempreserve));
1475 kmem_free(tr_list, sizeof (list_t));
1479 * This should be called from open context when we think we're going to write
1480 * or free space, for example when dirtying data. Be conservative; it's okay
1481 * to write less space or free more, but we don't want to write more or free
1482 * less than the amount specified.
1484 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1485 * version however it has been adjusted to use an iterative rather than
1486 * recursive algorithm to minimize stack usage.
1489 dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)
1491 int64_t parent_space;
1495 mutex_enter(&dd->dd_lock);
1497 dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;
1499 est_used = dsl_dir_space_towrite(dd) +
1500 dsl_dir_phys(dd)->dd_used_bytes;
1501 parent_space = parent_delta(dd, est_used, space);
1502 mutex_exit(&dd->dd_lock);
1504 /* Make sure that we clean up dd_space_to* */
1505 dsl_dir_dirty(dd, tx);
1508 space = parent_space;
1509 } while (space && dd);
1512 /* call from syncing context when we actually write/free space for this dd */
1514 dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
1515 int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)
1517 int64_t accounted_delta;
1520 * dsl_dataset_set_refreservation_sync_impl() calls this with
1521 * dd_lock held, so that it can atomically update
1522 * ds->ds_reserved and the dsl_dir accounting, so that
1523 * dsl_dataset_check_quota() can see dataset and dir accounting
1526 boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);
1528 ASSERT(dmu_tx_is_syncing(tx));
1529 ASSERT(type < DD_USED_NUM);
1531 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1534 mutex_enter(&dd->dd_lock);
1536 parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used);
1537 ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used);
1538 ASSERT(compressed >= 0 ||
1539 dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed);
1540 ASSERT(uncompressed >= 0 ||
1541 dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed);
1542 dsl_dir_phys(dd)->dd_used_bytes += used;
1543 dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed;
1544 dsl_dir_phys(dd)->dd_compressed_bytes += compressed;
1546 if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {
1548 dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used);
1549 dsl_dir_phys(dd)->dd_used_breakdown[type] += used;
1554 for (t = 0; t < DD_USED_NUM; t++)
1555 u += dsl_dir_phys(dd)->dd_used_breakdown[t];
1556 ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes);
1561 mutex_exit(&dd->dd_lock);
1563 if (dd->dd_parent != NULL) {
1564 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
1565 accounted_delta, compressed, uncompressed, tx);
1566 dsl_dir_transfer_space(dd->dd_parent,
1567 used - accounted_delta,
1568 DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);
1573 dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
1574 dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)
1576 ASSERT(dmu_tx_is_syncing(tx));
1577 ASSERT(oldtype < DD_USED_NUM);
1578 ASSERT(newtype < DD_USED_NUM);
1581 !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN))
1584 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1585 mutex_enter(&dd->dd_lock);
1587 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta :
1588 dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta);
1589 ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta));
1590 dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta;
1591 dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta;
1592 mutex_exit(&dd->dd_lock);
1595 typedef struct dsl_dir_set_qr_arg {
1596 const char *ddsqra_name;
1597 zprop_source_t ddsqra_source;
1598 uint64_t ddsqra_value;
1599 } dsl_dir_set_qr_arg_t;
1602 dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)
1604 dsl_dir_set_qr_arg_t *ddsqra = arg;
1605 dsl_pool_t *dp = dmu_tx_pool(tx);
1608 uint64_t towrite, newval;
1610 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1614 error = dsl_prop_predict(ds->ds_dir, "quota",
1615 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1617 dsl_dataset_rele(ds, FTAG);
1622 dsl_dataset_rele(ds, FTAG);
1626 mutex_enter(&ds->ds_dir->dd_lock);
1628 * If we are doing the preliminary check in open context, and
1629 * there are pending changes, then don't fail it, since the
1630 * pending changes could under-estimate the amount of space to be
1633 towrite = dsl_dir_space_towrite(ds->ds_dir);
1634 if ((dmu_tx_is_syncing(tx) || towrite == 0) &&
1635 (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||
1636 newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {
1637 error = SET_ERROR(ENOSPC);
1639 mutex_exit(&ds->ds_dir->dd_lock);
1640 dsl_dataset_rele(ds, FTAG);
1645 dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)
1647 dsl_dir_set_qr_arg_t *ddsqra = arg;
1648 dsl_pool_t *dp = dmu_tx_pool(tx);
1652 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1654 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1655 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),
1656 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1657 &ddsqra->ddsqra_value, tx);
1659 VERIFY0(dsl_prop_get_int_ds(ds,
1660 zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));
1662 newval = ddsqra->ddsqra_value;
1663 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1664 zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);
1667 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
1668 mutex_enter(&ds->ds_dir->dd_lock);
1669 dsl_dir_phys(ds->ds_dir)->dd_quota = newval;
1670 mutex_exit(&ds->ds_dir->dd_lock);
1671 dsl_dataset_rele(ds, FTAG);
1675 dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)
1677 dsl_dir_set_qr_arg_t ddsqra;
1679 ddsqra.ddsqra_name = ddname;
1680 ddsqra.ddsqra_source = source;
1681 ddsqra.ddsqra_value = quota;
1683 return (dsl_sync_task(ddname, dsl_dir_set_quota_check,
1684 dsl_dir_set_quota_sync, &ddsqra, 0,
1685 ZFS_SPACE_CHECK_EXTRA_RESERVED));
1689 dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)
1691 dsl_dir_set_qr_arg_t *ddsqra = arg;
1692 dsl_pool_t *dp = dmu_tx_pool(tx);
1695 uint64_t newval, used, avail;
1698 error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
1704 * If we are doing the preliminary check in open context, the
1705 * space estimates may be inaccurate.
1707 if (!dmu_tx_is_syncing(tx)) {
1708 dsl_dataset_rele(ds, FTAG);
1712 error = dsl_prop_predict(ds->ds_dir,
1713 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1714 ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
1716 dsl_dataset_rele(ds, FTAG);
1720 mutex_enter(&dd->dd_lock);
1721 used = dsl_dir_phys(dd)->dd_used_bytes;
1722 mutex_exit(&dd->dd_lock);
1724 if (dd->dd_parent) {
1725 avail = dsl_dir_space_available(dd->dd_parent,
1728 avail = dsl_pool_adjustedsize(dd->dd_pool,
1729 ZFS_SPACE_CHECK_NORMAL) - used;
1732 if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {
1733 uint64_t delta = MAX(used, newval) -
1734 MAX(used, dsl_dir_phys(dd)->dd_reserved);
1736 if (delta > avail ||
1737 (dsl_dir_phys(dd)->dd_quota > 0 &&
1738 newval > dsl_dir_phys(dd)->dd_quota))
1739 error = SET_ERROR(ENOSPC);
1742 dsl_dataset_rele(ds, FTAG);
1747 dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)
1752 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1754 mutex_enter(&dd->dd_lock);
1755 used = dsl_dir_phys(dd)->dd_used_bytes;
1756 delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);
1757 dsl_dir_phys(dd)->dd_reserved = value;
1759 if (dd->dd_parent != NULL) {
1760 /* Roll up this additional usage into our ancestors */
1761 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
1764 mutex_exit(&dd->dd_lock);
1768 dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)
1770 dsl_dir_set_qr_arg_t *ddsqra = arg;
1771 dsl_pool_t *dp = dmu_tx_pool(tx);
1775 VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
1777 if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {
1778 dsl_prop_set_sync_impl(ds,
1779 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1780 ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
1781 &ddsqra->ddsqra_value, tx);
1783 VERIFY0(dsl_prop_get_int_ds(ds,
1784 zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));
1786 newval = ddsqra->ddsqra_value;
1787 spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",
1788 zfs_prop_to_name(ZFS_PROP_RESERVATION),
1789 (longlong_t)newval);
1792 dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);
1793 dsl_dataset_rele(ds, FTAG);
1797 dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
1798 uint64_t reservation)
1800 dsl_dir_set_qr_arg_t ddsqra;
1802 ddsqra.ddsqra_name = ddname;
1803 ddsqra.ddsqra_source = source;
1804 ddsqra.ddsqra_value = reservation;
1806 return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,
1807 dsl_dir_set_reservation_sync, &ddsqra, 0,
1808 ZFS_SPACE_CHECK_EXTRA_RESERVED));
1812 closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)
1814 for (; ds1; ds1 = ds1->dd_parent) {
1816 for (dd = ds2; dd; dd = dd->dd_parent) {
1825 * If delta is applied to dd, how much of that delta would be applied to
1826 * ancestor? Syncing context only.
1829 would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)
1834 mutex_enter(&dd->dd_lock);
1835 delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);
1836 mutex_exit(&dd->dd_lock);
1837 return (would_change(dd->dd_parent, delta, ancestor));
1840 typedef struct dsl_dir_rename_arg {
1841 const char *ddra_oldname;
1842 const char *ddra_newname;
1845 } dsl_dir_rename_arg_t;
1847 typedef struct dsl_valid_rename_arg {
1850 } dsl_valid_rename_arg_t;
1854 dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1856 dsl_valid_rename_arg_t *dvra = arg;
1857 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1859 dsl_dataset_name(ds, namebuf);
1861 ASSERT3U(strnlen(namebuf, ZFS_MAX_DATASET_NAME_LEN),
1862 <, ZFS_MAX_DATASET_NAME_LEN);
1863 int namelen = strlen(namebuf) + dvra->char_delta;
1864 int depth = get_dataset_depth(namebuf) + dvra->nest_delta;
1866 if (namelen >= ZFS_MAX_DATASET_NAME_LEN)
1867 return (SET_ERROR(ENAMETOOLONG));
1868 if (dvra->nest_delta > 0 && depth >= zfs_max_dataset_nesting)
1869 return (SET_ERROR(ENAMETOOLONG));
1874 dsl_dir_rename_check(void *arg, dmu_tx_t *tx)
1876 dsl_dir_rename_arg_t *ddra = arg;
1877 dsl_pool_t *dp = dmu_tx_pool(tx);
1878 dsl_dir_t *dd, *newparent;
1879 dsl_valid_rename_arg_t dvra;
1880 dsl_dataset_t *parentds;
1882 const char *mynewname;
1885 /* target dir should exist */
1886 error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);
1890 /* new parent should exist */
1891 error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,
1892 &newparent, &mynewname);
1894 dsl_dir_rele(dd, FTAG);
1898 /* can't rename to different pool */
1899 if (dd->dd_pool != newparent->dd_pool) {
1900 dsl_dir_rele(newparent, FTAG);
1901 dsl_dir_rele(dd, FTAG);
1902 return (SET_ERROR(EXDEV));
1905 /* new name should not already exist */
1906 if (mynewname == NULL) {
1907 dsl_dir_rele(newparent, FTAG);
1908 dsl_dir_rele(dd, FTAG);
1909 return (SET_ERROR(EEXIST));
1912 /* can't rename below anything but filesystems (eg. no ZVOLs) */
1913 error = dsl_dataset_hold_obj(newparent->dd_pool,
1914 dsl_dir_phys(newparent)->dd_head_dataset_obj, FTAG, &parentds);
1916 dsl_dir_rele(newparent, FTAG);
1917 dsl_dir_rele(dd, FTAG);
1920 error = dmu_objset_from_ds(parentds, &parentos);
1922 dsl_dataset_rele(parentds, FTAG);
1923 dsl_dir_rele(newparent, FTAG);
1924 dsl_dir_rele(dd, FTAG);
1927 if (dmu_objset_type(parentos) != DMU_OST_ZFS) {
1928 dsl_dataset_rele(parentds, FTAG);
1929 dsl_dir_rele(newparent, FTAG);
1930 dsl_dir_rele(dd, FTAG);
1931 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
1933 dsl_dataset_rele(parentds, FTAG);
1935 ASSERT3U(strnlen(ddra->ddra_newname, ZFS_MAX_DATASET_NAME_LEN),
1936 <, ZFS_MAX_DATASET_NAME_LEN);
1937 ASSERT3U(strnlen(ddra->ddra_oldname, ZFS_MAX_DATASET_NAME_LEN),
1938 <, ZFS_MAX_DATASET_NAME_LEN);
1939 dvra.char_delta = strlen(ddra->ddra_newname)
1940 - strlen(ddra->ddra_oldname);
1941 dvra.nest_delta = get_dataset_depth(ddra->ddra_newname)
1942 - get_dataset_depth(ddra->ddra_oldname);
1944 /* if the name length is growing, validate child name lengths */
1945 if (dvra.char_delta > 0 || dvra.nest_delta > 0) {
1946 error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,
1947 &dvra, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1949 dsl_dir_rele(newparent, FTAG);
1950 dsl_dir_rele(dd, FTAG);
1955 if (dmu_tx_is_syncing(tx)) {
1956 if (spa_feature_is_active(dp->dp_spa,
1957 SPA_FEATURE_FS_SS_LIMIT)) {
1959 * Although this is the check function and we don't
1960 * normally make on-disk changes in check functions,
1961 * we need to do that here.
1963 * Ensure this portion of the tree's counts have been
1964 * initialized in case the new parent has limits set.
1966 dsl_dir_init_fs_ss_count(dd, tx);
1970 if (newparent != dd->dd_parent) {
1971 /* is there enough space? */
1973 MAX(dsl_dir_phys(dd)->dd_used_bytes,
1974 dsl_dir_phys(dd)->dd_reserved);
1975 objset_t *os = dd->dd_pool->dp_meta_objset;
1976 uint64_t fs_cnt = 0;
1977 uint64_t ss_cnt = 0;
1979 if (dsl_dir_is_zapified(dd)) {
1982 err = zap_lookup(os, dd->dd_object,
1983 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
1985 if (err != ENOENT && err != 0) {
1986 dsl_dir_rele(newparent, FTAG);
1987 dsl_dir_rele(dd, FTAG);
1992 * have to add 1 for the filesystem itself that we're
1997 err = zap_lookup(os, dd->dd_object,
1998 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2000 if (err != ENOENT && err != 0) {
2001 dsl_dir_rele(newparent, FTAG);
2002 dsl_dir_rele(dd, FTAG);
2007 /* check for encryption errors */
2008 error = dsl_dir_rename_crypt_check(dd, newparent);
2010 dsl_dir_rele(newparent, FTAG);
2011 dsl_dir_rele(dd, FTAG);
2012 return (SET_ERROR(EACCES));
2015 /* no rename into our descendant */
2016 if (closest_common_ancestor(dd, newparent) == dd) {
2017 dsl_dir_rele(newparent, FTAG);
2018 dsl_dir_rele(dd, FTAG);
2019 return (SET_ERROR(EINVAL));
2022 error = dsl_dir_transfer_possible(dd->dd_parent,
2023 newparent, fs_cnt, ss_cnt, myspace,
2024 ddra->ddra_cred, ddra->ddra_proc);
2026 dsl_dir_rele(newparent, FTAG);
2027 dsl_dir_rele(dd, FTAG);
2032 dsl_dir_rele(newparent, FTAG);
2033 dsl_dir_rele(dd, FTAG);
2038 dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)
2040 dsl_dir_rename_arg_t *ddra = arg;
2041 dsl_pool_t *dp = dmu_tx_pool(tx);
2042 dsl_dir_t *dd, *newparent;
2043 const char *mynewname;
2044 objset_t *mos = dp->dp_meta_objset;
2046 VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));
2047 VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,
2050 /* Log this before we change the name. */
2051 spa_history_log_internal_dd(dd, "rename", tx,
2052 "-> %s", ddra->ddra_newname);
2054 if (newparent != dd->dd_parent) {
2055 objset_t *os = dd->dd_pool->dp_meta_objset;
2056 uint64_t fs_cnt = 0;
2057 uint64_t ss_cnt = 0;
2060 * We already made sure the dd counts were initialized in the
2063 if (spa_feature_is_active(dp->dp_spa,
2064 SPA_FEATURE_FS_SS_LIMIT)) {
2065 VERIFY0(zap_lookup(os, dd->dd_object,
2066 DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,
2068 /* add 1 for the filesystem itself that we're moving */
2071 VERIFY0(zap_lookup(os, dd->dd_object,
2072 DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,
2076 dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,
2077 DD_FIELD_FILESYSTEM_COUNT, tx);
2078 dsl_fs_ss_count_adjust(newparent, fs_cnt,
2079 DD_FIELD_FILESYSTEM_COUNT, tx);
2081 dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,
2082 DD_FIELD_SNAPSHOT_COUNT, tx);
2083 dsl_fs_ss_count_adjust(newparent, ss_cnt,
2084 DD_FIELD_SNAPSHOT_COUNT, tx);
2086 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,
2087 -dsl_dir_phys(dd)->dd_used_bytes,
2088 -dsl_dir_phys(dd)->dd_compressed_bytes,
2089 -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2090 dsl_dir_diduse_space(newparent, DD_USED_CHILD,
2091 dsl_dir_phys(dd)->dd_used_bytes,
2092 dsl_dir_phys(dd)->dd_compressed_bytes,
2093 dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);
2095 if (dsl_dir_phys(dd)->dd_reserved >
2096 dsl_dir_phys(dd)->dd_used_bytes) {
2097 uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -
2098 dsl_dir_phys(dd)->dd_used_bytes;
2100 dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,
2101 -unused_rsrv, 0, 0, tx);
2102 dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,
2103 unused_rsrv, 0, 0, tx);
2107 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2109 /* remove from old parent zapobj */
2110 VERIFY0(zap_remove(mos,
2111 dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,
2112 dd->dd_myname, tx));
2114 (void) strlcpy(dd->dd_myname, mynewname,
2115 sizeof (dd->dd_myname));
2116 dsl_dir_rele(dd->dd_parent, dd);
2117 dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;
2118 VERIFY0(dsl_dir_hold_obj(dp,
2119 newparent->dd_object, NULL, dd, &dd->dd_parent));
2121 /* add to new parent zapobj */
2122 VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,
2123 dd->dd_myname, 8, 1, &dd->dd_object, tx));
2125 /* TODO: A rename callback to avoid these layering violations. */
2126 zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);
2127 zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname,
2128 ddra->ddra_newname, B_TRUE);
2130 dsl_prop_notify_all(dd);
2132 dsl_dir_rele(newparent, FTAG);
2133 dsl_dir_rele(dd, FTAG);
2137 dsl_dir_rename(const char *oldname, const char *newname)
2139 dsl_dir_rename_arg_t ddra;
2141 ddra.ddra_oldname = oldname;
2142 ddra.ddra_newname = newname;
2143 ddra.ddra_cred = CRED();
2144 ddra.ddra_proc = curproc;
2146 return (dsl_sync_task(oldname,
2147 dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,
2148 3, ZFS_SPACE_CHECK_RESERVED));
2152 dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
2153 uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space,
2154 cred_t *cr, proc_t *proc)
2156 dsl_dir_t *ancestor;
2161 ancestor = closest_common_ancestor(sdd, tdd);
2162 adelta = would_change(sdd, -space, ancestor);
2163 avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);
2165 return (SET_ERROR(ENOSPC));
2167 err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,
2168 ancestor, cr, proc);
2171 err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,
2172 ancestor, cr, proc);
2180 dsl_dir_snap_cmtime(dsl_dir_t *dd)
2184 mutex_enter(&dd->dd_lock);
2185 t = dd->dd_snap_cmtime;
2186 mutex_exit(&dd->dd_lock);
2192 dsl_dir_snap_cmtime_update(dsl_dir_t *dd)
2197 mutex_enter(&dd->dd_lock);
2198 dd->dd_snap_cmtime = t;
2199 mutex_exit(&dd->dd_lock);
2203 dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)
2205 objset_t *mos = dd->dd_pool->dp_meta_objset;
2206 dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);
2210 dsl_dir_is_zapified(dsl_dir_t *dd)
2212 dmu_object_info_t doi;
2214 dmu_object_info_from_db(dd->dd_dbuf, &doi);
2215 return (doi.doi_type == DMU_OTN_ZAP_METADATA);
2219 dsl_dir_livelist_open(dsl_dir_t *dd, uint64_t obj)
2221 objset_t *mos = dd->dd_pool->dp_meta_objset;
2222 ASSERT(spa_feature_is_active(dd->dd_pool->dp_spa,
2223 SPA_FEATURE_LIVELIST));
2224 dsl_deadlist_open(&dd->dd_livelist, mos, obj);
2225 bplist_create(&dd->dd_pending_allocs);
2226 bplist_create(&dd->dd_pending_frees);
2230 dsl_dir_livelist_close(dsl_dir_t *dd)
2232 dsl_deadlist_close(&dd->dd_livelist);
2233 bplist_destroy(&dd->dd_pending_allocs);
2234 bplist_destroy(&dd->dd_pending_frees);
2238 dsl_dir_remove_livelist(dsl_dir_t *dd, dmu_tx_t *tx, boolean_t total)
2241 dsl_pool_t *dp = dmu_tx_pool(tx);
2242 spa_t *spa = dp->dp_spa;
2243 livelist_condense_entry_t to_condense = spa->spa_to_condense;
2245 if (!dsl_deadlist_is_open(&dd->dd_livelist))
2249 * If the livelist being removed is set to be condensed, stop the
2250 * condense zthr and indicate the cancellation in the spa_to_condense
2251 * struct in case the condense no-wait synctask has already started
2253 zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;
2254 if (ll_condense_thread != NULL &&
2255 (to_condense.ds != NULL) && (to_condense.ds->ds_dir == dd)) {
2257 * We use zthr_wait_cycle_done instead of zthr_cancel
2258 * because we don't want to destroy the zthr, just have
2259 * it skip its current task.
2261 spa->spa_to_condense.cancelled = B_TRUE;
2262 zthr_wait_cycle_done(ll_condense_thread);
2264 * If we've returned from zthr_wait_cycle_done without
2265 * clearing the to_condense data structure it's either
2266 * because the no-wait synctask has started (which is
2267 * indicated by 'syncing' field of to_condense) and we
2268 * can expect it to clear to_condense on its own.
2269 * Otherwise, we returned before the zthr ran. The
2270 * checkfunc will now fail as cancelled == B_TRUE so we
2271 * can safely NULL out ds, allowing a different dir's
2272 * livelist to be condensed.
2274 * We can be sure that the to_condense struct will not
2275 * be repopulated at this stage because both this
2276 * function and dsl_livelist_try_condense execute in
2279 if ((spa->spa_to_condense.ds != NULL) &&
2280 !spa->spa_to_condense.syncing) {
2281 dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf,
2283 spa->spa_to_condense.ds = NULL;
2287 dsl_dir_livelist_close(dd);
2288 VERIFY0(zap_lookup(dp->dp_meta_objset, dd->dd_object,
2289 DD_FIELD_LIVELIST, sizeof (uint64_t), 1, &obj));
2290 VERIFY0(zap_remove(dp->dp_meta_objset, dd->dd_object,
2291 DD_FIELD_LIVELIST, tx));
2293 dsl_deadlist_free(dp->dp_meta_objset, obj, tx);
2294 spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx);
2299 dsl_dir_activity_in_progress(dsl_dir_t *dd, dsl_dataset_t *ds,
2300 zfs_wait_activity_t activity, boolean_t *in_progress)
2304 ASSERT(MUTEX_HELD(&dd->dd_activity_lock));
2307 case ZFS_WAIT_DELETEQ: {
2310 error = dmu_objset_from_ds(ds, &os);
2314 mutex_enter(&os->os_user_ptr_lock);
2315 void *user = dmu_objset_get_user(os);
2316 mutex_exit(&os->os_user_ptr_lock);
2317 if (dmu_objset_type(os) != DMU_OST_ZFS ||
2318 user == NULL || zfs_get_vfs_flag_unmounted(os)) {
2319 *in_progress = B_FALSE;
2323 uint64_t readonly = B_FALSE;
2324 error = zfs_get_temporary_prop(ds, ZFS_PROP_READONLY, &readonly,
2330 if (readonly || !spa_writeable(dd->dd_pool->dp_spa)) {
2331 *in_progress = B_FALSE;
2335 uint64_t count, unlinked_obj;
2336 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
2339 dsl_dataset_rele(ds, FTAG);
2342 error = zap_count(os, unlinked_obj, &count);
2345 *in_progress = (count != 0);
2349 * The delete queue is ZPL specific, and libzpool doesn't have
2350 * it. It doesn't make sense to wait for it.
2352 *in_progress = B_FALSE;
2357 panic("unrecognized value for activity %d", activity);
2364 dsl_dir_wait(dsl_dir_t *dd, dsl_dataset_t *ds, zfs_wait_activity_t activity,
2368 boolean_t in_progress;
2369 dsl_pool_t *dp = dd->dd_pool;
2371 dsl_pool_config_enter(dp, FTAG);
2372 error = dsl_dir_activity_in_progress(dd, ds, activity,
2374 dsl_pool_config_exit(dp, FTAG);
2375 if (error != 0 || !in_progress)
2380 if (cv_wait_sig(&dd->dd_activity_cv, &dd->dd_activity_lock) ==
2381 0 || dd->dd_activity_cancelled) {
2382 error = SET_ERROR(EINTR);
2390 dsl_dir_cancel_waiters(dsl_dir_t *dd)
2392 mutex_enter(&dd->dd_activity_lock);
2393 dd->dd_activity_cancelled = B_TRUE;
2394 cv_broadcast(&dd->dd_activity_cv);
2395 while (dd->dd_activity_waiters > 0)
2396 cv_wait(&dd->dd_activity_cv, &dd->dd_activity_lock);
2397 mutex_exit(&dd->dd_activity_lock);
2400 #if defined(_KERNEL)
2401 EXPORT_SYMBOL(dsl_dir_set_quota);
2402 EXPORT_SYMBOL(dsl_dir_set_reservation);