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> 4110242Schris.kirby@sun.com #include <sys/zvol.h> 42789Sahrens 436689Smaybee static char *dsl_reaper = "the grim reaper"; 446689Smaybee 452199Sahrens static dsl_checkfunc_t dsl_dataset_destroy_begin_check; 462199Sahrens static dsl_syncfunc_t dsl_dataset_destroy_begin_sync; 475378Sck153898 static dsl_syncfunc_t dsl_dataset_set_reservation_sync; 481731Sbonwick 493444Sek110237 #define DS_REF_MAX (1ULL << 62) 50789Sahrens 51789Sahrens #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 52789Sahrens 536689Smaybee #define DSL_DATASET_IS_DESTROYED(ds) ((ds)->ds_owner == dsl_reaper) 546689Smaybee 55789Sahrens 565378Sck153898 /* 575378Sck153898 * Figure out how much of this delta should be propogated to the dsl_dir 585378Sck153898 * layer. If there's a refreservation, that space has already been 595378Sck153898 * partially accounted for in our ancestors. 605378Sck153898 */ 615378Sck153898 static int64_t 625378Sck153898 parent_delta(dsl_dataset_t *ds, int64_t delta) 635378Sck153898 { 645378Sck153898 uint64_t old_bytes, new_bytes; 655378Sck153898 665378Sck153898 if (ds->ds_reserved == 0) 675378Sck153898 return (delta); 685378Sck153898 695378Sck153898 old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 705378Sck153898 new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 715378Sck153898 725378Sck153898 ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 735378Sck153898 return (new_bytes - old_bytes); 745378Sck153898 } 75789Sahrens 76789Sahrens void 7710922SJeff.Bonwick@Sun.COM dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 78789Sahrens { 7910922SJeff.Bonwick@Sun.COM int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 80789Sahrens int compressed = BP_GET_PSIZE(bp); 81789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 825378Sck153898 int64_t delta; 83789Sahrens 84789Sahrens dprintf_bp(bp, "born, ds=%p\n", ds); 85789Sahrens 86789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 87789Sahrens /* It could have been compressed away to nothing */ 88789Sahrens if (BP_IS_HOLE(bp)) 89789Sahrens return; 90789Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 91789Sahrens ASSERT3U(BP_GET_TYPE(bp), <, DMU_OT_NUMTYPES); 92789Sahrens if (ds == NULL) { 93789Sahrens /* 94789Sahrens * Account for the meta-objset space in its placeholder 95789Sahrens * dsl_dir. 96789Sahrens */ 97789Sahrens ASSERT3U(compressed, ==, uncompressed); /* it's all metadata */ 987390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 99789Sahrens used, compressed, uncompressed, tx); 100789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 101789Sahrens return; 102789Sahrens } 103789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1047595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 105789Sahrens mutex_enter(&ds->ds_lock); 1065378Sck153898 delta = parent_delta(ds, used); 107789Sahrens ds->ds_phys->ds_used_bytes += used; 108789Sahrens ds->ds_phys->ds_compressed_bytes += compressed; 109789Sahrens ds->ds_phys->ds_uncompressed_bytes += uncompressed; 110789Sahrens ds->ds_phys->ds_unique_bytes += used; 111789Sahrens mutex_exit(&ds->ds_lock); 1127390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 1137390SMatthew.Ahrens@Sun.COM compressed, uncompressed, tx); 1147390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used - delta, 1157390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1167595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 117789Sahrens } 118789Sahrens 1196992Smaybee int 12010922SJeff.Bonwick@Sun.COM dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 12110922SJeff.Bonwick@Sun.COM boolean_t async) 122789Sahrens { 12310922SJeff.Bonwick@Sun.COM if (BP_IS_HOLE(bp)) 12410922SJeff.Bonwick@Sun.COM return (0); 12510922SJeff.Bonwick@Sun.COM 12610922SJeff.Bonwick@Sun.COM ASSERT(dmu_tx_is_syncing(tx)); 12710922SJeff.Bonwick@Sun.COM ASSERT(bp->blk_birth <= tx->tx_txg); 12810922SJeff.Bonwick@Sun.COM 12910922SJeff.Bonwick@Sun.COM int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 130789Sahrens int compressed = BP_GET_PSIZE(bp); 131789Sahrens int uncompressed = BP_GET_UCSIZE(bp); 132789Sahrens 133789Sahrens ASSERT(used > 0); 134789Sahrens if (ds == NULL) { 135789Sahrens /* 136789Sahrens * Account for the meta-objset space in its placeholder 137789Sahrens * dataset. 138789Sahrens */ 13910922SJeff.Bonwick@Sun.COM dsl_free(tx->tx_pool, tx->tx_txg, bp); 140789Sahrens 1417390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(tx->tx_pool->dp_mos_dir, DD_USED_HEAD, 142789Sahrens -used, -compressed, -uncompressed, tx); 143789Sahrens dsl_dir_dirty(tx->tx_pool->dp_mos_dir, tx); 1446992Smaybee return (used); 145789Sahrens } 146789Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 147789Sahrens 1487390SMatthew.Ahrens@Sun.COM ASSERT(!dsl_dataset_is_snapshot(ds)); 149789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 150789Sahrens 151789Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 1525378Sck153898 int64_t delta; 1533547Smaybee 154789Sahrens dprintf_bp(bp, "freeing: %s", ""); 15510922SJeff.Bonwick@Sun.COM dsl_free(tx->tx_pool, tx->tx_txg, bp); 156789Sahrens 1577595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 158789Sahrens mutex_enter(&ds->ds_lock); 1595378Sck153898 ASSERT(ds->ds_phys->ds_unique_bytes >= used || 1605378Sck153898 !DS_UNIQUE_IS_ACCURATE(ds)); 1615378Sck153898 delta = parent_delta(ds, -used); 162789Sahrens ds->ds_phys->ds_unique_bytes -= used; 163789Sahrens mutex_exit(&ds->ds_lock); 1647390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 1655378Sck153898 delta, -compressed, -uncompressed, tx); 1667390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, -used - delta, 1677390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, DD_USED_HEAD, tx); 1687595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 169789Sahrens } else { 170789Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 17110922SJeff.Bonwick@Sun.COM if (async) { 17210922SJeff.Bonwick@Sun.COM /* 17310922SJeff.Bonwick@Sun.COM * We are here as part of zio's write done callback, 17410922SJeff.Bonwick@Sun.COM * which means we're a zio interrupt thread. We can't 17510922SJeff.Bonwick@Sun.COM * call bplist_enqueue() now because it may block 17610922SJeff.Bonwick@Sun.COM * waiting for I/O. Instead, put bp on the deferred 17710922SJeff.Bonwick@Sun.COM * queue and let dsl_pool_sync() finish the job. 17810922SJeff.Bonwick@Sun.COM */ 17910922SJeff.Bonwick@Sun.COM bplist_enqueue_deferred(&ds->ds_deadlist, bp); 18010922SJeff.Bonwick@Sun.COM } else { 18110922SJeff.Bonwick@Sun.COM VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, bp, tx)); 18210922SJeff.Bonwick@Sun.COM } 1835712Sahrens ASSERT3U(ds->ds_prev->ds_object, ==, 1845712Sahrens ds->ds_phys->ds_prev_snap_obj); 1855712Sahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 186789Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 1875712Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 1885712Sahrens ds->ds_object && bp->blk_birth > 1895712Sahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 1905712Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1915712Sahrens mutex_enter(&ds->ds_prev->ds_lock); 1925712Sahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 1935712Sahrens mutex_exit(&ds->ds_prev->ds_lock); 194789Sahrens } 1957390SMatthew.Ahrens@Sun.COM if (bp->blk_birth > ds->ds_origin_txg) { 1967390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(ds->ds_dir, used, 1977390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 1987390SMatthew.Ahrens@Sun.COM } 199789Sahrens } 200789Sahrens mutex_enter(&ds->ds_lock); 201789Sahrens ASSERT3U(ds->ds_phys->ds_used_bytes, >=, used); 202789Sahrens ds->ds_phys->ds_used_bytes -= used; 203789Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 204789Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 205789Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 206789Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 207789Sahrens mutex_exit(&ds->ds_lock); 2086992Smaybee 2096992Smaybee return (used); 210789Sahrens } 211789Sahrens 2121544Seschrock uint64_t 2131544Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 214789Sahrens { 2152885Sahrens uint64_t trysnap = 0; 2162885Sahrens 217789Sahrens if (ds == NULL) 2181544Seschrock return (0); 219789Sahrens /* 220789Sahrens * The snapshot creation could fail, but that would cause an 221789Sahrens * incorrect FALSE return, which would only result in an 222789Sahrens * overestimation of the amount of space that an operation would 223789Sahrens * consume, which is OK. 224789Sahrens * 225789Sahrens * There's also a small window where we could miss a pending 226789Sahrens * snapshot, because we could set the sync task in the quiescing 227789Sahrens * phase. So this should only be used as a guess. 228789Sahrens */ 2292885Sahrens if (ds->ds_trysnap_txg > 2302885Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 2312885Sahrens trysnap = ds->ds_trysnap_txg; 2322885Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 2331544Seschrock } 2341544Seschrock 2359653SSanjeev.Bagewadi@Sun.COM boolean_t 2361544Seschrock dsl_dataset_block_freeable(dsl_dataset_t *ds, uint64_t blk_birth) 2371544Seschrock { 2381544Seschrock return (blk_birth > dsl_dataset_prev_snap_txg(ds)); 239789Sahrens } 240789Sahrens 241789Sahrens /* ARGSUSED */ 242789Sahrens static void 243789Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 244789Sahrens { 245789Sahrens dsl_dataset_t *ds = dsv; 246789Sahrens 2476689Smaybee ASSERT(ds->ds_owner == NULL || DSL_DATASET_IS_DESTROYED(ds)); 248789Sahrens 2494787Sahrens unique_remove(ds->ds_fsid_guid); 250789Sahrens 25110298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) 25210298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 253789Sahrens 254789Sahrens if (ds->ds_prev) { 2556689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 256789Sahrens ds->ds_prev = NULL; 257789Sahrens } 258789Sahrens 259789Sahrens bplist_close(&ds->ds_deadlist); 2606689Smaybee if (ds->ds_dir) 2616689Smaybee dsl_dir_close(ds->ds_dir, ds); 262789Sahrens 2634787Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 264789Sahrens 2652856Snd150628 mutex_destroy(&ds->ds_lock); 26610204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 2674787Sahrens mutex_destroy(&ds->ds_opening_lock); 2686689Smaybee rw_destroy(&ds->ds_rwlock); 2696689Smaybee cv_destroy(&ds->ds_exclusive_cv); 27010922SJeff.Bonwick@Sun.COM bplist_fini(&ds->ds_deadlist); 2712856Snd150628 272789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 273789Sahrens } 274789Sahrens 2751544Seschrock static int 276789Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 277789Sahrens { 278789Sahrens dsl_dataset_phys_t *headphys; 279789Sahrens int err; 280789Sahrens dmu_buf_t *headdbuf; 281789Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 282789Sahrens objset_t *mos = dp->dp_meta_objset; 283789Sahrens 284789Sahrens if (ds->ds_snapname[0]) 2851544Seschrock return (0); 286789Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 2871544Seschrock return (0); 288789Sahrens 2891544Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 2901544Seschrock FTAG, &headdbuf); 2911544Seschrock if (err) 2921544Seschrock return (err); 293789Sahrens headphys = headdbuf->db_data; 294789Sahrens err = zap_value_search(dp->dp_meta_objset, 2954577Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 2961544Seschrock dmu_buf_rele(headdbuf, FTAG); 2971544Seschrock return (err); 298789Sahrens } 299789Sahrens 3006492Stimh static int 3016689Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 3026492Stimh { 3036689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3046689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3056492Stimh matchtype_t mt; 3066492Stimh int err; 3076492Stimh 3086689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3096492Stimh mt = MT_FIRST; 3106492Stimh else 3116492Stimh mt = MT_EXACT; 3126492Stimh 3136689Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 3146492Stimh value, mt, NULL, 0, NULL); 3156492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3166689Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 3176492Stimh return (err); 3186492Stimh } 3196492Stimh 3206492Stimh static int 3216689Smaybee dsl_dataset_snap_remove(dsl_dataset_t *ds, char *name, dmu_tx_t *tx) 3226492Stimh { 3236689Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 3246689Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 3256492Stimh matchtype_t mt; 3266492Stimh int err; 3276492Stimh 32810373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(ds->ds_dir); 32910373Schris.kirby@sun.com 3306689Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 3316492Stimh mt = MT_FIRST; 3326492Stimh else 3336492Stimh mt = MT_EXACT; 3346492Stimh 3356689Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 3366492Stimh if (err == ENOTSUP && mt == MT_FIRST) 3376689Smaybee err = zap_remove(mos, snapobj, name, tx); 3386492Stimh return (err); 3396492Stimh } 3406492Stimh 3416689Smaybee static int 3426689Smaybee dsl_dataset_get_ref(dsl_pool_t *dp, uint64_t dsobj, void *tag, 3436689Smaybee dsl_dataset_t **dsp) 344789Sahrens { 345789Sahrens objset_t *mos = dp->dp_meta_objset; 346789Sahrens dmu_buf_t *dbuf; 347789Sahrens dsl_dataset_t *ds; 3481544Seschrock int err; 349789Sahrens 350789Sahrens ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 351789Sahrens dsl_pool_sync_context(dp)); 352789Sahrens 3531544Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3541544Seschrock if (err) 3551544Seschrock return (err); 356789Sahrens ds = dmu_buf_get_user(dbuf); 357789Sahrens if (ds == NULL) { 358789Sahrens dsl_dataset_t *winner; 359789Sahrens 360789Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 361789Sahrens ds->ds_dbuf = dbuf; 362789Sahrens ds->ds_object = dsobj; 363789Sahrens ds->ds_phys = dbuf->db_data; 364789Sahrens 3652856Snd150628 mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 36610204SMatthew.Ahrens@Sun.COM mutex_init(&ds->ds_recvlock, NULL, MUTEX_DEFAULT, NULL); 3674787Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3686689Smaybee rw_init(&ds->ds_rwlock, 0, 0, 0); 3696689Smaybee cv_init(&ds->ds_exclusive_cv, NULL, CV_DEFAULT, NULL); 37010922SJeff.Bonwick@Sun.COM bplist_init(&ds->ds_deadlist); 3712856Snd150628 3721544Seschrock err = bplist_open(&ds->ds_deadlist, 373789Sahrens mos, ds->ds_phys->ds_deadlist_obj); 3741544Seschrock if (err == 0) { 3751544Seschrock err = dsl_dir_open_obj(dp, 3761544Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 3771544Seschrock } 3781544Seschrock if (err) { 3791544Seschrock /* 3801544Seschrock * we don't really need to close the blist if we 3811544Seschrock * just opened it. 3821544Seschrock */ 3832856Snd150628 mutex_destroy(&ds->ds_lock); 38410204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 3854787Sahrens mutex_destroy(&ds->ds_opening_lock); 3866689Smaybee rw_destroy(&ds->ds_rwlock); 3876689Smaybee cv_destroy(&ds->ds_exclusive_cv); 38810922SJeff.Bonwick@Sun.COM bplist_fini(&ds->ds_deadlist); 3891544Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 3901544Seschrock dmu_buf_rele(dbuf, tag); 3911544Seschrock return (err); 3921544Seschrock } 393789Sahrens 3947390SMatthew.Ahrens@Sun.COM if (!dsl_dataset_is_snapshot(ds)) { 395789Sahrens ds->ds_snapname[0] = '\0'; 396789Sahrens if (ds->ds_phys->ds_prev_snap_obj) { 3976689Smaybee err = dsl_dataset_get_ref(dp, 3986689Smaybee ds->ds_phys->ds_prev_snap_obj, 3996689Smaybee ds, &ds->ds_prev); 400789Sahrens } 4017390SMatthew.Ahrens@Sun.COM 4027390SMatthew.Ahrens@Sun.COM if (err == 0 && dsl_dir_is_clone(ds->ds_dir)) { 4037390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin; 4047390SMatthew.Ahrens@Sun.COM 4057390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, 4067390SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_phys->dd_origin_obj, 4077390SMatthew.Ahrens@Sun.COM FTAG, &origin); 4087390SMatthew.Ahrens@Sun.COM if (err == 0) { 4097390SMatthew.Ahrens@Sun.COM ds->ds_origin_txg = 4107390SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_creation_txg; 4117390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(origin, FTAG); 4127390SMatthew.Ahrens@Sun.COM } 4137390SMatthew.Ahrens@Sun.COM } 41410242Schris.kirby@sun.com } else { 41510242Schris.kirby@sun.com if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 41610242Schris.kirby@sun.com err = dsl_dataset_get_snapname(ds); 41710242Schris.kirby@sun.com if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) { 41810242Schris.kirby@sun.com err = zap_count( 41910242Schris.kirby@sun.com ds->ds_dir->dd_pool->dp_meta_objset, 42010242Schris.kirby@sun.com ds->ds_phys->ds_userrefs_obj, 42110242Schris.kirby@sun.com &ds->ds_userrefs); 42210242Schris.kirby@sun.com } 423789Sahrens } 424789Sahrens 4257390SMatthew.Ahrens@Sun.COM if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 4265569Sck153898 /* 4275569Sck153898 * In sync context, we're called with either no lock 4285569Sck153898 * or with the write lock. If we're not syncing, 4295569Sck153898 * we're always called with the read lock held. 4305569Sck153898 */ 4315475Sck153898 boolean_t need_lock = 4325569Sck153898 !RW_WRITE_HELD(&dp->dp_config_rwlock) && 4335569Sck153898 dsl_pool_sync_context(dp); 4345475Sck153898 4355475Sck153898 if (need_lock) 4365475Sck153898 rw_enter(&dp->dp_config_rwlock, RW_READER); 4375475Sck153898 4387265Sahrens err = dsl_prop_get_ds(ds, 4395475Sck153898 "refreservation", sizeof (uint64_t), 1, 4405475Sck153898 &ds->ds_reserved, NULL); 4415475Sck153898 if (err == 0) { 4427265Sahrens err = dsl_prop_get_ds(ds, 4435475Sck153898 "refquota", sizeof (uint64_t), 1, 4445475Sck153898 &ds->ds_quota, NULL); 4455475Sck153898 } 4465475Sck153898 4475475Sck153898 if (need_lock) 4485475Sck153898 rw_exit(&dp->dp_config_rwlock); 4495475Sck153898 } else { 4505475Sck153898 ds->ds_reserved = ds->ds_quota = 0; 4515475Sck153898 } 4525475Sck153898 4531544Seschrock if (err == 0) { 4541544Seschrock winner = dmu_buf_set_user_ie(dbuf, ds, &ds->ds_phys, 4551544Seschrock dsl_dataset_evict); 4561544Seschrock } 4571544Seschrock if (err || winner) { 458789Sahrens bplist_close(&ds->ds_deadlist); 4596689Smaybee if (ds->ds_prev) 4606689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 461789Sahrens dsl_dir_close(ds->ds_dir, ds); 4622856Snd150628 mutex_destroy(&ds->ds_lock); 46310204SMatthew.Ahrens@Sun.COM mutex_destroy(&ds->ds_recvlock); 4644787Sahrens mutex_destroy(&ds->ds_opening_lock); 4656689Smaybee rw_destroy(&ds->ds_rwlock); 4666689Smaybee cv_destroy(&ds->ds_exclusive_cv); 46710922SJeff.Bonwick@Sun.COM bplist_fini(&ds->ds_deadlist); 468789Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4691544Seschrock if (err) { 4701544Seschrock dmu_buf_rele(dbuf, tag); 4711544Seschrock return (err); 4721544Seschrock } 473789Sahrens ds = winner; 474789Sahrens } else { 4754787Sahrens ds->ds_fsid_guid = 476789Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 477789Sahrens } 478789Sahrens } 479789Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 480789Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 4817046Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 4827061Sahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 4837077Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 484789Sahrens mutex_enter(&ds->ds_lock); 4856689Smaybee if (!dsl_pool_sync_context(dp) && DSL_DATASET_IS_DESTROYED(ds)) { 486789Sahrens mutex_exit(&ds->ds_lock); 4876689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 4886689Smaybee return (ENOENT); 4896689Smaybee } 4906689Smaybee mutex_exit(&ds->ds_lock); 4916689Smaybee *dsp = ds; 4926689Smaybee return (0); 4936689Smaybee } 4946689Smaybee 4956689Smaybee static int 4966689Smaybee dsl_dataset_hold_ref(dsl_dataset_t *ds, void *tag) 4976689Smaybee { 4986689Smaybee dsl_pool_t *dp = ds->ds_dir->dd_pool; 4996689Smaybee 5006689Smaybee /* 5016689Smaybee * In syncing context we don't want the rwlock lock: there 5026689Smaybee * may be an existing writer waiting for sync phase to 5036689Smaybee * finish. We don't need to worry about such writers, since 5046689Smaybee * sync phase is single-threaded, so the writer can't be 5056689Smaybee * doing anything while we are active. 5066689Smaybee */ 5076689Smaybee if (dsl_pool_sync_context(dp)) { 5086689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 5096689Smaybee return (0); 510789Sahrens } 5116689Smaybee 5126689Smaybee /* 5136689Smaybee * Normal users will hold the ds_rwlock as a READER until they 5146689Smaybee * are finished (i.e., call dsl_dataset_rele()). "Owners" will 5156689Smaybee * drop their READER lock after they set the ds_owner field. 5166689Smaybee * 5176689Smaybee * If the dataset is being destroyed, the destroy thread will 5186689Smaybee * obtain a WRITER lock for exclusive access after it's done its 5196689Smaybee * open-context work and then change the ds_owner to 5206689Smaybee * dsl_reaper once destruction is assured. So threads 5216689Smaybee * may block here temporarily, until the "destructability" of 5226689Smaybee * the dataset is determined. 5236689Smaybee */ 5246689Smaybee ASSERT(!RW_WRITE_HELD(&dp->dp_config_rwlock)); 5256689Smaybee mutex_enter(&ds->ds_lock); 5266689Smaybee while (!rw_tryenter(&ds->ds_rwlock, RW_READER)) { 5276689Smaybee rw_exit(&dp->dp_config_rwlock); 5286689Smaybee cv_wait(&ds->ds_exclusive_cv, &ds->ds_lock); 5296689Smaybee if (DSL_DATASET_IS_DESTROYED(ds)) { 5306689Smaybee mutex_exit(&ds->ds_lock); 5316689Smaybee dsl_dataset_drop_ref(ds, tag); 5326689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5336689Smaybee return (ENOENT); 5346689Smaybee } 5356689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 5366689Smaybee } 537789Sahrens mutex_exit(&ds->ds_lock); 5381544Seschrock return (0); 539789Sahrens } 540789Sahrens 541789Sahrens int 5426689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 5436689Smaybee dsl_dataset_t **dsp) 5446689Smaybee { 5456689Smaybee int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 5466689Smaybee 5476689Smaybee if (err) 5486689Smaybee return (err); 5496689Smaybee return (dsl_dataset_hold_ref(*dsp, tag)); 5506689Smaybee } 5516689Smaybee 5526689Smaybee int 55310298SMatthew.Ahrens@Sun.COM dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok, 55410298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 5556689Smaybee { 55610298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5576689Smaybee if (err) 5586689Smaybee return (err); 55910298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 56010298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 5618779SMark.Musante@Sun.COM *dsp = NULL; 5626689Smaybee return (EBUSY); 5636689Smaybee } 5646689Smaybee return (0); 5656689Smaybee } 5666689Smaybee 5676689Smaybee int 5686689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 569789Sahrens { 570789Sahrens dsl_dir_t *dd; 571789Sahrens dsl_pool_t *dp; 5726689Smaybee const char *snapname; 573789Sahrens uint64_t obj; 574789Sahrens int err = 0; 575789Sahrens 5766689Smaybee err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 5771544Seschrock if (err) 5781544Seschrock return (err); 579789Sahrens 580789Sahrens dp = dd->dd_pool; 581789Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 582789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 5836689Smaybee if (obj) 5846689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, dsp); 5856689Smaybee else 586789Sahrens err = ENOENT; 5876689Smaybee if (err) 588789Sahrens goto out; 5896689Smaybee 5906689Smaybee err = dsl_dataset_hold_ref(*dsp, tag); 5916689Smaybee 5926689Smaybee /* we may be looking for a snapshot */ 5936689Smaybee if (err == 0 && snapname != NULL) { 5946689Smaybee dsl_dataset_t *ds = NULL; 5956689Smaybee 5966689Smaybee if (*snapname++ != '@') { 5976689Smaybee dsl_dataset_rele(*dsp, tag); 598789Sahrens err = ENOENT; 599789Sahrens goto out; 600789Sahrens } 6016689Smaybee 6026689Smaybee dprintf("looking for snapshot '%s'\n", snapname); 6036689Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 6046689Smaybee if (err == 0) 6056689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, &ds); 6066689Smaybee dsl_dataset_rele(*dsp, tag); 6076689Smaybee 6086689Smaybee ASSERT3U((err == 0), ==, (ds != NULL)); 6096689Smaybee 6106689Smaybee if (ds) { 6116689Smaybee mutex_enter(&ds->ds_lock); 6126689Smaybee if (ds->ds_snapname[0] == 0) 6136689Smaybee (void) strlcpy(ds->ds_snapname, snapname, 6146689Smaybee sizeof (ds->ds_snapname)); 6156689Smaybee mutex_exit(&ds->ds_lock); 6166689Smaybee err = dsl_dataset_hold_ref(ds, tag); 6176689Smaybee *dsp = err ? NULL : ds; 618789Sahrens } 619789Sahrens } 620789Sahrens out: 621789Sahrens rw_exit(&dp->dp_config_rwlock); 622789Sahrens dsl_dir_close(dd, FTAG); 623789Sahrens return (err); 624789Sahrens } 625789Sahrens 626789Sahrens int 62710298SMatthew.Ahrens@Sun.COM dsl_dataset_own(const char *name, boolean_t inconsistentok, 62810298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 629789Sahrens { 63010298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold(name, tag, dsp); 6316689Smaybee if (err) 6326689Smaybee return (err); 63310298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 63410298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 6356689Smaybee return (EBUSY); 6366689Smaybee } 6376689Smaybee return (0); 638789Sahrens } 639789Sahrens 640789Sahrens void 641789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 642789Sahrens { 643789Sahrens if (ds == NULL) { 644789Sahrens (void) strcpy(name, "mos"); 645789Sahrens } else { 646789Sahrens dsl_dir_name(ds->ds_dir, name); 6471544Seschrock VERIFY(0 == dsl_dataset_get_snapname(ds)); 648789Sahrens if (ds->ds_snapname[0]) { 649789Sahrens (void) strcat(name, "@"); 6506689Smaybee /* 6516689Smaybee * We use a "recursive" mutex so that we 6526689Smaybee * can call dprintf_ds() with ds_lock held. 6536689Smaybee */ 654789Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 655789Sahrens mutex_enter(&ds->ds_lock); 656789Sahrens (void) strcat(name, ds->ds_snapname); 657789Sahrens mutex_exit(&ds->ds_lock); 658789Sahrens } else { 659789Sahrens (void) strcat(name, ds->ds_snapname); 660789Sahrens } 661789Sahrens } 662789Sahrens } 663789Sahrens } 664789Sahrens 6653978Smmusante static int 6663978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds) 6673978Smmusante { 6683978Smmusante int result; 6693978Smmusante 6703978Smmusante if (ds == NULL) { 6713978Smmusante result = 3; /* "mos" */ 6723978Smmusante } else { 6733978Smmusante result = dsl_dir_namelen(ds->ds_dir); 6743978Smmusante VERIFY(0 == dsl_dataset_get_snapname(ds)); 6753978Smmusante if (ds->ds_snapname[0]) { 6763978Smmusante ++result; /* adding one for the @-sign */ 6773978Smmusante if (!MUTEX_HELD(&ds->ds_lock)) { 6783978Smmusante mutex_enter(&ds->ds_lock); 6793978Smmusante result += strlen(ds->ds_snapname); 6803978Smmusante mutex_exit(&ds->ds_lock); 6813978Smmusante } else { 6823978Smmusante result += strlen(ds->ds_snapname); 6833978Smmusante } 6843978Smmusante } 6853978Smmusante } 6863978Smmusante 6873978Smmusante return (result); 6883978Smmusante } 6893978Smmusante 6907046Sahrens void 6916689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 692789Sahrens { 6931544Seschrock dmu_buf_rele(ds->ds_dbuf, tag); 694789Sahrens } 695789Sahrens 696789Sahrens void 6976689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6985367Sahrens { 6996689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 7006689Smaybee rw_exit(&ds->ds_rwlock); 7016689Smaybee } 7026689Smaybee dsl_dataset_drop_ref(ds, tag); 7036689Smaybee } 7046689Smaybee 7056689Smaybee void 70610298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 7076689Smaybee { 70810298SMatthew.Ahrens@Sun.COM ASSERT((ds->ds_owner == tag && ds->ds_dbuf) || 7096689Smaybee (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 7106689Smaybee 7115367Sahrens mutex_enter(&ds->ds_lock); 7126689Smaybee ds->ds_owner = NULL; 7136689Smaybee if (RW_WRITE_HELD(&ds->ds_rwlock)) { 7146689Smaybee rw_exit(&ds->ds_rwlock); 7156689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 7166689Smaybee } 7175367Sahrens mutex_exit(&ds->ds_lock); 7186689Smaybee if (ds->ds_dbuf) 71910298SMatthew.Ahrens@Sun.COM dsl_dataset_drop_ref(ds, tag); 7206689Smaybee else 7216689Smaybee dsl_dataset_evict(ds->ds_dbuf, ds); 7225367Sahrens } 7235367Sahrens 7245367Sahrens boolean_t 72510298SMatthew.Ahrens@Sun.COM dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag) 7265367Sahrens { 7276689Smaybee boolean_t gotit = FALSE; 7286689Smaybee 7295367Sahrens mutex_enter(&ds->ds_lock); 7306689Smaybee if (ds->ds_owner == NULL && 7316689Smaybee (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 73210298SMatthew.Ahrens@Sun.COM ds->ds_owner = tag; 7336689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 7346689Smaybee rw_exit(&ds->ds_rwlock); 7356689Smaybee gotit = TRUE; 7365367Sahrens } 7375367Sahrens mutex_exit(&ds->ds_lock); 7386689Smaybee return (gotit); 7396689Smaybee } 7406689Smaybee 7416689Smaybee void 7426689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 7436689Smaybee { 7446689Smaybee ASSERT3P(owner, ==, ds->ds_owner); 7456689Smaybee if (!RW_WRITE_HELD(&ds->ds_rwlock)) 7466689Smaybee rw_enter(&ds->ds_rwlock, RW_WRITER); 7475367Sahrens } 7485367Sahrens 7492199Sahrens uint64_t 7507046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 7516492Stimh uint64_t flags, dmu_tx_t *tx) 752789Sahrens { 7535367Sahrens dsl_pool_t *dp = dd->dd_pool; 754789Sahrens dmu_buf_t *dbuf; 755789Sahrens dsl_dataset_phys_t *dsphys; 7565367Sahrens uint64_t dsobj; 757789Sahrens objset_t *mos = dp->dp_meta_objset; 758789Sahrens 7597046Sahrens if (origin == NULL) 7607046Sahrens origin = dp->dp_origin_snap; 7617046Sahrens 7625367Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 7635367Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 764789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 7655367Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 766789Sahrens 767928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 768928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7691544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 770789Sahrens dmu_buf_will_dirty(dbuf, tx); 771789Sahrens dsphys = dbuf->db_data; 7726689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 773789Sahrens dsphys->ds_dir_obj = dd->dd_object; 7746492Stimh dsphys->ds_flags = flags; 775789Sahrens dsphys->ds_fsid_guid = unique_create(); 776789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 777789Sahrens sizeof (dsphys->ds_guid)); 778789Sahrens dsphys->ds_snapnames_zapobj = 7796492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7806492Stimh DMU_OT_NONE, 0, tx); 781789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 7827046Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 783789Sahrens dsphys->ds_deadlist_obj = 784789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7855378Sck153898 7865367Sahrens if (origin) { 7875367Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 788789Sahrens dsphys->ds_prev_snap_txg = 7895367Sahrens origin->ds_phys->ds_creation_txg; 790789Sahrens dsphys->ds_used_bytes = 7915367Sahrens origin->ds_phys->ds_used_bytes; 792789Sahrens dsphys->ds_compressed_bytes = 7935367Sahrens origin->ds_phys->ds_compressed_bytes; 794789Sahrens dsphys->ds_uncompressed_bytes = 7955367Sahrens origin->ds_phys->ds_uncompressed_bytes; 7965367Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 7976502Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 798789Sahrens 7995367Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 8005367Sahrens origin->ds_phys->ds_num_children++; 801789Sahrens 8027046Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 8037046Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 8047046Sahrens origin->ds_phys->ds_next_clones_obj = 8057046Sahrens zap_create(mos, 8067046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 8077046Sahrens } 8087046Sahrens VERIFY(0 == zap_add_int(mos, 8097046Sahrens origin->ds_phys->ds_next_clones_obj, 8107046Sahrens dsobj, tx)); 8117046Sahrens } 8127046Sahrens 813789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 8145367Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 815789Sahrens } 8166492Stimh 8176492Stimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 8186492Stimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 8196492Stimh 8201544Seschrock dmu_buf_rele(dbuf, FTAG); 821789Sahrens 822789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 823789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 8245367Sahrens 8255367Sahrens return (dsobj); 8265367Sahrens } 8275367Sahrens 8285367Sahrens uint64_t 8296492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 8306492Stimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8315367Sahrens { 8325367Sahrens dsl_pool_t *dp = pdd->dd_pool; 8335367Sahrens uint64_t dsobj, ddobj; 8345367Sahrens dsl_dir_t *dd; 8355367Sahrens 8365367Sahrens ASSERT(lastname[0] != '@'); 8375367Sahrens 8387046Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8395367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 8405367Sahrens 8417046Sahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx); 8425367Sahrens 8435367Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8445367Sahrens 845789Sahrens dsl_dir_close(dd, FTAG); 846789Sahrens 8472199Sahrens return (dsobj); 8482199Sahrens } 8492199Sahrens 8502199Sahrens struct destroyarg { 8512199Sahrens dsl_sync_task_group_t *dstg; 8522199Sahrens char *snapname; 8532199Sahrens char *failed; 85410242Schris.kirby@sun.com boolean_t defer; 8552199Sahrens }; 8562199Sahrens 8572199Sahrens static int 8582199Sahrens dsl_snapshot_destroy_one(char *name, void *arg) 8592199Sahrens { 8602199Sahrens struct destroyarg *da = arg; 8612199Sahrens dsl_dataset_t *ds; 8622199Sahrens int err; 86310242Schris.kirby@sun.com char *dsname; 86410272SMatthew.Ahrens@Sun.COM 86510272SMatthew.Ahrens@Sun.COM dsname = kmem_asprintf("%s@%s", name, da->snapname); 86610298SMatthew.Ahrens@Sun.COM err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds); 86710272SMatthew.Ahrens@Sun.COM strfree(dsname); 8686689Smaybee if (err == 0) { 86910242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda; 87010242Schris.kirby@sun.com 8716689Smaybee dsl_dataset_make_exclusive(ds, da->dstg); 87210298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) { 87310298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 87410298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 8757237Sek110237 } 87610242Schris.kirby@sun.com dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP); 87710242Schris.kirby@sun.com dsda->ds = ds; 87810242Schris.kirby@sun.com dsda->defer = da->defer; 8796689Smaybee dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 88010242Schris.kirby@sun.com dsl_dataset_destroy_sync, dsda, da->dstg, 0); 8816689Smaybee } else if (err == ENOENT) { 8826689Smaybee err = 0; 8836689Smaybee } else { 8842199Sahrens (void) strcpy(da->failed, name); 8852199Sahrens } 8866689Smaybee return (err); 887789Sahrens } 888789Sahrens 8892199Sahrens /* 8902199Sahrens * Destroy 'snapname' in all descendants of 'fsname'. 8912199Sahrens */ 8922199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 8932199Sahrens int 89410242Schris.kirby@sun.com dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer) 8952199Sahrens { 8962199Sahrens int err; 8972199Sahrens struct destroyarg da; 8982199Sahrens dsl_sync_task_t *dst; 8992199Sahrens spa_t *spa; 9002199Sahrens 9014603Sahrens err = spa_open(fsname, &spa, FTAG); 9022199Sahrens if (err) 9032199Sahrens return (err); 9042199Sahrens da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 9052199Sahrens da.snapname = snapname; 9062199Sahrens da.failed = fsname; 90710242Schris.kirby@sun.com da.defer = defer; 9082199Sahrens 9092199Sahrens err = dmu_objset_find(fsname, 9102417Sahrens dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 9112199Sahrens 9122199Sahrens if (err == 0) 9132199Sahrens err = dsl_sync_task_group_wait(da.dstg); 9142199Sahrens 9152199Sahrens for (dst = list_head(&da.dstg->dstg_tasks); dst; 9162199Sahrens dst = list_next(&da.dstg->dstg_tasks, dst)) { 91710242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = dst->dst_arg1; 91810242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 91910242Schris.kirby@sun.com 9206689Smaybee /* 9216689Smaybee * Return the file system name that triggered the error 9226689Smaybee */ 9232199Sahrens if (dst->dst_err) { 9242199Sahrens dsl_dataset_name(ds, fsname); 9254603Sahrens *strchr(fsname, '@') = '\0'; 9262199Sahrens } 92710242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 9286689Smaybee dsl_dataset_disown(ds, da.dstg); 92910242Schris.kirby@sun.com kmem_free(dsda, sizeof (struct dsl_ds_destroyarg)); 9302199Sahrens } 9312199Sahrens 9322199Sahrens dsl_sync_task_group_destroy(da.dstg); 9332199Sahrens spa_close(spa, FTAG); 9342199Sahrens return (err); 9352199Sahrens } 9362199Sahrens 93710242Schris.kirby@sun.com static boolean_t 93810242Schris.kirby@sun.com dsl_dataset_might_destroy_origin(dsl_dataset_t *ds) 93910242Schris.kirby@sun.com { 94010242Schris.kirby@sun.com boolean_t might_destroy = B_FALSE; 94110242Schris.kirby@sun.com 94210242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 94310242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 && 94410242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 94510242Schris.kirby@sun.com might_destroy = B_TRUE; 94610242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 94710242Schris.kirby@sun.com 94810242Schris.kirby@sun.com return (might_destroy); 94910242Schris.kirby@sun.com } 95010242Schris.kirby@sun.com 95110242Schris.kirby@sun.com /* 95210242Schris.kirby@sun.com * If we're removing a clone, and these three conditions are true: 95310242Schris.kirby@sun.com * 1) the clone's origin has no other children 95410242Schris.kirby@sun.com * 2) the clone's origin has no user references 95510242Schris.kirby@sun.com * 3) the clone's origin has been marked for deferred destruction 95610242Schris.kirby@sun.com * Then, prepare to remove the origin as part of this sync task group. 95710242Schris.kirby@sun.com */ 95810242Schris.kirby@sun.com static int 95910242Schris.kirby@sun.com dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag) 96010242Schris.kirby@sun.com { 96110242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 96210242Schris.kirby@sun.com dsl_dataset_t *origin = ds->ds_prev; 96310242Schris.kirby@sun.com 96410242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(origin)) { 96510242Schris.kirby@sun.com char *name; 96610242Schris.kirby@sun.com int namelen; 96710242Schris.kirby@sun.com int error; 96810242Schris.kirby@sun.com 96910242Schris.kirby@sun.com namelen = dsl_dataset_namelen(origin) + 1; 97010242Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 97110242Schris.kirby@sun.com dsl_dataset_name(origin, name); 97210242Schris.kirby@sun.com #ifdef _KERNEL 97310242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 97410242Schris.kirby@sun.com if (error) { 97510242Schris.kirby@sun.com kmem_free(name, namelen); 97610242Schris.kirby@sun.com return (error); 97710242Schris.kirby@sun.com } 97810242Schris.kirby@sun.com #endif 97910298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(name, B_TRUE, tag, &origin); 98010242Schris.kirby@sun.com kmem_free(name, namelen); 98110242Schris.kirby@sun.com if (error) 98210242Schris.kirby@sun.com return (error); 98310242Schris.kirby@sun.com dsda->rm_origin = origin; 98410242Schris.kirby@sun.com dsl_dataset_make_exclusive(origin, tag); 98510342Schris.kirby@sun.com 98610342Schris.kirby@sun.com if (origin->ds_objset != NULL) { 98710342Schris.kirby@sun.com dmu_objset_evict(origin->ds_objset); 98810342Schris.kirby@sun.com origin->ds_objset = NULL; 98910342Schris.kirby@sun.com } 99010242Schris.kirby@sun.com } 99110242Schris.kirby@sun.com 99210242Schris.kirby@sun.com return (0); 99310242Schris.kirby@sun.com } 99410242Schris.kirby@sun.com 9955367Sahrens /* 9966689Smaybee * ds must be opened as OWNER. On return (whether successful or not), 9976689Smaybee * ds will be closed and caller can no longer dereference it. 9985367Sahrens */ 999789Sahrens int 100010242Schris.kirby@sun.com dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer) 1001789Sahrens { 1002789Sahrens int err; 10032199Sahrens dsl_sync_task_group_t *dstg; 10042199Sahrens objset_t *os; 1005789Sahrens dsl_dir_t *dd; 10062199Sahrens uint64_t obj; 1007*11022STom.Erickson@Sun.COM struct dsl_ds_destroyarg dsda = { 0 }; 1008*11022STom.Erickson@Sun.COM dsl_dataset_t dummy_ds = { 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; 1032*11022STom.Erickson@Sun.COM dummy_ds.ds_dir = dd; 1033*11022STom.Erickson@Sun.COM dummy_ds.ds_object = ds->ds_object; 1034789Sahrens 10352199Sahrens /* 10362199Sahrens * Check for errors and mark this ds as inconsistent, in 10372199Sahrens * case we crash while freeing the objects. 10382199Sahrens */ 10392199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 10402199Sahrens dsl_dataset_destroy_begin_sync, ds, NULL, 0); 10415367Sahrens if (err) 10425367Sahrens goto out; 10435367Sahrens 104410298SMatthew.Ahrens@Sun.COM err = dmu_objset_from_ds(ds, &os); 10455367Sahrens if (err) 10465367Sahrens goto out; 10472199Sahrens 10482199Sahrens /* 10492199Sahrens * remove the objects in open context, so that we won't 10502199Sahrens * have too much to do in syncing context. 10512199Sahrens */ 10523025Sahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 10533025Sahrens ds->ds_phys->ds_prev_snap_txg)) { 10546992Smaybee /* 10556992Smaybee * Ignore errors, if there is not enough disk space 10566992Smaybee * we will deal with it in dsl_dataset_destroy_sync(). 10576992Smaybee */ 10586992Smaybee (void) dmu_free_object(os, obj); 10592199Sahrens } 10602199Sahrens 10619396SMatthew.Ahrens@Sun.COM /* 10629396SMatthew.Ahrens@Sun.COM * We need to sync out all in-flight IO before we try to evict 10639396SMatthew.Ahrens@Sun.COM * (the dataset evict func is trying to clear the cached entries 10649396SMatthew.Ahrens@Sun.COM * for this dataset in the ARC). 10659396SMatthew.Ahrens@Sun.COM */ 10669396SMatthew.Ahrens@Sun.COM txg_wait_synced(dd->dd_pool, 0); 10679396SMatthew.Ahrens@Sun.COM 10689396SMatthew.Ahrens@Sun.COM /* 10699396SMatthew.Ahrens@Sun.COM * If we managed to free all the objects in open 10709396SMatthew.Ahrens@Sun.COM * context, the user space accounting should be zero. 10719396SMatthew.Ahrens@Sun.COM */ 10729396SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_bp.blk_fill == 0 && 107310298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(os)) { 10749396SMatthew.Ahrens@Sun.COM uint64_t count; 10759396SMatthew.Ahrens@Sun.COM 10769396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 || 10779396SMatthew.Ahrens@Sun.COM count == 0); 10789396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 || 10799396SMatthew.Ahrens@Sun.COM count == 0); 10809396SMatthew.Ahrens@Sun.COM } 10819396SMatthew.Ahrens@Sun.COM 10822199Sahrens if (err != ESRCH) 10835367Sahrens goto out; 10842199Sahrens 10856975Smaybee rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 10866975Smaybee err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 10876975Smaybee rw_exit(&dd->dd_pool->dp_config_rwlock); 10886975Smaybee 10896975Smaybee if (err) 10906975Smaybee goto out; 10916975Smaybee 109210298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 10936689Smaybee /* 10946689Smaybee * We need to sync out all in-flight IO before we try 10956689Smaybee * to evict (the dataset evict func is trying to clear 10966689Smaybee * the cached entries for this dataset in the ARC). 10976689Smaybee */ 10986689Smaybee txg_wait_synced(dd->dd_pool, 0); 10995367Sahrens } 11005367Sahrens 11012199Sahrens /* 11022199Sahrens * Blow away the dsl_dir + head dataset. 11032199Sahrens */ 11046689Smaybee dsl_dataset_make_exclusive(ds, tag); 110510298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 110610298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 110710298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 11086975Smaybee } 110910242Schris.kirby@sun.com 111010242Schris.kirby@sun.com /* 111110242Schris.kirby@sun.com * If we're removing a clone, we might also need to remove its 111210242Schris.kirby@sun.com * origin. 111310242Schris.kirby@sun.com */ 111410242Schris.kirby@sun.com do { 111510242Schris.kirby@sun.com dsda.need_prep = B_FALSE; 111610242Schris.kirby@sun.com if (dsl_dir_is_clone(dd)) { 111710242Schris.kirby@sun.com err = dsl_dataset_origin_rm_prep(&dsda, tag); 111810242Schris.kirby@sun.com if (err) { 111910242Schris.kirby@sun.com dsl_dir_close(dd, FTAG); 112010242Schris.kirby@sun.com goto out; 112110242Schris.kirby@sun.com } 112210242Schris.kirby@sun.com } 112310242Schris.kirby@sun.com 112410242Schris.kirby@sun.com dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 112510242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 112610242Schris.kirby@sun.com dsl_dataset_destroy_sync, &dsda, tag, 0); 112710242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dir_destroy_check, 1128*11022STom.Erickson@Sun.COM dsl_dir_destroy_sync, &dummy_ds, FTAG, 0); 112910242Schris.kirby@sun.com err = dsl_sync_task_group_wait(dstg); 113010242Schris.kirby@sun.com dsl_sync_task_group_destroy(dstg); 113110242Schris.kirby@sun.com 113210242Schris.kirby@sun.com /* 113310242Schris.kirby@sun.com * We could be racing against 'zfs release' or 'zfs destroy -d' 113410242Schris.kirby@sun.com * on the origin snap, in which case we can get EBUSY if we 113510242Schris.kirby@sun.com * needed to destroy the origin snap but were not ready to 113610242Schris.kirby@sun.com * do so. 113710242Schris.kirby@sun.com */ 113810242Schris.kirby@sun.com if (dsda.need_prep) { 113910242Schris.kirby@sun.com ASSERT(err == EBUSY); 114010242Schris.kirby@sun.com ASSERT(dsl_dir_is_clone(dd)); 114110242Schris.kirby@sun.com ASSERT(dsda.rm_origin == NULL); 114210242Schris.kirby@sun.com } 114310242Schris.kirby@sun.com } while (dsda.need_prep); 114410242Schris.kirby@sun.com 114510242Schris.kirby@sun.com if (dsda.rm_origin != NULL) 114610242Schris.kirby@sun.com dsl_dataset_disown(dsda.rm_origin, tag); 114710242Schris.kirby@sun.com 11486689Smaybee /* if it is successful, dsl_dir_destroy_sync will close the dd */ 11495367Sahrens if (err) 11502199Sahrens dsl_dir_close(dd, FTAG); 11515367Sahrens out: 11526689Smaybee dsl_dataset_disown(ds, tag); 1153789Sahrens return (err); 1154789Sahrens } 1155789Sahrens 11563547Smaybee blkptr_t * 11573547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1158789Sahrens { 11593547Smaybee return (&ds->ds_phys->ds_bp); 1160789Sahrens } 1161789Sahrens 1162789Sahrens void 1163789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1164789Sahrens { 1165789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1166789Sahrens /* If it's the meta-objset, set dp_meta_rootbp */ 1167789Sahrens if (ds == NULL) { 1168789Sahrens tx->tx_pool->dp_meta_rootbp = *bp; 1169789Sahrens } else { 1170789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1171789Sahrens ds->ds_phys->ds_bp = *bp; 1172789Sahrens } 1173789Sahrens } 1174789Sahrens 1175789Sahrens spa_t * 1176789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 1177789Sahrens { 1178789Sahrens return (ds->ds_dir->dd_pool->dp_spa); 1179789Sahrens } 1180789Sahrens 1181789Sahrens void 1182789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1183789Sahrens { 1184789Sahrens dsl_pool_t *dp; 1185789Sahrens 1186789Sahrens if (ds == NULL) /* this is the meta-objset */ 1187789Sahrens return; 1188789Sahrens 118910298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 11902885Sahrens 11912885Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 11922885Sahrens panic("dirtying snapshot!"); 1193789Sahrens 1194789Sahrens dp = ds->ds_dir->dd_pool; 1195789Sahrens 1196789Sahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1197789Sahrens /* up the hold count until we can be written out */ 1198789Sahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 1199789Sahrens } 1200789Sahrens } 1201789Sahrens 12025378Sck153898 /* 12035378Sck153898 * The unique space in the head dataset can be calculated by subtracting 12045378Sck153898 * the space used in the most recent snapshot, that is still being used 12055378Sck153898 * in this file system, from the space currently in use. To figure out 12065378Sck153898 * the space in the most recent snapshot still in use, we need to take 12075378Sck153898 * the total space used in the snapshot and subtract out the space that 12085378Sck153898 * has been freed up since the snapshot was taken. 12095378Sck153898 */ 12105378Sck153898 static void 12115378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 12125378Sck153898 { 12135378Sck153898 uint64_t mrs_used; 12145378Sck153898 uint64_t dlused, dlcomp, dluncomp; 12155378Sck153898 12165378Sck153898 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 12175378Sck153898 12185378Sck153898 if (ds->ds_phys->ds_prev_snap_obj != 0) 12195378Sck153898 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 12205378Sck153898 else 12215378Sck153898 mrs_used = 0; 12225378Sck153898 12235378Sck153898 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 12245378Sck153898 &dluncomp)); 12255378Sck153898 12265378Sck153898 ASSERT3U(dlused, <=, mrs_used); 12275378Sck153898 ds->ds_phys->ds_unique_bytes = 12285378Sck153898 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 12295378Sck153898 12305378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && 12315378Sck153898 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 12325378Sck153898 SPA_VERSION_UNIQUE_ACCURATE) 12335378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 12345378Sck153898 } 12355378Sck153898 12365378Sck153898 static uint64_t 12375378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds) 12385378Sck153898 { 12395378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 12405378Sck153898 dsl_dataset_recalc_head_uniq(ds); 12415378Sck153898 12425378Sck153898 return (ds->ds_phys->ds_unique_bytes); 12435378Sck153898 } 12445378Sck153898 1245789Sahrens struct killarg { 12467390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 1247789Sahrens dmu_tx_t *tx; 1248789Sahrens }; 1249789Sahrens 12507390SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 1251789Sahrens static int 125210922SJeff.Bonwick@Sun.COM kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 125310922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1254789Sahrens { 1255789Sahrens struct killarg *ka = arg; 125610922SJeff.Bonwick@Sun.COM dmu_tx_t *tx = ka->tx; 12577837SMatthew.Ahrens@Sun.COM 12587837SMatthew.Ahrens@Sun.COM if (bp == NULL) 12597837SMatthew.Ahrens@Sun.COM return (0); 1260789Sahrens 126110922SJeff.Bonwick@Sun.COM if (zb->zb_level == ZB_ZIL_LEVEL) { 126210922SJeff.Bonwick@Sun.COM ASSERT(zilog != NULL); 12638746SMatthew.Ahrens@Sun.COM /* 12648746SMatthew.Ahrens@Sun.COM * It's a block in the intent log. It has no 12658746SMatthew.Ahrens@Sun.COM * accounting, so just free it. 12668746SMatthew.Ahrens@Sun.COM */ 126710922SJeff.Bonwick@Sun.COM dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp); 12688746SMatthew.Ahrens@Sun.COM } else { 126910922SJeff.Bonwick@Sun.COM ASSERT(zilog == NULL); 12708746SMatthew.Ahrens@Sun.COM ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg); 127110922SJeff.Bonwick@Sun.COM (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE); 12728746SMatthew.Ahrens@Sun.COM } 12737390SMatthew.Ahrens@Sun.COM 1274789Sahrens return (0); 1275789Sahrens } 1276789Sahrens 1277789Sahrens /* ARGSUSED */ 12782199Sahrens static int 12792199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 12801731Sbonwick { 12812199Sahrens dsl_dataset_t *ds = arg1; 12825367Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 12835367Sahrens uint64_t count; 12845367Sahrens int err; 12851731Sbonwick 12861731Sbonwick /* 12871731Sbonwick * Can't delete a head dataset if there are snapshots of it. 12881731Sbonwick * (Except if the only snapshots are from the branch we cloned 12891731Sbonwick * from.) 12901731Sbonwick */ 12911731Sbonwick if (ds->ds_prev != NULL && 12921731Sbonwick ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 129310816SVitezslav.Batrla@Sun.COM return (EBUSY); 12941731Sbonwick 12955367Sahrens /* 12965367Sahrens * This is really a dsl_dir thing, but check it here so that 12975367Sahrens * we'll be less likely to leave this dataset inconsistent & 12985367Sahrens * nearly destroyed. 12995367Sahrens */ 13005367Sahrens err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 13015367Sahrens if (err) 13025367Sahrens return (err); 13035367Sahrens if (count != 0) 13045367Sahrens return (EEXIST); 13055367Sahrens 13061731Sbonwick return (0); 13071731Sbonwick } 13081731Sbonwick 13092199Sahrens /* ARGSUSED */ 13102199Sahrens static void 13114543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1312789Sahrens { 13132199Sahrens dsl_dataset_t *ds = arg1; 13144543Smarks dsl_pool_t *dp = ds->ds_dir->dd_pool; 1315789Sahrens 13162199Sahrens /* Mark it as inconsistent on-disk, in case we crash */ 13172199Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13182199Sahrens ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 13194543Smarks 13204543Smarks spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 13214543Smarks cr, "dataset = %llu", ds->ds_object); 13222199Sahrens } 1323789Sahrens 132410242Schris.kirby@sun.com static int 132510242Schris.kirby@sun.com dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag, 132610242Schris.kirby@sun.com dmu_tx_t *tx) 132710242Schris.kirby@sun.com { 132810242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 132910242Schris.kirby@sun.com dsl_dataset_t *ds_prev = ds->ds_prev; 133010242Schris.kirby@sun.com 133110242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(ds_prev)) { 133210242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 133310242Schris.kirby@sun.com 133410242Schris.kirby@sun.com /* 133510242Schris.kirby@sun.com * If we're not prepared to remove the origin, don't remove 133610242Schris.kirby@sun.com * the clone either. 133710242Schris.kirby@sun.com */ 133810242Schris.kirby@sun.com if (dsda->rm_origin == NULL) { 133910242Schris.kirby@sun.com dsda->need_prep = B_TRUE; 134010242Schris.kirby@sun.com return (EBUSY); 134110242Schris.kirby@sun.com } 134210242Schris.kirby@sun.com 134310242Schris.kirby@sun.com ndsda.ds = ds_prev; 134410242Schris.kirby@sun.com ndsda.is_origin_rm = B_TRUE; 134510242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&ndsda, tag, tx)); 134610242Schris.kirby@sun.com } 134710242Schris.kirby@sun.com 134810242Schris.kirby@sun.com /* 134910242Schris.kirby@sun.com * If we're not going to remove the origin after all, 135010242Schris.kirby@sun.com * undo the open context setup. 135110242Schris.kirby@sun.com */ 135210242Schris.kirby@sun.com if (dsda->rm_origin != NULL) { 135310242Schris.kirby@sun.com dsl_dataset_disown(dsda->rm_origin, tag); 135410242Schris.kirby@sun.com dsda->rm_origin = NULL; 135510242Schris.kirby@sun.com } 135610242Schris.kirby@sun.com 135710242Schris.kirby@sun.com return (0); 135810242Schris.kirby@sun.com } 135910242Schris.kirby@sun.com 13602199Sahrens /* ARGSUSED */ 13615367Sahrens int 13622199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 13632199Sahrens { 136410242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 136510242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 1366789Sahrens 13676689Smaybee /* we have an owner hold, so noone else can destroy us */ 13686689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 13696689Smaybee 137010242Schris.kirby@sun.com /* 137110242Schris.kirby@sun.com * Only allow deferred destroy on pools that support it. 137210242Schris.kirby@sun.com * NOTE: deferred destroy is only supported on snapshots. 137310242Schris.kirby@sun.com */ 137410242Schris.kirby@sun.com if (dsda->defer) { 137510242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 137610242Schris.kirby@sun.com SPA_VERSION_USERREFS) 137710242Schris.kirby@sun.com return (ENOTSUP); 137810242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 137910242Schris.kirby@sun.com return (0); 138010242Schris.kirby@sun.com } 1381789Sahrens 1382789Sahrens /* 1383789Sahrens * Can't delete a head dataset if there are snapshots of it. 1384789Sahrens * (Except if the only snapshots are from the branch we cloned 1385789Sahrens * from.) 1386789Sahrens */ 1387789Sahrens if (ds->ds_prev != NULL && 13882199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 138910816SVitezslav.Batrla@Sun.COM return (EBUSY); 1390789Sahrens 1391789Sahrens /* 1392789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1393789Sahrens * them. Try again. 1394789Sahrens */ 13952199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1396789Sahrens return (EAGAIN); 13972199Sahrens 139810242Schris.kirby@sun.com if (dsl_dataset_is_snapshot(ds)) { 139910242Schris.kirby@sun.com /* 140010242Schris.kirby@sun.com * If this snapshot has an elevated user reference count, 140110242Schris.kirby@sun.com * we can't destroy it yet. 140210242Schris.kirby@sun.com */ 140310242Schris.kirby@sun.com if (ds->ds_userrefs > 0 && !dsda->releasing) 140410242Schris.kirby@sun.com return (EBUSY); 140510242Schris.kirby@sun.com 140610242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 140710242Schris.kirby@sun.com /* 140810242Schris.kirby@sun.com * Can't delete a branch point. However, if we're destroying 140910242Schris.kirby@sun.com * a clone and removing its origin due to it having a user 141010242Schris.kirby@sun.com * hold count of 0 and having been marked for deferred destroy, 141110242Schris.kirby@sun.com * it's OK for the origin to have a single clone. 141210242Schris.kirby@sun.com */ 141310242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children > 141410242Schris.kirby@sun.com (dsda->is_origin_rm ? 2 : 1)) { 141510242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 141610242Schris.kirby@sun.com return (EEXIST); 141710242Schris.kirby@sun.com } 141810242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 141910242Schris.kirby@sun.com } else if (dsl_dir_is_clone(ds->ds_dir)) { 142010242Schris.kirby@sun.com return (dsl_dataset_origin_check(dsda, arg2, tx)); 142110242Schris.kirby@sun.com } 142210242Schris.kirby@sun.com 14232199Sahrens /* XXX we should do some i/o error checking... */ 14242199Sahrens return (0); 14252199Sahrens } 14262199Sahrens 14276689Smaybee struct refsarg { 14286689Smaybee kmutex_t lock; 14296689Smaybee boolean_t gone; 14306689Smaybee kcondvar_t cv; 14316689Smaybee }; 14326689Smaybee 14336689Smaybee /* ARGSUSED */ 14346689Smaybee static void 14356689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 14366689Smaybee { 14376689Smaybee struct refsarg *arg = argv; 14386689Smaybee 14396689Smaybee mutex_enter(&arg->lock); 14406689Smaybee arg->gone = TRUE; 14416689Smaybee cv_signal(&arg->cv); 14426689Smaybee mutex_exit(&arg->lock); 14436689Smaybee } 14446689Smaybee 14456689Smaybee static void 14466689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 14476689Smaybee { 14486689Smaybee struct refsarg arg; 14496689Smaybee 14506689Smaybee mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 14516689Smaybee cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 14526689Smaybee arg.gone = FALSE; 14536689Smaybee (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 14546689Smaybee dsl_dataset_refs_gone); 14556689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 14566689Smaybee mutex_enter(&arg.lock); 14576689Smaybee while (!arg.gone) 14586689Smaybee cv_wait(&arg.cv, &arg.lock); 14596689Smaybee ASSERT(arg.gone); 14606689Smaybee mutex_exit(&arg.lock); 14616689Smaybee ds->ds_dbuf = NULL; 14626689Smaybee ds->ds_phys = NULL; 14636689Smaybee mutex_destroy(&arg.lock); 14646689Smaybee cv_destroy(&arg.cv); 14656689Smaybee } 14666689Smaybee 146710801SMatthew.Ahrens@Sun.COM static void 146810801SMatthew.Ahrens@Sun.COM remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx) 146910801SMatthew.Ahrens@Sun.COM { 147010801SMatthew.Ahrens@Sun.COM objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 147110801SMatthew.Ahrens@Sun.COM uint64_t count; 147210801SMatthew.Ahrens@Sun.COM int err; 147310801SMatthew.Ahrens@Sun.COM 147410801SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_phys->ds_num_children >= 2); 147510801SMatthew.Ahrens@Sun.COM err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 147610801SMatthew.Ahrens@Sun.COM /* 147710801SMatthew.Ahrens@Sun.COM * The err should not be ENOENT, but a bug in a previous version 147810801SMatthew.Ahrens@Sun.COM * of the code could cause upgrade_clones_cb() to not set 147910801SMatthew.Ahrens@Sun.COM * ds_next_snap_obj when it should, leading to a missing entry. 148010801SMatthew.Ahrens@Sun.COM * If we knew that the pool was created after 148110801SMatthew.Ahrens@Sun.COM * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 148210801SMatthew.Ahrens@Sun.COM * ENOENT. However, at least we can check that we don't have 148310801SMatthew.Ahrens@Sun.COM * too many entries in the next_clones_obj even after failing to 148410801SMatthew.Ahrens@Sun.COM * remove this one. 148510801SMatthew.Ahrens@Sun.COM */ 148610801SMatthew.Ahrens@Sun.COM if (err != ENOENT) { 148710801SMatthew.Ahrens@Sun.COM VERIFY3U(err, ==, 0); 148810801SMatthew.Ahrens@Sun.COM } 148910801SMatthew.Ahrens@Sun.COM ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj, 149010801SMatthew.Ahrens@Sun.COM &count)); 149110801SMatthew.Ahrens@Sun.COM ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 149210801SMatthew.Ahrens@Sun.COM } 149310801SMatthew.Ahrens@Sun.COM 14945367Sahrens void 14954543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 14962199Sahrens { 149710242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 149810242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 14992199Sahrens int err; 15002199Sahrens int after_branch_point = FALSE; 15012199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 15022199Sahrens objset_t *mos = dp->dp_meta_objset; 15032199Sahrens dsl_dataset_t *ds_prev = NULL; 15042199Sahrens uint64_t obj; 15052199Sahrens 15066689Smaybee ASSERT(ds->ds_owner); 150710242Schris.kirby@sun.com ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1); 15082199Sahrens ASSERT(ds->ds_prev == NULL || 15092199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 15102199Sahrens ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 15112199Sahrens 151210242Schris.kirby@sun.com if (dsda->defer) { 151310242Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 151410242Schris.kirby@sun.com if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) { 151510242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 151610242Schris.kirby@sun.com ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY; 151710242Schris.kirby@sun.com return; 151810242Schris.kirby@sun.com } 151910242Schris.kirby@sun.com } 152010242Schris.kirby@sun.com 15216689Smaybee /* signal any waiters that this dataset is going away */ 15226689Smaybee mutex_enter(&ds->ds_lock); 15236689Smaybee ds->ds_owner = dsl_reaper; 15246689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 15256689Smaybee mutex_exit(&ds->ds_lock); 15266689Smaybee 15275378Sck153898 /* Remove our reservation */ 15285378Sck153898 if (ds->ds_reserved != 0) { 1529*11022STom.Erickson@Sun.COM dsl_prop_setarg_t psa; 1530*11022STom.Erickson@Sun.COM uint64_t value = 0; 1531*11022STom.Erickson@Sun.COM 1532*11022STom.Erickson@Sun.COM dsl_prop_setarg_init_uint64(&psa, "refreservation", 1533*11022STom.Erickson@Sun.COM (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED), 1534*11022STom.Erickson@Sun.COM &value); 1535*11022STom.Erickson@Sun.COM psa.psa_effective_value = 0; /* predict default value */ 1536*11022STom.Erickson@Sun.COM 1537*11022STom.Erickson@Sun.COM dsl_dataset_set_reservation_sync(ds, &psa, cr, tx); 15385378Sck153898 ASSERT3U(ds->ds_reserved, ==, 0); 15395378Sck153898 } 15405378Sck153898 15412199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 15422199Sahrens 15437046Sahrens dsl_pool_ds_destroyed(ds, tx); 15447046Sahrens 15452199Sahrens obj = ds->ds_object; 1546789Sahrens 1547789Sahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 1548789Sahrens if (ds->ds_prev) { 1549789Sahrens ds_prev = ds->ds_prev; 1550789Sahrens } else { 15516689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15526689Smaybee ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1553789Sahrens } 1554789Sahrens after_branch_point = 1555789Sahrens (ds_prev->ds_phys->ds_next_snap_obj != obj); 1556789Sahrens 1557789Sahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1558789Sahrens if (after_branch_point && 15597046Sahrens ds_prev->ds_phys->ds_next_clones_obj != 0) { 156010801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds_prev, obj, tx); 15617046Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15627046Sahrens VERIFY(0 == zap_add_int(mos, 15637046Sahrens ds_prev->ds_phys->ds_next_clones_obj, 15647046Sahrens ds->ds_phys->ds_next_snap_obj, tx)); 15657046Sahrens } 15667046Sahrens } 15677046Sahrens if (after_branch_point && 1568789Sahrens ds->ds_phys->ds_next_snap_obj == 0) { 1569789Sahrens /* This clone is toast. */ 1570789Sahrens ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1571789Sahrens ds_prev->ds_phys->ds_num_children--; 157210242Schris.kirby@sun.com 157310242Schris.kirby@sun.com /* 157410242Schris.kirby@sun.com * If the clone's origin has no other clones, no 157510242Schris.kirby@sun.com * user holds, and has been marked for deferred 157610242Schris.kirby@sun.com * deletion, then we should have done the necessary 157710242Schris.kirby@sun.com * destroy setup for it. 157810242Schris.kirby@sun.com */ 157910242Schris.kirby@sun.com if (ds_prev->ds_phys->ds_num_children == 1 && 158010242Schris.kirby@sun.com ds_prev->ds_userrefs == 0 && 158110242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds_prev)) { 158210242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, !=, NULL); 158310242Schris.kirby@sun.com } else { 158410242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 158510242Schris.kirby@sun.com } 1586789Sahrens } else if (!after_branch_point) { 1587789Sahrens ds_prev->ds_phys->ds_next_snap_obj = 1588789Sahrens ds->ds_phys->ds_next_snap_obj; 1589789Sahrens } 1590789Sahrens } 1591789Sahrens 1592789Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15932199Sahrens blkptr_t bp; 1594789Sahrens dsl_dataset_t *ds_next; 1595789Sahrens uint64_t itor = 0; 15965378Sck153898 uint64_t old_unique; 15977390SMatthew.Ahrens@Sun.COM int64_t used = 0, compressed = 0, uncompressed = 0; 1598789Sahrens 15996689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16006689Smaybee ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1601789Sahrens ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1602789Sahrens 16035378Sck153898 old_unique = dsl_dataset_unique(ds_next); 16045378Sck153898 1605789Sahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1606789Sahrens ds_next->ds_phys->ds_prev_snap_obj = 1607789Sahrens ds->ds_phys->ds_prev_snap_obj; 1608789Sahrens ds_next->ds_phys->ds_prev_snap_txg = 1609789Sahrens ds->ds_phys->ds_prev_snap_txg; 1610789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1611789Sahrens ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1612789Sahrens 1613789Sahrens /* 1614789Sahrens * Transfer to our deadlist (which will become next's 1615789Sahrens * new deadlist) any entries from next's current 1616789Sahrens * deadlist which were born before prev, and free the 1617789Sahrens * other entries. 1618789Sahrens * 1619789Sahrens * XXX we're doing this long task with the config lock held 1620789Sahrens */ 16216689Smaybee while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1622789Sahrens if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 16231544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 16241544Seschrock &bp, tx)); 1625789Sahrens if (ds_prev && !after_branch_point && 1626789Sahrens bp.blk_birth > 1627789Sahrens ds_prev->ds_phys->ds_prev_snap_txg) { 1628789Sahrens ds_prev->ds_phys->ds_unique_bytes += 162910922SJeff.Bonwick@Sun.COM bp_get_dsize_sync(dp->dp_spa, &bp); 1630789Sahrens } 1631789Sahrens } else { 163210922SJeff.Bonwick@Sun.COM used += bp_get_dsize_sync(dp->dp_spa, &bp); 1633789Sahrens compressed += BP_GET_PSIZE(&bp); 1634789Sahrens uncompressed += BP_GET_UCSIZE(&bp); 163510922SJeff.Bonwick@Sun.COM dsl_free(dp, tx->tx_txg, &bp); 1636789Sahrens } 1637789Sahrens } 1638789Sahrens 16397390SMatthew.Ahrens@Sun.COM ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 16407390SMatthew.Ahrens@Sun.COM 16417390SMatthew.Ahrens@Sun.COM /* change snapused */ 16427390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 16437390SMatthew.Ahrens@Sun.COM -used, -compressed, -uncompressed, tx); 16447390SMatthew.Ahrens@Sun.COM 1645789Sahrens /* free next's deadlist */ 1646789Sahrens bplist_close(&ds_next->ds_deadlist); 1647789Sahrens bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1648789Sahrens 1649789Sahrens /* set next's deadlist to our deadlist */ 16506689Smaybee bplist_close(&ds->ds_deadlist); 1651789Sahrens ds_next->ds_phys->ds_deadlist_obj = 1652789Sahrens ds->ds_phys->ds_deadlist_obj; 16531544Seschrock VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 16541544Seschrock ds_next->ds_phys->ds_deadlist_obj)); 1655789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1656789Sahrens 1657789Sahrens if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1658789Sahrens /* 1659789Sahrens * Update next's unique to include blocks which 1660789Sahrens * were previously shared by only this snapshot 1661789Sahrens * and it. Those blocks will be born after the 1662789Sahrens * prev snap and before this snap, and will have 1663789Sahrens * died after the next snap and before the one 1664789Sahrens * after that (ie. be on the snap after next's 1665789Sahrens * deadlist). 1666789Sahrens * 1667789Sahrens * XXX we're doing this long task with the 1668789Sahrens * config lock held 1669789Sahrens */ 1670789Sahrens dsl_dataset_t *ds_after_next; 16717390SMatthew.Ahrens@Sun.COM uint64_t space; 1672789Sahrens 16736689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16746689Smaybee ds_next->ds_phys->ds_next_snap_obj, 16756689Smaybee FTAG, &ds_after_next)); 16767390SMatthew.Ahrens@Sun.COM 16777390SMatthew.Ahrens@Sun.COM VERIFY(0 == 16787390SMatthew.Ahrens@Sun.COM bplist_space_birthrange(&ds_after_next->ds_deadlist, 16797390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_prev_snap_txg, 16807390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_creation_txg, &space)); 16817390SMatthew.Ahrens@Sun.COM ds_next->ds_phys->ds_unique_bytes += space; 1682789Sahrens 16836689Smaybee dsl_dataset_rele(ds_after_next, FTAG); 1684789Sahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 1685789Sahrens } else { 1686789Sahrens ASSERT3P(ds_next->ds_prev, ==, ds); 16876689Smaybee dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 16886689Smaybee ds_next->ds_prev = NULL; 1689789Sahrens if (ds_prev) { 16906689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 16916689Smaybee ds->ds_phys->ds_prev_snap_obj, 16926689Smaybee ds_next, &ds_next->ds_prev)); 1693789Sahrens } 16945378Sck153898 16955378Sck153898 dsl_dataset_recalc_head_uniq(ds_next); 16965378Sck153898 16975378Sck153898 /* 16985378Sck153898 * Reduce the amount of our unconsmed refreservation 16995378Sck153898 * being charged to our parent by the amount of 17005378Sck153898 * new unique data we have gained. 17015378Sck153898 */ 17025378Sck153898 if (old_unique < ds_next->ds_reserved) { 17035378Sck153898 int64_t mrsdelta; 17045378Sck153898 uint64_t new_unique = 17055378Sck153898 ds_next->ds_phys->ds_unique_bytes; 17065378Sck153898 17075378Sck153898 ASSERT(old_unique <= new_unique); 17085378Sck153898 mrsdelta = MIN(new_unique - old_unique, 17095378Sck153898 ds_next->ds_reserved - old_unique); 17107390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, 17117390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 17125378Sck153898 } 1713789Sahrens } 17146689Smaybee dsl_dataset_rele(ds_next, FTAG); 1715789Sahrens } else { 1716789Sahrens /* 1717789Sahrens * There's no next snapshot, so this is a head dataset. 1718789Sahrens * Destroy the deadlist. Unless it's a clone, the 1719789Sahrens * deadlist should be empty. (If it's a clone, it's 1720789Sahrens * safe to ignore the deadlist contents.) 1721789Sahrens */ 1722789Sahrens struct killarg ka; 1723789Sahrens 1724789Sahrens ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1725789Sahrens bplist_close(&ds->ds_deadlist); 1726789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1727789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1728789Sahrens 1729789Sahrens /* 1730789Sahrens * Free everything that we point to (that's born after 1731789Sahrens * the previous snapshot, if we are a clone) 1732789Sahrens * 17337390SMatthew.Ahrens@Sun.COM * NB: this should be very quick, because we already 17347390SMatthew.Ahrens@Sun.COM * freed all the objects in open context. 1735789Sahrens */ 17367390SMatthew.Ahrens@Sun.COM ka.ds = ds; 1737789Sahrens ka.tx = tx; 17387837SMatthew.Ahrens@Sun.COM err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 17397837SMatthew.Ahrens@Sun.COM TRAVERSE_POST, kill_blkptr, &ka); 1740789Sahrens ASSERT3U(err, ==, 0); 17419390Schris.kirby@sun.com ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 17427390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_unique_bytes == 0); 174310342Schris.kirby@sun.com 174410342Schris.kirby@sun.com if (ds->ds_prev != NULL) { 174510342Schris.kirby@sun.com dsl_dataset_rele(ds->ds_prev, ds); 174610342Schris.kirby@sun.com ds->ds_prev = ds_prev = NULL; 174710342Schris.kirby@sun.com } 1748789Sahrens } 1749789Sahrens 17506689Smaybee if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 17516689Smaybee /* Erase the link in the dir */ 17526689Smaybee dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 17536689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 17546689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1755789Sahrens err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1756789Sahrens ASSERT(err == 0); 1757789Sahrens } else { 1758789Sahrens /* remove from snapshot namespace */ 1759789Sahrens dsl_dataset_t *ds_head; 17606689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 17616689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 17626689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 17632207Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 1764789Sahrens #ifdef ZFS_DEBUG 1765789Sahrens { 1766789Sahrens uint64_t val; 17676492Stimh 17686689Smaybee err = dsl_dataset_snap_lookup(ds_head, 17696492Stimh ds->ds_snapname, &val); 1770789Sahrens ASSERT3U(err, ==, 0); 1771789Sahrens ASSERT3U(val, ==, obj); 1772789Sahrens } 1773789Sahrens #endif 17746689Smaybee err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1775789Sahrens ASSERT(err == 0); 17766689Smaybee dsl_dataset_rele(ds_head, FTAG); 1777789Sahrens } 1778789Sahrens 1779789Sahrens if (ds_prev && ds->ds_prev != ds_prev) 17806689Smaybee dsl_dataset_rele(ds_prev, FTAG); 1781789Sahrens 17825094Slling spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 17834543Smarks spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 17844543Smarks cr, "dataset = %llu", ds->ds_object); 17854543Smarks 17867046Sahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 17877046Sahrens uint64_t count; 17887046Sahrens ASSERT(0 == zap_count(mos, 17897046Sahrens ds->ds_phys->ds_next_clones_obj, &count) && count == 0); 17907046Sahrens VERIFY(0 == dmu_object_free(mos, 17917046Sahrens ds->ds_phys->ds_next_clones_obj, tx)); 17927046Sahrens } 17937390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_props_obj != 0) 17947390SMatthew.Ahrens@Sun.COM VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx)); 179510242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) 179610242Schris.kirby@sun.com VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx)); 17976689Smaybee dsl_dir_close(ds->ds_dir, ds); 17986689Smaybee ds->ds_dir = NULL; 17996689Smaybee dsl_dataset_drain_refs(ds, tag); 18002199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 180110242Schris.kirby@sun.com 180210242Schris.kirby@sun.com if (dsda->rm_origin) { 180310242Schris.kirby@sun.com /* 180410242Schris.kirby@sun.com * Remove the origin of the clone we just destroyed. 180510242Schris.kirby@sun.com */ 180610242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 180710242Schris.kirby@sun.com 180810342Schris.kirby@sun.com ndsda.ds = dsda->rm_origin; 180910242Schris.kirby@sun.com dsl_dataset_destroy_sync(&ndsda, tag, cr, tx); 181010242Schris.kirby@sun.com } 18112199Sahrens } 18122199Sahrens 18135378Sck153898 static int 18145378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 18155378Sck153898 { 18165378Sck153898 uint64_t asize; 18175378Sck153898 18185378Sck153898 if (!dmu_tx_is_syncing(tx)) 18195378Sck153898 return (0); 18205378Sck153898 18215378Sck153898 /* 18225378Sck153898 * If there's an fs-only reservation, any blocks that might become 18235378Sck153898 * owned by the snapshot dataset must be accommodated by space 18245378Sck153898 * outside of the reservation. 18255378Sck153898 */ 18265378Sck153898 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 18275378Sck153898 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 18285378Sck153898 return (ENOSPC); 18295378Sck153898 18305378Sck153898 /* 18315378Sck153898 * Propogate any reserved space for this snapshot to other 18325378Sck153898 * snapshot checks in this sync group. 18335378Sck153898 */ 18345378Sck153898 if (asize > 0) 18355378Sck153898 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 18365378Sck153898 18375378Sck153898 return (0); 18385378Sck153898 } 18395378Sck153898 18402199Sahrens /* ARGSUSED */ 18412199Sahrens int 18422199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 18432199Sahrens { 18445367Sahrens dsl_dataset_t *ds = arg1; 18452199Sahrens const char *snapname = arg2; 18462199Sahrens int err; 18472199Sahrens uint64_t value; 1848789Sahrens 1849789Sahrens /* 18502199Sahrens * We don't allow multiple snapshots of the same txg. If there 18512199Sahrens * is already one, try again. 18522199Sahrens */ 18532199Sahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 18542199Sahrens return (EAGAIN); 18552199Sahrens 18562199Sahrens /* 18572199Sahrens * Check for conflicting name snapshot name. 1858789Sahrens */ 18596689Smaybee err = dsl_dataset_snap_lookup(ds, snapname, &value); 18602199Sahrens if (err == 0) 18612199Sahrens return (EEXIST); 18622199Sahrens if (err != ENOENT) 18632199Sahrens return (err); 1864789Sahrens 18653978Smmusante /* 18663978Smmusante * Check that the dataset's name is not too long. Name consists 18673978Smmusante * of the dataset's length + 1 for the @-sign + snapshot name's length 18683978Smmusante */ 18693978Smmusante if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 18703978Smmusante return (ENAMETOOLONG); 18713978Smmusante 18725378Sck153898 err = dsl_dataset_snapshot_reserve_space(ds, tx); 18735378Sck153898 if (err) 18745378Sck153898 return (err); 18755378Sck153898 18762199Sahrens ds->ds_trysnap_txg = tx->tx_txg; 1877789Sahrens return (0); 1878789Sahrens } 1879789Sahrens 18802199Sahrens void 18814543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1882789Sahrens { 18835367Sahrens dsl_dataset_t *ds = arg1; 18842199Sahrens const char *snapname = arg2; 18852199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1886789Sahrens dmu_buf_t *dbuf; 1887789Sahrens dsl_dataset_phys_t *dsphys; 18887046Sahrens uint64_t dsobj, crtxg; 1889789Sahrens objset_t *mos = dp->dp_meta_objset; 1890789Sahrens int err; 1891789Sahrens 18922199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1893789Sahrens 18947046Sahrens /* 18957046Sahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 18967046Sahrens */ 18977046Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 18987046Sahrens crtxg = 1; 18997046Sahrens else 19007046Sahrens crtxg = tx->tx_txg; 19017046Sahrens 1902928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1903928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 19041544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1905789Sahrens dmu_buf_will_dirty(dbuf, tx); 1906789Sahrens dsphys = dbuf->db_data; 19076689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 19082199Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1909789Sahrens dsphys->ds_fsid_guid = unique_create(); 1910789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1911789Sahrens sizeof (dsphys->ds_guid)); 1912789Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1913789Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1914789Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1915789Sahrens dsphys->ds_num_children = 1; 1916789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 19177046Sahrens dsphys->ds_creation_txg = crtxg; 1918789Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1919789Sahrens dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1920789Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1921789Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 19222082Seschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1923789Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 19241544Seschrock dmu_buf_rele(dbuf, FTAG); 1925789Sahrens 19262199Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 19272199Sahrens if (ds->ds_prev) { 19287046Sahrens uint64_t next_clones_obj = 19297046Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 19302199Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1931789Sahrens ds->ds_object || 19322199Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 19332199Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 19342199Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1935789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 19362199Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 19372199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 19387046Sahrens } else if (next_clones_obj != 0) { 193910801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds->ds_prev, 194010801SMatthew.Ahrens@Sun.COM dsphys->ds_next_snap_obj, tx); 19417046Sahrens VERIFY3U(0, ==, zap_add_int(mos, 19427046Sahrens next_clones_obj, dsobj, tx)); 1943789Sahrens } 1944789Sahrens } 1945789Sahrens 19465378Sck153898 /* 19475378Sck153898 * If we have a reference-reservation on this dataset, we will 19485378Sck153898 * need to increase the amount of refreservation being charged 19495378Sck153898 * since our unique space is going to zero. 19505378Sck153898 */ 19515378Sck153898 if (ds->ds_reserved) { 19525378Sck153898 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 19537390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 19547390SMatthew.Ahrens@Sun.COM add, 0, 0, tx); 19555378Sck153898 } 19565378Sck153898 1957789Sahrens bplist_close(&ds->ds_deadlist); 1958789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 19595712Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1960789Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 19617046Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1962789Sahrens ds->ds_phys->ds_unique_bytes = 0; 19635378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 19645378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1965789Sahrens ds->ds_phys->ds_deadlist_obj = 1966789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 19671544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 19681544Seschrock ds->ds_phys->ds_deadlist_obj)); 1969789Sahrens 1970789Sahrens dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1971789Sahrens err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1972789Sahrens snapname, 8, 1, &dsobj, tx); 1973789Sahrens ASSERT(err == 0); 1974789Sahrens 1975789Sahrens if (ds->ds_prev) 19766689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 19776689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 19786689Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 19794543Smarks 19807046Sahrens dsl_pool_ds_snapshotted(ds, tx); 19817046Sahrens 198210373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(ds->ds_dir); 198310373Schris.kirby@sun.com 19844543Smarks spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 19854603Sahrens "dataset = %llu", dsobj); 1986789Sahrens } 1987789Sahrens 1988789Sahrens void 19893547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1990789Sahrens { 1991789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 199210298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 1993789Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 1994789Sahrens 19954787Sahrens /* 19964787Sahrens * in case we had to change ds_fsid_guid when we opened it, 19974787Sahrens * sync it out now. 19984787Sahrens */ 19994787Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 20004787Sahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 20014787Sahrens 2002789Sahrens dsl_dir_dirty(ds->ds_dir, tx); 200310298SMatthew.Ahrens@Sun.COM dmu_objset_sync(ds->ds_objset, zio, tx); 2004789Sahrens } 2005789Sahrens 2006789Sahrens void 20072885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 2008789Sahrens { 20095378Sck153898 uint64_t refd, avail, uobjs, aobjs; 20105378Sck153898 20112885Sahrens dsl_dir_stats(ds->ds_dir, nv); 2012789Sahrens 20135378Sck153898 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 20145378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 20155378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 20165378Sck153898 20172885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 20182885Sahrens ds->ds_phys->ds_creation_time); 20192885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 20202885Sahrens ds->ds_phys->ds_creation_txg); 20215378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 20225378Sck153898 ds->ds_quota); 20235378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 20245378Sck153898 ds->ds_reserved); 20256643Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 20266643Seschrock ds->ds_phys->ds_guid); 202710575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 202810575SEric.Schrock@Sun.COM dsl_dataset_unique(ds)); 202910575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 203010575SEric.Schrock@Sun.COM ds->ds_object); 2031*11022STom.Erickson@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 2032*11022STom.Erickson@Sun.COM ds->ds_userrefs); 203310242Schris.kirby@sun.com dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 203410242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 2035789Sahrens 2036789Sahrens if (ds->ds_phys->ds_next_snap_obj) { 2037789Sahrens /* 2038789Sahrens * This is a snapshot; override the dd's space used with 20392885Sahrens * our unique space and compression ratio. 2040789Sahrens */ 20412885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 20422885Sahrens ds->ds_phys->ds_unique_bytes); 20432885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 20442885Sahrens ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 20452885Sahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 20462885Sahrens ds->ds_phys->ds_compressed_bytes)); 2047789Sahrens } 2048789Sahrens } 2049789Sahrens 20502885Sahrens void 20512885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2052789Sahrens { 20532885Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 20542885Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 20555367Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 20562885Sahrens if (ds->ds_phys->ds_next_snap_obj) { 20572885Sahrens stat->dds_is_snapshot = B_TRUE; 20582885Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 20598228SEric.Taylor@Sun.COM } else { 20608228SEric.Taylor@Sun.COM stat->dds_is_snapshot = B_FALSE; 20618228SEric.Taylor@Sun.COM stat->dds_num_clones = 0; 20622885Sahrens } 20632885Sahrens 20642885Sahrens /* clone origin is really a dsl_dir thing... */ 20655446Sahrens rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 20667046Sahrens if (dsl_dir_is_clone(ds->ds_dir)) { 20672885Sahrens dsl_dataset_t *ods; 20682885Sahrens 20696689Smaybee VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 20706689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 20715367Sahrens dsl_dataset_name(ods, stat->dds_origin); 20726689Smaybee dsl_dataset_drop_ref(ods, FTAG); 20738228SEric.Taylor@Sun.COM } else { 20748228SEric.Taylor@Sun.COM stat->dds_origin[0] = '\0'; 20752885Sahrens } 20765446Sahrens rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 20772885Sahrens } 20782885Sahrens 20792885Sahrens uint64_t 20802885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 20812885Sahrens { 20824787Sahrens return (ds->ds_fsid_guid); 20832885Sahrens } 20842885Sahrens 20852885Sahrens void 20862885Sahrens dsl_dataset_space(dsl_dataset_t *ds, 20872885Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 20882885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 20892885Sahrens { 20902885Sahrens *refdbytesp = ds->ds_phys->ds_used_bytes; 20912885Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 20925378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 20935378Sck153898 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 20945378Sck153898 if (ds->ds_quota != 0) { 20955378Sck153898 /* 20965378Sck153898 * Adjust available bytes according to refquota 20975378Sck153898 */ 20985378Sck153898 if (*refdbytesp < ds->ds_quota) 20995378Sck153898 *availbytesp = MIN(*availbytesp, 21005378Sck153898 ds->ds_quota - *refdbytesp); 21015378Sck153898 else 21025378Sck153898 *availbytesp = 0; 21035378Sck153898 } 21042885Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 21052885Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2106789Sahrens } 2107789Sahrens 21085326Sek110237 boolean_t 21095326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 21105326Sek110237 { 21115326Sek110237 dsl_pool_t *dp = ds->ds_dir->dd_pool; 21125326Sek110237 21135326Sek110237 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 21145326Sek110237 dsl_pool_sync_context(dp)); 21155326Sek110237 if (ds->ds_prev == NULL) 21165326Sek110237 return (B_FALSE); 21175326Sek110237 if (ds->ds_phys->ds_bp.blk_birth > 21185326Sek110237 ds->ds_prev->ds_phys->ds_creation_txg) 21195326Sek110237 return (B_TRUE); 21205326Sek110237 return (B_FALSE); 21215326Sek110237 } 21225326Sek110237 21232199Sahrens /* ARGSUSED */ 2124789Sahrens static int 21252199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 2126789Sahrens { 21272199Sahrens dsl_dataset_t *ds = arg1; 21282199Sahrens char *newsnapname = arg2; 21292199Sahrens dsl_dir_t *dd = ds->ds_dir; 21302199Sahrens dsl_dataset_t *hds; 21312199Sahrens uint64_t val; 2132789Sahrens int err; 2133789Sahrens 21346689Smaybee err = dsl_dataset_hold_obj(dd->dd_pool, 21356689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2136789Sahrens if (err) 2137789Sahrens return (err); 2138789Sahrens 21392199Sahrens /* new name better not be in use */ 21406689Smaybee err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 21416689Smaybee dsl_dataset_rele(hds, FTAG); 2142789Sahrens 21432199Sahrens if (err == 0) 21442199Sahrens err = EEXIST; 21452199Sahrens else if (err == ENOENT) 21462199Sahrens err = 0; 21474007Smmusante 21484007Smmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 21494007Smmusante if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 21504007Smmusante err = ENAMETOOLONG; 21514007Smmusante 21522199Sahrens return (err); 21532199Sahrens } 2154789Sahrens 21552199Sahrens static void 21564543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 21574543Smarks cred_t *cr, dmu_tx_t *tx) 21582199Sahrens { 21592199Sahrens dsl_dataset_t *ds = arg1; 21604543Smarks const char *newsnapname = arg2; 21612199Sahrens dsl_dir_t *dd = ds->ds_dir; 21622199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 21632199Sahrens dsl_dataset_t *hds; 21642199Sahrens int err; 2165789Sahrens 21662199Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2167789Sahrens 21686689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 21696689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2170789Sahrens 21712199Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 21726689Smaybee err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2173789Sahrens ASSERT3U(err, ==, 0); 21742199Sahrens mutex_enter(&ds->ds_lock); 21752199Sahrens (void) strcpy(ds->ds_snapname, newsnapname); 21762199Sahrens mutex_exit(&ds->ds_lock); 21772199Sahrens err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 21782199Sahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2179789Sahrens ASSERT3U(err, ==, 0); 2180789Sahrens 21814543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 21824543Smarks cr, "dataset = %llu", ds->ds_object); 21836689Smaybee dsl_dataset_rele(hds, FTAG); 2184789Sahrens } 2185789Sahrens 21865326Sek110237 struct renamesnaparg { 21874007Smmusante dsl_sync_task_group_t *dstg; 21884007Smmusante char failed[MAXPATHLEN]; 21894007Smmusante char *oldsnap; 21904007Smmusante char *newsnap; 21914007Smmusante }; 21924007Smmusante 21934007Smmusante static int 21944007Smmusante dsl_snapshot_rename_one(char *name, void *arg) 21954007Smmusante { 21965326Sek110237 struct renamesnaparg *ra = arg; 21974007Smmusante dsl_dataset_t *ds = NULL; 21984007Smmusante char *cp; 21994007Smmusante int err; 22004007Smmusante 22014007Smmusante cp = name + strlen(name); 22024007Smmusante *cp = '@'; 22034007Smmusante (void) strcpy(cp + 1, ra->oldsnap); 22044543Smarks 22054543Smarks /* 22064543Smarks * For recursive snapshot renames the parent won't be changing 22074543Smarks * so we just pass name for both the to/from argument. 22084543Smarks */ 22097312SMatthew.Ahrens@Sun.COM err = zfs_secpolicy_rename_perms(name, name, CRED()); 22107312SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 22117312SMatthew.Ahrens@Sun.COM return (0); 22127312SMatthew.Ahrens@Sun.COM } else if (err) { 22134543Smarks (void) strcpy(ra->failed, name); 22144543Smarks return (err); 22154543Smarks } 22164543Smarks 22176689Smaybee #ifdef _KERNEL 22186689Smaybee /* 22196689Smaybee * For all filesystems undergoing rename, we'll need to unmount it. 22206689Smaybee */ 22216689Smaybee (void) zfs_unmount_snap(name, NULL); 22226689Smaybee #endif 22236689Smaybee err = dsl_dataset_hold(name, ra->dstg, &ds); 22246689Smaybee *cp = '\0'; 22254007Smmusante if (err == ENOENT) { 22264007Smmusante return (0); 22276689Smaybee } else if (err) { 22284007Smmusante (void) strcpy(ra->failed, name); 22294007Smmusante return (err); 22304007Smmusante } 22314007Smmusante 22324007Smmusante dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 22334007Smmusante dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 22344007Smmusante 22354007Smmusante return (0); 22364007Smmusante } 22374007Smmusante 22384007Smmusante static int 22394007Smmusante dsl_recursive_rename(char *oldname, const char *newname) 22404007Smmusante { 22414007Smmusante int err; 22425326Sek110237 struct renamesnaparg *ra; 22434007Smmusante dsl_sync_task_t *dst; 22444007Smmusante spa_t *spa; 22454007Smmusante char *cp, *fsname = spa_strdup(oldname); 22464007Smmusante int len = strlen(oldname); 22474007Smmusante 22484007Smmusante /* truncate the snapshot name to get the fsname */ 22494007Smmusante cp = strchr(fsname, '@'); 22504007Smmusante *cp = '\0'; 22514007Smmusante 22524603Sahrens err = spa_open(fsname, &spa, FTAG); 22534007Smmusante if (err) { 22544007Smmusante kmem_free(fsname, len + 1); 22554007Smmusante return (err); 22564007Smmusante } 22575326Sek110237 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 22584007Smmusante ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 22594007Smmusante 22604007Smmusante ra->oldsnap = strchr(oldname, '@') + 1; 22614007Smmusante ra->newsnap = strchr(newname, '@') + 1; 22624007Smmusante *ra->failed = '\0'; 22634007Smmusante 22644007Smmusante err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 22654007Smmusante DS_FIND_CHILDREN); 22664007Smmusante kmem_free(fsname, len + 1); 22674007Smmusante 22684007Smmusante if (err == 0) { 22694007Smmusante err = dsl_sync_task_group_wait(ra->dstg); 22704007Smmusante } 22714007Smmusante 22724007Smmusante for (dst = list_head(&ra->dstg->dstg_tasks); dst; 22734007Smmusante dst = list_next(&ra->dstg->dstg_tasks, dst)) { 22744007Smmusante dsl_dataset_t *ds = dst->dst_arg1; 22754007Smmusante if (dst->dst_err) { 22764007Smmusante dsl_dir_name(ds->ds_dir, ra->failed); 22774009Smmusante (void) strcat(ra->failed, "@"); 22784009Smmusante (void) strcat(ra->failed, ra->newsnap); 22794007Smmusante } 22806689Smaybee dsl_dataset_rele(ds, ra->dstg); 22814007Smmusante } 22824007Smmusante 22834543Smarks if (err) 22844543Smarks (void) strcpy(oldname, ra->failed); 22854007Smmusante 22864007Smmusante dsl_sync_task_group_destroy(ra->dstg); 22875326Sek110237 kmem_free(ra, sizeof (struct renamesnaparg)); 22884007Smmusante spa_close(spa, FTAG); 22894007Smmusante return (err); 22904007Smmusante } 22914007Smmusante 22924569Smmusante static int 22934569Smmusante dsl_valid_rename(char *oldname, void *arg) 22944569Smmusante { 22954569Smmusante int delta = *(int *)arg; 22964569Smmusante 22974569Smmusante if (strlen(oldname) + delta >= MAXNAMELEN) 22984569Smmusante return (ENAMETOOLONG); 22994569Smmusante 23004569Smmusante return (0); 23014569Smmusante } 23024569Smmusante 2303789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename 2304789Sahrens int 23056689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2306789Sahrens { 2307789Sahrens dsl_dir_t *dd; 23082199Sahrens dsl_dataset_t *ds; 2309789Sahrens const char *tail; 2310789Sahrens int err; 2311789Sahrens 23122199Sahrens err = dsl_dir_open(oldname, FTAG, &dd, &tail); 23131544Seschrock if (err) 23141544Seschrock return (err); 23158517SEric.Taylor@Sun.COM /* 23168517SEric.Taylor@Sun.COM * If there are more than 2 references there may be holds 23178517SEric.Taylor@Sun.COM * hanging around that haven't been cleared out yet. 23188517SEric.Taylor@Sun.COM */ 23198517SEric.Taylor@Sun.COM if (dmu_buf_refcount(dd->dd_dbuf) > 2) 23208517SEric.Taylor@Sun.COM txg_wait_synced(dd->dd_pool, 0); 2321789Sahrens if (tail == NULL) { 23224569Smmusante int delta = strlen(newname) - strlen(oldname); 23234569Smmusante 23247046Sahrens /* if we're growing, validate child name lengths */ 23254569Smmusante if (delta > 0) 23264569Smmusante err = dmu_objset_find(oldname, dsl_valid_rename, 23274569Smmusante &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 23284569Smmusante 23294569Smmusante if (!err) 23304569Smmusante err = dsl_dir_rename(dd, newname); 2331789Sahrens dsl_dir_close(dd, FTAG); 2332789Sahrens return (err); 2333789Sahrens } 2334789Sahrens if (tail[0] != '@') { 233510588SEric.Taylor@Sun.COM /* the name ended in a nonexistent component */ 2336789Sahrens dsl_dir_close(dd, FTAG); 2337789Sahrens return (ENOENT); 2338789Sahrens } 2339789Sahrens 23402199Sahrens dsl_dir_close(dd, FTAG); 23412199Sahrens 23422199Sahrens /* new name must be snapshot in same filesystem */ 23432199Sahrens tail = strchr(newname, '@'); 23442199Sahrens if (tail == NULL) 23452199Sahrens return (EINVAL); 23462199Sahrens tail++; 23472199Sahrens if (strncmp(oldname, newname, tail - newname) != 0) 23482199Sahrens return (EXDEV); 2349789Sahrens 23504007Smmusante if (recursive) { 23514007Smmusante err = dsl_recursive_rename(oldname, newname); 23524007Smmusante } else { 23536689Smaybee err = dsl_dataset_hold(oldname, FTAG, &ds); 23544007Smmusante if (err) 23554007Smmusante return (err); 23562199Sahrens 23574007Smmusante err = dsl_sync_task_do(ds->ds_dir->dd_pool, 23584007Smmusante dsl_dataset_snapshot_rename_check, 23594007Smmusante dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 23602199Sahrens 23616689Smaybee dsl_dataset_rele(ds, FTAG); 23624007Smmusante } 23632199Sahrens 2364789Sahrens return (err); 2365789Sahrens } 23662082Seschrock 23677046Sahrens struct promotenode { 23686689Smaybee list_node_t link; 23696689Smaybee dsl_dataset_t *ds; 23706689Smaybee }; 23716689Smaybee 23722199Sahrens struct promotearg { 23737390SMatthew.Ahrens@Sun.COM list_t shared_snaps, origin_snaps, clone_snaps; 23747390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_origin, *origin_head; 23757390SMatthew.Ahrens@Sun.COM uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 237610588SEric.Taylor@Sun.COM char *err_ds; 23772199Sahrens }; 23782199Sahrens 23797390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 23807390SMatthew.Ahrens@Sun.COM 23814543Smarks /* ARGSUSED */ 23822082Seschrock static int 23832199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 23842082Seschrock { 23852199Sahrens dsl_dataset_t *hds = arg1; 23862199Sahrens struct promotearg *pa = arg2; 23877390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 23886689Smaybee dsl_dataset_t *origin_ds = snap->ds; 23896689Smaybee int err; 23902199Sahrens 23917046Sahrens /* Check that it is a real clone */ 23927046Sahrens if (!dsl_dir_is_clone(hds->ds_dir)) 23932082Seschrock return (EINVAL); 23942082Seschrock 23952199Sahrens /* Since this is so expensive, don't do the preliminary check */ 23962199Sahrens if (!dmu_tx_is_syncing(tx)) 23972199Sahrens return (0); 23982199Sahrens 23996689Smaybee if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 24006689Smaybee return (EXDEV); 24012082Seschrock 24025367Sahrens /* compute origin's new unique space */ 24037390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 24047390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 24057390SMatthew.Ahrens@Sun.COM err = bplist_space_birthrange(&snap->ds->ds_deadlist, 24067390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique); 24077390SMatthew.Ahrens@Sun.COM if (err) 24086689Smaybee return (err); 24096689Smaybee 24106689Smaybee /* 24116689Smaybee * Walk the snapshots that we are moving 24126689Smaybee * 24137390SMatthew.Ahrens@Sun.COM * Compute space to transfer. Consider the incremental changes 24147390SMatthew.Ahrens@Sun.COM * to used for each snapshot: 24157390SMatthew.Ahrens@Sun.COM * (my used) = (prev's used) + (blocks born) - (blocks killed) 24167390SMatthew.Ahrens@Sun.COM * So each snapshot gave birth to: 24177390SMatthew.Ahrens@Sun.COM * (blocks born) = (my used) - (prev's used) + (blocks killed) 24186689Smaybee * So a sequence would look like: 24197390SMatthew.Ahrens@Sun.COM * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 24206689Smaybee * Which simplifies to: 24217390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + k1 + k0 24226689Smaybee * Note however, if we stop before we reach the ORIGIN we get: 24237390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + kM - uM-1 24246689Smaybee */ 24256689Smaybee pa->used = origin_ds->ds_phys->ds_used_bytes; 24266689Smaybee pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 24276689Smaybee pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 24287390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 24297390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 24302082Seschrock uint64_t val, dlused, dlcomp, dluncomp; 24316689Smaybee dsl_dataset_t *ds = snap->ds; 24322082Seschrock 24332082Seschrock /* Check that the snapshot name does not conflict */ 24347390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 24356689Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 243610588SEric.Taylor@Sun.COM if (err == 0) { 243710588SEric.Taylor@Sun.COM err = EEXIST; 243810588SEric.Taylor@Sun.COM goto out; 243910588SEric.Taylor@Sun.COM } 24406689Smaybee if (err != ENOENT) 244110588SEric.Taylor@Sun.COM goto out; 24426689Smaybee 24436689Smaybee /* The very first snapshot does not have a deadlist */ 24447390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_obj == 0) 24457390SMatthew.Ahrens@Sun.COM continue; 24467390SMatthew.Ahrens@Sun.COM 24477390SMatthew.Ahrens@Sun.COM if (err = bplist_space(&ds->ds_deadlist, 24487390SMatthew.Ahrens@Sun.COM &dlused, &dlcomp, &dluncomp)) 244910588SEric.Taylor@Sun.COM goto out; 24507390SMatthew.Ahrens@Sun.COM pa->used += dlused; 24517390SMatthew.Ahrens@Sun.COM pa->comp += dlcomp; 24527390SMatthew.Ahrens@Sun.COM pa->uncomp += dluncomp; 24537390SMatthew.Ahrens@Sun.COM } 24546689Smaybee 24556689Smaybee /* 24566689Smaybee * If we are a clone of a clone then we never reached ORIGIN, 24576689Smaybee * so we need to subtract out the clone origin's used space. 24586689Smaybee */ 24597390SMatthew.Ahrens@Sun.COM if (pa->origin_origin) { 24607390SMatthew.Ahrens@Sun.COM pa->used -= pa->origin_origin->ds_phys->ds_used_bytes; 24617390SMatthew.Ahrens@Sun.COM pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes; 24627390SMatthew.Ahrens@Sun.COM pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes; 24632082Seschrock } 24642082Seschrock 24652082Seschrock /* Check that there is enough space here */ 24667390SMatthew.Ahrens@Sun.COM err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 24677390SMatthew.Ahrens@Sun.COM pa->used); 24687390SMatthew.Ahrens@Sun.COM if (err) 24697390SMatthew.Ahrens@Sun.COM return (err); 24707390SMatthew.Ahrens@Sun.COM 24717390SMatthew.Ahrens@Sun.COM /* 24727390SMatthew.Ahrens@Sun.COM * Compute the amounts of space that will be used by snapshots 24737390SMatthew.Ahrens@Sun.COM * after the promotion (for both origin and clone). For each, 24747390SMatthew.Ahrens@Sun.COM * it is the amount of space that will be on all of their 24757390SMatthew.Ahrens@Sun.COM * deadlists (that was not born before their new origin). 24767390SMatthew.Ahrens@Sun.COM */ 24777390SMatthew.Ahrens@Sun.COM if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 24787390SMatthew.Ahrens@Sun.COM uint64_t space; 24797390SMatthew.Ahrens@Sun.COM 24807390SMatthew.Ahrens@Sun.COM /* 24817390SMatthew.Ahrens@Sun.COM * Note, typically this will not be a clone of a clone, 24827390SMatthew.Ahrens@Sun.COM * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so 24837390SMatthew.Ahrens@Sun.COM * these snaplist_space() -> bplist_space_birthrange() 24847390SMatthew.Ahrens@Sun.COM * calls will be fast because they do not have to 24857390SMatthew.Ahrens@Sun.COM * iterate over all bps. 24867390SMatthew.Ahrens@Sun.COM */ 24877390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 24887390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->shared_snaps, 24897390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &pa->cloneusedsnap); 24907390SMatthew.Ahrens@Sun.COM if (err) 24917390SMatthew.Ahrens@Sun.COM return (err); 24927390SMatthew.Ahrens@Sun.COM 24937390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->clone_snaps, 24947390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &space); 24957390SMatthew.Ahrens@Sun.COM if (err) 24967390SMatthew.Ahrens@Sun.COM return (err); 24977390SMatthew.Ahrens@Sun.COM pa->cloneusedsnap += space; 24986689Smaybee } 24997390SMatthew.Ahrens@Sun.COM if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 25007390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->origin_snaps, 25017390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap); 25027390SMatthew.Ahrens@Sun.COM if (err) 25037390SMatthew.Ahrens@Sun.COM return (err); 25047390SMatthew.Ahrens@Sun.COM } 25057390SMatthew.Ahrens@Sun.COM 25067390SMatthew.Ahrens@Sun.COM return (0); 250710588SEric.Taylor@Sun.COM out: 250810588SEric.Taylor@Sun.COM pa->err_ds = snap->ds->ds_snapname; 250910588SEric.Taylor@Sun.COM return (err); 25102199Sahrens } 25112082Seschrock 25122199Sahrens static void 25134543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 25142199Sahrens { 25152199Sahrens dsl_dataset_t *hds = arg1; 25162199Sahrens struct promotearg *pa = arg2; 25177390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 25186689Smaybee dsl_dataset_t *origin_ds = snap->ds; 25197390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_head; 25202199Sahrens dsl_dir_t *dd = hds->ds_dir; 25212199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 25225367Sahrens dsl_dir_t *odd = NULL; 25237046Sahrens uint64_t oldnext_obj; 25247390SMatthew.Ahrens@Sun.COM int64_t delta; 25252199Sahrens 25262199Sahrens ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 25272199Sahrens 25287390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 25297390SMatthew.Ahrens@Sun.COM origin_head = snap->ds; 25307390SMatthew.Ahrens@Sun.COM 25312417Sahrens /* 25325367Sahrens * We need to explicitly open odd, since origin_ds's dd will be 25332417Sahrens * changing. 25342417Sahrens */ 25355367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 25365367Sahrens NULL, FTAG, &odd)); 25372082Seschrock 25386689Smaybee /* change origin's next snap */ 25396689Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 25407046Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 25417390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 25427390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 25437390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 25446689Smaybee 25457046Sahrens /* change the origin's next clone */ 25467046Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 254710801SMatthew.Ahrens@Sun.COM remove_from_next_clones(origin_ds, snap->ds->ds_object, tx); 25487046Sahrens VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 25497046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 25507046Sahrens oldnext_obj, tx)); 25517046Sahrens } 25527046Sahrens 25536689Smaybee /* change origin */ 25546689Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 25556689Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 25566689Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 25577390SMatthew.Ahrens@Sun.COM hds->ds_origin_txg = origin_head->ds_origin_txg; 25586689Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 25596689Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 25607390SMatthew.Ahrens@Sun.COM origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg; 25616689Smaybee 25622082Seschrock /* move snapshots to this dir */ 25637390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 25647390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 25656689Smaybee dsl_dataset_t *ds = snap->ds; 25662082Seschrock 25677237Sek110237 /* unregister props as dsl_dir is changing */ 256810298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 256910298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 257010298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 25717237Sek110237 } 25722082Seschrock /* move snap name entry */ 25737390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 25747390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_snap_remove(origin_head, 25756689Smaybee ds->ds_snapname, tx)); 25762199Sahrens VERIFY(0 == zap_add(dp->dp_meta_objset, 25772082Seschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 25782082Seschrock 8, 1, &ds->ds_object, tx)); 25792082Seschrock /* change containing dsl_dir */ 25802082Seschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 25815367Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 25822082Seschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 25835367Sahrens ASSERT3P(ds->ds_dir, ==, odd); 25842082Seschrock dsl_dir_close(ds->ds_dir, ds); 25852199Sahrens VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 25862082Seschrock NULL, ds, &ds->ds_dir)); 25872082Seschrock 25882082Seschrock ASSERT3U(dsl_prop_numcb(ds), ==, 0); 25897390SMatthew.Ahrens@Sun.COM } 25907390SMatthew.Ahrens@Sun.COM 25917390SMatthew.Ahrens@Sun.COM /* 25927390SMatthew.Ahrens@Sun.COM * Change space accounting. 25937390SMatthew.Ahrens@Sun.COM * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 25947390SMatthew.Ahrens@Sun.COM * both be valid, or both be 0 (resulting in delta == 0). This 25957390SMatthew.Ahrens@Sun.COM * is true for each of {clone,origin} independently. 25967390SMatthew.Ahrens@Sun.COM */ 25977390SMatthew.Ahrens@Sun.COM 25987390SMatthew.Ahrens@Sun.COM delta = pa->cloneusedsnap - 25997390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 26007390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, >=, 0); 26017390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, delta); 26027390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 26037390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_HEAD, 26047390SMatthew.Ahrens@Sun.COM pa->used - delta, pa->comp, pa->uncomp, tx); 26057390SMatthew.Ahrens@Sun.COM 26067390SMatthew.Ahrens@Sun.COM delta = pa->originusedsnap - 26077390SMatthew.Ahrens@Sun.COM odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 26087390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, <=, 0); 26097390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, -delta); 26107390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 26117390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_HEAD, 26127390SMatthew.Ahrens@Sun.COM -pa->used - delta, -pa->comp, -pa->uncomp, tx); 26137390SMatthew.Ahrens@Sun.COM 26145367Sahrens origin_ds->ds_phys->ds_unique_bytes = pa->unique; 26152082Seschrock 26164543Smarks /* log history record */ 26174543Smarks spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 26186689Smaybee cr, "dataset = %llu", hds->ds_object); 26194543Smarks 26205367Sahrens dsl_dir_close(odd, FTAG); 26212082Seschrock } 26222082Seschrock 26237390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist"; 26247390SMatthew.Ahrens@Sun.COM /* 26257390SMatthew.Ahrens@Sun.COM * Make a list of dsl_dataset_t's for the snapshots between first_obj 26267390SMatthew.Ahrens@Sun.COM * (exclusive) and last_obj (inclusive). The list will be in reverse 26277390SMatthew.Ahrens@Sun.COM * order (last_obj will be the list_head()). If first_obj == 0, do all 26287390SMatthew.Ahrens@Sun.COM * snapshots back to this dataset's origin. 26297390SMatthew.Ahrens@Sun.COM */ 26307390SMatthew.Ahrens@Sun.COM static int 26317390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own, 26327390SMatthew.Ahrens@Sun.COM uint64_t first_obj, uint64_t last_obj, list_t *l) 26337390SMatthew.Ahrens@Sun.COM { 26347390SMatthew.Ahrens@Sun.COM uint64_t obj = last_obj; 26357390SMatthew.Ahrens@Sun.COM 26367390SMatthew.Ahrens@Sun.COM ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock)); 26377390SMatthew.Ahrens@Sun.COM 26387390SMatthew.Ahrens@Sun.COM list_create(l, sizeof (struct promotenode), 26397390SMatthew.Ahrens@Sun.COM offsetof(struct promotenode, link)); 26407390SMatthew.Ahrens@Sun.COM 26417390SMatthew.Ahrens@Sun.COM while (obj != first_obj) { 26427390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 26437390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26447390SMatthew.Ahrens@Sun.COM int err; 26457390SMatthew.Ahrens@Sun.COM 26467390SMatthew.Ahrens@Sun.COM if (own) { 26477390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, obj, 26487390SMatthew.Ahrens@Sun.COM 0, snaplist_tag, &ds); 26497390SMatthew.Ahrens@Sun.COM if (err == 0) 26507390SMatthew.Ahrens@Sun.COM dsl_dataset_make_exclusive(ds, snaplist_tag); 26517390SMatthew.Ahrens@Sun.COM } else { 26527390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds); 26537390SMatthew.Ahrens@Sun.COM } 26547390SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 26557390SMatthew.Ahrens@Sun.COM /* lost race with snapshot destroy */ 26567390SMatthew.Ahrens@Sun.COM struct promotenode *last = list_tail(l); 26577390SMatthew.Ahrens@Sun.COM ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj); 26587390SMatthew.Ahrens@Sun.COM obj = last->ds->ds_phys->ds_prev_snap_obj; 26597390SMatthew.Ahrens@Sun.COM continue; 26607390SMatthew.Ahrens@Sun.COM } else if (err) { 26617390SMatthew.Ahrens@Sun.COM return (err); 26627390SMatthew.Ahrens@Sun.COM } 26637390SMatthew.Ahrens@Sun.COM 26647390SMatthew.Ahrens@Sun.COM if (first_obj == 0) 26657390SMatthew.Ahrens@Sun.COM first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 26667390SMatthew.Ahrens@Sun.COM 26677390SMatthew.Ahrens@Sun.COM snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 26687390SMatthew.Ahrens@Sun.COM snap->ds = ds; 26697390SMatthew.Ahrens@Sun.COM list_insert_tail(l, snap); 26707390SMatthew.Ahrens@Sun.COM obj = ds->ds_phys->ds_prev_snap_obj; 26717390SMatthew.Ahrens@Sun.COM } 26727390SMatthew.Ahrens@Sun.COM 26737390SMatthew.Ahrens@Sun.COM return (0); 26747390SMatthew.Ahrens@Sun.COM } 26757390SMatthew.Ahrens@Sun.COM 26767390SMatthew.Ahrens@Sun.COM static int 26777390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 26787390SMatthew.Ahrens@Sun.COM { 26797390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26807390SMatthew.Ahrens@Sun.COM 26817390SMatthew.Ahrens@Sun.COM *spacep = 0; 26827390SMatthew.Ahrens@Sun.COM for (snap = list_head(l); snap; snap = list_next(l, snap)) { 26837390SMatthew.Ahrens@Sun.COM uint64_t used; 26847390SMatthew.Ahrens@Sun.COM int err = bplist_space_birthrange(&snap->ds->ds_deadlist, 26857390SMatthew.Ahrens@Sun.COM mintxg, UINT64_MAX, &used); 26867390SMatthew.Ahrens@Sun.COM if (err) 26877390SMatthew.Ahrens@Sun.COM return (err); 26887390SMatthew.Ahrens@Sun.COM *spacep += used; 26897390SMatthew.Ahrens@Sun.COM } 26907390SMatthew.Ahrens@Sun.COM return (0); 26917390SMatthew.Ahrens@Sun.COM } 26927390SMatthew.Ahrens@Sun.COM 26937390SMatthew.Ahrens@Sun.COM static void 26947390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own) 26957390SMatthew.Ahrens@Sun.COM { 26967390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26977390SMatthew.Ahrens@Sun.COM 26988779SMark.Musante@Sun.COM if (!l || !list_link_active(&l->list_head)) 26997390SMatthew.Ahrens@Sun.COM return; 27007390SMatthew.Ahrens@Sun.COM 27017390SMatthew.Ahrens@Sun.COM while ((snap = list_tail(l)) != NULL) { 27027390SMatthew.Ahrens@Sun.COM list_remove(l, snap); 27037390SMatthew.Ahrens@Sun.COM if (own) 27047390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(snap->ds, snaplist_tag); 27057390SMatthew.Ahrens@Sun.COM else 27067390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(snap->ds, snaplist_tag); 27077390SMatthew.Ahrens@Sun.COM kmem_free(snap, sizeof (struct promotenode)); 27087390SMatthew.Ahrens@Sun.COM } 27097390SMatthew.Ahrens@Sun.COM list_destroy(l); 27107390SMatthew.Ahrens@Sun.COM } 27117390SMatthew.Ahrens@Sun.COM 27127390SMatthew.Ahrens@Sun.COM /* 27137390SMatthew.Ahrens@Sun.COM * Promote a clone. Nomenclature note: 27147390SMatthew.Ahrens@Sun.COM * "clone" or "cds": the original clone which is being promoted 27157390SMatthew.Ahrens@Sun.COM * "origin" or "ods": the snapshot which is originally clone's origin 27167390SMatthew.Ahrens@Sun.COM * "origin head" or "ohds": the dataset which is the head 27177390SMatthew.Ahrens@Sun.COM * (filesystem/volume) for the origin 27187390SMatthew.Ahrens@Sun.COM * "origin origin": the origin of the origin's filesystem (typically 27197390SMatthew.Ahrens@Sun.COM * NULL, indicating that the clone is not a clone of a clone). 27207390SMatthew.Ahrens@Sun.COM */ 27212082Seschrock int 272210588SEric.Taylor@Sun.COM dsl_dataset_promote(const char *name, char *conflsnap) 27232082Seschrock { 27242082Seschrock dsl_dataset_t *ds; 27256689Smaybee dsl_dir_t *dd; 27266689Smaybee dsl_pool_t *dp; 27272082Seschrock dmu_object_info_t doi; 27287390SMatthew.Ahrens@Sun.COM struct promotearg pa = { 0 }; 27297046Sahrens struct promotenode *snap; 27306689Smaybee int err; 27316689Smaybee 27326689Smaybee err = dsl_dataset_hold(name, FTAG, &ds); 27332082Seschrock if (err) 27342082Seschrock return (err); 27356689Smaybee dd = ds->ds_dir; 27366689Smaybee dp = dd->dd_pool; 27376689Smaybee 27386689Smaybee err = dmu_object_info(dp->dp_meta_objset, 27392082Seschrock ds->ds_phys->ds_snapnames_zapobj, &doi); 27402082Seschrock if (err) { 27416689Smaybee dsl_dataset_rele(ds, FTAG); 27422082Seschrock return (err); 27432082Seschrock } 27442082Seschrock 27457390SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) { 27467390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 27477390SMatthew.Ahrens@Sun.COM return (EINVAL); 27487390SMatthew.Ahrens@Sun.COM } 27497390SMatthew.Ahrens@Sun.COM 27502082Seschrock /* 27516689Smaybee * We are going to inherit all the snapshots taken before our 27526689Smaybee * origin (i.e., our new origin will be our parent's origin). 27536689Smaybee * Take ownership of them so that we can rename them into our 27546689Smaybee * namespace. 27556689Smaybee */ 27566689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 27577390SMatthew.Ahrens@Sun.COM 27587390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj, 27597390SMatthew.Ahrens@Sun.COM &pa.shared_snaps); 27607390SMatthew.Ahrens@Sun.COM if (err != 0) 27617390SMatthew.Ahrens@Sun.COM goto out; 27627390SMatthew.Ahrens@Sun.COM 27637390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps); 27647390SMatthew.Ahrens@Sun.COM if (err != 0) 27657390SMatthew.Ahrens@Sun.COM goto out; 27667390SMatthew.Ahrens@Sun.COM 27677390SMatthew.Ahrens@Sun.COM snap = list_head(&pa.shared_snaps); 27687390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 27697390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj, 27707390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps); 27717390SMatthew.Ahrens@Sun.COM if (err != 0) 27727390SMatthew.Ahrens@Sun.COM goto out; 27737390SMatthew.Ahrens@Sun.COM 27747390SMatthew.Ahrens@Sun.COM if (dsl_dir_is_clone(snap->ds->ds_dir)) { 27757390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, 27767390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_origin_obj, 27777390SMatthew.Ahrens@Sun.COM 0, FTAG, &pa.origin_origin); 27787390SMatthew.Ahrens@Sun.COM if (err != 0) 27796689Smaybee goto out; 27806689Smaybee } 27817390SMatthew.Ahrens@Sun.COM 27827390SMatthew.Ahrens@Sun.COM out: 27836689Smaybee rw_exit(&dp->dp_config_rwlock); 27846689Smaybee 27856689Smaybee /* 27862082Seschrock * Add in 128x the snapnames zapobj size, since we will be moving 27872082Seschrock * a bunch of snapnames to the promoted ds, and dirtying their 27882082Seschrock * bonus buffers. 27892082Seschrock */ 27907390SMatthew.Ahrens@Sun.COM if (err == 0) { 27917390SMatthew.Ahrens@Sun.COM err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 27927390SMatthew.Ahrens@Sun.COM dsl_dataset_promote_sync, ds, &pa, 279310922SJeff.Bonwick@Sun.COM 2 + 2 * doi.doi_physical_blocks_512); 279410588SEric.Taylor@Sun.COM if (err && pa.err_ds && conflsnap) 279510588SEric.Taylor@Sun.COM (void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN); 27966689Smaybee } 27977390SMatthew.Ahrens@Sun.COM 27987390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.shared_snaps, B_TRUE); 27997390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.clone_snaps, B_FALSE); 28007390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.origin_snaps, B_FALSE); 28017390SMatthew.Ahrens@Sun.COM if (pa.origin_origin) 28027390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(pa.origin_origin, FTAG); 28036689Smaybee dsl_dataset_rele(ds, FTAG); 28042082Seschrock return (err); 28052082Seschrock } 28063912Slling 28075367Sahrens struct cloneswaparg { 28085367Sahrens dsl_dataset_t *cds; /* clone dataset */ 28095367Sahrens dsl_dataset_t *ohds; /* origin's head dataset */ 28105367Sahrens boolean_t force; 28115481Sck153898 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 28125367Sahrens }; 28135326Sek110237 28145326Sek110237 /* ARGSUSED */ 28155326Sek110237 static int 28165326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 28175326Sek110237 { 28185367Sahrens struct cloneswaparg *csa = arg1; 28195326Sek110237 28205367Sahrens /* they should both be heads */ 28215367Sahrens if (dsl_dataset_is_snapshot(csa->cds) || 28225367Sahrens dsl_dataset_is_snapshot(csa->ohds)) 28235326Sek110237 return (EINVAL); 28245326Sek110237 28255367Sahrens /* the branch point should be just before them */ 28265367Sahrens if (csa->cds->ds_prev != csa->ohds->ds_prev) 28275326Sek110237 return (EINVAL); 28285326Sek110237 282910272SMatthew.Ahrens@Sun.COM /* cds should be the clone (unless they are unrelated) */ 283010272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev != NULL && 283110272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap && 283210272SMatthew.Ahrens@Sun.COM csa->ohds->ds_object != 283310272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev->ds_phys->ds_next_snap_obj) 28345367Sahrens return (EINVAL); 28355326Sek110237 28365367Sahrens /* the clone should be a child of the origin */ 28375367Sahrens if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 28385367Sahrens return (EINVAL); 28395326Sek110237 28405367Sahrens /* ohds shouldn't be modified unless 'force' */ 28415367Sahrens if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 28425367Sahrens return (ETXTBSY); 28435481Sck153898 28445481Sck153898 /* adjust amount of any unconsumed refreservation */ 28455481Sck153898 csa->unused_refres_delta = 28465481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28475481Sck153898 csa->ohds->ds_phys->ds_unique_bytes) - 28485481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28495481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 28505481Sck153898 28515481Sck153898 if (csa->unused_refres_delta > 0 && 28525481Sck153898 csa->unused_refres_delta > 28535481Sck153898 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 28545481Sck153898 return (ENOSPC); 28555481Sck153898 285610799SChris.Kirby@sun.com if (csa->ohds->ds_quota != 0 && 285710799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota) 285810799SChris.Kirby@sun.com return (EDQUOT); 285910799SChris.Kirby@sun.com 28605367Sahrens return (0); 28615326Sek110237 } 28625326Sek110237 28635326Sek110237 /* ARGSUSED */ 28645326Sek110237 static void 28655326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 28665326Sek110237 { 28675367Sahrens struct cloneswaparg *csa = arg1; 28685367Sahrens dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 28695326Sek110237 28705481Sck153898 ASSERT(csa->cds->ds_reserved == 0); 287110799SChris.Kirby@sun.com ASSERT(csa->ohds->ds_quota == 0 || 287210799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota); 28735481Sck153898 28745367Sahrens dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 28755367Sahrens dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 28765326Sek110237 287710298SMatthew.Ahrens@Sun.COM if (csa->cds->ds_objset != NULL) { 287810298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->cds->ds_objset); 287910298SMatthew.Ahrens@Sun.COM csa->cds->ds_objset = NULL; 28805367Sahrens } 28815326Sek110237 288210298SMatthew.Ahrens@Sun.COM if (csa->ohds->ds_objset != NULL) { 288310298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->ohds->ds_objset); 288410298SMatthew.Ahrens@Sun.COM csa->ohds->ds_objset = NULL; 28855367Sahrens } 28865326Sek110237 288710272SMatthew.Ahrens@Sun.COM /* 288810272SMatthew.Ahrens@Sun.COM * Reset origin's unique bytes, if it exists. 288910272SMatthew.Ahrens@Sun.COM */ 289010272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev) { 289110272SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin = csa->cds->ds_prev; 289210272SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(origin->ds_dbuf, tx); 289310272SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 289410272SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 289510272SMatthew.Ahrens@Sun.COM &origin->ds_phys->ds_unique_bytes)); 289610272SMatthew.Ahrens@Sun.COM } 28975326Sek110237 28985326Sek110237 /* swap blkptrs */ 28995326Sek110237 { 29005326Sek110237 blkptr_t tmp; 29015367Sahrens tmp = csa->ohds->ds_phys->ds_bp; 29025367Sahrens csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 29035367Sahrens csa->cds->ds_phys->ds_bp = tmp; 29045326Sek110237 } 29055326Sek110237 29065326Sek110237 /* set dd_*_bytes */ 29075326Sek110237 { 29085326Sek110237 int64_t dused, dcomp, duncomp; 29095326Sek110237 uint64_t cdl_used, cdl_comp, cdl_uncomp; 29105326Sek110237 uint64_t odl_used, odl_comp, odl_uncomp; 29115326Sek110237 29127390SMatthew.Ahrens@Sun.COM ASSERT3U(csa->cds->ds_dir->dd_phys-> 29137390SMatthew.Ahrens@Sun.COM dd_used_breakdown[DD_USED_SNAP], ==, 0); 29147390SMatthew.Ahrens@Sun.COM 29155367Sahrens VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 29165326Sek110237 &cdl_comp, &cdl_uncomp)); 29175367Sahrens VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 29185326Sek110237 &odl_comp, &odl_uncomp)); 29197390SMatthew.Ahrens@Sun.COM 29205367Sahrens dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 29215367Sahrens (csa->ohds->ds_phys->ds_used_bytes + odl_used); 29225367Sahrens dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 29235367Sahrens (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 29245367Sahrens duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 29255367Sahrens cdl_uncomp - 29265367Sahrens (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 29275326Sek110237 29287390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD, 29295367Sahrens dused, dcomp, duncomp, tx); 29307390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD, 29315367Sahrens -dused, -dcomp, -duncomp, tx); 29327390SMatthew.Ahrens@Sun.COM 29337390SMatthew.Ahrens@Sun.COM /* 29347390SMatthew.Ahrens@Sun.COM * The difference in the space used by snapshots is the 29357390SMatthew.Ahrens@Sun.COM * difference in snapshot space due to the head's 29367390SMatthew.Ahrens@Sun.COM * deadlist (since that's the only thing that's 29377390SMatthew.Ahrens@Sun.COM * changing that affects the snapused). 29387390SMatthew.Ahrens@Sun.COM */ 29397390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 29407390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used)); 29417390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist, 29427390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used)); 29437390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used, 29447390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 29455367Sahrens } 29465367Sahrens 29475367Sahrens #define SWITCH64(x, y) \ 29485367Sahrens { \ 29495367Sahrens uint64_t __tmp = (x); \ 29505367Sahrens (x) = (y); \ 29515367Sahrens (y) = __tmp; \ 29525326Sek110237 } 29535326Sek110237 29545326Sek110237 /* swap ds_*_bytes */ 29555367Sahrens SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 29565367Sahrens csa->cds->ds_phys->ds_used_bytes); 29575367Sahrens SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 29585367Sahrens csa->cds->ds_phys->ds_compressed_bytes); 29595367Sahrens SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 29605367Sahrens csa->cds->ds_phys->ds_uncompressed_bytes); 29615481Sck153898 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 29625481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 29635481Sck153898 29645481Sck153898 /* apply any parent delta for change in unconsumed refreservation */ 29657390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV, 29667390SMatthew.Ahrens@Sun.COM csa->unused_refres_delta, 0, 0, tx); 29675326Sek110237 29685326Sek110237 /* swap deadlists */ 29695367Sahrens bplist_close(&csa->cds->ds_deadlist); 29705367Sahrens bplist_close(&csa->ohds->ds_deadlist); 29715367Sahrens SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 29725367Sahrens csa->cds->ds_phys->ds_deadlist_obj); 29735367Sahrens VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 29745367Sahrens csa->cds->ds_phys->ds_deadlist_obj)); 29755367Sahrens VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 29765367Sahrens csa->ohds->ds_phys->ds_deadlist_obj)); 29777837SMatthew.Ahrens@Sun.COM 29787837SMatthew.Ahrens@Sun.COM dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx); 29795326Sek110237 } 29805326Sek110237 29815326Sek110237 /* 298210272SMatthew.Ahrens@Sun.COM * Swap 'clone' with its origin head datasets. Used at the end of "zfs 298310272SMatthew.Ahrens@Sun.COM * recv" into an existing fs to swizzle the file system to the new 298410272SMatthew.Ahrens@Sun.COM * version, and by "zfs rollback". Can also be used to swap two 298510272SMatthew.Ahrens@Sun.COM * independent head datasets if neither has any snapshots. 29865326Sek110237 */ 29875326Sek110237 int 29885367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 29895367Sahrens boolean_t force) 29905326Sek110237 { 29915367Sahrens struct cloneswaparg csa; 29926689Smaybee int error; 29936689Smaybee 29946689Smaybee ASSERT(clone->ds_owner); 29956689Smaybee ASSERT(origin_head->ds_owner); 29966689Smaybee retry: 29976689Smaybee /* Need exclusive access for the swap */ 29986689Smaybee rw_enter(&clone->ds_rwlock, RW_WRITER); 29996689Smaybee if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 30006689Smaybee rw_exit(&clone->ds_rwlock); 30016689Smaybee rw_enter(&origin_head->ds_rwlock, RW_WRITER); 30026689Smaybee if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 30036689Smaybee rw_exit(&origin_head->ds_rwlock); 30046689Smaybee goto retry; 30056689Smaybee } 30066689Smaybee } 30075367Sahrens csa.cds = clone; 30085367Sahrens csa.ohds = origin_head; 30095367Sahrens csa.force = force; 30106689Smaybee error = dsl_sync_task_do(clone->ds_dir->dd_pool, 30115326Sek110237 dsl_dataset_clone_swap_check, 30126689Smaybee dsl_dataset_clone_swap_sync, &csa, NULL, 9); 30136689Smaybee return (error); 30145326Sek110237 } 30155326Sek110237 30163912Slling /* 30173912Slling * Given a pool name and a dataset object number in that pool, 30183912Slling * return the name of that dataset. 30193912Slling */ 30203912Slling int 30213912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 30223912Slling { 30233912Slling spa_t *spa; 30243912Slling dsl_pool_t *dp; 30256689Smaybee dsl_dataset_t *ds; 30263912Slling int error; 30273912Slling 30283912Slling if ((error = spa_open(pname, &spa, FTAG)) != 0) 30293912Slling return (error); 30303912Slling dp = spa_get_dsl(spa); 30313912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 30326689Smaybee if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 30336689Smaybee dsl_dataset_name(ds, buf); 30346689Smaybee dsl_dataset_rele(ds, FTAG); 30353912Slling } 30363912Slling rw_exit(&dp->dp_config_rwlock); 30373912Slling spa_close(spa, FTAG); 30383912Slling 30396689Smaybee return (error); 30403912Slling } 30415378Sck153898 30425378Sck153898 int 30435378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 30446689Smaybee uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 30455378Sck153898 { 30465378Sck153898 int error = 0; 30475378Sck153898 30485378Sck153898 ASSERT3S(asize, >, 0); 30495378Sck153898 30505831Sck153898 /* 30515831Sck153898 * *ref_rsrv is the portion of asize that will come from any 30525831Sck153898 * unconsumed refreservation space. 30535831Sck153898 */ 30545831Sck153898 *ref_rsrv = 0; 30555831Sck153898 30565378Sck153898 mutex_enter(&ds->ds_lock); 30575378Sck153898 /* 30585378Sck153898 * Make a space adjustment for reserved bytes. 30595378Sck153898 */ 30605378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 30615378Sck153898 ASSERT3U(*used, >=, 30625378Sck153898 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30635378Sck153898 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30645831Sck153898 *ref_rsrv = 30655831Sck153898 asize - MIN(asize, parent_delta(ds, asize + inflight)); 30665378Sck153898 } 30675378Sck153898 30685378Sck153898 if (!check_quota || ds->ds_quota == 0) { 30695378Sck153898 mutex_exit(&ds->ds_lock); 30705378Sck153898 return (0); 30715378Sck153898 } 30725378Sck153898 /* 30735378Sck153898 * If they are requesting more space, and our current estimate 30745378Sck153898 * is over quota, they get to try again unless the actual 30755378Sck153898 * on-disk is over quota and there are no pending changes (which 30765378Sck153898 * may free up space for us). 30775378Sck153898 */ 30785378Sck153898 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 30795378Sck153898 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 30805378Sck153898 error = ERESTART; 30815378Sck153898 else 30825378Sck153898 error = EDQUOT; 30835378Sck153898 } 30845378Sck153898 mutex_exit(&ds->ds_lock); 30855378Sck153898 30865378Sck153898 return (error); 30875378Sck153898 } 30885378Sck153898 30895378Sck153898 /* ARGSUSED */ 30905378Sck153898 static int 30915378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 30925378Sck153898 { 30935378Sck153898 dsl_dataset_t *ds = arg1; 3094*11022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 3095*11022STom.Erickson@Sun.COM int err; 30965378Sck153898 30975378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 30985378Sck153898 return (ENOTSUP); 30995378Sck153898 3100*11022STom.Erickson@Sun.COM if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) 3101*11022STom.Erickson@Sun.COM return (err); 3102*11022STom.Erickson@Sun.COM 3103*11022STom.Erickson@Sun.COM if (psa->psa_effective_value == 0) 31045378Sck153898 return (0); 31055378Sck153898 3106*11022STom.Erickson@Sun.COM if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes || 3107*11022STom.Erickson@Sun.COM psa->psa_effective_value < ds->ds_reserved) 31085378Sck153898 return (ENOSPC); 31095378Sck153898 31105378Sck153898 return (0); 31115378Sck153898 } 31125378Sck153898 3113*11022STom.Erickson@Sun.COM extern void dsl_prop_set_sync(void *, void *, cred_t *, dmu_tx_t *); 3114*11022STom.Erickson@Sun.COM 31155378Sck153898 void 31165378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 31175378Sck153898 { 31185378Sck153898 dsl_dataset_t *ds = arg1; 3119*11022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 3120*11022STom.Erickson@Sun.COM uint64_t effective_value = psa->psa_effective_value; 3121*11022STom.Erickson@Sun.COM 3122*11022STom.Erickson@Sun.COM dsl_prop_set_sync(ds, psa, cr, tx); 3123*11022STom.Erickson@Sun.COM DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa); 3124*11022STom.Erickson@Sun.COM 3125*11022STom.Erickson@Sun.COM if (ds->ds_quota != effective_value) { 3126*11022STom.Erickson@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx); 3127*11022STom.Erickson@Sun.COM ds->ds_quota = effective_value; 3128*11022STom.Erickson@Sun.COM 3129*11022STom.Erickson@Sun.COM spa_history_internal_log(LOG_DS_REFQUOTA, 3130*11022STom.Erickson@Sun.COM ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu ", 3131*11022STom.Erickson@Sun.COM (longlong_t)ds->ds_quota, ds->ds_object); 3132*11022STom.Erickson@Sun.COM } 31335378Sck153898 } 31345378Sck153898 31355378Sck153898 int 3136*11022STom.Erickson@Sun.COM dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota) 31375378Sck153898 { 31385378Sck153898 dsl_dataset_t *ds; 3139*11022STom.Erickson@Sun.COM dsl_prop_setarg_t psa; 31405378Sck153898 int err; 31415378Sck153898 3142*11022STom.Erickson@Sun.COM dsl_prop_setarg_init_uint64(&psa, "refquota", source, "a); 3143*11022STom.Erickson@Sun.COM 31446689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 31455378Sck153898 if (err) 31465378Sck153898 return (err); 31475378Sck153898 3148*11022STom.Erickson@Sun.COM /* 3149*11022STom.Erickson@Sun.COM * If someone removes a file, then tries to set the quota, we 3150*11022STom.Erickson@Sun.COM * want to make sure the file freeing takes effect. 3151*11022STom.Erickson@Sun.COM */ 3152*11022STom.Erickson@Sun.COM txg_wait_open(ds->ds_dir->dd_pool, 0); 3153*11022STom.Erickson@Sun.COM 3154*11022STom.Erickson@Sun.COM err = dsl_sync_task_do(ds->ds_dir->dd_pool, 3155*11022STom.Erickson@Sun.COM dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 3156*11022STom.Erickson@Sun.COM ds, &psa, 0); 3157*11022STom.Erickson@Sun.COM 31586689Smaybee dsl_dataset_rele(ds, FTAG); 31595378Sck153898 return (err); 31605378Sck153898 } 31615378Sck153898 31625378Sck153898 static int 31635378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 31645378Sck153898 { 31655378Sck153898 dsl_dataset_t *ds = arg1; 3166*11022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 3167*11022STom.Erickson@Sun.COM uint64_t effective_value; 31685378Sck153898 uint64_t unique; 3169*11022STom.Erickson@Sun.COM int err; 31705378Sck153898 31715378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 31725378Sck153898 SPA_VERSION_REFRESERVATION) 31735378Sck153898 return (ENOTSUP); 31745378Sck153898 31755378Sck153898 if (dsl_dataset_is_snapshot(ds)) 31765378Sck153898 return (EINVAL); 31775378Sck153898 3178*11022STom.Erickson@Sun.COM if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) 3179*11022STom.Erickson@Sun.COM return (err); 3180*11022STom.Erickson@Sun.COM 3181*11022STom.Erickson@Sun.COM effective_value = psa->psa_effective_value; 3182*11022STom.Erickson@Sun.COM 31835378Sck153898 /* 31845378Sck153898 * If we are doing the preliminary check in open context, the 31855378Sck153898 * space estimates may be inaccurate. 31865378Sck153898 */ 31875378Sck153898 if (!dmu_tx_is_syncing(tx)) 31885378Sck153898 return (0); 31895378Sck153898 31905378Sck153898 mutex_enter(&ds->ds_lock); 31915378Sck153898 unique = dsl_dataset_unique(ds); 31925378Sck153898 mutex_exit(&ds->ds_lock); 31935378Sck153898 3194*11022STom.Erickson@Sun.COM if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) { 3195*11022STom.Erickson@Sun.COM uint64_t delta = MAX(unique, effective_value) - 31968525SEric.Schrock@Sun.COM MAX(unique, ds->ds_reserved); 31978525SEric.Schrock@Sun.COM 31988525SEric.Schrock@Sun.COM if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 31998525SEric.Schrock@Sun.COM return (ENOSPC); 32008525SEric.Schrock@Sun.COM if (ds->ds_quota > 0 && 3201*11022STom.Erickson@Sun.COM effective_value > ds->ds_quota) 32028525SEric.Schrock@Sun.COM return (ENOSPC); 32038525SEric.Schrock@Sun.COM } 32045378Sck153898 32055378Sck153898 return (0); 32065378Sck153898 } 32075378Sck153898 32085378Sck153898 /* ARGSUSED */ 32095378Sck153898 static void 32105378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 32115378Sck153898 dmu_tx_t *tx) 32125378Sck153898 { 32135378Sck153898 dsl_dataset_t *ds = arg1; 3214*11022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 3215*11022STom.Erickson@Sun.COM uint64_t effective_value = psa->psa_effective_value; 32167595SMatthew.Ahrens@Sun.COM uint64_t unique; 32177595SMatthew.Ahrens@Sun.COM int64_t delta; 32187595SMatthew.Ahrens@Sun.COM 3219*11022STom.Erickson@Sun.COM dsl_prop_set_sync(ds, psa, cr, tx); 3220*11022STom.Erickson@Sun.COM DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa); 3221*11022STom.Erickson@Sun.COM 32227595SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx); 32237595SMatthew.Ahrens@Sun.COM 32247595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 32257595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_lock); 32267595SMatthew.Ahrens@Sun.COM unique = dsl_dataset_unique(ds); 3227*11022STom.Erickson@Sun.COM delta = MAX(0, (int64_t)(effective_value - unique)) - 32287595SMatthew.Ahrens@Sun.COM MAX(0, (int64_t)(ds->ds_reserved - unique)); 3229*11022STom.Erickson@Sun.COM ds->ds_reserved = effective_value; 32307595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_lock); 32317595SMatthew.Ahrens@Sun.COM 32327595SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 32337595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 32347595SMatthew.Ahrens@Sun.COM 32357595SMatthew.Ahrens@Sun.COM spa_history_internal_log(LOG_DS_REFRESERV, 32367595SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 3237*11022STom.Erickson@Sun.COM (longlong_t)effective_value, ds->ds_object); 32385378Sck153898 } 32395378Sck153898 32405378Sck153898 int 3241*11022STom.Erickson@Sun.COM dsl_dataset_set_reservation(const char *dsname, zprop_source_t source, 3242*11022STom.Erickson@Sun.COM uint64_t reservation) 32435378Sck153898 { 32445378Sck153898 dsl_dataset_t *ds; 3245*11022STom.Erickson@Sun.COM dsl_prop_setarg_t psa; 32465378Sck153898 int err; 32475378Sck153898 3248*11022STom.Erickson@Sun.COM dsl_prop_setarg_init_uint64(&psa, "refreservation", source, 3249*11022STom.Erickson@Sun.COM &reservation); 3250*11022STom.Erickson@Sun.COM 32516689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 32525378Sck153898 if (err) 32535378Sck153898 return (err); 32545378Sck153898 32555378Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 32565378Sck153898 dsl_dataset_set_reservation_check, 3257*11022STom.Erickson@Sun.COM dsl_dataset_set_reservation_sync, ds, &psa, 0); 3258*11022STom.Erickson@Sun.COM 32596689Smaybee dsl_dataset_rele(ds, FTAG); 32605378Sck153898 return (err); 32615378Sck153898 } 326210242Schris.kirby@sun.com 326310342Schris.kirby@sun.com struct dsl_ds_holdarg { 326410342Schris.kirby@sun.com dsl_sync_task_group_t *dstg; 326510342Schris.kirby@sun.com char *htag; 326610342Schris.kirby@sun.com char *snapname; 326710342Schris.kirby@sun.com boolean_t recursive; 326810342Schris.kirby@sun.com boolean_t gotone; 326910342Schris.kirby@sun.com boolean_t temphold; 327010342Schris.kirby@sun.com char failed[MAXPATHLEN]; 327110342Schris.kirby@sun.com }; 327210342Schris.kirby@sun.com 327310951SChris.Kirby@sun.com /* 327410951SChris.Kirby@sun.com * The max length of a temporary tag prefix is the number of hex digits 327510951SChris.Kirby@sun.com * required to express UINT64_MAX plus one for the hyphen. 327610951SChris.Kirby@sun.com */ 327710951SChris.Kirby@sun.com #define MAX_TAG_PREFIX_LEN 17 327810951SChris.Kirby@sun.com 327910242Schris.kirby@sun.com static int 328010242Schris.kirby@sun.com dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx) 328110242Schris.kirby@sun.com { 328210242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 328310342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 328410342Schris.kirby@sun.com char *htag = ha->htag; 328510242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 328610242Schris.kirby@sun.com int error = 0; 328710242Schris.kirby@sun.com 328810242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 328910242Schris.kirby@sun.com return (ENOTSUP); 329010242Schris.kirby@sun.com 329110242Schris.kirby@sun.com if (!dsl_dataset_is_snapshot(ds)) 329210242Schris.kirby@sun.com return (EINVAL); 329310242Schris.kirby@sun.com 329410242Schris.kirby@sun.com /* tags must be unique */ 329510242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 329610242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj) { 329710242Schris.kirby@sun.com error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag, 329810242Schris.kirby@sun.com 8, 1, tx); 329910242Schris.kirby@sun.com if (error == 0) 330010242Schris.kirby@sun.com error = EEXIST; 330110242Schris.kirby@sun.com else if (error == ENOENT) 330210242Schris.kirby@sun.com error = 0; 330310242Schris.kirby@sun.com } 330410242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 330510242Schris.kirby@sun.com 330610951SChris.Kirby@sun.com if (error == 0 && ha->temphold && 330710951SChris.Kirby@sun.com strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN) 330810951SChris.Kirby@sun.com error = E2BIG; 330910951SChris.Kirby@sun.com 331010242Schris.kirby@sun.com return (error); 331110242Schris.kirby@sun.com } 331210242Schris.kirby@sun.com 331310242Schris.kirby@sun.com static void 331410242Schris.kirby@sun.com dsl_dataset_user_hold_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 331510242Schris.kirby@sun.com { 331610242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 331710342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 331810342Schris.kirby@sun.com char *htag = ha->htag; 331910342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 332010342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 332110951SChris.Kirby@sun.com uint64_t now = gethrestime_sec(); 332210242Schris.kirby@sun.com uint64_t zapobj; 332310242Schris.kirby@sun.com 332410242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 332510242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj == 0) { 332610242Schris.kirby@sun.com /* 332710242Schris.kirby@sun.com * This is the first user hold for this dataset. Create 332810242Schris.kirby@sun.com * the userrefs zap object. 332910242Schris.kirby@sun.com */ 333010242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 333110242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj = 333210242Schris.kirby@sun.com zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx); 333310242Schris.kirby@sun.com } else { 333410242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 333510242Schris.kirby@sun.com } 333610242Schris.kirby@sun.com ds->ds_userrefs++; 333710242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 333810242Schris.kirby@sun.com 333910242Schris.kirby@sun.com VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx)); 334010242Schris.kirby@sun.com 334110342Schris.kirby@sun.com if (ha->temphold) { 334210342Schris.kirby@sun.com VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object, 334310342Schris.kirby@sun.com htag, &now, tx)); 334410342Schris.kirby@sun.com } 334510342Schris.kirby@sun.com 334610242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_HOLD, 334710342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> temp = %d dataset = %llu", htag, 334810342Schris.kirby@sun.com (int)ha->temphold, ds->ds_object); 334910242Schris.kirby@sun.com } 335010242Schris.kirby@sun.com 335110242Schris.kirby@sun.com static int 335210242Schris.kirby@sun.com dsl_dataset_user_hold_one(char *dsname, void *arg) 335310242Schris.kirby@sun.com { 335410242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 335510242Schris.kirby@sun.com dsl_dataset_t *ds; 335610242Schris.kirby@sun.com int error; 335710242Schris.kirby@sun.com char *name; 335810242Schris.kirby@sun.com 335910242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname plus terminating NULL */ 336010272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 336110242Schris.kirby@sun.com error = dsl_dataset_hold(name, ha->dstg, &ds); 336210272SMatthew.Ahrens@Sun.COM strfree(name); 336310242Schris.kirby@sun.com if (error == 0) { 336410268Schris.kirby@sun.com ha->gotone = B_TRUE; 336510242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check, 336610342Schris.kirby@sun.com dsl_dataset_user_hold_sync, ds, ha, 0); 336710242Schris.kirby@sun.com } else if (error == ENOENT && ha->recursive) { 336810242Schris.kirby@sun.com error = 0; 336910242Schris.kirby@sun.com } else { 337010242Schris.kirby@sun.com (void) strcpy(ha->failed, dsname); 337110242Schris.kirby@sun.com } 337210242Schris.kirby@sun.com return (error); 337310242Schris.kirby@sun.com } 337410242Schris.kirby@sun.com 337510242Schris.kirby@sun.com int 337610242Schris.kirby@sun.com dsl_dataset_user_hold(char *dsname, char *snapname, char *htag, 337710342Schris.kirby@sun.com boolean_t recursive, boolean_t temphold) 337810242Schris.kirby@sun.com { 337910242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 338010242Schris.kirby@sun.com dsl_sync_task_t *dst; 338110242Schris.kirby@sun.com spa_t *spa; 338210242Schris.kirby@sun.com int error; 338310242Schris.kirby@sun.com 338410242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 338510242Schris.kirby@sun.com 338610242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 338710242Schris.kirby@sun.com 338810242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 338910242Schris.kirby@sun.com if (error) { 339010242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 339110242Schris.kirby@sun.com return (error); 339210242Schris.kirby@sun.com } 339310242Schris.kirby@sun.com 339410242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 339510242Schris.kirby@sun.com ha->htag = htag; 339610242Schris.kirby@sun.com ha->snapname = snapname; 339710242Schris.kirby@sun.com ha->recursive = recursive; 339810342Schris.kirby@sun.com ha->temphold = temphold; 339910242Schris.kirby@sun.com if (recursive) { 340010242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_hold_one, 340110242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 340210242Schris.kirby@sun.com } else { 340310242Schris.kirby@sun.com error = dsl_dataset_user_hold_one(dsname, ha); 340410242Schris.kirby@sun.com } 340510242Schris.kirby@sun.com if (error == 0) 340610242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 340710242Schris.kirby@sun.com 340810242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 340910242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 341010242Schris.kirby@sun.com dsl_dataset_t *ds = dst->dst_arg1; 341110242Schris.kirby@sun.com 341210242Schris.kirby@sun.com if (dst->dst_err) { 341310242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 341410242Schris.kirby@sun.com *strchr(ha->failed, '@') = '\0'; 341510242Schris.kirby@sun.com } 341610242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 341710242Schris.kirby@sun.com } 341810242Schris.kirby@sun.com 341910268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 342010268Schris.kirby@sun.com error = ENOENT; 342110268Schris.kirby@sun.com 342210242Schris.kirby@sun.com if (error) 342310242Schris.kirby@sun.com (void) strcpy(dsname, ha->failed); 342410242Schris.kirby@sun.com 342510242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 342610242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 342710242Schris.kirby@sun.com spa_close(spa, FTAG); 342810242Schris.kirby@sun.com return (error); 342910242Schris.kirby@sun.com } 343010242Schris.kirby@sun.com 343110242Schris.kirby@sun.com struct dsl_ds_releasearg { 343210242Schris.kirby@sun.com dsl_dataset_t *ds; 343310242Schris.kirby@sun.com const char *htag; 343410242Schris.kirby@sun.com boolean_t own; /* do we own or just hold ds? */ 343510242Schris.kirby@sun.com }; 343610242Schris.kirby@sun.com 343710242Schris.kirby@sun.com static int 343810242Schris.kirby@sun.com dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag, 343910242Schris.kirby@sun.com boolean_t *might_destroy) 344010242Schris.kirby@sun.com { 344110242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 344210242Schris.kirby@sun.com uint64_t zapobj; 344310242Schris.kirby@sun.com uint64_t tmp; 344410242Schris.kirby@sun.com int error; 344510242Schris.kirby@sun.com 344610242Schris.kirby@sun.com *might_destroy = B_FALSE; 344710242Schris.kirby@sun.com 344810242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 344910242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 345010242Schris.kirby@sun.com if (zapobj == 0) { 345110242Schris.kirby@sun.com /* The tag can't possibly exist */ 345210242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 345310242Schris.kirby@sun.com return (ESRCH); 345410242Schris.kirby@sun.com } 345510242Schris.kirby@sun.com 345610242Schris.kirby@sun.com /* Make sure the tag exists */ 345710242Schris.kirby@sun.com error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp); 345810242Schris.kirby@sun.com if (error) { 345910242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 346010242Schris.kirby@sun.com if (error == ENOENT) 346110242Schris.kirby@sun.com error = ESRCH; 346210242Schris.kirby@sun.com return (error); 346310242Schris.kirby@sun.com } 346410242Schris.kirby@sun.com 346510242Schris.kirby@sun.com if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 && 346610242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 346710242Schris.kirby@sun.com *might_destroy = B_TRUE; 346810242Schris.kirby@sun.com 346910242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 347010242Schris.kirby@sun.com return (0); 347110242Schris.kirby@sun.com } 347210242Schris.kirby@sun.com 347310242Schris.kirby@sun.com static int 347410242Schris.kirby@sun.com dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx) 347510242Schris.kirby@sun.com { 347610242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 347710242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 347810242Schris.kirby@sun.com boolean_t might_destroy; 347910242Schris.kirby@sun.com int error; 348010242Schris.kirby@sun.com 348110242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 348210242Schris.kirby@sun.com return (ENOTSUP); 348310242Schris.kirby@sun.com 348410242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy); 348510242Schris.kirby@sun.com if (error) 348610242Schris.kirby@sun.com return (error); 348710242Schris.kirby@sun.com 348810242Schris.kirby@sun.com if (might_destroy) { 348910242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 349010242Schris.kirby@sun.com 349110242Schris.kirby@sun.com if (dmu_tx_is_syncing(tx)) { 349210242Schris.kirby@sun.com /* 349310242Schris.kirby@sun.com * If we're not prepared to remove the snapshot, 349410242Schris.kirby@sun.com * we can't allow the release to happen right now. 349510242Schris.kirby@sun.com */ 349610242Schris.kirby@sun.com if (!ra->own) 349710242Schris.kirby@sun.com return (EBUSY); 349810298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 349910298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 350010298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 350110242Schris.kirby@sun.com } 350210242Schris.kirby@sun.com } 350310242Schris.kirby@sun.com dsda.ds = ds; 350410242Schris.kirby@sun.com dsda.releasing = B_TRUE; 350510242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&dsda, tag, tx)); 350610242Schris.kirby@sun.com } 350710242Schris.kirby@sun.com 350810242Schris.kirby@sun.com return (0); 350910242Schris.kirby@sun.com } 351010242Schris.kirby@sun.com 351110242Schris.kirby@sun.com static void 351210242Schris.kirby@sun.com dsl_dataset_user_release_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 351310242Schris.kirby@sun.com { 351410242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 351510242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 351610342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 351710342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 351810242Schris.kirby@sun.com uint64_t zapobj; 351910242Schris.kirby@sun.com uint64_t dsobj = ds->ds_object; 352010242Schris.kirby@sun.com uint64_t refs; 352110342Schris.kirby@sun.com int error; 352210242Schris.kirby@sun.com 352310242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 352410242Schris.kirby@sun.com ds->ds_userrefs--; 352510242Schris.kirby@sun.com refs = ds->ds_userrefs; 352610242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 352710342Schris.kirby@sun.com error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx); 352810342Schris.kirby@sun.com VERIFY(error == 0 || error == ENOENT); 352910242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 353010242Schris.kirby@sun.com VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx)); 353110242Schris.kirby@sun.com if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && 353210242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) { 353310242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 353410242Schris.kirby@sun.com 353510242Schris.kirby@sun.com ASSERT(ra->own); 353610242Schris.kirby@sun.com dsda.ds = ds; 353710242Schris.kirby@sun.com dsda.releasing = B_TRUE; 353810242Schris.kirby@sun.com /* We already did the destroy_check */ 353910242Schris.kirby@sun.com dsl_dataset_destroy_sync(&dsda, tag, cr, tx); 354010242Schris.kirby@sun.com } 354110242Schris.kirby@sun.com 354210242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_RELEASE, 354310342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> %lld dataset = %llu", 354410242Schris.kirby@sun.com ra->htag, (longlong_t)refs, dsobj); 354510242Schris.kirby@sun.com } 354610242Schris.kirby@sun.com 354710242Schris.kirby@sun.com static int 354810242Schris.kirby@sun.com dsl_dataset_user_release_one(char *dsname, void *arg) 354910242Schris.kirby@sun.com { 355010242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 355110242Schris.kirby@sun.com struct dsl_ds_releasearg *ra; 355210242Schris.kirby@sun.com dsl_dataset_t *ds; 355310242Schris.kirby@sun.com int error; 355410242Schris.kirby@sun.com void *dtag = ha->dstg; 355510242Schris.kirby@sun.com char *name; 355610242Schris.kirby@sun.com boolean_t own = B_FALSE; 355710242Schris.kirby@sun.com boolean_t might_destroy; 355810242Schris.kirby@sun.com 355910242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */ 356010272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 356110242Schris.kirby@sun.com error = dsl_dataset_hold(name, dtag, &ds); 356210272SMatthew.Ahrens@Sun.COM strfree(name); 356310242Schris.kirby@sun.com if (error == ENOENT && ha->recursive) 356410242Schris.kirby@sun.com return (0); 356510242Schris.kirby@sun.com (void) strcpy(ha->failed, dsname); 356610242Schris.kirby@sun.com if (error) 356710242Schris.kirby@sun.com return (error); 356810242Schris.kirby@sun.com 356910268Schris.kirby@sun.com ha->gotone = B_TRUE; 357010268Schris.kirby@sun.com 357110242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 357210242Schris.kirby@sun.com 357310242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy); 357410242Schris.kirby@sun.com if (error) { 357510242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 357610242Schris.kirby@sun.com return (error); 357710242Schris.kirby@sun.com } 357810242Schris.kirby@sun.com 357910242Schris.kirby@sun.com if (might_destroy) { 358010242Schris.kirby@sun.com #ifdef _KERNEL 358110242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 358210242Schris.kirby@sun.com if (error) { 358310242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 358410242Schris.kirby@sun.com return (error); 358510242Schris.kirby@sun.com } 358610242Schris.kirby@sun.com #endif 358710298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) { 358810242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 358910242Schris.kirby@sun.com return (EBUSY); 359010242Schris.kirby@sun.com } else { 359110242Schris.kirby@sun.com own = B_TRUE; 359210242Schris.kirby@sun.com dsl_dataset_make_exclusive(ds, dtag); 359310242Schris.kirby@sun.com } 359410242Schris.kirby@sun.com } 359510242Schris.kirby@sun.com 359610242Schris.kirby@sun.com ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP); 359710242Schris.kirby@sun.com ra->ds = ds; 359810242Schris.kirby@sun.com ra->htag = ha->htag; 359910242Schris.kirby@sun.com ra->own = own; 360010242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check, 360110242Schris.kirby@sun.com dsl_dataset_user_release_sync, ra, dtag, 0); 360210242Schris.kirby@sun.com 360310242Schris.kirby@sun.com return (0); 360410242Schris.kirby@sun.com } 360510242Schris.kirby@sun.com 360610242Schris.kirby@sun.com int 360710242Schris.kirby@sun.com dsl_dataset_user_release(char *dsname, char *snapname, char *htag, 360810242Schris.kirby@sun.com boolean_t recursive) 360910242Schris.kirby@sun.com { 361010242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 361110242Schris.kirby@sun.com dsl_sync_task_t *dst; 361210242Schris.kirby@sun.com spa_t *spa; 361310242Schris.kirby@sun.com int error; 361410242Schris.kirby@sun.com 361510242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 361610242Schris.kirby@sun.com 361710242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 361810242Schris.kirby@sun.com 361910242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 362010242Schris.kirby@sun.com if (error) { 362110242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 362210242Schris.kirby@sun.com return (error); 362310242Schris.kirby@sun.com } 362410242Schris.kirby@sun.com 362510242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 362610242Schris.kirby@sun.com ha->htag = htag; 362710242Schris.kirby@sun.com ha->snapname = snapname; 362810242Schris.kirby@sun.com ha->recursive = recursive; 362910242Schris.kirby@sun.com if (recursive) { 363010242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_release_one, 363110242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 363210242Schris.kirby@sun.com } else { 363310242Schris.kirby@sun.com error = dsl_dataset_user_release_one(dsname, ha); 363410242Schris.kirby@sun.com } 363510242Schris.kirby@sun.com if (error == 0) 363610242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 363710242Schris.kirby@sun.com 363810242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 363910242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 364010242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = dst->dst_arg1; 364110242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 364210242Schris.kirby@sun.com 364310242Schris.kirby@sun.com if (dst->dst_err) 364410242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 364510242Schris.kirby@sun.com 364610242Schris.kirby@sun.com if (ra->own) 364710242Schris.kirby@sun.com dsl_dataset_disown(ds, ha->dstg); 364810242Schris.kirby@sun.com else 364910242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 365010242Schris.kirby@sun.com 365110242Schris.kirby@sun.com kmem_free(ra, sizeof (struct dsl_ds_releasearg)); 365210242Schris.kirby@sun.com } 365310242Schris.kirby@sun.com 365410268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 365510268Schris.kirby@sun.com error = ENOENT; 365610268Schris.kirby@sun.com 365710242Schris.kirby@sun.com if (error) 365810242Schris.kirby@sun.com (void) strcpy(dsname, ha->failed); 365910242Schris.kirby@sun.com 366010242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 366110242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 366210242Schris.kirby@sun.com spa_close(spa, FTAG); 366310242Schris.kirby@sun.com return (error); 366410242Schris.kirby@sun.com } 366510242Schris.kirby@sun.com 366610342Schris.kirby@sun.com /* 366710342Schris.kirby@sun.com * Called at spa_load time to release a stale temporary user hold. 366810342Schris.kirby@sun.com */ 366910342Schris.kirby@sun.com int 367010342Schris.kirby@sun.com dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag) 367110342Schris.kirby@sun.com { 367210342Schris.kirby@sun.com dsl_dataset_t *ds; 367310342Schris.kirby@sun.com char *snap; 367410342Schris.kirby@sun.com char *name; 367510342Schris.kirby@sun.com int namelen; 367610342Schris.kirby@sun.com int error; 367710342Schris.kirby@sun.com 367810342Schris.kirby@sun.com rw_enter(&dp->dp_config_rwlock, RW_READER); 367910342Schris.kirby@sun.com error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 368010342Schris.kirby@sun.com rw_exit(&dp->dp_config_rwlock); 368110342Schris.kirby@sun.com if (error) 368210342Schris.kirby@sun.com return (error); 368310342Schris.kirby@sun.com namelen = dsl_dataset_namelen(ds)+1; 368410342Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 368510342Schris.kirby@sun.com dsl_dataset_name(ds, name); 368610342Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 368710342Schris.kirby@sun.com 368810342Schris.kirby@sun.com snap = strchr(name, '@'); 368910342Schris.kirby@sun.com *snap = '\0'; 369010342Schris.kirby@sun.com ++snap; 369110342Schris.kirby@sun.com return (dsl_dataset_user_release(name, snap, htag, B_FALSE)); 369210342Schris.kirby@sun.com } 369310342Schris.kirby@sun.com 369410242Schris.kirby@sun.com int 369510242Schris.kirby@sun.com dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp) 369610242Schris.kirby@sun.com { 369710242Schris.kirby@sun.com dsl_dataset_t *ds; 369810242Schris.kirby@sun.com int err; 369910242Schris.kirby@sun.com 370010242Schris.kirby@sun.com err = dsl_dataset_hold(dsname, FTAG, &ds); 370110242Schris.kirby@sun.com if (err) 370210242Schris.kirby@sun.com return (err); 370310242Schris.kirby@sun.com 370410242Schris.kirby@sun.com VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)); 370510242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) { 370610242Schris.kirby@sun.com zap_attribute_t *za; 370710242Schris.kirby@sun.com zap_cursor_t zc; 370810242Schris.kirby@sun.com 370910242Schris.kirby@sun.com za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 371010242Schris.kirby@sun.com for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset, 371110242Schris.kirby@sun.com ds->ds_phys->ds_userrefs_obj); 371210242Schris.kirby@sun.com zap_cursor_retrieve(&zc, za) == 0; 371310242Schris.kirby@sun.com zap_cursor_advance(&zc)) { 371410242Schris.kirby@sun.com VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name, 371510242Schris.kirby@sun.com za->za_first_integer)); 371610242Schris.kirby@sun.com } 371710242Schris.kirby@sun.com zap_cursor_fini(&zc); 371810242Schris.kirby@sun.com kmem_free(za, sizeof (zap_attribute_t)); 371910242Schris.kirby@sun.com } 372010242Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 372110242Schris.kirby@sun.com return (0); 372210242Schris.kirby@sun.com } 372310298SMatthew.Ahrens@Sun.COM 372410298SMatthew.Ahrens@Sun.COM /* 372510298SMatthew.Ahrens@Sun.COM * Note, this fuction is used as the callback for dmu_objset_find(). We 372610298SMatthew.Ahrens@Sun.COM * always return 0 so that we will continue to find and process 372710298SMatthew.Ahrens@Sun.COM * inconsistent datasets, even if we encounter an error trying to 372810298SMatthew.Ahrens@Sun.COM * process one of them. 372910298SMatthew.Ahrens@Sun.COM */ 373010298SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 373110298SMatthew.Ahrens@Sun.COM int 373210298SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent(char *dsname, void *arg) 373310298SMatthew.Ahrens@Sun.COM { 373410298SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 373510298SMatthew.Ahrens@Sun.COM 373610298SMatthew.Ahrens@Sun.COM if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) { 373710298SMatthew.Ahrens@Sun.COM if (DS_IS_INCONSISTENT(ds)) 373810298SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(ds, FTAG, B_FALSE); 373910298SMatthew.Ahrens@Sun.COM else 374010298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 374110298SMatthew.Ahrens@Sun.COM } 374210298SMatthew.Ahrens@Sun.COM return (0); 374310298SMatthew.Ahrens@Sun.COM } 3744