1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 228525SEric.Schrock@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/dmu.h> 275378Sck153898 #include <sys/dmu_objset.h> 28789Sahrens #include <sys/dmu_tx.h> 29789Sahrens #include <sys/dsl_dataset.h> 30789Sahrens #include <sys/dsl_dir.h> 31789Sahrens #include <sys/dsl_prop.h> 322199Sahrens #include <sys/dsl_synctask.h> 334543Smarks #include <sys/dsl_deleg.h> 34789Sahrens #include <sys/spa.h> 35789Sahrens #include <sys/zap.h> 36789Sahrens #include <sys/zio.h> 37789Sahrens #include <sys/arc.h> 384543Smarks #include <sys/sunddi.h> 39789Sahrens #include "zfs_namecheck.h" 40789Sahrens 415378Sck153898 static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); 424543Smarks static void dsl_dir_set_reservation_sync(void *arg1, void *arg2, 434543Smarks cred_t *cr, dmu_tx_t *tx); 44789Sahrens 45789Sahrens 46789Sahrens /* ARGSUSED */ 47789Sahrens static void 48789Sahrens dsl_dir_evict(dmu_buf_t *db, void *arg) 49789Sahrens { 50789Sahrens dsl_dir_t *dd = arg; 51789Sahrens dsl_pool_t *dp = dd->dd_pool; 52789Sahrens int t; 53789Sahrens 54789Sahrens for (t = 0; t < TXG_SIZE; t++) { 55789Sahrens ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); 56789Sahrens ASSERT(dd->dd_tempreserved[t] == 0); 57789Sahrens ASSERT(dd->dd_space_towrite[t] == 0); 58789Sahrens } 59789Sahrens 60789Sahrens if (dd->dd_parent) 61789Sahrens dsl_dir_close(dd->dd_parent, dd); 62789Sahrens 63789Sahrens spa_close(dd->dd_pool->dp_spa, dd); 64789Sahrens 65789Sahrens /* 66789Sahrens * The props callback list should be empty since they hold the 67789Sahrens * dir open. 68789Sahrens */ 69789Sahrens list_destroy(&dd->dd_prop_cbs); 702856Snd150628 mutex_destroy(&dd->dd_lock); 71789Sahrens kmem_free(dd, sizeof (dsl_dir_t)); 72789Sahrens } 73789Sahrens 741544Seschrock int 75789Sahrens dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj, 761544Seschrock const char *tail, void *tag, dsl_dir_t **ddp) 77789Sahrens { 78789Sahrens dmu_buf_t *dbuf; 79789Sahrens dsl_dir_t *dd; 801544Seschrock int err; 81789Sahrens 82789Sahrens ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 83789Sahrens dsl_pool_sync_context(dp)); 84789Sahrens 851544Seschrock err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); 861544Seschrock if (err) 871544Seschrock return (err); 88789Sahrens dd = dmu_buf_get_user(dbuf); 89789Sahrens #ifdef ZFS_DEBUG 90789Sahrens { 91789Sahrens dmu_object_info_t doi; 92789Sahrens dmu_object_info_from_db(dbuf, &doi); 93928Stabriz ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR); 947390SMatthew.Ahrens@Sun.COM ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); 95789Sahrens } 96789Sahrens #endif 97789Sahrens if (dd == NULL) { 98789Sahrens dsl_dir_t *winner; 99789Sahrens 100789Sahrens dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); 101789Sahrens dd->dd_object = ddobj; 102789Sahrens dd->dd_dbuf = dbuf; 103789Sahrens dd->dd_pool = dp; 104789Sahrens dd->dd_phys = dbuf->db_data; 1052856Snd150628 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); 106789Sahrens 107789Sahrens list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t), 108789Sahrens offsetof(dsl_prop_cb_record_t, cbr_node)); 109789Sahrens 110789Sahrens if (dd->dd_phys->dd_parent_obj) { 1111544Seschrock err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj, 1121544Seschrock NULL, dd, &dd->dd_parent); 1137390SMatthew.Ahrens@Sun.COM if (err) 1147390SMatthew.Ahrens@Sun.COM goto errout; 115789Sahrens if (tail) { 116789Sahrens #ifdef ZFS_DEBUG 117789Sahrens uint64_t foundobj; 118789Sahrens 119789Sahrens err = zap_lookup(dp->dp_meta_objset, 1204577Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, 121789Sahrens tail, sizeof (foundobj), 1, &foundobj); 1221544Seschrock ASSERT(err || foundobj == ddobj); 123789Sahrens #endif 124789Sahrens (void) strcpy(dd->dd_myname, tail); 125789Sahrens } else { 126789Sahrens err = zap_value_search(dp->dp_meta_objset, 1274577Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, 1284577Sahrens ddobj, 0, dd->dd_myname); 1291544Seschrock } 1307390SMatthew.Ahrens@Sun.COM if (err) 1317390SMatthew.Ahrens@Sun.COM goto errout; 132789Sahrens } else { 133789Sahrens (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); 134789Sahrens } 135789Sahrens 136789Sahrens winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys, 137789Sahrens dsl_dir_evict); 138789Sahrens if (winner) { 139789Sahrens if (dd->dd_parent) 140789Sahrens dsl_dir_close(dd->dd_parent, dd); 1412856Snd150628 mutex_destroy(&dd->dd_lock); 142789Sahrens kmem_free(dd, sizeof (dsl_dir_t)); 143789Sahrens dd = winner; 144789Sahrens } else { 145789Sahrens spa_open_ref(dp->dp_spa, dd); 146789Sahrens } 147789Sahrens } 148789Sahrens 149789Sahrens /* 150789Sahrens * The dsl_dir_t has both open-to-close and instantiate-to-evict 151789Sahrens * holds on the spa. We need the open-to-close holds because 152789Sahrens * otherwise the spa_refcnt wouldn't change when we open a 153789Sahrens * dir which the spa also has open, so we could incorrectly 154789Sahrens * think it was OK to unload/export/destroy the pool. We need 155789Sahrens * the instantiate-to-evict hold because the dsl_dir_t has a 156789Sahrens * pointer to the dd_pool, which has a pointer to the spa_t. 157789Sahrens */ 158789Sahrens spa_open_ref(dp->dp_spa, tag); 159789Sahrens ASSERT3P(dd->dd_pool, ==, dp); 160789Sahrens ASSERT3U(dd->dd_object, ==, ddobj); 161789Sahrens ASSERT3P(dd->dd_dbuf, ==, dbuf); 1621544Seschrock *ddp = dd; 1631544Seschrock return (0); 1647390SMatthew.Ahrens@Sun.COM 1657390SMatthew.Ahrens@Sun.COM errout: 1667390SMatthew.Ahrens@Sun.COM if (dd->dd_parent) 1677390SMatthew.Ahrens@Sun.COM dsl_dir_close(dd->dd_parent, dd); 1687390SMatthew.Ahrens@Sun.COM mutex_destroy(&dd->dd_lock); 1697390SMatthew.Ahrens@Sun.COM kmem_free(dd, sizeof (dsl_dir_t)); 1707390SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, tag); 1717390SMatthew.Ahrens@Sun.COM return (err); 1727390SMatthew.Ahrens@Sun.COM 173789Sahrens } 174789Sahrens 175789Sahrens void 176789Sahrens dsl_dir_close(dsl_dir_t *dd, void *tag) 177789Sahrens { 178789Sahrens dprintf_dd(dd, "%s\n", ""); 179789Sahrens spa_close(dd->dd_pool->dp_spa, tag); 1801544Seschrock dmu_buf_rele(dd->dd_dbuf, tag); 181789Sahrens } 182789Sahrens 1832467Sek110237 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */ 184789Sahrens void 185789Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf) 186789Sahrens { 187789Sahrens if (dd->dd_parent) { 188789Sahrens dsl_dir_name(dd->dd_parent, buf); 189789Sahrens (void) strcat(buf, "/"); 190789Sahrens } else { 191789Sahrens buf[0] = '\0'; 192789Sahrens } 193789Sahrens if (!MUTEX_HELD(&dd->dd_lock)) { 194789Sahrens /* 195789Sahrens * recursive mutex so that we can use 196789Sahrens * dprintf_dd() with dd_lock held 197789Sahrens */ 198789Sahrens mutex_enter(&dd->dd_lock); 199789Sahrens (void) strcat(buf, dd->dd_myname); 200789Sahrens mutex_exit(&dd->dd_lock); 201789Sahrens } else { 202789Sahrens (void) strcat(buf, dd->dd_myname); 203789Sahrens } 204789Sahrens } 205789Sahrens 2063978Smmusante /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */ 2073978Smmusante int 2083978Smmusante dsl_dir_namelen(dsl_dir_t *dd) 2093978Smmusante { 2103978Smmusante int result = 0; 2113978Smmusante 2123978Smmusante if (dd->dd_parent) { 2133978Smmusante /* parent's name + 1 for the "/" */ 2143978Smmusante result = dsl_dir_namelen(dd->dd_parent) + 1; 2153978Smmusante } 2163978Smmusante 2173978Smmusante if (!MUTEX_HELD(&dd->dd_lock)) { 2183978Smmusante /* see dsl_dir_name */ 2193978Smmusante mutex_enter(&dd->dd_lock); 2203978Smmusante result += strlen(dd->dd_myname); 2213978Smmusante mutex_exit(&dd->dd_lock); 2223978Smmusante } else { 2233978Smmusante result += strlen(dd->dd_myname); 2243978Smmusante } 2253978Smmusante 2263978Smmusante return (result); 2273978Smmusante } 2283978Smmusante 229789Sahrens static int 230789Sahrens getcomponent(const char *path, char *component, const char **nextp) 231789Sahrens { 232789Sahrens char *p; 2338924SRichard.Morris@Sun.COM if ((path == NULL) || (path[0] == '\0')) 2342731Snd150628 return (ENOENT); 235789Sahrens /* This would be a good place to reserve some namespace... */ 236789Sahrens p = strpbrk(path, "/@"); 237789Sahrens if (p && (p[1] == '/' || p[1] == '@')) { 238789Sahrens /* two separators in a row */ 239789Sahrens return (EINVAL); 240789Sahrens } 241789Sahrens if (p == NULL || p == path) { 242789Sahrens /* 243789Sahrens * if the first thing is an @ or /, it had better be an 244789Sahrens * @ and it had better not have any more ats or slashes, 245789Sahrens * and it had better have something after the @. 246789Sahrens */ 247789Sahrens if (p != NULL && 248789Sahrens (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) 249789Sahrens return (EINVAL); 250789Sahrens if (strlen(path) >= MAXNAMELEN) 251789Sahrens return (ENAMETOOLONG); 252789Sahrens (void) strcpy(component, path); 253789Sahrens p = NULL; 254789Sahrens } else if (p[0] == '/') { 255789Sahrens if (p-path >= MAXNAMELEN) 256789Sahrens return (ENAMETOOLONG); 257789Sahrens (void) strncpy(component, path, p - path); 258789Sahrens component[p-path] = '\0'; 259789Sahrens p++; 260789Sahrens } else if (p[0] == '@') { 261789Sahrens /* 262789Sahrens * if the next separator is an @, there better not be 263789Sahrens * any more slashes. 264789Sahrens */ 265789Sahrens if (strchr(path, '/')) 266789Sahrens return (EINVAL); 267789Sahrens if (p-path >= MAXNAMELEN) 268789Sahrens return (ENAMETOOLONG); 269789Sahrens (void) strncpy(component, path, p - path); 270789Sahrens component[p-path] = '\0'; 271789Sahrens } else { 272789Sahrens ASSERT(!"invalid p"); 273789Sahrens } 274789Sahrens *nextp = p; 275789Sahrens return (0); 276789Sahrens } 277789Sahrens 278789Sahrens /* 279789Sahrens * same as dsl_open_dir, ignore the first component of name and use the 280789Sahrens * spa instead 281789Sahrens */ 2821544Seschrock int 2831544Seschrock dsl_dir_open_spa(spa_t *spa, const char *name, void *tag, 2841544Seschrock dsl_dir_t **ddp, const char **tailp) 285789Sahrens { 286789Sahrens char buf[MAXNAMELEN]; 287789Sahrens const char *next, *nextnext = NULL; 288789Sahrens int err; 289789Sahrens dsl_dir_t *dd; 290789Sahrens dsl_pool_t *dp; 291789Sahrens uint64_t ddobj; 292789Sahrens int openedspa = FALSE; 293789Sahrens 294789Sahrens dprintf("%s\n", name); 295789Sahrens 296789Sahrens err = getcomponent(name, buf, &next); 297789Sahrens if (err) 2981544Seschrock return (err); 299789Sahrens if (spa == NULL) { 300789Sahrens err = spa_open(buf, &spa, FTAG); 301789Sahrens if (err) { 302789Sahrens dprintf("spa_open(%s) failed\n", buf); 3031544Seschrock return (err); 304789Sahrens } 305789Sahrens openedspa = TRUE; 306789Sahrens 307789Sahrens /* XXX this assertion belongs in spa_open */ 308789Sahrens ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa))); 309789Sahrens } 310789Sahrens 311789Sahrens dp = spa_get_dsl(spa); 312789Sahrens 313789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 3141544Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); 3151544Seschrock if (err) { 3161544Seschrock rw_exit(&dp->dp_config_rwlock); 3171544Seschrock if (openedspa) 3181544Seschrock spa_close(spa, FTAG); 3191544Seschrock return (err); 3201544Seschrock } 3211544Seschrock 322789Sahrens while (next != NULL) { 323789Sahrens dsl_dir_t *child_ds; 324789Sahrens err = getcomponent(next, buf, &nextnext); 3251544Seschrock if (err) 3261544Seschrock break; 327789Sahrens ASSERT(next[0] != '\0'); 328789Sahrens if (next[0] == '@') 329789Sahrens break; 330789Sahrens dprintf("looking up %s in obj%lld\n", 331789Sahrens buf, dd->dd_phys->dd_child_dir_zapobj); 332789Sahrens 333789Sahrens err = zap_lookup(dp->dp_meta_objset, 334789Sahrens dd->dd_phys->dd_child_dir_zapobj, 335789Sahrens buf, sizeof (ddobj), 1, &ddobj); 3361544Seschrock if (err) { 3371544Seschrock if (err == ENOENT) 3381544Seschrock err = 0; 339789Sahrens break; 340789Sahrens } 341789Sahrens 3421544Seschrock err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds); 3431544Seschrock if (err) 3441544Seschrock break; 345789Sahrens dsl_dir_close(dd, tag); 346789Sahrens dd = child_ds; 347789Sahrens next = nextnext; 348789Sahrens } 349789Sahrens rw_exit(&dp->dp_config_rwlock); 350789Sahrens 3511544Seschrock if (err) { 3521544Seschrock dsl_dir_close(dd, tag); 3531544Seschrock if (openedspa) 3541544Seschrock spa_close(spa, FTAG); 3551544Seschrock return (err); 3561544Seschrock } 3571544Seschrock 358789Sahrens /* 359789Sahrens * It's an error if there's more than one component left, or 360789Sahrens * tailp==NULL and there's any component left. 361789Sahrens */ 362789Sahrens if (next != NULL && 363789Sahrens (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { 364789Sahrens /* bad path name */ 365789Sahrens dsl_dir_close(dd, tag); 366789Sahrens dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); 3671544Seschrock err = ENOENT; 368789Sahrens } 369789Sahrens if (tailp) 370789Sahrens *tailp = next; 371789Sahrens if (openedspa) 372789Sahrens spa_close(spa, FTAG); 3731544Seschrock *ddp = dd; 3741544Seschrock return (err); 375789Sahrens } 376789Sahrens 377789Sahrens /* 378789Sahrens * Return the dsl_dir_t, and possibly the last component which couldn't 379789Sahrens * be found in *tail. Return NULL if the path is bogus, or if 380789Sahrens * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@' 381789Sahrens * means that the last component is a snapshot. 382789Sahrens */ 3831544Seschrock int 3841544Seschrock dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp) 385789Sahrens { 3861544Seschrock return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp)); 387789Sahrens } 388789Sahrens 3892199Sahrens uint64_t 3907046Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, 3917046Sahrens dmu_tx_t *tx) 392789Sahrens { 3937046Sahrens objset_t *mos = dp->dp_meta_objset; 394789Sahrens uint64_t ddobj; 395789Sahrens dsl_dir_phys_t *dsphys; 396789Sahrens dmu_buf_t *dbuf; 397789Sahrens 398928Stabriz ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, 399928Stabriz DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); 4007046Sahrens if (pds) { 4017046Sahrens VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj, 4027046Sahrens name, sizeof (uint64_t), 1, &ddobj, tx)); 4037046Sahrens } else { 4047046Sahrens /* it's the root dir */ 4057046Sahrens VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 4067046Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); 4077046Sahrens } 4081544Seschrock VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); 409789Sahrens dmu_buf_will_dirty(dbuf, tx); 410789Sahrens dsphys = dbuf->db_data; 411789Sahrens 412789Sahrens dsphys->dd_creation_time = gethrestime_sec(); 4137046Sahrens if (pds) 4147046Sahrens dsphys->dd_parent_obj = pds->dd_object; 415789Sahrens dsphys->dd_props_zapobj = zap_create(mos, 416789Sahrens DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 417789Sahrens dsphys->dd_child_dir_zapobj = zap_create(mos, 418885Sahrens DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); 4197390SMatthew.Ahrens@Sun.COM if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) 4207390SMatthew.Ahrens@Sun.COM dsphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; 4211544Seschrock dmu_buf_rele(dbuf, FTAG); 422789Sahrens 4232199Sahrens return (ddobj); 4242199Sahrens } 4252199Sahrens 4262199Sahrens /* ARGSUSED */ 4272199Sahrens int 4282199Sahrens dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 4292199Sahrens { 4302199Sahrens dsl_dir_t *dd = arg1; 4312199Sahrens dsl_pool_t *dp = dd->dd_pool; 4322199Sahrens objset_t *mos = dp->dp_meta_objset; 4332199Sahrens int err; 4342199Sahrens uint64_t count; 4352199Sahrens 4362199Sahrens /* 4372199Sahrens * There should be exactly two holds, both from 4382199Sahrens * dsl_dataset_destroy: one on the dd directory, and one on its 4392199Sahrens * head ds. Otherwise, someone is trying to lookup something 4402199Sahrens * inside this dir while we want to destroy it. The 4412199Sahrens * config_rwlock ensures that nobody else opens it after we 4422199Sahrens * check. 4432199Sahrens */ 4442199Sahrens if (dmu_buf_refcount(dd->dd_dbuf) > 2) 4452199Sahrens return (EBUSY); 4462199Sahrens 4472199Sahrens err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count); 4482199Sahrens if (err) 4492199Sahrens return (err); 4502199Sahrens if (count != 0) 4512199Sahrens return (EEXIST); 452789Sahrens 453789Sahrens return (0); 454789Sahrens } 455789Sahrens 4562199Sahrens void 4574543Smarks dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 458789Sahrens { 4592199Sahrens dsl_dir_t *dd = arg1; 4602199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 4612199Sahrens uint64_t val, obj; 4627390SMatthew.Ahrens@Sun.COM dd_used_t t; 463789Sahrens 4642199Sahrens ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock)); 465789Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 466789Sahrens 4672199Sahrens /* Remove our reservation. */ 468789Sahrens val = 0; 4694543Smarks dsl_dir_set_reservation_sync(dd, &val, cr, tx); 4707390SMatthew.Ahrens@Sun.COM ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0); 471789Sahrens ASSERT3U(dd->dd_phys->dd_reserved, ==, 0); 4727390SMatthew.Ahrens@Sun.COM for (t = 0; t < DD_USED_NUM; t++) 4737390SMatthew.Ahrens@Sun.COM ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0); 474789Sahrens 4752199Sahrens VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx)); 4762199Sahrens VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx)); 4774543Smarks VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx)); 4782199Sahrens VERIFY(0 == zap_remove(mos, 4792199Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx)); 480789Sahrens 4812199Sahrens obj = dd->dd_object; 4822199Sahrens dsl_dir_close(dd, tag); 4832199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 484789Sahrens } 485789Sahrens 4867046Sahrens boolean_t 4877046Sahrens dsl_dir_is_clone(dsl_dir_t *dd) 488789Sahrens { 4897046Sahrens return (dd->dd_phys->dd_origin_obj && 4907046Sahrens (dd->dd_pool->dp_origin_snap == NULL || 4917046Sahrens dd->dd_phys->dd_origin_obj != 4927046Sahrens dd->dd_pool->dp_origin_snap->ds_object)); 493789Sahrens } 494789Sahrens 495789Sahrens void 4962885Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) 497789Sahrens { 498789Sahrens mutex_enter(&dd->dd_lock); 4997390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 5007390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes); 5015378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota); 5022885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, 5032885Sahrens dd->dd_phys->dd_reserved); 5042885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 5052885Sahrens dd->dd_phys->dd_compressed_bytes == 0 ? 100 : 5062885Sahrens (dd->dd_phys->dd_uncompressed_bytes * 100 / 5072885Sahrens dd->dd_phys->dd_compressed_bytes)); 5087390SMatthew.Ahrens@Sun.COM if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 5097390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, 5107390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]); 5117390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, 5127390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]); 5137390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, 5147390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]); 5157390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, 5167390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] + 5177390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]); 5187390SMatthew.Ahrens@Sun.COM } 519789Sahrens mutex_exit(&dd->dd_lock); 520789Sahrens 5215446Sahrens rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 5227046Sahrens if (dsl_dir_is_clone(dd)) { 523789Sahrens dsl_dataset_t *ds; 5242885Sahrens char buf[MAXNAMELEN]; 525789Sahrens 5266689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 5276689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &ds)); 5282885Sahrens dsl_dataset_name(ds, buf); 5296689Smaybee dsl_dataset_rele(ds, FTAG); 5302885Sahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); 531789Sahrens } 5325446Sahrens rw_exit(&dd->dd_pool->dp_config_rwlock); 533789Sahrens } 534789Sahrens 535789Sahrens void 536789Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) 537789Sahrens { 538789Sahrens dsl_pool_t *dp = dd->dd_pool; 539789Sahrens 540789Sahrens ASSERT(dd->dd_phys); 541789Sahrens 542789Sahrens if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) { 543789Sahrens /* up the hold count until we can be written out */ 544789Sahrens dmu_buf_add_ref(dd->dd_dbuf, dd); 545789Sahrens } 546789Sahrens } 547789Sahrens 548789Sahrens static int64_t 549789Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) 550789Sahrens { 551789Sahrens uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved); 552789Sahrens uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved); 553789Sahrens return (new_accounted - old_accounted); 554789Sahrens } 555789Sahrens 556789Sahrens void 557789Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) 558789Sahrens { 559789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 560789Sahrens 561789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 562789Sahrens 563789Sahrens mutex_enter(&dd->dd_lock); 564789Sahrens ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0); 565789Sahrens dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, 566789Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); 567789Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; 568789Sahrens mutex_exit(&dd->dd_lock); 569789Sahrens 570789Sahrens /* release the hold from dsl_dir_dirty */ 5711544Seschrock dmu_buf_rele(dd->dd_dbuf, dd); 572789Sahrens } 573789Sahrens 574789Sahrens static uint64_t 5755378Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd) 576789Sahrens { 5775378Sck153898 uint64_t space = 0; 578789Sahrens int i; 579789Sahrens 580789Sahrens ASSERT(MUTEX_HELD(&dd->dd_lock)); 581789Sahrens 582789Sahrens for (i = 0; i < TXG_SIZE; i++) { 583789Sahrens space += dd->dd_space_towrite[i&TXG_MASK]; 584789Sahrens ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0); 585789Sahrens } 586789Sahrens return (space); 587789Sahrens } 588789Sahrens 589789Sahrens /* 590789Sahrens * How much space would dd have available if ancestor had delta applied 591789Sahrens * to it? If ondiskonly is set, we're only interested in what's 592789Sahrens * on-disk, not estimated pending changes. 593789Sahrens */ 5942885Sahrens uint64_t 595789Sahrens dsl_dir_space_available(dsl_dir_t *dd, 596789Sahrens dsl_dir_t *ancestor, int64_t delta, int ondiskonly) 597789Sahrens { 598789Sahrens uint64_t parentspace, myspace, quota, used; 599789Sahrens 600789Sahrens /* 601789Sahrens * If there are no restrictions otherwise, assume we have 602789Sahrens * unlimited space available. 603789Sahrens */ 604789Sahrens quota = UINT64_MAX; 605789Sahrens parentspace = UINT64_MAX; 606789Sahrens 607789Sahrens if (dd->dd_parent != NULL) { 608789Sahrens parentspace = dsl_dir_space_available(dd->dd_parent, 609789Sahrens ancestor, delta, ondiskonly); 610789Sahrens } 611789Sahrens 612789Sahrens mutex_enter(&dd->dd_lock); 613789Sahrens if (dd->dd_phys->dd_quota != 0) 614789Sahrens quota = dd->dd_phys->dd_quota; 6157390SMatthew.Ahrens@Sun.COM used = dd->dd_phys->dd_used_bytes; 6165378Sck153898 if (!ondiskonly) 6175378Sck153898 used += dsl_dir_space_towrite(dd); 618789Sahrens 619789Sahrens if (dd->dd_parent == NULL) { 6202082Seschrock uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); 621789Sahrens quota = MIN(quota, poolsize); 622789Sahrens } 623789Sahrens 624789Sahrens if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) { 625789Sahrens /* 626789Sahrens * We have some space reserved, in addition to what our 627789Sahrens * parent gave us. 628789Sahrens */ 629789Sahrens parentspace += dd->dd_phys->dd_reserved - used; 630789Sahrens } 631789Sahrens 6327390SMatthew.Ahrens@Sun.COM if (dd == ancestor) { 6337390SMatthew.Ahrens@Sun.COM ASSERT(delta <= 0); 6347390SMatthew.Ahrens@Sun.COM ASSERT(used >= -delta); 6357390SMatthew.Ahrens@Sun.COM used += delta; 6367390SMatthew.Ahrens@Sun.COM if (parentspace != UINT64_MAX) 6377390SMatthew.Ahrens@Sun.COM parentspace -= delta; 6387390SMatthew.Ahrens@Sun.COM } 6397390SMatthew.Ahrens@Sun.COM 640789Sahrens if (used > quota) { 641789Sahrens /* over quota */ 642789Sahrens myspace = 0; 6432082Seschrock 6442082Seschrock /* 6452082Seschrock * While it's OK to be a little over quota, if 6462082Seschrock * we think we are using more space than there 6472082Seschrock * is in the pool (which is already 1.6% more than 6482082Seschrock * dsl_pool_adjustedsize()), something is very 6492082Seschrock * wrong. 6502082Seschrock */ 6512082Seschrock ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa)); 652789Sahrens } else { 653789Sahrens /* 6542082Seschrock * the lesser of the space provided by our parent and 6552082Seschrock * the space left in our quota 656789Sahrens */ 657789Sahrens myspace = MIN(parentspace, quota - used); 658789Sahrens } 659789Sahrens 660789Sahrens mutex_exit(&dd->dd_lock); 661789Sahrens 662789Sahrens return (myspace); 663789Sahrens } 664789Sahrens 665789Sahrens struct tempreserve { 666789Sahrens list_node_t tr_node; 6676245Smaybee dsl_pool_t *tr_dp; 668789Sahrens dsl_dir_t *tr_ds; 669789Sahrens uint64_t tr_size; 670789Sahrens }; 671789Sahrens 672789Sahrens static int 6735378Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, 6745378Sck153898 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list, 6756300Sck153898 dmu_tx_t *tx, boolean_t first) 676789Sahrens { 677789Sahrens uint64_t txg = tx->tx_txg; 6785378Sck153898 uint64_t est_inflight, used_on_disk, quota, parent_rsrv; 6795378Sck153898 struct tempreserve *tr; 6805392Smaybee int enospc = EDQUOT; 681789Sahrens int txgidx = txg & TXG_MASK; 682789Sahrens int i; 6835831Sck153898 uint64_t ref_rsrv = 0; 684789Sahrens 685789Sahrens ASSERT3U(txg, !=, 0); 6865378Sck153898 ASSERT3S(asize, >, 0); 687789Sahrens 688789Sahrens mutex_enter(&dd->dd_lock); 6895378Sck153898 690789Sahrens /* 691789Sahrens * Check against the dsl_dir's quota. We don't add in the delta 692789Sahrens * when checking for over-quota because they get one free hit. 693789Sahrens */ 6945378Sck153898 est_inflight = dsl_dir_space_towrite(dd); 695789Sahrens for (i = 0; i < TXG_SIZE; i++) 6965378Sck153898 est_inflight += dd->dd_tempreserved[i]; 6977390SMatthew.Ahrens@Sun.COM used_on_disk = dd->dd_phys->dd_used_bytes; 698789Sahrens 6994709Smaybee /* 7006300Sck153898 * On the first iteration, fetch the dataset's used-on-disk and 7016300Sck153898 * refreservation values. Also, if checkrefquota is set, test if 7026300Sck153898 * allocating this space would exceed the dataset's refquota. 7034709Smaybee */ 7046300Sck153898 if (first && tx->tx_objset) { 7055392Smaybee int error; 706*10298SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; 7075392Smaybee 7085378Sck153898 error = dsl_dataset_check_quota(ds, checkrefquota, 7095831Sck153898 asize, est_inflight, &used_on_disk, &ref_rsrv); 7105378Sck153898 if (error) { 7115378Sck153898 mutex_exit(&dd->dd_lock); 7125378Sck153898 return (error); 7135378Sck153898 } 7145378Sck153898 } 7155378Sck153898 7165378Sck153898 /* 7175378Sck153898 * If this transaction will result in a net free of space, 7185378Sck153898 * we want to let it through. 7195378Sck153898 */ 7205378Sck153898 if (ignorequota || netfree || dd->dd_phys->dd_quota == 0) 7214709Smaybee quota = UINT64_MAX; 7224709Smaybee else 723789Sahrens quota = dd->dd_phys->dd_quota; 724789Sahrens 725789Sahrens /* 7264709Smaybee * Adjust the quota against the actual pool size at the root. 7274709Smaybee * To ensure that it's possible to remove files from a full 7284709Smaybee * pool without inducing transient overcommits, we throttle 729789Sahrens * netfree transactions against a quota that is slightly larger, 730789Sahrens * but still within the pool's allocation slop. In cases where 731789Sahrens * we're very close to full, this will allow a steady trickle of 732789Sahrens * removes to get through. 733789Sahrens */ 7344944Smaybee if (dd->dd_parent == NULL) { 735789Sahrens uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); 736789Sahrens if (poolsize < quota) { 737789Sahrens quota = poolsize; 7385392Smaybee enospc = ENOSPC; 739789Sahrens } 740789Sahrens } 741789Sahrens 742789Sahrens /* 743789Sahrens * If they are requesting more space, and our current estimate 7445378Sck153898 * is over quota, they get to try again unless the actual 7451544Seschrock * on-disk is over quota and there are no pending changes (which 7461544Seschrock * may free up space for us). 747789Sahrens */ 7485378Sck153898 if (used_on_disk + est_inflight > quota) { 7495378Sck153898 if (est_inflight > 0 || used_on_disk < quota) 7505392Smaybee enospc = ERESTART; 7515378Sck153898 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " 752789Sahrens "quota=%lluK tr=%lluK err=%d\n", 7535378Sck153898 used_on_disk>>10, est_inflight>>10, 7545392Smaybee quota>>10, asize>>10, enospc); 755789Sahrens mutex_exit(&dd->dd_lock); 7565392Smaybee return (enospc); 757789Sahrens } 758789Sahrens 759789Sahrens /* We need to up our estimated delta before dropping dd_lock */ 760789Sahrens dd->dd_tempreserved[txgidx] += asize; 761789Sahrens 7625831Sck153898 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, 7635831Sck153898 asize - ref_rsrv); 764789Sahrens mutex_exit(&dd->dd_lock); 765789Sahrens 7666245Smaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 767789Sahrens tr->tr_ds = dd; 768789Sahrens tr->tr_size = asize; 769789Sahrens list_insert_tail(tr_list, tr); 770789Sahrens 771789Sahrens /* see if it's OK with our parent */ 7724944Smaybee if (dd->dd_parent && parent_rsrv) { 7734944Smaybee boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0); 7744944Smaybee 775789Sahrens return (dsl_dir_tempreserve_impl(dd->dd_parent, 7766300Sck153898 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE)); 777789Sahrens } else { 778789Sahrens return (0); 779789Sahrens } 780789Sahrens } 781789Sahrens 782789Sahrens /* 783789Sahrens * Reserve space in this dsl_dir, to be used in this tx's txg. 7845378Sck153898 * After the space has been dirtied (and dsl_dir_willuse_space() 7855378Sck153898 * has been called), the reservation should be canceled, using 7865378Sck153898 * dsl_dir_tempreserve_clear(). 787789Sahrens */ 788789Sahrens int 7895378Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, 7905378Sck153898 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx) 791789Sahrens { 7926245Smaybee int err; 793789Sahrens list_t *tr_list; 794789Sahrens 7955378Sck153898 if (asize == 0) { 7965378Sck153898 *tr_cookiep = NULL; 7975378Sck153898 return (0); 7985378Sck153898 } 7995378Sck153898 800789Sahrens tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); 801789Sahrens list_create(tr_list, sizeof (struct tempreserve), 802789Sahrens offsetof(struct tempreserve, tr_node)); 8035378Sck153898 ASSERT3S(asize, >, 0); 8041544Seschrock ASSERT3S(fsize, >=, 0); 805789Sahrens 8066245Smaybee err = arc_tempreserve_space(lsize, tx->tx_txg); 8076245Smaybee if (err == 0) { 8086245Smaybee struct tempreserve *tr; 8096245Smaybee 8106245Smaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 8116245Smaybee tr->tr_size = lsize; 8126245Smaybee list_insert_tail(tr_list, tr); 8136245Smaybee 8146245Smaybee err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx); 8156245Smaybee } else { 8166245Smaybee if (err == EAGAIN) { 8176245Smaybee txg_delay(dd->dd_pool, tx->tx_txg, 1); 8186245Smaybee err = ERESTART; 8196245Smaybee } 8206245Smaybee dsl_pool_memory_pressure(dd->dd_pool); 8216245Smaybee } 822789Sahrens 823789Sahrens if (err == 0) { 824789Sahrens struct tempreserve *tr; 825789Sahrens 8266245Smaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 8276245Smaybee tr->tr_dp = dd->dd_pool; 8286245Smaybee tr->tr_size = asize; 8296245Smaybee list_insert_tail(tr_list, tr); 8306245Smaybee 8316245Smaybee err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize, 8326300Sck153898 FALSE, asize > usize, tr_list, tx, TRUE); 833789Sahrens } 834789Sahrens 835789Sahrens if (err) 836789Sahrens dsl_dir_tempreserve_clear(tr_list, tx); 837789Sahrens else 838789Sahrens *tr_cookiep = tr_list; 8396245Smaybee 840789Sahrens return (err); 841789Sahrens } 842789Sahrens 843789Sahrens /* 844789Sahrens * Clear a temporary reservation that we previously made with 845789Sahrens * dsl_dir_tempreserve_space(). 846789Sahrens */ 847789Sahrens void 848789Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) 849789Sahrens { 850789Sahrens int txgidx = tx->tx_txg & TXG_MASK; 851789Sahrens list_t *tr_list = tr_cookie; 852789Sahrens struct tempreserve *tr; 853789Sahrens 854789Sahrens ASSERT3U(tx->tx_txg, !=, 0); 855789Sahrens 8565378Sck153898 if (tr_cookie == NULL) 8575378Sck153898 return; 8585378Sck153898 859789Sahrens while (tr = list_head(tr_list)) { 8606245Smaybee if (tr->tr_dp) { 8616245Smaybee dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx); 8626245Smaybee } else if (tr->tr_ds) { 863789Sahrens mutex_enter(&tr->tr_ds->dd_lock); 864789Sahrens ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, 865789Sahrens tr->tr_size); 866789Sahrens tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; 867789Sahrens mutex_exit(&tr->tr_ds->dd_lock); 8686245Smaybee } else { 8696245Smaybee arc_tempreserve_clear(tr->tr_size); 870789Sahrens } 871789Sahrens list_remove(tr_list, tr); 872789Sahrens kmem_free(tr, sizeof (struct tempreserve)); 873789Sahrens } 874789Sahrens 875789Sahrens kmem_free(tr_list, sizeof (list_t)); 876789Sahrens } 877789Sahrens 8786245Smaybee static void 8796245Smaybee dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 880789Sahrens { 881789Sahrens int64_t parent_space; 882789Sahrens uint64_t est_used; 883789Sahrens 884789Sahrens mutex_enter(&dd->dd_lock); 885789Sahrens if (space > 0) 886789Sahrens dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; 887789Sahrens 8887390SMatthew.Ahrens@Sun.COM est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes; 889789Sahrens parent_space = parent_delta(dd, est_used, space); 890789Sahrens mutex_exit(&dd->dd_lock); 891789Sahrens 892789Sahrens /* Make sure that we clean up dd_space_to* */ 893789Sahrens dsl_dir_dirty(dd, tx); 894789Sahrens 895789Sahrens /* XXX this is potentially expensive and unnecessary... */ 896789Sahrens if (parent_space && dd->dd_parent) 8976245Smaybee dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx); 8986245Smaybee } 8996245Smaybee 9006245Smaybee /* 9016245Smaybee * Call in open context when we think we're going to write/free space, 9026245Smaybee * eg. when dirtying data. Be conservative (ie. OK to write less than 9036245Smaybee * this or free more than this, but don't write more or free less). 9046245Smaybee */ 9056245Smaybee void 9066245Smaybee dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 9076245Smaybee { 9086245Smaybee dsl_pool_willuse_space(dd->dd_pool, space, tx); 9096245Smaybee dsl_dir_willuse_space_impl(dd, space, tx); 910789Sahrens } 911789Sahrens 912789Sahrens /* call from syncing context when we actually write/free space for this dd */ 913789Sahrens void 9147390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, 915789Sahrens int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) 916789Sahrens { 917789Sahrens int64_t accounted_delta; 9187595SMatthew.Ahrens@Sun.COM boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 919789Sahrens 920789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 9217390SMatthew.Ahrens@Sun.COM ASSERT(type < DD_USED_NUM); 922789Sahrens 923789Sahrens dsl_dir_dirty(dd, tx); 924789Sahrens 9257595SMatthew.Ahrens@Sun.COM if (needlock) 9267595SMatthew.Ahrens@Sun.COM mutex_enter(&dd->dd_lock); 9277390SMatthew.Ahrens@Sun.COM accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used); 9287390SMatthew.Ahrens@Sun.COM ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used); 929789Sahrens ASSERT(compressed >= 0 || 930789Sahrens dd->dd_phys->dd_compressed_bytes >= -compressed); 931789Sahrens ASSERT(uncompressed >= 0 || 932789Sahrens dd->dd_phys->dd_uncompressed_bytes >= -uncompressed); 9337390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes += used; 934789Sahrens dd->dd_phys->dd_uncompressed_bytes += uncompressed; 935789Sahrens dd->dd_phys->dd_compressed_bytes += compressed; 9367390SMatthew.Ahrens@Sun.COM 9377390SMatthew.Ahrens@Sun.COM if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 9387390SMatthew.Ahrens@Sun.COM ASSERT(used > 0 || 9397390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[type] >= -used); 9407390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[type] += used; 9417390SMatthew.Ahrens@Sun.COM #ifdef DEBUG 9427390SMatthew.Ahrens@Sun.COM dd_used_t t; 9437390SMatthew.Ahrens@Sun.COM uint64_t u = 0; 9447390SMatthew.Ahrens@Sun.COM for (t = 0; t < DD_USED_NUM; t++) 9457390SMatthew.Ahrens@Sun.COM u += dd->dd_phys->dd_used_breakdown[t]; 9467390SMatthew.Ahrens@Sun.COM ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes); 9477390SMatthew.Ahrens@Sun.COM #endif 9487390SMatthew.Ahrens@Sun.COM } 9497595SMatthew.Ahrens@Sun.COM if (needlock) 9507595SMatthew.Ahrens@Sun.COM mutex_exit(&dd->dd_lock); 951789Sahrens 952789Sahrens if (dd->dd_parent != NULL) { 9537390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 954789Sahrens accounted_delta, compressed, uncompressed, tx); 9557390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(dd->dd_parent, 9567390SMatthew.Ahrens@Sun.COM used - accounted_delta, 9577390SMatthew.Ahrens@Sun.COM DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); 958789Sahrens } 959789Sahrens } 960789Sahrens 9617390SMatthew.Ahrens@Sun.COM void 9627390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, 9637390SMatthew.Ahrens@Sun.COM dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 9647390SMatthew.Ahrens@Sun.COM { 9657595SMatthew.Ahrens@Sun.COM boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 9667595SMatthew.Ahrens@Sun.COM 9677390SMatthew.Ahrens@Sun.COM ASSERT(dmu_tx_is_syncing(tx)); 9687390SMatthew.Ahrens@Sun.COM ASSERT(oldtype < DD_USED_NUM); 9697390SMatthew.Ahrens@Sun.COM ASSERT(newtype < DD_USED_NUM); 9707390SMatthew.Ahrens@Sun.COM 9717390SMatthew.Ahrens@Sun.COM if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN)) 9727390SMatthew.Ahrens@Sun.COM return; 9737390SMatthew.Ahrens@Sun.COM 9747390SMatthew.Ahrens@Sun.COM dsl_dir_dirty(dd, tx); 9757595SMatthew.Ahrens@Sun.COM if (needlock) 9767595SMatthew.Ahrens@Sun.COM mutex_enter(&dd->dd_lock); 9777390SMatthew.Ahrens@Sun.COM ASSERT(delta > 0 ? 9787390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[oldtype] >= delta : 9797390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[newtype] >= -delta); 9807390SMatthew.Ahrens@Sun.COM ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta)); 9817390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[oldtype] -= delta; 9827390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[newtype] += delta; 9837595SMatthew.Ahrens@Sun.COM if (needlock) 9847595SMatthew.Ahrens@Sun.COM mutex_exit(&dd->dd_lock); 9857390SMatthew.Ahrens@Sun.COM } 9867390SMatthew.Ahrens@Sun.COM 987789Sahrens static int 9882199Sahrens dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 989789Sahrens { 9902199Sahrens dsl_dir_t *dd = arg1; 9912199Sahrens uint64_t *quotap = arg2; 992789Sahrens uint64_t new_quota = *quotap; 993789Sahrens int err = 0; 9942199Sahrens uint64_t towrite; 9952199Sahrens 9962199Sahrens if (new_quota == 0) 9972199Sahrens return (0); 9982199Sahrens 9992199Sahrens mutex_enter(&dd->dd_lock); 10002199Sahrens /* 10012199Sahrens * If we are doing the preliminary check in open context, and 10022199Sahrens * there are pending changes, then don't fail it, since the 10035378Sck153898 * pending changes could under-estimate the amount of space to be 10042199Sahrens * freed up. 10052199Sahrens */ 10065378Sck153898 towrite = dsl_dir_space_towrite(dd); 10072199Sahrens if ((dmu_tx_is_syncing(tx) || towrite == 0) && 10082199Sahrens (new_quota < dd->dd_phys->dd_reserved || 10097390SMatthew.Ahrens@Sun.COM new_quota < dd->dd_phys->dd_used_bytes + towrite)) { 10102199Sahrens err = ENOSPC; 10112199Sahrens } 10122199Sahrens mutex_exit(&dd->dd_lock); 10132199Sahrens return (err); 10142199Sahrens } 10152199Sahrens 10164543Smarks /* ARGSUSED */ 10172199Sahrens static void 10184543Smarks dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 10192199Sahrens { 10202199Sahrens dsl_dir_t *dd = arg1; 10212199Sahrens uint64_t *quotap = arg2; 10222199Sahrens uint64_t new_quota = *quotap; 1023789Sahrens 1024789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1025789Sahrens 1026789Sahrens mutex_enter(&dd->dd_lock); 10272199Sahrens dd->dd_phys->dd_quota = new_quota; 1028789Sahrens mutex_exit(&dd->dd_lock); 10294543Smarks 10304543Smarks spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa, 10314543Smarks tx, cr, "%lld dataset = %llu ", 10324543Smarks (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj); 1033789Sahrens } 1034789Sahrens 1035789Sahrens int 1036789Sahrens dsl_dir_set_quota(const char *ddname, uint64_t quota) 1037789Sahrens { 1038789Sahrens dsl_dir_t *dd; 1039789Sahrens int err; 1040789Sahrens 10411544Seschrock err = dsl_dir_open(ddname, FTAG, &dd, NULL); 10421544Seschrock if (err) 10431544Seschrock return (err); 1044789Sahrens 10455481Sck153898 if (quota != dd->dd_phys->dd_quota) { 10465481Sck153898 /* 10475481Sck153898 * If someone removes a file, then tries to set the quota, we 10485481Sck153898 * want to make sure the file freeing takes effect. 10495481Sck153898 */ 10505481Sck153898 txg_wait_open(dd->dd_pool, 0); 10515481Sck153898 10525481Sck153898 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check, 10535481Sck153898 dsl_dir_set_quota_sync, dd, "a, 0); 10545481Sck153898 } 1055789Sahrens dsl_dir_close(dd, FTAG); 1056789Sahrens return (err); 1057789Sahrens } 1058789Sahrens 10595378Sck153898 int 10602199Sahrens dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 1061789Sahrens { 10622199Sahrens dsl_dir_t *dd = arg1; 10632199Sahrens uint64_t *reservationp = arg2; 1064789Sahrens uint64_t new_reservation = *reservationp; 1065789Sahrens uint64_t used, avail; 1066789Sahrens 10672199Sahrens /* 10682199Sahrens * If we are doing the preliminary check in open context, the 10692199Sahrens * space estimates may be inaccurate. 10702199Sahrens */ 10712199Sahrens if (!dmu_tx_is_syncing(tx)) 10722199Sahrens return (0); 10732199Sahrens 1074789Sahrens mutex_enter(&dd->dd_lock); 10757390SMatthew.Ahrens@Sun.COM used = dd->dd_phys->dd_used_bytes; 1076789Sahrens mutex_exit(&dd->dd_lock); 1077789Sahrens 1078789Sahrens if (dd->dd_parent) { 1079789Sahrens avail = dsl_dir_space_available(dd->dd_parent, 1080789Sahrens NULL, 0, FALSE); 1081789Sahrens } else { 1082789Sahrens avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; 1083789Sahrens } 1084789Sahrens 10858525SEric.Schrock@Sun.COM if (MAX(used, new_reservation) > MAX(used, dd->dd_phys->dd_reserved)) { 10868525SEric.Schrock@Sun.COM uint64_t delta = MAX(used, new_reservation) - 10878525SEric.Schrock@Sun.COM MAX(used, dd->dd_phys->dd_reserved); 10888525SEric.Schrock@Sun.COM 10898525SEric.Schrock@Sun.COM if (delta > avail) 10908525SEric.Schrock@Sun.COM return (ENOSPC); 10918525SEric.Schrock@Sun.COM if (dd->dd_phys->dd_quota > 0 && 10928525SEric.Schrock@Sun.COM new_reservation > dd->dd_phys->dd_quota) 10938525SEric.Schrock@Sun.COM return (ENOSPC); 10948525SEric.Schrock@Sun.COM } 10958525SEric.Schrock@Sun.COM 10962199Sahrens return (0); 10972199Sahrens } 10982199Sahrens 10994543Smarks /* ARGSUSED */ 11002199Sahrens static void 11014543Smarks dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 11022199Sahrens { 11032199Sahrens dsl_dir_t *dd = arg1; 11042199Sahrens uint64_t *reservationp = arg2; 11052199Sahrens uint64_t new_reservation = *reservationp; 11062199Sahrens uint64_t used; 11072199Sahrens int64_t delta; 11082199Sahrens 11095378Sck153898 dmu_buf_will_dirty(dd->dd_dbuf, tx); 11105378Sck153898 11112199Sahrens mutex_enter(&dd->dd_lock); 11127390SMatthew.Ahrens@Sun.COM used = dd->dd_phys->dd_used_bytes; 11132199Sahrens delta = MAX(used, new_reservation) - 11142199Sahrens MAX(used, dd->dd_phys->dd_reserved); 11155378Sck153898 dd->dd_phys->dd_reserved = new_reservation; 1116789Sahrens 1117789Sahrens if (dd->dd_parent != NULL) { 1118789Sahrens /* Roll up this additional usage into our ancestors */ 11197390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 11207390SMatthew.Ahrens@Sun.COM delta, 0, 0, tx); 1121789Sahrens } 11227595SMatthew.Ahrens@Sun.COM mutex_exit(&dd->dd_lock); 11234543Smarks 11244543Smarks spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa, 11254543Smarks tx, cr, "%lld dataset = %llu", 11264543Smarks (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj); 1127789Sahrens } 1128789Sahrens 1129789Sahrens int 1130789Sahrens dsl_dir_set_reservation(const char *ddname, uint64_t reservation) 1131789Sahrens { 1132789Sahrens dsl_dir_t *dd; 1133789Sahrens int err; 1134789Sahrens 11351544Seschrock err = dsl_dir_open(ddname, FTAG, &dd, NULL); 11361544Seschrock if (err) 11371544Seschrock return (err); 11382199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check, 11392199Sahrens dsl_dir_set_reservation_sync, dd, &reservation, 0); 1140789Sahrens dsl_dir_close(dd, FTAG); 1141789Sahrens return (err); 1142789Sahrens } 1143789Sahrens 1144789Sahrens static dsl_dir_t * 1145789Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) 1146789Sahrens { 1147789Sahrens for (; ds1; ds1 = ds1->dd_parent) { 1148789Sahrens dsl_dir_t *dd; 1149789Sahrens for (dd = ds2; dd; dd = dd->dd_parent) { 1150789Sahrens if (ds1 == dd) 1151789Sahrens return (dd); 1152789Sahrens } 1153789Sahrens } 1154789Sahrens return (NULL); 1155789Sahrens } 1156789Sahrens 1157789Sahrens /* 1158789Sahrens * If delta is applied to dd, how much of that delta would be applied to 1159789Sahrens * ancestor? Syncing context only. 1160789Sahrens */ 1161789Sahrens static int64_t 1162789Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) 1163789Sahrens { 1164789Sahrens if (dd == ancestor) 1165789Sahrens return (delta); 1166789Sahrens 1167789Sahrens mutex_enter(&dd->dd_lock); 11687390SMatthew.Ahrens@Sun.COM delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta); 1169789Sahrens mutex_exit(&dd->dd_lock); 1170789Sahrens return (would_change(dd->dd_parent, delta, ancestor)); 1171789Sahrens } 1172789Sahrens 11732199Sahrens struct renamearg { 11742199Sahrens dsl_dir_t *newparent; 11752199Sahrens const char *mynewname; 11762199Sahrens }; 11772199Sahrens 11784543Smarks /*ARGSUSED*/ 11792199Sahrens static int 11802199Sahrens dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 1181789Sahrens { 11822199Sahrens dsl_dir_t *dd = arg1; 11832199Sahrens struct renamearg *ra = arg2; 1184789Sahrens dsl_pool_t *dp = dd->dd_pool; 1185789Sahrens objset_t *mos = dp->dp_meta_objset; 11862199Sahrens int err; 11872199Sahrens uint64_t val; 11882199Sahrens 11892199Sahrens /* There should be 2 references: the open and the dirty */ 11902199Sahrens if (dmu_buf_refcount(dd->dd_dbuf) > 2) 11912199Sahrens return (EBUSY); 1192789Sahrens 11932199Sahrens /* check for existing name */ 11942199Sahrens err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, 11952199Sahrens ra->mynewname, 8, 1, &val); 11962199Sahrens if (err == 0) 11972199Sahrens return (EEXIST); 11982199Sahrens if (err != ENOENT) 11991544Seschrock return (err); 1200789Sahrens 12012199Sahrens if (ra->newparent != dd->dd_parent) { 12022082Seschrock /* is there enough space? */ 12032082Seschrock uint64_t myspace = 12047390SMatthew.Ahrens@Sun.COM MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved); 1205789Sahrens 12062199Sahrens /* no rename into our descendant */ 12072199Sahrens if (closest_common_ancestor(dd, ra->newparent) == dd) 1208789Sahrens return (EINVAL); 12092199Sahrens 12102199Sahrens if (err = dsl_dir_transfer_possible(dd->dd_parent, 12112199Sahrens ra->newparent, myspace)) 12122199Sahrens return (err); 12132199Sahrens } 12142199Sahrens 12152199Sahrens return (0); 12162199Sahrens } 1217789Sahrens 12182199Sahrens static void 12194543Smarks dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 12202199Sahrens { 12212199Sahrens dsl_dir_t *dd = arg1; 12222199Sahrens struct renamearg *ra = arg2; 12232199Sahrens dsl_pool_t *dp = dd->dd_pool; 12242199Sahrens objset_t *mos = dp->dp_meta_objset; 12252199Sahrens int err; 1226789Sahrens 12272199Sahrens ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2); 12282199Sahrens 12292199Sahrens if (ra->newparent != dd->dd_parent) { 12307390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 12317390SMatthew.Ahrens@Sun.COM -dd->dd_phys->dd_used_bytes, 1232789Sahrens -dd->dd_phys->dd_compressed_bytes, 1233789Sahrens -dd->dd_phys->dd_uncompressed_bytes, tx); 12347390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD, 12357390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes, 1236789Sahrens dd->dd_phys->dd_compressed_bytes, 1237789Sahrens dd->dd_phys->dd_uncompressed_bytes, tx); 12387390SMatthew.Ahrens@Sun.COM 12397390SMatthew.Ahrens@Sun.COM if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) { 12407390SMatthew.Ahrens@Sun.COM uint64_t unused_rsrv = dd->dd_phys->dd_reserved - 12417390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes; 12427390SMatthew.Ahrens@Sun.COM 12437390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 12447390SMatthew.Ahrens@Sun.COM -unused_rsrv, 0, 0, tx); 12457390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV, 12467390SMatthew.Ahrens@Sun.COM unused_rsrv, 0, 0, tx); 12477390SMatthew.Ahrens@Sun.COM } 1248789Sahrens } 1249789Sahrens 1250789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1251789Sahrens 1252789Sahrens /* remove from old parent zapobj */ 1253789Sahrens err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj, 1254789Sahrens dd->dd_myname, tx); 1255789Sahrens ASSERT3U(err, ==, 0); 1256789Sahrens 12572199Sahrens (void) strcpy(dd->dd_myname, ra->mynewname); 1258789Sahrens dsl_dir_close(dd->dd_parent, dd); 12592199Sahrens dd->dd_phys->dd_parent_obj = ra->newparent->dd_object; 12601544Seschrock VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, 12612199Sahrens ra->newparent->dd_object, NULL, dd, &dd->dd_parent)); 1262789Sahrens 1263789Sahrens /* add to new parent zapobj */ 12642199Sahrens err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, 1265789Sahrens dd->dd_myname, 8, 1, &dd->dd_object, tx); 1266789Sahrens ASSERT3U(err, ==, 0); 12674543Smarks 12684543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, 12694543Smarks tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj); 12702199Sahrens } 1271789Sahrens 12722199Sahrens int 12732199Sahrens dsl_dir_rename(dsl_dir_t *dd, const char *newname) 12742199Sahrens { 12752199Sahrens struct renamearg ra; 12762199Sahrens int err; 12772199Sahrens 12782199Sahrens /* new parent should exist */ 12792199Sahrens err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname); 12802199Sahrens if (err) 12812199Sahrens return (err); 12822199Sahrens 12832199Sahrens /* can't rename to different pool */ 12842199Sahrens if (dd->dd_pool != ra.newparent->dd_pool) { 12852199Sahrens err = ENXIO; 12862199Sahrens goto out; 12872199Sahrens } 12882199Sahrens 12892199Sahrens /* new name should not already exist */ 12902199Sahrens if (ra.mynewname == NULL) { 12912199Sahrens err = EEXIST; 12922199Sahrens goto out; 12932199Sahrens } 12942199Sahrens 12952199Sahrens err = dsl_sync_task_do(dd->dd_pool, 12962199Sahrens dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3); 12972199Sahrens 12982199Sahrens out: 12992199Sahrens dsl_dir_close(ra.newparent, FTAG); 13002199Sahrens return (err); 1301789Sahrens } 13022082Seschrock 13032082Seschrock int 13042082Seschrock dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space) 13052082Seschrock { 13062082Seschrock dsl_dir_t *ancestor; 13072082Seschrock int64_t adelta; 13082082Seschrock uint64_t avail; 13092082Seschrock 13102082Seschrock ancestor = closest_common_ancestor(sdd, tdd); 13112082Seschrock adelta = would_change(sdd, -space, ancestor); 13122082Seschrock avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); 13132082Seschrock if (avail < space) 13142082Seschrock return (ENOSPC); 13152082Seschrock 13162082Seschrock return (0); 13172082Seschrock } 1318