1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 225831Sck153898 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/dmu_objset.h> 29789Sahrens #include <sys/dsl_dataset.h> 30789Sahrens #include <sys/dsl_dir.h> 312082Seschrock #include <sys/dsl_prop.h> 322199Sahrens #include <sys/dsl_synctask.h> 33789Sahrens #include <sys/dmu_traverse.h> 34789Sahrens #include <sys/dmu_tx.h> 35789Sahrens #include <sys/arc.h> 36789Sahrens #include <sys/zio.h> 37789Sahrens #include <sys/zap.h> 38789Sahrens #include <sys/unique.h> 39789Sahrens #include <sys/zfs_context.h> 404007Smmusante #include <sys/zfs_ioctl.h> 414543Smarks #include <sys/spa.h> 424543Smarks #include <sys/sunddi.h> 43789Sahrens 44*6689Smaybee static char *dsl_reaper = "the grim reaper"; 45*6689Smaybee 462199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check; 472199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync; 482199Sahrens static dsl_checkfunc_t dsl_dataset_rollback_check; 492199Sahrens static dsl_syncfunc_t dsl_dataset_rollback_sync; 505378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync; 511731Sbonwick 523444Sek110237 #define DS_REF_MAX (1ULL << 62) 53789Sahrens 54789Sahrens #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 55789Sahrens 56*6689Smaybee #define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper) 57*6689Smaybee 58*6689Smaybee static void dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag); 59789Sahrens 605378Sck153898 /* 615378Sck153898 * Figure out how much of this delta should be propogated to the dsl_dir 625378Sck153898 * layer. If there's a refreservation, that space has already been 635378Sck153898 * partially accounted for in our ancestors. 645378Sck153898 */ 655378Sck153898 static int64_t 665378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta) 675378Sck153898 { 685378Sck153898 uint64_t old_bytes, new_bytes; 695378Sck153898 705378Sck153898 if (ds->ds_reserved == 0) 715378Sck153898 return (delta); 725378Sck153898 735378Sck153898 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 745378Sck153898 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 755378Sck153898 765378Sck153898 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 775378Sck153898 return (new_bytes - old_bytes); 785378Sck153898 } 79789Sahrens 80789Sahrens void 81789Sahrens dsl_dataset_block_born(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 82789Sahrens { 832082Seschrock int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 84789Sahrens int compressed = BP_GET_PSIZE(bp); 85789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 865378Sck153898 int64_t delta; 87789Sahrens 88789Sahrens dprintf_bp(bp, "born, ds=%p\n", ds); 89789Sahrens 90789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 91789Sahrens /* It could have been compressed away to nothing */ 92789Sahrens if (BP_IS_HOLE(bp)) 93789Sahrens return; 94789Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 95789Sahrens ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 96789Sahrens if (ds == NULL) { 97789Sahrens /* 98789Sahrens * Account for the meta-objset space in its placeholder 99789Sahrens * dsl_dir. 100789Sahrens */ 101789Sahrens ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 102789Sahrens dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 103789Sahrens used, compressed, uncompressed, tx); 104789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 105789Sahrens return; 106789Sahrens } 107789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 108789Sahrens mutex_enter(&ds->ds_lock); 1095378Sck153898 delta = parent_delta(ds, used); 110789Sahrens ds->ds_phys->ds_used_bytes += used; 111789Sahrens ds->ds_phys->ds_compressed_bytes += compressed; 112789Sahrens ds->ds_phys->ds_uncompressed_bytes += uncompressed; 113789Sahrens ds->ds_phys->ds_unique_bytes += used; 114789Sahrens mutex_exit(&ds->ds_lock); 1155378Sck153898 dsl_dir_diduse_space(ds->ds_dir, delta, compressed, uncompressed, tx); 116789Sahrens } 117789Sahrens 118789Sahrens void 1193547Smaybee dsl_dataset_block_kill(dsl_dataset_t *ds, blkptr_t *bp, zio_t *pio, 1203547Smaybee dmu_tx_t *tx) 121789Sahrens { 1222082Seschrock int used = bp_get_dasize(tx->tx_pool->dp_spa, bp); 123789Sahrens int compressed = BP_GET_PSIZE(bp); 124789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 125789Sahrens 126789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1273547Smaybee /* No block pointer => nothing to free */ 128789Sahrens if (BP_IS_HOLE(bp)) 129789Sahrens return; 130789Sahrens 131789Sahrens ASSERT(used > 0); 132789Sahrens if (ds == NULL) { 1333547Smaybee int err; 134789Sahrens /* 135789Sahrens * Account for the meta-objset space in its placeholder 136789Sahrens * dataset. 137789Sahrens */ 1383547Smaybee err = arc_free(pio, tx->tx_pool->dp_spa, 1393547Smaybee tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 1403547Smaybee ASSERT(err == 0); 141789Sahrens 142789Sahrens dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, 143789Sahrens -used, -compressed, -uncompressed, tx); 144789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 145789Sahrens return; 146789Sahrens } 147789Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 148789Sahrens 149789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 150789Sahrens 151789Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 1523547Smaybee int err; 1535378Sck153898 int64_t delta; 1543547Smaybee 155789Sahrens dprintf_bp(bp, "freeing: %s", ""); 1563547Smaybee err = arc_free(pio, tx->tx_pool->dp_spa, 1573547Smaybee tx->tx_txg, bp, NULL, NULL, pio ? ARC_NOWAIT: ARC_WAIT); 1583547Smaybee ASSERT(err == 0); 159789Sahrens 160789Sahrens mutex_enter(&ds->ds_lock); 1615378Sck153898 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 1625378Sck153898 !DS_UNIQUE_IS_ACCURATE(ds)); 1635378Sck153898 delta = parent_delta(ds, -used); 164789Sahrens ds->ds_phys->ds_unique_bytes -= used; 165789Sahrens mutex_exit(&ds->ds_lock); 166789Sahrens dsl_dir_diduse_space(ds->ds_dir, 1675378Sck153898 delta, -compressed, -uncompressed, tx); 168789Sahrens } else { 169789Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 1701544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 1715712Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 1725712Sahrens ds->ds_phys->ds_prev_snap_obj); 1735712Sahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 174789Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 1755712Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 1765712Sahrens ds->ds_object && bp->blk_birth > 1775712Sahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 1785712Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1795712Sahrens mutex_enter(&ds->ds_prev->ds_lock); 1805712Sahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 1815712Sahrens mutex_exit(&ds->ds_prev->ds_lock); 182789Sahrens } 183789Sahrens } 184789Sahrens mutex_enter(&ds->ds_lock); 185789Sahrens ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 186789Sahrens ds->ds_phys->ds_used_bytes -= used; 187789Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 188789Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 189789Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 190789Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 191789Sahrens mutex_exit(&ds->ds_lock); 192789Sahrens } 193789Sahrens 1941544Seschrock uint64_t 1951544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 196789Sahrens { 1972885Sahrens uint64_t trysnap = 0; 1982885Sahrens 199789Sahrens if (ds == NULL) 2001544Seschrock return (0); 201789Sahrens /* 202789Sahrens * The snapshot creation could fail, but that would cause an 203789Sahrens * incorrect FALSE return, which would only result in an 204789Sahrens * overestimation of the amount of space that an operation would 205789Sahrens * consume, which is OK. 206789Sahrens * 207789Sahrens * There's also a small window where we could miss a pending 208789Sahrens * snapshot, because we could set the sync task in the quiescing 209789Sahrens * phase. So this should only be used as a guess. 210789Sahrens */ 2112885Sahrens if (ds->ds_trysnap_txg > 2122885Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 2132885Sahrens trysnap = ds->ds_trysnap_txg; 2142885Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 2151544Seschrock } 2161544Seschrock 2171544Seschrock int 2181544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 2191544Seschrock { 2201544Seschrock return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 221789Sahrens } 222789Sahrens 223789Sahrens /* ARGSUSED */ 224789Sahrens static void 225789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 226789Sahrens { 227789Sahrens dsl_dataset_t *ds = dsv; 228789Sahrens 229*6689Smaybee ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 230789Sahrens 231789Sahrens dprintf_ds(ds, "evicting %s\n", ""); 232789Sahrens 2334787Sahrens unique_remove(ds->ds_fsid_guid); 234789Sahrens 235789Sahrens if (ds->ds_user_ptr != NULL) 236789Sahrens ds->ds_user_evict_func(ds, ds->ds_user_ptr); 237789Sahrens 238789Sahrens if (ds->ds_prev) { 239*6689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 240789Sahrens ds->ds_prev = NULL; 241789Sahrens } 242789Sahrens 243789Sahrens bplist_close(&ds->ds_deadlist); 244*6689Smaybee if (ds->ds_dir) 245*6689Smaybee dsl_dir_close(ds->ds_dir, ds); 246789Sahrens 2474787Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 248789Sahrens 2492856Snd150628 mutex_destroy(&ds->ds_lock); 2504787Sahrens mutex_destroy(&ds->ds_opening_lock); 2512856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 252*6689Smaybee rw_destroy(&ds->ds_rwlock); 253*6689Smaybee cv_destroy(&ds->ds_exclusive_cv); 2542856Snd150628 255789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 256789Sahrens } 257789Sahrens 2581544Seschrock static int 259789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 260789Sahrens { 261789Sahrens dsl_dataset_phys_t *headphys; 262789Sahrens int err; 263789Sahrens dmu_buf_t *headdbuf; 264789Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 265789Sahrens objset_t *mos = dp->dp_meta_objset; 266789Sahrens 267789Sahrens if (ds->ds_snapname[0]) 2681544Seschrock return (0); 269789Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 2701544Seschrock return (0); 271789Sahrens 2721544Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 2731544Seschrock FTAG, &headdbuf); 2741544Seschrock if (err) 2751544Seschrock return (err); 276789Sahrens headphys = headdbuf->db_data; 277789Sahrens err = zap_value_search(dp->dp_meta_objset, 2784577Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 2791544Seschrock dmu_buf_rele(headdbuf, FTAG); 2801544Seschrock return (err); 281789Sahrens } 282789Sahrens 2836492Stimh static int 284*6689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 2856492Stimh { 286*6689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 287*6689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 2886492Stimh matchtype_t mt; 2896492Stimh int err; 2906492Stimh 291*6689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 2926492Stimh mt = MT_FIRST; 2936492Stimh else 2946492Stimh mt = MT_EXACT; 2956492Stimh 296*6689Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 2976492Stimh value, mt, NULL, 0, NULL); 2986492Stimh if (err == ENOTSUP && mt == MT_FIRST) 299*6689Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 3006492Stimh return (err); 3016492Stimh } 3026492Stimh 3036492Stimh static int 304*6689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 3056492Stimh { 306*6689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 307*6689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3086492Stimh matchtype_t mt; 3096492Stimh int err; 3106492Stimh 311*6689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3126492Stimh mt = MT_FIRST; 3136492Stimh else 3146492Stimh mt = MT_EXACT; 3156492Stimh 316*6689Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 3176492Stimh if (err == ENOTSUP && mt == MT_FIRST) 318*6689Smaybee err = zap_remove(mos, snapobj, name, tx); 3196492Stimh return (err); 3206492Stimh } 3216492Stimh 322*6689Smaybee static int 323*6689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag, 324*6689Smaybee dsl_dataset_t **dsp) 325789Sahrens { 326789Sahrens objset_t *mos = dp->dp_meta_objset; 327789Sahrens dmu_buf_t *dbuf; 328789Sahrens dsl_dataset_t *ds; 3291544Seschrock int err; 330789Sahrens 331789Sahrens ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 332789Sahrens dsl_pool_sync_context(dp)); 333789Sahrens 3341544Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3351544Seschrock if (err) 3361544Seschrock return (err); 337789Sahrens ds = dmu_buf_get_user(dbuf); 338789Sahrens if (ds == NULL) { 339789Sahrens dsl_dataset_t *winner; 340789Sahrens 341789Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 342789Sahrens ds->ds_dbuf = dbuf; 343789Sahrens ds->ds_object = dsobj; 344789Sahrens ds->ds_phys = dbuf->db_data; 345789Sahrens 3462856Snd150628 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 3474787Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3482856Snd150628 mutex_init(&ds->ds_deadlist.bpl_lock, NULL, MUTEX_DEFAULT, 3492856Snd150628 NULL); 350*6689Smaybee rw_init(&ds->ds_rwlock, 0, 0, 0); 351*6689Smaybee cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 3522856Snd150628 3531544Seschrock err = bplist_open(&ds->ds_deadlist, 354789Sahrens mos, ds->ds_phys->ds_deadlist_obj); 3551544Seschrock if (err == 0) { 3561544Seschrock err = dsl_dir_open_obj(dp, 3571544Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 3581544Seschrock } 3591544Seschrock if (err) { 3601544Seschrock /* 3611544Seschrock * we don't really need to close the blist if we 3621544Seschrock * just opened it. 3631544Seschrock */ 3642856Snd150628 mutex_destroy(&ds->ds_lock); 3654787Sahrens mutex_destroy(&ds->ds_opening_lock); 3662856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 367*6689Smaybee rw_destroy(&ds->ds_rwlock); 368*6689Smaybee cv_destroy(&ds->ds_exclusive_cv); 3691544Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 3701544Seschrock dmu_buf_rele(dbuf, tag); 3711544Seschrock return (err); 3721544Seschrock } 373789Sahrens 374789Sahrens if (ds->ds_dir->dd_phys->dd_head_dataset_obj == dsobj) { 375789Sahrens ds->ds_snapname[0] = '\0'; 376789Sahrens if (ds->ds_phys->ds_prev_snap_obj) { 377*6689Smaybee err = dsl_dataset_get_ref(dp, 378*6689Smaybee ds->ds_phys->ds_prev_snap_obj, 379*6689Smaybee ds, &ds->ds_prev); 380789Sahrens } 381*6689Smaybee } else if (zfs_flags & ZFS_DEBUG_SNAPNAMES) { 382*6689Smaybee err = dsl_dataset_get_snapname(ds); 383789Sahrens } 384789Sahrens 3855475Sck153898 if (!dsl_dataset_is_snapshot(ds)) { 3865569Sck153898 /* 3875569Sck153898 * In sync context, we're called with either no lock 3885569Sck153898 * or with the write lock. If we're not syncing, 3895569Sck153898 * we're always called with the read lock held. 3905569Sck153898 */ 3915475Sck153898 boolean_t need_lock = 3925569Sck153898 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 3935569Sck153898 dsl_pool_sync_context(dp); 3945475Sck153898 3955475Sck153898 if (need_lock) 3965475Sck153898 rw_enter(&dp->dp_config_rwlock, RW_READER); 3975475Sck153898 3985475Sck153898 err = dsl_prop_get_ds_locked(ds->ds_dir, 3995475Sck153898 "refreservation", sizeof (uint64_t), 1, 4005475Sck153898 &ds->ds_reserved, NULL); 4015475Sck153898 if (err == 0) { 4025475Sck153898 err = dsl_prop_get_ds_locked(ds->ds_dir, 4035475Sck153898 "refquota", sizeof (uint64_t), 1, 4045475Sck153898 &ds->ds_quota, NULL); 4055475Sck153898 } 4065475Sck153898 4075475Sck153898 if (need_lock) 4085475Sck153898 rw_exit(&dp->dp_config_rwlock); 4095475Sck153898 } else { 4105475Sck153898 ds->ds_reserved = ds->ds_quota = 0; 4115475Sck153898 } 4125475Sck153898 4131544Seschrock if (err == 0) { 4141544Seschrock winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 4151544Seschrock dsl_dataset_evict); 4161544Seschrock } 4171544Seschrock if (err || winner) { 418789Sahrens bplist_close(&ds->ds_deadlist); 419*6689Smaybee if (ds->ds_prev) 420*6689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 421789Sahrens dsl_dir_close(ds->ds_dir, ds); 4222856Snd150628 mutex_destroy(&ds->ds_lock); 4234787Sahrens mutex_destroy(&ds->ds_opening_lock); 4242856Snd150628 mutex_destroy(&ds->ds_deadlist.bpl_lock); 425*6689Smaybee rw_destroy(&ds->ds_rwlock); 426*6689Smaybee cv_destroy(&ds->ds_exclusive_cv); 427789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4281544Seschrock if (err) { 4291544Seschrock dmu_buf_rele(dbuf, tag); 4301544Seschrock return (err); 4311544Seschrock } 432789Sahrens ds = winner; 433789Sahrens } else { 4344787Sahrens ds->ds_fsid_guid = 435789Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 436789Sahrens } 437789Sahrens } 438789Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 439789Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 440789Sahrens mutex_enter(&ds->ds_lock); 441*6689Smaybee if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 442789Sahrens mutex_exit(&ds->ds_lock); 443*6689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 444*6689Smaybee return (ENOENT); 445*6689Smaybee } 446*6689Smaybee mutex_exit(&ds->ds_lock); 447*6689Smaybee *dsp = ds; 448*6689Smaybee return (0); 449*6689Smaybee } 450*6689Smaybee 451*6689Smaybee static int 452*6689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 453*6689Smaybee { 454*6689Smaybee dsl_pool_t *dp = ds->ds_dir->dd_pool; 455*6689Smaybee 456*6689Smaybee /* 457*6689Smaybee * In syncing context we don't want the rwlock lock: there 458*6689Smaybee * may be an existing writer waiting for sync phase to 459*6689Smaybee * finish. We don't need to worry about such writers, since 460*6689Smaybee * sync phase is single-threaded, so the writer can't be 461*6689Smaybee * doing anything while we are active. 462*6689Smaybee */ 463*6689Smaybee if (dsl_pool_sync_context(dp)) { 464*6689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 465*6689Smaybee return (0); 466789Sahrens } 467*6689Smaybee 468*6689Smaybee /* 469*6689Smaybee * Normal users will hold the ds_rwlock as a READER until they 470*6689Smaybee * are finished (i.e., call dsl_dataset_rele()). "Owners" will 471*6689Smaybee * drop their READER lock after they set the ds_owner field. 472*6689Smaybee * 473*6689Smaybee * If the dataset is being destroyed, the destroy thread will 474*6689Smaybee * obtain a WRITER lock for exclusive access after it's done its 475*6689Smaybee * open-context work and then change the ds_owner to 476*6689Smaybee * dsl_reaper once destruction is assured. So threads 477*6689Smaybee * may block here temporarily, until the "destructability" of 478*6689Smaybee * the dataset is determined. 479*6689Smaybee */ 480*6689Smaybee ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 481*6689Smaybee mutex_enter(&ds->ds_lock); 482*6689Smaybee while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 483*6689Smaybee rw_exit(&dp->dp_config_rwlock); 484*6689Smaybee cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 485*6689Smaybee if (DSL_DATASET_IS_DESTROYED(ds)) { 486*6689Smaybee mutex_exit(&ds->ds_lock); 487*6689Smaybee dsl_dataset_drop_ref(ds, tag); 488*6689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 489*6689Smaybee return (ENOENT); 490*6689Smaybee } 491*6689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 492*6689Smaybee } 493789Sahrens mutex_exit(&ds->ds_lock); 4941544Seschrock return (0); 495789Sahrens } 496789Sahrens 497789Sahrens int 498*6689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 499*6689Smaybee dsl_dataset_t **dsp) 500*6689Smaybee { 501*6689Smaybee int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 502*6689Smaybee 503*6689Smaybee if (err) 504*6689Smaybee return (err); 505*6689Smaybee return (dsl_dataset_hold_ref(*dsp, tag)); 506*6689Smaybee } 507*6689Smaybee 508*6689Smaybee int 509*6689Smaybee dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, int flags, void *owner, 510*6689Smaybee dsl_dataset_t **dsp) 511*6689Smaybee { 512*6689Smaybee int err = dsl_dataset_hold_obj(dp, dsobj, owner, dsp); 513*6689Smaybee 514*6689Smaybee ASSERT(DS_MODE_TYPE(flags) != DS_MODE_USER); 515*6689Smaybee 516*6689Smaybee if (err) 517*6689Smaybee return (err); 518*6689Smaybee if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 519*6689Smaybee dsl_dataset_rele(*dsp, owner); 520*6689Smaybee return (EBUSY); 521*6689Smaybee } 522*6689Smaybee return (0); 523*6689Smaybee } 524*6689Smaybee 525*6689Smaybee int 526*6689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 527789Sahrens { 528789Sahrens dsl_dir_t *dd; 529789Sahrens dsl_pool_t *dp; 530*6689Smaybee const char *snapname; 531789Sahrens uint64_t obj; 532789Sahrens int err = 0; 533789Sahrens 534*6689Smaybee err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 5351544Seschrock if (err) 5361544Seschrock return (err); 537789Sahrens 538789Sahrens dp = dd->dd_pool; 539789Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 540789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 541*6689Smaybee if (obj) 542*6689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, dsp); 543*6689Smaybee else 544789Sahrens err = ENOENT; 545*6689Smaybee if (err) 546789Sahrens goto out; 547*6689Smaybee 548*6689Smaybee err = dsl_dataset_hold_ref(*dsp, tag); 549*6689Smaybee 550*6689Smaybee /* we may be looking for a snapshot */ 551*6689Smaybee if (err == 0 && snapname != NULL) { 552*6689Smaybee dsl_dataset_t *ds = NULL; 553*6689Smaybee 554*6689Smaybee if (*snapname++ != '@') { 555*6689Smaybee dsl_dataset_rele(*dsp, tag); 556789Sahrens err = ENOENT; 557789Sahrens goto out; 558789Sahrens } 559*6689Smaybee 560*6689Smaybee dprintf("looking for snapshot '%s'\n", snapname); 561*6689Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 562*6689Smaybee if (err == 0) 563*6689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, &ds); 564*6689Smaybee dsl_dataset_rele(*dsp, tag); 565*6689Smaybee 566*6689Smaybee ASSERT3U((err == 0), ==, (ds != NULL)); 567*6689Smaybee 568*6689Smaybee if (ds) { 569*6689Smaybee mutex_enter(&ds->ds_lock); 570*6689Smaybee if (ds->ds_snapname[0] == 0) 571*6689Smaybee (void) strlcpy(ds->ds_snapname, snapname, 572*6689Smaybee sizeof (ds->ds_snapname)); 573*6689Smaybee mutex_exit(&ds->ds_lock); 574*6689Smaybee err = dsl_dataset_hold_ref(ds, tag); 575*6689Smaybee *dsp = err ? NULL : ds; 576789Sahrens } 577789Sahrens } 578789Sahrens out: 579789Sahrens rw_exit(&dp->dp_config_rwlock); 580789Sahrens dsl_dir_close(dd, FTAG); 581789Sahrens return (err); 582789Sahrens } 583789Sahrens 584789Sahrens int 585*6689Smaybee dsl_dataset_own(const char *name, int flags, void *owner, dsl_dataset_t **dsp) 586789Sahrens { 587*6689Smaybee int err = dsl_dataset_hold(name, owner, dsp); 588*6689Smaybee if (err) 589*6689Smaybee return (err); 590*6689Smaybee if ((*dsp)->ds_phys->ds_num_children > 0 && 591*6689Smaybee !DS_MODE_IS_READONLY(flags)) { 592*6689Smaybee dsl_dataset_rele(*dsp, owner); 593*6689Smaybee return (EROFS); 594*6689Smaybee } 595*6689Smaybee if (!dsl_dataset_tryown(*dsp, DS_MODE_IS_INCONSISTENT(flags), owner)) { 596*6689Smaybee dsl_dataset_rele(*dsp, owner); 597*6689Smaybee return (EBUSY); 598*6689Smaybee } 599*6689Smaybee return (0); 600789Sahrens } 601789Sahrens 602789Sahrens void 603789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 604789Sahrens { 605789Sahrens if (ds == NULL) { 606789Sahrens (void) strcpy(name, "mos"); 607789Sahrens } else { 608789Sahrens dsl_dir_name(ds->ds_dir, name); 6091544Seschrock VERIFY(0 == dsl_dataset_get_snapname(ds)); 610789Sahrens if (ds->ds_snapname[0]) { 611789Sahrens (void) strcat(name, "@"); 612*6689Smaybee /* 613*6689Smaybee * We use a "recursive" mutex so that we 614*6689Smaybee * can call dprintf_ds() with ds_lock held. 615*6689Smaybee */ 616789Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 617789Sahrens mutex_enter(&ds->ds_lock); 618789Sahrens (void) strcat(name, ds->ds_snapname); 619789Sahrens mutex_exit(&ds->ds_lock); 620789Sahrens } else { 621789Sahrens (void) strcat(name, ds->ds_snapname); 622789Sahrens } 623789Sahrens } 624789Sahrens } 625789Sahrens } 626789Sahrens 6273978Smmusante static int 6283978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds) 6293978Smmusante { 6303978Smmusante int result; 6313978Smmusante 6323978Smmusante if (ds == NULL) { 6333978Smmusante result = 3; /* "mos" */ 6343978Smmusante } else { 6353978Smmusante result = dsl_dir_namelen(ds->ds_dir); 6363978Smmusante VERIFY(0 == dsl_dataset_get_snapname(ds)); 6373978Smmusante if (ds->ds_snapname[0]) { 6383978Smmusante ++result; /* adding one for the @-sign */ 6393978Smmusante if (!MUTEX_HELD(&ds->ds_lock)) { 6403978Smmusante mutex_enter(&ds->ds_lock); 6413978Smmusante result += strlen(ds->ds_snapname); 6423978Smmusante mutex_exit(&ds->ds_lock); 6433978Smmusante } else { 6443978Smmusante result += strlen(ds->ds_snapname); 6453978Smmusante } 6463978Smmusante } 6473978Smmusante } 6483978Smmusante 6493978Smmusante return (result); 6503978Smmusante } 6513978Smmusante 652*6689Smaybee static void 653*6689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 654789Sahrens { 6551544Seschrock dmu_buf_rele(ds->ds_dbuf, tag); 656789Sahrens } 657789Sahrens 658789Sahrens void 659*6689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6605367Sahrens { 661*6689Smaybee ASSERT(ds->ds_owner != tag); 662*6689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 663*6689Smaybee rw_exit(&ds->ds_rwlock); 664*6689Smaybee } 665*6689Smaybee dsl_dataset_drop_ref(ds, tag); 666*6689Smaybee } 667*6689Smaybee 668*6689Smaybee void 669*6689Smaybee dsl_dataset_disown(dsl_dataset_t *ds, void *owner) 670*6689Smaybee { 671*6689Smaybee ASSERT((ds->ds_owner == owner && ds->ds_dbuf) || 672*6689Smaybee (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 673*6689Smaybee 6745367Sahrens mutex_enter(&ds->ds_lock); 675*6689Smaybee ds->ds_owner = NULL; 676*6689Smaybee if (RW_WRITE_HELD(&ds->ds_rwlock)) { 677*6689Smaybee rw_exit(&ds->ds_rwlock); 678*6689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 679*6689Smaybee } 6805367Sahrens mutex_exit(&ds->ds_lock); 681*6689Smaybee if (ds->ds_dbuf) 682*6689Smaybee dsl_dataset_drop_ref(ds, owner); 683*6689Smaybee else 684*6689Smaybee dsl_dataset_evict(ds->ds_dbuf, ds); 6855367Sahrens } 6865367Sahrens 6875367Sahrens boolean_t 688*6689Smaybee dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *owner) 6895367Sahrens { 690*6689Smaybee boolean_t gotit = FALSE; 691*6689Smaybee 6925367Sahrens mutex_enter(&ds->ds_lock); 693*6689Smaybee if (ds->ds_owner == NULL && 694*6689Smaybee (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 695*6689Smaybee ds->ds_owner = owner; 696*6689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 697*6689Smaybee rw_exit(&ds->ds_rwlock); 698*6689Smaybee gotit = TRUE; 6995367Sahrens } 7005367Sahrens mutex_exit(&ds->ds_lock); 701*6689Smaybee return (gotit); 702*6689Smaybee } 703*6689Smaybee 704*6689Smaybee void 705*6689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 706*6689Smaybee { 707*6689Smaybee ASSERT3P(owner, ==, ds->ds_owner); 708*6689Smaybee if (!RW_WRITE_HELD(&ds->ds_rwlock)) 709*6689Smaybee rw_enter(&ds->ds_rwlock, RW_WRITER); 7105367Sahrens } 7115367Sahrens 7125367Sahrens void 713789Sahrens dsl_dataset_create_root(dsl_pool_t *dp, uint64_t *ddobjp, dmu_tx_t *tx) 714789Sahrens { 715789Sahrens objset_t *mos = dp->dp_meta_objset; 716789Sahrens dmu_buf_t *dbuf; 717789Sahrens dsl_dataset_phys_t *dsphys; 718789Sahrens dsl_dataset_t *ds; 719789Sahrens uint64_t dsobj; 720789Sahrens dsl_dir_t *dd; 721789Sahrens 722789Sahrens dsl_dir_create_root(mos, ddobjp, tx); 7231544Seschrock VERIFY(0 == dsl_dir_open_obj(dp, *ddobjp, NULL, FTAG, &dd)); 724789Sahrens 725928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 726928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7271544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 728789Sahrens dmu_buf_will_dirty(dbuf, tx); 729789Sahrens dsphys = dbuf->db_data; 730789Sahrens dsphys->ds_dir_obj = dd->dd_object; 731789Sahrens dsphys->ds_fsid_guid = unique_create(); 732789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 733789Sahrens sizeof (dsphys->ds_guid)); 734789Sahrens dsphys->ds_snapnames_zapobj = 7356492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7366492Stimh DMU_OT_NONE, 0, tx); 737789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 738789Sahrens dsphys->ds_creation_txg = tx->tx_txg; 739789Sahrens dsphys->ds_deadlist_obj = 740789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7415378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 7425378Sck153898 dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 7431544Seschrock dmu_buf_rele(dbuf, FTAG); 744789Sahrens 745789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 746789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 747789Sahrens dsl_dir_close(dd, FTAG); 748789Sahrens 749*6689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, dsobj, FTAG, &ds)); 7503547Smaybee (void) dmu_objset_create_impl(dp->dp_spa, ds, 7513547Smaybee &ds->ds_phys->ds_bp, DMU_OST_ZFS, tx); 752*6689Smaybee dsl_dataset_drop_ref(ds, FTAG); 753789Sahrens } 754789Sahrens 7552199Sahrens uint64_t 7566492Stimh dsl_dataset_create_sync_impl(dsl_dir_t *dd, dsl_dataset_t *origin, 7576492Stimh uint64_t flags, dmu_tx_t *tx) 758789Sahrens { 7595367Sahrens dsl_pool_t *dp = dd->dd_pool; 760789Sahrens dmu_buf_t *dbuf; 761789Sahrens dsl_dataset_phys_t *dsphys; 7625367Sahrens uint64_t dsobj; 763789Sahrens objset_t *mos = dp->dp_meta_objset; 764789Sahrens 7655367Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 7665367Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 767789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 7685367Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 769789Sahrens 770928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 771928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7721544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 773789Sahrens dmu_buf_will_dirty(dbuf, tx); 774789Sahrens dsphys = dbuf->db_data; 775*6689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 776789Sahrens dsphys->ds_dir_obj = dd->dd_object; 7776492Stimh dsphys->ds_flags = flags; 778789Sahrens dsphys->ds_fsid_guid = unique_create(); 779789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 780789Sahrens sizeof (dsphys->ds_guid)); 781789Sahrens dsphys->ds_snapnames_zapobj = 7826492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7836492Stimh DMU_OT_NONE, 0, tx); 784789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 785789Sahrens dsphys->ds_creation_txg = tx->tx_txg; 786789Sahrens dsphys->ds_deadlist_obj = 787789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7885378Sck153898 7895367Sahrens if (origin) { 7905367Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 791789Sahrens dsphys->ds_prev_snap_txg = 7925367Sahrens origin->ds_phys->ds_creation_txg; 793789Sahrens dsphys->ds_used_bytes = 7945367Sahrens origin->ds_phys->ds_used_bytes; 795789Sahrens dsphys->ds_compressed_bytes = 7965367Sahrens origin->ds_phys->ds_compressed_bytes; 797789Sahrens dsphys->ds_uncompressed_bytes = 7985367Sahrens origin->ds_phys->ds_uncompressed_bytes; 7995367Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 8006502Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 801789Sahrens 8025367Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 8035367Sahrens origin->ds_phys->ds_num_children++; 804789Sahrens 805789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 8065367Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 807789Sahrens } 8086492Stimh 8096492Stimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 8106492Stimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 8116492Stimh 8121544Seschrock dmu_buf_rele(dbuf, FTAG); 813789Sahrens 814789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 815789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 8165367Sahrens 8175367Sahrens return (dsobj); 8185367Sahrens } 8195367Sahrens 8205367Sahrens uint64_t 8216492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 8226492Stimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8235367Sahrens { 8245367Sahrens dsl_pool_t *dp = pdd->dd_pool; 8255367Sahrens uint64_t dsobj, ddobj; 8265367Sahrens dsl_dir_t *dd; 8275367Sahrens 8285367Sahrens ASSERT(lastname[0] != '@'); 8295367Sahrens 8305367Sahrens ddobj = dsl_dir_create_sync(pdd, lastname, tx); 8315367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 8325367Sahrens 8336492Stimh dsobj = dsl_dataset_create_sync_impl(dd, origin, flags, tx); 8345367Sahrens 8355367Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8365367Sahrens 837789Sahrens dsl_dir_close(dd, FTAG); 838789Sahrens 8392199Sahrens return (dsobj); 8402199Sahrens } 8412199Sahrens 8422199Sahrens struct destroyarg { 8432199Sahrens dsl_sync_task_group_t *dstg; 8442199Sahrens char *snapname; 8452199Sahrens char *failed; 8462199Sahrens }; 8472199Sahrens 8482199Sahrens static int 8492199Sahrens dsl_snapshot_destroy_one(char *name, void *arg) 8502199Sahrens { 8512199Sahrens struct destroyarg *da = arg; 8522199Sahrens dsl_dataset_t *ds; 8532199Sahrens char *cp; 8542199Sahrens int err; 8552199Sahrens 8562199Sahrens (void) strcat(name, "@"); 8572199Sahrens (void) strcat(name, da->snapname); 858*6689Smaybee err = dsl_dataset_own(name, DS_MODE_READONLY | DS_MODE_INCONSISTENT, 8594007Smmusante da->dstg, &ds); 8602199Sahrens cp = strchr(name, '@'); 8612199Sahrens *cp = '\0'; 862*6689Smaybee if (err == 0) { 863*6689Smaybee dsl_dataset_make_exclusive(ds, da->dstg); 864*6689Smaybee dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 865*6689Smaybee dsl_dataset_destroy_sync, ds, da->dstg, 0); 866*6689Smaybee } else if (err == ENOENT) { 867*6689Smaybee err = 0; 868*6689Smaybee } else { 8692199Sahrens (void) strcpy(da->failed, name); 8702199Sahrens } 871*6689Smaybee return (err); 872789Sahrens } 873789Sahrens 8742199Sahrens /* 8752199Sahrens * Destroy 'snapname' in all descendants of 'fsname'. 8762199Sahrens */ 8772199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 8782199Sahrens int 8792199Sahrens dsl_snapshots_destroy(char *fsname, char *snapname) 8802199Sahrens { 8812199Sahrens int err; 8822199Sahrens struct destroyarg da; 8832199Sahrens dsl_sync_task_t *dst; 8842199Sahrens spa_t *spa; 8852199Sahrens 8864603Sahrens err = spa_open(fsname, &spa, FTAG); 8872199Sahrens if (err) 8882199Sahrens return (err); 8892199Sahrens da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 8902199Sahrens da.snapname = snapname; 8912199Sahrens da.failed = fsname; 8922199Sahrens 8932199Sahrens err = dmu_objset_find(fsname, 8942417Sahrens dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 8952199Sahrens 8962199Sahrens if (err == 0) 8972199Sahrens err = dsl_sync_task_group_wait(da.dstg); 8982199Sahrens 8992199Sahrens for (dst = list_head(&da.dstg->dstg_tasks); dst; 9002199Sahrens dst = list_next(&da.dstg->dstg_tasks, dst)) { 9012199Sahrens dsl_dataset_t *ds = dst->dst_arg1; 902*6689Smaybee /* 903*6689Smaybee * Return the file system name that triggered the error 904*6689Smaybee */ 9052199Sahrens if (dst->dst_err) { 9062199Sahrens dsl_dataset_name(ds, fsname); 9074603Sahrens *strchr(fsname, '@') = '\0'; 9082199Sahrens } 909*6689Smaybee dsl_dataset_disown(ds, da.dstg); 9102199Sahrens } 9112199Sahrens 9122199Sahrens dsl_sync_task_group_destroy(da.dstg); 9132199Sahrens spa_close(spa, FTAG); 9142199Sahrens return (err); 9152199Sahrens } 9162199Sahrens 9175367Sahrens /* 918*6689Smaybee * ds must be opened as OWNER. On return (whether successful or not), 919*6689Smaybee * ds will be closed and caller can no longer dereference it. 9205367Sahrens */ 921789Sahrens int 9225367Sahrens dsl_dataset_destroy(dsl_dataset_t *ds, void *tag) 923789Sahrens { 924789Sahrens int err; 9252199Sahrens dsl_sync_task_group_t *dstg; 9262199Sahrens objset_t *os; 927789Sahrens dsl_dir_t *dd; 9282199Sahrens uint64_t obj; 9292199Sahrens 9305367Sahrens if (dsl_dataset_is_snapshot(ds)) { 9312199Sahrens /* Destroying a snapshot is simpler */ 932*6689Smaybee dsl_dataset_make_exclusive(ds, tag); 9332199Sahrens err = dsl_sync_task_do(ds->ds_dir->dd_pool, 9342199Sahrens dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 9355367Sahrens ds, tag, 0); 9365367Sahrens goto out; 9372199Sahrens } 9382199Sahrens 9392199Sahrens dd = ds->ds_dir; 940789Sahrens 9412199Sahrens /* 9422199Sahrens * Check for errors and mark this ds as inconsistent, in 9432199Sahrens * case we crash while freeing the objects. 9442199Sahrens */ 9452199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 9462199Sahrens dsl_dataset_destroy_begin_sync, ds, NULL, 0); 9475367Sahrens if (err) 9485367Sahrens goto out; 9495367Sahrens 9505367Sahrens err = dmu_objset_open_ds(ds, DMU_OST_ANY, &os); 9515367Sahrens if (err) 9525367Sahrens goto out; 9532199Sahrens 9542199Sahrens /* 9552199Sahrens * remove the objects in open context, so that we won't 9562199Sahrens * have too much to do in syncing context. 9572199Sahrens */ 9583025Sahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 9593025Sahrens ds->ds_phys->ds_prev_snap_txg)) { 9602199Sahrens dmu_tx_t *tx = dmu_tx_create(os); 9612199Sahrens dmu_tx_hold_free(tx, obj, 0, DMU_OBJECT_END); 9622199Sahrens dmu_tx_hold_bonus(tx, obj); 9632199Sahrens err = dmu_tx_assign(tx, TXG_WAIT); 9642199Sahrens if (err) { 9652199Sahrens /* 9662199Sahrens * Perhaps there is not enough disk 9672199Sahrens * space. Just deal with it from 9682199Sahrens * dsl_dataset_destroy_sync(). 9692199Sahrens */ 9702199Sahrens dmu_tx_abort(tx); 9712199Sahrens continue; 9722199Sahrens } 9732199Sahrens VERIFY(0 == dmu_object_free(os, obj, tx)); 9742199Sahrens dmu_tx_commit(tx); 9752199Sahrens } 9762199Sahrens 9772199Sahrens dmu_objset_close(os); 9782199Sahrens if (err != ESRCH) 9795367Sahrens goto out; 9802199Sahrens 9815367Sahrens if (ds->ds_user_ptr) { 982*6689Smaybee /* 983*6689Smaybee * We need to sync out all in-flight IO before we try 984*6689Smaybee * to evict (the dataset evict func is trying to clear 985*6689Smaybee * the cached entries for this dataset in the ARC). 986*6689Smaybee */ 987*6689Smaybee txg_wait_synced(dd->dd_pool, 0); 9885367Sahrens ds->ds_user_evict_func(ds, ds->ds_user_ptr); 9895367Sahrens ds->ds_user_ptr = NULL; 9905367Sahrens } 9915367Sahrens 9925367Sahrens rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 9935367Sahrens err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 9945367Sahrens rw_exit(&dd->dd_pool->dp_config_rwlock); 9955367Sahrens 9961544Seschrock if (err) 9975367Sahrens goto out; 998789Sahrens 9992199Sahrens /* 10002199Sahrens * Blow away the dsl_dir + head dataset. 10012199Sahrens */ 1002*6689Smaybee dsl_dataset_make_exclusive(ds, tag); 10032199Sahrens dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 10042199Sahrens dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 10055367Sahrens dsl_dataset_destroy_sync, ds, tag, 0); 10062199Sahrens dsl_sync_task_create(dstg, dsl_dir_destroy_check, 10072199Sahrens dsl_dir_destroy_sync, dd, FTAG, 0); 10082199Sahrens err = dsl_sync_task_group_wait(dstg); 10092199Sahrens dsl_sync_task_group_destroy(dstg); 1010*6689Smaybee /* if it is successful, dsl_dir_destroy_sync will close the dd */ 10115367Sahrens if (err) 10122199Sahrens dsl_dir_close(dd, FTAG); 10135367Sahrens out: 1014*6689Smaybee dsl_dataset_disown(ds, tag); 1015789Sahrens return (err); 1016789Sahrens } 1017789Sahrens 1018789Sahrens int 10195367Sahrens dsl_dataset_rollback(dsl_dataset_t *ds, dmu_objset_type_t ost) 1020789Sahrens { 1021*6689Smaybee ASSERT(ds->ds_owner); 10225367Sahrens 10232199Sahrens return (dsl_sync_task_do(ds->ds_dir->dd_pool, 10242199Sahrens dsl_dataset_rollback_check, dsl_dataset_rollback_sync, 10255367Sahrens ds, &ost, 0)); 1026789Sahrens } 1027789Sahrens 1028789Sahrens void * 1029789Sahrens dsl_dataset_set_user_ptr(dsl_dataset_t *ds, 1030789Sahrens void *p, dsl_dataset_evict_func_t func) 1031789Sahrens { 1032789Sahrens void *old; 1033789Sahrens 1034789Sahrens mutex_enter(&ds->ds_lock); 1035789Sahrens old = ds->ds_user_ptr; 1036789Sahrens if (old == NULL) { 1037789Sahrens ds->ds_user_ptr = p; 1038789Sahrens ds->ds_user_evict_func = func; 1039789Sahrens } 1040789Sahrens mutex_exit(&ds->ds_lock); 1041789Sahrens return (old); 1042789Sahrens } 1043789Sahrens 1044789Sahrens void * 1045789Sahrens dsl_dataset_get_user_ptr(dsl_dataset_t *ds) 1046789Sahrens { 1047789Sahrens return (ds->ds_user_ptr); 1048789Sahrens } 1049789Sahrens 1050789Sahrens 10513547Smaybee blkptr_t * 10523547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1053789Sahrens { 10543547Smaybee return (&ds->ds_phys->ds_bp); 1055789Sahrens } 1056789Sahrens 1057789Sahrens void 1058789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1059789Sahrens { 1060789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1061789Sahrens /* If it's the meta-objset, set dp_meta_rootbp */ 1062789Sahrens if (ds == NULL) { 1063789Sahrens tx->tx_pool->dp_meta_rootbp = *bp; 1064789Sahrens } else { 1065789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1066789Sahrens ds->ds_phys->ds_bp = *bp; 1067789Sahrens } 1068789Sahrens } 1069789Sahrens 1070789Sahrens spa_t * 1071789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 1072789Sahrens { 1073789Sahrens return (ds->ds_dir->dd_pool->dp_spa); 1074789Sahrens } 1075789Sahrens 1076789Sahrens void 1077789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1078789Sahrens { 1079789Sahrens dsl_pool_t *dp; 1080789Sahrens 1081789Sahrens if (ds == NULL) /* this is the meta-objset */ 1082789Sahrens return; 1083789Sahrens 1084789Sahrens ASSERT(ds->ds_user_ptr != NULL); 10852885Sahrens 10862885Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 10872885Sahrens panic("dirtying snapshot!"); 1088789Sahrens 1089789Sahrens dp = ds->ds_dir->dd_pool; 1090789Sahrens 1091789Sahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1092789Sahrens /* up the hold count until we can be written out */ 1093789Sahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 1094789Sahrens } 1095789Sahrens } 1096789Sahrens 10975378Sck153898 /* 10985378Sck153898 * The unique space in the head dataset can be calculated by subtracting 10995378Sck153898 * the space used in the most recent snapshot, that is still being used 11005378Sck153898 * in this file system, from the space currently in use. To figure out 11015378Sck153898 * the space in the most recent snapshot still in use, we need to take 11025378Sck153898 * the total space used in the snapshot and subtract out the space that 11035378Sck153898 * has been freed up since the snapshot was taken. 11045378Sck153898 */ 11055378Sck153898 static void 11065378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 11075378Sck153898 { 11085378Sck153898 uint64_t mrs_used; 11095378Sck153898 uint64_t dlused, dlcomp, dluncomp; 11105378Sck153898 11115378Sck153898 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 11125378Sck153898 11135378Sck153898 if (ds->ds_phys->ds_prev_snap_obj != 0) 11145378Sck153898 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 11155378Sck153898 else 11165378Sck153898 mrs_used = 0; 11175378Sck153898 11185378Sck153898 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 11195378Sck153898 &dluncomp)); 11205378Sck153898 11215378Sck153898 ASSERT3U(dlused, <=, mrs_used); 11225378Sck153898 ds->ds_phys->ds_unique_bytes = 11235378Sck153898 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 11245378Sck153898 11255378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && 11265378Sck153898 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 11275378Sck153898 SPA_VERSION_UNIQUE_ACCURATE) 11285378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 11295378Sck153898 } 11305378Sck153898 11315378Sck153898 static uint64_t 11325378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds) 11335378Sck153898 { 11345378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 11355378Sck153898 dsl_dataset_recalc_head_uniq(ds); 11365378Sck153898 11375378Sck153898 return (ds->ds_phys->ds_unique_bytes); 11385378Sck153898 } 11395378Sck153898 1140789Sahrens struct killarg { 11415378Sck153898 int64_t *usedp; 11425378Sck153898 int64_t *compressedp; 11435378Sck153898 int64_t *uncompressedp; 1144789Sahrens zio_t *zio; 1145789Sahrens dmu_tx_t *tx; 1146789Sahrens }; 1147789Sahrens 1148789Sahrens static int 1149789Sahrens kill_blkptr(traverse_blk_cache_t *bc, spa_t *spa, void *arg) 1150789Sahrens { 1151789Sahrens struct killarg *ka = arg; 1152789Sahrens blkptr_t *bp = &bc->bc_blkptr; 1153789Sahrens 1154789Sahrens ASSERT3U(bc->bc_errno, ==, 0); 1155789Sahrens 1156789Sahrens /* 1157789Sahrens * Since this callback is not called concurrently, no lock is 1158789Sahrens * needed on the accounting values. 1159789Sahrens */ 11602082Seschrock *ka->usedp += bp_get_dasize(spa, bp); 1161789Sahrens *ka->compressedp += BP_GET_PSIZE(bp); 1162789Sahrens *ka->uncompressedp += BP_GET_UCSIZE(bp); 1163789Sahrens /* XXX check for EIO? */ 1164789Sahrens (void) arc_free(ka->zio, spa, ka->tx->tx_txg, bp, NULL, NULL, 1165789Sahrens ARC_NOWAIT); 1166789Sahrens return (0); 1167789Sahrens } 1168789Sahrens 1169789Sahrens /* ARGSUSED */ 11702199Sahrens static int 11712199Sahrens dsl_dataset_rollback_check(void *arg1, void *arg2, dmu_tx_t *tx) 1172789Sahrens { 11732199Sahrens dsl_dataset_t *ds = arg1; 11745367Sahrens dmu_objset_type_t *ost = arg2; 1175789Sahrens 11762199Sahrens /* 11775367Sahrens * We can only roll back to emptyness if it is a ZPL objset. 11782199Sahrens */ 11795367Sahrens if (*ost != DMU_OST_ZFS && ds->ds_phys->ds_prev_snap_txg == 0) 1180789Sahrens return (EINVAL); 1181789Sahrens 11822199Sahrens /* 11832199Sahrens * This must not be a snapshot. 11842199Sahrens */ 11852199Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 11862199Sahrens return (EINVAL); 1187789Sahrens 1188789Sahrens /* 1189789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1190789Sahrens * them. Try again. 1191789Sahrens */ 11922199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1193789Sahrens return (EAGAIN); 11942199Sahrens 11952199Sahrens return (0); 11962199Sahrens } 1197789Sahrens 11982199Sahrens /* ARGSUSED */ 11992199Sahrens static void 12004543Smarks dsl_dataset_rollback_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 12012199Sahrens { 12022199Sahrens dsl_dataset_t *ds = arg1; 12035367Sahrens dmu_objset_type_t *ost = arg2; 12042199Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 1205789Sahrens 1206789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1207789Sahrens 12084967Sperrin /* 12094967Sperrin * Before the roll back destroy the zil. 12104967Sperrin */ 12114967Sperrin if (ds->ds_user_ptr != NULL) { 12124967Sperrin zil_rollback_destroy( 12134967Sperrin ((objset_impl_t *)ds->ds_user_ptr)->os_zil, tx); 12145367Sahrens 12155367Sahrens /* 12165367Sahrens * We need to make sure that the objset_impl_t is reopened after 12175367Sahrens * we do the rollback, otherwise it will have the wrong 12185367Sahrens * objset_phys_t. Normally this would happen when this 1219*6689Smaybee * dataset-open is closed, thus causing the 12205367Sahrens * dataset to be immediately evicted. But when doing "zfs recv 12215367Sahrens * -F", we reopen the objset before that, so that there is no 12225367Sahrens * window where the dataset is closed and inconsistent. 12235367Sahrens */ 12245367Sahrens ds->ds_user_evict_func(ds, ds->ds_user_ptr); 12255367Sahrens ds->ds_user_ptr = NULL; 12264967Sperrin } 12274935Sperrin 1228789Sahrens /* Zero out the deadlist. */ 1229789Sahrens bplist_close(&ds->ds_deadlist); 1230789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1231789Sahrens ds->ds_phys->ds_deadlist_obj = 1232789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 12331544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 12341544Seschrock ds->ds_phys->ds_deadlist_obj)); 1235789Sahrens 1236789Sahrens { 1237789Sahrens /* Free blkptrs that we gave birth to */ 1238789Sahrens zio_t *zio; 12395378Sck153898 int64_t used = 0, compressed = 0, uncompressed = 0; 1240789Sahrens struct killarg ka; 12415518Sck153898 int64_t delta; 1242789Sahrens 1243789Sahrens zio = zio_root(tx->tx_pool->dp_spa, NULL, NULL, 1244789Sahrens ZIO_FLAG_MUSTSUCCEED); 1245789Sahrens ka.usedp = &used; 1246789Sahrens ka.compressedp = &compressed; 1247789Sahrens ka.uncompressedp = &uncompressed; 1248789Sahrens ka.zio = zio; 1249789Sahrens ka.tx = tx; 1250789Sahrens (void) traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1251789Sahrens ADVANCE_POST, kill_blkptr, &ka); 1252789Sahrens (void) zio_wait(zio); 1253789Sahrens 12545518Sck153898 /* only deduct space beyond any refreservation */ 12555518Sck153898 delta = parent_delta(ds, -used); 12562199Sahrens dsl_dir_diduse_space(ds->ds_dir, 12575518Sck153898 delta, -compressed, -uncompressed, tx); 1258789Sahrens } 1259789Sahrens 12605367Sahrens if (ds->ds_prev) { 12615367Sahrens /* Change our contents to that of the prev snapshot */ 12625367Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 12635367Sahrens ds->ds_phys->ds_prev_snap_obj); 12645367Sahrens ds->ds_phys->ds_bp = ds->ds_prev->ds_phys->ds_bp; 12655367Sahrens ds->ds_phys->ds_used_bytes = 12665367Sahrens ds->ds_prev->ds_phys->ds_used_bytes; 12675367Sahrens ds->ds_phys->ds_compressed_bytes = 12685367Sahrens ds->ds_prev->ds_phys->ds_compressed_bytes; 12695367Sahrens ds->ds_phys->ds_uncompressed_bytes = 12705367Sahrens ds->ds_prev->ds_phys->ds_uncompressed_bytes; 12715367Sahrens ds->ds_phys->ds_flags = ds->ds_prev->ds_phys->ds_flags; 12725367Sahrens ds->ds_phys->ds_unique_bytes = 0; 1273789Sahrens 12745367Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 12755367Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 12765367Sahrens ds->ds_prev->ds_phys->ds_unique_bytes = 0; 12775367Sahrens } 12785367Sahrens } else { 12795367Sahrens /* Zero out our contents, recreate objset */ 12805367Sahrens bzero(&ds->ds_phys->ds_bp, sizeof (blkptr_t)); 12815367Sahrens ds->ds_phys->ds_used_bytes = 0; 12825367Sahrens ds->ds_phys->ds_compressed_bytes = 0; 12835367Sahrens ds->ds_phys->ds_uncompressed_bytes = 0; 12845367Sahrens ds->ds_phys->ds_flags = 0; 12855367Sahrens ds->ds_phys->ds_unique_bytes = 0; 12865367Sahrens (void) dmu_objset_create_impl(ds->ds_dir->dd_pool->dp_spa, ds, 12875367Sahrens &ds->ds_phys->ds_bp, *ost, tx); 12882532Sahrens } 12894543Smarks 12904543Smarks spa_history_internal_log(LOG_DS_ROLLBACK, ds->ds_dir->dd_pool->dp_spa, 12914543Smarks tx, cr, "dataset = %llu", ds->ds_object); 1292789Sahrens } 1293789Sahrens 12941731Sbonwick /* ARGSUSED */ 12951731Sbonwick static int 12962199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 12971731Sbonwick { 12982199Sahrens dsl_dataset_t *ds = arg1; 12995367Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 13005367Sahrens uint64_t count; 13015367Sahrens int err; 13021731Sbonwick 13031731Sbonwick /* 13041731Sbonwick * Can't delete a head dataset if there are snapshots of it. 13051731Sbonwick * (Except if the only snapshots are from the branch we cloned 13061731Sbonwick * from.) 13071731Sbonwick */ 13081731Sbonwick if (ds->ds_prev != NULL && 13091731Sbonwick ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 13101731Sbonwick return (EINVAL); 13111731Sbonwick 13125367Sahrens /* 13135367Sahrens * This is really a dsl_dir thing, but check it here so that 13145367Sahrens * we'll be less likely to leave this dataset inconsistent & 13155367Sahrens * nearly destroyed. 13165367Sahrens */ 13175367Sahrens err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 13185367Sahrens if (err) 13195367Sahrens return (err); 13205367Sahrens if (count != 0) 13215367Sahrens return (EEXIST); 13225367Sahrens 13231731Sbonwick return (0); 13241731Sbonwick } 13251731Sbonwick 13262199Sahrens /* ARGSUSED */ 13272199Sahrens static void 13284543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1329789Sahrens { 13302199Sahrens dsl_dataset_t *ds = arg1; 13314543Smarks dsl_pool_t *dp = ds->ds_dir->dd_pool; 1332789Sahrens 13332199Sahrens /* Mark it as inconsistent on-disk, in case we crash */ 13342199Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13352199Sahrens ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 13364543Smarks 13374543Smarks spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 13384543Smarks cr, "dataset = %llu", ds->ds_object); 13392199Sahrens } 1340789Sahrens 13412199Sahrens /* ARGSUSED */ 13425367Sahrens int 13432199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 13442199Sahrens { 13452199Sahrens dsl_dataset_t *ds = arg1; 1346789Sahrens 1347*6689Smaybee /* we have an owner hold, so noone else can destroy us */ 1348*6689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 1349*6689Smaybee 1350789Sahrens /* Can't delete a branch point. */ 13512199Sahrens if (ds->ds_phys->ds_num_children > 1) 13522199Sahrens return (EEXIST); 1353789Sahrens 1354789Sahrens /* 1355789Sahrens * Can't delete a head dataset if there are snapshots of it. 1356789Sahrens * (Except if the only snapshots are from the branch we cloned 1357789Sahrens * from.) 1358789Sahrens */ 1359789Sahrens if (ds->ds_prev != NULL && 13602199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 1361789Sahrens return (EINVAL); 1362789Sahrens 1363789Sahrens /* 1364789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1365789Sahrens * them. Try again. 1366789Sahrens */ 13672199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1368789Sahrens return (EAGAIN); 13692199Sahrens 13702199Sahrens /* XXX we should do some i/o error checking... */ 13712199Sahrens return (0); 13722199Sahrens } 13732199Sahrens 1374*6689Smaybee struct refsarg { 1375*6689Smaybee kmutex_t lock; 1376*6689Smaybee boolean_t gone; 1377*6689Smaybee kcondvar_t cv; 1378*6689Smaybee }; 1379*6689Smaybee 1380*6689Smaybee /* ARGSUSED */ 1381*6689Smaybee static void 1382*6689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 1383*6689Smaybee { 1384*6689Smaybee struct refsarg *arg = argv; 1385*6689Smaybee 1386*6689Smaybee mutex_enter(&arg->lock); 1387*6689Smaybee arg->gone = TRUE; 1388*6689Smaybee cv_signal(&arg->cv); 1389*6689Smaybee mutex_exit(&arg->lock); 1390*6689Smaybee } 1391*6689Smaybee 1392*6689Smaybee static void 1393*6689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 1394*6689Smaybee { 1395*6689Smaybee struct refsarg arg; 1396*6689Smaybee 1397*6689Smaybee mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 1398*6689Smaybee cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 1399*6689Smaybee arg.gone = FALSE; 1400*6689Smaybee (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 1401*6689Smaybee dsl_dataset_refs_gone); 1402*6689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 1403*6689Smaybee mutex_enter(&arg.lock); 1404*6689Smaybee while (!arg.gone) 1405*6689Smaybee cv_wait(&arg.cv, &arg.lock); 1406*6689Smaybee ASSERT(arg.gone); 1407*6689Smaybee mutex_exit(&arg.lock); 1408*6689Smaybee ds->ds_dbuf = NULL; 1409*6689Smaybee ds->ds_phys = NULL; 1410*6689Smaybee mutex_destroy(&arg.lock); 1411*6689Smaybee cv_destroy(&arg.cv); 1412*6689Smaybee } 1413*6689Smaybee 14145367Sahrens void 14154543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 14162199Sahrens { 14172199Sahrens dsl_dataset_t *ds = arg1; 14185378Sck153898 int64_t used = 0, compressed = 0, uncompressed = 0; 14192199Sahrens zio_t *zio; 14202199Sahrens int err; 14212199Sahrens int after_branch_point = FALSE; 14222199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 14232199Sahrens objset_t *mos = dp->dp_meta_objset; 14242199Sahrens dsl_dataset_t *ds_prev = NULL; 14252199Sahrens uint64_t obj; 14262199Sahrens 1427*6689Smaybee ASSERT(ds->ds_owner); 14282199Sahrens ASSERT3U(ds->ds_phys->ds_num_children, <=, 1); 14292199Sahrens ASSERT(ds->ds_prev == NULL || 14302199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 14312199Sahrens ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 14322199Sahrens 1433*6689Smaybee /* signal any waiters that this dataset is going away */ 1434*6689Smaybee mutex_enter(&ds->ds_lock); 1435*6689Smaybee ds->ds_owner = dsl_reaper; 1436*6689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 1437*6689Smaybee mutex_exit(&ds->ds_lock); 1438*6689Smaybee 14395378Sck153898 /* Remove our reservation */ 14405378Sck153898 if (ds->ds_reserved != 0) { 14415378Sck153898 uint64_t val = 0; 14425378Sck153898 dsl_dataset_set_reservation_sync(ds, &val, cr, tx); 14435378Sck153898 ASSERT3U(ds->ds_reserved, ==, 0); 14445378Sck153898 } 14455378Sck153898 14462199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 14472199Sahrens 14482199Sahrens obj = ds->ds_object; 1449789Sahrens 1450789Sahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 1451789Sahrens if (ds->ds_prev) { 1452789Sahrens ds_prev = ds->ds_prev; 1453789Sahrens } else { 1454*6689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 1455*6689Smaybee ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1456789Sahrens } 1457789Sahrens after_branch_point = 1458789Sahrens (ds_prev->ds_phys->ds_next_snap_obj != obj); 1459789Sahrens 1460789Sahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1461789Sahrens if (after_branch_point && 1462789Sahrens ds->ds_phys->ds_next_snap_obj == 0) { 1463789Sahrens /* This clone is toast. */ 1464789Sahrens ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1465789Sahrens ds_prev->ds_phys->ds_num_children--; 1466789Sahrens } else if (!after_branch_point) { 1467789Sahrens ds_prev->ds_phys->ds_next_snap_obj = 1468789Sahrens ds->ds_phys->ds_next_snap_obj; 1469789Sahrens } 1470789Sahrens } 1471789Sahrens 1472789Sahrens zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED); 1473789Sahrens 1474789Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 14752199Sahrens blkptr_t bp; 1476789Sahrens dsl_dataset_t *ds_next; 1477789Sahrens uint64_t itor = 0; 14785378Sck153898 uint64_t old_unique; 1479789Sahrens 1480789Sahrens spa_scrub_restart(dp->dp_spa, tx->tx_txg); 1481789Sahrens 1482*6689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 1483*6689Smaybee ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1484789Sahrens ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1485789Sahrens 14865378Sck153898 old_unique = dsl_dataset_unique(ds_next); 14875378Sck153898 1488789Sahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1489789Sahrens ds_next->ds_phys->ds_prev_snap_obj = 1490789Sahrens ds->ds_phys->ds_prev_snap_obj; 1491789Sahrens ds_next->ds_phys->ds_prev_snap_txg = 1492789Sahrens ds->ds_phys->ds_prev_snap_txg; 1493789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1494789Sahrens ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1495789Sahrens 1496789Sahrens /* 1497789Sahrens * Transfer to our deadlist (which will become next's 1498789Sahrens * new deadlist) any entries from next's current 1499789Sahrens * deadlist which were born before prev, and free the 1500789Sahrens * other entries. 1501789Sahrens * 1502789Sahrens * XXX we're doing this long task with the config lock held 1503789Sahrens */ 1504*6689Smaybee while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1505789Sahrens if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 15061544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 15071544Seschrock &bp, tx)); 1508789Sahrens if (ds_prev && !after_branch_point && 1509789Sahrens bp.blk_birth > 1510789Sahrens ds_prev->ds_phys->ds_prev_snap_txg) { 1511789Sahrens ds_prev->ds_phys->ds_unique_bytes += 15122082Seschrock bp_get_dasize(dp->dp_spa, &bp); 1513789Sahrens } 1514789Sahrens } else { 15152082Seschrock used += bp_get_dasize(dp->dp_spa, &bp); 1516789Sahrens compressed += BP_GET_PSIZE(&bp); 1517789Sahrens uncompressed += BP_GET_UCSIZE(&bp); 1518789Sahrens /* XXX check return value? */ 1519789Sahrens (void) arc_free(zio, dp->dp_spa, tx->tx_txg, 1520789Sahrens &bp, NULL, NULL, ARC_NOWAIT); 1521789Sahrens } 1522789Sahrens } 1523789Sahrens 1524789Sahrens /* free next's deadlist */ 1525789Sahrens bplist_close(&ds_next->ds_deadlist); 1526789Sahrens bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1527789Sahrens 1528789Sahrens /* set next's deadlist to our deadlist */ 1529*6689Smaybee bplist_close(&ds->ds_deadlist); 1530789Sahrens ds_next->ds_phys->ds_deadlist_obj = 1531789Sahrens ds->ds_phys->ds_deadlist_obj; 15321544Seschrock VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 15331544Seschrock ds_next->ds_phys->ds_deadlist_obj)); 1534789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1535789Sahrens 1536789Sahrens if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1537789Sahrens /* 1538789Sahrens * Update next's unique to include blocks which 1539789Sahrens * were previously shared by only this snapshot 1540789Sahrens * and it. Those blocks will be born after the 1541789Sahrens * prev snap and before this snap, and will have 1542789Sahrens * died after the next snap and before the one 1543789Sahrens * after that (ie. be on the snap after next's 1544789Sahrens * deadlist). 1545789Sahrens * 1546789Sahrens * XXX we're doing this long task with the 1547789Sahrens * config lock held 1548789Sahrens */ 1549789Sahrens dsl_dataset_t *ds_after_next; 1550789Sahrens 1551*6689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 1552*6689Smaybee ds_next->ds_phys->ds_next_snap_obj, 1553*6689Smaybee FTAG, &ds_after_next)); 1554789Sahrens itor = 0; 1555789Sahrens while (bplist_iterate(&ds_after_next->ds_deadlist, 1556789Sahrens &itor, &bp) == 0) { 1557789Sahrens if (bp.blk_birth > 1558789Sahrens ds->ds_phys->ds_prev_snap_txg && 1559789Sahrens bp.blk_birth <= 1560789Sahrens ds->ds_phys->ds_creation_txg) { 1561789Sahrens ds_next->ds_phys->ds_unique_bytes += 15622082Seschrock bp_get_dasize(dp->dp_spa, &bp); 1563789Sahrens } 1564789Sahrens } 1565789Sahrens 1566*6689Smaybee dsl_dataset_rele(ds_after_next, FTAG); 1567789Sahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 1568789Sahrens } else { 1569789Sahrens ASSERT3P(ds_next->ds_prev, ==, ds); 1570*6689Smaybee dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 1571*6689Smaybee ds_next->ds_prev = NULL; 1572789Sahrens if (ds_prev) { 1573*6689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 1574*6689Smaybee ds->ds_phys->ds_prev_snap_obj, 1575*6689Smaybee ds_next, &ds_next->ds_prev)); 1576789Sahrens } 15775378Sck153898 15785378Sck153898 dsl_dataset_recalc_head_uniq(ds_next); 15795378Sck153898 15805378Sck153898 /* 15815378Sck153898 * Reduce the amount of our unconsmed refreservation 15825378Sck153898 * being charged to our parent by the amount of 15835378Sck153898 * new unique data we have gained. 15845378Sck153898 */ 15855378Sck153898 if (old_unique < ds_next->ds_reserved) { 15865378Sck153898 int64_t mrsdelta; 15875378Sck153898 uint64_t new_unique = 15885378Sck153898 ds_next->ds_phys->ds_unique_bytes; 15895378Sck153898 15905378Sck153898 ASSERT(old_unique <= new_unique); 15915378Sck153898 mrsdelta = MIN(new_unique - old_unique, 15925378Sck153898 ds_next->ds_reserved - old_unique); 15935378Sck153898 dsl_dir_diduse_space(ds->ds_dir, -mrsdelta, 15945378Sck153898 0, 0, tx); 15955378Sck153898 } 1596789Sahrens } 1597*6689Smaybee dsl_dataset_rele(ds_next, FTAG); 1598789Sahrens 1599789Sahrens /* 16005378Sck153898 * NB: unique_bytes might not be accurate for the head objset. 16015378Sck153898 * Before SPA_VERSION 9, we didn't update its value when we 16025378Sck153898 * deleted the most recent snapshot. 1603789Sahrens */ 1604789Sahrens ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 1605789Sahrens } else { 1606789Sahrens /* 1607789Sahrens * There's no next snapshot, so this is a head dataset. 1608789Sahrens * Destroy the deadlist. Unless it's a clone, the 1609789Sahrens * deadlist should be empty. (If it's a clone, it's 1610789Sahrens * safe to ignore the deadlist contents.) 1611789Sahrens */ 1612789Sahrens struct killarg ka; 1613789Sahrens 1614789Sahrens ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1615789Sahrens bplist_close(&ds->ds_deadlist); 1616789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1617789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1618789Sahrens 1619789Sahrens /* 1620789Sahrens * Free everything that we point to (that's born after 1621789Sahrens * the previous snapshot, if we are a clone) 1622789Sahrens * 1623789Sahrens * XXX we're doing this long task with the config lock held 1624789Sahrens */ 1625789Sahrens ka.usedp = &used; 1626789Sahrens ka.compressedp = &compressed; 1627789Sahrens ka.uncompressedp = &uncompressed; 1628789Sahrens ka.zio = zio; 1629789Sahrens ka.tx = tx; 1630789Sahrens err = traverse_dsl_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 1631789Sahrens ADVANCE_POST, kill_blkptr, &ka); 1632789Sahrens ASSERT3U(err, ==, 0); 16335378Sck153898 ASSERT(spa_version(dp->dp_spa) < 16345378Sck153898 SPA_VERSION_UNIQUE_ACCURATE || 16355378Sck153898 used == ds->ds_phys->ds_unique_bytes); 1636789Sahrens } 1637789Sahrens 1638789Sahrens err = zio_wait(zio); 1639789Sahrens ASSERT3U(err, ==, 0); 1640789Sahrens 16412199Sahrens dsl_dir_diduse_space(ds->ds_dir, -used, -compressed, -uncompressed, tx); 1642789Sahrens 1643*6689Smaybee if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 1644*6689Smaybee /* Erase the link in the dir */ 1645*6689Smaybee dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 1646*6689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 1647*6689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1648789Sahrens err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1649789Sahrens ASSERT(err == 0); 1650789Sahrens } else { 1651789Sahrens /* remove from snapshot namespace */ 1652789Sahrens dsl_dataset_t *ds_head; 1653*6689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 1654*6689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 1655*6689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 16562207Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 1657789Sahrens #ifdef ZFS_DEBUG 1658789Sahrens { 1659789Sahrens uint64_t val; 16606492Stimh 1661*6689Smaybee err = dsl_dataset_snap_lookup(ds_head, 16626492Stimh ds->ds_snapname, &val); 1663789Sahrens ASSERT3U(err, ==, 0); 1664789Sahrens ASSERT3U(val, ==, obj); 1665789Sahrens } 1666789Sahrens #endif 1667*6689Smaybee err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1668789Sahrens ASSERT(err == 0); 1669*6689Smaybee dsl_dataset_rele(ds_head, FTAG); 1670789Sahrens } 1671789Sahrens 1672789Sahrens if (ds_prev && ds->ds_prev != ds_prev) 1673*6689Smaybee dsl_dataset_rele(ds_prev, FTAG); 1674789Sahrens 16755094Slling spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 16764543Smarks spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 16774543Smarks cr, "dataset = %llu", ds->ds_object); 16784543Smarks 1679*6689Smaybee dsl_dir_close(ds->ds_dir, ds); 1680*6689Smaybee ds->ds_dir = NULL; 1681*6689Smaybee dsl_dataset_drain_refs(ds, tag); 16822199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 16832199Sahrens } 16842199Sahrens 16855378Sck153898 static int 16865378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 16875378Sck153898 { 16885378Sck153898 uint64_t asize; 16895378Sck153898 16905378Sck153898 if (!dmu_tx_is_syncing(tx)) 16915378Sck153898 return (0); 16925378Sck153898 16935378Sck153898 /* 16945378Sck153898 * If there's an fs-only reservation, any blocks that might become 16955378Sck153898 * owned by the snapshot dataset must be accommodated by space 16965378Sck153898 * outside of the reservation. 16975378Sck153898 */ 16985378Sck153898 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 16995378Sck153898 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 17005378Sck153898 return (ENOSPC); 17015378Sck153898 17025378Sck153898 /* 17035378Sck153898 * Propogate any reserved space for this snapshot to other 17045378Sck153898 * snapshot checks in this sync group. 17055378Sck153898 */ 17065378Sck153898 if (asize > 0) 17075378Sck153898 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 17085378Sck153898 17095378Sck153898 return (0); 17105378Sck153898 } 17115378Sck153898 17122199Sahrens /* ARGSUSED */ 17132199Sahrens int 17142199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 17152199Sahrens { 17165367Sahrens dsl_dataset_t *ds = arg1; 17172199Sahrens const char *snapname = arg2; 17182199Sahrens int err; 17192199Sahrens uint64_t value; 1720789Sahrens 1721789Sahrens /* 17222199Sahrens * We don't allow multiple snapshots of the same txg. If there 17232199Sahrens * is already one, try again. 17242199Sahrens */ 17252199Sahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 17262199Sahrens return (EAGAIN); 17272199Sahrens 17282199Sahrens /* 17292199Sahrens * Check for conflicting name snapshot name. 1730789Sahrens */ 1731*6689Smaybee err = dsl_dataset_snap_lookup(ds, snapname, &value); 17322199Sahrens if (err == 0) 17332199Sahrens return (EEXIST); 17342199Sahrens if (err != ENOENT) 17352199Sahrens return (err); 1736789Sahrens 17373978Smmusante /* 17383978Smmusante * Check that the dataset's name is not too long. Name consists 17393978Smmusante * of the dataset's length + 1 for the @-sign + snapshot name's length 17403978Smmusante */ 17413978Smmusante if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 17423978Smmusante return (ENAMETOOLONG); 17433978Smmusante 17445378Sck153898 err = dsl_dataset_snapshot_reserve_space(ds, tx); 17455378Sck153898 if (err) 17465378Sck153898 return (err); 17475378Sck153898 17482199Sahrens ds->ds_trysnap_txg = tx->tx_txg; 1749789Sahrens return (0); 1750789Sahrens } 1751789Sahrens 17522199Sahrens void 17534543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1754789Sahrens { 17555367Sahrens dsl_dataset_t *ds = arg1; 17562199Sahrens const char *snapname = arg2; 17572199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1758789Sahrens dmu_buf_t *dbuf; 1759789Sahrens dsl_dataset_phys_t *dsphys; 17602199Sahrens uint64_t dsobj; 1761789Sahrens objset_t *mos = dp->dp_meta_objset; 1762789Sahrens int err; 1763789Sahrens 1764789Sahrens spa_scrub_restart(dp->dp_spa, tx->tx_txg); 17652199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1766789Sahrens 1767928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1768928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 17691544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1770789Sahrens dmu_buf_will_dirty(dbuf, tx); 1771789Sahrens dsphys = dbuf->db_data; 1772*6689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 17732199Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1774789Sahrens dsphys->ds_fsid_guid = unique_create(); 1775789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1776789Sahrens sizeof (dsphys->ds_guid)); 1777789Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1778789Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1779789Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1780789Sahrens dsphys->ds_num_children = 1; 1781789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1782789Sahrens dsphys->ds_creation_txg = tx->tx_txg; 1783789Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1784789Sahrens dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1785789Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1786789Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 17872082Seschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1788789Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 17891544Seschrock dmu_buf_rele(dbuf, FTAG); 1790789Sahrens 17912199Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 17922199Sahrens if (ds->ds_prev) { 17932199Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1794789Sahrens ds->ds_object || 17952199Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 17962199Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 17972199Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1798789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 17992199Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 18002199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 1801789Sahrens } 1802789Sahrens } 1803789Sahrens 18045378Sck153898 /* 18055378Sck153898 * If we have a reference-reservation on this dataset, we will 18065378Sck153898 * need to increase the amount of refreservation being charged 18075378Sck153898 * since our unique space is going to zero. 18085378Sck153898 */ 18095378Sck153898 if (ds->ds_reserved) { 18105378Sck153898 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 18115378Sck153898 dsl_dir_diduse_space(ds->ds_dir, add, 0, 0, tx); 18125378Sck153898 } 18135378Sck153898 1814789Sahrens bplist_close(&ds->ds_deadlist); 1815789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 18165712Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1817789Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 18185712Sahrens ds->ds_phys->ds_prev_snap_txg = tx->tx_txg; 1819789Sahrens ds->ds_phys->ds_unique_bytes = 0; 18205378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 18215378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1822789Sahrens ds->ds_phys->ds_deadlist_obj = 1823789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 18241544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 18251544Seschrock ds->ds_phys->ds_deadlist_obj)); 1826789Sahrens 1827789Sahrens dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1828789Sahrens err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1829789Sahrens snapname, 8, 1, &dsobj, tx); 1830789Sahrens ASSERT(err == 0); 1831789Sahrens 1832789Sahrens if (ds->ds_prev) 1833*6689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 1834*6689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 1835*6689Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 18364543Smarks 18374543Smarks spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 18384603Sahrens "dataset = %llu", dsobj); 1839789Sahrens } 1840789Sahrens 1841789Sahrens void 18423547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1843789Sahrens { 1844789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1845789Sahrens ASSERT(ds->ds_user_ptr != NULL); 1846789Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1847789Sahrens 18484787Sahrens /* 18494787Sahrens * in case we had to change ds_fsid_guid when we opened it, 18504787Sahrens * sync it out now. 18514787Sahrens */ 18524787Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 18534787Sahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 18544787Sahrens 1855789Sahrens dsl_dir_dirty(ds->ds_dir, tx); 18563547Smaybee dmu_objset_sync(ds->ds_user_ptr, zio, tx); 1857789Sahrens } 1858789Sahrens 1859789Sahrens void 18602885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1861789Sahrens { 18625378Sck153898 uint64_t refd, avail, uobjs, aobjs; 18635378Sck153898 18642885Sahrens dsl_dir_stats(ds->ds_dir, nv); 1865789Sahrens 18665378Sck153898 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 18675378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 18685378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 18695378Sck153898 18702885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 18712885Sahrens ds->ds_phys->ds_creation_time); 18722885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 18732885Sahrens ds->ds_phys->ds_creation_txg); 18745378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 18755378Sck153898 ds->ds_quota); 18765378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 18775378Sck153898 ds->ds_reserved); 18786643Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 18796643Seschrock ds->ds_phys->ds_guid); 1880789Sahrens 1881789Sahrens if (ds->ds_phys->ds_next_snap_obj) { 1882789Sahrens /* 1883789Sahrens * This is a snapshot; override the dd's space used with 18842885Sahrens * our unique space and compression ratio. 1885789Sahrens */ 18862885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 18872885Sahrens ds->ds_phys->ds_unique_bytes); 18882885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 18892885Sahrens ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 18902885Sahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 18912885Sahrens ds->ds_phys->ds_compressed_bytes)); 1892789Sahrens } 1893789Sahrens } 1894789Sahrens 18952885Sahrens void 18962885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1897789Sahrens { 18982885Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 18992885Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 19005367Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 19012885Sahrens if (ds->ds_phys->ds_next_snap_obj) { 19022885Sahrens stat->dds_is_snapshot = B_TRUE; 19032885Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 19042885Sahrens } 19052885Sahrens 19062885Sahrens /* clone origin is really a dsl_dir thing... */ 19075446Sahrens rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 19085367Sahrens if (ds->ds_dir->dd_phys->dd_origin_obj) { 19092885Sahrens dsl_dataset_t *ods; 19102885Sahrens 1911*6689Smaybee VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 1912*6689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 19135367Sahrens dsl_dataset_name(ods, stat->dds_origin); 1914*6689Smaybee dsl_dataset_drop_ref(ods, FTAG); 19152885Sahrens } 19165446Sahrens rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 19172885Sahrens } 19182885Sahrens 19192885Sahrens uint64_t 19202885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 19212885Sahrens { 19224787Sahrens return (ds->ds_fsid_guid); 19232885Sahrens } 19242885Sahrens 19252885Sahrens void 19262885Sahrens dsl_dataset_space(dsl_dataset_t *ds, 19272885Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 19282885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 19292885Sahrens { 19302885Sahrens *refdbytesp = ds->ds_phys->ds_used_bytes; 19312885Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 19325378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 19335378Sck153898 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 19345378Sck153898 if (ds->ds_quota != 0) { 19355378Sck153898 /* 19365378Sck153898 * Adjust available bytes according to refquota 19375378Sck153898 */ 19385378Sck153898 if (*refdbytesp < ds->ds_quota) 19395378Sck153898 *availbytesp = MIN(*availbytesp, 19405378Sck153898 ds->ds_quota - *refdbytesp); 19415378Sck153898 else 19425378Sck153898 *availbytesp = 0; 19435378Sck153898 } 19442885Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 19452885Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1946789Sahrens } 1947789Sahrens 19485326Sek110237 boolean_t 19495326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 19505326Sek110237 { 19515326Sek110237 dsl_pool_t *dp = ds->ds_dir->dd_pool; 19525326Sek110237 19535326Sek110237 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 19545326Sek110237 dsl_pool_sync_context(dp)); 19555326Sek110237 if (ds->ds_prev == NULL) 19565326Sek110237 return (B_FALSE); 19575326Sek110237 if (ds->ds_phys->ds_bp.blk_birth > 19585326Sek110237 ds->ds_prev->ds_phys->ds_creation_txg) 19595326Sek110237 return (B_TRUE); 19605326Sek110237 return (B_FALSE); 19615326Sek110237 } 19625326Sek110237 19632199Sahrens /* ARGSUSED */ 1964789Sahrens static int 19652199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 1966789Sahrens { 19672199Sahrens dsl_dataset_t *ds = arg1; 19682199Sahrens char *newsnapname = arg2; 19692199Sahrens dsl_dir_t *dd = ds->ds_dir; 19702199Sahrens dsl_dataset_t *hds; 19712199Sahrens uint64_t val; 1972789Sahrens int err; 1973789Sahrens 1974*6689Smaybee err = dsl_dataset_hold_obj(dd->dd_pool, 1975*6689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 1976789Sahrens if (err) 1977789Sahrens return (err); 1978789Sahrens 19792199Sahrens /* new name better not be in use */ 1980*6689Smaybee err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 1981*6689Smaybee dsl_dataset_rele(hds, FTAG); 1982789Sahrens 19832199Sahrens if (err == 0) 19842199Sahrens err = EEXIST; 19852199Sahrens else if (err == ENOENT) 19862199Sahrens err = 0; 19874007Smmusante 19884007Smmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 19894007Smmusante if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 19904007Smmusante err = ENAMETOOLONG; 19914007Smmusante 19922199Sahrens return (err); 19932199Sahrens } 1994789Sahrens 19952199Sahrens static void 19964543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 19974543Smarks cred_t *cr, dmu_tx_t *tx) 19982199Sahrens { 19992199Sahrens dsl_dataset_t *ds = arg1; 20004543Smarks const char *newsnapname = arg2; 20012199Sahrens dsl_dir_t *dd = ds->ds_dir; 20022199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 20032199Sahrens dsl_dataset_t *hds; 20042199Sahrens int err; 2005789Sahrens 20062199Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2007789Sahrens 2008*6689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 2009*6689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2010789Sahrens 20112199Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 2012*6689Smaybee err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2013789Sahrens ASSERT3U(err, ==, 0); 20142199Sahrens mutex_enter(&ds->ds_lock); 20152199Sahrens (void) strcpy(ds->ds_snapname, newsnapname); 20162199Sahrens mutex_exit(&ds->ds_lock); 20172199Sahrens err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 20182199Sahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2019789Sahrens ASSERT3U(err, ==, 0); 2020789Sahrens 20214543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 20224543Smarks cr, "dataset = %llu", ds->ds_object); 2023*6689Smaybee dsl_dataset_rele(hds, FTAG); 2024789Sahrens } 2025789Sahrens 20265326Sek110237 struct renamesnaparg { 20274007Smmusante dsl_sync_task_group_t *dstg; 20284007Smmusante char failed[MAXPATHLEN]; 20294007Smmusante char *oldsnap; 20304007Smmusante char *newsnap; 20314007Smmusante }; 20324007Smmusante 20334007Smmusante static int 20344007Smmusante dsl_snapshot_rename_one(char *name, void *arg) 20354007Smmusante { 20365326Sek110237 struct renamesnaparg *ra = arg; 20374007Smmusante dsl_dataset_t *ds = NULL; 20384007Smmusante char *cp; 20394007Smmusante int err; 20404007Smmusante 20414007Smmusante cp = name + strlen(name); 20424007Smmusante *cp = '@'; 20434007Smmusante (void) strcpy(cp + 1, ra->oldsnap); 20444543Smarks 20454543Smarks /* 20464543Smarks * For recursive snapshot renames the parent won't be changing 20474543Smarks * so we just pass name for both the to/from argument. 20484543Smarks */ 20494543Smarks if (err = zfs_secpolicy_rename_perms(name, name, CRED())) { 20504543Smarks (void) strcpy(ra->failed, name); 20514543Smarks return (err); 20524543Smarks } 20534543Smarks 2054*6689Smaybee #ifdef _KERNEL 2055*6689Smaybee /* 2056*6689Smaybee * For all filesystems undergoing rename, we'll need to unmount it. 2057*6689Smaybee */ 2058*6689Smaybee (void) zfs_unmount_snap(name, NULL); 2059*6689Smaybee #endif 2060*6689Smaybee err = dsl_dataset_hold(name, ra->dstg, &ds); 2061*6689Smaybee *cp = '\0'; 20624007Smmusante if (err == ENOENT) { 20634007Smmusante return (0); 2064*6689Smaybee } else if (err) { 20654007Smmusante (void) strcpy(ra->failed, name); 20664007Smmusante return (err); 20674007Smmusante } 20684007Smmusante 20694007Smmusante dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 20704007Smmusante dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 20714007Smmusante 20724007Smmusante return (0); 20734007Smmusante } 20744007Smmusante 20754007Smmusante static int 20764007Smmusante dsl_recursive_rename(char *oldname, const char *newname) 20774007Smmusante { 20784007Smmusante int err; 20795326Sek110237 struct renamesnaparg *ra; 20804007Smmusante dsl_sync_task_t *dst; 20814007Smmusante spa_t *spa; 20824007Smmusante char *cp, *fsname = spa_strdup(oldname); 20834007Smmusante int len = strlen(oldname); 20844007Smmusante 20854007Smmusante /* truncate the snapshot name to get the fsname */ 20864007Smmusante cp = strchr(fsname, '@'); 20874007Smmusante *cp = '\0'; 20884007Smmusante 20894603Sahrens err = spa_open(fsname, &spa, FTAG); 20904007Smmusante if (err) { 20914007Smmusante kmem_free(fsname, len + 1); 20924007Smmusante return (err); 20934007Smmusante } 20945326Sek110237 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 20954007Smmusante ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 20964007Smmusante 20974007Smmusante ra->oldsnap = strchr(oldname, '@') + 1; 20984007Smmusante ra->newsnap = strchr(newname, '@') + 1; 20994007Smmusante *ra->failed = '\0'; 21004007Smmusante 21014007Smmusante err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 21024007Smmusante DS_FIND_CHILDREN); 21034007Smmusante kmem_free(fsname, len + 1); 21044007Smmusante 21054007Smmusante if (err == 0) { 21064007Smmusante err = dsl_sync_task_group_wait(ra->dstg); 21074007Smmusante } 21084007Smmusante 21094007Smmusante for (dst = list_head(&ra->dstg->dstg_tasks); dst; 21104007Smmusante dst = list_next(&ra->dstg->dstg_tasks, dst)) { 21114007Smmusante dsl_dataset_t *ds = dst->dst_arg1; 21124007Smmusante if (dst->dst_err) { 21134007Smmusante dsl_dir_name(ds->ds_dir, ra->failed); 21144009Smmusante (void) strcat(ra->failed, "@"); 21154009Smmusante (void) strcat(ra->failed, ra->newsnap); 21164007Smmusante } 2117*6689Smaybee dsl_dataset_rele(ds, ra->dstg); 21184007Smmusante } 21194007Smmusante 21204543Smarks if (err) 21214543Smarks (void) strcpy(oldname, ra->failed); 21224007Smmusante 21234007Smmusante dsl_sync_task_group_destroy(ra->dstg); 21245326Sek110237 kmem_free(ra, sizeof (struct renamesnaparg)); 21254007Smmusante spa_close(spa, FTAG); 21264007Smmusante return (err); 21274007Smmusante } 21284007Smmusante 21294569Smmusante static int 21304569Smmusante dsl_valid_rename(char *oldname, void *arg) 21314569Smmusante { 21324569Smmusante int delta = *(int *)arg; 21334569Smmusante 21344569Smmusante if (strlen(oldname) + delta >= MAXNAMELEN) 21354569Smmusante return (ENAMETOOLONG); 21364569Smmusante 21374569Smmusante return (0); 21384569Smmusante } 21394569Smmusante 2140789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename 2141789Sahrens int 2142*6689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2143789Sahrens { 2144789Sahrens dsl_dir_t *dd; 21452199Sahrens dsl_dataset_t *ds; 2146789Sahrens const char *tail; 2147789Sahrens int err; 2148789Sahrens 21492199Sahrens err = dsl_dir_open(oldname, FTAG, &dd, &tail); 21501544Seschrock if (err) 21511544Seschrock return (err); 2152789Sahrens if (tail == NULL) { 21534569Smmusante int delta = strlen(newname) - strlen(oldname); 21544569Smmusante 21554569Smmusante /* if we're growing, validate child size lengths */ 21564569Smmusante if (delta > 0) 21574569Smmusante err = dmu_objset_find(oldname, dsl_valid_rename, 21584569Smmusante &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 21594569Smmusante 21604569Smmusante if (!err) 21614569Smmusante err = dsl_dir_rename(dd, newname); 2162789Sahrens dsl_dir_close(dd, FTAG); 2163789Sahrens return (err); 2164789Sahrens } 2165789Sahrens if (tail[0] != '@') { 2166789Sahrens /* the name ended in a nonexistant component */ 2167789Sahrens dsl_dir_close(dd, FTAG); 2168789Sahrens return (ENOENT); 2169789Sahrens } 2170789Sahrens 21712199Sahrens dsl_dir_close(dd, FTAG); 21722199Sahrens 21732199Sahrens /* new name must be snapshot in same filesystem */ 21742199Sahrens tail = strchr(newname, '@'); 21752199Sahrens if (tail == NULL) 21762199Sahrens return (EINVAL); 21772199Sahrens tail++; 21782199Sahrens if (strncmp(oldname, newname, tail - newname) != 0) 21792199Sahrens return (EXDEV); 2180789Sahrens 21814007Smmusante if (recursive) { 21824007Smmusante err = dsl_recursive_rename(oldname, newname); 21834007Smmusante } else { 2184*6689Smaybee err = dsl_dataset_hold(oldname, FTAG, &ds); 21854007Smmusante if (err) 21864007Smmusante return (err); 21872199Sahrens 21884007Smmusante err = dsl_sync_task_do(ds->ds_dir->dd_pool, 21894007Smmusante dsl_dataset_snapshot_rename_check, 21904007Smmusante dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 21912199Sahrens 2192*6689Smaybee dsl_dataset_rele(ds, FTAG); 21934007Smmusante } 21942199Sahrens 2195789Sahrens return (err); 2196789Sahrens } 21972082Seschrock 2198*6689Smaybee struct promotedsarg { 2199*6689Smaybee list_node_t link; 2200*6689Smaybee dsl_dataset_t *ds; 2201*6689Smaybee }; 2202*6689Smaybee 22032199Sahrens struct promotearg { 2204*6689Smaybee list_t snap_list; 2205*6689Smaybee dsl_dataset_t *clone_origin, *old_head; 22062199Sahrens uint64_t used, comp, uncomp, unique; 2207*6689Smaybee uint64_t newnext_obj; 22082199Sahrens }; 22092199Sahrens 22104543Smarks /* ARGSUSED */ 22112082Seschrock static int 22122199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 22132082Seschrock { 22142199Sahrens dsl_dataset_t *hds = arg1; 22152199Sahrens struct promotearg *pa = arg2; 2216*6689Smaybee struct promotedsarg *snap = list_head(&pa->snap_list); 22172199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 2218*6689Smaybee dsl_dataset_t *origin_ds = snap->ds; 2219*6689Smaybee dsl_dataset_t *newnext_ds; 2220*6689Smaybee char *name; 22212199Sahrens uint64_t itor = 0; 22222082Seschrock blkptr_t bp; 2223*6689Smaybee int err; 22242199Sahrens 22252082Seschrock /* Check that it is a clone */ 2226*6689Smaybee if (hds->ds_dir->dd_phys->dd_origin_obj == 0) 22272082Seschrock return (EINVAL); 22282082Seschrock 22292199Sahrens /* Since this is so expensive, don't do the preliminary check */ 22302199Sahrens if (!dmu_tx_is_syncing(tx)) 22312199Sahrens return (0); 22322199Sahrens 2233*6689Smaybee if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 2234*6689Smaybee return (EXDEV); 22352082Seschrock 22365367Sahrens /* find origin's new next ds */ 2237*6689Smaybee newnext_ds = hds; 22385367Sahrens while (newnext_ds->ds_phys->ds_prev_snap_obj != origin_ds->ds_object) { 22392082Seschrock dsl_dataset_t *prev; 22402082Seschrock 2241*6689Smaybee err = dsl_dataset_hold_obj(dp, 2242*6689Smaybee newnext_ds->ds_phys->ds_prev_snap_obj, FTAG, &prev); 2243*6689Smaybee if (newnext_ds != hds) 2244*6689Smaybee dsl_dataset_rele(newnext_ds, FTAG); 2245*6689Smaybee if (err) 2246*6689Smaybee return (err); 22472082Seschrock newnext_ds = prev; 22482082Seschrock } 22492199Sahrens pa->newnext_obj = newnext_ds->ds_object; 22502082Seschrock 22515367Sahrens /* compute origin's new unique space */ 2252*6689Smaybee pa->unique = 0; 22532082Seschrock while ((err = bplist_iterate(&newnext_ds->ds_deadlist, 22542082Seschrock &itor, &bp)) == 0) { 22555367Sahrens if (bp.blk_birth > origin_ds->ds_phys->ds_prev_snap_txg) 2256*6689Smaybee pa->unique += bp_get_dasize(dp->dp_spa, &bp); 22572082Seschrock } 2258*6689Smaybee if (newnext_ds != hds) 2259*6689Smaybee dsl_dataset_rele(newnext_ds, FTAG); 22602082Seschrock if (err != ENOENT) 2261*6689Smaybee return (err); 2262*6689Smaybee 22632082Seschrock name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2264*6689Smaybee 2265*6689Smaybee /* 2266*6689Smaybee * Walk the snapshots that we are moving 2267*6689Smaybee * 2268*6689Smaybee * Compute space to transfer. Each snapshot gave birth to: 2269*6689Smaybee * (my used) - (prev's used) + (deadlist's used) 2270*6689Smaybee * So a sequence would look like: 2271*6689Smaybee * uN - u(N-1) + dN + ... + u1 - u0 + d1 + u0 - 0 + d0 2272*6689Smaybee * Which simplifies to: 2273*6689Smaybee * uN + dN + ... + d1 + d0 2274*6689Smaybee * Note however, if we stop before we reach the ORIGIN we get: 2275*6689Smaybee * uN + dN + ... + dM - uM-1 2276*6689Smaybee */ 2277*6689Smaybee pa->used = origin_ds->ds_phys->ds_used_bytes; 2278*6689Smaybee pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 2279*6689Smaybee pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 2280*6689Smaybee do { 22812082Seschrock uint64_t val, dlused, dlcomp, dluncomp; 2282*6689Smaybee dsl_dataset_t *ds = snap->ds; 22832082Seschrock 22842082Seschrock /* Check that the snapshot name does not conflict */ 22852082Seschrock dsl_dataset_name(ds, name); 2286*6689Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2287*6689Smaybee if (err == 0) 2288*6689Smaybee err = EEXIST; 2289*6689Smaybee if (err != ENOENT) 22902082Seschrock break; 2291*6689Smaybee err = 0; 2292*6689Smaybee 2293*6689Smaybee /* The very first snapshot does not have a deadlist */ 2294*6689Smaybee if (ds->ds_phys->ds_prev_snap_obj != 0) { 2295*6689Smaybee if (err = bplist_space(&ds->ds_deadlist, 2296*6689Smaybee &dlused, &dlcomp, &dluncomp)) 2297*6689Smaybee break; 2298*6689Smaybee pa->used += dlused; 2299*6689Smaybee pa->comp += dlcomp; 2300*6689Smaybee pa->uncomp += dluncomp; 23012082Seschrock } 2302*6689Smaybee } while (snap = list_next(&pa->snap_list, snap)); 2303*6689Smaybee 2304*6689Smaybee /* 2305*6689Smaybee * If we are a clone of a clone then we never reached ORIGIN, 2306*6689Smaybee * so we need to subtract out the clone origin's used space. 2307*6689Smaybee */ 2308*6689Smaybee if (pa->clone_origin) { 2309*6689Smaybee pa->used -= pa->clone_origin->ds_phys->ds_used_bytes; 2310*6689Smaybee pa->comp -= pa->clone_origin->ds_phys->ds_compressed_bytes; 2311*6689Smaybee pa->uncomp -= pa->clone_origin->ds_phys->ds_uncompressed_bytes; 23122082Seschrock } 23132082Seschrock 2314*6689Smaybee kmem_free(name, MAXPATHLEN); 2315*6689Smaybee 23162082Seschrock /* Check that there is enough space here */ 2317*6689Smaybee if (err == 0) { 2318*6689Smaybee dsl_dir_t *odd = origin_ds->ds_dir; 2319*6689Smaybee err = dsl_dir_transfer_possible(odd, hds->ds_dir, pa->used); 2320*6689Smaybee } 2321*6689Smaybee 23222199Sahrens return (err); 23232199Sahrens } 23242082Seschrock 23252199Sahrens static void 23264543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 23272199Sahrens { 23282199Sahrens dsl_dataset_t *hds = arg1; 23292199Sahrens struct promotearg *pa = arg2; 2330*6689Smaybee struct promotedsarg *snap = list_head(&pa->snap_list); 2331*6689Smaybee dsl_dataset_t *origin_ds = snap->ds; 23322199Sahrens dsl_dir_t *dd = hds->ds_dir; 23332199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 23345367Sahrens dsl_dir_t *odd = NULL; 23352199Sahrens char *name; 23362199Sahrens 23372199Sahrens ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 23382199Sahrens 23392417Sahrens /* 23405367Sahrens * We need to explicitly open odd, since origin_ds's dd will be 23412417Sahrens * changing. 23422417Sahrens */ 23435367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 23445367Sahrens NULL, FTAG, &odd)); 23452082Seschrock 2346*6689Smaybee /* change origin's next snap */ 2347*6689Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2348*6689Smaybee origin_ds->ds_phys->ds_next_snap_obj = pa->newnext_obj; 2349*6689Smaybee 2350*6689Smaybee /* change origin */ 2351*6689Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2352*6689Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 2353*6689Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 2354*6689Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2355*6689Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 2356*6689Smaybee 23572082Seschrock /* move snapshots to this dir */ 23582199Sahrens name = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2359*6689Smaybee do { 2360*6689Smaybee dsl_dataset_t *ds = snap->ds; 23612082Seschrock 23622082Seschrock /* move snap name entry */ 23632082Seschrock dsl_dataset_name(ds, name); 2364*6689Smaybee VERIFY(0 == dsl_dataset_snap_remove(pa->old_head, 2365*6689Smaybee ds->ds_snapname, tx)); 23662199Sahrens VERIFY(0 == zap_add(dp->dp_meta_objset, 23672082Seschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 23682082Seschrock 8, 1, &ds->ds_object, tx)); 23692082Seschrock 23702082Seschrock /* change containing dsl_dir */ 23712082Seschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 23725367Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 23732082Seschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 23745367Sahrens ASSERT3P(ds->ds_dir, ==, odd); 23752082Seschrock dsl_dir_close(ds->ds_dir, ds); 23762199Sahrens VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 23772082Seschrock NULL, ds, &ds->ds_dir)); 23782082Seschrock 23792082Seschrock ASSERT3U(dsl_prop_numcb(ds), ==, 0); 2380*6689Smaybee } while (snap = list_next(&pa->snap_list, snap)); 23812082Seschrock 23822082Seschrock /* change space accounting */ 23835367Sahrens dsl_dir_diduse_space(odd, -pa->used, -pa->comp, -pa->uncomp, tx); 23842199Sahrens dsl_dir_diduse_space(dd, pa->used, pa->comp, pa->uncomp, tx); 23855367Sahrens origin_ds->ds_phys->ds_unique_bytes = pa->unique; 23862082Seschrock 23874543Smarks /* log history record */ 23884543Smarks spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 2389*6689Smaybee cr, "dataset = %llu", hds->ds_object); 23904543Smarks 23915367Sahrens dsl_dir_close(odd, FTAG); 23922199Sahrens kmem_free(name, MAXPATHLEN); 23932082Seschrock } 23942082Seschrock 23952082Seschrock int 23962082Seschrock dsl_dataset_promote(const char *name) 23972082Seschrock { 23982082Seschrock dsl_dataset_t *ds; 2399*6689Smaybee dsl_dir_t *dd; 2400*6689Smaybee dsl_pool_t *dp; 24012082Seschrock dmu_object_info_t doi; 24022199Sahrens struct promotearg pa; 2403*6689Smaybee struct promotedsarg *snap; 2404*6689Smaybee uint64_t snap_obj; 2405*6689Smaybee uint64_t last_snap = 0; 2406*6689Smaybee int err; 2407*6689Smaybee 2408*6689Smaybee err = dsl_dataset_hold(name, FTAG, &ds); 24092082Seschrock if (err) 24102082Seschrock return (err); 2411*6689Smaybee dd = ds->ds_dir; 2412*6689Smaybee dp = dd->dd_pool; 2413*6689Smaybee 2414*6689Smaybee err = dmu_object_info(dp->dp_meta_objset, 24152082Seschrock ds->ds_phys->ds_snapnames_zapobj, &doi); 24162082Seschrock if (err) { 2417*6689Smaybee dsl_dataset_rele(ds, FTAG); 24182082Seschrock return (err); 24192082Seschrock } 24202082Seschrock 24212082Seschrock /* 2422*6689Smaybee * We are going to inherit all the snapshots taken before our 2423*6689Smaybee * origin (i.e., our new origin will be our parent's origin). 2424*6689Smaybee * Take ownership of them so that we can rename them into our 2425*6689Smaybee * namespace. 2426*6689Smaybee */ 2427*6689Smaybee pa.clone_origin = NULL; 2428*6689Smaybee list_create(&pa.snap_list, 2429*6689Smaybee sizeof (struct promotedsarg), offsetof(struct promotedsarg, link)); 2430*6689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 2431*6689Smaybee ASSERT(dd->dd_phys->dd_origin_obj != 0); 2432*6689Smaybee snap_obj = dd->dd_phys->dd_origin_obj; 2433*6689Smaybee while (snap_obj) { 2434*6689Smaybee snap = kmem_alloc(sizeof (struct promotedsarg), KM_SLEEP); 2435*6689Smaybee err = dsl_dataset_own_obj(dp, snap_obj, 0, FTAG, &snap->ds); 2436*6689Smaybee if (err == ENOENT) { 2437*6689Smaybee /* lost race with snapshot destroy */ 2438*6689Smaybee struct promotedsarg *last = list_tail(&pa.snap_list); 2439*6689Smaybee ASSERT(snap_obj != last->ds->ds_phys->ds_prev_snap_obj); 2440*6689Smaybee snap_obj = last->ds->ds_phys->ds_prev_snap_obj; 2441*6689Smaybee kmem_free(snap, sizeof (struct promotedsarg)); 2442*6689Smaybee continue; 2443*6689Smaybee } else if (err) { 2444*6689Smaybee kmem_free(snap, sizeof (struct promotedsarg)); 2445*6689Smaybee rw_exit(&dp->dp_config_rwlock); 2446*6689Smaybee goto out; 2447*6689Smaybee } 2448*6689Smaybee /* 2449*6689Smaybee * We could be a clone of a clone. If we reach our 2450*6689Smaybee * parent's branch point, we're done. 2451*6689Smaybee */ 2452*6689Smaybee if (last_snap && 2453*6689Smaybee snap->ds->ds_phys->ds_next_snap_obj != last_snap) { 2454*6689Smaybee pa.clone_origin = snap->ds; 2455*6689Smaybee kmem_free(snap, sizeof (struct promotedsarg)); 2456*6689Smaybee snap_obj = 0; 2457*6689Smaybee } else { 2458*6689Smaybee list_insert_tail(&pa.snap_list, snap); 2459*6689Smaybee last_snap = snap_obj; 2460*6689Smaybee snap_obj = snap->ds->ds_phys->ds_prev_snap_obj; 2461*6689Smaybee } 2462*6689Smaybee } 2463*6689Smaybee snap = list_head(&pa.snap_list); 2464*6689Smaybee ASSERT(snap != NULL); 2465*6689Smaybee err = dsl_dataset_hold_obj(dp, 2466*6689Smaybee snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &pa.old_head); 2467*6689Smaybee rw_exit(&dp->dp_config_rwlock); 2468*6689Smaybee 2469*6689Smaybee if (err) 2470*6689Smaybee goto out; 2471*6689Smaybee 2472*6689Smaybee /* 24732082Seschrock * Add in 128x the snapnames zapobj size, since we will be moving 24742082Seschrock * a bunch of snapnames to the promoted ds, and dirtying their 24752082Seschrock * bonus buffers. 24762082Seschrock */ 2477*6689Smaybee err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 24782199Sahrens dsl_dataset_promote_sync, ds, &pa, 2 + 2 * doi.doi_physical_blks); 2479*6689Smaybee 2480*6689Smaybee dsl_dataset_rele(pa.old_head, FTAG); 2481*6689Smaybee out: 2482*6689Smaybee while ((snap = list_tail(&pa.snap_list)) != NULL) { 2483*6689Smaybee list_remove(&pa.snap_list, snap); 2484*6689Smaybee dsl_dataset_disown(snap->ds, FTAG); 2485*6689Smaybee kmem_free(snap, sizeof (struct promotedsarg)); 2486*6689Smaybee } 2487*6689Smaybee list_destroy(&pa.snap_list); 2488*6689Smaybee if (pa.clone_origin) 2489*6689Smaybee dsl_dataset_disown(pa.clone_origin, FTAG); 2490*6689Smaybee dsl_dataset_rele(ds, FTAG); 24912082Seschrock return (err); 24922082Seschrock } 24933912Slling 24945367Sahrens struct cloneswaparg { 24955367Sahrens dsl_dataset_t *cds; /* clone dataset */ 24965367Sahrens dsl_dataset_t *ohds; /* origin's head dataset */ 24975367Sahrens boolean_t force; 24985481Sck153898 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 24995367Sahrens }; 25005326Sek110237 25015326Sek110237 /* ARGSUSED */ 25025326Sek110237 static int 25035326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 25045326Sek110237 { 25055367Sahrens struct cloneswaparg *csa = arg1; 25065326Sek110237 25075367Sahrens /* they should both be heads */ 25085367Sahrens if (dsl_dataset_is_snapshot(csa->cds) || 25095367Sahrens dsl_dataset_is_snapshot(csa->ohds)) 25105326Sek110237 return (EINVAL); 25115326Sek110237 25125367Sahrens /* the branch point should be just before them */ 25135367Sahrens if (csa->cds->ds_prev != csa->ohds->ds_prev) 25145326Sek110237 return (EINVAL); 25155326Sek110237 25165367Sahrens /* cds should be the clone */ 25175367Sahrens if (csa->cds->ds_prev->ds_phys->ds_next_snap_obj != 25185367Sahrens csa->ohds->ds_object) 25195367Sahrens return (EINVAL); 25205326Sek110237 25215367Sahrens /* the clone should be a child of the origin */ 25225367Sahrens if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 25235367Sahrens return (EINVAL); 25245326Sek110237 25255367Sahrens /* ohds shouldn't be modified unless 'force' */ 25265367Sahrens if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 25275367Sahrens return (ETXTBSY); 25285481Sck153898 25295481Sck153898 /* adjust amount of any unconsumed refreservation */ 25305481Sck153898 csa->unused_refres_delta = 25315481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 25325481Sck153898 csa->ohds->ds_phys->ds_unique_bytes) - 25335481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 25345481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 25355481Sck153898 25365481Sck153898 if (csa->unused_refres_delta > 0 && 25375481Sck153898 csa->unused_refres_delta > 25385481Sck153898 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 25395481Sck153898 return (ENOSPC); 25405481Sck153898 25415367Sahrens return (0); 25425326Sek110237 } 25435326Sek110237 25445326Sek110237 /* ARGSUSED */ 25455326Sek110237 static void 25465326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 25475326Sek110237 { 25485367Sahrens struct cloneswaparg *csa = arg1; 25495367Sahrens dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 25505326Sek110237 uint64_t itor = 0; 25515326Sek110237 blkptr_t bp; 25525326Sek110237 uint64_t unique = 0; 25535326Sek110237 int err; 25545326Sek110237 25555481Sck153898 ASSERT(csa->cds->ds_reserved == 0); 25565481Sck153898 ASSERT(csa->cds->ds_quota == csa->ohds->ds_quota); 25575481Sck153898 25585367Sahrens dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 25595367Sahrens dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 25605367Sahrens dmu_buf_will_dirty(csa->cds->ds_prev->ds_dbuf, tx); 25615326Sek110237 25625367Sahrens if (csa->cds->ds_user_ptr != NULL) { 25635367Sahrens csa->cds->ds_user_evict_func(csa->cds, csa->cds->ds_user_ptr); 25645367Sahrens csa->cds->ds_user_ptr = NULL; 25655367Sahrens } 25665326Sek110237 25675367Sahrens if (csa->ohds->ds_user_ptr != NULL) { 25685367Sahrens csa->ohds->ds_user_evict_func(csa->ohds, 25695367Sahrens csa->ohds->ds_user_ptr); 25705367Sahrens csa->ohds->ds_user_ptr = NULL; 25715367Sahrens } 25725326Sek110237 25735326Sek110237 /* compute unique space */ 25745367Sahrens while ((err = bplist_iterate(&csa->cds->ds_deadlist, 25755367Sahrens &itor, &bp)) == 0) { 25765367Sahrens if (bp.blk_birth > csa->cds->ds_prev->ds_phys->ds_prev_snap_txg) 25775367Sahrens unique += bp_get_dasize(dp->dp_spa, &bp); 25785326Sek110237 } 25795326Sek110237 VERIFY(err == ENOENT); 25805326Sek110237 25815326Sek110237 /* reset origin's unique bytes */ 25825367Sahrens csa->cds->ds_prev->ds_phys->ds_unique_bytes = unique; 25835326Sek110237 25845326Sek110237 /* swap blkptrs */ 25855326Sek110237 { 25865326Sek110237 blkptr_t tmp; 25875367Sahrens tmp = csa->ohds->ds_phys->ds_bp; 25885367Sahrens csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 25895367Sahrens csa->cds->ds_phys->ds_bp = tmp; 25905326Sek110237 } 25915326Sek110237 25925326Sek110237 /* set dd_*_bytes */ 25935326Sek110237 { 25945326Sek110237 int64_t dused, dcomp, duncomp; 25955326Sek110237 uint64_t cdl_used, cdl_comp, cdl_uncomp; 25965326Sek110237 uint64_t odl_used, odl_comp, odl_uncomp; 25975326Sek110237 25985367Sahrens VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 25995326Sek110237 &cdl_comp, &cdl_uncomp)); 26005367Sahrens VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 26015326Sek110237 &odl_comp, &odl_uncomp)); 26025367Sahrens dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 26035367Sahrens (csa->ohds->ds_phys->ds_used_bytes + odl_used); 26045367Sahrens dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 26055367Sahrens (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 26065367Sahrens duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 26075367Sahrens cdl_uncomp - 26085367Sahrens (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 26095326Sek110237 26105367Sahrens dsl_dir_diduse_space(csa->ohds->ds_dir, 26115367Sahrens dused, dcomp, duncomp, tx); 26125367Sahrens dsl_dir_diduse_space(csa->cds->ds_dir, 26135367Sahrens -dused, -dcomp, -duncomp, tx); 26145367Sahrens } 26155367Sahrens 26165367Sahrens #define SWITCH64(x, y) \ 26175367Sahrens { \ 26185367Sahrens uint64_t __tmp = (x); \ 26195367Sahrens (x) = (y); \ 26205367Sahrens (y) = __tmp; \ 26215326Sek110237 } 26225326Sek110237 26235326Sek110237 /* swap ds_*_bytes */ 26245367Sahrens SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 26255367Sahrens csa->cds->ds_phys->ds_used_bytes); 26265367Sahrens SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 26275367Sahrens csa->cds->ds_phys->ds_compressed_bytes); 26285367Sahrens SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 26295367Sahrens csa->cds->ds_phys->ds_uncompressed_bytes); 26305481Sck153898 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 26315481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 26325481Sck153898 26335481Sck153898 /* apply any parent delta for change in unconsumed refreservation */ 26345481Sck153898 dsl_dir_diduse_space(csa->ohds->ds_dir, csa->unused_refres_delta, 26355481Sck153898 0, 0, tx); 26365326Sek110237 26375326Sek110237 /* swap deadlists */ 26385367Sahrens bplist_close(&csa->cds->ds_deadlist); 26395367Sahrens bplist_close(&csa->ohds->ds_deadlist); 26405367Sahrens SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 26415367Sahrens csa->cds->ds_phys->ds_deadlist_obj); 26425367Sahrens VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 26435367Sahrens csa->cds->ds_phys->ds_deadlist_obj)); 26445367Sahrens VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 26455367Sahrens csa->ohds->ds_phys->ds_deadlist_obj)); 26465326Sek110237 } 26475326Sek110237 26485326Sek110237 /* 2649*6689Smaybee * Swap 'clone' with its origin head file system. Used at the end 2650*6689Smaybee * of "online recv" to swizzle the file system to the new version. 26515326Sek110237 */ 26525326Sek110237 int 26535367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 26545367Sahrens boolean_t force) 26555326Sek110237 { 26565367Sahrens struct cloneswaparg csa; 2657*6689Smaybee int error; 2658*6689Smaybee 2659*6689Smaybee ASSERT(clone->ds_owner); 2660*6689Smaybee ASSERT(origin_head->ds_owner); 2661*6689Smaybee retry: 2662*6689Smaybee /* Need exclusive access for the swap */ 2663*6689Smaybee rw_enter(&clone->ds_rwlock, RW_WRITER); 2664*6689Smaybee if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 2665*6689Smaybee rw_exit(&clone->ds_rwlock); 2666*6689Smaybee rw_enter(&origin_head->ds_rwlock, RW_WRITER); 2667*6689Smaybee if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 2668*6689Smaybee rw_exit(&origin_head->ds_rwlock); 2669*6689Smaybee goto retry; 2670*6689Smaybee } 2671*6689Smaybee } 26725367Sahrens csa.cds = clone; 26735367Sahrens csa.ohds = origin_head; 26745367Sahrens csa.force = force; 2675*6689Smaybee error = dsl_sync_task_do(clone->ds_dir->dd_pool, 26765326Sek110237 dsl_dataset_clone_swap_check, 2677*6689Smaybee dsl_dataset_clone_swap_sync, &csa, NULL, 9); 2678*6689Smaybee return (error); 26795326Sek110237 } 26805326Sek110237 26813912Slling /* 26823912Slling * Given a pool name and a dataset object number in that pool, 26833912Slling * return the name of that dataset. 26843912Slling */ 26853912Slling int 26863912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 26873912Slling { 26883912Slling spa_t *spa; 26893912Slling dsl_pool_t *dp; 2690*6689Smaybee dsl_dataset_t *ds; 26913912Slling int error; 26923912Slling 26933912Slling if ((error = spa_open(pname, &spa, FTAG)) != 0) 26943912Slling return (error); 26953912Slling dp = spa_get_dsl(spa); 26963912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 2697*6689Smaybee if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 2698*6689Smaybee dsl_dataset_name(ds, buf); 2699*6689Smaybee dsl_dataset_rele(ds, FTAG); 27003912Slling } 27013912Slling rw_exit(&dp->dp_config_rwlock); 27023912Slling spa_close(spa, FTAG); 27033912Slling 2704*6689Smaybee return (error); 27053912Slling } 27065378Sck153898 27075378Sck153898 int 27085378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 2709*6689Smaybee uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 27105378Sck153898 { 27115378Sck153898 int error = 0; 27125378Sck153898 27135378Sck153898 ASSERT3S(asize, >, 0); 27145378Sck153898 27155831Sck153898 /* 27165831Sck153898 * *ref_rsrv is the portion of asize that will come from any 27175831Sck153898 * unconsumed refreservation space. 27185831Sck153898 */ 27195831Sck153898 *ref_rsrv = 0; 27205831Sck153898 27215378Sck153898 mutex_enter(&ds->ds_lock); 27225378Sck153898 /* 27235378Sck153898 * Make a space adjustment for reserved bytes. 27245378Sck153898 */ 27255378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 27265378Sck153898 ASSERT3U(*used, >=, 27275378Sck153898 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 27285378Sck153898 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 27295831Sck153898 *ref_rsrv = 27305831Sck153898 asize - MIN(asize, parent_delta(ds, asize + inflight)); 27315378Sck153898 } 27325378Sck153898 27335378Sck153898 if (!check_quota || ds->ds_quota == 0) { 27345378Sck153898 mutex_exit(&ds->ds_lock); 27355378Sck153898 return (0); 27365378Sck153898 } 27375378Sck153898 /* 27385378Sck153898 * If they are requesting more space, and our current estimate 27395378Sck153898 * is over quota, they get to try again unless the actual 27405378Sck153898 * on-disk is over quota and there are no pending changes (which 27415378Sck153898 * may free up space for us). 27425378Sck153898 */ 27435378Sck153898 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 27445378Sck153898 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 27455378Sck153898 error = ERESTART; 27465378Sck153898 else 27475378Sck153898 error = EDQUOT; 27485378Sck153898 } 27495378Sck153898 mutex_exit(&ds->ds_lock); 27505378Sck153898 27515378Sck153898 return (error); 27525378Sck153898 } 27535378Sck153898 27545378Sck153898 /* ARGSUSED */ 27555378Sck153898 static int 27565378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 27575378Sck153898 { 27585378Sck153898 dsl_dataset_t *ds = arg1; 27595378Sck153898 uint64_t *quotap = arg2; 27605378Sck153898 uint64_t new_quota = *quotap; 27615378Sck153898 27625378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 27635378Sck153898 return (ENOTSUP); 27645378Sck153898 27655378Sck153898 if (new_quota == 0) 27665378Sck153898 return (0); 27675378Sck153898 27685378Sck153898 if (new_quota < ds->ds_phys->ds_used_bytes || 27695378Sck153898 new_quota < ds->ds_reserved) 27705378Sck153898 return (ENOSPC); 27715378Sck153898 27725378Sck153898 return (0); 27735378Sck153898 } 27745378Sck153898 27755378Sck153898 /* ARGSUSED */ 27765378Sck153898 void 27775378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 27785378Sck153898 { 27795378Sck153898 dsl_dataset_t *ds = arg1; 27805378Sck153898 uint64_t *quotap = arg2; 27815378Sck153898 uint64_t new_quota = *quotap; 27825378Sck153898 27835378Sck153898 dmu_buf_will_dirty(ds->ds_dbuf, tx); 27845378Sck153898 27855378Sck153898 ds->ds_quota = new_quota; 27865378Sck153898 27875378Sck153898 dsl_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx); 27885378Sck153898 27895378Sck153898 spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa, 27905378Sck153898 tx, cr, "%lld dataset = %llu ", 2791*6689Smaybee (longlong_t)new_quota, ds->ds_object); 27925378Sck153898 } 27935378Sck153898 27945378Sck153898 int 27955378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota) 27965378Sck153898 { 27975378Sck153898 dsl_dataset_t *ds; 27985378Sck153898 int err; 27995378Sck153898 2800*6689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 28015378Sck153898 if (err) 28025378Sck153898 return (err); 28035378Sck153898 28045481Sck153898 if (quota != ds->ds_quota) { 28055481Sck153898 /* 28065481Sck153898 * If someone removes a file, then tries to set the quota, we 28075481Sck153898 * want to make sure the file freeing takes effect. 28085481Sck153898 */ 28095481Sck153898 txg_wait_open(ds->ds_dir->dd_pool, 0); 28105481Sck153898 28115481Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 28125481Sck153898 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 28135481Sck153898 ds, "a, 0); 28145481Sck153898 } 2815*6689Smaybee dsl_dataset_rele(ds, FTAG); 28165378Sck153898 return (err); 28175378Sck153898 } 28185378Sck153898 28195378Sck153898 static int 28205378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 28215378Sck153898 { 28225378Sck153898 dsl_dataset_t *ds = arg1; 28235378Sck153898 uint64_t *reservationp = arg2; 28245378Sck153898 uint64_t new_reservation = *reservationp; 28255378Sck153898 int64_t delta; 28265378Sck153898 uint64_t unique; 28275378Sck153898 28285378Sck153898 if (new_reservation > INT64_MAX) 28295378Sck153898 return (EOVERFLOW); 28305378Sck153898 28315378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 28325378Sck153898 SPA_VERSION_REFRESERVATION) 28335378Sck153898 return (ENOTSUP); 28345378Sck153898 28355378Sck153898 if (dsl_dataset_is_snapshot(ds)) 28365378Sck153898 return (EINVAL); 28375378Sck153898 28385378Sck153898 /* 28395378Sck153898 * If we are doing the preliminary check in open context, the 28405378Sck153898 * space estimates may be inaccurate. 28415378Sck153898 */ 28425378Sck153898 if (!dmu_tx_is_syncing(tx)) 28435378Sck153898 return (0); 28445378Sck153898 28455378Sck153898 mutex_enter(&ds->ds_lock); 28465378Sck153898 unique = dsl_dataset_unique(ds); 28475378Sck153898 delta = MAX(unique, new_reservation) - MAX(unique, ds->ds_reserved); 28485378Sck153898 mutex_exit(&ds->ds_lock); 28495378Sck153898 28505378Sck153898 if (delta > 0 && 28515378Sck153898 delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 28525378Sck153898 return (ENOSPC); 28535378Sck153898 if (delta > 0 && ds->ds_quota > 0 && 28545378Sck153898 new_reservation > ds->ds_quota) 28555378Sck153898 return (ENOSPC); 28565378Sck153898 28575378Sck153898 return (0); 28585378Sck153898 } 28595378Sck153898 28605378Sck153898 /* ARGSUSED */ 28615378Sck153898 static void 28625378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 28635378Sck153898 dmu_tx_t *tx) 28645378Sck153898 { 28655378Sck153898 dsl_dataset_t *ds = arg1; 28665378Sck153898 uint64_t *reservationp = arg2; 28675378Sck153898 uint64_t new_reservation = *reservationp; 28685378Sck153898 uint64_t unique; 28695378Sck153898 int64_t delta; 28705378Sck153898 28715378Sck153898 dmu_buf_will_dirty(ds->ds_dbuf, tx); 28725378Sck153898 28735378Sck153898 mutex_enter(&ds->ds_lock); 28745378Sck153898 unique = dsl_dataset_unique(ds); 28755378Sck153898 delta = MAX(0, (int64_t)(new_reservation - unique)) - 28765378Sck153898 MAX(0, (int64_t)(ds->ds_reserved - unique)); 28775378Sck153898 ds->ds_reserved = new_reservation; 28785378Sck153898 mutex_exit(&ds->ds_lock); 28795378Sck153898 28805378Sck153898 dsl_prop_set_uint64_sync(ds->ds_dir, "refreservation", 28815378Sck153898 new_reservation, cr, tx); 28825378Sck153898 28835378Sck153898 dsl_dir_diduse_space(ds->ds_dir, delta, 0, 0, tx); 28845378Sck153898 28855378Sck153898 spa_history_internal_log(LOG_DS_REFRESERV, 28865378Sck153898 ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 28875378Sck153898 (longlong_t)new_reservation, 28885378Sck153898 ds->ds_dir->dd_phys->dd_head_dataset_obj); 28895378Sck153898 } 28905378Sck153898 28915378Sck153898 int 28925378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation) 28935378Sck153898 { 28945378Sck153898 dsl_dataset_t *ds; 28955378Sck153898 int err; 28965378Sck153898 2897*6689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 28985378Sck153898 if (err) 28995378Sck153898 return (err); 29005378Sck153898 29015378Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 29025378Sck153898 dsl_dataset_set_reservation_check, 29035378Sck153898 dsl_dataset_set_reservation_sync, ds, &reservation, 0); 2904*6689Smaybee dsl_dataset_rele(ds, FTAG); 29055378Sck153898 return (err); 29065378Sck153898 } 2907