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. 237d46dc6cSMatthew Ahrens * Copyright (c) 2012, 2014 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. 26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 2703b1c297SAlexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 28fa9e4066Sahrens */ 29fa9e4066Sahrens 30fa9e4066Sahrens #include <sys/dmu.h> 31a9799022Sck #include <sys/dmu_objset.h> 32fa9e4066Sahrens #include <sys/dmu_tx.h> 33fa9e4066Sahrens #include <sys/dsl_dataset.h> 34fa9e4066Sahrens #include <sys/dsl_dir.h> 35fa9e4066Sahrens #include <sys/dsl_prop.h> 361d452cf5Sahrens #include <sys/dsl_synctask.h> 37ecd6cf80Smarks #include <sys/dsl_deleg.h> 382acef22dSMatthew Ahrens #include <sys/dmu_impl.h> 39fa9e4066Sahrens #include <sys/spa.h> 40b24ab676SJeff Bonwick #include <sys/metaslab.h> 41fa9e4066Sahrens #include <sys/zap.h> 42fa9e4066Sahrens #include <sys/zio.h> 43fa9e4066Sahrens #include <sys/arc.h> 44ecd6cf80Smarks #include <sys/sunddi.h> 45a2afb611SJerry Jelinek #include <sys/zfeature.h> 46a2afb611SJerry Jelinek #include <sys/policy.h> 47a2afb611SJerry Jelinek #include <sys/zfs_znode.h> 48fa9e4066Sahrens #include "zfs_namecheck.h" 49a2afb611SJerry Jelinek #include "zfs_prop.h" 50a2afb611SJerry Jelinek 51a2afb611SJerry Jelinek /* 52a2afb611SJerry Jelinek * Filesystem and Snapshot Limits 53a2afb611SJerry Jelinek * ------------------------------ 54a2afb611SJerry Jelinek * 55a2afb611SJerry Jelinek * These limits are used to restrict the number of filesystems and/or snapshots 56a2afb611SJerry Jelinek * that can be created at a given level in the tree or below. A typical 57a2afb611SJerry Jelinek * use-case is with a delegated dataset where the administrator wants to ensure 58a2afb611SJerry Jelinek * that a user within the zone is not creating too many additional filesystems 59a2afb611SJerry Jelinek * or snapshots, even though they're not exceeding their space quota. 60a2afb611SJerry Jelinek * 61a2afb611SJerry Jelinek * The filesystem and snapshot counts are stored as extensible properties. This 62a2afb611SJerry Jelinek * capability is controlled by a feature flag and must be enabled to be used. 63a2afb611SJerry Jelinek * Once enabled, the feature is not active until the first limit is set. At 64a2afb611SJerry Jelinek * that point, future operations to create/destroy filesystems or snapshots 65a2afb611SJerry Jelinek * will validate and update the counts. 66a2afb611SJerry Jelinek * 67a2afb611SJerry Jelinek * Because the count properties will not exist before the feature is active, 68a2afb611SJerry Jelinek * the counts are updated when a limit is first set on an uninitialized 69a2afb611SJerry Jelinek * dsl_dir node in the tree (The filesystem/snapshot count on a node includes 70a2afb611SJerry Jelinek * all of the nested filesystems/snapshots. Thus, a new leaf node has a 71a2afb611SJerry Jelinek * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and 72a2afb611SJerry Jelinek * snapshot count properties on a node indicate uninitialized counts on that 73a2afb611SJerry Jelinek * node.) When first setting a limit on an uninitialized node, the code starts 74a2afb611SJerry Jelinek * at the filesystem with the new limit and descends into all sub-filesystems 75a2afb611SJerry Jelinek * to add the count properties. 76a2afb611SJerry Jelinek * 77a2afb611SJerry Jelinek * In practice this is lightweight since a limit is typically set when the 78a2afb611SJerry Jelinek * filesystem is created and thus has no children. Once valid, changing the 79a2afb611SJerry Jelinek * limit value won't require a re-traversal since the counts are already valid. 80a2afb611SJerry Jelinek * When recursively fixing the counts, if a node with a limit is encountered 81a2afb611SJerry Jelinek * during the descent, the counts are known to be valid and there is no need to 82a2afb611SJerry Jelinek * descend into that filesystem's children. The counts on filesystems above the 83a2afb611SJerry Jelinek * one with the new limit will still be uninitialized, unless a limit is 84a2afb611SJerry Jelinek * eventually set on one of those filesystems. The counts are always recursively 85a2afb611SJerry Jelinek * updated when a limit is set on a dataset, unless there is already a limit. 86a2afb611SJerry Jelinek * When a new limit value is set on a filesystem with an existing limit, it is 87a2afb611SJerry Jelinek * possible for the new limit to be less than the current count at that level 88a2afb611SJerry Jelinek * since a user who can change the limit is also allowed to exceed the limit. 89a2afb611SJerry Jelinek * 90a2afb611SJerry Jelinek * Once the feature is active, then whenever a filesystem or snapshot is 91a2afb611SJerry Jelinek * created, the code recurses up the tree, validating the new count against the 92a2afb611SJerry Jelinek * limit at each initialized level. In practice, most levels will not have a 93a2afb611SJerry Jelinek * limit set. If there is a limit at any initialized level up the tree, the 94a2afb611SJerry Jelinek * check must pass or the creation will fail. Likewise, when a filesystem or 95a2afb611SJerry Jelinek * snapshot is destroyed, the counts are recursively adjusted all the way up 96a2afb611SJerry Jelinek * the initizized nodes in the tree. Renaming a filesystem into different point 97a2afb611SJerry Jelinek * in the tree will first validate, then update the counts on each branch up to 98a2afb611SJerry Jelinek * the common ancestor. A receive will also validate the counts and then update 99a2afb611SJerry Jelinek * them. 100a2afb611SJerry Jelinek * 101a2afb611SJerry Jelinek * An exception to the above behavior is that the limit is not enforced if the 102a2afb611SJerry Jelinek * user has permission to modify the limit. This is primarily so that 103a2afb611SJerry Jelinek * recursive snapshots in the global zone always work. We want to prevent a 104a2afb611SJerry Jelinek * denial-of-service in which a lower level delegated dataset could max out its 105a2afb611SJerry Jelinek * limit and thus block recursive snapshots from being taken in the global zone. 106a2afb611SJerry Jelinek * Because of this, it is possible for the snapshot count to be over the limit 107a2afb611SJerry Jelinek * and snapshots taken in the global zone could cause a lower level dataset to 108a2afb611SJerry Jelinek * hit or exceed its limit. The administrator taking the global zone recursive 109a2afb611SJerry Jelinek * snapshot should be aware of this side-effect and behave accordingly. 110a2afb611SJerry Jelinek * For consistency, the filesystem limit is also not enforced if the user can 111a2afb611SJerry Jelinek * modify the limit. 112a2afb611SJerry Jelinek * 113a2afb611SJerry Jelinek * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check() 114a2afb611SJerry Jelinek * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in 115a2afb611SJerry Jelinek * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by 116a2afb611SJerry Jelinek * dsl_dir_init_fs_ss_count(). 117a2afb611SJerry Jelinek * 118a2afb611SJerry Jelinek * There is a special case when we receive a filesystem that already exists. In 119a2afb611SJerry Jelinek * this case a temporary clone name of %X is created (see dmu_recv_begin). We 120a2afb611SJerry Jelinek * never update the filesystem counts for temporary clones. 121a2afb611SJerry Jelinek * 122a2afb611SJerry Jelinek * Likewise, we do not update the snapshot counts for temporary snapshots, 123a2afb611SJerry Jelinek * such as those created by zfs diff. 124a2afb611SJerry Jelinek */ 125fa9e4066Sahrens 126c1379625SJustin T. Gibbs extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd); 127c1379625SJustin T. Gibbs 128a9799022Sck static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); 129fa9e4066Sahrens 130fa9e4066Sahrens static void 131bc9014e6SJustin Gibbs dsl_dir_evict(void *dbu) 132fa9e4066Sahrens { 133bc9014e6SJustin Gibbs dsl_dir_t *dd = dbu; 134fa9e4066Sahrens dsl_pool_t *dp = dd->dd_pool; 135fa9e4066Sahrens int t; 136fa9e4066Sahrens 137bc9014e6SJustin Gibbs dd->dd_dbuf = NULL; 138bc9014e6SJustin Gibbs 139fa9e4066Sahrens for (t = 0; t < TXG_SIZE; t++) { 140fa9e4066Sahrens ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); 141fa9e4066Sahrens ASSERT(dd->dd_tempreserved[t] == 0); 142fa9e4066Sahrens ASSERT(dd->dd_space_towrite[t] == 0); 143fa9e4066Sahrens } 144fa9e4066Sahrens 145fa9e4066Sahrens if (dd->dd_parent) 146bc9014e6SJustin Gibbs dsl_dir_async_rele(dd->dd_parent, dd); 147fa9e4066Sahrens 148bc9014e6SJustin Gibbs spa_async_close(dd->dd_pool->dp_spa, dd); 149fa9e4066Sahrens 150*03bad06fSJustin Gibbs dsl_prop_fini(dd); 1515ad82045Snd mutex_destroy(&dd->dd_lock); 152fa9e4066Sahrens kmem_free(dd, sizeof (dsl_dir_t)); 153fa9e4066Sahrens } 154fa9e4066Sahrens 155ea8dc4b6Seschrock int 1563b2aab18SMatthew Ahrens dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj, 157ea8dc4b6Seschrock const char *tail, void *tag, dsl_dir_t **ddp) 158fa9e4066Sahrens { 159fa9e4066Sahrens dmu_buf_t *dbuf; 160fa9e4066Sahrens dsl_dir_t *dd; 161ea8dc4b6Seschrock int err; 162fa9e4066Sahrens 1633b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 164fa9e4066Sahrens 165ea8dc4b6Seschrock err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); 1663b2aab18SMatthew Ahrens if (err != 0) 167ea8dc4b6Seschrock return (err); 168fa9e4066Sahrens dd = dmu_buf_get_user(dbuf); 169fa9e4066Sahrens #ifdef ZFS_DEBUG 170fa9e4066Sahrens { 171fa9e4066Sahrens dmu_object_info_t doi; 172fa9e4066Sahrens dmu_object_info_from_db(dbuf, &doi); 1732acef22dSMatthew Ahrens ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR); 17474e7dc98SMatthew Ahrens ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); 175fa9e4066Sahrens } 176fa9e4066Sahrens #endif 177fa9e4066Sahrens if (dd == NULL) { 178fa9e4066Sahrens dsl_dir_t *winner; 179fa9e4066Sahrens 180fa9e4066Sahrens dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); 181fa9e4066Sahrens dd->dd_object = ddobj; 182fa9e4066Sahrens dd->dd_dbuf = dbuf; 183fa9e4066Sahrens dd->dd_pool = dp; 1845ad82045Snd mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); 185*03bad06fSJustin Gibbs dsl_prop_init(dd); 186fa9e4066Sahrens 18771eb0538SChris Kirby dsl_dir_snap_cmtime_update(dd); 18871eb0538SChris Kirby 189c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_parent_obj) { 190c1379625SJustin T. Gibbs err = dsl_dir_hold_obj(dp, 191c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_parent_obj, NULL, dd, 192c1379625SJustin T. Gibbs &dd->dd_parent); 1933b2aab18SMatthew Ahrens if (err != 0) 19474e7dc98SMatthew Ahrens goto errout; 195fa9e4066Sahrens if (tail) { 196fa9e4066Sahrens #ifdef ZFS_DEBUG 197fa9e4066Sahrens uint64_t foundobj; 198fa9e4066Sahrens 199fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, 200c1379625SJustin T. Gibbs dsl_dir_phys(dd->dd_parent)-> 201c1379625SJustin T. Gibbs dd_child_dir_zapobj, tail, 202c1379625SJustin T. Gibbs 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, 208c1379625SJustin T. Gibbs dsl_dir_phys(dd->dd_parent)-> 209c1379625SJustin T. Gibbs dd_child_dir_zapobj, 210e7437265Sahrens ddobj, 0, dd->dd_myname); 211ea8dc4b6Seschrock } 2123b2aab18SMatthew Ahrens if (err != 0) 21374e7dc98SMatthew Ahrens goto errout; 214fa9e4066Sahrens } else { 215fa9e4066Sahrens (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); 216fa9e4066Sahrens } 217fa9e4066Sahrens 2183f9d6ad7SLin Ling if (dsl_dir_is_clone(dd)) { 2193f9d6ad7SLin Ling dmu_buf_t *origin_bonus; 2203f9d6ad7SLin Ling dsl_dataset_phys_t *origin_phys; 2213f9d6ad7SLin Ling 2223f9d6ad7SLin Ling /* 2233f9d6ad7SLin Ling * We can't open the origin dataset, because 2243f9d6ad7SLin Ling * that would require opening this dsl_dir. 2253f9d6ad7SLin Ling * Just look at its phys directly instead. 2263f9d6ad7SLin Ling */ 2273f9d6ad7SLin Ling err = dmu_bonus_hold(dp->dp_meta_objset, 228c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj, FTAG, 229c1379625SJustin T. Gibbs &origin_bonus); 2303b2aab18SMatthew Ahrens if (err != 0) 2313f9d6ad7SLin Ling goto errout; 2323f9d6ad7SLin Ling origin_phys = origin_bonus->db_data; 2333f9d6ad7SLin Ling dd->dd_origin_txg = 2343f9d6ad7SLin Ling origin_phys->ds_creation_txg; 2353f9d6ad7SLin Ling dmu_buf_rele(origin_bonus, FTAG); 2363f9d6ad7SLin Ling } 2373f9d6ad7SLin Ling 238bc9014e6SJustin Gibbs dmu_buf_init_user(&dd->dd_dbu, dsl_dir_evict, &dd->dd_dbuf); 239bc9014e6SJustin Gibbs winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu); 240bc9014e6SJustin Gibbs if (winner != NULL) { 241fa9e4066Sahrens if (dd->dd_parent) 2423b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd); 243*03bad06fSJustin Gibbs dsl_prop_fini(dd); 2445ad82045Snd mutex_destroy(&dd->dd_lock); 245fa9e4066Sahrens kmem_free(dd, sizeof (dsl_dir_t)); 246fa9e4066Sahrens dd = winner; 247fa9e4066Sahrens } else { 248fa9e4066Sahrens spa_open_ref(dp->dp_spa, dd); 249fa9e4066Sahrens } 250fa9e4066Sahrens } 251fa9e4066Sahrens 252fa9e4066Sahrens /* 253fa9e4066Sahrens * The dsl_dir_t has both open-to-close and instantiate-to-evict 254fa9e4066Sahrens * holds on the spa. We need the open-to-close holds because 255fa9e4066Sahrens * otherwise the spa_refcnt wouldn't change when we open a 256fa9e4066Sahrens * dir which the spa also has open, so we could incorrectly 257fa9e4066Sahrens * think it was OK to unload/export/destroy the pool. We need 258fa9e4066Sahrens * the instantiate-to-evict hold because the dsl_dir_t has a 259fa9e4066Sahrens * pointer to the dd_pool, which has a pointer to the spa_t. 260fa9e4066Sahrens */ 261fa9e4066Sahrens spa_open_ref(dp->dp_spa, tag); 262fa9e4066Sahrens ASSERT3P(dd->dd_pool, ==, dp); 263fa9e4066Sahrens ASSERT3U(dd->dd_object, ==, ddobj); 264fa9e4066Sahrens ASSERT3P(dd->dd_dbuf, ==, dbuf); 265ea8dc4b6Seschrock *ddp = dd; 266ea8dc4b6Seschrock return (0); 26774e7dc98SMatthew Ahrens 26874e7dc98SMatthew Ahrens errout: 26974e7dc98SMatthew Ahrens if (dd->dd_parent) 2703b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd); 271*03bad06fSJustin Gibbs dsl_prop_fini(dd); 27274e7dc98SMatthew Ahrens mutex_destroy(&dd->dd_lock); 27374e7dc98SMatthew Ahrens kmem_free(dd, sizeof (dsl_dir_t)); 27474e7dc98SMatthew Ahrens dmu_buf_rele(dbuf, tag); 27574e7dc98SMatthew Ahrens return (err); 276fa9e4066Sahrens } 277fa9e4066Sahrens 278fa9e4066Sahrens void 2793b2aab18SMatthew Ahrens dsl_dir_rele(dsl_dir_t *dd, void *tag) 280fa9e4066Sahrens { 281fa9e4066Sahrens dprintf_dd(dd, "%s\n", ""); 282fa9e4066Sahrens spa_close(dd->dd_pool->dp_spa, tag); 283ea8dc4b6Seschrock dmu_buf_rele(dd->dd_dbuf, tag); 284fa9e4066Sahrens } 285fa9e4066Sahrens 286bc9014e6SJustin Gibbs /* 287bc9014e6SJustin Gibbs * Remove a reference to the given dsl dir that is being asynchronously 288bc9014e6SJustin Gibbs * released. Async releases occur from a taskq performing eviction of 289bc9014e6SJustin Gibbs * dsl datasets and dirs. This process is identical to a normal release 290bc9014e6SJustin Gibbs * with the exception of using the async API for releasing the reference on 291bc9014e6SJustin Gibbs * the spa. 292bc9014e6SJustin Gibbs */ 293bc9014e6SJustin Gibbs void 294bc9014e6SJustin Gibbs dsl_dir_async_rele(dsl_dir_t *dd, void *tag) 295bc9014e6SJustin Gibbs { 296bc9014e6SJustin Gibbs dprintf_dd(dd, "%s\n", ""); 297bc9014e6SJustin Gibbs spa_async_close(dd->dd_pool->dp_spa, tag); 298bc9014e6SJustin Gibbs dmu_buf_rele(dd->dd_dbuf, tag); 299bc9014e6SJustin Gibbs } 300bc9014e6SJustin Gibbs 30115f66a7fSek /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */ 302fa9e4066Sahrens void 303fa9e4066Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf) 304fa9e4066Sahrens { 305fa9e4066Sahrens if (dd->dd_parent) { 306fa9e4066Sahrens dsl_dir_name(dd->dd_parent, buf); 307fa9e4066Sahrens (void) strcat(buf, "/"); 308fa9e4066Sahrens } else { 309fa9e4066Sahrens buf[0] = '\0'; 310fa9e4066Sahrens } 311fa9e4066Sahrens if (!MUTEX_HELD(&dd->dd_lock)) { 312fa9e4066Sahrens /* 313fa9e4066Sahrens * recursive mutex so that we can use 314fa9e4066Sahrens * dprintf_dd() with dd_lock held 315fa9e4066Sahrens */ 316fa9e4066Sahrens mutex_enter(&dd->dd_lock); 317fa9e4066Sahrens (void) strcat(buf, dd->dd_myname); 318fa9e4066Sahrens mutex_exit(&dd->dd_lock); 319fa9e4066Sahrens } else { 320fa9e4066Sahrens (void) strcat(buf, dd->dd_myname); 321fa9e4066Sahrens } 322fa9e4066Sahrens } 323fa9e4066Sahrens 324ce636f8bSMatthew Ahrens /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */ 325b7661cccSmmusante int 326b7661cccSmmusante dsl_dir_namelen(dsl_dir_t *dd) 327b7661cccSmmusante { 328b7661cccSmmusante int result = 0; 329b7661cccSmmusante 330b7661cccSmmusante if (dd->dd_parent) { 331b7661cccSmmusante /* parent's name + 1 for the "/" */ 332b7661cccSmmusante result = dsl_dir_namelen(dd->dd_parent) + 1; 333b7661cccSmmusante } 334b7661cccSmmusante 335b7661cccSmmusante if (!MUTEX_HELD(&dd->dd_lock)) { 336b7661cccSmmusante /* see dsl_dir_name */ 337b7661cccSmmusante mutex_enter(&dd->dd_lock); 338b7661cccSmmusante result += strlen(dd->dd_myname); 339b7661cccSmmusante mutex_exit(&dd->dd_lock); 340b7661cccSmmusante } else { 341b7661cccSmmusante result += strlen(dd->dd_myname); 342b7661cccSmmusante } 343b7661cccSmmusante 344b7661cccSmmusante return (result); 345b7661cccSmmusante } 346b7661cccSmmusante 347fa9e4066Sahrens static int 348fa9e4066Sahrens getcomponent(const char *path, char *component, const char **nextp) 349fa9e4066Sahrens { 350fa9e4066Sahrens char *p; 3513b2aab18SMatthew Ahrens 352ccba0801SRich Morris if ((path == NULL) || (path[0] == '\0')) 353be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 354fa9e4066Sahrens /* This would be a good place to reserve some namespace... */ 355fa9e4066Sahrens p = strpbrk(path, "/@"); 356fa9e4066Sahrens if (p && (p[1] == '/' || p[1] == '@')) { 357fa9e4066Sahrens /* two separators in a row */ 358be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 359fa9e4066Sahrens } 360fa9e4066Sahrens if (p == NULL || p == path) { 361fa9e4066Sahrens /* 362fa9e4066Sahrens * if the first thing is an @ or /, it had better be an 363fa9e4066Sahrens * @ and it had better not have any more ats or slashes, 364fa9e4066Sahrens * and it had better have something after the @. 365fa9e4066Sahrens */ 366fa9e4066Sahrens if (p != NULL && 367fa9e4066Sahrens (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) 368be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 369fa9e4066Sahrens if (strlen(path) >= MAXNAMELEN) 370be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 371fa9e4066Sahrens (void) strcpy(component, path); 372fa9e4066Sahrens p = NULL; 373fa9e4066Sahrens } else if (p[0] == '/') { 3743b2aab18SMatthew Ahrens if (p - path >= MAXNAMELEN) 375be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 376fa9e4066Sahrens (void) strncpy(component, path, p - path); 3773b2aab18SMatthew Ahrens component[p - path] = '\0'; 378fa9e4066Sahrens p++; 379fa9e4066Sahrens } else if (p[0] == '@') { 380fa9e4066Sahrens /* 381fa9e4066Sahrens * if the next separator is an @, there better not be 382fa9e4066Sahrens * any more slashes. 383fa9e4066Sahrens */ 384fa9e4066Sahrens if (strchr(path, '/')) 385be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3863b2aab18SMatthew Ahrens if (p - path >= MAXNAMELEN) 387be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 388fa9e4066Sahrens (void) strncpy(component, path, p - path); 3893b2aab18SMatthew Ahrens component[p - path] = '\0'; 390fa9e4066Sahrens } else { 3913b2aab18SMatthew Ahrens panic("invalid p=%p", (void *)p); 392fa9e4066Sahrens } 393fa9e4066Sahrens *nextp = p; 394fa9e4066Sahrens return (0); 395fa9e4066Sahrens } 396fa9e4066Sahrens 397fa9e4066Sahrens /* 3983b2aab18SMatthew Ahrens * Return the dsl_dir_t, and possibly the last component which couldn't 3993b2aab18SMatthew Ahrens * be found in *tail. The name must be in the specified dsl_pool_t. This 4003b2aab18SMatthew Ahrens * thread must hold the dp_config_rwlock for the pool. Returns NULL if the 4013b2aab18SMatthew Ahrens * path is bogus, or if tail==NULL and we couldn't parse the whole name. 4023b2aab18SMatthew Ahrens * (*tail)[0] == '@' means that the last component is a snapshot. 403fa9e4066Sahrens */ 404ea8dc4b6Seschrock int 4053b2aab18SMatthew Ahrens dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag, 406ea8dc4b6Seschrock dsl_dir_t **ddp, const char **tailp) 407fa9e4066Sahrens { 408fa9e4066Sahrens char buf[MAXNAMELEN]; 4093b2aab18SMatthew Ahrens const char *spaname, *next, *nextnext = NULL; 410fa9e4066Sahrens int err; 411fa9e4066Sahrens dsl_dir_t *dd; 412fa9e4066Sahrens uint64_t ddobj; 413fa9e4066Sahrens 414fa9e4066Sahrens err = getcomponent(name, buf, &next); 4153b2aab18SMatthew Ahrens if (err != 0) 416ea8dc4b6Seschrock return (err); 417fa9e4066Sahrens 4183b2aab18SMatthew Ahrens /* Make sure the name is in the specified pool. */ 4193b2aab18SMatthew Ahrens spaname = spa_name(dp->dp_spa); 4203b2aab18SMatthew Ahrens if (strcmp(buf, spaname) != 0) 42103b1c297SAlexander Eremin return (SET_ERROR(EXDEV)); 422fa9e4066Sahrens 4233b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 424fa9e4066Sahrens 4253b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); 4263b2aab18SMatthew Ahrens if (err != 0) { 427ea8dc4b6Seschrock return (err); 428ea8dc4b6Seschrock } 429ea8dc4b6Seschrock 430fa9e4066Sahrens while (next != NULL) { 431bc9014e6SJustin Gibbs dsl_dir_t *child_dd; 432fa9e4066Sahrens err = getcomponent(next, buf, &nextnext); 4333b2aab18SMatthew Ahrens if (err != 0) 434ea8dc4b6Seschrock break; 435fa9e4066Sahrens ASSERT(next[0] != '\0'); 436fa9e4066Sahrens if (next[0] == '@') 437fa9e4066Sahrens break; 438fa9e4066Sahrens dprintf("looking up %s in obj%lld\n", 439c1379625SJustin T. Gibbs buf, dsl_dir_phys(dd)->dd_child_dir_zapobj); 440fa9e4066Sahrens 441fa9e4066Sahrens err = zap_lookup(dp->dp_meta_objset, 442c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_child_dir_zapobj, 443fa9e4066Sahrens buf, sizeof (ddobj), 1, &ddobj); 4443b2aab18SMatthew Ahrens if (err != 0) { 445ea8dc4b6Seschrock if (err == ENOENT) 446ea8dc4b6Seschrock err = 0; 447fa9e4066Sahrens break; 448fa9e4066Sahrens } 449fa9e4066Sahrens 450bc9014e6SJustin Gibbs err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd); 4513b2aab18SMatthew Ahrens if (err != 0) 452ea8dc4b6Seschrock break; 4533b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag); 454bc9014e6SJustin Gibbs dd = child_dd; 455fa9e4066Sahrens next = nextnext; 456fa9e4066Sahrens } 457fa9e4066Sahrens 4583b2aab18SMatthew Ahrens if (err != 0) { 4593b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag); 460ea8dc4b6Seschrock return (err); 461ea8dc4b6Seschrock } 462ea8dc4b6Seschrock 463fa9e4066Sahrens /* 464fa9e4066Sahrens * It's an error if there's more than one component left, or 465fa9e4066Sahrens * tailp==NULL and there's any component left. 466fa9e4066Sahrens */ 467fa9e4066Sahrens if (next != NULL && 468fa9e4066Sahrens (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { 469fa9e4066Sahrens /* bad path name */ 4703b2aab18SMatthew Ahrens dsl_dir_rele(dd, tag); 471fa9e4066Sahrens dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); 472be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 473fa9e4066Sahrens } 4743b2aab18SMatthew Ahrens if (tailp != NULL) 475fa9e4066Sahrens *tailp = next; 476ea8dc4b6Seschrock *ddp = dd; 477ea8dc4b6Seschrock return (err); 478fa9e4066Sahrens } 479fa9e4066Sahrens 480a2afb611SJerry Jelinek /* 481a2afb611SJerry Jelinek * If the counts are already initialized for this filesystem and its 482a2afb611SJerry Jelinek * descendants then do nothing, otherwise initialize the counts. 483a2afb611SJerry Jelinek * 484a2afb611SJerry Jelinek * The counts on this filesystem, and those below, may be uninitialized due to 485a2afb611SJerry Jelinek * either the use of a pre-existing pool which did not support the 486a2afb611SJerry Jelinek * filesystem/snapshot limit feature, or one in which the feature had not yet 487a2afb611SJerry Jelinek * been enabled. 488a2afb611SJerry Jelinek * 489a2afb611SJerry Jelinek * Recursively descend the filesystem tree and update the filesystem/snapshot 490a2afb611SJerry Jelinek * counts on each filesystem below, then update the cumulative count on the 491a2afb611SJerry Jelinek * current filesystem. If the filesystem already has a count set on it, 492a2afb611SJerry Jelinek * then we know that its counts, and the counts on the filesystems below it, 493a2afb611SJerry Jelinek * are already correct, so we don't have to update this filesystem. 494a2afb611SJerry Jelinek */ 495a2afb611SJerry Jelinek static void 496a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx) 497a2afb611SJerry Jelinek { 498a2afb611SJerry Jelinek uint64_t my_fs_cnt = 0; 499a2afb611SJerry Jelinek uint64_t my_ss_cnt = 0; 500a2afb611SJerry Jelinek dsl_pool_t *dp = dd->dd_pool; 501a2afb611SJerry Jelinek objset_t *os = dp->dp_meta_objset; 502a2afb611SJerry Jelinek zap_cursor_t *zc; 503a2afb611SJerry Jelinek zap_attribute_t *za; 504a2afb611SJerry Jelinek dsl_dataset_t *ds; 505a2afb611SJerry Jelinek 506adf34077SJerry Jelinek ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)); 507a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dp)); 508a2afb611SJerry Jelinek ASSERT(dmu_tx_is_syncing(tx)); 509a2afb611SJerry Jelinek 510a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx); 511a2afb611SJerry Jelinek 512a2afb611SJerry Jelinek /* 513a2afb611SJerry Jelinek * If the filesystem count has already been initialized then we 514a2afb611SJerry Jelinek * don't need to recurse down any further. 515a2afb611SJerry Jelinek */ 516a2afb611SJerry Jelinek if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0) 517a2afb611SJerry Jelinek return; 518a2afb611SJerry Jelinek 519a2afb611SJerry Jelinek zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); 520a2afb611SJerry Jelinek za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 521a2afb611SJerry Jelinek 522a2afb611SJerry Jelinek /* Iterate my child dirs */ 523c1379625SJustin T. Gibbs for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj); 524a2afb611SJerry Jelinek zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) { 525a2afb611SJerry Jelinek dsl_dir_t *chld_dd; 526a2afb611SJerry Jelinek uint64_t count; 527a2afb611SJerry Jelinek 528a2afb611SJerry Jelinek VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG, 529a2afb611SJerry Jelinek &chld_dd)); 530a2afb611SJerry Jelinek 531a2afb611SJerry Jelinek /* 532a2afb611SJerry Jelinek * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and 533a2afb611SJerry Jelinek * temporary datasets. 534a2afb611SJerry Jelinek */ 535a2afb611SJerry Jelinek if (chld_dd->dd_myname[0] == '$' || 536a2afb611SJerry Jelinek chld_dd->dd_myname[0] == '%') { 537a2afb611SJerry Jelinek dsl_dir_rele(chld_dd, FTAG); 538a2afb611SJerry Jelinek continue; 539a2afb611SJerry Jelinek } 540a2afb611SJerry Jelinek 541a2afb611SJerry Jelinek my_fs_cnt++; /* count this child */ 542a2afb611SJerry Jelinek 543a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(chld_dd, tx); 544a2afb611SJerry Jelinek 545a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, chld_dd->dd_object, 546a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count)); 547a2afb611SJerry Jelinek my_fs_cnt += count; 548a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, chld_dd->dd_object, 549a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count)); 550a2afb611SJerry Jelinek my_ss_cnt += count; 551a2afb611SJerry Jelinek 552a2afb611SJerry Jelinek dsl_dir_rele(chld_dd, FTAG); 553a2afb611SJerry Jelinek } 554a2afb611SJerry Jelinek zap_cursor_fini(zc); 555a2afb611SJerry Jelinek /* Count my snapshots (we counted children's snapshots above) */ 556a2afb611SJerry Jelinek VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 557c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds)); 558a2afb611SJerry Jelinek 559c1379625SJustin T. Gibbs for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj); 560a2afb611SJerry Jelinek zap_cursor_retrieve(zc, za) == 0; 561a2afb611SJerry Jelinek zap_cursor_advance(zc)) { 562a2afb611SJerry Jelinek /* Don't count temporary snapshots */ 563a2afb611SJerry Jelinek if (za->za_name[0] != '%') 564a2afb611SJerry Jelinek my_ss_cnt++; 565a2afb611SJerry Jelinek } 566fb7001f1SAlex Reece zap_cursor_fini(zc); 567a2afb611SJerry Jelinek 568a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 569a2afb611SJerry Jelinek 570a2afb611SJerry Jelinek kmem_free(zc, sizeof (zap_cursor_t)); 571a2afb611SJerry Jelinek kmem_free(za, sizeof (zap_attribute_t)); 572a2afb611SJerry Jelinek 573a2afb611SJerry Jelinek /* we're in a sync task, update counts */ 574a2afb611SJerry Jelinek dmu_buf_will_dirty(dd->dd_dbuf, tx); 575a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 576a2afb611SJerry Jelinek sizeof (my_fs_cnt), 1, &my_fs_cnt, tx)); 577a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 578a2afb611SJerry Jelinek sizeof (my_ss_cnt), 1, &my_ss_cnt, tx)); 579a2afb611SJerry Jelinek } 580a2afb611SJerry Jelinek 581a2afb611SJerry Jelinek static int 582a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx) 583a2afb611SJerry Jelinek { 584a2afb611SJerry Jelinek char *ddname = (char *)arg; 585a2afb611SJerry Jelinek dsl_pool_t *dp = dmu_tx_pool(tx); 586a2afb611SJerry Jelinek dsl_dataset_t *ds; 587a2afb611SJerry Jelinek dsl_dir_t *dd; 588a2afb611SJerry Jelinek int error; 589a2afb611SJerry Jelinek 590a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, ddname, FTAG, &ds); 591a2afb611SJerry Jelinek if (error != 0) 592a2afb611SJerry Jelinek return (error); 593a2afb611SJerry Jelinek 594a2afb611SJerry Jelinek if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 595a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 596a2afb611SJerry Jelinek return (SET_ERROR(ENOTSUP)); 597a2afb611SJerry Jelinek } 598a2afb611SJerry Jelinek 599a2afb611SJerry Jelinek dd = ds->ds_dir; 600a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) && 601a2afb611SJerry Jelinek dsl_dir_is_zapified(dd) && 602a2afb611SJerry Jelinek zap_contains(dp->dp_meta_objset, dd->dd_object, 603a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT) == 0) { 604a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 605a2afb611SJerry Jelinek return (SET_ERROR(EALREADY)); 606a2afb611SJerry Jelinek } 607a2afb611SJerry Jelinek 608a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 609a2afb611SJerry Jelinek return (0); 610a2afb611SJerry Jelinek } 611a2afb611SJerry Jelinek 612a2afb611SJerry Jelinek static void 613a2afb611SJerry Jelinek dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx) 614a2afb611SJerry Jelinek { 615a2afb611SJerry Jelinek char *ddname = (char *)arg; 616a2afb611SJerry Jelinek dsl_pool_t *dp = dmu_tx_pool(tx); 617a2afb611SJerry Jelinek dsl_dataset_t *ds; 618a2afb611SJerry Jelinek spa_t *spa; 619a2afb611SJerry Jelinek 620a2afb611SJerry Jelinek VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds)); 621a2afb611SJerry Jelinek 622a2afb611SJerry Jelinek spa = dsl_dataset_get_spa(ds); 623a2afb611SJerry Jelinek 624a2afb611SJerry Jelinek if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) { 625a2afb611SJerry Jelinek /* 626a2afb611SJerry Jelinek * Since the feature was not active and we're now setting a 627a2afb611SJerry Jelinek * limit, increment the feature-active counter so that the 628a2afb611SJerry Jelinek * feature becomes active for the first time. 629a2afb611SJerry Jelinek * 630a2afb611SJerry Jelinek * We are already in a sync task so we can update the MOS. 631a2afb611SJerry Jelinek */ 632a2afb611SJerry Jelinek spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx); 633a2afb611SJerry Jelinek } 634a2afb611SJerry Jelinek 635a2afb611SJerry Jelinek /* 636a2afb611SJerry Jelinek * Since we are now setting a non-UINT64_MAX limit on the filesystem, 637a2afb611SJerry Jelinek * we need to ensure the counts are correct. Descend down the tree from 638a2afb611SJerry Jelinek * this point and update all of the counts to be accurate. 639a2afb611SJerry Jelinek */ 640a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(ds->ds_dir, tx); 641a2afb611SJerry Jelinek 642a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 643a2afb611SJerry Jelinek } 644a2afb611SJerry Jelinek 645a2afb611SJerry Jelinek /* 646a2afb611SJerry Jelinek * Make sure the feature is enabled and activate it if necessary. 647a2afb611SJerry Jelinek * Since we're setting a limit, ensure the on-disk counts are valid. 648a2afb611SJerry Jelinek * This is only called by the ioctl path when setting a limit value. 649a2afb611SJerry Jelinek * 650a2afb611SJerry Jelinek * We do not need to validate the new limit, since users who can change the 651a2afb611SJerry Jelinek * limit are also allowed to exceed the limit. 652a2afb611SJerry Jelinek */ 653a2afb611SJerry Jelinek int 654a2afb611SJerry Jelinek dsl_dir_activate_fs_ss_limit(const char *ddname) 655a2afb611SJerry Jelinek { 656a2afb611SJerry Jelinek int error; 657a2afb611SJerry Jelinek 658a2afb611SJerry Jelinek error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check, 6597d46dc6cSMatthew Ahrens dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0, 6607d46dc6cSMatthew Ahrens ZFS_SPACE_CHECK_RESERVED); 661a2afb611SJerry Jelinek 662a2afb611SJerry Jelinek if (error == EALREADY) 663a2afb611SJerry Jelinek error = 0; 664a2afb611SJerry Jelinek 665a2afb611SJerry Jelinek return (error); 666a2afb611SJerry Jelinek } 667a2afb611SJerry Jelinek 668a2afb611SJerry Jelinek /* 669a2afb611SJerry Jelinek * Used to determine if the filesystem_limit or snapshot_limit should be 670a2afb611SJerry Jelinek * enforced. We allow the limit to be exceeded if the user has permission to 671a2afb611SJerry Jelinek * write the property value. We pass in the creds that we got in the open 672a2afb611SJerry Jelinek * context since we will always be the GZ root in syncing context. We also have 673a2afb611SJerry Jelinek * to handle the case where we are allowed to change the limit on the current 674a2afb611SJerry Jelinek * dataset, but there may be another limit in the tree above. 675a2afb611SJerry Jelinek * 676a2afb611SJerry Jelinek * We can never modify these two properties within a non-global zone. In 677a2afb611SJerry Jelinek * addition, the other checks are modeled on zfs_secpolicy_write_perms. We 678a2afb611SJerry Jelinek * can't use that function since we are already holding the dp_config_rwlock. 679a2afb611SJerry Jelinek * In addition, we already have the dd and dealing with snapshots is simplified 680a2afb611SJerry Jelinek * in this code. 681a2afb611SJerry Jelinek */ 682a2afb611SJerry Jelinek 683a2afb611SJerry Jelinek typedef enum { 684a2afb611SJerry Jelinek ENFORCE_ALWAYS, 685a2afb611SJerry Jelinek ENFORCE_NEVER, 686a2afb611SJerry Jelinek ENFORCE_ABOVE 687a2afb611SJerry Jelinek } enforce_res_t; 688a2afb611SJerry Jelinek 689a2afb611SJerry Jelinek static enforce_res_t 690a2afb611SJerry Jelinek dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr) 691a2afb611SJerry Jelinek { 692a2afb611SJerry Jelinek enforce_res_t enforce = ENFORCE_ALWAYS; 693a2afb611SJerry Jelinek uint64_t obj; 694a2afb611SJerry Jelinek dsl_dataset_t *ds; 695a2afb611SJerry Jelinek uint64_t zoned; 696a2afb611SJerry Jelinek 697a2afb611SJerry Jelinek ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 698a2afb611SJerry Jelinek prop == ZFS_PROP_SNAPSHOT_LIMIT); 699a2afb611SJerry Jelinek 700a2afb611SJerry Jelinek #ifdef _KERNEL 701a2afb611SJerry Jelinek if (crgetzoneid(cr) != GLOBAL_ZONEID) 702a2afb611SJerry Jelinek return (ENFORCE_ALWAYS); 703a2afb611SJerry Jelinek 704a2afb611SJerry Jelinek if (secpolicy_zfs(cr) == 0) 705a2afb611SJerry Jelinek return (ENFORCE_NEVER); 706a2afb611SJerry Jelinek #endif 707a2afb611SJerry Jelinek 708c1379625SJustin T. Gibbs if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0) 709a2afb611SJerry Jelinek return (ENFORCE_ALWAYS); 710a2afb611SJerry Jelinek 711a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool)); 712a2afb611SJerry Jelinek 713a2afb611SJerry Jelinek if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0) 714a2afb611SJerry Jelinek return (ENFORCE_ALWAYS); 715a2afb611SJerry Jelinek 716a2afb611SJerry Jelinek if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) { 717a2afb611SJerry Jelinek /* Only root can access zoned fs's from the GZ */ 718a2afb611SJerry Jelinek enforce = ENFORCE_ALWAYS; 719a2afb611SJerry Jelinek } else { 720a2afb611SJerry Jelinek if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0) 721a2afb611SJerry Jelinek enforce = ENFORCE_ABOVE; 722a2afb611SJerry Jelinek } 723a2afb611SJerry Jelinek 724a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 725a2afb611SJerry Jelinek return (enforce); 726a2afb611SJerry Jelinek } 727a2afb611SJerry Jelinek 728a2afb611SJerry Jelinek /* 729a2afb611SJerry Jelinek * Check if adding additional child filesystem(s) would exceed any filesystem 730a2afb611SJerry Jelinek * limits or adding additional snapshot(s) would exceed any snapshot limits. 731a2afb611SJerry Jelinek * The prop argument indicates which limit to check. 732a2afb611SJerry Jelinek * 733a2afb611SJerry Jelinek * Note that all filesystem limits up to the root (or the highest 734a2afb611SJerry Jelinek * initialized) filesystem or the given ancestor must be satisfied. 735a2afb611SJerry Jelinek */ 736a2afb611SJerry Jelinek int 737a2afb611SJerry Jelinek dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop, 738a2afb611SJerry Jelinek dsl_dir_t *ancestor, cred_t *cr) 739a2afb611SJerry Jelinek { 740a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 741a2afb611SJerry Jelinek uint64_t limit, count; 742a2afb611SJerry Jelinek char *count_prop; 743a2afb611SJerry Jelinek enforce_res_t enforce; 744a2afb611SJerry Jelinek int err = 0; 745a2afb611SJerry Jelinek 746a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool)); 747a2afb611SJerry Jelinek ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || 748a2afb611SJerry Jelinek prop == ZFS_PROP_SNAPSHOT_LIMIT); 749a2afb611SJerry Jelinek 750a2afb611SJerry Jelinek /* 751a2afb611SJerry Jelinek * If we're allowed to change the limit, don't enforce the limit 752a2afb611SJerry Jelinek * e.g. this can happen if a snapshot is taken by an administrative 753a2afb611SJerry Jelinek * user in the global zone (i.e. a recursive snapshot by root). 754a2afb611SJerry Jelinek * However, we must handle the case of delegated permissions where we 755a2afb611SJerry Jelinek * are allowed to change the limit on the current dataset, but there 756a2afb611SJerry Jelinek * is another limit in the tree above. 757a2afb611SJerry Jelinek */ 758a2afb611SJerry Jelinek enforce = dsl_enforce_ds_ss_limits(dd, prop, cr); 759a2afb611SJerry Jelinek if (enforce == ENFORCE_NEVER) 760a2afb611SJerry Jelinek return (0); 761a2afb611SJerry Jelinek 762a2afb611SJerry Jelinek /* 763a2afb611SJerry Jelinek * e.g. if renaming a dataset with no snapshots, count adjustment 764a2afb611SJerry Jelinek * is 0. 765a2afb611SJerry Jelinek */ 766a2afb611SJerry Jelinek if (delta == 0) 767a2afb611SJerry Jelinek return (0); 768a2afb611SJerry Jelinek 769a2afb611SJerry Jelinek if (prop == ZFS_PROP_SNAPSHOT_LIMIT) { 770a2afb611SJerry Jelinek /* 771a2afb611SJerry Jelinek * We don't enforce the limit for temporary snapshots. This is 772a2afb611SJerry Jelinek * indicated by a NULL cred_t argument. 773a2afb611SJerry Jelinek */ 774a2afb611SJerry Jelinek if (cr == NULL) 775a2afb611SJerry Jelinek return (0); 776a2afb611SJerry Jelinek 777a2afb611SJerry Jelinek count_prop = DD_FIELD_SNAPSHOT_COUNT; 778a2afb611SJerry Jelinek } else { 779a2afb611SJerry Jelinek count_prop = DD_FIELD_FILESYSTEM_COUNT; 780a2afb611SJerry Jelinek } 781a2afb611SJerry Jelinek 782a2afb611SJerry Jelinek /* 783a2afb611SJerry Jelinek * If an ancestor has been provided, stop checking the limit once we 784a2afb611SJerry Jelinek * hit that dir. We need this during rename so that we don't overcount 785a2afb611SJerry Jelinek * the check once we recurse up to the common ancestor. 786a2afb611SJerry Jelinek */ 787a2afb611SJerry Jelinek if (ancestor == dd) 788a2afb611SJerry Jelinek return (0); 789a2afb611SJerry Jelinek 790a2afb611SJerry Jelinek /* 791a2afb611SJerry Jelinek * If we hit an uninitialized node while recursing up the tree, we can 792a2afb611SJerry Jelinek * stop since we know there is no limit here (or above). The counts are 793a2afb611SJerry Jelinek * not valid on this node and we know we won't touch this node's counts. 794a2afb611SJerry Jelinek */ 795a2afb611SJerry Jelinek if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object, 796a2afb611SJerry Jelinek count_prop, sizeof (count), 1, &count) == ENOENT) 797a2afb611SJerry Jelinek return (0); 798a2afb611SJerry Jelinek 799a2afb611SJerry Jelinek err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL, 800a2afb611SJerry Jelinek B_FALSE); 801a2afb611SJerry Jelinek if (err != 0) 802a2afb611SJerry Jelinek return (err); 803a2afb611SJerry Jelinek 804a2afb611SJerry Jelinek /* Is there a limit which we've hit? */ 805a2afb611SJerry Jelinek if (enforce == ENFORCE_ALWAYS && (count + delta) > limit) 806a2afb611SJerry Jelinek return (SET_ERROR(EDQUOT)); 807a2afb611SJerry Jelinek 808a2afb611SJerry Jelinek if (dd->dd_parent != NULL) 809a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop, 810a2afb611SJerry Jelinek ancestor, cr); 811a2afb611SJerry Jelinek 812a2afb611SJerry Jelinek return (err); 813a2afb611SJerry Jelinek } 814a2afb611SJerry Jelinek 815a2afb611SJerry Jelinek /* 816a2afb611SJerry Jelinek * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all 817a2afb611SJerry Jelinek * parents. When a new filesystem/snapshot is created, increment the count on 818a2afb611SJerry Jelinek * all parents, and when a filesystem/snapshot is destroyed, decrement the 819a2afb611SJerry Jelinek * count. 820a2afb611SJerry Jelinek */ 821a2afb611SJerry Jelinek void 822a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop, 823a2afb611SJerry Jelinek dmu_tx_t *tx) 824a2afb611SJerry Jelinek { 825a2afb611SJerry Jelinek int err; 826a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 827a2afb611SJerry Jelinek uint64_t count; 828a2afb611SJerry Jelinek 829a2afb611SJerry Jelinek ASSERT(dsl_pool_config_held(dd->dd_pool)); 830a2afb611SJerry Jelinek ASSERT(dmu_tx_is_syncing(tx)); 831a2afb611SJerry Jelinek ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 || 832a2afb611SJerry Jelinek strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0); 833a2afb611SJerry Jelinek 834a2afb611SJerry Jelinek /* 835a2afb611SJerry Jelinek * When we receive an incremental stream into a filesystem that already 836a2afb611SJerry Jelinek * exists, a temporary clone is created. We don't count this temporary 837a2afb611SJerry Jelinek * clone, whose name begins with a '%'. We also ignore hidden ($FREE, 838a2afb611SJerry Jelinek * $MOS & $ORIGIN) objsets. 839a2afb611SJerry Jelinek */ 840a2afb611SJerry Jelinek if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') && 841a2afb611SJerry Jelinek strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0) 842a2afb611SJerry Jelinek return; 843a2afb611SJerry Jelinek 844a2afb611SJerry Jelinek /* 845a2afb611SJerry Jelinek * e.g. if renaming a dataset with no snapshots, count adjustment is 0 846a2afb611SJerry Jelinek */ 847a2afb611SJerry Jelinek if (delta == 0) 848a2afb611SJerry Jelinek return; 849a2afb611SJerry Jelinek 850a2afb611SJerry Jelinek /* 851a2afb611SJerry Jelinek * If we hit an uninitialized node while recursing up the tree, we can 852a2afb611SJerry Jelinek * stop since we know the counts are not valid on this node and we 853a2afb611SJerry Jelinek * know we shouldn't touch this node's counts. An uninitialized count 854a2afb611SJerry Jelinek * on the node indicates that either the feature has not yet been 855a2afb611SJerry Jelinek * activated or there are no limits on this part of the tree. 856a2afb611SJerry Jelinek */ 857a2afb611SJerry Jelinek if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object, 858a2afb611SJerry Jelinek prop, sizeof (count), 1, &count)) == ENOENT) 859a2afb611SJerry Jelinek return; 860a2afb611SJerry Jelinek VERIFY0(err); 861a2afb611SJerry Jelinek 862a2afb611SJerry Jelinek count += delta; 863a2afb611SJerry Jelinek /* Use a signed verify to make sure we're not neg. */ 864a2afb611SJerry Jelinek VERIFY3S(count, >=, 0); 865a2afb611SJerry Jelinek 866a2afb611SJerry Jelinek VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count, 867a2afb611SJerry Jelinek tx)); 868a2afb611SJerry Jelinek 869a2afb611SJerry Jelinek /* Roll up this additional count into our ancestors */ 870a2afb611SJerry Jelinek if (dd->dd_parent != NULL) 871a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx); 872a2afb611SJerry Jelinek } 873a2afb611SJerry Jelinek 8741d452cf5Sahrens uint64_t 875088f3894Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, 876088f3894Sahrens dmu_tx_t *tx) 877fa9e4066Sahrens { 878088f3894Sahrens objset_t *mos = dp->dp_meta_objset; 879fa9e4066Sahrens uint64_t ddobj; 880cde58dbcSMatthew Ahrens dsl_dir_phys_t *ddphys; 881fa9e4066Sahrens dmu_buf_t *dbuf; 882fa9e4066Sahrens 8831649cd4bStabriz ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, 8841649cd4bStabriz DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); 885088f3894Sahrens if (pds) { 886c1379625SJustin T. Gibbs VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj, 887088f3894Sahrens name, sizeof (uint64_t), 1, &ddobj, tx)); 888088f3894Sahrens } else { 889088f3894Sahrens /* it's the root dir */ 890088f3894Sahrens VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 891088f3894Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); 892088f3894Sahrens } 893ea8dc4b6Seschrock VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); 894fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 895cde58dbcSMatthew Ahrens ddphys = dbuf->db_data; 896fa9e4066Sahrens 897cde58dbcSMatthew Ahrens ddphys->dd_creation_time = gethrestime_sec(); 898a2afb611SJerry Jelinek if (pds) { 899cde58dbcSMatthew Ahrens ddphys->dd_parent_obj = pds->dd_object; 900a2afb611SJerry Jelinek 901a2afb611SJerry Jelinek /* update the filesystem counts */ 902a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx); 903a2afb611SJerry Jelinek } 904cde58dbcSMatthew Ahrens ddphys->dd_props_zapobj = zap_create(mos, 905fa9e4066Sahrens DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 906cde58dbcSMatthew Ahrens ddphys->dd_child_dir_zapobj = zap_create(mos, 90787e5029aSahrens DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); 90874e7dc98SMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) 909cde58dbcSMatthew Ahrens ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; 910ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 911fa9e4066Sahrens 9121d452cf5Sahrens return (ddobj); 913fa9e4066Sahrens } 914fa9e4066Sahrens 915088f3894Sahrens boolean_t 916088f3894Sahrens dsl_dir_is_clone(dsl_dir_t *dd) 917fa9e4066Sahrens { 918c1379625SJustin T. Gibbs return (dsl_dir_phys(dd)->dd_origin_obj && 919088f3894Sahrens (dd->dd_pool->dp_origin_snap == NULL || 920c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj != 921088f3894Sahrens dd->dd_pool->dp_origin_snap->ds_object)); 922fa9e4066Sahrens } 923fa9e4066Sahrens 924fa9e4066Sahrens void 925a2eea2e1Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) 926fa9e4066Sahrens { 927fa9e4066Sahrens mutex_enter(&dd->dd_lock); 92874e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 929c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes); 930c1379625SJustin T. Gibbs dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, 931c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_quota); 932a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, 933c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_reserved); 934a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 935c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 : 936c1379625SJustin T. Gibbs (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 / 937c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes)); 93877372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED, 939c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes); 940c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 94174e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, 942c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]); 94374e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, 944c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]); 94574e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, 946c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]); 94774e7dc98SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, 948c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] + 949c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]); 95074e7dc98SMatthew Ahrens } 951fa9e4066Sahrens mutex_exit(&dd->dd_lock); 952fa9e4066Sahrens 953a2afb611SJerry Jelinek if (dsl_dir_is_zapified(dd)) { 954a2afb611SJerry Jelinek uint64_t count; 955a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 956a2afb611SJerry Jelinek 957a2afb611SJerry Jelinek if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 958a2afb611SJerry Jelinek sizeof (count), 1, &count) == 0) { 959a2afb611SJerry Jelinek dsl_prop_nvlist_add_uint64(nv, 960a2afb611SJerry Jelinek ZFS_PROP_FILESYSTEM_COUNT, count); 961a2afb611SJerry Jelinek } 962a2afb611SJerry Jelinek if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 963a2afb611SJerry Jelinek sizeof (count), 1, &count) == 0) { 964a2afb611SJerry Jelinek dsl_prop_nvlist_add_uint64(nv, 965a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_COUNT, count); 966a2afb611SJerry Jelinek } 967a2afb611SJerry Jelinek } 968a2afb611SJerry Jelinek 969088f3894Sahrens if (dsl_dir_is_clone(dd)) { 970fa9e4066Sahrens dsl_dataset_t *ds; 971a2eea2e1Sahrens char buf[MAXNAMELEN]; 972fa9e4066Sahrens 9733b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, 974c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds)); 975a2eea2e1Sahrens dsl_dataset_name(ds, buf); 976745cd3c5Smaybee dsl_dataset_rele(ds, FTAG); 977a2eea2e1Sahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); 978fa9e4066Sahrens } 979fa9e4066Sahrens } 980fa9e4066Sahrens 981fa9e4066Sahrens void 982fa9e4066Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) 983fa9e4066Sahrens { 984fa9e4066Sahrens dsl_pool_t *dp = dd->dd_pool; 985fa9e4066Sahrens 986c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)); 987fa9e4066Sahrens 9883b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) { 989fa9e4066Sahrens /* up the hold count until we can be written out */ 990fa9e4066Sahrens dmu_buf_add_ref(dd->dd_dbuf, dd); 991fa9e4066Sahrens } 992fa9e4066Sahrens } 993fa9e4066Sahrens 994fa9e4066Sahrens static int64_t 995fa9e4066Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) 996fa9e4066Sahrens { 997c1379625SJustin T. Gibbs uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved); 998c1379625SJustin T. Gibbs uint64_t new_accounted = 999c1379625SJustin T. Gibbs MAX(used + delta, dsl_dir_phys(dd)->dd_reserved); 1000fa9e4066Sahrens return (new_accounted - old_accounted); 1001fa9e4066Sahrens } 1002fa9e4066Sahrens 1003fa9e4066Sahrens void 1004fa9e4066Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) 1005fa9e4066Sahrens { 1006fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1007fa9e4066Sahrens 1008fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1009fb09f5aaSMadhav Suresh ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]); 1010fa9e4066Sahrens dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, 1011fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); 1012fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; 1013fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1014fa9e4066Sahrens 1015fa9e4066Sahrens /* release the hold from dsl_dir_dirty */ 1016ea8dc4b6Seschrock dmu_buf_rele(dd->dd_dbuf, dd); 1017fa9e4066Sahrens } 1018fa9e4066Sahrens 1019fa9e4066Sahrens static uint64_t 1020a9799022Sck dsl_dir_space_towrite(dsl_dir_t *dd) 1021fa9e4066Sahrens { 1022a9799022Sck uint64_t space = 0; 1023fa9e4066Sahrens int i; 1024fa9e4066Sahrens 1025fa9e4066Sahrens ASSERT(MUTEX_HELD(&dd->dd_lock)); 1026fa9e4066Sahrens 1027fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) { 1028fa9e4066Sahrens space += dd->dd_space_towrite[i&TXG_MASK]; 1029fa9e4066Sahrens ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0); 1030fa9e4066Sahrens } 1031fa9e4066Sahrens return (space); 1032fa9e4066Sahrens } 1033fa9e4066Sahrens 1034fa9e4066Sahrens /* 1035fa9e4066Sahrens * How much space would dd have available if ancestor had delta applied 1036fa9e4066Sahrens * to it? If ondiskonly is set, we're only interested in what's 1037fa9e4066Sahrens * on-disk, not estimated pending changes. 1038fa9e4066Sahrens */ 1039a2eea2e1Sahrens uint64_t 1040fa9e4066Sahrens dsl_dir_space_available(dsl_dir_t *dd, 1041fa9e4066Sahrens dsl_dir_t *ancestor, int64_t delta, int ondiskonly) 1042fa9e4066Sahrens { 1043fa9e4066Sahrens uint64_t parentspace, myspace, quota, used; 1044fa9e4066Sahrens 1045fa9e4066Sahrens /* 1046fa9e4066Sahrens * If there are no restrictions otherwise, assume we have 1047fa9e4066Sahrens * unlimited space available. 1048fa9e4066Sahrens */ 1049fa9e4066Sahrens quota = UINT64_MAX; 1050fa9e4066Sahrens parentspace = UINT64_MAX; 1051fa9e4066Sahrens 1052fa9e4066Sahrens if (dd->dd_parent != NULL) { 1053fa9e4066Sahrens parentspace = dsl_dir_space_available(dd->dd_parent, 1054fa9e4066Sahrens ancestor, delta, ondiskonly); 1055fa9e4066Sahrens } 1056fa9e4066Sahrens 1057fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1058c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_quota != 0) 1059c1379625SJustin T. Gibbs quota = dsl_dir_phys(dd)->dd_quota; 1060c1379625SJustin T. Gibbs used = dsl_dir_phys(dd)->dd_used_bytes; 1061a9799022Sck if (!ondiskonly) 1062a9799022Sck used += dsl_dir_space_towrite(dd); 1063fa9e4066Sahrens 1064fa9e4066Sahrens if (dd->dd_parent == NULL) { 106599653d4eSeschrock uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); 1066fa9e4066Sahrens quota = MIN(quota, poolsize); 1067fa9e4066Sahrens } 1068fa9e4066Sahrens 1069c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) { 1070fa9e4066Sahrens /* 1071fa9e4066Sahrens * We have some space reserved, in addition to what our 1072fa9e4066Sahrens * parent gave us. 1073fa9e4066Sahrens */ 1074c1379625SJustin T. Gibbs parentspace += dsl_dir_phys(dd)->dd_reserved - used; 1075fa9e4066Sahrens } 1076fa9e4066Sahrens 107774e7dc98SMatthew Ahrens if (dd == ancestor) { 107874e7dc98SMatthew Ahrens ASSERT(delta <= 0); 107974e7dc98SMatthew Ahrens ASSERT(used >= -delta); 108074e7dc98SMatthew Ahrens used += delta; 108174e7dc98SMatthew Ahrens if (parentspace != UINT64_MAX) 108274e7dc98SMatthew Ahrens parentspace -= delta; 108374e7dc98SMatthew Ahrens } 108474e7dc98SMatthew Ahrens 1085fa9e4066Sahrens if (used > quota) { 1086fa9e4066Sahrens /* over quota */ 1087fa9e4066Sahrens myspace = 0; 1088fa9e4066Sahrens } else { 1089fa9e4066Sahrens /* 109099653d4eSeschrock * the lesser of the space provided by our parent and 109199653d4eSeschrock * the space left in our quota 1092fa9e4066Sahrens */ 1093fa9e4066Sahrens myspace = MIN(parentspace, quota - used); 1094fa9e4066Sahrens } 1095fa9e4066Sahrens 1096fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1097fa9e4066Sahrens 1098fa9e4066Sahrens return (myspace); 1099fa9e4066Sahrens } 1100fa9e4066Sahrens 1101fa9e4066Sahrens struct tempreserve { 1102fa9e4066Sahrens list_node_t tr_node; 1103fa9e4066Sahrens dsl_dir_t *tr_ds; 1104fa9e4066Sahrens uint64_t tr_size; 1105fa9e4066Sahrens }; 1106fa9e4066Sahrens 1107fa9e4066Sahrens static int 1108a9799022Sck dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, 1109a9799022Sck boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list, 11106df6c3bcSck dmu_tx_t *tx, boolean_t first) 1111fa9e4066Sahrens { 1112fa9e4066Sahrens uint64_t txg = tx->tx_txg; 1113a9799022Sck uint64_t est_inflight, used_on_disk, quota, parent_rsrv; 1114468c413aSTim Haley uint64_t deferred = 0; 1115a9799022Sck struct tempreserve *tr; 1116468c413aSTim Haley int retval = EDQUOT; 1117fa9e4066Sahrens int txgidx = txg & TXG_MASK; 1118fa9e4066Sahrens int i; 11199082849eSck uint64_t ref_rsrv = 0; 1120fa9e4066Sahrens 1121fa9e4066Sahrens ASSERT3U(txg, !=, 0); 1122a9799022Sck ASSERT3S(asize, >, 0); 1123fa9e4066Sahrens 1124fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1125a9799022Sck 1126fa9e4066Sahrens /* 1127fa9e4066Sahrens * Check against the dsl_dir's quota. We don't add in the delta 1128fa9e4066Sahrens * when checking for over-quota because they get one free hit. 1129fa9e4066Sahrens */ 1130a9799022Sck est_inflight = dsl_dir_space_towrite(dd); 1131fa9e4066Sahrens for (i = 0; i < TXG_SIZE; i++) 1132a9799022Sck est_inflight += dd->dd_tempreserved[i]; 1133c1379625SJustin T. Gibbs used_on_disk = dsl_dir_phys(dd)->dd_used_bytes; 1134fa9e4066Sahrens 1135f4d2e9e6Smaybee /* 11366df6c3bcSck * On the first iteration, fetch the dataset's used-on-disk and 11376df6c3bcSck * refreservation values. Also, if checkrefquota is set, test if 11386df6c3bcSck * allocating this space would exceed the dataset's refquota. 1139f4d2e9e6Smaybee */ 11406df6c3bcSck if (first && tx->tx_objset) { 1141c3fdb13aSmaybee int error; 1142503ad85cSMatthew Ahrens dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; 11439082849eSck 1144a9799022Sck error = dsl_dataset_check_quota(ds, checkrefquota, 11459082849eSck asize, est_inflight, &used_on_disk, &ref_rsrv); 1146a9799022Sck if (error) { 1147a9799022Sck mutex_exit(&dd->dd_lock); 1148a9799022Sck return (error); 1149a9799022Sck } 1150a9799022Sck } 1151a9799022Sck 1152a9799022Sck /* 1153a9799022Sck * If this transaction will result in a net free of space, 1154a9799022Sck * we want to let it through. 1155a9799022Sck */ 1156c1379625SJustin T. Gibbs if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0) 1157f4d2e9e6Smaybee quota = UINT64_MAX; 1158f4d2e9e6Smaybee else 1159c1379625SJustin T. Gibbs quota = dsl_dir_phys(dd)->dd_quota; 1160fa9e4066Sahrens 1161fa9e4066Sahrens /* 1162468c413aSTim Haley * Adjust the quota against the actual pool size at the root 1163468c413aSTim Haley * minus any outstanding deferred frees. 1164f4d2e9e6Smaybee * To ensure that it's possible to remove files from a full 1165f4d2e9e6Smaybee * pool without inducing transient overcommits, we throttle 1166fa9e4066Sahrens * netfree transactions against a quota that is slightly larger, 1167fa9e4066Sahrens * but still within the pool's allocation slop. In cases where 1168fa9e4066Sahrens * we're very close to full, this will allow a steady trickle of 1169fa9e4066Sahrens * removes to get through. 1170fa9e4066Sahrens */ 11711934e92fSmaybee if (dd->dd_parent == NULL) { 1172b24ab676SJeff Bonwick spa_t *spa = dd->dd_pool->dp_spa; 1173fa9e4066Sahrens uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); 1174b24ab676SJeff Bonwick deferred = metaslab_class_get_deferred(spa_normal_class(spa)); 1175468c413aSTim Haley if (poolsize - deferred < quota) { 1176468c413aSTim Haley quota = poolsize - deferred; 1177468c413aSTim Haley retval = ENOSPC; 1178fa9e4066Sahrens } 1179fa9e4066Sahrens } 1180fa9e4066Sahrens 1181fa9e4066Sahrens /* 1182fa9e4066Sahrens * If they are requesting more space, and our current estimate 1183a9799022Sck * is over quota, they get to try again unless the actual 1184ea8dc4b6Seschrock * on-disk is over quota and there are no pending changes (which 1185ea8dc4b6Seschrock * may free up space for us). 1186fa9e4066Sahrens */ 1187468c413aSTim Haley if (used_on_disk + est_inflight >= quota) { 1188468c413aSTim Haley if (est_inflight > 0 || used_on_disk < quota || 1189468c413aSTim Haley (retval == ENOSPC && used_on_disk < quota + deferred)) 1190468c413aSTim Haley retval = ERESTART; 1191a9799022Sck dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " 1192fa9e4066Sahrens "quota=%lluK tr=%lluK err=%d\n", 1193a9799022Sck used_on_disk>>10, est_inflight>>10, 1194468c413aSTim Haley quota>>10, asize>>10, retval); 1195fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1196be6fd75aSMatthew Ahrens return (SET_ERROR(retval)); 1197fa9e4066Sahrens } 1198fa9e4066Sahrens 1199fa9e4066Sahrens /* We need to up our estimated delta before dropping dd_lock */ 1200fa9e4066Sahrens dd->dd_tempreserved[txgidx] += asize; 1201fa9e4066Sahrens 12029082849eSck parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, 12039082849eSck asize - ref_rsrv); 1204fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1205fa9e4066Sahrens 12061ab7f2deSmaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 1207fa9e4066Sahrens tr->tr_ds = dd; 1208fa9e4066Sahrens tr->tr_size = asize; 1209fa9e4066Sahrens list_insert_tail(tr_list, tr); 1210fa9e4066Sahrens 1211fa9e4066Sahrens /* see if it's OK with our parent */ 12121934e92fSmaybee if (dd->dd_parent && parent_rsrv) { 1213c1379625SJustin T. Gibbs boolean_t ismos = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 12141934e92fSmaybee 1215fa9e4066Sahrens return (dsl_dir_tempreserve_impl(dd->dd_parent, 12166df6c3bcSck parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE)); 1217fa9e4066Sahrens } else { 1218fa9e4066Sahrens return (0); 1219fa9e4066Sahrens } 1220fa9e4066Sahrens } 1221fa9e4066Sahrens 1222fa9e4066Sahrens /* 1223fa9e4066Sahrens * Reserve space in this dsl_dir, to be used in this tx's txg. 1224a9799022Sck * After the space has been dirtied (and dsl_dir_willuse_space() 1225a9799022Sck * has been called), the reservation should be canceled, using 1226a9799022Sck * dsl_dir_tempreserve_clear(). 1227fa9e4066Sahrens */ 1228fa9e4066Sahrens int 1229a9799022Sck dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, 1230a9799022Sck uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx) 1231fa9e4066Sahrens { 12321ab7f2deSmaybee int err; 1233fa9e4066Sahrens list_t *tr_list; 1234fa9e4066Sahrens 1235a9799022Sck if (asize == 0) { 1236a9799022Sck *tr_cookiep = NULL; 1237a9799022Sck return (0); 1238a9799022Sck } 1239a9799022Sck 1240fa9e4066Sahrens tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); 1241fa9e4066Sahrens list_create(tr_list, sizeof (struct tempreserve), 1242fa9e4066Sahrens offsetof(struct tempreserve, tr_node)); 1243a9799022Sck ASSERT3S(asize, >, 0); 1244ea8dc4b6Seschrock ASSERT3S(fsize, >=, 0); 1245fa9e4066Sahrens 12461ab7f2deSmaybee err = arc_tempreserve_space(lsize, tx->tx_txg); 1247fa9e4066Sahrens if (err == 0) { 1248fa9e4066Sahrens struct tempreserve *tr; 1249fa9e4066Sahrens 12501ab7f2deSmaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 12511ab7f2deSmaybee tr->tr_size = lsize; 12521ab7f2deSmaybee list_insert_tail(tr_list, tr); 12531ab7f2deSmaybee } else { 12541ab7f2deSmaybee if (err == EAGAIN) { 125569962b56SMatthew Ahrens /* 125669962b56SMatthew Ahrens * If arc_memory_throttle() detected that pageout 125769962b56SMatthew Ahrens * is running and we are low on memory, we delay new 125869962b56SMatthew Ahrens * non-pageout transactions to give pageout an 125969962b56SMatthew Ahrens * advantage. 126069962b56SMatthew Ahrens * 126169962b56SMatthew Ahrens * It is unfortunate to be delaying while the caller's 126269962b56SMatthew Ahrens * locks are held. 126369962b56SMatthew Ahrens */ 12640689f76cSAdam Leventhal txg_delay(dd->dd_pool, tx->tx_txg, 12650689f76cSAdam Leventhal MSEC2NSEC(10), MSEC2NSEC(10)); 1266be6fd75aSMatthew Ahrens err = SET_ERROR(ERESTART); 1267fa9e4066Sahrens } 12681ab7f2deSmaybee } 12691ab7f2deSmaybee 12701ab7f2deSmaybee if (err == 0) { 12711ab7f2deSmaybee err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize, 12726df6c3bcSck FALSE, asize > usize, tr_list, tx, TRUE); 1273fa9e4066Sahrens } 1274fa9e4066Sahrens 12753b2aab18SMatthew Ahrens if (err != 0) 1276fa9e4066Sahrens dsl_dir_tempreserve_clear(tr_list, tx); 1277fa9e4066Sahrens else 1278fa9e4066Sahrens *tr_cookiep = tr_list; 12791ab7f2deSmaybee 1280fa9e4066Sahrens return (err); 1281fa9e4066Sahrens } 1282fa9e4066Sahrens 1283fa9e4066Sahrens /* 1284fa9e4066Sahrens * Clear a temporary reservation that we previously made with 1285fa9e4066Sahrens * dsl_dir_tempreserve_space(). 1286fa9e4066Sahrens */ 1287fa9e4066Sahrens void 1288fa9e4066Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) 1289fa9e4066Sahrens { 1290fa9e4066Sahrens int txgidx = tx->tx_txg & TXG_MASK; 1291fa9e4066Sahrens list_t *tr_list = tr_cookie; 1292fa9e4066Sahrens struct tempreserve *tr; 1293fa9e4066Sahrens 1294fa9e4066Sahrens ASSERT3U(tx->tx_txg, !=, 0); 1295fa9e4066Sahrens 1296a9799022Sck if (tr_cookie == NULL) 1297a9799022Sck return; 1298a9799022Sck 129969962b56SMatthew Ahrens while ((tr = list_head(tr_list)) != NULL) { 130069962b56SMatthew Ahrens if (tr->tr_ds) { 1301fa9e4066Sahrens mutex_enter(&tr->tr_ds->dd_lock); 1302fa9e4066Sahrens ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, 1303fa9e4066Sahrens tr->tr_size); 1304fa9e4066Sahrens tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; 1305fa9e4066Sahrens mutex_exit(&tr->tr_ds->dd_lock); 13061ab7f2deSmaybee } else { 13071ab7f2deSmaybee arc_tempreserve_clear(tr->tr_size); 1308fa9e4066Sahrens } 1309fa9e4066Sahrens list_remove(tr_list, tr); 1310fa9e4066Sahrens kmem_free(tr, sizeof (struct tempreserve)); 1311fa9e4066Sahrens } 1312fa9e4066Sahrens 1313fa9e4066Sahrens kmem_free(tr_list, sizeof (list_t)); 1314fa9e4066Sahrens } 1315fa9e4066Sahrens 131669962b56SMatthew Ahrens /* 131769962b56SMatthew Ahrens * This should be called from open context when we think we're going to write 131869962b56SMatthew Ahrens * or free space, for example when dirtying data. Be conservative; it's okay 131969962b56SMatthew Ahrens * to write less space or free more, but we don't want to write more or free 132069962b56SMatthew Ahrens * less than the amount specified. 132169962b56SMatthew Ahrens */ 132269962b56SMatthew Ahrens void 132369962b56SMatthew Ahrens dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 1324fa9e4066Sahrens { 1325fa9e4066Sahrens int64_t parent_space; 1326fa9e4066Sahrens uint64_t est_used; 1327fa9e4066Sahrens 1328fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1329fa9e4066Sahrens if (space > 0) 1330fa9e4066Sahrens dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; 1331fa9e4066Sahrens 1332c1379625SJustin T. Gibbs est_used = dsl_dir_space_towrite(dd) + dsl_dir_phys(dd)->dd_used_bytes; 1333fa9e4066Sahrens parent_space = parent_delta(dd, est_used, space); 1334fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1335fa9e4066Sahrens 1336fa9e4066Sahrens /* Make sure that we clean up dd_space_to* */ 1337fa9e4066Sahrens dsl_dir_dirty(dd, tx); 1338fa9e4066Sahrens 1339fa9e4066Sahrens /* XXX this is potentially expensive and unnecessary... */ 1340fa9e4066Sahrens if (parent_space && dd->dd_parent) 134169962b56SMatthew Ahrens dsl_dir_willuse_space(dd->dd_parent, parent_space, tx); 1342fa9e4066Sahrens } 1343fa9e4066Sahrens 1344fa9e4066Sahrens /* call from syncing context when we actually write/free space for this dd */ 1345fa9e4066Sahrens void 134674e7dc98SMatthew Ahrens dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, 1347fa9e4066Sahrens int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) 1348fa9e4066Sahrens { 1349fa9e4066Sahrens int64_t accounted_delta; 1350b62969f8SMatthew Ahrens 1351b62969f8SMatthew Ahrens /* 1352b62969f8SMatthew Ahrens * dsl_dataset_set_refreservation_sync_impl() calls this with 1353b62969f8SMatthew Ahrens * dd_lock held, so that it can atomically update 1354b62969f8SMatthew Ahrens * ds->ds_reserved and the dsl_dir accounting, so that 1355b62969f8SMatthew Ahrens * dsl_dataset_check_quota() can see dataset and dir accounting 1356b62969f8SMatthew Ahrens * consistently. 1357b62969f8SMatthew Ahrens */ 135802c8f3f0SMatthew Ahrens boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 1359fa9e4066Sahrens 1360fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 136174e7dc98SMatthew Ahrens ASSERT(type < DD_USED_NUM); 1362fa9e4066Sahrens 1363b62969f8SMatthew Ahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1364b62969f8SMatthew Ahrens 136502c8f3f0SMatthew Ahrens if (needlock) 136602c8f3f0SMatthew Ahrens mutex_enter(&dd->dd_lock); 1367c1379625SJustin T. Gibbs accounted_delta = 1368c1379625SJustin T. Gibbs parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used); 1369c1379625SJustin T. Gibbs ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used); 1370fa9e4066Sahrens ASSERT(compressed >= 0 || 1371c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed); 1372fa9e4066Sahrens ASSERT(uncompressed >= 0 || 1373c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed); 1374c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes += used; 1375c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed; 1376c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes += compressed; 137774e7dc98SMatthew Ahrens 1378c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 137974e7dc98SMatthew Ahrens ASSERT(used > 0 || 1380c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used); 1381c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[type] += used; 138274e7dc98SMatthew Ahrens #ifdef DEBUG 138374e7dc98SMatthew Ahrens dd_used_t t; 138474e7dc98SMatthew Ahrens uint64_t u = 0; 138574e7dc98SMatthew Ahrens for (t = 0; t < DD_USED_NUM; t++) 1386c1379625SJustin T. Gibbs u += dsl_dir_phys(dd)->dd_used_breakdown[t]; 1387c1379625SJustin T. Gibbs ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes); 138874e7dc98SMatthew Ahrens #endif 138974e7dc98SMatthew Ahrens } 139002c8f3f0SMatthew Ahrens if (needlock) 139102c8f3f0SMatthew Ahrens mutex_exit(&dd->dd_lock); 1392fa9e4066Sahrens 1393fa9e4066Sahrens if (dd->dd_parent != NULL) { 139474e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 1395fa9e4066Sahrens accounted_delta, compressed, uncompressed, tx); 139674e7dc98SMatthew Ahrens dsl_dir_transfer_space(dd->dd_parent, 139774e7dc98SMatthew Ahrens used - accounted_delta, 139874e7dc98SMatthew Ahrens DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); 1399fa9e4066Sahrens } 1400fa9e4066Sahrens } 1401fa9e4066Sahrens 140274e7dc98SMatthew Ahrens void 140374e7dc98SMatthew Ahrens dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, 140474e7dc98SMatthew Ahrens dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 140574e7dc98SMatthew Ahrens { 140674e7dc98SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 140774e7dc98SMatthew Ahrens ASSERT(oldtype < DD_USED_NUM); 140874e7dc98SMatthew Ahrens ASSERT(newtype < DD_USED_NUM); 140974e7dc98SMatthew Ahrens 1410c1379625SJustin T. Gibbs if (delta == 0 || 1411c1379625SJustin T. Gibbs !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN)) 141274e7dc98SMatthew Ahrens return; 141374e7dc98SMatthew Ahrens 1414b62969f8SMatthew Ahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1415b62969f8SMatthew Ahrens mutex_enter(&dd->dd_lock); 141674e7dc98SMatthew Ahrens ASSERT(delta > 0 ? 1417c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta : 1418c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta); 1419c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta)); 1420c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta; 1421c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta; 1422b62969f8SMatthew Ahrens mutex_exit(&dd->dd_lock); 142374e7dc98SMatthew Ahrens } 142474e7dc98SMatthew Ahrens 14253b2aab18SMatthew Ahrens typedef struct dsl_dir_set_qr_arg { 14263b2aab18SMatthew Ahrens const char *ddsqra_name; 14273b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 14283b2aab18SMatthew Ahrens uint64_t ddsqra_value; 14293b2aab18SMatthew Ahrens } dsl_dir_set_qr_arg_t; 14303b2aab18SMatthew Ahrens 1431fa9e4066Sahrens static int 14323b2aab18SMatthew Ahrens dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx) 1433fa9e4066Sahrens { 14343b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 14353b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14363b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14373b2aab18SMatthew Ahrens int error; 14383b2aab18SMatthew Ahrens uint64_t towrite, newval; 1439fa9e4066Sahrens 14403b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 14413b2aab18SMatthew Ahrens if (error != 0) 14423b2aab18SMatthew Ahrens return (error); 14433b2aab18SMatthew Ahrens 14443b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, "quota", 14453b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 14463b2aab18SMatthew Ahrens if (error != 0) { 14473b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14483b2aab18SMatthew Ahrens return (error); 14493b2aab18SMatthew Ahrens } 145092241e0bSTom Erickson 14513b2aab18SMatthew Ahrens if (newval == 0) { 14523b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14531d452cf5Sahrens return (0); 14543b2aab18SMatthew Ahrens } 1455fa9e4066Sahrens 14563b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 14571d452cf5Sahrens /* 14581d452cf5Sahrens * If we are doing the preliminary check in open context, and 14591d452cf5Sahrens * there are pending changes, then don't fail it, since the 1460a9799022Sck * pending changes could under-estimate the amount of space to be 14611d452cf5Sahrens * freed up. 14621d452cf5Sahrens */ 14633b2aab18SMatthew Ahrens towrite = dsl_dir_space_towrite(ds->ds_dir); 14641d452cf5Sahrens if ((dmu_tx_is_syncing(tx) || towrite == 0) && 1465c1379625SJustin T. Gibbs (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved || 1466c1379625SJustin T. Gibbs newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) { 1467be6fd75aSMatthew Ahrens error = SET_ERROR(ENOSPC); 1468fa9e4066Sahrens } 14693b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 14703b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14713b2aab18SMatthew Ahrens return (error); 1472fa9e4066Sahrens } 1473fa9e4066Sahrens 14741d452cf5Sahrens static void 14753b2aab18SMatthew Ahrens dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx) 14761d452cf5Sahrens { 14773b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 14783b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14793b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14803b2aab18SMatthew Ahrens uint64_t newval; 148192241e0bSTom Erickson 14823b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 14831d452cf5Sahrens 1484013023d4SMartin Matuska if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1485013023d4SMartin Matuska dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA), 1486013023d4SMartin Matuska ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1487013023d4SMartin Matuska &ddsqra->ddsqra_value, tx); 14881d452cf5Sahrens 1489013023d4SMartin Matuska VERIFY0(dsl_prop_get_int_ds(ds, 1490013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_QUOTA), &newval)); 1491013023d4SMartin Matuska } else { 1492013023d4SMartin Matuska newval = ddsqra->ddsqra_value; 1493013023d4SMartin Matuska spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1494013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval); 1495013023d4SMartin Matuska } 14963b2aab18SMatthew Ahrens 14973b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 14983b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 1499c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_quota = newval; 15003b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 15013b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15021d452cf5Sahrens } 15031d452cf5Sahrens 1504fa9e4066Sahrens int 150592241e0bSTom Erickson dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota) 1506fa9e4066Sahrens { 15073b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t ddsqra; 1508fa9e4066Sahrens 15093b2aab18SMatthew Ahrens ddsqra.ddsqra_name = ddname; 15103b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 15113b2aab18SMatthew Ahrens ddsqra.ddsqra_value = quota; 151292241e0bSTom Erickson 15133b2aab18SMatthew Ahrens return (dsl_sync_task(ddname, dsl_dir_set_quota_check, 15147d46dc6cSMatthew Ahrens dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 1515fa9e4066Sahrens } 1516fa9e4066Sahrens 1517a9799022Sck int 15183b2aab18SMatthew Ahrens dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx) 1519fa9e4066Sahrens { 15203b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 15213b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15223b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15233b2aab18SMatthew Ahrens dsl_dir_t *dd; 15243b2aab18SMatthew Ahrens uint64_t newval, used, avail; 15253b2aab18SMatthew Ahrens int error; 152692241e0bSTom Erickson 15273b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 15283b2aab18SMatthew Ahrens if (error != 0) 15293b2aab18SMatthew Ahrens return (error); 15303b2aab18SMatthew Ahrens dd = ds->ds_dir; 1531fa9e4066Sahrens 15321d452cf5Sahrens /* 15331d452cf5Sahrens * If we are doing the preliminary check in open context, the 15341d452cf5Sahrens * space estimates may be inaccurate. 15351d452cf5Sahrens */ 15363b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 15373b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15381d452cf5Sahrens return (0); 15393b2aab18SMatthew Ahrens } 15403b2aab18SMatthew Ahrens 15413b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 15423b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_RESERVATION), 15433b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 15443b2aab18SMatthew Ahrens if (error != 0) { 15453b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15463b2aab18SMatthew Ahrens return (error); 15473b2aab18SMatthew Ahrens } 15481d452cf5Sahrens 1549fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1550c1379625SJustin T. Gibbs used = dsl_dir_phys(dd)->dd_used_bytes; 1551fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1552fa9e4066Sahrens 1553fa9e4066Sahrens if (dd->dd_parent) { 1554fa9e4066Sahrens avail = dsl_dir_space_available(dd->dd_parent, 1555fa9e4066Sahrens NULL, 0, FALSE); 1556fa9e4066Sahrens } else { 1557fa9e4066Sahrens avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; 1558fa9e4066Sahrens } 1559fa9e4066Sahrens 1560c1379625SJustin T. Gibbs if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) { 15613b2aab18SMatthew Ahrens uint64_t delta = MAX(used, newval) - 1562c1379625SJustin T. Gibbs MAX(used, dsl_dir_phys(dd)->dd_reserved); 1563379c004dSEric Schrock 15643b2aab18SMatthew Ahrens if (delta > avail || 1565c1379625SJustin T. Gibbs (dsl_dir_phys(dd)->dd_quota > 0 && 1566c1379625SJustin T. Gibbs newval > dsl_dir_phys(dd)->dd_quota)) 1567be6fd75aSMatthew Ahrens error = SET_ERROR(ENOSPC); 1568379c004dSEric Schrock } 1569379c004dSEric Schrock 15703b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15713b2aab18SMatthew Ahrens return (error); 15721d452cf5Sahrens } 15731d452cf5Sahrens 15743b2aab18SMatthew Ahrens void 15754445fffbSMatthew Ahrens dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx) 15761d452cf5Sahrens { 15771d452cf5Sahrens uint64_t used; 15781d452cf5Sahrens int64_t delta; 15791d452cf5Sahrens 1580a9799022Sck dmu_buf_will_dirty(dd->dd_dbuf, tx); 1581a9799022Sck 15821d452cf5Sahrens mutex_enter(&dd->dd_lock); 1583c1379625SJustin T. Gibbs used = dsl_dir_phys(dd)->dd_used_bytes; 1584c1379625SJustin T. Gibbs delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved); 1585c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_reserved = value; 1586fa9e4066Sahrens 1587fa9e4066Sahrens if (dd->dd_parent != NULL) { 1588fa9e4066Sahrens /* Roll up this additional usage into our ancestors */ 158974e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 159074e7dc98SMatthew Ahrens delta, 0, 0, tx); 1591fa9e4066Sahrens } 159202c8f3f0SMatthew Ahrens mutex_exit(&dd->dd_lock); 15934445fffbSMatthew Ahrens } 15944445fffbSMatthew Ahrens 15954445fffbSMatthew Ahrens 15964445fffbSMatthew Ahrens static void 15973b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx) 15984445fffbSMatthew Ahrens { 15993b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t *ddsqra = arg; 16003b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16013b2aab18SMatthew Ahrens dsl_dataset_t *ds; 16023b2aab18SMatthew Ahrens uint64_t newval; 16033b2aab18SMatthew Ahrens 16043b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 16053b2aab18SMatthew Ahrens 1606013023d4SMartin Matuska if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { 1607013023d4SMartin Matuska dsl_prop_set_sync_impl(ds, 1608013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION), 1609013023d4SMartin Matuska ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 1610013023d4SMartin Matuska &ddsqra->ddsqra_value, tx); 16114445fffbSMatthew Ahrens 1612013023d4SMartin Matuska VERIFY0(dsl_prop_get_int_ds(ds, 1613013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval)); 1614013023d4SMartin Matuska } else { 1615013023d4SMartin Matuska newval = ddsqra->ddsqra_value; 1616013023d4SMartin Matuska spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", 1617013023d4SMartin Matuska zfs_prop_to_name(ZFS_PROP_RESERVATION), 1618013023d4SMartin Matuska (longlong_t)newval); 1619013023d4SMartin Matuska } 1620ecd6cf80Smarks 16213b2aab18SMatthew Ahrens dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx); 16223b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1623fa9e4066Sahrens } 1624fa9e4066Sahrens 1625fa9e4066Sahrens int 162692241e0bSTom Erickson dsl_dir_set_reservation(const char *ddname, zprop_source_t source, 162792241e0bSTom Erickson uint64_t reservation) 1628fa9e4066Sahrens { 16293b2aab18SMatthew Ahrens dsl_dir_set_qr_arg_t ddsqra; 163092241e0bSTom Erickson 16313b2aab18SMatthew Ahrens ddsqra.ddsqra_name = ddname; 16323b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 16333b2aab18SMatthew Ahrens ddsqra.ddsqra_value = reservation; 163492241e0bSTom Erickson 16353b2aab18SMatthew Ahrens return (dsl_sync_task(ddname, dsl_dir_set_reservation_check, 16367d46dc6cSMatthew Ahrens dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 1637fa9e4066Sahrens } 1638fa9e4066Sahrens 1639fa9e4066Sahrens static dsl_dir_t * 1640fa9e4066Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) 1641fa9e4066Sahrens { 1642fa9e4066Sahrens for (; ds1; ds1 = ds1->dd_parent) { 1643fa9e4066Sahrens dsl_dir_t *dd; 1644fa9e4066Sahrens for (dd = ds2; dd; dd = dd->dd_parent) { 1645fa9e4066Sahrens if (ds1 == dd) 1646fa9e4066Sahrens return (dd); 1647fa9e4066Sahrens } 1648fa9e4066Sahrens } 1649fa9e4066Sahrens return (NULL); 1650fa9e4066Sahrens } 1651fa9e4066Sahrens 1652fa9e4066Sahrens /* 1653fa9e4066Sahrens * If delta is applied to dd, how much of that delta would be applied to 1654fa9e4066Sahrens * ancestor? Syncing context only. 1655fa9e4066Sahrens */ 1656fa9e4066Sahrens static int64_t 1657fa9e4066Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) 1658fa9e4066Sahrens { 1659fa9e4066Sahrens if (dd == ancestor) 1660fa9e4066Sahrens return (delta); 1661fa9e4066Sahrens 1662fa9e4066Sahrens mutex_enter(&dd->dd_lock); 1663c1379625SJustin T. Gibbs delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta); 1664fa9e4066Sahrens mutex_exit(&dd->dd_lock); 1665fa9e4066Sahrens return (would_change(dd->dd_parent, delta, ancestor)); 1666fa9e4066Sahrens } 1667fa9e4066Sahrens 16683b2aab18SMatthew Ahrens typedef struct dsl_dir_rename_arg { 16693b2aab18SMatthew Ahrens const char *ddra_oldname; 16703b2aab18SMatthew Ahrens const char *ddra_newname; 1671a2afb611SJerry Jelinek cred_t *ddra_cred; 16723b2aab18SMatthew Ahrens } dsl_dir_rename_arg_t; 16731d452cf5Sahrens 16743b2aab18SMatthew Ahrens /* ARGSUSED */ 16751d452cf5Sahrens static int 16763b2aab18SMatthew Ahrens dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) 1677fa9e4066Sahrens { 16783b2aab18SMatthew Ahrens int *deltap = arg; 16793b2aab18SMatthew Ahrens char namebuf[MAXNAMELEN]; 1680fa9e4066Sahrens 16813b2aab18SMatthew Ahrens dsl_dataset_name(ds, namebuf); 16823b2aab18SMatthew Ahrens 16833b2aab18SMatthew Ahrens if (strlen(namebuf) + *deltap >= MAXNAMELEN) 1684be6fd75aSMatthew Ahrens return (SET_ERROR(ENAMETOOLONG)); 16853b2aab18SMatthew Ahrens return (0); 16863b2aab18SMatthew Ahrens } 16873b2aab18SMatthew Ahrens 16883b2aab18SMatthew Ahrens static int 16893b2aab18SMatthew Ahrens dsl_dir_rename_check(void *arg, dmu_tx_t *tx) 16903b2aab18SMatthew Ahrens { 16913b2aab18SMatthew Ahrens dsl_dir_rename_arg_t *ddra = arg; 16923b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16933b2aab18SMatthew Ahrens dsl_dir_t *dd, *newparent; 16943b2aab18SMatthew Ahrens const char *mynewname; 16953b2aab18SMatthew Ahrens int error; 16963b2aab18SMatthew Ahrens int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname); 1697fa9e4066Sahrens 16983b2aab18SMatthew Ahrens /* target dir should exist */ 16993b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL); 17003b2aab18SMatthew Ahrens if (error != 0) 17013b2aab18SMatthew Ahrens return (error); 17023b2aab18SMatthew Ahrens 17033b2aab18SMatthew Ahrens /* new parent should exist */ 17043b2aab18SMatthew Ahrens error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG, 17053b2aab18SMatthew Ahrens &newparent, &mynewname); 17063b2aab18SMatthew Ahrens if (error != 0) { 17073b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 17083b2aab18SMatthew Ahrens return (error); 17093b2aab18SMatthew Ahrens } 17103b2aab18SMatthew Ahrens 17113b2aab18SMatthew Ahrens /* can't rename to different pool */ 17123b2aab18SMatthew Ahrens if (dd->dd_pool != newparent->dd_pool) { 17133b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17143b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 1715be6fd75aSMatthew Ahrens return (SET_ERROR(ENXIO)); 17163b2aab18SMatthew Ahrens } 17173b2aab18SMatthew Ahrens 17183b2aab18SMatthew Ahrens /* new name should not already exist */ 17193b2aab18SMatthew Ahrens if (mynewname == NULL) { 17203b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17213b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 1722be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 17233b2aab18SMatthew Ahrens } 17243b2aab18SMatthew Ahrens 17253b2aab18SMatthew Ahrens /* if the name length is growing, validate child name lengths */ 17263b2aab18SMatthew Ahrens if (delta > 0) { 17273b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename, 17283b2aab18SMatthew Ahrens &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 17293b2aab18SMatthew Ahrens if (error != 0) { 17303b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17313b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 17323b2aab18SMatthew Ahrens return (error); 17333b2aab18SMatthew Ahrens } 17343b2aab18SMatthew Ahrens } 17351d452cf5Sahrens 1736a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) { 1737adf34077SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, 1738a2afb611SJerry Jelinek SPA_FEATURE_FS_SS_LIMIT)) { 1739a2afb611SJerry Jelinek /* 1740a2afb611SJerry Jelinek * Although this is the check function and we don't 1741a2afb611SJerry Jelinek * normally make on-disk changes in check functions, 1742a2afb611SJerry Jelinek * we need to do that here. 1743a2afb611SJerry Jelinek * 1744a2afb611SJerry Jelinek * Ensure this portion of the tree's counts have been 1745a2afb611SJerry Jelinek * initialized in case the new parent has limits set. 1746a2afb611SJerry Jelinek */ 1747a2afb611SJerry Jelinek dsl_dir_init_fs_ss_count(dd, tx); 1748a2afb611SJerry Jelinek } 1749a2afb611SJerry Jelinek } 1750a2afb611SJerry Jelinek 17513b2aab18SMatthew Ahrens if (newparent != dd->dd_parent) { 175299653d4eSeschrock /* is there enough space? */ 175399653d4eSeschrock uint64_t myspace = 1754c1379625SJustin T. Gibbs MAX(dsl_dir_phys(dd)->dd_used_bytes, 1755c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_reserved); 1756a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 1757a2afb611SJerry Jelinek uint64_t fs_cnt = 0; 1758a2afb611SJerry Jelinek uint64_t ss_cnt = 0; 1759a2afb611SJerry Jelinek 1760a2afb611SJerry Jelinek if (dsl_dir_is_zapified(dd)) { 1761a2afb611SJerry Jelinek int err; 1762a2afb611SJerry Jelinek 1763a2afb611SJerry Jelinek err = zap_lookup(os, dd->dd_object, 1764a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 1765a2afb611SJerry Jelinek &fs_cnt); 1766adf34077SJerry Jelinek if (err != ENOENT && err != 0) { 1767adf34077SJerry Jelinek dsl_dir_rele(newparent, FTAG); 1768adf34077SJerry Jelinek dsl_dir_rele(dd, FTAG); 1769a2afb611SJerry Jelinek return (err); 1770adf34077SJerry Jelinek } 1771a2afb611SJerry Jelinek 1772a2afb611SJerry Jelinek /* 1773a2afb611SJerry Jelinek * have to add 1 for the filesystem itself that we're 1774a2afb611SJerry Jelinek * moving 1775a2afb611SJerry Jelinek */ 1776a2afb611SJerry Jelinek fs_cnt++; 1777a2afb611SJerry Jelinek 1778a2afb611SJerry Jelinek err = zap_lookup(os, dd->dd_object, 1779a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 1780a2afb611SJerry Jelinek &ss_cnt); 1781adf34077SJerry Jelinek if (err != ENOENT && err != 0) { 1782adf34077SJerry Jelinek dsl_dir_rele(newparent, FTAG); 1783adf34077SJerry Jelinek dsl_dir_rele(dd, FTAG); 1784a2afb611SJerry Jelinek return (err); 1785adf34077SJerry Jelinek } 1786a2afb611SJerry Jelinek } 1787fa9e4066Sahrens 17881d452cf5Sahrens /* no rename into our descendant */ 17893b2aab18SMatthew Ahrens if (closest_common_ancestor(dd, newparent) == dd) { 17903b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17913b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 1792be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 17933b2aab18SMatthew Ahrens } 1794fa9e4066Sahrens 17953b2aab18SMatthew Ahrens error = dsl_dir_transfer_possible(dd->dd_parent, 1796a2afb611SJerry Jelinek newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred); 17973b2aab18SMatthew Ahrens if (error != 0) { 17983b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 17993b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 18003b2aab18SMatthew Ahrens return (error); 18013b2aab18SMatthew Ahrens } 18021d452cf5Sahrens } 1803fa9e4066Sahrens 18043b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 18053b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 18061d452cf5Sahrens return (0); 18071d452cf5Sahrens } 18081d452cf5Sahrens 18091d452cf5Sahrens static void 18103b2aab18SMatthew Ahrens dsl_dir_rename_sync(void *arg, dmu_tx_t *tx) 18111d452cf5Sahrens { 18123b2aab18SMatthew Ahrens dsl_dir_rename_arg_t *ddra = arg; 18133b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 18143b2aab18SMatthew Ahrens dsl_dir_t *dd, *newparent; 18153b2aab18SMatthew Ahrens const char *mynewname; 18163b2aab18SMatthew Ahrens int error; 18171d452cf5Sahrens objset_t *mos = dp->dp_meta_objset; 18181d452cf5Sahrens 18193b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL)); 18203b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent, 18213b2aab18SMatthew Ahrens &mynewname)); 18221d452cf5Sahrens 18234445fffbSMatthew Ahrens /* Log this before we change the name. */ 18244445fffbSMatthew Ahrens spa_history_log_internal_dd(dd, "rename", tx, 18253b2aab18SMatthew Ahrens "-> %s", ddra->ddra_newname); 18264445fffbSMatthew Ahrens 18273b2aab18SMatthew Ahrens if (newparent != dd->dd_parent) { 1828a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 1829a2afb611SJerry Jelinek uint64_t fs_cnt = 0; 1830a2afb611SJerry Jelinek uint64_t ss_cnt = 0; 1831a2afb611SJerry Jelinek 1832a2afb611SJerry Jelinek /* 1833a2afb611SJerry Jelinek * We already made sure the dd counts were initialized in the 1834a2afb611SJerry Jelinek * check function. 1835a2afb611SJerry Jelinek */ 1836adf34077SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, 1837a2afb611SJerry Jelinek SPA_FEATURE_FS_SS_LIMIT)) { 1838a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, dd->dd_object, 1839a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, 1840a2afb611SJerry Jelinek &fs_cnt)); 1841a2afb611SJerry Jelinek /* add 1 for the filesystem itself that we're moving */ 1842a2afb611SJerry Jelinek fs_cnt++; 1843a2afb611SJerry Jelinek 1844a2afb611SJerry Jelinek VERIFY0(zap_lookup(os, dd->dd_object, 1845a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, 1846a2afb611SJerry Jelinek &ss_cnt)); 1847a2afb611SJerry Jelinek } 1848a2afb611SJerry Jelinek 1849a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt, 1850a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx); 1851a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(newparent, fs_cnt, 1852a2afb611SJerry Jelinek DD_FIELD_FILESYSTEM_COUNT, tx); 1853a2afb611SJerry Jelinek 1854a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt, 1855a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 1856a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(newparent, ss_cnt, 1857a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 1858a2afb611SJerry Jelinek 185974e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 1860c1379625SJustin T. Gibbs -dsl_dir_phys(dd)->dd_used_bytes, 1861c1379625SJustin T. Gibbs -dsl_dir_phys(dd)->dd_compressed_bytes, 1862c1379625SJustin T. Gibbs -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); 18633b2aab18SMatthew Ahrens dsl_dir_diduse_space(newparent, DD_USED_CHILD, 1864c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes, 1865c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_compressed_bytes, 1866c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); 186774e7dc98SMatthew Ahrens 1868c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_reserved > 1869c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes) { 1870c1379625SJustin T. Gibbs uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved - 1871c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_bytes; 187274e7dc98SMatthew Ahrens 187374e7dc98SMatthew Ahrens dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 187474e7dc98SMatthew Ahrens -unused_rsrv, 0, 0, tx); 18753b2aab18SMatthew Ahrens dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV, 187674e7dc98SMatthew Ahrens unused_rsrv, 0, 0, tx); 187774e7dc98SMatthew Ahrens } 1878fa9e4066Sahrens } 1879fa9e4066Sahrens 1880fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1881fa9e4066Sahrens 1882fa9e4066Sahrens /* remove from old parent zapobj */ 1883c1379625SJustin T. Gibbs error = zap_remove(mos, 1884c1379625SJustin T. Gibbs dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj, 1885fa9e4066Sahrens dd->dd_myname, tx); 18863b2aab18SMatthew Ahrens ASSERT0(error); 1887fa9e4066Sahrens 18883b2aab18SMatthew Ahrens (void) strcpy(dd->dd_myname, mynewname); 18893b2aab18SMatthew Ahrens dsl_dir_rele(dd->dd_parent, dd); 1890c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object; 18913b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, 18923b2aab18SMatthew Ahrens newparent->dd_object, NULL, dd, &dd->dd_parent)); 1893fa9e4066Sahrens 1894fa9e4066Sahrens /* add to new parent zapobj */ 1895c1379625SJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj, 18963b2aab18SMatthew Ahrens dd->dd_myname, 8, 1, &dd->dd_object, tx)); 18973b2aab18SMatthew Ahrens 18983b2aab18SMatthew Ahrens dsl_prop_notify_all(dd); 1899ecd6cf80Smarks 19003b2aab18SMatthew Ahrens dsl_dir_rele(newparent, FTAG); 19013b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 19021d452cf5Sahrens } 1903fa9e4066Sahrens 19041d452cf5Sahrens int 19053b2aab18SMatthew Ahrens dsl_dir_rename(const char *oldname, const char *newname) 19061d452cf5Sahrens { 19073b2aab18SMatthew Ahrens dsl_dir_rename_arg_t ddra; 19081d452cf5Sahrens 19093b2aab18SMatthew Ahrens ddra.ddra_oldname = oldname; 19103b2aab18SMatthew Ahrens ddra.ddra_newname = newname; 1911a2afb611SJerry Jelinek ddra.ddra_cred = CRED(); 19121d452cf5Sahrens 19133b2aab18SMatthew Ahrens return (dsl_sync_task(oldname, 19147d46dc6cSMatthew Ahrens dsl_dir_rename_check, dsl_dir_rename_sync, &ddra, 19157d46dc6cSMatthew Ahrens 3, ZFS_SPACE_CHECK_RESERVED)); 1916fa9e4066Sahrens } 191799653d4eSeschrock 191899653d4eSeschrock int 1919a2afb611SJerry Jelinek dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, 1920a2afb611SJerry Jelinek uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr) 192199653d4eSeschrock { 192299653d4eSeschrock dsl_dir_t *ancestor; 192399653d4eSeschrock int64_t adelta; 192499653d4eSeschrock uint64_t avail; 1925a2afb611SJerry Jelinek int err; 192699653d4eSeschrock 192799653d4eSeschrock ancestor = closest_common_ancestor(sdd, tdd); 192899653d4eSeschrock adelta = would_change(sdd, -space, ancestor); 192999653d4eSeschrock avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); 193099653d4eSeschrock if (avail < space) 1931be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 193299653d4eSeschrock 1933a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT, 1934a2afb611SJerry Jelinek ancestor, cr); 1935a2afb611SJerry Jelinek if (err != 0) 1936a2afb611SJerry Jelinek return (err); 1937a2afb611SJerry Jelinek err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT, 1938a2afb611SJerry Jelinek ancestor, cr); 1939a2afb611SJerry Jelinek if (err != 0) 1940a2afb611SJerry Jelinek return (err); 1941a2afb611SJerry Jelinek 194299653d4eSeschrock return (0); 194399653d4eSeschrock } 194471eb0538SChris Kirby 194571eb0538SChris Kirby timestruc_t 194671eb0538SChris Kirby dsl_dir_snap_cmtime(dsl_dir_t *dd) 194771eb0538SChris Kirby { 194871eb0538SChris Kirby timestruc_t t; 194971eb0538SChris Kirby 195071eb0538SChris Kirby mutex_enter(&dd->dd_lock); 195171eb0538SChris Kirby t = dd->dd_snap_cmtime; 195271eb0538SChris Kirby mutex_exit(&dd->dd_lock); 195371eb0538SChris Kirby 195471eb0538SChris Kirby return (t); 195571eb0538SChris Kirby } 195671eb0538SChris Kirby 195771eb0538SChris Kirby void 195871eb0538SChris Kirby dsl_dir_snap_cmtime_update(dsl_dir_t *dd) 195971eb0538SChris Kirby { 196071eb0538SChris Kirby timestruc_t t; 196171eb0538SChris Kirby 196271eb0538SChris Kirby gethrestime(&t); 196371eb0538SChris Kirby mutex_enter(&dd->dd_lock); 196471eb0538SChris Kirby dd->dd_snap_cmtime = t; 196571eb0538SChris Kirby mutex_exit(&dd->dd_lock); 196671eb0538SChris Kirby } 19672acef22dSMatthew Ahrens 19682acef22dSMatthew Ahrens void 19692acef22dSMatthew Ahrens dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx) 19702acef22dSMatthew Ahrens { 19712acef22dSMatthew Ahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 19722acef22dSMatthew Ahrens dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx); 19732acef22dSMatthew Ahrens } 1974a2afb611SJerry Jelinek 1975a2afb611SJerry Jelinek boolean_t 1976a2afb611SJerry Jelinek dsl_dir_is_zapified(dsl_dir_t *dd) 1977a2afb611SJerry Jelinek { 1978a2afb611SJerry Jelinek dmu_object_info_t doi; 1979a2afb611SJerry Jelinek 1980a2afb611SJerry Jelinek dmu_object_info_from_db(dd->dd_dbuf, &doi); 1981a2afb611SJerry Jelinek return (doi.doi_type == DMU_OTN_ZAP_METADATA); 1982a2afb611SJerry Jelinek } 1983