Path: blob/main/sys/contrib/openzfs/module/zfs/dsl_dir.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*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 <[email protected]>. All rights reserved.29* Copyright (c) 2023 Hewlett Packard Enterprise Development LP.30* Copyright (c) 2025, Rob Norris <[email protected]>31*/3233#include <sys/dmu.h>34#include <sys/dmu_objset.h>35#include <sys/dmu_tx.h>36#include <sys/dsl_dataset.h>37#include <sys/dsl_dir.h>38#include <sys/dsl_prop.h>39#include <sys/dsl_synctask.h>40#include <sys/dsl_deleg.h>41#include <sys/dmu_impl.h>42#include <sys/spa.h>43#include <sys/spa_impl.h>44#include <sys/metaslab.h>45#include <sys/zap.h>46#include <sys/zio.h>47#include <sys/arc.h>48#include <sys/sunddi.h>49#include <sys/zfeature.h>50#include <sys/policy.h>51#include <sys/zfs_vfsops.h>52#include <sys/zfs_znode.h>53#include <sys/zvol.h>54#include <sys/zthr.h>55#include "zfs_namecheck.h"56#include "zfs_prop.h"5758/*59* This controls if we verify the ZVOL quota or not.60* Currently, quotas are not implemented for ZVOLs.61* The quota size is the size of the ZVOL.62* The size of the volume already implies the ZVOL size quota.63* The quota mechanism can introduce a significant performance drop.64*/65static int zvol_enforce_quotas = B_TRUE;6667/*68* Filesystem and Snapshot Limits69* ------------------------------70*71* These limits are used to restrict the number of filesystems and/or snapshots72* that can be created at a given level in the tree or below. A typical73* use-case is with a delegated dataset where the administrator wants to ensure74* that a user within the zone is not creating too many additional filesystems75* or snapshots, even though they're not exceeding their space quota.76*77* The filesystem and snapshot counts are stored as extensible properties. This78* capability is controlled by a feature flag and must be enabled to be used.79* Once enabled, the feature is not active until the first limit is set. At80* that point, future operations to create/destroy filesystems or snapshots81* will validate and update the counts.82*83* Because the count properties will not exist before the feature is active,84* the counts are updated when a limit is first set on an uninitialized85* dsl_dir node in the tree (The filesystem/snapshot count on a node includes86* all of the nested filesystems/snapshots. Thus, a new leaf node has a87* filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and88* snapshot count properties on a node indicate uninitialized counts on that89* node.) When first setting a limit on an uninitialized node, the code starts90* at the filesystem with the new limit and descends into all sub-filesystems91* to add the count properties.92*93* In practice this is lightweight since a limit is typically set when the94* filesystem is created and thus has no children. Once valid, changing the95* limit value won't require a re-traversal since the counts are already valid.96* When recursively fixing the counts, if a node with a limit is encountered97* during the descent, the counts are known to be valid and there is no need to98* descend into that filesystem's children. The counts on filesystems above the99* one with the new limit will still be uninitialized, unless a limit is100* eventually set on one of those filesystems. The counts are always recursively101* updated when a limit is set on a dataset, unless there is already a limit.102* When a new limit value is set on a filesystem with an existing limit, it is103* possible for the new limit to be less than the current count at that level104* since a user who can change the limit is also allowed to exceed the limit.105*106* Once the feature is active, then whenever a filesystem or snapshot is107* created, the code recurses up the tree, validating the new count against the108* limit at each initialized level. In practice, most levels will not have a109* limit set. If there is a limit at any initialized level up the tree, the110* check must pass or the creation will fail. Likewise, when a filesystem or111* snapshot is destroyed, the counts are recursively adjusted all the way up112* the initialized nodes in the tree. Renaming a filesystem into different point113* in the tree will first validate, then update the counts on each branch up to114* the common ancestor. A receive will also validate the counts and then update115* them.116*117* An exception to the above behavior is that the limit is not enforced if the118* user has permission to modify the limit. This is primarily so that119* recursive snapshots in the global zone always work. We want to prevent a120* denial-of-service in which a lower level delegated dataset could max out its121* limit and thus block recursive snapshots from being taken in the global zone.122* Because of this, it is possible for the snapshot count to be over the limit123* and snapshots taken in the global zone could cause a lower level dataset to124* hit or exceed its limit. The administrator taking the global zone recursive125* snapshot should be aware of this side-effect and behave accordingly.126* For consistency, the filesystem limit is also not enforced if the user can127* modify the limit.128*129* The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check()130* and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in131* dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by132* dsl_dir_init_fs_ss_count().133*/134135static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd);136137typedef struct ddulrt_arg {138dsl_dir_t *ddulrta_dd;139uint64_t ddlrta_txg;140} ddulrt_arg_t;141142static void143dsl_dir_evict_async(void *dbu)144{145dsl_dir_t *dd = dbu;146int t;147dsl_pool_t *dp __maybe_unused = dd->dd_pool;148149dd->dd_dbuf = NULL;150151for (t = 0; t < TXG_SIZE; t++) {152ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t));153ASSERT0(dd->dd_tempreserved[t]);154ASSERT0(dd->dd_space_towrite[t]);155}156157if (dd->dd_parent)158dsl_dir_async_rele(dd->dd_parent, dd);159160spa_async_close(dd->dd_pool->dp_spa, dd);161162if (dsl_deadlist_is_open(&dd->dd_livelist))163dsl_dir_livelist_close(dd);164165dsl_prop_fini(dd);166cv_destroy(&dd->dd_activity_cv);167mutex_destroy(&dd->dd_activity_lock);168mutex_destroy(&dd->dd_lock);169kmem_free(dd, sizeof (dsl_dir_t));170}171172int173dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,174const char *tail, const void *tag, dsl_dir_t **ddp)175{176dmu_buf_t *dbuf;177dsl_dir_t *dd;178dmu_object_info_t doi;179int err;180181ASSERT(dsl_pool_config_held(dp));182183err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf);184if (err != 0)185return (err);186dd = dmu_buf_get_user(dbuf);187188dmu_object_info_from_db(dbuf, &doi);189ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR);190ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t));191192if (dd == NULL) {193dsl_dir_t *winner;194195dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP);196dd->dd_object = ddobj;197dd->dd_dbuf = dbuf;198dd->dd_pool = dp;199200mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL);201mutex_init(&dd->dd_activity_lock, NULL, MUTEX_DEFAULT, NULL);202cv_init(&dd->dd_activity_cv, NULL, CV_DEFAULT, NULL);203dsl_prop_init(dd);204205if (dsl_dir_is_zapified(dd)) {206err = zap_lookup(dp->dp_meta_objset,207ddobj, DD_FIELD_CRYPTO_KEY_OBJ,208sizeof (uint64_t), 1, &dd->dd_crypto_obj);209if (err == 0) {210/* check for on-disk format errata */211if (dsl_dir_incompatible_encryption_version(212dd)) {213dp->dp_spa->spa_errata =214ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;215}216} else if (err != ENOENT) {217goto errout;218}219}220221if (dsl_dir_phys(dd)->dd_parent_obj) {222err = dsl_dir_hold_obj(dp,223dsl_dir_phys(dd)->dd_parent_obj, NULL, dd,224&dd->dd_parent);225if (err != 0)226goto errout;227if (tail) {228#ifdef ZFS_DEBUG229uint64_t foundobj;230231err = zap_lookup(dp->dp_meta_objset,232dsl_dir_phys(dd->dd_parent)->233dd_child_dir_zapobj, tail,234sizeof (foundobj), 1, &foundobj);235ASSERT(err || foundobj == ddobj);236#endif237(void) strlcpy(dd->dd_myname, tail,238sizeof (dd->dd_myname));239} else {240err = zap_value_search(dp->dp_meta_objset,241dsl_dir_phys(dd->dd_parent)->242dd_child_dir_zapobj,243ddobj, 0, dd->dd_myname,244sizeof (dd->dd_myname));245}246if (err != 0)247goto errout;248} else {249(void) strlcpy(dd->dd_myname, spa_name(dp->dp_spa),250sizeof (dd->dd_myname));251}252253if (dsl_dir_is_clone(dd)) {254dmu_buf_t *origin_bonus;255dsl_dataset_phys_t *origin_phys;256257/*258* We can't open the origin dataset, because259* that would require opening this dsl_dir.260* Just look at its phys directly instead.261*/262err = dmu_bonus_hold(dp->dp_meta_objset,263dsl_dir_phys(dd)->dd_origin_obj, FTAG,264&origin_bonus);265if (err != 0)266goto errout;267origin_phys = origin_bonus->db_data;268dd->dd_origin_txg =269origin_phys->ds_creation_txg;270dmu_buf_rele(origin_bonus, FTAG);271if (dsl_dir_is_zapified(dd)) {272uint64_t obj;273err = zap_lookup(dp->dp_meta_objset,274dd->dd_object, DD_FIELD_LIVELIST,275sizeof (uint64_t), 1, &obj);276if (err == 0) {277err = dsl_dir_livelist_open(dd, obj);278if (err != 0)279goto errout;280} else if (err != ENOENT)281goto errout;282}283}284285if (dsl_dir_is_zapified(dd)) {286inode_timespec_t t = {0};287(void) zap_lookup(dp->dp_meta_objset, ddobj,288DD_FIELD_SNAPSHOTS_CHANGED,289sizeof (uint64_t),290sizeof (inode_timespec_t) / sizeof (uint64_t),291&t);292dd->dd_snap_cmtime = t;293}294295dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async,296&dd->dd_dbuf);297winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu);298if (winner != NULL) {299if (dd->dd_parent)300dsl_dir_rele(dd->dd_parent, dd);301if (dsl_deadlist_is_open(&dd->dd_livelist))302dsl_dir_livelist_close(dd);303dsl_prop_fini(dd);304cv_destroy(&dd->dd_activity_cv);305mutex_destroy(&dd->dd_activity_lock);306mutex_destroy(&dd->dd_lock);307kmem_free(dd, sizeof (dsl_dir_t));308dd = winner;309} else {310spa_open_ref(dp->dp_spa, dd);311}312}313314/*315* The dsl_dir_t has both open-to-close and instantiate-to-evict316* holds on the spa. We need the open-to-close holds because317* otherwise the spa_refcnt wouldn't change when we open a318* dir which the spa also has open, so we could incorrectly319* think it was OK to unload/export/destroy the pool. We need320* the instantiate-to-evict hold because the dsl_dir_t has a321* pointer to the dd_pool, which has a pointer to the spa_t.322*/323spa_open_ref(dp->dp_spa, tag);324ASSERT3P(dd->dd_pool, ==, dp);325ASSERT3U(dd->dd_object, ==, ddobj);326ASSERT3P(dd->dd_dbuf, ==, dbuf);327*ddp = dd;328return (0);329330errout:331if (dd->dd_parent)332dsl_dir_rele(dd->dd_parent, dd);333if (dsl_deadlist_is_open(&dd->dd_livelist))334dsl_dir_livelist_close(dd);335dsl_prop_fini(dd);336cv_destroy(&dd->dd_activity_cv);337mutex_destroy(&dd->dd_activity_lock);338mutex_destroy(&dd->dd_lock);339kmem_free(dd, sizeof (dsl_dir_t));340dmu_buf_rele(dbuf, tag);341return (err);342}343344void345dsl_dir_rele(dsl_dir_t *dd, const void *tag)346{347dprintf_dd(dd, "%s\n", "");348spa_close(dd->dd_pool->dp_spa, tag);349dmu_buf_rele(dd->dd_dbuf, tag);350}351352/*353* Remove a reference to the given dsl dir that is being asynchronously354* released. Async releases occur from a taskq performing eviction of355* dsl datasets and dirs. This process is identical to a normal release356* with the exception of using the async API for releasing the reference on357* the spa.358*/359void360dsl_dir_async_rele(dsl_dir_t *dd, const void *tag)361{362dprintf_dd(dd, "%s\n", "");363spa_async_close(dd->dd_pool->dp_spa, tag);364dmu_buf_rele(dd->dd_dbuf, tag);365}366367/* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */368void369dsl_dir_name(dsl_dir_t *dd, char *buf)370{371if (dd->dd_parent) {372dsl_dir_name(dd->dd_parent, buf);373VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <,374ZFS_MAX_DATASET_NAME_LEN);375} else {376buf[0] = '\0';377}378if (!MUTEX_HELD(&dd->dd_lock)) {379/*380* recursive mutex so that we can use381* dprintf_dd() with dd_lock held382*/383mutex_enter(&dd->dd_lock);384VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),385<, ZFS_MAX_DATASET_NAME_LEN);386mutex_exit(&dd->dd_lock);387} else {388VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN),389<, ZFS_MAX_DATASET_NAME_LEN);390}391}392393/* Calculate name length, avoiding all the strcat calls of dsl_dir_name */394int395dsl_dir_namelen(dsl_dir_t *dd)396{397int result = 0;398399if (dd->dd_parent) {400/* parent's name + 1 for the "/" */401result = dsl_dir_namelen(dd->dd_parent) + 1;402}403404if (!MUTEX_HELD(&dd->dd_lock)) {405/* see dsl_dir_name */406mutex_enter(&dd->dd_lock);407result += strlen(dd->dd_myname);408mutex_exit(&dd->dd_lock);409} else {410result += strlen(dd->dd_myname);411}412413return (result);414}415416static int417getcomponent(const char *path, char *component, const char **nextp)418{419char *p;420421if ((path == NULL) || (path[0] == '\0'))422return (SET_ERROR(ENOENT));423/* This would be a good place to reserve some namespace... */424p = strpbrk(path, "/@");425if (p && (p[1] == '/' || p[1] == '@')) {426/* two separators in a row */427return (SET_ERROR(EINVAL));428}429if (p == NULL || p == path) {430/*431* if the first thing is an @ or /, it had better be an432* @ and it had better not have any more ats or slashes,433* and it had better have something after the @.434*/435if (p != NULL &&436(p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0'))437return (SET_ERROR(EINVAL));438if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN)439return (SET_ERROR(ENAMETOOLONG));440(void) strlcpy(component, path, ZFS_MAX_DATASET_NAME_LEN);441p = NULL;442} else if (p[0] == '/') {443if (p - path >= ZFS_MAX_DATASET_NAME_LEN)444return (SET_ERROR(ENAMETOOLONG));445(void) strlcpy(component, path, p - path + 1);446p++;447} else if (p[0] == '@') {448/*449* if the next separator is an @, there better not be450* any more slashes.451*/452if (strchr(path, '/'))453return (SET_ERROR(EINVAL));454if (p - path >= ZFS_MAX_DATASET_NAME_LEN)455return (SET_ERROR(ENAMETOOLONG));456(void) strlcpy(component, path, p - path + 1);457} else {458panic("invalid p=%p", (void *)p);459}460*nextp = p;461return (0);462}463464/*465* Return the dsl_dir_t, and possibly the last component which couldn't466* be found in *tail. The name must be in the specified dsl_pool_t. This467* thread must hold the dp_config_rwlock for the pool. Returns NULL if the468* path is bogus, or if tail==NULL and we couldn't parse the whole name.469* (*tail)[0] == '@' means that the last component is a snapshot.470*/471int472dsl_dir_hold(dsl_pool_t *dp, const char *name, const void *tag,473dsl_dir_t **ddp, const char **tailp)474{475char *buf;476const char *spaname, *next, *nextnext = NULL;477int err;478dsl_dir_t *dd;479uint64_t ddobj;480481buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP);482err = getcomponent(name, buf, &next);483if (err != 0)484goto error;485486/* Make sure the name is in the specified pool. */487spaname = spa_name(dp->dp_spa);488if (strcmp(buf, spaname) != 0) {489err = SET_ERROR(EXDEV);490goto error;491}492493ASSERT(dsl_pool_config_held(dp));494495err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd);496if (err != 0) {497goto error;498}499500while (next != NULL) {501dsl_dir_t *child_dd;502err = getcomponent(next, buf, &nextnext);503if (err != 0)504break;505ASSERT(next[0] != '\0');506if (next[0] == '@')507break;508dprintf("looking up %s in obj%lld\n",509buf, (longlong_t)dsl_dir_phys(dd)->dd_child_dir_zapobj);510511err = zap_lookup(dp->dp_meta_objset,512dsl_dir_phys(dd)->dd_child_dir_zapobj,513buf, sizeof (ddobj), 1, &ddobj);514if (err != 0) {515if (err == ENOENT)516err = 0;517break;518}519520err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd);521if (err != 0)522break;523dsl_dir_rele(dd, tag);524dd = child_dd;525next = nextnext;526}527528if (err != 0) {529dsl_dir_rele(dd, tag);530goto error;531}532533/*534* It's an error if there's more than one component left, or535* tailp==NULL and there's any component left.536*/537if (next != NULL &&538(tailp == NULL || (nextnext && nextnext[0] != '\0'))) {539/* bad path name */540dsl_dir_rele(dd, tag);541dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp);542err = SET_ERROR(ENOENT);543}544if (tailp != NULL)545*tailp = next;546if (err == 0)547*ddp = dd;548error:549kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN);550return (err);551}552553/*554* If the counts are already initialized for this filesystem and its555* descendants then do nothing, otherwise initialize the counts.556*557* The counts on this filesystem, and those below, may be uninitialized due to558* either the use of a pre-existing pool which did not support the559* filesystem/snapshot limit feature, or one in which the feature had not yet560* been enabled.561*562* Recursively descend the filesystem tree and update the filesystem/snapshot563* counts on each filesystem below, then update the cumulative count on the564* current filesystem. If the filesystem already has a count set on it,565* then we know that its counts, and the counts on the filesystems below it,566* are already correct, so we don't have to update this filesystem.567*/568static void569dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx)570{571uint64_t my_fs_cnt = 0;572uint64_t my_ss_cnt = 0;573dsl_pool_t *dp = dd->dd_pool;574objset_t *os = dp->dp_meta_objset;575zap_cursor_t *zc;576zap_attribute_t *za;577dsl_dataset_t *ds;578579ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT));580ASSERT(dsl_pool_config_held(dp));581ASSERT(dmu_tx_is_syncing(tx));582583dsl_dir_zapify(dd, tx);584585/*586* If the filesystem count has already been initialized then we587* don't need to recurse down any further.588*/589if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0)590return;591592zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);593za = zap_attribute_alloc();594595/* Iterate my child dirs */596for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj);597zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) {598dsl_dir_t *chld_dd;599uint64_t count;600601VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG,602&chld_dd));603604/*605* Ignore hidden ($FREE, $MOS & $ORIGIN) objsets.606*/607if (chld_dd->dd_myname[0] == '$') {608dsl_dir_rele(chld_dd, FTAG);609continue;610}611612my_fs_cnt++; /* count this child */613614dsl_dir_init_fs_ss_count(chld_dd, tx);615616VERIFY0(zap_lookup(os, chld_dd->dd_object,617DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count));618my_fs_cnt += count;619VERIFY0(zap_lookup(os, chld_dd->dd_object,620DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count));621my_ss_cnt += count;622623dsl_dir_rele(chld_dd, FTAG);624}625zap_cursor_fini(zc);626/* Count my snapshots (we counted children's snapshots above) */627VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,628dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds));629630for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj);631zap_cursor_retrieve(zc, za) == 0;632zap_cursor_advance(zc)) {633/* Don't count temporary snapshots */634if (za->za_name[0] != '%')635my_ss_cnt++;636}637zap_cursor_fini(zc);638639dsl_dataset_rele(ds, FTAG);640641kmem_free(zc, sizeof (zap_cursor_t));642zap_attribute_free(za);643644/* we're in a sync task, update counts */645dmu_buf_will_dirty(dd->dd_dbuf, tx);646VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,647sizeof (my_fs_cnt), 1, &my_fs_cnt, tx));648VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,649sizeof (my_ss_cnt), 1, &my_ss_cnt, tx));650}651652static int653dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx)654{655char *ddname = (char *)arg;656dsl_pool_t *dp = dmu_tx_pool(tx);657dsl_dataset_t *ds;658dsl_dir_t *dd;659int error;660661error = dsl_dataset_hold(dp, ddname, FTAG, &ds);662if (error != 0)663return (error);664665if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) {666dsl_dataset_rele(ds, FTAG);667return (SET_ERROR(ENOTSUP));668}669670dd = ds->ds_dir;671if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) &&672dsl_dir_is_zapified(dd) &&673zap_contains(dp->dp_meta_objset, dd->dd_object,674DD_FIELD_FILESYSTEM_COUNT) == 0) {675dsl_dataset_rele(ds, FTAG);676return (SET_ERROR(EALREADY));677}678679dsl_dataset_rele(ds, FTAG);680return (0);681}682683static void684dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx)685{686char *ddname = (char *)arg;687dsl_pool_t *dp = dmu_tx_pool(tx);688dsl_dataset_t *ds;689spa_t *spa;690691VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds));692693spa = dsl_dataset_get_spa(ds);694695if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) {696/*697* Since the feature was not active and we're now setting a698* limit, increment the feature-active counter so that the699* feature becomes active for the first time.700*701* We are already in a sync task so we can update the MOS.702*/703spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx);704}705706/*707* Since we are now setting a non-UINT64_MAX limit on the filesystem,708* we need to ensure the counts are correct. Descend down the tree from709* this point and update all of the counts to be accurate.710*/711dsl_dir_init_fs_ss_count(ds->ds_dir, tx);712713dsl_dataset_rele(ds, FTAG);714}715716/*717* Make sure the feature is enabled and activate it if necessary.718* Since we're setting a limit, ensure the on-disk counts are valid.719* This is only called by the ioctl path when setting a limit value.720*721* We do not need to validate the new limit, since users who can change the722* limit are also allowed to exceed the limit.723*/724int725dsl_dir_activate_fs_ss_limit(const char *ddname)726{727int error;728729error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check,730dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0,731ZFS_SPACE_CHECK_RESERVED);732733if (error == EALREADY)734error = 0;735736return (error);737}738739/*740* Used to determine if the filesystem_limit or snapshot_limit should be741* enforced. We allow the limit to be exceeded if the user has permission to742* write the property value. We pass in the creds that we got in the open743* context since we will always be the GZ root in syncing context. We also have744* to handle the case where we are allowed to change the limit on the current745* dataset, but there may be another limit in the tree above.746*747* We can never modify these two properties within a non-global zone. In748* addition, the other checks are modeled on zfs_secpolicy_write_perms. We749* can't use that function since we are already holding the dp_config_rwlock.750* In addition, we already have the dd and dealing with snapshots is simplified751* in this code.752*/753754typedef enum {755ENFORCE_ALWAYS,756ENFORCE_NEVER,757ENFORCE_ABOVE758} enforce_res_t;759760static enforce_res_t761dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop,762cred_t *cr)763{764enforce_res_t enforce = ENFORCE_ALWAYS;765uint64_t obj;766dsl_dataset_t *ds;767uint64_t zoned;768const char *zonedstr;769770ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||771prop == ZFS_PROP_SNAPSHOT_LIMIT);772773#ifdef _KERNEL774if (crgetzoneid(cr) != GLOBAL_ZONEID)775return (ENFORCE_ALWAYS);776777if (secpolicy_zfs(cr) == 0)778return (ENFORCE_NEVER);779#endif780781if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0)782return (ENFORCE_ALWAYS);783784ASSERT(dsl_pool_config_held(dd->dd_pool));785786if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0)787return (ENFORCE_ALWAYS);788789zonedstr = zfs_prop_to_name(ZFS_PROP_ZONED);790if (dsl_prop_get_ds(ds, zonedstr, 8, 1, &zoned, NULL) || zoned) {791/* Only root can access zoned fs's from the GZ */792enforce = ENFORCE_ALWAYS;793} else {794if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0)795enforce = ENFORCE_ABOVE;796}797798dsl_dataset_rele(ds, FTAG);799return (enforce);800}801802/*803* Check if adding additional child filesystem(s) would exceed any filesystem804* limits or adding additional snapshot(s) would exceed any snapshot limits.805* The prop argument indicates which limit to check.806*807* Note that all filesystem limits up to the root (or the highest808* initialized) filesystem or the given ancestor must be satisfied.809*/810int811dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop,812dsl_dir_t *ancestor, cred_t *cr)813{814objset_t *os = dd->dd_pool->dp_meta_objset;815uint64_t limit, count;816const char *count_prop;817enforce_res_t enforce;818int err = 0;819820ASSERT(dsl_pool_config_held(dd->dd_pool));821ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT ||822prop == ZFS_PROP_SNAPSHOT_LIMIT);823824if (prop == ZFS_PROP_SNAPSHOT_LIMIT) {825/*826* We don't enforce the limit for temporary snapshots. This is827* indicated by a NULL cred_t argument.828*/829if (cr == NULL)830return (0);831832count_prop = DD_FIELD_SNAPSHOT_COUNT;833} else {834count_prop = DD_FIELD_FILESYSTEM_COUNT;835}836/*837* If we're allowed to change the limit, don't enforce the limit838* e.g. this can happen if a snapshot is taken by an administrative839* user in the global zone (i.e. a recursive snapshot by root).840* However, we must handle the case of delegated permissions where we841* are allowed to change the limit on the current dataset, but there842* is another limit in the tree above.843*/844enforce = dsl_enforce_ds_ss_limits(dd, prop, cr);845if (enforce == ENFORCE_NEVER)846return (0);847848/*849* e.g. if renaming a dataset with no snapshots, count adjustment850* is 0.851*/852if (delta == 0)853return (0);854855/*856* If an ancestor has been provided, stop checking the limit once we857* hit that dir. We need this during rename so that we don't overcount858* the check once we recurse up to the common ancestor.859*/860if (ancestor == dd)861return (0);862863/*864* If we hit an uninitialized node while recursing up the tree, we can865* stop since we know there is no limit here (or above). The counts are866* not valid on this node and we know we won't touch this node's counts.867*/868if (!dsl_dir_is_zapified(dd))869return (0);870err = zap_lookup(os, dd->dd_object,871count_prop, sizeof (count), 1, &count);872if (err == ENOENT)873return (0);874if (err != 0)875return (err);876877err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL,878B_FALSE);879if (err != 0)880return (err);881882/* Is there a limit which we've hit? */883if (enforce == ENFORCE_ALWAYS && (count + delta) > limit)884return (SET_ERROR(EDQUOT));885886if (dd->dd_parent != NULL)887err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop,888ancestor, cr);889890return (err);891}892893/*894* Adjust the filesystem or snapshot count for the specified dsl_dir_t and all895* parents. When a new filesystem/snapshot is created, increment the count on896* all parents, and when a filesystem/snapshot is destroyed, decrement the897* count.898*/899void900dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop,901dmu_tx_t *tx)902{903int err;904objset_t *os = dd->dd_pool->dp_meta_objset;905uint64_t count;906907ASSERT(dsl_pool_config_held(dd->dd_pool));908ASSERT(dmu_tx_is_syncing(tx));909ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 ||910strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0);911912/*913* We don't do accounting for hidden ($FREE, $MOS & $ORIGIN) objsets.914*/915if (dd->dd_myname[0] == '$' && strcmp(prop,916DD_FIELD_FILESYSTEM_COUNT) == 0) {917return;918}919920/*921* e.g. if renaming a dataset with no snapshots, count adjustment is 0922*/923if (delta == 0)924return;925926/*927* If we hit an uninitialized node while recursing up the tree, we can928* stop since we know the counts are not valid on this node and we929* know we shouldn't touch this node's counts. An uninitialized count930* on the node indicates that either the feature has not yet been931* activated or there are no limits on this part of the tree.932*/933if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object,934prop, sizeof (count), 1, &count)) == ENOENT)935return;936VERIFY0(err);937938count += delta;939/* Use a signed verify to make sure we're not neg. */940VERIFY3S(count, >=, 0);941942VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count,943tx));944945/* Roll up this additional count into our ancestors */946if (dd->dd_parent != NULL)947dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx);948}949950uint64_t951dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name,952dmu_tx_t *tx)953{954objset_t *mos = dp->dp_meta_objset;955uint64_t ddobj;956dsl_dir_phys_t *ddphys;957dmu_buf_t *dbuf;958959ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0,960DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx);961if (pds) {962VERIFY0(zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj,963name, sizeof (uint64_t), 1, &ddobj, tx));964} else {965/* it's the root dir */966VERIFY0(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT,967DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx));968}969VERIFY0(dmu_bonus_hold(mos, ddobj, FTAG, &dbuf));970dmu_buf_will_dirty(dbuf, tx);971ddphys = dbuf->db_data;972973ddphys->dd_creation_time = gethrestime_sec();974if (pds) {975ddphys->dd_parent_obj = pds->dd_object;976977/* update the filesystem counts */978dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx);979}980ddphys->dd_props_zapobj = zap_create(mos,981DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx);982ddphys->dd_child_dir_zapobj = zap_create(mos,983DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx);984if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN)985ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN;986987dmu_buf_rele(dbuf, FTAG);988989return (ddobj);990}991992boolean_t993dsl_dir_is_clone(dsl_dir_t *dd)994{995return (dsl_dir_phys(dd)->dd_origin_obj &&996(dd->dd_pool->dp_origin_snap == NULL ||997dsl_dir_phys(dd)->dd_origin_obj !=998dd->dd_pool->dp_origin_snap->ds_object));999}10001001uint64_t1002dsl_dir_get_used(dsl_dir_t *dd)1003{1004return (dsl_dir_phys(dd)->dd_used_bytes);1005}10061007uint64_t1008dsl_dir_get_compressed(dsl_dir_t *dd)1009{1010return (dsl_dir_phys(dd)->dd_compressed_bytes);1011}10121013uint64_t1014dsl_dir_get_quota(dsl_dir_t *dd)1015{1016return (dsl_dir_phys(dd)->dd_quota);1017}10181019uint64_t1020dsl_dir_get_reservation(dsl_dir_t *dd)1021{1022return (dsl_dir_phys(dd)->dd_reserved);1023}10241025uint64_t1026dsl_dir_get_compressratio(dsl_dir_t *dd)1027{1028/* a fixed point number, 100x the ratio */1029return (dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 :1030(dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 /1031dsl_dir_phys(dd)->dd_compressed_bytes));1032}10331034uint64_t1035dsl_dir_get_logicalused(dsl_dir_t *dd)1036{1037return (dsl_dir_phys(dd)->dd_uncompressed_bytes);1038}10391040uint64_t1041dsl_dir_get_usedsnap(dsl_dir_t *dd)1042{1043return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]);1044}10451046uint64_t1047dsl_dir_get_usedds(dsl_dir_t *dd)1048{1049return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]);1050}10511052uint64_t1053dsl_dir_get_usedrefreserv(dsl_dir_t *dd)1054{1055return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]);1056}10571058uint64_t1059dsl_dir_get_usedchild(dsl_dir_t *dd)1060{1061return (dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] +1062dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]);1063}10641065void1066dsl_dir_get_origin(dsl_dir_t *dd, char *buf)1067{1068dsl_dataset_t *ds;1069VERIFY0(dsl_dataset_hold_obj(dd->dd_pool,1070dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds));10711072dsl_dataset_name(ds, buf);10731074dsl_dataset_rele(ds, FTAG);1075}10761077int1078dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count)1079{1080if (dsl_dir_is_zapified(dd)) {1081objset_t *os = dd->dd_pool->dp_meta_objset;1082return (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT,1083sizeof (*count), 1, count));1084} else {1085return (SET_ERROR(ENOENT));1086}1087}10881089int1090dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count)1091{1092if (dsl_dir_is_zapified(dd)) {1093objset_t *os = dd->dd_pool->dp_meta_objset;1094return (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT,1095sizeof (*count), 1, count));1096} else {1097return (SET_ERROR(ENOENT));1098}1099}11001101void1102dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv)1103{1104mutex_enter(&dd->dd_lock);1105dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA,1106dsl_dir_get_quota(dd));1107dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION,1108dsl_dir_get_reservation(dd));1109dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED,1110dsl_dir_get_logicalused(dd));1111if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) {1112dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP,1113dsl_dir_get_usedsnap(dd));1114dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS,1115dsl_dir_get_usedds(dd));1116dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV,1117dsl_dir_get_usedrefreserv(dd));1118dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD,1119dsl_dir_get_usedchild(dd));1120}1121mutex_exit(&dd->dd_lock);11221123uint64_t count;1124if (dsl_dir_get_filesystem_count(dd, &count) == 0) {1125dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_FILESYSTEM_COUNT,1126count);1127}1128if (dsl_dir_get_snapshot_count(dd, &count) == 0) {1129dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_SNAPSHOT_COUNT,1130count);1131}11321133if (dsl_dir_is_clone(dd)) {1134char buf[ZFS_MAX_DATASET_NAME_LEN];1135dsl_dir_get_origin(dd, buf);1136dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf);1137}11381139}11401141void1142dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx)1143{1144dsl_pool_t *dp = dd->dd_pool;11451146ASSERT(dsl_dir_phys(dd));11471148if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) {1149/* up the hold count until we can be written out */1150dmu_buf_add_ref(dd->dd_dbuf, dd);1151}1152}11531154static int64_t1155parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta)1156{1157uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved);1158uint64_t new_accounted =1159MAX(used + delta, dsl_dir_phys(dd)->dd_reserved);1160return (new_accounted - old_accounted);1161}11621163void1164dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx)1165{1166ASSERT(dmu_tx_is_syncing(tx));11671168mutex_enter(&dd->dd_lock);1169ASSERT0(dd->dd_tempreserved[tx->tx_txg & TXG_MASK]);1170dprintf_dd(dd, "txg=%llu towrite=%lluK\n", (u_longlong_t)tx->tx_txg,1171(u_longlong_t)dd->dd_space_towrite[tx->tx_txg & TXG_MASK] / 1024);1172dd->dd_space_towrite[tx->tx_txg & TXG_MASK] = 0;1173mutex_exit(&dd->dd_lock);11741175/* release the hold from dsl_dir_dirty */1176dmu_buf_rele(dd->dd_dbuf, dd);1177}11781179static uint64_t1180dsl_dir_space_towrite(dsl_dir_t *dd)1181{1182uint64_t space = 0;11831184ASSERT(MUTEX_HELD(&dd->dd_lock));11851186for (int i = 0; i < TXG_SIZE; i++)1187space += dd->dd_space_towrite[i & TXG_MASK];11881189return (space);1190}11911192/*1193* How much space would dd have available if ancestor had delta applied1194* to it? If ondiskonly is set, we're only interested in what's1195* on-disk, not estimated pending changes.1196*/1197uint64_t1198dsl_dir_space_available(dsl_dir_t *dd,1199dsl_dir_t *ancestor, int64_t delta, int ondiskonly)1200{1201uint64_t parentspace, myspace, quota, used;12021203/*1204* If there are no restrictions otherwise, assume we have1205* unlimited space available.1206*/1207quota = UINT64_MAX;1208parentspace = UINT64_MAX;12091210if (dd->dd_parent != NULL) {1211parentspace = dsl_dir_space_available(dd->dd_parent,1212ancestor, delta, ondiskonly);1213}12141215mutex_enter(&dd->dd_lock);1216if (dsl_dir_phys(dd)->dd_quota != 0)1217quota = dsl_dir_phys(dd)->dd_quota;1218used = dsl_dir_phys(dd)->dd_used_bytes;1219if (!ondiskonly)1220used += dsl_dir_space_towrite(dd);12211222if (dd->dd_parent == NULL) {1223uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool,1224ZFS_SPACE_CHECK_NORMAL);1225quota = MIN(quota, poolsize);1226}12271228if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) {1229/*1230* We have some space reserved, in addition to what our1231* parent gave us.1232*/1233parentspace += dsl_dir_phys(dd)->dd_reserved - used;1234}12351236if (dd == ancestor) {1237ASSERT(delta <= 0);1238ASSERT(used >= -delta);1239used += delta;1240if (parentspace != UINT64_MAX)1241parentspace -= delta;1242}12431244if (used > quota) {1245/* over quota */1246myspace = 0;1247} else {1248/*1249* the lesser of the space provided by our parent and1250* the space left in our quota1251*/1252myspace = MIN(parentspace, quota - used);1253}12541255mutex_exit(&dd->dd_lock);12561257return (myspace);1258}12591260struct tempreserve {1261list_node_t tr_node;1262dsl_dir_t *tr_ds;1263uint64_t tr_size;1264};12651266static int1267dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree,1268boolean_t ignorequota, list_t *tr_list,1269dmu_tx_t *tx, boolean_t first)1270{1271uint64_t txg;1272uint64_t quota;1273struct tempreserve *tr;1274int retval;1275uint64_t ext_quota;1276uint64_t ref_rsrv;12771278top_of_function:1279txg = tx->tx_txg;1280retval = EDQUOT;1281ref_rsrv = 0;12821283ASSERT3U(txg, !=, 0);1284ASSERT3S(asize, >, 0);12851286mutex_enter(&dd->dd_lock);12871288/*1289* Check against the dsl_dir's quota. We don't add in the delta1290* when checking for over-quota because they get one free hit.1291*/1292uint64_t est_inflight = dsl_dir_space_towrite(dd);1293for (int i = 0; i < TXG_SIZE; i++)1294est_inflight += dd->dd_tempreserved[i];1295uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes;12961297/*1298* On the first iteration, fetch the dataset's used-on-disk and1299* refreservation values. Also, if checkrefquota is set, test if1300* allocating this space would exceed the dataset's refquota.1301*/1302if (first && tx->tx_objset) {1303int error;1304dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset;13051306error = dsl_dataset_check_quota(ds, !netfree,1307asize, est_inflight, &used_on_disk, &ref_rsrv);1308if (error != 0) {1309mutex_exit(&dd->dd_lock);1310DMU_TX_STAT_BUMP(dmu_tx_quota);1311return (error);1312}1313}13141315/*1316* If this transaction will result in a net free of space,1317* we want to let it through.1318*/1319if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0 ||1320(tx->tx_objset && dmu_objset_type(tx->tx_objset) == DMU_OST_ZVOL &&1321zvol_enforce_quotas == B_FALSE))1322quota = UINT64_MAX;1323else1324quota = dsl_dir_phys(dd)->dd_quota;13251326/*1327* Adjust the quota against the actual pool size at the root1328* minus any outstanding deferred frees.1329* To ensure that it's possible to remove files from a full1330* pool without inducing transient overcommits, we throttle1331* netfree transactions against a quota that is slightly larger,1332* but still within the pool's allocation slop. In cases where1333* we're very close to full, this will allow a steady trickle of1334* removes to get through.1335*/1336if (dd->dd_parent == NULL) {1337uint64_t avail = dsl_pool_unreserved_space(dd->dd_pool,1338(netfree) ?1339ZFS_SPACE_CHECK_RESERVED : ZFS_SPACE_CHECK_NORMAL);13401341if (avail < quota) {1342quota = avail;1343retval = SET_ERROR(ENOSPC);1344}1345}13461347/*1348* If they are requesting more space, and our current estimate1349* is over quota, they get to try again unless the actual1350* on-disk is over quota and there are no pending changes1351* or deferred frees (which may free up space for us).1352*/1353ext_quota = quota >> 5;1354if (quota == UINT64_MAX)1355ext_quota = 0;13561357if (used_on_disk >= quota) {1358if (retval == ENOSPC && (used_on_disk - quota) <1359dsl_pool_deferred_space(dd->dd_pool)) {1360retval = SET_ERROR(ERESTART);1361}1362/* Quota exceeded */1363mutex_exit(&dd->dd_lock);1364DMU_TX_STAT_BUMP(dmu_tx_quota);1365return (retval);1366} else if (used_on_disk + est_inflight >= quota + ext_quota) {1367dprintf_dd(dd, "failing: used=%lluK inflight = %lluK "1368"quota=%lluK tr=%lluK\n",1369(u_longlong_t)used_on_disk>>10,1370(u_longlong_t)est_inflight>>10,1371(u_longlong_t)quota>>10, (u_longlong_t)asize>>10);1372mutex_exit(&dd->dd_lock);1373DMU_TX_STAT_BUMP(dmu_tx_quota);1374return (SET_ERROR(ERESTART));1375}13761377/* We need to up our estimated delta before dropping dd_lock */1378dd->dd_tempreserved[txg & TXG_MASK] += asize;13791380uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight,1381asize - ref_rsrv);1382mutex_exit(&dd->dd_lock);13831384tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);1385tr->tr_ds = dd;1386tr->tr_size = asize;1387list_insert_tail(tr_list, tr);13881389/* see if it's OK with our parent */1390if (dd->dd_parent != NULL && parent_rsrv != 0) {1391/*1392* Recurse on our parent without recursion. This has been1393* observed to be potentially large stack usage even within1394* the test suite. Largest seen stack was 7632 bytes on linux.1395*/13961397dd = dd->dd_parent;1398asize = parent_rsrv;1399ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0);1400first = B_FALSE;1401goto top_of_function;1402}14031404return (0);1405}14061407/*1408* Reserve space in this dsl_dir, to be used in this tx's txg.1409* After the space has been dirtied (and dsl_dir_willuse_space()1410* has been called), the reservation should be canceled, using1411* dsl_dir_tempreserve_clear().1412*/1413int1414dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize,1415boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx)1416{1417int err;1418list_t *tr_list;14191420if (asize == 0) {1421*tr_cookiep = NULL;1422return (0);1423}14241425tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP);1426list_create(tr_list, sizeof (struct tempreserve),1427offsetof(struct tempreserve, tr_node));1428ASSERT3S(asize, >, 0);14291430err = arc_tempreserve_space(dd->dd_pool->dp_spa, lsize, tx->tx_txg);1431if (err == 0) {1432struct tempreserve *tr;14331434tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP);1435tr->tr_size = lsize;1436list_insert_tail(tr_list, tr);1437} else {1438if (err == EAGAIN) {1439/*1440* If arc_memory_throttle() detected that pageout1441* is running and we are low on memory, we delay new1442* non-pageout transactions to give pageout an1443* advantage.1444*1445* It is unfortunate to be delaying while the caller's1446* locks are held.1447*/1448txg_delay(dd->dd_pool, tx->tx_txg,1449MSEC2NSEC(10), MSEC2NSEC(10));1450err = SET_ERROR(ERESTART);1451}14521453ASSERT3U(err, ==, ERESTART);1454}14551456if (err == 0) {1457err = dsl_dir_tempreserve_impl(dd, asize, netfree,1458B_FALSE, tr_list, tx, B_TRUE);1459}14601461if (err != 0)1462dsl_dir_tempreserve_clear(tr_list, tx);1463else1464*tr_cookiep = tr_list;14651466return (err);1467}14681469/*1470* Clear a temporary reservation that we previously made with1471* dsl_dir_tempreserve_space().1472*/1473void1474dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx)1475{1476int txgidx = tx->tx_txg & TXG_MASK;1477list_t *tr_list = tr_cookie;1478struct tempreserve *tr;14791480ASSERT3U(tx->tx_txg, !=, 0);14811482if (tr_cookie == NULL)1483return;14841485while ((tr = list_remove_head(tr_list)) != NULL) {1486if (tr->tr_ds) {1487mutex_enter(&tr->tr_ds->dd_lock);1488ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=,1489tr->tr_size);1490tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size;1491mutex_exit(&tr->tr_ds->dd_lock);1492} else {1493arc_tempreserve_clear(tr->tr_size);1494}1495kmem_free(tr, sizeof (struct tempreserve));1496}14971498kmem_free(tr_list, sizeof (list_t));1499}15001501/*1502* This should be called from open context when we think we're going to write1503* or free space, for example when dirtying data. Be conservative; it's okay1504* to write less space or free more, but we don't want to write more or free1505* less than the amount specified.1506*1507* NOTE: The behavior of this function is identical to the Illumos / FreeBSD1508* version however it has been adjusted to use an iterative rather than1509* recursive algorithm to minimize stack usage.1510*/1511void1512dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx)1513{1514int64_t parent_space;1515uint64_t est_used;15161517do {1518mutex_enter(&dd->dd_lock);1519if (space > 0)1520dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space;15211522est_used = dsl_dir_space_towrite(dd) +1523dsl_dir_phys(dd)->dd_used_bytes;1524parent_space = parent_delta(dd, est_used, space);1525mutex_exit(&dd->dd_lock);15261527/* Make sure that we clean up dd_space_to* */1528dsl_dir_dirty(dd, tx);15291530dd = dd->dd_parent;1531space = parent_space;1532} while (space && dd);1533}15341535/* call from syncing context when we actually write/free space for this dd */1536void1537dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,1538int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx)1539{1540int64_t accounted_delta;15411542ASSERT(dmu_tx_is_syncing(tx));1543ASSERT(type < DD_USED_NUM);15441545dmu_buf_will_dirty(dd->dd_dbuf, tx);15461547/*1548* dsl_dataset_set_refreservation_sync_impl() calls this with1549* dd_lock held, so that it can atomically update1550* ds->ds_reserved and the dsl_dir accounting, so that1551* dsl_dataset_check_quota() can see dataset and dir accounting1552* consistently.1553*/1554boolean_t needlock = !MUTEX_HELD(&dd->dd_lock);1555if (needlock)1556mutex_enter(&dd->dd_lock);1557dsl_dir_phys_t *ddp = dsl_dir_phys(dd);1558accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used);1559ASSERT(used >= 0 || ddp->dd_used_bytes >= -used);1560ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed);1561ASSERT(uncompressed >= 0 ||1562ddp->dd_uncompressed_bytes >= -uncompressed);1563ddp->dd_used_bytes += used;1564ddp->dd_uncompressed_bytes += uncompressed;1565ddp->dd_compressed_bytes += compressed;15661567if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) {1568ASSERT(used >= 0 || ddp->dd_used_breakdown[type] >= -used);1569ddp->dd_used_breakdown[type] += used;1570#ifdef ZFS_DEBUG1571{1572dd_used_t t;1573uint64_t u = 0;1574for (t = 0; t < DD_USED_NUM; t++)1575u += ddp->dd_used_breakdown[t];1576ASSERT3U(u, ==, ddp->dd_used_bytes);1577}1578#endif1579}1580if (needlock)1581mutex_exit(&dd->dd_lock);15821583if (dd->dd_parent != NULL) {1584dsl_dir_diduse_transfer_space(dd->dd_parent,1585accounted_delta, compressed, uncompressed,1586used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);1587}1588}15891590void1591dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,1592dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)1593{1594ASSERT(dmu_tx_is_syncing(tx));1595ASSERT(oldtype < DD_USED_NUM);1596ASSERT(newtype < DD_USED_NUM);15971598dsl_dir_phys_t *ddp = dsl_dir_phys(dd);1599if (delta == 0 ||1600!(ddp->dd_flags & DD_FLAG_USED_BREAKDOWN))1601return;16021603dmu_buf_will_dirty(dd->dd_dbuf, tx);1604mutex_enter(&dd->dd_lock);1605ASSERT(delta > 0 ?1606ddp->dd_used_breakdown[oldtype] >= delta :1607ddp->dd_used_breakdown[newtype] >= -delta);1608ASSERT(ddp->dd_used_bytes >= ABS(delta));1609ddp->dd_used_breakdown[oldtype] -= delta;1610ddp->dd_used_breakdown[newtype] += delta;1611mutex_exit(&dd->dd_lock);1612}16131614void1615dsl_dir_diduse_transfer_space(dsl_dir_t *dd, int64_t used,1616int64_t compressed, int64_t uncompressed, int64_t tonew,1617dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx)1618{1619int64_t accounted_delta;16201621ASSERT(dmu_tx_is_syncing(tx));1622ASSERT(oldtype < DD_USED_NUM);1623ASSERT(newtype < DD_USED_NUM);16241625dmu_buf_will_dirty(dd->dd_dbuf, tx);16261627mutex_enter(&dd->dd_lock);1628dsl_dir_phys_t *ddp = dsl_dir_phys(dd);1629accounted_delta = parent_delta(dd, ddp->dd_used_bytes, used);1630ASSERT(used >= 0 || ddp->dd_used_bytes >= -used);1631ASSERT(compressed >= 0 || ddp->dd_compressed_bytes >= -compressed);1632ASSERT(uncompressed >= 0 ||1633ddp->dd_uncompressed_bytes >= -uncompressed);1634ddp->dd_used_bytes += used;1635ddp->dd_uncompressed_bytes += uncompressed;1636ddp->dd_compressed_bytes += compressed;16371638if (ddp->dd_flags & DD_FLAG_USED_BREAKDOWN) {1639ASSERT(tonew - used <= 0 ||1640ddp->dd_used_breakdown[oldtype] >= tonew - used);1641ASSERT(tonew >= 0 ||1642ddp->dd_used_breakdown[newtype] >= -tonew);1643ddp->dd_used_breakdown[oldtype] -= tonew - used;1644ddp->dd_used_breakdown[newtype] += tonew;1645#ifdef ZFS_DEBUG1646{1647dd_used_t t;1648uint64_t u = 0;1649for (t = 0; t < DD_USED_NUM; t++)1650u += ddp->dd_used_breakdown[t];1651ASSERT3U(u, ==, ddp->dd_used_bytes);1652}1653#endif1654}1655mutex_exit(&dd->dd_lock);16561657if (dd->dd_parent != NULL) {1658dsl_dir_diduse_transfer_space(dd->dd_parent,1659accounted_delta, compressed, uncompressed,1660used, DD_USED_CHILD_RSRV, DD_USED_CHILD, tx);1661}1662}16631664typedef struct dsl_dir_set_qr_arg {1665const char *ddsqra_name;1666zprop_source_t ddsqra_source;1667uint64_t ddsqra_value;1668} dsl_dir_set_qr_arg_t;16691670static int1671dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx)1672{1673dsl_dir_set_qr_arg_t *ddsqra = arg;1674dsl_pool_t *dp = dmu_tx_pool(tx);1675dsl_dataset_t *ds;1676int error;1677uint64_t towrite, newval;16781679error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);1680if (error != 0)1681return (error);16821683error = dsl_prop_predict(ds->ds_dir, "quota",1684ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);1685if (error != 0) {1686dsl_dataset_rele(ds, FTAG);1687return (error);1688}16891690if (newval == 0) {1691dsl_dataset_rele(ds, FTAG);1692return (0);1693}16941695mutex_enter(&ds->ds_dir->dd_lock);1696/*1697* If we are doing the preliminary check in open context, and1698* there are pending changes, then don't fail it, since the1699* pending changes could under-estimate the amount of space to be1700* freed up.1701*/1702towrite = dsl_dir_space_towrite(ds->ds_dir);1703if ((dmu_tx_is_syncing(tx) || towrite == 0) &&1704(newval < dsl_dir_phys(ds->ds_dir)->dd_reserved ||1705newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) {1706error = SET_ERROR(ENOSPC);1707}1708mutex_exit(&ds->ds_dir->dd_lock);1709dsl_dataset_rele(ds, FTAG);1710return (error);1711}17121713static void1714dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx)1715{1716dsl_dir_set_qr_arg_t *ddsqra = arg;1717dsl_pool_t *dp = dmu_tx_pool(tx);1718dsl_dataset_t *ds;1719uint64_t newval;17201721VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));17221723if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {1724dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA),1725ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,1726&ddsqra->ddsqra_value, tx);17271728VERIFY0(dsl_prop_get_int_ds(ds,1729zfs_prop_to_name(ZFS_PROP_QUOTA), &newval));1730} else {1731newval = ddsqra->ddsqra_value;1732spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",1733zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval);1734}17351736dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);1737mutex_enter(&ds->ds_dir->dd_lock);1738dsl_dir_phys(ds->ds_dir)->dd_quota = newval;1739mutex_exit(&ds->ds_dir->dd_lock);1740dsl_dataset_rele(ds, FTAG);1741}17421743int1744dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota)1745{1746dsl_dir_set_qr_arg_t ddsqra;17471748ddsqra.ddsqra_name = ddname;1749ddsqra.ddsqra_source = source;1750ddsqra.ddsqra_value = quota;17511752return (dsl_sync_task(ddname, dsl_dir_set_quota_check,1753dsl_dir_set_quota_sync, &ddsqra, 0,1754ZFS_SPACE_CHECK_EXTRA_RESERVED));1755}17561757static int1758dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx)1759{1760dsl_dir_set_qr_arg_t *ddsqra = arg;1761dsl_pool_t *dp = dmu_tx_pool(tx);1762dsl_dataset_t *ds;1763dsl_dir_t *dd;1764uint64_t newval, used, avail;1765int error;17661767error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);1768if (error != 0)1769return (error);1770dd = ds->ds_dir;17711772/*1773* If we are doing the preliminary check in open context, the1774* space estimates may be inaccurate.1775*/1776if (!dmu_tx_is_syncing(tx)) {1777dsl_dataset_rele(ds, FTAG);1778return (0);1779}17801781error = dsl_prop_predict(ds->ds_dir,1782zfs_prop_to_name(ZFS_PROP_RESERVATION),1783ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);1784if (error != 0) {1785dsl_dataset_rele(ds, FTAG);1786return (error);1787}17881789mutex_enter(&dd->dd_lock);1790used = dsl_dir_phys(dd)->dd_used_bytes;1791mutex_exit(&dd->dd_lock);17921793if (dd->dd_parent) {1794avail = dsl_dir_space_available(dd->dd_parent,1795NULL, 0, FALSE);1796} else {1797avail = dsl_pool_adjustedsize(dd->dd_pool,1798ZFS_SPACE_CHECK_NORMAL) - used;1799}18001801if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) {1802uint64_t delta = MAX(used, newval) -1803MAX(used, dsl_dir_phys(dd)->dd_reserved);18041805if (delta > avail ||1806(dsl_dir_phys(dd)->dd_quota > 0 &&1807newval > dsl_dir_phys(dd)->dd_quota))1808error = SET_ERROR(ENOSPC);1809}18101811dsl_dataset_rele(ds, FTAG);1812return (error);1813}18141815void1816dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx)1817{1818uint64_t used;1819int64_t delta;18201821dmu_buf_will_dirty(dd->dd_dbuf, tx);18221823mutex_enter(&dd->dd_lock);1824used = dsl_dir_phys(dd)->dd_used_bytes;1825delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved);1826dsl_dir_phys(dd)->dd_reserved = value;18271828if (dd->dd_parent != NULL) {1829/* Roll up this additional usage into our ancestors */1830dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,1831delta, 0, 0, tx);1832}1833mutex_exit(&dd->dd_lock);1834}18351836static void1837dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx)1838{1839dsl_dir_set_qr_arg_t *ddsqra = arg;1840dsl_pool_t *dp = dmu_tx_pool(tx);1841dsl_dataset_t *ds;1842uint64_t newval;18431844VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));18451846if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) {1847dsl_prop_set_sync_impl(ds,1848zfs_prop_to_name(ZFS_PROP_RESERVATION),1849ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,1850&ddsqra->ddsqra_value, tx);18511852VERIFY0(dsl_prop_get_int_ds(ds,1853zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval));1854} else {1855newval = ddsqra->ddsqra_value;1856spa_history_log_internal_ds(ds, "set", tx, "%s=%lld",1857zfs_prop_to_name(ZFS_PROP_RESERVATION),1858(longlong_t)newval);1859}18601861dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx);1862dsl_dataset_rele(ds, FTAG);1863}18641865int1866dsl_dir_set_reservation(const char *ddname, zprop_source_t source,1867uint64_t reservation)1868{1869dsl_dir_set_qr_arg_t ddsqra;18701871ddsqra.ddsqra_name = ddname;1872ddsqra.ddsqra_source = source;1873ddsqra.ddsqra_value = reservation;18741875return (dsl_sync_task(ddname, dsl_dir_set_reservation_check,1876dsl_dir_set_reservation_sync, &ddsqra, 0,1877ZFS_SPACE_CHECK_EXTRA_RESERVED));1878}18791880static dsl_dir_t *1881closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2)1882{1883for (; ds1; ds1 = ds1->dd_parent) {1884dsl_dir_t *dd;1885for (dd = ds2; dd; dd = dd->dd_parent) {1886if (ds1 == dd)1887return (dd);1888}1889}1890return (NULL);1891}18921893/*1894* If delta is applied to dd, how much of that delta would be applied to1895* ancestor? Syncing context only.1896*/1897static int64_t1898would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor)1899{1900if (dd == ancestor)1901return (delta);19021903mutex_enter(&dd->dd_lock);1904delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta);1905mutex_exit(&dd->dd_lock);1906return (would_change(dd->dd_parent, delta, ancestor));1907}19081909typedef struct dsl_dir_rename_arg {1910const char *ddra_oldname;1911const char *ddra_newname;1912cred_t *ddra_cred;1913} dsl_dir_rename_arg_t;19141915typedef struct dsl_valid_rename_arg {1916int char_delta;1917int nest_delta;1918} dsl_valid_rename_arg_t;19191920static int1921dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)1922{1923(void) dp;1924dsl_valid_rename_arg_t *dvra = arg;1925char namebuf[ZFS_MAX_DATASET_NAME_LEN];19261927dsl_dataset_name(ds, namebuf);19281929ASSERT3U(strnlen(namebuf, ZFS_MAX_DATASET_NAME_LEN),1930<, ZFS_MAX_DATASET_NAME_LEN);1931int namelen = strlen(namebuf) + dvra->char_delta;1932int depth = get_dataset_depth(namebuf) + dvra->nest_delta;19331934if (namelen >= ZFS_MAX_DATASET_NAME_LEN)1935return (SET_ERROR(ENAMETOOLONG));1936if (dvra->nest_delta > 0 && depth >= zfs_max_dataset_nesting)1937return (SET_ERROR(ENAMETOOLONG));1938return (0);1939}19401941static int1942dsl_dir_rename_check(void *arg, dmu_tx_t *tx)1943{1944dsl_dir_rename_arg_t *ddra = arg;1945dsl_pool_t *dp = dmu_tx_pool(tx);1946dsl_dir_t *dd, *newparent;1947dsl_valid_rename_arg_t dvra;1948dsl_dataset_t *parentds;1949objset_t *parentos;1950const char *mynewname;1951int error;19521953/* target dir should exist */1954error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL);1955if (error != 0)1956return (error);19571958/* new parent should exist */1959error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG,1960&newparent, &mynewname);1961if (error != 0) {1962dsl_dir_rele(dd, FTAG);1963return (error);1964}19651966/* can't rename to different pool */1967if (dd->dd_pool != newparent->dd_pool) {1968dsl_dir_rele(newparent, FTAG);1969dsl_dir_rele(dd, FTAG);1970return (SET_ERROR(EXDEV));1971}19721973/* new name should not already exist */1974if (mynewname == NULL) {1975dsl_dir_rele(newparent, FTAG);1976dsl_dir_rele(dd, FTAG);1977return (SET_ERROR(EEXIST));1978}19791980/* can't rename below anything but filesystems (eg. no ZVOLs) */1981error = dsl_dataset_hold_obj(newparent->dd_pool,1982dsl_dir_phys(newparent)->dd_head_dataset_obj, FTAG, &parentds);1983if (error != 0) {1984dsl_dir_rele(newparent, FTAG);1985dsl_dir_rele(dd, FTAG);1986return (error);1987}1988error = dmu_objset_from_ds(parentds, &parentos);1989if (error != 0) {1990dsl_dataset_rele(parentds, FTAG);1991dsl_dir_rele(newparent, FTAG);1992dsl_dir_rele(dd, FTAG);1993return (error);1994}1995if (dmu_objset_type(parentos) != DMU_OST_ZFS) {1996dsl_dataset_rele(parentds, FTAG);1997dsl_dir_rele(newparent, FTAG);1998dsl_dir_rele(dd, FTAG);1999return (SET_ERROR(ZFS_ERR_WRONG_PARENT));2000}2001dsl_dataset_rele(parentds, FTAG);20022003ASSERT3U(strnlen(ddra->ddra_newname, ZFS_MAX_DATASET_NAME_LEN),2004<, ZFS_MAX_DATASET_NAME_LEN);2005ASSERT3U(strnlen(ddra->ddra_oldname, ZFS_MAX_DATASET_NAME_LEN),2006<, ZFS_MAX_DATASET_NAME_LEN);2007dvra.char_delta = strlen(ddra->ddra_newname)2008- strlen(ddra->ddra_oldname);2009dvra.nest_delta = get_dataset_depth(ddra->ddra_newname)2010- get_dataset_depth(ddra->ddra_oldname);20112012/* if the name length is growing, validate child name lengths */2013if (dvra.char_delta > 0 || dvra.nest_delta > 0) {2014error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename,2015&dvra, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);2016if (error != 0) {2017dsl_dir_rele(newparent, FTAG);2018dsl_dir_rele(dd, FTAG);2019return (error);2020}2021}20222023if (dmu_tx_is_syncing(tx)) {2024if (spa_feature_is_active(dp->dp_spa,2025SPA_FEATURE_FS_SS_LIMIT)) {2026/*2027* Although this is the check function and we don't2028* normally make on-disk changes in check functions,2029* we need to do that here.2030*2031* Ensure this portion of the tree's counts have been2032* initialized in case the new parent has limits set.2033*/2034dsl_dir_init_fs_ss_count(dd, tx);2035}2036}20372038if (newparent != dd->dd_parent) {2039/* is there enough space? */2040uint64_t myspace =2041MAX(dsl_dir_phys(dd)->dd_used_bytes,2042dsl_dir_phys(dd)->dd_reserved);2043objset_t *os = dd->dd_pool->dp_meta_objset;2044uint64_t fs_cnt = 0;2045uint64_t ss_cnt = 0;20462047if (dsl_dir_is_zapified(dd)) {2048int err;20492050err = zap_lookup(os, dd->dd_object,2051DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,2052&fs_cnt);2053if (err != ENOENT && err != 0) {2054dsl_dir_rele(newparent, FTAG);2055dsl_dir_rele(dd, FTAG);2056return (err);2057}20582059/*2060* have to add 1 for the filesystem itself that we're2061* moving2062*/2063fs_cnt++;20642065err = zap_lookup(os, dd->dd_object,2066DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,2067&ss_cnt);2068if (err != ENOENT && err != 0) {2069dsl_dir_rele(newparent, FTAG);2070dsl_dir_rele(dd, FTAG);2071return (err);2072}2073}20742075/* check for encryption errors */2076error = dsl_dir_rename_crypt_check(dd, newparent);2077if (error != 0) {2078dsl_dir_rele(newparent, FTAG);2079dsl_dir_rele(dd, FTAG);2080return (SET_ERROR(EACCES));2081}20822083/* no rename into our descendant */2084if (closest_common_ancestor(dd, newparent) == dd) {2085dsl_dir_rele(newparent, FTAG);2086dsl_dir_rele(dd, FTAG);2087return (SET_ERROR(EINVAL));2088}20892090error = dsl_dir_transfer_possible(dd->dd_parent,2091newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred);2092if (error != 0) {2093dsl_dir_rele(newparent, FTAG);2094dsl_dir_rele(dd, FTAG);2095return (error);2096}2097}20982099dsl_dir_rele(newparent, FTAG);2100dsl_dir_rele(dd, FTAG);2101return (0);2102}21032104static void2105dsl_dir_rename_sync(void *arg, dmu_tx_t *tx)2106{2107dsl_dir_rename_arg_t *ddra = arg;2108dsl_pool_t *dp = dmu_tx_pool(tx);2109dsl_dir_t *dd, *newparent;2110const char *mynewname;2111objset_t *mos = dp->dp_meta_objset;21122113VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL));2114VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent,2115&mynewname));21162117ASSERT3P(mynewname, !=, NULL);21182119/* Log this before we change the name. */2120spa_history_log_internal_dd(dd, "rename", tx,2121"-> %s", ddra->ddra_newname);21222123if (newparent != dd->dd_parent) {2124objset_t *os = dd->dd_pool->dp_meta_objset;2125uint64_t fs_cnt = 0;2126uint64_t ss_cnt = 0;21272128/*2129* We already made sure the dd counts were initialized in the2130* check function.2131*/2132if (spa_feature_is_active(dp->dp_spa,2133SPA_FEATURE_FS_SS_LIMIT)) {2134VERIFY0(zap_lookup(os, dd->dd_object,2135DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1,2136&fs_cnt));2137/* add 1 for the filesystem itself that we're moving */2138fs_cnt++;21392140VERIFY0(zap_lookup(os, dd->dd_object,2141DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1,2142&ss_cnt));2143}21442145dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt,2146DD_FIELD_FILESYSTEM_COUNT, tx);2147dsl_fs_ss_count_adjust(newparent, fs_cnt,2148DD_FIELD_FILESYSTEM_COUNT, tx);21492150dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt,2151DD_FIELD_SNAPSHOT_COUNT, tx);2152dsl_fs_ss_count_adjust(newparent, ss_cnt,2153DD_FIELD_SNAPSHOT_COUNT, tx);21542155dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD,2156-dsl_dir_phys(dd)->dd_used_bytes,2157-dsl_dir_phys(dd)->dd_compressed_bytes,2158-dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);2159dsl_dir_diduse_space(newparent, DD_USED_CHILD,2160dsl_dir_phys(dd)->dd_used_bytes,2161dsl_dir_phys(dd)->dd_compressed_bytes,2162dsl_dir_phys(dd)->dd_uncompressed_bytes, tx);21632164if (dsl_dir_phys(dd)->dd_reserved >2165dsl_dir_phys(dd)->dd_used_bytes) {2166uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved -2167dsl_dir_phys(dd)->dd_used_bytes;21682169dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV,2170-unused_rsrv, 0, 0, tx);2171dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV,2172unused_rsrv, 0, 0, tx);2173}2174}21752176dmu_buf_will_dirty(dd->dd_dbuf, tx);21772178/* remove from old parent zapobj */2179VERIFY0(zap_remove(mos,2180dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj,2181dd->dd_myname, tx));21822183(void) strlcpy(dd->dd_myname, mynewname,2184sizeof (dd->dd_myname));2185dsl_dir_rele(dd->dd_parent, dd);2186dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object;2187VERIFY0(dsl_dir_hold_obj(dp,2188newparent->dd_object, NULL, dd, &dd->dd_parent));21892190/* add to new parent zapobj */2191VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj,2192dd->dd_myname, 8, 1, &dd->dd_object, tx));21932194/* TODO: A rename callback to avoid these layering violations. */2195zfsvfs_update_fromname(ddra->ddra_oldname, ddra->ddra_newname);2196zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname,2197ddra->ddra_newname, B_TRUE);21982199dsl_prop_notify_all(dd);22002201dsl_dir_rele(newparent, FTAG);2202dsl_dir_rele(dd, FTAG);2203}22042205int2206dsl_dir_rename(const char *oldname, const char *newname)2207{2208cred_t *cr = CRED();2209crhold(cr);22102211dsl_dir_rename_arg_t ddra;22122213ddra.ddra_oldname = oldname;2214ddra.ddra_newname = newname;2215ddra.ddra_cred = cr;22162217int err = dsl_sync_task(oldname,2218dsl_dir_rename_check, dsl_dir_rename_sync, &ddra,22193, ZFS_SPACE_CHECK_RESERVED);22202221crfree(cr);2222return (err);2223}22242225int2226dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,2227uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space,2228cred_t *cr)2229{2230dsl_dir_t *ancestor;2231int64_t adelta;2232uint64_t avail;2233int err;22342235ancestor = closest_common_ancestor(sdd, tdd);2236adelta = would_change(sdd, -space, ancestor);2237avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE);2238if (avail < space)2239return (SET_ERROR(ENOSPC));22402241err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT,2242ancestor, cr);2243if (err != 0)2244return (err);2245err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT,2246ancestor, cr);2247if (err != 0)2248return (err);22492250return (0);2251}22522253inode_timespec_t2254dsl_dir_snap_cmtime(dsl_dir_t *dd)2255{2256inode_timespec_t t;22572258mutex_enter(&dd->dd_lock);2259t = dd->dd_snap_cmtime;2260mutex_exit(&dd->dd_lock);22612262return (t);2263}22642265void2266dsl_dir_snap_cmtime_update(dsl_dir_t *dd, dmu_tx_t *tx)2267{2268dsl_pool_t *dp = dmu_tx_pool(tx);2269inode_timespec_t t;2270gethrestime(&t);22712272mutex_enter(&dd->dd_lock);2273dd->dd_snap_cmtime = t;2274if (spa_feature_is_enabled(dp->dp_spa,2275SPA_FEATURE_EXTENSIBLE_DATASET)) {2276objset_t *mos = dd->dd_pool->dp_meta_objset;2277uint64_t ddobj = dd->dd_object;2278dsl_dir_zapify(dd, tx);2279VERIFY0(zap_update(mos, ddobj,2280DD_FIELD_SNAPSHOTS_CHANGED,2281sizeof (uint64_t),2282sizeof (inode_timespec_t) / sizeof (uint64_t),2283&t, tx));2284}2285mutex_exit(&dd->dd_lock);2286}22872288void2289dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx)2290{2291objset_t *mos = dd->dd_pool->dp_meta_objset;2292dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx);2293}22942295boolean_t2296dsl_dir_is_zapified(dsl_dir_t *dd)2297{2298dmu_object_info_t doi;22992300dmu_object_info_from_db(dd->dd_dbuf, &doi);2301return (doi.doi_type == DMU_OTN_ZAP_METADATA);2302}23032304int2305dsl_dir_livelist_open(dsl_dir_t *dd, uint64_t obj)2306{2307objset_t *mos = dd->dd_pool->dp_meta_objset;2308ASSERT(spa_feature_is_active(dd->dd_pool->dp_spa,2309SPA_FEATURE_LIVELIST));2310int err = dsl_deadlist_open(&dd->dd_livelist, mos, obj);2311if (err != 0)2312return (err);2313bplist_create(&dd->dd_pending_allocs);2314bplist_create(&dd->dd_pending_frees);2315return (0);2316}23172318void2319dsl_dir_livelist_close(dsl_dir_t *dd)2320{2321dsl_deadlist_close(&dd->dd_livelist);2322bplist_destroy(&dd->dd_pending_allocs);2323bplist_destroy(&dd->dd_pending_frees);2324}23252326void2327dsl_dir_remove_livelist(dsl_dir_t *dd, dmu_tx_t *tx, boolean_t total)2328{2329uint64_t obj;2330dsl_pool_t *dp = dmu_tx_pool(tx);2331spa_t *spa = dp->dp_spa;2332livelist_condense_entry_t to_condense = spa->spa_to_condense;23332334if (!dsl_deadlist_is_open(&dd->dd_livelist))2335return;23362337/*2338* If the livelist being removed is set to be condensed, stop the2339* condense zthr and indicate the cancellation in the spa_to_condense2340* struct in case the condense no-wait synctask has already started2341*/2342zthr_t *ll_condense_thread = spa->spa_livelist_condense_zthr;2343if (ll_condense_thread != NULL &&2344(to_condense.ds != NULL) && (to_condense.ds->ds_dir == dd)) {2345/*2346* We use zthr_wait_cycle_done instead of zthr_cancel2347* because we don't want to destroy the zthr, just have2348* it skip its current task.2349*/2350spa->spa_to_condense.cancelled = B_TRUE;2351zthr_wait_cycle_done(ll_condense_thread);2352/*2353* If we've returned from zthr_wait_cycle_done without2354* clearing the to_condense data structure it's either2355* because the no-wait synctask has started (which is2356* indicated by 'syncing' field of to_condense) and we2357* can expect it to clear to_condense on its own.2358* Otherwise, we returned before the zthr ran. The2359* checkfunc will now fail as cancelled == B_TRUE so we2360* can safely NULL out ds, allowing a different dir's2361* livelist to be condensed.2362*2363* We can be sure that the to_condense struct will not2364* be repopulated at this stage because both this2365* function and dsl_livelist_try_condense execute in2366* syncing context.2367*/2368if ((spa->spa_to_condense.ds != NULL) &&2369!spa->spa_to_condense.syncing) {2370dmu_buf_rele(spa->spa_to_condense.ds->ds_dbuf,2371spa);2372spa->spa_to_condense.ds = NULL;2373}2374}23752376dsl_dir_livelist_close(dd);2377VERIFY0(zap_lookup(dp->dp_meta_objset, dd->dd_object,2378DD_FIELD_LIVELIST, sizeof (uint64_t), 1, &obj));2379VERIFY0(zap_remove(dp->dp_meta_objset, dd->dd_object,2380DD_FIELD_LIVELIST, tx));2381if (total) {2382dsl_deadlist_free(dp->dp_meta_objset, obj, tx);2383spa_feature_decr(spa, SPA_FEATURE_LIVELIST, tx);2384}2385}23862387static int2388dsl_dir_activity_in_progress(dsl_dir_t *dd, dsl_dataset_t *ds,2389zfs_wait_activity_t activity, boolean_t *in_progress)2390{2391int error = 0;23922393ASSERT(MUTEX_HELD(&dd->dd_activity_lock));23942395switch (activity) {2396case ZFS_WAIT_DELETEQ: {2397#ifdef _KERNEL2398objset_t *os;2399error = dmu_objset_from_ds(ds, &os);2400if (error != 0)2401break;24022403mutex_enter(&os->os_user_ptr_lock);2404void *user = dmu_objset_get_user(os);2405mutex_exit(&os->os_user_ptr_lock);2406if (dmu_objset_type(os) != DMU_OST_ZFS ||2407user == NULL || zfs_get_vfs_flag_unmounted(os)) {2408*in_progress = B_FALSE;2409return (0);2410}24112412uint64_t readonly = B_FALSE;2413error = zfs_get_temporary_prop(ds, ZFS_PROP_READONLY, &readonly,2414NULL);24152416if (error != 0)2417break;24182419if (readonly || !spa_writeable(dd->dd_pool->dp_spa)) {2420*in_progress = B_FALSE;2421return (0);2422}24232424uint64_t count, unlinked_obj;2425error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,2426&unlinked_obj);2427if (error != 0) {2428dsl_dataset_rele(ds, FTAG);2429break;2430}2431error = zap_count(os, unlinked_obj, &count);24322433if (error == 0)2434*in_progress = (count != 0);2435break;2436#else2437/*2438* The delete queue is ZPL specific, and libzpool doesn't have2439* it. It doesn't make sense to wait for it.2440*/2441(void) ds;2442*in_progress = B_FALSE;2443break;2444#endif2445}2446default:2447panic("unrecognized value for activity %d", activity);2448}24492450return (error);2451}24522453int2454dsl_dir_wait(dsl_dir_t *dd, dsl_dataset_t *ds, zfs_wait_activity_t activity,2455boolean_t *waited)2456{2457int error = 0;2458boolean_t in_progress;2459dsl_pool_t *dp = dd->dd_pool;2460for (;;) {2461dsl_pool_config_enter(dp, FTAG);2462error = dsl_dir_activity_in_progress(dd, ds, activity,2463&in_progress);2464dsl_pool_config_exit(dp, FTAG);2465if (error != 0 || !in_progress)2466break;24672468*waited = B_TRUE;24692470if (cv_wait_sig(&dd->dd_activity_cv, &dd->dd_activity_lock) ==24710 || dd->dd_activity_cancelled) {2472error = SET_ERROR(EINTR);2473break;2474}2475}2476return (error);2477}24782479void2480dsl_dir_cancel_waiters(dsl_dir_t *dd)2481{2482mutex_enter(&dd->dd_activity_lock);2483dd->dd_activity_cancelled = B_TRUE;2484cv_broadcast(&dd->dd_activity_cv);2485while (dd->dd_activity_waiters > 0)2486cv_wait(&dd->dd_activity_cv, &dd->dd_activity_lock);2487mutex_exit(&dd->dd_activity_lock);2488}24892490#if defined(_KERNEL)2491EXPORT_SYMBOL(dsl_dir_set_quota);2492EXPORT_SYMBOL(dsl_dir_set_reservation);2493#endif24942495ZFS_MODULE_PARAM(zfs, , zvol_enforce_quotas, INT, ZMOD_RW,2496"Enable strict ZVOL quota enforcment");249724982499