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_objset.h> 27789Sahrens #include <sys/dsl_dataset.h> 28789Sahrens #include <sys/dsl_dir.h> 292082Seschrock #include <sys/dsl_prop.h> 302199Sahrens #include <sys/dsl_synctask.h> 31789Sahrens #include <sys/dmu_traverse.h> 32789Sahrens #include <sys/dmu_tx.h> 33789Sahrens #include <sys/arc.h> 34789Sahrens #include <sys/zio.h> 35789Sahrens #include <sys/zap.h> 36789Sahrens #include <sys/unique.h> 37789Sahrens #include <sys/zfs_context.h> 384007Smmusante #include <sys/zfs_ioctl.h> 394543Smarks #include <sys/spa.h> 407046Sahrens #include <sys/zfs_znode.h> 414543Smarks #include <sys/sunddi.h> 42789Sahrens 436689Smaybee static char *dsl_reaper = "the grim reaper"; 446689Smaybee 452199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check; 462199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync; 472199Sahrens static dsl_checkfunc_t dsl_dataset_rollback_check; 482199Sahrens static dsl_syncfunc_t dsl_dataset_rollback_sync; 495378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync; 501731Sbonwick 513444Sek110237 #define DS_REF_MAX (1ULL << 62) 52789Sahrens 53789Sahrens #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 54789Sahrens 556689Smaybee #define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper) 566689Smaybee 57789Sahrens 585378Sck153898 /* 595378Sck153898 * Figure out how much of this delta should be propogated to the dsl_dir 605378Sck153898 * layer. If there's a refreservation, that space has already been 615378Sck153898 * partially accounted for in our ancestors. 625378Sck153898 */ 635378Sck153898 static int64_t 645378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta) 655378Sck153898 { 665378Sck153898 uint64_t old_bytes, new_bytes; 675378Sck153898 685378Sck153898 if (ds->ds_reserved == 0) 695378Sck153898 return (delta); 705378Sck153898 715378Sck153898 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 725378Sck153898 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 735378Sck153898 745378Sck153898 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 755378Sck153898 return (new_bytes - old_bytes); 765378Sck153898 } 77789Sahrens 78789Sahrens void 79789Sahrens dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 80789Sahrens { 812082Seschrock int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 82789Sahrens int compressed = BP_GET_PSIZE(bp); 83789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 845378Sck153898 int64_t delta; 85789Sahrens 86789Sahrens dprintf_bp(bp, "born, ds=%p\n", ds); 87789Sahrens 88789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 89789Sahrens /* It could have been compressed away to nothing */ 90789Sahrens if (BP_IS_HOLE(bp)) 91789Sahrens return; 92789Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 93789Sahrens ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 94789Sahrens if (ds == NULL) { 95789Sahrens /* 96789Sahrens * Account for the meta-objset space in its placeholder 97789Sahrens * dsl_dir. 98789Sahrens */ 99789Sahrens ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 100789Sahrens dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 101789Sahrens used, compressed, uncompressed, tx); 102789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 103789Sahrens return; 104789Sahrens } 105789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 106789Sahrens mutex_enter(&ds->ds_lock); 1075378Sck153898 delta = parent_delta(ds, used); 108789Sahrens ds->ds_phys->ds_used_bytes += used; 109789Sahrens ds->ds_phys->ds_compressed_bytes += compressed; 110789Sahrens ds->ds_phys->ds_uncompressed_bytes += uncompressed; 111789Sahrens ds->ds_phys->ds_unique_bytes += used; 112789Sahrens mutex_exit(&ds->ds_lock); 1135378Sck153898 dsl_dir_diduse_space(ds->ds_dir, delta, compressed, uncompressed, tx); 114789Sahrens } 115789Sahrens 1166992Smaybee int 1173547Smaybee dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio, 1183547Smaybee dmu_tx_t *tx) 119789Sahrens { 1202082Seschrock int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 121789Sahrens int compressed = BP_GET_PSIZE(bp); 122789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 123789Sahrens 124789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1253547Smaybee /* No block pointer => nothing to free */ 126789Sahrens if (BP_IS_HOLE(bp)) 1276992Smaybee return (0); 128789Sahrens 129789Sahrens ASSERT(used > 0); 130789Sahrens if (ds == NULL) { 1313547Smaybee int err; 132789Sahrens /* 133789Sahrens * Account for the meta-objset space in its placeholder 134789Sahrens * dataset. 135789Sahrens */ 1367046Sahrens err = dsl_free(pio, tx->tx_pool, 1373547Smaybee tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 1383547Smaybee ASSERT(err == 0); 139789Sahrens 140789Sahrens dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 141789Sahrens -used, -compressed, -uncompressed, tx); 142789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 1436992Smaybee return (used); 144789Sahrens } 145789Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 146789Sahrens 147789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 148789Sahrens 149789Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 1503547Smaybee int err; 1515378Sck153898 int64_t delta; 1523547Smaybee 153789Sahrens dprintf_bp(bp, "freeing: %s", ""); 1547046Sahrens err = dsl_free(pio, tx->tx_pool, 1553547Smaybee tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 1563547Smaybee ASSERT(err == 0); 157789Sahrens 158789Sahrens mutex_enter(&ds->ds_lock); 1595378Sck153898 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 1605378Sck153898 !DS_UNIQUE_IS_ACCURATE(ds)); 1615378Sck153898 delta = parent_delta(ds, -used); 162789Sahrens ds->ds_phys->ds_unique_bytes -= used; 163789Sahrens mutex_exit(&ds->ds_lock); 164789Sahrens dsl_dir_diduse_space(ds->ds_dir, 1655378Sck153898 delta, -compressed, -uncompressed, tx); 166789Sahrens } else { 167789Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 1681544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 1695712Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 1705712Sahrens ds->ds_phys->ds_prev_snap_obj); 1715712Sahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 172789Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 1735712Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 1745712Sahrens ds->ds_object && bp->blk_birth > 1755712Sahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 1765712Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1775712Sahrens mutex_enter(&ds->ds_prev->ds_lock); 1785712Sahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 1795712Sahrens mutex_exit(&ds->ds_prev->ds_lock); 180789Sahrens } 181789Sahrens } 182789Sahrens mutex_enter(&ds->ds_lock); 183789Sahrens ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 184789Sahrens ds->ds_phys->ds_used_bytes -= used; 185789Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 186789Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 187789Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 188789Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 189789Sahrens mutex_exit(&ds->ds_lock); 1906992Smaybee 1916992Smaybee return (used); 192789Sahrens } 193789Sahrens 1941544Seschrock uint64_t 1951544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 196789Sahrens { 1972885Sahrens uint64_t trysnap = 0; 1982885Sahrens 199789Sahrens if (ds == NULL) 2001544Seschrock return (0); 201789Sahrens /* 202789Sahrens * The snapshot creation could fail, but that would cause an 203789Sahrens * incorrect FALSE return, which would only result in an 204789Sahrens * overestimation of the amount of space that an operation would 205789Sahrens * consume, which is OK. 206789Sahrens * 207789Sahrens * There's also a small window where we could miss a pending 208789Sahrens * snapshot, because we could set the sync task in the quiescing 209789Sahrens * phase. So this should only be used as a guess. 210789Sahrens */ 2112885Sahrens if (ds->ds_trysnap_txg > 2122885Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 2132885Sahrens trysnap = ds->ds_trysnap_txg; 2142885Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 2151544Seschrock } 2161544Seschrock 2171544Seschrock int 2181544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 2191544Seschrock { 2201544Seschrock return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 221789Sahrens } 222789Sahrens 223789Sahrens /* ARGSUSED */ 224789Sahrens static void 225789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 226789Sahrens { 227789Sahrens dsl_dataset_t *ds = dsv; 228789Sahrens 2296689Smaybee ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 230789Sahrens 231789Sahrens dprintf_ds(ds, "evicting %s\n", ""); 232789Sahrens 2334787Sahrens unique_remove(ds->ds_fsid_guid); 234789Sahrens 235789Sahrens if (ds->ds_user_ptr != NULL) 236789Sahrens ds->ds_user_evict_func(ds, ds->ds_user_ptr); 237789Sahrens 238789Sahrens if (ds->ds_prev) { 2396689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 240789Sahrens ds->ds_prev = NULL; 241789Sahrens } 242789Sahrens 243789Sahrens bplist_close(&ds->ds_deadlist); 2446689Smaybee if (ds->ds_dir) 2456689Smaybee dsl_dir_close(ds->ds_dir, ds); 246789Sahrens 2474787Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 248789Sahrens 2492856Snd150628 mutex_destroy(&ds->ds_lock); 2504787Sahrens mutex_destroy(&ds->ds_opening_lock); 2512856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 2526689Smaybee rw_destroy(&ds->ds_rwlock); 2536689Smaybee cv_destroy(&ds->ds_exclusive_cv); 2542856Snd150628 255789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 256789Sahrens } 257789Sahrens 2581544Seschrock static int 259789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 260789Sahrens { 261789Sahrens dsl_dataset_phys_t *headphys; 262789Sahrens int err; 263789Sahrens dmu_buf_t *headdbuf; 264789Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 265789Sahrens objset_t *mos = dp->dp_meta_objset; 266789Sahrens 267789Sahrens if (ds->ds_snapname[0]) 2681544Seschrock return (0); 269789Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 2701544Seschrock return (0); 271789Sahrens 2721544Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 2731544Seschrock FTAG, &headdbuf); 2741544Seschrock if (err) 2751544Seschrock return (err); 276789Sahrens headphys = headdbuf->db_data; 277789Sahrens err = zap_value_search(dp->dp_meta_objset, 2784577Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 2791544Seschrock dmu_buf_rele(headdbuf, FTAG); 2801544Seschrock return (err); 281789Sahrens } 282789Sahrens 2836492Stimh static int 2846689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 2856492Stimh { 2866689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 2876689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 2886492Stimh matchtype_t mt; 2896492Stimh int err; 2906492Stimh 2916689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 2926492Stimh mt = MT_FIRST; 2936492Stimh else 2946492Stimh mt = MT_EXACT; 2956492Stimh 2966689Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 2976492Stimh value, mt, NULL, 0, NULL); 2986492Stimh if (err == ENOTSUP && mt == MT_FIRST) 2996689Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 3006492Stimh return (err); 3016492Stimh } 3026492Stimh 3036492Stimh static int 3046689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 3056492Stimh { 3066689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3076689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3086492Stimh matchtype_t mt; 3096492Stimh int err; 3106492Stimh 3116689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3126492Stimh mt = MT_FIRST; 3136492Stimh else 3146492Stimh mt = MT_EXACT; 3156492Stimh 3166689Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 3176492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3186689Smaybee err = zap_remove(mos, snapobj, name, tx); 3196492Stimh return (err); 3206492Stimh } 3216492Stimh 3226689Smaybee static int 3236689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag, 3246689Smaybee dsl_dataset_t **dsp) 325789Sahrens { 326789Sahrens objset_t *mos = dp->dp_meta_objset; 327789Sahrens dmu_buf_t *dbuf; 328789Sahrens dsl_dataset_t *ds; 3291544Seschrock int err; 330789Sahrens 331789Sahrens ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 332789Sahrens dsl_pool_sync_context(dp)); 333789Sahrens 3341544Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3351544Seschrock if (err) 3361544Seschrock return (err); 337789Sahrens ds = dmu_buf_get_user(dbuf); 338789Sahrens if (ds == NULL) { 339789Sahrens dsl_dataset_t *winner; 340789Sahrens 341789Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 342789Sahrens ds->ds_dbuf = dbuf; 343789Sahrens ds->ds_object = dsobj; 344789Sahrens ds->ds_phys = dbuf->db_data; 345789Sahrens 3462856Snd150628 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 3474787Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3482856Snd150628 mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT, 3492856Snd150628 NULL); 3506689Smaybee rw_init(&ds->ds_rwlock, 0, 0, 0); 3516689Smaybee cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 3522856Snd150628 3531544Seschrock err = bplist_open(&ds->ds_deadlist, 354789Sahrens mos, ds->ds_phys->ds_deadlist_obj); 3551544Seschrock if (err == 0) { 3561544Seschrock err = dsl_dir_open_obj(dp, 3571544Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 3581544Seschrock } 3591544Seschrock if (err) { 3601544Seschrock /* 3611544Seschrock * we don't really need to close the blist if we 3621544Seschrock * just opened it. 3631544Seschrock */ 3642856Snd150628 mutex_destroy(&ds->ds_lock); 3654787Sahrens mutex_destroy(&ds->ds_opening_lock); 3662856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 3676689Smaybee rw_destroy(&ds->ds_rwlock); 3686689Smaybee cv_destroy(&ds->ds_exclusive_cv); 3691544Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 3701544Seschrock dmu_buf_rele(dbuf, tag); 3711544Seschrock return (err); 3721544Seschrock } 373789Sahrens 374789Sahrens if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) { 375789Sahrens ds->ds_snapname[0] = '\0'; 376789Sahrens if (ds->ds_phys->ds_prev_snap_obj) { 3776689Smaybee err = dsl_dataset_get_ref(dp, 3786689Smaybee ds->ds_phys->ds_prev_snap_obj, 3796689Smaybee ds, &ds->ds_prev); 380789Sahrens } 3816689Smaybee } else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) { 3826689Smaybee err = dsl_dataset_get_snapname(ds); 383789Sahrens } 384789Sahrens 3855475Sck153898 if (!dsl_dataset_is_snapshot(ds)) { 3865569Sck153898 /* 3875569Sck153898 * In sync context, we're called with either no lock 3885569Sck153898 * or with the write lock. If we're not syncing, 3895569Sck153898 * we're always called with the read lock held. 3905569Sck153898 */ 3915475Sck153898 boolean_t need_lock = 3925569Sck153898 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 3935569Sck153898 dsl_pool_sync_context(dp); 3945475Sck153898 3955475Sck153898 if (need_lock) 3965475Sck153898 rw_enter(&dp->dp_config_rwlock, RW_READER); 3975475Sck153898 3987265Sahrens err = dsl_prop_get_ds(ds, 3995475Sck153898 "refreservation", sizeof (uint64_t), 1, 4005475Sck153898 &ds->ds_reserved, NULL); 4015475Sck153898 if (err == 0) { 4027265Sahrens err = dsl_prop_get_ds(ds, 4035475Sck153898 "refquota", sizeof (uint64_t), 1, 4045475Sck153898 &ds->ds_quota, NULL); 4055475Sck153898 } 4065475Sck153898 4075475Sck153898 if (need_lock) 4085475Sck153898 rw_exit(&dp->dp_config_rwlock); 4095475Sck153898 } else { 4105475Sck153898 ds->ds_reserved = ds->ds_quota = 0; 4115475Sck153898 } 4125475Sck153898 4131544Seschrock if (err == 0) { 4141544Seschrock winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 4151544Seschrock dsl_dataset_evict); 4161544Seschrock } 4171544Seschrock if (err || winner) { 418789Sahrens bplist_close(&ds->ds_deadlist); 4196689Smaybee if (ds->ds_prev) 4206689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 421789Sahrens dsl_dir_close(ds->ds_dir, ds); 4222856Snd150628 mutex_destroy(&ds->ds_lock); 4234787Sahrens mutex_destroy(&ds->ds_opening_lock); 4242856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 4256689Smaybee rw_destroy(&ds->ds_rwlock); 4266689Smaybee cv_destroy(&ds->ds_exclusive_cv); 427789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4281544Seschrock if (err) { 4291544Seschrock dmu_buf_rele(dbuf, tag); 4301544Seschrock return (err); 4311544Seschrock } 432789Sahrens ds = winner; 433789Sahrens } else { 4344787Sahrens ds->ds_fsid_guid = 435789Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 436789Sahrens } 437789Sahrens } 438789Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 439789Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 4407046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 4417061Sahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 4427077Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 443789Sahrens mutex_enter(&ds->ds_lock); 4446689Smaybee if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 445789Sahrens mutex_exit(&ds->ds_lock); 4466689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 4476689Smaybee return (ENOENT); 4486689Smaybee } 4496689Smaybee mutex_exit(&ds->ds_lock); 4506689Smaybee *dsp = ds; 4516689Smaybee return (0); 4526689Smaybee } 4536689Smaybee 4546689Smaybee static int 4556689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 4566689Smaybee { 4576689Smaybee dsl_pool_t *dp = ds->ds_dir->dd_pool; 4586689Smaybee 4596689Smaybee /* 4606689Smaybee * In syncing context we don't want the rwlock lock: there 4616689Smaybee * may be an existing writer waiting for sync phase to 4626689Smaybee * finish. We don't need to worry about such writers, since 4636689Smaybee * sync phase is single-threaded, so the writer can't be 4646689Smaybee * doing anything while we are active. 4656689Smaybee */ 4666689Smaybee if (dsl_pool_sync_context(dp)) { 4676689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 4686689Smaybee return (0); 469789Sahrens } 4706689Smaybee 4716689Smaybee /* 4726689Smaybee * Normal users will hold the ds_rwlock as a READER until they 4736689Smaybee * are finished (i.e., call dsl_dataset_rele()). "Owners" will 4746689Smaybee * drop their READER lock after they set the ds_owner field. 4756689Smaybee * 4766689Smaybee * If the dataset is being destroyed, the destroy thread will 4776689Smaybee * obtain a WRITER lock for exclusive access after it's done its 4786689Smaybee * open-context work and then change the ds_owner to 4796689Smaybee * dsl_reaper once destruction is assured. So threads 4806689Smaybee * may block here temporarily, until the "destructability" of 4816689Smaybee * the dataset is determined. 4826689Smaybee */ 4836689Smaybee ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 4846689Smaybee mutex_enter(&ds->ds_lock); 4856689Smaybee while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 4866689Smaybee rw_exit(&dp->dp_config_rwlock); 4876689Smaybee cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 4886689Smaybee if (DSL_DATASET_IS_DESTROYED(ds)) { 4896689Smaybee mutex_exit(&ds->ds_lock); 4906689Smaybee dsl_dataset_drop_ref(ds, tag); 4916689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 4926689Smaybee return (ENOENT); 4936689Smaybee } 4946689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 4956689Smaybee } 496789Sahrens mutex_exit(&ds->ds_lock); 4971544Seschrock return (0); 498789Sahrens } 499789Sahrens 500789Sahrens int 5016689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 5026689Smaybee dsl_dataset_t **dsp) 5036689Smaybee { 5046689Smaybee int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 5056689Smaybee 5066689Smaybee if (err) 5076689Smaybee return (err); 5086689Smaybee return (dsl_dataset_hold_ref(*dsp, tag)); 5096689Smaybee } 5106689Smaybee 5116689Smaybee int 5126689Smaybee dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, int flags, void *owner, 5136689Smaybee dsl_dataset_t **dsp) 5146689Smaybee { 5156689Smaybee int err = dsl_dataset_hold_obj(dp, dsobj, owner, dsp); 5166689Smaybee 5176689Smaybee ASSERT(DS_MODE_TYPE(flags) != DS_MODE_USER); 5186689Smaybee 5196689Smaybee if (err) 5206689Smaybee return (err); 5216689Smaybee if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 5226689Smaybee dsl_dataset_rele(*dsp, owner); 5236689Smaybee return (EBUSY); 5246689Smaybee } 5256689Smaybee return (0); 5266689Smaybee } 5276689Smaybee 5286689Smaybee int 5296689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 530789Sahrens { 531789Sahrens dsl_dir_t *dd; 532789Sahrens dsl_pool_t *dp; 5336689Smaybee const char *snapname; 534789Sahrens uint64_t obj; 535789Sahrens int err = 0; 536789Sahrens 5376689Smaybee err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 5381544Seschrock if (err) 5391544Seschrock return (err); 540789Sahrens 541789Sahrens dp = dd->dd_pool; 542789Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 543789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 5446689Smaybee if (obj) 5456689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, dsp); 5466689Smaybee else 547789Sahrens err = ENOENT; 5486689Smaybee if (err) 549789Sahrens goto out; 5506689Smaybee 5516689Smaybee err = dsl_dataset_hold_ref(*dsp, tag); 5526689Smaybee 5536689Smaybee /* we may be looking for a snapshot */ 5546689Smaybee if (err == 0 && snapname != NULL) { 5556689Smaybee dsl_dataset_t *ds = NULL; 5566689Smaybee 5576689Smaybee if (*snapname++ != '@') { 5586689Smaybee dsl_dataset_rele(*dsp, tag); 559789Sahrens err = ENOENT; 560789Sahrens goto out; 561789Sahrens } 5626689Smaybee 5636689Smaybee dprintf("looking for snapshot '%s'\n", snapname); 5646689Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 5656689Smaybee if (err == 0) 5666689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, &ds); 5676689Smaybee dsl_dataset_rele(*dsp, tag); 5686689Smaybee 5696689Smaybee ASSERT3U((err == 0), ==, (ds != NULL)); 5706689Smaybee 5716689Smaybee if (ds) { 5726689Smaybee mutex_enter(&ds->ds_lock); 5736689Smaybee if (ds->ds_snapname[0] == 0) 5746689Smaybee (void) strlcpy(ds->ds_snapname, snapname, 5756689Smaybee sizeof (ds->ds_snapname)); 5766689Smaybee mutex_exit(&ds->ds_lock); 5776689Smaybee err = dsl_dataset_hold_ref(ds, tag); 5786689Smaybee *dsp = err ? NULL : ds; 579789Sahrens } 580789Sahrens } 581789Sahrens out: 582789Sahrens rw_exit(&dp->dp_config_rwlock); 583789Sahrens dsl_dir_close(dd, FTAG); 584789Sahrens return (err); 585789Sahrens } 586789Sahrens 587789Sahrens int 5886689Smaybee dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp) 589789Sahrens { 5906689Smaybee int err = dsl_dataset_hold(name, owner, dsp); 5916689Smaybee if (err) 5926689Smaybee return (err); 5936689Smaybee if ((*dsp)->ds_phys->ds_num_children > 0 && 5946689Smaybee !DS_MODE_IS_READONLY(flags)) { 5956689Smaybee dsl_dataset_rele(*dsp, owner); 5966689Smaybee return (EROFS); 5976689Smaybee } 5986689Smaybee if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 5996689Smaybee dsl_dataset_rele(*dsp, owner); 6006689Smaybee return (EBUSY); 6016689Smaybee } 6026689Smaybee return (0); 603789Sahrens } 604789Sahrens 605789Sahrens void 606789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 607789Sahrens { 608789Sahrens if (ds == NULL) { 609789Sahrens (void) strcpy(name, "mos"); 610789Sahrens } else { 611789Sahrens dsl_dir_name(ds->ds_dir, name); 6121544Seschrock VERIFY(0 == dsl_dataset_get_snapname(ds)); 613789Sahrens if (ds->ds_snapname[0]) { 614789Sahrens (void) strcat(name, "@"); 6156689Smaybee /* 6166689Smaybee * We use a "recursive" mutex so that we 6176689Smaybee * can call dprintf_ds() with ds_lock held. 6186689Smaybee */ 619789Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 620789Sahrens mutex_enter(&ds->ds_lock); 621789Sahrens (void) strcat(name, ds->ds_snapname); 622789Sahrens mutex_exit(&ds->ds_lock); 623789Sahrens } else { 624789Sahrens (void) strcat(name, ds->ds_snapname); 625789Sahrens } 626789Sahrens } 627789Sahrens } 628789Sahrens } 629789Sahrens 6303978Smmusante static int 6313978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds) 6323978Smmusante { 6333978Smmusante int result; 6343978Smmusante 6353978Smmusante if (ds == NULL) { 6363978Smmusante result = 3; /* "mos" */ 6373978Smmusante } else { 6383978Smmusante result = dsl_dir_namelen(ds->ds_dir); 6393978Smmusante VERIFY(0 == dsl_dataset_get_snapname(ds)); 6403978Smmusante if (ds->ds_snapname[0]) { 6413978Smmusante ++result; /* adding one for the @-sign */ 6423978Smmusante if (!MUTEX_HELD(&ds->ds_lock)) { 6433978Smmusante mutex_enter(&ds->ds_lock); 6443978Smmusante result += strlen(ds->ds_snapname); 6453978Smmusante mutex_exit(&ds->ds_lock); 6463978Smmusante } else { 6473978Smmusante result += strlen(ds->ds_snapname); 6483978Smmusante } 6493978Smmusante } 6503978Smmusante } 6513978Smmusante 6523978Smmusante return (result); 6533978Smmusante } 6543978Smmusante 6557046Sahrens void 6566689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 657789Sahrens { 6581544Seschrock dmu_buf_rele(ds->ds_dbuf, tag); 659789Sahrens } 660789Sahrens 661789Sahrens void 6626689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6635367Sahrens { 6646689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 6656689Smaybee rw_exit(&ds->ds_rwlock); 6666689Smaybee } 6676689Smaybee dsl_dataset_drop_ref(ds, tag); 6686689Smaybee } 6696689Smaybee 6706689Smaybee void 6716689Smaybee dsl_dataset_disown(dsl_dataset_t *ds, void *owner) 6726689Smaybee { 6736689Smaybee ASSERT((ds->ds_owner == owner && ds->ds_dbuf) || 6746689Smaybee (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 6756689Smaybee 6765367Sahrens mutex_enter(&ds->ds_lock); 6776689Smaybee ds->ds_owner = NULL; 6786689Smaybee if (RW_WRITE_HELD(&ds->ds_rwlock)) { 6796689Smaybee rw_exit(&ds->ds_rwlock); 6806689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 6816689Smaybee } 6825367Sahrens mutex_exit(&ds->ds_lock); 6836689Smaybee if (ds->ds_dbuf) 6846689Smaybee dsl_dataset_drop_ref(ds, owner); 6856689Smaybee else 6866689Smaybee dsl_dataset_evict(ds->ds_dbuf, ds); 6875367Sahrens } 6885367Sahrens 6895367Sahrens boolean_t 6906689Smaybee dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner) 6915367Sahrens { 6926689Smaybee boolean_t gotit = FALSE; 6936689Smaybee 6945367Sahrens mutex_enter(&ds->ds_lock); 6956689Smaybee if (ds->ds_owner == NULL && 6966689Smaybee (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 6976689Smaybee ds->ds_owner = owner; 6986689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 6996689Smaybee rw_exit(&ds->ds_rwlock); 7006689Smaybee gotit = TRUE; 7015367Sahrens } 7025367Sahrens mutex_exit(&ds->ds_lock); 7036689Smaybee return (gotit); 7046689Smaybee } 7056689Smaybee 7066689Smaybee void 7076689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 7086689Smaybee { 7096689Smaybee ASSERT3P(owner, ==, ds->ds_owner); 7106689Smaybee if (!RW_WRITE_HELD(&ds->ds_rwlock)) 7116689Smaybee rw_enter(&ds->ds_rwlock, RW_WRITER); 7125367Sahrens } 7135367Sahrens 7142199Sahrens uint64_t 7157046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 7166492Stimh uint64_t flags, dmu_tx_t *tx) 717789Sahrens { 7185367Sahrens dsl_pool_t *dp = dd->dd_pool; 719789Sahrens dmu_buf_t *dbuf; 720789Sahrens dsl_dataset_phys_t *dsphys; 7215367Sahrens uint64_t dsobj; 722789Sahrens objset_t *mos = dp->dp_meta_objset; 723789Sahrens 7247046Sahrens if (origin == NULL) 7257046Sahrens origin = dp->dp_origin_snap; 7267046Sahrens 7275367Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 7285367Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 729789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 7305367Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 731789Sahrens 732928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 733928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7341544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 735789Sahrens dmu_buf_will_dirty(dbuf, tx); 736789Sahrens dsphys = dbuf->db_data; 7376689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 738789Sahrens dsphys->ds_dir_obj = dd->dd_object; 7396492Stimh dsphys->ds_flags = flags; 740789Sahrens dsphys->ds_fsid_guid = unique_create(); 741789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 742789Sahrens sizeof (dsphys->ds_guid)); 743789Sahrens dsphys->ds_snapnames_zapobj = 7446492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7456492Stimh DMU_OT_NONE, 0, tx); 746789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 7477046Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 748789Sahrens dsphys->ds_deadlist_obj = 749789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7505378Sck153898 7515367Sahrens if (origin) { 7525367Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 753789Sahrens dsphys->ds_prev_snap_txg = 7545367Sahrens origin->ds_phys->ds_creation_txg; 755789Sahrens dsphys->ds_used_bytes = 7565367Sahrens origin->ds_phys->ds_used_bytes; 757789Sahrens dsphys->ds_compressed_bytes = 7585367Sahrens origin->ds_phys->ds_compressed_bytes; 759789Sahrens dsphys->ds_uncompressed_bytes = 7605367Sahrens origin->ds_phys->ds_uncompressed_bytes; 7615367Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 7626502Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 763789Sahrens 7645367Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 7655367Sahrens origin->ds_phys->ds_num_children++; 766789Sahrens 7677046Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 7687046Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 7697046Sahrens origin->ds_phys->ds_next_clones_obj = 7707046Sahrens zap_create(mos, 7717046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 7727046Sahrens } 7737046Sahrens VERIFY(0 == zap_add_int(mos, 7747046Sahrens origin->ds_phys->ds_next_clones_obj, 7757046Sahrens dsobj, tx)); 7767046Sahrens } 7777046Sahrens 778789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 7795367Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 780789Sahrens } 7816492Stimh 7826492Stimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 7836492Stimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 7846492Stimh 7851544Seschrock dmu_buf_rele(dbuf, FTAG); 786789Sahrens 787789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 788789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 7895367Sahrens 7905367Sahrens return (dsobj); 7915367Sahrens } 7925367Sahrens 7935367Sahrens uint64_t 7946492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 7956492Stimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 7965367Sahrens { 7975367Sahrens dsl_pool_t *dp = pdd->dd_pool; 7985367Sahrens uint64_t dsobj, ddobj; 7995367Sahrens dsl_dir_t *dd; 8005367Sahrens 8015367Sahrens ASSERT(lastname[0] != '@'); 8025367Sahrens 8037046Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8045367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 8055367Sahrens 8067046Sahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx); 8075367Sahrens 8085367Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8095367Sahrens 810789Sahrens dsl_dir_close(dd, FTAG); 811789Sahrens 8122199Sahrens return (dsobj); 8132199Sahrens } 8142199Sahrens 8152199Sahrens struct destroyarg { 8162199Sahrens dsl_sync_task_group_t *dstg; 8172199Sahrens char *snapname; 8182199Sahrens char *failed; 8192199Sahrens }; 8202199Sahrens 8212199Sahrens static int 8222199Sahrens dsl_snapshot_destroy_one(char *name, void *arg) 8232199Sahrens { 8242199Sahrens struct destroyarg *da = arg; 8252199Sahrens dsl_dataset_t *ds; 8262199Sahrens char *cp; 8272199Sahrens int err; 8282199Sahrens 8292199Sahrens (void) strcat(name, "@"); 8302199Sahrens (void) strcat(name, da->snapname); 8316689Smaybee err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT, 8324007Smmusante da->dstg, &ds); 8332199Sahrens cp = strchr(name, '@'); 8342199Sahrens *cp = '\0'; 8356689Smaybee if (err == 0) { 8366689Smaybee dsl_dataset_make_exclusive(ds, da->dstg); 8377237Sek110237 if (ds->ds_user_ptr) { 8387237Sek110237 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 8397237Sek110237 ds->ds_user_ptr = NULL; 8407237Sek110237 } 8416689Smaybee dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 8426689Smaybee dsl_dataset_destroy_sync, ds, da->dstg, 0); 8436689Smaybee } else if (err == ENOENT) { 8446689Smaybee err = 0; 8456689Smaybee } else { 8462199Sahrens (void) strcpy(da->failed, name); 8472199Sahrens } 8486689Smaybee return (err); 849789Sahrens } 850789Sahrens 8512199Sahrens /* 8522199Sahrens * Destroy 'snapname' in all descendants of 'fsname'. 8532199Sahrens */ 8542199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 8552199Sahrens int 8562199Sahrens dsl_snapshots_destroy(char *fsname, char *snapname) 8572199Sahrens { 8582199Sahrens int err; 8592199Sahrens struct destroyarg da; 8602199Sahrens dsl_sync_task_t *dst; 8612199Sahrens spa_t *spa; 8622199Sahrens 8634603Sahrens err = spa_open(fsname, &spa, FTAG); 8642199Sahrens if (err) 8652199Sahrens return (err); 8662199Sahrens da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 8672199Sahrens da.snapname = snapname; 8682199Sahrens da.failed = fsname; 8692199Sahrens 8702199Sahrens err = dmu_objset_find(fsname, 8712417Sahrens dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 8722199Sahrens 8732199Sahrens if (err == 0) 8742199Sahrens err = dsl_sync_task_group_wait(da.dstg); 8752199Sahrens 8762199Sahrens for (dst = list_head(&da.dstg->dstg_tasks); dst; 8772199Sahrens dst = list_next(&da.dstg->dstg_tasks, dst)) { 8782199Sahrens dsl_dataset_t *ds = dst->dst_arg1; 8796689Smaybee /* 8806689Smaybee * Return the file system name that triggered the error 8816689Smaybee */ 8822199Sahrens if (dst->dst_err) { 8832199Sahrens dsl_dataset_name(ds, fsname); 8844603Sahrens *strchr(fsname, '@') = '\0'; 8852199Sahrens } 8866689Smaybee dsl_dataset_disown(ds, da.dstg); 8872199Sahrens } 8882199Sahrens 8892199Sahrens dsl_sync_task_group_destroy(da.dstg); 8902199Sahrens spa_close(spa, FTAG); 8912199Sahrens return (err); 8922199Sahrens } 8932199Sahrens 8945367Sahrens /* 8956689Smaybee * ds must be opened as OWNER. On return (whether successful or not), 8966689Smaybee * ds will be closed and caller can no longer dereference it. 8975367Sahrens */ 898789Sahrens int 8995367Sahrens dsl_dataset_destroy(dsl_dataset_t *ds, void *tag) 900789Sahrens { 901789Sahrens int err; 9022199Sahrens dsl_sync_task_group_t *dstg; 9032199Sahrens objset_t *os; 904789Sahrens dsl_dir_t *dd; 9052199Sahrens uint64_t obj; 9062199Sahrens 9075367Sahrens if (dsl_dataset_is_snapshot(ds)) { 9082199Sahrens /* Destroying a snapshot is simpler */ 9096689Smaybee dsl_dataset_make_exclusive(ds, tag); 9107237Sek110237 9117237Sek110237 if (ds->ds_user_ptr) { 9127237Sek110237 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 9137237Sek110237 ds->ds_user_ptr = NULL; 9147237Sek110237 } 9152199Sahrens err = dsl_sync_task_do(ds->ds_dir->dd_pool, 9162199Sahrens dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 9175367Sahrens ds, tag, 0); 9185367Sahrens goto out; 9192199Sahrens } 9202199Sahrens 9212199Sahrens dd = ds->ds_dir; 922789Sahrens 9232199Sahrens /* 9242199Sahrens * Check for errors and mark this ds as inconsistent, in 9252199Sahrens * case we crash while freeing the objects. 9262199Sahrens */ 9272199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 9282199Sahrens dsl_dataset_destroy_begin_sync, ds, NULL, 0); 9295367Sahrens if (err) 9305367Sahrens goto out; 9315367Sahrens 9325367Sahrens err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os); 9335367Sahrens if (err) 9345367Sahrens goto out; 9352199Sahrens 9362199Sahrens /* 9372199Sahrens * remove the objects in open context, so that we won't 9382199Sahrens * have too much to do in syncing context. 9392199Sahrens */ 9403025Sahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 9413025Sahrens ds->ds_phys->ds_prev_snap_txg)) { 9426992Smaybee /* 9436992Smaybee * Ignore errors, if there is not enough disk space 9446992Smaybee * we will deal with it in dsl_dataset_destroy_sync(). 9456992Smaybee */ 9466992Smaybee (void) dmu_free_object(os, obj); 9472199Sahrens } 9482199Sahrens 9492199Sahrens dmu_objset_close(os); 9502199Sahrens if (err != ESRCH) 9515367Sahrens goto out; 9522199Sahrens 9536975Smaybee rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 9546975Smaybee err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 9556975Smaybee rw_exit(&dd->dd_pool->dp_config_rwlock); 9566975Smaybee 9576975Smaybee if (err) 9586975Smaybee goto out; 9596975Smaybee 9605367Sahrens if (ds->ds_user_ptr) { 9616689Smaybee /* 9626689Smaybee * We need to sync out all in-flight IO before we try 9636689Smaybee * to evict (the dataset evict func is trying to clear 9646689Smaybee * the cached entries for this dataset in the ARC). 9656689Smaybee */ 9666689Smaybee txg_wait_synced(dd->dd_pool, 0); 9675367Sahrens } 9685367Sahrens 9692199Sahrens /* 9702199Sahrens * Blow away the dsl_dir + head dataset. 9712199Sahrens */ 9726689Smaybee dsl_dataset_make_exclusive(ds, tag); 9736975Smaybee if (ds->ds_user_ptr) { 9746975Smaybee ds->ds_user_evict_func(ds, ds->ds_user_ptr); 9756975Smaybee ds->ds_user_ptr = NULL; 9766975Smaybee } 9772199Sahrens dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 9782199Sahrens dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 9795367Sahrens dsl_dataset_destroy_sync, ds, tag, 0); 9802199Sahrens dsl_sync_task_create(dstg, dsl_dir_destroy_check, 9812199Sahrens dsl_dir_destroy_sync, dd, FTAG, 0); 9822199Sahrens err = dsl_sync_task_group_wait(dstg); 9832199Sahrens dsl_sync_task_group_destroy(dstg); 9846689Smaybee /* if it is successful, dsl_dir_destroy_sync will close the dd */ 9855367Sahrens if (err) 9862199Sahrens dsl_dir_close(dd, FTAG); 9875367Sahrens out: 9886689Smaybee dsl_dataset_disown(ds, tag); 989789Sahrens return (err); 990789Sahrens } 991789Sahrens 992789Sahrens int 9935367Sahrens dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost) 994789Sahrens { 995*7385SMark.Maybee@Sun.COM int err; 996*7385SMark.Maybee@Sun.COM 9976689Smaybee ASSERT(ds->ds_owner); 9985367Sahrens 999*7385SMark.Maybee@Sun.COM dsl_dataset_make_exclusive(ds, ds->ds_owner); 1000*7385SMark.Maybee@Sun.COM err = dsl_sync_task_do(ds->ds_dir->dd_pool, 10012199Sahrens dsl_dataset_rollback_check, dsl_dataset_rollback_sync, 1002*7385SMark.Maybee@Sun.COM ds, &ost, 0); 1003*7385SMark.Maybee@Sun.COM /* drop exclusive access */ 1004*7385SMark.Maybee@Sun.COM mutex_enter(&ds->ds_lock); 1005*7385SMark.Maybee@Sun.COM rw_exit(&ds->ds_rwlock); 1006*7385SMark.Maybee@Sun.COM cv_broadcast(&ds->ds_exclusive_cv); 1007*7385SMark.Maybee@Sun.COM mutex_exit(&ds->ds_lock); 1008*7385SMark.Maybee@Sun.COM return (err); 1009789Sahrens } 1010789Sahrens 1011789Sahrens void * 1012789Sahrens dsl_dataset_set_user_ptr(dsl_dataset_t *ds, 1013789Sahrens void *p, dsl_dataset_evict_func_t func) 1014789Sahrens { 1015789Sahrens void *old; 1016789Sahrens 1017789Sahrens mutex_enter(&ds->ds_lock); 1018789Sahrens old = ds->ds_user_ptr; 1019789Sahrens if (old == NULL) { 1020789Sahrens ds->ds_user_ptr = p; 1021789Sahrens ds->ds_user_evict_func = func; 1022789Sahrens } 1023789Sahrens mutex_exit(&ds->ds_lock); 1024789Sahrens return (old); 1025789Sahrens } 1026789Sahrens 1027789Sahrens void * 1028789Sahrens dsl_dataset_get_user_ptr(dsl_dataset_t *ds) 1029789Sahrens { 1030789Sahrens return (ds->ds_user_ptr); 1031789Sahrens } 1032789Sahrens 1033789Sahrens 10343547Smaybee blkptr_t * 10353547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1036789Sahrens { 10373547Smaybee return (&ds->ds_phys->ds_bp); 1038789Sahrens } 1039789Sahrens 1040789Sahrens void 1041789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1042789Sahrens { 1043789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1044789Sahrens /* If it's the meta-objset, set dp_meta_rootbp */ 1045789Sahrens if (ds == NULL) { 1046789Sahrens tx->tx_pool->dp_meta_rootbp = *bp; 1047789Sahrens } else { 1048789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1049789Sahrens ds->ds_phys->ds_bp = *bp; 1050789Sahrens } 1051789Sahrens } 1052789Sahrens 1053789Sahrens spa_t * 1054789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 1055789Sahrens { 1056789Sahrens return (ds->ds_dir->dd_pool->dp_spa); 1057789Sahrens } 1058789Sahrens 1059789Sahrens void 1060789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1061789Sahrens { 1062789Sahrens dsl_pool_t *dp; 1063789Sahrens 1064789Sahrens if (ds == NULL) /* this is the meta-objset */ 1065789Sahrens return; 1066789Sahrens 1067789Sahrens ASSERT(ds->ds_user_ptr != NULL); 10682885Sahrens 10692885Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 10702885Sahrens panic("dirtying snapshot!"); 1071789Sahrens 1072789Sahrens dp = ds->ds_dir->dd_pool; 1073789Sahrens 1074789Sahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1075789Sahrens /* up the hold count until we can be written out */ 1076789Sahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 1077789Sahrens } 1078789Sahrens } 1079789Sahrens 10805378Sck153898 /* 10815378Sck153898 * The unique space in the head dataset can be calculated by subtracting 10825378Sck153898 * the space used in the most recent snapshot, that is still being used 10835378Sck153898 * in this file system, from the space currently in use. To figure out 10845378Sck153898 * the space in the most recent snapshot still in use, we need to take 10855378Sck153898 * the total space used in the snapshot and subtract out the space that 10865378Sck153898 * has been freed up since the snapshot was taken. 10875378Sck153898 */ 10885378Sck153898 static void 10895378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 10905378Sck153898 { 10915378Sck153898 uint64_t mrs_used; 10925378Sck153898 uint64_t dlused, dlcomp, dluncomp; 10935378Sck153898 10945378Sck153898 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 10955378Sck153898 10965378Sck153898 if (ds->ds_phys->ds_prev_snap_obj != 0) 10975378Sck153898 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 10985378Sck153898 else 10995378Sck153898 mrs_used = 0; 11005378Sck153898 11015378Sck153898 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 11025378Sck153898 &dluncomp)); 11035378Sck153898 11045378Sck153898 ASSERT3U(dlused, <=, mrs_used); 11055378Sck153898 ds->ds_phys->ds_unique_bytes = 11065378Sck153898 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 11075378Sck153898 11085378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && 11095378Sck153898 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 11105378Sck153898 SPA_VERSION_UNIQUE_ACCURATE) 11115378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 11125378Sck153898 } 11135378Sck153898 11145378Sck153898 static uint64_t 11155378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds) 11165378Sck153898 { 11175378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 11185378Sck153898 dsl_dataset_recalc_head_uniq(ds); 11195378Sck153898 11205378Sck153898 return (ds->ds_phys->ds_unique_bytes); 11215378Sck153898 } 11225378Sck153898 1123789Sahrens struct killarg { 11245378Sck153898 int64_t *usedp; 11255378Sck153898 int64_t *compressedp; 11265378Sck153898 int64_t *uncompressedp; 1127789Sahrens zio_t *zio; 1128789Sahrens dmu_tx_t *tx; 1129789Sahrens }; 1130789Sahrens 1131789Sahrens static int 1132789Sahrens kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg) 1133789Sahrens { 1134789Sahrens struct killarg *ka = arg; 1135789Sahrens blkptr_t *bp = &bc->bc_blkptr; 1136789Sahrens 1137789Sahrens ASSERT3U(bc->bc_errno, ==, 0); 1138789Sahrens 1139789Sahrens /* 1140789Sahrens * Since this callback is not called concurrently, no lock is 1141789Sahrens * needed on the accounting values. 1142789Sahrens */ 11432082Seschrock *ka->usedp += bp_get_dasize(spa, bp); 1144789Sahrens *ka->compressedp += BP_GET_PSIZE(bp); 1145789Sahrens *ka->uncompressedp += BP_GET_UCSIZE(bp); 1146789Sahrens /* XXX check for EIO? */ 11477046Sahrens (void) dsl_free(ka->zio, spa_get_dsl(spa), ka->tx->tx_txg, 11487046Sahrens bp, NULL, NULL, ARC_NOWAIT); 1149789Sahrens return (0); 1150789Sahrens } 1151789Sahrens 1152789Sahrens /* ARGSUSED */ 11532199Sahrens static int 11542199Sahrens dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx) 1155789Sahrens { 11562199Sahrens dsl_dataset_t *ds = arg1; 11575367Sahrens dmu_objset_type_t *ost = arg2; 1158789Sahrens 11592199Sahrens /* 11605367Sahrens * We can only roll back to emptyness if it is a ZPL objset. 11612199Sahrens */ 11625367Sahrens if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0) 1163789Sahrens return (EINVAL); 1164789Sahrens 11652199Sahrens /* 11662199Sahrens * This must not be a snapshot. 11672199Sahrens */ 11682199Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 11692199Sahrens return (EINVAL); 1170789Sahrens 1171789Sahrens /* 1172789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1173789Sahrens * them. Try again. 1174789Sahrens */ 11752199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1176789Sahrens return (EAGAIN); 11772199Sahrens 11782199Sahrens return (0); 11792199Sahrens } 1180789Sahrens 11812199Sahrens /* ARGSUSED */ 11822199Sahrens static void 11834543Smarks dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 11842199Sahrens { 11852199Sahrens dsl_dataset_t *ds = arg1; 11865367Sahrens dmu_objset_type_t *ost = arg2; 11872199Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1188789Sahrens 1189789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1190789Sahrens 11914967Sperrin /* 11924967Sperrin * Before the roll back destroy the zil. 11934967Sperrin */ 11944967Sperrin if (ds->ds_user_ptr != NULL) { 11954967Sperrin zil_rollback_destroy( 11964967Sperrin ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx); 11975367Sahrens 11985367Sahrens /* 11995367Sahrens * We need to make sure that the objset_impl_t is reopened after 12005367Sahrens * we do the rollback, otherwise it will have the wrong 12015367Sahrens * objset_phys_t. Normally this would happen when this 12026689Smaybee * dataset-open is closed, thus causing the 12035367Sahrens * dataset to be immediately evicted. But when doing "zfs recv 12045367Sahrens * -F", we reopen the objset before that, so that there is no 12055367Sahrens * window where the dataset is closed and inconsistent. 12065367Sahrens */ 12075367Sahrens ds->ds_user_evict_func(ds, ds->ds_user_ptr); 12085367Sahrens ds->ds_user_ptr = NULL; 12094967Sperrin } 12104935Sperrin 1211789Sahrens /* Zero out the deadlist. */ 1212789Sahrens bplist_close(&ds->ds_deadlist); 1213789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1214789Sahrens ds->ds_phys->ds_deadlist_obj = 1215789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 12161544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 12171544Seschrock ds->ds_phys->ds_deadlist_obj)); 1218789Sahrens 1219789Sahrens { 1220789Sahrens /* Free blkptrs that we gave birth to */ 1221789Sahrens zio_t *zio; 12225378Sck153898 int64_t used = 0, compressed = 0, uncompressed = 0; 1223789Sahrens struct killarg ka; 12245518Sck153898 int64_t delta; 1225789Sahrens 1226789Sahrens zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL, 1227789Sahrens ZIO_FLAG_MUSTSUCCEED); 1228789Sahrens ka.usedp = &used; 1229789Sahrens ka.compressedp = &compressed; 1230789Sahrens ka.uncompressedp = &uncompressed; 1231789Sahrens ka.zio = zio; 1232789Sahrens ka.tx = tx; 1233789Sahrens (void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1234789Sahrens ADVANCE_POST, kill_blkptr, &ka); 1235789Sahrens (void) zio_wait(zio); 1236789Sahrens 12375518Sck153898 /* only deduct space beyond any refreservation */ 12385518Sck153898 delta = parent_delta(ds, -used); 12392199Sahrens dsl_dir_diduse_space(ds->ds_dir, 12405518Sck153898 delta, -compressed, -uncompressed, tx); 1241789Sahrens } 1242789Sahrens 12437046Sahrens if (ds->ds_prev && ds->ds_prev != ds->ds_dir->dd_pool->dp_origin_snap) { 12445367Sahrens /* Change our contents to that of the prev snapshot */ 12455367Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 12465367Sahrens ds->ds_phys->ds_prev_snap_obj); 12475367Sahrens ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp; 12485367Sahrens ds->ds_phys->ds_used_bytes = 12495367Sahrens ds->ds_prev->ds_phys->ds_used_bytes; 12505367Sahrens ds->ds_phys->ds_compressed_bytes = 12515367Sahrens ds->ds_prev->ds_phys->ds_compressed_bytes; 12525367Sahrens ds->ds_phys->ds_uncompressed_bytes = 12535367Sahrens ds->ds_prev->ds_phys->ds_uncompressed_bytes; 12545367Sahrens ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags; 12555367Sahrens ds->ds_phys->ds_unique_bytes = 0; 1256789Sahrens 12575367Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 12585367Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 12595367Sahrens ds->ds_prev->ds_phys->ds_unique_bytes = 0; 12605367Sahrens } 12615367Sahrens } else { 12627046Sahrens objset_impl_t *osi; 12637046Sahrens 12645367Sahrens /* Zero out our contents, recreate objset */ 12655367Sahrens bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t)); 12665367Sahrens ds->ds_phys->ds_used_bytes = 0; 12675367Sahrens ds->ds_phys->ds_compressed_bytes = 0; 12685367Sahrens ds->ds_phys->ds_uncompressed_bytes = 0; 12695367Sahrens ds->ds_phys->ds_flags = 0; 12705367Sahrens ds->ds_phys->ds_unique_bytes = 0; 12717046Sahrens osi = dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds, 12725367Sahrens &ds->ds_phys->ds_bp, *ost, tx); 12737046Sahrens #ifdef _KERNEL 12747046Sahrens zfs_create_fs(&osi->os, kcred, NULL, tx); 12757046Sahrens #endif 12762532Sahrens } 12774543Smarks 12784543Smarks spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa, 12794543Smarks tx, cr, "dataset = %llu", ds->ds_object); 1280789Sahrens } 1281789Sahrens 12821731Sbonwick /* ARGSUSED */ 12831731Sbonwick static int 12842199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 12851731Sbonwick { 12862199Sahrens dsl_dataset_t *ds = arg1; 12875367Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 12885367Sahrens uint64_t count; 12895367Sahrens int err; 12901731Sbonwick 12911731Sbonwick /* 12921731Sbonwick * Can't delete a head dataset if there are snapshots of it. 12931731Sbonwick * (Except if the only snapshots are from the branch we cloned 12941731Sbonwick * from.) 12951731Sbonwick */ 12961731Sbonwick if (ds->ds_prev != NULL && 12971731Sbonwick ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 12981731Sbonwick return (EINVAL); 12991731Sbonwick 13005367Sahrens /* 13015367Sahrens * This is really a dsl_dir thing, but check it here so that 13025367Sahrens * we'll be less likely to leave this dataset inconsistent & 13035367Sahrens * nearly destroyed. 13045367Sahrens */ 13055367Sahrens err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 13065367Sahrens if (err) 13075367Sahrens return (err); 13085367Sahrens if (count != 0) 13095367Sahrens return (EEXIST); 13105367Sahrens 13111731Sbonwick return (0); 13121731Sbonwick } 13131731Sbonwick 13142199Sahrens /* ARGSUSED */ 13152199Sahrens static void 13164543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1317789Sahrens { 13182199Sahrens dsl_dataset_t *ds = arg1; 13194543Smarks dsl_pool_t *dp = ds->ds_dir->dd_pool; 1320789Sahrens 13212199Sahrens /* Mark it as inconsistent on-disk, in case we crash */ 13222199Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13232199Sahrens ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 13244543Smarks 13254543Smarks spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 13264543Smarks cr, "dataset = %llu", ds->ds_object); 13272199Sahrens } 1328789Sahrens 13292199Sahrens /* ARGSUSED */ 13305367Sahrens int 13312199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 13322199Sahrens { 13332199Sahrens dsl_dataset_t *ds = arg1; 1334789Sahrens 13356689Smaybee /* we have an owner hold, so noone else can destroy us */ 13366689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 13376689Smaybee 1338789Sahrens /* Can't delete a branch point. */ 13392199Sahrens if (ds->ds_phys->ds_num_children > 1) 13402199Sahrens return (EEXIST); 1341789Sahrens 1342789Sahrens /* 1343789Sahrens * Can't delete a head dataset if there are snapshots of it. 1344789Sahrens * (Except if the only snapshots are from the branch we cloned 1345789Sahrens * from.) 1346789Sahrens */ 1347789Sahrens if (ds->ds_prev != NULL && 13482199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1349789Sahrens return (EINVAL); 1350789Sahrens 1351789Sahrens /* 1352789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1353789Sahrens * them. Try again. 1354789Sahrens */ 13552199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1356789Sahrens return (EAGAIN); 13572199Sahrens 13582199Sahrens /* XXX we should do some i/o error checking... */ 13592199Sahrens return (0); 13602199Sahrens } 13612199Sahrens 13626689Smaybee struct refsarg { 13636689Smaybee kmutex_t lock; 13646689Smaybee boolean_t gone; 13656689Smaybee kcondvar_t cv; 13666689Smaybee }; 13676689Smaybee 13686689Smaybee /* ARGSUSED */ 13696689Smaybee static void 13706689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 13716689Smaybee { 13726689Smaybee struct refsarg *arg = argv; 13736689Smaybee 13746689Smaybee mutex_enter(&arg->lock); 13756689Smaybee arg->gone = TRUE; 13766689Smaybee cv_signal(&arg->cv); 13776689Smaybee mutex_exit(&arg->lock); 13786689Smaybee } 13796689Smaybee 13806689Smaybee static void 13816689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 13826689Smaybee { 13836689Smaybee struct refsarg arg; 13846689Smaybee 13856689Smaybee mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 13866689Smaybee cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 13876689Smaybee arg.gone = FALSE; 13886689Smaybee (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 13896689Smaybee dsl_dataset_refs_gone); 13906689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 13916689Smaybee mutex_enter(&arg.lock); 13926689Smaybee while (!arg.gone) 13936689Smaybee cv_wait(&arg.cv, &arg.lock); 13946689Smaybee ASSERT(arg.gone); 13956689Smaybee mutex_exit(&arg.lock); 13966689Smaybee ds->ds_dbuf = NULL; 13976689Smaybee ds->ds_phys = NULL; 13986689Smaybee mutex_destroy(&arg.lock); 13996689Smaybee cv_destroy(&arg.cv); 14006689Smaybee } 14016689Smaybee 14025367Sahrens void 14034543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 14042199Sahrens { 14052199Sahrens dsl_dataset_t *ds = arg1; 14065378Sck153898 int64_t used = 0, compressed = 0, uncompressed = 0; 14072199Sahrens zio_t *zio; 14082199Sahrens int err; 14092199Sahrens int after_branch_point = FALSE; 14102199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 14112199Sahrens objset_t *mos = dp->dp_meta_objset; 14122199Sahrens dsl_dataset_t *ds_prev = NULL; 14132199Sahrens uint64_t obj; 14142199Sahrens 14156689Smaybee ASSERT(ds->ds_owner); 14162199Sahrens ASSERT3U(ds->ds_phys->ds_num_children, <=, 1); 14172199Sahrens ASSERT(ds->ds_prev == NULL || 14182199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 14192199Sahrens ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 14202199Sahrens 14216689Smaybee /* signal any waiters that this dataset is going away */ 14226689Smaybee mutex_enter(&ds->ds_lock); 14236689Smaybee ds->ds_owner = dsl_reaper; 14246689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 14256689Smaybee mutex_exit(&ds->ds_lock); 14266689Smaybee 14275378Sck153898 /* Remove our reservation */ 14285378Sck153898 if (ds->ds_reserved != 0) { 14295378Sck153898 uint64_t val = 0; 14305378Sck153898 dsl_dataset_set_reservation_sync(ds, &val, cr, tx); 14315378Sck153898 ASSERT3U(ds->ds_reserved, ==, 0); 14325378Sck153898 } 14335378Sck153898 14342199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 14352199Sahrens 14367046Sahrens dsl_pool_ds_destroyed(ds, tx); 14377046Sahrens 14382199Sahrens obj = ds->ds_object; 1439789Sahrens 1440789Sahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 1441789Sahrens if (ds->ds_prev) { 1442789Sahrens ds_prev = ds->ds_prev; 1443789Sahrens } else { 14446689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 14456689Smaybee ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1446789Sahrens } 1447789Sahrens after_branch_point = 1448789Sahrens (ds_prev->ds_phys->ds_next_snap_obj != obj); 1449789Sahrens 1450789Sahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1451789Sahrens if (after_branch_point && 14527046Sahrens ds_prev->ds_phys->ds_next_clones_obj != 0) { 14537046Sahrens VERIFY(0 == zap_remove_int(mos, 14547046Sahrens ds_prev->ds_phys->ds_next_clones_obj, obj, tx)); 14557046Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 14567046Sahrens VERIFY(0 == zap_add_int(mos, 14577046Sahrens ds_prev->ds_phys->ds_next_clones_obj, 14587046Sahrens ds->ds_phys->ds_next_snap_obj, tx)); 14597046Sahrens } 14607046Sahrens } 14617046Sahrens if (after_branch_point && 1462789Sahrens ds->ds_phys->ds_next_snap_obj == 0) { 1463789Sahrens /* This clone is toast. */ 1464789Sahrens ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1465789Sahrens ds_prev->ds_phys->ds_num_children--; 1466789Sahrens } else if (!after_branch_point) { 1467789Sahrens ds_prev->ds_phys->ds_next_snap_obj = 1468789Sahrens ds->ds_phys->ds_next_snap_obj; 1469789Sahrens } 1470789Sahrens } 1471789Sahrens 1472789Sahrens zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1473789Sahrens 1474789Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 14752199Sahrens blkptr_t bp; 1476789Sahrens dsl_dataset_t *ds_next; 1477789Sahrens uint64_t itor = 0; 14785378Sck153898 uint64_t old_unique; 1479789Sahrens 14806689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 14816689Smaybee ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1482789Sahrens ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1483789Sahrens 14845378Sck153898 old_unique = dsl_dataset_unique(ds_next); 14855378Sck153898 1486789Sahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1487789Sahrens ds_next->ds_phys->ds_prev_snap_obj = 1488789Sahrens ds->ds_phys->ds_prev_snap_obj; 1489789Sahrens ds_next->ds_phys->ds_prev_snap_txg = 1490789Sahrens ds->ds_phys->ds_prev_snap_txg; 1491789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1492789Sahrens ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1493789Sahrens 1494789Sahrens /* 1495789Sahrens * Transfer to our deadlist (which will become next's 1496789Sahrens * new deadlist) any entries from next's current 1497789Sahrens * deadlist which were born before prev, and free the 1498789Sahrens * other entries. 1499789Sahrens * 1500789Sahrens * XXX we're doing this long task with the config lock held 1501789Sahrens */ 15026689Smaybee while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1503789Sahrens if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 15041544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 15051544Seschrock &bp, tx)); 1506789Sahrens if (ds_prev && !after_branch_point && 1507789Sahrens bp.blk_birth > 1508789Sahrens ds_prev->ds_phys->ds_prev_snap_txg) { 1509789Sahrens ds_prev->ds_phys->ds_unique_bytes += 15102082Seschrock bp_get_dasize(dp->dp_spa, &bp); 1511789Sahrens } 1512789Sahrens } else { 15132082Seschrock used += bp_get_dasize(dp->dp_spa, &bp); 1514789Sahrens compressed += BP_GET_PSIZE(&bp); 1515789Sahrens uncompressed += BP_GET_UCSIZE(&bp); 1516789Sahrens /* XXX check return value? */ 15177046Sahrens (void) dsl_free(zio, dp, tx->tx_txg, 1518789Sahrens &bp, NULL, NULL, ARC_NOWAIT); 1519789Sahrens } 1520789Sahrens } 1521789Sahrens 1522789Sahrens /* free next's deadlist */ 1523789Sahrens bplist_close(&ds_next->ds_deadlist); 1524789Sahrens bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1525789Sahrens 1526789Sahrens /* set next's deadlist to our deadlist */ 15276689Smaybee bplist_close(&ds->ds_deadlist); 1528789Sahrens ds_next->ds_phys->ds_deadlist_obj = 1529789Sahrens ds->ds_phys->ds_deadlist_obj; 15301544Seschrock VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 15311544Seschrock ds_next->ds_phys->ds_deadlist_obj)); 1532789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1533789Sahrens 1534789Sahrens if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1535789Sahrens /* 1536789Sahrens * Update next's unique to include blocks which 1537789Sahrens * were previously shared by only this snapshot 1538789Sahrens * and it. Those blocks will be born after the 1539789Sahrens * prev snap and before this snap, and will have 1540789Sahrens * died after the next snap and before the one 1541789Sahrens * after that (ie. be on the snap after next's 1542789Sahrens * deadlist). 1543789Sahrens * 1544789Sahrens * XXX we're doing this long task with the 1545789Sahrens * config lock held 1546789Sahrens */ 1547789Sahrens dsl_dataset_t *ds_after_next; 1548789Sahrens 15496689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15506689Smaybee ds_next->ds_phys->ds_next_snap_obj, 15516689Smaybee FTAG, &ds_after_next)); 1552789Sahrens itor = 0; 1553789Sahrens while (bplist_iterate(&ds_after_next->ds_deadlist, 1554789Sahrens &itor, &bp) == 0) { 1555789Sahrens if (bp.blk_birth > 1556789Sahrens ds->ds_phys->ds_prev_snap_txg && 1557789Sahrens bp.blk_birth <= 1558789Sahrens ds->ds_phys->ds_creation_txg) { 1559789Sahrens ds_next->ds_phys->ds_unique_bytes += 15602082Seschrock bp_get_dasize(dp->dp_spa, &bp); 1561789Sahrens } 1562789Sahrens } 1563789Sahrens 15646689Smaybee dsl_dataset_rele(ds_after_next, FTAG); 1565789Sahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 1566789Sahrens } else { 1567789Sahrens ASSERT3P(ds_next->ds_prev, ==, ds); 15686689Smaybee dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 15696689Smaybee ds_next->ds_prev = NULL; 1570789Sahrens if (ds_prev) { 15716689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 15726689Smaybee ds->ds_phys->ds_prev_snap_obj, 15736689Smaybee ds_next, &ds_next->ds_prev)); 1574789Sahrens } 15755378Sck153898 15765378Sck153898 dsl_dataset_recalc_head_uniq(ds_next); 15775378Sck153898 15785378Sck153898 /* 15795378Sck153898 * Reduce the amount of our unconsmed refreservation 15805378Sck153898 * being charged to our parent by the amount of 15815378Sck153898 * new unique data we have gained. 15825378Sck153898 */ 15835378Sck153898 if (old_unique < ds_next->ds_reserved) { 15845378Sck153898 int64_t mrsdelta; 15855378Sck153898 uint64_t new_unique = 15865378Sck153898 ds_next->ds_phys->ds_unique_bytes; 15875378Sck153898 15885378Sck153898 ASSERT(old_unique <= new_unique); 15895378Sck153898 mrsdelta = MIN(new_unique - old_unique, 15905378Sck153898 ds_next->ds_reserved - old_unique); 15915378Sck153898 dsl_dir_diduse_space(ds->ds_dir, -mrsdelta, 15925378Sck153898 0, 0, tx); 15935378Sck153898 } 1594789Sahrens } 15956689Smaybee dsl_dataset_rele(ds_next, FTAG); 1596789Sahrens 1597789Sahrens /* 15985378Sck153898 * NB: unique_bytes might not be accurate for the head objset. 15995378Sck153898 * Before SPA_VERSION 9, we didn't update its value when we 16005378Sck153898 * deleted the most recent snapshot. 1601789Sahrens */ 1602789Sahrens ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 1603789Sahrens } else { 1604789Sahrens /* 1605789Sahrens * There's no next snapshot, so this is a head dataset. 1606789Sahrens * Destroy the deadlist. Unless it's a clone, the 1607789Sahrens * deadlist should be empty. (If it's a clone, it's 1608789Sahrens * safe to ignore the deadlist contents.) 1609789Sahrens */ 1610789Sahrens struct killarg ka; 1611789Sahrens 1612789Sahrens ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1613789Sahrens bplist_close(&ds->ds_deadlist); 1614789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1615789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1616789Sahrens 1617789Sahrens /* 1618789Sahrens * Free everything that we point to (that's born after 1619789Sahrens * the previous snapshot, if we are a clone) 1620789Sahrens * 1621789Sahrens * XXX we're doing this long task with the config lock held 1622789Sahrens */ 1623789Sahrens ka.usedp = &used; 1624789Sahrens ka.compressedp = &compressed; 1625789Sahrens ka.uncompressedp = &uncompressed; 1626789Sahrens ka.zio = zio; 1627789Sahrens ka.tx = tx; 1628789Sahrens err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1629789Sahrens ADVANCE_POST, kill_blkptr, &ka); 1630789Sahrens ASSERT3U(err, ==, 0); 16315378Sck153898 ASSERT(spa_version(dp->dp_spa) < 16325378Sck153898 SPA_VERSION_UNIQUE_ACCURATE || 16335378Sck153898 used == ds->ds_phys->ds_unique_bytes); 1634789Sahrens } 1635789Sahrens 1636789Sahrens err = zio_wait(zio); 1637789Sahrens ASSERT3U(err, ==, 0); 1638789Sahrens 16392199Sahrens dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx); 1640789Sahrens 16416689Smaybee if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 16426689Smaybee /* Erase the link in the dir */ 16436689Smaybee dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 16446689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 16456689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1646789Sahrens err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1647789Sahrens ASSERT(err == 0); 1648789Sahrens } else { 1649789Sahrens /* remove from snapshot namespace */ 1650789Sahrens dsl_dataset_t *ds_head; 16516689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 16526689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16536689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 16542207Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 1655789Sahrens #ifdef ZFS_DEBUG 1656789Sahrens { 1657789Sahrens uint64_t val; 16586492Stimh 16596689Smaybee err = dsl_dataset_snap_lookup(ds_head, 16606492Stimh ds->ds_snapname, &val); 1661789Sahrens ASSERT3U(err, ==, 0); 1662789Sahrens ASSERT3U(val, ==, obj); 1663789Sahrens } 1664789Sahrens #endif 16656689Smaybee err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1666789Sahrens ASSERT(err == 0); 16676689Smaybee dsl_dataset_rele(ds_head, FTAG); 1668789Sahrens } 1669789Sahrens 1670789Sahrens if (ds_prev && ds->ds_prev != ds_prev) 16716689Smaybee dsl_dataset_rele(ds_prev, FTAG); 1672789Sahrens 16735094Slling spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 16744543Smarks spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 16754543Smarks cr, "dataset = %llu", ds->ds_object); 16764543Smarks 16777046Sahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 16787046Sahrens uint64_t count; 16797046Sahrens ASSERT(0 == zap_count(mos, 16807046Sahrens ds->ds_phys->ds_next_clones_obj, &count) && count == 0); 16817046Sahrens VERIFY(0 == dmu_object_free(mos, 16827046Sahrens ds->ds_phys->ds_next_clones_obj, tx)); 16837046Sahrens } 16847265Sahrens if (ds->ds_phys->ds_props_obj != 0) { 16857265Sahrens VERIFY(0 == zap_destroy(mos, 16867265Sahrens ds->ds_phys->ds_props_obj, tx)); 16877265Sahrens } 16886689Smaybee dsl_dir_close(ds->ds_dir, ds); 16896689Smaybee ds->ds_dir = NULL; 16906689Smaybee dsl_dataset_drain_refs(ds, tag); 16912199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 16922199Sahrens } 16932199Sahrens 16945378Sck153898 static int 16955378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 16965378Sck153898 { 16975378Sck153898 uint64_t asize; 16985378Sck153898 16995378Sck153898 if (!dmu_tx_is_syncing(tx)) 17005378Sck153898 return (0); 17015378Sck153898 17025378Sck153898 /* 17035378Sck153898 * If there's an fs-only reservation, any blocks that might become 17045378Sck153898 * owned by the snapshot dataset must be accommodated by space 17055378Sck153898 * outside of the reservation. 17065378Sck153898 */ 17075378Sck153898 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 17085378Sck153898 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 17095378Sck153898 return (ENOSPC); 17105378Sck153898 17115378Sck153898 /* 17125378Sck153898 * Propogate any reserved space for this snapshot to other 17135378Sck153898 * snapshot checks in this sync group. 17145378Sck153898 */ 17155378Sck153898 if (asize > 0) 17165378Sck153898 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 17175378Sck153898 17185378Sck153898 return (0); 17195378Sck153898 } 17205378Sck153898 17212199Sahrens /* ARGSUSED */ 17222199Sahrens int 17232199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 17242199Sahrens { 17255367Sahrens dsl_dataset_t *ds = arg1; 17262199Sahrens const char *snapname = arg2; 17272199Sahrens int err; 17282199Sahrens uint64_t value; 1729789Sahrens 1730789Sahrens /* 17312199Sahrens * We don't allow multiple snapshots of the same txg. If there 17322199Sahrens * is already one, try again. 17332199Sahrens */ 17342199Sahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 17352199Sahrens return (EAGAIN); 17362199Sahrens 17372199Sahrens /* 17382199Sahrens * Check for conflicting name snapshot name. 1739789Sahrens */ 17406689Smaybee err = dsl_dataset_snap_lookup(ds, snapname, &value); 17412199Sahrens if (err == 0) 17422199Sahrens return (EEXIST); 17432199Sahrens if (err != ENOENT) 17442199Sahrens return (err); 1745789Sahrens 17463978Smmusante /* 17473978Smmusante * Check that the dataset's name is not too long. Name consists 17483978Smmusante * of the dataset's length + 1 for the @-sign + snapshot name's length 17493978Smmusante */ 17503978Smmusante if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 17513978Smmusante return (ENAMETOOLONG); 17523978Smmusante 17535378Sck153898 err = dsl_dataset_snapshot_reserve_space(ds, tx); 17545378Sck153898 if (err) 17555378Sck153898 return (err); 17565378Sck153898 17572199Sahrens ds->ds_trysnap_txg = tx->tx_txg; 1758789Sahrens return (0); 1759789Sahrens } 1760789Sahrens 17612199Sahrens void 17624543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1763789Sahrens { 17645367Sahrens dsl_dataset_t *ds = arg1; 17652199Sahrens const char *snapname = arg2; 17662199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1767789Sahrens dmu_buf_t *dbuf; 1768789Sahrens dsl_dataset_phys_t *dsphys; 17697046Sahrens uint64_t dsobj, crtxg; 1770789Sahrens objset_t *mos = dp->dp_meta_objset; 1771789Sahrens int err; 1772789Sahrens 17732199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1774789Sahrens 17757046Sahrens /* 17767046Sahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 17777046Sahrens */ 17787046Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 17797046Sahrens crtxg = 1; 17807046Sahrens else 17817046Sahrens crtxg = tx->tx_txg; 17827046Sahrens 1783928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1784928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 17851544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1786789Sahrens dmu_buf_will_dirty(dbuf, tx); 1787789Sahrens dsphys = dbuf->db_data; 17886689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 17892199Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1790789Sahrens dsphys->ds_fsid_guid = unique_create(); 1791789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1792789Sahrens sizeof (dsphys->ds_guid)); 1793789Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1794789Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1795789Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1796789Sahrens dsphys->ds_num_children = 1; 1797789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 17987046Sahrens dsphys->ds_creation_txg = crtxg; 1799789Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1800789Sahrens dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1801789Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1802789Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 18032082Seschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1804789Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 18051544Seschrock dmu_buf_rele(dbuf, FTAG); 1806789Sahrens 18072199Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 18082199Sahrens if (ds->ds_prev) { 18097046Sahrens uint64_t next_clones_obj = 18107046Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 18112199Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1812789Sahrens ds->ds_object || 18132199Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 18142199Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 18152199Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1816789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 18172199Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 18182199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 18197046Sahrens } else if (next_clones_obj != 0) { 18207046Sahrens VERIFY3U(0, ==, zap_remove_int(mos, 18217046Sahrens next_clones_obj, dsphys->ds_next_snap_obj, tx)); 18227046Sahrens VERIFY3U(0, ==, zap_add_int(mos, 18237046Sahrens next_clones_obj, dsobj, tx)); 1824789Sahrens } 1825789Sahrens } 1826789Sahrens 18275378Sck153898 /* 18285378Sck153898 * If we have a reference-reservation on this dataset, we will 18295378Sck153898 * need to increase the amount of refreservation being charged 18305378Sck153898 * since our unique space is going to zero. 18315378Sck153898 */ 18325378Sck153898 if (ds->ds_reserved) { 18335378Sck153898 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 18345378Sck153898 dsl_dir_diduse_space(ds->ds_dir, add, 0, 0, tx); 18355378Sck153898 } 18365378Sck153898 1837789Sahrens bplist_close(&ds->ds_deadlist); 1838789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 18395712Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1840789Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 18417046Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1842789Sahrens ds->ds_phys->ds_unique_bytes = 0; 18435378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 18445378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1845789Sahrens ds->ds_phys->ds_deadlist_obj = 1846789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 18471544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 18481544Seschrock ds->ds_phys->ds_deadlist_obj)); 1849789Sahrens 1850789Sahrens dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1851789Sahrens err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1852789Sahrens snapname, 8, 1, &dsobj, tx); 1853789Sahrens ASSERT(err == 0); 1854789Sahrens 1855789Sahrens if (ds->ds_prev) 18566689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 18576689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 18586689Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 18594543Smarks 18607046Sahrens dsl_pool_ds_snapshotted(ds, tx); 18617046Sahrens 18624543Smarks spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 18634603Sahrens "dataset = %llu", dsobj); 1864789Sahrens } 1865789Sahrens 1866789Sahrens void 18673547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1868789Sahrens { 1869789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1870789Sahrens ASSERT(ds->ds_user_ptr != NULL); 1871789Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1872789Sahrens 18734787Sahrens /* 18744787Sahrens * in case we had to change ds_fsid_guid when we opened it, 18754787Sahrens * sync it out now. 18764787Sahrens */ 18774787Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 18784787Sahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 18794787Sahrens 1880789Sahrens dsl_dir_dirty(ds->ds_dir, tx); 18813547Smaybee dmu_objset_sync(ds->ds_user_ptr, zio, tx); 1882789Sahrens } 1883789Sahrens 1884789Sahrens void 18852885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1886789Sahrens { 18875378Sck153898 uint64_t refd, avail, uobjs, aobjs; 18885378Sck153898 18892885Sahrens dsl_dir_stats(ds->ds_dir, nv); 1890789Sahrens 18915378Sck153898 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 18925378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 18935378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 18945378Sck153898 18952885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 18962885Sahrens ds->ds_phys->ds_creation_time); 18972885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 18982885Sahrens ds->ds_phys->ds_creation_txg); 18995378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 19005378Sck153898 ds->ds_quota); 19015378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 19025378Sck153898 ds->ds_reserved); 19036643Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 19046643Seschrock ds->ds_phys->ds_guid); 1905789Sahrens 1906789Sahrens if (ds->ds_phys->ds_next_snap_obj) { 1907789Sahrens /* 1908789Sahrens * This is a snapshot; override the dd's space used with 19092885Sahrens * our unique space and compression ratio. 1910789Sahrens */ 19112885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 19122885Sahrens ds->ds_phys->ds_unique_bytes); 19132885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 19142885Sahrens ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 19152885Sahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 19162885Sahrens ds->ds_phys->ds_compressed_bytes)); 1917789Sahrens } 1918789Sahrens } 1919789Sahrens 19202885Sahrens void 19212885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1922789Sahrens { 19232885Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 19242885Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 19255367Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 19262885Sahrens if (ds->ds_phys->ds_next_snap_obj) { 19272885Sahrens stat->dds_is_snapshot = B_TRUE; 19282885Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 19292885Sahrens } 19302885Sahrens 19312885Sahrens /* clone origin is really a dsl_dir thing... */ 19325446Sahrens rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 19337046Sahrens if (dsl_dir_is_clone(ds->ds_dir)) { 19342885Sahrens dsl_dataset_t *ods; 19352885Sahrens 19366689Smaybee VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 19376689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 19385367Sahrens dsl_dataset_name(ods, stat->dds_origin); 19396689Smaybee dsl_dataset_drop_ref(ods, FTAG); 19402885Sahrens } 19415446Sahrens rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 19422885Sahrens } 19432885Sahrens 19442885Sahrens uint64_t 19452885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 19462885Sahrens { 19474787Sahrens return (ds->ds_fsid_guid); 19482885Sahrens } 19492885Sahrens 19502885Sahrens void 19512885Sahrens dsl_dataset_space(dsl_dataset_t *ds, 19522885Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 19532885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 19542885Sahrens { 19552885Sahrens *refdbytesp = ds->ds_phys->ds_used_bytes; 19562885Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 19575378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 19585378Sck153898 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 19595378Sck153898 if (ds->ds_quota != 0) { 19605378Sck153898 /* 19615378Sck153898 * Adjust available bytes according to refquota 19625378Sck153898 */ 19635378Sck153898 if (*refdbytesp < ds->ds_quota) 19645378Sck153898 *availbytesp = MIN(*availbytesp, 19655378Sck153898 ds->ds_quota - *refdbytesp); 19665378Sck153898 else 19675378Sck153898 *availbytesp = 0; 19685378Sck153898 } 19692885Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 19702885Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1971789Sahrens } 1972789Sahrens 19735326Sek110237 boolean_t 19745326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 19755326Sek110237 { 19765326Sek110237 dsl_pool_t *dp = ds->ds_dir->dd_pool; 19775326Sek110237 19785326Sek110237 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 19795326Sek110237 dsl_pool_sync_context(dp)); 19805326Sek110237 if (ds->ds_prev == NULL) 19815326Sek110237 return (B_FALSE); 19825326Sek110237 if (ds->ds_phys->ds_bp.blk_birth > 19835326Sek110237 ds->ds_prev->ds_phys->ds_creation_txg) 19845326Sek110237 return (B_TRUE); 19855326Sek110237 return (B_FALSE); 19865326Sek110237 } 19875326Sek110237 19882199Sahrens /* ARGSUSED */ 1989789Sahrens static int 19902199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 1991789Sahrens { 19922199Sahrens dsl_dataset_t *ds = arg1; 19932199Sahrens char *newsnapname = arg2; 19942199Sahrens dsl_dir_t *dd = ds->ds_dir; 19952199Sahrens dsl_dataset_t *hds; 19962199Sahrens uint64_t val; 1997789Sahrens int err; 1998789Sahrens 19996689Smaybee err = dsl_dataset_hold_obj(dd->dd_pool, 20006689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2001789Sahrens if (err) 2002789Sahrens return (err); 2003789Sahrens 20042199Sahrens /* new name better not be in use */ 20056689Smaybee err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 20066689Smaybee dsl_dataset_rele(hds, FTAG); 2007789Sahrens 20082199Sahrens if (err == 0) 20092199Sahrens err = EEXIST; 20102199Sahrens else if (err == ENOENT) 20112199Sahrens err = 0; 20124007Smmusante 20134007Smmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 20144007Smmusante if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 20154007Smmusante err = ENAMETOOLONG; 20164007Smmusante 20172199Sahrens return (err); 20182199Sahrens } 2019789Sahrens 20202199Sahrens static void 20214543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 20224543Smarks cred_t *cr, dmu_tx_t *tx) 20232199Sahrens { 20242199Sahrens dsl_dataset_t *ds = arg1; 20254543Smarks const char *newsnapname = arg2; 20262199Sahrens dsl_dir_t *dd = ds->ds_dir; 20272199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 20282199Sahrens dsl_dataset_t *hds; 20292199Sahrens int err; 2030789Sahrens 20312199Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2032789Sahrens 20336689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 20346689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2035789Sahrens 20362199Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 20376689Smaybee err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2038789Sahrens ASSERT3U(err, ==, 0); 20392199Sahrens mutex_enter(&ds->ds_lock); 20402199Sahrens (void) strcpy(ds->ds_snapname, newsnapname); 20412199Sahrens mutex_exit(&ds->ds_lock); 20422199Sahrens err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 20432199Sahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2044789Sahrens ASSERT3U(err, ==, 0); 2045789Sahrens 20464543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 20474543Smarks cr, "dataset = %llu", ds->ds_object); 20486689Smaybee dsl_dataset_rele(hds, FTAG); 2049789Sahrens } 2050789Sahrens 20515326Sek110237 struct renamesnaparg { 20524007Smmusante dsl_sync_task_group_t *dstg; 20534007Smmusante char failed[MAXPATHLEN]; 20544007Smmusante char *oldsnap; 20554007Smmusante char *newsnap; 20564007Smmusante }; 20574007Smmusante 20584007Smmusante static int 20594007Smmusante dsl_snapshot_rename_one(char *name, void *arg) 20604007Smmusante { 20615326Sek110237 struct renamesnaparg *ra = arg; 20624007Smmusante dsl_dataset_t *ds = NULL; 20634007Smmusante char *cp; 20644007Smmusante int err; 20654007Smmusante 20664007Smmusante cp = name + strlen(name); 20674007Smmusante *cp = '@'; 20684007Smmusante (void) strcpy(cp + 1, ra->oldsnap); 20694543Smarks 20704543Smarks /* 20714543Smarks * For recursive snapshot renames the parent won't be changing 20724543Smarks * so we just pass name for both the to/from argument. 20734543Smarks */ 20747312SMatthew.Ahrens@Sun.COM err = zfs_secpolicy_rename_perms(name, name, CRED()); 20757312SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 20767312SMatthew.Ahrens@Sun.COM return (0); 20777312SMatthew.Ahrens@Sun.COM } else if (err) { 20784543Smarks (void) strcpy(ra->failed, name); 20794543Smarks return (err); 20804543Smarks } 20814543Smarks 20826689Smaybee #ifdef _KERNEL 20836689Smaybee /* 20846689Smaybee * For all filesystems undergoing rename, we'll need to unmount it. 20856689Smaybee */ 20866689Smaybee (void) zfs_unmount_snap(name, NULL); 20876689Smaybee #endif 20886689Smaybee err = dsl_dataset_hold(name, ra->dstg, &ds); 20896689Smaybee *cp = '\0'; 20904007Smmusante if (err == ENOENT) { 20914007Smmusante return (0); 20926689Smaybee } else if (err) { 20934007Smmusante (void) strcpy(ra->failed, name); 20944007Smmusante return (err); 20954007Smmusante } 20964007Smmusante 20974007Smmusante dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 20984007Smmusante dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 20994007Smmusante 21004007Smmusante return (0); 21014007Smmusante } 21024007Smmusante 21034007Smmusante static int 21044007Smmusante dsl_recursive_rename(char *oldname, const char *newname) 21054007Smmusante { 21064007Smmusante int err; 21075326Sek110237 struct renamesnaparg *ra; 21084007Smmusante dsl_sync_task_t *dst; 21094007Smmusante spa_t *spa; 21104007Smmusante char *cp, *fsname = spa_strdup(oldname); 21114007Smmusante int len = strlen(oldname); 21124007Smmusante 21134007Smmusante /* truncate the snapshot name to get the fsname */ 21144007Smmusante cp = strchr(fsname, '@'); 21154007Smmusante *cp = '\0'; 21164007Smmusante 21174603Sahrens err = spa_open(fsname, &spa, FTAG); 21184007Smmusante if (err) { 21194007Smmusante kmem_free(fsname, len + 1); 21204007Smmusante return (err); 21214007Smmusante } 21225326Sek110237 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 21234007Smmusante ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 21244007Smmusante 21254007Smmusante ra->oldsnap = strchr(oldname, '@') + 1; 21264007Smmusante ra->newsnap = strchr(newname, '@') + 1; 21274007Smmusante *ra->failed = '\0'; 21284007Smmusante 21294007Smmusante err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 21304007Smmusante DS_FIND_CHILDREN); 21314007Smmusante kmem_free(fsname, len + 1); 21324007Smmusante 21334007Smmusante if (err == 0) { 21344007Smmusante err = dsl_sync_task_group_wait(ra->dstg); 21354007Smmusante } 21364007Smmusante 21374007Smmusante for (dst = list_head(&ra->dstg->dstg_tasks); dst; 21384007Smmusante dst = list_next(&ra->dstg->dstg_tasks, dst)) { 21394007Smmusante dsl_dataset_t *ds = dst->dst_arg1; 21404007Smmusante if (dst->dst_err) { 21414007Smmusante dsl_dir_name(ds->ds_dir, ra->failed); 21424009Smmusante (void) strcat(ra->failed, "@"); 21434009Smmusante (void) strcat(ra->failed, ra->newsnap); 21444007Smmusante } 21456689Smaybee dsl_dataset_rele(ds, ra->dstg); 21464007Smmusante } 21474007Smmusante 21484543Smarks if (err) 21494543Smarks (void) strcpy(oldname, ra->failed); 21504007Smmusante 21514007Smmusante dsl_sync_task_group_destroy(ra->dstg); 21525326Sek110237 kmem_free(ra, sizeof (struct renamesnaparg)); 21534007Smmusante spa_close(spa, FTAG); 21544007Smmusante return (err); 21554007Smmusante } 21564007Smmusante 21574569Smmusante static int 21584569Smmusante dsl_valid_rename(char *oldname, void *arg) 21594569Smmusante { 21604569Smmusante int delta = *(int *)arg; 21614569Smmusante 21624569Smmusante if (strlen(oldname) + delta >= MAXNAMELEN) 21634569Smmusante return (ENAMETOOLONG); 21644569Smmusante 21654569Smmusante return (0); 21664569Smmusante } 21674569Smmusante 2168789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename 2169789Sahrens int 21706689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2171789Sahrens { 2172789Sahrens dsl_dir_t *dd; 21732199Sahrens dsl_dataset_t *ds; 2174789Sahrens const char *tail; 2175789Sahrens int err; 2176789Sahrens 21772199Sahrens err = dsl_dir_open(oldname, FTAG, &dd, &tail); 21781544Seschrock if (err) 21791544Seschrock return (err); 2180789Sahrens if (tail == NULL) { 21814569Smmusante int delta = strlen(newname) - strlen(oldname); 21824569Smmusante 21837046Sahrens /* if we're growing, validate child name lengths */ 21844569Smmusante if (delta > 0) 21854569Smmusante err = dmu_objset_find(oldname, dsl_valid_rename, 21864569Smmusante &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 21874569Smmusante 21884569Smmusante if (!err) 21894569Smmusante err = dsl_dir_rename(dd, newname); 2190789Sahrens dsl_dir_close(dd, FTAG); 2191789Sahrens return (err); 2192789Sahrens } 2193789Sahrens if (tail[0] != '@') { 2194789Sahrens /* the name ended in a nonexistant component */ 2195789Sahrens dsl_dir_close(dd, FTAG); 2196789Sahrens return (ENOENT); 2197789Sahrens } 2198789Sahrens 21992199Sahrens dsl_dir_close(dd, FTAG); 22002199Sahrens 22012199Sahrens /* new name must be snapshot in same filesystem */ 22022199Sahrens tail = strchr(newname, '@'); 22032199Sahrens if (tail == NULL) 22042199Sahrens return (EINVAL); 22052199Sahrens tail++; 22062199Sahrens if (strncmp(oldname, newname, tail - newname) != 0) 22072199Sahrens return (EXDEV); 2208789Sahrens 22094007Smmusante if (recursive) { 22104007Smmusante err = dsl_recursive_rename(oldname, newname); 22114007Smmusante } else { 22126689Smaybee err = dsl_dataset_hold(oldname, FTAG, &ds); 22134007Smmusante if (err) 22144007Smmusante return (err); 22152199Sahrens 22164007Smmusante err = dsl_sync_task_do(ds->ds_dir->dd_pool, 22174007Smmusante dsl_dataset_snapshot_rename_check, 22184007Smmusante dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 22192199Sahrens 22206689Smaybee dsl_dataset_rele(ds, FTAG); 22214007Smmusante } 22222199Sahrens 2223789Sahrens return (err); 2224789Sahrens } 22252082Seschrock 22267046Sahrens struct promotenode { 22276689Smaybee list_node_t link; 22286689Smaybee dsl_dataset_t *ds; 22296689Smaybee }; 22306689Smaybee 22312199Sahrens struct promotearg { 22326689Smaybee list_t snap_list; 22336689Smaybee dsl_dataset_t *clone_origin, *old_head; 22342199Sahrens uint64_t used, comp, uncomp, unique; 22356689Smaybee uint64_t newnext_obj; 22362199Sahrens }; 22372199Sahrens 22384543Smarks /* ARGSUSED */ 22392082Seschrock static int 22402199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 22412082Seschrock { 22422199Sahrens dsl_dataset_t *hds = arg1; 22432199Sahrens struct promotearg *pa = arg2; 22447046Sahrens struct promotenode *snap = list_head(&pa->snap_list); 22452199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 22466689Smaybee dsl_dataset_t *origin_ds = snap->ds; 22476689Smaybee dsl_dataset_t *newnext_ds; 22486689Smaybee char *name; 22492199Sahrens uint64_t itor = 0; 22502082Seschrock blkptr_t bp; 22516689Smaybee int err; 22522199Sahrens 22537046Sahrens /* Check that it is a real clone */ 22547046Sahrens if (!dsl_dir_is_clone(hds->ds_dir)) 22552082Seschrock return (EINVAL); 22562082Seschrock 22572199Sahrens /* Since this is so expensive, don't do the preliminary check */ 22582199Sahrens if (!dmu_tx_is_syncing(tx)) 22592199Sahrens return (0); 22602199Sahrens 22616689Smaybee if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 22626689Smaybee return (EXDEV); 22632082Seschrock 22645367Sahrens /* find origin's new next ds */ 22656689Smaybee newnext_ds = hds; 22665367Sahrens while (newnext_ds->ds_phys->ds_prev_snap_obj != origin_ds->ds_object) { 22672082Seschrock dsl_dataset_t *prev; 22682082Seschrock 22696689Smaybee err = dsl_dataset_hold_obj(dp, 22706689Smaybee newnext_ds->ds_phys->ds_prev_snap_obj, FTAG, &prev); 22716689Smaybee if (newnext_ds != hds) 22726689Smaybee dsl_dataset_rele(newnext_ds, FTAG); 22736689Smaybee if (err) 22746689Smaybee return (err); 22752082Seschrock newnext_ds = prev; 22762082Seschrock } 22772199Sahrens pa->newnext_obj = newnext_ds->ds_object; 22782082Seschrock 22795367Sahrens /* compute origin's new unique space */ 22806689Smaybee pa->unique = 0; 22812082Seschrock while ((err = bplist_iterate(&newnext_ds->ds_deadlist, 22822082Seschrock &itor, &bp)) == 0) { 22835367Sahrens if (bp.blk_birth > origin_ds->ds_phys->ds_prev_snap_txg) 22846689Smaybee pa->unique += bp_get_dasize(dp->dp_spa, &bp); 22852082Seschrock } 22866689Smaybee if (newnext_ds != hds) 22876689Smaybee dsl_dataset_rele(newnext_ds, FTAG); 22882082Seschrock if (err != ENOENT) 22896689Smaybee return (err); 22906689Smaybee 22912082Seschrock name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 22926689Smaybee 22936689Smaybee /* 22946689Smaybee * Walk the snapshots that we are moving 22956689Smaybee * 22966689Smaybee * Compute space to transfer. Each snapshot gave birth to: 22976689Smaybee * (my used) - (prev's used) + (deadlist's used) 22986689Smaybee * So a sequence would look like: 22996689Smaybee * uN - u(N-1) + dN + ... + u1 - u0 + d1 + u0 - 0 + d0 23006689Smaybee * Which simplifies to: 23016689Smaybee * uN + dN + ... + d1 + d0 23026689Smaybee * Note however, if we stop before we reach the ORIGIN we get: 23036689Smaybee * uN + dN + ... + dM - uM-1 23046689Smaybee */ 23056689Smaybee pa->used = origin_ds->ds_phys->ds_used_bytes; 23066689Smaybee pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 23076689Smaybee pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 23086689Smaybee do { 23092082Seschrock uint64_t val, dlused, dlcomp, dluncomp; 23106689Smaybee dsl_dataset_t *ds = snap->ds; 23112082Seschrock 23122082Seschrock /* Check that the snapshot name does not conflict */ 23132082Seschrock dsl_dataset_name(ds, name); 23146689Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 23156689Smaybee if (err == 0) 23166689Smaybee err = EEXIST; 23176689Smaybee if (err != ENOENT) 23182082Seschrock break; 23196689Smaybee err = 0; 23206689Smaybee 23216689Smaybee /* The very first snapshot does not have a deadlist */ 23226689Smaybee if (ds->ds_phys->ds_prev_snap_obj != 0) { 23236689Smaybee if (err = bplist_space(&ds->ds_deadlist, 23246689Smaybee &dlused, &dlcomp, &dluncomp)) 23256689Smaybee break; 23266689Smaybee pa->used += dlused; 23276689Smaybee pa->comp += dlcomp; 23286689Smaybee pa->uncomp += dluncomp; 23292082Seschrock } 23306689Smaybee } while (snap = list_next(&pa->snap_list, snap)); 23316689Smaybee 23326689Smaybee /* 23336689Smaybee * If we are a clone of a clone then we never reached ORIGIN, 23346689Smaybee * so we need to subtract out the clone origin's used space. 23356689Smaybee */ 23366689Smaybee if (pa->clone_origin) { 23376689Smaybee pa->used -= pa->clone_origin->ds_phys->ds_used_bytes; 23386689Smaybee pa->comp -= pa->clone_origin->ds_phys->ds_compressed_bytes; 23396689Smaybee pa->uncomp -= pa->clone_origin->ds_phys->ds_uncompressed_bytes; 23402082Seschrock } 23412082Seschrock 23426689Smaybee kmem_free(name, MAXPATHLEN); 23436689Smaybee 23442082Seschrock /* Check that there is enough space here */ 23456689Smaybee if (err == 0) { 23466689Smaybee dsl_dir_t *odd = origin_ds->ds_dir; 23476689Smaybee err = dsl_dir_transfer_possible(odd, hds->ds_dir, pa->used); 23486689Smaybee } 23496689Smaybee 23502199Sahrens return (err); 23512199Sahrens } 23522082Seschrock 23532199Sahrens static void 23544543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 23552199Sahrens { 23562199Sahrens dsl_dataset_t *hds = arg1; 23572199Sahrens struct promotearg *pa = arg2; 23587046Sahrens struct promotenode *snap = list_head(&pa->snap_list); 23596689Smaybee dsl_dataset_t *origin_ds = snap->ds; 23602199Sahrens dsl_dir_t *dd = hds->ds_dir; 23612199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 23625367Sahrens dsl_dir_t *odd = NULL; 23632199Sahrens char *name; 23647046Sahrens uint64_t oldnext_obj; 23652199Sahrens 23662199Sahrens ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 23672199Sahrens 23682417Sahrens /* 23695367Sahrens * We need to explicitly open odd, since origin_ds's dd will be 23702417Sahrens * changing. 23712417Sahrens */ 23725367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 23735367Sahrens NULL, FTAG, &odd)); 23742082Seschrock 23756689Smaybee /* change origin's next snap */ 23766689Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 23777046Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 23786689Smaybee origin_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj; 23796689Smaybee 23807046Sahrens /* change the origin's next clone */ 23817046Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 23827046Sahrens VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 23837046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 23847046Sahrens pa->newnext_obj, tx)); 23857046Sahrens VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 23867046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 23877046Sahrens oldnext_obj, tx)); 23887046Sahrens } 23897046Sahrens 23906689Smaybee /* change origin */ 23916689Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 23926689Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 23936689Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 23946689Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 23956689Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 23966689Smaybee 23972082Seschrock /* move snapshots to this dir */ 23982199Sahrens name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 23996689Smaybee do { 24006689Smaybee dsl_dataset_t *ds = snap->ds; 24012082Seschrock 24027237Sek110237 /* unregister props as dsl_dir is changing */ 24037237Sek110237 if (ds->ds_user_ptr) { 24047237Sek110237 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 24057237Sek110237 ds->ds_user_ptr = NULL; 24067237Sek110237 } 24072082Seschrock /* move snap name entry */ 24082082Seschrock dsl_dataset_name(ds, name); 24096689Smaybee VERIFY(0 == dsl_dataset_snap_remove(pa->old_head, 24106689Smaybee ds->ds_snapname, tx)); 24112199Sahrens VERIFY(0 == zap_add(dp->dp_meta_objset, 24122082Seschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 24132082Seschrock 8, 1, &ds->ds_object, tx)); 24142082Seschrock /* change containing dsl_dir */ 24152082Seschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 24165367Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 24172082Seschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 24185367Sahrens ASSERT3P(ds->ds_dir, ==, odd); 24192082Seschrock dsl_dir_close(ds->ds_dir, ds); 24202199Sahrens VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 24212082Seschrock NULL, ds, &ds->ds_dir)); 24222082Seschrock 24232082Seschrock ASSERT3U(dsl_prop_numcb(ds), ==, 0); 24246689Smaybee } while (snap = list_next(&pa->snap_list, snap)); 24252082Seschrock 24262082Seschrock /* change space accounting */ 24275367Sahrens dsl_dir_diduse_space(odd, -pa->used, -pa->comp, -pa->uncomp, tx); 24282199Sahrens dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx); 24295367Sahrens origin_ds->ds_phys->ds_unique_bytes = pa->unique; 24302082Seschrock 24314543Smarks /* log history record */ 24324543Smarks spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 24336689Smaybee cr, "dataset = %llu", hds->ds_object); 24344543Smarks 24355367Sahrens dsl_dir_close(odd, FTAG); 24362199Sahrens kmem_free(name, MAXPATHLEN); 24372082Seschrock } 24382082Seschrock 24392082Seschrock int 24402082Seschrock dsl_dataset_promote(const char *name) 24412082Seschrock { 24422082Seschrock dsl_dataset_t *ds; 24436689Smaybee dsl_dir_t *dd; 24446689Smaybee dsl_pool_t *dp; 24452082Seschrock dmu_object_info_t doi; 24462199Sahrens struct promotearg pa; 24477046Sahrens struct promotenode *snap; 24486689Smaybee uint64_t snap_obj; 24496689Smaybee uint64_t last_snap = 0; 24506689Smaybee int err; 24516689Smaybee 24526689Smaybee err = dsl_dataset_hold(name, FTAG, &ds); 24532082Seschrock if (err) 24542082Seschrock return (err); 24556689Smaybee dd = ds->ds_dir; 24566689Smaybee dp = dd->dd_pool; 24576689Smaybee 24586689Smaybee err = dmu_object_info(dp->dp_meta_objset, 24592082Seschrock ds->ds_phys->ds_snapnames_zapobj, &doi); 24602082Seschrock if (err) { 24616689Smaybee dsl_dataset_rele(ds, FTAG); 24622082Seschrock return (err); 24632082Seschrock } 24642082Seschrock 24652082Seschrock /* 24666689Smaybee * We are going to inherit all the snapshots taken before our 24676689Smaybee * origin (i.e., our new origin will be our parent's origin). 24686689Smaybee * Take ownership of them so that we can rename them into our 24696689Smaybee * namespace. 24706689Smaybee */ 24716689Smaybee pa.clone_origin = NULL; 24726689Smaybee list_create(&pa.snap_list, 24737046Sahrens sizeof (struct promotenode), offsetof(struct promotenode, link)); 24746689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 24756689Smaybee ASSERT(dd->dd_phys->dd_origin_obj != 0); 24766689Smaybee snap_obj = dd->dd_phys->dd_origin_obj; 24776689Smaybee while (snap_obj) { 24787046Sahrens dsl_dataset_t *snapds; 24797046Sahrens 24807046Sahrens /* 24817046Sahrens * NB: this would be handled by the below check for 24827046Sahrens * clone of a clone, but then we'd always own_obj() the 24837046Sahrens * $ORIGIN, thus causing unnecessary EBUSYs. We don't 24847046Sahrens * need to set pa.clone_origin because the $ORIGIN has 24857046Sahrens * no data to account for. 24867046Sahrens */ 24877046Sahrens if (dp->dp_origin_snap && 24887046Sahrens snap_obj == dp->dp_origin_snap->ds_object) 24897046Sahrens break; 24907046Sahrens 24917046Sahrens err = dsl_dataset_own_obj(dp, snap_obj, 0, FTAG, &snapds); 24926689Smaybee if (err == ENOENT) { 24936689Smaybee /* lost race with snapshot destroy */ 24947046Sahrens struct promotenode *last = list_tail(&pa.snap_list); 24956689Smaybee ASSERT(snap_obj != last->ds->ds_phys->ds_prev_snap_obj); 24966689Smaybee snap_obj = last->ds->ds_phys->ds_prev_snap_obj; 24976689Smaybee continue; 24986689Smaybee } else if (err) { 24996689Smaybee rw_exit(&dp->dp_config_rwlock); 25006689Smaybee goto out; 25016689Smaybee } 25027046Sahrens 25036689Smaybee /* 25046689Smaybee * We could be a clone of a clone. If we reach our 25056689Smaybee * parent's branch point, we're done. 25066689Smaybee */ 25076689Smaybee if (last_snap && 25087046Sahrens snapds->ds_phys->ds_next_snap_obj != last_snap) { 25097046Sahrens pa.clone_origin = snapds; 25107046Sahrens break; 25116689Smaybee } 25127046Sahrens 25137046Sahrens snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 25147046Sahrens snap->ds = snapds; 25157046Sahrens list_insert_tail(&pa.snap_list, snap); 25167046Sahrens last_snap = snap_obj; 25177046Sahrens snap_obj = snap->ds->ds_phys->ds_prev_snap_obj; 2518*7385SMark.Maybee@Sun.COM dsl_dataset_make_exclusive(snapds, FTAG); 25196689Smaybee } 25206689Smaybee snap = list_head(&pa.snap_list); 25216689Smaybee ASSERT(snap != NULL); 25226689Smaybee err = dsl_dataset_hold_obj(dp, 25236689Smaybee snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &pa.old_head); 25246689Smaybee rw_exit(&dp->dp_config_rwlock); 25256689Smaybee 25266689Smaybee if (err) 25276689Smaybee goto out; 25286689Smaybee 25296689Smaybee /* 25302082Seschrock * Add in 128x the snapnames zapobj size, since we will be moving 25312082Seschrock * a bunch of snapnames to the promoted ds, and dirtying their 25322082Seschrock * bonus buffers. 25332082Seschrock */ 25346689Smaybee err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 25352199Sahrens dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks); 25366689Smaybee 25376689Smaybee dsl_dataset_rele(pa.old_head, FTAG); 25386689Smaybee out: 25396689Smaybee while ((snap = list_tail(&pa.snap_list)) != NULL) { 25406689Smaybee list_remove(&pa.snap_list, snap); 25416689Smaybee dsl_dataset_disown(snap->ds, FTAG); 25427046Sahrens kmem_free(snap, sizeof (struct promotenode)); 25436689Smaybee } 25446689Smaybee list_destroy(&pa.snap_list); 25456689Smaybee if (pa.clone_origin) 25466689Smaybee dsl_dataset_disown(pa.clone_origin, FTAG); 25476689Smaybee dsl_dataset_rele(ds, FTAG); 25482082Seschrock return (err); 25492082Seschrock } 25503912Slling 25515367Sahrens struct cloneswaparg { 25525367Sahrens dsl_dataset_t *cds; /* clone dataset */ 25535367Sahrens dsl_dataset_t *ohds; /* origin's head dataset */ 25545367Sahrens boolean_t force; 25555481Sck153898 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 25565367Sahrens }; 25575326Sek110237 25585326Sek110237 /* ARGSUSED */ 25595326Sek110237 static int 25605326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 25615326Sek110237 { 25625367Sahrens struct cloneswaparg *csa = arg1; 25635326Sek110237 25645367Sahrens /* they should both be heads */ 25655367Sahrens if (dsl_dataset_is_snapshot(csa->cds) || 25665367Sahrens dsl_dataset_is_snapshot(csa->ohds)) 25675326Sek110237 return (EINVAL); 25685326Sek110237 25695367Sahrens /* the branch point should be just before them */ 25705367Sahrens if (csa->cds->ds_prev != csa->ohds->ds_prev) 25715326Sek110237 return (EINVAL); 25725326Sek110237 25735367Sahrens /* cds should be the clone */ 25745367Sahrens if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj != 25755367Sahrens csa->ohds->ds_object) 25765367Sahrens return (EINVAL); 25775326Sek110237 25785367Sahrens /* the clone should be a child of the origin */ 25795367Sahrens if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 25805367Sahrens return (EINVAL); 25815326Sek110237 25825367Sahrens /* ohds shouldn't be modified unless 'force' */ 25835367Sahrens if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 25845367Sahrens return (ETXTBSY); 25855481Sck153898 25865481Sck153898 /* adjust amount of any unconsumed refreservation */ 25875481Sck153898 csa->unused_refres_delta = 25885481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 25895481Sck153898 csa->ohds->ds_phys->ds_unique_bytes) - 25905481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 25915481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 25925481Sck153898 25935481Sck153898 if (csa->unused_refres_delta > 0 && 25945481Sck153898 csa->unused_refres_delta > 25955481Sck153898 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 25965481Sck153898 return (ENOSPC); 25975481Sck153898 25985367Sahrens return (0); 25995326Sek110237 } 26005326Sek110237 26015326Sek110237 /* ARGSUSED */ 26025326Sek110237 static void 26035326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 26045326Sek110237 { 26055367Sahrens struct cloneswaparg *csa = arg1; 26065367Sahrens dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 26075326Sek110237 uint64_t itor = 0; 26085326Sek110237 blkptr_t bp; 26095326Sek110237 uint64_t unique = 0; 26105326Sek110237 int err; 26115326Sek110237 26125481Sck153898 ASSERT(csa->cds->ds_reserved == 0); 26135481Sck153898 ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota); 26145481Sck153898 26155367Sahrens dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 26165367Sahrens dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 26175367Sahrens dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx); 26185326Sek110237 26195367Sahrens if (csa->cds->ds_user_ptr != NULL) { 26205367Sahrens csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr); 26215367Sahrens csa->cds->ds_user_ptr = NULL; 26225367Sahrens } 26235326Sek110237 26245367Sahrens if (csa->ohds->ds_user_ptr != NULL) { 26255367Sahrens csa->ohds->ds_user_evict_func(csa->ohds, 26265367Sahrens csa->ohds->ds_user_ptr); 26275367Sahrens csa->ohds->ds_user_ptr = NULL; 26285367Sahrens } 26295326Sek110237 26305326Sek110237 /* compute unique space */ 26315367Sahrens while ((err = bplist_iterate(&csa->cds->ds_deadlist, 26325367Sahrens &itor, &bp)) == 0) { 26335367Sahrens if (bp.blk_birth > csa->cds->ds_prev->ds_phys->ds_prev_snap_txg) 26345367Sahrens unique += bp_get_dasize(dp->dp_spa, &bp); 26355326Sek110237 } 26365326Sek110237 VERIFY(err == ENOENT); 26375326Sek110237 26385326Sek110237 /* reset origin's unique bytes */ 26395367Sahrens csa->cds->ds_prev->ds_phys->ds_unique_bytes = unique; 26405326Sek110237 26415326Sek110237 /* swap blkptrs */ 26425326Sek110237 { 26435326Sek110237 blkptr_t tmp; 26445367Sahrens tmp = csa->ohds->ds_phys->ds_bp; 26455367Sahrens csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 26465367Sahrens csa->cds->ds_phys->ds_bp = tmp; 26475326Sek110237 } 26485326Sek110237 26495326Sek110237 /* set dd_*_bytes */ 26505326Sek110237 { 26515326Sek110237 int64_t dused, dcomp, duncomp; 26525326Sek110237 uint64_t cdl_used, cdl_comp, cdl_uncomp; 26535326Sek110237 uint64_t odl_used, odl_comp, odl_uncomp; 26545326Sek110237 26555367Sahrens VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 26565326Sek110237 &cdl_comp, &cdl_uncomp)); 26575367Sahrens VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 26585326Sek110237 &odl_comp, &odl_uncomp)); 26595367Sahrens dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 26605367Sahrens (csa->ohds->ds_phys->ds_used_bytes + odl_used); 26615367Sahrens dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 26625367Sahrens (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 26635367Sahrens duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 26645367Sahrens cdl_uncomp - 26655367Sahrens (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 26665326Sek110237 26675367Sahrens dsl_dir_diduse_space(csa->ohds->ds_dir, 26685367Sahrens dused, dcomp, duncomp, tx); 26695367Sahrens dsl_dir_diduse_space(csa->cds->ds_dir, 26705367Sahrens -dused, -dcomp, -duncomp, tx); 26715367Sahrens } 26725367Sahrens 26735367Sahrens #define SWITCH64(x, y) \ 26745367Sahrens { \ 26755367Sahrens uint64_t __tmp = (x); \ 26765367Sahrens (x) = (y); \ 26775367Sahrens (y) = __tmp; \ 26785326Sek110237 } 26795326Sek110237 26805326Sek110237 /* swap ds_*_bytes */ 26815367Sahrens SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 26825367Sahrens csa->cds->ds_phys->ds_used_bytes); 26835367Sahrens SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 26845367Sahrens csa->cds->ds_phys->ds_compressed_bytes); 26855367Sahrens SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 26865367Sahrens csa->cds->ds_phys->ds_uncompressed_bytes); 26875481Sck153898 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 26885481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 26895481Sck153898 26905481Sck153898 /* apply any parent delta for change in unconsumed refreservation */ 26915481Sck153898 dsl_dir_diduse_space(csa->ohds->ds_dir, csa->unused_refres_delta, 26925481Sck153898 0, 0, tx); 26935326Sek110237 26945326Sek110237 /* swap deadlists */ 26955367Sahrens bplist_close(&csa->cds->ds_deadlist); 26965367Sahrens bplist_close(&csa->ohds->ds_deadlist); 26975367Sahrens SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 26985367Sahrens csa->cds->ds_phys->ds_deadlist_obj); 26995367Sahrens VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 27005367Sahrens csa->cds->ds_phys->ds_deadlist_obj)); 27015367Sahrens VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 27025367Sahrens csa->ohds->ds_phys->ds_deadlist_obj)); 27035326Sek110237 } 27045326Sek110237 27055326Sek110237 /* 27066689Smaybee * Swap 'clone' with its origin head file system. Used at the end 27076689Smaybee * of "online recv" to swizzle the file system to the new version. 27085326Sek110237 */ 27095326Sek110237 int 27105367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 27115367Sahrens boolean_t force) 27125326Sek110237 { 27135367Sahrens struct cloneswaparg csa; 27146689Smaybee int error; 27156689Smaybee 27166689Smaybee ASSERT(clone->ds_owner); 27176689Smaybee ASSERT(origin_head->ds_owner); 27186689Smaybee retry: 27196689Smaybee /* Need exclusive access for the swap */ 27206689Smaybee rw_enter(&clone->ds_rwlock, RW_WRITER); 27216689Smaybee if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 27226689Smaybee rw_exit(&clone->ds_rwlock); 27236689Smaybee rw_enter(&origin_head->ds_rwlock, RW_WRITER); 27246689Smaybee if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 27256689Smaybee rw_exit(&origin_head->ds_rwlock); 27266689Smaybee goto retry; 27276689Smaybee } 27286689Smaybee } 27295367Sahrens csa.cds = clone; 27305367Sahrens csa.ohds = origin_head; 27315367Sahrens csa.force = force; 27326689Smaybee error = dsl_sync_task_do(clone->ds_dir->dd_pool, 27335326Sek110237 dsl_dataset_clone_swap_check, 27346689Smaybee dsl_dataset_clone_swap_sync, &csa, NULL, 9); 27356689Smaybee return (error); 27365326Sek110237 } 27375326Sek110237 27383912Slling /* 27393912Slling * Given a pool name and a dataset object number in that pool, 27403912Slling * return the name of that dataset. 27413912Slling */ 27423912Slling int 27433912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 27443912Slling { 27453912Slling spa_t *spa; 27463912Slling dsl_pool_t *dp; 27476689Smaybee dsl_dataset_t *ds; 27483912Slling int error; 27493912Slling 27503912Slling if ((error = spa_open(pname, &spa, FTAG)) != 0) 27513912Slling return (error); 27523912Slling dp = spa_get_dsl(spa); 27533912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 27546689Smaybee if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 27556689Smaybee dsl_dataset_name(ds, buf); 27566689Smaybee dsl_dataset_rele(ds, FTAG); 27573912Slling } 27583912Slling rw_exit(&dp->dp_config_rwlock); 27593912Slling spa_close(spa, FTAG); 27603912Slling 27616689Smaybee return (error); 27623912Slling } 27635378Sck153898 27645378Sck153898 int 27655378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 27666689Smaybee uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 27675378Sck153898 { 27685378Sck153898 int error = 0; 27695378Sck153898 27705378Sck153898 ASSERT3S(asize, >, 0); 27715378Sck153898 27725831Sck153898 /* 27735831Sck153898 * *ref_rsrv is the portion of asize that will come from any 27745831Sck153898 * unconsumed refreservation space. 27755831Sck153898 */ 27765831Sck153898 *ref_rsrv = 0; 27775831Sck153898 27785378Sck153898 mutex_enter(&ds->ds_lock); 27795378Sck153898 /* 27805378Sck153898 * Make a space adjustment for reserved bytes. 27815378Sck153898 */ 27825378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 27835378Sck153898 ASSERT3U(*used, >=, 27845378Sck153898 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 27855378Sck153898 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 27865831Sck153898 *ref_rsrv = 27875831Sck153898 asize - MIN(asize, parent_delta(ds, asize + inflight)); 27885378Sck153898 } 27895378Sck153898 27905378Sck153898 if (!check_quota || ds->ds_quota == 0) { 27915378Sck153898 mutex_exit(&ds->ds_lock); 27925378Sck153898 return (0); 27935378Sck153898 } 27945378Sck153898 /* 27955378Sck153898 * If they are requesting more space, and our current estimate 27965378Sck153898 * is over quota, they get to try again unless the actual 27975378Sck153898 * on-disk is over quota and there are no pending changes (which 27985378Sck153898 * may free up space for us). 27995378Sck153898 */ 28005378Sck153898 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 28015378Sck153898 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 28025378Sck153898 error = ERESTART; 28035378Sck153898 else 28045378Sck153898 error = EDQUOT; 28055378Sck153898 } 28065378Sck153898 mutex_exit(&ds->ds_lock); 28075378Sck153898 28085378Sck153898 return (error); 28095378Sck153898 } 28105378Sck153898 28115378Sck153898 /* ARGSUSED */ 28125378Sck153898 static int 28135378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 28145378Sck153898 { 28155378Sck153898 dsl_dataset_t *ds = arg1; 28165378Sck153898 uint64_t *quotap = arg2; 28175378Sck153898 uint64_t new_quota = *quotap; 28185378Sck153898 28195378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 28205378Sck153898 return (ENOTSUP); 28215378Sck153898 28225378Sck153898 if (new_quota == 0) 28235378Sck153898 return (0); 28245378Sck153898 28255378Sck153898 if (new_quota < ds->ds_phys->ds_used_bytes || 28265378Sck153898 new_quota < ds->ds_reserved) 28275378Sck153898 return (ENOSPC); 28285378Sck153898 28295378Sck153898 return (0); 28305378Sck153898 } 28315378Sck153898 28325378Sck153898 /* ARGSUSED */ 28335378Sck153898 void 28345378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 28355378Sck153898 { 28365378Sck153898 dsl_dataset_t *ds = arg1; 28375378Sck153898 uint64_t *quotap = arg2; 28385378Sck153898 uint64_t new_quota = *quotap; 28395378Sck153898 28405378Sck153898 dmu_buf_will_dirty(ds->ds_dbuf, tx); 28415378Sck153898 28425378Sck153898 ds->ds_quota = new_quota; 28435378Sck153898 28445378Sck153898 dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx); 28455378Sck153898 28465378Sck153898 spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa, 28475378Sck153898 tx, cr, "%lld dataset = %llu ", 28486689Smaybee (longlong_t)new_quota, ds->ds_object); 28495378Sck153898 } 28505378Sck153898 28515378Sck153898 int 28525378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota) 28535378Sck153898 { 28545378Sck153898 dsl_dataset_t *ds; 28555378Sck153898 int err; 28565378Sck153898 28576689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 28585378Sck153898 if (err) 28595378Sck153898 return (err); 28605378Sck153898 28615481Sck153898 if (quota != ds->ds_quota) { 28625481Sck153898 /* 28635481Sck153898 * If someone removes a file, then tries to set the quota, we 28645481Sck153898 * want to make sure the file freeing takes effect. 28655481Sck153898 */ 28665481Sck153898 txg_wait_open(ds->ds_dir->dd_pool, 0); 28675481Sck153898 28685481Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 28695481Sck153898 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 28705481Sck153898 ds, "a, 0); 28715481Sck153898 } 28726689Smaybee dsl_dataset_rele(ds, FTAG); 28735378Sck153898 return (err); 28745378Sck153898 } 28755378Sck153898 28765378Sck153898 static int 28775378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 28785378Sck153898 { 28795378Sck153898 dsl_dataset_t *ds = arg1; 28805378Sck153898 uint64_t *reservationp = arg2; 28815378Sck153898 uint64_t new_reservation = *reservationp; 28825378Sck153898 int64_t delta; 28835378Sck153898 uint64_t unique; 28845378Sck153898 28855378Sck153898 if (new_reservation > INT64_MAX) 28865378Sck153898 return (EOVERFLOW); 28875378Sck153898 28885378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 28895378Sck153898 SPA_VERSION_REFRESERVATION) 28905378Sck153898 return (ENOTSUP); 28915378Sck153898 28925378Sck153898 if (dsl_dataset_is_snapshot(ds)) 28935378Sck153898 return (EINVAL); 28945378Sck153898 28955378Sck153898 /* 28965378Sck153898 * If we are doing the preliminary check in open context, the 28975378Sck153898 * space estimates may be inaccurate. 28985378Sck153898 */ 28995378Sck153898 if (!dmu_tx_is_syncing(tx)) 29005378Sck153898 return (0); 29015378Sck153898 29025378Sck153898 mutex_enter(&ds->ds_lock); 29035378Sck153898 unique = dsl_dataset_unique(ds); 29045378Sck153898 delta = MAX(unique, new_reservation) - MAX(unique, ds->ds_reserved); 29055378Sck153898 mutex_exit(&ds->ds_lock); 29065378Sck153898 29075378Sck153898 if (delta > 0 && 29085378Sck153898 delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 29095378Sck153898 return (ENOSPC); 29105378Sck153898 if (delta > 0 && ds->ds_quota > 0 && 29115378Sck153898 new_reservation > ds->ds_quota) 29125378Sck153898 return (ENOSPC); 29135378Sck153898 29145378Sck153898 return (0); 29155378Sck153898 } 29165378Sck153898 29175378Sck153898 /* ARGSUSED */ 29185378Sck153898 static void 29195378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 29205378Sck153898 dmu_tx_t *tx) 29215378Sck153898 { 29225378Sck153898 dsl_dataset_t *ds = arg1; 29235378Sck153898 uint64_t *reservationp = arg2; 29245378Sck153898 uint64_t new_reservation = *reservationp; 29255378Sck153898 uint64_t unique; 29265378Sck153898 int64_t delta; 29275378Sck153898 29285378Sck153898 dmu_buf_will_dirty(ds->ds_dbuf, tx); 29295378Sck153898 29305378Sck153898 mutex_enter(&ds->ds_lock); 29315378Sck153898 unique = dsl_dataset_unique(ds); 29325378Sck153898 delta = MAX(0, (int64_t)(new_reservation - unique)) - 29335378Sck153898 MAX(0, (int64_t)(ds->ds_reserved - unique)); 29345378Sck153898 ds->ds_reserved = new_reservation; 29355378Sck153898 mutex_exit(&ds->ds_lock); 29365378Sck153898 29375378Sck153898 dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation", 29385378Sck153898 new_reservation, cr, tx); 29395378Sck153898 29405378Sck153898 dsl_dir_diduse_space(ds->ds_dir, delta, 0, 0, tx); 29415378Sck153898 29425378Sck153898 spa_history_internal_log(LOG_DS_REFRESERV, 29435378Sck153898 ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 29445378Sck153898 (longlong_t)new_reservation, 29455378Sck153898 ds->ds_dir->dd_phys->dd_head_dataset_obj); 29465378Sck153898 } 29475378Sck153898 29485378Sck153898 int 29495378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation) 29505378Sck153898 { 29515378Sck153898 dsl_dataset_t *ds; 29525378Sck153898 int err; 29535378Sck153898 29546689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 29555378Sck153898 if (err) 29565378Sck153898 return (err); 29575378Sck153898 29585378Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 29595378Sck153898 dsl_dataset_set_reservation_check, 29605378Sck153898 dsl_dataset_set_reservation_sync, ds, &reservation, 0); 29616689Smaybee dsl_dataset_rele(ds, FTAG); 29625378Sck153898 return (err); 29635378Sck153898 } 2964