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 /* 228517SEric.Taylor@Sun.COM * Copyright 2009 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 */ 1007390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 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); 1067595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 107789Sahrens mutex_enter(&ds->ds_lock); 1085378Sck153898 delta = parent_delta(ds, used); 109789Sahrens ds->ds_phys->ds_used_bytes += used; 110789Sahrens ds->ds_phys->ds_compressed_bytes += compressed; 111789Sahrens ds->ds_phys->ds_uncompressed_bytes += uncompressed; 112789Sahrens ds->ds_phys->ds_unique_bytes += used; 113789Sahrens mutex_exit(&ds->ds_lock); 1147390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 1157390SMatthew.Ahrens@Sun.COM compressed, uncompressed, tx); 1167390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used - delta, 1177390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1187595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 119789Sahrens } 120789Sahrens 1216992Smaybee int 1223547Smaybee dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio, 1233547Smaybee dmu_tx_t *tx) 124789Sahrens { 1252082Seschrock int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 126789Sahrens int compressed = BP_GET_PSIZE(bp); 127789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 128789Sahrens 1297754SJeff.Bonwick@Sun.COM ASSERT(pio != NULL); 130789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1313547Smaybee /* No block pointer => nothing to free */ 132789Sahrens if (BP_IS_HOLE(bp)) 1336992Smaybee return (0); 134789Sahrens 135789Sahrens ASSERT(used > 0); 136789Sahrens if (ds == NULL) { 1373547Smaybee int err; 138789Sahrens /* 139789Sahrens * Account for the meta-objset space in its placeholder 140789Sahrens * dataset. 141789Sahrens */ 1427046Sahrens err = dsl_free(pio, tx->tx_pool, 1437754SJeff.Bonwick@Sun.COM tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT); 1443547Smaybee ASSERT(err == 0); 145789Sahrens 1467390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 147789Sahrens -used, -compressed, -uncompressed, tx); 148789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 1496992Smaybee return (used); 150789Sahrens } 151789Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 152789Sahrens 1537390SMatthew.Ahrens@Sun.COM ASSERT(!dsl_dataset_is_snapshot(ds)); 154789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 155789Sahrens 156789Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 1573547Smaybee int err; 1585378Sck153898 int64_t delta; 1593547Smaybee 160789Sahrens dprintf_bp(bp, "freeing: %s", ""); 1617046Sahrens err = dsl_free(pio, tx->tx_pool, 1627754SJeff.Bonwick@Sun.COM tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT); 1633547Smaybee ASSERT(err == 0); 164789Sahrens 1657595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 166789Sahrens mutex_enter(&ds->ds_lock); 1675378Sck153898 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 1685378Sck153898 !DS_UNIQUE_IS_ACCURATE(ds)); 1695378Sck153898 delta = parent_delta(ds, -used); 170789Sahrens ds->ds_phys->ds_unique_bytes -= used; 171789Sahrens mutex_exit(&ds->ds_lock); 1727390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 1735378Sck153898 delta, -compressed, -uncompressed, tx); 1747390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, -used - delta, 1757390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1767595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 177789Sahrens } else { 178789Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 1791544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 1805712Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 1815712Sahrens ds->ds_phys->ds_prev_snap_obj); 1825712Sahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 183789Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 1845712Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 1855712Sahrens ds->ds_object && bp->blk_birth > 1865712Sahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 1875712Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1885712Sahrens mutex_enter(&ds->ds_prev->ds_lock); 1895712Sahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 1905712Sahrens mutex_exit(&ds->ds_prev->ds_lock); 191789Sahrens } 1927390SMatthew.Ahrens@Sun.COM if (bp->blk_birth > ds->ds_origin_txg) { 1937390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used, 1947390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 1957390SMatthew.Ahrens@Sun.COM } 196789Sahrens } 197789Sahrens mutex_enter(&ds->ds_lock); 198789Sahrens ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 199789Sahrens ds->ds_phys->ds_used_bytes -= used; 200789Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 201789Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 202789Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 203789Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 204789Sahrens mutex_exit(&ds->ds_lock); 2056992Smaybee 2066992Smaybee return (used); 207789Sahrens } 208789Sahrens 2091544Seschrock uint64_t 2101544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 211789Sahrens { 2122885Sahrens uint64_t trysnap = 0; 2132885Sahrens 214789Sahrens if (ds == NULL) 2151544Seschrock return (0); 216789Sahrens /* 217789Sahrens * The snapshot creation could fail, but that would cause an 218789Sahrens * incorrect FALSE return, which would only result in an 219789Sahrens * overestimation of the amount of space that an operation would 220789Sahrens * consume, which is OK. 221789Sahrens * 222789Sahrens * There's also a small window where we could miss a pending 223789Sahrens * snapshot, because we could set the sync task in the quiescing 224789Sahrens * phase. So this should only be used as a guess. 225789Sahrens */ 2262885Sahrens if (ds->ds_trysnap_txg > 2272885Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 2282885Sahrens trysnap = ds->ds_trysnap_txg; 2292885Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 2301544Seschrock } 2311544Seschrock 2321544Seschrock int 2331544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 2341544Seschrock { 2351544Seschrock return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 236789Sahrens } 237789Sahrens 238789Sahrens /* ARGSUSED */ 239789Sahrens static void 240789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 241789Sahrens { 242789Sahrens dsl_dataset_t *ds = dsv; 243789Sahrens 2446689Smaybee ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 245789Sahrens 246789Sahrens dprintf_ds(ds, "evicting %s\n", ""); 247789Sahrens 2484787Sahrens unique_remove(ds->ds_fsid_guid); 249789Sahrens 250789Sahrens if (ds->ds_user_ptr != NULL) 251789Sahrens ds->ds_user_evict_func(ds, ds->ds_user_ptr); 252789Sahrens 253789Sahrens if (ds->ds_prev) { 2546689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 255789Sahrens ds->ds_prev = NULL; 256789Sahrens } 257789Sahrens 258789Sahrens bplist_close(&ds->ds_deadlist); 2596689Smaybee if (ds->ds_dir) 2606689Smaybee dsl_dir_close(ds->ds_dir, ds); 261789Sahrens 2624787Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 263789Sahrens 2642856Snd150628 mutex_destroy(&ds->ds_lock); 2654787Sahrens mutex_destroy(&ds->ds_opening_lock); 2662856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 2676689Smaybee rw_destroy(&ds->ds_rwlock); 2686689Smaybee cv_destroy(&ds->ds_exclusive_cv); 2692856Snd150628 270789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 271789Sahrens } 272789Sahrens 2731544Seschrock static int 274789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 275789Sahrens { 276789Sahrens dsl_dataset_phys_t *headphys; 277789Sahrens int err; 278789Sahrens dmu_buf_t *headdbuf; 279789Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 280789Sahrens objset_t *mos = dp->dp_meta_objset; 281789Sahrens 282789Sahrens if (ds->ds_snapname[0]) 2831544Seschrock return (0); 284789Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 2851544Seschrock return (0); 286789Sahrens 2871544Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 2881544Seschrock FTAG, &headdbuf); 2891544Seschrock if (err) 2901544Seschrock return (err); 291789Sahrens headphys = headdbuf->db_data; 292789Sahrens err = zap_value_search(dp->dp_meta_objset, 2934577Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 2941544Seschrock dmu_buf_rele(headdbuf, FTAG); 2951544Seschrock return (err); 296789Sahrens } 297789Sahrens 2986492Stimh static int 2996689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 3006492Stimh { 3016689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3026689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3036492Stimh matchtype_t mt; 3046492Stimh int err; 3056492Stimh 3066689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3076492Stimh mt = MT_FIRST; 3086492Stimh else 3096492Stimh mt = MT_EXACT; 3106492Stimh 3116689Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 3126492Stimh value, mt, NULL, 0, NULL); 3136492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3146689Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 3156492Stimh return (err); 3166492Stimh } 3176492Stimh 3186492Stimh static int 3196689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 3206492Stimh { 3216689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3226689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3236492Stimh matchtype_t mt; 3246492Stimh int err; 3256492Stimh 3266689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3276492Stimh mt = MT_FIRST; 3286492Stimh else 3296492Stimh mt = MT_EXACT; 3306492Stimh 3316689Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 3326492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3336689Smaybee err = zap_remove(mos, snapobj, name, tx); 3346492Stimh return (err); 3356492Stimh } 3366492Stimh 3376689Smaybee static int 3386689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag, 3396689Smaybee dsl_dataset_t **dsp) 340789Sahrens { 341789Sahrens objset_t *mos = dp->dp_meta_objset; 342789Sahrens dmu_buf_t *dbuf; 343789Sahrens dsl_dataset_t *ds; 3441544Seschrock int err; 345789Sahrens 346789Sahrens ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 347789Sahrens dsl_pool_sync_context(dp)); 348789Sahrens 3491544Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3501544Seschrock if (err) 3511544Seschrock return (err); 352789Sahrens ds = dmu_buf_get_user(dbuf); 353789Sahrens if (ds == NULL) { 354789Sahrens dsl_dataset_t *winner; 355789Sahrens 356789Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 357789Sahrens ds->ds_dbuf = dbuf; 358789Sahrens ds->ds_object = dsobj; 359789Sahrens ds->ds_phys = dbuf->db_data; 360789Sahrens 3612856Snd150628 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 3624787Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3632856Snd150628 mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT, 3642856Snd150628 NULL); 3656689Smaybee rw_init(&ds->ds_rwlock, 0, 0, 0); 3666689Smaybee cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 3672856Snd150628 3681544Seschrock err = bplist_open(&ds->ds_deadlist, 369789Sahrens mos, ds->ds_phys->ds_deadlist_obj); 3701544Seschrock if (err == 0) { 3711544Seschrock err = dsl_dir_open_obj(dp, 3721544Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 3731544Seschrock } 3741544Seschrock if (err) { 3751544Seschrock /* 3761544Seschrock * we don't really need to close the blist if we 3771544Seschrock * just opened it. 3781544Seschrock */ 3792856Snd150628 mutex_destroy(&ds->ds_lock); 3804787Sahrens mutex_destroy(&ds->ds_opening_lock); 3812856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 3826689Smaybee rw_destroy(&ds->ds_rwlock); 3836689Smaybee cv_destroy(&ds->ds_exclusive_cv); 3841544Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 3851544Seschrock dmu_buf_rele(dbuf, tag); 3861544Seschrock return (err); 3871544Seschrock } 388789Sahrens 3897390SMatthew.Ahrens@Sun.COM if (!dsl_dataset_is_snapshot(ds)) { 390789Sahrens ds->ds_snapname[0] = '\0'; 391789Sahrens if (ds->ds_phys->ds_prev_snap_obj) { 3926689Smaybee err = dsl_dataset_get_ref(dp, 3936689Smaybee ds->ds_phys->ds_prev_snap_obj, 3946689Smaybee ds, &ds->ds_prev); 395789Sahrens } 3967390SMatthew.Ahrens@Sun.COM 3977390SMatthew.Ahrens@Sun.COM if (err == 0 && dsl_dir_is_clone(ds->ds_dir)) { 3987390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin; 3997390SMatthew.Ahrens@Sun.COM 4007390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, 4017390SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_phys->dd_origin_obj, 4027390SMatthew.Ahrens@Sun.COM FTAG, &origin); 4037390SMatthew.Ahrens@Sun.COM if (err == 0) { 4047390SMatthew.Ahrens@Sun.COM ds->ds_origin_txg = 4057390SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_creation_txg; 4067390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(origin, FTAG); 4077390SMatthew.Ahrens@Sun.COM } 4087390SMatthew.Ahrens@Sun.COM } 4096689Smaybee } else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) { 4106689Smaybee err = dsl_dataset_get_snapname(ds); 411789Sahrens } 412789Sahrens 4137390SMatthew.Ahrens@Sun.COM if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 4145569Sck153898 /* 4155569Sck153898 * In sync context, we're called with either no lock 4165569Sck153898 * or with the write lock. If we're not syncing, 4175569Sck153898 * we're always called with the read lock held. 4185569Sck153898 */ 4195475Sck153898 boolean_t need_lock = 4205569Sck153898 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 4215569Sck153898 dsl_pool_sync_context(dp); 4225475Sck153898 4235475Sck153898 if (need_lock) 4245475Sck153898 rw_enter(&dp->dp_config_rwlock, RW_READER); 4255475Sck153898 4267265Sahrens err = dsl_prop_get_ds(ds, 4275475Sck153898 "refreservation", sizeof (uint64_t), 1, 4285475Sck153898 &ds->ds_reserved, NULL); 4295475Sck153898 if (err == 0) { 4307265Sahrens err = dsl_prop_get_ds(ds, 4315475Sck153898 "refquota", sizeof (uint64_t), 1, 4325475Sck153898 &ds->ds_quota, NULL); 4335475Sck153898 } 4345475Sck153898 4355475Sck153898 if (need_lock) 4365475Sck153898 rw_exit(&dp->dp_config_rwlock); 4375475Sck153898 } else { 4385475Sck153898 ds->ds_reserved = ds->ds_quota = 0; 4395475Sck153898 } 4405475Sck153898 4411544Seschrock if (err == 0) { 4421544Seschrock winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 4431544Seschrock dsl_dataset_evict); 4441544Seschrock } 4451544Seschrock if (err || winner) { 446789Sahrens bplist_close(&ds->ds_deadlist); 4476689Smaybee if (ds->ds_prev) 4486689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 449789Sahrens dsl_dir_close(ds->ds_dir, ds); 4502856Snd150628 mutex_destroy(&ds->ds_lock); 4514787Sahrens mutex_destroy(&ds->ds_opening_lock); 4522856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 4536689Smaybee rw_destroy(&ds->ds_rwlock); 4546689Smaybee cv_destroy(&ds->ds_exclusive_cv); 455789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4561544Seschrock if (err) { 4571544Seschrock dmu_buf_rele(dbuf, tag); 4581544Seschrock return (err); 4591544Seschrock } 460789Sahrens ds = winner; 461789Sahrens } else { 4624787Sahrens ds->ds_fsid_guid = 463789Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 464789Sahrens } 465789Sahrens } 466789Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 467789Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 4687046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 4697061Sahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 4707077Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 471789Sahrens mutex_enter(&ds->ds_lock); 4726689Smaybee if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 473789Sahrens mutex_exit(&ds->ds_lock); 4746689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 4756689Smaybee return (ENOENT); 4766689Smaybee } 4776689Smaybee mutex_exit(&ds->ds_lock); 4786689Smaybee *dsp = ds; 4796689Smaybee return (0); 4806689Smaybee } 4816689Smaybee 4826689Smaybee static int 4836689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 4846689Smaybee { 4856689Smaybee dsl_pool_t *dp = ds->ds_dir->dd_pool; 4866689Smaybee 4876689Smaybee /* 4886689Smaybee * In syncing context we don't want the rwlock lock: there 4896689Smaybee * may be an existing writer waiting for sync phase to 4906689Smaybee * finish. We don't need to worry about such writers, since 4916689Smaybee * sync phase is single-threaded, so the writer can't be 4926689Smaybee * doing anything while we are active. 4936689Smaybee */ 4946689Smaybee if (dsl_pool_sync_context(dp)) { 4956689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 4966689Smaybee return (0); 497789Sahrens } 4986689Smaybee 4996689Smaybee /* 5006689Smaybee * Normal users will hold the ds_rwlock as a READER until they 5016689Smaybee * are finished (i.e., call dsl_dataset_rele()). "Owners" will 5026689Smaybee * drop their READER lock after they set the ds_owner field. 5036689Smaybee * 5046689Smaybee * If the dataset is being destroyed, the destroy thread will 5056689Smaybee * obtain a WRITER lock for exclusive access after it's done its 5066689Smaybee * open-context work and then change the ds_owner to 5076689Smaybee * dsl_reaper once destruction is assured. So threads 5086689Smaybee * may block here temporarily, until the "destructability" of 5096689Smaybee * the dataset is determined. 5106689Smaybee */ 5116689Smaybee ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 5126689Smaybee mutex_enter(&ds->ds_lock); 5136689Smaybee while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 5146689Smaybee rw_exit(&dp->dp_config_rwlock); 5156689Smaybee cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 5166689Smaybee if (DSL_DATASET_IS_DESTROYED(ds)) { 5176689Smaybee mutex_exit(&ds->ds_lock); 5186689Smaybee dsl_dataset_drop_ref(ds, tag); 5196689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5206689Smaybee return (ENOENT); 5216689Smaybee } 5226689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5236689Smaybee } 524789Sahrens mutex_exit(&ds->ds_lock); 5251544Seschrock return (0); 526789Sahrens } 527789Sahrens 528789Sahrens int 5296689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 5306689Smaybee dsl_dataset_t **dsp) 5316689Smaybee { 5326689Smaybee int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 5336689Smaybee 5346689Smaybee if (err) 5356689Smaybee return (err); 5366689Smaybee return (dsl_dataset_hold_ref(*dsp, tag)); 5376689Smaybee } 5386689Smaybee 5396689Smaybee int 5406689Smaybee dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, int flags, void *owner, 5416689Smaybee dsl_dataset_t **dsp) 5426689Smaybee { 5436689Smaybee int err = dsl_dataset_hold_obj(dp, dsobj, owner, dsp); 5446689Smaybee 5456689Smaybee ASSERT(DS_MODE_TYPE(flags) != DS_MODE_USER); 5466689Smaybee 5476689Smaybee if (err) 5486689Smaybee return (err); 5496689Smaybee if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 5506689Smaybee dsl_dataset_rele(*dsp, owner); 5516689Smaybee return (EBUSY); 5526689Smaybee } 5536689Smaybee return (0); 5546689Smaybee } 5556689Smaybee 5566689Smaybee int 5576689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 558789Sahrens { 559789Sahrens dsl_dir_t *dd; 560789Sahrens dsl_pool_t *dp; 5616689Smaybee const char *snapname; 562789Sahrens uint64_t obj; 563789Sahrens int err = 0; 564789Sahrens 5656689Smaybee err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 5661544Seschrock if (err) 5671544Seschrock return (err); 568789Sahrens 569789Sahrens dp = dd->dd_pool; 570789Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 571789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 5726689Smaybee if (obj) 5736689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, dsp); 5746689Smaybee else 575789Sahrens err = ENOENT; 5766689Smaybee if (err) 577789Sahrens goto out; 5786689Smaybee 5796689Smaybee err = dsl_dataset_hold_ref(*dsp, tag); 5806689Smaybee 5816689Smaybee /* we may be looking for a snapshot */ 5826689Smaybee if (err == 0 && snapname != NULL) { 5836689Smaybee dsl_dataset_t *ds = NULL; 5846689Smaybee 5856689Smaybee if (*snapname++ != '@') { 5866689Smaybee dsl_dataset_rele(*dsp, tag); 587789Sahrens err = ENOENT; 588789Sahrens goto out; 589789Sahrens } 5906689Smaybee 5916689Smaybee dprintf("looking for snapshot '%s'\n", snapname); 5926689Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 5936689Smaybee if (err == 0) 5946689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, &ds); 5956689Smaybee dsl_dataset_rele(*dsp, tag); 5966689Smaybee 5976689Smaybee ASSERT3U((err == 0), ==, (ds != NULL)); 5986689Smaybee 5996689Smaybee if (ds) { 6006689Smaybee mutex_enter(&ds->ds_lock); 6016689Smaybee if (ds->ds_snapname[0] == 0) 6026689Smaybee (void) strlcpy(ds->ds_snapname, snapname, 6036689Smaybee sizeof (ds->ds_snapname)); 6046689Smaybee mutex_exit(&ds->ds_lock); 6056689Smaybee err = dsl_dataset_hold_ref(ds, tag); 6066689Smaybee *dsp = err ? NULL : ds; 607789Sahrens } 608789Sahrens } 609789Sahrens out: 610789Sahrens rw_exit(&dp->dp_config_rwlock); 611789Sahrens dsl_dir_close(dd, FTAG); 612789Sahrens return (err); 613789Sahrens } 614789Sahrens 615789Sahrens int 6166689Smaybee dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp) 617789Sahrens { 6186689Smaybee int err = dsl_dataset_hold(name, owner, dsp); 6196689Smaybee if (err) 6206689Smaybee return (err); 6216689Smaybee if ((*dsp)->ds_phys->ds_num_children > 0 && 6226689Smaybee !DS_MODE_IS_READONLY(flags)) { 6236689Smaybee dsl_dataset_rele(*dsp, owner); 6246689Smaybee return (EROFS); 6256689Smaybee } 6266689Smaybee if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 6276689Smaybee dsl_dataset_rele(*dsp, owner); 6286689Smaybee return (EBUSY); 6296689Smaybee } 6306689Smaybee return (0); 631789Sahrens } 632789Sahrens 633789Sahrens void 634789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 635789Sahrens { 636789Sahrens if (ds == NULL) { 637789Sahrens (void) strcpy(name, "mos"); 638789Sahrens } else { 639789Sahrens dsl_dir_name(ds->ds_dir, name); 6401544Seschrock VERIFY(0 == dsl_dataset_get_snapname(ds)); 641789Sahrens if (ds->ds_snapname[0]) { 642789Sahrens (void) strcat(name, "@"); 6436689Smaybee /* 6446689Smaybee * We use a "recursive" mutex so that we 6456689Smaybee * can call dprintf_ds() with ds_lock held. 6466689Smaybee */ 647789Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 648789Sahrens mutex_enter(&ds->ds_lock); 649789Sahrens (void) strcat(name, ds->ds_snapname); 650789Sahrens mutex_exit(&ds->ds_lock); 651789Sahrens } else { 652789Sahrens (void) strcat(name, ds->ds_snapname); 653789Sahrens } 654789Sahrens } 655789Sahrens } 656789Sahrens } 657789Sahrens 6583978Smmusante static int 6593978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds) 6603978Smmusante { 6613978Smmusante int result; 6623978Smmusante 6633978Smmusante if (ds == NULL) { 6643978Smmusante result = 3; /* "mos" */ 6653978Smmusante } else { 6663978Smmusante result = dsl_dir_namelen(ds->ds_dir); 6673978Smmusante VERIFY(0 == dsl_dataset_get_snapname(ds)); 6683978Smmusante if (ds->ds_snapname[0]) { 6693978Smmusante ++result; /* adding one for the @-sign */ 6703978Smmusante if (!MUTEX_HELD(&ds->ds_lock)) { 6713978Smmusante mutex_enter(&ds->ds_lock); 6723978Smmusante result += strlen(ds->ds_snapname); 6733978Smmusante mutex_exit(&ds->ds_lock); 6743978Smmusante } else { 6753978Smmusante result += strlen(ds->ds_snapname); 6763978Smmusante } 6773978Smmusante } 6783978Smmusante } 6793978Smmusante 6803978Smmusante return (result); 6813978Smmusante } 6823978Smmusante 6837046Sahrens void 6846689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 685789Sahrens { 6861544Seschrock dmu_buf_rele(ds->ds_dbuf, tag); 687789Sahrens } 688789Sahrens 689789Sahrens void 6906689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6915367Sahrens { 6926689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 6936689Smaybee rw_exit(&ds->ds_rwlock); 6946689Smaybee } 6956689Smaybee dsl_dataset_drop_ref(ds, tag); 6966689Smaybee } 6976689Smaybee 6986689Smaybee void 6996689Smaybee dsl_dataset_disown(dsl_dataset_t *ds, void *owner) 7006689Smaybee { 7016689Smaybee ASSERT((ds->ds_owner == owner && ds->ds_dbuf) || 7026689Smaybee (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 7036689Smaybee 7045367Sahrens mutex_enter(&ds->ds_lock); 7056689Smaybee ds->ds_owner = NULL; 7066689Smaybee if (RW_WRITE_HELD(&ds->ds_rwlock)) { 7076689Smaybee rw_exit(&ds->ds_rwlock); 7086689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 7096689Smaybee } 7105367Sahrens mutex_exit(&ds->ds_lock); 7116689Smaybee if (ds->ds_dbuf) 7126689Smaybee dsl_dataset_drop_ref(ds, owner); 7136689Smaybee else 7146689Smaybee dsl_dataset_evict(ds->ds_dbuf, ds); 7155367Sahrens } 7165367Sahrens 7175367Sahrens boolean_t 7186689Smaybee dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner) 7195367Sahrens { 7206689Smaybee boolean_t gotit = FALSE; 7216689Smaybee 7225367Sahrens mutex_enter(&ds->ds_lock); 7236689Smaybee if (ds->ds_owner == NULL && 7246689Smaybee (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 7256689Smaybee ds->ds_owner = owner; 7266689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 7276689Smaybee rw_exit(&ds->ds_rwlock); 7286689Smaybee gotit = TRUE; 7295367Sahrens } 7305367Sahrens mutex_exit(&ds->ds_lock); 7316689Smaybee return (gotit); 7326689Smaybee } 7336689Smaybee 7346689Smaybee void 7356689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 7366689Smaybee { 7376689Smaybee ASSERT3P(owner, ==, ds->ds_owner); 7386689Smaybee if (!RW_WRITE_HELD(&ds->ds_rwlock)) 7396689Smaybee rw_enter(&ds->ds_rwlock, RW_WRITER); 7405367Sahrens } 7415367Sahrens 7422199Sahrens uint64_t 7437046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 7446492Stimh uint64_t flags, dmu_tx_t *tx) 745789Sahrens { 7465367Sahrens dsl_pool_t *dp = dd->dd_pool; 747789Sahrens dmu_buf_t *dbuf; 748789Sahrens dsl_dataset_phys_t *dsphys; 7495367Sahrens uint64_t dsobj; 750789Sahrens objset_t *mos = dp->dp_meta_objset; 751789Sahrens 7527046Sahrens if (origin == NULL) 7537046Sahrens origin = dp->dp_origin_snap; 7547046Sahrens 7555367Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 7565367Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 757789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 7585367Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 759789Sahrens 760928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 761928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7621544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 763789Sahrens dmu_buf_will_dirty(dbuf, tx); 764789Sahrens dsphys = dbuf->db_data; 7656689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 766789Sahrens dsphys->ds_dir_obj = dd->dd_object; 7676492Stimh dsphys->ds_flags = flags; 768789Sahrens dsphys->ds_fsid_guid = unique_create(); 769789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 770789Sahrens sizeof (dsphys->ds_guid)); 771789Sahrens dsphys->ds_snapnames_zapobj = 7726492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7736492Stimh DMU_OT_NONE, 0, tx); 774789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 7757046Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 776789Sahrens dsphys->ds_deadlist_obj = 777789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7785378Sck153898 7795367Sahrens if (origin) { 7805367Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 781789Sahrens dsphys->ds_prev_snap_txg = 7825367Sahrens origin->ds_phys->ds_creation_txg; 783789Sahrens dsphys->ds_used_bytes = 7845367Sahrens origin->ds_phys->ds_used_bytes; 785789Sahrens dsphys->ds_compressed_bytes = 7865367Sahrens origin->ds_phys->ds_compressed_bytes; 787789Sahrens dsphys->ds_uncompressed_bytes = 7885367Sahrens origin->ds_phys->ds_uncompressed_bytes; 7895367Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 7906502Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 791789Sahrens 7925367Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 7935367Sahrens origin->ds_phys->ds_num_children++; 794789Sahrens 7957046Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 7967046Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 7977046Sahrens origin->ds_phys->ds_next_clones_obj = 7987046Sahrens zap_create(mos, 7997046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 8007046Sahrens } 8017046Sahrens VERIFY(0 == zap_add_int(mos, 8027046Sahrens origin->ds_phys->ds_next_clones_obj, 8037046Sahrens dsobj, tx)); 8047046Sahrens } 8057046Sahrens 806789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 8075367Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 808789Sahrens } 8096492Stimh 8106492Stimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 8116492Stimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 8126492Stimh 8131544Seschrock dmu_buf_rele(dbuf, FTAG); 814789Sahrens 815789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 816789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 8175367Sahrens 8185367Sahrens return (dsobj); 8195367Sahrens } 8205367Sahrens 8215367Sahrens uint64_t 8226492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 8236492Stimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8245367Sahrens { 8255367Sahrens dsl_pool_t *dp = pdd->dd_pool; 8265367Sahrens uint64_t dsobj, ddobj; 8275367Sahrens dsl_dir_t *dd; 8285367Sahrens 8295367Sahrens ASSERT(lastname[0] != '@'); 8305367Sahrens 8317046Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8325367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 8335367Sahrens 8347046Sahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx); 8355367Sahrens 8365367Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8375367Sahrens 838789Sahrens dsl_dir_close(dd, FTAG); 839789Sahrens 8402199Sahrens return (dsobj); 8412199Sahrens } 8422199Sahrens 8432199Sahrens struct destroyarg { 8442199Sahrens dsl_sync_task_group_t *dstg; 8452199Sahrens char *snapname; 8462199Sahrens char *failed; 8472199Sahrens }; 8482199Sahrens 8492199Sahrens static int 8502199Sahrens dsl_snapshot_destroy_one(char *name, void *arg) 8512199Sahrens { 8522199Sahrens struct destroyarg *da = arg; 8532199Sahrens dsl_dataset_t *ds; 8542199Sahrens char *cp; 8552199Sahrens int err; 8562199Sahrens 8572199Sahrens (void) strcat(name, "@"); 8582199Sahrens (void) strcat(name, da->snapname); 8596689Smaybee err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT, 8604007Smmusante da->dstg, &ds); 8612199Sahrens cp = strchr(name, '@'); 8622199Sahrens *cp = '\0'; 8636689Smaybee if (err == 0) { 8646689Smaybee dsl_dataset_make_exclusive(ds, da->dstg); 8657237Sek110237 if (ds->ds_user_ptr) { 8667237Sek110237 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 8677237Sek110237 ds->ds_user_ptr = NULL; 8687237Sek110237 } 8696689Smaybee dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 8706689Smaybee dsl_dataset_destroy_sync, ds, da->dstg, 0); 8716689Smaybee } else if (err == ENOENT) { 8726689Smaybee err = 0; 8736689Smaybee } else { 8742199Sahrens (void) strcpy(da->failed, name); 8752199Sahrens } 8766689Smaybee return (err); 877789Sahrens } 878789Sahrens 8792199Sahrens /* 8802199Sahrens * Destroy 'snapname' in all descendants of 'fsname'. 8812199Sahrens */ 8822199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 8832199Sahrens int 8842199Sahrens dsl_snapshots_destroy(char *fsname, char *snapname) 8852199Sahrens { 8862199Sahrens int err; 8872199Sahrens struct destroyarg da; 8882199Sahrens dsl_sync_task_t *dst; 8892199Sahrens spa_t *spa; 8902199Sahrens 8914603Sahrens err = spa_open(fsname, &spa, FTAG); 8922199Sahrens if (err) 8932199Sahrens return (err); 8942199Sahrens da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 8952199Sahrens da.snapname = snapname; 8962199Sahrens da.failed = fsname; 8972199Sahrens 8982199Sahrens err = dmu_objset_find(fsname, 8992417Sahrens dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 9002199Sahrens 9012199Sahrens if (err == 0) 9022199Sahrens err = dsl_sync_task_group_wait(da.dstg); 9032199Sahrens 9042199Sahrens for (dst = list_head(&da.dstg->dstg_tasks); dst; 9052199Sahrens dst = list_next(&da.dstg->dstg_tasks, dst)) { 9062199Sahrens dsl_dataset_t *ds = dst->dst_arg1; 9076689Smaybee /* 9086689Smaybee * Return the file system name that triggered the error 9096689Smaybee */ 9102199Sahrens if (dst->dst_err) { 9112199Sahrens dsl_dataset_name(ds, fsname); 9124603Sahrens *strchr(fsname, '@') = '\0'; 9132199Sahrens } 9146689Smaybee dsl_dataset_disown(ds, da.dstg); 9152199Sahrens } 9162199Sahrens 9172199Sahrens dsl_sync_task_group_destroy(da.dstg); 9182199Sahrens spa_close(spa, FTAG); 9192199Sahrens return (err); 9202199Sahrens } 9212199Sahrens 9225367Sahrens /* 9236689Smaybee * ds must be opened as OWNER. On return (whether successful or not), 9246689Smaybee * ds will be closed and caller can no longer dereference it. 9255367Sahrens */ 926789Sahrens int 9275367Sahrens dsl_dataset_destroy(dsl_dataset_t *ds, void *tag) 928789Sahrens { 929789Sahrens int err; 9302199Sahrens dsl_sync_task_group_t *dstg; 9312199Sahrens objset_t *os; 932789Sahrens dsl_dir_t *dd; 9332199Sahrens uint64_t obj; 9342199Sahrens 9355367Sahrens if (dsl_dataset_is_snapshot(ds)) { 9362199Sahrens /* Destroying a snapshot is simpler */ 9376689Smaybee dsl_dataset_make_exclusive(ds, tag); 9387237Sek110237 9397237Sek110237 if (ds->ds_user_ptr) { 9407237Sek110237 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 9417237Sek110237 ds->ds_user_ptr = NULL; 9427237Sek110237 } 9432199Sahrens err = dsl_sync_task_do(ds->ds_dir->dd_pool, 9442199Sahrens dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 9455367Sahrens ds, tag, 0); 9465367Sahrens goto out; 9472199Sahrens } 9482199Sahrens 9492199Sahrens dd = ds->ds_dir; 950789Sahrens 9512199Sahrens /* 9522199Sahrens * Check for errors and mark this ds as inconsistent, in 9532199Sahrens * case we crash while freeing the objects. 9542199Sahrens */ 9552199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 9562199Sahrens dsl_dataset_destroy_begin_sync, ds, NULL, 0); 9575367Sahrens if (err) 9585367Sahrens goto out; 9595367Sahrens 9605367Sahrens err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os); 9615367Sahrens if (err) 9625367Sahrens goto out; 9632199Sahrens 9642199Sahrens /* 9652199Sahrens * remove the objects in open context, so that we won't 9662199Sahrens * have too much to do in syncing context. 9672199Sahrens */ 9683025Sahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 9693025Sahrens ds->ds_phys->ds_prev_snap_txg)) { 9706992Smaybee /* 9716992Smaybee * Ignore errors, if there is not enough disk space 9726992Smaybee * we will deal with it in dsl_dataset_destroy_sync(). 9736992Smaybee */ 9746992Smaybee (void) dmu_free_object(os, obj); 9752199Sahrens } 9762199Sahrens 9772199Sahrens dmu_objset_close(os); 9782199Sahrens if (err != ESRCH) 9795367Sahrens goto out; 9802199Sahrens 9816975Smaybee rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 9826975Smaybee err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 9836975Smaybee rw_exit(&dd->dd_pool->dp_config_rwlock); 9846975Smaybee 9856975Smaybee if (err) 9866975Smaybee goto out; 9876975Smaybee 9885367Sahrens if (ds->ds_user_ptr) { 9896689Smaybee /* 9906689Smaybee * We need to sync out all in-flight IO before we try 9916689Smaybee * to evict (the dataset evict func is trying to clear 9926689Smaybee * the cached entries for this dataset in the ARC). 9936689Smaybee */ 9946689Smaybee txg_wait_synced(dd->dd_pool, 0); 9955367Sahrens } 9965367Sahrens 9972199Sahrens /* 9982199Sahrens * Blow away the dsl_dir + head dataset. 9992199Sahrens */ 10006689Smaybee dsl_dataset_make_exclusive(ds, tag); 10016975Smaybee if (ds->ds_user_ptr) { 10026975Smaybee ds->ds_user_evict_func(ds, ds->ds_user_ptr); 10036975Smaybee ds->ds_user_ptr = NULL; 10046975Smaybee } 10052199Sahrens dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 10062199Sahrens dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 10075367Sahrens dsl_dataset_destroy_sync, ds, tag, 0); 10082199Sahrens dsl_sync_task_create(dstg, dsl_dir_destroy_check, 10092199Sahrens dsl_dir_destroy_sync, dd, FTAG, 0); 10102199Sahrens err = dsl_sync_task_group_wait(dstg); 10112199Sahrens dsl_sync_task_group_destroy(dstg); 10126689Smaybee /* if it is successful, dsl_dir_destroy_sync will close the dd */ 10135367Sahrens if (err) 10142199Sahrens dsl_dir_close(dd, FTAG); 10155367Sahrens out: 10166689Smaybee dsl_dataset_disown(ds, tag); 1017789Sahrens return (err); 1018789Sahrens } 1019789Sahrens 1020789Sahrens int 10215367Sahrens dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost) 1022789Sahrens { 10237385SMark.Maybee@Sun.COM int err; 10247385SMark.Maybee@Sun.COM 10256689Smaybee ASSERT(ds->ds_owner); 10265367Sahrens 10277385SMark.Maybee@Sun.COM dsl_dataset_make_exclusive(ds, ds->ds_owner); 10287385SMark.Maybee@Sun.COM err = dsl_sync_task_do(ds->ds_dir->dd_pool, 10292199Sahrens dsl_dataset_rollback_check, dsl_dataset_rollback_sync, 10307385SMark.Maybee@Sun.COM ds, &ost, 0); 10317385SMark.Maybee@Sun.COM /* drop exclusive access */ 10327385SMark.Maybee@Sun.COM mutex_enter(&ds->ds_lock); 10337385SMark.Maybee@Sun.COM rw_exit(&ds->ds_rwlock); 10347385SMark.Maybee@Sun.COM cv_broadcast(&ds->ds_exclusive_cv); 10357385SMark.Maybee@Sun.COM mutex_exit(&ds->ds_lock); 10367385SMark.Maybee@Sun.COM return (err); 1037789Sahrens } 1038789Sahrens 1039789Sahrens void * 1040789Sahrens dsl_dataset_set_user_ptr(dsl_dataset_t *ds, 1041789Sahrens void *p, dsl_dataset_evict_func_t func) 1042789Sahrens { 1043789Sahrens void *old; 1044789Sahrens 1045789Sahrens mutex_enter(&ds->ds_lock); 1046789Sahrens old = ds->ds_user_ptr; 1047789Sahrens if (old == NULL) { 1048789Sahrens ds->ds_user_ptr = p; 1049789Sahrens ds->ds_user_evict_func = func; 1050789Sahrens } 1051789Sahrens mutex_exit(&ds->ds_lock); 1052789Sahrens return (old); 1053789Sahrens } 1054789Sahrens 1055789Sahrens void * 1056789Sahrens dsl_dataset_get_user_ptr(dsl_dataset_t *ds) 1057789Sahrens { 1058789Sahrens return (ds->ds_user_ptr); 1059789Sahrens } 1060789Sahrens 1061789Sahrens 10623547Smaybee blkptr_t * 10633547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1064789Sahrens { 10653547Smaybee return (&ds->ds_phys->ds_bp); 1066789Sahrens } 1067789Sahrens 1068789Sahrens void 1069789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1070789Sahrens { 1071789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1072789Sahrens /* If it's the meta-objset, set dp_meta_rootbp */ 1073789Sahrens if (ds == NULL) { 1074789Sahrens tx->tx_pool->dp_meta_rootbp = *bp; 1075789Sahrens } else { 1076789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1077789Sahrens ds->ds_phys->ds_bp = *bp; 1078789Sahrens } 1079789Sahrens } 1080789Sahrens 1081789Sahrens spa_t * 1082789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 1083789Sahrens { 1084789Sahrens return (ds->ds_dir->dd_pool->dp_spa); 1085789Sahrens } 1086789Sahrens 1087789Sahrens void 1088789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1089789Sahrens { 1090789Sahrens dsl_pool_t *dp; 1091789Sahrens 1092789Sahrens if (ds == NULL) /* this is the meta-objset */ 1093789Sahrens return; 1094789Sahrens 1095789Sahrens ASSERT(ds->ds_user_ptr != NULL); 10962885Sahrens 10972885Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 10982885Sahrens panic("dirtying snapshot!"); 1099789Sahrens 1100789Sahrens dp = ds->ds_dir->dd_pool; 1101789Sahrens 1102789Sahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1103789Sahrens /* up the hold count until we can be written out */ 1104789Sahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 1105789Sahrens } 1106789Sahrens } 1107789Sahrens 11085378Sck153898 /* 11095378Sck153898 * The unique space in the head dataset can be calculated by subtracting 11105378Sck153898 * the space used in the most recent snapshot, that is still being used 11115378Sck153898 * in this file system, from the space currently in use. To figure out 11125378Sck153898 * the space in the most recent snapshot still in use, we need to take 11135378Sck153898 * the total space used in the snapshot and subtract out the space that 11145378Sck153898 * has been freed up since the snapshot was taken. 11155378Sck153898 */ 11165378Sck153898 static void 11175378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 11185378Sck153898 { 11195378Sck153898 uint64_t mrs_used; 11205378Sck153898 uint64_t dlused, dlcomp, dluncomp; 11215378Sck153898 11225378Sck153898 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 11235378Sck153898 11245378Sck153898 if (ds->ds_phys->ds_prev_snap_obj != 0) 11255378Sck153898 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 11265378Sck153898 else 11275378Sck153898 mrs_used = 0; 11285378Sck153898 11295378Sck153898 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 11305378Sck153898 &dluncomp)); 11315378Sck153898 11325378Sck153898 ASSERT3U(dlused, <=, mrs_used); 11335378Sck153898 ds->ds_phys->ds_unique_bytes = 11345378Sck153898 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 11355378Sck153898 11365378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && 11375378Sck153898 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 11385378Sck153898 SPA_VERSION_UNIQUE_ACCURATE) 11395378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 11405378Sck153898 } 11415378Sck153898 11425378Sck153898 static uint64_t 11435378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds) 11445378Sck153898 { 11455378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 11465378Sck153898 dsl_dataset_recalc_head_uniq(ds); 11475378Sck153898 11485378Sck153898 return (ds->ds_phys->ds_unique_bytes); 11495378Sck153898 } 11505378Sck153898 1151789Sahrens struct killarg { 11527390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 1153789Sahrens zio_t *zio; 1154789Sahrens dmu_tx_t *tx; 1155789Sahrens }; 1156789Sahrens 11577390SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 1158789Sahrens static int 11597837SMatthew.Ahrens@Sun.COM kill_blkptr(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb, 11607837SMatthew.Ahrens@Sun.COM const dnode_phys_t *dnp, void *arg) 1161789Sahrens { 1162789Sahrens struct killarg *ka = arg; 11637837SMatthew.Ahrens@Sun.COM 11647837SMatthew.Ahrens@Sun.COM if (bp == NULL) 11657837SMatthew.Ahrens@Sun.COM return (0); 1166789Sahrens 11677390SMatthew.Ahrens@Sun.COM ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg); 11687390SMatthew.Ahrens@Sun.COM (void) dsl_dataset_block_kill(ka->ds, bp, ka->zio, ka->tx); 11697390SMatthew.Ahrens@Sun.COM 1170789Sahrens return (0); 1171789Sahrens } 1172789Sahrens 1173789Sahrens /* ARGSUSED */ 11742199Sahrens static int 11752199Sahrens dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx) 1176789Sahrens { 11772199Sahrens dsl_dataset_t *ds = arg1; 11785367Sahrens dmu_objset_type_t *ost = arg2; 1179789Sahrens 11802199Sahrens /* 11815367Sahrens * We can only roll back to emptyness if it is a ZPL objset. 11822199Sahrens */ 11835367Sahrens if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0) 1184789Sahrens return (EINVAL); 1185789Sahrens 11862199Sahrens /* 11872199Sahrens * This must not be a snapshot. 11882199Sahrens */ 11892199Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 11902199Sahrens return (EINVAL); 1191789Sahrens 1192789Sahrens /* 11937837SMatthew.Ahrens@Sun.COM * If we made changes this txg, traverse_dataset won't find 1194789Sahrens * them. Try again. 1195789Sahrens */ 11962199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1197789Sahrens return (EAGAIN); 11982199Sahrens 11992199Sahrens return (0); 12002199Sahrens } 1201789Sahrens 12022199Sahrens /* ARGSUSED */ 12032199Sahrens static void 12044543Smarks dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 12052199Sahrens { 12062199Sahrens dsl_dataset_t *ds = arg1; 12075367Sahrens dmu_objset_type_t *ost = arg2; 12082199Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1209789Sahrens 1210789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1211789Sahrens 12124967Sperrin /* 12134967Sperrin * Before the roll back destroy the zil. 12144967Sperrin */ 12154967Sperrin if (ds->ds_user_ptr != NULL) { 12164967Sperrin zil_rollback_destroy( 12174967Sperrin ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx); 12185367Sahrens 12195367Sahrens /* 12205367Sahrens * We need to make sure that the objset_impl_t is reopened after 12215367Sahrens * we do the rollback, otherwise it will have the wrong 12225367Sahrens * objset_phys_t. Normally this would happen when this 12236689Smaybee * dataset-open is closed, thus causing the 12245367Sahrens * dataset to be immediately evicted. But when doing "zfs recv 12255367Sahrens * -F", we reopen the objset before that, so that there is no 12265367Sahrens * window where the dataset is closed and inconsistent. 12275367Sahrens */ 12285367Sahrens ds->ds_user_evict_func(ds, ds->ds_user_ptr); 12295367Sahrens ds->ds_user_ptr = NULL; 12304967Sperrin } 12314935Sperrin 12327390SMatthew.Ahrens@Sun.COM /* Transfer space that was freed since last snap back to the head. */ 12337390SMatthew.Ahrens@Sun.COM { 12347390SMatthew.Ahrens@Sun.COM uint64_t used; 12357390SMatthew.Ahrens@Sun.COM 12367390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&ds->ds_deadlist, 12377390SMatthew.Ahrens@Sun.COM ds->ds_origin_txg, UINT64_MAX, &used)); 12387390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used, 12397390SMatthew.Ahrens@Sun.COM DD_USED_SNAP, DD_USED_HEAD, tx); 12407390SMatthew.Ahrens@Sun.COM } 12417390SMatthew.Ahrens@Sun.COM 1242789Sahrens /* Zero out the deadlist. */ 1243789Sahrens bplist_close(&ds->ds_deadlist); 1244789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1245789Sahrens ds->ds_phys->ds_deadlist_obj = 1246789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 12471544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 12481544Seschrock ds->ds_phys->ds_deadlist_obj)); 1249789Sahrens 1250789Sahrens { 1251789Sahrens /* Free blkptrs that we gave birth to */ 1252789Sahrens zio_t *zio; 1253789Sahrens struct killarg ka; 1254789Sahrens 1255789Sahrens zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL, 1256789Sahrens ZIO_FLAG_MUSTSUCCEED); 12577390SMatthew.Ahrens@Sun.COM ka.ds = ds; 1258789Sahrens ka.zio = zio; 1259789Sahrens ka.tx = tx; 12607837SMatthew.Ahrens@Sun.COM (void) traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 12617837SMatthew.Ahrens@Sun.COM TRAVERSE_POST, kill_blkptr, &ka); 1262789Sahrens (void) zio_wait(zio); 1263789Sahrens } 1264789Sahrens 12657390SMatthew.Ahrens@Sun.COM ASSERT(!(ds->ds_phys->ds_flags & DS_FLAG_UNIQUE_ACCURATE) || 12667390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_unique_bytes == 0); 12677390SMatthew.Ahrens@Sun.COM 12687046Sahrens if (ds->ds_prev && ds->ds_prev != ds->ds_dir->dd_pool->dp_origin_snap) { 12695367Sahrens /* Change our contents to that of the prev snapshot */ 12707390SMatthew.Ahrens@Sun.COM 12715367Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 12725367Sahrens ds->ds_phys->ds_prev_snap_obj); 12737390SMatthew.Ahrens@Sun.COM ASSERT3U(ds->ds_phys->ds_used_bytes, <=, 12747390SMatthew.Ahrens@Sun.COM ds->ds_prev->ds_phys->ds_used_bytes); 12757390SMatthew.Ahrens@Sun.COM 12765367Sahrens ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp; 12775367Sahrens ds->ds_phys->ds_used_bytes = 12785367Sahrens ds->ds_prev->ds_phys->ds_used_bytes; 12795367Sahrens ds->ds_phys->ds_compressed_bytes = 12805367Sahrens ds->ds_prev->ds_phys->ds_compressed_bytes; 12815367Sahrens ds->ds_phys->ds_uncompressed_bytes = 12825367Sahrens ds->ds_prev->ds_phys->ds_uncompressed_bytes; 12835367Sahrens ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags; 1284789Sahrens 12855367Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 12865367Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 12875367Sahrens ds->ds_prev->ds_phys->ds_unique_bytes = 0; 12885367Sahrens } 12895367Sahrens } else { 12907046Sahrens objset_impl_t *osi; 12917046Sahrens 12927390SMatthew.Ahrens@Sun.COM ASSERT3U(ds->ds_phys->ds_used_bytes, ==, 0); 12937390SMatthew.Ahrens@Sun.COM ASSERT3U(ds->ds_phys->ds_compressed_bytes, ==, 0); 12947390SMatthew.Ahrens@Sun.COM ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, ==, 0); 12957390SMatthew.Ahrens@Sun.COM 12965367Sahrens bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t)); 12975367Sahrens ds->ds_phys->ds_flags = 0; 12985367Sahrens ds->ds_phys->ds_unique_bytes = 0; 12997390SMatthew.Ahrens@Sun.COM if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 13007390SMatthew.Ahrens@Sun.COM SPA_VERSION_UNIQUE_ACCURATE) 13017390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 13027390SMatthew.Ahrens@Sun.COM 13037046Sahrens osi = dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds, 13045367Sahrens &ds->ds_phys->ds_bp, *ost, tx); 13057046Sahrens #ifdef _KERNEL 13067046Sahrens zfs_create_fs(&osi->os, kcred, NULL, tx); 13077046Sahrens #endif 13082532Sahrens } 13094543Smarks 13104543Smarks spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa, 13114543Smarks tx, cr, "dataset = %llu", ds->ds_object); 1312789Sahrens } 1313789Sahrens 13141731Sbonwick /* ARGSUSED */ 13151731Sbonwick static int 13162199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 13171731Sbonwick { 13182199Sahrens dsl_dataset_t *ds = arg1; 13195367Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 13205367Sahrens uint64_t count; 13215367Sahrens int err; 13221731Sbonwick 13231731Sbonwick /* 13241731Sbonwick * Can't delete a head dataset if there are snapshots of it. 13251731Sbonwick * (Except if the only snapshots are from the branch we cloned 13261731Sbonwick * from.) 13271731Sbonwick */ 13281731Sbonwick if (ds->ds_prev != NULL && 13291731Sbonwick ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 13301731Sbonwick return (EINVAL); 13311731Sbonwick 13325367Sahrens /* 13335367Sahrens * This is really a dsl_dir thing, but check it here so that 13345367Sahrens * we'll be less likely to leave this dataset inconsistent & 13355367Sahrens * nearly destroyed. 13365367Sahrens */ 13375367Sahrens err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 13385367Sahrens if (err) 13395367Sahrens return (err); 13405367Sahrens if (count != 0) 13415367Sahrens return (EEXIST); 13425367Sahrens 13431731Sbonwick return (0); 13441731Sbonwick } 13451731Sbonwick 13462199Sahrens /* ARGSUSED */ 13472199Sahrens static void 13484543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1349789Sahrens { 13502199Sahrens dsl_dataset_t *ds = arg1; 13514543Smarks dsl_pool_t *dp = ds->ds_dir->dd_pool; 1352789Sahrens 13532199Sahrens /* Mark it as inconsistent on-disk, in case we crash */ 13542199Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13552199Sahrens ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 13564543Smarks 13574543Smarks spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 13584543Smarks cr, "dataset = %llu", ds->ds_object); 13592199Sahrens } 1360789Sahrens 13612199Sahrens /* ARGSUSED */ 13625367Sahrens int 13632199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 13642199Sahrens { 13652199Sahrens dsl_dataset_t *ds = arg1; 1366789Sahrens 13676689Smaybee /* we have an owner hold, so noone else can destroy us */ 13686689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 13696689Smaybee 1370789Sahrens /* Can't delete a branch point. */ 13712199Sahrens if (ds->ds_phys->ds_num_children > 1) 13722199Sahrens return (EEXIST); 1373789Sahrens 1374789Sahrens /* 1375789Sahrens * Can't delete a head dataset if there are snapshots of it. 1376789Sahrens * (Except if the only snapshots are from the branch we cloned 1377789Sahrens * from.) 1378789Sahrens */ 1379789Sahrens if (ds->ds_prev != NULL && 13802199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1381789Sahrens return (EINVAL); 1382789Sahrens 1383789Sahrens /* 1384789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1385789Sahrens * them. Try again. 1386789Sahrens */ 13872199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1388789Sahrens return (EAGAIN); 13892199Sahrens 13902199Sahrens /* XXX we should do some i/o error checking... */ 13912199Sahrens return (0); 13922199Sahrens } 13932199Sahrens 13946689Smaybee struct refsarg { 13956689Smaybee kmutex_t lock; 13966689Smaybee boolean_t gone; 13976689Smaybee kcondvar_t cv; 13986689Smaybee }; 13996689Smaybee 14006689Smaybee /* ARGSUSED */ 14016689Smaybee static void 14026689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 14036689Smaybee { 14046689Smaybee struct refsarg *arg = argv; 14056689Smaybee 14066689Smaybee mutex_enter(&arg->lock); 14076689Smaybee arg->gone = TRUE; 14086689Smaybee cv_signal(&arg->cv); 14096689Smaybee mutex_exit(&arg->lock); 14106689Smaybee } 14116689Smaybee 14126689Smaybee static void 14136689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 14146689Smaybee { 14156689Smaybee struct refsarg arg; 14166689Smaybee 14176689Smaybee mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 14186689Smaybee cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 14196689Smaybee arg.gone = FALSE; 14206689Smaybee (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 14216689Smaybee dsl_dataset_refs_gone); 14226689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 14236689Smaybee mutex_enter(&arg.lock); 14246689Smaybee while (!arg.gone) 14256689Smaybee cv_wait(&arg.cv, &arg.lock); 14266689Smaybee ASSERT(arg.gone); 14276689Smaybee mutex_exit(&arg.lock); 14286689Smaybee ds->ds_dbuf = NULL; 14296689Smaybee ds->ds_phys = NULL; 14306689Smaybee mutex_destroy(&arg.lock); 14316689Smaybee cv_destroy(&arg.cv); 14326689Smaybee } 14336689Smaybee 14345367Sahrens void 14354543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 14362199Sahrens { 14372199Sahrens dsl_dataset_t *ds = arg1; 14382199Sahrens zio_t *zio; 14392199Sahrens int err; 14402199Sahrens int after_branch_point = FALSE; 14412199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 14422199Sahrens objset_t *mos = dp->dp_meta_objset; 14432199Sahrens dsl_dataset_t *ds_prev = NULL; 14442199Sahrens uint64_t obj; 14452199Sahrens 14466689Smaybee ASSERT(ds->ds_owner); 14472199Sahrens ASSERT3U(ds->ds_phys->ds_num_children, <=, 1); 14482199Sahrens ASSERT(ds->ds_prev == NULL || 14492199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 14502199Sahrens ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 14512199Sahrens 14526689Smaybee /* signal any waiters that this dataset is going away */ 14536689Smaybee mutex_enter(&ds->ds_lock); 14546689Smaybee ds->ds_owner = dsl_reaper; 14556689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 14566689Smaybee mutex_exit(&ds->ds_lock); 14576689Smaybee 14585378Sck153898 /* Remove our reservation */ 14595378Sck153898 if (ds->ds_reserved != 0) { 14605378Sck153898 uint64_t val = 0; 14615378Sck153898 dsl_dataset_set_reservation_sync(ds, &val, cr, tx); 14625378Sck153898 ASSERT3U(ds->ds_reserved, ==, 0); 14635378Sck153898 } 14645378Sck153898 14652199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 14662199Sahrens 14677046Sahrens dsl_pool_ds_destroyed(ds, tx); 14687046Sahrens 14692199Sahrens obj = ds->ds_object; 1470789Sahrens 1471789Sahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 1472789Sahrens if (ds->ds_prev) { 1473789Sahrens ds_prev = ds->ds_prev; 1474789Sahrens } else { 14756689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 14766689Smaybee ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1477789Sahrens } 1478789Sahrens after_branch_point = 1479789Sahrens (ds_prev->ds_phys->ds_next_snap_obj != obj); 1480789Sahrens 1481789Sahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1482789Sahrens if (after_branch_point && 14837046Sahrens ds_prev->ds_phys->ds_next_clones_obj != 0) { 14847046Sahrens VERIFY(0 == zap_remove_int(mos, 14857046Sahrens ds_prev->ds_phys->ds_next_clones_obj, obj, tx)); 14867046Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 14877046Sahrens VERIFY(0 == zap_add_int(mos, 14887046Sahrens ds_prev->ds_phys->ds_next_clones_obj, 14897046Sahrens ds->ds_phys->ds_next_snap_obj, tx)); 14907046Sahrens } 14917046Sahrens } 14927046Sahrens if (after_branch_point && 1493789Sahrens ds->ds_phys->ds_next_snap_obj == 0) { 1494789Sahrens /* This clone is toast. */ 1495789Sahrens ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1496789Sahrens ds_prev->ds_phys->ds_num_children--; 1497789Sahrens } else if (!after_branch_point) { 1498789Sahrens ds_prev->ds_phys->ds_next_snap_obj = 1499789Sahrens ds->ds_phys->ds_next_snap_obj; 1500789Sahrens } 1501789Sahrens } 1502789Sahrens 1503789Sahrens zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1504789Sahrens 1505789Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15062199Sahrens blkptr_t bp; 1507789Sahrens dsl_dataset_t *ds_next; 1508789Sahrens uint64_t itor = 0; 15095378Sck153898 uint64_t old_unique; 15107390SMatthew.Ahrens@Sun.COM int64_t used = 0, compressed = 0, uncompressed = 0; 1511789Sahrens 15126689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15136689Smaybee ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1514789Sahrens ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1515789Sahrens 15165378Sck153898 old_unique = dsl_dataset_unique(ds_next); 15175378Sck153898 1518789Sahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1519789Sahrens ds_next->ds_phys->ds_prev_snap_obj = 1520789Sahrens ds->ds_phys->ds_prev_snap_obj; 1521789Sahrens ds_next->ds_phys->ds_prev_snap_txg = 1522789Sahrens ds->ds_phys->ds_prev_snap_txg; 1523789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1524789Sahrens ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1525789Sahrens 1526789Sahrens /* 1527789Sahrens * Transfer to our deadlist (which will become next's 1528789Sahrens * new deadlist) any entries from next's current 1529789Sahrens * deadlist which were born before prev, and free the 1530789Sahrens * other entries. 1531789Sahrens * 1532789Sahrens * XXX we're doing this long task with the config lock held 1533789Sahrens */ 15346689Smaybee while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1535789Sahrens if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 15361544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 15371544Seschrock &bp, tx)); 1538789Sahrens if (ds_prev && !after_branch_point && 1539789Sahrens bp.blk_birth > 1540789Sahrens ds_prev->ds_phys->ds_prev_snap_txg) { 1541789Sahrens ds_prev->ds_phys->ds_unique_bytes += 15422082Seschrock bp_get_dasize(dp->dp_spa, &bp); 1543789Sahrens } 1544789Sahrens } else { 15452082Seschrock used += bp_get_dasize(dp->dp_spa, &bp); 1546789Sahrens compressed += BP_GET_PSIZE(&bp); 1547789Sahrens uncompressed += BP_GET_UCSIZE(&bp); 1548789Sahrens /* XXX check return value? */ 15497046Sahrens (void) dsl_free(zio, dp, tx->tx_txg, 1550789Sahrens &bp, NULL, NULL, ARC_NOWAIT); 1551789Sahrens } 1552789Sahrens } 1553789Sahrens 15547390SMatthew.Ahrens@Sun.COM ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 15557390SMatthew.Ahrens@Sun.COM 15567390SMatthew.Ahrens@Sun.COM /* change snapused */ 15577390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 15587390SMatthew.Ahrens@Sun.COM -used, -compressed, -uncompressed, tx); 15597390SMatthew.Ahrens@Sun.COM 1560789Sahrens /* free next's deadlist */ 1561789Sahrens bplist_close(&ds_next->ds_deadlist); 1562789Sahrens bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1563789Sahrens 1564789Sahrens /* set next's deadlist to our deadlist */ 15656689Smaybee bplist_close(&ds->ds_deadlist); 1566789Sahrens ds_next->ds_phys->ds_deadlist_obj = 1567789Sahrens ds->ds_phys->ds_deadlist_obj; 15681544Seschrock VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 15691544Seschrock ds_next->ds_phys->ds_deadlist_obj)); 1570789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1571789Sahrens 1572789Sahrens if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1573789Sahrens /* 1574789Sahrens * Update next's unique to include blocks which 1575789Sahrens * were previously shared by only this snapshot 1576789Sahrens * and it. Those blocks will be born after the 1577789Sahrens * prev snap and before this snap, and will have 1578789Sahrens * died after the next snap and before the one 1579789Sahrens * after that (ie. be on the snap after next's 1580789Sahrens * deadlist). 1581789Sahrens * 1582789Sahrens * XXX we're doing this long task with the 1583789Sahrens * config lock held 1584789Sahrens */ 1585789Sahrens dsl_dataset_t *ds_after_next; 15867390SMatthew.Ahrens@Sun.COM uint64_t space; 1587789Sahrens 15886689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15896689Smaybee ds_next->ds_phys->ds_next_snap_obj, 15906689Smaybee FTAG, &ds_after_next)); 15917390SMatthew.Ahrens@Sun.COM 15927390SMatthew.Ahrens@Sun.COM VERIFY(0 == 15937390SMatthew.Ahrens@Sun.COM bplist_space_birthrange(&ds_after_next->ds_deadlist, 15947390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_prev_snap_txg, 15957390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_creation_txg, &space)); 15967390SMatthew.Ahrens@Sun.COM ds_next->ds_phys->ds_unique_bytes += space; 1597789Sahrens 15986689Smaybee dsl_dataset_rele(ds_after_next, FTAG); 1599789Sahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 1600789Sahrens } else { 1601789Sahrens ASSERT3P(ds_next->ds_prev, ==, ds); 16026689Smaybee dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 16036689Smaybee ds_next->ds_prev = NULL; 1604789Sahrens if (ds_prev) { 16056689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 16066689Smaybee ds->ds_phys->ds_prev_snap_obj, 16076689Smaybee ds_next, &ds_next->ds_prev)); 1608789Sahrens } 16095378Sck153898 16105378Sck153898 dsl_dataset_recalc_head_uniq(ds_next); 16115378Sck153898 16125378Sck153898 /* 16135378Sck153898 * Reduce the amount of our unconsmed refreservation 16145378Sck153898 * being charged to our parent by the amount of 16155378Sck153898 * new unique data we have gained. 16165378Sck153898 */ 16175378Sck153898 if (old_unique < ds_next->ds_reserved) { 16185378Sck153898 int64_t mrsdelta; 16195378Sck153898 uint64_t new_unique = 16205378Sck153898 ds_next->ds_phys->ds_unique_bytes; 16215378Sck153898 16225378Sck153898 ASSERT(old_unique <= new_unique); 16235378Sck153898 mrsdelta = MIN(new_unique - old_unique, 16245378Sck153898 ds_next->ds_reserved - old_unique); 16257390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, 16267390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 16275378Sck153898 } 1628789Sahrens } 16296689Smaybee dsl_dataset_rele(ds_next, FTAG); 1630789Sahrens } else { 1631789Sahrens /* 1632789Sahrens * There's no next snapshot, so this is a head dataset. 1633789Sahrens * Destroy the deadlist. Unless it's a clone, the 1634789Sahrens * deadlist should be empty. (If it's a clone, it's 1635789Sahrens * safe to ignore the deadlist contents.) 1636789Sahrens */ 1637789Sahrens struct killarg ka; 1638789Sahrens 1639789Sahrens ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1640789Sahrens bplist_close(&ds->ds_deadlist); 1641789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1642789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1643789Sahrens 1644789Sahrens /* 1645789Sahrens * Free everything that we point to (that's born after 1646789Sahrens * the previous snapshot, if we are a clone) 1647789Sahrens * 16487390SMatthew.Ahrens@Sun.COM * NB: this should be very quick, because we already 16497390SMatthew.Ahrens@Sun.COM * freed all the objects in open context. 1650789Sahrens */ 16517390SMatthew.Ahrens@Sun.COM ka.ds = ds; 1652789Sahrens ka.zio = zio; 1653789Sahrens ka.tx = tx; 16547837SMatthew.Ahrens@Sun.COM err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 16557837SMatthew.Ahrens@Sun.COM TRAVERSE_POST, kill_blkptr, &ka); 1656789Sahrens ASSERT3U(err, ==, 0); 16577390SMatthew.Ahrens@Sun.COM ASSERT(spa_version(dp->dp_spa) < SPA_VERSION_UNIQUE_ACCURATE || 16587390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_unique_bytes == 0); 1659789Sahrens } 1660789Sahrens 1661789Sahrens err = zio_wait(zio); 1662789Sahrens ASSERT3U(err, ==, 0); 1663789Sahrens 16646689Smaybee if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 16656689Smaybee /* Erase the link in the dir */ 16666689Smaybee dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 16676689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 16686689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1669789Sahrens err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1670789Sahrens ASSERT(err == 0); 1671789Sahrens } else { 1672789Sahrens /* remove from snapshot namespace */ 1673789Sahrens dsl_dataset_t *ds_head; 16746689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 16756689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16766689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 16772207Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 1678789Sahrens #ifdef ZFS_DEBUG 1679789Sahrens { 1680789Sahrens uint64_t val; 16816492Stimh 16826689Smaybee err = dsl_dataset_snap_lookup(ds_head, 16836492Stimh ds->ds_snapname, &val); 1684789Sahrens ASSERT3U(err, ==, 0); 1685789Sahrens ASSERT3U(val, ==, obj); 1686789Sahrens } 1687789Sahrens #endif 16886689Smaybee err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1689789Sahrens ASSERT(err == 0); 16906689Smaybee dsl_dataset_rele(ds_head, FTAG); 1691789Sahrens } 1692789Sahrens 1693789Sahrens if (ds_prev && ds->ds_prev != ds_prev) 16946689Smaybee dsl_dataset_rele(ds_prev, FTAG); 1695789Sahrens 16965094Slling spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 16974543Smarks spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 16984543Smarks cr, "dataset = %llu", ds->ds_object); 16994543Smarks 17007046Sahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 17017046Sahrens uint64_t count; 17027046Sahrens ASSERT(0 == zap_count(mos, 17037046Sahrens ds->ds_phys->ds_next_clones_obj, &count) && count == 0); 17047046Sahrens VERIFY(0 == dmu_object_free(mos, 17057046Sahrens ds->ds_phys->ds_next_clones_obj, tx)); 17067046Sahrens } 17077390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_props_obj != 0) 17087390SMatthew.Ahrens@Sun.COM VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx)); 17096689Smaybee dsl_dir_close(ds->ds_dir, ds); 17106689Smaybee ds->ds_dir = NULL; 17116689Smaybee dsl_dataset_drain_refs(ds, tag); 17122199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 17132199Sahrens } 17142199Sahrens 17155378Sck153898 static int 17165378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 17175378Sck153898 { 17185378Sck153898 uint64_t asize; 17195378Sck153898 17205378Sck153898 if (!dmu_tx_is_syncing(tx)) 17215378Sck153898 return (0); 17225378Sck153898 17235378Sck153898 /* 17245378Sck153898 * If there's an fs-only reservation, any blocks that might become 17255378Sck153898 * owned by the snapshot dataset must be accommodated by space 17265378Sck153898 * outside of the reservation. 17275378Sck153898 */ 17285378Sck153898 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 17295378Sck153898 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 17305378Sck153898 return (ENOSPC); 17315378Sck153898 17325378Sck153898 /* 17335378Sck153898 * Propogate any reserved space for this snapshot to other 17345378Sck153898 * snapshot checks in this sync group. 17355378Sck153898 */ 17365378Sck153898 if (asize > 0) 17375378Sck153898 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 17385378Sck153898 17395378Sck153898 return (0); 17405378Sck153898 } 17415378Sck153898 17422199Sahrens /* ARGSUSED */ 17432199Sahrens int 17442199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 17452199Sahrens { 17465367Sahrens dsl_dataset_t *ds = arg1; 17472199Sahrens const char *snapname = arg2; 17482199Sahrens int err; 17492199Sahrens uint64_t value; 1750789Sahrens 1751789Sahrens /* 17522199Sahrens * We don't allow multiple snapshots of the same txg. If there 17532199Sahrens * is already one, try again. 17542199Sahrens */ 17552199Sahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 17562199Sahrens return (EAGAIN); 17572199Sahrens 17582199Sahrens /* 17592199Sahrens * Check for conflicting name snapshot name. 1760789Sahrens */ 17616689Smaybee err = dsl_dataset_snap_lookup(ds, snapname, &value); 17622199Sahrens if (err == 0) 17632199Sahrens return (EEXIST); 17642199Sahrens if (err != ENOENT) 17652199Sahrens return (err); 1766789Sahrens 17673978Smmusante /* 17683978Smmusante * Check that the dataset's name is not too long. Name consists 17693978Smmusante * of the dataset's length + 1 for the @-sign + snapshot name's length 17703978Smmusante */ 17713978Smmusante if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 17723978Smmusante return (ENAMETOOLONG); 17733978Smmusante 17745378Sck153898 err = dsl_dataset_snapshot_reserve_space(ds, tx); 17755378Sck153898 if (err) 17765378Sck153898 return (err); 17775378Sck153898 17782199Sahrens ds->ds_trysnap_txg = tx->tx_txg; 1779789Sahrens return (0); 1780789Sahrens } 1781789Sahrens 17822199Sahrens void 17834543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1784789Sahrens { 17855367Sahrens dsl_dataset_t *ds = arg1; 17862199Sahrens const char *snapname = arg2; 17872199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1788789Sahrens dmu_buf_t *dbuf; 1789789Sahrens dsl_dataset_phys_t *dsphys; 17907046Sahrens uint64_t dsobj, crtxg; 1791789Sahrens objset_t *mos = dp->dp_meta_objset; 1792789Sahrens int err; 1793789Sahrens 17942199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1795789Sahrens 17967046Sahrens /* 17977046Sahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 17987046Sahrens */ 17997046Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 18007046Sahrens crtxg = 1; 18017046Sahrens else 18027046Sahrens crtxg = tx->tx_txg; 18037046Sahrens 1804928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1805928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 18061544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1807789Sahrens dmu_buf_will_dirty(dbuf, tx); 1808789Sahrens dsphys = dbuf->db_data; 18096689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 18102199Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1811789Sahrens dsphys->ds_fsid_guid = unique_create(); 1812789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1813789Sahrens sizeof (dsphys->ds_guid)); 1814789Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1815789Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1816789Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1817789Sahrens dsphys->ds_num_children = 1; 1818789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 18197046Sahrens dsphys->ds_creation_txg = crtxg; 1820789Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1821789Sahrens dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1822789Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1823789Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 18242082Seschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1825789Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 18261544Seschrock dmu_buf_rele(dbuf, FTAG); 1827789Sahrens 18282199Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 18292199Sahrens if (ds->ds_prev) { 18307046Sahrens uint64_t next_clones_obj = 18317046Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 18322199Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1833789Sahrens ds->ds_object || 18342199Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 18352199Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 18362199Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1837789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 18382199Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 18392199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 18407046Sahrens } else if (next_clones_obj != 0) { 18417046Sahrens VERIFY3U(0, ==, zap_remove_int(mos, 18427046Sahrens next_clones_obj, dsphys->ds_next_snap_obj, tx)); 18437046Sahrens VERIFY3U(0, ==, zap_add_int(mos, 18447046Sahrens next_clones_obj, dsobj, tx)); 1845789Sahrens } 1846789Sahrens } 1847789Sahrens 18485378Sck153898 /* 18495378Sck153898 * If we have a reference-reservation on this dataset, we will 18505378Sck153898 * need to increase the amount of refreservation being charged 18515378Sck153898 * since our unique space is going to zero. 18525378Sck153898 */ 18535378Sck153898 if (ds->ds_reserved) { 18545378Sck153898 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 18557390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 18567390SMatthew.Ahrens@Sun.COM add, 0, 0, tx); 18575378Sck153898 } 18585378Sck153898 1859789Sahrens bplist_close(&ds->ds_deadlist); 1860789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 18615712Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1862789Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 18637046Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1864789Sahrens ds->ds_phys->ds_unique_bytes = 0; 18655378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 18665378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1867789Sahrens ds->ds_phys->ds_deadlist_obj = 1868789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 18691544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 18701544Seschrock ds->ds_phys->ds_deadlist_obj)); 1871789Sahrens 1872789Sahrens dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1873789Sahrens err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1874789Sahrens snapname, 8, 1, &dsobj, tx); 1875789Sahrens ASSERT(err == 0); 1876789Sahrens 1877789Sahrens if (ds->ds_prev) 18786689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 18796689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 18806689Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 18814543Smarks 18827046Sahrens dsl_pool_ds_snapshotted(ds, tx); 18837046Sahrens 18844543Smarks spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 18854603Sahrens "dataset = %llu", dsobj); 1886789Sahrens } 1887789Sahrens 1888789Sahrens void 18893547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1890789Sahrens { 1891789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1892789Sahrens ASSERT(ds->ds_user_ptr != NULL); 1893789Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1894789Sahrens 18954787Sahrens /* 18964787Sahrens * in case we had to change ds_fsid_guid when we opened it, 18974787Sahrens * sync it out now. 18984787Sahrens */ 18994787Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 19004787Sahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 19014787Sahrens 1902789Sahrens dsl_dir_dirty(ds->ds_dir, tx); 19033547Smaybee dmu_objset_sync(ds->ds_user_ptr, zio, tx); 1904789Sahrens } 1905789Sahrens 1906789Sahrens void 19072885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1908789Sahrens { 19095378Sck153898 uint64_t refd, avail, uobjs, aobjs; 19105378Sck153898 19112885Sahrens dsl_dir_stats(ds->ds_dir, nv); 1912789Sahrens 19135378Sck153898 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 19145378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 19155378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 19165378Sck153898 19172885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 19182885Sahrens ds->ds_phys->ds_creation_time); 19192885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 19202885Sahrens ds->ds_phys->ds_creation_txg); 19215378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 19225378Sck153898 ds->ds_quota); 19235378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 19245378Sck153898 ds->ds_reserved); 19256643Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 19266643Seschrock ds->ds_phys->ds_guid); 1927789Sahrens 1928789Sahrens if (ds->ds_phys->ds_next_snap_obj) { 1929789Sahrens /* 1930789Sahrens * This is a snapshot; override the dd's space used with 19312885Sahrens * our unique space and compression ratio. 1932789Sahrens */ 19332885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 19342885Sahrens ds->ds_phys->ds_unique_bytes); 19352885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 19362885Sahrens ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 19372885Sahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 19382885Sahrens ds->ds_phys->ds_compressed_bytes)); 1939789Sahrens } 1940789Sahrens } 1941789Sahrens 19422885Sahrens void 19432885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1944789Sahrens { 19452885Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 19462885Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 19475367Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 19482885Sahrens if (ds->ds_phys->ds_next_snap_obj) { 19492885Sahrens stat->dds_is_snapshot = B_TRUE; 19502885Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 19518228SEric.Taylor@Sun.COM } else { 19528228SEric.Taylor@Sun.COM stat->dds_is_snapshot = B_FALSE; 19538228SEric.Taylor@Sun.COM stat->dds_num_clones = 0; 19542885Sahrens } 19552885Sahrens 19562885Sahrens /* clone origin is really a dsl_dir thing... */ 19575446Sahrens rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 19587046Sahrens if (dsl_dir_is_clone(ds->ds_dir)) { 19592885Sahrens dsl_dataset_t *ods; 19602885Sahrens 19616689Smaybee VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 19626689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 19635367Sahrens dsl_dataset_name(ods, stat->dds_origin); 19646689Smaybee dsl_dataset_drop_ref(ods, FTAG); 19658228SEric.Taylor@Sun.COM } else { 19668228SEric.Taylor@Sun.COM stat->dds_origin[0] = '\0'; 19672885Sahrens } 19685446Sahrens rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 19692885Sahrens } 19702885Sahrens 19712885Sahrens uint64_t 19722885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 19732885Sahrens { 19744787Sahrens return (ds->ds_fsid_guid); 19752885Sahrens } 19762885Sahrens 19772885Sahrens void 19782885Sahrens dsl_dataset_space(dsl_dataset_t *ds, 19792885Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 19802885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 19812885Sahrens { 19822885Sahrens *refdbytesp = ds->ds_phys->ds_used_bytes; 19832885Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 19845378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 19855378Sck153898 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 19865378Sck153898 if (ds->ds_quota != 0) { 19875378Sck153898 /* 19885378Sck153898 * Adjust available bytes according to refquota 19895378Sck153898 */ 19905378Sck153898 if (*refdbytesp < ds->ds_quota) 19915378Sck153898 *availbytesp = MIN(*availbytesp, 19925378Sck153898 ds->ds_quota - *refdbytesp); 19935378Sck153898 else 19945378Sck153898 *availbytesp = 0; 19955378Sck153898 } 19962885Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 19972885Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1998789Sahrens } 1999789Sahrens 20005326Sek110237 boolean_t 20015326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 20025326Sek110237 { 20035326Sek110237 dsl_pool_t *dp = ds->ds_dir->dd_pool; 20045326Sek110237 20055326Sek110237 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 20065326Sek110237 dsl_pool_sync_context(dp)); 20075326Sek110237 if (ds->ds_prev == NULL) 20085326Sek110237 return (B_FALSE); 20095326Sek110237 if (ds->ds_phys->ds_bp.blk_birth > 20105326Sek110237 ds->ds_prev->ds_phys->ds_creation_txg) 20115326Sek110237 return (B_TRUE); 20125326Sek110237 return (B_FALSE); 20135326Sek110237 } 20145326Sek110237 20152199Sahrens /* ARGSUSED */ 2016789Sahrens static int 20172199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 2018789Sahrens { 20192199Sahrens dsl_dataset_t *ds = arg1; 20202199Sahrens char *newsnapname = arg2; 20212199Sahrens dsl_dir_t *dd = ds->ds_dir; 20222199Sahrens dsl_dataset_t *hds; 20232199Sahrens uint64_t val; 2024789Sahrens int err; 2025789Sahrens 20266689Smaybee err = dsl_dataset_hold_obj(dd->dd_pool, 20276689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2028789Sahrens if (err) 2029789Sahrens return (err); 2030789Sahrens 20312199Sahrens /* new name better not be in use */ 20326689Smaybee err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 20336689Smaybee dsl_dataset_rele(hds, FTAG); 2034789Sahrens 20352199Sahrens if (err == 0) 20362199Sahrens err = EEXIST; 20372199Sahrens else if (err == ENOENT) 20382199Sahrens err = 0; 20394007Smmusante 20404007Smmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 20414007Smmusante if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 20424007Smmusante err = ENAMETOOLONG; 20434007Smmusante 20442199Sahrens return (err); 20452199Sahrens } 2046789Sahrens 20472199Sahrens static void 20484543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 20494543Smarks cred_t *cr, dmu_tx_t *tx) 20502199Sahrens { 20512199Sahrens dsl_dataset_t *ds = arg1; 20524543Smarks const char *newsnapname = arg2; 20532199Sahrens dsl_dir_t *dd = ds->ds_dir; 20542199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 20552199Sahrens dsl_dataset_t *hds; 20562199Sahrens int err; 2057789Sahrens 20582199Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2059789Sahrens 20606689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 20616689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2062789Sahrens 20632199Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 20646689Smaybee err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2065789Sahrens ASSERT3U(err, ==, 0); 20662199Sahrens mutex_enter(&ds->ds_lock); 20672199Sahrens (void) strcpy(ds->ds_snapname, newsnapname); 20682199Sahrens mutex_exit(&ds->ds_lock); 20692199Sahrens err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 20702199Sahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2071789Sahrens ASSERT3U(err, ==, 0); 2072789Sahrens 20734543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 20744543Smarks cr, "dataset = %llu", ds->ds_object); 20756689Smaybee dsl_dataset_rele(hds, FTAG); 2076789Sahrens } 2077789Sahrens 20785326Sek110237 struct renamesnaparg { 20794007Smmusante dsl_sync_task_group_t *dstg; 20804007Smmusante char failed[MAXPATHLEN]; 20814007Smmusante char *oldsnap; 20824007Smmusante char *newsnap; 20834007Smmusante }; 20844007Smmusante 20854007Smmusante static int 20864007Smmusante dsl_snapshot_rename_one(char *name, void *arg) 20874007Smmusante { 20885326Sek110237 struct renamesnaparg *ra = arg; 20894007Smmusante dsl_dataset_t *ds = NULL; 20904007Smmusante char *cp; 20914007Smmusante int err; 20924007Smmusante 20934007Smmusante cp = name + strlen(name); 20944007Smmusante *cp = '@'; 20954007Smmusante (void) strcpy(cp + 1, ra->oldsnap); 20964543Smarks 20974543Smarks /* 20984543Smarks * For recursive snapshot renames the parent won't be changing 20994543Smarks * so we just pass name for both the to/from argument. 21004543Smarks */ 21017312SMatthew.Ahrens@Sun.COM err = zfs_secpolicy_rename_perms(name, name, CRED()); 21027312SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 21037312SMatthew.Ahrens@Sun.COM return (0); 21047312SMatthew.Ahrens@Sun.COM } else if (err) { 21054543Smarks (void) strcpy(ra->failed, name); 21064543Smarks return (err); 21074543Smarks } 21084543Smarks 21096689Smaybee #ifdef _KERNEL 21106689Smaybee /* 21116689Smaybee * For all filesystems undergoing rename, we'll need to unmount it. 21126689Smaybee */ 21136689Smaybee (void) zfs_unmount_snap(name, NULL); 21146689Smaybee #endif 21156689Smaybee err = dsl_dataset_hold(name, ra->dstg, &ds); 21166689Smaybee *cp = '\0'; 21174007Smmusante if (err == ENOENT) { 21184007Smmusante return (0); 21196689Smaybee } else if (err) { 21204007Smmusante (void) strcpy(ra->failed, name); 21214007Smmusante return (err); 21224007Smmusante } 21234007Smmusante 21244007Smmusante dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 21254007Smmusante dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 21264007Smmusante 21274007Smmusante return (0); 21284007Smmusante } 21294007Smmusante 21304007Smmusante static int 21314007Smmusante dsl_recursive_rename(char *oldname, const char *newname) 21324007Smmusante { 21334007Smmusante int err; 21345326Sek110237 struct renamesnaparg *ra; 21354007Smmusante dsl_sync_task_t *dst; 21364007Smmusante spa_t *spa; 21374007Smmusante char *cp, *fsname = spa_strdup(oldname); 21384007Smmusante int len = strlen(oldname); 21394007Smmusante 21404007Smmusante /* truncate the snapshot name to get the fsname */ 21414007Smmusante cp = strchr(fsname, '@'); 21424007Smmusante *cp = '\0'; 21434007Smmusante 21444603Sahrens err = spa_open(fsname, &spa, FTAG); 21454007Smmusante if (err) { 21464007Smmusante kmem_free(fsname, len + 1); 21474007Smmusante return (err); 21484007Smmusante } 21495326Sek110237 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 21504007Smmusante ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 21514007Smmusante 21524007Smmusante ra->oldsnap = strchr(oldname, '@') + 1; 21534007Smmusante ra->newsnap = strchr(newname, '@') + 1; 21544007Smmusante *ra->failed = '\0'; 21554007Smmusante 21564007Smmusante err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 21574007Smmusante DS_FIND_CHILDREN); 21584007Smmusante kmem_free(fsname, len + 1); 21594007Smmusante 21604007Smmusante if (err == 0) { 21614007Smmusante err = dsl_sync_task_group_wait(ra->dstg); 21624007Smmusante } 21634007Smmusante 21644007Smmusante for (dst = list_head(&ra->dstg->dstg_tasks); dst; 21654007Smmusante dst = list_next(&ra->dstg->dstg_tasks, dst)) { 21664007Smmusante dsl_dataset_t *ds = dst->dst_arg1; 21674007Smmusante if (dst->dst_err) { 21684007Smmusante dsl_dir_name(ds->ds_dir, ra->failed); 21694009Smmusante (void) strcat(ra->failed, "@"); 21704009Smmusante (void) strcat(ra->failed, ra->newsnap); 21714007Smmusante } 21726689Smaybee dsl_dataset_rele(ds, ra->dstg); 21734007Smmusante } 21744007Smmusante 21754543Smarks if (err) 21764543Smarks (void) strcpy(oldname, ra->failed); 21774007Smmusante 21784007Smmusante dsl_sync_task_group_destroy(ra->dstg); 21795326Sek110237 kmem_free(ra, sizeof (struct renamesnaparg)); 21804007Smmusante spa_close(spa, FTAG); 21814007Smmusante return (err); 21824007Smmusante } 21834007Smmusante 21844569Smmusante static int 21854569Smmusante dsl_valid_rename(char *oldname, void *arg) 21864569Smmusante { 21874569Smmusante int delta = *(int *)arg; 21884569Smmusante 21894569Smmusante if (strlen(oldname) + delta >= MAXNAMELEN) 21904569Smmusante return (ENAMETOOLONG); 21914569Smmusante 21924569Smmusante return (0); 21934569Smmusante } 21944569Smmusante 2195789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename 2196789Sahrens int 21976689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2198789Sahrens { 2199789Sahrens dsl_dir_t *dd; 22002199Sahrens dsl_dataset_t *ds; 2201789Sahrens const char *tail; 2202789Sahrens int err; 2203789Sahrens 22042199Sahrens err = dsl_dir_open(oldname, FTAG, &dd, &tail); 22051544Seschrock if (err) 22061544Seschrock return (err); 22078517SEric.Taylor@Sun.COM /* 22088517SEric.Taylor@Sun.COM * If there are more than 2 references there may be holds 22098517SEric.Taylor@Sun.COM * hanging around that haven't been cleared out yet. 22108517SEric.Taylor@Sun.COM */ 22118517SEric.Taylor@Sun.COM if (dmu_buf_refcount(dd->dd_dbuf) > 2) 22128517SEric.Taylor@Sun.COM txg_wait_synced(dd->dd_pool, 0); 2213789Sahrens if (tail == NULL) { 22144569Smmusante int delta = strlen(newname) - strlen(oldname); 22154569Smmusante 22167046Sahrens /* if we're growing, validate child name lengths */ 22174569Smmusante if (delta > 0) 22184569Smmusante err = dmu_objset_find(oldname, dsl_valid_rename, 22194569Smmusante &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 22204569Smmusante 22214569Smmusante if (!err) 22224569Smmusante err = dsl_dir_rename(dd, newname); 2223789Sahrens dsl_dir_close(dd, FTAG); 2224789Sahrens return (err); 2225789Sahrens } 2226789Sahrens if (tail[0] != '@') { 2227789Sahrens /* the name ended in a nonexistant component */ 2228789Sahrens dsl_dir_close(dd, FTAG); 2229789Sahrens return (ENOENT); 2230789Sahrens } 2231789Sahrens 22322199Sahrens dsl_dir_close(dd, FTAG); 22332199Sahrens 22342199Sahrens /* new name must be snapshot in same filesystem */ 22352199Sahrens tail = strchr(newname, '@'); 22362199Sahrens if (tail == NULL) 22372199Sahrens return (EINVAL); 22382199Sahrens tail++; 22392199Sahrens if (strncmp(oldname, newname, tail - newname) != 0) 22402199Sahrens return (EXDEV); 2241789Sahrens 22424007Smmusante if (recursive) { 22434007Smmusante err = dsl_recursive_rename(oldname, newname); 22444007Smmusante } else { 22456689Smaybee err = dsl_dataset_hold(oldname, FTAG, &ds); 22464007Smmusante if (err) 22474007Smmusante return (err); 22482199Sahrens 22494007Smmusante err = dsl_sync_task_do(ds->ds_dir->dd_pool, 22504007Smmusante dsl_dataset_snapshot_rename_check, 22514007Smmusante dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 22522199Sahrens 22536689Smaybee dsl_dataset_rele(ds, FTAG); 22544007Smmusante } 22552199Sahrens 2256789Sahrens return (err); 2257789Sahrens } 22582082Seschrock 22597046Sahrens struct promotenode { 22606689Smaybee list_node_t link; 22616689Smaybee dsl_dataset_t *ds; 22626689Smaybee }; 22636689Smaybee 22642199Sahrens struct promotearg { 22657390SMatthew.Ahrens@Sun.COM list_t shared_snaps, origin_snaps, clone_snaps; 22667390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_origin, *origin_head; 22677390SMatthew.Ahrens@Sun.COM uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 22682199Sahrens }; 22692199Sahrens 22707390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 22717390SMatthew.Ahrens@Sun.COM 22724543Smarks /* ARGSUSED */ 22732082Seschrock static int 22742199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 22752082Seschrock { 22762199Sahrens dsl_dataset_t *hds = arg1; 22772199Sahrens struct promotearg *pa = arg2; 22787390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 22796689Smaybee dsl_dataset_t *origin_ds = snap->ds; 22806689Smaybee int err; 22812199Sahrens 22827046Sahrens /* Check that it is a real clone */ 22837046Sahrens if (!dsl_dir_is_clone(hds->ds_dir)) 22842082Seschrock return (EINVAL); 22852082Seschrock 22862199Sahrens /* Since this is so expensive, don't do the preliminary check */ 22872199Sahrens if (!dmu_tx_is_syncing(tx)) 22882199Sahrens return (0); 22892199Sahrens 22906689Smaybee if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 22916689Smaybee return (EXDEV); 22922082Seschrock 22935367Sahrens /* compute origin's new unique space */ 22947390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 22957390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 22967390SMatthew.Ahrens@Sun.COM err = bplist_space_birthrange(&snap->ds->ds_deadlist, 22977390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique); 22987390SMatthew.Ahrens@Sun.COM if (err) 22996689Smaybee return (err); 23006689Smaybee 23016689Smaybee /* 23026689Smaybee * Walk the snapshots that we are moving 23036689Smaybee * 23047390SMatthew.Ahrens@Sun.COM * Compute space to transfer. Consider the incremental changes 23057390SMatthew.Ahrens@Sun.COM * to used for each snapshot: 23067390SMatthew.Ahrens@Sun.COM * (my used) = (prev's used) + (blocks born) - (blocks killed) 23077390SMatthew.Ahrens@Sun.COM * So each snapshot gave birth to: 23087390SMatthew.Ahrens@Sun.COM * (blocks born) = (my used) - (prev's used) + (blocks killed) 23096689Smaybee * So a sequence would look like: 23107390SMatthew.Ahrens@Sun.COM * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 23116689Smaybee * Which simplifies to: 23127390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + k1 + k0 23136689Smaybee * Note however, if we stop before we reach the ORIGIN we get: 23147390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + kM - uM-1 23156689Smaybee */ 23166689Smaybee pa->used = origin_ds->ds_phys->ds_used_bytes; 23176689Smaybee pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 23186689Smaybee pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 23197390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 23207390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 23212082Seschrock uint64_t val, dlused, dlcomp, dluncomp; 23226689Smaybee dsl_dataset_t *ds = snap->ds; 23232082Seschrock 23242082Seschrock /* Check that the snapshot name does not conflict */ 23257390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 23266689Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 23276689Smaybee if (err == 0) 23287390SMatthew.Ahrens@Sun.COM return (EEXIST); 23296689Smaybee if (err != ENOENT) 23307390SMatthew.Ahrens@Sun.COM return (err); 23316689Smaybee 23326689Smaybee /* The very first snapshot does not have a deadlist */ 23337390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_obj == 0) 23347390SMatthew.Ahrens@Sun.COM continue; 23357390SMatthew.Ahrens@Sun.COM 23367390SMatthew.Ahrens@Sun.COM if (err = bplist_space(&ds->ds_deadlist, 23377390SMatthew.Ahrens@Sun.COM &dlused, &dlcomp, &dluncomp)) 23387390SMatthew.Ahrens@Sun.COM return (err); 23397390SMatthew.Ahrens@Sun.COM pa->used += dlused; 23407390SMatthew.Ahrens@Sun.COM pa->comp += dlcomp; 23417390SMatthew.Ahrens@Sun.COM pa->uncomp += dluncomp; 23427390SMatthew.Ahrens@Sun.COM } 23436689Smaybee 23446689Smaybee /* 23456689Smaybee * If we are a clone of a clone then we never reached ORIGIN, 23466689Smaybee * so we need to subtract out the clone origin's used space. 23476689Smaybee */ 23487390SMatthew.Ahrens@Sun.COM if (pa->origin_origin) { 23497390SMatthew.Ahrens@Sun.COM pa->used -= pa->origin_origin->ds_phys->ds_used_bytes; 23507390SMatthew.Ahrens@Sun.COM pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes; 23517390SMatthew.Ahrens@Sun.COM pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes; 23522082Seschrock } 23532082Seschrock 23542082Seschrock /* Check that there is enough space here */ 23557390SMatthew.Ahrens@Sun.COM err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 23567390SMatthew.Ahrens@Sun.COM pa->used); 23577390SMatthew.Ahrens@Sun.COM if (err) 23587390SMatthew.Ahrens@Sun.COM return (err); 23597390SMatthew.Ahrens@Sun.COM 23607390SMatthew.Ahrens@Sun.COM /* 23617390SMatthew.Ahrens@Sun.COM * Compute the amounts of space that will be used by snapshots 23627390SMatthew.Ahrens@Sun.COM * after the promotion (for both origin and clone). For each, 23637390SMatthew.Ahrens@Sun.COM * it is the amount of space that will be on all of their 23647390SMatthew.Ahrens@Sun.COM * deadlists (that was not born before their new origin). 23657390SMatthew.Ahrens@Sun.COM */ 23667390SMatthew.Ahrens@Sun.COM if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 23677390SMatthew.Ahrens@Sun.COM uint64_t space; 23687390SMatthew.Ahrens@Sun.COM 23697390SMatthew.Ahrens@Sun.COM /* 23707390SMatthew.Ahrens@Sun.COM * Note, typically this will not be a clone of a clone, 23717390SMatthew.Ahrens@Sun.COM * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so 23727390SMatthew.Ahrens@Sun.COM * these snaplist_space() -> bplist_space_birthrange() 23737390SMatthew.Ahrens@Sun.COM * calls will be fast because they do not have to 23747390SMatthew.Ahrens@Sun.COM * iterate over all bps. 23757390SMatthew.Ahrens@Sun.COM */ 23767390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 23777390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->shared_snaps, 23787390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &pa->cloneusedsnap); 23797390SMatthew.Ahrens@Sun.COM if (err) 23807390SMatthew.Ahrens@Sun.COM return (err); 23817390SMatthew.Ahrens@Sun.COM 23827390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->clone_snaps, 23837390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &space); 23847390SMatthew.Ahrens@Sun.COM if (err) 23857390SMatthew.Ahrens@Sun.COM return (err); 23867390SMatthew.Ahrens@Sun.COM pa->cloneusedsnap += space; 23876689Smaybee } 23887390SMatthew.Ahrens@Sun.COM if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 23897390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->origin_snaps, 23907390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap); 23917390SMatthew.Ahrens@Sun.COM if (err) 23927390SMatthew.Ahrens@Sun.COM return (err); 23937390SMatthew.Ahrens@Sun.COM } 23947390SMatthew.Ahrens@Sun.COM 23957390SMatthew.Ahrens@Sun.COM return (0); 23962199Sahrens } 23972082Seschrock 23982199Sahrens static void 23994543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 24002199Sahrens { 24012199Sahrens dsl_dataset_t *hds = arg1; 24022199Sahrens struct promotearg *pa = arg2; 24037390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 24046689Smaybee dsl_dataset_t *origin_ds = snap->ds; 24057390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_head; 24062199Sahrens dsl_dir_t *dd = hds->ds_dir; 24072199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 24085367Sahrens dsl_dir_t *odd = NULL; 24097046Sahrens uint64_t oldnext_obj; 24107390SMatthew.Ahrens@Sun.COM int64_t delta; 24112199Sahrens 24122199Sahrens ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 24132199Sahrens 24147390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 24157390SMatthew.Ahrens@Sun.COM origin_head = snap->ds; 24167390SMatthew.Ahrens@Sun.COM 24172417Sahrens /* 24185367Sahrens * We need to explicitly open odd, since origin_ds's dd will be 24192417Sahrens * changing. 24202417Sahrens */ 24215367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 24225367Sahrens NULL, FTAG, &odd)); 24232082Seschrock 24246689Smaybee /* change origin's next snap */ 24256689Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 24267046Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 24277390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 24287390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 24297390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 24306689Smaybee 24317046Sahrens /* change the origin's next clone */ 24327046Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 24337046Sahrens VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset, 24347046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 24357390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_next_snap_obj, tx)); 24367046Sahrens VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 24377046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 24387046Sahrens oldnext_obj, tx)); 24397046Sahrens } 24407046Sahrens 24416689Smaybee /* change origin */ 24426689Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 24436689Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 24446689Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 24457390SMatthew.Ahrens@Sun.COM hds->ds_origin_txg = origin_head->ds_origin_txg; 24466689Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 24476689Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 24487390SMatthew.Ahrens@Sun.COM origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg; 24496689Smaybee 24502082Seschrock /* move snapshots to this dir */ 24517390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 24527390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 24536689Smaybee dsl_dataset_t *ds = snap->ds; 24542082Seschrock 24557237Sek110237 /* unregister props as dsl_dir is changing */ 24567237Sek110237 if (ds->ds_user_ptr) { 24577237Sek110237 ds->ds_user_evict_func(ds, ds->ds_user_ptr); 24587237Sek110237 ds->ds_user_ptr = NULL; 24597237Sek110237 } 24602082Seschrock /* move snap name entry */ 24617390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 24627390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_snap_remove(origin_head, 24636689Smaybee ds->ds_snapname, tx)); 24642199Sahrens VERIFY(0 == zap_add(dp->dp_meta_objset, 24652082Seschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 24662082Seschrock 8, 1, &ds->ds_object, tx)); 24672082Seschrock /* change containing dsl_dir */ 24682082Seschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 24695367Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 24702082Seschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 24715367Sahrens ASSERT3P(ds->ds_dir, ==, odd); 24722082Seschrock dsl_dir_close(ds->ds_dir, ds); 24732199Sahrens VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 24742082Seschrock NULL, ds, &ds->ds_dir)); 24752082Seschrock 24762082Seschrock ASSERT3U(dsl_prop_numcb(ds), ==, 0); 24777390SMatthew.Ahrens@Sun.COM } 24787390SMatthew.Ahrens@Sun.COM 24797390SMatthew.Ahrens@Sun.COM /* 24807390SMatthew.Ahrens@Sun.COM * Change space accounting. 24817390SMatthew.Ahrens@Sun.COM * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 24827390SMatthew.Ahrens@Sun.COM * both be valid, or both be 0 (resulting in delta == 0). This 24837390SMatthew.Ahrens@Sun.COM * is true for each of {clone,origin} independently. 24847390SMatthew.Ahrens@Sun.COM */ 24857390SMatthew.Ahrens@Sun.COM 24867390SMatthew.Ahrens@Sun.COM delta = pa->cloneusedsnap - 24877390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 24887390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, >=, 0); 24897390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, delta); 24907390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 24917390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_HEAD, 24927390SMatthew.Ahrens@Sun.COM pa->used - delta, pa->comp, pa->uncomp, tx); 24937390SMatthew.Ahrens@Sun.COM 24947390SMatthew.Ahrens@Sun.COM delta = pa->originusedsnap - 24957390SMatthew.Ahrens@Sun.COM odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 24967390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, <=, 0); 24977390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, -delta); 24987390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 24997390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_HEAD, 25007390SMatthew.Ahrens@Sun.COM -pa->used - delta, -pa->comp, -pa->uncomp, tx); 25017390SMatthew.Ahrens@Sun.COM 25025367Sahrens origin_ds->ds_phys->ds_unique_bytes = pa->unique; 25032082Seschrock 25044543Smarks /* log history record */ 25054543Smarks spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 25066689Smaybee cr, "dataset = %llu", hds->ds_object); 25074543Smarks 25085367Sahrens dsl_dir_close(odd, FTAG); 25092082Seschrock } 25102082Seschrock 25117390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist"; 25127390SMatthew.Ahrens@Sun.COM /* 25137390SMatthew.Ahrens@Sun.COM * Make a list of dsl_dataset_t's for the snapshots between first_obj 25147390SMatthew.Ahrens@Sun.COM * (exclusive) and last_obj (inclusive). The list will be in reverse 25157390SMatthew.Ahrens@Sun.COM * order (last_obj will be the list_head()). If first_obj == 0, do all 25167390SMatthew.Ahrens@Sun.COM * snapshots back to this dataset's origin. 25177390SMatthew.Ahrens@Sun.COM */ 25187390SMatthew.Ahrens@Sun.COM static int 25197390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own, 25207390SMatthew.Ahrens@Sun.COM uint64_t first_obj, uint64_t last_obj, list_t *l) 25217390SMatthew.Ahrens@Sun.COM { 25227390SMatthew.Ahrens@Sun.COM uint64_t obj = last_obj; 25237390SMatthew.Ahrens@Sun.COM 25247390SMatthew.Ahrens@Sun.COM ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock)); 25257390SMatthew.Ahrens@Sun.COM 25267390SMatthew.Ahrens@Sun.COM list_create(l, sizeof (struct promotenode), 25277390SMatthew.Ahrens@Sun.COM offsetof(struct promotenode, link)); 25287390SMatthew.Ahrens@Sun.COM 25297390SMatthew.Ahrens@Sun.COM while (obj != first_obj) { 25307390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 25317390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 25327390SMatthew.Ahrens@Sun.COM int err; 25337390SMatthew.Ahrens@Sun.COM 25347390SMatthew.Ahrens@Sun.COM if (own) { 25357390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, obj, 25367390SMatthew.Ahrens@Sun.COM 0, snaplist_tag, &ds); 25377390SMatthew.Ahrens@Sun.COM if (err == 0) 25387390SMatthew.Ahrens@Sun.COM dsl_dataset_make_exclusive(ds, snaplist_tag); 25397390SMatthew.Ahrens@Sun.COM } else { 25407390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds); 25417390SMatthew.Ahrens@Sun.COM } 25427390SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 25437390SMatthew.Ahrens@Sun.COM /* lost race with snapshot destroy */ 25447390SMatthew.Ahrens@Sun.COM struct promotenode *last = list_tail(l); 25457390SMatthew.Ahrens@Sun.COM ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj); 25467390SMatthew.Ahrens@Sun.COM obj = last->ds->ds_phys->ds_prev_snap_obj; 25477390SMatthew.Ahrens@Sun.COM continue; 25487390SMatthew.Ahrens@Sun.COM } else if (err) { 25497390SMatthew.Ahrens@Sun.COM return (err); 25507390SMatthew.Ahrens@Sun.COM } 25517390SMatthew.Ahrens@Sun.COM 25527390SMatthew.Ahrens@Sun.COM if (first_obj == 0) 25537390SMatthew.Ahrens@Sun.COM first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 25547390SMatthew.Ahrens@Sun.COM 25557390SMatthew.Ahrens@Sun.COM snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 25567390SMatthew.Ahrens@Sun.COM snap->ds = ds; 25577390SMatthew.Ahrens@Sun.COM list_insert_tail(l, snap); 25587390SMatthew.Ahrens@Sun.COM obj = ds->ds_phys->ds_prev_snap_obj; 25597390SMatthew.Ahrens@Sun.COM } 25607390SMatthew.Ahrens@Sun.COM 25617390SMatthew.Ahrens@Sun.COM return (0); 25627390SMatthew.Ahrens@Sun.COM } 25637390SMatthew.Ahrens@Sun.COM 25647390SMatthew.Ahrens@Sun.COM static int 25657390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 25667390SMatthew.Ahrens@Sun.COM { 25677390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 25687390SMatthew.Ahrens@Sun.COM 25697390SMatthew.Ahrens@Sun.COM *spacep = 0; 25707390SMatthew.Ahrens@Sun.COM for (snap = list_head(l); snap; snap = list_next(l, snap)) { 25717390SMatthew.Ahrens@Sun.COM uint64_t used; 25727390SMatthew.Ahrens@Sun.COM int err = bplist_space_birthrange(&snap->ds->ds_deadlist, 25737390SMatthew.Ahrens@Sun.COM mintxg, UINT64_MAX, &used); 25747390SMatthew.Ahrens@Sun.COM if (err) 25757390SMatthew.Ahrens@Sun.COM return (err); 25767390SMatthew.Ahrens@Sun.COM *spacep += used; 25777390SMatthew.Ahrens@Sun.COM } 25787390SMatthew.Ahrens@Sun.COM return (0); 25797390SMatthew.Ahrens@Sun.COM } 25807390SMatthew.Ahrens@Sun.COM 25817390SMatthew.Ahrens@Sun.COM static void 25827390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own) 25837390SMatthew.Ahrens@Sun.COM { 25847390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 25857390SMatthew.Ahrens@Sun.COM 25867390SMatthew.Ahrens@Sun.COM if (!list_link_active(&l->list_head)) 25877390SMatthew.Ahrens@Sun.COM return; 25887390SMatthew.Ahrens@Sun.COM 25897390SMatthew.Ahrens@Sun.COM while ((snap = list_tail(l)) != NULL) { 25907390SMatthew.Ahrens@Sun.COM list_remove(l, snap); 25917390SMatthew.Ahrens@Sun.COM if (own) 25927390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(snap->ds, snaplist_tag); 25937390SMatthew.Ahrens@Sun.COM else 25947390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(snap->ds, snaplist_tag); 25957390SMatthew.Ahrens@Sun.COM kmem_free(snap, sizeof (struct promotenode)); 25967390SMatthew.Ahrens@Sun.COM } 25977390SMatthew.Ahrens@Sun.COM list_destroy(l); 25987390SMatthew.Ahrens@Sun.COM } 25997390SMatthew.Ahrens@Sun.COM 26007390SMatthew.Ahrens@Sun.COM /* 26017390SMatthew.Ahrens@Sun.COM * Promote a clone. Nomenclature note: 26027390SMatthew.Ahrens@Sun.COM * "clone" or "cds": the original clone which is being promoted 26037390SMatthew.Ahrens@Sun.COM * "origin" or "ods": the snapshot which is originally clone's origin 26047390SMatthew.Ahrens@Sun.COM * "origin head" or "ohds": the dataset which is the head 26057390SMatthew.Ahrens@Sun.COM * (filesystem/volume) for the origin 26067390SMatthew.Ahrens@Sun.COM * "origin origin": the origin of the origin's filesystem (typically 26077390SMatthew.Ahrens@Sun.COM * NULL, indicating that the clone is not a clone of a clone). 26087390SMatthew.Ahrens@Sun.COM */ 26092082Seschrock int 26102082Seschrock dsl_dataset_promote(const char *name) 26112082Seschrock { 26122082Seschrock dsl_dataset_t *ds; 26136689Smaybee dsl_dir_t *dd; 26146689Smaybee dsl_pool_t *dp; 26152082Seschrock dmu_object_info_t doi; 26167390SMatthew.Ahrens@Sun.COM struct promotearg pa = { 0 }; 26177046Sahrens struct promotenode *snap; 26186689Smaybee int err; 26196689Smaybee 26206689Smaybee err = dsl_dataset_hold(name, FTAG, &ds); 26212082Seschrock if (err) 26222082Seschrock return (err); 26236689Smaybee dd = ds->ds_dir; 26246689Smaybee dp = dd->dd_pool; 26256689Smaybee 26266689Smaybee err = dmu_object_info(dp->dp_meta_objset, 26272082Seschrock ds->ds_phys->ds_snapnames_zapobj, &doi); 26282082Seschrock if (err) { 26296689Smaybee dsl_dataset_rele(ds, FTAG); 26302082Seschrock return (err); 26312082Seschrock } 26322082Seschrock 26337390SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) { 26347390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 26357390SMatthew.Ahrens@Sun.COM return (EINVAL); 26367390SMatthew.Ahrens@Sun.COM } 26377390SMatthew.Ahrens@Sun.COM 26382082Seschrock /* 26396689Smaybee * We are going to inherit all the snapshots taken before our 26406689Smaybee * origin (i.e., our new origin will be our parent's origin). 26416689Smaybee * Take ownership of them so that we can rename them into our 26426689Smaybee * namespace. 26436689Smaybee */ 26446689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 26457390SMatthew.Ahrens@Sun.COM 26467390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj, 26477390SMatthew.Ahrens@Sun.COM &pa.shared_snaps); 26487390SMatthew.Ahrens@Sun.COM if (err != 0) 26497390SMatthew.Ahrens@Sun.COM goto out; 26507390SMatthew.Ahrens@Sun.COM 26517390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps); 26527390SMatthew.Ahrens@Sun.COM if (err != 0) 26537390SMatthew.Ahrens@Sun.COM goto out; 26547390SMatthew.Ahrens@Sun.COM 26557390SMatthew.Ahrens@Sun.COM snap = list_head(&pa.shared_snaps); 26567390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 26577390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj, 26587390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps); 26597390SMatthew.Ahrens@Sun.COM if (err != 0) 26607390SMatthew.Ahrens@Sun.COM goto out; 26617390SMatthew.Ahrens@Sun.COM 26627390SMatthew.Ahrens@Sun.COM if (dsl_dir_is_clone(snap->ds->ds_dir)) { 26637390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, 26647390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_origin_obj, 26657390SMatthew.Ahrens@Sun.COM 0, FTAG, &pa.origin_origin); 26667390SMatthew.Ahrens@Sun.COM if (err != 0) 26676689Smaybee goto out; 26686689Smaybee } 26697390SMatthew.Ahrens@Sun.COM 26707390SMatthew.Ahrens@Sun.COM out: 26716689Smaybee rw_exit(&dp->dp_config_rwlock); 26726689Smaybee 26736689Smaybee /* 26742082Seschrock * Add in 128x the snapnames zapobj size, since we will be moving 26752082Seschrock * a bunch of snapnames to the promoted ds, and dirtying their 26762082Seschrock * bonus buffers. 26772082Seschrock */ 26787390SMatthew.Ahrens@Sun.COM if (err == 0) { 26797390SMatthew.Ahrens@Sun.COM err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 26807390SMatthew.Ahrens@Sun.COM dsl_dataset_promote_sync, ds, &pa, 26817390SMatthew.Ahrens@Sun.COM 2 + 2 * doi.doi_physical_blks); 26826689Smaybee } 26837390SMatthew.Ahrens@Sun.COM 26847390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.shared_snaps, B_TRUE); 26857390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.clone_snaps, B_FALSE); 26867390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.origin_snaps, B_FALSE); 26877390SMatthew.Ahrens@Sun.COM if (pa.origin_origin) 26887390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(pa.origin_origin, FTAG); 26896689Smaybee dsl_dataset_rele(ds, FTAG); 26902082Seschrock return (err); 26912082Seschrock } 26923912Slling 26935367Sahrens struct cloneswaparg { 26945367Sahrens dsl_dataset_t *cds; /* clone dataset */ 26955367Sahrens dsl_dataset_t *ohds; /* origin's head dataset */ 26965367Sahrens boolean_t force; 26975481Sck153898 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 26985367Sahrens }; 26995326Sek110237 27005326Sek110237 /* ARGSUSED */ 27015326Sek110237 static int 27025326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 27035326Sek110237 { 27045367Sahrens struct cloneswaparg *csa = arg1; 27055326Sek110237 27065367Sahrens /* they should both be heads */ 27075367Sahrens if (dsl_dataset_is_snapshot(csa->cds) || 27085367Sahrens dsl_dataset_is_snapshot(csa->ohds)) 27095326Sek110237 return (EINVAL); 27105326Sek110237 27115367Sahrens /* the branch point should be just before them */ 27125367Sahrens if (csa->cds->ds_prev != csa->ohds->ds_prev) 27135326Sek110237 return (EINVAL); 27145326Sek110237 27155367Sahrens /* cds should be the clone */ 27165367Sahrens if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj != 27175367Sahrens csa->ohds->ds_object) 27185367Sahrens return (EINVAL); 27195326Sek110237 27205367Sahrens /* the clone should be a child of the origin */ 27215367Sahrens if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 27225367Sahrens return (EINVAL); 27235326Sek110237 27245367Sahrens /* ohds shouldn't be modified unless 'force' */ 27255367Sahrens if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 27265367Sahrens return (ETXTBSY); 27275481Sck153898 27285481Sck153898 /* adjust amount of any unconsumed refreservation */ 27295481Sck153898 csa->unused_refres_delta = 27305481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 27315481Sck153898 csa->ohds->ds_phys->ds_unique_bytes) - 27325481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 27335481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 27345481Sck153898 27355481Sck153898 if (csa->unused_refres_delta > 0 && 27365481Sck153898 csa->unused_refres_delta > 27375481Sck153898 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 27385481Sck153898 return (ENOSPC); 27395481Sck153898 27405367Sahrens return (0); 27415326Sek110237 } 27425326Sek110237 27435326Sek110237 /* ARGSUSED */ 27445326Sek110237 static void 27455326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 27465326Sek110237 { 27475367Sahrens struct cloneswaparg *csa = arg1; 27485367Sahrens dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 27495326Sek110237 27505481Sck153898 ASSERT(csa->cds->ds_reserved == 0); 27515481Sck153898 ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota); 27525481Sck153898 27535367Sahrens dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 27545367Sahrens dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 27555367Sahrens dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx); 27565326Sek110237 27575367Sahrens if (csa->cds->ds_user_ptr != NULL) { 27585367Sahrens csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr); 27595367Sahrens csa->cds->ds_user_ptr = NULL; 27605367Sahrens } 27615326Sek110237 27625367Sahrens if (csa->ohds->ds_user_ptr != NULL) { 27635367Sahrens csa->ohds->ds_user_evict_func(csa->ohds, 27645367Sahrens csa->ohds->ds_user_ptr); 27655367Sahrens csa->ohds->ds_user_ptr = NULL; 27665367Sahrens } 27675326Sek110237 27685326Sek110237 /* reset origin's unique bytes */ 27697390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 27707390SMatthew.Ahrens@Sun.COM csa->cds->ds_prev->ds_phys->ds_prev_snap_txg, UINT64_MAX, 27717390SMatthew.Ahrens@Sun.COM &csa->cds->ds_prev->ds_phys->ds_unique_bytes)); 27725326Sek110237 27735326Sek110237 /* swap blkptrs */ 27745326Sek110237 { 27755326Sek110237 blkptr_t tmp; 27765367Sahrens tmp = csa->ohds->ds_phys->ds_bp; 27775367Sahrens csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 27785367Sahrens csa->cds->ds_phys->ds_bp = tmp; 27795326Sek110237 } 27805326Sek110237 27815326Sek110237 /* set dd_*_bytes */ 27825326Sek110237 { 27835326Sek110237 int64_t dused, dcomp, duncomp; 27845326Sek110237 uint64_t cdl_used, cdl_comp, cdl_uncomp; 27855326Sek110237 uint64_t odl_used, odl_comp, odl_uncomp; 27865326Sek110237 27877390SMatthew.Ahrens@Sun.COM ASSERT3U(csa->cds->ds_dir->dd_phys-> 27887390SMatthew.Ahrens@Sun.COM dd_used_breakdown[DD_USED_SNAP], ==, 0); 27897390SMatthew.Ahrens@Sun.COM 27905367Sahrens VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 27915326Sek110237 &cdl_comp, &cdl_uncomp)); 27925367Sahrens VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 27935326Sek110237 &odl_comp, &odl_uncomp)); 27947390SMatthew.Ahrens@Sun.COM 27955367Sahrens dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 27965367Sahrens (csa->ohds->ds_phys->ds_used_bytes + odl_used); 27975367Sahrens dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 27985367Sahrens (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 27995367Sahrens duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 28005367Sahrens cdl_uncomp - 28015367Sahrens (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 28025326Sek110237 28037390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD, 28045367Sahrens dused, dcomp, duncomp, tx); 28057390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD, 28065367Sahrens -dused, -dcomp, -duncomp, tx); 28077390SMatthew.Ahrens@Sun.COM 28087390SMatthew.Ahrens@Sun.COM /* 28097390SMatthew.Ahrens@Sun.COM * The difference in the space used by snapshots is the 28107390SMatthew.Ahrens@Sun.COM * difference in snapshot space due to the head's 28117390SMatthew.Ahrens@Sun.COM * deadlist (since that's the only thing that's 28127390SMatthew.Ahrens@Sun.COM * changing that affects the snapused). 28137390SMatthew.Ahrens@Sun.COM */ 28147390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 28157390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used)); 28167390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist, 28177390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used)); 28187390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used, 28197390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 28205367Sahrens } 28215367Sahrens 28225367Sahrens #define SWITCH64(x, y) \ 28235367Sahrens { \ 28245367Sahrens uint64_t __tmp = (x); \ 28255367Sahrens (x) = (y); \ 28265367Sahrens (y) = __tmp; \ 28275326Sek110237 } 28285326Sek110237 28295326Sek110237 /* swap ds_*_bytes */ 28305367Sahrens SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 28315367Sahrens csa->cds->ds_phys->ds_used_bytes); 28325367Sahrens SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 28335367Sahrens csa->cds->ds_phys->ds_compressed_bytes); 28345367Sahrens SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 28355367Sahrens csa->cds->ds_phys->ds_uncompressed_bytes); 28365481Sck153898 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 28375481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 28385481Sck153898 28395481Sck153898 /* apply any parent delta for change in unconsumed refreservation */ 28407390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV, 28417390SMatthew.Ahrens@Sun.COM csa->unused_refres_delta, 0, 0, tx); 28425326Sek110237 28435326Sek110237 /* swap deadlists */ 28445367Sahrens bplist_close(&csa->cds->ds_deadlist); 28455367Sahrens bplist_close(&csa->ohds->ds_deadlist); 28465367Sahrens SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 28475367Sahrens csa->cds->ds_phys->ds_deadlist_obj); 28485367Sahrens VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 28495367Sahrens csa->cds->ds_phys->ds_deadlist_obj)); 28505367Sahrens VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 28515367Sahrens csa->ohds->ds_phys->ds_deadlist_obj)); 28527837SMatthew.Ahrens@Sun.COM 28537837SMatthew.Ahrens@Sun.COM dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx); 28545326Sek110237 } 28555326Sek110237 28565326Sek110237 /* 28576689Smaybee * Swap 'clone' with its origin head file system. Used at the end 28586689Smaybee * of "online recv" to swizzle the file system to the new version. 28595326Sek110237 */ 28605326Sek110237 int 28615367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 28625367Sahrens boolean_t force) 28635326Sek110237 { 28645367Sahrens struct cloneswaparg csa; 28656689Smaybee int error; 28666689Smaybee 28676689Smaybee ASSERT(clone->ds_owner); 28686689Smaybee ASSERT(origin_head->ds_owner); 28696689Smaybee retry: 28706689Smaybee /* Need exclusive access for the swap */ 28716689Smaybee rw_enter(&clone->ds_rwlock, RW_WRITER); 28726689Smaybee if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 28736689Smaybee rw_exit(&clone->ds_rwlock); 28746689Smaybee rw_enter(&origin_head->ds_rwlock, RW_WRITER); 28756689Smaybee if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 28766689Smaybee rw_exit(&origin_head->ds_rwlock); 28776689Smaybee goto retry; 28786689Smaybee } 28796689Smaybee } 28805367Sahrens csa.cds = clone; 28815367Sahrens csa.ohds = origin_head; 28825367Sahrens csa.force = force; 28836689Smaybee error = dsl_sync_task_do(clone->ds_dir->dd_pool, 28845326Sek110237 dsl_dataset_clone_swap_check, 28856689Smaybee dsl_dataset_clone_swap_sync, &csa, NULL, 9); 28866689Smaybee return (error); 28875326Sek110237 } 28885326Sek110237 28893912Slling /* 28903912Slling * Given a pool name and a dataset object number in that pool, 28913912Slling * return the name of that dataset. 28923912Slling */ 28933912Slling int 28943912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 28953912Slling { 28963912Slling spa_t *spa; 28973912Slling dsl_pool_t *dp; 28986689Smaybee dsl_dataset_t *ds; 28993912Slling int error; 29003912Slling 29013912Slling if ((error = spa_open(pname, &spa, FTAG)) != 0) 29023912Slling return (error); 29033912Slling dp = spa_get_dsl(spa); 29043912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 29056689Smaybee if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 29066689Smaybee dsl_dataset_name(ds, buf); 29076689Smaybee dsl_dataset_rele(ds, FTAG); 29083912Slling } 29093912Slling rw_exit(&dp->dp_config_rwlock); 29103912Slling spa_close(spa, FTAG); 29113912Slling 29126689Smaybee return (error); 29133912Slling } 29145378Sck153898 29155378Sck153898 int 29165378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 29176689Smaybee uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 29185378Sck153898 { 29195378Sck153898 int error = 0; 29205378Sck153898 29215378Sck153898 ASSERT3S(asize, >, 0); 29225378Sck153898 29235831Sck153898 /* 29245831Sck153898 * *ref_rsrv is the portion of asize that will come from any 29255831Sck153898 * unconsumed refreservation space. 29265831Sck153898 */ 29275831Sck153898 *ref_rsrv = 0; 29285831Sck153898 29295378Sck153898 mutex_enter(&ds->ds_lock); 29305378Sck153898 /* 29315378Sck153898 * Make a space adjustment for reserved bytes. 29325378Sck153898 */ 29335378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 29345378Sck153898 ASSERT3U(*used, >=, 29355378Sck153898 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 29365378Sck153898 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 29375831Sck153898 *ref_rsrv = 29385831Sck153898 asize - MIN(asize, parent_delta(ds, asize + inflight)); 29395378Sck153898 } 29405378Sck153898 29415378Sck153898 if (!check_quota || ds->ds_quota == 0) { 29425378Sck153898 mutex_exit(&ds->ds_lock); 29435378Sck153898 return (0); 29445378Sck153898 } 29455378Sck153898 /* 29465378Sck153898 * If they are requesting more space, and our current estimate 29475378Sck153898 * is over quota, they get to try again unless the actual 29485378Sck153898 * on-disk is over quota and there are no pending changes (which 29495378Sck153898 * may free up space for us). 29505378Sck153898 */ 29515378Sck153898 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 29525378Sck153898 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 29535378Sck153898 error = ERESTART; 29545378Sck153898 else 29555378Sck153898 error = EDQUOT; 29565378Sck153898 } 29575378Sck153898 mutex_exit(&ds->ds_lock); 29585378Sck153898 29595378Sck153898 return (error); 29605378Sck153898 } 29615378Sck153898 29625378Sck153898 /* ARGSUSED */ 29635378Sck153898 static int 29645378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 29655378Sck153898 { 29665378Sck153898 dsl_dataset_t *ds = arg1; 29675378Sck153898 uint64_t *quotap = arg2; 29685378Sck153898 uint64_t new_quota = *quotap; 29695378Sck153898 29705378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 29715378Sck153898 return (ENOTSUP); 29725378Sck153898 29735378Sck153898 if (new_quota == 0) 29745378Sck153898 return (0); 29755378Sck153898 29765378Sck153898 if (new_quota < ds->ds_phys->ds_used_bytes || 29775378Sck153898 new_quota < ds->ds_reserved) 29785378Sck153898 return (ENOSPC); 29795378Sck153898 29805378Sck153898 return (0); 29815378Sck153898 } 29825378Sck153898 29835378Sck153898 /* ARGSUSED */ 29845378Sck153898 void 29855378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 29865378Sck153898 { 29875378Sck153898 dsl_dataset_t *ds = arg1; 29885378Sck153898 uint64_t *quotap = arg2; 29895378Sck153898 uint64_t new_quota = *quotap; 29905378Sck153898 29915378Sck153898 dmu_buf_will_dirty(ds->ds_dbuf, tx); 29925378Sck153898 29935378Sck153898 ds->ds_quota = new_quota; 29945378Sck153898 29955378Sck153898 dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx); 29965378Sck153898 29975378Sck153898 spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa, 29985378Sck153898 tx, cr, "%lld dataset = %llu ", 29996689Smaybee (longlong_t)new_quota, ds->ds_object); 30005378Sck153898 } 30015378Sck153898 30025378Sck153898 int 30035378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota) 30045378Sck153898 { 30055378Sck153898 dsl_dataset_t *ds; 30065378Sck153898 int err; 30075378Sck153898 30086689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 30095378Sck153898 if (err) 30105378Sck153898 return (err); 30115378Sck153898 30125481Sck153898 if (quota != ds->ds_quota) { 30135481Sck153898 /* 30145481Sck153898 * If someone removes a file, then tries to set the quota, we 30155481Sck153898 * want to make sure the file freeing takes effect. 30165481Sck153898 */ 30175481Sck153898 txg_wait_open(ds->ds_dir->dd_pool, 0); 30185481Sck153898 30195481Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 30205481Sck153898 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 30215481Sck153898 ds, "a, 0); 30225481Sck153898 } 30236689Smaybee dsl_dataset_rele(ds, FTAG); 30245378Sck153898 return (err); 30255378Sck153898 } 30265378Sck153898 30275378Sck153898 static int 30285378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 30295378Sck153898 { 30305378Sck153898 dsl_dataset_t *ds = arg1; 30315378Sck153898 uint64_t *reservationp = arg2; 30325378Sck153898 uint64_t new_reservation = *reservationp; 30335378Sck153898 uint64_t unique; 30345378Sck153898 30355378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 30365378Sck153898 SPA_VERSION_REFRESERVATION) 30375378Sck153898 return (ENOTSUP); 30385378Sck153898 30395378Sck153898 if (dsl_dataset_is_snapshot(ds)) 30405378Sck153898 return (EINVAL); 30415378Sck153898 30425378Sck153898 /* 30435378Sck153898 * If we are doing the preliminary check in open context, the 30445378Sck153898 * space estimates may be inaccurate. 30455378Sck153898 */ 30465378Sck153898 if (!dmu_tx_is_syncing(tx)) 30475378Sck153898 return (0); 30485378Sck153898 30495378Sck153898 mutex_enter(&ds->ds_lock); 30505378Sck153898 unique = dsl_dataset_unique(ds); 30515378Sck153898 mutex_exit(&ds->ds_lock); 30525378Sck153898 3053*8525SEric.Schrock@Sun.COM if (MAX(unique, new_reservation) > MAX(unique, ds->ds_reserved)) { 3054*8525SEric.Schrock@Sun.COM uint64_t delta = MAX(unique, new_reservation) - 3055*8525SEric.Schrock@Sun.COM MAX(unique, ds->ds_reserved); 3056*8525SEric.Schrock@Sun.COM 3057*8525SEric.Schrock@Sun.COM if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 3058*8525SEric.Schrock@Sun.COM return (ENOSPC); 3059*8525SEric.Schrock@Sun.COM if (ds->ds_quota > 0 && 3060*8525SEric.Schrock@Sun.COM new_reservation > ds->ds_quota) 3061*8525SEric.Schrock@Sun.COM return (ENOSPC); 3062*8525SEric.Schrock@Sun.COM } 30635378Sck153898 30645378Sck153898 return (0); 30655378Sck153898 } 30665378Sck153898 30675378Sck153898 /* ARGSUSED */ 30685378Sck153898 static void 30695378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 30705378Sck153898 dmu_tx_t *tx) 30715378Sck153898 { 30725378Sck153898 dsl_dataset_t *ds = arg1; 30735378Sck153898 uint64_t *reservationp = arg2; 30745378Sck153898 uint64_t new_reservation = *reservationp; 30757595SMatthew.Ahrens@Sun.COM uint64_t unique; 30767595SMatthew.Ahrens@Sun.COM int64_t delta; 30777595SMatthew.Ahrens@Sun.COM 30787595SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx); 30797595SMatthew.Ahrens@Sun.COM 30807595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 30817595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_lock); 30827595SMatthew.Ahrens@Sun.COM unique = dsl_dataset_unique(ds); 30837595SMatthew.Ahrens@Sun.COM delta = MAX(0, (int64_t)(new_reservation - unique)) - 30847595SMatthew.Ahrens@Sun.COM MAX(0, (int64_t)(ds->ds_reserved - unique)); 30857595SMatthew.Ahrens@Sun.COM ds->ds_reserved = new_reservation; 30867595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_lock); 30877595SMatthew.Ahrens@Sun.COM 30887595SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 30897595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 30907595SMatthew.Ahrens@Sun.COM dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation", 30917595SMatthew.Ahrens@Sun.COM new_reservation, cr, tx); 30927595SMatthew.Ahrens@Sun.COM 30937595SMatthew.Ahrens@Sun.COM spa_history_internal_log(LOG_DS_REFRESERV, 30947595SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 30957595SMatthew.Ahrens@Sun.COM (longlong_t)new_reservation, ds->ds_object); 30965378Sck153898 } 30975378Sck153898 30985378Sck153898 int 30995378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation) 31005378Sck153898 { 31015378Sck153898 dsl_dataset_t *ds; 31025378Sck153898 int err; 31035378Sck153898 31046689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 31055378Sck153898 if (err) 31065378Sck153898 return (err); 31075378Sck153898 31085378Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 31095378Sck153898 dsl_dataset_set_reservation_check, 31105378Sck153898 dsl_dataset_set_reservation_sync, ds, &reservation, 0); 31116689Smaybee dsl_dataset_rele(ds, FTAG); 31125378Sck153898 return (err); 31135378Sck153898 } 3114