1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 228517SEric.Taylor@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/dmu_objset.h> 27789Sahrens #include <sys/dsl_dataset.h> 28789Sahrens #include <sys/dsl_dir.h> 292082Seschrock #include <sys/dsl_prop.h> 302199Sahrens #include <sys/dsl_synctask.h> 31789Sahrens #include <sys/dmu_traverse.h> 32789Sahrens #include <sys/dmu_tx.h> 33789Sahrens #include <sys/arc.h> 34789Sahrens #include <sys/zio.h> 35789Sahrens #include <sys/zap.h> 36789Sahrens #include <sys/unique.h> 37789Sahrens #include <sys/zfs_context.h> 384007Smmusante #include <sys/zfs_ioctl.h> 394543Smarks #include <sys/spa.h> 407046Sahrens #include <sys/zfs_znode.h> 414543Smarks #include <sys/sunddi.h> 4210242Schris.kirby@sun.com #include <sys/zvol.h> 43789Sahrens 446689Smaybee static char *dsl_reaper = "the grim reaper"; 456689Smaybee 462199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check; 472199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync; 485378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync; 491731Sbonwick 503444Sek110237 #define DS_REF_MAX (1ULL << 62) 51789Sahrens 52789Sahrens #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 53789Sahrens 546689Smaybee #define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper) 556689Smaybee 56789Sahrens 575378Sck153898 /* 585378Sck153898 * Figure out how much of this delta should be propogated to the dsl_dir 595378Sck153898 * layer. If there's a refreservation, that space has already been 605378Sck153898 * partially accounted for in our ancestors. 615378Sck153898 */ 625378Sck153898 static int64_t 635378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta) 645378Sck153898 { 655378Sck153898 uint64_t old_bytes, new_bytes; 665378Sck153898 675378Sck153898 if (ds->ds_reserved == 0) 685378Sck153898 return (delta); 695378Sck153898 705378Sck153898 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 715378Sck153898 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 725378Sck153898 735378Sck153898 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 745378Sck153898 return (new_bytes - old_bytes); 755378Sck153898 } 76789Sahrens 77789Sahrens void 78*10922SJeff.Bonwick@Sun.COM dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 79789Sahrens { 80*10922SJeff.Bonwick@Sun.COM int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 81789Sahrens int compressed = BP_GET_PSIZE(bp); 82789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 835378Sck153898 int64_t delta; 84789Sahrens 85789Sahrens dprintf_bp(bp, "born, ds=%p\n", ds); 86789Sahrens 87789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 88789Sahrens /* It could have been compressed away to nothing */ 89789Sahrens if (BP_IS_HOLE(bp)) 90789Sahrens return; 91789Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 92789Sahrens ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 93789Sahrens if (ds == NULL) { 94789Sahrens /* 95789Sahrens * Account for the meta-objset space in its placeholder 96789Sahrens * dsl_dir. 97789Sahrens */ 98789Sahrens ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 997390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 100789Sahrens used, compressed, uncompressed, tx); 101789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 102789Sahrens return; 103789Sahrens } 104789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1057595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 106789Sahrens mutex_enter(&ds->ds_lock); 1075378Sck153898 delta = parent_delta(ds, used); 108789Sahrens ds->ds_phys->ds_used_bytes += used; 109789Sahrens ds->ds_phys->ds_compressed_bytes += compressed; 110789Sahrens ds->ds_phys->ds_uncompressed_bytes += uncompressed; 111789Sahrens ds->ds_phys->ds_unique_bytes += used; 112789Sahrens mutex_exit(&ds->ds_lock); 1137390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 1147390SMatthew.Ahrens@Sun.COM compressed, uncompressed, tx); 1157390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used - delta, 1167390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1177595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 118789Sahrens } 119789Sahrens 1206992Smaybee int 121*10922SJeff.Bonwick@Sun.COM dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 122*10922SJeff.Bonwick@Sun.COM boolean_t async) 123789Sahrens { 124*10922SJeff.Bonwick@Sun.COM if (BP_IS_HOLE(bp)) 125*10922SJeff.Bonwick@Sun.COM return (0); 126*10922SJeff.Bonwick@Sun.COM 127*10922SJeff.Bonwick@Sun.COM ASSERT(dmu_tx_is_syncing(tx)); 128*10922SJeff.Bonwick@Sun.COM ASSERT(bp->blk_birth <= tx->tx_txg); 129*10922SJeff.Bonwick@Sun.COM 130*10922SJeff.Bonwick@Sun.COM int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 131789Sahrens int compressed = BP_GET_PSIZE(bp); 132789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 133789Sahrens 134789Sahrens ASSERT(used > 0); 135789Sahrens if (ds == NULL) { 136789Sahrens /* 137789Sahrens * Account for the meta-objset space in its placeholder 138789Sahrens * dataset. 139789Sahrens */ 140*10922SJeff.Bonwick@Sun.COM dsl_free(tx->tx_pool, tx->tx_txg, bp); 141789Sahrens 1427390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 143789Sahrens -used, -compressed, -uncompressed, tx); 144789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 1456992Smaybee return (used); 146789Sahrens } 147789Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 148789Sahrens 1497390SMatthew.Ahrens@Sun.COM ASSERT(!dsl_dataset_is_snapshot(ds)); 150789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 151789Sahrens 152789Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 1535378Sck153898 int64_t delta; 1543547Smaybee 155789Sahrens dprintf_bp(bp, "freeing: %s", ""); 156*10922SJeff.Bonwick@Sun.COM dsl_free(tx->tx_pool, tx->tx_txg, bp); 157789Sahrens 1587595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 159789Sahrens mutex_enter(&ds->ds_lock); 1605378Sck153898 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 1615378Sck153898 !DS_UNIQUE_IS_ACCURATE(ds)); 1625378Sck153898 delta = parent_delta(ds, -used); 163789Sahrens ds->ds_phys->ds_unique_bytes -= used; 164789Sahrens mutex_exit(&ds->ds_lock); 1657390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 1665378Sck153898 delta, -compressed, -uncompressed, tx); 1677390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, -used - delta, 1687390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1697595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 170789Sahrens } else { 171789Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 172*10922SJeff.Bonwick@Sun.COM if (async) { 173*10922SJeff.Bonwick@Sun.COM /* 174*10922SJeff.Bonwick@Sun.COM * We are here as part of zio's write done callback, 175*10922SJeff.Bonwick@Sun.COM * which means we're a zio interrupt thread. We can't 176*10922SJeff.Bonwick@Sun.COM * call bplist_enqueue() now because it may block 177*10922SJeff.Bonwick@Sun.COM * waiting for I/O. Instead, put bp on the deferred 178*10922SJeff.Bonwick@Sun.COM * queue and let dsl_pool_sync() finish the job. 179*10922SJeff.Bonwick@Sun.COM */ 180*10922SJeff.Bonwick@Sun.COM bplist_enqueue_deferred(&ds->ds_deadlist, bp); 181*10922SJeff.Bonwick@Sun.COM } else { 182*10922SJeff.Bonwick@Sun.COM VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 183*10922SJeff.Bonwick@Sun.COM } 1845712Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 1855712Sahrens ds->ds_phys->ds_prev_snap_obj); 1865712Sahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 187789Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 1885712Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 1895712Sahrens ds->ds_object && bp->blk_birth > 1905712Sahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 1915712Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1925712Sahrens mutex_enter(&ds->ds_prev->ds_lock); 1935712Sahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 1945712Sahrens mutex_exit(&ds->ds_prev->ds_lock); 195789Sahrens } 1967390SMatthew.Ahrens@Sun.COM if (bp->blk_birth > ds->ds_origin_txg) { 1977390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used, 1987390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 1997390SMatthew.Ahrens@Sun.COM } 200789Sahrens } 201789Sahrens mutex_enter(&ds->ds_lock); 202789Sahrens ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 203789Sahrens ds->ds_phys->ds_used_bytes -= used; 204789Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 205789Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 206789Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 207789Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 208789Sahrens mutex_exit(&ds->ds_lock); 2096992Smaybee 2106992Smaybee return (used); 211789Sahrens } 212789Sahrens 2131544Seschrock uint64_t 2141544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 215789Sahrens { 2162885Sahrens uint64_t trysnap = 0; 2172885Sahrens 218789Sahrens if (ds == NULL) 2191544Seschrock return (0); 220789Sahrens /* 221789Sahrens * The snapshot creation could fail, but that would cause an 222789Sahrens * incorrect FALSE return, which would only result in an 223789Sahrens * overestimation of the amount of space that an operation would 224789Sahrens * consume, which is OK. 225789Sahrens * 226789Sahrens * There's also a small window where we could miss a pending 227789Sahrens * snapshot, because we could set the sync task in the quiescing 228789Sahrens * phase. So this should only be used as a guess. 229789Sahrens */ 2302885Sahrens if (ds->ds_trysnap_txg > 2312885Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 2322885Sahrens trysnap = ds->ds_trysnap_txg; 2332885Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 2341544Seschrock } 2351544Seschrock 2369653SSanjeev.Bagewadi@Sun.COM boolean_t 2371544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 2381544Seschrock { 2391544Seschrock return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 240789Sahrens } 241789Sahrens 242789Sahrens /* ARGSUSED */ 243789Sahrens static void 244789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 245789Sahrens { 246789Sahrens dsl_dataset_t *ds = dsv; 247789Sahrens 2486689Smaybee ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 249789Sahrens 2504787Sahrens unique_remove(ds->ds_fsid_guid); 251789Sahrens 25210298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) 25310298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 254789Sahrens 255789Sahrens if (ds->ds_prev) { 2566689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 257789Sahrens ds->ds_prev = NULL; 258789Sahrens } 259789Sahrens 260789Sahrens bplist_close(&ds->ds_deadlist); 2616689Smaybee if (ds->ds_dir) 2626689Smaybee dsl_dir_close(ds->ds_dir, ds); 263789Sahrens 2644787Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 265789Sahrens 2662856Snd150628 mutex_destroy(&ds->ds_lock); 26710204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 2684787Sahrens mutex_destroy(&ds->ds_opening_lock); 2696689Smaybee rw_destroy(&ds->ds_rwlock); 2706689Smaybee cv_destroy(&ds->ds_exclusive_cv); 271*10922SJeff.Bonwick@Sun.COM bplist_fini(&ds->ds_deadlist); 2722856Snd150628 273789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 274789Sahrens } 275789Sahrens 2761544Seschrock static int 277789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 278789Sahrens { 279789Sahrens dsl_dataset_phys_t *headphys; 280789Sahrens int err; 281789Sahrens dmu_buf_t *headdbuf; 282789Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 283789Sahrens objset_t *mos = dp->dp_meta_objset; 284789Sahrens 285789Sahrens if (ds->ds_snapname[0]) 2861544Seschrock return (0); 287789Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 2881544Seschrock return (0); 289789Sahrens 2901544Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 2911544Seschrock FTAG, &headdbuf); 2921544Seschrock if (err) 2931544Seschrock return (err); 294789Sahrens headphys = headdbuf->db_data; 295789Sahrens err = zap_value_search(dp->dp_meta_objset, 2964577Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 2971544Seschrock dmu_buf_rele(headdbuf, FTAG); 2981544Seschrock return (err); 299789Sahrens } 300789Sahrens 3016492Stimh static int 3026689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 3036492Stimh { 3046689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3056689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3066492Stimh matchtype_t mt; 3076492Stimh int err; 3086492Stimh 3096689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3106492Stimh mt = MT_FIRST; 3116492Stimh else 3126492Stimh mt = MT_EXACT; 3136492Stimh 3146689Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 3156492Stimh value, mt, NULL, 0, NULL); 3166492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3176689Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 3186492Stimh return (err); 3196492Stimh } 3206492Stimh 3216492Stimh static int 3226689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 3236492Stimh { 3246689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3256689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3266492Stimh matchtype_t mt; 3276492Stimh int err; 3286492Stimh 32910373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(ds->ds_dir); 33010373Schris.kirby@sun.com 3316689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3326492Stimh mt = MT_FIRST; 3336492Stimh else 3346492Stimh mt = MT_EXACT; 3356492Stimh 3366689Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 3376492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3386689Smaybee err = zap_remove(mos, snapobj, name, tx); 3396492Stimh return (err); 3406492Stimh } 3416492Stimh 3426689Smaybee static int 3436689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag, 3446689Smaybee dsl_dataset_t **dsp) 345789Sahrens { 346789Sahrens objset_t *mos = dp->dp_meta_objset; 347789Sahrens dmu_buf_t *dbuf; 348789Sahrens dsl_dataset_t *ds; 3491544Seschrock int err; 350789Sahrens 351789Sahrens ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 352789Sahrens dsl_pool_sync_context(dp)); 353789Sahrens 3541544Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3551544Seschrock if (err) 3561544Seschrock return (err); 357789Sahrens ds = dmu_buf_get_user(dbuf); 358789Sahrens if (ds == NULL) { 359789Sahrens dsl_dataset_t *winner; 360789Sahrens 361789Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 362789Sahrens ds->ds_dbuf = dbuf; 363789Sahrens ds->ds_object = dsobj; 364789Sahrens ds->ds_phys = dbuf->db_data; 365789Sahrens 3662856Snd150628 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 36710204SMatthew.Ahrens@Sun.COM mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL); 3684787Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3696689Smaybee rw_init(&ds->ds_rwlock, 0, 0, 0); 3706689Smaybee cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 371*10922SJeff.Bonwick@Sun.COM bplist_init(&ds->ds_deadlist); 3722856Snd150628 3731544Seschrock err = bplist_open(&ds->ds_deadlist, 374789Sahrens mos, ds->ds_phys->ds_deadlist_obj); 3751544Seschrock if (err == 0) { 3761544Seschrock err = dsl_dir_open_obj(dp, 3771544Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 3781544Seschrock } 3791544Seschrock if (err) { 3801544Seschrock /* 3811544Seschrock * we don't really need to close the blist if we 3821544Seschrock * just opened it. 3831544Seschrock */ 3842856Snd150628 mutex_destroy(&ds->ds_lock); 38510204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 3864787Sahrens mutex_destroy(&ds->ds_opening_lock); 3876689Smaybee rw_destroy(&ds->ds_rwlock); 3886689Smaybee cv_destroy(&ds->ds_exclusive_cv); 389*10922SJeff.Bonwick@Sun.COM bplist_fini(&ds->ds_deadlist); 3901544Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 3911544Seschrock dmu_buf_rele(dbuf, tag); 3921544Seschrock return (err); 3931544Seschrock } 394789Sahrens 3957390SMatthew.Ahrens@Sun.COM if (!dsl_dataset_is_snapshot(ds)) { 396789Sahrens ds->ds_snapname[0] = '\0'; 397789Sahrens if (ds->ds_phys->ds_prev_snap_obj) { 3986689Smaybee err = dsl_dataset_get_ref(dp, 3996689Smaybee ds->ds_phys->ds_prev_snap_obj, 4006689Smaybee ds, &ds->ds_prev); 401789Sahrens } 4027390SMatthew.Ahrens@Sun.COM 4037390SMatthew.Ahrens@Sun.COM if (err == 0 && dsl_dir_is_clone(ds->ds_dir)) { 4047390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin; 4057390SMatthew.Ahrens@Sun.COM 4067390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, 4077390SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_phys->dd_origin_obj, 4087390SMatthew.Ahrens@Sun.COM FTAG, &origin); 4097390SMatthew.Ahrens@Sun.COM if (err == 0) { 4107390SMatthew.Ahrens@Sun.COM ds->ds_origin_txg = 4117390SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_creation_txg; 4127390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(origin, FTAG); 4137390SMatthew.Ahrens@Sun.COM } 4147390SMatthew.Ahrens@Sun.COM } 41510242Schris.kirby@sun.com } else { 41610242Schris.kirby@sun.com if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 41710242Schris.kirby@sun.com err = dsl_dataset_get_snapname(ds); 41810242Schris.kirby@sun.com if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) { 41910242Schris.kirby@sun.com err = zap_count( 42010242Schris.kirby@sun.com ds->ds_dir->dd_pool->dp_meta_objset, 42110242Schris.kirby@sun.com ds->ds_phys->ds_userrefs_obj, 42210242Schris.kirby@sun.com &ds->ds_userrefs); 42310242Schris.kirby@sun.com } 424789Sahrens } 425789Sahrens 4267390SMatthew.Ahrens@Sun.COM if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 4275569Sck153898 /* 4285569Sck153898 * In sync context, we're called with either no lock 4295569Sck153898 * or with the write lock. If we're not syncing, 4305569Sck153898 * we're always called with the read lock held. 4315569Sck153898 */ 4325475Sck153898 boolean_t need_lock = 4335569Sck153898 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 4345569Sck153898 dsl_pool_sync_context(dp); 4355475Sck153898 4365475Sck153898 if (need_lock) 4375475Sck153898 rw_enter(&dp->dp_config_rwlock, RW_READER); 4385475Sck153898 4397265Sahrens err = dsl_prop_get_ds(ds, 4405475Sck153898 "refreservation", sizeof (uint64_t), 1, 4415475Sck153898 &ds->ds_reserved, NULL); 4425475Sck153898 if (err == 0) { 4437265Sahrens err = dsl_prop_get_ds(ds, 4445475Sck153898 "refquota", sizeof (uint64_t), 1, 4455475Sck153898 &ds->ds_quota, NULL); 4465475Sck153898 } 4475475Sck153898 4485475Sck153898 if (need_lock) 4495475Sck153898 rw_exit(&dp->dp_config_rwlock); 4505475Sck153898 } else { 4515475Sck153898 ds->ds_reserved = ds->ds_quota = 0; 4525475Sck153898 } 4535475Sck153898 4541544Seschrock if (err == 0) { 4551544Seschrock winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 4561544Seschrock dsl_dataset_evict); 4571544Seschrock } 4581544Seschrock if (err || winner) { 459789Sahrens bplist_close(&ds->ds_deadlist); 4606689Smaybee if (ds->ds_prev) 4616689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 462789Sahrens dsl_dir_close(ds->ds_dir, ds); 4632856Snd150628 mutex_destroy(&ds->ds_lock); 46410204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 4654787Sahrens mutex_destroy(&ds->ds_opening_lock); 4666689Smaybee rw_destroy(&ds->ds_rwlock); 4676689Smaybee cv_destroy(&ds->ds_exclusive_cv); 468*10922SJeff.Bonwick@Sun.COM bplist_fini(&ds->ds_deadlist); 469789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4701544Seschrock if (err) { 4711544Seschrock dmu_buf_rele(dbuf, tag); 4721544Seschrock return (err); 4731544Seschrock } 474789Sahrens ds = winner; 475789Sahrens } else { 4764787Sahrens ds->ds_fsid_guid = 477789Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 478789Sahrens } 479789Sahrens } 480789Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 481789Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 4827046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 4837061Sahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 4847077Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 485789Sahrens mutex_enter(&ds->ds_lock); 4866689Smaybee if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 487789Sahrens mutex_exit(&ds->ds_lock); 4886689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 4896689Smaybee return (ENOENT); 4906689Smaybee } 4916689Smaybee mutex_exit(&ds->ds_lock); 4926689Smaybee *dsp = ds; 4936689Smaybee return (0); 4946689Smaybee } 4956689Smaybee 4966689Smaybee static int 4976689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 4986689Smaybee { 4996689Smaybee dsl_pool_t *dp = ds->ds_dir->dd_pool; 5006689Smaybee 5016689Smaybee /* 5026689Smaybee * In syncing context we don't want the rwlock lock: there 5036689Smaybee * may be an existing writer waiting for sync phase to 5046689Smaybee * finish. We don't need to worry about such writers, since 5056689Smaybee * sync phase is single-threaded, so the writer can't be 5066689Smaybee * doing anything while we are active. 5076689Smaybee */ 5086689Smaybee if (dsl_pool_sync_context(dp)) { 5096689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 5106689Smaybee return (0); 511789Sahrens } 5126689Smaybee 5136689Smaybee /* 5146689Smaybee * Normal users will hold the ds_rwlock as a READER until they 5156689Smaybee * are finished (i.e., call dsl_dataset_rele()). "Owners" will 5166689Smaybee * drop their READER lock after they set the ds_owner field. 5176689Smaybee * 5186689Smaybee * If the dataset is being destroyed, the destroy thread will 5196689Smaybee * obtain a WRITER lock for exclusive access after it's done its 5206689Smaybee * open-context work and then change the ds_owner to 5216689Smaybee * dsl_reaper once destruction is assured. So threads 5226689Smaybee * may block here temporarily, until the "destructability" of 5236689Smaybee * the dataset is determined. 5246689Smaybee */ 5256689Smaybee ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 5266689Smaybee mutex_enter(&ds->ds_lock); 5276689Smaybee while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 5286689Smaybee rw_exit(&dp->dp_config_rwlock); 5296689Smaybee cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 5306689Smaybee if (DSL_DATASET_IS_DESTROYED(ds)) { 5316689Smaybee mutex_exit(&ds->ds_lock); 5326689Smaybee dsl_dataset_drop_ref(ds, tag); 5336689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5346689Smaybee return (ENOENT); 5356689Smaybee } 5366689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5376689Smaybee } 538789Sahrens mutex_exit(&ds->ds_lock); 5391544Seschrock return (0); 540789Sahrens } 541789Sahrens 542789Sahrens int 5436689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 5446689Smaybee dsl_dataset_t **dsp) 5456689Smaybee { 5466689Smaybee int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 5476689Smaybee 5486689Smaybee if (err) 5496689Smaybee return (err); 5506689Smaybee return (dsl_dataset_hold_ref(*dsp, tag)); 5516689Smaybee } 5526689Smaybee 5536689Smaybee int 55410298SMatthew.Ahrens@Sun.COM dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok, 55510298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 5566689Smaybee { 55710298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5586689Smaybee if (err) 5596689Smaybee return (err); 56010298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 56110298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 5628779SMark.Musante@Sun.COM *dsp = NULL; 5636689Smaybee return (EBUSY); 5646689Smaybee } 5656689Smaybee return (0); 5666689Smaybee } 5676689Smaybee 5686689Smaybee int 5696689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 570789Sahrens { 571789Sahrens dsl_dir_t *dd; 572789Sahrens dsl_pool_t *dp; 5736689Smaybee const char *snapname; 574789Sahrens uint64_t obj; 575789Sahrens int err = 0; 576789Sahrens 5776689Smaybee err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 5781544Seschrock if (err) 5791544Seschrock return (err); 580789Sahrens 581789Sahrens dp = dd->dd_pool; 582789Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 583789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 5846689Smaybee if (obj) 5856689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, dsp); 5866689Smaybee else 587789Sahrens err = ENOENT; 5886689Smaybee if (err) 589789Sahrens goto out; 5906689Smaybee 5916689Smaybee err = dsl_dataset_hold_ref(*dsp, tag); 5926689Smaybee 5936689Smaybee /* we may be looking for a snapshot */ 5946689Smaybee if (err == 0 && snapname != NULL) { 5956689Smaybee dsl_dataset_t *ds = NULL; 5966689Smaybee 5976689Smaybee if (*snapname++ != '@') { 5986689Smaybee dsl_dataset_rele(*dsp, tag); 599789Sahrens err = ENOENT; 600789Sahrens goto out; 601789Sahrens } 6026689Smaybee 6036689Smaybee dprintf("looking for snapshot '%s'\n", snapname); 6046689Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 6056689Smaybee if (err == 0) 6066689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, &ds); 6076689Smaybee dsl_dataset_rele(*dsp, tag); 6086689Smaybee 6096689Smaybee ASSERT3U((err == 0), ==, (ds != NULL)); 6106689Smaybee 6116689Smaybee if (ds) { 6126689Smaybee mutex_enter(&ds->ds_lock); 6136689Smaybee if (ds->ds_snapname[0] == 0) 6146689Smaybee (void) strlcpy(ds->ds_snapname, snapname, 6156689Smaybee sizeof (ds->ds_snapname)); 6166689Smaybee mutex_exit(&ds->ds_lock); 6176689Smaybee err = dsl_dataset_hold_ref(ds, tag); 6186689Smaybee *dsp = err ? NULL : ds; 619789Sahrens } 620789Sahrens } 621789Sahrens out: 622789Sahrens rw_exit(&dp->dp_config_rwlock); 623789Sahrens dsl_dir_close(dd, FTAG); 624789Sahrens return (err); 625789Sahrens } 626789Sahrens 627789Sahrens int 62810298SMatthew.Ahrens@Sun.COM dsl_dataset_own(const char *name, boolean_t inconsistentok, 62910298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 630789Sahrens { 63110298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold(name, tag, dsp); 6326689Smaybee if (err) 6336689Smaybee return (err); 63410298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 63510298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 6366689Smaybee return (EBUSY); 6376689Smaybee } 6386689Smaybee return (0); 639789Sahrens } 640789Sahrens 641789Sahrens void 642789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 643789Sahrens { 644789Sahrens if (ds == NULL) { 645789Sahrens (void) strcpy(name, "mos"); 646789Sahrens } else { 647789Sahrens dsl_dir_name(ds->ds_dir, name); 6481544Seschrock VERIFY(0 == dsl_dataset_get_snapname(ds)); 649789Sahrens if (ds->ds_snapname[0]) { 650789Sahrens (void) strcat(name, "@"); 6516689Smaybee /* 6526689Smaybee * We use a "recursive" mutex so that we 6536689Smaybee * can call dprintf_ds() with ds_lock held. 6546689Smaybee */ 655789Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 656789Sahrens mutex_enter(&ds->ds_lock); 657789Sahrens (void) strcat(name, ds->ds_snapname); 658789Sahrens mutex_exit(&ds->ds_lock); 659789Sahrens } else { 660789Sahrens (void) strcat(name, ds->ds_snapname); 661789Sahrens } 662789Sahrens } 663789Sahrens } 664789Sahrens } 665789Sahrens 6663978Smmusante static int 6673978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds) 6683978Smmusante { 6693978Smmusante int result; 6703978Smmusante 6713978Smmusante if (ds == NULL) { 6723978Smmusante result = 3; /* "mos" */ 6733978Smmusante } else { 6743978Smmusante result = dsl_dir_namelen(ds->ds_dir); 6753978Smmusante VERIFY(0 == dsl_dataset_get_snapname(ds)); 6763978Smmusante if (ds->ds_snapname[0]) { 6773978Smmusante ++result; /* adding one for the @-sign */ 6783978Smmusante if (!MUTEX_HELD(&ds->ds_lock)) { 6793978Smmusante mutex_enter(&ds->ds_lock); 6803978Smmusante result += strlen(ds->ds_snapname); 6813978Smmusante mutex_exit(&ds->ds_lock); 6823978Smmusante } else { 6833978Smmusante result += strlen(ds->ds_snapname); 6843978Smmusante } 6853978Smmusante } 6863978Smmusante } 6873978Smmusante 6883978Smmusante return (result); 6893978Smmusante } 6903978Smmusante 6917046Sahrens void 6926689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 693789Sahrens { 6941544Seschrock dmu_buf_rele(ds->ds_dbuf, tag); 695789Sahrens } 696789Sahrens 697789Sahrens void 6986689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6995367Sahrens { 7006689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 7016689Smaybee rw_exit(&ds->ds_rwlock); 7026689Smaybee } 7036689Smaybee dsl_dataset_drop_ref(ds, tag); 7046689Smaybee } 7056689Smaybee 7066689Smaybee void 70710298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 7086689Smaybee { 70910298SMatthew.Ahrens@Sun.COM ASSERT((ds->ds_owner == tag && ds->ds_dbuf) || 7106689Smaybee (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 7116689Smaybee 7125367Sahrens mutex_enter(&ds->ds_lock); 7136689Smaybee ds->ds_owner = NULL; 7146689Smaybee if (RW_WRITE_HELD(&ds->ds_rwlock)) { 7156689Smaybee rw_exit(&ds->ds_rwlock); 7166689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 7176689Smaybee } 7185367Sahrens mutex_exit(&ds->ds_lock); 7196689Smaybee if (ds->ds_dbuf) 72010298SMatthew.Ahrens@Sun.COM dsl_dataset_drop_ref(ds, tag); 7216689Smaybee else 7226689Smaybee dsl_dataset_evict(ds->ds_dbuf, ds); 7235367Sahrens } 7245367Sahrens 7255367Sahrens boolean_t 72610298SMatthew.Ahrens@Sun.COM dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag) 7275367Sahrens { 7286689Smaybee boolean_t gotit = FALSE; 7296689Smaybee 7305367Sahrens mutex_enter(&ds->ds_lock); 7316689Smaybee if (ds->ds_owner == NULL && 7326689Smaybee (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 73310298SMatthew.Ahrens@Sun.COM ds->ds_owner = tag; 7346689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 7356689Smaybee rw_exit(&ds->ds_rwlock); 7366689Smaybee gotit = TRUE; 7375367Sahrens } 7385367Sahrens mutex_exit(&ds->ds_lock); 7396689Smaybee return (gotit); 7406689Smaybee } 7416689Smaybee 7426689Smaybee void 7436689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 7446689Smaybee { 7456689Smaybee ASSERT3P(owner, ==, ds->ds_owner); 7466689Smaybee if (!RW_WRITE_HELD(&ds->ds_rwlock)) 7476689Smaybee rw_enter(&ds->ds_rwlock, RW_WRITER); 7485367Sahrens } 7495367Sahrens 7502199Sahrens uint64_t 7517046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 7526492Stimh uint64_t flags, dmu_tx_t *tx) 753789Sahrens { 7545367Sahrens dsl_pool_t *dp = dd->dd_pool; 755789Sahrens dmu_buf_t *dbuf; 756789Sahrens dsl_dataset_phys_t *dsphys; 7575367Sahrens uint64_t dsobj; 758789Sahrens objset_t *mos = dp->dp_meta_objset; 759789Sahrens 7607046Sahrens if (origin == NULL) 7617046Sahrens origin = dp->dp_origin_snap; 7627046Sahrens 7635367Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 7645367Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 765789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 7665367Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 767789Sahrens 768928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 769928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7701544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 771789Sahrens dmu_buf_will_dirty(dbuf, tx); 772789Sahrens dsphys = dbuf->db_data; 7736689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 774789Sahrens dsphys->ds_dir_obj = dd->dd_object; 7756492Stimh dsphys->ds_flags = flags; 776789Sahrens dsphys->ds_fsid_guid = unique_create(); 777789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 778789Sahrens sizeof (dsphys->ds_guid)); 779789Sahrens dsphys->ds_snapnames_zapobj = 7806492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7816492Stimh DMU_OT_NONE, 0, tx); 782789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 7837046Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 784789Sahrens dsphys->ds_deadlist_obj = 785789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7865378Sck153898 7875367Sahrens if (origin) { 7885367Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 789789Sahrens dsphys->ds_prev_snap_txg = 7905367Sahrens origin->ds_phys->ds_creation_txg; 791789Sahrens dsphys->ds_used_bytes = 7925367Sahrens origin->ds_phys->ds_used_bytes; 793789Sahrens dsphys->ds_compressed_bytes = 7945367Sahrens origin->ds_phys->ds_compressed_bytes; 795789Sahrens dsphys->ds_uncompressed_bytes = 7965367Sahrens origin->ds_phys->ds_uncompressed_bytes; 7975367Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 7986502Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 799789Sahrens 8005367Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 8015367Sahrens origin->ds_phys->ds_num_children++; 802789Sahrens 8037046Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 8047046Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 8057046Sahrens origin->ds_phys->ds_next_clones_obj = 8067046Sahrens zap_create(mos, 8077046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 8087046Sahrens } 8097046Sahrens VERIFY(0 == zap_add_int(mos, 8107046Sahrens origin->ds_phys->ds_next_clones_obj, 8117046Sahrens dsobj, tx)); 8127046Sahrens } 8137046Sahrens 814789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 8155367Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 816789Sahrens } 8176492Stimh 8186492Stimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 8196492Stimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 8206492Stimh 8211544Seschrock dmu_buf_rele(dbuf, FTAG); 822789Sahrens 823789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 824789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 8255367Sahrens 8265367Sahrens return (dsobj); 8275367Sahrens } 8285367Sahrens 8295367Sahrens uint64_t 8306492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 8316492Stimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8325367Sahrens { 8335367Sahrens dsl_pool_t *dp = pdd->dd_pool; 8345367Sahrens uint64_t dsobj, ddobj; 8355367Sahrens dsl_dir_t *dd; 8365367Sahrens 8375367Sahrens ASSERT(lastname[0] != '@'); 8385367Sahrens 8397046Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8405367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 8415367Sahrens 8427046Sahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx); 8435367Sahrens 8445367Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8455367Sahrens 846789Sahrens dsl_dir_close(dd, FTAG); 847789Sahrens 8482199Sahrens return (dsobj); 8492199Sahrens } 8502199Sahrens 8512199Sahrens struct destroyarg { 8522199Sahrens dsl_sync_task_group_t *dstg; 8532199Sahrens char *snapname; 8542199Sahrens char *failed; 85510242Schris.kirby@sun.com boolean_t defer; 8562199Sahrens }; 8572199Sahrens 8582199Sahrens static int 8592199Sahrens dsl_snapshot_destroy_one(char *name, void *arg) 8602199Sahrens { 8612199Sahrens struct destroyarg *da = arg; 8622199Sahrens dsl_dataset_t *ds; 8632199Sahrens int err; 86410242Schris.kirby@sun.com char *dsname; 86510272SMatthew.Ahrens@Sun.COM 86610272SMatthew.Ahrens@Sun.COM dsname = kmem_asprintf("%s@%s", name, da->snapname); 86710298SMatthew.Ahrens@Sun.COM err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds); 86810272SMatthew.Ahrens@Sun.COM strfree(dsname); 8696689Smaybee if (err == 0) { 87010242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda; 87110242Schris.kirby@sun.com 8726689Smaybee dsl_dataset_make_exclusive(ds, da->dstg); 87310298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) { 87410298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 87510298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 8767237Sek110237 } 87710242Schris.kirby@sun.com dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP); 87810242Schris.kirby@sun.com dsda->ds = ds; 87910242Schris.kirby@sun.com dsda->defer = da->defer; 8806689Smaybee dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 88110242Schris.kirby@sun.com dsl_dataset_destroy_sync, dsda, da->dstg, 0); 8826689Smaybee } else if (err == ENOENT) { 8836689Smaybee err = 0; 8846689Smaybee } else { 8852199Sahrens (void) strcpy(da->failed, name); 8862199Sahrens } 8876689Smaybee return (err); 888789Sahrens } 889789Sahrens 8902199Sahrens /* 8912199Sahrens * Destroy 'snapname' in all descendants of 'fsname'. 8922199Sahrens */ 8932199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 8942199Sahrens int 89510242Schris.kirby@sun.com dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer) 8962199Sahrens { 8972199Sahrens int err; 8982199Sahrens struct destroyarg da; 8992199Sahrens dsl_sync_task_t *dst; 9002199Sahrens spa_t *spa; 9012199Sahrens 9024603Sahrens err = spa_open(fsname, &spa, FTAG); 9032199Sahrens if (err) 9042199Sahrens return (err); 9052199Sahrens da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 9062199Sahrens da.snapname = snapname; 9072199Sahrens da.failed = fsname; 90810242Schris.kirby@sun.com da.defer = defer; 9092199Sahrens 9102199Sahrens err = dmu_objset_find(fsname, 9112417Sahrens dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 9122199Sahrens 9132199Sahrens if (err == 0) 9142199Sahrens err = dsl_sync_task_group_wait(da.dstg); 9152199Sahrens 9162199Sahrens for (dst = list_head(&da.dstg->dstg_tasks); dst; 9172199Sahrens dst = list_next(&da.dstg->dstg_tasks, dst)) { 91810242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = dst->dst_arg1; 91910242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 92010242Schris.kirby@sun.com 9216689Smaybee /* 9226689Smaybee * Return the file system name that triggered the error 9236689Smaybee */ 9242199Sahrens if (dst->dst_err) { 9252199Sahrens dsl_dataset_name(ds, fsname); 9264603Sahrens *strchr(fsname, '@') = '\0'; 9272199Sahrens } 92810242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 9296689Smaybee dsl_dataset_disown(ds, da.dstg); 93010242Schris.kirby@sun.com kmem_free(dsda, sizeof (struct dsl_ds_destroyarg)); 9312199Sahrens } 9322199Sahrens 9332199Sahrens dsl_sync_task_group_destroy(da.dstg); 9342199Sahrens spa_close(spa, FTAG); 9352199Sahrens return (err); 9362199Sahrens } 9372199Sahrens 93810242Schris.kirby@sun.com static boolean_t 93910242Schris.kirby@sun.com dsl_dataset_might_destroy_origin(dsl_dataset_t *ds) 94010242Schris.kirby@sun.com { 94110242Schris.kirby@sun.com boolean_t might_destroy = B_FALSE; 94210242Schris.kirby@sun.com 94310242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 94410242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 && 94510242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 94610242Schris.kirby@sun.com might_destroy = B_TRUE; 94710242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 94810242Schris.kirby@sun.com 94910242Schris.kirby@sun.com return (might_destroy); 95010242Schris.kirby@sun.com } 95110242Schris.kirby@sun.com 95210242Schris.kirby@sun.com /* 95310242Schris.kirby@sun.com * If we're removing a clone, and these three conditions are true: 95410242Schris.kirby@sun.com * 1) the clone's origin has no other children 95510242Schris.kirby@sun.com * 2) the clone's origin has no user references 95610242Schris.kirby@sun.com * 3) the clone's origin has been marked for deferred destruction 95710242Schris.kirby@sun.com * Then, prepare to remove the origin as part of this sync task group. 95810242Schris.kirby@sun.com */ 95910242Schris.kirby@sun.com static int 96010242Schris.kirby@sun.com dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag) 96110242Schris.kirby@sun.com { 96210242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 96310242Schris.kirby@sun.com dsl_dataset_t *origin = ds->ds_prev; 96410242Schris.kirby@sun.com 96510242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(origin)) { 96610242Schris.kirby@sun.com char *name; 96710242Schris.kirby@sun.com int namelen; 96810242Schris.kirby@sun.com int error; 96910242Schris.kirby@sun.com 97010242Schris.kirby@sun.com namelen = dsl_dataset_namelen(origin) + 1; 97110242Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 97210242Schris.kirby@sun.com dsl_dataset_name(origin, name); 97310242Schris.kirby@sun.com #ifdef _KERNEL 97410242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 97510242Schris.kirby@sun.com if (error) { 97610242Schris.kirby@sun.com kmem_free(name, namelen); 97710242Schris.kirby@sun.com return (error); 97810242Schris.kirby@sun.com } 97910242Schris.kirby@sun.com #endif 98010298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(name, B_TRUE, tag, &origin); 98110242Schris.kirby@sun.com kmem_free(name, namelen); 98210242Schris.kirby@sun.com if (error) 98310242Schris.kirby@sun.com return (error); 98410242Schris.kirby@sun.com dsda->rm_origin = origin; 98510242Schris.kirby@sun.com dsl_dataset_make_exclusive(origin, tag); 98610342Schris.kirby@sun.com 98710342Schris.kirby@sun.com if (origin->ds_objset != NULL) { 98810342Schris.kirby@sun.com dmu_objset_evict(origin->ds_objset); 98910342Schris.kirby@sun.com origin->ds_objset = NULL; 99010342Schris.kirby@sun.com } 99110242Schris.kirby@sun.com } 99210242Schris.kirby@sun.com 99310242Schris.kirby@sun.com return (0); 99410242Schris.kirby@sun.com } 99510242Schris.kirby@sun.com 9965367Sahrens /* 9976689Smaybee * ds must be opened as OWNER. On return (whether successful or not), 9986689Smaybee * ds will be closed and caller can no longer dereference it. 9995367Sahrens */ 1000789Sahrens int 100110242Schris.kirby@sun.com dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer) 1002789Sahrens { 1003789Sahrens int err; 10042199Sahrens dsl_sync_task_group_t *dstg; 10052199Sahrens objset_t *os; 1006789Sahrens dsl_dir_t *dd; 10072199Sahrens uint64_t obj; 100810242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 100910242Schris.kirby@sun.com 101010242Schris.kirby@sun.com dsda.ds = ds; 10112199Sahrens 10125367Sahrens if (dsl_dataset_is_snapshot(ds)) { 10132199Sahrens /* Destroying a snapshot is simpler */ 10146689Smaybee dsl_dataset_make_exclusive(ds, tag); 10157237Sek110237 101610298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) { 101710298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 101810298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 10197237Sek110237 } 102010242Schris.kirby@sun.com dsda.defer = defer; 10212199Sahrens err = dsl_sync_task_do(ds->ds_dir->dd_pool, 10222199Sahrens dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 102310242Schris.kirby@sun.com &dsda, tag, 0); 102410242Schris.kirby@sun.com ASSERT3P(dsda.rm_origin, ==, NULL); 10255367Sahrens goto out; 102610385Schris.kirby@sun.com } else if (defer) { 102710385Schris.kirby@sun.com err = EINVAL; 102810385Schris.kirby@sun.com goto out; 10292199Sahrens } 10302199Sahrens 10312199Sahrens dd = ds->ds_dir; 1032789Sahrens 10332199Sahrens /* 10342199Sahrens * Check for errors and mark this ds as inconsistent, in 10352199Sahrens * case we crash while freeing the objects. 10362199Sahrens */ 10372199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 10382199Sahrens dsl_dataset_destroy_begin_sync, ds, NULL, 0); 10395367Sahrens if (err) 10405367Sahrens goto out; 10415367Sahrens 104210298SMatthew.Ahrens@Sun.COM err = dmu_objset_from_ds(ds, &os); 10435367Sahrens if (err) 10445367Sahrens goto out; 10452199Sahrens 10462199Sahrens /* 10472199Sahrens * remove the objects in open context, so that we won't 10482199Sahrens * have too much to do in syncing context. 10492199Sahrens */ 10503025Sahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 10513025Sahrens ds->ds_phys->ds_prev_snap_txg)) { 10526992Smaybee /* 10536992Smaybee * Ignore errors, if there is not enough disk space 10546992Smaybee * we will deal with it in dsl_dataset_destroy_sync(). 10556992Smaybee */ 10566992Smaybee (void) dmu_free_object(os, obj); 10572199Sahrens } 10582199Sahrens 10599396SMatthew.Ahrens@Sun.COM /* 10609396SMatthew.Ahrens@Sun.COM * We need to sync out all in-flight IO before we try to evict 10619396SMatthew.Ahrens@Sun.COM * (the dataset evict func is trying to clear the cached entries 10629396SMatthew.Ahrens@Sun.COM * for this dataset in the ARC). 10639396SMatthew.Ahrens@Sun.COM */ 10649396SMatthew.Ahrens@Sun.COM txg_wait_synced(dd->dd_pool, 0); 10659396SMatthew.Ahrens@Sun.COM 10669396SMatthew.Ahrens@Sun.COM /* 10679396SMatthew.Ahrens@Sun.COM * If we managed to free all the objects in open 10689396SMatthew.Ahrens@Sun.COM * context, the user space accounting should be zero. 10699396SMatthew.Ahrens@Sun.COM */ 10709396SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_bp.blk_fill == 0 && 107110298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(os)) { 10729396SMatthew.Ahrens@Sun.COM uint64_t count; 10739396SMatthew.Ahrens@Sun.COM 10749396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 || 10759396SMatthew.Ahrens@Sun.COM count == 0); 10769396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 || 10779396SMatthew.Ahrens@Sun.COM count == 0); 10789396SMatthew.Ahrens@Sun.COM } 10799396SMatthew.Ahrens@Sun.COM 10802199Sahrens if (err != ESRCH) 10815367Sahrens goto out; 10822199Sahrens 10836975Smaybee rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 10846975Smaybee err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 10856975Smaybee rw_exit(&dd->dd_pool->dp_config_rwlock); 10866975Smaybee 10876975Smaybee if (err) 10886975Smaybee goto out; 10896975Smaybee 109010298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 10916689Smaybee /* 10926689Smaybee * We need to sync out all in-flight IO before we try 10936689Smaybee * to evict (the dataset evict func is trying to clear 10946689Smaybee * the cached entries for this dataset in the ARC). 10956689Smaybee */ 10966689Smaybee txg_wait_synced(dd->dd_pool, 0); 10975367Sahrens } 10985367Sahrens 10992199Sahrens /* 11002199Sahrens * Blow away the dsl_dir + head dataset. 11012199Sahrens */ 11026689Smaybee dsl_dataset_make_exclusive(ds, tag); 110310298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 110410298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 110510298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 11066975Smaybee } 110710242Schris.kirby@sun.com 110810242Schris.kirby@sun.com /* 110910242Schris.kirby@sun.com * If we're removing a clone, we might also need to remove its 111010242Schris.kirby@sun.com * origin. 111110242Schris.kirby@sun.com */ 111210242Schris.kirby@sun.com do { 111310242Schris.kirby@sun.com dsda.need_prep = B_FALSE; 111410242Schris.kirby@sun.com if (dsl_dir_is_clone(dd)) { 111510242Schris.kirby@sun.com err = dsl_dataset_origin_rm_prep(&dsda, tag); 111610242Schris.kirby@sun.com if (err) { 111710242Schris.kirby@sun.com dsl_dir_close(dd, FTAG); 111810242Schris.kirby@sun.com goto out; 111910242Schris.kirby@sun.com } 112010242Schris.kirby@sun.com } 112110242Schris.kirby@sun.com 112210242Schris.kirby@sun.com dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 112310242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 112410242Schris.kirby@sun.com dsl_dataset_destroy_sync, &dsda, tag, 0); 112510242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dir_destroy_check, 112610242Schris.kirby@sun.com dsl_dir_destroy_sync, dd, FTAG, 0); 112710242Schris.kirby@sun.com err = dsl_sync_task_group_wait(dstg); 112810242Schris.kirby@sun.com dsl_sync_task_group_destroy(dstg); 112910242Schris.kirby@sun.com 113010242Schris.kirby@sun.com /* 113110242Schris.kirby@sun.com * We could be racing against 'zfs release' or 'zfs destroy -d' 113210242Schris.kirby@sun.com * on the origin snap, in which case we can get EBUSY if we 113310242Schris.kirby@sun.com * needed to destroy the origin snap but were not ready to 113410242Schris.kirby@sun.com * do so. 113510242Schris.kirby@sun.com */ 113610242Schris.kirby@sun.com if (dsda.need_prep) { 113710242Schris.kirby@sun.com ASSERT(err == EBUSY); 113810242Schris.kirby@sun.com ASSERT(dsl_dir_is_clone(dd)); 113910242Schris.kirby@sun.com ASSERT(dsda.rm_origin == NULL); 114010242Schris.kirby@sun.com } 114110242Schris.kirby@sun.com } while (dsda.need_prep); 114210242Schris.kirby@sun.com 114310242Schris.kirby@sun.com if (dsda.rm_origin != NULL) 114410242Schris.kirby@sun.com dsl_dataset_disown(dsda.rm_origin, tag); 114510242Schris.kirby@sun.com 11466689Smaybee /* if it is successful, dsl_dir_destroy_sync will close the dd */ 11475367Sahrens if (err) 11482199Sahrens dsl_dir_close(dd, FTAG); 11495367Sahrens out: 11506689Smaybee dsl_dataset_disown(ds, tag); 1151789Sahrens return (err); 1152789Sahrens } 1153789Sahrens 11543547Smaybee blkptr_t * 11553547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1156789Sahrens { 11573547Smaybee return (&ds->ds_phys->ds_bp); 1158789Sahrens } 1159789Sahrens 1160789Sahrens void 1161789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1162789Sahrens { 1163789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1164789Sahrens /* If it's the meta-objset, set dp_meta_rootbp */ 1165789Sahrens if (ds == NULL) { 1166789Sahrens tx->tx_pool->dp_meta_rootbp = *bp; 1167789Sahrens } else { 1168789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1169789Sahrens ds->ds_phys->ds_bp = *bp; 1170789Sahrens } 1171789Sahrens } 1172789Sahrens 1173789Sahrens spa_t * 1174789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 1175789Sahrens { 1176789Sahrens return (ds->ds_dir->dd_pool->dp_spa); 1177789Sahrens } 1178789Sahrens 1179789Sahrens void 1180789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1181789Sahrens { 1182789Sahrens dsl_pool_t *dp; 1183789Sahrens 1184789Sahrens if (ds == NULL) /* this is the meta-objset */ 1185789Sahrens return; 1186789Sahrens 118710298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 11882885Sahrens 11892885Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 11902885Sahrens panic("dirtying snapshot!"); 1191789Sahrens 1192789Sahrens dp = ds->ds_dir->dd_pool; 1193789Sahrens 1194789Sahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1195789Sahrens /* up the hold count until we can be written out */ 1196789Sahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 1197789Sahrens } 1198789Sahrens } 1199789Sahrens 12005378Sck153898 /* 12015378Sck153898 * The unique space in the head dataset can be calculated by subtracting 12025378Sck153898 * the space used in the most recent snapshot, that is still being used 12035378Sck153898 * in this file system, from the space currently in use. To figure out 12045378Sck153898 * the space in the most recent snapshot still in use, we need to take 12055378Sck153898 * the total space used in the snapshot and subtract out the space that 12065378Sck153898 * has been freed up since the snapshot was taken. 12075378Sck153898 */ 12085378Sck153898 static void 12095378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 12105378Sck153898 { 12115378Sck153898 uint64_t mrs_used; 12125378Sck153898 uint64_t dlused, dlcomp, dluncomp; 12135378Sck153898 12145378Sck153898 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 12155378Sck153898 12165378Sck153898 if (ds->ds_phys->ds_prev_snap_obj != 0) 12175378Sck153898 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 12185378Sck153898 else 12195378Sck153898 mrs_used = 0; 12205378Sck153898 12215378Sck153898 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 12225378Sck153898 &dluncomp)); 12235378Sck153898 12245378Sck153898 ASSERT3U(dlused, <=, mrs_used); 12255378Sck153898 ds->ds_phys->ds_unique_bytes = 12265378Sck153898 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 12275378Sck153898 12285378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && 12295378Sck153898 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 12305378Sck153898 SPA_VERSION_UNIQUE_ACCURATE) 12315378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 12325378Sck153898 } 12335378Sck153898 12345378Sck153898 static uint64_t 12355378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds) 12365378Sck153898 { 12375378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 12385378Sck153898 dsl_dataset_recalc_head_uniq(ds); 12395378Sck153898 12405378Sck153898 return (ds->ds_phys->ds_unique_bytes); 12415378Sck153898 } 12425378Sck153898 1243789Sahrens struct killarg { 12447390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 1245789Sahrens dmu_tx_t *tx; 1246789Sahrens }; 1247789Sahrens 12487390SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 1249789Sahrens static int 1250*10922SJeff.Bonwick@Sun.COM kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 1251*10922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1252789Sahrens { 1253789Sahrens struct killarg *ka = arg; 1254*10922SJeff.Bonwick@Sun.COM dmu_tx_t *tx = ka->tx; 12557837SMatthew.Ahrens@Sun.COM 12567837SMatthew.Ahrens@Sun.COM if (bp == NULL) 12577837SMatthew.Ahrens@Sun.COM return (0); 1258789Sahrens 1259*10922SJeff.Bonwick@Sun.COM if (zb->zb_level == ZB_ZIL_LEVEL) { 1260*10922SJeff.Bonwick@Sun.COM ASSERT(zilog != NULL); 12618746SMatthew.Ahrens@Sun.COM /* 12628746SMatthew.Ahrens@Sun.COM * It's a block in the intent log. It has no 12638746SMatthew.Ahrens@Sun.COM * accounting, so just free it. 12648746SMatthew.Ahrens@Sun.COM */ 1265*10922SJeff.Bonwick@Sun.COM dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp); 12668746SMatthew.Ahrens@Sun.COM } else { 1267*10922SJeff.Bonwick@Sun.COM ASSERT(zilog == NULL); 12688746SMatthew.Ahrens@Sun.COM ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg); 1269*10922SJeff.Bonwick@Sun.COM (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE); 12708746SMatthew.Ahrens@Sun.COM } 12717390SMatthew.Ahrens@Sun.COM 1272789Sahrens return (0); 1273789Sahrens } 1274789Sahrens 1275789Sahrens /* ARGSUSED */ 12762199Sahrens static int 12772199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 12781731Sbonwick { 12792199Sahrens dsl_dataset_t *ds = arg1; 12805367Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 12815367Sahrens uint64_t count; 12825367Sahrens int err; 12831731Sbonwick 12841731Sbonwick /* 12851731Sbonwick * Can't delete a head dataset if there are snapshots of it. 12861731Sbonwick * (Except if the only snapshots are from the branch we cloned 12871731Sbonwick * from.) 12881731Sbonwick */ 12891731Sbonwick if (ds->ds_prev != NULL && 12901731Sbonwick ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 129110816SVitezslav.Batrla@Sun.COM return (EBUSY); 12921731Sbonwick 12935367Sahrens /* 12945367Sahrens * This is really a dsl_dir thing, but check it here so that 12955367Sahrens * we'll be less likely to leave this dataset inconsistent & 12965367Sahrens * nearly destroyed. 12975367Sahrens */ 12985367Sahrens err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 12995367Sahrens if (err) 13005367Sahrens return (err); 13015367Sahrens if (count != 0) 13025367Sahrens return (EEXIST); 13035367Sahrens 13041731Sbonwick return (0); 13051731Sbonwick } 13061731Sbonwick 13072199Sahrens /* ARGSUSED */ 13082199Sahrens static void 13094543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1310789Sahrens { 13112199Sahrens dsl_dataset_t *ds = arg1; 13124543Smarks dsl_pool_t *dp = ds->ds_dir->dd_pool; 1313789Sahrens 13142199Sahrens /* Mark it as inconsistent on-disk, in case we crash */ 13152199Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13162199Sahrens ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 13174543Smarks 13184543Smarks spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 13194543Smarks cr, "dataset = %llu", ds->ds_object); 13202199Sahrens } 1321789Sahrens 132210242Schris.kirby@sun.com static int 132310242Schris.kirby@sun.com dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag, 132410242Schris.kirby@sun.com dmu_tx_t *tx) 132510242Schris.kirby@sun.com { 132610242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 132710242Schris.kirby@sun.com dsl_dataset_t *ds_prev = ds->ds_prev; 132810242Schris.kirby@sun.com 132910242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(ds_prev)) { 133010242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 133110242Schris.kirby@sun.com 133210242Schris.kirby@sun.com /* 133310242Schris.kirby@sun.com * If we're not prepared to remove the origin, don't remove 133410242Schris.kirby@sun.com * the clone either. 133510242Schris.kirby@sun.com */ 133610242Schris.kirby@sun.com if (dsda->rm_origin == NULL) { 133710242Schris.kirby@sun.com dsda->need_prep = B_TRUE; 133810242Schris.kirby@sun.com return (EBUSY); 133910242Schris.kirby@sun.com } 134010242Schris.kirby@sun.com 134110242Schris.kirby@sun.com ndsda.ds = ds_prev; 134210242Schris.kirby@sun.com ndsda.is_origin_rm = B_TRUE; 134310242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&ndsda, tag, tx)); 134410242Schris.kirby@sun.com } 134510242Schris.kirby@sun.com 134610242Schris.kirby@sun.com /* 134710242Schris.kirby@sun.com * If we're not going to remove the origin after all, 134810242Schris.kirby@sun.com * undo the open context setup. 134910242Schris.kirby@sun.com */ 135010242Schris.kirby@sun.com if (dsda->rm_origin != NULL) { 135110242Schris.kirby@sun.com dsl_dataset_disown(dsda->rm_origin, tag); 135210242Schris.kirby@sun.com dsda->rm_origin = NULL; 135310242Schris.kirby@sun.com } 135410242Schris.kirby@sun.com 135510242Schris.kirby@sun.com return (0); 135610242Schris.kirby@sun.com } 135710242Schris.kirby@sun.com 13582199Sahrens /* ARGSUSED */ 13595367Sahrens int 13602199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 13612199Sahrens { 136210242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 136310242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 1364789Sahrens 13656689Smaybee /* we have an owner hold, so noone else can destroy us */ 13666689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 13676689Smaybee 136810242Schris.kirby@sun.com /* 136910242Schris.kirby@sun.com * Only allow deferred destroy on pools that support it. 137010242Schris.kirby@sun.com * NOTE: deferred destroy is only supported on snapshots. 137110242Schris.kirby@sun.com */ 137210242Schris.kirby@sun.com if (dsda->defer) { 137310242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 137410242Schris.kirby@sun.com SPA_VERSION_USERREFS) 137510242Schris.kirby@sun.com return (ENOTSUP); 137610242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 137710242Schris.kirby@sun.com return (0); 137810242Schris.kirby@sun.com } 1379789Sahrens 1380789Sahrens /* 1381789Sahrens * Can't delete a head dataset if there are snapshots of it. 1382789Sahrens * (Except if the only snapshots are from the branch we cloned 1383789Sahrens * from.) 1384789Sahrens */ 1385789Sahrens if (ds->ds_prev != NULL && 13862199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 138710816SVitezslav.Batrla@Sun.COM return (EBUSY); 1388789Sahrens 1389789Sahrens /* 1390789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1391789Sahrens * them. Try again. 1392789Sahrens */ 13932199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1394789Sahrens return (EAGAIN); 13952199Sahrens 139610242Schris.kirby@sun.com if (dsl_dataset_is_snapshot(ds)) { 139710242Schris.kirby@sun.com /* 139810242Schris.kirby@sun.com * If this snapshot has an elevated user reference count, 139910242Schris.kirby@sun.com * we can't destroy it yet. 140010242Schris.kirby@sun.com */ 140110242Schris.kirby@sun.com if (ds->ds_userrefs > 0 && !dsda->releasing) 140210242Schris.kirby@sun.com return (EBUSY); 140310242Schris.kirby@sun.com 140410242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 140510242Schris.kirby@sun.com /* 140610242Schris.kirby@sun.com * Can't delete a branch point. However, if we're destroying 140710242Schris.kirby@sun.com * a clone and removing its origin due to it having a user 140810242Schris.kirby@sun.com * hold count of 0 and having been marked for deferred destroy, 140910242Schris.kirby@sun.com * it's OK for the origin to have a single clone. 141010242Schris.kirby@sun.com */ 141110242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children > 141210242Schris.kirby@sun.com (dsda->is_origin_rm ? 2 : 1)) { 141310242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 141410242Schris.kirby@sun.com return (EEXIST); 141510242Schris.kirby@sun.com } 141610242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 141710242Schris.kirby@sun.com } else if (dsl_dir_is_clone(ds->ds_dir)) { 141810242Schris.kirby@sun.com return (dsl_dataset_origin_check(dsda, arg2, tx)); 141910242Schris.kirby@sun.com } 142010242Schris.kirby@sun.com 14212199Sahrens /* XXX we should do some i/o error checking... */ 14222199Sahrens return (0); 14232199Sahrens } 14242199Sahrens 14256689Smaybee struct refsarg { 14266689Smaybee kmutex_t lock; 14276689Smaybee boolean_t gone; 14286689Smaybee kcondvar_t cv; 14296689Smaybee }; 14306689Smaybee 14316689Smaybee /* ARGSUSED */ 14326689Smaybee static void 14336689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 14346689Smaybee { 14356689Smaybee struct refsarg *arg = argv; 14366689Smaybee 14376689Smaybee mutex_enter(&arg->lock); 14386689Smaybee arg->gone = TRUE; 14396689Smaybee cv_signal(&arg->cv); 14406689Smaybee mutex_exit(&arg->lock); 14416689Smaybee } 14426689Smaybee 14436689Smaybee static void 14446689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 14456689Smaybee { 14466689Smaybee struct refsarg arg; 14476689Smaybee 14486689Smaybee mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 14496689Smaybee cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 14506689Smaybee arg.gone = FALSE; 14516689Smaybee (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 14526689Smaybee dsl_dataset_refs_gone); 14536689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 14546689Smaybee mutex_enter(&arg.lock); 14556689Smaybee while (!arg.gone) 14566689Smaybee cv_wait(&arg.cv, &arg.lock); 14576689Smaybee ASSERT(arg.gone); 14586689Smaybee mutex_exit(&arg.lock); 14596689Smaybee ds->ds_dbuf = NULL; 14606689Smaybee ds->ds_phys = NULL; 14616689Smaybee mutex_destroy(&arg.lock); 14626689Smaybee cv_destroy(&arg.cv); 14636689Smaybee } 14646689Smaybee 146510801SMatthew.Ahrens@Sun.COM static void 146610801SMatthew.Ahrens@Sun.COM remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx) 146710801SMatthew.Ahrens@Sun.COM { 146810801SMatthew.Ahrens@Sun.COM objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 146910801SMatthew.Ahrens@Sun.COM uint64_t count; 147010801SMatthew.Ahrens@Sun.COM int err; 147110801SMatthew.Ahrens@Sun.COM 147210801SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_phys->ds_num_children >= 2); 147310801SMatthew.Ahrens@Sun.COM err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 147410801SMatthew.Ahrens@Sun.COM /* 147510801SMatthew.Ahrens@Sun.COM * The err should not be ENOENT, but a bug in a previous version 147610801SMatthew.Ahrens@Sun.COM * of the code could cause upgrade_clones_cb() to not set 147710801SMatthew.Ahrens@Sun.COM * ds_next_snap_obj when it should, leading to a missing entry. 147810801SMatthew.Ahrens@Sun.COM * If we knew that the pool was created after 147910801SMatthew.Ahrens@Sun.COM * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 148010801SMatthew.Ahrens@Sun.COM * ENOENT. However, at least we can check that we don't have 148110801SMatthew.Ahrens@Sun.COM * too many entries in the next_clones_obj even after failing to 148210801SMatthew.Ahrens@Sun.COM * remove this one. 148310801SMatthew.Ahrens@Sun.COM */ 148410801SMatthew.Ahrens@Sun.COM if (err != ENOENT) { 148510801SMatthew.Ahrens@Sun.COM VERIFY3U(err, ==, 0); 148610801SMatthew.Ahrens@Sun.COM } 148710801SMatthew.Ahrens@Sun.COM ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj, 148810801SMatthew.Ahrens@Sun.COM &count)); 148910801SMatthew.Ahrens@Sun.COM ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 149010801SMatthew.Ahrens@Sun.COM } 149110801SMatthew.Ahrens@Sun.COM 14925367Sahrens void 14934543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 14942199Sahrens { 149510242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 149610242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 14972199Sahrens int err; 14982199Sahrens int after_branch_point = FALSE; 14992199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 15002199Sahrens objset_t *mos = dp->dp_meta_objset; 15012199Sahrens dsl_dataset_t *ds_prev = NULL; 15022199Sahrens uint64_t obj; 15032199Sahrens 15046689Smaybee ASSERT(ds->ds_owner); 150510242Schris.kirby@sun.com ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1); 15062199Sahrens ASSERT(ds->ds_prev == NULL || 15072199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 15082199Sahrens ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 15092199Sahrens 151010242Schris.kirby@sun.com if (dsda->defer) { 151110242Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 151210242Schris.kirby@sun.com if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) { 151310242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 151410242Schris.kirby@sun.com ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY; 151510242Schris.kirby@sun.com return; 151610242Schris.kirby@sun.com } 151710242Schris.kirby@sun.com } 151810242Schris.kirby@sun.com 15196689Smaybee /* signal any waiters that this dataset is going away */ 15206689Smaybee mutex_enter(&ds->ds_lock); 15216689Smaybee ds->ds_owner = dsl_reaper; 15226689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 15236689Smaybee mutex_exit(&ds->ds_lock); 15246689Smaybee 15255378Sck153898 /* Remove our reservation */ 15265378Sck153898 if (ds->ds_reserved != 0) { 15275378Sck153898 uint64_t val = 0; 15285378Sck153898 dsl_dataset_set_reservation_sync(ds, &val, cr, tx); 15295378Sck153898 ASSERT3U(ds->ds_reserved, ==, 0); 15305378Sck153898 } 15315378Sck153898 15322199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 15332199Sahrens 15347046Sahrens dsl_pool_ds_destroyed(ds, tx); 15357046Sahrens 15362199Sahrens obj = ds->ds_object; 1537789Sahrens 1538789Sahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 1539789Sahrens if (ds->ds_prev) { 1540789Sahrens ds_prev = ds->ds_prev; 1541789Sahrens } else { 15426689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15436689Smaybee ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1544789Sahrens } 1545789Sahrens after_branch_point = 1546789Sahrens (ds_prev->ds_phys->ds_next_snap_obj != obj); 1547789Sahrens 1548789Sahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1549789Sahrens if (after_branch_point && 15507046Sahrens ds_prev->ds_phys->ds_next_clones_obj != 0) { 155110801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds_prev, obj, tx); 15527046Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15537046Sahrens VERIFY(0 == zap_add_int(mos, 15547046Sahrens ds_prev->ds_phys->ds_next_clones_obj, 15557046Sahrens ds->ds_phys->ds_next_snap_obj, tx)); 15567046Sahrens } 15577046Sahrens } 15587046Sahrens if (after_branch_point && 1559789Sahrens ds->ds_phys->ds_next_snap_obj == 0) { 1560789Sahrens /* This clone is toast. */ 1561789Sahrens ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1562789Sahrens ds_prev->ds_phys->ds_num_children--; 156310242Schris.kirby@sun.com 156410242Schris.kirby@sun.com /* 156510242Schris.kirby@sun.com * If the clone's origin has no other clones, no 156610242Schris.kirby@sun.com * user holds, and has been marked for deferred 156710242Schris.kirby@sun.com * deletion, then we should have done the necessary 156810242Schris.kirby@sun.com * destroy setup for it. 156910242Schris.kirby@sun.com */ 157010242Schris.kirby@sun.com if (ds_prev->ds_phys->ds_num_children == 1 && 157110242Schris.kirby@sun.com ds_prev->ds_userrefs == 0 && 157210242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds_prev)) { 157310242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, !=, NULL); 157410242Schris.kirby@sun.com } else { 157510242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 157610242Schris.kirby@sun.com } 1577789Sahrens } else if (!after_branch_point) { 1578789Sahrens ds_prev->ds_phys->ds_next_snap_obj = 1579789Sahrens ds->ds_phys->ds_next_snap_obj; 1580789Sahrens } 1581789Sahrens } 1582789Sahrens 1583789Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15842199Sahrens blkptr_t bp; 1585789Sahrens dsl_dataset_t *ds_next; 1586789Sahrens uint64_t itor = 0; 15875378Sck153898 uint64_t old_unique; 15887390SMatthew.Ahrens@Sun.COM int64_t used = 0, compressed = 0, uncompressed = 0; 1589789Sahrens 15906689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15916689Smaybee ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1592789Sahrens ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1593789Sahrens 15945378Sck153898 old_unique = dsl_dataset_unique(ds_next); 15955378Sck153898 1596789Sahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1597789Sahrens ds_next->ds_phys->ds_prev_snap_obj = 1598789Sahrens ds->ds_phys->ds_prev_snap_obj; 1599789Sahrens ds_next->ds_phys->ds_prev_snap_txg = 1600789Sahrens ds->ds_phys->ds_prev_snap_txg; 1601789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1602789Sahrens ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1603789Sahrens 1604789Sahrens /* 1605789Sahrens * Transfer to our deadlist (which will become next's 1606789Sahrens * new deadlist) any entries from next's current 1607789Sahrens * deadlist which were born before prev, and free the 1608789Sahrens * other entries. 1609789Sahrens * 1610789Sahrens * XXX we're doing this long task with the config lock held 1611789Sahrens */ 16126689Smaybee while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1613789Sahrens if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 16141544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 16151544Seschrock &bp, tx)); 1616789Sahrens if (ds_prev && !after_branch_point && 1617789Sahrens bp.blk_birth > 1618789Sahrens ds_prev->ds_phys->ds_prev_snap_txg) { 1619789Sahrens ds_prev->ds_phys->ds_unique_bytes += 1620*10922SJeff.Bonwick@Sun.COM bp_get_dsize_sync(dp->dp_spa, &bp); 1621789Sahrens } 1622789Sahrens } else { 1623*10922SJeff.Bonwick@Sun.COM used += bp_get_dsize_sync(dp->dp_spa, &bp); 1624789Sahrens compressed += BP_GET_PSIZE(&bp); 1625789Sahrens uncompressed += BP_GET_UCSIZE(&bp); 1626*10922SJeff.Bonwick@Sun.COM dsl_free(dp, tx->tx_txg, &bp); 1627789Sahrens } 1628789Sahrens } 1629789Sahrens 16307390SMatthew.Ahrens@Sun.COM ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 16317390SMatthew.Ahrens@Sun.COM 16327390SMatthew.Ahrens@Sun.COM /* change snapused */ 16337390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 16347390SMatthew.Ahrens@Sun.COM -used, -compressed, -uncompressed, tx); 16357390SMatthew.Ahrens@Sun.COM 1636789Sahrens /* free next's deadlist */ 1637789Sahrens bplist_close(&ds_next->ds_deadlist); 1638789Sahrens bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1639789Sahrens 1640789Sahrens /* set next's deadlist to our deadlist */ 16416689Smaybee bplist_close(&ds->ds_deadlist); 1642789Sahrens ds_next->ds_phys->ds_deadlist_obj = 1643789Sahrens ds->ds_phys->ds_deadlist_obj; 16441544Seschrock VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 16451544Seschrock ds_next->ds_phys->ds_deadlist_obj)); 1646789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1647789Sahrens 1648789Sahrens if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1649789Sahrens /* 1650789Sahrens * Update next's unique to include blocks which 1651789Sahrens * were previously shared by only this snapshot 1652789Sahrens * and it. Those blocks will be born after the 1653789Sahrens * prev snap and before this snap, and will have 1654789Sahrens * died after the next snap and before the one 1655789Sahrens * after that (ie. be on the snap after next's 1656789Sahrens * deadlist). 1657789Sahrens * 1658789Sahrens * XXX we're doing this long task with the 1659789Sahrens * config lock held 1660789Sahrens */ 1661789Sahrens dsl_dataset_t *ds_after_next; 16627390SMatthew.Ahrens@Sun.COM uint64_t space; 1663789Sahrens 16646689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16656689Smaybee ds_next->ds_phys->ds_next_snap_obj, 16666689Smaybee FTAG, &ds_after_next)); 16677390SMatthew.Ahrens@Sun.COM 16687390SMatthew.Ahrens@Sun.COM VERIFY(0 == 16697390SMatthew.Ahrens@Sun.COM bplist_space_birthrange(&ds_after_next->ds_deadlist, 16707390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_prev_snap_txg, 16717390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_creation_txg, &space)); 16727390SMatthew.Ahrens@Sun.COM ds_next->ds_phys->ds_unique_bytes += space; 1673789Sahrens 16746689Smaybee dsl_dataset_rele(ds_after_next, FTAG); 1675789Sahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 1676789Sahrens } else { 1677789Sahrens ASSERT3P(ds_next->ds_prev, ==, ds); 16786689Smaybee dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 16796689Smaybee ds_next->ds_prev = NULL; 1680789Sahrens if (ds_prev) { 16816689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 16826689Smaybee ds->ds_phys->ds_prev_snap_obj, 16836689Smaybee ds_next, &ds_next->ds_prev)); 1684789Sahrens } 16855378Sck153898 16865378Sck153898 dsl_dataset_recalc_head_uniq(ds_next); 16875378Sck153898 16885378Sck153898 /* 16895378Sck153898 * Reduce the amount of our unconsmed refreservation 16905378Sck153898 * being charged to our parent by the amount of 16915378Sck153898 * new unique data we have gained. 16925378Sck153898 */ 16935378Sck153898 if (old_unique < ds_next->ds_reserved) { 16945378Sck153898 int64_t mrsdelta; 16955378Sck153898 uint64_t new_unique = 16965378Sck153898 ds_next->ds_phys->ds_unique_bytes; 16975378Sck153898 16985378Sck153898 ASSERT(old_unique <= new_unique); 16995378Sck153898 mrsdelta = MIN(new_unique - old_unique, 17005378Sck153898 ds_next->ds_reserved - old_unique); 17017390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, 17027390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 17035378Sck153898 } 1704789Sahrens } 17056689Smaybee dsl_dataset_rele(ds_next, FTAG); 1706789Sahrens } else { 1707789Sahrens /* 1708789Sahrens * There's no next snapshot, so this is a head dataset. 1709789Sahrens * Destroy the deadlist. Unless it's a clone, the 1710789Sahrens * deadlist should be empty. (If it's a clone, it's 1711789Sahrens * safe to ignore the deadlist contents.) 1712789Sahrens */ 1713789Sahrens struct killarg ka; 1714789Sahrens 1715789Sahrens ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1716789Sahrens bplist_close(&ds->ds_deadlist); 1717789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1718789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1719789Sahrens 1720789Sahrens /* 1721789Sahrens * Free everything that we point to (that's born after 1722789Sahrens * the previous snapshot, if we are a clone) 1723789Sahrens * 17247390SMatthew.Ahrens@Sun.COM * NB: this should be very quick, because we already 17257390SMatthew.Ahrens@Sun.COM * freed all the objects in open context. 1726789Sahrens */ 17277390SMatthew.Ahrens@Sun.COM ka.ds = ds; 1728789Sahrens ka.tx = tx; 17297837SMatthew.Ahrens@Sun.COM err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 17307837SMatthew.Ahrens@Sun.COM TRAVERSE_POST, kill_blkptr, &ka); 1731789Sahrens ASSERT3U(err, ==, 0); 17329390Schris.kirby@sun.com ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 17337390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_unique_bytes == 0); 173410342Schris.kirby@sun.com 173510342Schris.kirby@sun.com if (ds->ds_prev != NULL) { 173610342Schris.kirby@sun.com dsl_dataset_rele(ds->ds_prev, ds); 173710342Schris.kirby@sun.com ds->ds_prev = ds_prev = NULL; 173810342Schris.kirby@sun.com } 1739789Sahrens } 1740789Sahrens 17416689Smaybee if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 17426689Smaybee /* Erase the link in the dir */ 17436689Smaybee dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 17446689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 17456689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1746789Sahrens err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1747789Sahrens ASSERT(err == 0); 1748789Sahrens } else { 1749789Sahrens /* remove from snapshot namespace */ 1750789Sahrens dsl_dataset_t *ds_head; 17516689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 17526689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 17536689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 17542207Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 1755789Sahrens #ifdef ZFS_DEBUG 1756789Sahrens { 1757789Sahrens uint64_t val; 17586492Stimh 17596689Smaybee err = dsl_dataset_snap_lookup(ds_head, 17606492Stimh ds->ds_snapname, &val); 1761789Sahrens ASSERT3U(err, ==, 0); 1762789Sahrens ASSERT3U(val, ==, obj); 1763789Sahrens } 1764789Sahrens #endif 17656689Smaybee err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1766789Sahrens ASSERT(err == 0); 17676689Smaybee dsl_dataset_rele(ds_head, FTAG); 1768789Sahrens } 1769789Sahrens 1770789Sahrens if (ds_prev && ds->ds_prev != ds_prev) 17716689Smaybee dsl_dataset_rele(ds_prev, FTAG); 1772789Sahrens 17735094Slling spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 17744543Smarks spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 17754543Smarks cr, "dataset = %llu", ds->ds_object); 17764543Smarks 17777046Sahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 17787046Sahrens uint64_t count; 17797046Sahrens ASSERT(0 == zap_count(mos, 17807046Sahrens ds->ds_phys->ds_next_clones_obj, &count) && count == 0); 17817046Sahrens VERIFY(0 == dmu_object_free(mos, 17827046Sahrens ds->ds_phys->ds_next_clones_obj, tx)); 17837046Sahrens } 17847390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_props_obj != 0) 17857390SMatthew.Ahrens@Sun.COM VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx)); 178610242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) 178710242Schris.kirby@sun.com VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx)); 17886689Smaybee dsl_dir_close(ds->ds_dir, ds); 17896689Smaybee ds->ds_dir = NULL; 17906689Smaybee dsl_dataset_drain_refs(ds, tag); 17912199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 179210242Schris.kirby@sun.com 179310242Schris.kirby@sun.com if (dsda->rm_origin) { 179410242Schris.kirby@sun.com /* 179510242Schris.kirby@sun.com * Remove the origin of the clone we just destroyed. 179610242Schris.kirby@sun.com */ 179710242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 179810242Schris.kirby@sun.com 179910342Schris.kirby@sun.com ndsda.ds = dsda->rm_origin; 180010242Schris.kirby@sun.com dsl_dataset_destroy_sync(&ndsda, tag, cr, tx); 180110242Schris.kirby@sun.com } 18022199Sahrens } 18032199Sahrens 18045378Sck153898 static int 18055378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 18065378Sck153898 { 18075378Sck153898 uint64_t asize; 18085378Sck153898 18095378Sck153898 if (!dmu_tx_is_syncing(tx)) 18105378Sck153898 return (0); 18115378Sck153898 18125378Sck153898 /* 18135378Sck153898 * If there's an fs-only reservation, any blocks that might become 18145378Sck153898 * owned by the snapshot dataset must be accommodated by space 18155378Sck153898 * outside of the reservation. 18165378Sck153898 */ 18175378Sck153898 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 18185378Sck153898 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 18195378Sck153898 return (ENOSPC); 18205378Sck153898 18215378Sck153898 /* 18225378Sck153898 * Propogate any reserved space for this snapshot to other 18235378Sck153898 * snapshot checks in this sync group. 18245378Sck153898 */ 18255378Sck153898 if (asize > 0) 18265378Sck153898 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 18275378Sck153898 18285378Sck153898 return (0); 18295378Sck153898 } 18305378Sck153898 18312199Sahrens /* ARGSUSED */ 18322199Sahrens int 18332199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 18342199Sahrens { 18355367Sahrens dsl_dataset_t *ds = arg1; 18362199Sahrens const char *snapname = arg2; 18372199Sahrens int err; 18382199Sahrens uint64_t value; 1839789Sahrens 1840789Sahrens /* 18412199Sahrens * We don't allow multiple snapshots of the same txg. If there 18422199Sahrens * is already one, try again. 18432199Sahrens */ 18442199Sahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 18452199Sahrens return (EAGAIN); 18462199Sahrens 18472199Sahrens /* 18482199Sahrens * Check for conflicting name snapshot name. 1849789Sahrens */ 18506689Smaybee err = dsl_dataset_snap_lookup(ds, snapname, &value); 18512199Sahrens if (err == 0) 18522199Sahrens return (EEXIST); 18532199Sahrens if (err != ENOENT) 18542199Sahrens return (err); 1855789Sahrens 18563978Smmusante /* 18573978Smmusante * Check that the dataset's name is not too long. Name consists 18583978Smmusante * of the dataset's length + 1 for the @-sign + snapshot name's length 18593978Smmusante */ 18603978Smmusante if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 18613978Smmusante return (ENAMETOOLONG); 18623978Smmusante 18635378Sck153898 err = dsl_dataset_snapshot_reserve_space(ds, tx); 18645378Sck153898 if (err) 18655378Sck153898 return (err); 18665378Sck153898 18672199Sahrens ds->ds_trysnap_txg = tx->tx_txg; 1868789Sahrens return (0); 1869789Sahrens } 1870789Sahrens 18712199Sahrens void 18724543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1873789Sahrens { 18745367Sahrens dsl_dataset_t *ds = arg1; 18752199Sahrens const char *snapname = arg2; 18762199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1877789Sahrens dmu_buf_t *dbuf; 1878789Sahrens dsl_dataset_phys_t *dsphys; 18797046Sahrens uint64_t dsobj, crtxg; 1880789Sahrens objset_t *mos = dp->dp_meta_objset; 1881789Sahrens int err; 1882789Sahrens 18832199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1884789Sahrens 18857046Sahrens /* 18867046Sahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 18877046Sahrens */ 18887046Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 18897046Sahrens crtxg = 1; 18907046Sahrens else 18917046Sahrens crtxg = tx->tx_txg; 18927046Sahrens 1893928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1894928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 18951544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1896789Sahrens dmu_buf_will_dirty(dbuf, tx); 1897789Sahrens dsphys = dbuf->db_data; 18986689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 18992199Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1900789Sahrens dsphys->ds_fsid_guid = unique_create(); 1901789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1902789Sahrens sizeof (dsphys->ds_guid)); 1903789Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1904789Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1905789Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1906789Sahrens dsphys->ds_num_children = 1; 1907789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 19087046Sahrens dsphys->ds_creation_txg = crtxg; 1909789Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1910789Sahrens dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1911789Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1912789Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 19132082Seschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1914789Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 19151544Seschrock dmu_buf_rele(dbuf, FTAG); 1916789Sahrens 19172199Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 19182199Sahrens if (ds->ds_prev) { 19197046Sahrens uint64_t next_clones_obj = 19207046Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 19212199Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1922789Sahrens ds->ds_object || 19232199Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 19242199Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 19252199Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1926789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 19272199Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 19282199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 19297046Sahrens } else if (next_clones_obj != 0) { 193010801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds->ds_prev, 193110801SMatthew.Ahrens@Sun.COM dsphys->ds_next_snap_obj, tx); 19327046Sahrens VERIFY3U(0, ==, zap_add_int(mos, 19337046Sahrens next_clones_obj, dsobj, tx)); 1934789Sahrens } 1935789Sahrens } 1936789Sahrens 19375378Sck153898 /* 19385378Sck153898 * If we have a reference-reservation on this dataset, we will 19395378Sck153898 * need to increase the amount of refreservation being charged 19405378Sck153898 * since our unique space is going to zero. 19415378Sck153898 */ 19425378Sck153898 if (ds->ds_reserved) { 19435378Sck153898 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 19447390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 19457390SMatthew.Ahrens@Sun.COM add, 0, 0, tx); 19465378Sck153898 } 19475378Sck153898 1948789Sahrens bplist_close(&ds->ds_deadlist); 1949789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 19505712Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1951789Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 19527046Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1953789Sahrens ds->ds_phys->ds_unique_bytes = 0; 19545378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 19555378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1956789Sahrens ds->ds_phys->ds_deadlist_obj = 1957789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 19581544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 19591544Seschrock ds->ds_phys->ds_deadlist_obj)); 1960789Sahrens 1961789Sahrens dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1962789Sahrens err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1963789Sahrens snapname, 8, 1, &dsobj, tx); 1964789Sahrens ASSERT(err == 0); 1965789Sahrens 1966789Sahrens if (ds->ds_prev) 19676689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 19686689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 19696689Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 19704543Smarks 19717046Sahrens dsl_pool_ds_snapshotted(ds, tx); 19727046Sahrens 197310373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(ds->ds_dir); 197410373Schris.kirby@sun.com 19754543Smarks spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 19764603Sahrens "dataset = %llu", dsobj); 1977789Sahrens } 1978789Sahrens 1979789Sahrens void 19803547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1981789Sahrens { 1982789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 198310298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 1984789Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1985789Sahrens 19864787Sahrens /* 19874787Sahrens * in case we had to change ds_fsid_guid when we opened it, 19884787Sahrens * sync it out now. 19894787Sahrens */ 19904787Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 19914787Sahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 19924787Sahrens 1993789Sahrens dsl_dir_dirty(ds->ds_dir, tx); 199410298SMatthew.Ahrens@Sun.COM dmu_objset_sync(ds->ds_objset, zio, tx); 1995789Sahrens } 1996789Sahrens 1997789Sahrens void 19982885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1999789Sahrens { 20005378Sck153898 uint64_t refd, avail, uobjs, aobjs; 20015378Sck153898 20022885Sahrens dsl_dir_stats(ds->ds_dir, nv); 2003789Sahrens 20045378Sck153898 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 20055378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 20065378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 20075378Sck153898 20082885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 20092885Sahrens ds->ds_phys->ds_creation_time); 20102885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 20112885Sahrens ds->ds_phys->ds_creation_txg); 20125378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 20135378Sck153898 ds->ds_quota); 20145378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 20155378Sck153898 ds->ds_reserved); 20166643Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 20176643Seschrock ds->ds_phys->ds_guid); 201810575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 201910575SEric.Schrock@Sun.COM dsl_dataset_unique(ds)); 202010575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 202110575SEric.Schrock@Sun.COM ds->ds_object); 202210242Schris.kirby@sun.com dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, ds->ds_userrefs); 202310242Schris.kirby@sun.com dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 202410242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 2025789Sahrens 2026789Sahrens if (ds->ds_phys->ds_next_snap_obj) { 2027789Sahrens /* 2028789Sahrens * This is a snapshot; override the dd's space used with 20292885Sahrens * our unique space and compression ratio. 2030789Sahrens */ 20312885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 20322885Sahrens ds->ds_phys->ds_unique_bytes); 20332885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 20342885Sahrens ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 20352885Sahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 20362885Sahrens ds->ds_phys->ds_compressed_bytes)); 2037789Sahrens } 2038789Sahrens } 2039789Sahrens 20402885Sahrens void 20412885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2042789Sahrens { 20432885Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 20442885Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 20455367Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 20462885Sahrens if (ds->ds_phys->ds_next_snap_obj) { 20472885Sahrens stat->dds_is_snapshot = B_TRUE; 20482885Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 20498228SEric.Taylor@Sun.COM } else { 20508228SEric.Taylor@Sun.COM stat->dds_is_snapshot = B_FALSE; 20518228SEric.Taylor@Sun.COM stat->dds_num_clones = 0; 20522885Sahrens } 20532885Sahrens 20542885Sahrens /* clone origin is really a dsl_dir thing... */ 20555446Sahrens rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 20567046Sahrens if (dsl_dir_is_clone(ds->ds_dir)) { 20572885Sahrens dsl_dataset_t *ods; 20582885Sahrens 20596689Smaybee VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 20606689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 20615367Sahrens dsl_dataset_name(ods, stat->dds_origin); 20626689Smaybee dsl_dataset_drop_ref(ods, FTAG); 20638228SEric.Taylor@Sun.COM } else { 20648228SEric.Taylor@Sun.COM stat->dds_origin[0] = '\0'; 20652885Sahrens } 20665446Sahrens rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 20672885Sahrens } 20682885Sahrens 20692885Sahrens uint64_t 20702885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 20712885Sahrens { 20724787Sahrens return (ds->ds_fsid_guid); 20732885Sahrens } 20742885Sahrens 20752885Sahrens void 20762885Sahrens dsl_dataset_space(dsl_dataset_t *ds, 20772885Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 20782885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 20792885Sahrens { 20802885Sahrens *refdbytesp = ds->ds_phys->ds_used_bytes; 20812885Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 20825378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 20835378Sck153898 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 20845378Sck153898 if (ds->ds_quota != 0) { 20855378Sck153898 /* 20865378Sck153898 * Adjust available bytes according to refquota 20875378Sck153898 */ 20885378Sck153898 if (*refdbytesp < ds->ds_quota) 20895378Sck153898 *availbytesp = MIN(*availbytesp, 20905378Sck153898 ds->ds_quota - *refdbytesp); 20915378Sck153898 else 20925378Sck153898 *availbytesp = 0; 20935378Sck153898 } 20942885Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 20952885Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2096789Sahrens } 2097789Sahrens 20985326Sek110237 boolean_t 20995326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 21005326Sek110237 { 21015326Sek110237 dsl_pool_t *dp = ds->ds_dir->dd_pool; 21025326Sek110237 21035326Sek110237 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 21045326Sek110237 dsl_pool_sync_context(dp)); 21055326Sek110237 if (ds->ds_prev == NULL) 21065326Sek110237 return (B_FALSE); 21075326Sek110237 if (ds->ds_phys->ds_bp.blk_birth > 21085326Sek110237 ds->ds_prev->ds_phys->ds_creation_txg) 21095326Sek110237 return (B_TRUE); 21105326Sek110237 return (B_FALSE); 21115326Sek110237 } 21125326Sek110237 21132199Sahrens /* ARGSUSED */ 2114789Sahrens static int 21152199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 2116789Sahrens { 21172199Sahrens dsl_dataset_t *ds = arg1; 21182199Sahrens char *newsnapname = arg2; 21192199Sahrens dsl_dir_t *dd = ds->ds_dir; 21202199Sahrens dsl_dataset_t *hds; 21212199Sahrens uint64_t val; 2122789Sahrens int err; 2123789Sahrens 21246689Smaybee err = dsl_dataset_hold_obj(dd->dd_pool, 21256689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2126789Sahrens if (err) 2127789Sahrens return (err); 2128789Sahrens 21292199Sahrens /* new name better not be in use */ 21306689Smaybee err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 21316689Smaybee dsl_dataset_rele(hds, FTAG); 2132789Sahrens 21332199Sahrens if (err == 0) 21342199Sahrens err = EEXIST; 21352199Sahrens else if (err == ENOENT) 21362199Sahrens err = 0; 21374007Smmusante 21384007Smmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 21394007Smmusante if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 21404007Smmusante err = ENAMETOOLONG; 21414007Smmusante 21422199Sahrens return (err); 21432199Sahrens } 2144789Sahrens 21452199Sahrens static void 21464543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 21474543Smarks cred_t *cr, dmu_tx_t *tx) 21482199Sahrens { 21492199Sahrens dsl_dataset_t *ds = arg1; 21504543Smarks const char *newsnapname = arg2; 21512199Sahrens dsl_dir_t *dd = ds->ds_dir; 21522199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 21532199Sahrens dsl_dataset_t *hds; 21542199Sahrens int err; 2155789Sahrens 21562199Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2157789Sahrens 21586689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 21596689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2160789Sahrens 21612199Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 21626689Smaybee err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2163789Sahrens ASSERT3U(err, ==, 0); 21642199Sahrens mutex_enter(&ds->ds_lock); 21652199Sahrens (void) strcpy(ds->ds_snapname, newsnapname); 21662199Sahrens mutex_exit(&ds->ds_lock); 21672199Sahrens err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 21682199Sahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2169789Sahrens ASSERT3U(err, ==, 0); 2170789Sahrens 21714543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 21724543Smarks cr, "dataset = %llu", ds->ds_object); 21736689Smaybee dsl_dataset_rele(hds, FTAG); 2174789Sahrens } 2175789Sahrens 21765326Sek110237 struct renamesnaparg { 21774007Smmusante dsl_sync_task_group_t *dstg; 21784007Smmusante char failed[MAXPATHLEN]; 21794007Smmusante char *oldsnap; 21804007Smmusante char *newsnap; 21814007Smmusante }; 21824007Smmusante 21834007Smmusante static int 21844007Smmusante dsl_snapshot_rename_one(char *name, void *arg) 21854007Smmusante { 21865326Sek110237 struct renamesnaparg *ra = arg; 21874007Smmusante dsl_dataset_t *ds = NULL; 21884007Smmusante char *cp; 21894007Smmusante int err; 21904007Smmusante 21914007Smmusante cp = name + strlen(name); 21924007Smmusante *cp = '@'; 21934007Smmusante (void) strcpy(cp + 1, ra->oldsnap); 21944543Smarks 21954543Smarks /* 21964543Smarks * For recursive snapshot renames the parent won't be changing 21974543Smarks * so we just pass name for both the to/from argument. 21984543Smarks */ 21997312SMatthew.Ahrens@Sun.COM err = zfs_secpolicy_rename_perms(name, name, CRED()); 22007312SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 22017312SMatthew.Ahrens@Sun.COM return (0); 22027312SMatthew.Ahrens@Sun.COM } else if (err) { 22034543Smarks (void) strcpy(ra->failed, name); 22044543Smarks return (err); 22054543Smarks } 22064543Smarks 22076689Smaybee #ifdef _KERNEL 22086689Smaybee /* 22096689Smaybee * For all filesystems undergoing rename, we'll need to unmount it. 22106689Smaybee */ 22116689Smaybee (void) zfs_unmount_snap(name, NULL); 22126689Smaybee #endif 22136689Smaybee err = dsl_dataset_hold(name, ra->dstg, &ds); 22146689Smaybee *cp = '\0'; 22154007Smmusante if (err == ENOENT) { 22164007Smmusante return (0); 22176689Smaybee } else if (err) { 22184007Smmusante (void) strcpy(ra->failed, name); 22194007Smmusante return (err); 22204007Smmusante } 22214007Smmusante 22224007Smmusante dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 22234007Smmusante dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 22244007Smmusante 22254007Smmusante return (0); 22264007Smmusante } 22274007Smmusante 22284007Smmusante static int 22294007Smmusante dsl_recursive_rename(char *oldname, const char *newname) 22304007Smmusante { 22314007Smmusante int err; 22325326Sek110237 struct renamesnaparg *ra; 22334007Smmusante dsl_sync_task_t *dst; 22344007Smmusante spa_t *spa; 22354007Smmusante char *cp, *fsname = spa_strdup(oldname); 22364007Smmusante int len = strlen(oldname); 22374007Smmusante 22384007Smmusante /* truncate the snapshot name to get the fsname */ 22394007Smmusante cp = strchr(fsname, '@'); 22404007Smmusante *cp = '\0'; 22414007Smmusante 22424603Sahrens err = spa_open(fsname, &spa, FTAG); 22434007Smmusante if (err) { 22444007Smmusante kmem_free(fsname, len + 1); 22454007Smmusante return (err); 22464007Smmusante } 22475326Sek110237 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 22484007Smmusante ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 22494007Smmusante 22504007Smmusante ra->oldsnap = strchr(oldname, '@') + 1; 22514007Smmusante ra->newsnap = strchr(newname, '@') + 1; 22524007Smmusante *ra->failed = '\0'; 22534007Smmusante 22544007Smmusante err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 22554007Smmusante DS_FIND_CHILDREN); 22564007Smmusante kmem_free(fsname, len + 1); 22574007Smmusante 22584007Smmusante if (err == 0) { 22594007Smmusante err = dsl_sync_task_group_wait(ra->dstg); 22604007Smmusante } 22614007Smmusante 22624007Smmusante for (dst = list_head(&ra->dstg->dstg_tasks); dst; 22634007Smmusante dst = list_next(&ra->dstg->dstg_tasks, dst)) { 22644007Smmusante dsl_dataset_t *ds = dst->dst_arg1; 22654007Smmusante if (dst->dst_err) { 22664007Smmusante dsl_dir_name(ds->ds_dir, ra->failed); 22674009Smmusante (void) strcat(ra->failed, "@"); 22684009Smmusante (void) strcat(ra->failed, ra->newsnap); 22694007Smmusante } 22706689Smaybee dsl_dataset_rele(ds, ra->dstg); 22714007Smmusante } 22724007Smmusante 22734543Smarks if (err) 22744543Smarks (void) strcpy(oldname, ra->failed); 22754007Smmusante 22764007Smmusante dsl_sync_task_group_destroy(ra->dstg); 22775326Sek110237 kmem_free(ra, sizeof (struct renamesnaparg)); 22784007Smmusante spa_close(spa, FTAG); 22794007Smmusante return (err); 22804007Smmusante } 22814007Smmusante 22824569Smmusante static int 22834569Smmusante dsl_valid_rename(char *oldname, void *arg) 22844569Smmusante { 22854569Smmusante int delta = *(int *)arg; 22864569Smmusante 22874569Smmusante if (strlen(oldname) + delta >= MAXNAMELEN) 22884569Smmusante return (ENAMETOOLONG); 22894569Smmusante 22904569Smmusante return (0); 22914569Smmusante } 22924569Smmusante 2293789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename 2294789Sahrens int 22956689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2296789Sahrens { 2297789Sahrens dsl_dir_t *dd; 22982199Sahrens dsl_dataset_t *ds; 2299789Sahrens const char *tail; 2300789Sahrens int err; 2301789Sahrens 23022199Sahrens err = dsl_dir_open(oldname, FTAG, &dd, &tail); 23031544Seschrock if (err) 23041544Seschrock return (err); 23058517SEric.Taylor@Sun.COM /* 23068517SEric.Taylor@Sun.COM * If there are more than 2 references there may be holds 23078517SEric.Taylor@Sun.COM * hanging around that haven't been cleared out yet. 23088517SEric.Taylor@Sun.COM */ 23098517SEric.Taylor@Sun.COM if (dmu_buf_refcount(dd->dd_dbuf) > 2) 23108517SEric.Taylor@Sun.COM txg_wait_synced(dd->dd_pool, 0); 2311789Sahrens if (tail == NULL) { 23124569Smmusante int delta = strlen(newname) - strlen(oldname); 23134569Smmusante 23147046Sahrens /* if we're growing, validate child name lengths */ 23154569Smmusante if (delta > 0) 23164569Smmusante err = dmu_objset_find(oldname, dsl_valid_rename, 23174569Smmusante &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 23184569Smmusante 23194569Smmusante if (!err) 23204569Smmusante err = dsl_dir_rename(dd, newname); 2321789Sahrens dsl_dir_close(dd, FTAG); 2322789Sahrens return (err); 2323789Sahrens } 2324789Sahrens if (tail[0] != '@') { 232510588SEric.Taylor@Sun.COM /* the name ended in a nonexistent component */ 2326789Sahrens dsl_dir_close(dd, FTAG); 2327789Sahrens return (ENOENT); 2328789Sahrens } 2329789Sahrens 23302199Sahrens dsl_dir_close(dd, FTAG); 23312199Sahrens 23322199Sahrens /* new name must be snapshot in same filesystem */ 23332199Sahrens tail = strchr(newname, '@'); 23342199Sahrens if (tail == NULL) 23352199Sahrens return (EINVAL); 23362199Sahrens tail++; 23372199Sahrens if (strncmp(oldname, newname, tail - newname) != 0) 23382199Sahrens return (EXDEV); 2339789Sahrens 23404007Smmusante if (recursive) { 23414007Smmusante err = dsl_recursive_rename(oldname, newname); 23424007Smmusante } else { 23436689Smaybee err = dsl_dataset_hold(oldname, FTAG, &ds); 23444007Smmusante if (err) 23454007Smmusante return (err); 23462199Sahrens 23474007Smmusante err = dsl_sync_task_do(ds->ds_dir->dd_pool, 23484007Smmusante dsl_dataset_snapshot_rename_check, 23494007Smmusante dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 23502199Sahrens 23516689Smaybee dsl_dataset_rele(ds, FTAG); 23524007Smmusante } 23532199Sahrens 2354789Sahrens return (err); 2355789Sahrens } 23562082Seschrock 23577046Sahrens struct promotenode { 23586689Smaybee list_node_t link; 23596689Smaybee dsl_dataset_t *ds; 23606689Smaybee }; 23616689Smaybee 23622199Sahrens struct promotearg { 23637390SMatthew.Ahrens@Sun.COM list_t shared_snaps, origin_snaps, clone_snaps; 23647390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_origin, *origin_head; 23657390SMatthew.Ahrens@Sun.COM uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 236610588SEric.Taylor@Sun.COM char *err_ds; 23672199Sahrens }; 23682199Sahrens 23697390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 23707390SMatthew.Ahrens@Sun.COM 23714543Smarks /* ARGSUSED */ 23722082Seschrock static int 23732199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 23742082Seschrock { 23752199Sahrens dsl_dataset_t *hds = arg1; 23762199Sahrens struct promotearg *pa = arg2; 23777390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 23786689Smaybee dsl_dataset_t *origin_ds = snap->ds; 23796689Smaybee int err; 23802199Sahrens 23817046Sahrens /* Check that it is a real clone */ 23827046Sahrens if (!dsl_dir_is_clone(hds->ds_dir)) 23832082Seschrock return (EINVAL); 23842082Seschrock 23852199Sahrens /* Since this is so expensive, don't do the preliminary check */ 23862199Sahrens if (!dmu_tx_is_syncing(tx)) 23872199Sahrens return (0); 23882199Sahrens 23896689Smaybee if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 23906689Smaybee return (EXDEV); 23912082Seschrock 23925367Sahrens /* compute origin's new unique space */ 23937390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 23947390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 23957390SMatthew.Ahrens@Sun.COM err = bplist_space_birthrange(&snap->ds->ds_deadlist, 23967390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique); 23977390SMatthew.Ahrens@Sun.COM if (err) 23986689Smaybee return (err); 23996689Smaybee 24006689Smaybee /* 24016689Smaybee * Walk the snapshots that we are moving 24026689Smaybee * 24037390SMatthew.Ahrens@Sun.COM * Compute space to transfer. Consider the incremental changes 24047390SMatthew.Ahrens@Sun.COM * to used for each snapshot: 24057390SMatthew.Ahrens@Sun.COM * (my used) = (prev's used) + (blocks born) - (blocks killed) 24067390SMatthew.Ahrens@Sun.COM * So each snapshot gave birth to: 24077390SMatthew.Ahrens@Sun.COM * (blocks born) = (my used) - (prev's used) + (blocks killed) 24086689Smaybee * So a sequence would look like: 24097390SMatthew.Ahrens@Sun.COM * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 24106689Smaybee * Which simplifies to: 24117390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + k1 + k0 24126689Smaybee * Note however, if we stop before we reach the ORIGIN we get: 24137390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + kM - uM-1 24146689Smaybee */ 24156689Smaybee pa->used = origin_ds->ds_phys->ds_used_bytes; 24166689Smaybee pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 24176689Smaybee pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 24187390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 24197390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 24202082Seschrock uint64_t val, dlused, dlcomp, dluncomp; 24216689Smaybee dsl_dataset_t *ds = snap->ds; 24222082Seschrock 24232082Seschrock /* Check that the snapshot name does not conflict */ 24247390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 24256689Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 242610588SEric.Taylor@Sun.COM if (err == 0) { 242710588SEric.Taylor@Sun.COM err = EEXIST; 242810588SEric.Taylor@Sun.COM goto out; 242910588SEric.Taylor@Sun.COM } 24306689Smaybee if (err != ENOENT) 243110588SEric.Taylor@Sun.COM goto out; 24326689Smaybee 24336689Smaybee /* The very first snapshot does not have a deadlist */ 24347390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_obj == 0) 24357390SMatthew.Ahrens@Sun.COM continue; 24367390SMatthew.Ahrens@Sun.COM 24377390SMatthew.Ahrens@Sun.COM if (err = bplist_space(&ds->ds_deadlist, 24387390SMatthew.Ahrens@Sun.COM &dlused, &dlcomp, &dluncomp)) 243910588SEric.Taylor@Sun.COM goto out; 24407390SMatthew.Ahrens@Sun.COM pa->used += dlused; 24417390SMatthew.Ahrens@Sun.COM pa->comp += dlcomp; 24427390SMatthew.Ahrens@Sun.COM pa->uncomp += dluncomp; 24437390SMatthew.Ahrens@Sun.COM } 24446689Smaybee 24456689Smaybee /* 24466689Smaybee * If we are a clone of a clone then we never reached ORIGIN, 24476689Smaybee * so we need to subtract out the clone origin's used space. 24486689Smaybee */ 24497390SMatthew.Ahrens@Sun.COM if (pa->origin_origin) { 24507390SMatthew.Ahrens@Sun.COM pa->used -= pa->origin_origin->ds_phys->ds_used_bytes; 24517390SMatthew.Ahrens@Sun.COM pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes; 24527390SMatthew.Ahrens@Sun.COM pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes; 24532082Seschrock } 24542082Seschrock 24552082Seschrock /* Check that there is enough space here */ 24567390SMatthew.Ahrens@Sun.COM err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 24577390SMatthew.Ahrens@Sun.COM pa->used); 24587390SMatthew.Ahrens@Sun.COM if (err) 24597390SMatthew.Ahrens@Sun.COM return (err); 24607390SMatthew.Ahrens@Sun.COM 24617390SMatthew.Ahrens@Sun.COM /* 24627390SMatthew.Ahrens@Sun.COM * Compute the amounts of space that will be used by snapshots 24637390SMatthew.Ahrens@Sun.COM * after the promotion (for both origin and clone). For each, 24647390SMatthew.Ahrens@Sun.COM * it is the amount of space that will be on all of their 24657390SMatthew.Ahrens@Sun.COM * deadlists (that was not born before their new origin). 24667390SMatthew.Ahrens@Sun.COM */ 24677390SMatthew.Ahrens@Sun.COM if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 24687390SMatthew.Ahrens@Sun.COM uint64_t space; 24697390SMatthew.Ahrens@Sun.COM 24707390SMatthew.Ahrens@Sun.COM /* 24717390SMatthew.Ahrens@Sun.COM * Note, typically this will not be a clone of a clone, 24727390SMatthew.Ahrens@Sun.COM * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so 24737390SMatthew.Ahrens@Sun.COM * these snaplist_space() -> bplist_space_birthrange() 24747390SMatthew.Ahrens@Sun.COM * calls will be fast because they do not have to 24757390SMatthew.Ahrens@Sun.COM * iterate over all bps. 24767390SMatthew.Ahrens@Sun.COM */ 24777390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 24787390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->shared_snaps, 24797390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &pa->cloneusedsnap); 24807390SMatthew.Ahrens@Sun.COM if (err) 24817390SMatthew.Ahrens@Sun.COM return (err); 24827390SMatthew.Ahrens@Sun.COM 24837390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->clone_snaps, 24847390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &space); 24857390SMatthew.Ahrens@Sun.COM if (err) 24867390SMatthew.Ahrens@Sun.COM return (err); 24877390SMatthew.Ahrens@Sun.COM pa->cloneusedsnap += space; 24886689Smaybee } 24897390SMatthew.Ahrens@Sun.COM if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 24907390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->origin_snaps, 24917390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap); 24927390SMatthew.Ahrens@Sun.COM if (err) 24937390SMatthew.Ahrens@Sun.COM return (err); 24947390SMatthew.Ahrens@Sun.COM } 24957390SMatthew.Ahrens@Sun.COM 24967390SMatthew.Ahrens@Sun.COM return (0); 249710588SEric.Taylor@Sun.COM out: 249810588SEric.Taylor@Sun.COM pa->err_ds = snap->ds->ds_snapname; 249910588SEric.Taylor@Sun.COM return (err); 25002199Sahrens } 25012082Seschrock 25022199Sahrens static void 25034543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 25042199Sahrens { 25052199Sahrens dsl_dataset_t *hds = arg1; 25062199Sahrens struct promotearg *pa = arg2; 25077390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 25086689Smaybee dsl_dataset_t *origin_ds = snap->ds; 25097390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_head; 25102199Sahrens dsl_dir_t *dd = hds->ds_dir; 25112199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 25125367Sahrens dsl_dir_t *odd = NULL; 25137046Sahrens uint64_t oldnext_obj; 25147390SMatthew.Ahrens@Sun.COM int64_t delta; 25152199Sahrens 25162199Sahrens ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 25172199Sahrens 25187390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 25197390SMatthew.Ahrens@Sun.COM origin_head = snap->ds; 25207390SMatthew.Ahrens@Sun.COM 25212417Sahrens /* 25225367Sahrens * We need to explicitly open odd, since origin_ds's dd will be 25232417Sahrens * changing. 25242417Sahrens */ 25255367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 25265367Sahrens NULL, FTAG, &odd)); 25272082Seschrock 25286689Smaybee /* change origin's next snap */ 25296689Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 25307046Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 25317390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 25327390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 25337390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 25346689Smaybee 25357046Sahrens /* change the origin's next clone */ 25367046Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 253710801SMatthew.Ahrens@Sun.COM remove_from_next_clones(origin_ds, snap->ds->ds_object, tx); 25387046Sahrens VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 25397046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 25407046Sahrens oldnext_obj, tx)); 25417046Sahrens } 25427046Sahrens 25436689Smaybee /* change origin */ 25446689Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 25456689Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 25466689Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 25477390SMatthew.Ahrens@Sun.COM hds->ds_origin_txg = origin_head->ds_origin_txg; 25486689Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 25496689Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 25507390SMatthew.Ahrens@Sun.COM origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg; 25516689Smaybee 25522082Seschrock /* move snapshots to this dir */ 25537390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 25547390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 25556689Smaybee dsl_dataset_t *ds = snap->ds; 25562082Seschrock 25577237Sek110237 /* unregister props as dsl_dir is changing */ 255810298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 255910298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 256010298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 25617237Sek110237 } 25622082Seschrock /* move snap name entry */ 25637390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 25647390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_snap_remove(origin_head, 25656689Smaybee ds->ds_snapname, tx)); 25662199Sahrens VERIFY(0 == zap_add(dp->dp_meta_objset, 25672082Seschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 25682082Seschrock 8, 1, &ds->ds_object, tx)); 25692082Seschrock /* change containing dsl_dir */ 25702082Seschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 25715367Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 25722082Seschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 25735367Sahrens ASSERT3P(ds->ds_dir, ==, odd); 25742082Seschrock dsl_dir_close(ds->ds_dir, ds); 25752199Sahrens VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 25762082Seschrock NULL, ds, &ds->ds_dir)); 25772082Seschrock 25782082Seschrock ASSERT3U(dsl_prop_numcb(ds), ==, 0); 25797390SMatthew.Ahrens@Sun.COM } 25807390SMatthew.Ahrens@Sun.COM 25817390SMatthew.Ahrens@Sun.COM /* 25827390SMatthew.Ahrens@Sun.COM * Change space accounting. 25837390SMatthew.Ahrens@Sun.COM * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 25847390SMatthew.Ahrens@Sun.COM * both be valid, or both be 0 (resulting in delta == 0). This 25857390SMatthew.Ahrens@Sun.COM * is true for each of {clone,origin} independently. 25867390SMatthew.Ahrens@Sun.COM */ 25877390SMatthew.Ahrens@Sun.COM 25887390SMatthew.Ahrens@Sun.COM delta = pa->cloneusedsnap - 25897390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 25907390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, >=, 0); 25917390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, delta); 25927390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 25937390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_HEAD, 25947390SMatthew.Ahrens@Sun.COM pa->used - delta, pa->comp, pa->uncomp, tx); 25957390SMatthew.Ahrens@Sun.COM 25967390SMatthew.Ahrens@Sun.COM delta = pa->originusedsnap - 25977390SMatthew.Ahrens@Sun.COM odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 25987390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, <=, 0); 25997390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, -delta); 26007390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 26017390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_HEAD, 26027390SMatthew.Ahrens@Sun.COM -pa->used - delta, -pa->comp, -pa->uncomp, tx); 26037390SMatthew.Ahrens@Sun.COM 26045367Sahrens origin_ds->ds_phys->ds_unique_bytes = pa->unique; 26052082Seschrock 26064543Smarks /* log history record */ 26074543Smarks spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 26086689Smaybee cr, "dataset = %llu", hds->ds_object); 26094543Smarks 26105367Sahrens dsl_dir_close(odd, FTAG); 26112082Seschrock } 26122082Seschrock 26137390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist"; 26147390SMatthew.Ahrens@Sun.COM /* 26157390SMatthew.Ahrens@Sun.COM * Make a list of dsl_dataset_t's for the snapshots between first_obj 26167390SMatthew.Ahrens@Sun.COM * (exclusive) and last_obj (inclusive). The list will be in reverse 26177390SMatthew.Ahrens@Sun.COM * order (last_obj will be the list_head()). If first_obj == 0, do all 26187390SMatthew.Ahrens@Sun.COM * snapshots back to this dataset's origin. 26197390SMatthew.Ahrens@Sun.COM */ 26207390SMatthew.Ahrens@Sun.COM static int 26217390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own, 26227390SMatthew.Ahrens@Sun.COM uint64_t first_obj, uint64_t last_obj, list_t *l) 26237390SMatthew.Ahrens@Sun.COM { 26247390SMatthew.Ahrens@Sun.COM uint64_t obj = last_obj; 26257390SMatthew.Ahrens@Sun.COM 26267390SMatthew.Ahrens@Sun.COM ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock)); 26277390SMatthew.Ahrens@Sun.COM 26287390SMatthew.Ahrens@Sun.COM list_create(l, sizeof (struct promotenode), 26297390SMatthew.Ahrens@Sun.COM offsetof(struct promotenode, link)); 26307390SMatthew.Ahrens@Sun.COM 26317390SMatthew.Ahrens@Sun.COM while (obj != first_obj) { 26327390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 26337390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26347390SMatthew.Ahrens@Sun.COM int err; 26357390SMatthew.Ahrens@Sun.COM 26367390SMatthew.Ahrens@Sun.COM if (own) { 26377390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, obj, 26387390SMatthew.Ahrens@Sun.COM 0, snaplist_tag, &ds); 26397390SMatthew.Ahrens@Sun.COM if (err == 0) 26407390SMatthew.Ahrens@Sun.COM dsl_dataset_make_exclusive(ds, snaplist_tag); 26417390SMatthew.Ahrens@Sun.COM } else { 26427390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds); 26437390SMatthew.Ahrens@Sun.COM } 26447390SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 26457390SMatthew.Ahrens@Sun.COM /* lost race with snapshot destroy */ 26467390SMatthew.Ahrens@Sun.COM struct promotenode *last = list_tail(l); 26477390SMatthew.Ahrens@Sun.COM ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj); 26487390SMatthew.Ahrens@Sun.COM obj = last->ds->ds_phys->ds_prev_snap_obj; 26497390SMatthew.Ahrens@Sun.COM continue; 26507390SMatthew.Ahrens@Sun.COM } else if (err) { 26517390SMatthew.Ahrens@Sun.COM return (err); 26527390SMatthew.Ahrens@Sun.COM } 26537390SMatthew.Ahrens@Sun.COM 26547390SMatthew.Ahrens@Sun.COM if (first_obj == 0) 26557390SMatthew.Ahrens@Sun.COM first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 26567390SMatthew.Ahrens@Sun.COM 26577390SMatthew.Ahrens@Sun.COM snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 26587390SMatthew.Ahrens@Sun.COM snap->ds = ds; 26597390SMatthew.Ahrens@Sun.COM list_insert_tail(l, snap); 26607390SMatthew.Ahrens@Sun.COM obj = ds->ds_phys->ds_prev_snap_obj; 26617390SMatthew.Ahrens@Sun.COM } 26627390SMatthew.Ahrens@Sun.COM 26637390SMatthew.Ahrens@Sun.COM return (0); 26647390SMatthew.Ahrens@Sun.COM } 26657390SMatthew.Ahrens@Sun.COM 26667390SMatthew.Ahrens@Sun.COM static int 26677390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 26687390SMatthew.Ahrens@Sun.COM { 26697390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26707390SMatthew.Ahrens@Sun.COM 26717390SMatthew.Ahrens@Sun.COM *spacep = 0; 26727390SMatthew.Ahrens@Sun.COM for (snap = list_head(l); snap; snap = list_next(l, snap)) { 26737390SMatthew.Ahrens@Sun.COM uint64_t used; 26747390SMatthew.Ahrens@Sun.COM int err = bplist_space_birthrange(&snap->ds->ds_deadlist, 26757390SMatthew.Ahrens@Sun.COM mintxg, UINT64_MAX, &used); 26767390SMatthew.Ahrens@Sun.COM if (err) 26777390SMatthew.Ahrens@Sun.COM return (err); 26787390SMatthew.Ahrens@Sun.COM *spacep += used; 26797390SMatthew.Ahrens@Sun.COM } 26807390SMatthew.Ahrens@Sun.COM return (0); 26817390SMatthew.Ahrens@Sun.COM } 26827390SMatthew.Ahrens@Sun.COM 26837390SMatthew.Ahrens@Sun.COM static void 26847390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own) 26857390SMatthew.Ahrens@Sun.COM { 26867390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26877390SMatthew.Ahrens@Sun.COM 26888779SMark.Musante@Sun.COM if (!l || !list_link_active(&l->list_head)) 26897390SMatthew.Ahrens@Sun.COM return; 26907390SMatthew.Ahrens@Sun.COM 26917390SMatthew.Ahrens@Sun.COM while ((snap = list_tail(l)) != NULL) { 26927390SMatthew.Ahrens@Sun.COM list_remove(l, snap); 26937390SMatthew.Ahrens@Sun.COM if (own) 26947390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(snap->ds, snaplist_tag); 26957390SMatthew.Ahrens@Sun.COM else 26967390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(snap->ds, snaplist_tag); 26977390SMatthew.Ahrens@Sun.COM kmem_free(snap, sizeof (struct promotenode)); 26987390SMatthew.Ahrens@Sun.COM } 26997390SMatthew.Ahrens@Sun.COM list_destroy(l); 27007390SMatthew.Ahrens@Sun.COM } 27017390SMatthew.Ahrens@Sun.COM 27027390SMatthew.Ahrens@Sun.COM /* 27037390SMatthew.Ahrens@Sun.COM * Promote a clone. Nomenclature note: 27047390SMatthew.Ahrens@Sun.COM * "clone" or "cds": the original clone which is being promoted 27057390SMatthew.Ahrens@Sun.COM * "origin" or "ods": the snapshot which is originally clone's origin 27067390SMatthew.Ahrens@Sun.COM * "origin head" or "ohds": the dataset which is the head 27077390SMatthew.Ahrens@Sun.COM * (filesystem/volume) for the origin 27087390SMatthew.Ahrens@Sun.COM * "origin origin": the origin of the origin's filesystem (typically 27097390SMatthew.Ahrens@Sun.COM * NULL, indicating that the clone is not a clone of a clone). 27107390SMatthew.Ahrens@Sun.COM */ 27112082Seschrock int 271210588SEric.Taylor@Sun.COM dsl_dataset_promote(const char *name, char *conflsnap) 27132082Seschrock { 27142082Seschrock dsl_dataset_t *ds; 27156689Smaybee dsl_dir_t *dd; 27166689Smaybee dsl_pool_t *dp; 27172082Seschrock dmu_object_info_t doi; 27187390SMatthew.Ahrens@Sun.COM struct promotearg pa = { 0 }; 27197046Sahrens struct promotenode *snap; 27206689Smaybee int err; 27216689Smaybee 27226689Smaybee err = dsl_dataset_hold(name, FTAG, &ds); 27232082Seschrock if (err) 27242082Seschrock return (err); 27256689Smaybee dd = ds->ds_dir; 27266689Smaybee dp = dd->dd_pool; 27276689Smaybee 27286689Smaybee err = dmu_object_info(dp->dp_meta_objset, 27292082Seschrock ds->ds_phys->ds_snapnames_zapobj, &doi); 27302082Seschrock if (err) { 27316689Smaybee dsl_dataset_rele(ds, FTAG); 27322082Seschrock return (err); 27332082Seschrock } 27342082Seschrock 27357390SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) { 27367390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 27377390SMatthew.Ahrens@Sun.COM return (EINVAL); 27387390SMatthew.Ahrens@Sun.COM } 27397390SMatthew.Ahrens@Sun.COM 27402082Seschrock /* 27416689Smaybee * We are going to inherit all the snapshots taken before our 27426689Smaybee * origin (i.e., our new origin will be our parent's origin). 27436689Smaybee * Take ownership of them so that we can rename them into our 27446689Smaybee * namespace. 27456689Smaybee */ 27466689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 27477390SMatthew.Ahrens@Sun.COM 27487390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj, 27497390SMatthew.Ahrens@Sun.COM &pa.shared_snaps); 27507390SMatthew.Ahrens@Sun.COM if (err != 0) 27517390SMatthew.Ahrens@Sun.COM goto out; 27527390SMatthew.Ahrens@Sun.COM 27537390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps); 27547390SMatthew.Ahrens@Sun.COM if (err != 0) 27557390SMatthew.Ahrens@Sun.COM goto out; 27567390SMatthew.Ahrens@Sun.COM 27577390SMatthew.Ahrens@Sun.COM snap = list_head(&pa.shared_snaps); 27587390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 27597390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj, 27607390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps); 27617390SMatthew.Ahrens@Sun.COM if (err != 0) 27627390SMatthew.Ahrens@Sun.COM goto out; 27637390SMatthew.Ahrens@Sun.COM 27647390SMatthew.Ahrens@Sun.COM if (dsl_dir_is_clone(snap->ds->ds_dir)) { 27657390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, 27667390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_origin_obj, 27677390SMatthew.Ahrens@Sun.COM 0, FTAG, &pa.origin_origin); 27687390SMatthew.Ahrens@Sun.COM if (err != 0) 27696689Smaybee goto out; 27706689Smaybee } 27717390SMatthew.Ahrens@Sun.COM 27727390SMatthew.Ahrens@Sun.COM out: 27736689Smaybee rw_exit(&dp->dp_config_rwlock); 27746689Smaybee 27756689Smaybee /* 27762082Seschrock * Add in 128x the snapnames zapobj size, since we will be moving 27772082Seschrock * a bunch of snapnames to the promoted ds, and dirtying their 27782082Seschrock * bonus buffers. 27792082Seschrock */ 27807390SMatthew.Ahrens@Sun.COM if (err == 0) { 27817390SMatthew.Ahrens@Sun.COM err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 27827390SMatthew.Ahrens@Sun.COM dsl_dataset_promote_sync, ds, &pa, 2783*10922SJeff.Bonwick@Sun.COM 2 + 2 * doi.doi_physical_blocks_512); 278410588SEric.Taylor@Sun.COM if (err && pa.err_ds && conflsnap) 278510588SEric.Taylor@Sun.COM (void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN); 27866689Smaybee } 27877390SMatthew.Ahrens@Sun.COM 27887390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.shared_snaps, B_TRUE); 27897390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.clone_snaps, B_FALSE); 27907390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.origin_snaps, B_FALSE); 27917390SMatthew.Ahrens@Sun.COM if (pa.origin_origin) 27927390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(pa.origin_origin, FTAG); 27936689Smaybee dsl_dataset_rele(ds, FTAG); 27942082Seschrock return (err); 27952082Seschrock } 27963912Slling 27975367Sahrens struct cloneswaparg { 27985367Sahrens dsl_dataset_t *cds; /* clone dataset */ 27995367Sahrens dsl_dataset_t *ohds; /* origin's head dataset */ 28005367Sahrens boolean_t force; 28015481Sck153898 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 28025367Sahrens }; 28035326Sek110237 28045326Sek110237 /* ARGSUSED */ 28055326Sek110237 static int 28065326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 28075326Sek110237 { 28085367Sahrens struct cloneswaparg *csa = arg1; 28095326Sek110237 28105367Sahrens /* they should both be heads */ 28115367Sahrens if (dsl_dataset_is_snapshot(csa->cds) || 28125367Sahrens dsl_dataset_is_snapshot(csa->ohds)) 28135326Sek110237 return (EINVAL); 28145326Sek110237 28155367Sahrens /* the branch point should be just before them */ 28165367Sahrens if (csa->cds->ds_prev != csa->ohds->ds_prev) 28175326Sek110237 return (EINVAL); 28185326Sek110237 281910272SMatthew.Ahrens@Sun.COM /* cds should be the clone (unless they are unrelated) */ 282010272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev != NULL && 282110272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap && 282210272SMatthew.Ahrens@Sun.COM csa->ohds->ds_object != 282310272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev->ds_phys->ds_next_snap_obj) 28245367Sahrens return (EINVAL); 28255326Sek110237 28265367Sahrens /* the clone should be a child of the origin */ 28275367Sahrens if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 28285367Sahrens return (EINVAL); 28295326Sek110237 28305367Sahrens /* ohds shouldn't be modified unless 'force' */ 28315367Sahrens if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 28325367Sahrens return (ETXTBSY); 28335481Sck153898 28345481Sck153898 /* adjust amount of any unconsumed refreservation */ 28355481Sck153898 csa->unused_refres_delta = 28365481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28375481Sck153898 csa->ohds->ds_phys->ds_unique_bytes) - 28385481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28395481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 28405481Sck153898 28415481Sck153898 if (csa->unused_refres_delta > 0 && 28425481Sck153898 csa->unused_refres_delta > 28435481Sck153898 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 28445481Sck153898 return (ENOSPC); 28455481Sck153898 284610799SChris.Kirby@sun.com if (csa->ohds->ds_quota != 0 && 284710799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota) 284810799SChris.Kirby@sun.com return (EDQUOT); 284910799SChris.Kirby@sun.com 28505367Sahrens return (0); 28515326Sek110237 } 28525326Sek110237 28535326Sek110237 /* ARGSUSED */ 28545326Sek110237 static void 28555326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 28565326Sek110237 { 28575367Sahrens struct cloneswaparg *csa = arg1; 28585367Sahrens dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 28595326Sek110237 28605481Sck153898 ASSERT(csa->cds->ds_reserved == 0); 286110799SChris.Kirby@sun.com ASSERT(csa->ohds->ds_quota == 0 || 286210799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota); 28635481Sck153898 28645367Sahrens dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 28655367Sahrens dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 28665326Sek110237 286710298SMatthew.Ahrens@Sun.COM if (csa->cds->ds_objset != NULL) { 286810298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->cds->ds_objset); 286910298SMatthew.Ahrens@Sun.COM csa->cds->ds_objset = NULL; 28705367Sahrens } 28715326Sek110237 287210298SMatthew.Ahrens@Sun.COM if (csa->ohds->ds_objset != NULL) { 287310298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->ohds->ds_objset); 287410298SMatthew.Ahrens@Sun.COM csa->ohds->ds_objset = NULL; 28755367Sahrens } 28765326Sek110237 287710272SMatthew.Ahrens@Sun.COM /* 287810272SMatthew.Ahrens@Sun.COM * Reset origin's unique bytes, if it exists. 287910272SMatthew.Ahrens@Sun.COM */ 288010272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev) { 288110272SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin = csa->cds->ds_prev; 288210272SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(origin->ds_dbuf, tx); 288310272SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 288410272SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 288510272SMatthew.Ahrens@Sun.COM &origin->ds_phys->ds_unique_bytes)); 288610272SMatthew.Ahrens@Sun.COM } 28875326Sek110237 28885326Sek110237 /* swap blkptrs */ 28895326Sek110237 { 28905326Sek110237 blkptr_t tmp; 28915367Sahrens tmp = csa->ohds->ds_phys->ds_bp; 28925367Sahrens csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 28935367Sahrens csa->cds->ds_phys->ds_bp = tmp; 28945326Sek110237 } 28955326Sek110237 28965326Sek110237 /* set dd_*_bytes */ 28975326Sek110237 { 28985326Sek110237 int64_t dused, dcomp, duncomp; 28995326Sek110237 uint64_t cdl_used, cdl_comp, cdl_uncomp; 29005326Sek110237 uint64_t odl_used, odl_comp, odl_uncomp; 29015326Sek110237 29027390SMatthew.Ahrens@Sun.COM ASSERT3U(csa->cds->ds_dir->dd_phys-> 29037390SMatthew.Ahrens@Sun.COM dd_used_breakdown[DD_USED_SNAP], ==, 0); 29047390SMatthew.Ahrens@Sun.COM 29055367Sahrens VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 29065326Sek110237 &cdl_comp, &cdl_uncomp)); 29075367Sahrens VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 29085326Sek110237 &odl_comp, &odl_uncomp)); 29097390SMatthew.Ahrens@Sun.COM 29105367Sahrens dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 29115367Sahrens (csa->ohds->ds_phys->ds_used_bytes + odl_used); 29125367Sahrens dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 29135367Sahrens (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 29145367Sahrens duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 29155367Sahrens cdl_uncomp - 29165367Sahrens (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 29175326Sek110237 29187390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD, 29195367Sahrens dused, dcomp, duncomp, tx); 29207390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD, 29215367Sahrens -dused, -dcomp, -duncomp, tx); 29227390SMatthew.Ahrens@Sun.COM 29237390SMatthew.Ahrens@Sun.COM /* 29247390SMatthew.Ahrens@Sun.COM * The difference in the space used by snapshots is the 29257390SMatthew.Ahrens@Sun.COM * difference in snapshot space due to the head's 29267390SMatthew.Ahrens@Sun.COM * deadlist (since that's the only thing that's 29277390SMatthew.Ahrens@Sun.COM * changing that affects the snapused). 29287390SMatthew.Ahrens@Sun.COM */ 29297390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 29307390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used)); 29317390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist, 29327390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used)); 29337390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used, 29347390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 29355367Sahrens } 29365367Sahrens 29375367Sahrens #define SWITCH64(x, y) \ 29385367Sahrens { \ 29395367Sahrens uint64_t __tmp = (x); \ 29405367Sahrens (x) = (y); \ 29415367Sahrens (y) = __tmp; \ 29425326Sek110237 } 29435326Sek110237 29445326Sek110237 /* swap ds_*_bytes */ 29455367Sahrens SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 29465367Sahrens csa->cds->ds_phys->ds_used_bytes); 29475367Sahrens SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 29485367Sahrens csa->cds->ds_phys->ds_compressed_bytes); 29495367Sahrens SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 29505367Sahrens csa->cds->ds_phys->ds_uncompressed_bytes); 29515481Sck153898 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 29525481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 29535481Sck153898 29545481Sck153898 /* apply any parent delta for change in unconsumed refreservation */ 29557390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV, 29567390SMatthew.Ahrens@Sun.COM csa->unused_refres_delta, 0, 0, tx); 29575326Sek110237 29585326Sek110237 /* swap deadlists */ 29595367Sahrens bplist_close(&csa->cds->ds_deadlist); 29605367Sahrens bplist_close(&csa->ohds->ds_deadlist); 29615367Sahrens SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 29625367Sahrens csa->cds->ds_phys->ds_deadlist_obj); 29635367Sahrens VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 29645367Sahrens csa->cds->ds_phys->ds_deadlist_obj)); 29655367Sahrens VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 29665367Sahrens csa->ohds->ds_phys->ds_deadlist_obj)); 29677837SMatthew.Ahrens@Sun.COM 29687837SMatthew.Ahrens@Sun.COM dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx); 29695326Sek110237 } 29705326Sek110237 29715326Sek110237 /* 297210272SMatthew.Ahrens@Sun.COM * Swap 'clone' with its origin head datasets. Used at the end of "zfs 297310272SMatthew.Ahrens@Sun.COM * recv" into an existing fs to swizzle the file system to the new 297410272SMatthew.Ahrens@Sun.COM * version, and by "zfs rollback". Can also be used to swap two 297510272SMatthew.Ahrens@Sun.COM * independent head datasets if neither has any snapshots. 29765326Sek110237 */ 29775326Sek110237 int 29785367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 29795367Sahrens boolean_t force) 29805326Sek110237 { 29815367Sahrens struct cloneswaparg csa; 29826689Smaybee int error; 29836689Smaybee 29846689Smaybee ASSERT(clone->ds_owner); 29856689Smaybee ASSERT(origin_head->ds_owner); 29866689Smaybee retry: 29876689Smaybee /* Need exclusive access for the swap */ 29886689Smaybee rw_enter(&clone->ds_rwlock, RW_WRITER); 29896689Smaybee if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 29906689Smaybee rw_exit(&clone->ds_rwlock); 29916689Smaybee rw_enter(&origin_head->ds_rwlock, RW_WRITER); 29926689Smaybee if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 29936689Smaybee rw_exit(&origin_head->ds_rwlock); 29946689Smaybee goto retry; 29956689Smaybee } 29966689Smaybee } 29975367Sahrens csa.cds = clone; 29985367Sahrens csa.ohds = origin_head; 29995367Sahrens csa.force = force; 30006689Smaybee error = dsl_sync_task_do(clone->ds_dir->dd_pool, 30015326Sek110237 dsl_dataset_clone_swap_check, 30026689Smaybee dsl_dataset_clone_swap_sync, &csa, NULL, 9); 30036689Smaybee return (error); 30045326Sek110237 } 30055326Sek110237 30063912Slling /* 30073912Slling * Given a pool name and a dataset object number in that pool, 30083912Slling * return the name of that dataset. 30093912Slling */ 30103912Slling int 30113912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 30123912Slling { 30133912Slling spa_t *spa; 30143912Slling dsl_pool_t *dp; 30156689Smaybee dsl_dataset_t *ds; 30163912Slling int error; 30173912Slling 30183912Slling if ((error = spa_open(pname, &spa, FTAG)) != 0) 30193912Slling return (error); 30203912Slling dp = spa_get_dsl(spa); 30213912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 30226689Smaybee if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 30236689Smaybee dsl_dataset_name(ds, buf); 30246689Smaybee dsl_dataset_rele(ds, FTAG); 30253912Slling } 30263912Slling rw_exit(&dp->dp_config_rwlock); 30273912Slling spa_close(spa, FTAG); 30283912Slling 30296689Smaybee return (error); 30303912Slling } 30315378Sck153898 30325378Sck153898 int 30335378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 30346689Smaybee uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 30355378Sck153898 { 30365378Sck153898 int error = 0; 30375378Sck153898 30385378Sck153898 ASSERT3S(asize, >, 0); 30395378Sck153898 30405831Sck153898 /* 30415831Sck153898 * *ref_rsrv is the portion of asize that will come from any 30425831Sck153898 * unconsumed refreservation space. 30435831Sck153898 */ 30445831Sck153898 *ref_rsrv = 0; 30455831Sck153898 30465378Sck153898 mutex_enter(&ds->ds_lock); 30475378Sck153898 /* 30485378Sck153898 * Make a space adjustment for reserved bytes. 30495378Sck153898 */ 30505378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 30515378Sck153898 ASSERT3U(*used, >=, 30525378Sck153898 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30535378Sck153898 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30545831Sck153898 *ref_rsrv = 30555831Sck153898 asize - MIN(asize, parent_delta(ds, asize + inflight)); 30565378Sck153898 } 30575378Sck153898 30585378Sck153898 if (!check_quota || ds->ds_quota == 0) { 30595378Sck153898 mutex_exit(&ds->ds_lock); 30605378Sck153898 return (0); 30615378Sck153898 } 30625378Sck153898 /* 30635378Sck153898 * If they are requesting more space, and our current estimate 30645378Sck153898 * is over quota, they get to try again unless the actual 30655378Sck153898 * on-disk is over quota and there are no pending changes (which 30665378Sck153898 * may free up space for us). 30675378Sck153898 */ 30685378Sck153898 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 30695378Sck153898 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 30705378Sck153898 error = ERESTART; 30715378Sck153898 else 30725378Sck153898 error = EDQUOT; 30735378Sck153898 } 30745378Sck153898 mutex_exit(&ds->ds_lock); 30755378Sck153898 30765378Sck153898 return (error); 30775378Sck153898 } 30785378Sck153898 30795378Sck153898 /* ARGSUSED */ 30805378Sck153898 static int 30815378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 30825378Sck153898 { 30835378Sck153898 dsl_dataset_t *ds = arg1; 30845378Sck153898 uint64_t *quotap = arg2; 30855378Sck153898 uint64_t new_quota = *quotap; 30865378Sck153898 30875378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 30885378Sck153898 return (ENOTSUP); 30895378Sck153898 30905378Sck153898 if (new_quota == 0) 30915378Sck153898 return (0); 30925378Sck153898 30935378Sck153898 if (new_quota < ds->ds_phys->ds_used_bytes || 30945378Sck153898 new_quota < ds->ds_reserved) 30955378Sck153898 return (ENOSPC); 30965378Sck153898 30975378Sck153898 return (0); 30985378Sck153898 } 30995378Sck153898 31005378Sck153898 /* ARGSUSED */ 31015378Sck153898 void 31025378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 31035378Sck153898 { 31045378Sck153898 dsl_dataset_t *ds = arg1; 31055378Sck153898 uint64_t *quotap = arg2; 31065378Sck153898 uint64_t new_quota = *quotap; 31075378Sck153898 31085378Sck153898 dmu_buf_will_dirty(ds->ds_dbuf, tx); 31095378Sck153898 31105378Sck153898 ds->ds_quota = new_quota; 31115378Sck153898 311210242Schris.kirby@sun.com dsl_dir_prop_set_uint64_sync(ds->ds_dir, "refquota", new_quota, cr, tx); 31135378Sck153898 31145378Sck153898 spa_history_internal_log(LOG_DS_REFQUOTA, ds->ds_dir->dd_pool->dp_spa, 31155378Sck153898 tx, cr, "%lld dataset = %llu ", 31166689Smaybee (longlong_t)new_quota, ds->ds_object); 31175378Sck153898 } 31185378Sck153898 31195378Sck153898 int 31205378Sck153898 dsl_dataset_set_quota(const char *dsname, uint64_t quota) 31215378Sck153898 { 31225378Sck153898 dsl_dataset_t *ds; 31235378Sck153898 int err; 31245378Sck153898 31256689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 31265378Sck153898 if (err) 31275378Sck153898 return (err); 31285378Sck153898 31295481Sck153898 if (quota != ds->ds_quota) { 31305481Sck153898 /* 31315481Sck153898 * If someone removes a file, then tries to set the quota, we 31325481Sck153898 * want to make sure the file freeing takes effect. 31335481Sck153898 */ 31345481Sck153898 txg_wait_open(ds->ds_dir->dd_pool, 0); 31355481Sck153898 31365481Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 31375481Sck153898 dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 31385481Sck153898 ds, "a, 0); 31395481Sck153898 } 31406689Smaybee dsl_dataset_rele(ds, FTAG); 31415378Sck153898 return (err); 31425378Sck153898 } 31435378Sck153898 31445378Sck153898 static int 31455378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 31465378Sck153898 { 31475378Sck153898 dsl_dataset_t *ds = arg1; 31485378Sck153898 uint64_t *reservationp = arg2; 31495378Sck153898 uint64_t new_reservation = *reservationp; 31505378Sck153898 uint64_t unique; 31515378Sck153898 31525378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 31535378Sck153898 SPA_VERSION_REFRESERVATION) 31545378Sck153898 return (ENOTSUP); 31555378Sck153898 31565378Sck153898 if (dsl_dataset_is_snapshot(ds)) 31575378Sck153898 return (EINVAL); 31585378Sck153898 31595378Sck153898 /* 31605378Sck153898 * If we are doing the preliminary check in open context, the 31615378Sck153898 * space estimates may be inaccurate. 31625378Sck153898 */ 31635378Sck153898 if (!dmu_tx_is_syncing(tx)) 31645378Sck153898 return (0); 31655378Sck153898 31665378Sck153898 mutex_enter(&ds->ds_lock); 31675378Sck153898 unique = dsl_dataset_unique(ds); 31685378Sck153898 mutex_exit(&ds->ds_lock); 31695378Sck153898 31708525SEric.Schrock@Sun.COM if (MAX(unique, new_reservation) > MAX(unique, ds->ds_reserved)) { 31718525SEric.Schrock@Sun.COM uint64_t delta = MAX(unique, new_reservation) - 31728525SEric.Schrock@Sun.COM MAX(unique, ds->ds_reserved); 31738525SEric.Schrock@Sun.COM 31748525SEric.Schrock@Sun.COM if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 31758525SEric.Schrock@Sun.COM return (ENOSPC); 31768525SEric.Schrock@Sun.COM if (ds->ds_quota > 0 && 31778525SEric.Schrock@Sun.COM new_reservation > ds->ds_quota) 31788525SEric.Schrock@Sun.COM return (ENOSPC); 31798525SEric.Schrock@Sun.COM } 31805378Sck153898 31815378Sck153898 return (0); 31825378Sck153898 } 31835378Sck153898 31845378Sck153898 /* ARGSUSED */ 31855378Sck153898 static void 31865378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 31875378Sck153898 dmu_tx_t *tx) 31885378Sck153898 { 31895378Sck153898 dsl_dataset_t *ds = arg1; 31905378Sck153898 uint64_t *reservationp = arg2; 31915378Sck153898 uint64_t new_reservation = *reservationp; 31927595SMatthew.Ahrens@Sun.COM uint64_t unique; 31937595SMatthew.Ahrens@Sun.COM int64_t delta; 31947595SMatthew.Ahrens@Sun.COM 31957595SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx); 31967595SMatthew.Ahrens@Sun.COM 31977595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 31987595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_lock); 31997595SMatthew.Ahrens@Sun.COM unique = dsl_dataset_unique(ds); 32007595SMatthew.Ahrens@Sun.COM delta = MAX(0, (int64_t)(new_reservation - unique)) - 32017595SMatthew.Ahrens@Sun.COM MAX(0, (int64_t)(ds->ds_reserved - unique)); 32027595SMatthew.Ahrens@Sun.COM ds->ds_reserved = new_reservation; 32037595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_lock); 32047595SMatthew.Ahrens@Sun.COM 32057595SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 32067595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 320710242Schris.kirby@sun.com dsl_dir_prop_set_uint64_sync(ds->ds_dir, "refreservation", 32087595SMatthew.Ahrens@Sun.COM new_reservation, cr, tx); 32097595SMatthew.Ahrens@Sun.COM 32107595SMatthew.Ahrens@Sun.COM spa_history_internal_log(LOG_DS_REFRESERV, 32117595SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 32127595SMatthew.Ahrens@Sun.COM (longlong_t)new_reservation, ds->ds_object); 32135378Sck153898 } 32145378Sck153898 32155378Sck153898 int 32165378Sck153898 dsl_dataset_set_reservation(const char *dsname, uint64_t reservation) 32175378Sck153898 { 32185378Sck153898 dsl_dataset_t *ds; 32195378Sck153898 int err; 32205378Sck153898 32216689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 32225378Sck153898 if (err) 32235378Sck153898 return (err); 32245378Sck153898 32255378Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 32265378Sck153898 dsl_dataset_set_reservation_check, 32275378Sck153898 dsl_dataset_set_reservation_sync, ds, &reservation, 0); 32286689Smaybee dsl_dataset_rele(ds, FTAG); 32295378Sck153898 return (err); 32305378Sck153898 } 323110242Schris.kirby@sun.com 323210342Schris.kirby@sun.com struct dsl_ds_holdarg { 323310342Schris.kirby@sun.com dsl_sync_task_group_t *dstg; 323410342Schris.kirby@sun.com char *htag; 323510342Schris.kirby@sun.com char *snapname; 323610342Schris.kirby@sun.com boolean_t recursive; 323710342Schris.kirby@sun.com boolean_t gotone; 323810342Schris.kirby@sun.com boolean_t temphold; 323910342Schris.kirby@sun.com char failed[MAXPATHLEN]; 324010342Schris.kirby@sun.com }; 324110342Schris.kirby@sun.com 324210242Schris.kirby@sun.com static int 324310242Schris.kirby@sun.com dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx) 324410242Schris.kirby@sun.com { 324510242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 324610342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 324710342Schris.kirby@sun.com char *htag = ha->htag; 324810242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 324910242Schris.kirby@sun.com int error = 0; 325010242Schris.kirby@sun.com 325110242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 325210242Schris.kirby@sun.com return (ENOTSUP); 325310242Schris.kirby@sun.com 325410242Schris.kirby@sun.com if (!dsl_dataset_is_snapshot(ds)) 325510242Schris.kirby@sun.com return (EINVAL); 325610242Schris.kirby@sun.com 325710242Schris.kirby@sun.com /* tags must be unique */ 325810242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 325910242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj) { 326010242Schris.kirby@sun.com error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag, 326110242Schris.kirby@sun.com 8, 1, tx); 326210242Schris.kirby@sun.com if (error == 0) 326310242Schris.kirby@sun.com error = EEXIST; 326410242Schris.kirby@sun.com else if (error == ENOENT) 326510242Schris.kirby@sun.com error = 0; 326610242Schris.kirby@sun.com } 326710242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 326810242Schris.kirby@sun.com 326910242Schris.kirby@sun.com return (error); 327010242Schris.kirby@sun.com } 327110242Schris.kirby@sun.com 327210242Schris.kirby@sun.com static void 327310242Schris.kirby@sun.com dsl_dataset_user_hold_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 327410242Schris.kirby@sun.com { 327510242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 327610342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 327710342Schris.kirby@sun.com char *htag = ha->htag; 327810342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 327910342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 328010242Schris.kirby@sun.com time_t now = gethrestime_sec(); 328110242Schris.kirby@sun.com uint64_t zapobj; 328210242Schris.kirby@sun.com 328310242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 328410242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj == 0) { 328510242Schris.kirby@sun.com /* 328610242Schris.kirby@sun.com * This is the first user hold for this dataset. Create 328710242Schris.kirby@sun.com * the userrefs zap object. 328810242Schris.kirby@sun.com */ 328910242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 329010242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj = 329110242Schris.kirby@sun.com zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx); 329210242Schris.kirby@sun.com } else { 329310242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 329410242Schris.kirby@sun.com } 329510242Schris.kirby@sun.com ds->ds_userrefs++; 329610242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 329710242Schris.kirby@sun.com 329810242Schris.kirby@sun.com VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx)); 329910242Schris.kirby@sun.com 330010342Schris.kirby@sun.com if (ha->temphold) { 330110342Schris.kirby@sun.com VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object, 330210342Schris.kirby@sun.com htag, &now, tx)); 330310342Schris.kirby@sun.com } 330410342Schris.kirby@sun.com 330510242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_HOLD, 330610342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> temp = %d dataset = %llu", htag, 330710342Schris.kirby@sun.com (int)ha->temphold, ds->ds_object); 330810242Schris.kirby@sun.com } 330910242Schris.kirby@sun.com 331010242Schris.kirby@sun.com static int 331110242Schris.kirby@sun.com dsl_dataset_user_hold_one(char *dsname, void *arg) 331210242Schris.kirby@sun.com { 331310242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 331410242Schris.kirby@sun.com dsl_dataset_t *ds; 331510242Schris.kirby@sun.com int error; 331610242Schris.kirby@sun.com char *name; 331710242Schris.kirby@sun.com 331810242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname plus terminating NULL */ 331910272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 332010242Schris.kirby@sun.com error = dsl_dataset_hold(name, ha->dstg, &ds); 332110272SMatthew.Ahrens@Sun.COM strfree(name); 332210242Schris.kirby@sun.com if (error == 0) { 332310268Schris.kirby@sun.com ha->gotone = B_TRUE; 332410242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check, 332510342Schris.kirby@sun.com dsl_dataset_user_hold_sync, ds, ha, 0); 332610242Schris.kirby@sun.com } else if (error == ENOENT && ha->recursive) { 332710242Schris.kirby@sun.com error = 0; 332810242Schris.kirby@sun.com } else { 332910242Schris.kirby@sun.com (void) strcpy(ha->failed, dsname); 333010242Schris.kirby@sun.com } 333110242Schris.kirby@sun.com return (error); 333210242Schris.kirby@sun.com } 333310242Schris.kirby@sun.com 333410242Schris.kirby@sun.com int 333510242Schris.kirby@sun.com dsl_dataset_user_hold(char *dsname, char *snapname, char *htag, 333610342Schris.kirby@sun.com boolean_t recursive, boolean_t temphold) 333710242Schris.kirby@sun.com { 333810242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 333910242Schris.kirby@sun.com dsl_sync_task_t *dst; 334010242Schris.kirby@sun.com spa_t *spa; 334110242Schris.kirby@sun.com int error; 334210242Schris.kirby@sun.com 334310242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 334410242Schris.kirby@sun.com 334510242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 334610242Schris.kirby@sun.com 334710242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 334810242Schris.kirby@sun.com if (error) { 334910242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 335010242Schris.kirby@sun.com return (error); 335110242Schris.kirby@sun.com } 335210242Schris.kirby@sun.com 335310242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 335410242Schris.kirby@sun.com ha->htag = htag; 335510242Schris.kirby@sun.com ha->snapname = snapname; 335610242Schris.kirby@sun.com ha->recursive = recursive; 335710342Schris.kirby@sun.com ha->temphold = temphold; 335810242Schris.kirby@sun.com if (recursive) { 335910242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_hold_one, 336010242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 336110242Schris.kirby@sun.com } else { 336210242Schris.kirby@sun.com error = dsl_dataset_user_hold_one(dsname, ha); 336310242Schris.kirby@sun.com } 336410242Schris.kirby@sun.com if (error == 0) 336510242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 336610242Schris.kirby@sun.com 336710242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 336810242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 336910242Schris.kirby@sun.com dsl_dataset_t *ds = dst->dst_arg1; 337010242Schris.kirby@sun.com 337110242Schris.kirby@sun.com if (dst->dst_err) { 337210242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 337310242Schris.kirby@sun.com *strchr(ha->failed, '@') = '\0'; 337410242Schris.kirby@sun.com } 337510242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 337610242Schris.kirby@sun.com } 337710242Schris.kirby@sun.com 337810268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 337910268Schris.kirby@sun.com error = ENOENT; 338010268Schris.kirby@sun.com 338110242Schris.kirby@sun.com if (error) 338210242Schris.kirby@sun.com (void) strcpy(dsname, ha->failed); 338310242Schris.kirby@sun.com 338410242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 338510242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 338610242Schris.kirby@sun.com spa_close(spa, FTAG); 338710242Schris.kirby@sun.com return (error); 338810242Schris.kirby@sun.com } 338910242Schris.kirby@sun.com 339010242Schris.kirby@sun.com struct dsl_ds_releasearg { 339110242Schris.kirby@sun.com dsl_dataset_t *ds; 339210242Schris.kirby@sun.com const char *htag; 339310242Schris.kirby@sun.com boolean_t own; /* do we own or just hold ds? */ 339410242Schris.kirby@sun.com }; 339510242Schris.kirby@sun.com 339610242Schris.kirby@sun.com static int 339710242Schris.kirby@sun.com dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag, 339810242Schris.kirby@sun.com boolean_t *might_destroy) 339910242Schris.kirby@sun.com { 340010242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 340110242Schris.kirby@sun.com uint64_t zapobj; 340210242Schris.kirby@sun.com uint64_t tmp; 340310242Schris.kirby@sun.com int error; 340410242Schris.kirby@sun.com 340510242Schris.kirby@sun.com *might_destroy = B_FALSE; 340610242Schris.kirby@sun.com 340710242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 340810242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 340910242Schris.kirby@sun.com if (zapobj == 0) { 341010242Schris.kirby@sun.com /* The tag can't possibly exist */ 341110242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 341210242Schris.kirby@sun.com return (ESRCH); 341310242Schris.kirby@sun.com } 341410242Schris.kirby@sun.com 341510242Schris.kirby@sun.com /* Make sure the tag exists */ 341610242Schris.kirby@sun.com error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp); 341710242Schris.kirby@sun.com if (error) { 341810242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 341910242Schris.kirby@sun.com if (error == ENOENT) 342010242Schris.kirby@sun.com error = ESRCH; 342110242Schris.kirby@sun.com return (error); 342210242Schris.kirby@sun.com } 342310242Schris.kirby@sun.com 342410242Schris.kirby@sun.com if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 && 342510242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 342610242Schris.kirby@sun.com *might_destroy = B_TRUE; 342710242Schris.kirby@sun.com 342810242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 342910242Schris.kirby@sun.com return (0); 343010242Schris.kirby@sun.com } 343110242Schris.kirby@sun.com 343210242Schris.kirby@sun.com static int 343310242Schris.kirby@sun.com dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx) 343410242Schris.kirby@sun.com { 343510242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 343610242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 343710242Schris.kirby@sun.com boolean_t might_destroy; 343810242Schris.kirby@sun.com int error; 343910242Schris.kirby@sun.com 344010242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 344110242Schris.kirby@sun.com return (ENOTSUP); 344210242Schris.kirby@sun.com 344310242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy); 344410242Schris.kirby@sun.com if (error) 344510242Schris.kirby@sun.com return (error); 344610242Schris.kirby@sun.com 344710242Schris.kirby@sun.com if (might_destroy) { 344810242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 344910242Schris.kirby@sun.com 345010242Schris.kirby@sun.com if (dmu_tx_is_syncing(tx)) { 345110242Schris.kirby@sun.com /* 345210242Schris.kirby@sun.com * If we're not prepared to remove the snapshot, 345310242Schris.kirby@sun.com * we can't allow the release to happen right now. 345410242Schris.kirby@sun.com */ 345510242Schris.kirby@sun.com if (!ra->own) 345610242Schris.kirby@sun.com return (EBUSY); 345710298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 345810298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 345910298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 346010242Schris.kirby@sun.com } 346110242Schris.kirby@sun.com } 346210242Schris.kirby@sun.com dsda.ds = ds; 346310242Schris.kirby@sun.com dsda.releasing = B_TRUE; 346410242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&dsda, tag, tx)); 346510242Schris.kirby@sun.com } 346610242Schris.kirby@sun.com 346710242Schris.kirby@sun.com return (0); 346810242Schris.kirby@sun.com } 346910242Schris.kirby@sun.com 347010242Schris.kirby@sun.com static void 347110242Schris.kirby@sun.com dsl_dataset_user_release_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 347210242Schris.kirby@sun.com { 347310242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 347410242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 347510342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 347610342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 347710242Schris.kirby@sun.com uint64_t zapobj; 347810242Schris.kirby@sun.com uint64_t dsobj = ds->ds_object; 347910242Schris.kirby@sun.com uint64_t refs; 348010342Schris.kirby@sun.com int error; 348110242Schris.kirby@sun.com 348210242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 348310242Schris.kirby@sun.com ds->ds_userrefs--; 348410242Schris.kirby@sun.com refs = ds->ds_userrefs; 348510242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 348610342Schris.kirby@sun.com error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx); 348710342Schris.kirby@sun.com VERIFY(error == 0 || error == ENOENT); 348810242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 348910242Schris.kirby@sun.com VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx)); 349010242Schris.kirby@sun.com if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && 349110242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) { 349210242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 349310242Schris.kirby@sun.com 349410242Schris.kirby@sun.com ASSERT(ra->own); 349510242Schris.kirby@sun.com dsda.ds = ds; 349610242Schris.kirby@sun.com dsda.releasing = B_TRUE; 349710242Schris.kirby@sun.com /* We already did the destroy_check */ 349810242Schris.kirby@sun.com dsl_dataset_destroy_sync(&dsda, tag, cr, tx); 349910242Schris.kirby@sun.com } 350010242Schris.kirby@sun.com 350110242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_RELEASE, 350210342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> %lld dataset = %llu", 350310242Schris.kirby@sun.com ra->htag, (longlong_t)refs, dsobj); 350410242Schris.kirby@sun.com } 350510242Schris.kirby@sun.com 350610242Schris.kirby@sun.com static int 350710242Schris.kirby@sun.com dsl_dataset_user_release_one(char *dsname, void *arg) 350810242Schris.kirby@sun.com { 350910242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 351010242Schris.kirby@sun.com struct dsl_ds_releasearg *ra; 351110242Schris.kirby@sun.com dsl_dataset_t *ds; 351210242Schris.kirby@sun.com int error; 351310242Schris.kirby@sun.com void *dtag = ha->dstg; 351410242Schris.kirby@sun.com char *name; 351510242Schris.kirby@sun.com boolean_t own = B_FALSE; 351610242Schris.kirby@sun.com boolean_t might_destroy; 351710242Schris.kirby@sun.com 351810242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */ 351910272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 352010242Schris.kirby@sun.com error = dsl_dataset_hold(name, dtag, &ds); 352110272SMatthew.Ahrens@Sun.COM strfree(name); 352210242Schris.kirby@sun.com if (error == ENOENT && ha->recursive) 352310242Schris.kirby@sun.com return (0); 352410242Schris.kirby@sun.com (void) strcpy(ha->failed, dsname); 352510242Schris.kirby@sun.com if (error) 352610242Schris.kirby@sun.com return (error); 352710242Schris.kirby@sun.com 352810268Schris.kirby@sun.com ha->gotone = B_TRUE; 352910268Schris.kirby@sun.com 353010242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 353110242Schris.kirby@sun.com 353210242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy); 353310242Schris.kirby@sun.com if (error) { 353410242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 353510242Schris.kirby@sun.com return (error); 353610242Schris.kirby@sun.com } 353710242Schris.kirby@sun.com 353810242Schris.kirby@sun.com if (might_destroy) { 353910242Schris.kirby@sun.com #ifdef _KERNEL 354010242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 354110242Schris.kirby@sun.com if (error) { 354210242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 354310242Schris.kirby@sun.com return (error); 354410242Schris.kirby@sun.com } 354510242Schris.kirby@sun.com #endif 354610298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) { 354710242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 354810242Schris.kirby@sun.com return (EBUSY); 354910242Schris.kirby@sun.com } else { 355010242Schris.kirby@sun.com own = B_TRUE; 355110242Schris.kirby@sun.com dsl_dataset_make_exclusive(ds, dtag); 355210242Schris.kirby@sun.com } 355310242Schris.kirby@sun.com } 355410242Schris.kirby@sun.com 355510242Schris.kirby@sun.com ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP); 355610242Schris.kirby@sun.com ra->ds = ds; 355710242Schris.kirby@sun.com ra->htag = ha->htag; 355810242Schris.kirby@sun.com ra->own = own; 355910242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check, 356010242Schris.kirby@sun.com dsl_dataset_user_release_sync, ra, dtag, 0); 356110242Schris.kirby@sun.com 356210242Schris.kirby@sun.com return (0); 356310242Schris.kirby@sun.com } 356410242Schris.kirby@sun.com 356510242Schris.kirby@sun.com int 356610242Schris.kirby@sun.com dsl_dataset_user_release(char *dsname, char *snapname, char *htag, 356710242Schris.kirby@sun.com boolean_t recursive) 356810242Schris.kirby@sun.com { 356910242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 357010242Schris.kirby@sun.com dsl_sync_task_t *dst; 357110242Schris.kirby@sun.com spa_t *spa; 357210242Schris.kirby@sun.com int error; 357310242Schris.kirby@sun.com 357410242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 357510242Schris.kirby@sun.com 357610242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 357710242Schris.kirby@sun.com 357810242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 357910242Schris.kirby@sun.com if (error) { 358010242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 358110242Schris.kirby@sun.com return (error); 358210242Schris.kirby@sun.com } 358310242Schris.kirby@sun.com 358410242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 358510242Schris.kirby@sun.com ha->htag = htag; 358610242Schris.kirby@sun.com ha->snapname = snapname; 358710242Schris.kirby@sun.com ha->recursive = recursive; 358810242Schris.kirby@sun.com if (recursive) { 358910242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_release_one, 359010242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 359110242Schris.kirby@sun.com } else { 359210242Schris.kirby@sun.com error = dsl_dataset_user_release_one(dsname, ha); 359310242Schris.kirby@sun.com } 359410242Schris.kirby@sun.com if (error == 0) 359510242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 359610242Schris.kirby@sun.com 359710242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 359810242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 359910242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = dst->dst_arg1; 360010242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 360110242Schris.kirby@sun.com 360210242Schris.kirby@sun.com if (dst->dst_err) 360310242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 360410242Schris.kirby@sun.com 360510242Schris.kirby@sun.com if (ra->own) 360610242Schris.kirby@sun.com dsl_dataset_disown(ds, ha->dstg); 360710242Schris.kirby@sun.com else 360810242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 360910242Schris.kirby@sun.com 361010242Schris.kirby@sun.com kmem_free(ra, sizeof (struct dsl_ds_releasearg)); 361110242Schris.kirby@sun.com } 361210242Schris.kirby@sun.com 361310268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 361410268Schris.kirby@sun.com error = ENOENT; 361510268Schris.kirby@sun.com 361610242Schris.kirby@sun.com if (error) 361710242Schris.kirby@sun.com (void) strcpy(dsname, ha->failed); 361810242Schris.kirby@sun.com 361910242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 362010242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 362110242Schris.kirby@sun.com spa_close(spa, FTAG); 362210242Schris.kirby@sun.com return (error); 362310242Schris.kirby@sun.com } 362410242Schris.kirby@sun.com 362510342Schris.kirby@sun.com /* 362610342Schris.kirby@sun.com * Called at spa_load time to release a stale temporary user hold. 362710342Schris.kirby@sun.com */ 362810342Schris.kirby@sun.com int 362910342Schris.kirby@sun.com dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag) 363010342Schris.kirby@sun.com { 363110342Schris.kirby@sun.com dsl_dataset_t *ds; 363210342Schris.kirby@sun.com char *snap; 363310342Schris.kirby@sun.com char *name; 363410342Schris.kirby@sun.com int namelen; 363510342Schris.kirby@sun.com int error; 363610342Schris.kirby@sun.com 363710342Schris.kirby@sun.com rw_enter(&dp->dp_config_rwlock, RW_READER); 363810342Schris.kirby@sun.com error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 363910342Schris.kirby@sun.com rw_exit(&dp->dp_config_rwlock); 364010342Schris.kirby@sun.com if (error) 364110342Schris.kirby@sun.com return (error); 364210342Schris.kirby@sun.com namelen = dsl_dataset_namelen(ds)+1; 364310342Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 364410342Schris.kirby@sun.com dsl_dataset_name(ds, name); 364510342Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 364610342Schris.kirby@sun.com 364710342Schris.kirby@sun.com snap = strchr(name, '@'); 364810342Schris.kirby@sun.com *snap = '\0'; 364910342Schris.kirby@sun.com ++snap; 365010342Schris.kirby@sun.com return (dsl_dataset_user_release(name, snap, htag, B_FALSE)); 365110342Schris.kirby@sun.com } 365210342Schris.kirby@sun.com 365310242Schris.kirby@sun.com int 365410242Schris.kirby@sun.com dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp) 365510242Schris.kirby@sun.com { 365610242Schris.kirby@sun.com dsl_dataset_t *ds; 365710242Schris.kirby@sun.com int err; 365810242Schris.kirby@sun.com 365910242Schris.kirby@sun.com err = dsl_dataset_hold(dsname, FTAG, &ds); 366010242Schris.kirby@sun.com if (err) 366110242Schris.kirby@sun.com return (err); 366210242Schris.kirby@sun.com 366310242Schris.kirby@sun.com VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)); 366410242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) { 366510242Schris.kirby@sun.com zap_attribute_t *za; 366610242Schris.kirby@sun.com zap_cursor_t zc; 366710242Schris.kirby@sun.com 366810242Schris.kirby@sun.com za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 366910242Schris.kirby@sun.com for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset, 367010242Schris.kirby@sun.com ds->ds_phys->ds_userrefs_obj); 367110242Schris.kirby@sun.com zap_cursor_retrieve(&zc, za) == 0; 367210242Schris.kirby@sun.com zap_cursor_advance(&zc)) { 367310242Schris.kirby@sun.com VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name, 367410242Schris.kirby@sun.com za->za_first_integer)); 367510242Schris.kirby@sun.com } 367610242Schris.kirby@sun.com zap_cursor_fini(&zc); 367710242Schris.kirby@sun.com kmem_free(za, sizeof (zap_attribute_t)); 367810242Schris.kirby@sun.com } 367910242Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 368010242Schris.kirby@sun.com return (0); 368110242Schris.kirby@sun.com } 368210298SMatthew.Ahrens@Sun.COM 368310298SMatthew.Ahrens@Sun.COM /* 368410298SMatthew.Ahrens@Sun.COM * Note, this fuction is used as the callback for dmu_objset_find(). We 368510298SMatthew.Ahrens@Sun.COM * always return 0 so that we will continue to find and process 368610298SMatthew.Ahrens@Sun.COM * inconsistent datasets, even if we encounter an error trying to 368710298SMatthew.Ahrens@Sun.COM * process one of them. 368810298SMatthew.Ahrens@Sun.COM */ 368910298SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 369010298SMatthew.Ahrens@Sun.COM int 369110298SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent(char *dsname, void *arg) 369210298SMatthew.Ahrens@Sun.COM { 369310298SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 369410298SMatthew.Ahrens@Sun.COM 369510298SMatthew.Ahrens@Sun.COM if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) { 369610298SMatthew.Ahrens@Sun.COM if (DS_IS_INCONSISTENT(ds)) 369710298SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(ds, FTAG, B_FALSE); 369810298SMatthew.Ahrens@Sun.COM else 369910298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 370010298SMatthew.Ahrens@Sun.COM } 370110298SMatthew.Ahrens@Sun.COM return (0); 370210298SMatthew.Ahrens@Sun.COM } 3703