1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 223f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23be6fd75aSMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved. 24013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved. 25a2afb611SJerry Jelinek * Copyright (c) 2014 Joyent, Inc. All rights reserved. 26fa9e4066Sahrens */ 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/dmu.h> 29a9799022Sck #include <sys/dmu_objset.h> 30fa9e4066Sahrens #include <sys/dmu_tx.h> 31fa9e4066Sahrens #include <sys/dsl_dataset.h> 32fa9e4066Sahrens #include <sys/dsl_dir.h> 33fa9e4066Sahrens #include <sys/dsl_prop.h> 341d452cf5Sahrens #include <sys/dsl_synctask.h> 35ecd6cf80Smarks #include <sys/dsl_deleg.h> 362acef22dSMatthew Ahrens #include <sys/dmu_impl.h> 37fa9e4066Sahrens #include <sys/spa.h> 38b24ab676SJeff Bonwick #include <sys/metaslab.h> 39fa9e4066Sahrens #include <sys/zap.h> 40fa9e4066Sahrens #include <sys/zio.h> 41fa9e4066Sahrens #include <sys/arc.h> 42ecd6cf80Smarks #include <sys/sunddi.h> 43a2afb611SJerry Jelinek #include <sys/zfeature.h> 44a2afb611SJerry Jelinek #include <sys/policy.h> 45a2afb611SJerry Jelinek #include <sys/zfs_znode.h> 46fa9e4066Sahrens #include "zfs_namecheck.h" 47a2afb611SJerry Jelinek #include "zfs_prop.h" 48a2afb611SJerry Jelinek 49a2afb611SJerry Jelinek /* 50a2afb611SJerry Jelinek * Filesystem and Snapshot Limits 51a2afb611SJerry Jelinek * ------------------------------ 52a2afb611SJerry Jelinek * 53a2afb611SJerry Jelinek * These limits are used to restrict the number of filesystems and/or snapshots 54a2afb611SJerry Jelinek * that can be created at a given level in the tree or below. A typical 55a2afb611SJerry Jelinek * use-case is with a delegated dataset where the administrator wants to ensure 56a2afb611SJerry Jelinek * that a user within the zone is not creating too many additional filesystems 57a2afb611SJerry Jelinek * or snapshots, even though they're not exceeding their space quota. 58a2afb611SJerry Jelinek * 59a2afb611SJerry Jelinek * The filesystem and snapshot counts are stored as extensible properties. This 60a2afb611SJerry Jelinek * capability is controlled by a feature flag and must be enabled to be used. 61a2afb611SJerry Jelinek * Once enabled, the feature is not active until the first limit is set. At 62a2afb611SJerry Jelinek * that point, future operations to create/destroy filesystems or snapshots 63a2afb611SJerry Jelinek * will validate and update the counts. 64a2afb611SJerry Jelinek * 65a2afb611SJerry Jelinek * Because the count properties will not exist before the feature is active, 66a2afb611SJerry Jelinek * the counts are updated when a limit is first set on an uninitialized 67a2afb611SJerry Jelinek * dsl_dir node in the tree (The filesystem/snapshot count on a node includes 68a2afb611SJerry Jelinek * all of the nested filesystems/snapshots. Thus, a new leaf node has a 69a2afb611SJerry Jelinek * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and 70a2afb611SJerry Jelinek * snapshot count properties on a node indicate uninitialized counts on that 71a2afb611SJerry Jelinek * node.) When first setting a limit on an uninitialized node, the code starts 72a2afb611SJerry Jelinek * at the filesystem with the new limit and descends into all sub-filesystems 73a2afb611SJerry Jelinek * to add the count properties. 74a2afb611SJerry Jelinek * 75a2afb611SJerry Jelinek * In practice this is lightweight since a limit is typically set when the 76a2afb611SJerry Jelinek * filesystem is created and thus has no children. Once valid, changing the 77a2afb611SJerry Jelinek * limit value won't require a re-traversal since the counts are already valid. 78a2afb611SJerry Jelinek * When recursively fixing the counts, if a node with a limit is encountered 79a2afb611SJerry Jelinek * during the descent, the counts are known to be valid and there is no need to 80a2afb611SJerry Jelinek * descend into that filesystem's children. The counts on filesystems above the 81a2afb611SJerry Jelinek * one with the new limit will still be uninitialized, unless a limit is 82a2afb611SJerry Jelinek * eventually set on one of those filesystems. The counts are always recursively 83a2afb611SJerry Jelinek * updated when a limit is set on a dataset, unless there is already a limit. 84a2afb611SJerry Jelinek * When a new limit value is set on a filesystem with an existing limit, it is 85a2afb611SJerry Jelinek * possible for the new limit to be less than the current count at that level 86a2afb611SJerry Jelinek * since a user who can change the limit is also allowed to exceed the limit. 87a2afb611SJerry Jelinek * 88a2afb611SJerry Jelinek * Once the feature is active, then whenever a filesystem or snapshot is 89a2afb611SJerry Jelinek * created, the code recurses up the tree, validating the new count against the 90a2afb611SJerry Jelinek * limit at each initialized level. In practice, most levels will not have a 91a2afb611SJerry Jelinek * limit set. If there is a limit at any initialized level up the tree, the 92a2afb611SJerry Jelinek * check must pass or the creation will fail. Likewise, when a filesystem or 93a2afb611SJerry Jelinek * snapshot is destroyed, the counts are recursively adjusted all the way up 94a2afb611SJerry Jelinek * the initizized nodes in the tree. Renaming a filesystem into different point 95a2afb611SJerry Jelinek * in the tree will first validate, then update the counts on each branch up to 96a2afb611SJerry Jelinek * the common ancestor. A receive will also validate the counts and then update 97a2afb611SJerry Jelinek * them. 98a2afb611SJerry Jelinek * 99a2afb611SJerry Jelinek * An exception to the above behavior is that the limit is not enforced if the 100a2afb611SJerry Jelinek * user has permission to modify the limit. This is primarily so that 101a2afb611SJerry Jelinek * recursive snapshots in the global zone always work. We want to prevent a 102a2afb611SJerry Jelinek * denial-of-service in which a lower level delegated dataset could max out its 103a2afb611SJerry Jelinek * limit and thus block recursive snapshots from being taken in the global zone. 104a2afb611SJerry Jelinek * Because of this, it is possible for the snapshot count to be over the limit 105a2afb611SJerry Jelinek * and snapshots taken in the global zone could cause a lower level dataset to 106a2afb611SJerry Jelinek * hit or exceed its limit. The administrator taking the global zone recursive 107a2afb611SJerry Jelinek * snapshot should be aware of this side-effect and behave accordingly. 108a2afb611SJerry Jelinek * For consistency, the filesystem limit is also not enforced if the user can 109a2afb611SJerry Jelinek * modify the limit. 110a2afb611SJerry Jelinek * 111a2afb611SJerry Jelinek * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check() 112a2afb611SJerry Jelinek * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in 113a2afb611SJerry Jelinek * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by 114a2afb611SJerry Jelinek * dsl_dir_init_fs_ss_count(). 115a2afb611SJerry Jelinek * 116a2afb611SJerry Jelinek * There is a special case when we receive a filesystem that already exists. In 117a2afb611SJerry Jelinek * this case a temporary clone name of %X is created (see dmu_recv_begin). We 118a2afb611SJerry Jelinek * never update the filesystem counts for temporary clones. 119a2afb611SJerry Jelinek * 120a2afb611SJerry Jelinek * Likewise, we do not update the snapshot counts for temporary snapshots, 121a2afb611SJerry Jelinek * such as those created by zfs diff. 122a2afb611SJerry Jelinek */ 123fa9e4066Sahrens 124a9799022Sck static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); 125fa9e4066Sahrens 126fa9e4066Sahrens /* ARGSUSED */ 127fa9e4066Sahrens static void 128fa9e4066Sahrens dsl_dir_evict(dmu_buf_t *db, void *arg) 129fa9e4066Sahrens { 130fa9e4066Sahrens dsl_dir_t *dd = arg; 131fa9e4066Sahrens dsl_pool_t *dp = dd->dd_pool; 132fa9e4066Sahrens int t; 133fa9e4066Sahrens 134fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 135fa9e4066Sahrens ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); 136fa9e4066Sahrens ASSERT(dd->dd_tempreserved[t] == 0); 137fa9e4066Sahrens ASSERT(dd->dd_space_towrite[t] == 0); 138fa9e4066Sahrens } 139fa9e4066Sahrens 140fa9e4066Sahrens if (dd->dd_parent) 1413b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd); 142fa9e4066Sahrens 143fa9e4066Sahrens spa_close(dd->dd_pool->dp_spa, dd); 144fa9e4066Sahrens 145fa9e4066Sahrens /* 1463f9d6ad7SLin Ling * The props callback list should have been cleaned up by 1473f9d6ad7SLin Ling * objset_evict(). 148fa9e4066Sahrens */ 149fa9e4066Sahrens list_destroy(&dd->dd_prop_cbs); 1505ad82045Snd mutex_destroy(&dd->dd_lock); 151fa9e4066Sahrens kmem_free(dd, sizeof (dsl_dir_t)); 152fa9e4066Sahrens } 153fa9e4066Sahrens 154ea8dc4b6Seschrock int 1553b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj, 156ea8dc4b6Seschrock const char *tail, void *tag, dsl_dir_t **ddp) 157fa9e4066Sahrens { 158fa9e4066Sahrens dmu_buf_t *dbuf; 159fa9e4066Sahrens dsl_dir_t *dd; 160ea8dc4b6Seschrock int err; 161fa9e4066Sahrens 1623b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 163fa9e4066Sahrens 164ea8dc4b6Seschrock err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); 1653b2aab18SMatthew Ahrens if (err != 0) 166ea8dc4b6Seschrock return (err); 167fa9e4066Sahrens dd = dmu_buf_get_user(dbuf); 168fa9e4066Sahrens #ifdef ZFS_DEBUG 169fa9e4066Sahrens { 170fa9e4066Sahrens dmu_object_info_t doi; 171fa9e4066Sahrens dmu_object_info_from_db(dbuf, &doi); 1722acef22dSMatthew Ahrens ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR); 17374e7dc98SMatthew Ahrens ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); 174fa9e4066Sahrens } 175fa9e4066Sahrens #endif 176fa9e4066Sahrens if (dd == NULL) { 177fa9e4066Sahrens dsl_dir_t *winner; 178fa9e4066Sahrens 179fa9e4066Sahrens dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); 180fa9e4066Sahrens dd->dd_object = ddobj; 181fa9e4066Sahrens dd->dd_dbuf = dbuf; 182fa9e4066Sahrens dd->dd_pool = dp; 183fa9e4066Sahrens dd->dd_phys = dbuf->db_data; 1845ad82045Snd mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); 185fa9e4066Sahrens 186fa9e4066Sahrens list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t), 187fa9e4066Sahrens offsetof(dsl_prop_cb_record_t, cbr_node)); 188fa9e4066Sahrens 18971eb0538SChris Kirby dsl_dir_snap_cmtime_update(dd); 19071eb0538SChris Kirby 191fa9e4066Sahrens if (dd->dd_phys->dd_parent_obj) { 1923b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, dd->dd_phys->dd_parent_obj, 193ea8dc4b6Seschrock NULL, dd, &dd->dd_parent); 1943b2aab18SMatthew Ahrens if (err != 0) 19574e7dc98SMatthew Ahrens goto errout; 196fa9e4066Sahrens if (tail) { 197fa9e4066Sahrens #ifdef ZFS_DEBUG 198fa9e4066Sahrens uint64_t foundobj; 199fa9e4066Sahrens 200fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, 201e7437265Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, 202fa9e4066Sahrens tail, sizeof (foundobj), 1, &foundobj); 203ea8dc4b6Seschrock ASSERT(err || foundobj == ddobj); 204fa9e4066Sahrens #endif 205fa9e4066Sahrens (void) strcpy(dd->dd_myname, tail); 206fa9e4066Sahrens } else { 207fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 208e7437265Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, 209e7437265Sahrens ddobj, 0, dd->dd_myname); 210ea8dc4b6Seschrock } 2113b2aab18SMatthew Ahrens if (err != 0) 21274e7dc98SMatthew Ahrens goto errout; 213fa9e4066Sahrens } else { 214fa9e4066Sahrens (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); 215fa9e4066Sahrens } 216fa9e4066Sahrens 2173f9d6ad7SLin Ling if (dsl_dir_is_clone(dd)) { 2183f9d6ad7SLin Ling dmu_buf_t *origin_bonus; 2193f9d6ad7SLin Ling dsl_dataset_phys_t *origin_phys; 2203f9d6ad7SLin Ling 2213f9d6ad7SLin Ling /* 2223f9d6ad7SLin Ling * We can't open the origin dataset, because 2233f9d6ad7SLin Ling * that would require opening this dsl_dir. 2243f9d6ad7SLin Ling * Just look at its phys directly instead. 2253f9d6ad7SLin Ling */ 2263f9d6ad7SLin Ling err = dmu_bonus_hold(dp->dp_meta_objset, 2273f9d6ad7SLin Ling dd->dd_phys->dd_origin_obj, FTAG, &origin_bonus); 2283b2aab18SMatthew Ahrens if (err != 0) 2293f9d6ad7SLin Ling goto errout; 2303f9d6ad7SLin Ling origin_phys = origin_bonus->db_data; 2313f9d6ad7SLin Ling dd->dd_origin_txg = 2323f9d6ad7SLin Ling origin_phys->ds_creation_txg; 2333f9d6ad7SLin Ling dmu_buf_rele(origin_bonus, FTAG); 2343f9d6ad7SLin Ling } 2353f9d6ad7SLin Ling 236fa9e4066Sahrens winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys, 237fa9e4066Sahrens dsl_dir_evict); 238fa9e4066Sahrens if (winner) { 239fa9e4066Sahrens if (dd->dd_parent) 2403b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd); 2415ad82045Snd mutex_destroy(&dd->dd_lock); 242fa9e4066Sahrens kmem_free(dd, sizeof (dsl_dir_t)); 243fa9e4066Sahrens dd = winner; 244fa9e4066Sahrens } else { 245fa9e4066Sahrens spa_open_ref(dp->dp_spa, dd); 246fa9e4066Sahrens } 247fa9e4066Sahrens } 248fa9e4066Sahrens 249fa9e4066Sahrens /* 250fa9e4066Sahrens * The dsl_dir_t has both open-to-close and instantiate-to-evict 251fa9e4066Sahrens * holds on the spa. We need the open-to-close holds because 252fa9e4066Sahrens * otherwise the spa_refcnt wouldn't change when we open a 253fa9e4066Sahrens * dir which the spa also has open, so we could incorrectly 254fa9e4066Sahrens * think it was OK to unload/export/destroy the pool. We need 255fa9e4066Sahrens * the instantiate-to-evict hold because the dsl_dir_t has a 256fa9e4066Sahrens * pointer to the dd_pool, which has a pointer to the spa_t. 257fa9e4066Sahrens */ 258fa9e4066Sahrens spa_open_ref(dp->dp_spa, tag); 259fa9e4066Sahrens ASSERT3P(dd->dd_pool, ==, dp); 260fa9e4066Sahrens ASSERT3U(dd->dd_object, ==, ddobj); 261fa9e4066Sahrens ASSERT3P(dd->dd_dbuf, ==, dbuf); 262ea8dc4b6Seschrock *ddp = dd; 263ea8dc4b6Seschrock return (0); 26474e7dc98SMatthew Ahrens 26574e7dc98SMatthew Ahrens errout: 26674e7dc98SMatthew Ahrens if (dd->dd_parent) 2673b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd); 26874e7dc98SMatthew Ahrens mutex_destroy(&dd->dd_lock); 26974e7dc98SMatthew Ahrens kmem_free(dd, sizeof (dsl_dir_t)); 27074e7dc98SMatthew Ahrens dmu_buf_rele(dbuf, tag); 27174e7dc98SMatthew Ahrens return (err); 272fa9e4066Sahrens } 273fa9e4066Sahrens 274fa9e4066Sahrens void 2753b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag) 276fa9e4066Sahrens { 277fa9e4066Sahrens dprintf_dd(dd, "%s\n", ""); 278fa9e4066Sahrens spa_close(dd->dd_pool->dp_spa, tag); 279ea8dc4b6Seschrock dmu_buf_rele(dd->dd_dbuf, tag); 280fa9e4066Sahrens } 281fa9e4066Sahrens 28215f66a7fSek /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */ 283fa9e4066Sahrens void 284fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf) 285fa9e4066Sahrens { 286fa9e4066Sahrens if (dd->dd_parent) { 287fa9e4066Sahrens dsl_dir_name(dd->dd_parent, buf); 288fa9e4066Sahrens (void) strcat(buf, "/"); 289fa9e4066Sahrens } else { 290fa9e4066Sahrens buf[0] = '\0'; 291fa9e4066Sahrens } 292fa9e4066Sahrens if (!MUTEX_HELD(&dd->dd_lock)) { 293fa9e4066Sahrens /* 294fa9e4066Sahrens * recursive mutex so that we can use 295fa9e4066Sahrens * dprintf_dd() with dd_lock held 296fa9e4066Sahrens */ 297fa9e4066Sahrens mutex_enter(&dd->dd_lock); 298fa9e4066Sahrens (void) strcat(buf, dd->dd_myname); 299fa9e4066Sahrens mutex_exit(&dd->dd_lock); 300fa9e4066Sahrens } else { 301fa9e4066Sahrens (void) strcat(buf, dd->dd_myname); 302fa9e4066Sahrens } 303fa9e4066Sahrens } 304fa9e4066Sahrens 305ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */ 306b7661cccSmmusante int 307b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd) 308b7661cccSmmusante { 309b7661cccSmmusante int result = 0; 310b7661cccSmmusante 311b7661cccSmmusante if (dd->dd_parent) { 312b7661cccSmmusante /* parent's name + 1 for the "/" */ 313b7661cccSmmusante result = dsl_dir_namelen(dd->dd_parent) + 1; 314b7661cccSmmusante } 315b7661cccSmmusante 316b7661cccSmmusante if (!MUTEX_HELD(&dd->dd_lock)) { 317b7661cccSmmusante /* see dsl_dir_name */ 318b7661cccSmmusante mutex_enter(&dd->dd_lock); 319b7661cccSmmusante result += strlen(dd->dd_myname); 320b7661cccSmmusante mutex_exit(&dd->dd_lock); 321b7661cccSmmusante } else { 322b7661cccSmmusante result += strlen(dd->dd_myname); 323b7661cccSmmusante } 324b7661cccSmmusante 325b7661cccSmmusante return (result); 326b7661cccSmmusante } 327b7661cccSmmusante 328fa9e4066Sahrens static int 329fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp) 330fa9e4066Sahrens { 331fa9e4066Sahrens char *p; 3323b2aab18SMatthew Ahrens 333ccba0801SRich Morris if ((path == NULL) || (path[0] == '\0')) 334be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 335fa9e4066Sahrens /* This would be a good place to reserve some namespace... */ 336fa9e4066Sahrens p = strpbrk(path, "/@"); 337fa9e4066Sahrens if (p && (p[1] == '/' || p[1] == '@')) { 338fa9e4066Sahrens /* two separators in a row */ 339be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 340fa9e4066Sahrens } 341fa9e4066Sahrens if (p == NULL || p == path) { 342fa9e4066Sahrens /* 343fa9e4066Sahrens * if the first thing is an @ or /, it had better be an 344fa9e4066Sahrens * @ and it had better not have any more ats or slashes, 345fa9e4066Sahrens * and it had better have something after the @. 346fa9e4066Sahrens */ 347fa9e4066Sahrens if (p != NULL && 348fa9e4066Sahrens (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) 349be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 350fa9e4066Sahrens if (strlen(path) >= MAXNAMELEN) 351be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 352fa9e4066Sahrens (void) strcpy(component, path); 353fa9e4066Sahrens p = NULL; 354fa9e4066Sahrens } else if (p[0] == '/') { 3553b2aab18SMatthew Ahrens if (p - path >= MAXNAMELEN) 356be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 357fa9e4066Sahrens (void) strncpy(component, path, p - path); 3583b2aab18SMatthew Ahrens component[p - path] = '\0'; 359fa9e4066Sahrens p++; 360fa9e4066Sahrens } else if (p[0] == '@') { 361fa9e4066Sahrens /* 362fa9e4066Sahrens * if the next separator is an @, there better not be 363fa9e4066Sahrens * any more slashes. 364fa9e4066Sahrens */ 365fa9e4066Sahrens if (strchr(path, '/')) 366be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3673b2aab18SMatthew Ahrens if (p - path >= MAXNAMELEN) 368be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 369fa9e4066Sahrens (void) strncpy(component, path, p - path); 3703b2aab18SMatthew Ahrens component[p - path] = '\0'; 371fa9e4066Sahrens } else { 3723b2aab18SMatthew Ahrens panic("invalid p=%p", (void *)p); 373fa9e4066Sahrens } 374fa9e4066Sahrens *nextp = p; 375fa9e4066Sahrens return (0); 376fa9e4066Sahrens } 377fa9e4066Sahrens 378fa9e4066Sahrens /* 3793b2aab18SMatthew Ahrens * Return the dsl_dir_t, and possibly the last component which couldn't 3803b2aab18SMatthew Ahrens * be found in *tail. The name must be in the specified dsl_pool_t. This 3813b2aab18SMatthew Ahrens * thread must hold the dp_config_rwlock for the pool. Returns NULL if the 3823b2aab18SMatthew Ahrens * path is bogus, or if tail==NULL and we couldn't parse the whole name. 3833b2aab18SMatthew Ahrens * (*tail)[0] == '@' means that the last component is a snapshot. 384fa9e4066Sahrens */ 385ea8dc4b6Seschrock int 3863b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag, 387ea8dc4b6Seschrock dsl_dir_t **ddp, const char **tailp) 388fa9e4066Sahrens { 389fa9e4066Sahrens char buf[MAXNAMELEN]; 3903b2aab18SMatthew Ahrens const char *spaname, *next, *nextnext = NULL; 391fa9e4066Sahrens int err; 392fa9e4066Sahrens dsl_dir_t *dd; 393fa9e4066Sahrens uint64_t ddobj; 394fa9e4066Sahrens 395fa9e4066Sahrens err = getcomponent(name, buf, &next); 3963b2aab18SMatthew Ahrens if (err != 0) 397ea8dc4b6Seschrock return (err); 398fa9e4066Sahrens 3993b2aab18SMatthew Ahrens /* Make sure the name is in the specified pool. */ 4003b2aab18SMatthew Ahrens spaname = spa_name(dp->dp_spa); 4013b2aab18SMatthew Ahrens if (strcmp(buf, spaname) != 0) 402be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 403fa9e4066Sahrens 4043b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 405fa9e4066Sahrens 4063b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); 4073b2aab18SMatthew Ahrens if (err != 0) { 408ea8dc4b6Seschrock return (err); 409ea8dc4b6Seschrock } 410ea8dc4b6Seschrock 411fa9e4066Sahrens while (next != NULL) { 412fa9e4066Sahrens dsl_dir_t *child_ds; 413fa9e4066Sahrens err = getcomponent(next, buf, &nextnext); 4143b2aab18SMatthew Ahrens if (err != 0) 415ea8dc4b6Seschrock break; 416fa9e4066Sahrens ASSERT(next[0] != '\0'); 417fa9e4066Sahrens if (next[0] == '@') 418fa9e4066Sahrens break; 419fa9e4066Sahrens dprintf("looking up %s in obj%lld\n", 420fa9e4066Sahrens buf, dd->dd_phys->dd_child_dir_zapobj); 421fa9e4066Sahrens 422fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, 423fa9e4066Sahrens dd->dd_phys->dd_child_dir_zapobj, 424fa9e4066Sahrens buf, sizeof (ddobj), 1, &ddobj); 4253b2aab18SMatthew Ahrens if (err != 0) { 426ea8dc4b6Seschrock if (err == ENOENT) 427ea8dc4b6Seschrock err = 0; 428fa9e4066Sahrens break; 429fa9e4066Sahrens } 430fa9e4066Sahrens 4313b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_ds); 4323b2aab18SMatthew Ahrens if (err != 0) 433ea8dc4b6Seschrock break; 4343b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag); 435fa9e4066Sahrens dd = child_ds; 436fa9e4066Sahrens next = nextnext; 437fa9e4066Sahrens } 438fa9e4066Sahrens 4393b2aab18SMatthew Ahrens if (err != 0) { 4403b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag); 441ea8dc4b6Seschrock return (err); 442ea8dc4b6Seschrock } 443ea8dc4b6Seschrock 444fa9e4066Sahrens /* 445fa9e4066Sahrens * It's an error if there's more than one component left, or 446fa9e4066Sahrens * tailp==NULL and there's any component left. 447fa9e4066Sahrens */ 448fa9e4066Sahrens if (next != NULL && 449fa9e4066Sahrens (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { 450fa9e4066Sahrens /* bad path name */ 4513b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag); 452fa9e4066Sahrens dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); 453be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 454fa9e4066Sahrens } 4553b2aab18SMatthew Ahrens if (tailp != NULL) 456fa9e4066Sahrens *tailp = next; 457ea8dc4b6Seschrock *ddp = dd; 458ea8dc4b6Seschrock return (err); 459fa9e4066Sahrens } 460fa9e4066Sahrens 461a2afb611SJerry Jelinek /* 462a2afb611SJerry Jelinek * If the counts are already initialized for this filesystem and its 463a2afb611SJerry Jelinek * descendants then do nothing, otherwise initialize the counts. 464a2afb611SJerry Jelinek * 465a2afb611SJerry Jelinek * The counts on this filesystem, and those below, may be uninitialized due to 466a2afb611SJerry Jelinek * either the use of a pre-existing pool which did not support the 467a2afb611SJerry Jelinek * filesystem/snapshot limit feature, or one in which the feature had not yet 468a2afb611SJerry Jelinek * been enabled. 469a2afb611SJerry Jelinek * 470a2afb611SJerry Jelinek * Recursively descend the filesystem tree and update the filesystem/snapshot 471a2afb611SJerry Jelinek * counts on each filesystem below, then update the cumulative count on the 472a2afb611SJerry Jelinek * current filesystem. If the filesystem already has a count set on it, 473a2afb611SJerry Jelinek * then we know that its counts, and the counts on the filesystems below it, 474a2afb611SJerry Jelinek * are already correct, so we don't have to update this filesystem. 475a2afb611SJerry Jelinek */ 476a2afb611SJerry Jelinek static void 477a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx) 478a2afb611SJerry Jelinek { 479a2afb611SJerry Jelinek uint64_t my_fs_cnt = 0; 480a2afb611SJerry Jelinek uint64_t my_ss_cnt = 0; 481a2afb611SJerry Jelinek dsl_pool_t *dp = dd->dd_pool; 482a2afb611SJerry Jelinek objset_t *os = dp->dp_meta_objset; 483a2afb611SJerry Jelinek zap_cursor_t *zc; 484a2afb611SJerry Jelinek zap_attribute_t *za; 485a2afb611SJerry Jelinek dsl_dataset_t *ds; 486a2afb611SJerry Jelinek 487a2afb611SJerry Jelinek ASSERT(spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)); 488a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dp)); 489a2afb611SJerry Jelinek ASSERT(dmu_tx_is_syncing(tx)); 490a2afb611SJerry Jelinek 491a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx); 492a2afb611SJerry Jelinek 493a2afb611SJerry Jelinek /* 494a2afb611SJerry Jelinek * If the filesystem count has already been initialized then we 495a2afb611SJerry Jelinek * don't need to recurse down any further. 496a2afb611SJerry Jelinek */ 497a2afb611SJerry Jelinek if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0) 498a2afb611SJerry Jelinek return; 499a2afb611SJerry Jelinek 500a2afb611SJerry Jelinek zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 501a2afb611SJerry Jelinek za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 502a2afb611SJerry Jelinek 503a2afb611SJerry Jelinek /* Iterate my child dirs */ 504a2afb611SJerry Jelinek for (zap_cursor_init(zc, os, dd->dd_phys->dd_child_dir_zapobj); 505a2afb611SJerry Jelinek zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) { 506a2afb611SJerry Jelinek dsl_dir_t *chld_dd; 507a2afb611SJerry Jelinek uint64_t count; 508a2afb611SJerry Jelinek 509a2afb611SJerry Jelinek VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG, 510a2afb611SJerry Jelinek &chld_dd)); 511a2afb611SJerry Jelinek 512a2afb611SJerry Jelinek /* 513a2afb611SJerry Jelinek * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and 514a2afb611SJerry Jelinek * temporary datasets. 515a2afb611SJerry Jelinek */ 516a2afb611SJerry Jelinek if (chld_dd->dd_myname[0] == '$' || 517a2afb611SJerry Jelinek chld_dd->dd_myname[0] == '%') { 518a2afb611SJerry Jelinek dsl_dir_rele(chld_dd, FTAG); 519a2afb611SJerry Jelinek continue; 520a2afb611SJerry Jelinek } 521a2afb611SJerry Jelinek 522a2afb611SJerry Jelinek my_fs_cnt++; /* count this child */ 523a2afb611SJerry Jelinek 524a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(chld_dd, tx); 525a2afb611SJerry Jelinek 526a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, chld_dd->dd_object, 527a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count)); 528a2afb611SJerry Jelinek my_fs_cnt += count; 529a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, chld_dd->dd_object, 530a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count)); 531a2afb611SJerry Jelinek my_ss_cnt += count; 532a2afb611SJerry Jelinek 533a2afb611SJerry Jelinek dsl_dir_rele(chld_dd, FTAG); 534a2afb611SJerry Jelinek } 535a2afb611SJerry Jelinek zap_cursor_fini(zc); 536a2afb611SJerry Jelinek /* Count my snapshots (we counted children's snapshots above) */ 537a2afb611SJerry Jelinek VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 538a2afb611SJerry Jelinek dd->dd_phys->dd_head_dataset_obj, FTAG, &ds)); 539a2afb611SJerry Jelinek 540a2afb611SJerry Jelinek for (zap_cursor_init(zc, os, ds->ds_phys->ds_snapnames_zapobj); 541a2afb611SJerry Jelinek zap_cursor_retrieve(zc, za) == 0; 542a2afb611SJerry Jelinek zap_cursor_advance(zc)) { 543a2afb611SJerry Jelinek /* Don't count temporary snapshots */ 544a2afb611SJerry Jelinek if (za->za_name[0] != '%') 545a2afb611SJerry Jelinek my_ss_cnt++; 546a2afb611SJerry Jelinek } 547*fb7001f1SAlex Reece zap_cursor_fini(zc); 548a2afb611SJerry Jelinek 549a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 550a2afb611SJerry Jelinek 551a2afb611SJerry Jelinek kmem_free(zc, sizeof (zap_cursor_t)); 552a2afb611SJerry Jelinek kmem_free(za, sizeof (zap_attribute_t)); 553a2afb611SJerry Jelinek 554a2afb611SJerry Jelinek /* we're in a sync task, update counts */ 555a2afb611SJerry Jelinek dmu_buf_will_dirty(dd->dd_dbuf, tx); 556a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 557a2afb611SJerry Jelinek sizeof (my_fs_cnt), 1, &my_fs_cnt, tx)); 558a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 559a2afb611SJerry Jelinek sizeof (my_ss_cnt), 1, &my_ss_cnt, tx)); 560a2afb611SJerry Jelinek } 561a2afb611SJerry Jelinek 562a2afb611SJerry Jelinek static int 563a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx) 564a2afb611SJerry Jelinek { 565a2afb611SJerry Jelinek char *ddname = (char *)arg; 566a2afb611SJerry Jelinek dsl_pool_t *dp = dmu_tx_pool(tx); 567a2afb611SJerry Jelinek dsl_dataset_t *ds; 568a2afb611SJerry Jelinek dsl_dir_t *dd; 569a2afb611SJerry Jelinek int error; 570a2afb611SJerry Jelinek 571a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, ddname, FTAG, &ds); 572a2afb611SJerry Jelinek if (error != 0) 573a2afb611SJerry Jelinek return (error); 574a2afb611SJerry Jelinek 575a2afb611SJerry Jelinek if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 576a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 577a2afb611SJerry Jelinek return (SET_ERROR(ENOTSUP)); 578a2afb611SJerry Jelinek } 579a2afb611SJerry Jelinek 580a2afb611SJerry Jelinek dd = ds->ds_dir; 581a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) && 582a2afb611SJerry Jelinek dsl_dir_is_zapified(dd) && 583a2afb611SJerry Jelinek zap_contains(dp->dp_meta_objset, dd->dd_object, 584a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT) == 0) { 585a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 586a2afb611SJerry Jelinek return (SET_ERROR(EALREADY)); 587a2afb611SJerry Jelinek } 588a2afb611SJerry Jelinek 589a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 590a2afb611SJerry Jelinek return (0); 591a2afb611SJerry Jelinek } 592a2afb611SJerry Jelinek 593a2afb611SJerry Jelinek static void 594a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx) 595a2afb611SJerry Jelinek { 596a2afb611SJerry Jelinek char *ddname = (char *)arg; 597a2afb611SJerry Jelinek dsl_pool_t *dp = dmu_tx_pool(tx); 598a2afb611SJerry Jelinek dsl_dataset_t *ds; 599a2afb611SJerry Jelinek spa_t *spa; 600a2afb611SJerry Jelinek 601a2afb611SJerry Jelinek VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds)); 602a2afb611SJerry Jelinek 603a2afb611SJerry Jelinek spa = dsl_dataset_get_spa(ds); 604a2afb611SJerry Jelinek 605a2afb611SJerry Jelinek if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) { 606a2afb611SJerry Jelinek /* 607a2afb611SJerry Jelinek * Since the feature was not active and we're now setting a 608a2afb611SJerry Jelinek * limit, increment the feature-active counter so that the 609a2afb611SJerry Jelinek * feature becomes active for the first time. 610a2afb611SJerry Jelinek * 611a2afb611SJerry Jelinek * We are already in a sync task so we can update the MOS. 612a2afb611SJerry Jelinek */ 613a2afb611SJerry Jelinek spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx); 614a2afb611SJerry Jelinek } 615a2afb611SJerry Jelinek 616a2afb611SJerry Jelinek /* 617a2afb611SJerry Jelinek * Since we are now setting a non-UINT64_MAX limit on the filesystem, 618a2afb611SJerry Jelinek * we need to ensure the counts are correct. Descend down the tree from 619a2afb611SJerry Jelinek * this point and update all of the counts to be accurate. 620a2afb611SJerry Jelinek */ 621a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(ds->ds_dir, tx); 622a2afb611SJerry Jelinek 623a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 624a2afb611SJerry Jelinek } 625a2afb611SJerry Jelinek 626a2afb611SJerry Jelinek /* 627a2afb611SJerry Jelinek * Make sure the feature is enabled and activate it if necessary. 628a2afb611SJerry Jelinek * Since we're setting a limit, ensure the on-disk counts are valid. 629a2afb611SJerry Jelinek * This is only called by the ioctl path when setting a limit value. 630a2afb611SJerry Jelinek * 631a2afb611SJerry Jelinek * We do not need to validate the new limit, since users who can change the 632a2afb611SJerry Jelinek * limit are also allowed to exceed the limit. 633a2afb611SJerry Jelinek */ 634a2afb611SJerry Jelinek int 635a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname) 636a2afb611SJerry Jelinek { 637a2afb611SJerry Jelinek int error; 638a2afb611SJerry Jelinek 639a2afb611SJerry Jelinek error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check, 640a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0); 641a2afb611SJerry Jelinek 642a2afb611SJerry Jelinek if (error == EALREADY) 643a2afb611SJerry Jelinek error = 0; 644a2afb611SJerry Jelinek 645a2afb611SJerry Jelinek return (error); 646a2afb611SJerry Jelinek } 647a2afb611SJerry Jelinek 648a2afb611SJerry Jelinek /* 649a2afb611SJerry Jelinek * Used to determine if the filesystem_limit or snapshot_limit should be 650a2afb611SJerry Jelinek * enforced. We allow the limit to be exceeded if the user has permission to 651a2afb611SJerry Jelinek * write the property value. We pass in the creds that we got in the open 652a2afb611SJerry Jelinek * context since we will always be the GZ root in syncing context. We also have 653a2afb611SJerry Jelinek * to handle the case where we are allowed to change the limit on the current 654a2afb611SJerry Jelinek * dataset, but there may be another limit in the tree above. 655a2afb611SJerry Jelinek * 656a2afb611SJerry Jelinek * We can never modify these two properties within a non-global zone. In 657a2afb611SJerry Jelinek * addition, the other checks are modeled on zfs_secpolicy_write_perms. We 658a2afb611SJerry Jelinek * can't use that function since we are already holding the dp_config_rwlock. 659a2afb611SJerry Jelinek * In addition, we already have the dd and dealing with snapshots is simplified 660a2afb611SJerry Jelinek * in this code. 661a2afb611SJerry Jelinek */ 662a2afb611SJerry Jelinek 663a2afb611SJerry Jelinek typedef enum { 664a2afb611SJerry Jelinek ENFORCE_ALWAYS, 665a2afb611SJerry Jelinek ENFORCE_NEVER, 666a2afb611SJerry Jelinek ENFORCE_ABOVE 667a2afb611SJerry Jelinek } enforce_res_t; 668a2afb611SJerry Jelinek 669a2afb611SJerry Jelinek static enforce_res_t 670a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr) 671a2afb611SJerry Jelinek { 672a2afb611SJerry Jelinek enforce_res_t enforce = ENFORCE_ALWAYS; 673a2afb611SJerry Jelinek uint64_t obj; 674a2afb611SJerry Jelinek dsl_dataset_t *ds; 675a2afb611SJerry Jelinek uint64_t zoned; 676a2afb611SJerry Jelinek 677a2afb611SJerry Jelinek ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 678a2afb611SJerry Jelinek prop == ZFS_PROP_SNAPSHOT_LIMIT); 679a2afb611SJerry Jelinek 680a2afb611SJerry Jelinek #ifdef _KERNEL 681a2afb611SJerry Jelinek if (crgetzoneid(cr) != GLOBAL_ZONEID) 682a2afb611SJerry Jelinek return (ENFORCE_ALWAYS); 683a2afb611SJerry Jelinek 684a2afb611SJerry Jelinek if (secpolicy_zfs(cr) == 0) 685a2afb611SJerry Jelinek return (ENFORCE_NEVER); 686a2afb611SJerry Jelinek #endif 687a2afb611SJerry Jelinek 688a2afb611SJerry Jelinek if ((obj = dd->dd_phys->dd_head_dataset_obj) == 0) 689a2afb611SJerry Jelinek return (ENFORCE_ALWAYS); 690a2afb611SJerry Jelinek 691a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool)); 692a2afb611SJerry Jelinek 693a2afb611SJerry Jelinek if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0) 694a2afb611SJerry Jelinek return (ENFORCE_ALWAYS); 695a2afb611SJerry Jelinek 696a2afb611SJerry Jelinek if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) { 697a2afb611SJerry Jelinek /* Only root can access zoned fs's from the GZ */ 698a2afb611SJerry Jelinek enforce = ENFORCE_ALWAYS; 699a2afb611SJerry Jelinek } else { 700a2afb611SJerry Jelinek if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0) 701a2afb611SJerry Jelinek enforce = ENFORCE_ABOVE; 702a2afb611SJerry Jelinek } 703a2afb611SJerry Jelinek 704a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 705a2afb611SJerry Jelinek return (enforce); 706a2afb611SJerry Jelinek } 707a2afb611SJerry Jelinek 708a2afb611SJerry Jelinek /* 709a2afb611SJerry Jelinek * Check if adding additional child filesystem(s) would exceed any filesystem 710a2afb611SJerry Jelinek * limits or adding additional snapshot(s) would exceed any snapshot limits. 711a2afb611SJerry Jelinek * The prop argument indicates which limit to check. 712a2afb611SJerry Jelinek * 713a2afb611SJerry Jelinek * Note that all filesystem limits up to the root (or the highest 714a2afb611SJerry Jelinek * initialized) filesystem or the given ancestor must be satisfied. 715a2afb611SJerry Jelinek */ 716a2afb611SJerry Jelinek int 717a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop, 718a2afb611SJerry Jelinek dsl_dir_t *ancestor, cred_t *cr) 719a2afb611SJerry Jelinek { 720a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 721a2afb611SJerry Jelinek uint64_t limit, count; 722a2afb611SJerry Jelinek char *count_prop; 723a2afb611SJerry Jelinek enforce_res_t enforce; 724a2afb611SJerry Jelinek int err = 0; 725a2afb611SJerry Jelinek 726a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool)); 727a2afb611SJerry Jelinek ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 728a2afb611SJerry Jelinek prop == ZFS_PROP_SNAPSHOT_LIMIT); 729a2afb611SJerry Jelinek 730a2afb611SJerry Jelinek /* 731a2afb611SJerry Jelinek * If we're allowed to change the limit, don't enforce the limit 732a2afb611SJerry Jelinek * e.g. this can happen if a snapshot is taken by an administrative 733a2afb611SJerry Jelinek * user in the global zone (i.e. a recursive snapshot by root). 734a2afb611SJerry Jelinek * However, we must handle the case of delegated permissions where we 735a2afb611SJerry Jelinek * are allowed to change the limit on the current dataset, but there 736a2afb611SJerry Jelinek * is another limit in the tree above. 737a2afb611SJerry Jelinek */ 738a2afb611SJerry Jelinek enforce = dsl_enforce_ds_ss_limits(dd, prop, cr); 739a2afb611SJerry Jelinek if (enforce == ENFORCE_NEVER) 740a2afb611SJerry Jelinek return (0); 741a2afb611SJerry Jelinek 742a2afb611SJerry Jelinek /* 743a2afb611SJerry Jelinek * e.g. if renaming a dataset with no snapshots, count adjustment 744a2afb611SJerry Jelinek * is 0. 745a2afb611SJerry Jelinek */ 746a2afb611SJerry Jelinek if (delta == 0) 747a2afb611SJerry Jelinek return (0); 748a2afb611SJerry Jelinek 749a2afb611SJerry Jelinek if (prop == ZFS_PROP_SNAPSHOT_LIMIT) { 750a2afb611SJerry Jelinek /* 751a2afb611SJerry Jelinek * We don't enforce the limit for temporary snapshots. This is 752a2afb611SJerry Jelinek * indicated by a NULL cred_t argument. 753a2afb611SJerry Jelinek */ 754a2afb611SJerry Jelinek if (cr == NULL) 755a2afb611SJerry Jelinek return (0); 756a2afb611SJerry Jelinek 757a2afb611SJerry Jelinek count_prop = DD_FIELD_SNAPSHOT_COUNT; 758a2afb611SJerry Jelinek } else { 759a2afb611SJerry Jelinek count_prop = DD_FIELD_FILESYSTEM_COUNT; 760a2afb611SJerry Jelinek } 761a2afb611SJerry Jelinek 762a2afb611SJerry Jelinek /* 763a2afb611SJerry Jelinek * If an ancestor has been provided, stop checking the limit once we 764a2afb611SJerry Jelinek * hit that dir. We need this during rename so that we don't overcount 765a2afb611SJerry Jelinek * the check once we recurse up to the common ancestor. 766a2afb611SJerry Jelinek */ 767a2afb611SJerry Jelinek if (ancestor == dd) 768a2afb611SJerry Jelinek return (0); 769a2afb611SJerry Jelinek 770a2afb611SJerry Jelinek /* 771a2afb611SJerry Jelinek * If we hit an uninitialized node while recursing up the tree, we can 772a2afb611SJerry Jelinek * stop since we know there is no limit here (or above). The counts are 773a2afb611SJerry Jelinek * not valid on this node and we know we won't touch this node's counts. 774a2afb611SJerry Jelinek */ 775a2afb611SJerry Jelinek if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object, 776a2afb611SJerry Jelinek count_prop, sizeof (count), 1, &count) == ENOENT) 777a2afb611SJerry Jelinek return (0); 778a2afb611SJerry Jelinek 779a2afb611SJerry Jelinek err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL, 780a2afb611SJerry Jelinek B_FALSE); 781a2afb611SJerry Jelinek if (err != 0) 782a2afb611SJerry Jelinek return (err); 783a2afb611SJerry Jelinek 784a2afb611SJerry Jelinek /* Is there a limit which we've hit? */ 785a2afb611SJerry Jelinek if (enforce == ENFORCE_ALWAYS && (count + delta) > limit) 786a2afb611SJerry Jelinek return (SET_ERROR(EDQUOT)); 787a2afb611SJerry Jelinek 788a2afb611SJerry Jelinek if (dd->dd_parent != NULL) 789a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop, 790a2afb611SJerry Jelinek ancestor, cr); 791a2afb611SJerry Jelinek 792a2afb611SJerry Jelinek return (err); 793a2afb611SJerry Jelinek } 794a2afb611SJerry Jelinek 795a2afb611SJerry Jelinek /* 796a2afb611SJerry Jelinek * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all 797a2afb611SJerry Jelinek * parents. When a new filesystem/snapshot is created, increment the count on 798a2afb611SJerry Jelinek * all parents, and when a filesystem/snapshot is destroyed, decrement the 799a2afb611SJerry Jelinek * count. 800a2afb611SJerry Jelinek */ 801a2afb611SJerry Jelinek void 802a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop, 803a2afb611SJerry Jelinek dmu_tx_t *tx) 804a2afb611SJerry Jelinek { 805a2afb611SJerry Jelinek int err; 806a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 807a2afb611SJerry Jelinek uint64_t count; 808a2afb611SJerry Jelinek 809a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool)); 810a2afb611SJerry Jelinek ASSERT(dmu_tx_is_syncing(tx)); 811a2afb611SJerry Jelinek ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 || 812a2afb611SJerry Jelinek strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0); 813a2afb611SJerry Jelinek 814a2afb611SJerry Jelinek /* 815a2afb611SJerry Jelinek * When we receive an incremental stream into a filesystem that already 816a2afb611SJerry Jelinek * exists, a temporary clone is created. We don't count this temporary 817a2afb611SJerry Jelinek * clone, whose name begins with a '%'. We also ignore hidden ($FREE, 818a2afb611SJerry Jelinek * $MOS & $ORIGIN) objsets. 819a2afb611SJerry Jelinek */ 820a2afb611SJerry Jelinek if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') && 821a2afb611SJerry Jelinek strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0) 822a2afb611SJerry Jelinek return; 823a2afb611SJerry Jelinek 824a2afb611SJerry Jelinek /* 825a2afb611SJerry Jelinek * e.g. if renaming a dataset with no snapshots, count adjustment is 0 826a2afb611SJerry Jelinek */ 827a2afb611SJerry Jelinek if (delta == 0) 828a2afb611SJerry Jelinek return; 829a2afb611SJerry Jelinek 830a2afb611SJerry Jelinek /* 831a2afb611SJerry Jelinek * If we hit an uninitialized node while recursing up the tree, we can 832a2afb611SJerry Jelinek * stop since we know the counts are not valid on this node and we 833a2afb611SJerry Jelinek * know we shouldn't touch this node's counts. An uninitialized count 834a2afb611SJerry Jelinek * on the node indicates that either the feature has not yet been 835a2afb611SJerry Jelinek * activated or there are no limits on this part of the tree. 836a2afb611SJerry Jelinek */ 837a2afb611SJerry Jelinek if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object, 838a2afb611SJerry Jelinek prop, sizeof (count), 1, &count)) == ENOENT) 839a2afb611SJerry Jelinek return; 840a2afb611SJerry Jelinek VERIFY0(err); 841a2afb611SJerry Jelinek 842a2afb611SJerry Jelinek count += delta; 843a2afb611SJerry Jelinek /* Use a signed verify to make sure we're not neg. */ 844a2afb611SJerry Jelinek VERIFY3S(count, >=, 0); 845a2afb611SJerry Jelinek 846a2afb611SJerry Jelinek VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count, 847a2afb611SJerry Jelinek tx)); 848a2afb611SJerry Jelinek 849a2afb611SJerry Jelinek /* Roll up this additional count into our ancestors */ 850a2afb611SJerry Jelinek if (dd->dd_parent != NULL) 851a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx); 852a2afb611SJerry Jelinek } 853a2afb611SJerry Jelinek 8541d452cf5Sahrens uint64_t 855088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, 856088f3894Sahrens dmu_tx_t *tx) 857fa9e4066Sahrens { 858088f3894Sahrens objset_t *mos = dp->dp_meta_objset; 859fa9e4066Sahrens uint64_t ddobj; 860cde58dbcSMatthew Ahrens dsl_dir_phys_t *ddphys; 861fa9e4066Sahrens dmu_buf_t *dbuf; 862fa9e4066Sahrens 8631649cd4bStabriz ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, 8641649cd4bStabriz DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); 865088f3894Sahrens if (pds) { 866088f3894Sahrens VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj, 867088f3894Sahrens name, sizeof (uint64_t), 1, &ddobj, tx)); 868088f3894Sahrens } else { 869088f3894Sahrens /* it's the root dir */ 870088f3894Sahrens VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 871088f3894Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); 872088f3894Sahrens } 873ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); 874fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 875cde58dbcSMatthew Ahrens ddphys = dbuf->db_data; 876fa9e4066Sahrens 877cde58dbcSMatthew Ahrens ddphys->dd_creation_time = gethrestime_sec(); 878a2afb611SJerry Jelinek if (pds) { 879cde58dbcSMatthew Ahrens ddphys->dd_parent_obj = pds->dd_object; 880a2afb611SJerry Jelinek 881a2afb611SJerry Jelinek /* update the filesystem counts */ 882a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx); 883a2afb611SJerry Jelinek } 884cde58dbcSMatthew Ahrens ddphys->dd_props_zapobj = zap_create(mos, 885fa9e4066Sahrens DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 886cde58dbcSMatthew Ahrens ddphys->dd_child_dir_zapobj = zap_create(mos, 88787e5029aSahrens DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); 88874e7dc98SMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) 889cde58dbcSMatthew Ahrens ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; 890ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 891fa9e4066Sahrens 8921d452cf5Sahrens return (ddobj); 893fa9e4066Sahrens } 894fa9e4066Sahrens 895088f3894Sahrens boolean_t 896088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd) 897fa9e4066Sahrens { 898088f3894Sahrens return (dd->dd_phys->dd_origin_obj && 899088f3894Sahrens (dd->dd_pool->dp_origin_snap == NULL || 900088f3894Sahrens dd->dd_phys->dd_origin_obj != 901088f3894Sahrens dd->dd_pool->dp_origin_snap->ds_object)); 902fa9e4066Sahrens } 903fa9e4066Sahrens 904fa9e4066Sahrens void 905a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) 906fa9e4066Sahrens { 907fa9e4066Sahrens mutex_enter(&dd->dd_lock); 90874e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 90974e7dc98SMatthew Ahrens dd->dd_phys->dd_used_bytes); 910a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota); 911a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, 912a2eea2e1Sahrens dd->dd_phys->dd_reserved); 913a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 914a2eea2e1Sahrens dd->dd_phys->dd_compressed_bytes == 0 ? 100 : 915a2eea2e1Sahrens (dd->dd_phys->dd_uncompressed_bytes * 100 / 916a2eea2e1Sahrens dd->dd_phys->dd_compressed_bytes)); 91777372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED, 91877372cb0SMatthew Ahrens dd->dd_phys->dd_uncompressed_bytes); 91974e7dc98SMatthew Ahrens if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 92074e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, 92174e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]); 92274e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, 92374e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]); 92474e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, 92574e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]); 92674e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, 92774e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] + 92874e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]); 92974e7dc98SMatthew Ahrens } 930fa9e4066Sahrens mutex_exit(&dd->dd_lock); 931fa9e4066Sahrens 932a2afb611SJerry Jelinek if (dsl_dir_is_zapified(dd)) { 933a2afb611SJerry Jelinek uint64_t count; 934a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 935a2afb611SJerry Jelinek 936a2afb611SJerry Jelinek if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 937a2afb611SJerry Jelinek sizeof (count), 1, &count) == 0) { 938a2afb611SJerry Jelinek dsl_prop_nvlist_add_uint64(nv, 939a2afb611SJerry Jelinek ZFS_PROP_FILESYSTEM_COUNT, count); 940a2afb611SJerry Jelinek } 941a2afb611SJerry Jelinek if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 942a2afb611SJerry Jelinek sizeof (count), 1, &count) == 0) { 943a2afb611SJerry Jelinek dsl_prop_nvlist_add_uint64(nv, 944a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_COUNT, count); 945a2afb611SJerry Jelinek } 946a2afb611SJerry Jelinek } 947a2afb611SJerry Jelinek 948088f3894Sahrens if (dsl_dir_is_clone(dd)) { 949fa9e4066Sahrens dsl_dataset_t *ds; 950a2eea2e1Sahrens char buf[MAXNAMELEN]; 951fa9e4066Sahrens 9523b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 953745cd3c5Smaybee dd->dd_phys->dd_origin_obj, FTAG, &ds)); 954a2eea2e1Sahrens dsl_dataset_name(ds, buf); 955745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 956a2eea2e1Sahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); 957fa9e4066Sahrens } 958fa9e4066Sahrens } 959fa9e4066Sahrens 960fa9e4066Sahrens void 961fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) 962fa9e4066Sahrens { 963fa9e4066Sahrens dsl_pool_t *dp = dd->dd_pool; 964fa9e4066Sahrens 965fa9e4066Sahrens ASSERT(dd->dd_phys); 966fa9e4066Sahrens 9673b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) { 968fa9e4066Sahrens /* up the hold count until we can be written out */ 969fa9e4066Sahrens dmu_buf_add_ref(dd->dd_dbuf, dd); 970fa9e4066Sahrens } 971fa9e4066Sahrens } 972fa9e4066Sahrens 973fa9e4066Sahrens static int64_t 974fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) 975fa9e4066Sahrens { 976fa9e4066Sahrens uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved); 977fa9e4066Sahrens uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved); 978fa9e4066Sahrens return (new_accounted - old_accounted); 979fa9e4066Sahrens } 980fa9e4066Sahrens 981fa9e4066Sahrens void 982fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) 983fa9e4066Sahrens { 984fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 985fa9e4066Sahrens 986fa9e4066Sahrens mutex_enter(&dd->dd_lock); 987fb09f5aaSMadhav Suresh ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]); 988fa9e4066Sahrens dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, 989fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); 990fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; 991fa9e4066Sahrens mutex_exit(&dd->dd_lock); 992fa9e4066Sahrens 993fa9e4066Sahrens /* release the hold from dsl_dir_dirty */ 994ea8dc4b6Seschrock dmu_buf_rele(dd->dd_dbuf, dd); 995fa9e4066Sahrens } 996fa9e4066Sahrens 997fa9e4066Sahrens static uint64_t 998a9799022Sck dsl_dir_space_towrite(dsl_dir_t *dd) 999fa9e4066Sahrens { 1000a9799022Sck uint64_t space = 0; 1001fa9e4066Sahrens int i; 1002fa9e4066Sahrens 1003fa9e4066Sahrens ASSERT(MUTEX_HELD(&dd->dd_lock)); 1004fa9e4066Sahrens 1005fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 1006fa9e4066Sahrens space += dd->dd_space_towrite[i&TXG_MASK]; 1007fa9e4066Sahrens ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0); 1008fa9e4066Sahrens } 1009fa9e4066Sahrens return (space); 1010fa9e4066Sahrens } 1011fa9e4066Sahrens 1012fa9e4066Sahrens /* 1013fa9e4066Sahrens * How much space would dd have available if ancestor had delta applied 1014fa9e4066Sahrens * to it? If ondiskonly is set, we're only interested in what's 1015fa9e4066Sahrens * on-disk, not estimated pending changes. 1016fa9e4066Sahrens */ 1017a2eea2e1Sahrens uint64_t 1018fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd, 1019fa9e4066Sahrens dsl_dir_t *ancestor, int64_t delta, int ondiskonly) 1020fa9e4066Sahrens { 1021fa9e4066Sahrens uint64_t parentspace, myspace, quota, used; 1022fa9e4066Sahrens 1023fa9e4066Sahrens /* 1024fa9e4066Sahrens * If there are no restrictions otherwise, assume we have 1025fa9e4066Sahrens * unlimited space available. 1026fa9e4066Sahrens */ 1027fa9e4066Sahrens quota = UINT64_MAX; 1028fa9e4066Sahrens parentspace = UINT64_MAX; 1029fa9e4066Sahrens 1030fa9e4066Sahrens if (dd->dd_parent != NULL) { 1031fa9e4066Sahrens parentspace = dsl_dir_space_available(dd->dd_parent, 1032fa9e4066Sahrens ancestor, delta, ondiskonly); 1033fa9e4066Sahrens } 1034fa9e4066Sahrens 1035fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1036fa9e4066Sahrens if (dd->dd_phys->dd_quota != 0) 1037fa9e4066Sahrens quota = dd->dd_phys->dd_quota; 103874e7dc98SMatthew Ahrens used = dd->dd_phys->dd_used_bytes; 1039a9799022Sck if (!ondiskonly) 1040a9799022Sck used += dsl_dir_space_towrite(dd); 1041fa9e4066Sahrens 1042fa9e4066Sahrens if (dd->dd_parent == NULL) { 104399653d4eSeschrock uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); 1044fa9e4066Sahrens quota = MIN(quota, poolsize); 1045fa9e4066Sahrens } 1046fa9e4066Sahrens 1047fa9e4066Sahrens if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) { 1048fa9e4066Sahrens /* 1049fa9e4066Sahrens * We have some space reserved, in addition to what our 1050fa9e4066Sahrens * parent gave us. 1051fa9e4066Sahrens */ 1052fa9e4066Sahrens parentspace += dd->dd_phys->dd_reserved - used; 1053fa9e4066Sahrens } 1054fa9e4066Sahrens 105574e7dc98SMatthew Ahrens if (dd == ancestor) { 105674e7dc98SMatthew Ahrens ASSERT(delta <= 0); 105774e7dc98SMatthew Ahrens ASSERT(used >= -delta); 105874e7dc98SMatthew Ahrens used += delta; 105974e7dc98SMatthew Ahrens if (parentspace != UINT64_MAX) 106074e7dc98SMatthew Ahrens parentspace -= delta; 106174e7dc98SMatthew Ahrens } 106274e7dc98SMatthew Ahrens 1063fa9e4066Sahrens if (used > quota) { 1064fa9e4066Sahrens /* over quota */ 1065fa9e4066Sahrens myspace = 0; 1066fa9e4066Sahrens } else { 1067fa9e4066Sahrens /* 106899653d4eSeschrock * the lesser of the space provided by our parent and 106999653d4eSeschrock * the space left in our quota 1070fa9e4066Sahrens */ 1071fa9e4066Sahrens myspace = MIN(parentspace, quota - used); 1072fa9e4066Sahrens } 1073fa9e4066Sahrens 1074fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1075fa9e4066Sahrens 1076fa9e4066Sahrens return (myspace); 1077fa9e4066Sahrens } 1078fa9e4066Sahrens 1079fa9e4066Sahrens struct tempreserve { 1080fa9e4066Sahrens list_node_t tr_node; 1081fa9e4066Sahrens dsl_dir_t *tr_ds; 1082fa9e4066Sahrens uint64_t tr_size; 1083fa9e4066Sahrens }; 1084fa9e4066Sahrens 1085fa9e4066Sahrens static int 1086a9799022Sck dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, 1087a9799022Sck boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list, 10886df6c3bcSck dmu_tx_t *tx, boolean_t first) 1089fa9e4066Sahrens { 1090fa9e4066Sahrens uint64_t txg = tx->tx_txg; 1091a9799022Sck uint64_t est_inflight, used_on_disk, quota, parent_rsrv; 1092468c413aSTim Haley uint64_t deferred = 0; 1093a9799022Sck struct tempreserve *tr; 1094468c413aSTim Haley int retval = EDQUOT; 1095fa9e4066Sahrens int txgidx = txg & TXG_MASK; 1096fa9e4066Sahrens int i; 10979082849eSck uint64_t ref_rsrv = 0; 1098fa9e4066Sahrens 1099fa9e4066Sahrens ASSERT3U(txg, !=, 0); 1100a9799022Sck ASSERT3S(asize, >, 0); 1101fa9e4066Sahrens 1102fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1103a9799022Sck 1104fa9e4066Sahrens /* 1105fa9e4066Sahrens * Check against the dsl_dir's quota. We don't add in the delta 1106fa9e4066Sahrens * when checking for over-quota because they get one free hit. 1107fa9e4066Sahrens */ 1108a9799022Sck est_inflight = dsl_dir_space_towrite(dd); 1109fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) 1110a9799022Sck est_inflight += dd->dd_tempreserved[i]; 111174e7dc98SMatthew Ahrens used_on_disk = dd->dd_phys->dd_used_bytes; 1112fa9e4066Sahrens 1113f4d2e9e6Smaybee /* 11146df6c3bcSck * On the first iteration, fetch the dataset's used-on-disk and 11156df6c3bcSck * refreservation values. Also, if checkrefquota is set, test if 11166df6c3bcSck * allocating this space would exceed the dataset's refquota. 1117f4d2e9e6Smaybee */ 11186df6c3bcSck if (first && tx->tx_objset) { 1119c3fdb13aSmaybee int error; 1120503ad85cSMatthew Ahrens dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; 11219082849eSck 1122a9799022Sck error = dsl_dataset_check_quota(ds, checkrefquota, 11239082849eSck asize, est_inflight, &used_on_disk, &ref_rsrv); 1124a9799022Sck if (error) { 1125a9799022Sck mutex_exit(&dd->dd_lock); 1126a9799022Sck return (error); 1127a9799022Sck } 1128a9799022Sck } 1129a9799022Sck 1130a9799022Sck /* 1131a9799022Sck * If this transaction will result in a net free of space, 1132a9799022Sck * we want to let it through. 1133a9799022Sck */ 1134a9799022Sck if (ignorequota || netfree || dd->dd_phys->dd_quota == 0) 1135f4d2e9e6Smaybee quota = UINT64_MAX; 1136f4d2e9e6Smaybee else 1137fa9e4066Sahrens quota = dd->dd_phys->dd_quota; 1138fa9e4066Sahrens 1139fa9e4066Sahrens /* 1140468c413aSTim Haley * Adjust the quota against the actual pool size at the root 1141468c413aSTim Haley * minus any outstanding deferred frees. 1142f4d2e9e6Smaybee * To ensure that it's possible to remove files from a full 1143f4d2e9e6Smaybee * pool without inducing transient overcommits, we throttle 1144fa9e4066Sahrens * netfree transactions against a quota that is slightly larger, 1145fa9e4066Sahrens * but still within the pool's allocation slop. In cases where 1146fa9e4066Sahrens * we're very close to full, this will allow a steady trickle of 1147fa9e4066Sahrens * removes to get through. 1148fa9e4066Sahrens */ 11491934e92fSmaybee if (dd->dd_parent == NULL) { 1150b24ab676SJeff Bonwick spa_t *spa = dd->dd_pool->dp_spa; 1151fa9e4066Sahrens uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); 1152b24ab676SJeff Bonwick deferred = metaslab_class_get_deferred(spa_normal_class(spa)); 1153468c413aSTim Haley if (poolsize - deferred < quota) { 1154468c413aSTim Haley quota = poolsize - deferred; 1155468c413aSTim Haley retval = ENOSPC; 1156fa9e4066Sahrens } 1157fa9e4066Sahrens } 1158fa9e4066Sahrens 1159fa9e4066Sahrens /* 1160fa9e4066Sahrens * If they are requesting more space, and our current estimate 1161a9799022Sck * is over quota, they get to try again unless the actual 1162ea8dc4b6Seschrock * on-disk is over quota and there are no pending changes (which 1163ea8dc4b6Seschrock * may free up space for us). 1164fa9e4066Sahrens */ 1165468c413aSTim Haley if (used_on_disk + est_inflight >= quota) { 1166468c413aSTim Haley if (est_inflight > 0 || used_on_disk < quota || 1167468c413aSTim Haley (retval == ENOSPC && used_on_disk < quota + deferred)) 1168468c413aSTim Haley retval = ERESTART; 1169a9799022Sck dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " 1170fa9e4066Sahrens "quota=%lluK tr=%lluK err=%d\n", 1171a9799022Sck used_on_disk>>10, est_inflight>>10, 1172468c413aSTim Haley quota>>10, asize>>10, retval); 1173fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1174be6fd75aSMatthew Ahrens return (SET_ERROR(retval)); 1175fa9e4066Sahrens } 1176fa9e4066Sahrens 1177fa9e4066Sahrens /* We need to up our estimated delta before dropping dd_lock */ 1178fa9e4066Sahrens dd->dd_tempreserved[txgidx] += asize; 1179fa9e4066Sahrens 11809082849eSck parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, 11819082849eSck asize - ref_rsrv); 1182fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1183fa9e4066Sahrens 11841ab7f2deSmaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 1185fa9e4066Sahrens tr->tr_ds = dd; 1186fa9e4066Sahrens tr->tr_size = asize; 1187fa9e4066Sahrens list_insert_tail(tr_list, tr); 1188fa9e4066Sahrens 1189fa9e4066Sahrens /* see if it's OK with our parent */ 11901934e92fSmaybee if (dd->dd_parent && parent_rsrv) { 11911934e92fSmaybee boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0); 11921934e92fSmaybee 1193fa9e4066Sahrens return (dsl_dir_tempreserve_impl(dd->dd_parent, 11946df6c3bcSck parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE)); 1195fa9e4066Sahrens } else { 1196fa9e4066Sahrens return (0); 1197fa9e4066Sahrens } 1198fa9e4066Sahrens } 1199fa9e4066Sahrens 1200fa9e4066Sahrens /* 1201fa9e4066Sahrens * Reserve space in this dsl_dir, to be used in this tx's txg. 1202a9799022Sck * After the space has been dirtied (and dsl_dir_willuse_space() 1203a9799022Sck * has been called), the reservation should be canceled, using 1204a9799022Sck * dsl_dir_tempreserve_clear(). 1205fa9e4066Sahrens */ 1206fa9e4066Sahrens int 1207a9799022Sck dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, 1208a9799022Sck uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx) 1209fa9e4066Sahrens { 12101ab7f2deSmaybee int err; 1211fa9e4066Sahrens list_t *tr_list; 1212fa9e4066Sahrens 1213a9799022Sck if (asize == 0) { 1214a9799022Sck *tr_cookiep = NULL; 1215a9799022Sck return (0); 1216a9799022Sck } 1217a9799022Sck 1218fa9e4066Sahrens tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); 1219fa9e4066Sahrens list_create(tr_list, sizeof (struct tempreserve), 1220fa9e4066Sahrens offsetof(struct tempreserve, tr_node)); 1221a9799022Sck ASSERT3S(asize, >, 0); 1222ea8dc4b6Seschrock ASSERT3S(fsize, >=, 0); 1223fa9e4066Sahrens 12241ab7f2deSmaybee err = arc_tempreserve_space(lsize, tx->tx_txg); 1225fa9e4066Sahrens if (err == 0) { 1226fa9e4066Sahrens struct tempreserve *tr; 1227fa9e4066Sahrens 12281ab7f2deSmaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 12291ab7f2deSmaybee tr->tr_size = lsize; 12301ab7f2deSmaybee list_insert_tail(tr_list, tr); 12311ab7f2deSmaybee } else { 12321ab7f2deSmaybee if (err == EAGAIN) { 123369962b56SMatthew Ahrens /* 123469962b56SMatthew Ahrens * If arc_memory_throttle() detected that pageout 123569962b56SMatthew Ahrens * is running and we are low on memory, we delay new 123669962b56SMatthew Ahrens * non-pageout transactions to give pageout an 123769962b56SMatthew Ahrens * advantage. 123869962b56SMatthew Ahrens * 123969962b56SMatthew Ahrens * It is unfortunate to be delaying while the caller's 124069962b56SMatthew Ahrens * locks are held. 124169962b56SMatthew Ahrens */ 12420689f76cSAdam Leventhal txg_delay(dd->dd_pool, tx->tx_txg, 12430689f76cSAdam Leventhal MSEC2NSEC(10), MSEC2NSEC(10)); 1244be6fd75aSMatthew Ahrens err = SET_ERROR(ERESTART); 1245fa9e4066Sahrens } 12461ab7f2deSmaybee } 12471ab7f2deSmaybee 12481ab7f2deSmaybee if (err == 0) { 12491ab7f2deSmaybee err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize, 12506df6c3bcSck FALSE, asize > usize, tr_list, tx, TRUE); 1251fa9e4066Sahrens } 1252fa9e4066Sahrens 12533b2aab18SMatthew Ahrens if (err != 0) 1254fa9e4066Sahrens dsl_dir_tempreserve_clear(tr_list, tx); 1255fa9e4066Sahrens else 1256fa9e4066Sahrens *tr_cookiep = tr_list; 12571ab7f2deSmaybee 1258fa9e4066Sahrens return (err); 1259fa9e4066Sahrens } 1260fa9e4066Sahrens 1261fa9e4066Sahrens /* 1262fa9e4066Sahrens * Clear a temporary reservation that we previously made with 1263fa9e4066Sahrens * dsl_dir_tempreserve_space(). 1264fa9e4066Sahrens */ 1265fa9e4066Sahrens void 1266fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) 1267fa9e4066Sahrens { 1268fa9e4066Sahrens int txgidx = tx->tx_txg & TXG_MASK; 1269fa9e4066Sahrens list_t *tr_list = tr_cookie; 1270fa9e4066Sahrens struct tempreserve *tr; 1271fa9e4066Sahrens 1272fa9e4066Sahrens ASSERT3U(tx->tx_txg, !=, 0); 1273fa9e4066Sahrens 1274a9799022Sck if (tr_cookie == NULL) 1275a9799022Sck return; 1276a9799022Sck 127769962b56SMatthew Ahrens while ((tr = list_head(tr_list)) != NULL) { 127869962b56SMatthew Ahrens if (tr->tr_ds) { 1279fa9e4066Sahrens mutex_enter(&tr->tr_ds->dd_lock); 1280fa9e4066Sahrens ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, 1281fa9e4066Sahrens tr->tr_size); 1282fa9e4066Sahrens tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; 1283fa9e4066Sahrens mutex_exit(&tr->tr_ds->dd_lock); 12841ab7f2deSmaybee } else { 12851ab7f2deSmaybee arc_tempreserve_clear(tr->tr_size); 1286fa9e4066Sahrens } 1287fa9e4066Sahrens list_remove(tr_list, tr); 1288fa9e4066Sahrens kmem_free(tr, sizeof (struct tempreserve)); 1289fa9e4066Sahrens } 1290fa9e4066Sahrens 1291fa9e4066Sahrens kmem_free(tr_list, sizeof (list_t)); 1292fa9e4066Sahrens } 1293fa9e4066Sahrens 129469962b56SMatthew Ahrens /* 129569962b56SMatthew Ahrens * This should be called from open context when we think we're going to write 129669962b56SMatthew Ahrens * or free space, for example when dirtying data. Be conservative; it's okay 129769962b56SMatthew Ahrens * to write less space or free more, but we don't want to write more or free 129869962b56SMatthew Ahrens * less than the amount specified. 129969962b56SMatthew Ahrens */ 130069962b56SMatthew Ahrens void 130169962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 1302fa9e4066Sahrens { 1303fa9e4066Sahrens int64_t parent_space; 1304fa9e4066Sahrens uint64_t est_used; 1305fa9e4066Sahrens 1306fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1307fa9e4066Sahrens if (space > 0) 1308fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; 1309fa9e4066Sahrens 131074e7dc98SMatthew Ahrens est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes; 1311fa9e4066Sahrens parent_space = parent_delta(dd, est_used, space); 1312fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1313fa9e4066Sahrens 1314fa9e4066Sahrens /* Make sure that we clean up dd_space_to* */ 1315fa9e4066Sahrens dsl_dir_dirty(dd, tx); 1316fa9e4066Sahrens 1317fa9e4066Sahrens /* XXX this is potentially expensive and unnecessary... */ 1318fa9e4066Sahrens if (parent_space && dd->dd_parent) 131969962b56SMatthew Ahrens dsl_dir_willuse_space(dd->dd_parent, parent_space, tx); 1320fa9e4066Sahrens } 1321fa9e4066Sahrens 1322fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */ 1323fa9e4066Sahrens void 132474e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, 1325fa9e4066Sahrens int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) 1326fa9e4066Sahrens { 1327fa9e4066Sahrens int64_t accounted_delta; 1328b62969f8SMatthew Ahrens 1329b62969f8SMatthew Ahrens /* 1330b62969f8SMatthew Ahrens * dsl_dataset_set_refreservation_sync_impl() calls this with 1331b62969f8SMatthew Ahrens * dd_lock held, so that it can atomically update 1332b62969f8SMatthew Ahrens * ds->ds_reserved and the dsl_dir accounting, so that 1333b62969f8SMatthew Ahrens * dsl_dataset_check_quota() can see dataset and dir accounting 1334b62969f8SMatthew Ahrens * consistently. 1335b62969f8SMatthew Ahrens */ 133602c8f3f0SMatthew Ahrens boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 1337fa9e4066Sahrens 1338fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 133974e7dc98SMatthew Ahrens ASSERT(type < DD_USED_NUM); 1340fa9e4066Sahrens 1341b62969f8SMatthew Ahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1342b62969f8SMatthew Ahrens 134302c8f3f0SMatthew Ahrens if (needlock) 134402c8f3f0SMatthew Ahrens mutex_enter(&dd->dd_lock); 134574e7dc98SMatthew Ahrens accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used); 134674e7dc98SMatthew Ahrens ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used); 1347fa9e4066Sahrens ASSERT(compressed >= 0 || 1348fa9e4066Sahrens dd->dd_phys->dd_compressed_bytes >= -compressed); 1349fa9e4066Sahrens ASSERT(uncompressed >= 0 || 1350fa9e4066Sahrens dd->dd_phys->dd_uncompressed_bytes >= -uncompressed); 135174e7dc98SMatthew Ahrens dd->dd_phys->dd_used_bytes += used; 1352fa9e4066Sahrens dd->dd_phys->dd_uncompressed_bytes += uncompressed; 1353fa9e4066Sahrens dd->dd_phys->dd_compressed_bytes += compressed; 135474e7dc98SMatthew Ahrens 135574e7dc98SMatthew Ahrens if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 135674e7dc98SMatthew Ahrens ASSERT(used > 0 || 135774e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[type] >= -used); 135874e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[type] += used; 135974e7dc98SMatthew Ahrens #ifdef DEBUG 136074e7dc98SMatthew Ahrens dd_used_t t; 136174e7dc98SMatthew Ahrens uint64_t u = 0; 136274e7dc98SMatthew Ahrens for (t = 0; t < DD_USED_NUM; t++) 136374e7dc98SMatthew Ahrens u += dd->dd_phys->dd_used_breakdown[t]; 136474e7dc98SMatthew Ahrens ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes); 136574e7dc98SMatthew Ahrens #endif 136674e7dc98SMatthew Ahrens } 136702c8f3f0SMatthew Ahrens if (needlock) 136802c8f3f0SMatthew Ahrens mutex_exit(&dd->dd_lock); 1369fa9e4066Sahrens 1370fa9e4066Sahrens if (dd->dd_parent != NULL) { 137174e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 1372fa9e4066Sahrens accounted_delta, compressed, uncompressed, tx); 137374e7dc98SMatthew Ahrens dsl_dir_transfer_space(dd->dd_parent, 137474e7dc98SMatthew Ahrens used - accounted_delta, 137574e7dc98SMatthew Ahrens DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); 1376fa9e4066Sahrens } 1377fa9e4066Sahrens } 1378fa9e4066Sahrens 137974e7dc98SMatthew Ahrens void 138074e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, 138174e7dc98SMatthew Ahrens dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 138274e7dc98SMatthew Ahrens { 138374e7dc98SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 138474e7dc98SMatthew Ahrens ASSERT(oldtype < DD_USED_NUM); 138574e7dc98SMatthew Ahrens ASSERT(newtype < DD_USED_NUM); 138674e7dc98SMatthew Ahrens 138774e7dc98SMatthew Ahrens if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN)) 138874e7dc98SMatthew Ahrens return; 138974e7dc98SMatthew Ahrens 1390b62969f8SMatthew Ahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1391b62969f8SMatthew Ahrens mutex_enter(&dd->dd_lock); 139274e7dc98SMatthew Ahrens ASSERT(delta > 0 ? 139374e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[oldtype] >= delta : 139474e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[newtype] >= -delta); 139574e7dc98SMatthew Ahrens ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta)); 139674e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[oldtype] -= delta; 139774e7dc98SMatthew Ahrens dd->dd_phys->dd_used_breakdown[newtype] += delta; 1398b62969f8SMatthew Ahrens mutex_exit(&dd->dd_lock); 139974e7dc98SMatthew Ahrens } 140074e7dc98SMatthew Ahrens 14013b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg { 14023b2aab18SMatthew Ahrens const char *ddsqra_name; 14033b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 14043b2aab18SMatthew Ahrens uint64_t ddsqra_value; 14053b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t; 14063b2aab18SMatthew Ahrens 1407fa9e4066Sahrens static int 14083b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx) 1409fa9e4066Sahrens { 14103b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 14113b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14123b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14133b2aab18SMatthew Ahrens int error; 14143b2aab18SMatthew Ahrens uint64_t towrite, newval; 1415fa9e4066Sahrens 14163b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 14173b2aab18SMatthew Ahrens if (error != 0) 14183b2aab18SMatthew Ahrens return (error); 14193b2aab18SMatthew Ahrens 14203b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, "quota", 14213b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 14223b2aab18SMatthew Ahrens if (error != 0) { 14233b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14243b2aab18SMatthew Ahrens return (error); 14253b2aab18SMatthew Ahrens } 142692241e0bSTom Erickson 14273b2aab18SMatthew Ahrens if (newval == 0) { 14283b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14291d452cf5Sahrens return (0); 14303b2aab18SMatthew Ahrens } 1431fa9e4066Sahrens 14323b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 14331d452cf5Sahrens /* 14341d452cf5Sahrens * If we are doing the preliminary check in open context, and 14351d452cf5Sahrens * there are pending changes, then don't fail it, since the 1436a9799022Sck * pending changes could under-estimate the amount of space to be 14371d452cf5Sahrens * freed up. 14381d452cf5Sahrens */ 14393b2aab18SMatthew Ahrens towrite = dsl_dir_space_towrite(ds->ds_dir); 14401d452cf5Sahrens if ((dmu_tx_is_syncing(tx) || towrite == 0) && 14413b2aab18SMatthew Ahrens (newval < ds->ds_dir->dd_phys->dd_reserved || 14423b2aab18SMatthew Ahrens newval < ds->ds_dir->dd_phys->dd_used_bytes + towrite)) { 1443be6fd75aSMatthew Ahrens error = SET_ERROR(ENOSPC); 1444fa9e4066Sahrens } 14453b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 14463b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14473b2aab18SMatthew Ahrens return (error); 1448fa9e4066Sahrens } 1449fa9e4066Sahrens 14501d452cf5Sahrens static void 14513b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx) 14521d452cf5Sahrens { 14533b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 14543b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14553b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14563b2aab18SMatthew Ahrens uint64_t newval; 145792241e0bSTom Erickson 14583b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 14591d452cf5Sahrens 1460013023d4SMartin Matuska if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1461013023d4SMartin Matuska dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA), 1462013023d4SMartin Matuska ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1463013023d4SMartin Matuska &ddsqra->ddsqra_value, tx); 14641d452cf5Sahrens 1465013023d4SMartin Matuska VERIFY0(dsl_prop_get_int_ds(ds, 1466013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_QUOTA), &newval)); 1467013023d4SMartin Matuska } else { 1468013023d4SMartin Matuska newval = ddsqra->ddsqra_value; 1469013023d4SMartin Matuska spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1470013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval); 1471013023d4SMartin Matuska } 14723b2aab18SMatthew Ahrens 14733b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 14743b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 14753b2aab18SMatthew Ahrens ds->ds_dir->dd_phys->dd_quota = newval; 14763b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 14773b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14781d452cf5Sahrens } 14791d452cf5Sahrens 1480fa9e4066Sahrens int 148192241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota) 1482fa9e4066Sahrens { 14833b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t ddsqra; 1484fa9e4066Sahrens 14853b2aab18SMatthew Ahrens ddsqra.ddsqra_name = ddname; 14863b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 14873b2aab18SMatthew Ahrens ddsqra.ddsqra_value = quota; 148892241e0bSTom Erickson 14893b2aab18SMatthew Ahrens return (dsl_sync_task(ddname, dsl_dir_set_quota_check, 14903b2aab18SMatthew Ahrens dsl_dir_set_quota_sync, &ddsqra, 0)); 1491fa9e4066Sahrens } 1492fa9e4066Sahrens 1493a9799022Sck int 14943b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx) 1495fa9e4066Sahrens { 14963b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 14973b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14983b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14993b2aab18SMatthew Ahrens dsl_dir_t *dd; 15003b2aab18SMatthew Ahrens uint64_t newval, used, avail; 15013b2aab18SMatthew Ahrens int error; 150292241e0bSTom Erickson 15033b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 15043b2aab18SMatthew Ahrens if (error != 0) 15053b2aab18SMatthew Ahrens return (error); 15063b2aab18SMatthew Ahrens dd = ds->ds_dir; 1507fa9e4066Sahrens 15081d452cf5Sahrens /* 15091d452cf5Sahrens * If we are doing the preliminary check in open context, the 15101d452cf5Sahrens * space estimates may be inaccurate. 15111d452cf5Sahrens */ 15123b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 15133b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15141d452cf5Sahrens return (0); 15153b2aab18SMatthew Ahrens } 15163b2aab18SMatthew Ahrens 15173b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 15183b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RESERVATION), 15193b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 15203b2aab18SMatthew Ahrens if (error != 0) { 15213b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15223b2aab18SMatthew Ahrens return (error); 15233b2aab18SMatthew Ahrens } 15241d452cf5Sahrens 1525fa9e4066Sahrens mutex_enter(&dd->dd_lock); 152674e7dc98SMatthew Ahrens used = dd->dd_phys->dd_used_bytes; 1527fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1528fa9e4066Sahrens 1529fa9e4066Sahrens if (dd->dd_parent) { 1530fa9e4066Sahrens avail = dsl_dir_space_available(dd->dd_parent, 1531fa9e4066Sahrens NULL, 0, FALSE); 1532fa9e4066Sahrens } else { 1533fa9e4066Sahrens avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; 1534fa9e4066Sahrens } 1535fa9e4066Sahrens 15363b2aab18SMatthew Ahrens if (MAX(used, newval) > MAX(used, dd->dd_phys->dd_reserved)) { 15373b2aab18SMatthew Ahrens uint64_t delta = MAX(used, newval) - 1538379c004dSEric Schrock MAX(used, dd->dd_phys->dd_reserved); 1539379c004dSEric Schrock 15403b2aab18SMatthew Ahrens if (delta > avail || 15413b2aab18SMatthew Ahrens (dd->dd_phys->dd_quota > 0 && 15423b2aab18SMatthew Ahrens newval > dd->dd_phys->dd_quota)) 1543be6fd75aSMatthew Ahrens error = SET_ERROR(ENOSPC); 1544379c004dSEric Schrock } 1545379c004dSEric Schrock 15463b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15473b2aab18SMatthew Ahrens return (error); 15481d452cf5Sahrens } 15491d452cf5Sahrens 15503b2aab18SMatthew Ahrens void 15514445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx) 15521d452cf5Sahrens { 15531d452cf5Sahrens uint64_t used; 15541d452cf5Sahrens int64_t delta; 15551d452cf5Sahrens 1556a9799022Sck dmu_buf_will_dirty(dd->dd_dbuf, tx); 1557a9799022Sck 15581d452cf5Sahrens mutex_enter(&dd->dd_lock); 155974e7dc98SMatthew Ahrens used = dd->dd_phys->dd_used_bytes; 15604445fffbSMatthew Ahrens delta = MAX(used, value) - MAX(used, dd->dd_phys->dd_reserved); 15614445fffbSMatthew Ahrens dd->dd_phys->dd_reserved = value; 1562fa9e4066Sahrens 1563fa9e4066Sahrens if (dd->dd_parent != NULL) { 1564fa9e4066Sahrens /* Roll up this additional usage into our ancestors */ 156574e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 156674e7dc98SMatthew Ahrens delta, 0, 0, tx); 1567fa9e4066Sahrens } 156802c8f3f0SMatthew Ahrens mutex_exit(&dd->dd_lock); 15694445fffbSMatthew Ahrens } 15704445fffbSMatthew Ahrens 15714445fffbSMatthew Ahrens 15724445fffbSMatthew Ahrens static void 15733b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx) 15744445fffbSMatthew Ahrens { 15753b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 15763b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15773b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15783b2aab18SMatthew Ahrens uint64_t newval; 15793b2aab18SMatthew Ahrens 15803b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 15813b2aab18SMatthew Ahrens 1582013023d4SMartin Matuska if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1583013023d4SMartin Matuska dsl_prop_set_sync_impl(ds, 1584013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION), 1585013023d4SMartin Matuska ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1586013023d4SMartin Matuska &ddsqra->ddsqra_value, tx); 15874445fffbSMatthew Ahrens 1588013023d4SMartin Matuska VERIFY0(dsl_prop_get_int_ds(ds, 1589013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval)); 1590013023d4SMartin Matuska } else { 1591013023d4SMartin Matuska newval = ddsqra->ddsqra_value; 1592013023d4SMartin Matuska spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1593013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION), 1594013023d4SMartin Matuska (longlong_t)newval); 1595013023d4SMartin Matuska } 1596ecd6cf80Smarks 15973b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx); 15983b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1599fa9e4066Sahrens } 1600fa9e4066Sahrens 1601fa9e4066Sahrens int 160292241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source, 160392241e0bSTom Erickson uint64_t reservation) 1604fa9e4066Sahrens { 16053b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t ddsqra; 160692241e0bSTom Erickson 16073b2aab18SMatthew Ahrens ddsqra.ddsqra_name = ddname; 16083b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 16093b2aab18SMatthew Ahrens ddsqra.ddsqra_value = reservation; 161092241e0bSTom Erickson 16113b2aab18SMatthew Ahrens return (dsl_sync_task(ddname, dsl_dir_set_reservation_check, 16123b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync, &ddsqra, 0)); 1613fa9e4066Sahrens } 1614fa9e4066Sahrens 1615fa9e4066Sahrens static dsl_dir_t * 1616fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) 1617fa9e4066Sahrens { 1618fa9e4066Sahrens for (; ds1; ds1 = ds1->dd_parent) { 1619fa9e4066Sahrens dsl_dir_t *dd; 1620fa9e4066Sahrens for (dd = ds2; dd; dd = dd->dd_parent) { 1621fa9e4066Sahrens if (ds1 == dd) 1622fa9e4066Sahrens return (dd); 1623fa9e4066Sahrens } 1624fa9e4066Sahrens } 1625fa9e4066Sahrens return (NULL); 1626fa9e4066Sahrens } 1627fa9e4066Sahrens 1628fa9e4066Sahrens /* 1629fa9e4066Sahrens * If delta is applied to dd, how much of that delta would be applied to 1630fa9e4066Sahrens * ancestor? Syncing context only. 1631fa9e4066Sahrens */ 1632fa9e4066Sahrens static int64_t 1633fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) 1634fa9e4066Sahrens { 1635fa9e4066Sahrens if (dd == ancestor) 1636fa9e4066Sahrens return (delta); 1637fa9e4066Sahrens 1638fa9e4066Sahrens mutex_enter(&dd->dd_lock); 163974e7dc98SMatthew Ahrens delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta); 1640fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1641fa9e4066Sahrens return (would_change(dd->dd_parent, delta, ancestor)); 1642fa9e4066Sahrens } 1643fa9e4066Sahrens 16443b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg { 16453b2aab18SMatthew Ahrens const char *ddra_oldname; 16463b2aab18SMatthew Ahrens const char *ddra_newname; 1647a2afb611SJerry Jelinek cred_t *ddra_cred; 16483b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t; 16491d452cf5Sahrens 16503b2aab18SMatthew Ahrens /* ARGSUSED */ 16511d452cf5Sahrens static int 16523b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1653fa9e4066Sahrens { 16543b2aab18SMatthew Ahrens int *deltap = arg; 16553b2aab18SMatthew Ahrens char namebuf[MAXNAMELEN]; 1656fa9e4066Sahrens 16573b2aab18SMatthew Ahrens dsl_dataset_name(ds, namebuf); 16583b2aab18SMatthew Ahrens 16593b2aab18SMatthew Ahrens if (strlen(namebuf) + *deltap >= MAXNAMELEN) 1660be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 16613b2aab18SMatthew Ahrens return (0); 16623b2aab18SMatthew Ahrens } 16633b2aab18SMatthew Ahrens 16643b2aab18SMatthew Ahrens static int 16653b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx) 16663b2aab18SMatthew Ahrens { 16673b2aab18SMatthew Ahrens dsl_dir_rename_arg_t *ddra = arg; 16683b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16693b2aab18SMatthew Ahrens dsl_dir_t *dd, *newparent; 16703b2aab18SMatthew Ahrens const char *mynewname; 16713b2aab18SMatthew Ahrens int error; 16723b2aab18SMatthew Ahrens int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname); 1673fa9e4066Sahrens 16743b2aab18SMatthew Ahrens /* target dir should exist */ 16753b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL); 16763b2aab18SMatthew Ahrens if (error != 0) 16773b2aab18SMatthew Ahrens return (error); 16783b2aab18SMatthew Ahrens 16793b2aab18SMatthew Ahrens /* new parent should exist */ 16803b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG, 16813b2aab18SMatthew Ahrens &newparent, &mynewname); 16823b2aab18SMatthew Ahrens if (error != 0) { 16833b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 16843b2aab18SMatthew Ahrens return (error); 16853b2aab18SMatthew Ahrens } 16863b2aab18SMatthew Ahrens 16873b2aab18SMatthew Ahrens /* can't rename to different pool */ 16883b2aab18SMatthew Ahrens if (dd->dd_pool != newparent->dd_pool) { 16893b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 16903b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 1691be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 16923b2aab18SMatthew Ahrens } 16933b2aab18SMatthew Ahrens 16943b2aab18SMatthew Ahrens /* new name should not already exist */ 16953b2aab18SMatthew Ahrens if (mynewname == NULL) { 16963b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 16973b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 1698be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 16993b2aab18SMatthew Ahrens } 17003b2aab18SMatthew Ahrens 17013b2aab18SMatthew Ahrens /* if the name length is growing, validate child name lengths */ 17023b2aab18SMatthew Ahrens if (delta > 0) { 17033b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename, 17043b2aab18SMatthew Ahrens &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 17053b2aab18SMatthew Ahrens if (error != 0) { 17063b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17073b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 17083b2aab18SMatthew Ahrens return (error); 17093b2aab18SMatthew Ahrens } 17103b2aab18SMatthew Ahrens } 17111d452cf5Sahrens 1712a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) { 1713a2afb611SJerry Jelinek if (spa_feature_is_enabled(dp->dp_spa, 1714a2afb611SJerry Jelinek SPA_FEATURE_FS_SS_LIMIT)) { 1715a2afb611SJerry Jelinek /* 1716a2afb611SJerry Jelinek * Although this is the check function and we don't 1717a2afb611SJerry Jelinek * normally make on-disk changes in check functions, 1718a2afb611SJerry Jelinek * we need to do that here. 1719a2afb611SJerry Jelinek * 1720a2afb611SJerry Jelinek * Ensure this portion of the tree's counts have been 1721a2afb611SJerry Jelinek * initialized in case the new parent has limits set. 1722a2afb611SJerry Jelinek */ 1723a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dd, tx); 1724a2afb611SJerry Jelinek } 1725a2afb611SJerry Jelinek } 1726a2afb611SJerry Jelinek 17273b2aab18SMatthew Ahrens if (newparent != dd->dd_parent) { 172899653d4eSeschrock /* is there enough space? */ 172999653d4eSeschrock uint64_t myspace = 173074e7dc98SMatthew Ahrens MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved); 1731a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 1732a2afb611SJerry Jelinek uint64_t fs_cnt = 0; 1733a2afb611SJerry Jelinek uint64_t ss_cnt = 0; 1734a2afb611SJerry Jelinek 1735a2afb611SJerry Jelinek if (dsl_dir_is_zapified(dd)) { 1736a2afb611SJerry Jelinek int err; 1737a2afb611SJerry Jelinek 1738a2afb611SJerry Jelinek err = zap_lookup(os, dd->dd_object, 1739a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 1740a2afb611SJerry Jelinek &fs_cnt); 1741a2afb611SJerry Jelinek if (err != ENOENT && err != 0) 1742a2afb611SJerry Jelinek return (err); 1743a2afb611SJerry Jelinek 1744a2afb611SJerry Jelinek /* 1745a2afb611SJerry Jelinek * have to add 1 for the filesystem itself that we're 1746a2afb611SJerry Jelinek * moving 1747a2afb611SJerry Jelinek */ 1748a2afb611SJerry Jelinek fs_cnt++; 1749a2afb611SJerry Jelinek 1750a2afb611SJerry Jelinek err = zap_lookup(os, dd->dd_object, 1751a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 1752a2afb611SJerry Jelinek &ss_cnt); 1753a2afb611SJerry Jelinek if (err != ENOENT && err != 0) 1754a2afb611SJerry Jelinek return (err); 1755a2afb611SJerry Jelinek } 1756fa9e4066Sahrens 17571d452cf5Sahrens /* no rename into our descendant */ 17583b2aab18SMatthew Ahrens if (closest_common_ancestor(dd, newparent) == dd) { 17593b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17603b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 1761be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 17623b2aab18SMatthew Ahrens } 1763fa9e4066Sahrens 17643b2aab18SMatthew Ahrens error = dsl_dir_transfer_possible(dd->dd_parent, 1765a2afb611SJerry Jelinek newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred); 17663b2aab18SMatthew Ahrens if (error != 0) { 17673b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17683b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 17693b2aab18SMatthew Ahrens return (error); 17703b2aab18SMatthew Ahrens } 17711d452cf5Sahrens } 1772fa9e4066Sahrens 17733b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17743b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 17751d452cf5Sahrens return (0); 17761d452cf5Sahrens } 17771d452cf5Sahrens 17781d452cf5Sahrens static void 17793b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx) 17801d452cf5Sahrens { 17813b2aab18SMatthew Ahrens dsl_dir_rename_arg_t *ddra = arg; 17823b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17833b2aab18SMatthew Ahrens dsl_dir_t *dd, *newparent; 17843b2aab18SMatthew Ahrens const char *mynewname; 17853b2aab18SMatthew Ahrens int error; 17861d452cf5Sahrens objset_t *mos = dp->dp_meta_objset; 17871d452cf5Sahrens 17883b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL)); 17893b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent, 17903b2aab18SMatthew Ahrens &mynewname)); 17911d452cf5Sahrens 17924445fffbSMatthew Ahrens /* Log this before we change the name. */ 17934445fffbSMatthew Ahrens spa_history_log_internal_dd(dd, "rename", tx, 17943b2aab18SMatthew Ahrens "-> %s", ddra->ddra_newname); 17954445fffbSMatthew Ahrens 17963b2aab18SMatthew Ahrens if (newparent != dd->dd_parent) { 1797a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 1798a2afb611SJerry Jelinek uint64_t fs_cnt = 0; 1799a2afb611SJerry Jelinek uint64_t ss_cnt = 0; 1800a2afb611SJerry Jelinek 1801a2afb611SJerry Jelinek /* 1802a2afb611SJerry Jelinek * We already made sure the dd counts were initialized in the 1803a2afb611SJerry Jelinek * check function. 1804a2afb611SJerry Jelinek */ 1805a2afb611SJerry Jelinek if (spa_feature_is_enabled(dp->dp_spa, 1806a2afb611SJerry Jelinek SPA_FEATURE_FS_SS_LIMIT)) { 1807a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, dd->dd_object, 1808a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 1809a2afb611SJerry Jelinek &fs_cnt)); 1810a2afb611SJerry Jelinek /* add 1 for the filesystem itself that we're moving */ 1811a2afb611SJerry Jelinek fs_cnt++; 1812a2afb611SJerry Jelinek 1813a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, dd->dd_object, 1814a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 1815a2afb611SJerry Jelinek &ss_cnt)); 1816a2afb611SJerry Jelinek } 1817a2afb611SJerry Jelinek 1818a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt, 1819a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx); 1820a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(newparent, fs_cnt, 1821a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx); 1822a2afb611SJerry Jelinek 1823a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt, 1824a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 1825a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(newparent, ss_cnt, 1826a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 1827a2afb611SJerry Jelinek 182874e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 182974e7dc98SMatthew Ahrens -dd->dd_phys->dd_used_bytes, 1830fa9e4066Sahrens -dd->dd_phys->dd_compressed_bytes, 1831fa9e4066Sahrens -dd->dd_phys->dd_uncompressed_bytes, tx); 18323b2aab18SMatthew Ahrens dsl_dir_diduse_space(newparent, DD_USED_CHILD, 183374e7dc98SMatthew Ahrens dd->dd_phys->dd_used_bytes, 1834fa9e4066Sahrens dd->dd_phys->dd_compressed_bytes, 1835fa9e4066Sahrens dd->dd_phys->dd_uncompressed_bytes, tx); 183674e7dc98SMatthew Ahrens 183774e7dc98SMatthew Ahrens if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) { 183874e7dc98SMatthew Ahrens uint64_t unused_rsrv = dd->dd_phys->dd_reserved - 183974e7dc98SMatthew Ahrens dd->dd_phys->dd_used_bytes; 184074e7dc98SMatthew Ahrens 184174e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 184274e7dc98SMatthew Ahrens -unused_rsrv, 0, 0, tx); 18433b2aab18SMatthew Ahrens dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV, 184474e7dc98SMatthew Ahrens unused_rsrv, 0, 0, tx); 184574e7dc98SMatthew Ahrens } 1846fa9e4066Sahrens } 1847fa9e4066Sahrens 1848fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1849fa9e4066Sahrens 1850fa9e4066Sahrens /* remove from old parent zapobj */ 18513b2aab18SMatthew Ahrens error = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj, 1852fa9e4066Sahrens dd->dd_myname, tx); 18533b2aab18SMatthew Ahrens ASSERT0(error); 1854fa9e4066Sahrens 18553b2aab18SMatthew Ahrens (void) strcpy(dd->dd_myname, mynewname); 18563b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd); 18573b2aab18SMatthew Ahrens dd->dd_phys->dd_parent_obj = newparent->dd_object; 18583b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, 18593b2aab18SMatthew Ahrens newparent->dd_object, NULL, dd, &dd->dd_parent)); 1860fa9e4066Sahrens 1861fa9e4066Sahrens /* add to new parent zapobj */ 18623b2aab18SMatthew Ahrens VERIFY0(zap_add(mos, newparent->dd_phys->dd_child_dir_zapobj, 18633b2aab18SMatthew Ahrens dd->dd_myname, 8, 1, &dd->dd_object, tx)); 18643b2aab18SMatthew Ahrens 18653b2aab18SMatthew Ahrens dsl_prop_notify_all(dd); 1866ecd6cf80Smarks 18673b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 18683b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 18691d452cf5Sahrens } 1870fa9e4066Sahrens 18711d452cf5Sahrens int 18723b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname) 18731d452cf5Sahrens { 18743b2aab18SMatthew Ahrens dsl_dir_rename_arg_t ddra; 18751d452cf5Sahrens 18763b2aab18SMatthew Ahrens ddra.ddra_oldname = oldname; 18773b2aab18SMatthew Ahrens ddra.ddra_newname = newname; 1878a2afb611SJerry Jelinek ddra.ddra_cred = CRED(); 18791d452cf5Sahrens 18803b2aab18SMatthew Ahrens return (dsl_sync_task(oldname, 18813b2aab18SMatthew Ahrens dsl_dir_rename_check, dsl_dir_rename_sync, &ddra, 3)); 1882fa9e4066Sahrens } 188399653d4eSeschrock 188499653d4eSeschrock int 1885a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, 1886a2afb611SJerry Jelinek uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr) 188799653d4eSeschrock { 188899653d4eSeschrock dsl_dir_t *ancestor; 188999653d4eSeschrock int64_t adelta; 189099653d4eSeschrock uint64_t avail; 1891a2afb611SJerry Jelinek int err; 189299653d4eSeschrock 189399653d4eSeschrock ancestor = closest_common_ancestor(sdd, tdd); 189499653d4eSeschrock adelta = would_change(sdd, -space, ancestor); 189599653d4eSeschrock avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); 189699653d4eSeschrock if (avail < space) 1897be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 189899653d4eSeschrock 1899a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT, 1900a2afb611SJerry Jelinek ancestor, cr); 1901a2afb611SJerry Jelinek if (err != 0) 1902a2afb611SJerry Jelinek return (err); 1903a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT, 1904a2afb611SJerry Jelinek ancestor, cr); 1905a2afb611SJerry Jelinek if (err != 0) 1906a2afb611SJerry Jelinek return (err); 1907a2afb611SJerry Jelinek 190899653d4eSeschrock return (0); 190999653d4eSeschrock } 191071eb0538SChris Kirby 191171eb0538SChris Kirby timestruc_t 191271eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd) 191371eb0538SChris Kirby { 191471eb0538SChris Kirby timestruc_t t; 191571eb0538SChris Kirby 191671eb0538SChris Kirby mutex_enter(&dd->dd_lock); 191771eb0538SChris Kirby t = dd->dd_snap_cmtime; 191871eb0538SChris Kirby mutex_exit(&dd->dd_lock); 191971eb0538SChris Kirby 192071eb0538SChris Kirby return (t); 192171eb0538SChris Kirby } 192271eb0538SChris Kirby 192371eb0538SChris Kirby void 192471eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd) 192571eb0538SChris Kirby { 192671eb0538SChris Kirby timestruc_t t; 192771eb0538SChris Kirby 192871eb0538SChris Kirby gethrestime(&t); 192971eb0538SChris Kirby mutex_enter(&dd->dd_lock); 193071eb0538SChris Kirby dd->dd_snap_cmtime = t; 193171eb0538SChris Kirby mutex_exit(&dd->dd_lock); 193271eb0538SChris Kirby } 19332acef22dSMatthew Ahrens 19342acef22dSMatthew Ahrens void 19352acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx) 19362acef22dSMatthew Ahrens { 19372acef22dSMatthew Ahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 19382acef22dSMatthew Ahrens dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx); 19392acef22dSMatthew Ahrens } 1940a2afb611SJerry Jelinek 1941a2afb611SJerry Jelinek boolean_t 1942a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd) 1943a2afb611SJerry Jelinek { 1944a2afb611SJerry Jelinek dmu_object_info_t doi; 1945a2afb611SJerry Jelinek 1946a2afb611SJerry Jelinek dmu_object_info_from_db(dd->dd_dbuf, &doi); 1947a2afb611SJerry Jelinek return (doi.doi_type == DMU_OTN_ZAP_METADATA); 1948a2afb611SJerry Jelinek } 1949