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) 2013 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.
30 #include <sys/dmu_objset.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_prop.h>
35 #include <sys/dsl_synctask.h>
36 #include <sys/dsl_deleg.h>
37 #include <sys/dmu_impl.h>
39 #include <sys/metaslab.h>
43 #include <sys/sunddi.h>
44 #include <sys/zfeature.h>
45 #include <sys/policy.h>
46 #include <sys/zfs_znode.h>
48 #include "zfs_namecheck.h"
52 * Filesystem and Snapshot Limits
53 * ------------------------------
55 * These limits are used to restrict the number of filesystems and/or snapshots
56 * that can be created at a given level in the tree or below. A typical
57 * use-case is with a delegated dataset where the administrator wants to ensure
58 * that a user within the zone is not creating too many additional filesystems
59 * or snapshots, even though they're not exceeding their space quota.
61 * The filesystem and snapshot counts are stored as extensible properties. This
62 * capability is controlled by a feature flag and must be enabled to be used.
63 * Once enabled, the feature is not active until the first limit is set. At
64 * that point, future operations to create/destroy filesystems or snapshots
65 * will validate and update the counts.
67 * Because the count properties will not exist before the feature is active,
68 * the counts are updated when a limit is first set on an uninitialized
69 * dsl_dir node in the tree (The filesystem/snapshot count on a node includes
70 * all of the nested filesystems/snapshots. Thus, a new leaf node has a
71 * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and
72 * snapshot count properties on a node indicate uninitialized counts on that
73 * node.) When first setting a limit on an uninitialized node, the code starts
74 * at the filesystem with the new limit and descends into all sub-filesystems
75 * to add the count properties.
77 * In practice this is lightweight since a limit is typically set when the
78 * filesystem is created and thus has no children. Once valid, changing the
79 * limit value won't require a re-traversal since the counts are already valid.
80 * When recursively fixing the counts, if a node with a limit is encountered
81 * during the descent, the counts are known to be valid and there is no need to
82 * descend into that filesystem's children. The counts on filesystems above the
83 * one with the new limit will still be uninitialized, unless a limit is
84 * eventually set on one of those filesystems. The counts are always recursively
85 * updated when a limit is set on a dataset, unless there is already a limit.
86 * When a new limit value is set on a filesystem with an existing limit, it is
87 * possible for the new limit to be less than the current count at that level
88 * since a user who can change the limit is also allowed to exceed the limit.
90 * Once the feature is active, then whenever a filesystem or snapshot is
91 * created, the code recurses up the tree, validating the new count against the
92 * limit at each initialized level. In practice, most levels will not have a
93 * limit set. If there is a limit at any initialized level up the tree, the
94 * check must pass or the creation will fail. Likewise, when a filesystem or
95 * snapshot is destroyed, the counts are recursively adjusted all the way up
96 * the initizized nodes in the tree. Renaming a filesystem into different point
97 * in the tree will first validate, then update the counts on each branch up to
98 * the common ancestor. A receive will also validate the counts and then update
101 * An exception to the above behavior is that the limit is not enforced if the
102 * user has permission to modify the limit. This is primarily so that
103 * recursive snapshots in the global zone always work. We want to prevent a
104 * denial-of-service in which a lower level delegated dataset could max out its
105 * limit and thus block recursive snapshots from being taken in the global zone.
106 * Because of this, it is possible for the snapshot count to be over the limit
107 * and snapshots taken in the global zone could cause a lower level dataset to
108 * hit or exceed its limit. The administrator taking the global zone recursive
109 * snapshot should be aware of this side-effect and behave accordingly.
110 * For consistency, the filesystem limit is also not enforced if the user can
113 * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()
114 * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in
115 * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by
116 * dsl_dir_init_fs_ss_count().
118 * There is a special case when we receive a filesystem that already exists. In
119 * this case a temporary clone name of %X is created (see dmu_recv_begin). We
120 * never update the filesystem counts for temporary clones.
122 * Likewise, we do not update the snapshot counts for temporary snapshots,
123 * such as those created by zfs diff.
126 extern inline dsl_dir_phys_t
*dsl_dir_phys(dsl_dir_t
*dd
);
128 static uint64_t dsl_dir_space_towrite(dsl_dir_t
*dd
);
131 dsl_dir_evict(void *dbu
)
135 ASSERTV(dsl_pool_t
*dp
= dd
->dd_pool
);
139 for (t
= 0; t
< TXG_SIZE
; t
++) {
140 ASSERT(!txg_list_member(&dp
->dp_dirty_dirs
, dd
, t
));
141 ASSERT(dd
->dd_tempreserved
[t
] == 0);
142 ASSERT(dd
->dd_space_towrite
[t
] == 0);
146 dsl_dir_async_rele(dd
->dd_parent
, dd
);
148 spa_async_close(dd
->dd_pool
->dp_spa
, dd
);
151 * The props callback list should have been cleaned up by
154 list_destroy(&dd
->dd_prop_cbs
);
155 mutex_destroy(&dd
->dd_lock
);
156 kmem_free(dd
, sizeof (dsl_dir_t
));
160 dsl_dir_hold_obj(dsl_pool_t
*dp
, uint64_t ddobj
,
161 const char *tail
, void *tag
, dsl_dir_t
**ddp
)
167 ASSERT(dsl_pool_config_held(dp
));
169 err
= dmu_bonus_hold(dp
->dp_meta_objset
, ddobj
, tag
, &dbuf
);
172 dd
= dmu_buf_get_user(dbuf
);
175 dmu_object_info_t doi
;
176 dmu_object_info_from_db(dbuf
, &doi
);
177 ASSERT3U(doi
.doi_bonus_type
, ==, DMU_OT_DSL_DIR
);
178 ASSERT3U(doi
.doi_bonus_size
, >=, sizeof (dsl_dir_phys_t
));
184 dd
= kmem_zalloc(sizeof (dsl_dir_t
), KM_SLEEP
);
185 dd
->dd_object
= ddobj
;
188 mutex_init(&dd
->dd_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
190 list_create(&dd
->dd_prop_cbs
, sizeof (dsl_prop_cb_record_t
),
191 offsetof(dsl_prop_cb_record_t
, cbr_node
));
193 dsl_dir_snap_cmtime_update(dd
);
195 if (dsl_dir_phys(dd
)->dd_parent_obj
) {
196 err
= dsl_dir_hold_obj(dp
,
197 dsl_dir_phys(dd
)->dd_parent_obj
, NULL
, dd
,
205 err
= zap_lookup(dp
->dp_meta_objset
,
206 dsl_dir_phys(dd
->dd_parent
)->
207 dd_child_dir_zapobj
, tail
,
208 sizeof (foundobj
), 1, &foundobj
);
209 ASSERT(err
|| foundobj
== ddobj
);
211 (void) strcpy(dd
->dd_myname
, tail
);
213 err
= zap_value_search(dp
->dp_meta_objset
,
214 dsl_dir_phys(dd
->dd_parent
)->
216 ddobj
, 0, dd
->dd_myname
);
221 (void) strcpy(dd
->dd_myname
, spa_name(dp
->dp_spa
));
224 if (dsl_dir_is_clone(dd
)) {
225 dmu_buf_t
*origin_bonus
;
226 dsl_dataset_phys_t
*origin_phys
;
229 * We can't open the origin dataset, because
230 * that would require opening this dsl_dir.
231 * Just look at its phys directly instead.
233 err
= dmu_bonus_hold(dp
->dp_meta_objset
,
234 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
,
238 origin_phys
= origin_bonus
->db_data
;
240 origin_phys
->ds_creation_txg
;
241 dmu_buf_rele(origin_bonus
, FTAG
);
244 dmu_buf_init_user(&dd
->dd_dbu
, dsl_dir_evict
, &dd
->dd_dbuf
);
245 winner
= dmu_buf_set_user_ie(dbuf
, &dd
->dd_dbu
);
246 if (winner
!= NULL
) {
248 dsl_dir_rele(dd
->dd_parent
, dd
);
249 mutex_destroy(&dd
->dd_lock
);
250 kmem_free(dd
, sizeof (dsl_dir_t
));
253 spa_open_ref(dp
->dp_spa
, dd
);
258 * The dsl_dir_t has both open-to-close and instantiate-to-evict
259 * holds on the spa. We need the open-to-close holds because
260 * otherwise the spa_refcnt wouldn't change when we open a
261 * dir which the spa also has open, so we could incorrectly
262 * think it was OK to unload/export/destroy the pool. We need
263 * the instantiate-to-evict hold because the dsl_dir_t has a
264 * pointer to the dd_pool, which has a pointer to the spa_t.
266 spa_open_ref(dp
->dp_spa
, tag
);
267 ASSERT3P(dd
->dd_pool
, ==, dp
);
268 ASSERT3U(dd
->dd_object
, ==, ddobj
);
269 ASSERT3P(dd
->dd_dbuf
, ==, dbuf
);
275 dsl_dir_rele(dd
->dd_parent
, dd
);
276 mutex_destroy(&dd
->dd_lock
);
277 kmem_free(dd
, sizeof (dsl_dir_t
));
278 dmu_buf_rele(dbuf
, tag
);
283 dsl_dir_rele(dsl_dir_t
*dd
, void *tag
)
285 dprintf_dd(dd
, "%s\n", "");
286 spa_close(dd
->dd_pool
->dp_spa
, tag
);
287 dmu_buf_rele(dd
->dd_dbuf
, tag
);
291 * Remove a reference to the given dsl dir that is being asynchronously
292 * released. Async releases occur from a taskq performing eviction of
293 * dsl datasets and dirs. This process is identical to a normal release
294 * with the exception of using the async API for releasing the reference on
298 dsl_dir_async_rele(dsl_dir_t
*dd
, void *tag
)
300 dprintf_dd(dd
, "%s\n", "");
301 spa_async_close(dd
->dd_pool
->dp_spa
, tag
);
302 dmu_buf_rele(dd
->dd_dbuf
, tag
);
305 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */
307 dsl_dir_name(dsl_dir_t
*dd
, char *buf
)
310 dsl_dir_name(dd
->dd_parent
, buf
);
311 (void) strcat(buf
, "/");
315 if (!MUTEX_HELD(&dd
->dd_lock
)) {
317 * recursive mutex so that we can use
318 * dprintf_dd() with dd_lock held
320 mutex_enter(&dd
->dd_lock
);
321 (void) strcat(buf
, dd
->dd_myname
);
322 mutex_exit(&dd
->dd_lock
);
324 (void) strcat(buf
, dd
->dd_myname
);
328 /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */
330 dsl_dir_namelen(dsl_dir_t
*dd
)
335 /* parent's name + 1 for the "/" */
336 result
= dsl_dir_namelen(dd
->dd_parent
) + 1;
339 if (!MUTEX_HELD(&dd
->dd_lock
)) {
340 /* see dsl_dir_name */
341 mutex_enter(&dd
->dd_lock
);
342 result
+= strlen(dd
->dd_myname
);
343 mutex_exit(&dd
->dd_lock
);
345 result
+= strlen(dd
->dd_myname
);
352 getcomponent(const char *path
, char *component
, const char **nextp
)
356 if ((path
== NULL
) || (path
[0] == '\0'))
357 return (SET_ERROR(ENOENT
));
358 /* This would be a good place to reserve some namespace... */
359 p
= strpbrk(path
, "/@");
360 if (p
&& (p
[1] == '/' || p
[1] == '@')) {
361 /* two separators in a row */
362 return (SET_ERROR(EINVAL
));
364 if (p
== NULL
|| p
== path
) {
366 * if the first thing is an @ or /, it had better be an
367 * @ and it had better not have any more ats or slashes,
368 * and it had better have something after the @.
371 (p
[0] != '@' || strpbrk(path
+1, "/@") || p
[1] == '\0'))
372 return (SET_ERROR(EINVAL
));
373 if (strlen(path
) >= MAXNAMELEN
)
374 return (SET_ERROR(ENAMETOOLONG
));
375 (void) strcpy(component
, path
);
377 } else if (p
[0] == '/') {
378 if (p
- path
>= MAXNAMELEN
)
379 return (SET_ERROR(ENAMETOOLONG
));
380 (void) strncpy(component
, path
, p
- path
);
381 component
[p
- path
] = '\0';
383 } else if (p
[0] == '@') {
385 * if the next separator is an @, there better not be
388 if (strchr(path
, '/'))
389 return (SET_ERROR(EINVAL
));
390 if (p
- path
>= MAXNAMELEN
)
391 return (SET_ERROR(ENAMETOOLONG
));
392 (void) strncpy(component
, path
, p
- path
);
393 component
[p
- path
] = '\0';
395 panic("invalid p=%p", (void *)p
);
402 * Return the dsl_dir_t, and possibly the last component which couldn't
403 * be found in *tail. The name must be in the specified dsl_pool_t. This
404 * thread must hold the dp_config_rwlock for the pool. Returns NULL if the
405 * path is bogus, or if tail==NULL and we couldn't parse the whole name.
406 * (*tail)[0] == '@' means that the last component is a snapshot.
409 dsl_dir_hold(dsl_pool_t
*dp
, const char *name
, void *tag
,
410 dsl_dir_t
**ddp
, const char **tailp
)
413 const char *spaname
, *next
, *nextnext
= NULL
;
418 buf
= kmem_alloc(MAXNAMELEN
, KM_SLEEP
);
419 err
= getcomponent(name
, buf
, &next
);
423 /* Make sure the name is in the specified pool. */
424 spaname
= spa_name(dp
->dp_spa
);
425 if (strcmp(buf
, spaname
) != 0) {
426 err
= SET_ERROR(EXDEV
);
430 ASSERT(dsl_pool_config_held(dp
));
432 err
= dsl_dir_hold_obj(dp
, dp
->dp_root_dir_obj
, NULL
, tag
, &dd
);
437 while (next
!= NULL
) {
439 err
= getcomponent(next
, buf
, &nextnext
);
442 ASSERT(next
[0] != '\0');
445 dprintf("looking up %s in obj%lld\n",
446 buf
, dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
448 err
= zap_lookup(dp
->dp_meta_objset
,
449 dsl_dir_phys(dd
)->dd_child_dir_zapobj
,
450 buf
, sizeof (ddobj
), 1, &ddobj
);
457 err
= dsl_dir_hold_obj(dp
, ddobj
, buf
, tag
, &child_dd
);
460 dsl_dir_rele(dd
, tag
);
466 dsl_dir_rele(dd
, tag
);
471 * It's an error if there's more than one component left, or
472 * tailp==NULL and there's any component left.
475 (tailp
== NULL
|| (nextnext
&& nextnext
[0] != '\0'))) {
477 dsl_dir_rele(dd
, tag
);
478 dprintf("next=%p (%s) tail=%p\n", next
, next
?next
:"", tailp
);
479 err
= SET_ERROR(ENOENT
);
485 kmem_free(buf
, MAXNAMELEN
);
490 * If the counts are already initialized for this filesystem and its
491 * descendants then do nothing, otherwise initialize the counts.
493 * The counts on this filesystem, and those below, may be uninitialized due to
494 * either the use of a pre-existing pool which did not support the
495 * filesystem/snapshot limit feature, or one in which the feature had not yet
498 * Recursively descend the filesystem tree and update the filesystem/snapshot
499 * counts on each filesystem below, then update the cumulative count on the
500 * current filesystem. If the filesystem already has a count set on it,
501 * then we know that its counts, and the counts on the filesystems below it,
502 * are already correct, so we don't have to update this filesystem.
505 dsl_dir_init_fs_ss_count(dsl_dir_t
*dd
, dmu_tx_t
*tx
)
507 uint64_t my_fs_cnt
= 0;
508 uint64_t my_ss_cnt
= 0;
509 dsl_pool_t
*dp
= dd
->dd_pool
;
510 objset_t
*os
= dp
->dp_meta_objset
;
515 ASSERT(spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_FS_SS_LIMIT
));
516 ASSERT(dsl_pool_config_held(dp
));
517 ASSERT(dmu_tx_is_syncing(tx
));
519 dsl_dir_zapify(dd
, tx
);
522 * If the filesystem count has already been initialized then we
523 * don't need to recurse down any further.
525 if (zap_contains(os
, dd
->dd_object
, DD_FIELD_FILESYSTEM_COUNT
) == 0)
528 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
529 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
531 /* Iterate my child dirs */
532 for (zap_cursor_init(zc
, os
, dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
533 zap_cursor_retrieve(zc
, za
) == 0; zap_cursor_advance(zc
)) {
537 VERIFY0(dsl_dir_hold_obj(dp
, za
->za_first_integer
, NULL
, FTAG
,
541 * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and
542 * temporary datasets.
544 if (chld_dd
->dd_myname
[0] == '$' ||
545 chld_dd
->dd_myname
[0] == '%') {
546 dsl_dir_rele(chld_dd
, FTAG
);
550 my_fs_cnt
++; /* count this child */
552 dsl_dir_init_fs_ss_count(chld_dd
, tx
);
554 VERIFY0(zap_lookup(os
, chld_dd
->dd_object
,
555 DD_FIELD_FILESYSTEM_COUNT
, sizeof (count
), 1, &count
));
557 VERIFY0(zap_lookup(os
, chld_dd
->dd_object
,
558 DD_FIELD_SNAPSHOT_COUNT
, sizeof (count
), 1, &count
));
561 dsl_dir_rele(chld_dd
, FTAG
);
564 /* Count my snapshots (we counted children's snapshots above) */
565 VERIFY0(dsl_dataset_hold_obj(dd
->dd_pool
,
566 dsl_dir_phys(dd
)->dd_head_dataset_obj
, FTAG
, &ds
));
568 for (zap_cursor_init(zc
, os
, dsl_dataset_phys(ds
)->ds_snapnames_zapobj
);
569 zap_cursor_retrieve(zc
, za
) == 0;
570 zap_cursor_advance(zc
)) {
571 /* Don't count temporary snapshots */
572 if (za
->za_name
[0] != '%')
577 dsl_dataset_rele(ds
, FTAG
);
579 kmem_free(zc
, sizeof (zap_cursor_t
));
580 kmem_free(za
, sizeof (zap_attribute_t
));
582 /* we're in a sync task, update counts */
583 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
584 VERIFY0(zap_add(os
, dd
->dd_object
, DD_FIELD_FILESYSTEM_COUNT
,
585 sizeof (my_fs_cnt
), 1, &my_fs_cnt
, tx
));
586 VERIFY0(zap_add(os
, dd
->dd_object
, DD_FIELD_SNAPSHOT_COUNT
,
587 sizeof (my_ss_cnt
), 1, &my_ss_cnt
, tx
));
591 dsl_dir_actv_fs_ss_limit_check(void *arg
, dmu_tx_t
*tx
)
593 char *ddname
= (char *)arg
;
594 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
599 error
= dsl_dataset_hold(dp
, ddname
, FTAG
, &ds
);
603 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_FS_SS_LIMIT
)) {
604 dsl_dataset_rele(ds
, FTAG
);
605 return (SET_ERROR(ENOTSUP
));
609 if (spa_feature_is_active(dp
->dp_spa
, SPA_FEATURE_FS_SS_LIMIT
) &&
610 dsl_dir_is_zapified(dd
) &&
611 zap_contains(dp
->dp_meta_objset
, dd
->dd_object
,
612 DD_FIELD_FILESYSTEM_COUNT
) == 0) {
613 dsl_dataset_rele(ds
, FTAG
);
614 return (SET_ERROR(EALREADY
));
617 dsl_dataset_rele(ds
, FTAG
);
622 dsl_dir_actv_fs_ss_limit_sync(void *arg
, dmu_tx_t
*tx
)
624 char *ddname
= (char *)arg
;
625 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
629 VERIFY0(dsl_dataset_hold(dp
, ddname
, FTAG
, &ds
));
631 spa
= dsl_dataset_get_spa(ds
);
633 if (!spa_feature_is_active(spa
, SPA_FEATURE_FS_SS_LIMIT
)) {
635 * Since the feature was not active and we're now setting a
636 * limit, increment the feature-active counter so that the
637 * feature becomes active for the first time.
639 * We are already in a sync task so we can update the MOS.
641 spa_feature_incr(spa
, SPA_FEATURE_FS_SS_LIMIT
, tx
);
645 * Since we are now setting a non-UINT64_MAX limit on the filesystem,
646 * we need to ensure the counts are correct. Descend down the tree from
647 * this point and update all of the counts to be accurate.
649 dsl_dir_init_fs_ss_count(ds
->ds_dir
, tx
);
651 dsl_dataset_rele(ds
, FTAG
);
655 * Make sure the feature is enabled and activate it if necessary.
656 * Since we're setting a limit, ensure the on-disk counts are valid.
657 * This is only called by the ioctl path when setting a limit value.
659 * We do not need to validate the new limit, since users who can change the
660 * limit are also allowed to exceed the limit.
663 dsl_dir_activate_fs_ss_limit(const char *ddname
)
667 error
= dsl_sync_task(ddname
, dsl_dir_actv_fs_ss_limit_check
,
668 dsl_dir_actv_fs_ss_limit_sync
, (void *)ddname
, 0);
670 if (error
== EALREADY
)
677 * Used to determine if the filesystem_limit or snapshot_limit should be
678 * enforced. We allow the limit to be exceeded if the user has permission to
679 * write the property value. We pass in the creds that we got in the open
680 * context since we will always be the GZ root in syncing context. We also have
681 * to handle the case where we are allowed to change the limit on the current
682 * dataset, but there may be another limit in the tree above.
684 * We can never modify these two properties within a non-global zone. In
685 * addition, the other checks are modeled on zfs_secpolicy_write_perms. We
686 * can't use that function since we are already holding the dp_config_rwlock.
687 * In addition, we already have the dd and dealing with snapshots is simplified
698 dsl_enforce_ds_ss_limits(dsl_dir_t
*dd
, zfs_prop_t prop
, cred_t
*cr
)
700 enforce_res_t enforce
= ENFORCE_ALWAYS
;
705 ASSERT(prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
706 prop
== ZFS_PROP_SNAPSHOT_LIMIT
);
709 if (crgetzoneid(cr
) != GLOBAL_ZONEID
)
710 return (ENFORCE_ALWAYS
);
712 if (secpolicy_zfs(cr
) == 0)
713 return (ENFORCE_NEVER
);
716 if ((obj
= dsl_dir_phys(dd
)->dd_head_dataset_obj
) == 0)
717 return (ENFORCE_ALWAYS
);
719 ASSERT(dsl_pool_config_held(dd
->dd_pool
));
721 if (dsl_dataset_hold_obj(dd
->dd_pool
, obj
, FTAG
, &ds
) != 0)
722 return (ENFORCE_ALWAYS
);
724 if (dsl_prop_get_ds(ds
, "zoned", 8, 1, &zoned
, NULL
) || zoned
) {
725 /* Only root can access zoned fs's from the GZ */
726 enforce
= ENFORCE_ALWAYS
;
728 if (dsl_deleg_access_impl(ds
, zfs_prop_to_name(prop
), cr
) == 0)
729 enforce
= ENFORCE_ABOVE
;
732 dsl_dataset_rele(ds
, FTAG
);
737 * Check if adding additional child filesystem(s) would exceed any filesystem
738 * limits or adding additional snapshot(s) would exceed any snapshot limits.
739 * The prop argument indicates which limit to check.
741 * Note that all filesystem limits up to the root (or the highest
742 * initialized) filesystem or the given ancestor must be satisfied.
745 dsl_fs_ss_limit_check(dsl_dir_t
*dd
, uint64_t delta
, zfs_prop_t prop
,
746 dsl_dir_t
*ancestor
, cred_t
*cr
)
748 objset_t
*os
= dd
->dd_pool
->dp_meta_objset
;
749 uint64_t limit
, count
;
751 enforce_res_t enforce
;
754 ASSERT(dsl_pool_config_held(dd
->dd_pool
));
755 ASSERT(prop
== ZFS_PROP_FILESYSTEM_LIMIT
||
756 prop
== ZFS_PROP_SNAPSHOT_LIMIT
);
759 * If we're allowed to change the limit, don't enforce the limit
760 * e.g. this can happen if a snapshot is taken by an administrative
761 * user in the global zone (i.e. a recursive snapshot by root).
762 * However, we must handle the case of delegated permissions where we
763 * are allowed to change the limit on the current dataset, but there
764 * is another limit in the tree above.
766 enforce
= dsl_enforce_ds_ss_limits(dd
, prop
, cr
);
767 if (enforce
== ENFORCE_NEVER
)
771 * e.g. if renaming a dataset with no snapshots, count adjustment
777 if (prop
== ZFS_PROP_SNAPSHOT_LIMIT
) {
779 * We don't enforce the limit for temporary snapshots. This is
780 * indicated by a NULL cred_t argument.
785 count_prop
= DD_FIELD_SNAPSHOT_COUNT
;
787 count_prop
= DD_FIELD_FILESYSTEM_COUNT
;
791 * If an ancestor has been provided, stop checking the limit once we
792 * hit that dir. We need this during rename so that we don't overcount
793 * the check once we recurse up to the common ancestor.
799 * If we hit an uninitialized node while recursing up the tree, we can
800 * stop since we know there is no limit here (or above). The counts are
801 * not valid on this node and we know we won't touch this node's counts.
803 if (!dsl_dir_is_zapified(dd
) || zap_lookup(os
, dd
->dd_object
,
804 count_prop
, sizeof (count
), 1, &count
) == ENOENT
)
807 err
= dsl_prop_get_dd(dd
, zfs_prop_to_name(prop
), 8, 1, &limit
, NULL
,
812 /* Is there a limit which we've hit? */
813 if (enforce
== ENFORCE_ALWAYS
&& (count
+ delta
) > limit
)
814 return (SET_ERROR(EDQUOT
));
816 if (dd
->dd_parent
!= NULL
)
817 err
= dsl_fs_ss_limit_check(dd
->dd_parent
, delta
, prop
,
824 * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all
825 * parents. When a new filesystem/snapshot is created, increment the count on
826 * all parents, and when a filesystem/snapshot is destroyed, decrement the
830 dsl_fs_ss_count_adjust(dsl_dir_t
*dd
, int64_t delta
, const char *prop
,
834 objset_t
*os
= dd
->dd_pool
->dp_meta_objset
;
837 ASSERT(dsl_pool_config_held(dd
->dd_pool
));
838 ASSERT(dmu_tx_is_syncing(tx
));
839 ASSERT(strcmp(prop
, DD_FIELD_FILESYSTEM_COUNT
) == 0 ||
840 strcmp(prop
, DD_FIELD_SNAPSHOT_COUNT
) == 0);
843 * When we receive an incremental stream into a filesystem that already
844 * exists, a temporary clone is created. We don't count this temporary
845 * clone, whose name begins with a '%'. We also ignore hidden ($FREE,
846 * $MOS & $ORIGIN) objsets.
848 if ((dd
->dd_myname
[0] == '%' || dd
->dd_myname
[0] == '$') &&
849 strcmp(prop
, DD_FIELD_FILESYSTEM_COUNT
) == 0)
853 * e.g. if renaming a dataset with no snapshots, count adjustment is 0
859 * If we hit an uninitialized node while recursing up the tree, we can
860 * stop since we know the counts are not valid on this node and we
861 * know we shouldn't touch this node's counts. An uninitialized count
862 * on the node indicates that either the feature has not yet been
863 * activated or there are no limits on this part of the tree.
865 if (!dsl_dir_is_zapified(dd
) || (err
= zap_lookup(os
, dd
->dd_object
,
866 prop
, sizeof (count
), 1, &count
)) == ENOENT
)
871 /* Use a signed verify to make sure we're not neg. */
872 VERIFY3S(count
, >=, 0);
874 VERIFY0(zap_update(os
, dd
->dd_object
, prop
, sizeof (count
), 1, &count
,
877 /* Roll up this additional count into our ancestors */
878 if (dd
->dd_parent
!= NULL
)
879 dsl_fs_ss_count_adjust(dd
->dd_parent
, delta
, prop
, tx
);
883 dsl_dir_create_sync(dsl_pool_t
*dp
, dsl_dir_t
*pds
, const char *name
,
886 objset_t
*mos
= dp
->dp_meta_objset
;
888 dsl_dir_phys_t
*ddphys
;
891 ddobj
= dmu_object_alloc(mos
, DMU_OT_DSL_DIR
, 0,
892 DMU_OT_DSL_DIR
, sizeof (dsl_dir_phys_t
), tx
);
894 VERIFY(0 == zap_add(mos
, dsl_dir_phys(pds
)->dd_child_dir_zapobj
,
895 name
, sizeof (uint64_t), 1, &ddobj
, tx
));
897 /* it's the root dir */
898 VERIFY(0 == zap_add(mos
, DMU_POOL_DIRECTORY_OBJECT
,
899 DMU_POOL_ROOT_DATASET
, sizeof (uint64_t), 1, &ddobj
, tx
));
901 VERIFY(0 == dmu_bonus_hold(mos
, ddobj
, FTAG
, &dbuf
));
902 dmu_buf_will_dirty(dbuf
, tx
);
903 ddphys
= dbuf
->db_data
;
905 ddphys
->dd_creation_time
= gethrestime_sec();
907 ddphys
->dd_parent_obj
= pds
->dd_object
;
909 /* update the filesystem counts */
910 dsl_fs_ss_count_adjust(pds
, 1, DD_FIELD_FILESYSTEM_COUNT
, tx
);
912 ddphys
->dd_props_zapobj
= zap_create(mos
,
913 DMU_OT_DSL_PROPS
, DMU_OT_NONE
, 0, tx
);
914 ddphys
->dd_child_dir_zapobj
= zap_create(mos
,
915 DMU_OT_DSL_DIR_CHILD_MAP
, DMU_OT_NONE
, 0, tx
);
916 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_USED_BREAKDOWN
)
917 ddphys
->dd_flags
|= DD_FLAG_USED_BREAKDOWN
;
918 dmu_buf_rele(dbuf
, FTAG
);
924 dsl_dir_is_clone(dsl_dir_t
*dd
)
926 return (dsl_dir_phys(dd
)->dd_origin_obj
&&
927 (dd
->dd_pool
->dp_origin_snap
== NULL
||
928 dsl_dir_phys(dd
)->dd_origin_obj
!=
929 dd
->dd_pool
->dp_origin_snap
->ds_object
));
933 dsl_dir_stats(dsl_dir_t
*dd
, nvlist_t
*nv
)
935 mutex_enter(&dd
->dd_lock
);
936 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USED
,
937 dsl_dir_phys(dd
)->dd_used_bytes
);
938 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_QUOTA
,
939 dsl_dir_phys(dd
)->dd_quota
);
940 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_RESERVATION
,
941 dsl_dir_phys(dd
)->dd_reserved
);
942 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_COMPRESSRATIO
,
943 dsl_dir_phys(dd
)->dd_compressed_bytes
== 0 ? 100 :
944 (dsl_dir_phys(dd
)->dd_uncompressed_bytes
* 100 /
945 dsl_dir_phys(dd
)->dd_compressed_bytes
));
946 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_LOGICALUSED
,
947 dsl_dir_phys(dd
)->dd_uncompressed_bytes
);
948 if (dsl_dir_phys(dd
)->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
949 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDSNAP
,
950 dsl_dir_phys(dd
)->dd_used_breakdown
[DD_USED_SNAP
]);
951 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDDS
,
952 dsl_dir_phys(dd
)->dd_used_breakdown
[DD_USED_HEAD
]);
953 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDREFRESERV
,
954 dsl_dir_phys(dd
)->dd_used_breakdown
[DD_USED_REFRSRV
]);
955 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_USEDCHILD
,
956 dsl_dir_phys(dd
)->dd_used_breakdown
[DD_USED_CHILD
] +
957 dsl_dir_phys(dd
)->dd_used_breakdown
[DD_USED_CHILD_RSRV
]);
959 mutex_exit(&dd
->dd_lock
);
961 if (dsl_dir_is_zapified(dd
)) {
963 objset_t
*os
= dd
->dd_pool
->dp_meta_objset
;
965 if (zap_lookup(os
, dd
->dd_object
, DD_FIELD_FILESYSTEM_COUNT
,
966 sizeof (count
), 1, &count
) == 0) {
967 dsl_prop_nvlist_add_uint64(nv
,
968 ZFS_PROP_FILESYSTEM_COUNT
, count
);
970 if (zap_lookup(os
, dd
->dd_object
, DD_FIELD_SNAPSHOT_COUNT
,
971 sizeof (count
), 1, &count
) == 0) {
972 dsl_prop_nvlist_add_uint64(nv
,
973 ZFS_PROP_SNAPSHOT_COUNT
, count
);
977 if (dsl_dir_is_clone(dd
)) {
979 char buf
[MAXNAMELEN
];
981 VERIFY0(dsl_dataset_hold_obj(dd
->dd_pool
,
982 dsl_dir_phys(dd
)->dd_origin_obj
, FTAG
, &ds
));
983 dsl_dataset_name(ds
, buf
);
984 dsl_dataset_rele(ds
, FTAG
);
985 dsl_prop_nvlist_add_string(nv
, ZFS_PROP_ORIGIN
, buf
);
990 dsl_dir_dirty(dsl_dir_t
*dd
, dmu_tx_t
*tx
)
992 dsl_pool_t
*dp
= dd
->dd_pool
;
994 ASSERT(dsl_dir_phys(dd
));
996 if (txg_list_add(&dp
->dp_dirty_dirs
, dd
, tx
->tx_txg
)) {
997 /* up the hold count until we can be written out */
998 dmu_buf_add_ref(dd
->dd_dbuf
, dd
);
1003 parent_delta(dsl_dir_t
*dd
, uint64_t used
, int64_t delta
)
1005 uint64_t old_accounted
= MAX(used
, dsl_dir_phys(dd
)->dd_reserved
);
1006 uint64_t new_accounted
=
1007 MAX(used
+ delta
, dsl_dir_phys(dd
)->dd_reserved
);
1008 return (new_accounted
- old_accounted
);
1012 dsl_dir_sync(dsl_dir_t
*dd
, dmu_tx_t
*tx
)
1014 ASSERT(dmu_tx_is_syncing(tx
));
1016 mutex_enter(&dd
->dd_lock
);
1017 ASSERT0(dd
->dd_tempreserved
[tx
->tx_txg
&TXG_MASK
]);
1018 dprintf_dd(dd
, "txg=%llu towrite=%lluK\n", tx
->tx_txg
,
1019 dd
->dd_space_towrite
[tx
->tx_txg
&TXG_MASK
] / 1024);
1020 dd
->dd_space_towrite
[tx
->tx_txg
&TXG_MASK
] = 0;
1021 mutex_exit(&dd
->dd_lock
);
1023 /* release the hold from dsl_dir_dirty */
1024 dmu_buf_rele(dd
->dd_dbuf
, dd
);
1028 dsl_dir_space_towrite(dsl_dir_t
*dd
)
1033 ASSERT(MUTEX_HELD(&dd
->dd_lock
));
1035 for (i
= 0; i
< TXG_SIZE
; i
++) {
1036 space
+= dd
->dd_space_towrite
[i
&TXG_MASK
];
1037 ASSERT3U(dd
->dd_space_towrite
[i
&TXG_MASK
], >=, 0);
1043 * How much space would dd have available if ancestor had delta applied
1044 * to it? If ondiskonly is set, we're only interested in what's
1045 * on-disk, not estimated pending changes.
1048 dsl_dir_space_available(dsl_dir_t
*dd
,
1049 dsl_dir_t
*ancestor
, int64_t delta
, int ondiskonly
)
1051 uint64_t parentspace
, myspace
, quota
, used
;
1054 * If there are no restrictions otherwise, assume we have
1055 * unlimited space available.
1058 parentspace
= UINT64_MAX
;
1060 if (dd
->dd_parent
!= NULL
) {
1061 parentspace
= dsl_dir_space_available(dd
->dd_parent
,
1062 ancestor
, delta
, ondiskonly
);
1065 mutex_enter(&dd
->dd_lock
);
1066 if (dsl_dir_phys(dd
)->dd_quota
!= 0)
1067 quota
= dsl_dir_phys(dd
)->dd_quota
;
1068 used
= dsl_dir_phys(dd
)->dd_used_bytes
;
1070 used
+= dsl_dir_space_towrite(dd
);
1072 if (dd
->dd_parent
== NULL
) {
1073 uint64_t poolsize
= dsl_pool_adjustedsize(dd
->dd_pool
, FALSE
);
1074 quota
= MIN(quota
, poolsize
);
1077 if (dsl_dir_phys(dd
)->dd_reserved
> used
&& parentspace
!= UINT64_MAX
) {
1079 * We have some space reserved, in addition to what our
1082 parentspace
+= dsl_dir_phys(dd
)->dd_reserved
- used
;
1085 if (dd
== ancestor
) {
1087 ASSERT(used
>= -delta
);
1089 if (parentspace
!= UINT64_MAX
)
1090 parentspace
-= delta
;
1098 * the lesser of the space provided by our parent and
1099 * the space left in our quota
1101 myspace
= MIN(parentspace
, quota
- used
);
1104 mutex_exit(&dd
->dd_lock
);
1109 struct tempreserve
{
1110 list_node_t tr_node
;
1116 dsl_dir_tempreserve_impl(dsl_dir_t
*dd
, uint64_t asize
, boolean_t netfree
,
1117 boolean_t ignorequota
, boolean_t checkrefquota
, list_t
*tr_list
,
1118 dmu_tx_t
*tx
, boolean_t first
)
1120 uint64_t txg
= tx
->tx_txg
;
1121 uint64_t est_inflight
, used_on_disk
, quota
, parent_rsrv
;
1122 uint64_t deferred
= 0;
1123 struct tempreserve
*tr
;
1124 int retval
= EDQUOT
;
1125 int txgidx
= txg
& TXG_MASK
;
1127 uint64_t ref_rsrv
= 0;
1129 ASSERT3U(txg
, !=, 0);
1130 ASSERT3S(asize
, >, 0);
1132 mutex_enter(&dd
->dd_lock
);
1135 * Check against the dsl_dir's quota. We don't add in the delta
1136 * when checking for over-quota because they get one free hit.
1138 est_inflight
= dsl_dir_space_towrite(dd
);
1139 for (i
= 0; i
< TXG_SIZE
; i
++)
1140 est_inflight
+= dd
->dd_tempreserved
[i
];
1141 used_on_disk
= dsl_dir_phys(dd
)->dd_used_bytes
;
1144 * On the first iteration, fetch the dataset's used-on-disk and
1145 * refreservation values. Also, if checkrefquota is set, test if
1146 * allocating this space would exceed the dataset's refquota.
1148 if (first
&& tx
->tx_objset
) {
1150 dsl_dataset_t
*ds
= tx
->tx_objset
->os_dsl_dataset
;
1152 error
= dsl_dataset_check_quota(ds
, checkrefquota
,
1153 asize
, est_inflight
, &used_on_disk
, &ref_rsrv
);
1155 mutex_exit(&dd
->dd_lock
);
1156 DMU_TX_STAT_BUMP(dmu_tx_quota
);
1162 * If this transaction will result in a net free of space,
1163 * we want to let it through.
1165 if (ignorequota
|| netfree
|| dsl_dir_phys(dd
)->dd_quota
== 0)
1168 quota
= dsl_dir_phys(dd
)->dd_quota
;
1171 * Adjust the quota against the actual pool size at the root
1172 * minus any outstanding deferred frees.
1173 * To ensure that it's possible to remove files from a full
1174 * pool without inducing transient overcommits, we throttle
1175 * netfree transactions against a quota that is slightly larger,
1176 * but still within the pool's allocation slop. In cases where
1177 * we're very close to full, this will allow a steady trickle of
1178 * removes to get through.
1180 if (dd
->dd_parent
== NULL
) {
1181 spa_t
*spa
= dd
->dd_pool
->dp_spa
;
1182 uint64_t poolsize
= dsl_pool_adjustedsize(dd
->dd_pool
, netfree
);
1183 deferred
= metaslab_class_get_deferred(spa_normal_class(spa
));
1184 if (poolsize
- deferred
< quota
) {
1185 quota
= poolsize
- deferred
;
1191 * If they are requesting more space, and our current estimate
1192 * is over quota, they get to try again unless the actual
1193 * on-disk is over quota and there are no pending changes (which
1194 * may free up space for us).
1196 if (used_on_disk
+ est_inflight
>= quota
) {
1197 if (est_inflight
> 0 || used_on_disk
< quota
||
1198 (retval
== ENOSPC
&& used_on_disk
< quota
+ deferred
))
1200 dprintf_dd(dd
, "failing: used=%lluK inflight = %lluK "
1201 "quota=%lluK tr=%lluK err=%d\n",
1202 used_on_disk
>>10, est_inflight
>>10,
1203 quota
>>10, asize
>>10, retval
);
1204 mutex_exit(&dd
->dd_lock
);
1205 DMU_TX_STAT_BUMP(dmu_tx_quota
);
1206 return (SET_ERROR(retval
));
1209 /* We need to up our estimated delta before dropping dd_lock */
1210 dd
->dd_tempreserved
[txgidx
] += asize
;
1212 parent_rsrv
= parent_delta(dd
, used_on_disk
+ est_inflight
,
1214 mutex_exit(&dd
->dd_lock
);
1216 tr
= kmem_zalloc(sizeof (struct tempreserve
), KM_SLEEP
);
1218 tr
->tr_size
= asize
;
1219 list_insert_tail(tr_list
, tr
);
1221 /* see if it's OK with our parent */
1222 if (dd
->dd_parent
&& parent_rsrv
) {
1223 boolean_t ismos
= (dsl_dir_phys(dd
)->dd_head_dataset_obj
== 0);
1225 return (dsl_dir_tempreserve_impl(dd
->dd_parent
,
1226 parent_rsrv
, netfree
, ismos
, TRUE
, tr_list
, tx
, FALSE
));
1233 * Reserve space in this dsl_dir, to be used in this tx's txg.
1234 * After the space has been dirtied (and dsl_dir_willuse_space()
1235 * has been called), the reservation should be canceled, using
1236 * dsl_dir_tempreserve_clear().
1239 dsl_dir_tempreserve_space(dsl_dir_t
*dd
, uint64_t lsize
, uint64_t asize
,
1240 uint64_t fsize
, uint64_t usize
, void **tr_cookiep
, dmu_tx_t
*tx
)
1250 tr_list
= kmem_alloc(sizeof (list_t
), KM_SLEEP
);
1251 list_create(tr_list
, sizeof (struct tempreserve
),
1252 offsetof(struct tempreserve
, tr_node
));
1253 ASSERT3S(asize
, >, 0);
1254 ASSERT3S(fsize
, >=, 0);
1256 err
= arc_tempreserve_space(lsize
, tx
->tx_txg
);
1258 struct tempreserve
*tr
;
1260 tr
= kmem_zalloc(sizeof (struct tempreserve
), KM_SLEEP
);
1261 tr
->tr_size
= lsize
;
1262 list_insert_tail(tr_list
, tr
);
1264 if (err
== EAGAIN
) {
1266 * If arc_memory_throttle() detected that pageout
1267 * is running and we are low on memory, we delay new
1268 * non-pageout transactions to give pageout an
1271 * It is unfortunate to be delaying while the caller's
1274 txg_delay(dd
->dd_pool
, tx
->tx_txg
,
1275 MSEC2NSEC(10), MSEC2NSEC(10));
1276 err
= SET_ERROR(ERESTART
);
1281 err
= dsl_dir_tempreserve_impl(dd
, asize
, fsize
>= asize
,
1282 FALSE
, asize
> usize
, tr_list
, tx
, TRUE
);
1286 dsl_dir_tempreserve_clear(tr_list
, tx
);
1288 *tr_cookiep
= tr_list
;
1294 * Clear a temporary reservation that we previously made with
1295 * dsl_dir_tempreserve_space().
1298 dsl_dir_tempreserve_clear(void *tr_cookie
, dmu_tx_t
*tx
)
1300 int txgidx
= tx
->tx_txg
& TXG_MASK
;
1301 list_t
*tr_list
= tr_cookie
;
1302 struct tempreserve
*tr
;
1304 ASSERT3U(tx
->tx_txg
, !=, 0);
1306 if (tr_cookie
== NULL
)
1309 while ((tr
= list_head(tr_list
)) != NULL
) {
1311 mutex_enter(&tr
->tr_ds
->dd_lock
);
1312 ASSERT3U(tr
->tr_ds
->dd_tempreserved
[txgidx
], >=,
1314 tr
->tr_ds
->dd_tempreserved
[txgidx
] -= tr
->tr_size
;
1315 mutex_exit(&tr
->tr_ds
->dd_lock
);
1317 arc_tempreserve_clear(tr
->tr_size
);
1319 list_remove(tr_list
, tr
);
1320 kmem_free(tr
, sizeof (struct tempreserve
));
1323 kmem_free(tr_list
, sizeof (list_t
));
1327 * This should be called from open context when we think we're going to write
1328 * or free space, for example when dirtying data. Be conservative; it's okay
1329 * to write less space or free more, but we don't want to write more or free
1330 * less than the amount specified.
1332 * NOTE: The behavior of this function is identical to the Illumos / FreeBSD
1333 * version however it has been adjusted to use an iterative rather then
1334 * recursive algorithm to minimize stack usage.
1337 dsl_dir_willuse_space(dsl_dir_t
*dd
, int64_t space
, dmu_tx_t
*tx
)
1339 int64_t parent_space
;
1343 mutex_enter(&dd
->dd_lock
);
1345 dd
->dd_space_towrite
[tx
->tx_txg
& TXG_MASK
] += space
;
1347 est_used
= dsl_dir_space_towrite(dd
) +
1348 dsl_dir_phys(dd
)->dd_used_bytes
;
1349 parent_space
= parent_delta(dd
, est_used
, space
);
1350 mutex_exit(&dd
->dd_lock
);
1352 /* Make sure that we clean up dd_space_to* */
1353 dsl_dir_dirty(dd
, tx
);
1356 space
= parent_space
;
1357 } while (space
&& dd
);
1360 /* call from syncing context when we actually write/free space for this dd */
1362 dsl_dir_diduse_space(dsl_dir_t
*dd
, dd_used_t type
,
1363 int64_t used
, int64_t compressed
, int64_t uncompressed
, dmu_tx_t
*tx
)
1365 int64_t accounted_delta
;
1368 * dsl_dataset_set_refreservation_sync_impl() calls this with
1369 * dd_lock held, so that it can atomically update
1370 * ds->ds_reserved and the dsl_dir accounting, so that
1371 * dsl_dataset_check_quota() can see dataset and dir accounting
1374 boolean_t needlock
= !MUTEX_HELD(&dd
->dd_lock
);
1376 ASSERT(dmu_tx_is_syncing(tx
));
1377 ASSERT(type
< DD_USED_NUM
);
1379 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1382 mutex_enter(&dd
->dd_lock
);
1384 parent_delta(dd
, dsl_dir_phys(dd
)->dd_used_bytes
, used
);
1385 ASSERT(used
>= 0 || dsl_dir_phys(dd
)->dd_used_bytes
>= -used
);
1386 ASSERT(compressed
>= 0 ||
1387 dsl_dir_phys(dd
)->dd_compressed_bytes
>= -compressed
);
1388 ASSERT(uncompressed
>= 0 ||
1389 dsl_dir_phys(dd
)->dd_uncompressed_bytes
>= -uncompressed
);
1390 dsl_dir_phys(dd
)->dd_used_bytes
+= used
;
1391 dsl_dir_phys(dd
)->dd_uncompressed_bytes
+= uncompressed
;
1392 dsl_dir_phys(dd
)->dd_compressed_bytes
+= compressed
;
1394 if (dsl_dir_phys(dd
)->dd_flags
& DD_FLAG_USED_BREAKDOWN
) {
1396 dsl_dir_phys(dd
)->dd_used_breakdown
[type
] >= -used
);
1397 dsl_dir_phys(dd
)->dd_used_breakdown
[type
] += used
;
1402 for (t
= 0; t
< DD_USED_NUM
; t
++)
1403 u
+= dsl_dir_phys(dd
)->dd_used_breakdown
[t
];
1404 ASSERT3U(u
, ==, dsl_dir_phys(dd
)->dd_used_bytes
);
1409 mutex_exit(&dd
->dd_lock
);
1411 if (dd
->dd_parent
!= NULL
) {
1412 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD
,
1413 accounted_delta
, compressed
, uncompressed
, tx
);
1414 dsl_dir_transfer_space(dd
->dd_parent
,
1415 used
- accounted_delta
,
1416 DD_USED_CHILD_RSRV
, DD_USED_CHILD
, tx
);
1421 dsl_dir_transfer_space(dsl_dir_t
*dd
, int64_t delta
,
1422 dd_used_t oldtype
, dd_used_t newtype
, dmu_tx_t
*tx
)
1424 ASSERT(dmu_tx_is_syncing(tx
));
1425 ASSERT(oldtype
< DD_USED_NUM
);
1426 ASSERT(newtype
< DD_USED_NUM
);
1429 !(dsl_dir_phys(dd
)->dd_flags
& DD_FLAG_USED_BREAKDOWN
))
1432 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1433 mutex_enter(&dd
->dd_lock
);
1435 dsl_dir_phys(dd
)->dd_used_breakdown
[oldtype
] >= delta
:
1436 dsl_dir_phys(dd
)->dd_used_breakdown
[newtype
] >= -delta
);
1437 ASSERT(dsl_dir_phys(dd
)->dd_used_bytes
>= ABS(delta
));
1438 dsl_dir_phys(dd
)->dd_used_breakdown
[oldtype
] -= delta
;
1439 dsl_dir_phys(dd
)->dd_used_breakdown
[newtype
] += delta
;
1440 mutex_exit(&dd
->dd_lock
);
1443 typedef struct dsl_dir_set_qr_arg
{
1444 const char *ddsqra_name
;
1445 zprop_source_t ddsqra_source
;
1446 uint64_t ddsqra_value
;
1447 } dsl_dir_set_qr_arg_t
;
1450 dsl_dir_set_quota_check(void *arg
, dmu_tx_t
*tx
)
1452 dsl_dir_set_qr_arg_t
*ddsqra
= arg
;
1453 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1456 uint64_t towrite
, newval
;
1458 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
1462 error
= dsl_prop_predict(ds
->ds_dir
, "quota",
1463 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
1465 dsl_dataset_rele(ds
, FTAG
);
1470 dsl_dataset_rele(ds
, FTAG
);
1474 mutex_enter(&ds
->ds_dir
->dd_lock
);
1476 * If we are doing the preliminary check in open context, and
1477 * there are pending changes, then don't fail it, since the
1478 * pending changes could under-estimate the amount of space to be
1481 towrite
= dsl_dir_space_towrite(ds
->ds_dir
);
1482 if ((dmu_tx_is_syncing(tx
) || towrite
== 0) &&
1483 (newval
< dsl_dir_phys(ds
->ds_dir
)->dd_reserved
||
1484 newval
< dsl_dir_phys(ds
->ds_dir
)->dd_used_bytes
+ towrite
)) {
1485 error
= SET_ERROR(ENOSPC
);
1487 mutex_exit(&ds
->ds_dir
->dd_lock
);
1488 dsl_dataset_rele(ds
, FTAG
);
1493 dsl_dir_set_quota_sync(void *arg
, dmu_tx_t
*tx
)
1495 dsl_dir_set_qr_arg_t
*ddsqra
= arg
;
1496 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1500 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
1502 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_RECVD_PROPS
) {
1503 dsl_prop_set_sync_impl(ds
, zfs_prop_to_name(ZFS_PROP_QUOTA
),
1504 ddsqra
->ddsqra_source
, sizeof (ddsqra
->ddsqra_value
), 1,
1505 &ddsqra
->ddsqra_value
, tx
);
1507 VERIFY0(dsl_prop_get_int_ds(ds
,
1508 zfs_prop_to_name(ZFS_PROP_QUOTA
), &newval
));
1510 newval
= ddsqra
->ddsqra_value
;
1511 spa_history_log_internal_ds(ds
, "set", tx
, "%s=%lld",
1512 zfs_prop_to_name(ZFS_PROP_QUOTA
), (longlong_t
)newval
);
1515 dmu_buf_will_dirty(ds
->ds_dir
->dd_dbuf
, tx
);
1516 mutex_enter(&ds
->ds_dir
->dd_lock
);
1517 dsl_dir_phys(ds
->ds_dir
)->dd_quota
= newval
;
1518 mutex_exit(&ds
->ds_dir
->dd_lock
);
1519 dsl_dataset_rele(ds
, FTAG
);
1523 dsl_dir_set_quota(const char *ddname
, zprop_source_t source
, uint64_t quota
)
1525 dsl_dir_set_qr_arg_t ddsqra
;
1527 ddsqra
.ddsqra_name
= ddname
;
1528 ddsqra
.ddsqra_source
= source
;
1529 ddsqra
.ddsqra_value
= quota
;
1531 return (dsl_sync_task(ddname
, dsl_dir_set_quota_check
,
1532 dsl_dir_set_quota_sync
, &ddsqra
, 0));
1536 dsl_dir_set_reservation_check(void *arg
, dmu_tx_t
*tx
)
1538 dsl_dir_set_qr_arg_t
*ddsqra
= arg
;
1539 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1542 uint64_t newval
, used
, avail
;
1545 error
= dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
);
1551 * If we are doing the preliminary check in open context, the
1552 * space estimates may be inaccurate.
1554 if (!dmu_tx_is_syncing(tx
)) {
1555 dsl_dataset_rele(ds
, FTAG
);
1559 error
= dsl_prop_predict(ds
->ds_dir
,
1560 zfs_prop_to_name(ZFS_PROP_RESERVATION
),
1561 ddsqra
->ddsqra_source
, ddsqra
->ddsqra_value
, &newval
);
1563 dsl_dataset_rele(ds
, FTAG
);
1567 mutex_enter(&dd
->dd_lock
);
1568 used
= dsl_dir_phys(dd
)->dd_used_bytes
;
1569 mutex_exit(&dd
->dd_lock
);
1571 if (dd
->dd_parent
) {
1572 avail
= dsl_dir_space_available(dd
->dd_parent
,
1575 avail
= dsl_pool_adjustedsize(dd
->dd_pool
, B_FALSE
) - used
;
1578 if (MAX(used
, newval
) > MAX(used
, dsl_dir_phys(dd
)->dd_reserved
)) {
1579 uint64_t delta
= MAX(used
, newval
) -
1580 MAX(used
, dsl_dir_phys(dd
)->dd_reserved
);
1582 if (delta
> avail
||
1583 (dsl_dir_phys(dd
)->dd_quota
> 0 &&
1584 newval
> dsl_dir_phys(dd
)->dd_quota
))
1585 error
= SET_ERROR(ENOSPC
);
1588 dsl_dataset_rele(ds
, FTAG
);
1593 dsl_dir_set_reservation_sync_impl(dsl_dir_t
*dd
, uint64_t value
, dmu_tx_t
*tx
)
1598 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1600 mutex_enter(&dd
->dd_lock
);
1601 used
= dsl_dir_phys(dd
)->dd_used_bytes
;
1602 delta
= MAX(used
, value
) - MAX(used
, dsl_dir_phys(dd
)->dd_reserved
);
1603 dsl_dir_phys(dd
)->dd_reserved
= value
;
1605 if (dd
->dd_parent
!= NULL
) {
1606 /* Roll up this additional usage into our ancestors */
1607 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD_RSRV
,
1610 mutex_exit(&dd
->dd_lock
);
1614 dsl_dir_set_reservation_sync(void *arg
, dmu_tx_t
*tx
)
1616 dsl_dir_set_qr_arg_t
*ddsqra
= arg
;
1617 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1621 VERIFY0(dsl_dataset_hold(dp
, ddsqra
->ddsqra_name
, FTAG
, &ds
));
1623 if (spa_version(dp
->dp_spa
) >= SPA_VERSION_RECVD_PROPS
) {
1624 dsl_prop_set_sync_impl(ds
,
1625 zfs_prop_to_name(ZFS_PROP_RESERVATION
),
1626 ddsqra
->ddsqra_source
, sizeof (ddsqra
->ddsqra_value
), 1,
1627 &ddsqra
->ddsqra_value
, tx
);
1629 VERIFY0(dsl_prop_get_int_ds(ds
,
1630 zfs_prop_to_name(ZFS_PROP_RESERVATION
), &newval
));
1632 newval
= ddsqra
->ddsqra_value
;
1633 spa_history_log_internal_ds(ds
, "set", tx
, "%s=%lld",
1634 zfs_prop_to_name(ZFS_PROP_RESERVATION
),
1635 (longlong_t
)newval
);
1638 dsl_dir_set_reservation_sync_impl(ds
->ds_dir
, newval
, tx
);
1639 dsl_dataset_rele(ds
, FTAG
);
1643 dsl_dir_set_reservation(const char *ddname
, zprop_source_t source
,
1644 uint64_t reservation
)
1646 dsl_dir_set_qr_arg_t ddsqra
;
1648 ddsqra
.ddsqra_name
= ddname
;
1649 ddsqra
.ddsqra_source
= source
;
1650 ddsqra
.ddsqra_value
= reservation
;
1652 return (dsl_sync_task(ddname
, dsl_dir_set_reservation_check
,
1653 dsl_dir_set_reservation_sync
, &ddsqra
, 0));
1657 closest_common_ancestor(dsl_dir_t
*ds1
, dsl_dir_t
*ds2
)
1659 for (; ds1
; ds1
= ds1
->dd_parent
) {
1661 for (dd
= ds2
; dd
; dd
= dd
->dd_parent
) {
1670 * If delta is applied to dd, how much of that delta would be applied to
1671 * ancestor? Syncing context only.
1674 would_change(dsl_dir_t
*dd
, int64_t delta
, dsl_dir_t
*ancestor
)
1679 mutex_enter(&dd
->dd_lock
);
1680 delta
= parent_delta(dd
, dsl_dir_phys(dd
)->dd_used_bytes
, delta
);
1681 mutex_exit(&dd
->dd_lock
);
1682 return (would_change(dd
->dd_parent
, delta
, ancestor
));
1685 typedef struct dsl_dir_rename_arg
{
1686 const char *ddra_oldname
;
1687 const char *ddra_newname
;
1689 } dsl_dir_rename_arg_t
;
1693 dsl_valid_rename(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1696 char namebuf
[MAXNAMELEN
];
1698 dsl_dataset_name(ds
, namebuf
);
1700 if (strlen(namebuf
) + *deltap
>= MAXNAMELEN
)
1701 return (SET_ERROR(ENAMETOOLONG
));
1706 dsl_dir_rename_check(void *arg
, dmu_tx_t
*tx
)
1708 dsl_dir_rename_arg_t
*ddra
= arg
;
1709 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1710 dsl_dir_t
*dd
, *newparent
;
1711 const char *mynewname
;
1713 int delta
= strlen(ddra
->ddra_newname
) - strlen(ddra
->ddra_oldname
);
1715 /* target dir should exist */
1716 error
= dsl_dir_hold(dp
, ddra
->ddra_oldname
, FTAG
, &dd
, NULL
);
1720 /* new parent should exist */
1721 error
= dsl_dir_hold(dp
, ddra
->ddra_newname
, FTAG
,
1722 &newparent
, &mynewname
);
1724 dsl_dir_rele(dd
, FTAG
);
1728 /* can't rename to different pool */
1729 if (dd
->dd_pool
!= newparent
->dd_pool
) {
1730 dsl_dir_rele(newparent
, FTAG
);
1731 dsl_dir_rele(dd
, FTAG
);
1732 return (SET_ERROR(EXDEV
));
1735 /* new name should not already exist */
1736 if (mynewname
== NULL
) {
1737 dsl_dir_rele(newparent
, FTAG
);
1738 dsl_dir_rele(dd
, FTAG
);
1739 return (SET_ERROR(EEXIST
));
1742 /* if the name length is growing, validate child name lengths */
1744 error
= dmu_objset_find_dp(dp
, dd
->dd_object
, dsl_valid_rename
,
1745 &delta
, DS_FIND_CHILDREN
| DS_FIND_SNAPSHOTS
);
1747 dsl_dir_rele(newparent
, FTAG
);
1748 dsl_dir_rele(dd
, FTAG
);
1753 if (dmu_tx_is_syncing(tx
)) {
1754 if (spa_feature_is_enabled(dp
->dp_spa
,
1755 SPA_FEATURE_FS_SS_LIMIT
)) {
1757 * Although this is the check function and we don't
1758 * normally make on-disk changes in check functions,
1759 * we need to do that here.
1761 * Ensure this portion of the tree's counts have been
1762 * initialized in case the new parent has limits set.
1764 dsl_dir_init_fs_ss_count(dd
, tx
);
1768 if (newparent
!= dd
->dd_parent
) {
1769 /* is there enough space? */
1771 MAX(dsl_dir_phys(dd
)->dd_used_bytes
,
1772 dsl_dir_phys(dd
)->dd_reserved
);
1773 objset_t
*os
= dd
->dd_pool
->dp_meta_objset
;
1774 uint64_t fs_cnt
= 0;
1775 uint64_t ss_cnt
= 0;
1777 if (dsl_dir_is_zapified(dd
)) {
1780 err
= zap_lookup(os
, dd
->dd_object
,
1781 DD_FIELD_FILESYSTEM_COUNT
, sizeof (fs_cnt
), 1,
1783 if (err
!= ENOENT
&& err
!= 0)
1787 * have to add 1 for the filesystem itself that we're
1792 err
= zap_lookup(os
, dd
->dd_object
,
1793 DD_FIELD_SNAPSHOT_COUNT
, sizeof (ss_cnt
), 1,
1795 if (err
!= ENOENT
&& err
!= 0)
1799 /* no rename into our descendant */
1800 if (closest_common_ancestor(dd
, newparent
) == dd
) {
1801 dsl_dir_rele(newparent
, FTAG
);
1802 dsl_dir_rele(dd
, FTAG
);
1803 return (SET_ERROR(EINVAL
));
1806 error
= dsl_dir_transfer_possible(dd
->dd_parent
,
1807 newparent
, fs_cnt
, ss_cnt
, myspace
, ddra
->ddra_cred
);
1809 dsl_dir_rele(newparent
, FTAG
);
1810 dsl_dir_rele(dd
, FTAG
);
1815 dsl_dir_rele(newparent
, FTAG
);
1816 dsl_dir_rele(dd
, FTAG
);
1821 dsl_dir_rename_sync(void *arg
, dmu_tx_t
*tx
)
1823 dsl_dir_rename_arg_t
*ddra
= arg
;
1824 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1825 dsl_dir_t
*dd
, *newparent
;
1826 const char *mynewname
;
1828 objset_t
*mos
= dp
->dp_meta_objset
;
1830 VERIFY0(dsl_dir_hold(dp
, ddra
->ddra_oldname
, FTAG
, &dd
, NULL
));
1831 VERIFY0(dsl_dir_hold(dp
, ddra
->ddra_newname
, FTAG
, &newparent
,
1834 /* Log this before we change the name. */
1835 spa_history_log_internal_dd(dd
, "rename", tx
,
1836 "-> %s", ddra
->ddra_newname
);
1838 if (newparent
!= dd
->dd_parent
) {
1839 objset_t
*os
= dd
->dd_pool
->dp_meta_objset
;
1840 uint64_t fs_cnt
= 0;
1841 uint64_t ss_cnt
= 0;
1844 * We already made sure the dd counts were initialized in the
1847 if (spa_feature_is_enabled(dp
->dp_spa
,
1848 SPA_FEATURE_FS_SS_LIMIT
)) {
1849 VERIFY0(zap_lookup(os
, dd
->dd_object
,
1850 DD_FIELD_FILESYSTEM_COUNT
, sizeof (fs_cnt
), 1,
1852 /* add 1 for the filesystem itself that we're moving */
1855 VERIFY0(zap_lookup(os
, dd
->dd_object
,
1856 DD_FIELD_SNAPSHOT_COUNT
, sizeof (ss_cnt
), 1,
1860 dsl_fs_ss_count_adjust(dd
->dd_parent
, -fs_cnt
,
1861 DD_FIELD_FILESYSTEM_COUNT
, tx
);
1862 dsl_fs_ss_count_adjust(newparent
, fs_cnt
,
1863 DD_FIELD_FILESYSTEM_COUNT
, tx
);
1865 dsl_fs_ss_count_adjust(dd
->dd_parent
, -ss_cnt
,
1866 DD_FIELD_SNAPSHOT_COUNT
, tx
);
1867 dsl_fs_ss_count_adjust(newparent
, ss_cnt
,
1868 DD_FIELD_SNAPSHOT_COUNT
, tx
);
1870 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD
,
1871 -dsl_dir_phys(dd
)->dd_used_bytes
,
1872 -dsl_dir_phys(dd
)->dd_compressed_bytes
,
1873 -dsl_dir_phys(dd
)->dd_uncompressed_bytes
, tx
);
1874 dsl_dir_diduse_space(newparent
, DD_USED_CHILD
,
1875 dsl_dir_phys(dd
)->dd_used_bytes
,
1876 dsl_dir_phys(dd
)->dd_compressed_bytes
,
1877 dsl_dir_phys(dd
)->dd_uncompressed_bytes
, tx
);
1879 if (dsl_dir_phys(dd
)->dd_reserved
>
1880 dsl_dir_phys(dd
)->dd_used_bytes
) {
1881 uint64_t unused_rsrv
= dsl_dir_phys(dd
)->dd_reserved
-
1882 dsl_dir_phys(dd
)->dd_used_bytes
;
1884 dsl_dir_diduse_space(dd
->dd_parent
, DD_USED_CHILD_RSRV
,
1885 -unused_rsrv
, 0, 0, tx
);
1886 dsl_dir_diduse_space(newparent
, DD_USED_CHILD_RSRV
,
1887 unused_rsrv
, 0, 0, tx
);
1891 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1893 /* remove from old parent zapobj */
1894 error
= zap_remove(mos
,
1895 dsl_dir_phys(dd
->dd_parent
)->dd_child_dir_zapobj
,
1899 (void) strcpy(dd
->dd_myname
, mynewname
);
1900 dsl_dir_rele(dd
->dd_parent
, dd
);
1901 dsl_dir_phys(dd
)->dd_parent_obj
= newparent
->dd_object
;
1902 VERIFY0(dsl_dir_hold_obj(dp
,
1903 newparent
->dd_object
, NULL
, dd
, &dd
->dd_parent
));
1905 /* add to new parent zapobj */
1906 VERIFY0(zap_add(mos
, dsl_dir_phys(newparent
)->dd_child_dir_zapobj
,
1907 dd
->dd_myname
, 8, 1, &dd
->dd_object
, tx
));
1910 zvol_rename_minors(ddra
->ddra_oldname
, ddra
->ddra_newname
);
1913 dsl_prop_notify_all(dd
);
1915 dsl_dir_rele(newparent
, FTAG
);
1916 dsl_dir_rele(dd
, FTAG
);
1920 dsl_dir_rename(const char *oldname
, const char *newname
)
1922 dsl_dir_rename_arg_t ddra
;
1924 ddra
.ddra_oldname
= oldname
;
1925 ddra
.ddra_newname
= newname
;
1926 ddra
.ddra_cred
= CRED();
1928 return (dsl_sync_task(oldname
,
1929 dsl_dir_rename_check
, dsl_dir_rename_sync
, &ddra
, 3));
1933 dsl_dir_transfer_possible(dsl_dir_t
*sdd
, dsl_dir_t
*tdd
,
1934 uint64_t fs_cnt
, uint64_t ss_cnt
, uint64_t space
, cred_t
*cr
)
1936 dsl_dir_t
*ancestor
;
1941 ancestor
= closest_common_ancestor(sdd
, tdd
);
1942 adelta
= would_change(sdd
, -space
, ancestor
);
1943 avail
= dsl_dir_space_available(tdd
, ancestor
, adelta
, FALSE
);
1945 return (SET_ERROR(ENOSPC
));
1947 err
= dsl_fs_ss_limit_check(tdd
, fs_cnt
, ZFS_PROP_FILESYSTEM_LIMIT
,
1951 err
= dsl_fs_ss_limit_check(tdd
, ss_cnt
, ZFS_PROP_SNAPSHOT_LIMIT
,
1960 dsl_dir_snap_cmtime(dsl_dir_t
*dd
)
1964 mutex_enter(&dd
->dd_lock
);
1965 t
= dd
->dd_snap_cmtime
;
1966 mutex_exit(&dd
->dd_lock
);
1972 dsl_dir_snap_cmtime_update(dsl_dir_t
*dd
)
1977 mutex_enter(&dd
->dd_lock
);
1978 dd
->dd_snap_cmtime
= t
;
1979 mutex_exit(&dd
->dd_lock
);
1983 dsl_dir_zapify(dsl_dir_t
*dd
, dmu_tx_t
*tx
)
1985 objset_t
*mos
= dd
->dd_pool
->dp_meta_objset
;
1986 dmu_object_zapify(mos
, dd
->dd_object
, DMU_OT_DSL_DIR
, tx
);
1990 dsl_dir_is_zapified(dsl_dir_t
*dd
)
1992 dmu_object_info_t doi
;
1994 dmu_object_info_from_db(dd
->dd_dbuf
, &doi
);
1995 return (doi
.doi_type
== DMU_OTN_ZAP_METADATA
);
1998 #if defined(_KERNEL) && defined(HAVE_SPL)
1999 EXPORT_SYMBOL(dsl_dir_set_quota
);
2000 EXPORT_SYMBOL(dsl_dir_set_reservation
);