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> 4210242Schris.kirby@sun.com #include <sys/zvol.h> 43789Sahrens 446689Smaybee static char *dsl_reaper = "the grim reaper"; 456689Smaybee 462199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check; 472199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync; 485378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync; 491731Sbonwick 503444Sek110237 #define DS_REF_MAX (1ULL << 62) 51789Sahrens 52789Sahrens #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 53789Sahrens 546689Smaybee #define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper) 556689Smaybee 56789Sahrens 575378Sck153898 /* 585378Sck153898 * Figure out how much of this delta should be propogated to the dsl_dir 595378Sck153898 * layer. If there's a refreservation, that space has already been 605378Sck153898 * partially accounted for in our ancestors. 615378Sck153898 */ 625378Sck153898 static int64_t 635378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta) 645378Sck153898 { 655378Sck153898 uint64_t old_bytes, new_bytes; 665378Sck153898 675378Sck153898 if (ds->ds_reserved == 0) 685378Sck153898 return (delta); 695378Sck153898 705378Sck153898 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 715378Sck153898 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 725378Sck153898 735378Sck153898 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 745378Sck153898 return (new_bytes - old_bytes); 755378Sck153898 } 76789Sahrens 77789Sahrens void 78789Sahrens dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 79789Sahrens { 802082Seschrock int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 81789Sahrens int compressed = BP_GET_PSIZE(bp); 82789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 835378Sck153898 int64_t delta; 84789Sahrens 85789Sahrens dprintf_bp(bp, "born, ds=%p\n", ds); 86789Sahrens 87789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 88789Sahrens /* It could have been compressed away to nothing */ 89789Sahrens if (BP_IS_HOLE(bp)) 90789Sahrens return; 91789Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 92789Sahrens ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 93789Sahrens if (ds == NULL) { 94789Sahrens /* 95789Sahrens * Account for the meta-objset space in its placeholder 96789Sahrens * dsl_dir. 97789Sahrens */ 98789Sahrens ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 997390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 100789Sahrens used, compressed, uncompressed, tx); 101789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 102789Sahrens return; 103789Sahrens } 104789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1057595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 106789Sahrens mutex_enter(&ds->ds_lock); 1075378Sck153898 delta = parent_delta(ds, used); 108789Sahrens ds->ds_phys->ds_used_bytes += used; 109789Sahrens ds->ds_phys->ds_compressed_bytes += compressed; 110789Sahrens ds->ds_phys->ds_uncompressed_bytes += uncompressed; 111789Sahrens ds->ds_phys->ds_unique_bytes += used; 112789Sahrens mutex_exit(&ds->ds_lock); 1137390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 1147390SMatthew.Ahrens@Sun.COM compressed, uncompressed, tx); 1157390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used - delta, 1167390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1177595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 118789Sahrens } 119789Sahrens 1206992Smaybee int 1213547Smaybee dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio, 1223547Smaybee dmu_tx_t *tx) 123789Sahrens { 1242082Seschrock int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 125789Sahrens int compressed = BP_GET_PSIZE(bp); 126789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 127789Sahrens 1287754SJeff.Bonwick@Sun.COM ASSERT(pio != NULL); 129789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1303547Smaybee /* No block pointer => nothing to free */ 131789Sahrens if (BP_IS_HOLE(bp)) 1326992Smaybee return (0); 133789Sahrens 134789Sahrens ASSERT(used > 0); 135789Sahrens if (ds == NULL) { 1363547Smaybee int err; 137789Sahrens /* 138789Sahrens * Account for the meta-objset space in its placeholder 139789Sahrens * dataset. 140789Sahrens */ 1417046Sahrens err = dsl_free(pio, tx->tx_pool, 1427754SJeff.Bonwick@Sun.COM tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT); 1433547Smaybee ASSERT(err == 0); 144789Sahrens 1457390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 146789Sahrens -used, -compressed, -uncompressed, tx); 147789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 1486992Smaybee return (used); 149789Sahrens } 150789Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 151789Sahrens 1527390SMatthew.Ahrens@Sun.COM ASSERT(!dsl_dataset_is_snapshot(ds)); 153789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 154789Sahrens 155789Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 1563547Smaybee int err; 1575378Sck153898 int64_t delta; 1583547Smaybee 159789Sahrens dprintf_bp(bp, "freeing: %s", ""); 1607046Sahrens err = dsl_free(pio, tx->tx_pool, 1617754SJeff.Bonwick@Sun.COM tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT); 1623547Smaybee ASSERT(err == 0); 163789Sahrens 1647595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 165789Sahrens mutex_enter(&ds->ds_lock); 1665378Sck153898 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 1675378Sck153898 !DS_UNIQUE_IS_ACCURATE(ds)); 1685378Sck153898 delta = parent_delta(ds, -used); 169789Sahrens ds->ds_phys->ds_unique_bytes -= used; 170789Sahrens mutex_exit(&ds->ds_lock); 1717390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 1725378Sck153898 delta, -compressed, -uncompressed, tx); 1737390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, -used - delta, 1747390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1757595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 176789Sahrens } else { 177789Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 1781544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 1795712Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 1805712Sahrens ds->ds_phys->ds_prev_snap_obj); 1815712Sahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 182789Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 1835712Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 1845712Sahrens ds->ds_object && bp->blk_birth > 1855712Sahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 1865712Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1875712Sahrens mutex_enter(&ds->ds_prev->ds_lock); 1885712Sahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 1895712Sahrens mutex_exit(&ds->ds_prev->ds_lock); 190789Sahrens } 1917390SMatthew.Ahrens@Sun.COM if (bp->blk_birth > ds->ds_origin_txg) { 1927390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used, 1937390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 1947390SMatthew.Ahrens@Sun.COM } 195789Sahrens } 196789Sahrens mutex_enter(&ds->ds_lock); 197789Sahrens ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 198789Sahrens ds->ds_phys->ds_used_bytes -= used; 199789Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 200789Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 201789Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 202789Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 203789Sahrens mutex_exit(&ds->ds_lock); 2046992Smaybee 2056992Smaybee return (used); 206789Sahrens } 207789Sahrens 2081544Seschrock uint64_t 2091544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 210789Sahrens { 2112885Sahrens uint64_t trysnap = 0; 2122885Sahrens 213789Sahrens if (ds == NULL) 2141544Seschrock return (0); 215789Sahrens /* 216789Sahrens * The snapshot creation could fail, but that would cause an 217789Sahrens * incorrect FALSE return, which would only result in an 218789Sahrens * overestimation of the amount of space that an operation would 219789Sahrens * consume, which is OK. 220789Sahrens * 221789Sahrens * There's also a small window where we could miss a pending 222789Sahrens * snapshot, because we could set the sync task in the quiescing 223789Sahrens * phase. So this should only be used as a guess. 224789Sahrens */ 2252885Sahrens if (ds->ds_trysnap_txg > 2262885Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 2272885Sahrens trysnap = ds->ds_trysnap_txg; 2282885Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 2291544Seschrock } 2301544Seschrock 2319653SSanjeev.Bagewadi@Sun.COM boolean_t 2321544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 2331544Seschrock { 2341544Seschrock return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 235789Sahrens } 236789Sahrens 237789Sahrens /* ARGSUSED */ 238789Sahrens static void 239789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 240789Sahrens { 241789Sahrens dsl_dataset_t *ds = dsv; 242789Sahrens 2436689Smaybee ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 244789Sahrens 2454787Sahrens unique_remove(ds->ds_fsid_guid); 246789Sahrens 24710298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) 24810298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 249789Sahrens 250789Sahrens if (ds->ds_prev) { 2516689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 252789Sahrens ds->ds_prev = NULL; 253789Sahrens } 254789Sahrens 255789Sahrens bplist_close(&ds->ds_deadlist); 2566689Smaybee if (ds->ds_dir) 2576689Smaybee dsl_dir_close(ds->ds_dir, ds); 258789Sahrens 2594787Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 260789Sahrens 2612856Snd150628 mutex_destroy(&ds->ds_lock); 26210204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 2634787Sahrens mutex_destroy(&ds->ds_opening_lock); 2642856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 2656689Smaybee rw_destroy(&ds->ds_rwlock); 2666689Smaybee cv_destroy(&ds->ds_exclusive_cv); 2672856Snd150628 268789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 269789Sahrens } 270789Sahrens 2711544Seschrock static int 272789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 273789Sahrens { 274789Sahrens dsl_dataset_phys_t *headphys; 275789Sahrens int err; 276789Sahrens dmu_buf_t *headdbuf; 277789Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 278789Sahrens objset_t *mos = dp->dp_meta_objset; 279789Sahrens 280789Sahrens if (ds->ds_snapname[0]) 2811544Seschrock return (0); 282789Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 2831544Seschrock return (0); 284789Sahrens 2851544Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 2861544Seschrock FTAG, &headdbuf); 2871544Seschrock if (err) 2881544Seschrock return (err); 289789Sahrens headphys = headdbuf->db_data; 290789Sahrens err = zap_value_search(dp->dp_meta_objset, 2914577Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 2921544Seschrock dmu_buf_rele(headdbuf, FTAG); 2931544Seschrock return (err); 294789Sahrens } 295789Sahrens 2966492Stimh static int 2976689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 2986492Stimh { 2996689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3006689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3016492Stimh matchtype_t mt; 3026492Stimh int err; 3036492Stimh 3046689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3056492Stimh mt = MT_FIRST; 3066492Stimh else 3076492Stimh mt = MT_EXACT; 3086492Stimh 3096689Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 3106492Stimh value, mt, NULL, 0, NULL); 3116492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3126689Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 3136492Stimh return (err); 3146492Stimh } 3156492Stimh 3166492Stimh static int 3176689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 3186492Stimh { 3196689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3206689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3216492Stimh matchtype_t mt; 3226492Stimh int err; 3236492Stimh 32410373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(ds->ds_dir); 32510373Schris.kirby@sun.com 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); 36210204SMatthew.Ahrens@Sun.COM mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL); 3634787Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3642856Snd150628 mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT, 3652856Snd150628 NULL); 3666689Smaybee rw_init(&ds->ds_rwlock, 0, 0, 0); 3676689Smaybee cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 3682856Snd150628 3691544Seschrock err = bplist_open(&ds->ds_deadlist, 370789Sahrens mos, ds->ds_phys->ds_deadlist_obj); 3711544Seschrock if (err == 0) { 3721544Seschrock err = dsl_dir_open_obj(dp, 3731544Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 3741544Seschrock } 3751544Seschrock if (err) { 3761544Seschrock /* 3771544Seschrock * we don't really need to close the blist if we 3781544Seschrock * just opened it. 3791544Seschrock */ 3802856Snd150628 mutex_destroy(&ds->ds_lock); 38110204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 3824787Sahrens mutex_destroy(&ds->ds_opening_lock); 3832856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 3846689Smaybee rw_destroy(&ds->ds_rwlock); 3856689Smaybee cv_destroy(&ds->ds_exclusive_cv); 3861544Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 3871544Seschrock dmu_buf_rele(dbuf, tag); 3881544Seschrock return (err); 3891544Seschrock } 390789Sahrens 3917390SMatthew.Ahrens@Sun.COM if (!dsl_dataset_is_snapshot(ds)) { 392789Sahrens ds->ds_snapname[0] = '\0'; 393789Sahrens if (ds->ds_phys->ds_prev_snap_obj) { 3946689Smaybee err = dsl_dataset_get_ref(dp, 3956689Smaybee ds->ds_phys->ds_prev_snap_obj, 3966689Smaybee ds, &ds->ds_prev); 397789Sahrens } 3987390SMatthew.Ahrens@Sun.COM 3997390SMatthew.Ahrens@Sun.COM if (err == 0 && dsl_dir_is_clone(ds->ds_dir)) { 4007390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin; 4017390SMatthew.Ahrens@Sun.COM 4027390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, 4037390SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_phys->dd_origin_obj, 4047390SMatthew.Ahrens@Sun.COM FTAG, &origin); 4057390SMatthew.Ahrens@Sun.COM if (err == 0) { 4067390SMatthew.Ahrens@Sun.COM ds->ds_origin_txg = 4077390SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_creation_txg; 4087390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(origin, FTAG); 4097390SMatthew.Ahrens@Sun.COM } 4107390SMatthew.Ahrens@Sun.COM } 41110242Schris.kirby@sun.com } else { 41210242Schris.kirby@sun.com if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 41310242Schris.kirby@sun.com err = dsl_dataset_get_snapname(ds); 41410242Schris.kirby@sun.com if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) { 41510242Schris.kirby@sun.com err = zap_count( 41610242Schris.kirby@sun.com ds->ds_dir->dd_pool->dp_meta_objset, 41710242Schris.kirby@sun.com ds->ds_phys->ds_userrefs_obj, 41810242Schris.kirby@sun.com &ds->ds_userrefs); 41910242Schris.kirby@sun.com } 420789Sahrens } 421789Sahrens 4227390SMatthew.Ahrens@Sun.COM if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 4235569Sck153898 /* 4245569Sck153898 * In sync context, we're called with either no lock 4255569Sck153898 * or with the write lock. If we're not syncing, 4265569Sck153898 * we're always called with the read lock held. 4275569Sck153898 */ 4285475Sck153898 boolean_t need_lock = 4295569Sck153898 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 4305569Sck153898 dsl_pool_sync_context(dp); 4315475Sck153898 4325475Sck153898 if (need_lock) 4335475Sck153898 rw_enter(&dp->dp_config_rwlock, RW_READER); 4345475Sck153898 4357265Sahrens err = dsl_prop_get_ds(ds, 4365475Sck153898 "refreservation", sizeof (uint64_t), 1, 4375475Sck153898 &ds->ds_reserved, NULL); 4385475Sck153898 if (err == 0) { 4397265Sahrens err = dsl_prop_get_ds(ds, 4405475Sck153898 "refquota", sizeof (uint64_t), 1, 4415475Sck153898 &ds->ds_quota, NULL); 4425475Sck153898 } 4435475Sck153898 4445475Sck153898 if (need_lock) 4455475Sck153898 rw_exit(&dp->dp_config_rwlock); 4465475Sck153898 } else { 4475475Sck153898 ds->ds_reserved = ds->ds_quota = 0; 4485475Sck153898 } 4495475Sck153898 4501544Seschrock if (err == 0) { 4511544Seschrock winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 4521544Seschrock dsl_dataset_evict); 4531544Seschrock } 4541544Seschrock if (err || winner) { 455789Sahrens bplist_close(&ds->ds_deadlist); 4566689Smaybee if (ds->ds_prev) 4576689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 458789Sahrens dsl_dir_close(ds->ds_dir, ds); 4592856Snd150628 mutex_destroy(&ds->ds_lock); 46010204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 4614787Sahrens mutex_destroy(&ds->ds_opening_lock); 4622856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 4636689Smaybee rw_destroy(&ds->ds_rwlock); 4646689Smaybee cv_destroy(&ds->ds_exclusive_cv); 465789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4661544Seschrock if (err) { 4671544Seschrock dmu_buf_rele(dbuf, tag); 4681544Seschrock return (err); 4691544Seschrock } 470789Sahrens ds = winner; 471789Sahrens } else { 4724787Sahrens ds->ds_fsid_guid = 473789Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 474789Sahrens } 475789Sahrens } 476789Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 477789Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 4787046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 4797061Sahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 4807077Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 481789Sahrens mutex_enter(&ds->ds_lock); 4826689Smaybee if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 483789Sahrens mutex_exit(&ds->ds_lock); 4846689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 4856689Smaybee return (ENOENT); 4866689Smaybee } 4876689Smaybee mutex_exit(&ds->ds_lock); 4886689Smaybee *dsp = ds; 4896689Smaybee return (0); 4906689Smaybee } 4916689Smaybee 4926689Smaybee static int 4936689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 4946689Smaybee { 4956689Smaybee dsl_pool_t *dp = ds->ds_dir->dd_pool; 4966689Smaybee 4976689Smaybee /* 4986689Smaybee * In syncing context we don't want the rwlock lock: there 4996689Smaybee * may be an existing writer waiting for sync phase to 5006689Smaybee * finish. We don't need to worry about such writers, since 5016689Smaybee * sync phase is single-threaded, so the writer can't be 5026689Smaybee * doing anything while we are active. 5036689Smaybee */ 5046689Smaybee if (dsl_pool_sync_context(dp)) { 5056689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 5066689Smaybee return (0); 507789Sahrens } 5086689Smaybee 5096689Smaybee /* 5106689Smaybee * Normal users will hold the ds_rwlock as a READER until they 5116689Smaybee * are finished (i.e., call dsl_dataset_rele()). "Owners" will 5126689Smaybee * drop their READER lock after they set the ds_owner field. 5136689Smaybee * 5146689Smaybee * If the dataset is being destroyed, the destroy thread will 5156689Smaybee * obtain a WRITER lock for exclusive access after it's done its 5166689Smaybee * open-context work and then change the ds_owner to 5176689Smaybee * dsl_reaper once destruction is assured. So threads 5186689Smaybee * may block here temporarily, until the "destructability" of 5196689Smaybee * the dataset is determined. 5206689Smaybee */ 5216689Smaybee ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 5226689Smaybee mutex_enter(&ds->ds_lock); 5236689Smaybee while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 5246689Smaybee rw_exit(&dp->dp_config_rwlock); 5256689Smaybee cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 5266689Smaybee if (DSL_DATASET_IS_DESTROYED(ds)) { 5276689Smaybee mutex_exit(&ds->ds_lock); 5286689Smaybee dsl_dataset_drop_ref(ds, tag); 5296689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5306689Smaybee return (ENOENT); 5316689Smaybee } 5326689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5336689Smaybee } 534789Sahrens mutex_exit(&ds->ds_lock); 5351544Seschrock return (0); 536789Sahrens } 537789Sahrens 538789Sahrens int 5396689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 5406689Smaybee dsl_dataset_t **dsp) 5416689Smaybee { 5426689Smaybee int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 5436689Smaybee 5446689Smaybee if (err) 5456689Smaybee return (err); 5466689Smaybee return (dsl_dataset_hold_ref(*dsp, tag)); 5476689Smaybee } 5486689Smaybee 5496689Smaybee int 55010298SMatthew.Ahrens@Sun.COM dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok, 55110298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 5526689Smaybee { 55310298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5546689Smaybee if (err) 5556689Smaybee return (err); 55610298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 55710298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 5588779SMark.Musante@Sun.COM *dsp = NULL; 5596689Smaybee return (EBUSY); 5606689Smaybee } 5616689Smaybee return (0); 5626689Smaybee } 5636689Smaybee 5646689Smaybee int 5656689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 566789Sahrens { 567789Sahrens dsl_dir_t *dd; 568789Sahrens dsl_pool_t *dp; 5696689Smaybee const char *snapname; 570789Sahrens uint64_t obj; 571789Sahrens int err = 0; 572789Sahrens 5736689Smaybee err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 5741544Seschrock if (err) 5751544Seschrock return (err); 576789Sahrens 577789Sahrens dp = dd->dd_pool; 578789Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 579789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 5806689Smaybee if (obj) 5816689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, dsp); 5826689Smaybee else 583789Sahrens err = ENOENT; 5846689Smaybee if (err) 585789Sahrens goto out; 5866689Smaybee 5876689Smaybee err = dsl_dataset_hold_ref(*dsp, tag); 5886689Smaybee 5896689Smaybee /* we may be looking for a snapshot */ 5906689Smaybee if (err == 0 && snapname != NULL) { 5916689Smaybee dsl_dataset_t *ds = NULL; 5926689Smaybee 5936689Smaybee if (*snapname++ != '@') { 5946689Smaybee dsl_dataset_rele(*dsp, tag); 595789Sahrens err = ENOENT; 596789Sahrens goto out; 597789Sahrens } 5986689Smaybee 5996689Smaybee dprintf("looking for snapshot '%s'\n", snapname); 6006689Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 6016689Smaybee if (err == 0) 6026689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, &ds); 6036689Smaybee dsl_dataset_rele(*dsp, tag); 6046689Smaybee 6056689Smaybee ASSERT3U((err == 0), ==, (ds != NULL)); 6066689Smaybee 6076689Smaybee if (ds) { 6086689Smaybee mutex_enter(&ds->ds_lock); 6096689Smaybee if (ds->ds_snapname[0] == 0) 6106689Smaybee (void) strlcpy(ds->ds_snapname, snapname, 6116689Smaybee sizeof (ds->ds_snapname)); 6126689Smaybee mutex_exit(&ds->ds_lock); 6136689Smaybee err = dsl_dataset_hold_ref(ds, tag); 6146689Smaybee *dsp = err ? NULL : ds; 615789Sahrens } 616789Sahrens } 617789Sahrens out: 618789Sahrens rw_exit(&dp->dp_config_rwlock); 619789Sahrens dsl_dir_close(dd, FTAG); 620789Sahrens return (err); 621789Sahrens } 622789Sahrens 623789Sahrens int 62410298SMatthew.Ahrens@Sun.COM dsl_dataset_own(const char *name, boolean_t inconsistentok, 62510298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 626789Sahrens { 62710298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold(name, tag, dsp); 6286689Smaybee if (err) 6296689Smaybee return (err); 63010298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 63110298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 6326689Smaybee return (EBUSY); 6336689Smaybee } 6346689Smaybee return (0); 635789Sahrens } 636789Sahrens 637789Sahrens void 638789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 639789Sahrens { 640789Sahrens if (ds == NULL) { 641789Sahrens (void) strcpy(name, "mos"); 642789Sahrens } else { 643789Sahrens dsl_dir_name(ds->ds_dir, name); 6441544Seschrock VERIFY(0 == dsl_dataset_get_snapname(ds)); 645789Sahrens if (ds->ds_snapname[0]) { 646789Sahrens (void) strcat(name, "@"); 6476689Smaybee /* 6486689Smaybee * We use a "recursive" mutex so that we 6496689Smaybee * can call dprintf_ds() with ds_lock held. 6506689Smaybee */ 651789Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 652789Sahrens mutex_enter(&ds->ds_lock); 653789Sahrens (void) strcat(name, ds->ds_snapname); 654789Sahrens mutex_exit(&ds->ds_lock); 655789Sahrens } else { 656789Sahrens (void) strcat(name, ds->ds_snapname); 657789Sahrens } 658789Sahrens } 659789Sahrens } 660789Sahrens } 661789Sahrens 6623978Smmusante static int 6633978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds) 6643978Smmusante { 6653978Smmusante int result; 6663978Smmusante 6673978Smmusante if (ds == NULL) { 6683978Smmusante result = 3; /* "mos" */ 6693978Smmusante } else { 6703978Smmusante result = dsl_dir_namelen(ds->ds_dir); 6713978Smmusante VERIFY(0 == dsl_dataset_get_snapname(ds)); 6723978Smmusante if (ds->ds_snapname[0]) { 6733978Smmusante ++result; /* adding one for the @-sign */ 6743978Smmusante if (!MUTEX_HELD(&ds->ds_lock)) { 6753978Smmusante mutex_enter(&ds->ds_lock); 6763978Smmusante result += strlen(ds->ds_snapname); 6773978Smmusante mutex_exit(&ds->ds_lock); 6783978Smmusante } else { 6793978Smmusante result += strlen(ds->ds_snapname); 6803978Smmusante } 6813978Smmusante } 6823978Smmusante } 6833978Smmusante 6843978Smmusante return (result); 6853978Smmusante } 6863978Smmusante 6877046Sahrens void 6886689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 689789Sahrens { 6901544Seschrock dmu_buf_rele(ds->ds_dbuf, tag); 691789Sahrens } 692789Sahrens 693789Sahrens void 6946689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6955367Sahrens { 6966689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 6976689Smaybee rw_exit(&ds->ds_rwlock); 6986689Smaybee } 6996689Smaybee dsl_dataset_drop_ref(ds, tag); 7006689Smaybee } 7016689Smaybee 7026689Smaybee void 70310298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 7046689Smaybee { 70510298SMatthew.Ahrens@Sun.COM ASSERT((ds->ds_owner == tag && ds->ds_dbuf) || 7066689Smaybee (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 7076689Smaybee 7085367Sahrens mutex_enter(&ds->ds_lock); 7096689Smaybee ds->ds_owner = NULL; 7106689Smaybee if (RW_WRITE_HELD(&ds->ds_rwlock)) { 7116689Smaybee rw_exit(&ds->ds_rwlock); 7126689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 7136689Smaybee } 7145367Sahrens mutex_exit(&ds->ds_lock); 7156689Smaybee if (ds->ds_dbuf) 71610298SMatthew.Ahrens@Sun.COM dsl_dataset_drop_ref(ds, tag); 7176689Smaybee else 7186689Smaybee dsl_dataset_evict(ds->ds_dbuf, ds); 7195367Sahrens } 7205367Sahrens 7215367Sahrens boolean_t 72210298SMatthew.Ahrens@Sun.COM dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag) 7235367Sahrens { 7246689Smaybee boolean_t gotit = FALSE; 7256689Smaybee 7265367Sahrens mutex_enter(&ds->ds_lock); 7276689Smaybee if (ds->ds_owner == NULL && 7286689Smaybee (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 72910298SMatthew.Ahrens@Sun.COM ds->ds_owner = tag; 7306689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 7316689Smaybee rw_exit(&ds->ds_rwlock); 7326689Smaybee gotit = TRUE; 7335367Sahrens } 7345367Sahrens mutex_exit(&ds->ds_lock); 7356689Smaybee return (gotit); 7366689Smaybee } 7376689Smaybee 7386689Smaybee void 7396689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 7406689Smaybee { 7416689Smaybee ASSERT3P(owner, ==, ds->ds_owner); 7426689Smaybee if (!RW_WRITE_HELD(&ds->ds_rwlock)) 7436689Smaybee rw_enter(&ds->ds_rwlock, RW_WRITER); 7445367Sahrens } 7455367Sahrens 7462199Sahrens uint64_t 7477046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 7486492Stimh uint64_t flags, dmu_tx_t *tx) 749789Sahrens { 7505367Sahrens dsl_pool_t *dp = dd->dd_pool; 751789Sahrens dmu_buf_t *dbuf; 752789Sahrens dsl_dataset_phys_t *dsphys; 7535367Sahrens uint64_t dsobj; 754789Sahrens objset_t *mos = dp->dp_meta_objset; 755789Sahrens 7567046Sahrens if (origin == NULL) 7577046Sahrens origin = dp->dp_origin_snap; 7587046Sahrens 7595367Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 7605367Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 761789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 7625367Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 763789Sahrens 764928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 765928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7661544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 767789Sahrens dmu_buf_will_dirty(dbuf, tx); 768789Sahrens dsphys = dbuf->db_data; 7696689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 770789Sahrens dsphys->ds_dir_obj = dd->dd_object; 7716492Stimh dsphys->ds_flags = flags; 772789Sahrens dsphys->ds_fsid_guid = unique_create(); 773789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 774789Sahrens sizeof (dsphys->ds_guid)); 775789Sahrens dsphys->ds_snapnames_zapobj = 7766492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7776492Stimh DMU_OT_NONE, 0, tx); 778789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 7797046Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 780789Sahrens dsphys->ds_deadlist_obj = 781789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7825378Sck153898 7835367Sahrens if (origin) { 7845367Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 785789Sahrens dsphys->ds_prev_snap_txg = 7865367Sahrens origin->ds_phys->ds_creation_txg; 787789Sahrens dsphys->ds_used_bytes = 7885367Sahrens origin->ds_phys->ds_used_bytes; 789789Sahrens dsphys->ds_compressed_bytes = 7905367Sahrens origin->ds_phys->ds_compressed_bytes; 791789Sahrens dsphys->ds_uncompressed_bytes = 7925367Sahrens origin->ds_phys->ds_uncompressed_bytes; 7935367Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 7946502Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 795789Sahrens 7965367Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 7975367Sahrens origin->ds_phys->ds_num_children++; 798789Sahrens 7997046Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 8007046Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 8017046Sahrens origin->ds_phys->ds_next_clones_obj = 8027046Sahrens zap_create(mos, 8037046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 8047046Sahrens } 8057046Sahrens VERIFY(0 == zap_add_int(mos, 8067046Sahrens origin->ds_phys->ds_next_clones_obj, 8077046Sahrens dsobj, tx)); 8087046Sahrens } 8097046Sahrens 810789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 8115367Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 812789Sahrens } 8136492Stimh 8146492Stimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 8156492Stimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 8166492Stimh 8171544Seschrock dmu_buf_rele(dbuf, FTAG); 818789Sahrens 819789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 820789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 8215367Sahrens 8225367Sahrens return (dsobj); 8235367Sahrens } 8245367Sahrens 8255367Sahrens uint64_t 8266492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 8276492Stimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8285367Sahrens { 8295367Sahrens dsl_pool_t *dp = pdd->dd_pool; 8305367Sahrens uint64_t dsobj, ddobj; 8315367Sahrens dsl_dir_t *dd; 8325367Sahrens 8335367Sahrens ASSERT(lastname[0] != '@'); 8345367Sahrens 8357046Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8365367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 8375367Sahrens 8387046Sahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx); 8395367Sahrens 8405367Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8415367Sahrens 842789Sahrens dsl_dir_close(dd, FTAG); 843789Sahrens 8442199Sahrens return (dsobj); 8452199Sahrens } 8462199Sahrens 8472199Sahrens struct destroyarg { 8482199Sahrens dsl_sync_task_group_t *dstg; 8492199Sahrens char *snapname; 8502199Sahrens char *failed; 85110242Schris.kirby@sun.com boolean_t defer; 8522199Sahrens }; 8532199Sahrens 8542199Sahrens static int 8552199Sahrens dsl_snapshot_destroy_one(char *name, void *arg) 8562199Sahrens { 8572199Sahrens struct destroyarg *da = arg; 8582199Sahrens dsl_dataset_t *ds; 8592199Sahrens int err; 86010242Schris.kirby@sun.com char *dsname; 86110272SMatthew.Ahrens@Sun.COM 86210272SMatthew.Ahrens@Sun.COM dsname = kmem_asprintf("%s@%s", name, da->snapname); 86310298SMatthew.Ahrens@Sun.COM err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds); 86410272SMatthew.Ahrens@Sun.COM strfree(dsname); 8656689Smaybee if (err == 0) { 86610242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda; 86710242Schris.kirby@sun.com 8686689Smaybee dsl_dataset_make_exclusive(ds, da->dstg); 86910298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) { 87010298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 87110298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 8727237Sek110237 } 87310242Schris.kirby@sun.com dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP); 87410242Schris.kirby@sun.com dsda->ds = ds; 87510242Schris.kirby@sun.com dsda->defer = da->defer; 8766689Smaybee dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 87710242Schris.kirby@sun.com dsl_dataset_destroy_sync, dsda, da->dstg, 0); 8786689Smaybee } else if (err == ENOENT) { 8796689Smaybee err = 0; 8806689Smaybee } else { 8812199Sahrens (void) strcpy(da->failed, name); 8822199Sahrens } 8836689Smaybee return (err); 884789Sahrens } 885789Sahrens 8862199Sahrens /* 8872199Sahrens * Destroy 'snapname' in all descendants of 'fsname'. 8882199Sahrens */ 8892199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 8902199Sahrens int 89110242Schris.kirby@sun.com dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer) 8922199Sahrens { 8932199Sahrens int err; 8942199Sahrens struct destroyarg da; 8952199Sahrens dsl_sync_task_t *dst; 8962199Sahrens spa_t *spa; 8972199Sahrens 8984603Sahrens err = spa_open(fsname, &spa, FTAG); 8992199Sahrens if (err) 9002199Sahrens return (err); 9012199Sahrens da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 9022199Sahrens da.snapname = snapname; 9032199Sahrens da.failed = fsname; 90410242Schris.kirby@sun.com da.defer = defer; 9052199Sahrens 9062199Sahrens err = dmu_objset_find(fsname, 9072417Sahrens dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 9082199Sahrens 9092199Sahrens if (err == 0) 9102199Sahrens err = dsl_sync_task_group_wait(da.dstg); 9112199Sahrens 9122199Sahrens for (dst = list_head(&da.dstg->dstg_tasks); dst; 9132199Sahrens dst = list_next(&da.dstg->dstg_tasks, dst)) { 91410242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = dst->dst_arg1; 91510242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 91610242Schris.kirby@sun.com 9176689Smaybee /* 9186689Smaybee * Return the file system name that triggered the error 9196689Smaybee */ 9202199Sahrens if (dst->dst_err) { 9212199Sahrens dsl_dataset_name(ds, fsname); 9224603Sahrens *strchr(fsname, '@') = '\0'; 9232199Sahrens } 92410242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 9256689Smaybee dsl_dataset_disown(ds, da.dstg); 92610242Schris.kirby@sun.com kmem_free(dsda, sizeof (struct dsl_ds_destroyarg)); 9272199Sahrens } 9282199Sahrens 9292199Sahrens dsl_sync_task_group_destroy(da.dstg); 9302199Sahrens spa_close(spa, FTAG); 9312199Sahrens return (err); 9322199Sahrens } 9332199Sahrens 93410242Schris.kirby@sun.com static boolean_t 93510242Schris.kirby@sun.com dsl_dataset_might_destroy_origin(dsl_dataset_t *ds) 93610242Schris.kirby@sun.com { 93710242Schris.kirby@sun.com boolean_t might_destroy = B_FALSE; 93810242Schris.kirby@sun.com 93910242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 94010242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 && 94110242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 94210242Schris.kirby@sun.com might_destroy = B_TRUE; 94310242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 94410242Schris.kirby@sun.com 94510242Schris.kirby@sun.com return (might_destroy); 94610242Schris.kirby@sun.com } 94710242Schris.kirby@sun.com 94810242Schris.kirby@sun.com /* 94910242Schris.kirby@sun.com * If we're removing a clone, and these three conditions are true: 95010242Schris.kirby@sun.com * 1) the clone's origin has no other children 95110242Schris.kirby@sun.com * 2) the clone's origin has no user references 95210242Schris.kirby@sun.com * 3) the clone's origin has been marked for deferred destruction 95310242Schris.kirby@sun.com * Then, prepare to remove the origin as part of this sync task group. 95410242Schris.kirby@sun.com */ 95510242Schris.kirby@sun.com static int 95610242Schris.kirby@sun.com dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag) 95710242Schris.kirby@sun.com { 95810242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 95910242Schris.kirby@sun.com dsl_dataset_t *origin = ds->ds_prev; 96010242Schris.kirby@sun.com 96110242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(origin)) { 96210242Schris.kirby@sun.com char *name; 96310242Schris.kirby@sun.com int namelen; 96410242Schris.kirby@sun.com int error; 96510242Schris.kirby@sun.com 96610242Schris.kirby@sun.com namelen = dsl_dataset_namelen(origin) + 1; 96710242Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 96810242Schris.kirby@sun.com dsl_dataset_name(origin, name); 96910242Schris.kirby@sun.com #ifdef _KERNEL 97010242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 97110242Schris.kirby@sun.com if (error) { 97210242Schris.kirby@sun.com kmem_free(name, namelen); 97310242Schris.kirby@sun.com return (error); 97410242Schris.kirby@sun.com } 97510242Schris.kirby@sun.com #endif 97610298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(name, B_TRUE, tag, &origin); 97710242Schris.kirby@sun.com kmem_free(name, namelen); 97810242Schris.kirby@sun.com if (error) 97910242Schris.kirby@sun.com return (error); 98010242Schris.kirby@sun.com dsda->rm_origin = origin; 98110242Schris.kirby@sun.com dsl_dataset_make_exclusive(origin, tag); 98210342Schris.kirby@sun.com 98310342Schris.kirby@sun.com if (origin->ds_objset != NULL) { 98410342Schris.kirby@sun.com dmu_objset_evict(origin->ds_objset); 98510342Schris.kirby@sun.com origin->ds_objset = NULL; 98610342Schris.kirby@sun.com } 98710242Schris.kirby@sun.com } 98810242Schris.kirby@sun.com 98910242Schris.kirby@sun.com return (0); 99010242Schris.kirby@sun.com } 99110242Schris.kirby@sun.com 9925367Sahrens /* 9936689Smaybee * ds must be opened as OWNER. On return (whether successful or not), 9946689Smaybee * ds will be closed and caller can no longer dereference it. 9955367Sahrens */ 996789Sahrens int 99710242Schris.kirby@sun.com dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer) 998789Sahrens { 999789Sahrens int err; 10002199Sahrens dsl_sync_task_group_t *dstg; 10012199Sahrens objset_t *os; 1002789Sahrens dsl_dir_t *dd; 10032199Sahrens uint64_t obj; 100410242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 100510242Schris.kirby@sun.com 100610242Schris.kirby@sun.com dsda.ds = ds; 10072199Sahrens 10085367Sahrens if (dsl_dataset_is_snapshot(ds)) { 10092199Sahrens /* Destroying a snapshot is simpler */ 10106689Smaybee dsl_dataset_make_exclusive(ds, tag); 10117237Sek110237 101210298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) { 101310298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 101410298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 10157237Sek110237 } 101610242Schris.kirby@sun.com dsda.defer = defer; 10172199Sahrens err = dsl_sync_task_do(ds->ds_dir->dd_pool, 10182199Sahrens dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 101910242Schris.kirby@sun.com &dsda, tag, 0); 102010242Schris.kirby@sun.com ASSERT3P(dsda.rm_origin, ==, NULL); 10215367Sahrens goto out; 102210385Schris.kirby@sun.com } else if (defer) { 102310385Schris.kirby@sun.com err = EINVAL; 102410385Schris.kirby@sun.com goto out; 10252199Sahrens } 10262199Sahrens 10272199Sahrens dd = ds->ds_dir; 1028789Sahrens 10292199Sahrens /* 10302199Sahrens * Check for errors and mark this ds as inconsistent, in 10312199Sahrens * case we crash while freeing the objects. 10322199Sahrens */ 10332199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 10342199Sahrens dsl_dataset_destroy_begin_sync, ds, NULL, 0); 10355367Sahrens if (err) 10365367Sahrens goto out; 10375367Sahrens 103810298SMatthew.Ahrens@Sun.COM err = dmu_objset_from_ds(ds, &os); 10395367Sahrens if (err) 10405367Sahrens goto out; 10412199Sahrens 10422199Sahrens /* 10432199Sahrens * remove the objects in open context, so that we won't 10442199Sahrens * have too much to do in syncing context. 10452199Sahrens */ 10463025Sahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 10473025Sahrens ds->ds_phys->ds_prev_snap_txg)) { 10486992Smaybee /* 10496992Smaybee * Ignore errors, if there is not enough disk space 10506992Smaybee * we will deal with it in dsl_dataset_destroy_sync(). 10516992Smaybee */ 10526992Smaybee (void) dmu_free_object(os, obj); 10532199Sahrens } 10542199Sahrens 10559396SMatthew.Ahrens@Sun.COM /* 10569396SMatthew.Ahrens@Sun.COM * We need to sync out all in-flight IO before we try to evict 10579396SMatthew.Ahrens@Sun.COM * (the dataset evict func is trying to clear the cached entries 10589396SMatthew.Ahrens@Sun.COM * for this dataset in the ARC). 10599396SMatthew.Ahrens@Sun.COM */ 10609396SMatthew.Ahrens@Sun.COM txg_wait_synced(dd->dd_pool, 0); 10619396SMatthew.Ahrens@Sun.COM 10629396SMatthew.Ahrens@Sun.COM /* 10639396SMatthew.Ahrens@Sun.COM * If we managed to free all the objects in open 10649396SMatthew.Ahrens@Sun.COM * context, the user space accounting should be zero. 10659396SMatthew.Ahrens@Sun.COM */ 10669396SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_bp.blk_fill == 0 && 106710298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(os)) { 10689396SMatthew.Ahrens@Sun.COM uint64_t count; 10699396SMatthew.Ahrens@Sun.COM 10709396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 || 10719396SMatthew.Ahrens@Sun.COM count == 0); 10729396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 || 10739396SMatthew.Ahrens@Sun.COM count == 0); 10749396SMatthew.Ahrens@Sun.COM } 10759396SMatthew.Ahrens@Sun.COM 10762199Sahrens if (err != ESRCH) 10775367Sahrens goto out; 10782199Sahrens 10796975Smaybee rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 10806975Smaybee err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 10816975Smaybee rw_exit(&dd->dd_pool->dp_config_rwlock); 10826975Smaybee 10836975Smaybee if (err) 10846975Smaybee goto out; 10856975Smaybee 108610298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 10876689Smaybee /* 10886689Smaybee * We need to sync out all in-flight IO before we try 10896689Smaybee * to evict (the dataset evict func is trying to clear 10906689Smaybee * the cached entries for this dataset in the ARC). 10916689Smaybee */ 10926689Smaybee txg_wait_synced(dd->dd_pool, 0); 10935367Sahrens } 10945367Sahrens 10952199Sahrens /* 10962199Sahrens * Blow away the dsl_dir + head dataset. 10972199Sahrens */ 10986689Smaybee dsl_dataset_make_exclusive(ds, tag); 109910298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 110010298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 110110298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 11026975Smaybee } 110310242Schris.kirby@sun.com 110410242Schris.kirby@sun.com /* 110510242Schris.kirby@sun.com * If we're removing a clone, we might also need to remove its 110610242Schris.kirby@sun.com * origin. 110710242Schris.kirby@sun.com */ 110810242Schris.kirby@sun.com do { 110910242Schris.kirby@sun.com dsda.need_prep = B_FALSE; 111010242Schris.kirby@sun.com if (dsl_dir_is_clone(dd)) { 111110242Schris.kirby@sun.com err = dsl_dataset_origin_rm_prep(&dsda, tag); 111210242Schris.kirby@sun.com if (err) { 111310242Schris.kirby@sun.com dsl_dir_close(dd, FTAG); 111410242Schris.kirby@sun.com goto out; 111510242Schris.kirby@sun.com } 111610242Schris.kirby@sun.com } 111710242Schris.kirby@sun.com 111810242Schris.kirby@sun.com dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 111910242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 112010242Schris.kirby@sun.com dsl_dataset_destroy_sync, &dsda, tag, 0); 112110242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dir_destroy_check, 112210242Schris.kirby@sun.com dsl_dir_destroy_sync, dd, FTAG, 0); 112310242Schris.kirby@sun.com err = dsl_sync_task_group_wait(dstg); 112410242Schris.kirby@sun.com dsl_sync_task_group_destroy(dstg); 112510242Schris.kirby@sun.com 112610242Schris.kirby@sun.com /* 112710242Schris.kirby@sun.com * We could be racing against 'zfs release' or 'zfs destroy -d' 112810242Schris.kirby@sun.com * on the origin snap, in which case we can get EBUSY if we 112910242Schris.kirby@sun.com * needed to destroy the origin snap but were not ready to 113010242Schris.kirby@sun.com * do so. 113110242Schris.kirby@sun.com */ 113210242Schris.kirby@sun.com if (dsda.need_prep) { 113310242Schris.kirby@sun.com ASSERT(err == EBUSY); 113410242Schris.kirby@sun.com ASSERT(dsl_dir_is_clone(dd)); 113510242Schris.kirby@sun.com ASSERT(dsda.rm_origin == NULL); 113610242Schris.kirby@sun.com } 113710242Schris.kirby@sun.com } while (dsda.need_prep); 113810242Schris.kirby@sun.com 113910242Schris.kirby@sun.com if (dsda.rm_origin != NULL) 114010242Schris.kirby@sun.com dsl_dataset_disown(dsda.rm_origin, tag); 114110242Schris.kirby@sun.com 11426689Smaybee /* if it is successful, dsl_dir_destroy_sync will close the dd */ 11435367Sahrens if (err) 11442199Sahrens dsl_dir_close(dd, FTAG); 11455367Sahrens out: 11466689Smaybee dsl_dataset_disown(ds, tag); 1147789Sahrens return (err); 1148789Sahrens } 1149789Sahrens 11503547Smaybee blkptr_t * 11513547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1152789Sahrens { 11533547Smaybee return (&ds->ds_phys->ds_bp); 1154789Sahrens } 1155789Sahrens 1156789Sahrens void 1157789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1158789Sahrens { 1159789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1160789Sahrens /* If it's the meta-objset, set dp_meta_rootbp */ 1161789Sahrens if (ds == NULL) { 1162789Sahrens tx->tx_pool->dp_meta_rootbp = *bp; 1163789Sahrens } else { 1164789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1165789Sahrens ds->ds_phys->ds_bp = *bp; 1166789Sahrens } 1167789Sahrens } 1168789Sahrens 1169789Sahrens spa_t * 1170789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 1171789Sahrens { 1172789Sahrens return (ds->ds_dir->dd_pool->dp_spa); 1173789Sahrens } 1174789Sahrens 1175789Sahrens void 1176789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1177789Sahrens { 1178789Sahrens dsl_pool_t *dp; 1179789Sahrens 1180789Sahrens if (ds == NULL) /* this is the meta-objset */ 1181789Sahrens return; 1182789Sahrens 118310298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 11842885Sahrens 11852885Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 11862885Sahrens panic("dirtying snapshot!"); 1187789Sahrens 1188789Sahrens dp = ds->ds_dir->dd_pool; 1189789Sahrens 1190789Sahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1191789Sahrens /* up the hold count until we can be written out */ 1192789Sahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 1193789Sahrens } 1194789Sahrens } 1195789Sahrens 11965378Sck153898 /* 11975378Sck153898 * The unique space in the head dataset can be calculated by subtracting 11985378Sck153898 * the space used in the most recent snapshot, that is still being used 11995378Sck153898 * in this file system, from the space currently in use. To figure out 12005378Sck153898 * the space in the most recent snapshot still in use, we need to take 12015378Sck153898 * the total space used in the snapshot and subtract out the space that 12025378Sck153898 * has been freed up since the snapshot was taken. 12035378Sck153898 */ 12045378Sck153898 static void 12055378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 12065378Sck153898 { 12075378Sck153898 uint64_t mrs_used; 12085378Sck153898 uint64_t dlused, dlcomp, dluncomp; 12095378Sck153898 12105378Sck153898 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 12115378Sck153898 12125378Sck153898 if (ds->ds_phys->ds_prev_snap_obj != 0) 12135378Sck153898 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 12145378Sck153898 else 12155378Sck153898 mrs_used = 0; 12165378Sck153898 12175378Sck153898 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 12185378Sck153898 &dluncomp)); 12195378Sck153898 12205378Sck153898 ASSERT3U(dlused, <=, mrs_used); 12215378Sck153898 ds->ds_phys->ds_unique_bytes = 12225378Sck153898 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 12235378Sck153898 12245378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && 12255378Sck153898 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 12265378Sck153898 SPA_VERSION_UNIQUE_ACCURATE) 12275378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 12285378Sck153898 } 12295378Sck153898 12305378Sck153898 static uint64_t 12315378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds) 12325378Sck153898 { 12335378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 12345378Sck153898 dsl_dataset_recalc_head_uniq(ds); 12355378Sck153898 12365378Sck153898 return (ds->ds_phys->ds_unique_bytes); 12375378Sck153898 } 12385378Sck153898 1239789Sahrens struct killarg { 12407390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 1241789Sahrens zio_t *zio; 1242789Sahrens dmu_tx_t *tx; 1243789Sahrens }; 1244789Sahrens 12457390SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 1246789Sahrens static int 12477837SMatthew.Ahrens@Sun.COM kill_blkptr(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb, 12487837SMatthew.Ahrens@Sun.COM const dnode_phys_t *dnp, void *arg) 1249789Sahrens { 1250789Sahrens struct killarg *ka = arg; 12517837SMatthew.Ahrens@Sun.COM 12527837SMatthew.Ahrens@Sun.COM if (bp == NULL) 12537837SMatthew.Ahrens@Sun.COM return (0); 1254789Sahrens 12558746SMatthew.Ahrens@Sun.COM if ((zb->zb_level == -1ULL && zb->zb_blkid != 0) || 12568746SMatthew.Ahrens@Sun.COM (zb->zb_object != 0 && dnp == NULL)) { 12578746SMatthew.Ahrens@Sun.COM /* 12588746SMatthew.Ahrens@Sun.COM * It's a block in the intent log. It has no 12598746SMatthew.Ahrens@Sun.COM * accounting, so just free it. 12608746SMatthew.Ahrens@Sun.COM */ 12618746SMatthew.Ahrens@Sun.COM VERIFY3U(0, ==, dsl_free(ka->zio, ka->tx->tx_pool, 12628746SMatthew.Ahrens@Sun.COM ka->tx->tx_txg, bp, NULL, NULL, ARC_NOWAIT)); 12638746SMatthew.Ahrens@Sun.COM } else { 12648746SMatthew.Ahrens@Sun.COM ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg); 12658746SMatthew.Ahrens@Sun.COM (void) dsl_dataset_block_kill(ka->ds, bp, ka->zio, ka->tx); 12668746SMatthew.Ahrens@Sun.COM } 12677390SMatthew.Ahrens@Sun.COM 1268789Sahrens return (0); 1269789Sahrens } 1270789Sahrens 1271789Sahrens /* ARGSUSED */ 12722199Sahrens static int 12732199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 12741731Sbonwick { 12752199Sahrens dsl_dataset_t *ds = arg1; 12765367Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 12775367Sahrens uint64_t count; 12785367Sahrens int err; 12791731Sbonwick 12801731Sbonwick /* 12811731Sbonwick * Can't delete a head dataset if there are snapshots of it. 12821731Sbonwick * (Except if the only snapshots are from the branch we cloned 12831731Sbonwick * from.) 12841731Sbonwick */ 12851731Sbonwick if (ds->ds_prev != NULL && 12861731Sbonwick ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1287*10816SVitezslav.Batrla@Sun.COM return (EBUSY); 12881731Sbonwick 12895367Sahrens /* 12905367Sahrens * This is really a dsl_dir thing, but check it here so that 12915367Sahrens * we'll be less likely to leave this dataset inconsistent & 12925367Sahrens * nearly destroyed. 12935367Sahrens */ 12945367Sahrens err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 12955367Sahrens if (err) 12965367Sahrens return (err); 12975367Sahrens if (count != 0) 12985367Sahrens return (EEXIST); 12995367Sahrens 13001731Sbonwick return (0); 13011731Sbonwick } 13021731Sbonwick 13032199Sahrens /* ARGSUSED */ 13042199Sahrens static void 13054543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1306789Sahrens { 13072199Sahrens dsl_dataset_t *ds = arg1; 13084543Smarks dsl_pool_t *dp = ds->ds_dir->dd_pool; 1309789Sahrens 13102199Sahrens /* Mark it as inconsistent on-disk, in case we crash */ 13112199Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13122199Sahrens ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 13134543Smarks 13144543Smarks spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 13154543Smarks cr, "dataset = %llu", ds->ds_object); 13162199Sahrens } 1317789Sahrens 131810242Schris.kirby@sun.com static int 131910242Schris.kirby@sun.com dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag, 132010242Schris.kirby@sun.com dmu_tx_t *tx) 132110242Schris.kirby@sun.com { 132210242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 132310242Schris.kirby@sun.com dsl_dataset_t *ds_prev = ds->ds_prev; 132410242Schris.kirby@sun.com 132510242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(ds_prev)) { 132610242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 132710242Schris.kirby@sun.com 132810242Schris.kirby@sun.com /* 132910242Schris.kirby@sun.com * If we're not prepared to remove the origin, don't remove 133010242Schris.kirby@sun.com * the clone either. 133110242Schris.kirby@sun.com */ 133210242Schris.kirby@sun.com if (dsda->rm_origin == NULL) { 133310242Schris.kirby@sun.com dsda->need_prep = B_TRUE; 133410242Schris.kirby@sun.com return (EBUSY); 133510242Schris.kirby@sun.com } 133610242Schris.kirby@sun.com 133710242Schris.kirby@sun.com ndsda.ds = ds_prev; 133810242Schris.kirby@sun.com ndsda.is_origin_rm = B_TRUE; 133910242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&ndsda, tag, tx)); 134010242Schris.kirby@sun.com } 134110242Schris.kirby@sun.com 134210242Schris.kirby@sun.com /* 134310242Schris.kirby@sun.com * If we're not going to remove the origin after all, 134410242Schris.kirby@sun.com * undo the open context setup. 134510242Schris.kirby@sun.com */ 134610242Schris.kirby@sun.com if (dsda->rm_origin != NULL) { 134710242Schris.kirby@sun.com dsl_dataset_disown(dsda->rm_origin, tag); 134810242Schris.kirby@sun.com dsda->rm_origin = NULL; 134910242Schris.kirby@sun.com } 135010242Schris.kirby@sun.com 135110242Schris.kirby@sun.com return (0); 135210242Schris.kirby@sun.com } 135310242Schris.kirby@sun.com 13542199Sahrens /* ARGSUSED */ 13555367Sahrens int 13562199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 13572199Sahrens { 135810242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 135910242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 1360789Sahrens 13616689Smaybee /* we have an owner hold, so noone else can destroy us */ 13626689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 13636689Smaybee 136410242Schris.kirby@sun.com /* 136510242Schris.kirby@sun.com * Only allow deferred destroy on pools that support it. 136610242Schris.kirby@sun.com * NOTE: deferred destroy is only supported on snapshots. 136710242Schris.kirby@sun.com */ 136810242Schris.kirby@sun.com if (dsda->defer) { 136910242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 137010242Schris.kirby@sun.com SPA_VERSION_USERREFS) 137110242Schris.kirby@sun.com return (ENOTSUP); 137210242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 137310242Schris.kirby@sun.com return (0); 137410242Schris.kirby@sun.com } 1375789Sahrens 1376789Sahrens /* 1377789Sahrens * Can't delete a head dataset if there are snapshots of it. 1378789Sahrens * (Except if the only snapshots are from the branch we cloned 1379789Sahrens * from.) 1380789Sahrens */ 1381789Sahrens if (ds->ds_prev != NULL && 13822199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1383*10816SVitezslav.Batrla@Sun.COM return (EBUSY); 1384789Sahrens 1385789Sahrens /* 1386789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1387789Sahrens * them. Try again. 1388789Sahrens */ 13892199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1390789Sahrens return (EAGAIN); 13912199Sahrens 139210242Schris.kirby@sun.com if (dsl_dataset_is_snapshot(ds)) { 139310242Schris.kirby@sun.com /* 139410242Schris.kirby@sun.com * If this snapshot has an elevated user reference count, 139510242Schris.kirby@sun.com * we can't destroy it yet. 139610242Schris.kirby@sun.com */ 139710242Schris.kirby@sun.com if (ds->ds_userrefs > 0 && !dsda->releasing) 139810242Schris.kirby@sun.com return (EBUSY); 139910242Schris.kirby@sun.com 140010242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 140110242Schris.kirby@sun.com /* 140210242Schris.kirby@sun.com * Can't delete a branch point. However, if we're destroying 140310242Schris.kirby@sun.com * a clone and removing its origin due to it having a user 140410242Schris.kirby@sun.com * hold count of 0 and having been marked for deferred destroy, 140510242Schris.kirby@sun.com * it's OK for the origin to have a single clone. 140610242Schris.kirby@sun.com */ 140710242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children > 140810242Schris.kirby@sun.com (dsda->is_origin_rm ? 2 : 1)) { 140910242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 141010242Schris.kirby@sun.com return (EEXIST); 141110242Schris.kirby@sun.com } 141210242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 141310242Schris.kirby@sun.com } else if (dsl_dir_is_clone(ds->ds_dir)) { 141410242Schris.kirby@sun.com return (dsl_dataset_origin_check(dsda, arg2, tx)); 141510242Schris.kirby@sun.com } 141610242Schris.kirby@sun.com 14172199Sahrens /* XXX we should do some i/o error checking... */ 14182199Sahrens return (0); 14192199Sahrens } 14202199Sahrens 14216689Smaybee struct refsarg { 14226689Smaybee kmutex_t lock; 14236689Smaybee boolean_t gone; 14246689Smaybee kcondvar_t cv; 14256689Smaybee }; 14266689Smaybee 14276689Smaybee /* ARGSUSED */ 14286689Smaybee static void 14296689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 14306689Smaybee { 14316689Smaybee struct refsarg *arg = argv; 14326689Smaybee 14336689Smaybee mutex_enter(&arg->lock); 14346689Smaybee arg->gone = TRUE; 14356689Smaybee cv_signal(&arg->cv); 14366689Smaybee mutex_exit(&arg->lock); 14376689Smaybee } 14386689Smaybee 14396689Smaybee static void 14406689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 14416689Smaybee { 14426689Smaybee struct refsarg arg; 14436689Smaybee 14446689Smaybee mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 14456689Smaybee cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 14466689Smaybee arg.gone = FALSE; 14476689Smaybee (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 14486689Smaybee dsl_dataset_refs_gone); 14496689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 14506689Smaybee mutex_enter(&arg.lock); 14516689Smaybee while (!arg.gone) 14526689Smaybee cv_wait(&arg.cv, &arg.lock); 14536689Smaybee ASSERT(arg.gone); 14546689Smaybee mutex_exit(&arg.lock); 14556689Smaybee ds->ds_dbuf = NULL; 14566689Smaybee ds->ds_phys = NULL; 14576689Smaybee mutex_destroy(&arg.lock); 14586689Smaybee cv_destroy(&arg.cv); 14596689Smaybee } 14606689Smaybee 146110801SMatthew.Ahrens@Sun.COM static void 146210801SMatthew.Ahrens@Sun.COM remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx) 146310801SMatthew.Ahrens@Sun.COM { 146410801SMatthew.Ahrens@Sun.COM objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 146510801SMatthew.Ahrens@Sun.COM uint64_t count; 146610801SMatthew.Ahrens@Sun.COM int err; 146710801SMatthew.Ahrens@Sun.COM 146810801SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_phys->ds_num_children >= 2); 146910801SMatthew.Ahrens@Sun.COM err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 147010801SMatthew.Ahrens@Sun.COM /* 147110801SMatthew.Ahrens@Sun.COM * The err should not be ENOENT, but a bug in a previous version 147210801SMatthew.Ahrens@Sun.COM * of the code could cause upgrade_clones_cb() to not set 147310801SMatthew.Ahrens@Sun.COM * ds_next_snap_obj when it should, leading to a missing entry. 147410801SMatthew.Ahrens@Sun.COM * If we knew that the pool was created after 147510801SMatthew.Ahrens@Sun.COM * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 147610801SMatthew.Ahrens@Sun.COM * ENOENT. However, at least we can check that we don't have 147710801SMatthew.Ahrens@Sun.COM * too many entries in the next_clones_obj even after failing to 147810801SMatthew.Ahrens@Sun.COM * remove this one. 147910801SMatthew.Ahrens@Sun.COM */ 148010801SMatthew.Ahrens@Sun.COM if (err != ENOENT) { 148110801SMatthew.Ahrens@Sun.COM VERIFY3U(err, ==, 0); 148210801SMatthew.Ahrens@Sun.COM } 148310801SMatthew.Ahrens@Sun.COM ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj, 148410801SMatthew.Ahrens@Sun.COM &count)); 148510801SMatthew.Ahrens@Sun.COM ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 148610801SMatthew.Ahrens@Sun.COM } 148710801SMatthew.Ahrens@Sun.COM 14885367Sahrens void 14894543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 14902199Sahrens { 149110242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 149210242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 14932199Sahrens zio_t *zio; 14942199Sahrens int err; 14952199Sahrens int after_branch_point = FALSE; 14962199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 14972199Sahrens objset_t *mos = dp->dp_meta_objset; 14982199Sahrens dsl_dataset_t *ds_prev = NULL; 14992199Sahrens uint64_t obj; 15002199Sahrens 15016689Smaybee ASSERT(ds->ds_owner); 150210242Schris.kirby@sun.com ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1); 15032199Sahrens ASSERT(ds->ds_prev == NULL || 15042199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 15052199Sahrens ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 15062199Sahrens 150710242Schris.kirby@sun.com if (dsda->defer) { 150810242Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 150910242Schris.kirby@sun.com if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) { 151010242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 151110242Schris.kirby@sun.com ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY; 151210242Schris.kirby@sun.com return; 151310242Schris.kirby@sun.com } 151410242Schris.kirby@sun.com } 151510242Schris.kirby@sun.com 15166689Smaybee /* signal any waiters that this dataset is going away */ 15176689Smaybee mutex_enter(&ds->ds_lock); 15186689Smaybee ds->ds_owner = dsl_reaper; 15196689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 15206689Smaybee mutex_exit(&ds->ds_lock); 15216689Smaybee 15225378Sck153898 /* Remove our reservation */ 15235378Sck153898 if (ds->ds_reserved != 0) { 15245378Sck153898 uint64_t val = 0; 15255378Sck153898 dsl_dataset_set_reservation_sync(ds, &val, cr, tx); 15265378Sck153898 ASSERT3U(ds->ds_reserved, ==, 0); 15275378Sck153898 } 15285378Sck153898 15292199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 15302199Sahrens 15317046Sahrens dsl_pool_ds_destroyed(ds, tx); 15327046Sahrens 15332199Sahrens obj = ds->ds_object; 1534789Sahrens 1535789Sahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 1536789Sahrens if (ds->ds_prev) { 1537789Sahrens ds_prev = ds->ds_prev; 1538789Sahrens } else { 15396689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15406689Smaybee ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1541789Sahrens } 1542789Sahrens after_branch_point = 1543789Sahrens (ds_prev->ds_phys->ds_next_snap_obj != obj); 1544789Sahrens 1545789Sahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1546789Sahrens if (after_branch_point && 15477046Sahrens ds_prev->ds_phys->ds_next_clones_obj != 0) { 154810801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds_prev, obj, tx); 15497046Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15507046Sahrens VERIFY(0 == zap_add_int(mos, 15517046Sahrens ds_prev->ds_phys->ds_next_clones_obj, 15527046Sahrens ds->ds_phys->ds_next_snap_obj, tx)); 15537046Sahrens } 15547046Sahrens } 15557046Sahrens if (after_branch_point && 1556789Sahrens ds->ds_phys->ds_next_snap_obj == 0) { 1557789Sahrens /* This clone is toast. */ 1558789Sahrens ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1559789Sahrens ds_prev->ds_phys->ds_num_children--; 156010242Schris.kirby@sun.com 156110242Schris.kirby@sun.com /* 156210242Schris.kirby@sun.com * If the clone's origin has no other clones, no 156310242Schris.kirby@sun.com * user holds, and has been marked for deferred 156410242Schris.kirby@sun.com * deletion, then we should have done the necessary 156510242Schris.kirby@sun.com * destroy setup for it. 156610242Schris.kirby@sun.com */ 156710242Schris.kirby@sun.com if (ds_prev->ds_phys->ds_num_children == 1 && 156810242Schris.kirby@sun.com ds_prev->ds_userrefs == 0 && 156910242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds_prev)) { 157010242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, !=, NULL); 157110242Schris.kirby@sun.com } else { 157210242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 157310242Schris.kirby@sun.com } 1574789Sahrens } else if (!after_branch_point) { 1575789Sahrens ds_prev->ds_phys->ds_next_snap_obj = 1576789Sahrens ds->ds_phys->ds_next_snap_obj; 1577789Sahrens } 1578789Sahrens } 1579789Sahrens 1580789Sahrens zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1581789Sahrens 1582789Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15832199Sahrens blkptr_t bp; 1584789Sahrens dsl_dataset_t *ds_next; 1585789Sahrens uint64_t itor = 0; 15865378Sck153898 uint64_t old_unique; 15877390SMatthew.Ahrens@Sun.COM int64_t used = 0, compressed = 0, uncompressed = 0; 1588789Sahrens 15896689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15906689Smaybee ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1591789Sahrens ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1592789Sahrens 15935378Sck153898 old_unique = dsl_dataset_unique(ds_next); 15945378Sck153898 1595789Sahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1596789Sahrens ds_next->ds_phys->ds_prev_snap_obj = 1597789Sahrens ds->ds_phys->ds_prev_snap_obj; 1598789Sahrens ds_next->ds_phys->ds_prev_snap_txg = 1599789Sahrens ds->ds_phys->ds_prev_snap_txg; 1600789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1601789Sahrens ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1602789Sahrens 1603789Sahrens /* 1604789Sahrens * Transfer to our deadlist (which will become next's 1605789Sahrens * new deadlist) any entries from next's current 1606789Sahrens * deadlist which were born before prev, and free the 1607789Sahrens * other entries. 1608789Sahrens * 1609789Sahrens * XXX we're doing this long task with the config lock held 1610789Sahrens */ 16116689Smaybee while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1612789Sahrens if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 16131544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 16141544Seschrock &bp, tx)); 1615789Sahrens if (ds_prev && !after_branch_point && 1616789Sahrens bp.blk_birth > 1617789Sahrens ds_prev->ds_phys->ds_prev_snap_txg) { 1618789Sahrens ds_prev->ds_phys->ds_unique_bytes += 16192082Seschrock bp_get_dasize(dp->dp_spa, &bp); 1620789Sahrens } 1621789Sahrens } else { 16222082Seschrock used += bp_get_dasize(dp->dp_spa, &bp); 1623789Sahrens compressed += BP_GET_PSIZE(&bp); 1624789Sahrens uncompressed += BP_GET_UCSIZE(&bp); 1625789Sahrens /* XXX check return value? */ 16267046Sahrens (void) dsl_free(zio, dp, tx->tx_txg, 1627789Sahrens &bp, NULL, NULL, ARC_NOWAIT); 1628789Sahrens } 1629789Sahrens } 1630789Sahrens 16317390SMatthew.Ahrens@Sun.COM ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 16327390SMatthew.Ahrens@Sun.COM 16337390SMatthew.Ahrens@Sun.COM /* change snapused */ 16347390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 16357390SMatthew.Ahrens@Sun.COM -used, -compressed, -uncompressed, tx); 16367390SMatthew.Ahrens@Sun.COM 1637789Sahrens /* free next's deadlist */ 1638789Sahrens bplist_close(&ds_next->ds_deadlist); 1639789Sahrens bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1640789Sahrens 1641789Sahrens /* set next's deadlist to our deadlist */ 16426689Smaybee bplist_close(&ds->ds_deadlist); 1643789Sahrens ds_next->ds_phys->ds_deadlist_obj = 1644789Sahrens ds->ds_phys->ds_deadlist_obj; 16451544Seschrock VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 16461544Seschrock ds_next->ds_phys->ds_deadlist_obj)); 1647789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1648789Sahrens 1649789Sahrens if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1650789Sahrens /* 1651789Sahrens * Update next's unique to include blocks which 1652789Sahrens * were previously shared by only this snapshot 1653789Sahrens * and it. Those blocks will be born after the 1654789Sahrens * prev snap and before this snap, and will have 1655789Sahrens * died after the next snap and before the one 1656789Sahrens * after that (ie. be on the snap after next's 1657789Sahrens * deadlist). 1658789Sahrens * 1659789Sahrens * XXX we're doing this long task with the 1660789Sahrens * config lock held 1661789Sahrens */ 1662789Sahrens dsl_dataset_t *ds_after_next; 16637390SMatthew.Ahrens@Sun.COM uint64_t space; 1664789Sahrens 16656689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16666689Smaybee ds_next->ds_phys->ds_next_snap_obj, 16676689Smaybee FTAG, &ds_after_next)); 16687390SMatthew.Ahrens@Sun.COM 16697390SMatthew.Ahrens@Sun.COM VERIFY(0 == 16707390SMatthew.Ahrens@Sun.COM bplist_space_birthrange(&ds_after_next->ds_deadlist, 16717390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_prev_snap_txg, 16727390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_creation_txg, &space)); 16737390SMatthew.Ahrens@Sun.COM ds_next->ds_phys->ds_unique_bytes += space; 1674789Sahrens 16756689Smaybee dsl_dataset_rele(ds_after_next, FTAG); 1676789Sahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 1677789Sahrens } else { 1678789Sahrens ASSERT3P(ds_next->ds_prev, ==, ds); 16796689Smaybee dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 16806689Smaybee ds_next->ds_prev = NULL; 1681789Sahrens if (ds_prev) { 16826689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 16836689Smaybee ds->ds_phys->ds_prev_snap_obj, 16846689Smaybee ds_next, &ds_next->ds_prev)); 1685789Sahrens } 16865378Sck153898 16875378Sck153898 dsl_dataset_recalc_head_uniq(ds_next); 16885378Sck153898 16895378Sck153898 /* 16905378Sck153898 * Reduce the amount of our unconsmed refreservation 16915378Sck153898 * being charged to our parent by the amount of 16925378Sck153898 * new unique data we have gained. 16935378Sck153898 */ 16945378Sck153898 if (old_unique < ds_next->ds_reserved) { 16955378Sck153898 int64_t mrsdelta; 16965378Sck153898 uint64_t new_unique = 16975378Sck153898 ds_next->ds_phys->ds_unique_bytes; 16985378Sck153898 16995378Sck153898 ASSERT(old_unique <= new_unique); 17005378Sck153898 mrsdelta = MIN(new_unique - old_unique, 17015378Sck153898 ds_next->ds_reserved - old_unique); 17027390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, 17037390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 17045378Sck153898 } 1705789Sahrens } 17066689Smaybee dsl_dataset_rele(ds_next, FTAG); 1707789Sahrens } else { 1708789Sahrens /* 1709789Sahrens * There's no next snapshot, so this is a head dataset. 1710789Sahrens * Destroy the deadlist. Unless it's a clone, the 1711789Sahrens * deadlist should be empty. (If it's a clone, it's 1712789Sahrens * safe to ignore the deadlist contents.) 1713789Sahrens */ 1714789Sahrens struct killarg ka; 1715789Sahrens 1716789Sahrens ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1717789Sahrens bplist_close(&ds->ds_deadlist); 1718789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1719789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1720789Sahrens 1721789Sahrens /* 1722789Sahrens * Free everything that we point to (that's born after 1723789Sahrens * the previous snapshot, if we are a clone) 1724789Sahrens * 17257390SMatthew.Ahrens@Sun.COM * NB: this should be very quick, because we already 17267390SMatthew.Ahrens@Sun.COM * freed all the objects in open context. 1727789Sahrens */ 17287390SMatthew.Ahrens@Sun.COM ka.ds = ds; 1729789Sahrens ka.zio = zio; 1730789Sahrens ka.tx = tx; 17317837SMatthew.Ahrens@Sun.COM err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 17327837SMatthew.Ahrens@Sun.COM TRAVERSE_POST, kill_blkptr, &ka); 1733789Sahrens ASSERT3U(err, ==, 0); 17349390Schris.kirby@sun.com ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 17357390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_unique_bytes == 0); 173610342Schris.kirby@sun.com 173710342Schris.kirby@sun.com if (ds->ds_prev != NULL) { 173810342Schris.kirby@sun.com dsl_dataset_rele(ds->ds_prev, ds); 173910342Schris.kirby@sun.com ds->ds_prev = ds_prev = NULL; 174010342Schris.kirby@sun.com } 1741789Sahrens } 1742789Sahrens 1743789Sahrens err = zio_wait(zio); 1744789Sahrens ASSERT3U(err, ==, 0); 1745789Sahrens 17466689Smaybee if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 17476689Smaybee /* Erase the link in the dir */ 17486689Smaybee dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 17496689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 17506689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1751789Sahrens err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1752789Sahrens ASSERT(err == 0); 1753789Sahrens } else { 1754789Sahrens /* remove from snapshot namespace */ 1755789Sahrens dsl_dataset_t *ds_head; 17566689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 17576689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 17586689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 17592207Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 1760789Sahrens #ifdef ZFS_DEBUG 1761789Sahrens { 1762789Sahrens uint64_t val; 17636492Stimh 17646689Smaybee err = dsl_dataset_snap_lookup(ds_head, 17656492Stimh ds->ds_snapname, &val); 1766789Sahrens ASSERT3U(err, ==, 0); 1767789Sahrens ASSERT3U(val, ==, obj); 1768789Sahrens } 1769789Sahrens #endif 17706689Smaybee err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1771789Sahrens ASSERT(err == 0); 17726689Smaybee dsl_dataset_rele(ds_head, FTAG); 1773789Sahrens } 1774789Sahrens 1775789Sahrens if (ds_prev && ds->ds_prev != ds_prev) 17766689Smaybee dsl_dataset_rele(ds_prev, FTAG); 1777789Sahrens 17785094Slling spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 17794543Smarks spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 17804543Smarks cr, "dataset = %llu", ds->ds_object); 17814543Smarks 17827046Sahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 17837046Sahrens uint64_t count; 17847046Sahrens ASSERT(0 == zap_count(mos, 17857046Sahrens ds->ds_phys->ds_next_clones_obj, &count) && count == 0); 17867046Sahrens VERIFY(0 == dmu_object_free(mos, 17877046Sahrens ds->ds_phys->ds_next_clones_obj, tx)); 17887046Sahrens } 17897390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_props_obj != 0) 17907390SMatthew.Ahrens@Sun.COM VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx)); 179110242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) 179210242Schris.kirby@sun.com VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx)); 17936689Smaybee dsl_dir_close(ds->ds_dir, ds); 17946689Smaybee ds->ds_dir = NULL; 17956689Smaybee dsl_dataset_drain_refs(ds, tag); 17962199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 179710242Schris.kirby@sun.com 179810242Schris.kirby@sun.com if (dsda->rm_origin) { 179910242Schris.kirby@sun.com /* 180010242Schris.kirby@sun.com * Remove the origin of the clone we just destroyed. 180110242Schris.kirby@sun.com */ 180210242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 180310242Schris.kirby@sun.com 180410342Schris.kirby@sun.com ndsda.ds = dsda->rm_origin; 180510242Schris.kirby@sun.com dsl_dataset_destroy_sync(&ndsda, tag, cr, tx); 180610242Schris.kirby@sun.com } 18072199Sahrens } 18082199Sahrens 18095378Sck153898 static int 18105378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 18115378Sck153898 { 18125378Sck153898 uint64_t asize; 18135378Sck153898 18145378Sck153898 if (!dmu_tx_is_syncing(tx)) 18155378Sck153898 return (0); 18165378Sck153898 18175378Sck153898 /* 18185378Sck153898 * If there's an fs-only reservation, any blocks that might become 18195378Sck153898 * owned by the snapshot dataset must be accommodated by space 18205378Sck153898 * outside of the reservation. 18215378Sck153898 */ 18225378Sck153898 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 18235378Sck153898 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 18245378Sck153898 return (ENOSPC); 18255378Sck153898 18265378Sck153898 /* 18275378Sck153898 * Propogate any reserved space for this snapshot to other 18285378Sck153898 * snapshot checks in this sync group. 18295378Sck153898 */ 18305378Sck153898 if (asize > 0) 18315378Sck153898 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 18325378Sck153898 18335378Sck153898 return (0); 18345378Sck153898 } 18355378Sck153898 18362199Sahrens /* ARGSUSED */ 18372199Sahrens int 18382199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 18392199Sahrens { 18405367Sahrens dsl_dataset_t *ds = arg1; 18412199Sahrens const char *snapname = arg2; 18422199Sahrens int err; 18432199Sahrens uint64_t value; 1844789Sahrens 1845789Sahrens /* 18462199Sahrens * We don't allow multiple snapshots of the same txg. If there 18472199Sahrens * is already one, try again. 18482199Sahrens */ 18492199Sahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 18502199Sahrens return (EAGAIN); 18512199Sahrens 18522199Sahrens /* 18532199Sahrens * Check for conflicting name snapshot name. 1854789Sahrens */ 18556689Smaybee err = dsl_dataset_snap_lookup(ds, snapname, &value); 18562199Sahrens if (err == 0) 18572199Sahrens return (EEXIST); 18582199Sahrens if (err != ENOENT) 18592199Sahrens return (err); 1860789Sahrens 18613978Smmusante /* 18623978Smmusante * Check that the dataset's name is not too long. Name consists 18633978Smmusante * of the dataset's length + 1 for the @-sign + snapshot name's length 18643978Smmusante */ 18653978Smmusante if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 18663978Smmusante return (ENAMETOOLONG); 18673978Smmusante 18685378Sck153898 err = dsl_dataset_snapshot_reserve_space(ds, tx); 18695378Sck153898 if (err) 18705378Sck153898 return (err); 18715378Sck153898 18722199Sahrens ds->ds_trysnap_txg = tx->tx_txg; 1873789Sahrens return (0); 1874789Sahrens } 1875789Sahrens 18762199Sahrens void 18774543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1878789Sahrens { 18795367Sahrens dsl_dataset_t *ds = arg1; 18802199Sahrens const char *snapname = arg2; 18812199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1882789Sahrens dmu_buf_t *dbuf; 1883789Sahrens dsl_dataset_phys_t *dsphys; 18847046Sahrens uint64_t dsobj, crtxg; 1885789Sahrens objset_t *mos = dp->dp_meta_objset; 1886789Sahrens int err; 1887789Sahrens 18882199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1889789Sahrens 18907046Sahrens /* 18917046Sahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 18927046Sahrens */ 18937046Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 18947046Sahrens crtxg = 1; 18957046Sahrens else 18967046Sahrens crtxg = tx->tx_txg; 18977046Sahrens 1898928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1899928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 19001544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1901789Sahrens dmu_buf_will_dirty(dbuf, tx); 1902789Sahrens dsphys = dbuf->db_data; 19036689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 19042199Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1905789Sahrens dsphys->ds_fsid_guid = unique_create(); 1906789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1907789Sahrens sizeof (dsphys->ds_guid)); 1908789Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1909789Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1910789Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1911789Sahrens dsphys->ds_num_children = 1; 1912789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 19137046Sahrens dsphys->ds_creation_txg = crtxg; 1914789Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1915789Sahrens dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1916789Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1917789Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 19182082Seschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1919789Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 19201544Seschrock dmu_buf_rele(dbuf, FTAG); 1921789Sahrens 19222199Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 19232199Sahrens if (ds->ds_prev) { 19247046Sahrens uint64_t next_clones_obj = 19257046Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 19262199Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1927789Sahrens ds->ds_object || 19282199Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 19292199Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 19302199Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1931789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 19322199Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 19332199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 19347046Sahrens } else if (next_clones_obj != 0) { 193510801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds->ds_prev, 193610801SMatthew.Ahrens@Sun.COM dsphys->ds_next_snap_obj, tx); 19377046Sahrens VERIFY3U(0, ==, zap_add_int(mos, 19387046Sahrens next_clones_obj, dsobj, tx)); 1939789Sahrens } 1940789Sahrens } 1941789Sahrens 19425378Sck153898 /* 19435378Sck153898 * If we have a reference-reservation on this dataset, we will 19445378Sck153898 * need to increase the amount of refreservation being charged 19455378Sck153898 * since our unique space is going to zero. 19465378Sck153898 */ 19475378Sck153898 if (ds->ds_reserved) { 19485378Sck153898 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 19497390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 19507390SMatthew.Ahrens@Sun.COM add, 0, 0, tx); 19515378Sck153898 } 19525378Sck153898 1953789Sahrens bplist_close(&ds->ds_deadlist); 1954789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 19555712Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1956789Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 19577046Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1958789Sahrens ds->ds_phys->ds_unique_bytes = 0; 19595378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 19605378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1961789Sahrens ds->ds_phys->ds_deadlist_obj = 1962789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 19631544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 19641544Seschrock ds->ds_phys->ds_deadlist_obj)); 1965789Sahrens 1966789Sahrens dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1967789Sahrens err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1968789Sahrens snapname, 8, 1, &dsobj, tx); 1969789Sahrens ASSERT(err == 0); 1970789Sahrens 1971789Sahrens if (ds->ds_prev) 19726689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 19736689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 19746689Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 19754543Smarks 19767046Sahrens dsl_pool_ds_snapshotted(ds, tx); 19777046Sahrens 197810373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(ds->ds_dir); 197910373Schris.kirby@sun.com 19804543Smarks spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 19814603Sahrens "dataset = %llu", dsobj); 1982789Sahrens } 1983789Sahrens 1984789Sahrens void 19853547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1986789Sahrens { 1987789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 198810298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 1989789Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1990789Sahrens 19914787Sahrens /* 19924787Sahrens * in case we had to change ds_fsid_guid when we opened it, 19934787Sahrens * sync it out now. 19944787Sahrens */ 19954787Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 19964787Sahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 19974787Sahrens 1998789Sahrens dsl_dir_dirty(ds->ds_dir, tx); 199910298SMatthew.Ahrens@Sun.COM dmu_objset_sync(ds->ds_objset, zio, tx); 2000789Sahrens } 2001789Sahrens 2002789Sahrens void 20032885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 2004789Sahrens { 20055378Sck153898 uint64_t refd, avail, uobjs, aobjs; 20065378Sck153898 20072885Sahrens dsl_dir_stats(ds->ds_dir, nv); 2008789Sahrens 20095378Sck153898 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 20105378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 20115378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 20125378Sck153898 20132885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 20142885Sahrens ds->ds_phys->ds_creation_time); 20152885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 20162885Sahrens ds->ds_phys->ds_creation_txg); 20175378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 20185378Sck153898 ds->ds_quota); 20195378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 20205378Sck153898 ds->ds_reserved); 20216643Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 20226643Seschrock ds->ds_phys->ds_guid); 202310575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 202410575SEric.Schrock@Sun.COM dsl_dataset_unique(ds)); 202510575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 202610575SEric.Schrock@Sun.COM ds->ds_object); 202710242Schris.kirby@sun.com dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, ds->ds_userrefs); 202810242Schris.kirby@sun.com dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 202910242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 2030789Sahrens 2031789Sahrens if (ds->ds_phys->ds_next_snap_obj) { 2032789Sahrens /* 2033789Sahrens * This is a snapshot; override the dd's space used with 20342885Sahrens * our unique space and compression ratio. 2035789Sahrens */ 20362885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 20372885Sahrens ds->ds_phys->ds_unique_bytes); 20382885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 20392885Sahrens ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 20402885Sahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 20412885Sahrens ds->ds_phys->ds_compressed_bytes)); 2042789Sahrens } 2043789Sahrens } 2044789Sahrens 20452885Sahrens void 20462885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2047789Sahrens { 20482885Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 20492885Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 20505367Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 20512885Sahrens if (ds->ds_phys->ds_next_snap_obj) { 20522885Sahrens stat->dds_is_snapshot = B_TRUE; 20532885Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 20548228SEric.Taylor@Sun.COM } else { 20558228SEric.Taylor@Sun.COM stat->dds_is_snapshot = B_FALSE; 20568228SEric.Taylor@Sun.COM stat->dds_num_clones = 0; 20572885Sahrens } 20582885Sahrens 20592885Sahrens /* clone origin is really a dsl_dir thing... */ 20605446Sahrens rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 20617046Sahrens if (dsl_dir_is_clone(ds->ds_dir)) { 20622885Sahrens dsl_dataset_t *ods; 20632885Sahrens 20646689Smaybee VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 20656689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 20665367Sahrens dsl_dataset_name(ods, stat->dds_origin); 20676689Smaybee dsl_dataset_drop_ref(ods, FTAG); 20688228SEric.Taylor@Sun.COM } else { 20698228SEric.Taylor@Sun.COM stat->dds_origin[0] = '\0'; 20702885Sahrens } 20715446Sahrens rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 20722885Sahrens } 20732885Sahrens 20742885Sahrens uint64_t 20752885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 20762885Sahrens { 20774787Sahrens return (ds->ds_fsid_guid); 20782885Sahrens } 20792885Sahrens 20802885Sahrens void 20812885Sahrens dsl_dataset_space(dsl_dataset_t *ds, 20822885Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 20832885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 20842885Sahrens { 20852885Sahrens *refdbytesp = ds->ds_phys->ds_used_bytes; 20862885Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 20875378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 20885378Sck153898 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 20895378Sck153898 if (ds->ds_quota != 0) { 20905378Sck153898 /* 20915378Sck153898 * Adjust available bytes according to refquota 20925378Sck153898 */ 20935378Sck153898 if (*refdbytesp < ds->ds_quota) 20945378Sck153898 *availbytesp = MIN(*availbytesp, 20955378Sck153898 ds->ds_quota - *refdbytesp); 20965378Sck153898 else 20975378Sck153898 *availbytesp = 0; 20985378Sck153898 } 20992885Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 21002885Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2101789Sahrens } 2102789Sahrens 21035326Sek110237 boolean_t 21045326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 21055326Sek110237 { 21065326Sek110237 dsl_pool_t *dp = ds->ds_dir->dd_pool; 21075326Sek110237 21085326Sek110237 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 21095326Sek110237 dsl_pool_sync_context(dp)); 21105326Sek110237 if (ds->ds_prev == NULL) 21115326Sek110237 return (B_FALSE); 21125326Sek110237 if (ds->ds_phys->ds_bp.blk_birth > 21135326Sek110237 ds->ds_prev->ds_phys->ds_creation_txg) 21145326Sek110237 return (B_TRUE); 21155326Sek110237 return (B_FALSE); 21165326Sek110237 } 21175326Sek110237 21182199Sahrens /* ARGSUSED */ 2119789Sahrens static int 21202199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 2121789Sahrens { 21222199Sahrens dsl_dataset_t *ds = arg1; 21232199Sahrens char *newsnapname = arg2; 21242199Sahrens dsl_dir_t *dd = ds->ds_dir; 21252199Sahrens dsl_dataset_t *hds; 21262199Sahrens uint64_t val; 2127789Sahrens int err; 2128789Sahrens 21296689Smaybee err = dsl_dataset_hold_obj(dd->dd_pool, 21306689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2131789Sahrens if (err) 2132789Sahrens return (err); 2133789Sahrens 21342199Sahrens /* new name better not be in use */ 21356689Smaybee err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 21366689Smaybee dsl_dataset_rele(hds, FTAG); 2137789Sahrens 21382199Sahrens if (err == 0) 21392199Sahrens err = EEXIST; 21402199Sahrens else if (err == ENOENT) 21412199Sahrens err = 0; 21424007Smmusante 21434007Smmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 21444007Smmusante if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 21454007Smmusante err = ENAMETOOLONG; 21464007Smmusante 21472199Sahrens return (err); 21482199Sahrens } 2149789Sahrens 21502199Sahrens static void 21514543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 21524543Smarks cred_t *cr, dmu_tx_t *tx) 21532199Sahrens { 21542199Sahrens dsl_dataset_t *ds = arg1; 21554543Smarks const char *newsnapname = arg2; 21562199Sahrens dsl_dir_t *dd = ds->ds_dir; 21572199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 21582199Sahrens dsl_dataset_t *hds; 21592199Sahrens int err; 2160789Sahrens 21612199Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2162789Sahrens 21636689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 21646689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2165789Sahrens 21662199Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 21676689Smaybee err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2168789Sahrens ASSERT3U(err, ==, 0); 21692199Sahrens mutex_enter(&ds->ds_lock); 21702199Sahrens (void) strcpy(ds->ds_snapname, newsnapname); 21712199Sahrens mutex_exit(&ds->ds_lock); 21722199Sahrens err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 21732199Sahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2174789Sahrens ASSERT3U(err, ==, 0); 2175789Sahrens 21764543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 21774543Smarks cr, "dataset = %llu", ds->ds_object); 21786689Smaybee dsl_dataset_rele(hds, FTAG); 2179789Sahrens } 2180789Sahrens 21815326Sek110237 struct renamesnaparg { 21824007Smmusante dsl_sync_task_group_t *dstg; 21834007Smmusante char failed[MAXPATHLEN]; 21844007Smmusante char *oldsnap; 21854007Smmusante char *newsnap; 21864007Smmusante }; 21874007Smmusante 21884007Smmusante static int 21894007Smmusante dsl_snapshot_rename_one(char *name, void *arg) 21904007Smmusante { 21915326Sek110237 struct renamesnaparg *ra = arg; 21924007Smmusante dsl_dataset_t *ds = NULL; 21934007Smmusante char *cp; 21944007Smmusante int err; 21954007Smmusante 21964007Smmusante cp = name + strlen(name); 21974007Smmusante *cp = '@'; 21984007Smmusante (void) strcpy(cp + 1, ra->oldsnap); 21994543Smarks 22004543Smarks /* 22014543Smarks * For recursive snapshot renames the parent won't be changing 22024543Smarks * so we just pass name for both the to/from argument. 22034543Smarks */ 22047312SMatthew.Ahrens@Sun.COM err = zfs_secpolicy_rename_perms(name, name, CRED()); 22057312SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 22067312SMatthew.Ahrens@Sun.COM return (0); 22077312SMatthew.Ahrens@Sun.COM } else if (err) { 22084543Smarks (void) strcpy(ra->failed, name); 22094543Smarks return (err); 22104543Smarks } 22114543Smarks 22126689Smaybee #ifdef _KERNEL 22136689Smaybee /* 22146689Smaybee * For all filesystems undergoing rename, we'll need to unmount it. 22156689Smaybee */ 22166689Smaybee (void) zfs_unmount_snap(name, NULL); 22176689Smaybee #endif 22186689Smaybee err = dsl_dataset_hold(name, ra->dstg, &ds); 22196689Smaybee *cp = '\0'; 22204007Smmusante if (err == ENOENT) { 22214007Smmusante return (0); 22226689Smaybee } else if (err) { 22234007Smmusante (void) strcpy(ra->failed, name); 22244007Smmusante return (err); 22254007Smmusante } 22264007Smmusante 22274007Smmusante dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 22284007Smmusante dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 22294007Smmusante 22304007Smmusante return (0); 22314007Smmusante } 22324007Smmusante 22334007Smmusante static int 22344007Smmusante dsl_recursive_rename(char *oldname, const char *newname) 22354007Smmusante { 22364007Smmusante int err; 22375326Sek110237 struct renamesnaparg *ra; 22384007Smmusante dsl_sync_task_t *dst; 22394007Smmusante spa_t *spa; 22404007Smmusante char *cp, *fsname = spa_strdup(oldname); 22414007Smmusante int len = strlen(oldname); 22424007Smmusante 22434007Smmusante /* truncate the snapshot name to get the fsname */ 22444007Smmusante cp = strchr(fsname, '@'); 22454007Smmusante *cp = '\0'; 22464007Smmusante 22474603Sahrens err = spa_open(fsname, &spa, FTAG); 22484007Smmusante if (err) { 22494007Smmusante kmem_free(fsname, len + 1); 22504007Smmusante return (err); 22514007Smmusante } 22525326Sek110237 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 22534007Smmusante ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 22544007Smmusante 22554007Smmusante ra->oldsnap = strchr(oldname, '@') + 1; 22564007Smmusante ra->newsnap = strchr(newname, '@') + 1; 22574007Smmusante *ra->failed = '\0'; 22584007Smmusante 22594007Smmusante err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 22604007Smmusante DS_FIND_CHILDREN); 22614007Smmusante kmem_free(fsname, len + 1); 22624007Smmusante 22634007Smmusante if (err == 0) { 22644007Smmusante err = dsl_sync_task_group_wait(ra->dstg); 22654007Smmusante } 22664007Smmusante 22674007Smmusante for (dst = list_head(&ra->dstg->dstg_tasks); dst; 22684007Smmusante dst = list_next(&ra->dstg->dstg_tasks, dst)) { 22694007Smmusante dsl_dataset_t *ds = dst->dst_arg1; 22704007Smmusante if (dst->dst_err) { 22714007Smmusante dsl_dir_name(ds->ds_dir, ra->failed); 22724009Smmusante (void) strcat(ra->failed, "@"); 22734009Smmusante (void) strcat(ra->failed, ra->newsnap); 22744007Smmusante } 22756689Smaybee dsl_dataset_rele(ds, ra->dstg); 22764007Smmusante } 22774007Smmusante 22784543Smarks if (err) 22794543Smarks (void) strcpy(oldname, ra->failed); 22804007Smmusante 22814007Smmusante dsl_sync_task_group_destroy(ra->dstg); 22825326Sek110237 kmem_free(ra, sizeof (struct renamesnaparg)); 22834007Smmusante spa_close(spa, FTAG); 22844007Smmusante return (err); 22854007Smmusante } 22864007Smmusante 22874569Smmusante static int 22884569Smmusante dsl_valid_rename(char *oldname, void *arg) 22894569Smmusante { 22904569Smmusante int delta = *(int *)arg; 22914569Smmusante 22924569Smmusante if (strlen(oldname) + delta >= MAXNAMELEN) 22934569Smmusante return (ENAMETOOLONG); 22944569Smmusante 22954569Smmusante return (0); 22964569Smmusante } 22974569Smmusante 2298789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename 2299789Sahrens int 23006689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2301789Sahrens { 2302789Sahrens dsl_dir_t *dd; 23032199Sahrens dsl_dataset_t *ds; 2304789Sahrens const char *tail; 2305789Sahrens int err; 2306789Sahrens 23072199Sahrens err = dsl_dir_open(oldname, FTAG, &dd, &tail); 23081544Seschrock if (err) 23091544Seschrock return (err); 23108517SEric.Taylor@Sun.COM /* 23118517SEric.Taylor@Sun.COM * If there are more than 2 references there may be holds 23128517SEric.Taylor@Sun.COM * hanging around that haven't been cleared out yet. 23138517SEric.Taylor@Sun.COM */ 23148517SEric.Taylor@Sun.COM if (dmu_buf_refcount(dd->dd_dbuf) > 2) 23158517SEric.Taylor@Sun.COM txg_wait_synced(dd->dd_pool, 0); 2316789Sahrens if (tail == NULL) { 23174569Smmusante int delta = strlen(newname) - strlen(oldname); 23184569Smmusante 23197046Sahrens /* if we're growing, validate child name lengths */ 23204569Smmusante if (delta > 0) 23214569Smmusante err = dmu_objset_find(oldname, dsl_valid_rename, 23224569Smmusante &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 23234569Smmusante 23244569Smmusante if (!err) 23254569Smmusante err = dsl_dir_rename(dd, newname); 2326789Sahrens dsl_dir_close(dd, FTAG); 2327789Sahrens return (err); 2328789Sahrens } 2329789Sahrens if (tail[0] != '@') { 233010588SEric.Taylor@Sun.COM /* the name ended in a nonexistent component */ 2331789Sahrens dsl_dir_close(dd, FTAG); 2332789Sahrens return (ENOENT); 2333789Sahrens } 2334789Sahrens 23352199Sahrens dsl_dir_close(dd, FTAG); 23362199Sahrens 23372199Sahrens /* new name must be snapshot in same filesystem */ 23382199Sahrens tail = strchr(newname, '@'); 23392199Sahrens if (tail == NULL) 23402199Sahrens return (EINVAL); 23412199Sahrens tail++; 23422199Sahrens if (strncmp(oldname, newname, tail - newname) != 0) 23432199Sahrens return (EXDEV); 2344789Sahrens 23454007Smmusante if (recursive) { 23464007Smmusante err = dsl_recursive_rename(oldname, newname); 23474007Smmusante } else { 23486689Smaybee err = dsl_dataset_hold(oldname, FTAG, &ds); 23494007Smmusante if (err) 23504007Smmusante return (err); 23512199Sahrens 23524007Smmusante err = dsl_sync_task_do(ds->ds_dir->dd_pool, 23534007Smmusante dsl_dataset_snapshot_rename_check, 23544007Smmusante dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 23552199Sahrens 23566689Smaybee dsl_dataset_rele(ds, FTAG); 23574007Smmusante } 23582199Sahrens 2359789Sahrens return (err); 2360789Sahrens } 23612082Seschrock 23627046Sahrens struct promotenode { 23636689Smaybee list_node_t link; 23646689Smaybee dsl_dataset_t *ds; 23656689Smaybee }; 23666689Smaybee 23672199Sahrens struct promotearg { 23687390SMatthew.Ahrens@Sun.COM list_t shared_snaps, origin_snaps, clone_snaps; 23697390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_origin, *origin_head; 23707390SMatthew.Ahrens@Sun.COM uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 237110588SEric.Taylor@Sun.COM char *err_ds; 23722199Sahrens }; 23732199Sahrens 23747390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 23757390SMatthew.Ahrens@Sun.COM 23764543Smarks /* ARGSUSED */ 23772082Seschrock static int 23782199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 23792082Seschrock { 23802199Sahrens dsl_dataset_t *hds = arg1; 23812199Sahrens struct promotearg *pa = arg2; 23827390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 23836689Smaybee dsl_dataset_t *origin_ds = snap->ds; 23846689Smaybee int err; 23852199Sahrens 23867046Sahrens /* Check that it is a real clone */ 23877046Sahrens if (!dsl_dir_is_clone(hds->ds_dir)) 23882082Seschrock return (EINVAL); 23892082Seschrock 23902199Sahrens /* Since this is so expensive, don't do the preliminary check */ 23912199Sahrens if (!dmu_tx_is_syncing(tx)) 23922199Sahrens return (0); 23932199Sahrens 23946689Smaybee if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 23956689Smaybee return (EXDEV); 23962082Seschrock 23975367Sahrens /* compute origin's new unique space */ 23987390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 23997390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 24007390SMatthew.Ahrens@Sun.COM err = bplist_space_birthrange(&snap->ds->ds_deadlist, 24017390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique); 24027390SMatthew.Ahrens@Sun.COM if (err) 24036689Smaybee return (err); 24046689Smaybee 24056689Smaybee /* 24066689Smaybee * Walk the snapshots that we are moving 24076689Smaybee * 24087390SMatthew.Ahrens@Sun.COM * Compute space to transfer. Consider the incremental changes 24097390SMatthew.Ahrens@Sun.COM * to used for each snapshot: 24107390SMatthew.Ahrens@Sun.COM * (my used) = (prev's used) + (blocks born) - (blocks killed) 24117390SMatthew.Ahrens@Sun.COM * So each snapshot gave birth to: 24127390SMatthew.Ahrens@Sun.COM * (blocks born) = (my used) - (prev's used) + (blocks killed) 24136689Smaybee * So a sequence would look like: 24147390SMatthew.Ahrens@Sun.COM * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 24156689Smaybee * Which simplifies to: 24167390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + k1 + k0 24176689Smaybee * Note however, if we stop before we reach the ORIGIN we get: 24187390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + kM - uM-1 24196689Smaybee */ 24206689Smaybee pa->used = origin_ds->ds_phys->ds_used_bytes; 24216689Smaybee pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 24226689Smaybee pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 24237390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 24247390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 24252082Seschrock uint64_t val, dlused, dlcomp, dluncomp; 24266689Smaybee dsl_dataset_t *ds = snap->ds; 24272082Seschrock 24282082Seschrock /* Check that the snapshot name does not conflict */ 24297390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 24306689Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 243110588SEric.Taylor@Sun.COM if (err == 0) { 243210588SEric.Taylor@Sun.COM err = EEXIST; 243310588SEric.Taylor@Sun.COM goto out; 243410588SEric.Taylor@Sun.COM } 24356689Smaybee if (err != ENOENT) 243610588SEric.Taylor@Sun.COM goto out; 24376689Smaybee 24386689Smaybee /* The very first snapshot does not have a deadlist */ 24397390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_obj == 0) 24407390SMatthew.Ahrens@Sun.COM continue; 24417390SMatthew.Ahrens@Sun.COM 24427390SMatthew.Ahrens@Sun.COM if (err = bplist_space(&ds->ds_deadlist, 24437390SMatthew.Ahrens@Sun.COM &dlused, &dlcomp, &dluncomp)) 244410588SEric.Taylor@Sun.COM goto out; 24457390SMatthew.Ahrens@Sun.COM pa->used += dlused; 24467390SMatthew.Ahrens@Sun.COM pa->comp += dlcomp; 24477390SMatthew.Ahrens@Sun.COM pa->uncomp += dluncomp; 24487390SMatthew.Ahrens@Sun.COM } 24496689Smaybee 24506689Smaybee /* 24516689Smaybee * If we are a clone of a clone then we never reached ORIGIN, 24526689Smaybee * so we need to subtract out the clone origin's used space. 24536689Smaybee */ 24547390SMatthew.Ahrens@Sun.COM if (pa->origin_origin) { 24557390SMatthew.Ahrens@Sun.COM pa->used -= pa->origin_origin->ds_phys->ds_used_bytes; 24567390SMatthew.Ahrens@Sun.COM pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes; 24577390SMatthew.Ahrens@Sun.COM pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes; 24582082Seschrock } 24592082Seschrock 24602082Seschrock /* Check that there is enough space here */ 24617390SMatthew.Ahrens@Sun.COM err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 24627390SMatthew.Ahrens@Sun.COM pa->used); 24637390SMatthew.Ahrens@Sun.COM if (err) 24647390SMatthew.Ahrens@Sun.COM return (err); 24657390SMatthew.Ahrens@Sun.COM 24667390SMatthew.Ahrens@Sun.COM /* 24677390SMatthew.Ahrens@Sun.COM * Compute the amounts of space that will be used by snapshots 24687390SMatthew.Ahrens@Sun.COM * after the promotion (for both origin and clone). For each, 24697390SMatthew.Ahrens@Sun.COM * it is the amount of space that will be on all of their 24707390SMatthew.Ahrens@Sun.COM * deadlists (that was not born before their new origin). 24717390SMatthew.Ahrens@Sun.COM */ 24727390SMatthew.Ahrens@Sun.COM if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 24737390SMatthew.Ahrens@Sun.COM uint64_t space; 24747390SMatthew.Ahrens@Sun.COM 24757390SMatthew.Ahrens@Sun.COM /* 24767390SMatthew.Ahrens@Sun.COM * Note, typically this will not be a clone of a clone, 24777390SMatthew.Ahrens@Sun.COM * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so 24787390SMatthew.Ahrens@Sun.COM * these snaplist_space() -> bplist_space_birthrange() 24797390SMatthew.Ahrens@Sun.COM * calls will be fast because they do not have to 24807390SMatthew.Ahrens@Sun.COM * iterate over all bps. 24817390SMatthew.Ahrens@Sun.COM */ 24827390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 24837390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->shared_snaps, 24847390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &pa->cloneusedsnap); 24857390SMatthew.Ahrens@Sun.COM if (err) 24867390SMatthew.Ahrens@Sun.COM return (err); 24877390SMatthew.Ahrens@Sun.COM 24887390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->clone_snaps, 24897390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &space); 24907390SMatthew.Ahrens@Sun.COM if (err) 24917390SMatthew.Ahrens@Sun.COM return (err); 24927390SMatthew.Ahrens@Sun.COM pa->cloneusedsnap += space; 24936689Smaybee } 24947390SMatthew.Ahrens@Sun.COM if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 24957390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->origin_snaps, 24967390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap); 24977390SMatthew.Ahrens@Sun.COM if (err) 24987390SMatthew.Ahrens@Sun.COM return (err); 24997390SMatthew.Ahrens@Sun.COM } 25007390SMatthew.Ahrens@Sun.COM 25017390SMatthew.Ahrens@Sun.COM return (0); 250210588SEric.Taylor@Sun.COM out: 250310588SEric.Taylor@Sun.COM pa->err_ds = snap->ds->ds_snapname; 250410588SEric.Taylor@Sun.COM return (err); 25052199Sahrens } 25062082Seschrock 25072199Sahrens static void 25084543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 25092199Sahrens { 25102199Sahrens dsl_dataset_t *hds = arg1; 25112199Sahrens struct promotearg *pa = arg2; 25127390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 25136689Smaybee dsl_dataset_t *origin_ds = snap->ds; 25147390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_head; 25152199Sahrens dsl_dir_t *dd = hds->ds_dir; 25162199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 25175367Sahrens dsl_dir_t *odd = NULL; 25187046Sahrens uint64_t oldnext_obj; 25197390SMatthew.Ahrens@Sun.COM int64_t delta; 25202199Sahrens 25212199Sahrens ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 25222199Sahrens 25237390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 25247390SMatthew.Ahrens@Sun.COM origin_head = snap->ds; 25257390SMatthew.Ahrens@Sun.COM 25262417Sahrens /* 25275367Sahrens * We need to explicitly open odd, since origin_ds's dd will be 25282417Sahrens * changing. 25292417Sahrens */ 25305367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 25315367Sahrens NULL, FTAG, &odd)); 25322082Seschrock 25336689Smaybee /* change origin's next snap */ 25346689Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 25357046Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 25367390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 25377390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 25387390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 25396689Smaybee 25407046Sahrens /* change the origin's next clone */ 25417046Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 254210801SMatthew.Ahrens@Sun.COM remove_from_next_clones(origin_ds, snap->ds->ds_object, tx); 25437046Sahrens VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 25447046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 25457046Sahrens oldnext_obj, tx)); 25467046Sahrens } 25477046Sahrens 25486689Smaybee /* change origin */ 25496689Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 25506689Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 25516689Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 25527390SMatthew.Ahrens@Sun.COM hds->ds_origin_txg = origin_head->ds_origin_txg; 25536689Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 25546689Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 25557390SMatthew.Ahrens@Sun.COM origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg; 25566689Smaybee 25572082Seschrock /* move snapshots to this dir */ 25587390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 25597390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 25606689Smaybee dsl_dataset_t *ds = snap->ds; 25612082Seschrock 25627237Sek110237 /* unregister props as dsl_dir is changing */ 256310298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 256410298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 256510298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 25667237Sek110237 } 25672082Seschrock /* move snap name entry */ 25687390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 25697390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_snap_remove(origin_head, 25706689Smaybee ds->ds_snapname, tx)); 25712199Sahrens VERIFY(0 == zap_add(dp->dp_meta_objset, 25722082Seschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 25732082Seschrock 8, 1, &ds->ds_object, tx)); 25742082Seschrock /* change containing dsl_dir */ 25752082Seschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 25765367Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 25772082Seschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 25785367Sahrens ASSERT3P(ds->ds_dir, ==, odd); 25792082Seschrock dsl_dir_close(ds->ds_dir, ds); 25802199Sahrens VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 25812082Seschrock NULL, ds, &ds->ds_dir)); 25822082Seschrock 25832082Seschrock ASSERT3U(dsl_prop_numcb(ds), ==, 0); 25847390SMatthew.Ahrens@Sun.COM } 25857390SMatthew.Ahrens@Sun.COM 25867390SMatthew.Ahrens@Sun.COM /* 25877390SMatthew.Ahrens@Sun.COM * Change space accounting. 25887390SMatthew.Ahrens@Sun.COM * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 25897390SMatthew.Ahrens@Sun.COM * both be valid, or both be 0 (resulting in delta == 0). This 25907390SMatthew.Ahrens@Sun.COM * is true for each of {clone,origin} independently. 25917390SMatthew.Ahrens@Sun.COM */ 25927390SMatthew.Ahrens@Sun.COM 25937390SMatthew.Ahrens@Sun.COM delta = pa->cloneusedsnap - 25947390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 25957390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, >=, 0); 25967390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, delta); 25977390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 25987390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_HEAD, 25997390SMatthew.Ahrens@Sun.COM pa->used - delta, pa->comp, pa->uncomp, tx); 26007390SMatthew.Ahrens@Sun.COM 26017390SMatthew.Ahrens@Sun.COM delta = pa->originusedsnap - 26027390SMatthew.Ahrens@Sun.COM odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 26037390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, <=, 0); 26047390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, -delta); 26057390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 26067390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_HEAD, 26077390SMatthew.Ahrens@Sun.COM -pa->used - delta, -pa->comp, -pa->uncomp, tx); 26087390SMatthew.Ahrens@Sun.COM 26095367Sahrens origin_ds->ds_phys->ds_unique_bytes = pa->unique; 26102082Seschrock 26114543Smarks /* log history record */ 26124543Smarks spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 26136689Smaybee cr, "dataset = %llu", hds->ds_object); 26144543Smarks 26155367Sahrens dsl_dir_close(odd, FTAG); 26162082Seschrock } 26172082Seschrock 26187390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist"; 26197390SMatthew.Ahrens@Sun.COM /* 26207390SMatthew.Ahrens@Sun.COM * Make a list of dsl_dataset_t's for the snapshots between first_obj 26217390SMatthew.Ahrens@Sun.COM * (exclusive) and last_obj (inclusive). The list will be in reverse 26227390SMatthew.Ahrens@Sun.COM * order (last_obj will be the list_head()). If first_obj == 0, do all 26237390SMatthew.Ahrens@Sun.COM * snapshots back to this dataset's origin. 26247390SMatthew.Ahrens@Sun.COM */ 26257390SMatthew.Ahrens@Sun.COM static int 26267390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own, 26277390SMatthew.Ahrens@Sun.COM uint64_t first_obj, uint64_t last_obj, list_t *l) 26287390SMatthew.Ahrens@Sun.COM { 26297390SMatthew.Ahrens@Sun.COM uint64_t obj = last_obj; 26307390SMatthew.Ahrens@Sun.COM 26317390SMatthew.Ahrens@Sun.COM ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock)); 26327390SMatthew.Ahrens@Sun.COM 26337390SMatthew.Ahrens@Sun.COM list_create(l, sizeof (struct promotenode), 26347390SMatthew.Ahrens@Sun.COM offsetof(struct promotenode, link)); 26357390SMatthew.Ahrens@Sun.COM 26367390SMatthew.Ahrens@Sun.COM while (obj != first_obj) { 26377390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 26387390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26397390SMatthew.Ahrens@Sun.COM int err; 26407390SMatthew.Ahrens@Sun.COM 26417390SMatthew.Ahrens@Sun.COM if (own) { 26427390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, obj, 26437390SMatthew.Ahrens@Sun.COM 0, snaplist_tag, &ds); 26447390SMatthew.Ahrens@Sun.COM if (err == 0) 26457390SMatthew.Ahrens@Sun.COM dsl_dataset_make_exclusive(ds, snaplist_tag); 26467390SMatthew.Ahrens@Sun.COM } else { 26477390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds); 26487390SMatthew.Ahrens@Sun.COM } 26497390SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 26507390SMatthew.Ahrens@Sun.COM /* lost race with snapshot destroy */ 26517390SMatthew.Ahrens@Sun.COM struct promotenode *last = list_tail(l); 26527390SMatthew.Ahrens@Sun.COM ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj); 26537390SMatthew.Ahrens@Sun.COM obj = last->ds->ds_phys->ds_prev_snap_obj; 26547390SMatthew.Ahrens@Sun.COM continue; 26557390SMatthew.Ahrens@Sun.COM } else if (err) { 26567390SMatthew.Ahrens@Sun.COM return (err); 26577390SMatthew.Ahrens@Sun.COM } 26587390SMatthew.Ahrens@Sun.COM 26597390SMatthew.Ahrens@Sun.COM if (first_obj == 0) 26607390SMatthew.Ahrens@Sun.COM first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 26617390SMatthew.Ahrens@Sun.COM 26627390SMatthew.Ahrens@Sun.COM snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 26637390SMatthew.Ahrens@Sun.COM snap->ds = ds; 26647390SMatthew.Ahrens@Sun.COM list_insert_tail(l, snap); 26657390SMatthew.Ahrens@Sun.COM obj = ds->ds_phys->ds_prev_snap_obj; 26667390SMatthew.Ahrens@Sun.COM } 26677390SMatthew.Ahrens@Sun.COM 26687390SMatthew.Ahrens@Sun.COM return (0); 26697390SMatthew.Ahrens@Sun.COM } 26707390SMatthew.Ahrens@Sun.COM 26717390SMatthew.Ahrens@Sun.COM static int 26727390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 26737390SMatthew.Ahrens@Sun.COM { 26747390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26757390SMatthew.Ahrens@Sun.COM 26767390SMatthew.Ahrens@Sun.COM *spacep = 0; 26777390SMatthew.Ahrens@Sun.COM for (snap = list_head(l); snap; snap = list_next(l, snap)) { 26787390SMatthew.Ahrens@Sun.COM uint64_t used; 26797390SMatthew.Ahrens@Sun.COM int err = bplist_space_birthrange(&snap->ds->ds_deadlist, 26807390SMatthew.Ahrens@Sun.COM mintxg, UINT64_MAX, &used); 26817390SMatthew.Ahrens@Sun.COM if (err) 26827390SMatthew.Ahrens@Sun.COM return (err); 26837390SMatthew.Ahrens@Sun.COM *spacep += used; 26847390SMatthew.Ahrens@Sun.COM } 26857390SMatthew.Ahrens@Sun.COM return (0); 26867390SMatthew.Ahrens@Sun.COM } 26877390SMatthew.Ahrens@Sun.COM 26887390SMatthew.Ahrens@Sun.COM static void 26897390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own) 26907390SMatthew.Ahrens@Sun.COM { 26917390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26927390SMatthew.Ahrens@Sun.COM 26938779SMark.Musante@Sun.COM if (!l || !list_link_active(&l->list_head)) 26947390SMatthew.Ahrens@Sun.COM return; 26957390SMatthew.Ahrens@Sun.COM 26967390SMatthew.Ahrens@Sun.COM while ((snap = list_tail(l)) != NULL) { 26977390SMatthew.Ahrens@Sun.COM list_remove(l, snap); 26987390SMatthew.Ahrens@Sun.COM if (own) 26997390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(snap->ds, snaplist_tag); 27007390SMatthew.Ahrens@Sun.COM else 27017390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(snap->ds, snaplist_tag); 27027390SMatthew.Ahrens@Sun.COM kmem_free(snap, sizeof (struct promotenode)); 27037390SMatthew.Ahrens@Sun.COM } 27047390SMatthew.Ahrens@Sun.COM list_destroy(l); 27057390SMatthew.Ahrens@Sun.COM } 27067390SMatthew.Ahrens@Sun.COM 27077390SMatthew.Ahrens@Sun.COM /* 27087390SMatthew.Ahrens@Sun.COM * Promote a clone. Nomenclature note: 27097390SMatthew.Ahrens@Sun.COM * "clone" or "cds": the original clone which is being promoted 27107390SMatthew.Ahrens@Sun.COM * "origin" or "ods": the snapshot which is originally clone's origin 27117390SMatthew.Ahrens@Sun.COM * "origin head" or "ohds": the dataset which is the head 27127390SMatthew.Ahrens@Sun.COM * (filesystem/volume) for the origin 27137390SMatthew.Ahrens@Sun.COM * "origin origin": the origin of the origin's filesystem (typically 27147390SMatthew.Ahrens@Sun.COM * NULL, indicating that the clone is not a clone of a clone). 27157390SMatthew.Ahrens@Sun.COM */ 27162082Seschrock int 271710588SEric.Taylor@Sun.COM dsl_dataset_promote(const char *name, char *conflsnap) 27182082Seschrock { 27192082Seschrock dsl_dataset_t *ds; 27206689Smaybee dsl_dir_t *dd; 27216689Smaybee dsl_pool_t *dp; 27222082Seschrock dmu_object_info_t doi; 27237390SMatthew.Ahrens@Sun.COM struct promotearg pa = { 0 }; 27247046Sahrens struct promotenode *snap; 27256689Smaybee int err; 27266689Smaybee 27276689Smaybee err = dsl_dataset_hold(name, FTAG, &ds); 27282082Seschrock if (err) 27292082Seschrock return (err); 27306689Smaybee dd = ds->ds_dir; 27316689Smaybee dp = dd->dd_pool; 27326689Smaybee 27336689Smaybee err = dmu_object_info(dp->dp_meta_objset, 27342082Seschrock ds->ds_phys->ds_snapnames_zapobj, &doi); 27352082Seschrock if (err) { 27366689Smaybee dsl_dataset_rele(ds, FTAG); 27372082Seschrock return (err); 27382082Seschrock } 27392082Seschrock 27407390SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) { 27417390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 27427390SMatthew.Ahrens@Sun.COM return (EINVAL); 27437390SMatthew.Ahrens@Sun.COM } 27447390SMatthew.Ahrens@Sun.COM 27452082Seschrock /* 27466689Smaybee * We are going to inherit all the snapshots taken before our 27476689Smaybee * origin (i.e., our new origin will be our parent's origin). 27486689Smaybee * Take ownership of them so that we can rename them into our 27496689Smaybee * namespace. 27506689Smaybee */ 27516689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 27527390SMatthew.Ahrens@Sun.COM 27537390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj, 27547390SMatthew.Ahrens@Sun.COM &pa.shared_snaps); 27557390SMatthew.Ahrens@Sun.COM if (err != 0) 27567390SMatthew.Ahrens@Sun.COM goto out; 27577390SMatthew.Ahrens@Sun.COM 27587390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps); 27597390SMatthew.Ahrens@Sun.COM if (err != 0) 27607390SMatthew.Ahrens@Sun.COM goto out; 27617390SMatthew.Ahrens@Sun.COM 27627390SMatthew.Ahrens@Sun.COM snap = list_head(&pa.shared_snaps); 27637390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 27647390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj, 27657390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps); 27667390SMatthew.Ahrens@Sun.COM if (err != 0) 27677390SMatthew.Ahrens@Sun.COM goto out; 27687390SMatthew.Ahrens@Sun.COM 27697390SMatthew.Ahrens@Sun.COM if (dsl_dir_is_clone(snap->ds->ds_dir)) { 27707390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, 27717390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_origin_obj, 27727390SMatthew.Ahrens@Sun.COM 0, FTAG, &pa.origin_origin); 27737390SMatthew.Ahrens@Sun.COM if (err != 0) 27746689Smaybee goto out; 27756689Smaybee } 27767390SMatthew.Ahrens@Sun.COM 27777390SMatthew.Ahrens@Sun.COM out: 27786689Smaybee rw_exit(&dp->dp_config_rwlock); 27796689Smaybee 27806689Smaybee /* 27812082Seschrock * Add in 128x the snapnames zapobj size, since we will be moving 27822082Seschrock * a bunch of snapnames to the promoted ds, and dirtying their 27832082Seschrock * bonus buffers. 27842082Seschrock */ 27857390SMatthew.Ahrens@Sun.COM if (err == 0) { 27867390SMatthew.Ahrens@Sun.COM err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 27877390SMatthew.Ahrens@Sun.COM dsl_dataset_promote_sync, ds, &pa, 27887390SMatthew.Ahrens@Sun.COM 2 + 2 * doi.doi_physical_blks); 278910588SEric.Taylor@Sun.COM if (err && pa.err_ds && conflsnap) 279010588SEric.Taylor@Sun.COM (void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN); 27916689Smaybee } 27927390SMatthew.Ahrens@Sun.COM 27937390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.shared_snaps, B_TRUE); 27947390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.clone_snaps, B_FALSE); 27957390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.origin_snaps, B_FALSE); 27967390SMatthew.Ahrens@Sun.COM if (pa.origin_origin) 27977390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(pa.origin_origin, FTAG); 27986689Smaybee dsl_dataset_rele(ds, FTAG); 27992082Seschrock return (err); 28002082Seschrock } 28013912Slling 28025367Sahrens struct cloneswaparg { 28035367Sahrens dsl_dataset_t *cds; /* clone dataset */ 28045367Sahrens dsl_dataset_t *ohds; /* origin's head dataset */ 28055367Sahrens boolean_t force; 28065481Sck153898 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 28075367Sahrens }; 28085326Sek110237 28095326Sek110237 /* ARGSUSED */ 28105326Sek110237 static int 28115326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 28125326Sek110237 { 28135367Sahrens struct cloneswaparg *csa = arg1; 28145326Sek110237 28155367Sahrens /* they should both be heads */ 28165367Sahrens if (dsl_dataset_is_snapshot(csa->cds) || 28175367Sahrens dsl_dataset_is_snapshot(csa->ohds)) 28185326Sek110237 return (EINVAL); 28195326Sek110237 28205367Sahrens /* the branch point should be just before them */ 28215367Sahrens if (csa->cds->ds_prev != csa->ohds->ds_prev) 28225326Sek110237 return (EINVAL); 28235326Sek110237 282410272SMatthew.Ahrens@Sun.COM /* cds should be the clone (unless they are unrelated) */ 282510272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev != NULL && 282610272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap && 282710272SMatthew.Ahrens@Sun.COM csa->ohds->ds_object != 282810272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev->ds_phys->ds_next_snap_obj) 28295367Sahrens return (EINVAL); 28305326Sek110237 28315367Sahrens /* the clone should be a child of the origin */ 28325367Sahrens if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 28335367Sahrens return (EINVAL); 28345326Sek110237 28355367Sahrens /* ohds shouldn't be modified unless 'force' */ 28365367Sahrens if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 28375367Sahrens return (ETXTBSY); 28385481Sck153898 28395481Sck153898 /* adjust amount of any unconsumed refreservation */ 28405481Sck153898 csa->unused_refres_delta = 28415481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28425481Sck153898 csa->ohds->ds_phys->ds_unique_bytes) - 28435481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28445481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 28455481Sck153898 28465481Sck153898 if (csa->unused_refres_delta > 0 && 28475481Sck153898 csa->unused_refres_delta > 28485481Sck153898 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 28495481Sck153898 return (ENOSPC); 28505481Sck153898 285110799SChris.Kirby@sun.com if (csa->ohds->ds_quota != 0 && 285210799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota) 285310799SChris.Kirby@sun.com return (EDQUOT); 285410799SChris.Kirby@sun.com 28555367Sahrens return (0); 28565326Sek110237 } 28575326Sek110237 28585326Sek110237 /* ARGSUSED */ 28595326Sek110237 static void 28605326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 28615326Sek110237 { 28625367Sahrens struct cloneswaparg *csa = arg1; 28635367Sahrens dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 28645326Sek110237 28655481Sck153898 ASSERT(csa->cds->ds_reserved == 0); 286610799SChris.Kirby@sun.com ASSERT(csa->ohds->ds_quota == 0 || 286710799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota); 28685481Sck153898 28695367Sahrens dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 28705367Sahrens dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 28715326Sek110237 287210298SMatthew.Ahrens@Sun.COM if (csa->cds->ds_objset != NULL) { 287310298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->cds->ds_objset); 287410298SMatthew.Ahrens@Sun.COM csa->cds->ds_objset = NULL; 28755367Sahrens } 28765326Sek110237 287710298SMatthew.Ahrens@Sun.COM if (csa->ohds->ds_objset != NULL) { 287810298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->ohds->ds_objset); 287910298SMatthew.Ahrens@Sun.COM csa->ohds->ds_objset = NULL; 28805367Sahrens } 28815326Sek110237 288210272SMatthew.Ahrens@Sun.COM /* 288310272SMatthew.Ahrens@Sun.COM * Reset origin's unique bytes, if it exists. 288410272SMatthew.Ahrens@Sun.COM */ 288510272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev) { 288610272SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin = csa->cds->ds_prev; 288710272SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(origin->ds_dbuf, tx); 288810272SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 288910272SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 289010272SMatthew.Ahrens@Sun.COM &origin->ds_phys->ds_unique_bytes)); 289110272SMatthew.Ahrens@Sun.COM } 28925326Sek110237 28935326Sek110237 /* swap blkptrs */ 28945326Sek110237 { 28955326Sek110237 blkptr_t tmp; 28965367Sahrens tmp = csa->ohds->ds_phys->ds_bp; 28975367Sahrens csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 28985367Sahrens csa->cds->ds_phys->ds_bp = tmp; 28995326Sek110237 } 29005326Sek110237 29015326Sek110237 /* set dd_*_bytes */ 29025326Sek110237 { 29035326Sek110237 int64_t dused, dcomp, duncomp; 29045326Sek110237 uint64_t cdl_used, cdl_comp, cdl_uncomp; 29055326Sek110237 uint64_t odl_used, odl_comp, odl_uncomp; 29065326Sek110237 29077390SMatthew.Ahrens@Sun.COM ASSERT3U(csa->cds->ds_dir->dd_phys-> 29087390SMatthew.Ahrens@Sun.COM dd_used_breakdown[DD_USED_SNAP], ==, 0); 29097390SMatthew.Ahrens@Sun.COM 29105367Sahrens VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 29115326Sek110237 &cdl_comp, &cdl_uncomp)); 29125367Sahrens VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 29135326Sek110237 &odl_comp, &odl_uncomp)); 29147390SMatthew.Ahrens@Sun.COM 29155367Sahrens dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 29165367Sahrens (csa->ohds->ds_phys->ds_used_bytes + odl_used); 29175367Sahrens dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 29185367Sahrens (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 29195367Sahrens duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 29205367Sahrens cdl_uncomp - 29215367Sahrens (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 29225326Sek110237 29237390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD, 29245367Sahrens dused, dcomp, duncomp, tx); 29257390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD, 29265367Sahrens -dused, -dcomp, -duncomp, tx); 29277390SMatthew.Ahrens@Sun.COM 29287390SMatthew.Ahrens@Sun.COM /* 29297390SMatthew.Ahrens@Sun.COM * The difference in the space used by snapshots is the 29307390SMatthew.Ahrens@Sun.COM * difference in snapshot space due to the head's 29317390SMatthew.Ahrens@Sun.COM * deadlist (since that's the only thing that's 29327390SMatthew.Ahrens@Sun.COM * changing that affects the snapused). 29337390SMatthew.Ahrens@Sun.COM */ 29347390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 29357390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used)); 29367390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist, 29377390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used)); 29387390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used, 29397390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 29405367Sahrens } 29415367Sahrens 29425367Sahrens #define SWITCH64(x, y) \ 29435367Sahrens { \ 29445367Sahrens uint64_t __tmp = (x); \ 29455367Sahrens (x) = (y); \ 29465367Sahrens (y) = __tmp; \ 29475326Sek110237 } 29485326Sek110237 29495326Sek110237 /* swap ds_*_bytes */ 29505367Sahrens SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 29515367Sahrens csa->cds->ds_phys->ds_used_bytes); 29525367Sahrens SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 29535367Sahrens csa->cds->ds_phys->ds_compressed_bytes); 29545367Sahrens SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 29555367Sahrens csa->cds->ds_phys->ds_uncompressed_bytes); 29565481Sck153898 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 29575481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 29585481Sck153898 29595481Sck153898 /* apply any parent delta for change in unconsumed refreservation */ 29607390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV, 29617390SMatthew.Ahrens@Sun.COM csa->unused_refres_delta, 0, 0, tx); 29625326Sek110237 29635326Sek110237 /* swap deadlists */ 29645367Sahrens bplist_close(&csa->cds->ds_deadlist); 29655367Sahrens bplist_close(&csa->ohds->ds_deadlist); 29665367Sahrens SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 29675367Sahrens csa->cds->ds_phys->ds_deadlist_obj); 29685367Sahrens VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 29695367Sahrens csa->cds->ds_phys->ds_deadlist_obj)); 29705367Sahrens VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 29715367Sahrens csa->ohds->ds_phys->ds_deadlist_obj)); 29727837SMatthew.Ahrens@Sun.COM 29737837SMatthew.Ahrens@Sun.COM dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx); 29745326Sek110237 } 29755326Sek110237 29765326Sek110237 /* 297710272SMatthew.Ahrens@Sun.COM * Swap 'clone' with its origin head datasets. Used at the end of "zfs 297810272SMatthew.Ahrens@Sun.COM * recv" into an existing fs to swizzle the file system to the new 297910272SMatthew.Ahrens@Sun.COM * version, and by "zfs rollback". Can also be used to swap two 298010272SMatthew.Ahrens@Sun.COM * independent head datasets if neither has any snapshots. 29815326Sek110237 */ 29825326Sek110237 int 29835367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 29845367Sahrens boolean_t force) 29855326Sek110237 { 29865367Sahrens struct cloneswaparg csa; 29876689Smaybee int error; 29886689Smaybee 29896689Smaybee ASSERT(clone->ds_owner); 29906689Smaybee ASSERT(origin_head->ds_owner); 29916689Smaybee retry: 29926689Smaybee /* Need exclusive access for the swap */ 29936689Smaybee rw_enter(&clone->ds_rwlock, RW_WRITER); 29946689Smaybee if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 29956689Smaybee rw_exit(&clone->ds_rwlock); 29966689Smaybee rw_enter(&origin_head->ds_rwlock, RW_WRITER); 29976689Smaybee if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 29986689Smaybee rw_exit(&origin_head->ds_rwlock); 29996689Smaybee goto retry; 30006689Smaybee } 30016689Smaybee } 30025367Sahrens csa.cds = clone; 30035367Sahrens csa.ohds = origin_head; 30045367Sahrens csa.force = force; 30056689Smaybee error = dsl_sync_task_do(clone->ds_dir->dd_pool, 30065326Sek110237 dsl_dataset_clone_swap_check, 30076689Smaybee dsl_dataset_clone_swap_sync, &csa, NULL, 9); 30086689Smaybee return (error); 30095326Sek110237 } 30105326Sek110237 30113912Slling /* 30123912Slling * Given a pool name and a dataset object number in that pool, 30133912Slling * return the name of that dataset. 30143912Slling */ 30153912Slling int 30163912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 30173912Slling { 30183912Slling spa_t *spa; 30193912Slling dsl_pool_t *dp; 30206689Smaybee dsl_dataset_t *ds; 30213912Slling int error; 30223912Slling 30233912Slling if ((error = spa_open(pname, &spa, FTAG)) != 0) 30243912Slling return (error); 30253912Slling dp = spa_get_dsl(spa); 30263912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 30276689Smaybee if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 30286689Smaybee dsl_dataset_name(ds, buf); 30296689Smaybee dsl_dataset_rele(ds, FTAG); 30303912Slling } 30313912Slling rw_exit(&dp->dp_config_rwlock); 30323912Slling spa_close(spa, FTAG); 30333912Slling 30346689Smaybee return (error); 30353912Slling } 30365378Sck153898 30375378Sck153898 int 30385378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 30396689Smaybee uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 30405378Sck153898 { 30415378Sck153898 int error = 0; 30425378Sck153898 30435378Sck153898 ASSERT3S(asize, >, 0); 30445378Sck153898 30455831Sck153898 /* 30465831Sck153898 * *ref_rsrv is the portion of asize that will come from any 30475831Sck153898 * unconsumed refreservation space. 30485831Sck153898 */ 30495831Sck153898 *ref_rsrv = 0; 30505831Sck153898 30515378Sck153898 mutex_enter(&ds->ds_lock); 30525378Sck153898 /* 30535378Sck153898 * Make a space adjustment for reserved bytes. 30545378Sck153898 */ 30555378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 30565378Sck153898 ASSERT3U(*used, >=, 30575378Sck153898 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30585378Sck153898 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30595831Sck153898 *ref_rsrv = 30605831Sck153898 asize - MIN(asize, parent_delta(ds, asize + inflight)); 30615378Sck153898 } 30625378Sck153898 30635378Sck153898 if (!check_quota || ds->ds_quota == 0) { 30645378Sck153898 mutex_exit(&ds->ds_lock); 30655378Sck153898 return (0); 30665378Sck153898 } 30675378Sck153898 /* 30685378Sck153898 * If they are requesting more space, and our current estimate 30695378Sck153898 * is over quota, they get to try again unless the actual 30705378Sck153898 * on-disk is over quota and there are no pending changes (which 30715378Sck153898 * may free up space for us). 30725378Sck153898 */ 30735378Sck153898 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 30745378Sck153898 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 30755378Sck153898 error = ERESTART; 30765378Sck153898 else 30775378Sck153898 error = EDQUOT; 30785378Sck153898 } 30795378Sck153898 mutex_exit(&ds->ds_lock); 30805378Sck153898 30815378Sck153898 return (error); 30825378Sck153898 } 30835378Sck153898 30845378Sck153898 /* ARGSUSED */ 30855378Sck153898 static int 30865378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 30875378Sck153898 { 30885378Sck153898 dsl_dataset_t *ds = arg1; 30895378Sck153898 uint64_t *quotap = arg2; 30905378Sck153898 uint64_t new_quota = *quotap; 30915378Sck153898 30925378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 30935378Sck153898 return (ENOTSUP); 30945378Sck153898 30955378Sck153898 if (new_quota == 0) 30965378Sck153898 return (0); 30975378Sck153898 30985378Sck153898 if (new_quota < ds->ds_phys->ds_used_bytes || 30995378Sck153898 new_quota < ds->ds_reserved) 31005378Sck153898 return (ENOSPC); 31015378Sck153898 31025378Sck153898 return (0); 31035378Sck153898 } 31045378Sck153898 31055378Sck153898 /* ARGSUSED */ 31065378Sck153898 void 31075378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 31085378Sck153898 { 31095378Sck153898 dsl_dataset_t *ds = arg1; 31105378Sck153898 uint64_t *quotap = arg2; 31115378Sck153898 uint64_t new_quota = *quotap; 31125378Sck153898 31135378Sck153898 dmu_buf_will_dirty(ds->ds_dbuf, tx); 31145378Sck153898 31155378Sck153898 ds->ds_quota = new_quota; 31165378Sck153898 311710242Schris.kirby@sun.com dsl_dir_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx); 31185378Sck153898 31195378Sck153898 spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa, 31205378Sck153898 tx, cr, "%lld dataset = %llu ", 31216689Smaybee (longlong_t)new_quota, ds->ds_object); 31225378Sck153898 } 31235378Sck153898 31245378Sck153898 int 31255378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota) 31265378Sck153898 { 31275378Sck153898 dsl_dataset_t *ds; 31285378Sck153898 int err; 31295378Sck153898 31306689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 31315378Sck153898 if (err) 31325378Sck153898 return (err); 31335378Sck153898 31345481Sck153898 if (quota != ds->ds_quota) { 31355481Sck153898 /* 31365481Sck153898 * If someone removes a file, then tries to set the quota, we 31375481Sck153898 * want to make sure the file freeing takes effect. 31385481Sck153898 */ 31395481Sck153898 txg_wait_open(ds->ds_dir->dd_pool, 0); 31405481Sck153898 31415481Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 31425481Sck153898 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 31435481Sck153898 ds, "a, 0); 31445481Sck153898 } 31456689Smaybee dsl_dataset_rele(ds, FTAG); 31465378Sck153898 return (err); 31475378Sck153898 } 31485378Sck153898 31495378Sck153898 static int 31505378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 31515378Sck153898 { 31525378Sck153898 dsl_dataset_t *ds = arg1; 31535378Sck153898 uint64_t *reservationp = arg2; 31545378Sck153898 uint64_t new_reservation = *reservationp; 31555378Sck153898 uint64_t unique; 31565378Sck153898 31575378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 31585378Sck153898 SPA_VERSION_REFRESERVATION) 31595378Sck153898 return (ENOTSUP); 31605378Sck153898 31615378Sck153898 if (dsl_dataset_is_snapshot(ds)) 31625378Sck153898 return (EINVAL); 31635378Sck153898 31645378Sck153898 /* 31655378Sck153898 * If we are doing the preliminary check in open context, the 31665378Sck153898 * space estimates may be inaccurate. 31675378Sck153898 */ 31685378Sck153898 if (!dmu_tx_is_syncing(tx)) 31695378Sck153898 return (0); 31705378Sck153898 31715378Sck153898 mutex_enter(&ds->ds_lock); 31725378Sck153898 unique = dsl_dataset_unique(ds); 31735378Sck153898 mutex_exit(&ds->ds_lock); 31745378Sck153898 31758525SEric.Schrock@Sun.COM if (MAX(unique, new_reservation) > MAX(unique, ds->ds_reserved)) { 31768525SEric.Schrock@Sun.COM uint64_t delta = MAX(unique, new_reservation) - 31778525SEric.Schrock@Sun.COM MAX(unique, ds->ds_reserved); 31788525SEric.Schrock@Sun.COM 31798525SEric.Schrock@Sun.COM if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 31808525SEric.Schrock@Sun.COM return (ENOSPC); 31818525SEric.Schrock@Sun.COM if (ds->ds_quota > 0 && 31828525SEric.Schrock@Sun.COM new_reservation > ds->ds_quota) 31838525SEric.Schrock@Sun.COM return (ENOSPC); 31848525SEric.Schrock@Sun.COM } 31855378Sck153898 31865378Sck153898 return (0); 31875378Sck153898 } 31885378Sck153898 31895378Sck153898 /* ARGSUSED */ 31905378Sck153898 static void 31915378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 31925378Sck153898 dmu_tx_t *tx) 31935378Sck153898 { 31945378Sck153898 dsl_dataset_t *ds = arg1; 31955378Sck153898 uint64_t *reservationp = arg2; 31965378Sck153898 uint64_t new_reservation = *reservationp; 31977595SMatthew.Ahrens@Sun.COM uint64_t unique; 31987595SMatthew.Ahrens@Sun.COM int64_t delta; 31997595SMatthew.Ahrens@Sun.COM 32007595SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx); 32017595SMatthew.Ahrens@Sun.COM 32027595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 32037595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_lock); 32047595SMatthew.Ahrens@Sun.COM unique = dsl_dataset_unique(ds); 32057595SMatthew.Ahrens@Sun.COM delta = MAX(0, (int64_t)(new_reservation - unique)) - 32067595SMatthew.Ahrens@Sun.COM MAX(0, (int64_t)(ds->ds_reserved - unique)); 32077595SMatthew.Ahrens@Sun.COM ds->ds_reserved = new_reservation; 32087595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_lock); 32097595SMatthew.Ahrens@Sun.COM 32107595SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 32117595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 321210242Schris.kirby@sun.com dsl_dir_prop_set_uint64_sync(ds->ds_dir, "refreservation", 32137595SMatthew.Ahrens@Sun.COM new_reservation, cr, tx); 32147595SMatthew.Ahrens@Sun.COM 32157595SMatthew.Ahrens@Sun.COM spa_history_internal_log(LOG_DS_REFRESERV, 32167595SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 32177595SMatthew.Ahrens@Sun.COM (longlong_t)new_reservation, ds->ds_object); 32185378Sck153898 } 32195378Sck153898 32205378Sck153898 int 32215378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation) 32225378Sck153898 { 32235378Sck153898 dsl_dataset_t *ds; 32245378Sck153898 int err; 32255378Sck153898 32266689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 32275378Sck153898 if (err) 32285378Sck153898 return (err); 32295378Sck153898 32305378Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 32315378Sck153898 dsl_dataset_set_reservation_check, 32325378Sck153898 dsl_dataset_set_reservation_sync, ds, &reservation, 0); 32336689Smaybee dsl_dataset_rele(ds, FTAG); 32345378Sck153898 return (err); 32355378Sck153898 } 323610242Schris.kirby@sun.com 323710342Schris.kirby@sun.com struct dsl_ds_holdarg { 323810342Schris.kirby@sun.com dsl_sync_task_group_t *dstg; 323910342Schris.kirby@sun.com char *htag; 324010342Schris.kirby@sun.com char *snapname; 324110342Schris.kirby@sun.com boolean_t recursive; 324210342Schris.kirby@sun.com boolean_t gotone; 324310342Schris.kirby@sun.com boolean_t temphold; 324410342Schris.kirby@sun.com char failed[MAXPATHLEN]; 324510342Schris.kirby@sun.com }; 324610342Schris.kirby@sun.com 324710242Schris.kirby@sun.com static int 324810242Schris.kirby@sun.com dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx) 324910242Schris.kirby@sun.com { 325010242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 325110342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 325210342Schris.kirby@sun.com char *htag = ha->htag; 325310242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 325410242Schris.kirby@sun.com int error = 0; 325510242Schris.kirby@sun.com 325610242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 325710242Schris.kirby@sun.com return (ENOTSUP); 325810242Schris.kirby@sun.com 325910242Schris.kirby@sun.com if (!dsl_dataset_is_snapshot(ds)) 326010242Schris.kirby@sun.com return (EINVAL); 326110242Schris.kirby@sun.com 326210242Schris.kirby@sun.com /* tags must be unique */ 326310242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 326410242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj) { 326510242Schris.kirby@sun.com error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag, 326610242Schris.kirby@sun.com 8, 1, tx); 326710242Schris.kirby@sun.com if (error == 0) 326810242Schris.kirby@sun.com error = EEXIST; 326910242Schris.kirby@sun.com else if (error == ENOENT) 327010242Schris.kirby@sun.com error = 0; 327110242Schris.kirby@sun.com } 327210242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 327310242Schris.kirby@sun.com 327410242Schris.kirby@sun.com return (error); 327510242Schris.kirby@sun.com } 327610242Schris.kirby@sun.com 327710242Schris.kirby@sun.com static void 327810242Schris.kirby@sun.com dsl_dataset_user_hold_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 327910242Schris.kirby@sun.com { 328010242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 328110342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 328210342Schris.kirby@sun.com char *htag = ha->htag; 328310342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 328410342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 328510242Schris.kirby@sun.com time_t now = gethrestime_sec(); 328610242Schris.kirby@sun.com uint64_t zapobj; 328710242Schris.kirby@sun.com 328810242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 328910242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj == 0) { 329010242Schris.kirby@sun.com /* 329110242Schris.kirby@sun.com * This is the first user hold for this dataset. Create 329210242Schris.kirby@sun.com * the userrefs zap object. 329310242Schris.kirby@sun.com */ 329410242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 329510242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj = 329610242Schris.kirby@sun.com zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx); 329710242Schris.kirby@sun.com } else { 329810242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 329910242Schris.kirby@sun.com } 330010242Schris.kirby@sun.com ds->ds_userrefs++; 330110242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 330210242Schris.kirby@sun.com 330310242Schris.kirby@sun.com VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx)); 330410242Schris.kirby@sun.com 330510342Schris.kirby@sun.com if (ha->temphold) { 330610342Schris.kirby@sun.com VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object, 330710342Schris.kirby@sun.com htag, &now, tx)); 330810342Schris.kirby@sun.com } 330910342Schris.kirby@sun.com 331010242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_HOLD, 331110342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> temp = %d dataset = %llu", htag, 331210342Schris.kirby@sun.com (int)ha->temphold, ds->ds_object); 331310242Schris.kirby@sun.com } 331410242Schris.kirby@sun.com 331510242Schris.kirby@sun.com static int 331610242Schris.kirby@sun.com dsl_dataset_user_hold_one(char *dsname, void *arg) 331710242Schris.kirby@sun.com { 331810242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 331910242Schris.kirby@sun.com dsl_dataset_t *ds; 332010242Schris.kirby@sun.com int error; 332110242Schris.kirby@sun.com char *name; 332210242Schris.kirby@sun.com 332310242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname plus terminating NULL */ 332410272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 332510242Schris.kirby@sun.com error = dsl_dataset_hold(name, ha->dstg, &ds); 332610272SMatthew.Ahrens@Sun.COM strfree(name); 332710242Schris.kirby@sun.com if (error == 0) { 332810268Schris.kirby@sun.com ha->gotone = B_TRUE; 332910242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check, 333010342Schris.kirby@sun.com dsl_dataset_user_hold_sync, ds, ha, 0); 333110242Schris.kirby@sun.com } else if (error == ENOENT && ha->recursive) { 333210242Schris.kirby@sun.com error = 0; 333310242Schris.kirby@sun.com } else { 333410242Schris.kirby@sun.com (void) strcpy(ha->failed, dsname); 333510242Schris.kirby@sun.com } 333610242Schris.kirby@sun.com return (error); 333710242Schris.kirby@sun.com } 333810242Schris.kirby@sun.com 333910242Schris.kirby@sun.com int 334010242Schris.kirby@sun.com dsl_dataset_user_hold(char *dsname, char *snapname, char *htag, 334110342Schris.kirby@sun.com boolean_t recursive, boolean_t temphold) 334210242Schris.kirby@sun.com { 334310242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 334410242Schris.kirby@sun.com dsl_sync_task_t *dst; 334510242Schris.kirby@sun.com spa_t *spa; 334610242Schris.kirby@sun.com int error; 334710242Schris.kirby@sun.com 334810242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 334910242Schris.kirby@sun.com 335010242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 335110242Schris.kirby@sun.com 335210242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 335310242Schris.kirby@sun.com if (error) { 335410242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 335510242Schris.kirby@sun.com return (error); 335610242Schris.kirby@sun.com } 335710242Schris.kirby@sun.com 335810242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 335910242Schris.kirby@sun.com ha->htag = htag; 336010242Schris.kirby@sun.com ha->snapname = snapname; 336110242Schris.kirby@sun.com ha->recursive = recursive; 336210342Schris.kirby@sun.com ha->temphold = temphold; 336310242Schris.kirby@sun.com if (recursive) { 336410242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_hold_one, 336510242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 336610242Schris.kirby@sun.com } else { 336710242Schris.kirby@sun.com error = dsl_dataset_user_hold_one(dsname, ha); 336810242Schris.kirby@sun.com } 336910242Schris.kirby@sun.com if (error == 0) 337010242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 337110242Schris.kirby@sun.com 337210242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 337310242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 337410242Schris.kirby@sun.com dsl_dataset_t *ds = dst->dst_arg1; 337510242Schris.kirby@sun.com 337610242Schris.kirby@sun.com if (dst->dst_err) { 337710242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 337810242Schris.kirby@sun.com *strchr(ha->failed, '@') = '\0'; 337910242Schris.kirby@sun.com } 338010242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 338110242Schris.kirby@sun.com } 338210242Schris.kirby@sun.com 338310268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 338410268Schris.kirby@sun.com error = ENOENT; 338510268Schris.kirby@sun.com 338610242Schris.kirby@sun.com if (error) 338710242Schris.kirby@sun.com (void) strcpy(dsname, ha->failed); 338810242Schris.kirby@sun.com 338910242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 339010242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 339110242Schris.kirby@sun.com spa_close(spa, FTAG); 339210242Schris.kirby@sun.com return (error); 339310242Schris.kirby@sun.com } 339410242Schris.kirby@sun.com 339510242Schris.kirby@sun.com struct dsl_ds_releasearg { 339610242Schris.kirby@sun.com dsl_dataset_t *ds; 339710242Schris.kirby@sun.com const char *htag; 339810242Schris.kirby@sun.com boolean_t own; /* do we own or just hold ds? */ 339910242Schris.kirby@sun.com }; 340010242Schris.kirby@sun.com 340110242Schris.kirby@sun.com static int 340210242Schris.kirby@sun.com dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag, 340310242Schris.kirby@sun.com boolean_t *might_destroy) 340410242Schris.kirby@sun.com { 340510242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 340610242Schris.kirby@sun.com uint64_t zapobj; 340710242Schris.kirby@sun.com uint64_t tmp; 340810242Schris.kirby@sun.com int error; 340910242Schris.kirby@sun.com 341010242Schris.kirby@sun.com *might_destroy = B_FALSE; 341110242Schris.kirby@sun.com 341210242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 341310242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 341410242Schris.kirby@sun.com if (zapobj == 0) { 341510242Schris.kirby@sun.com /* The tag can't possibly exist */ 341610242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 341710242Schris.kirby@sun.com return (ESRCH); 341810242Schris.kirby@sun.com } 341910242Schris.kirby@sun.com 342010242Schris.kirby@sun.com /* Make sure the tag exists */ 342110242Schris.kirby@sun.com error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp); 342210242Schris.kirby@sun.com if (error) { 342310242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 342410242Schris.kirby@sun.com if (error == ENOENT) 342510242Schris.kirby@sun.com error = ESRCH; 342610242Schris.kirby@sun.com return (error); 342710242Schris.kirby@sun.com } 342810242Schris.kirby@sun.com 342910242Schris.kirby@sun.com if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 && 343010242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 343110242Schris.kirby@sun.com *might_destroy = B_TRUE; 343210242Schris.kirby@sun.com 343310242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 343410242Schris.kirby@sun.com return (0); 343510242Schris.kirby@sun.com } 343610242Schris.kirby@sun.com 343710242Schris.kirby@sun.com static int 343810242Schris.kirby@sun.com dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx) 343910242Schris.kirby@sun.com { 344010242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 344110242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 344210242Schris.kirby@sun.com boolean_t might_destroy; 344310242Schris.kirby@sun.com int error; 344410242Schris.kirby@sun.com 344510242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 344610242Schris.kirby@sun.com return (ENOTSUP); 344710242Schris.kirby@sun.com 344810242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy); 344910242Schris.kirby@sun.com if (error) 345010242Schris.kirby@sun.com return (error); 345110242Schris.kirby@sun.com 345210242Schris.kirby@sun.com if (might_destroy) { 345310242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 345410242Schris.kirby@sun.com 345510242Schris.kirby@sun.com if (dmu_tx_is_syncing(tx)) { 345610242Schris.kirby@sun.com /* 345710242Schris.kirby@sun.com * If we're not prepared to remove the snapshot, 345810242Schris.kirby@sun.com * we can't allow the release to happen right now. 345910242Schris.kirby@sun.com */ 346010242Schris.kirby@sun.com if (!ra->own) 346110242Schris.kirby@sun.com return (EBUSY); 346210298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 346310298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 346410298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 346510242Schris.kirby@sun.com } 346610242Schris.kirby@sun.com } 346710242Schris.kirby@sun.com dsda.ds = ds; 346810242Schris.kirby@sun.com dsda.releasing = B_TRUE; 346910242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&dsda, tag, tx)); 347010242Schris.kirby@sun.com } 347110242Schris.kirby@sun.com 347210242Schris.kirby@sun.com return (0); 347310242Schris.kirby@sun.com } 347410242Schris.kirby@sun.com 347510242Schris.kirby@sun.com static void 347610242Schris.kirby@sun.com dsl_dataset_user_release_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 347710242Schris.kirby@sun.com { 347810242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 347910242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 348010342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 348110342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 348210242Schris.kirby@sun.com uint64_t zapobj; 348310242Schris.kirby@sun.com uint64_t dsobj = ds->ds_object; 348410242Schris.kirby@sun.com uint64_t refs; 348510342Schris.kirby@sun.com int error; 348610242Schris.kirby@sun.com 348710242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 348810242Schris.kirby@sun.com ds->ds_userrefs--; 348910242Schris.kirby@sun.com refs = ds->ds_userrefs; 349010242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 349110342Schris.kirby@sun.com error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx); 349210342Schris.kirby@sun.com VERIFY(error == 0 || error == ENOENT); 349310242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 349410242Schris.kirby@sun.com VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx)); 349510242Schris.kirby@sun.com if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && 349610242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) { 349710242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 349810242Schris.kirby@sun.com 349910242Schris.kirby@sun.com ASSERT(ra->own); 350010242Schris.kirby@sun.com dsda.ds = ds; 350110242Schris.kirby@sun.com dsda.releasing = B_TRUE; 350210242Schris.kirby@sun.com /* We already did the destroy_check */ 350310242Schris.kirby@sun.com dsl_dataset_destroy_sync(&dsda, tag, cr, tx); 350410242Schris.kirby@sun.com } 350510242Schris.kirby@sun.com 350610242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_RELEASE, 350710342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> %lld dataset = %llu", 350810242Schris.kirby@sun.com ra->htag, (longlong_t)refs, dsobj); 350910242Schris.kirby@sun.com } 351010242Schris.kirby@sun.com 351110242Schris.kirby@sun.com static int 351210242Schris.kirby@sun.com dsl_dataset_user_release_one(char *dsname, void *arg) 351310242Schris.kirby@sun.com { 351410242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 351510242Schris.kirby@sun.com struct dsl_ds_releasearg *ra; 351610242Schris.kirby@sun.com dsl_dataset_t *ds; 351710242Schris.kirby@sun.com int error; 351810242Schris.kirby@sun.com void *dtag = ha->dstg; 351910242Schris.kirby@sun.com char *name; 352010242Schris.kirby@sun.com boolean_t own = B_FALSE; 352110242Schris.kirby@sun.com boolean_t might_destroy; 352210242Schris.kirby@sun.com 352310242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */ 352410272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 352510242Schris.kirby@sun.com error = dsl_dataset_hold(name, dtag, &ds); 352610272SMatthew.Ahrens@Sun.COM strfree(name); 352710242Schris.kirby@sun.com if (error == ENOENT && ha->recursive) 352810242Schris.kirby@sun.com return (0); 352910242Schris.kirby@sun.com (void) strcpy(ha->failed, dsname); 353010242Schris.kirby@sun.com if (error) 353110242Schris.kirby@sun.com return (error); 353210242Schris.kirby@sun.com 353310268Schris.kirby@sun.com ha->gotone = B_TRUE; 353410268Schris.kirby@sun.com 353510242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 353610242Schris.kirby@sun.com 353710242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy); 353810242Schris.kirby@sun.com if (error) { 353910242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 354010242Schris.kirby@sun.com return (error); 354110242Schris.kirby@sun.com } 354210242Schris.kirby@sun.com 354310242Schris.kirby@sun.com if (might_destroy) { 354410242Schris.kirby@sun.com #ifdef _KERNEL 354510242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 354610242Schris.kirby@sun.com if (error) { 354710242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 354810242Schris.kirby@sun.com return (error); 354910242Schris.kirby@sun.com } 355010242Schris.kirby@sun.com #endif 355110298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) { 355210242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 355310242Schris.kirby@sun.com return (EBUSY); 355410242Schris.kirby@sun.com } else { 355510242Schris.kirby@sun.com own = B_TRUE; 355610242Schris.kirby@sun.com dsl_dataset_make_exclusive(ds, dtag); 355710242Schris.kirby@sun.com } 355810242Schris.kirby@sun.com } 355910242Schris.kirby@sun.com 356010242Schris.kirby@sun.com ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP); 356110242Schris.kirby@sun.com ra->ds = ds; 356210242Schris.kirby@sun.com ra->htag = ha->htag; 356310242Schris.kirby@sun.com ra->own = own; 356410242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check, 356510242Schris.kirby@sun.com dsl_dataset_user_release_sync, ra, dtag, 0); 356610242Schris.kirby@sun.com 356710242Schris.kirby@sun.com return (0); 356810242Schris.kirby@sun.com } 356910242Schris.kirby@sun.com 357010242Schris.kirby@sun.com int 357110242Schris.kirby@sun.com dsl_dataset_user_release(char *dsname, char *snapname, char *htag, 357210242Schris.kirby@sun.com boolean_t recursive) 357310242Schris.kirby@sun.com { 357410242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 357510242Schris.kirby@sun.com dsl_sync_task_t *dst; 357610242Schris.kirby@sun.com spa_t *spa; 357710242Schris.kirby@sun.com int error; 357810242Schris.kirby@sun.com 357910242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 358010242Schris.kirby@sun.com 358110242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 358210242Schris.kirby@sun.com 358310242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 358410242Schris.kirby@sun.com if (error) { 358510242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 358610242Schris.kirby@sun.com return (error); 358710242Schris.kirby@sun.com } 358810242Schris.kirby@sun.com 358910242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 359010242Schris.kirby@sun.com ha->htag = htag; 359110242Schris.kirby@sun.com ha->snapname = snapname; 359210242Schris.kirby@sun.com ha->recursive = recursive; 359310242Schris.kirby@sun.com if (recursive) { 359410242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_release_one, 359510242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 359610242Schris.kirby@sun.com } else { 359710242Schris.kirby@sun.com error = dsl_dataset_user_release_one(dsname, ha); 359810242Schris.kirby@sun.com } 359910242Schris.kirby@sun.com if (error == 0) 360010242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 360110242Schris.kirby@sun.com 360210242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 360310242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 360410242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = dst->dst_arg1; 360510242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 360610242Schris.kirby@sun.com 360710242Schris.kirby@sun.com if (dst->dst_err) 360810242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 360910242Schris.kirby@sun.com 361010242Schris.kirby@sun.com if (ra->own) 361110242Schris.kirby@sun.com dsl_dataset_disown(ds, ha->dstg); 361210242Schris.kirby@sun.com else 361310242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 361410242Schris.kirby@sun.com 361510242Schris.kirby@sun.com kmem_free(ra, sizeof (struct dsl_ds_releasearg)); 361610242Schris.kirby@sun.com } 361710242Schris.kirby@sun.com 361810268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 361910268Schris.kirby@sun.com error = ENOENT; 362010268Schris.kirby@sun.com 362110242Schris.kirby@sun.com if (error) 362210242Schris.kirby@sun.com (void) strcpy(dsname, ha->failed); 362310242Schris.kirby@sun.com 362410242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 362510242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 362610242Schris.kirby@sun.com spa_close(spa, FTAG); 362710242Schris.kirby@sun.com return (error); 362810242Schris.kirby@sun.com } 362910242Schris.kirby@sun.com 363010342Schris.kirby@sun.com /* 363110342Schris.kirby@sun.com * Called at spa_load time to release a stale temporary user hold. 363210342Schris.kirby@sun.com */ 363310342Schris.kirby@sun.com int 363410342Schris.kirby@sun.com dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag) 363510342Schris.kirby@sun.com { 363610342Schris.kirby@sun.com dsl_dataset_t *ds; 363710342Schris.kirby@sun.com char *snap; 363810342Schris.kirby@sun.com char *name; 363910342Schris.kirby@sun.com int namelen; 364010342Schris.kirby@sun.com int error; 364110342Schris.kirby@sun.com 364210342Schris.kirby@sun.com rw_enter(&dp->dp_config_rwlock, RW_READER); 364310342Schris.kirby@sun.com error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 364410342Schris.kirby@sun.com rw_exit(&dp->dp_config_rwlock); 364510342Schris.kirby@sun.com if (error) 364610342Schris.kirby@sun.com return (error); 364710342Schris.kirby@sun.com namelen = dsl_dataset_namelen(ds)+1; 364810342Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 364910342Schris.kirby@sun.com dsl_dataset_name(ds, name); 365010342Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 365110342Schris.kirby@sun.com 365210342Schris.kirby@sun.com snap = strchr(name, '@'); 365310342Schris.kirby@sun.com *snap = '\0'; 365410342Schris.kirby@sun.com ++snap; 365510342Schris.kirby@sun.com return (dsl_dataset_user_release(name, snap, htag, B_FALSE)); 365610342Schris.kirby@sun.com } 365710342Schris.kirby@sun.com 365810242Schris.kirby@sun.com int 365910242Schris.kirby@sun.com dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp) 366010242Schris.kirby@sun.com { 366110242Schris.kirby@sun.com dsl_dataset_t *ds; 366210242Schris.kirby@sun.com int err; 366310242Schris.kirby@sun.com 366410242Schris.kirby@sun.com err = dsl_dataset_hold(dsname, FTAG, &ds); 366510242Schris.kirby@sun.com if (err) 366610242Schris.kirby@sun.com return (err); 366710242Schris.kirby@sun.com 366810242Schris.kirby@sun.com VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)); 366910242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) { 367010242Schris.kirby@sun.com zap_attribute_t *za; 367110242Schris.kirby@sun.com zap_cursor_t zc; 367210242Schris.kirby@sun.com 367310242Schris.kirby@sun.com za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 367410242Schris.kirby@sun.com for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset, 367510242Schris.kirby@sun.com ds->ds_phys->ds_userrefs_obj); 367610242Schris.kirby@sun.com zap_cursor_retrieve(&zc, za) == 0; 367710242Schris.kirby@sun.com zap_cursor_advance(&zc)) { 367810242Schris.kirby@sun.com VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name, 367910242Schris.kirby@sun.com za->za_first_integer)); 368010242Schris.kirby@sun.com } 368110242Schris.kirby@sun.com zap_cursor_fini(&zc); 368210242Schris.kirby@sun.com kmem_free(za, sizeof (zap_attribute_t)); 368310242Schris.kirby@sun.com } 368410242Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 368510242Schris.kirby@sun.com return (0); 368610242Schris.kirby@sun.com } 368710298SMatthew.Ahrens@Sun.COM 368810298SMatthew.Ahrens@Sun.COM /* 368910298SMatthew.Ahrens@Sun.COM * Note, this fuction is used as the callback for dmu_objset_find(). We 369010298SMatthew.Ahrens@Sun.COM * always return 0 so that we will continue to find and process 369110298SMatthew.Ahrens@Sun.COM * inconsistent datasets, even if we encounter an error trying to 369210298SMatthew.Ahrens@Sun.COM * process one of them. 369310298SMatthew.Ahrens@Sun.COM */ 369410298SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 369510298SMatthew.Ahrens@Sun.COM int 369610298SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent(char *dsname, void *arg) 369710298SMatthew.Ahrens@Sun.COM { 369810298SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 369910298SMatthew.Ahrens@Sun.COM 370010298SMatthew.Ahrens@Sun.COM if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) { 370110298SMatthew.Ahrens@Sun.COM if (DS_IS_INCONSISTENT(ds)) 370210298SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(ds, FTAG, B_FALSE); 370310298SMatthew.Ahrens@Sun.COM else 370410298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 370510298SMatthew.Ahrens@Sun.COM } 370610298SMatthew.Ahrens@Sun.COM return (0); 370710298SMatthew.Ahrens@Sun.COM } 3708