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 /* 225831Sck153898 * Copyright 2008 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 int err; 100789Sahrens 101789Sahrens dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); 102789Sahrens dd->dd_object = ddobj; 103789Sahrens dd->dd_dbuf = dbuf; 104789Sahrens dd->dd_pool = dp; 105789Sahrens dd->dd_phys = dbuf->db_data; 1062856Snd150628 mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); 107789Sahrens 108789Sahrens list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t), 109789Sahrens offsetof(dsl_prop_cb_record_t, cbr_node)); 110789Sahrens 111789Sahrens if (dd->dd_phys->dd_parent_obj) { 1121544Seschrock err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj, 1131544Seschrock NULL, dd, &dd->dd_parent); 1147390SMatthew.Ahrens@Sun.COM if (err) 1157390SMatthew.Ahrens@Sun.COM goto errout; 116789Sahrens if (tail) { 117789Sahrens #ifdef ZFS_DEBUG 118789Sahrens uint64_t foundobj; 119789Sahrens 120789Sahrens err = zap_lookup(dp->dp_meta_objset, 1214577Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, 122789Sahrens tail, sizeof (foundobj), 1, &foundobj); 1231544Seschrock ASSERT(err || foundobj == ddobj); 124789Sahrens #endif 125789Sahrens (void) strcpy(dd->dd_myname, tail); 126789Sahrens } else { 127789Sahrens err = zap_value_search(dp->dp_meta_objset, 1284577Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, 1294577Sahrens ddobj, 0, dd->dd_myname); 1301544Seschrock } 1317390SMatthew.Ahrens@Sun.COM if (err) 1327390SMatthew.Ahrens@Sun.COM goto errout; 133789Sahrens } else { 134789Sahrens (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); 135789Sahrens } 136789Sahrens 137789Sahrens winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys, 138789Sahrens dsl_dir_evict); 139789Sahrens if (winner) { 140789Sahrens if (dd->dd_parent) 141789Sahrens dsl_dir_close(dd->dd_parent, dd); 1422856Snd150628 mutex_destroy(&dd->dd_lock); 143789Sahrens kmem_free(dd, sizeof (dsl_dir_t)); 144789Sahrens dd = winner; 145789Sahrens } else { 146789Sahrens spa_open_ref(dp->dp_spa, dd); 147789Sahrens } 148789Sahrens } 149789Sahrens 150789Sahrens /* 151789Sahrens * The dsl_dir_t has both open-to-close and instantiate-to-evict 152789Sahrens * holds on the spa. We need the open-to-close holds because 153789Sahrens * otherwise the spa_refcnt wouldn't change when we open a 154789Sahrens * dir which the spa also has open, so we could incorrectly 155789Sahrens * think it was OK to unload/export/destroy the pool. We need 156789Sahrens * the instantiate-to-evict hold because the dsl_dir_t has a 157789Sahrens * pointer to the dd_pool, which has a pointer to the spa_t. 158789Sahrens */ 159789Sahrens spa_open_ref(dp->dp_spa, tag); 160789Sahrens ASSERT3P(dd->dd_pool, ==, dp); 161789Sahrens ASSERT3U(dd->dd_object, ==, ddobj); 162789Sahrens ASSERT3P(dd->dd_dbuf, ==, dbuf); 1631544Seschrock *ddp = dd; 1641544Seschrock return (0); 1657390SMatthew.Ahrens@Sun.COM 1667390SMatthew.Ahrens@Sun.COM errout: 1677390SMatthew.Ahrens@Sun.COM if (dd->dd_parent) 1687390SMatthew.Ahrens@Sun.COM dsl_dir_close(dd->dd_parent, dd); 1697390SMatthew.Ahrens@Sun.COM mutex_destroy(&dd->dd_lock); 1707390SMatthew.Ahrens@Sun.COM kmem_free(dd, sizeof (dsl_dir_t)); 1717390SMatthew.Ahrens@Sun.COM dmu_buf_rele(dbuf, tag); 1727390SMatthew.Ahrens@Sun.COM return (err); 1737390SMatthew.Ahrens@Sun.COM 174789Sahrens } 175789Sahrens 176789Sahrens void 177789Sahrens dsl_dir_close(dsl_dir_t *dd, void *tag) 178789Sahrens { 179789Sahrens dprintf_dd(dd, "%s\n", ""); 180789Sahrens spa_close(dd->dd_pool->dp_spa, tag); 1811544Seschrock dmu_buf_rele(dd->dd_dbuf, tag); 182789Sahrens } 183789Sahrens 1842467Sek110237 /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */ 185789Sahrens void 186789Sahrens dsl_dir_name(dsl_dir_t *dd, char *buf) 187789Sahrens { 188789Sahrens if (dd->dd_parent) { 189789Sahrens dsl_dir_name(dd->dd_parent, buf); 190789Sahrens (void) strcat(buf, "/"); 191789Sahrens } else { 192789Sahrens buf[0] = '\0'; 193789Sahrens } 194789Sahrens if (!MUTEX_HELD(&dd->dd_lock)) { 195789Sahrens /* 196789Sahrens * recursive mutex so that we can use 197789Sahrens * dprintf_dd() with dd_lock held 198789Sahrens */ 199789Sahrens mutex_enter(&dd->dd_lock); 200789Sahrens (void) strcat(buf, dd->dd_myname); 201789Sahrens mutex_exit(&dd->dd_lock); 202789Sahrens } else { 203789Sahrens (void) strcat(buf, dd->dd_myname); 204789Sahrens } 205789Sahrens } 206789Sahrens 2073978Smmusante /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */ 2083978Smmusante int 2093978Smmusante dsl_dir_namelen(dsl_dir_t *dd) 2103978Smmusante { 2113978Smmusante int result = 0; 2123978Smmusante 2133978Smmusante if (dd->dd_parent) { 2143978Smmusante /* parent's name + 1 for the "/" */ 2153978Smmusante result = dsl_dir_namelen(dd->dd_parent) + 1; 2163978Smmusante } 2173978Smmusante 2183978Smmusante if (!MUTEX_HELD(&dd->dd_lock)) { 2193978Smmusante /* see dsl_dir_name */ 2203978Smmusante mutex_enter(&dd->dd_lock); 2213978Smmusante result += strlen(dd->dd_myname); 2223978Smmusante mutex_exit(&dd->dd_lock); 2233978Smmusante } else { 2243978Smmusante result += strlen(dd->dd_myname); 2253978Smmusante } 2263978Smmusante 2273978Smmusante return (result); 2283978Smmusante } 2293978Smmusante 230789Sahrens int 231789Sahrens dsl_dir_is_private(dsl_dir_t *dd) 232789Sahrens { 233789Sahrens int rv = FALSE; 234789Sahrens 235789Sahrens if (dd->dd_parent && dsl_dir_is_private(dd->dd_parent)) 236789Sahrens rv = TRUE; 237789Sahrens if (dataset_name_hidden(dd->dd_myname)) 238789Sahrens rv = TRUE; 239789Sahrens return (rv); 240789Sahrens } 241789Sahrens 242789Sahrens 243789Sahrens static int 244789Sahrens getcomponent(const char *path, char *component, const char **nextp) 245789Sahrens { 246789Sahrens char *p; 247789Sahrens if (path == NULL) 2482731Snd150628 return (ENOENT); 249789Sahrens /* This would be a good place to reserve some namespace... */ 250789Sahrens p = strpbrk(path, "/@"); 251789Sahrens if (p && (p[1] == '/' || p[1] == '@')) { 252789Sahrens /* two separators in a row */ 253789Sahrens return (EINVAL); 254789Sahrens } 255789Sahrens if (p == NULL || p == path) { 256789Sahrens /* 257789Sahrens * if the first thing is an @ or /, it had better be an 258789Sahrens * @ and it had better not have any more ats or slashes, 259789Sahrens * and it had better have something after the @. 260789Sahrens */ 261789Sahrens if (p != NULL && 262789Sahrens (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) 263789Sahrens return (EINVAL); 264789Sahrens if (strlen(path) >= MAXNAMELEN) 265789Sahrens return (ENAMETOOLONG); 266789Sahrens (void) strcpy(component, path); 267789Sahrens p = NULL; 268789Sahrens } else if (p[0] == '/') { 269789Sahrens if (p-path >= MAXNAMELEN) 270789Sahrens return (ENAMETOOLONG); 271789Sahrens (void) strncpy(component, path, p - path); 272789Sahrens component[p-path] = '\0'; 273789Sahrens p++; 274789Sahrens } else if (p[0] == '@') { 275789Sahrens /* 276789Sahrens * if the next separator is an @, there better not be 277789Sahrens * any more slashes. 278789Sahrens */ 279789Sahrens if (strchr(path, '/')) 280789Sahrens return (EINVAL); 281789Sahrens if (p-path >= MAXNAMELEN) 282789Sahrens return (ENAMETOOLONG); 283789Sahrens (void) strncpy(component, path, p - path); 284789Sahrens component[p-path] = '\0'; 285789Sahrens } else { 286789Sahrens ASSERT(!"invalid p"); 287789Sahrens } 288789Sahrens *nextp = p; 289789Sahrens return (0); 290789Sahrens } 291789Sahrens 292789Sahrens /* 293789Sahrens * same as dsl_open_dir, ignore the first component of name and use the 294789Sahrens * spa instead 295789Sahrens */ 2961544Seschrock int 2971544Seschrock dsl_dir_open_spa(spa_t *spa, const char *name, void *tag, 2981544Seschrock dsl_dir_t **ddp, const char **tailp) 299789Sahrens { 300789Sahrens char buf[MAXNAMELEN]; 301789Sahrens const char *next, *nextnext = NULL; 302789Sahrens int err; 303789Sahrens dsl_dir_t *dd; 304789Sahrens dsl_pool_t *dp; 305789Sahrens uint64_t ddobj; 306789Sahrens int openedspa = FALSE; 307789Sahrens 308789Sahrens dprintf("%s\n", name); 309789Sahrens 310789Sahrens err = getcomponent(name, buf, &next); 311789Sahrens if (err) 3121544Seschrock return (err); 313789Sahrens if (spa == NULL) { 314789Sahrens err = spa_open(buf, &spa, FTAG); 315789Sahrens if (err) { 316789Sahrens dprintf("spa_open(%s) failed\n", buf); 3171544Seschrock return (err); 318789Sahrens } 319789Sahrens openedspa = TRUE; 320789Sahrens 321789Sahrens /* XXX this assertion belongs in spa_open */ 322789Sahrens ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa))); 323789Sahrens } 324789Sahrens 325789Sahrens dp = spa_get_dsl(spa); 326789Sahrens 327789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 3281544Seschrock err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); 3291544Seschrock if (err) { 3301544Seschrock rw_exit(&dp->dp_config_rwlock); 3311544Seschrock if (openedspa) 3321544Seschrock spa_close(spa, FTAG); 3331544Seschrock return (err); 3341544Seschrock } 3351544Seschrock 336789Sahrens while (next != NULL) { 337789Sahrens dsl_dir_t *child_ds; 338789Sahrens err = getcomponent(next, buf, &nextnext); 3391544Seschrock if (err) 3401544Seschrock break; 341789Sahrens ASSERT(next[0] != '\0'); 342789Sahrens if (next[0] == '@') 343789Sahrens break; 344789Sahrens dprintf("looking up %s in obj%lld\n", 345789Sahrens buf, dd->dd_phys->dd_child_dir_zapobj); 346789Sahrens 347789Sahrens err = zap_lookup(dp->dp_meta_objset, 348789Sahrens dd->dd_phys->dd_child_dir_zapobj, 349789Sahrens buf, sizeof (ddobj), 1, &ddobj); 3501544Seschrock if (err) { 3511544Seschrock if (err == ENOENT) 3521544Seschrock err = 0; 353789Sahrens break; 354789Sahrens } 355789Sahrens 3561544Seschrock err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds); 3571544Seschrock if (err) 3581544Seschrock break; 359789Sahrens dsl_dir_close(dd, tag); 360789Sahrens dd = child_ds; 361789Sahrens next = nextnext; 362789Sahrens } 363789Sahrens rw_exit(&dp->dp_config_rwlock); 364789Sahrens 3651544Seschrock if (err) { 3661544Seschrock dsl_dir_close(dd, tag); 3671544Seschrock if (openedspa) 3681544Seschrock spa_close(spa, FTAG); 3691544Seschrock return (err); 3701544Seschrock } 3711544Seschrock 372789Sahrens /* 373789Sahrens * It's an error if there's more than one component left, or 374789Sahrens * tailp==NULL and there's any component left. 375789Sahrens */ 376789Sahrens if (next != NULL && 377789Sahrens (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { 378789Sahrens /* bad path name */ 379789Sahrens dsl_dir_close(dd, tag); 380789Sahrens dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); 3811544Seschrock err = ENOENT; 382789Sahrens } 383789Sahrens if (tailp) 384789Sahrens *tailp = next; 385789Sahrens if (openedspa) 386789Sahrens spa_close(spa, FTAG); 3871544Seschrock *ddp = dd; 3881544Seschrock return (err); 389789Sahrens } 390789Sahrens 391789Sahrens /* 392789Sahrens * Return the dsl_dir_t, and possibly the last component which couldn't 393789Sahrens * be found in *tail. Return NULL if the path is bogus, or if 394789Sahrens * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@' 395789Sahrens * means that the last component is a snapshot. 396789Sahrens */ 3971544Seschrock int 3981544Seschrock dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp) 399789Sahrens { 4001544Seschrock return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp)); 401789Sahrens } 402789Sahrens 4032199Sahrens uint64_t 4047046Sahrens dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, 4057046Sahrens dmu_tx_t *tx) 406789Sahrens { 4077046Sahrens objset_t *mos = dp->dp_meta_objset; 408789Sahrens uint64_t ddobj; 409789Sahrens dsl_dir_phys_t *dsphys; 410789Sahrens dmu_buf_t *dbuf; 411789Sahrens 412928Stabriz ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, 413928Stabriz DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); 4147046Sahrens if (pds) { 4157046Sahrens VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj, 4167046Sahrens name, sizeof (uint64_t), 1, &ddobj, tx)); 4177046Sahrens } else { 4187046Sahrens /* it's the root dir */ 4197046Sahrens VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, 4207046Sahrens DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); 4217046Sahrens } 4221544Seschrock VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); 423789Sahrens dmu_buf_will_dirty(dbuf, tx); 424789Sahrens dsphys = dbuf->db_data; 425789Sahrens 426789Sahrens dsphys->dd_creation_time = gethrestime_sec(); 4277046Sahrens if (pds) 4287046Sahrens dsphys->dd_parent_obj = pds->dd_object; 429789Sahrens dsphys->dd_props_zapobj = zap_create(mos, 430789Sahrens DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); 431789Sahrens dsphys->dd_child_dir_zapobj = zap_create(mos, 432885Sahrens DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); 4337390SMatthew.Ahrens@Sun.COM if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) 4347390SMatthew.Ahrens@Sun.COM dsphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; 4351544Seschrock dmu_buf_rele(dbuf, FTAG); 436789Sahrens 4372199Sahrens return (ddobj); 4382199Sahrens } 4392199Sahrens 4402199Sahrens /* ARGSUSED */ 4412199Sahrens int 4422199Sahrens dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 4432199Sahrens { 4442199Sahrens dsl_dir_t *dd = arg1; 4452199Sahrens dsl_pool_t *dp = dd->dd_pool; 4462199Sahrens objset_t *mos = dp->dp_meta_objset; 4472199Sahrens int err; 4482199Sahrens uint64_t count; 4492199Sahrens 4502199Sahrens /* 4512199Sahrens * There should be exactly two holds, both from 4522199Sahrens * dsl_dataset_destroy: one on the dd directory, and one on its 4532199Sahrens * head ds. Otherwise, someone is trying to lookup something 4542199Sahrens * inside this dir while we want to destroy it. The 4552199Sahrens * config_rwlock ensures that nobody else opens it after we 4562199Sahrens * check. 4572199Sahrens */ 4582199Sahrens if (dmu_buf_refcount(dd->dd_dbuf) > 2) 4592199Sahrens return (EBUSY); 4602199Sahrens 4612199Sahrens err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count); 4622199Sahrens if (err) 4632199Sahrens return (err); 4642199Sahrens if (count != 0) 4652199Sahrens return (EEXIST); 466789Sahrens 467789Sahrens return (0); 468789Sahrens } 469789Sahrens 4702199Sahrens void 4714543Smarks dsl_dir_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 472789Sahrens { 4732199Sahrens dsl_dir_t *dd = arg1; 4742199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 4752199Sahrens uint64_t val, obj; 4767390SMatthew.Ahrens@Sun.COM dd_used_t t; 477789Sahrens 4782199Sahrens ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock)); 479789Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 480789Sahrens 4812199Sahrens /* Remove our reservation. */ 482789Sahrens val = 0; 4834543Smarks dsl_dir_set_reservation_sync(dd, &val, cr, tx); 4847390SMatthew.Ahrens@Sun.COM ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0); 485789Sahrens ASSERT3U(dd->dd_phys->dd_reserved, ==, 0); 4867390SMatthew.Ahrens@Sun.COM for (t = 0; t < DD_USED_NUM; t++) 4877390SMatthew.Ahrens@Sun.COM ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0); 488789Sahrens 4892199Sahrens VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx)); 4902199Sahrens VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx)); 4914543Smarks VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx)); 4922199Sahrens VERIFY(0 == zap_remove(mos, 4932199Sahrens dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx)); 494789Sahrens 4952199Sahrens obj = dd->dd_object; 4962199Sahrens dsl_dir_close(dd, tag); 4972199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 498789Sahrens } 499789Sahrens 5007046Sahrens boolean_t 5017046Sahrens dsl_dir_is_clone(dsl_dir_t *dd) 502789Sahrens { 5037046Sahrens return (dd->dd_phys->dd_origin_obj && 5047046Sahrens (dd->dd_pool->dp_origin_snap == NULL || 5057046Sahrens dd->dd_phys->dd_origin_obj != 5067046Sahrens dd->dd_pool->dp_origin_snap->ds_object)); 507789Sahrens } 508789Sahrens 509789Sahrens void 5102885Sahrens dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) 511789Sahrens { 512789Sahrens mutex_enter(&dd->dd_lock); 5137390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 5147390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes); 5155378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota); 5162885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, 5172885Sahrens dd->dd_phys->dd_reserved); 5182885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 5192885Sahrens dd->dd_phys->dd_compressed_bytes == 0 ? 100 : 5202885Sahrens (dd->dd_phys->dd_uncompressed_bytes * 100 / 5212885Sahrens dd->dd_phys->dd_compressed_bytes)); 5227390SMatthew.Ahrens@Sun.COM if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 5237390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, 5247390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]); 5257390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, 5267390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]); 5277390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, 5287390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]); 5297390SMatthew.Ahrens@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, 5307390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] + 5317390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]); 5327390SMatthew.Ahrens@Sun.COM } 533789Sahrens mutex_exit(&dd->dd_lock); 534789Sahrens 5355446Sahrens rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 5367046Sahrens if (dsl_dir_is_clone(dd)) { 537789Sahrens dsl_dataset_t *ds; 5382885Sahrens char buf[MAXNAMELEN]; 539789Sahrens 5406689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 5416689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &ds)); 5422885Sahrens dsl_dataset_name(ds, buf); 5436689Smaybee dsl_dataset_rele(ds, FTAG); 5442885Sahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); 545789Sahrens } 5465446Sahrens rw_exit(&dd->dd_pool->dp_config_rwlock); 547789Sahrens } 548789Sahrens 549789Sahrens void 550789Sahrens dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) 551789Sahrens { 552789Sahrens dsl_pool_t *dp = dd->dd_pool; 553789Sahrens 554789Sahrens ASSERT(dd->dd_phys); 555789Sahrens 556789Sahrens if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) { 557789Sahrens /* up the hold count until we can be written out */ 558789Sahrens dmu_buf_add_ref(dd->dd_dbuf, dd); 559789Sahrens } 560789Sahrens } 561789Sahrens 562789Sahrens static int64_t 563789Sahrens parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) 564789Sahrens { 565789Sahrens uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved); 566789Sahrens uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved); 567789Sahrens return (new_accounted - old_accounted); 568789Sahrens } 569789Sahrens 570789Sahrens void 571789Sahrens dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) 572789Sahrens { 573789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 574789Sahrens 575789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 576789Sahrens 577789Sahrens mutex_enter(&dd->dd_lock); 578789Sahrens ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0); 579789Sahrens dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, 580789Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); 581789Sahrens dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; 582789Sahrens mutex_exit(&dd->dd_lock); 583789Sahrens 584789Sahrens /* release the hold from dsl_dir_dirty */ 5851544Seschrock dmu_buf_rele(dd->dd_dbuf, dd); 586789Sahrens } 587789Sahrens 588789Sahrens static uint64_t 5895378Sck153898 dsl_dir_space_towrite(dsl_dir_t *dd) 590789Sahrens { 5915378Sck153898 uint64_t space = 0; 592789Sahrens int i; 593789Sahrens 594789Sahrens ASSERT(MUTEX_HELD(&dd->dd_lock)); 595789Sahrens 596789Sahrens for (i = 0; i < TXG_SIZE; i++) { 597789Sahrens space += dd->dd_space_towrite[i&TXG_MASK]; 598789Sahrens ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0); 599789Sahrens } 600789Sahrens return (space); 601789Sahrens } 602789Sahrens 603789Sahrens /* 604789Sahrens * How much space would dd have available if ancestor had delta applied 605789Sahrens * to it? If ondiskonly is set, we're only interested in what's 606789Sahrens * on-disk, not estimated pending changes. 607789Sahrens */ 6082885Sahrens uint64_t 609789Sahrens dsl_dir_space_available(dsl_dir_t *dd, 610789Sahrens dsl_dir_t *ancestor, int64_t delta, int ondiskonly) 611789Sahrens { 612789Sahrens uint64_t parentspace, myspace, quota, used; 613789Sahrens 614789Sahrens /* 615789Sahrens * If there are no restrictions otherwise, assume we have 616789Sahrens * unlimited space available. 617789Sahrens */ 618789Sahrens quota = UINT64_MAX; 619789Sahrens parentspace = UINT64_MAX; 620789Sahrens 621789Sahrens if (dd->dd_parent != NULL) { 622789Sahrens parentspace = dsl_dir_space_available(dd->dd_parent, 623789Sahrens ancestor, delta, ondiskonly); 624789Sahrens } 625789Sahrens 626789Sahrens mutex_enter(&dd->dd_lock); 627789Sahrens if (dd->dd_phys->dd_quota != 0) 628789Sahrens quota = dd->dd_phys->dd_quota; 6297390SMatthew.Ahrens@Sun.COM used = dd->dd_phys->dd_used_bytes; 6305378Sck153898 if (!ondiskonly) 6315378Sck153898 used += dsl_dir_space_towrite(dd); 632789Sahrens 633789Sahrens if (dd->dd_parent == NULL) { 6342082Seschrock uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); 635789Sahrens quota = MIN(quota, poolsize); 636789Sahrens } 637789Sahrens 638789Sahrens if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) { 639789Sahrens /* 640789Sahrens * We have some space reserved, in addition to what our 641789Sahrens * parent gave us. 642789Sahrens */ 643789Sahrens parentspace += dd->dd_phys->dd_reserved - used; 644789Sahrens } 645789Sahrens 6467390SMatthew.Ahrens@Sun.COM if (dd == ancestor) { 6477390SMatthew.Ahrens@Sun.COM ASSERT(delta <= 0); 6487390SMatthew.Ahrens@Sun.COM ASSERT(used >= -delta); 6497390SMatthew.Ahrens@Sun.COM used += delta; 6507390SMatthew.Ahrens@Sun.COM if (parentspace != UINT64_MAX) 6517390SMatthew.Ahrens@Sun.COM parentspace -= delta; 6527390SMatthew.Ahrens@Sun.COM } 6537390SMatthew.Ahrens@Sun.COM 654789Sahrens if (used > quota) { 655789Sahrens /* over quota */ 656789Sahrens myspace = 0; 6572082Seschrock 6582082Seschrock /* 6592082Seschrock * While it's OK to be a little over quota, if 6602082Seschrock * we think we are using more space than there 6612082Seschrock * is in the pool (which is already 1.6% more than 6622082Seschrock * dsl_pool_adjustedsize()), something is very 6632082Seschrock * wrong. 6642082Seschrock */ 6652082Seschrock ASSERT3U(used, <=, spa_get_space(dd->dd_pool->dp_spa)); 666789Sahrens } else { 667789Sahrens /* 6682082Seschrock * the lesser of the space provided by our parent and 6692082Seschrock * the space left in our quota 670789Sahrens */ 671789Sahrens myspace = MIN(parentspace, quota - used); 672789Sahrens } 673789Sahrens 674789Sahrens mutex_exit(&dd->dd_lock); 675789Sahrens 676789Sahrens return (myspace); 677789Sahrens } 678789Sahrens 679789Sahrens struct tempreserve { 680789Sahrens list_node_t tr_node; 6816245Smaybee dsl_pool_t *tr_dp; 682789Sahrens dsl_dir_t *tr_ds; 683789Sahrens uint64_t tr_size; 684789Sahrens }; 685789Sahrens 686789Sahrens static int 6875378Sck153898 dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, 6885378Sck153898 boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list, 6896300Sck153898 dmu_tx_t *tx, boolean_t first) 690789Sahrens { 691789Sahrens uint64_t txg = tx->tx_txg; 6925378Sck153898 uint64_t est_inflight, used_on_disk, quota, parent_rsrv; 6935378Sck153898 struct tempreserve *tr; 6945392Smaybee int enospc = EDQUOT; 695789Sahrens int txgidx = txg & TXG_MASK; 696789Sahrens int i; 6975831Sck153898 uint64_t ref_rsrv = 0; 698789Sahrens 699789Sahrens ASSERT3U(txg, !=, 0); 7005378Sck153898 ASSERT3S(asize, >, 0); 701789Sahrens 702789Sahrens mutex_enter(&dd->dd_lock); 7035378Sck153898 704789Sahrens /* 705789Sahrens * Check against the dsl_dir's quota. We don't add in the delta 706789Sahrens * when checking for over-quota because they get one free hit. 707789Sahrens */ 7085378Sck153898 est_inflight = dsl_dir_space_towrite(dd); 709789Sahrens for (i = 0; i < TXG_SIZE; i++) 7105378Sck153898 est_inflight += dd->dd_tempreserved[i]; 7117390SMatthew.Ahrens@Sun.COM used_on_disk = dd->dd_phys->dd_used_bytes; 712789Sahrens 7134709Smaybee /* 7146300Sck153898 * On the first iteration, fetch the dataset's used-on-disk and 7156300Sck153898 * refreservation values. Also, if checkrefquota is set, test if 7166300Sck153898 * allocating this space would exceed the dataset's refquota. 7174709Smaybee */ 7186300Sck153898 if (first && tx->tx_objset) { 7195392Smaybee int error; 7205831Sck153898 dsl_dataset_t *ds = tx->tx_objset->os->os_dsl_dataset; 7215392Smaybee 7225378Sck153898 error = dsl_dataset_check_quota(ds, checkrefquota, 7235831Sck153898 asize, est_inflight, &used_on_disk, &ref_rsrv); 7245378Sck153898 if (error) { 7255378Sck153898 mutex_exit(&dd->dd_lock); 7265378Sck153898 return (error); 7275378Sck153898 } 7285378Sck153898 } 7295378Sck153898 7305378Sck153898 /* 7315378Sck153898 * If this transaction will result in a net free of space, 7325378Sck153898 * we want to let it through. 7335378Sck153898 */ 7345378Sck153898 if (ignorequota || netfree || dd->dd_phys->dd_quota == 0) 7354709Smaybee quota = UINT64_MAX; 7364709Smaybee else 737789Sahrens quota = dd->dd_phys->dd_quota; 738789Sahrens 739789Sahrens /* 7404709Smaybee * Adjust the quota against the actual pool size at the root. 7414709Smaybee * To ensure that it's possible to remove files from a full 7424709Smaybee * pool without inducing transient overcommits, we throttle 743789Sahrens * netfree transactions against a quota that is slightly larger, 744789Sahrens * but still within the pool's allocation slop. In cases where 745789Sahrens * we're very close to full, this will allow a steady trickle of 746789Sahrens * removes to get through. 747789Sahrens */ 7484944Smaybee if (dd->dd_parent == NULL) { 749789Sahrens uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); 750789Sahrens if (poolsize < quota) { 751789Sahrens quota = poolsize; 7525392Smaybee enospc = ENOSPC; 753789Sahrens } 754789Sahrens } 755789Sahrens 756789Sahrens /* 757789Sahrens * If they are requesting more space, and our current estimate 7585378Sck153898 * is over quota, they get to try again unless the actual 7591544Seschrock * on-disk is over quota and there are no pending changes (which 7601544Seschrock * may free up space for us). 761789Sahrens */ 7625378Sck153898 if (used_on_disk + est_inflight > quota) { 7635378Sck153898 if (est_inflight > 0 || used_on_disk < quota) 7645392Smaybee enospc = ERESTART; 7655378Sck153898 dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " 766789Sahrens "quota=%lluK tr=%lluK err=%d\n", 7675378Sck153898 used_on_disk>>10, est_inflight>>10, 7685392Smaybee quota>>10, asize>>10, enospc); 769789Sahrens mutex_exit(&dd->dd_lock); 7705392Smaybee return (enospc); 771789Sahrens } 772789Sahrens 773789Sahrens /* We need to up our estimated delta before dropping dd_lock */ 774789Sahrens dd->dd_tempreserved[txgidx] += asize; 775789Sahrens 7765831Sck153898 parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, 7775831Sck153898 asize - ref_rsrv); 778789Sahrens mutex_exit(&dd->dd_lock); 779789Sahrens 7806245Smaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 781789Sahrens tr->tr_ds = dd; 782789Sahrens tr->tr_size = asize; 783789Sahrens list_insert_tail(tr_list, tr); 784789Sahrens 785789Sahrens /* see if it's OK with our parent */ 7864944Smaybee if (dd->dd_parent && parent_rsrv) { 7874944Smaybee boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0); 7884944Smaybee 789789Sahrens return (dsl_dir_tempreserve_impl(dd->dd_parent, 7906300Sck153898 parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE)); 791789Sahrens } else { 792789Sahrens return (0); 793789Sahrens } 794789Sahrens } 795789Sahrens 796789Sahrens /* 797789Sahrens * Reserve space in this dsl_dir, to be used in this tx's txg. 7985378Sck153898 * After the space has been dirtied (and dsl_dir_willuse_space() 7995378Sck153898 * has been called), the reservation should be canceled, using 8005378Sck153898 * dsl_dir_tempreserve_clear(). 801789Sahrens */ 802789Sahrens int 8035378Sck153898 dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, 8045378Sck153898 uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx) 805789Sahrens { 8066245Smaybee int err; 807789Sahrens list_t *tr_list; 808789Sahrens 8095378Sck153898 if (asize == 0) { 8105378Sck153898 *tr_cookiep = NULL; 8115378Sck153898 return (0); 8125378Sck153898 } 8135378Sck153898 814789Sahrens tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); 815789Sahrens list_create(tr_list, sizeof (struct tempreserve), 816789Sahrens offsetof(struct tempreserve, tr_node)); 8175378Sck153898 ASSERT3S(asize, >, 0); 8181544Seschrock ASSERT3S(fsize, >=, 0); 819789Sahrens 8206245Smaybee err = arc_tempreserve_space(lsize, tx->tx_txg); 8216245Smaybee if (err == 0) { 8226245Smaybee struct tempreserve *tr; 8236245Smaybee 8246245Smaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 8256245Smaybee tr->tr_size = lsize; 8266245Smaybee list_insert_tail(tr_list, tr); 8276245Smaybee 8286245Smaybee err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx); 8296245Smaybee } else { 8306245Smaybee if (err == EAGAIN) { 8316245Smaybee txg_delay(dd->dd_pool, tx->tx_txg, 1); 8326245Smaybee err = ERESTART; 8336245Smaybee } 8346245Smaybee dsl_pool_memory_pressure(dd->dd_pool); 8356245Smaybee } 836789Sahrens 837789Sahrens if (err == 0) { 838789Sahrens struct tempreserve *tr; 839789Sahrens 8406245Smaybee tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); 8416245Smaybee tr->tr_dp = dd->dd_pool; 8426245Smaybee tr->tr_size = asize; 8436245Smaybee list_insert_tail(tr_list, tr); 8446245Smaybee 8456245Smaybee err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize, 8466300Sck153898 FALSE, asize > usize, tr_list, tx, TRUE); 847789Sahrens } 848789Sahrens 849789Sahrens if (err) 850789Sahrens dsl_dir_tempreserve_clear(tr_list, tx); 851789Sahrens else 852789Sahrens *tr_cookiep = tr_list; 8536245Smaybee 854789Sahrens return (err); 855789Sahrens } 856789Sahrens 857789Sahrens /* 858789Sahrens * Clear a temporary reservation that we previously made with 859789Sahrens * dsl_dir_tempreserve_space(). 860789Sahrens */ 861789Sahrens void 862789Sahrens dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) 863789Sahrens { 864789Sahrens int txgidx = tx->tx_txg & TXG_MASK; 865789Sahrens list_t *tr_list = tr_cookie; 866789Sahrens struct tempreserve *tr; 867789Sahrens 868789Sahrens ASSERT3U(tx->tx_txg, !=, 0); 869789Sahrens 8705378Sck153898 if (tr_cookie == NULL) 8715378Sck153898 return; 8725378Sck153898 873789Sahrens while (tr = list_head(tr_list)) { 8746245Smaybee if (tr->tr_dp) { 8756245Smaybee dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx); 8766245Smaybee } else if (tr->tr_ds) { 877789Sahrens mutex_enter(&tr->tr_ds->dd_lock); 878789Sahrens ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, 879789Sahrens tr->tr_size); 880789Sahrens tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; 881789Sahrens mutex_exit(&tr->tr_ds->dd_lock); 8826245Smaybee } else { 8836245Smaybee arc_tempreserve_clear(tr->tr_size); 884789Sahrens } 885789Sahrens list_remove(tr_list, tr); 886789Sahrens kmem_free(tr, sizeof (struct tempreserve)); 887789Sahrens } 888789Sahrens 889789Sahrens kmem_free(tr_list, sizeof (list_t)); 890789Sahrens } 891789Sahrens 8926245Smaybee static void 8936245Smaybee dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 894789Sahrens { 895789Sahrens int64_t parent_space; 896789Sahrens uint64_t est_used; 897789Sahrens 898789Sahrens mutex_enter(&dd->dd_lock); 899789Sahrens if (space > 0) 900789Sahrens dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; 901789Sahrens 9027390SMatthew.Ahrens@Sun.COM est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes; 903789Sahrens parent_space = parent_delta(dd, est_used, space); 904789Sahrens mutex_exit(&dd->dd_lock); 905789Sahrens 906789Sahrens /* Make sure that we clean up dd_space_to* */ 907789Sahrens dsl_dir_dirty(dd, tx); 908789Sahrens 909789Sahrens /* XXX this is potentially expensive and unnecessary... */ 910789Sahrens if (parent_space && dd->dd_parent) 9116245Smaybee dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx); 9126245Smaybee } 9136245Smaybee 9146245Smaybee /* 9156245Smaybee * Call in open context when we think we're going to write/free space, 9166245Smaybee * eg. when dirtying data. Be conservative (ie. OK to write less than 9176245Smaybee * this or free more than this, but don't write more or free less). 9186245Smaybee */ 9196245Smaybee void 9206245Smaybee dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) 9216245Smaybee { 9226245Smaybee dsl_pool_willuse_space(dd->dd_pool, space, tx); 9236245Smaybee dsl_dir_willuse_space_impl(dd, space, tx); 924789Sahrens } 925789Sahrens 926789Sahrens /* call from syncing context when we actually write/free space for this dd */ 927789Sahrens void 9287390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, 929789Sahrens int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) 930789Sahrens { 931789Sahrens int64_t accounted_delta; 932*7595SMatthew.Ahrens@Sun.COM boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 933789Sahrens 934789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 9357390SMatthew.Ahrens@Sun.COM ASSERT(type < DD_USED_NUM); 936789Sahrens 937789Sahrens dsl_dir_dirty(dd, tx); 938789Sahrens 939*7595SMatthew.Ahrens@Sun.COM if (needlock) 940*7595SMatthew.Ahrens@Sun.COM mutex_enter(&dd->dd_lock); 9417390SMatthew.Ahrens@Sun.COM accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used); 9427390SMatthew.Ahrens@Sun.COM ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used); 943789Sahrens ASSERT(compressed >= 0 || 944789Sahrens dd->dd_phys->dd_compressed_bytes >= -compressed); 945789Sahrens ASSERT(uncompressed >= 0 || 946789Sahrens dd->dd_phys->dd_uncompressed_bytes >= -uncompressed); 9477390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes += used; 948789Sahrens dd->dd_phys->dd_uncompressed_bytes += uncompressed; 949789Sahrens dd->dd_phys->dd_compressed_bytes += compressed; 9507390SMatthew.Ahrens@Sun.COM 9517390SMatthew.Ahrens@Sun.COM if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 9527390SMatthew.Ahrens@Sun.COM ASSERT(used > 0 || 9537390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[type] >= -used); 9547390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[type] += used; 9557390SMatthew.Ahrens@Sun.COM #ifdef DEBUG 9567390SMatthew.Ahrens@Sun.COM dd_used_t t; 9577390SMatthew.Ahrens@Sun.COM uint64_t u = 0; 9587390SMatthew.Ahrens@Sun.COM for (t = 0; t < DD_USED_NUM; t++) 9597390SMatthew.Ahrens@Sun.COM u += dd->dd_phys->dd_used_breakdown[t]; 9607390SMatthew.Ahrens@Sun.COM ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes); 9617390SMatthew.Ahrens@Sun.COM #endif 9627390SMatthew.Ahrens@Sun.COM } 963*7595SMatthew.Ahrens@Sun.COM if (needlock) 964*7595SMatthew.Ahrens@Sun.COM mutex_exit(&dd->dd_lock); 965789Sahrens 966789Sahrens if (dd->dd_parent != NULL) { 9677390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 968789Sahrens accounted_delta, compressed, uncompressed, tx); 9697390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(dd->dd_parent, 9707390SMatthew.Ahrens@Sun.COM used - accounted_delta, 9717390SMatthew.Ahrens@Sun.COM DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); 972789Sahrens } 973789Sahrens } 974789Sahrens 9757390SMatthew.Ahrens@Sun.COM void 9767390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, 9777390SMatthew.Ahrens@Sun.COM dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) 9787390SMatthew.Ahrens@Sun.COM { 979*7595SMatthew.Ahrens@Sun.COM boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); 980*7595SMatthew.Ahrens@Sun.COM 9817390SMatthew.Ahrens@Sun.COM ASSERT(dmu_tx_is_syncing(tx)); 9827390SMatthew.Ahrens@Sun.COM ASSERT(oldtype < DD_USED_NUM); 9837390SMatthew.Ahrens@Sun.COM ASSERT(newtype < DD_USED_NUM); 9847390SMatthew.Ahrens@Sun.COM 9857390SMatthew.Ahrens@Sun.COM if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN)) 9867390SMatthew.Ahrens@Sun.COM return; 9877390SMatthew.Ahrens@Sun.COM 9887390SMatthew.Ahrens@Sun.COM dsl_dir_dirty(dd, tx); 989*7595SMatthew.Ahrens@Sun.COM if (needlock) 990*7595SMatthew.Ahrens@Sun.COM mutex_enter(&dd->dd_lock); 9917390SMatthew.Ahrens@Sun.COM ASSERT(delta > 0 ? 9927390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[oldtype] >= delta : 9937390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[newtype] >= -delta); 9947390SMatthew.Ahrens@Sun.COM ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta)); 9957390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[oldtype] -= delta; 9967390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[newtype] += delta; 997*7595SMatthew.Ahrens@Sun.COM if (needlock) 998*7595SMatthew.Ahrens@Sun.COM mutex_exit(&dd->dd_lock); 9997390SMatthew.Ahrens@Sun.COM } 10007390SMatthew.Ahrens@Sun.COM 1001789Sahrens static int 10022199Sahrens dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 1003789Sahrens { 10042199Sahrens dsl_dir_t *dd = arg1; 10052199Sahrens uint64_t *quotap = arg2; 1006789Sahrens uint64_t new_quota = *quotap; 1007789Sahrens int err = 0; 10082199Sahrens uint64_t towrite; 10092199Sahrens 10102199Sahrens if (new_quota == 0) 10112199Sahrens return (0); 10122199Sahrens 10132199Sahrens mutex_enter(&dd->dd_lock); 10142199Sahrens /* 10152199Sahrens * If we are doing the preliminary check in open context, and 10162199Sahrens * there are pending changes, then don't fail it, since the 10175378Sck153898 * pending changes could under-estimate the amount of space to be 10182199Sahrens * freed up. 10192199Sahrens */ 10205378Sck153898 towrite = dsl_dir_space_towrite(dd); 10212199Sahrens if ((dmu_tx_is_syncing(tx) || towrite == 0) && 10222199Sahrens (new_quota < dd->dd_phys->dd_reserved || 10237390SMatthew.Ahrens@Sun.COM new_quota < dd->dd_phys->dd_used_bytes + towrite)) { 10242199Sahrens err = ENOSPC; 10252199Sahrens } 10262199Sahrens mutex_exit(&dd->dd_lock); 10272199Sahrens return (err); 10282199Sahrens } 10292199Sahrens 10304543Smarks /* ARGSUSED */ 10312199Sahrens static void 10324543Smarks dsl_dir_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 10332199Sahrens { 10342199Sahrens dsl_dir_t *dd = arg1; 10352199Sahrens uint64_t *quotap = arg2; 10362199Sahrens uint64_t new_quota = *quotap; 1037789Sahrens 1038789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1039789Sahrens 1040789Sahrens mutex_enter(&dd->dd_lock); 10412199Sahrens dd->dd_phys->dd_quota = new_quota; 1042789Sahrens mutex_exit(&dd->dd_lock); 10434543Smarks 10444543Smarks spa_history_internal_log(LOG_DS_QUOTA, dd->dd_pool->dp_spa, 10454543Smarks tx, cr, "%lld dataset = %llu ", 10464543Smarks (longlong_t)new_quota, dd->dd_phys->dd_head_dataset_obj); 1047789Sahrens } 1048789Sahrens 1049789Sahrens int 1050789Sahrens dsl_dir_set_quota(const char *ddname, uint64_t quota) 1051789Sahrens { 1052789Sahrens dsl_dir_t *dd; 1053789Sahrens int err; 1054789Sahrens 10551544Seschrock err = dsl_dir_open(ddname, FTAG, &dd, NULL); 10561544Seschrock if (err) 10571544Seschrock return (err); 1058789Sahrens 10595481Sck153898 if (quota != dd->dd_phys->dd_quota) { 10605481Sck153898 /* 10615481Sck153898 * If someone removes a file, then tries to set the quota, we 10625481Sck153898 * want to make sure the file freeing takes effect. 10635481Sck153898 */ 10645481Sck153898 txg_wait_open(dd->dd_pool, 0); 10655481Sck153898 10665481Sck153898 err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check, 10675481Sck153898 dsl_dir_set_quota_sync, dd, "a, 0); 10685481Sck153898 } 1069789Sahrens dsl_dir_close(dd, FTAG); 1070789Sahrens return (err); 1071789Sahrens } 1072789Sahrens 10735378Sck153898 int 10742199Sahrens dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 1075789Sahrens { 10762199Sahrens dsl_dir_t *dd = arg1; 10772199Sahrens uint64_t *reservationp = arg2; 1078789Sahrens uint64_t new_reservation = *reservationp; 1079789Sahrens uint64_t used, avail; 1080789Sahrens int64_t delta; 1081789Sahrens 1082789Sahrens if (new_reservation > INT64_MAX) 1083789Sahrens return (EOVERFLOW); 1084789Sahrens 10852199Sahrens /* 10862199Sahrens * If we are doing the preliminary check in open context, the 10872199Sahrens * space estimates may be inaccurate. 10882199Sahrens */ 10892199Sahrens if (!dmu_tx_is_syncing(tx)) 10902199Sahrens return (0); 10912199Sahrens 1092789Sahrens mutex_enter(&dd->dd_lock); 10937390SMatthew.Ahrens@Sun.COM used = dd->dd_phys->dd_used_bytes; 1094789Sahrens delta = MAX(used, new_reservation) - 1095789Sahrens MAX(used, dd->dd_phys->dd_reserved); 1096789Sahrens mutex_exit(&dd->dd_lock); 1097789Sahrens 1098789Sahrens if (dd->dd_parent) { 1099789Sahrens avail = dsl_dir_space_available(dd->dd_parent, 1100789Sahrens NULL, 0, FALSE); 1101789Sahrens } else { 1102789Sahrens avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; 1103789Sahrens } 1104789Sahrens 1105789Sahrens if (delta > 0 && delta > avail) 1106789Sahrens return (ENOSPC); 1107789Sahrens if (delta > 0 && dd->dd_phys->dd_quota > 0 && 1108789Sahrens new_reservation > dd->dd_phys->dd_quota) 1109789Sahrens return (ENOSPC); 11102199Sahrens return (0); 11112199Sahrens } 11122199Sahrens 11134543Smarks /* ARGSUSED */ 11142199Sahrens static void 11154543Smarks dsl_dir_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 11162199Sahrens { 11172199Sahrens dsl_dir_t *dd = arg1; 11182199Sahrens uint64_t *reservationp = arg2; 11192199Sahrens uint64_t new_reservation = *reservationp; 11202199Sahrens uint64_t used; 11212199Sahrens int64_t delta; 11222199Sahrens 11235378Sck153898 dmu_buf_will_dirty(dd->dd_dbuf, tx); 11245378Sck153898 11252199Sahrens mutex_enter(&dd->dd_lock); 11267390SMatthew.Ahrens@Sun.COM used = dd->dd_phys->dd_used_bytes; 11272199Sahrens delta = MAX(used, new_reservation) - 11282199Sahrens MAX(used, dd->dd_phys->dd_reserved); 11295378Sck153898 dd->dd_phys->dd_reserved = new_reservation; 1130789Sahrens 1131789Sahrens if (dd->dd_parent != NULL) { 1132789Sahrens /* Roll up this additional usage into our ancestors */ 11337390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 11347390SMatthew.Ahrens@Sun.COM delta, 0, 0, tx); 1135789Sahrens } 1136*7595SMatthew.Ahrens@Sun.COM mutex_exit(&dd->dd_lock); 11374543Smarks 11384543Smarks spa_history_internal_log(LOG_DS_RESERVATION, dd->dd_pool->dp_spa, 11394543Smarks tx, cr, "%lld dataset = %llu", 11404543Smarks (longlong_t)new_reservation, dd->dd_phys->dd_head_dataset_obj); 1141789Sahrens } 1142789Sahrens 1143789Sahrens int 1144789Sahrens dsl_dir_set_reservation(const char *ddname, uint64_t reservation) 1145789Sahrens { 1146789Sahrens dsl_dir_t *dd; 1147789Sahrens int err; 1148789Sahrens 11491544Seschrock err = dsl_dir_open(ddname, FTAG, &dd, NULL); 11501544Seschrock if (err) 11511544Seschrock return (err); 11522199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check, 11532199Sahrens dsl_dir_set_reservation_sync, dd, &reservation, 0); 1154789Sahrens dsl_dir_close(dd, FTAG); 1155789Sahrens return (err); 1156789Sahrens } 1157789Sahrens 1158789Sahrens static dsl_dir_t * 1159789Sahrens closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) 1160789Sahrens { 1161789Sahrens for (; ds1; ds1 = ds1->dd_parent) { 1162789Sahrens dsl_dir_t *dd; 1163789Sahrens for (dd = ds2; dd; dd = dd->dd_parent) { 1164789Sahrens if (ds1 == dd) 1165789Sahrens return (dd); 1166789Sahrens } 1167789Sahrens } 1168789Sahrens return (NULL); 1169789Sahrens } 1170789Sahrens 1171789Sahrens /* 1172789Sahrens * If delta is applied to dd, how much of that delta would be applied to 1173789Sahrens * ancestor? Syncing context only. 1174789Sahrens */ 1175789Sahrens static int64_t 1176789Sahrens would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) 1177789Sahrens { 1178789Sahrens if (dd == ancestor) 1179789Sahrens return (delta); 1180789Sahrens 1181789Sahrens mutex_enter(&dd->dd_lock); 11827390SMatthew.Ahrens@Sun.COM delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta); 1183789Sahrens mutex_exit(&dd->dd_lock); 1184789Sahrens return (would_change(dd->dd_parent, delta, ancestor)); 1185789Sahrens } 1186789Sahrens 11872199Sahrens struct renamearg { 11882199Sahrens dsl_dir_t *newparent; 11892199Sahrens const char *mynewname; 11902199Sahrens }; 11912199Sahrens 11924543Smarks /*ARGSUSED*/ 11932199Sahrens static int 11942199Sahrens dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 1195789Sahrens { 11962199Sahrens dsl_dir_t *dd = arg1; 11972199Sahrens struct renamearg *ra = arg2; 1198789Sahrens dsl_pool_t *dp = dd->dd_pool; 1199789Sahrens objset_t *mos = dp->dp_meta_objset; 12002199Sahrens int err; 12012199Sahrens uint64_t val; 12022199Sahrens 12032199Sahrens /* There should be 2 references: the open and the dirty */ 12042199Sahrens if (dmu_buf_refcount(dd->dd_dbuf) > 2) 12052199Sahrens return (EBUSY); 1206789Sahrens 12072199Sahrens /* check for existing name */ 12082199Sahrens err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, 12092199Sahrens ra->mynewname, 8, 1, &val); 12102199Sahrens if (err == 0) 12112199Sahrens return (EEXIST); 12122199Sahrens if (err != ENOENT) 12131544Seschrock return (err); 1214789Sahrens 12152199Sahrens if (ra->newparent != dd->dd_parent) { 12162082Seschrock /* is there enough space? */ 12172082Seschrock uint64_t myspace = 12187390SMatthew.Ahrens@Sun.COM MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved); 1219789Sahrens 12202199Sahrens /* no rename into our descendant */ 12212199Sahrens if (closest_common_ancestor(dd, ra->newparent) == dd) 1222789Sahrens return (EINVAL); 12232199Sahrens 12242199Sahrens if (err = dsl_dir_transfer_possible(dd->dd_parent, 12252199Sahrens ra->newparent, myspace)) 12262199Sahrens return (err); 12272199Sahrens } 12282199Sahrens 12292199Sahrens return (0); 12302199Sahrens } 1231789Sahrens 12322199Sahrens static void 12334543Smarks dsl_dir_rename_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 12342199Sahrens { 12352199Sahrens dsl_dir_t *dd = arg1; 12362199Sahrens struct renamearg *ra = arg2; 12372199Sahrens dsl_pool_t *dp = dd->dd_pool; 12382199Sahrens objset_t *mos = dp->dp_meta_objset; 12392199Sahrens int err; 1240789Sahrens 12412199Sahrens ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2); 12422199Sahrens 12432199Sahrens if (ra->newparent != dd->dd_parent) { 12447390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, 12457390SMatthew.Ahrens@Sun.COM -dd->dd_phys->dd_used_bytes, 1246789Sahrens -dd->dd_phys->dd_compressed_bytes, 1247789Sahrens -dd->dd_phys->dd_uncompressed_bytes, tx); 12487390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD, 12497390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes, 1250789Sahrens dd->dd_phys->dd_compressed_bytes, 1251789Sahrens dd->dd_phys->dd_uncompressed_bytes, tx); 12527390SMatthew.Ahrens@Sun.COM 12537390SMatthew.Ahrens@Sun.COM if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) { 12547390SMatthew.Ahrens@Sun.COM uint64_t unused_rsrv = dd->dd_phys->dd_reserved - 12557390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_bytes; 12567390SMatthew.Ahrens@Sun.COM 12577390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, 12587390SMatthew.Ahrens@Sun.COM -unused_rsrv, 0, 0, tx); 12597390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV, 12607390SMatthew.Ahrens@Sun.COM unused_rsrv, 0, 0, tx); 12617390SMatthew.Ahrens@Sun.COM } 1262789Sahrens } 1263789Sahrens 1264789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 1265789Sahrens 1266789Sahrens /* remove from old parent zapobj */ 1267789Sahrens err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj, 1268789Sahrens dd->dd_myname, tx); 1269789Sahrens ASSERT3U(err, ==, 0); 1270789Sahrens 12712199Sahrens (void) strcpy(dd->dd_myname, ra->mynewname); 1272789Sahrens dsl_dir_close(dd->dd_parent, dd); 12732199Sahrens dd->dd_phys->dd_parent_obj = ra->newparent->dd_object; 12741544Seschrock VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, 12752199Sahrens ra->newparent->dd_object, NULL, dd, &dd->dd_parent)); 1276789Sahrens 1277789Sahrens /* add to new parent zapobj */ 12782199Sahrens err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, 1279789Sahrens dd->dd_myname, 8, 1, &dd->dd_object, tx); 1280789Sahrens ASSERT3U(err, ==, 0); 12814543Smarks 12824543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, 12834543Smarks tx, cr, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj); 12842199Sahrens } 1285789Sahrens 12862199Sahrens int 12872199Sahrens dsl_dir_rename(dsl_dir_t *dd, const char *newname) 12882199Sahrens { 12892199Sahrens struct renamearg ra; 12902199Sahrens int err; 12912199Sahrens 12922199Sahrens /* new parent should exist */ 12932199Sahrens err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname); 12942199Sahrens if (err) 12952199Sahrens return (err); 12962199Sahrens 12972199Sahrens /* can't rename to different pool */ 12982199Sahrens if (dd->dd_pool != ra.newparent->dd_pool) { 12992199Sahrens err = ENXIO; 13002199Sahrens goto out; 13012199Sahrens } 13022199Sahrens 13032199Sahrens /* new name should not already exist */ 13042199Sahrens if (ra.mynewname == NULL) { 13052199Sahrens err = EEXIST; 13062199Sahrens goto out; 13072199Sahrens } 13082199Sahrens 13092199Sahrens err = dsl_sync_task_do(dd->dd_pool, 13102199Sahrens dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3); 13112199Sahrens 13122199Sahrens out: 13132199Sahrens dsl_dir_close(ra.newparent, FTAG); 13142199Sahrens return (err); 1315789Sahrens } 13162082Seschrock 13172082Seschrock int 13182082Seschrock dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space) 13192082Seschrock { 13202082Seschrock dsl_dir_t *ancestor; 13212082Seschrock int64_t adelta; 13222082Seschrock uint64_t avail; 13232082Seschrock 13242082Seschrock ancestor = closest_common_ancestor(sdd, tdd); 13252082Seschrock adelta = would_change(sdd, -space, ancestor); 13262082Seschrock avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); 13272082Seschrock if (avail < space) 13282082Seschrock return (ENOSPC); 13292082Seschrock 13302082Seschrock return (0); 13312082Seschrock } 1332