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 /* 2211546SChris.Kirby@sun.com * Copyright 2010 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 } 53511546SChris.Kirby@sun.com /* 53611546SChris.Kirby@sun.com * The dp_config_rwlock lives above the ds_lock. And 53711546SChris.Kirby@sun.com * we need to check DSL_DATASET_IS_DESTROYED() while 53811546SChris.Kirby@sun.com * holding the ds_lock, so we have to drop and reacquire 53911546SChris.Kirby@sun.com * the ds_lock here. 54011546SChris.Kirby@sun.com */ 54111546SChris.Kirby@sun.com mutex_exit(&ds->ds_lock); 5426689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 54311546SChris.Kirby@sun.com mutex_enter(&ds->ds_lock); 5446689Smaybee } 545789Sahrens mutex_exit(&ds->ds_lock); 5461544Seschrock return (0); 547789Sahrens } 548789Sahrens 549789Sahrens int 5506689Smaybee dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 5516689Smaybee dsl_dataset_t **dsp) 5526689Smaybee { 5536689Smaybee int err = dsl_dataset_get_ref(dp, dsobj, tag, dsp); 5546689Smaybee 5556689Smaybee if (err) 5566689Smaybee return (err); 5576689Smaybee return (dsl_dataset_hold_ref(*dsp, tag)); 5586689Smaybee } 5596689Smaybee 5606689Smaybee int 56110298SMatthew.Ahrens@Sun.COM dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, boolean_t inconsistentok, 56210298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 5636689Smaybee { 56410298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5656689Smaybee if (err) 5666689Smaybee return (err); 56710298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 56810298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 5698779SMark.Musante@Sun.COM *dsp = NULL; 5706689Smaybee return (EBUSY); 5716689Smaybee } 5726689Smaybee return (0); 5736689Smaybee } 5746689Smaybee 5756689Smaybee int 5766689Smaybee dsl_dataset_hold(const char *name, void *tag, dsl_dataset_t **dsp) 577789Sahrens { 578789Sahrens dsl_dir_t *dd; 579789Sahrens dsl_pool_t *dp; 5806689Smaybee const char *snapname; 581789Sahrens uint64_t obj; 582789Sahrens int err = 0; 583789Sahrens 5846689Smaybee err = dsl_dir_open_spa(NULL, name, FTAG, &dd, &snapname); 5851544Seschrock if (err) 5861544Seschrock return (err); 587789Sahrens 588789Sahrens dp = dd->dd_pool; 589789Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 590789Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER); 5916689Smaybee if (obj) 5926689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, dsp); 5936689Smaybee else 594789Sahrens err = ENOENT; 5956689Smaybee if (err) 596789Sahrens goto out; 5976689Smaybee 5986689Smaybee err = dsl_dataset_hold_ref(*dsp, tag); 5996689Smaybee 6006689Smaybee /* we may be looking for a snapshot */ 6016689Smaybee if (err == 0 && snapname != NULL) { 6026689Smaybee dsl_dataset_t *ds = NULL; 6036689Smaybee 6046689Smaybee if (*snapname++ != '@') { 6056689Smaybee dsl_dataset_rele(*dsp, tag); 606789Sahrens err = ENOENT; 607789Sahrens goto out; 608789Sahrens } 6096689Smaybee 6106689Smaybee dprintf("looking for snapshot '%s'\n", snapname); 6116689Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 6126689Smaybee if (err == 0) 6136689Smaybee err = dsl_dataset_get_ref(dp, obj, tag, &ds); 6146689Smaybee dsl_dataset_rele(*dsp, tag); 6156689Smaybee 6166689Smaybee ASSERT3U((err == 0), ==, (ds != NULL)); 6176689Smaybee 6186689Smaybee if (ds) { 6196689Smaybee mutex_enter(&ds->ds_lock); 6206689Smaybee if (ds->ds_snapname[0] == 0) 6216689Smaybee (void) strlcpy(ds->ds_snapname, snapname, 6226689Smaybee sizeof (ds->ds_snapname)); 6236689Smaybee mutex_exit(&ds->ds_lock); 6246689Smaybee err = dsl_dataset_hold_ref(ds, tag); 6256689Smaybee *dsp = err ? NULL : ds; 626789Sahrens } 627789Sahrens } 628789Sahrens out: 629789Sahrens rw_exit(&dp->dp_config_rwlock); 630789Sahrens dsl_dir_close(dd, FTAG); 631789Sahrens return (err); 632789Sahrens } 633789Sahrens 634789Sahrens int 63510298SMatthew.Ahrens@Sun.COM dsl_dataset_own(const char *name, boolean_t inconsistentok, 63610298SMatthew.Ahrens@Sun.COM void *tag, dsl_dataset_t **dsp) 637789Sahrens { 63810298SMatthew.Ahrens@Sun.COM int err = dsl_dataset_hold(name, tag, dsp); 6396689Smaybee if (err) 6406689Smaybee return (err); 64110298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(*dsp, inconsistentok, tag)) { 64210298SMatthew.Ahrens@Sun.COM dsl_dataset_rele(*dsp, tag); 6436689Smaybee return (EBUSY); 6446689Smaybee } 6456689Smaybee return (0); 646789Sahrens } 647789Sahrens 648789Sahrens void 649789Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 650789Sahrens { 651789Sahrens if (ds == NULL) { 652789Sahrens (void) strcpy(name, "mos"); 653789Sahrens } else { 654789Sahrens dsl_dir_name(ds->ds_dir, name); 6551544Seschrock VERIFY(0 == dsl_dataset_get_snapname(ds)); 656789Sahrens if (ds->ds_snapname[0]) { 657789Sahrens (void) strcat(name, "@"); 6586689Smaybee /* 6596689Smaybee * We use a "recursive" mutex so that we 6606689Smaybee * can call dprintf_ds() with ds_lock held. 6616689Smaybee */ 662789Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 663789Sahrens mutex_enter(&ds->ds_lock); 664789Sahrens (void) strcat(name, ds->ds_snapname); 665789Sahrens mutex_exit(&ds->ds_lock); 666789Sahrens } else { 667789Sahrens (void) strcat(name, ds->ds_snapname); 668789Sahrens } 669789Sahrens } 670789Sahrens } 671789Sahrens } 672789Sahrens 6733978Smmusante static int 6743978Smmusante dsl_dataset_namelen(dsl_dataset_t *ds) 6753978Smmusante { 6763978Smmusante int result; 6773978Smmusante 6783978Smmusante if (ds == NULL) { 6793978Smmusante result = 3; /* "mos" */ 6803978Smmusante } else { 6813978Smmusante result = dsl_dir_namelen(ds->ds_dir); 6823978Smmusante VERIFY(0 == dsl_dataset_get_snapname(ds)); 6833978Smmusante if (ds->ds_snapname[0]) { 6843978Smmusante ++result; /* adding one for the @-sign */ 6853978Smmusante if (!MUTEX_HELD(&ds->ds_lock)) { 6863978Smmusante mutex_enter(&ds->ds_lock); 6873978Smmusante result += strlen(ds->ds_snapname); 6883978Smmusante mutex_exit(&ds->ds_lock); 6893978Smmusante } else { 6903978Smmusante result += strlen(ds->ds_snapname); 6913978Smmusante } 6923978Smmusante } 6933978Smmusante } 6943978Smmusante 6953978Smmusante return (result); 6963978Smmusante } 6973978Smmusante 6987046Sahrens void 6996689Smaybee dsl_dataset_drop_ref(dsl_dataset_t *ds, void *tag) 700789Sahrens { 7011544Seschrock dmu_buf_rele(ds->ds_dbuf, tag); 702789Sahrens } 703789Sahrens 704789Sahrens void 7056689Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 7065367Sahrens { 7076689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) { 7086689Smaybee rw_exit(&ds->ds_rwlock); 7096689Smaybee } 7106689Smaybee dsl_dataset_drop_ref(ds, tag); 7116689Smaybee } 7126689Smaybee 7136689Smaybee void 71410298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 7156689Smaybee { 71610298SMatthew.Ahrens@Sun.COM ASSERT((ds->ds_owner == tag && ds->ds_dbuf) || 7176689Smaybee (DSL_DATASET_IS_DESTROYED(ds) && ds->ds_dbuf == NULL)); 7186689Smaybee 7195367Sahrens mutex_enter(&ds->ds_lock); 7206689Smaybee ds->ds_owner = NULL; 7216689Smaybee if (RW_WRITE_HELD(&ds->ds_rwlock)) { 7226689Smaybee rw_exit(&ds->ds_rwlock); 7236689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 7246689Smaybee } 7255367Sahrens mutex_exit(&ds->ds_lock); 7266689Smaybee if (ds->ds_dbuf) 72710298SMatthew.Ahrens@Sun.COM dsl_dataset_drop_ref(ds, tag); 7286689Smaybee else 7296689Smaybee dsl_dataset_evict(ds->ds_dbuf, ds); 7305367Sahrens } 7315367Sahrens 7325367Sahrens boolean_t 73310298SMatthew.Ahrens@Sun.COM dsl_dataset_tryown(dsl_dataset_t *ds, boolean_t inconsistentok, void *tag) 7345367Sahrens { 7356689Smaybee boolean_t gotit = FALSE; 7366689Smaybee 7375367Sahrens mutex_enter(&ds->ds_lock); 7386689Smaybee if (ds->ds_owner == NULL && 7396689Smaybee (!DS_IS_INCONSISTENT(ds) || inconsistentok)) { 74010298SMatthew.Ahrens@Sun.COM ds->ds_owner = tag; 7416689Smaybee if (!dsl_pool_sync_context(ds->ds_dir->dd_pool)) 7426689Smaybee rw_exit(&ds->ds_rwlock); 7436689Smaybee gotit = TRUE; 7445367Sahrens } 7455367Sahrens mutex_exit(&ds->ds_lock); 7466689Smaybee return (gotit); 7476689Smaybee } 7486689Smaybee 7496689Smaybee void 7506689Smaybee dsl_dataset_make_exclusive(dsl_dataset_t *ds, void *owner) 7516689Smaybee { 7526689Smaybee ASSERT3P(owner, ==, ds->ds_owner); 7536689Smaybee if (!RW_WRITE_HELD(&ds->ds_rwlock)) 7546689Smaybee rw_enter(&ds->ds_rwlock, RW_WRITER); 7555367Sahrens } 7565367Sahrens 7572199Sahrens uint64_t 7587046Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 7596492Stimh uint64_t flags, dmu_tx_t *tx) 760789Sahrens { 7615367Sahrens dsl_pool_t *dp = dd->dd_pool; 762789Sahrens dmu_buf_t *dbuf; 763789Sahrens dsl_dataset_phys_t *dsphys; 7645367Sahrens uint64_t dsobj; 765789Sahrens objset_t *mos = dp->dp_meta_objset; 766789Sahrens 7677046Sahrens if (origin == NULL) 7687046Sahrens origin = dp->dp_origin_snap; 7697046Sahrens 7705367Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 7715367Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 772789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 7735367Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 774789Sahrens 775928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 776928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7771544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 778789Sahrens dmu_buf_will_dirty(dbuf, tx); 779789Sahrens dsphys = dbuf->db_data; 7806689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 781789Sahrens dsphys->ds_dir_obj = dd->dd_object; 7826492Stimh dsphys->ds_flags = flags; 783789Sahrens dsphys->ds_fsid_guid = unique_create(); 784789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 785789Sahrens sizeof (dsphys->ds_guid)); 786789Sahrens dsphys->ds_snapnames_zapobj = 7876492Stimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 7886492Stimh DMU_OT_NONE, 0, tx); 789789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 7907046Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 791789Sahrens dsphys->ds_deadlist_obj = 792789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 7935378Sck153898 7945367Sahrens if (origin) { 7955367Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 796789Sahrens dsphys->ds_prev_snap_txg = 7975367Sahrens origin->ds_phys->ds_creation_txg; 798789Sahrens dsphys->ds_used_bytes = 7995367Sahrens origin->ds_phys->ds_used_bytes; 800789Sahrens dsphys->ds_compressed_bytes = 8015367Sahrens origin->ds_phys->ds_compressed_bytes; 802789Sahrens dsphys->ds_uncompressed_bytes = 8035367Sahrens origin->ds_phys->ds_uncompressed_bytes; 8045367Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 8056502Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 806789Sahrens 8075367Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 8085367Sahrens origin->ds_phys->ds_num_children++; 809789Sahrens 8107046Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 8117046Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 8127046Sahrens origin->ds_phys->ds_next_clones_obj = 8137046Sahrens zap_create(mos, 8147046Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 8157046Sahrens } 8167046Sahrens VERIFY(0 == zap_add_int(mos, 8177046Sahrens origin->ds_phys->ds_next_clones_obj, 8187046Sahrens dsobj, tx)); 8197046Sahrens } 8207046Sahrens 821789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 8225367Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 823789Sahrens } 8246492Stimh 8256492Stimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 8266492Stimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 8276492Stimh 8281544Seschrock dmu_buf_rele(dbuf, FTAG); 829789Sahrens 830789Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 831789Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 8325367Sahrens 8335367Sahrens return (dsobj); 8345367Sahrens } 8355367Sahrens 8365367Sahrens uint64_t 8376492Stimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 8386492Stimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8395367Sahrens { 8405367Sahrens dsl_pool_t *dp = pdd->dd_pool; 8415367Sahrens uint64_t dsobj, ddobj; 8425367Sahrens dsl_dir_t *dd; 8435367Sahrens 8445367Sahrens ASSERT(lastname[0] != '@'); 8455367Sahrens 8467046Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8475367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, ddobj, lastname, FTAG, &dd)); 8485367Sahrens 8497046Sahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, flags, tx); 8505367Sahrens 8515367Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8525367Sahrens 853789Sahrens dsl_dir_close(dd, FTAG); 854789Sahrens 8552199Sahrens return (dsobj); 8562199Sahrens } 8572199Sahrens 8582199Sahrens struct destroyarg { 8592199Sahrens dsl_sync_task_group_t *dstg; 8602199Sahrens char *snapname; 8612199Sahrens char *failed; 86210242Schris.kirby@sun.com boolean_t defer; 8632199Sahrens }; 8642199Sahrens 8652199Sahrens static int 86611209SMatthew.Ahrens@Sun.COM dsl_snapshot_destroy_one(const char *name, void *arg) 8672199Sahrens { 8682199Sahrens struct destroyarg *da = arg; 8692199Sahrens dsl_dataset_t *ds; 8702199Sahrens int err; 87110242Schris.kirby@sun.com char *dsname; 87210272SMatthew.Ahrens@Sun.COM 87310272SMatthew.Ahrens@Sun.COM dsname = kmem_asprintf("%s@%s", name, da->snapname); 87410298SMatthew.Ahrens@Sun.COM err = dsl_dataset_own(dsname, B_TRUE, da->dstg, &ds); 87510272SMatthew.Ahrens@Sun.COM strfree(dsname); 8766689Smaybee if (err == 0) { 87710242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda; 87810242Schris.kirby@sun.com 8796689Smaybee dsl_dataset_make_exclusive(ds, da->dstg); 88010298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) { 88110298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 88210298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 8837237Sek110237 } 88410242Schris.kirby@sun.com dsda = kmem_zalloc(sizeof (struct dsl_ds_destroyarg), KM_SLEEP); 88510242Schris.kirby@sun.com dsda->ds = ds; 88610242Schris.kirby@sun.com dsda->defer = da->defer; 8876689Smaybee dsl_sync_task_create(da->dstg, dsl_dataset_destroy_check, 88810242Schris.kirby@sun.com dsl_dataset_destroy_sync, dsda, da->dstg, 0); 8896689Smaybee } else if (err == ENOENT) { 8906689Smaybee err = 0; 8916689Smaybee } else { 8922199Sahrens (void) strcpy(da->failed, name); 8932199Sahrens } 8946689Smaybee return (err); 895789Sahrens } 896789Sahrens 8972199Sahrens /* 8982199Sahrens * Destroy 'snapname' in all descendants of 'fsname'. 8992199Sahrens */ 9002199Sahrens #pragma weak dmu_snapshots_destroy = dsl_snapshots_destroy 9012199Sahrens int 90210242Schris.kirby@sun.com dsl_snapshots_destroy(char *fsname, char *snapname, boolean_t defer) 9032199Sahrens { 9042199Sahrens int err; 9052199Sahrens struct destroyarg da; 9062199Sahrens dsl_sync_task_t *dst; 9072199Sahrens spa_t *spa; 9082199Sahrens 9094603Sahrens err = spa_open(fsname, &spa, FTAG); 9102199Sahrens if (err) 9112199Sahrens return (err); 9122199Sahrens da.dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 9132199Sahrens da.snapname = snapname; 9142199Sahrens da.failed = fsname; 91510242Schris.kirby@sun.com da.defer = defer; 9162199Sahrens 9172199Sahrens err = dmu_objset_find(fsname, 9182417Sahrens dsl_snapshot_destroy_one, &da, DS_FIND_CHILDREN); 9192199Sahrens 9202199Sahrens if (err == 0) 9212199Sahrens err = dsl_sync_task_group_wait(da.dstg); 9222199Sahrens 9232199Sahrens for (dst = list_head(&da.dstg->dstg_tasks); dst; 9242199Sahrens dst = list_next(&da.dstg->dstg_tasks, dst)) { 92510242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = dst->dst_arg1; 92610242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 92710242Schris.kirby@sun.com 9286689Smaybee /* 9296689Smaybee * Return the file system name that triggered the error 9306689Smaybee */ 9312199Sahrens if (dst->dst_err) { 9322199Sahrens dsl_dataset_name(ds, fsname); 9334603Sahrens *strchr(fsname, '@') = '\0'; 9342199Sahrens } 93510242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 9366689Smaybee dsl_dataset_disown(ds, da.dstg); 93710242Schris.kirby@sun.com kmem_free(dsda, sizeof (struct dsl_ds_destroyarg)); 9382199Sahrens } 9392199Sahrens 9402199Sahrens dsl_sync_task_group_destroy(da.dstg); 9412199Sahrens spa_close(spa, FTAG); 9422199Sahrens return (err); 9432199Sahrens } 9442199Sahrens 94510242Schris.kirby@sun.com static boolean_t 94610242Schris.kirby@sun.com dsl_dataset_might_destroy_origin(dsl_dataset_t *ds) 94710242Schris.kirby@sun.com { 94810242Schris.kirby@sun.com boolean_t might_destroy = B_FALSE; 94910242Schris.kirby@sun.com 95010242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 95110242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children == 2 && ds->ds_userrefs == 0 && 95210242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 95310242Schris.kirby@sun.com might_destroy = B_TRUE; 95410242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 95510242Schris.kirby@sun.com 95610242Schris.kirby@sun.com return (might_destroy); 95710242Schris.kirby@sun.com } 95810242Schris.kirby@sun.com 95910242Schris.kirby@sun.com /* 96010242Schris.kirby@sun.com * If we're removing a clone, and these three conditions are true: 96110242Schris.kirby@sun.com * 1) the clone's origin has no other children 96210242Schris.kirby@sun.com * 2) the clone's origin has no user references 96310242Schris.kirby@sun.com * 3) the clone's origin has been marked for deferred destruction 96410242Schris.kirby@sun.com * Then, prepare to remove the origin as part of this sync task group. 96510242Schris.kirby@sun.com */ 96610242Schris.kirby@sun.com static int 96710242Schris.kirby@sun.com dsl_dataset_origin_rm_prep(struct dsl_ds_destroyarg *dsda, void *tag) 96810242Schris.kirby@sun.com { 96910242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 97010242Schris.kirby@sun.com dsl_dataset_t *origin = ds->ds_prev; 97110242Schris.kirby@sun.com 97210242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(origin)) { 97310242Schris.kirby@sun.com char *name; 97410242Schris.kirby@sun.com int namelen; 97510242Schris.kirby@sun.com int error; 97610242Schris.kirby@sun.com 97710242Schris.kirby@sun.com namelen = dsl_dataset_namelen(origin) + 1; 97810242Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 97910242Schris.kirby@sun.com dsl_dataset_name(origin, name); 98010242Schris.kirby@sun.com #ifdef _KERNEL 98110242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 98210242Schris.kirby@sun.com if (error) { 98310242Schris.kirby@sun.com kmem_free(name, namelen); 98410242Schris.kirby@sun.com return (error); 98510242Schris.kirby@sun.com } 98610242Schris.kirby@sun.com #endif 98710298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(name, B_TRUE, tag, &origin); 98810242Schris.kirby@sun.com kmem_free(name, namelen); 98910242Schris.kirby@sun.com if (error) 99010242Schris.kirby@sun.com return (error); 99110242Schris.kirby@sun.com dsda->rm_origin = origin; 99210242Schris.kirby@sun.com dsl_dataset_make_exclusive(origin, tag); 99310342Schris.kirby@sun.com 99410342Schris.kirby@sun.com if (origin->ds_objset != NULL) { 99510342Schris.kirby@sun.com dmu_objset_evict(origin->ds_objset); 99610342Schris.kirby@sun.com origin->ds_objset = NULL; 99710342Schris.kirby@sun.com } 99810242Schris.kirby@sun.com } 99910242Schris.kirby@sun.com 100010242Schris.kirby@sun.com return (0); 100110242Schris.kirby@sun.com } 100210242Schris.kirby@sun.com 10035367Sahrens /* 10046689Smaybee * ds must be opened as OWNER. On return (whether successful or not), 10056689Smaybee * ds will be closed and caller can no longer dereference it. 10065367Sahrens */ 1007789Sahrens int 100810242Schris.kirby@sun.com dsl_dataset_destroy(dsl_dataset_t *ds, void *tag, boolean_t defer) 1009789Sahrens { 1010789Sahrens int err; 10112199Sahrens dsl_sync_task_group_t *dstg; 10122199Sahrens objset_t *os; 1013789Sahrens dsl_dir_t *dd; 10142199Sahrens uint64_t obj; 101511022STom.Erickson@Sun.COM struct dsl_ds_destroyarg dsda = { 0 }; 101611022STom.Erickson@Sun.COM dsl_dataset_t dummy_ds = { 0 }; 101710242Schris.kirby@sun.com 101810242Schris.kirby@sun.com dsda.ds = ds; 10192199Sahrens 10205367Sahrens if (dsl_dataset_is_snapshot(ds)) { 10212199Sahrens /* Destroying a snapshot is simpler */ 10226689Smaybee dsl_dataset_make_exclusive(ds, tag); 10237237Sek110237 102410298SMatthew.Ahrens@Sun.COM if (ds->ds_objset != NULL) { 102510298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 102610298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 10277237Sek110237 } 102810242Schris.kirby@sun.com dsda.defer = defer; 10292199Sahrens err = dsl_sync_task_do(ds->ds_dir->dd_pool, 10302199Sahrens dsl_dataset_destroy_check, dsl_dataset_destroy_sync, 103110242Schris.kirby@sun.com &dsda, tag, 0); 103210242Schris.kirby@sun.com ASSERT3P(dsda.rm_origin, ==, NULL); 10335367Sahrens goto out; 103410385Schris.kirby@sun.com } else if (defer) { 103510385Schris.kirby@sun.com err = EINVAL; 103610385Schris.kirby@sun.com goto out; 10372199Sahrens } 10382199Sahrens 10392199Sahrens dd = ds->ds_dir; 104011022STom.Erickson@Sun.COM dummy_ds.ds_dir = dd; 104111022STom.Erickson@Sun.COM dummy_ds.ds_object = ds->ds_object; 1042789Sahrens 10432199Sahrens /* 10442199Sahrens * Check for errors and mark this ds as inconsistent, in 10452199Sahrens * case we crash while freeing the objects. 10462199Sahrens */ 10472199Sahrens err = dsl_sync_task_do(dd->dd_pool, dsl_dataset_destroy_begin_check, 10482199Sahrens dsl_dataset_destroy_begin_sync, ds, NULL, 0); 10495367Sahrens if (err) 10505367Sahrens goto out; 10515367Sahrens 105210298SMatthew.Ahrens@Sun.COM err = dmu_objset_from_ds(ds, &os); 10535367Sahrens if (err) 10545367Sahrens goto out; 10552199Sahrens 10562199Sahrens /* 10572199Sahrens * remove the objects in open context, so that we won't 10582199Sahrens * have too much to do in syncing context. 10592199Sahrens */ 10603025Sahrens for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 10613025Sahrens ds->ds_phys->ds_prev_snap_txg)) { 10626992Smaybee /* 10636992Smaybee * Ignore errors, if there is not enough disk space 10646992Smaybee * we will deal with it in dsl_dataset_destroy_sync(). 10656992Smaybee */ 10666992Smaybee (void) dmu_free_object(os, obj); 10672199Sahrens } 10682199Sahrens 10699396SMatthew.Ahrens@Sun.COM /* 10709396SMatthew.Ahrens@Sun.COM * We need to sync out all in-flight IO before we try to evict 10719396SMatthew.Ahrens@Sun.COM * (the dataset evict func is trying to clear the cached entries 10729396SMatthew.Ahrens@Sun.COM * for this dataset in the ARC). 10739396SMatthew.Ahrens@Sun.COM */ 10749396SMatthew.Ahrens@Sun.COM txg_wait_synced(dd->dd_pool, 0); 10759396SMatthew.Ahrens@Sun.COM 10769396SMatthew.Ahrens@Sun.COM /* 10779396SMatthew.Ahrens@Sun.COM * If we managed to free all the objects in open 10789396SMatthew.Ahrens@Sun.COM * context, the user space accounting should be zero. 10799396SMatthew.Ahrens@Sun.COM */ 10809396SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_bp.blk_fill == 0 && 108110298SMatthew.Ahrens@Sun.COM dmu_objset_userused_enabled(os)) { 10829396SMatthew.Ahrens@Sun.COM uint64_t count; 10839396SMatthew.Ahrens@Sun.COM 10849396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_USERUSED_OBJECT, &count) != 0 || 10859396SMatthew.Ahrens@Sun.COM count == 0); 10869396SMatthew.Ahrens@Sun.COM ASSERT(zap_count(os, DMU_GROUPUSED_OBJECT, &count) != 0 || 10879396SMatthew.Ahrens@Sun.COM count == 0); 10889396SMatthew.Ahrens@Sun.COM } 10899396SMatthew.Ahrens@Sun.COM 10902199Sahrens if (err != ESRCH) 10915367Sahrens goto out; 10922199Sahrens 10936975Smaybee rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 10946975Smaybee err = dsl_dir_open_obj(dd->dd_pool, dd->dd_object, NULL, FTAG, &dd); 10956975Smaybee rw_exit(&dd->dd_pool->dp_config_rwlock); 10966975Smaybee 10976975Smaybee if (err) 10986975Smaybee goto out; 10996975Smaybee 110010298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 11016689Smaybee /* 11026689Smaybee * We need to sync out all in-flight IO before we try 11036689Smaybee * to evict (the dataset evict func is trying to clear 11046689Smaybee * the cached entries for this dataset in the ARC). 11056689Smaybee */ 11066689Smaybee txg_wait_synced(dd->dd_pool, 0); 11075367Sahrens } 11085367Sahrens 11092199Sahrens /* 11102199Sahrens * Blow away the dsl_dir + head dataset. 11112199Sahrens */ 11126689Smaybee dsl_dataset_make_exclusive(ds, tag); 111310298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 111410298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 111510298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 11166975Smaybee } 111710242Schris.kirby@sun.com 111810242Schris.kirby@sun.com /* 111910242Schris.kirby@sun.com * If we're removing a clone, we might also need to remove its 112010242Schris.kirby@sun.com * origin. 112110242Schris.kirby@sun.com */ 112210242Schris.kirby@sun.com do { 112310242Schris.kirby@sun.com dsda.need_prep = B_FALSE; 112410242Schris.kirby@sun.com if (dsl_dir_is_clone(dd)) { 112510242Schris.kirby@sun.com err = dsl_dataset_origin_rm_prep(&dsda, tag); 112610242Schris.kirby@sun.com if (err) { 112710242Schris.kirby@sun.com dsl_dir_close(dd, FTAG); 112810242Schris.kirby@sun.com goto out; 112910242Schris.kirby@sun.com } 113010242Schris.kirby@sun.com } 113110242Schris.kirby@sun.com 113210242Schris.kirby@sun.com dstg = dsl_sync_task_group_create(ds->ds_dir->dd_pool); 113310242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dataset_destroy_check, 113410242Schris.kirby@sun.com dsl_dataset_destroy_sync, &dsda, tag, 0); 113510242Schris.kirby@sun.com dsl_sync_task_create(dstg, dsl_dir_destroy_check, 113611022STom.Erickson@Sun.COM dsl_dir_destroy_sync, &dummy_ds, FTAG, 0); 113710242Schris.kirby@sun.com err = dsl_sync_task_group_wait(dstg); 113810242Schris.kirby@sun.com dsl_sync_task_group_destroy(dstg); 113910242Schris.kirby@sun.com 114010242Schris.kirby@sun.com /* 114110242Schris.kirby@sun.com * We could be racing against 'zfs release' or 'zfs destroy -d' 114210242Schris.kirby@sun.com * on the origin snap, in which case we can get EBUSY if we 114310242Schris.kirby@sun.com * needed to destroy the origin snap but were not ready to 114410242Schris.kirby@sun.com * do so. 114510242Schris.kirby@sun.com */ 114610242Schris.kirby@sun.com if (dsda.need_prep) { 114710242Schris.kirby@sun.com ASSERT(err == EBUSY); 114810242Schris.kirby@sun.com ASSERT(dsl_dir_is_clone(dd)); 114910242Schris.kirby@sun.com ASSERT(dsda.rm_origin == NULL); 115010242Schris.kirby@sun.com } 115110242Schris.kirby@sun.com } while (dsda.need_prep); 115210242Schris.kirby@sun.com 115310242Schris.kirby@sun.com if (dsda.rm_origin != NULL) 115410242Schris.kirby@sun.com dsl_dataset_disown(dsda.rm_origin, tag); 115510242Schris.kirby@sun.com 11566689Smaybee /* if it is successful, dsl_dir_destroy_sync will close the dd */ 11575367Sahrens if (err) 11582199Sahrens dsl_dir_close(dd, FTAG); 11595367Sahrens out: 11606689Smaybee dsl_dataset_disown(ds, tag); 1161789Sahrens return (err); 1162789Sahrens } 1163789Sahrens 11643547Smaybee blkptr_t * 11653547Smaybee dsl_dataset_get_blkptr(dsl_dataset_t *ds) 1166789Sahrens { 11673547Smaybee return (&ds->ds_phys->ds_bp); 1168789Sahrens } 1169789Sahrens 1170789Sahrens void 1171789Sahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 1172789Sahrens { 1173789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 1174789Sahrens /* If it's the meta-objset, set dp_meta_rootbp */ 1175789Sahrens if (ds == NULL) { 1176789Sahrens tx->tx_pool->dp_meta_rootbp = *bp; 1177789Sahrens } else { 1178789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1179789Sahrens ds->ds_phys->ds_bp = *bp; 1180789Sahrens } 1181789Sahrens } 1182789Sahrens 1183789Sahrens spa_t * 1184789Sahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 1185789Sahrens { 1186789Sahrens return (ds->ds_dir->dd_pool->dp_spa); 1187789Sahrens } 1188789Sahrens 1189789Sahrens void 1190789Sahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1191789Sahrens { 1192789Sahrens dsl_pool_t *dp; 1193789Sahrens 1194789Sahrens if (ds == NULL) /* this is the meta-objset */ 1195789Sahrens return; 1196789Sahrens 119710298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 11982885Sahrens 11992885Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) 12002885Sahrens panic("dirtying snapshot!"); 1201789Sahrens 1202789Sahrens dp = ds->ds_dir->dd_pool; 1203789Sahrens 1204789Sahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg) == 0) { 1205789Sahrens /* up the hold count until we can be written out */ 1206789Sahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 1207789Sahrens } 1208789Sahrens } 1209789Sahrens 12105378Sck153898 /* 12115378Sck153898 * The unique space in the head dataset can be calculated by subtracting 12125378Sck153898 * the space used in the most recent snapshot, that is still being used 12135378Sck153898 * in this file system, from the space currently in use. To figure out 12145378Sck153898 * the space in the most recent snapshot still in use, we need to take 12155378Sck153898 * the total space used in the snapshot and subtract out the space that 12165378Sck153898 * has been freed up since the snapshot was taken. 12175378Sck153898 */ 12185378Sck153898 static void 12195378Sck153898 dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 12205378Sck153898 { 12215378Sck153898 uint64_t mrs_used; 12225378Sck153898 uint64_t dlused, dlcomp, dluncomp; 12235378Sck153898 12245378Sck153898 ASSERT(ds->ds_object == ds->ds_dir->dd_phys->dd_head_dataset_obj); 12255378Sck153898 12265378Sck153898 if (ds->ds_phys->ds_prev_snap_obj != 0) 12275378Sck153898 mrs_used = ds->ds_prev->ds_phys->ds_used_bytes; 12285378Sck153898 else 12295378Sck153898 mrs_used = 0; 12305378Sck153898 12315378Sck153898 VERIFY(0 == bplist_space(&ds->ds_deadlist, &dlused, &dlcomp, 12325378Sck153898 &dluncomp)); 12335378Sck153898 12345378Sck153898 ASSERT3U(dlused, <=, mrs_used); 12355378Sck153898 ds->ds_phys->ds_unique_bytes = 12365378Sck153898 ds->ds_phys->ds_used_bytes - (mrs_used - dlused); 12375378Sck153898 12385378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && 12395378Sck153898 spa_version(ds->ds_dir->dd_pool->dp_spa) >= 12405378Sck153898 SPA_VERSION_UNIQUE_ACCURATE) 12415378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 12425378Sck153898 } 12435378Sck153898 12445378Sck153898 static uint64_t 12455378Sck153898 dsl_dataset_unique(dsl_dataset_t *ds) 12465378Sck153898 { 12475378Sck153898 if (!DS_UNIQUE_IS_ACCURATE(ds) && !dsl_dataset_is_snapshot(ds)) 12485378Sck153898 dsl_dataset_recalc_head_uniq(ds); 12495378Sck153898 12505378Sck153898 return (ds->ds_phys->ds_unique_bytes); 12515378Sck153898 } 12525378Sck153898 1253789Sahrens struct killarg { 12547390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 1255789Sahrens dmu_tx_t *tx; 1256789Sahrens }; 1257789Sahrens 12587390SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 1259789Sahrens static int 126010922SJeff.Bonwick@Sun.COM kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 126110922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 1262789Sahrens { 1263789Sahrens struct killarg *ka = arg; 126410922SJeff.Bonwick@Sun.COM dmu_tx_t *tx = ka->tx; 12657837SMatthew.Ahrens@Sun.COM 12667837SMatthew.Ahrens@Sun.COM if (bp == NULL) 12677837SMatthew.Ahrens@Sun.COM return (0); 1268789Sahrens 126910922SJeff.Bonwick@Sun.COM if (zb->zb_level == ZB_ZIL_LEVEL) { 127010922SJeff.Bonwick@Sun.COM ASSERT(zilog != NULL); 12718746SMatthew.Ahrens@Sun.COM /* 12728746SMatthew.Ahrens@Sun.COM * It's a block in the intent log. It has no 12738746SMatthew.Ahrens@Sun.COM * accounting, so just free it. 12748746SMatthew.Ahrens@Sun.COM */ 127510922SJeff.Bonwick@Sun.COM dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp); 12768746SMatthew.Ahrens@Sun.COM } else { 127710922SJeff.Bonwick@Sun.COM ASSERT(zilog == NULL); 12788746SMatthew.Ahrens@Sun.COM ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg); 127910922SJeff.Bonwick@Sun.COM (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE); 12808746SMatthew.Ahrens@Sun.COM } 12817390SMatthew.Ahrens@Sun.COM 1282789Sahrens return (0); 1283789Sahrens } 1284789Sahrens 1285789Sahrens /* ARGSUSED */ 12862199Sahrens static int 12872199Sahrens dsl_dataset_destroy_begin_check(void *arg1, void *arg2, dmu_tx_t *tx) 12881731Sbonwick { 12892199Sahrens dsl_dataset_t *ds = arg1; 12905367Sahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 12915367Sahrens uint64_t count; 12925367Sahrens int err; 12931731Sbonwick 12941731Sbonwick /* 12951731Sbonwick * Can't delete a head dataset if there are snapshots of it. 12961731Sbonwick * (Except if the only snapshots are from the branch we cloned 12971731Sbonwick * from.) 12981731Sbonwick */ 12991731Sbonwick if (ds->ds_prev != NULL && 13001731Sbonwick ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 130110816SVitezslav.Batrla@Sun.COM return (EBUSY); 13021731Sbonwick 13035367Sahrens /* 13045367Sahrens * This is really a dsl_dir thing, but check it here so that 13055367Sahrens * we'll be less likely to leave this dataset inconsistent & 13065367Sahrens * nearly destroyed. 13075367Sahrens */ 13085367Sahrens err = zap_count(mos, ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count); 13095367Sahrens if (err) 13105367Sahrens return (err); 13115367Sahrens if (count != 0) 13125367Sahrens return (EEXIST); 13135367Sahrens 13141731Sbonwick return (0); 13151731Sbonwick } 13161731Sbonwick 13172199Sahrens /* ARGSUSED */ 13182199Sahrens static void 13194543Smarks dsl_dataset_destroy_begin_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1320789Sahrens { 13212199Sahrens dsl_dataset_t *ds = arg1; 13224543Smarks dsl_pool_t *dp = ds->ds_dir->dd_pool; 1323789Sahrens 13242199Sahrens /* Mark it as inconsistent on-disk, in case we crash */ 13252199Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13262199Sahrens ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT; 13274543Smarks 13284543Smarks spa_history_internal_log(LOG_DS_DESTROY_BEGIN, dp->dp_spa, tx, 13294543Smarks cr, "dataset = %llu", ds->ds_object); 13302199Sahrens } 1331789Sahrens 133210242Schris.kirby@sun.com static int 133310242Schris.kirby@sun.com dsl_dataset_origin_check(struct dsl_ds_destroyarg *dsda, void *tag, 133410242Schris.kirby@sun.com dmu_tx_t *tx) 133510242Schris.kirby@sun.com { 133610242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 133710242Schris.kirby@sun.com dsl_dataset_t *ds_prev = ds->ds_prev; 133810242Schris.kirby@sun.com 133910242Schris.kirby@sun.com if (dsl_dataset_might_destroy_origin(ds_prev)) { 134010242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 134110242Schris.kirby@sun.com 134210242Schris.kirby@sun.com /* 134310242Schris.kirby@sun.com * If we're not prepared to remove the origin, don't remove 134410242Schris.kirby@sun.com * the clone either. 134510242Schris.kirby@sun.com */ 134610242Schris.kirby@sun.com if (dsda->rm_origin == NULL) { 134710242Schris.kirby@sun.com dsda->need_prep = B_TRUE; 134810242Schris.kirby@sun.com return (EBUSY); 134910242Schris.kirby@sun.com } 135010242Schris.kirby@sun.com 135110242Schris.kirby@sun.com ndsda.ds = ds_prev; 135210242Schris.kirby@sun.com ndsda.is_origin_rm = B_TRUE; 135310242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&ndsda, tag, tx)); 135410242Schris.kirby@sun.com } 135510242Schris.kirby@sun.com 135610242Schris.kirby@sun.com /* 135710242Schris.kirby@sun.com * If we're not going to remove the origin after all, 135810242Schris.kirby@sun.com * undo the open context setup. 135910242Schris.kirby@sun.com */ 136010242Schris.kirby@sun.com if (dsda->rm_origin != NULL) { 136110242Schris.kirby@sun.com dsl_dataset_disown(dsda->rm_origin, tag); 136210242Schris.kirby@sun.com dsda->rm_origin = NULL; 136310242Schris.kirby@sun.com } 136410242Schris.kirby@sun.com 136510242Schris.kirby@sun.com return (0); 136610242Schris.kirby@sun.com } 136710242Schris.kirby@sun.com 13682199Sahrens /* ARGSUSED */ 13695367Sahrens int 13702199Sahrens dsl_dataset_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) 13712199Sahrens { 137210242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 137310242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 1374789Sahrens 13756689Smaybee /* we have an owner hold, so noone else can destroy us */ 13766689Smaybee ASSERT(!DSL_DATASET_IS_DESTROYED(ds)); 13776689Smaybee 137810242Schris.kirby@sun.com /* 137910242Schris.kirby@sun.com * Only allow deferred destroy on pools that support it. 138010242Schris.kirby@sun.com * NOTE: deferred destroy is only supported on snapshots. 138110242Schris.kirby@sun.com */ 138210242Schris.kirby@sun.com if (dsda->defer) { 138310242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 138410242Schris.kirby@sun.com SPA_VERSION_USERREFS) 138510242Schris.kirby@sun.com return (ENOTSUP); 138610242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 138710242Schris.kirby@sun.com return (0); 138810242Schris.kirby@sun.com } 1389789Sahrens 1390789Sahrens /* 1391789Sahrens * Can't delete a head dataset if there are snapshots of it. 1392789Sahrens * (Except if the only snapshots are from the branch we cloned 1393789Sahrens * from.) 1394789Sahrens */ 1395789Sahrens if (ds->ds_prev != NULL && 13962199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) 139710816SVitezslav.Batrla@Sun.COM return (EBUSY); 1398789Sahrens 1399789Sahrens /* 1400789Sahrens * If we made changes this txg, traverse_dsl_dataset won't find 1401789Sahrens * them. Try again. 1402789Sahrens */ 14032199Sahrens if (ds->ds_phys->ds_bp.blk_birth >= tx->tx_txg) 1404789Sahrens return (EAGAIN); 14052199Sahrens 140610242Schris.kirby@sun.com if (dsl_dataset_is_snapshot(ds)) { 140710242Schris.kirby@sun.com /* 140810242Schris.kirby@sun.com * If this snapshot has an elevated user reference count, 140910242Schris.kirby@sun.com * we can't destroy it yet. 141010242Schris.kirby@sun.com */ 141110242Schris.kirby@sun.com if (ds->ds_userrefs > 0 && !dsda->releasing) 141210242Schris.kirby@sun.com return (EBUSY); 141310242Schris.kirby@sun.com 141410242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 141510242Schris.kirby@sun.com /* 141610242Schris.kirby@sun.com * Can't delete a branch point. However, if we're destroying 141710242Schris.kirby@sun.com * a clone and removing its origin due to it having a user 141810242Schris.kirby@sun.com * hold count of 0 and having been marked for deferred destroy, 141910242Schris.kirby@sun.com * it's OK for the origin to have a single clone. 142010242Schris.kirby@sun.com */ 142110242Schris.kirby@sun.com if (ds->ds_phys->ds_num_children > 142210242Schris.kirby@sun.com (dsda->is_origin_rm ? 2 : 1)) { 142310242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 142410242Schris.kirby@sun.com return (EEXIST); 142510242Schris.kirby@sun.com } 142610242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 142710242Schris.kirby@sun.com } else if (dsl_dir_is_clone(ds->ds_dir)) { 142810242Schris.kirby@sun.com return (dsl_dataset_origin_check(dsda, arg2, tx)); 142910242Schris.kirby@sun.com } 143010242Schris.kirby@sun.com 14312199Sahrens /* XXX we should do some i/o error checking... */ 14322199Sahrens return (0); 14332199Sahrens } 14342199Sahrens 14356689Smaybee struct refsarg { 14366689Smaybee kmutex_t lock; 14376689Smaybee boolean_t gone; 14386689Smaybee kcondvar_t cv; 14396689Smaybee }; 14406689Smaybee 14416689Smaybee /* ARGSUSED */ 14426689Smaybee static void 14436689Smaybee dsl_dataset_refs_gone(dmu_buf_t *db, void *argv) 14446689Smaybee { 14456689Smaybee struct refsarg *arg = argv; 14466689Smaybee 14476689Smaybee mutex_enter(&arg->lock); 14486689Smaybee arg->gone = TRUE; 14496689Smaybee cv_signal(&arg->cv); 14506689Smaybee mutex_exit(&arg->lock); 14516689Smaybee } 14526689Smaybee 14536689Smaybee static void 14546689Smaybee dsl_dataset_drain_refs(dsl_dataset_t *ds, void *tag) 14556689Smaybee { 14566689Smaybee struct refsarg arg; 14576689Smaybee 14586689Smaybee mutex_init(&arg.lock, NULL, MUTEX_DEFAULT, NULL); 14596689Smaybee cv_init(&arg.cv, NULL, CV_DEFAULT, NULL); 14606689Smaybee arg.gone = FALSE; 14616689Smaybee (void) dmu_buf_update_user(ds->ds_dbuf, ds, &arg, &ds->ds_phys, 14626689Smaybee dsl_dataset_refs_gone); 14636689Smaybee dmu_buf_rele(ds->ds_dbuf, tag); 14646689Smaybee mutex_enter(&arg.lock); 14656689Smaybee while (!arg.gone) 14666689Smaybee cv_wait(&arg.cv, &arg.lock); 14676689Smaybee ASSERT(arg.gone); 14686689Smaybee mutex_exit(&arg.lock); 14696689Smaybee ds->ds_dbuf = NULL; 14706689Smaybee ds->ds_phys = NULL; 14716689Smaybee mutex_destroy(&arg.lock); 14726689Smaybee cv_destroy(&arg.cv); 14736689Smaybee } 14746689Smaybee 147510801SMatthew.Ahrens@Sun.COM static void 147610801SMatthew.Ahrens@Sun.COM remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, dmu_tx_t *tx) 147710801SMatthew.Ahrens@Sun.COM { 147810801SMatthew.Ahrens@Sun.COM objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 147910801SMatthew.Ahrens@Sun.COM uint64_t count; 148010801SMatthew.Ahrens@Sun.COM int err; 148110801SMatthew.Ahrens@Sun.COM 148210801SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_phys->ds_num_children >= 2); 148310801SMatthew.Ahrens@Sun.COM err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 148410801SMatthew.Ahrens@Sun.COM /* 148510801SMatthew.Ahrens@Sun.COM * The err should not be ENOENT, but a bug in a previous version 148610801SMatthew.Ahrens@Sun.COM * of the code could cause upgrade_clones_cb() to not set 148710801SMatthew.Ahrens@Sun.COM * ds_next_snap_obj when it should, leading to a missing entry. 148810801SMatthew.Ahrens@Sun.COM * If we knew that the pool was created after 148910801SMatthew.Ahrens@Sun.COM * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 149010801SMatthew.Ahrens@Sun.COM * ENOENT. However, at least we can check that we don't have 149110801SMatthew.Ahrens@Sun.COM * too many entries in the next_clones_obj even after failing to 149210801SMatthew.Ahrens@Sun.COM * remove this one. 149310801SMatthew.Ahrens@Sun.COM */ 149410801SMatthew.Ahrens@Sun.COM if (err != ENOENT) { 149510801SMatthew.Ahrens@Sun.COM VERIFY3U(err, ==, 0); 149610801SMatthew.Ahrens@Sun.COM } 149710801SMatthew.Ahrens@Sun.COM ASSERT3U(0, ==, zap_count(mos, ds->ds_phys->ds_next_clones_obj, 149810801SMatthew.Ahrens@Sun.COM &count)); 149910801SMatthew.Ahrens@Sun.COM ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 150010801SMatthew.Ahrens@Sun.COM } 150110801SMatthew.Ahrens@Sun.COM 15025367Sahrens void 15034543Smarks dsl_dataset_destroy_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 15042199Sahrens { 150510242Schris.kirby@sun.com struct dsl_ds_destroyarg *dsda = arg1; 150610242Schris.kirby@sun.com dsl_dataset_t *ds = dsda->ds; 15072199Sahrens int err; 15082199Sahrens int after_branch_point = FALSE; 15092199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 15102199Sahrens objset_t *mos = dp->dp_meta_objset; 15112199Sahrens dsl_dataset_t *ds_prev = NULL; 15122199Sahrens uint64_t obj; 15132199Sahrens 15146689Smaybee ASSERT(ds->ds_owner); 151510242Schris.kirby@sun.com ASSERT(dsda->defer || ds->ds_phys->ds_num_children <= 1); 15162199Sahrens ASSERT(ds->ds_prev == NULL || 15172199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object); 15182199Sahrens ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg); 15192199Sahrens 152010242Schris.kirby@sun.com if (dsda->defer) { 152110242Schris.kirby@sun.com ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS); 152210242Schris.kirby@sun.com if (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1) { 152310242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 152410242Schris.kirby@sun.com ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY; 152510242Schris.kirby@sun.com return; 152610242Schris.kirby@sun.com } 152710242Schris.kirby@sun.com } 152810242Schris.kirby@sun.com 15296689Smaybee /* signal any waiters that this dataset is going away */ 15306689Smaybee mutex_enter(&ds->ds_lock); 15316689Smaybee ds->ds_owner = dsl_reaper; 15326689Smaybee cv_broadcast(&ds->ds_exclusive_cv); 15336689Smaybee mutex_exit(&ds->ds_lock); 15346689Smaybee 15355378Sck153898 /* Remove our reservation */ 15365378Sck153898 if (ds->ds_reserved != 0) { 153711022STom.Erickson@Sun.COM dsl_prop_setarg_t psa; 153811022STom.Erickson@Sun.COM uint64_t value = 0; 153911022STom.Erickson@Sun.COM 154011022STom.Erickson@Sun.COM dsl_prop_setarg_init_uint64(&psa, "refreservation", 154111022STom.Erickson@Sun.COM (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED), 154211022STom.Erickson@Sun.COM &value); 154311022STom.Erickson@Sun.COM psa.psa_effective_value = 0; /* predict default value */ 154411022STom.Erickson@Sun.COM 154511022STom.Erickson@Sun.COM dsl_dataset_set_reservation_sync(ds, &psa, cr, tx); 15465378Sck153898 ASSERT3U(ds->ds_reserved, ==, 0); 15475378Sck153898 } 15485378Sck153898 15492199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 15502199Sahrens 15517046Sahrens dsl_pool_ds_destroyed(ds, tx); 15527046Sahrens 15532199Sahrens obj = ds->ds_object; 1554789Sahrens 1555789Sahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 1556789Sahrens if (ds->ds_prev) { 1557789Sahrens ds_prev = ds->ds_prev; 1558789Sahrens } else { 15596689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 15606689Smaybee ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev)); 1561789Sahrens } 1562789Sahrens after_branch_point = 1563789Sahrens (ds_prev->ds_phys->ds_next_snap_obj != obj); 1564789Sahrens 1565789Sahrens dmu_buf_will_dirty(ds_prev->ds_dbuf, tx); 1566789Sahrens if (after_branch_point && 15677046Sahrens ds_prev->ds_phys->ds_next_clones_obj != 0) { 156810801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds_prev, obj, tx); 15697046Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 15707046Sahrens VERIFY(0 == zap_add_int(mos, 15717046Sahrens ds_prev->ds_phys->ds_next_clones_obj, 15727046Sahrens ds->ds_phys->ds_next_snap_obj, tx)); 15737046Sahrens } 15747046Sahrens } 15757046Sahrens if (after_branch_point && 1576789Sahrens ds->ds_phys->ds_next_snap_obj == 0) { 1577789Sahrens /* This clone is toast. */ 1578789Sahrens ASSERT(ds_prev->ds_phys->ds_num_children > 1); 1579789Sahrens ds_prev->ds_phys->ds_num_children--; 158010242Schris.kirby@sun.com 158110242Schris.kirby@sun.com /* 158210242Schris.kirby@sun.com * If the clone's origin has no other clones, no 158310242Schris.kirby@sun.com * user holds, and has been marked for deferred 158410242Schris.kirby@sun.com * deletion, then we should have done the necessary 158510242Schris.kirby@sun.com * destroy setup for it. 158610242Schris.kirby@sun.com */ 158710242Schris.kirby@sun.com if (ds_prev->ds_phys->ds_num_children == 1 && 158810242Schris.kirby@sun.com ds_prev->ds_userrefs == 0 && 158910242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds_prev)) { 159010242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, !=, NULL); 159110242Schris.kirby@sun.com } else { 159210242Schris.kirby@sun.com ASSERT3P(dsda->rm_origin, ==, NULL); 159310242Schris.kirby@sun.com } 1594789Sahrens } else if (!after_branch_point) { 1595789Sahrens ds_prev->ds_phys->ds_next_snap_obj = 1596789Sahrens ds->ds_phys->ds_next_snap_obj; 1597789Sahrens } 1598789Sahrens } 1599789Sahrens 1600789Sahrens if (ds->ds_phys->ds_next_snap_obj != 0) { 16012199Sahrens blkptr_t bp; 1602789Sahrens dsl_dataset_t *ds_next; 1603789Sahrens uint64_t itor = 0; 16045378Sck153898 uint64_t old_unique; 16057390SMatthew.Ahrens@Sun.COM int64_t used = 0, compressed = 0, uncompressed = 0; 1606789Sahrens 16076689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16086689Smaybee ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next)); 1609789Sahrens ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj); 1610789Sahrens 16115378Sck153898 old_unique = dsl_dataset_unique(ds_next); 16125378Sck153898 1613789Sahrens dmu_buf_will_dirty(ds_next->ds_dbuf, tx); 1614789Sahrens ds_next->ds_phys->ds_prev_snap_obj = 1615789Sahrens ds->ds_phys->ds_prev_snap_obj; 1616789Sahrens ds_next->ds_phys->ds_prev_snap_txg = 1617789Sahrens ds->ds_phys->ds_prev_snap_txg; 1618789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 1619789Sahrens ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0); 1620789Sahrens 1621789Sahrens /* 1622789Sahrens * Transfer to our deadlist (which will become next's 1623789Sahrens * new deadlist) any entries from next's current 1624789Sahrens * deadlist which were born before prev, and free the 1625789Sahrens * other entries. 1626789Sahrens * 1627789Sahrens * XXX we're doing this long task with the config lock held 1628789Sahrens */ 16296689Smaybee while (bplist_iterate(&ds_next->ds_deadlist, &itor, &bp) == 0) { 1630789Sahrens if (bp.blk_birth <= ds->ds_phys->ds_prev_snap_txg) { 16311544Seschrock VERIFY(0 == bplist_enqueue(&ds->ds_deadlist, 16321544Seschrock &bp, tx)); 1633789Sahrens if (ds_prev && !after_branch_point && 1634789Sahrens bp.blk_birth > 1635789Sahrens ds_prev->ds_phys->ds_prev_snap_txg) { 1636789Sahrens ds_prev->ds_phys->ds_unique_bytes += 163710922SJeff.Bonwick@Sun.COM bp_get_dsize_sync(dp->dp_spa, &bp); 1638789Sahrens } 1639789Sahrens } else { 164010922SJeff.Bonwick@Sun.COM used += bp_get_dsize_sync(dp->dp_spa, &bp); 1641789Sahrens compressed += BP_GET_PSIZE(&bp); 1642789Sahrens uncompressed += BP_GET_UCSIZE(&bp); 164310922SJeff.Bonwick@Sun.COM dsl_free(dp, tx->tx_txg, &bp); 1644789Sahrens } 1645789Sahrens } 1646789Sahrens 16477390SMatthew.Ahrens@Sun.COM ASSERT3U(used, ==, ds->ds_phys->ds_unique_bytes); 16487390SMatthew.Ahrens@Sun.COM 16497390SMatthew.Ahrens@Sun.COM /* change snapused */ 16507390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP, 16517390SMatthew.Ahrens@Sun.COM -used, -compressed, -uncompressed, tx); 16527390SMatthew.Ahrens@Sun.COM 1653789Sahrens /* free next's deadlist */ 1654789Sahrens bplist_close(&ds_next->ds_deadlist); 1655789Sahrens bplist_destroy(mos, ds_next->ds_phys->ds_deadlist_obj, tx); 1656789Sahrens 1657789Sahrens /* set next's deadlist to our deadlist */ 16586689Smaybee bplist_close(&ds->ds_deadlist); 1659789Sahrens ds_next->ds_phys->ds_deadlist_obj = 1660789Sahrens ds->ds_phys->ds_deadlist_obj; 16611544Seschrock VERIFY(0 == bplist_open(&ds_next->ds_deadlist, mos, 16621544Seschrock ds_next->ds_phys->ds_deadlist_obj)); 1663789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1664789Sahrens 1665789Sahrens if (ds_next->ds_phys->ds_next_snap_obj != 0) { 1666789Sahrens /* 1667789Sahrens * Update next's unique to include blocks which 1668789Sahrens * were previously shared by only this snapshot 1669789Sahrens * and it. Those blocks will be born after the 1670789Sahrens * prev snap and before this snap, and will have 1671789Sahrens * died after the next snap and before the one 1672789Sahrens * after that (ie. be on the snap after next's 1673789Sahrens * deadlist). 1674789Sahrens * 1675789Sahrens * XXX we're doing this long task with the 1676789Sahrens * config lock held 1677789Sahrens */ 1678789Sahrens dsl_dataset_t *ds_after_next; 16797390SMatthew.Ahrens@Sun.COM uint64_t space; 1680789Sahrens 16816689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 16826689Smaybee ds_next->ds_phys->ds_next_snap_obj, 16836689Smaybee FTAG, &ds_after_next)); 16847390SMatthew.Ahrens@Sun.COM 16857390SMatthew.Ahrens@Sun.COM VERIFY(0 == 16867390SMatthew.Ahrens@Sun.COM bplist_space_birthrange(&ds_after_next->ds_deadlist, 16877390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_prev_snap_txg, 16887390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_creation_txg, &space)); 16897390SMatthew.Ahrens@Sun.COM ds_next->ds_phys->ds_unique_bytes += space; 1690789Sahrens 16916689Smaybee dsl_dataset_rele(ds_after_next, FTAG); 1692789Sahrens ASSERT3P(ds_next->ds_prev, ==, NULL); 1693789Sahrens } else { 1694789Sahrens ASSERT3P(ds_next->ds_prev, ==, ds); 16956689Smaybee dsl_dataset_drop_ref(ds_next->ds_prev, ds_next); 16966689Smaybee ds_next->ds_prev = NULL; 1697789Sahrens if (ds_prev) { 16986689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 16996689Smaybee ds->ds_phys->ds_prev_snap_obj, 17006689Smaybee ds_next, &ds_next->ds_prev)); 1701789Sahrens } 17025378Sck153898 17035378Sck153898 dsl_dataset_recalc_head_uniq(ds_next); 17045378Sck153898 17055378Sck153898 /* 17065378Sck153898 * Reduce the amount of our unconsmed refreservation 17075378Sck153898 * being charged to our parent by the amount of 17085378Sck153898 * new unique data we have gained. 17095378Sck153898 */ 17105378Sck153898 if (old_unique < ds_next->ds_reserved) { 17115378Sck153898 int64_t mrsdelta; 17125378Sck153898 uint64_t new_unique = 17135378Sck153898 ds_next->ds_phys->ds_unique_bytes; 17145378Sck153898 17155378Sck153898 ASSERT(old_unique <= new_unique); 17165378Sck153898 mrsdelta = MIN(new_unique - old_unique, 17175378Sck153898 ds_next->ds_reserved - old_unique); 17187390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, 17197390SMatthew.Ahrens@Sun.COM DD_USED_REFRSRV, -mrsdelta, 0, 0, tx); 17205378Sck153898 } 1721789Sahrens } 17226689Smaybee dsl_dataset_rele(ds_next, FTAG); 1723789Sahrens } else { 1724789Sahrens /* 1725789Sahrens * There's no next snapshot, so this is a head dataset. 1726789Sahrens * Destroy the deadlist. Unless it's a clone, the 1727789Sahrens * deadlist should be empty. (If it's a clone, it's 1728789Sahrens * safe to ignore the deadlist contents.) 1729789Sahrens */ 1730789Sahrens struct killarg ka; 1731789Sahrens 1732789Sahrens ASSERT(after_branch_point || bplist_empty(&ds->ds_deadlist)); 1733789Sahrens bplist_close(&ds->ds_deadlist); 1734789Sahrens bplist_destroy(mos, ds->ds_phys->ds_deadlist_obj, tx); 1735789Sahrens ds->ds_phys->ds_deadlist_obj = 0; 1736789Sahrens 1737789Sahrens /* 1738789Sahrens * Free everything that we point to (that's born after 1739789Sahrens * the previous snapshot, if we are a clone) 1740789Sahrens * 17417390SMatthew.Ahrens@Sun.COM * NB: this should be very quick, because we already 17427390SMatthew.Ahrens@Sun.COM * freed all the objects in open context. 1743789Sahrens */ 17447390SMatthew.Ahrens@Sun.COM ka.ds = ds; 1745789Sahrens ka.tx = tx; 17467837SMatthew.Ahrens@Sun.COM err = traverse_dataset(ds, ds->ds_phys->ds_prev_snap_txg, 17477837SMatthew.Ahrens@Sun.COM TRAVERSE_POST, kill_blkptr, &ka); 1748789Sahrens ASSERT3U(err, ==, 0); 17499390Schris.kirby@sun.com ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || 17507390SMatthew.Ahrens@Sun.COM ds->ds_phys->ds_unique_bytes == 0); 175110342Schris.kirby@sun.com 175210342Schris.kirby@sun.com if (ds->ds_prev != NULL) { 175310342Schris.kirby@sun.com dsl_dataset_rele(ds->ds_prev, ds); 175410342Schris.kirby@sun.com ds->ds_prev = ds_prev = NULL; 175510342Schris.kirby@sun.com } 1756789Sahrens } 1757789Sahrens 17586689Smaybee if (ds->ds_dir->dd_phys->dd_head_dataset_obj == ds->ds_object) { 17596689Smaybee /* Erase the link in the dir */ 17606689Smaybee dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); 17616689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj = 0; 17626689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0); 1763789Sahrens err = zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx); 1764789Sahrens ASSERT(err == 0); 1765789Sahrens } else { 1766789Sahrens /* remove from snapshot namespace */ 1767789Sahrens dsl_dataset_t *ds_head; 17686689Smaybee ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0); 17696689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dp, 17706689Smaybee ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head)); 17712207Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 1772789Sahrens #ifdef ZFS_DEBUG 1773789Sahrens { 1774789Sahrens uint64_t val; 17756492Stimh 17766689Smaybee err = dsl_dataset_snap_lookup(ds_head, 17776492Stimh ds->ds_snapname, &val); 1778789Sahrens ASSERT3U(err, ==, 0); 1779789Sahrens ASSERT3U(val, ==, obj); 1780789Sahrens } 1781789Sahrens #endif 17826689Smaybee err = dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx); 1783789Sahrens ASSERT(err == 0); 17846689Smaybee dsl_dataset_rele(ds_head, FTAG); 1785789Sahrens } 1786789Sahrens 1787789Sahrens if (ds_prev && ds->ds_prev != ds_prev) 17886689Smaybee dsl_dataset_rele(ds_prev, FTAG); 1789789Sahrens 17905094Slling spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx); 17914543Smarks spa_history_internal_log(LOG_DS_DESTROY, dp->dp_spa, tx, 17924543Smarks cr, "dataset = %llu", ds->ds_object); 17934543Smarks 17947046Sahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 17957046Sahrens uint64_t count; 17967046Sahrens ASSERT(0 == zap_count(mos, 17977046Sahrens ds->ds_phys->ds_next_clones_obj, &count) && count == 0); 17987046Sahrens VERIFY(0 == dmu_object_free(mos, 17997046Sahrens ds->ds_phys->ds_next_clones_obj, tx)); 18007046Sahrens } 18017390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_props_obj != 0) 18027390SMatthew.Ahrens@Sun.COM VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_props_obj, tx)); 180310242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) 180410242Schris.kirby@sun.com VERIFY(0 == zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx)); 18056689Smaybee dsl_dir_close(ds->ds_dir, ds); 18066689Smaybee ds->ds_dir = NULL; 18076689Smaybee dsl_dataset_drain_refs(ds, tag); 18082199Sahrens VERIFY(0 == dmu_object_free(mos, obj, tx)); 180910242Schris.kirby@sun.com 181010242Schris.kirby@sun.com if (dsda->rm_origin) { 181110242Schris.kirby@sun.com /* 181210242Schris.kirby@sun.com * Remove the origin of the clone we just destroyed. 181310242Schris.kirby@sun.com */ 181410242Schris.kirby@sun.com struct dsl_ds_destroyarg ndsda = {0}; 181510242Schris.kirby@sun.com 181610342Schris.kirby@sun.com ndsda.ds = dsda->rm_origin; 181710242Schris.kirby@sun.com dsl_dataset_destroy_sync(&ndsda, tag, cr, tx); 181810242Schris.kirby@sun.com } 18192199Sahrens } 18202199Sahrens 18215378Sck153898 static int 18225378Sck153898 dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 18235378Sck153898 { 18245378Sck153898 uint64_t asize; 18255378Sck153898 18265378Sck153898 if (!dmu_tx_is_syncing(tx)) 18275378Sck153898 return (0); 18285378Sck153898 18295378Sck153898 /* 18305378Sck153898 * If there's an fs-only reservation, any blocks that might become 18315378Sck153898 * owned by the snapshot dataset must be accommodated by space 18325378Sck153898 * outside of the reservation. 18335378Sck153898 */ 18345378Sck153898 asize = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 18355378Sck153898 if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, FALSE)) 18365378Sck153898 return (ENOSPC); 18375378Sck153898 18385378Sck153898 /* 18395378Sck153898 * Propogate any reserved space for this snapshot to other 18405378Sck153898 * snapshot checks in this sync group. 18415378Sck153898 */ 18425378Sck153898 if (asize > 0) 18435378Sck153898 dsl_dir_willuse_space(ds->ds_dir, asize, tx); 18445378Sck153898 18455378Sck153898 return (0); 18465378Sck153898 } 18475378Sck153898 18482199Sahrens /* ARGSUSED */ 18492199Sahrens int 18502199Sahrens dsl_dataset_snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx) 18512199Sahrens { 18525367Sahrens dsl_dataset_t *ds = arg1; 18532199Sahrens const char *snapname = arg2; 18542199Sahrens int err; 18552199Sahrens uint64_t value; 1856789Sahrens 1857789Sahrens /* 18582199Sahrens * We don't allow multiple snapshots of the same txg. If there 18592199Sahrens * is already one, try again. 18602199Sahrens */ 18612199Sahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 18622199Sahrens return (EAGAIN); 18632199Sahrens 18642199Sahrens /* 18652199Sahrens * Check for conflicting name snapshot name. 1866789Sahrens */ 18676689Smaybee err = dsl_dataset_snap_lookup(ds, snapname, &value); 18682199Sahrens if (err == 0) 18692199Sahrens return (EEXIST); 18702199Sahrens if (err != ENOENT) 18712199Sahrens return (err); 1872789Sahrens 18733978Smmusante /* 18743978Smmusante * Check that the dataset's name is not too long. Name consists 18753978Smmusante * of the dataset's length + 1 for the @-sign + snapshot name's length 18763978Smmusante */ 18773978Smmusante if (dsl_dataset_namelen(ds) + 1 + strlen(snapname) >= MAXNAMELEN) 18783978Smmusante return (ENAMETOOLONG); 18793978Smmusante 18805378Sck153898 err = dsl_dataset_snapshot_reserve_space(ds, tx); 18815378Sck153898 if (err) 18825378Sck153898 return (err); 18835378Sck153898 18842199Sahrens ds->ds_trysnap_txg = tx->tx_txg; 1885789Sahrens return (0); 1886789Sahrens } 1887789Sahrens 18882199Sahrens void 18894543Smarks dsl_dataset_snapshot_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1890789Sahrens { 18915367Sahrens dsl_dataset_t *ds = arg1; 18922199Sahrens const char *snapname = arg2; 18932199Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1894789Sahrens dmu_buf_t *dbuf; 1895789Sahrens dsl_dataset_phys_t *dsphys; 18967046Sahrens uint64_t dsobj, crtxg; 1897789Sahrens objset_t *mos = dp->dp_meta_objset; 1898789Sahrens int err; 1899789Sahrens 19002199Sahrens ASSERT(RW_WRITE_HELD(&dp->dp_config_rwlock)); 1901789Sahrens 19027046Sahrens /* 19037046Sahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 19047046Sahrens */ 19057046Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 19067046Sahrens crtxg = 1; 19077046Sahrens else 19087046Sahrens crtxg = tx->tx_txg; 19097046Sahrens 1910928Stabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 1911928Stabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 19121544Seschrock VERIFY(0 == dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1913789Sahrens dmu_buf_will_dirty(dbuf, tx); 1914789Sahrens dsphys = dbuf->db_data; 19156689Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 19162199Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1917789Sahrens dsphys->ds_fsid_guid = unique_create(); 1918789Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1919789Sahrens sizeof (dsphys->ds_guid)); 1920789Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1921789Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1922789Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1923789Sahrens dsphys->ds_num_children = 1; 1924789Sahrens dsphys->ds_creation_time = gethrestime_sec(); 19257046Sahrens dsphys->ds_creation_txg = crtxg; 1926789Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1927789Sahrens dsphys->ds_used_bytes = ds->ds_phys->ds_used_bytes; 1928789Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1929789Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 19302082Seschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1931789Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 19321544Seschrock dmu_buf_rele(dbuf, FTAG); 1933789Sahrens 19342199Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 19352199Sahrens if (ds->ds_prev) { 19367046Sahrens uint64_t next_clones_obj = 19377046Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 19382199Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1939789Sahrens ds->ds_object || 19402199Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 19412199Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 19422199Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1943789Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 19442199Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 19452199Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 19467046Sahrens } else if (next_clones_obj != 0) { 194710801SMatthew.Ahrens@Sun.COM remove_from_next_clones(ds->ds_prev, 194810801SMatthew.Ahrens@Sun.COM dsphys->ds_next_snap_obj, tx); 19497046Sahrens VERIFY3U(0, ==, zap_add_int(mos, 19507046Sahrens next_clones_obj, dsobj, tx)); 1951789Sahrens } 1952789Sahrens } 1953789Sahrens 19545378Sck153898 /* 19555378Sck153898 * If we have a reference-reservation on this dataset, we will 19565378Sck153898 * need to increase the amount of refreservation being charged 19575378Sck153898 * since our unique space is going to zero. 19585378Sck153898 */ 19595378Sck153898 if (ds->ds_reserved) { 19605378Sck153898 int64_t add = MIN(dsl_dataset_unique(ds), ds->ds_reserved); 19617390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 19627390SMatthew.Ahrens@Sun.COM add, 0, 0, tx); 19635378Sck153898 } 19645378Sck153898 1965789Sahrens bplist_close(&ds->ds_deadlist); 1966789Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 19675712Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1968789Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 19697046Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1970789Sahrens ds->ds_phys->ds_unique_bytes = 0; 19715378Sck153898 if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 19725378Sck153898 ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1973789Sahrens ds->ds_phys->ds_deadlist_obj = 1974789Sahrens bplist_create(mos, DSL_DEADLIST_BLOCKSIZE, tx); 19751544Seschrock VERIFY(0 == bplist_open(&ds->ds_deadlist, mos, 19761544Seschrock ds->ds_phys->ds_deadlist_obj)); 1977789Sahrens 1978789Sahrens dprintf("snap '%s' -> obj %llu\n", snapname, dsobj); 1979789Sahrens err = zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 1980789Sahrens snapname, 8, 1, &dsobj, tx); 1981789Sahrens ASSERT(err == 0); 1982789Sahrens 1983789Sahrens if (ds->ds_prev) 19846689Smaybee dsl_dataset_drop_ref(ds->ds_prev, ds); 19856689Smaybee VERIFY(0 == dsl_dataset_get_ref(dp, 19866689Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 19874543Smarks 19887046Sahrens dsl_pool_ds_snapshotted(ds, tx); 19897046Sahrens 199010373Schris.kirby@sun.com dsl_dir_snap_cmtime_update(ds->ds_dir); 199110373Schris.kirby@sun.com 19924543Smarks spa_history_internal_log(LOG_DS_SNAPSHOT, dp->dp_spa, tx, cr, 19934603Sahrens "dataset = %llu", dsobj); 1994789Sahrens } 1995789Sahrens 1996789Sahrens void 19973547Smaybee dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 1998789Sahrens { 1999789Sahrens ASSERT(dmu_tx_is_syncing(tx)); 200010298SMatthew.Ahrens@Sun.COM ASSERT(ds->ds_objset != NULL); 2001789Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 2002789Sahrens 20034787Sahrens /* 20044787Sahrens * in case we had to change ds_fsid_guid when we opened it, 20054787Sahrens * sync it out now. 20064787Sahrens */ 20074787Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 20084787Sahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 20094787Sahrens 2010789Sahrens dsl_dir_dirty(ds->ds_dir, tx); 201110298SMatthew.Ahrens@Sun.COM dmu_objset_sync(ds->ds_objset, zio, tx); 2012789Sahrens } 2013789Sahrens 2014789Sahrens void 20152885Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 2016789Sahrens { 20175378Sck153898 uint64_t refd, avail, uobjs, aobjs; 20185378Sck153898 20192885Sahrens dsl_dir_stats(ds->ds_dir, nv); 2020789Sahrens 20215378Sck153898 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 20225378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 20235378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 20245378Sck153898 20252885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 20262885Sahrens ds->ds_phys->ds_creation_time); 20272885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 20282885Sahrens ds->ds_phys->ds_creation_txg); 20295378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 20305378Sck153898 ds->ds_quota); 20315378Sck153898 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 20325378Sck153898 ds->ds_reserved); 20336643Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 20346643Seschrock ds->ds_phys->ds_guid); 203510575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 203610575SEric.Schrock@Sun.COM dsl_dataset_unique(ds)); 203710575SEric.Schrock@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 203810575SEric.Schrock@Sun.COM ds->ds_object); 203911022STom.Erickson@Sun.COM dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 204011022STom.Erickson@Sun.COM ds->ds_userrefs); 204110242Schris.kirby@sun.com dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 204210242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 2043789Sahrens 2044789Sahrens if (ds->ds_phys->ds_next_snap_obj) { 2045789Sahrens /* 2046789Sahrens * This is a snapshot; override the dd's space used with 20472885Sahrens * our unique space and compression ratio. 2048789Sahrens */ 20492885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 20502885Sahrens ds->ds_phys->ds_unique_bytes); 20512885Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, 20522885Sahrens ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 20532885Sahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 20542885Sahrens ds->ds_phys->ds_compressed_bytes)); 2055789Sahrens } 2056789Sahrens } 2057789Sahrens 20582885Sahrens void 20592885Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 2060789Sahrens { 20612885Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 20622885Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 20635367Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 20642885Sahrens if (ds->ds_phys->ds_next_snap_obj) { 20652885Sahrens stat->dds_is_snapshot = B_TRUE; 20662885Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 20678228SEric.Taylor@Sun.COM } else { 20688228SEric.Taylor@Sun.COM stat->dds_is_snapshot = B_FALSE; 20698228SEric.Taylor@Sun.COM stat->dds_num_clones = 0; 20702885Sahrens } 20712885Sahrens 20722885Sahrens /* clone origin is really a dsl_dir thing... */ 20735446Sahrens rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER); 20747046Sahrens if (dsl_dir_is_clone(ds->ds_dir)) { 20752885Sahrens dsl_dataset_t *ods; 20762885Sahrens 20776689Smaybee VERIFY(0 == dsl_dataset_get_ref(ds->ds_dir->dd_pool, 20786689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 20795367Sahrens dsl_dataset_name(ods, stat->dds_origin); 20806689Smaybee dsl_dataset_drop_ref(ods, FTAG); 20818228SEric.Taylor@Sun.COM } else { 20828228SEric.Taylor@Sun.COM stat->dds_origin[0] = '\0'; 20832885Sahrens } 20845446Sahrens rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock); 20852885Sahrens } 20862885Sahrens 20872885Sahrens uint64_t 20882885Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 20892885Sahrens { 20904787Sahrens return (ds->ds_fsid_guid); 20912885Sahrens } 20922885Sahrens 20932885Sahrens void 20942885Sahrens dsl_dataset_space(dsl_dataset_t *ds, 20952885Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 20962885Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 20972885Sahrens { 20982885Sahrens *refdbytesp = ds->ds_phys->ds_used_bytes; 20992885Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 21005378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 21015378Sck153898 *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 21025378Sck153898 if (ds->ds_quota != 0) { 21035378Sck153898 /* 21045378Sck153898 * Adjust available bytes according to refquota 21055378Sck153898 */ 21065378Sck153898 if (*refdbytesp < ds->ds_quota) 21075378Sck153898 *availbytesp = MIN(*availbytesp, 21085378Sck153898 ds->ds_quota - *refdbytesp); 21095378Sck153898 else 21105378Sck153898 *availbytesp = 0; 21115378Sck153898 } 21122885Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 21132885Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 2114789Sahrens } 2115789Sahrens 21165326Sek110237 boolean_t 21175326Sek110237 dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 21185326Sek110237 { 21195326Sek110237 dsl_pool_t *dp = ds->ds_dir->dd_pool; 21205326Sek110237 21215326Sek110237 ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || 21225326Sek110237 dsl_pool_sync_context(dp)); 21235326Sek110237 if (ds->ds_prev == NULL) 21245326Sek110237 return (B_FALSE); 21255326Sek110237 if (ds->ds_phys->ds_bp.blk_birth > 21265326Sek110237 ds->ds_prev->ds_phys->ds_creation_txg) 21275326Sek110237 return (B_TRUE); 21285326Sek110237 return (B_FALSE); 21295326Sek110237 } 21305326Sek110237 21312199Sahrens /* ARGSUSED */ 2132789Sahrens static int 21332199Sahrens dsl_dataset_snapshot_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) 2134789Sahrens { 21352199Sahrens dsl_dataset_t *ds = arg1; 21362199Sahrens char *newsnapname = arg2; 21372199Sahrens dsl_dir_t *dd = ds->ds_dir; 21382199Sahrens dsl_dataset_t *hds; 21392199Sahrens uint64_t val; 2140789Sahrens int err; 2141789Sahrens 21426689Smaybee err = dsl_dataset_hold_obj(dd->dd_pool, 21436689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds); 2144789Sahrens if (err) 2145789Sahrens return (err); 2146789Sahrens 21472199Sahrens /* new name better not be in use */ 21486689Smaybee err = dsl_dataset_snap_lookup(hds, newsnapname, &val); 21496689Smaybee dsl_dataset_rele(hds, FTAG); 2150789Sahrens 21512199Sahrens if (err == 0) 21522199Sahrens err = EEXIST; 21532199Sahrens else if (err == ENOENT) 21542199Sahrens err = 0; 21554007Smmusante 21564007Smmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 21574007Smmusante if (dsl_dir_namelen(ds->ds_dir) + 1 + strlen(newsnapname) >= MAXNAMELEN) 21584007Smmusante err = ENAMETOOLONG; 21594007Smmusante 21602199Sahrens return (err); 21612199Sahrens } 2162789Sahrens 21632199Sahrens static void 21644543Smarks dsl_dataset_snapshot_rename_sync(void *arg1, void *arg2, 21654543Smarks cred_t *cr, dmu_tx_t *tx) 21662199Sahrens { 21672199Sahrens dsl_dataset_t *ds = arg1; 21684543Smarks const char *newsnapname = arg2; 21692199Sahrens dsl_dir_t *dd = ds->ds_dir; 21702199Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset; 21712199Sahrens dsl_dataset_t *hds; 21722199Sahrens int err; 2173789Sahrens 21742199Sahrens ASSERT(ds->ds_phys->ds_next_snap_obj != 0); 2175789Sahrens 21766689Smaybee VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, 21776689Smaybee dd->dd_phys->dd_head_dataset_obj, FTAG, &hds)); 2178789Sahrens 21792199Sahrens VERIFY(0 == dsl_dataset_get_snapname(ds)); 21806689Smaybee err = dsl_dataset_snap_remove(hds, ds->ds_snapname, tx); 2181789Sahrens ASSERT3U(err, ==, 0); 21822199Sahrens mutex_enter(&ds->ds_lock); 21832199Sahrens (void) strcpy(ds->ds_snapname, newsnapname); 21842199Sahrens mutex_exit(&ds->ds_lock); 21852199Sahrens err = zap_add(mos, hds->ds_phys->ds_snapnames_zapobj, 21862199Sahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx); 2187789Sahrens ASSERT3U(err, ==, 0); 2188789Sahrens 21894543Smarks spa_history_internal_log(LOG_DS_RENAME, dd->dd_pool->dp_spa, tx, 21904543Smarks cr, "dataset = %llu", ds->ds_object); 21916689Smaybee dsl_dataset_rele(hds, FTAG); 2192789Sahrens } 2193789Sahrens 21945326Sek110237 struct renamesnaparg { 21954007Smmusante dsl_sync_task_group_t *dstg; 21964007Smmusante char failed[MAXPATHLEN]; 21974007Smmusante char *oldsnap; 21984007Smmusante char *newsnap; 21994007Smmusante }; 22004007Smmusante 22014007Smmusante static int 220211209SMatthew.Ahrens@Sun.COM dsl_snapshot_rename_one(const char *name, void *arg) 22034007Smmusante { 22045326Sek110237 struct renamesnaparg *ra = arg; 22054007Smmusante dsl_dataset_t *ds = NULL; 220611209SMatthew.Ahrens@Sun.COM char *snapname; 22074007Smmusante int err; 22084007Smmusante 220911209SMatthew.Ahrens@Sun.COM snapname = kmem_asprintf("%s@%s", name, ra->oldsnap); 221011209SMatthew.Ahrens@Sun.COM (void) strlcpy(ra->failed, snapname, sizeof (ra->failed)); 22114543Smarks 22124543Smarks /* 22134543Smarks * For recursive snapshot renames the parent won't be changing 22144543Smarks * so we just pass name for both the to/from argument. 22154543Smarks */ 221611209SMatthew.Ahrens@Sun.COM err = zfs_secpolicy_rename_perms(snapname, snapname, CRED()); 221711209SMatthew.Ahrens@Sun.COM if (err != 0) { 221811209SMatthew.Ahrens@Sun.COM strfree(snapname); 221911209SMatthew.Ahrens@Sun.COM return (err == ENOENT ? 0 : err); 22204543Smarks } 22214543Smarks 22226689Smaybee #ifdef _KERNEL 22236689Smaybee /* 22246689Smaybee * For all filesystems undergoing rename, we'll need to unmount it. 22256689Smaybee */ 222611209SMatthew.Ahrens@Sun.COM (void) zfs_unmount_snap(snapname, NULL); 22276689Smaybee #endif 222811209SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold(snapname, ra->dstg, &ds); 222911609SMatthew.Ahrens@Sun.COM strfree(snapname); 223011609SMatthew.Ahrens@Sun.COM if (err != 0) 223111209SMatthew.Ahrens@Sun.COM return (err == ENOENT ? 0 : err); 22324007Smmusante 22334007Smmusante dsl_sync_task_create(ra->dstg, dsl_dataset_snapshot_rename_check, 22344007Smmusante dsl_dataset_snapshot_rename_sync, ds, ra->newsnap, 0); 22354007Smmusante 22364007Smmusante return (0); 22374007Smmusante } 22384007Smmusante 22394007Smmusante static int 22404007Smmusante dsl_recursive_rename(char *oldname, const char *newname) 22414007Smmusante { 22424007Smmusante int err; 22435326Sek110237 struct renamesnaparg *ra; 22444007Smmusante dsl_sync_task_t *dst; 22454007Smmusante spa_t *spa; 22464007Smmusante char *cp, *fsname = spa_strdup(oldname); 224711209SMatthew.Ahrens@Sun.COM int len = strlen(oldname) + 1; 22484007Smmusante 22494007Smmusante /* truncate the snapshot name to get the fsname */ 22504007Smmusante cp = strchr(fsname, '@'); 22514007Smmusante *cp = '\0'; 22524007Smmusante 22534603Sahrens err = spa_open(fsname, &spa, FTAG); 22544007Smmusante if (err) { 225511209SMatthew.Ahrens@Sun.COM kmem_free(fsname, len); 22564007Smmusante return (err); 22574007Smmusante } 22585326Sek110237 ra = kmem_alloc(sizeof (struct renamesnaparg), KM_SLEEP); 22594007Smmusante ra->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 22604007Smmusante 22614007Smmusante ra->oldsnap = strchr(oldname, '@') + 1; 22624007Smmusante ra->newsnap = strchr(newname, '@') + 1; 22634007Smmusante *ra->failed = '\0'; 22644007Smmusante 22654007Smmusante err = dmu_objset_find(fsname, dsl_snapshot_rename_one, ra, 22664007Smmusante DS_FIND_CHILDREN); 226711209SMatthew.Ahrens@Sun.COM kmem_free(fsname, len); 22684007Smmusante 22694007Smmusante if (err == 0) { 22704007Smmusante err = dsl_sync_task_group_wait(ra->dstg); 22714007Smmusante } 22724007Smmusante 22734007Smmusante for (dst = list_head(&ra->dstg->dstg_tasks); dst; 22744007Smmusante dst = list_next(&ra->dstg->dstg_tasks, dst)) { 22754007Smmusante dsl_dataset_t *ds = dst->dst_arg1; 22764007Smmusante if (dst->dst_err) { 22774007Smmusante dsl_dir_name(ds->ds_dir, ra->failed); 227811209SMatthew.Ahrens@Sun.COM (void) strlcat(ra->failed, "@", sizeof (ra->failed)); 227911209SMatthew.Ahrens@Sun.COM (void) strlcat(ra->failed, ra->newsnap, 228011209SMatthew.Ahrens@Sun.COM sizeof (ra->failed)); 22814007Smmusante } 22826689Smaybee dsl_dataset_rele(ds, ra->dstg); 22834007Smmusante } 22844007Smmusante 22854543Smarks if (err) 228611209SMatthew.Ahrens@Sun.COM (void) strlcpy(oldname, ra->failed, sizeof (ra->failed)); 22874007Smmusante 22884007Smmusante dsl_sync_task_group_destroy(ra->dstg); 22895326Sek110237 kmem_free(ra, sizeof (struct renamesnaparg)); 22904007Smmusante spa_close(spa, FTAG); 22914007Smmusante return (err); 22924007Smmusante } 22934007Smmusante 22944569Smmusante static int 229511209SMatthew.Ahrens@Sun.COM dsl_valid_rename(const char *oldname, void *arg) 22964569Smmusante { 22974569Smmusante int delta = *(int *)arg; 22984569Smmusante 22994569Smmusante if (strlen(oldname) + delta >= MAXNAMELEN) 23004569Smmusante return (ENAMETOOLONG); 23014569Smmusante 23024569Smmusante return (0); 23034569Smmusante } 23044569Smmusante 2305789Sahrens #pragma weak dmu_objset_rename = dsl_dataset_rename 2306789Sahrens int 23076689Smaybee dsl_dataset_rename(char *oldname, const char *newname, boolean_t recursive) 2308789Sahrens { 2309789Sahrens dsl_dir_t *dd; 23102199Sahrens dsl_dataset_t *ds; 2311789Sahrens const char *tail; 2312789Sahrens int err; 2313789Sahrens 23142199Sahrens err = dsl_dir_open(oldname, FTAG, &dd, &tail); 23151544Seschrock if (err) 23161544Seschrock return (err); 231711701SSanjeev.Bagewadi@Sun.COM 2318789Sahrens if (tail == NULL) { 23194569Smmusante int delta = strlen(newname) - strlen(oldname); 23204569Smmusante 23217046Sahrens /* if we're growing, validate child name lengths */ 23224569Smmusante if (delta > 0) 23234569Smmusante err = dmu_objset_find(oldname, dsl_valid_rename, 23244569Smmusante &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); 23254569Smmusante 2326*11823SMatthew.Ahrens@Sun.COM if (err == 0) 23274569Smmusante err = dsl_dir_rename(dd, newname); 2328789Sahrens dsl_dir_close(dd, FTAG); 2329789Sahrens return (err); 2330789Sahrens } 233111701SSanjeev.Bagewadi@Sun.COM 2332789Sahrens if (tail[0] != '@') { 233310588SEric.Taylor@Sun.COM /* the name ended in a nonexistent component */ 2334789Sahrens dsl_dir_close(dd, FTAG); 2335789Sahrens return (ENOENT); 2336789Sahrens } 2337789Sahrens 23382199Sahrens dsl_dir_close(dd, FTAG); 23392199Sahrens 23402199Sahrens /* new name must be snapshot in same filesystem */ 23412199Sahrens tail = strchr(newname, '@'); 23422199Sahrens if (tail == NULL) 23432199Sahrens return (EINVAL); 23442199Sahrens tail++; 23452199Sahrens if (strncmp(oldname, newname, tail - newname) != 0) 23462199Sahrens return (EXDEV); 2347789Sahrens 23484007Smmusante if (recursive) { 23494007Smmusante err = dsl_recursive_rename(oldname, newname); 23504007Smmusante } else { 23516689Smaybee err = dsl_dataset_hold(oldname, FTAG, &ds); 23524007Smmusante if (err) 23534007Smmusante return (err); 23542199Sahrens 23554007Smmusante err = dsl_sync_task_do(ds->ds_dir->dd_pool, 23564007Smmusante dsl_dataset_snapshot_rename_check, 23574007Smmusante dsl_dataset_snapshot_rename_sync, ds, (char *)tail, 1); 23582199Sahrens 23596689Smaybee dsl_dataset_rele(ds, FTAG); 23604007Smmusante } 23612199Sahrens 2362789Sahrens return (err); 2363789Sahrens } 23642082Seschrock 23657046Sahrens struct promotenode { 23666689Smaybee list_node_t link; 23676689Smaybee dsl_dataset_t *ds; 23686689Smaybee }; 23696689Smaybee 23702199Sahrens struct promotearg { 23717390SMatthew.Ahrens@Sun.COM list_t shared_snaps, origin_snaps, clone_snaps; 23727390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_origin, *origin_head; 23737390SMatthew.Ahrens@Sun.COM uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 237410588SEric.Taylor@Sun.COM char *err_ds; 23752199Sahrens }; 23762199Sahrens 23777390SMatthew.Ahrens@Sun.COM static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 23787390SMatthew.Ahrens@Sun.COM 23794543Smarks /* ARGSUSED */ 23802082Seschrock static int 23812199Sahrens dsl_dataset_promote_check(void *arg1, void *arg2, dmu_tx_t *tx) 23822082Seschrock { 23832199Sahrens dsl_dataset_t *hds = arg1; 23842199Sahrens struct promotearg *pa = arg2; 23857390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 23866689Smaybee dsl_dataset_t *origin_ds = snap->ds; 23876689Smaybee int err; 23882199Sahrens 23897046Sahrens /* Check that it is a real clone */ 23907046Sahrens if (!dsl_dir_is_clone(hds->ds_dir)) 23912082Seschrock return (EINVAL); 23922082Seschrock 23932199Sahrens /* Since this is so expensive, don't do the preliminary check */ 23942199Sahrens if (!dmu_tx_is_syncing(tx)) 23952199Sahrens return (0); 23962199Sahrens 23976689Smaybee if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) 23986689Smaybee return (EXDEV); 23992082Seschrock 24005367Sahrens /* compute origin's new unique space */ 24017390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 24027390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 24037390SMatthew.Ahrens@Sun.COM err = bplist_space_birthrange(&snap->ds->ds_deadlist, 24047390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, &pa->unique); 24057390SMatthew.Ahrens@Sun.COM if (err) 24066689Smaybee return (err); 24076689Smaybee 24086689Smaybee /* 24096689Smaybee * Walk the snapshots that we are moving 24106689Smaybee * 24117390SMatthew.Ahrens@Sun.COM * Compute space to transfer. Consider the incremental changes 24127390SMatthew.Ahrens@Sun.COM * to used for each snapshot: 24137390SMatthew.Ahrens@Sun.COM * (my used) = (prev's used) + (blocks born) - (blocks killed) 24147390SMatthew.Ahrens@Sun.COM * So each snapshot gave birth to: 24157390SMatthew.Ahrens@Sun.COM * (blocks born) = (my used) - (prev's used) + (blocks killed) 24166689Smaybee * So a sequence would look like: 24177390SMatthew.Ahrens@Sun.COM * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 24186689Smaybee * Which simplifies to: 24197390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + k1 + k0 24206689Smaybee * Note however, if we stop before we reach the ORIGIN we get: 24217390SMatthew.Ahrens@Sun.COM * uN + kN + kN-1 + ... + kM - uM-1 24226689Smaybee */ 24236689Smaybee pa->used = origin_ds->ds_phys->ds_used_bytes; 24246689Smaybee pa->comp = origin_ds->ds_phys->ds_compressed_bytes; 24256689Smaybee pa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 24267390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 24277390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 24282082Seschrock uint64_t val, dlused, dlcomp, dluncomp; 24296689Smaybee dsl_dataset_t *ds = snap->ds; 24302082Seschrock 24312082Seschrock /* Check that the snapshot name does not conflict */ 24327390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 24336689Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 243410588SEric.Taylor@Sun.COM if (err == 0) { 243510588SEric.Taylor@Sun.COM err = EEXIST; 243610588SEric.Taylor@Sun.COM goto out; 243710588SEric.Taylor@Sun.COM } 24386689Smaybee if (err != ENOENT) 243910588SEric.Taylor@Sun.COM goto out; 24406689Smaybee 24416689Smaybee /* The very first snapshot does not have a deadlist */ 24427390SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_obj == 0) 24437390SMatthew.Ahrens@Sun.COM continue; 24447390SMatthew.Ahrens@Sun.COM 24457390SMatthew.Ahrens@Sun.COM if (err = bplist_space(&ds->ds_deadlist, 24467390SMatthew.Ahrens@Sun.COM &dlused, &dlcomp, &dluncomp)) 244710588SEric.Taylor@Sun.COM goto out; 24487390SMatthew.Ahrens@Sun.COM pa->used += dlused; 24497390SMatthew.Ahrens@Sun.COM pa->comp += dlcomp; 24507390SMatthew.Ahrens@Sun.COM pa->uncomp += dluncomp; 24517390SMatthew.Ahrens@Sun.COM } 24526689Smaybee 24536689Smaybee /* 24546689Smaybee * If we are a clone of a clone then we never reached ORIGIN, 24556689Smaybee * so we need to subtract out the clone origin's used space. 24566689Smaybee */ 24577390SMatthew.Ahrens@Sun.COM if (pa->origin_origin) { 24587390SMatthew.Ahrens@Sun.COM pa->used -= pa->origin_origin->ds_phys->ds_used_bytes; 24597390SMatthew.Ahrens@Sun.COM pa->comp -= pa->origin_origin->ds_phys->ds_compressed_bytes; 24607390SMatthew.Ahrens@Sun.COM pa->uncomp -= pa->origin_origin->ds_phys->ds_uncompressed_bytes; 24612082Seschrock } 24622082Seschrock 24632082Seschrock /* Check that there is enough space here */ 24647390SMatthew.Ahrens@Sun.COM err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 24657390SMatthew.Ahrens@Sun.COM pa->used); 24667390SMatthew.Ahrens@Sun.COM if (err) 24677390SMatthew.Ahrens@Sun.COM return (err); 24687390SMatthew.Ahrens@Sun.COM 24697390SMatthew.Ahrens@Sun.COM /* 24707390SMatthew.Ahrens@Sun.COM * Compute the amounts of space that will be used by snapshots 24717390SMatthew.Ahrens@Sun.COM * after the promotion (for both origin and clone). For each, 24727390SMatthew.Ahrens@Sun.COM * it is the amount of space that will be on all of their 24737390SMatthew.Ahrens@Sun.COM * deadlists (that was not born before their new origin). 24747390SMatthew.Ahrens@Sun.COM */ 24757390SMatthew.Ahrens@Sun.COM if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 24767390SMatthew.Ahrens@Sun.COM uint64_t space; 24777390SMatthew.Ahrens@Sun.COM 24787390SMatthew.Ahrens@Sun.COM /* 24797390SMatthew.Ahrens@Sun.COM * Note, typically this will not be a clone of a clone, 24807390SMatthew.Ahrens@Sun.COM * so snap->ds->ds_origin_txg will be < TXG_INITIAL, so 24817390SMatthew.Ahrens@Sun.COM * these snaplist_space() -> bplist_space_birthrange() 24827390SMatthew.Ahrens@Sun.COM * calls will be fast because they do not have to 24837390SMatthew.Ahrens@Sun.COM * iterate over all bps. 24847390SMatthew.Ahrens@Sun.COM */ 24857390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 24867390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->shared_snaps, 24877390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &pa->cloneusedsnap); 24887390SMatthew.Ahrens@Sun.COM if (err) 24897390SMatthew.Ahrens@Sun.COM return (err); 24907390SMatthew.Ahrens@Sun.COM 24917390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->clone_snaps, 24927390SMatthew.Ahrens@Sun.COM snap->ds->ds_origin_txg, &space); 24937390SMatthew.Ahrens@Sun.COM if (err) 24947390SMatthew.Ahrens@Sun.COM return (err); 24957390SMatthew.Ahrens@Sun.COM pa->cloneusedsnap += space; 24966689Smaybee } 24977390SMatthew.Ahrens@Sun.COM if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 24987390SMatthew.Ahrens@Sun.COM err = snaplist_space(&pa->origin_snaps, 24997390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_creation_txg, &pa->originusedsnap); 25007390SMatthew.Ahrens@Sun.COM if (err) 25017390SMatthew.Ahrens@Sun.COM return (err); 25027390SMatthew.Ahrens@Sun.COM } 25037390SMatthew.Ahrens@Sun.COM 25047390SMatthew.Ahrens@Sun.COM return (0); 250510588SEric.Taylor@Sun.COM out: 250610588SEric.Taylor@Sun.COM pa->err_ds = snap->ds->ds_snapname; 250710588SEric.Taylor@Sun.COM return (err); 25082199Sahrens } 25092082Seschrock 25102199Sahrens static void 25114543Smarks dsl_dataset_promote_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 25122199Sahrens { 25132199Sahrens dsl_dataset_t *hds = arg1; 25142199Sahrens struct promotearg *pa = arg2; 25157390SMatthew.Ahrens@Sun.COM struct promotenode *snap = list_head(&pa->shared_snaps); 25166689Smaybee dsl_dataset_t *origin_ds = snap->ds; 25177390SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin_head; 25182199Sahrens dsl_dir_t *dd = hds->ds_dir; 25192199Sahrens dsl_pool_t *dp = hds->ds_dir->dd_pool; 25205367Sahrens dsl_dir_t *odd = NULL; 25217046Sahrens uint64_t oldnext_obj; 25227390SMatthew.Ahrens@Sun.COM int64_t delta; 25232199Sahrens 25242199Sahrens ASSERT(0 == (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE)); 25252199Sahrens 25267390SMatthew.Ahrens@Sun.COM snap = list_head(&pa->origin_snaps); 25277390SMatthew.Ahrens@Sun.COM origin_head = snap->ds; 25287390SMatthew.Ahrens@Sun.COM 25292417Sahrens /* 25305367Sahrens * We need to explicitly open odd, since origin_ds's dd will be 25312417Sahrens * changing. 25322417Sahrens */ 25335367Sahrens VERIFY(0 == dsl_dir_open_obj(dp, origin_ds->ds_dir->dd_object, 25345367Sahrens NULL, FTAG, &odd)); 25352082Seschrock 25366689Smaybee /* change origin's next snap */ 25376689Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 25387046Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 25397390SMatthew.Ahrens@Sun.COM snap = list_tail(&pa->clone_snaps); 25407390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 25417390SMatthew.Ahrens@Sun.COM origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 25426689Smaybee 25437046Sahrens /* change the origin's next clone */ 25447046Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 254510801SMatthew.Ahrens@Sun.COM remove_from_next_clones(origin_ds, snap->ds->ds_object, tx); 25467046Sahrens VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset, 25477046Sahrens origin_ds->ds_phys->ds_next_clones_obj, 25487046Sahrens oldnext_obj, tx)); 25497046Sahrens } 25507046Sahrens 25516689Smaybee /* change origin */ 25526689Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 25536689Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 25546689Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 25557390SMatthew.Ahrens@Sun.COM hds->ds_origin_txg = origin_head->ds_origin_txg; 25566689Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 25576689Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 25587390SMatthew.Ahrens@Sun.COM origin_head->ds_origin_txg = origin_ds->ds_phys->ds_creation_txg; 25596689Smaybee 25602082Seschrock /* move snapshots to this dir */ 25617390SMatthew.Ahrens@Sun.COM for (snap = list_head(&pa->shared_snaps); snap; 25627390SMatthew.Ahrens@Sun.COM snap = list_next(&pa->shared_snaps, snap)) { 25636689Smaybee dsl_dataset_t *ds = snap->ds; 25642082Seschrock 25657237Sek110237 /* unregister props as dsl_dir is changing */ 256610298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 256710298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 256810298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 25697237Sek110237 } 25702082Seschrock /* move snap name entry */ 25717390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_get_snapname(ds)); 25727390SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_snap_remove(origin_head, 25736689Smaybee ds->ds_snapname, tx)); 25742199Sahrens VERIFY(0 == zap_add(dp->dp_meta_objset, 25752082Seschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 25762082Seschrock 8, 1, &ds->ds_object, tx)); 25772082Seschrock /* change containing dsl_dir */ 25782082Seschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 25795367Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 25802082Seschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 25815367Sahrens ASSERT3P(ds->ds_dir, ==, odd); 25822082Seschrock dsl_dir_close(ds->ds_dir, ds); 25832199Sahrens VERIFY(0 == dsl_dir_open_obj(dp, dd->dd_object, 25842082Seschrock NULL, ds, &ds->ds_dir)); 25852082Seschrock 25862082Seschrock ASSERT3U(dsl_prop_numcb(ds), ==, 0); 25877390SMatthew.Ahrens@Sun.COM } 25887390SMatthew.Ahrens@Sun.COM 25897390SMatthew.Ahrens@Sun.COM /* 25907390SMatthew.Ahrens@Sun.COM * Change space accounting. 25917390SMatthew.Ahrens@Sun.COM * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 25927390SMatthew.Ahrens@Sun.COM * both be valid, or both be 0 (resulting in delta == 0). This 25937390SMatthew.Ahrens@Sun.COM * is true for each of {clone,origin} independently. 25947390SMatthew.Ahrens@Sun.COM */ 25957390SMatthew.Ahrens@Sun.COM 25967390SMatthew.Ahrens@Sun.COM delta = pa->cloneusedsnap - 25977390SMatthew.Ahrens@Sun.COM dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 25987390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, >=, 0); 25997390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, delta); 26007390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 26017390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(dd, DD_USED_HEAD, 26027390SMatthew.Ahrens@Sun.COM pa->used - delta, pa->comp, pa->uncomp, tx); 26037390SMatthew.Ahrens@Sun.COM 26047390SMatthew.Ahrens@Sun.COM delta = pa->originusedsnap - 26057390SMatthew.Ahrens@Sun.COM odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 26067390SMatthew.Ahrens@Sun.COM ASSERT3S(delta, <=, 0); 26077390SMatthew.Ahrens@Sun.COM ASSERT3U(pa->used, >=, -delta); 26087390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 26097390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(odd, DD_USED_HEAD, 26107390SMatthew.Ahrens@Sun.COM -pa->used - delta, -pa->comp, -pa->uncomp, tx); 26117390SMatthew.Ahrens@Sun.COM 26125367Sahrens origin_ds->ds_phys->ds_unique_bytes = pa->unique; 26132082Seschrock 26144543Smarks /* log history record */ 26154543Smarks spa_history_internal_log(LOG_DS_PROMOTE, dd->dd_pool->dp_spa, tx, 26166689Smaybee cr, "dataset = %llu", hds->ds_object); 26174543Smarks 26185367Sahrens dsl_dir_close(odd, FTAG); 26192082Seschrock } 26202082Seschrock 26217390SMatthew.Ahrens@Sun.COM static char *snaplist_tag = "snaplist"; 26227390SMatthew.Ahrens@Sun.COM /* 26237390SMatthew.Ahrens@Sun.COM * Make a list of dsl_dataset_t's for the snapshots between first_obj 26247390SMatthew.Ahrens@Sun.COM * (exclusive) and last_obj (inclusive). The list will be in reverse 26257390SMatthew.Ahrens@Sun.COM * order (last_obj will be the list_head()). If first_obj == 0, do all 26267390SMatthew.Ahrens@Sun.COM * snapshots back to this dataset's origin. 26277390SMatthew.Ahrens@Sun.COM */ 26287390SMatthew.Ahrens@Sun.COM static int 26297390SMatthew.Ahrens@Sun.COM snaplist_make(dsl_pool_t *dp, boolean_t own, 26307390SMatthew.Ahrens@Sun.COM uint64_t first_obj, uint64_t last_obj, list_t *l) 26317390SMatthew.Ahrens@Sun.COM { 26327390SMatthew.Ahrens@Sun.COM uint64_t obj = last_obj; 26337390SMatthew.Ahrens@Sun.COM 26347390SMatthew.Ahrens@Sun.COM ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock)); 26357390SMatthew.Ahrens@Sun.COM 26367390SMatthew.Ahrens@Sun.COM list_create(l, sizeof (struct promotenode), 26377390SMatthew.Ahrens@Sun.COM offsetof(struct promotenode, link)); 26387390SMatthew.Ahrens@Sun.COM 26397390SMatthew.Ahrens@Sun.COM while (obj != first_obj) { 26407390SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 26417390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26427390SMatthew.Ahrens@Sun.COM int err; 26437390SMatthew.Ahrens@Sun.COM 26447390SMatthew.Ahrens@Sun.COM if (own) { 26457390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, obj, 26467390SMatthew.Ahrens@Sun.COM 0, snaplist_tag, &ds); 26477390SMatthew.Ahrens@Sun.COM if (err == 0) 26487390SMatthew.Ahrens@Sun.COM dsl_dataset_make_exclusive(ds, snaplist_tag); 26497390SMatthew.Ahrens@Sun.COM } else { 26507390SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold_obj(dp, obj, snaplist_tag, &ds); 26517390SMatthew.Ahrens@Sun.COM } 26527390SMatthew.Ahrens@Sun.COM if (err == ENOENT) { 26537390SMatthew.Ahrens@Sun.COM /* lost race with snapshot destroy */ 26547390SMatthew.Ahrens@Sun.COM struct promotenode *last = list_tail(l); 26557390SMatthew.Ahrens@Sun.COM ASSERT(obj != last->ds->ds_phys->ds_prev_snap_obj); 26567390SMatthew.Ahrens@Sun.COM obj = last->ds->ds_phys->ds_prev_snap_obj; 26577390SMatthew.Ahrens@Sun.COM continue; 26587390SMatthew.Ahrens@Sun.COM } else if (err) { 26597390SMatthew.Ahrens@Sun.COM return (err); 26607390SMatthew.Ahrens@Sun.COM } 26617390SMatthew.Ahrens@Sun.COM 26627390SMatthew.Ahrens@Sun.COM if (first_obj == 0) 26637390SMatthew.Ahrens@Sun.COM first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 26647390SMatthew.Ahrens@Sun.COM 26657390SMatthew.Ahrens@Sun.COM snap = kmem_alloc(sizeof (struct promotenode), KM_SLEEP); 26667390SMatthew.Ahrens@Sun.COM snap->ds = ds; 26677390SMatthew.Ahrens@Sun.COM list_insert_tail(l, snap); 26687390SMatthew.Ahrens@Sun.COM obj = ds->ds_phys->ds_prev_snap_obj; 26697390SMatthew.Ahrens@Sun.COM } 26707390SMatthew.Ahrens@Sun.COM 26717390SMatthew.Ahrens@Sun.COM return (0); 26727390SMatthew.Ahrens@Sun.COM } 26737390SMatthew.Ahrens@Sun.COM 26747390SMatthew.Ahrens@Sun.COM static int 26757390SMatthew.Ahrens@Sun.COM snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 26767390SMatthew.Ahrens@Sun.COM { 26777390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26787390SMatthew.Ahrens@Sun.COM 26797390SMatthew.Ahrens@Sun.COM *spacep = 0; 26807390SMatthew.Ahrens@Sun.COM for (snap = list_head(l); snap; snap = list_next(l, snap)) { 26817390SMatthew.Ahrens@Sun.COM uint64_t used; 26827390SMatthew.Ahrens@Sun.COM int err = bplist_space_birthrange(&snap->ds->ds_deadlist, 26837390SMatthew.Ahrens@Sun.COM mintxg, UINT64_MAX, &used); 26847390SMatthew.Ahrens@Sun.COM if (err) 26857390SMatthew.Ahrens@Sun.COM return (err); 26867390SMatthew.Ahrens@Sun.COM *spacep += used; 26877390SMatthew.Ahrens@Sun.COM } 26887390SMatthew.Ahrens@Sun.COM return (0); 26897390SMatthew.Ahrens@Sun.COM } 26907390SMatthew.Ahrens@Sun.COM 26917390SMatthew.Ahrens@Sun.COM static void 26927390SMatthew.Ahrens@Sun.COM snaplist_destroy(list_t *l, boolean_t own) 26937390SMatthew.Ahrens@Sun.COM { 26947390SMatthew.Ahrens@Sun.COM struct promotenode *snap; 26957390SMatthew.Ahrens@Sun.COM 26968779SMark.Musante@Sun.COM if (!l || !list_link_active(&l->list_head)) 26977390SMatthew.Ahrens@Sun.COM return; 26987390SMatthew.Ahrens@Sun.COM 26997390SMatthew.Ahrens@Sun.COM while ((snap = list_tail(l)) != NULL) { 27007390SMatthew.Ahrens@Sun.COM list_remove(l, snap); 27017390SMatthew.Ahrens@Sun.COM if (own) 27027390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(snap->ds, snaplist_tag); 27037390SMatthew.Ahrens@Sun.COM else 27047390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(snap->ds, snaplist_tag); 27057390SMatthew.Ahrens@Sun.COM kmem_free(snap, sizeof (struct promotenode)); 27067390SMatthew.Ahrens@Sun.COM } 27077390SMatthew.Ahrens@Sun.COM list_destroy(l); 27087390SMatthew.Ahrens@Sun.COM } 27097390SMatthew.Ahrens@Sun.COM 27107390SMatthew.Ahrens@Sun.COM /* 27117390SMatthew.Ahrens@Sun.COM * Promote a clone. Nomenclature note: 27127390SMatthew.Ahrens@Sun.COM * "clone" or "cds": the original clone which is being promoted 27137390SMatthew.Ahrens@Sun.COM * "origin" or "ods": the snapshot which is originally clone's origin 27147390SMatthew.Ahrens@Sun.COM * "origin head" or "ohds": the dataset which is the head 27157390SMatthew.Ahrens@Sun.COM * (filesystem/volume) for the origin 27167390SMatthew.Ahrens@Sun.COM * "origin origin": the origin of the origin's filesystem (typically 27177390SMatthew.Ahrens@Sun.COM * NULL, indicating that the clone is not a clone of a clone). 27187390SMatthew.Ahrens@Sun.COM */ 27192082Seschrock int 272010588SEric.Taylor@Sun.COM dsl_dataset_promote(const char *name, char *conflsnap) 27212082Seschrock { 27222082Seschrock dsl_dataset_t *ds; 27236689Smaybee dsl_dir_t *dd; 27246689Smaybee dsl_pool_t *dp; 27252082Seschrock dmu_object_info_t doi; 27267390SMatthew.Ahrens@Sun.COM struct promotearg pa = { 0 }; 27277046Sahrens struct promotenode *snap; 27286689Smaybee int err; 27296689Smaybee 27306689Smaybee err = dsl_dataset_hold(name, FTAG, &ds); 27312082Seschrock if (err) 27322082Seschrock return (err); 27336689Smaybee dd = ds->ds_dir; 27346689Smaybee dp = dd->dd_pool; 27356689Smaybee 27366689Smaybee err = dmu_object_info(dp->dp_meta_objset, 27372082Seschrock ds->ds_phys->ds_snapnames_zapobj, &doi); 27382082Seschrock if (err) { 27396689Smaybee dsl_dataset_rele(ds, FTAG); 27402082Seschrock return (err); 27412082Seschrock } 27422082Seschrock 27437390SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds) || dd->dd_phys->dd_origin_obj == 0) { 27447390SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 27457390SMatthew.Ahrens@Sun.COM return (EINVAL); 27467390SMatthew.Ahrens@Sun.COM } 27477390SMatthew.Ahrens@Sun.COM 27482082Seschrock /* 27496689Smaybee * We are going to inherit all the snapshots taken before our 27506689Smaybee * origin (i.e., our new origin will be our parent's origin). 27516689Smaybee * Take ownership of them so that we can rename them into our 27526689Smaybee * namespace. 27536689Smaybee */ 27546689Smaybee rw_enter(&dp->dp_config_rwlock, RW_READER); 27557390SMatthew.Ahrens@Sun.COM 27567390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_TRUE, 0, dd->dd_phys->dd_origin_obj, 27577390SMatthew.Ahrens@Sun.COM &pa.shared_snaps); 27587390SMatthew.Ahrens@Sun.COM if (err != 0) 27597390SMatthew.Ahrens@Sun.COM goto out; 27607390SMatthew.Ahrens@Sun.COM 27617390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, 0, ds->ds_object, &pa.clone_snaps); 27627390SMatthew.Ahrens@Sun.COM if (err != 0) 27637390SMatthew.Ahrens@Sun.COM goto out; 27647390SMatthew.Ahrens@Sun.COM 27657390SMatthew.Ahrens@Sun.COM snap = list_head(&pa.shared_snaps); 27667390SMatthew.Ahrens@Sun.COM ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 27677390SMatthew.Ahrens@Sun.COM err = snaplist_make(dp, B_FALSE, dd->dd_phys->dd_origin_obj, 27687390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, &pa.origin_snaps); 27697390SMatthew.Ahrens@Sun.COM if (err != 0) 27707390SMatthew.Ahrens@Sun.COM goto out; 27717390SMatthew.Ahrens@Sun.COM 27727390SMatthew.Ahrens@Sun.COM if (dsl_dir_is_clone(snap->ds->ds_dir)) { 27737390SMatthew.Ahrens@Sun.COM err = dsl_dataset_own_obj(dp, 27747390SMatthew.Ahrens@Sun.COM snap->ds->ds_dir->dd_phys->dd_origin_obj, 27757390SMatthew.Ahrens@Sun.COM 0, FTAG, &pa.origin_origin); 27767390SMatthew.Ahrens@Sun.COM if (err != 0) 27776689Smaybee goto out; 27786689Smaybee } 27797390SMatthew.Ahrens@Sun.COM 27807390SMatthew.Ahrens@Sun.COM out: 27816689Smaybee rw_exit(&dp->dp_config_rwlock); 27826689Smaybee 27836689Smaybee /* 27842082Seschrock * Add in 128x the snapnames zapobj size, since we will be moving 27852082Seschrock * a bunch of snapnames to the promoted ds, and dirtying their 27862082Seschrock * bonus buffers. 27872082Seschrock */ 27887390SMatthew.Ahrens@Sun.COM if (err == 0) { 27897390SMatthew.Ahrens@Sun.COM err = dsl_sync_task_do(dp, dsl_dataset_promote_check, 27907390SMatthew.Ahrens@Sun.COM dsl_dataset_promote_sync, ds, &pa, 279110922SJeff.Bonwick@Sun.COM 2 + 2 * doi.doi_physical_blocks_512); 279210588SEric.Taylor@Sun.COM if (err && pa.err_ds && conflsnap) 279310588SEric.Taylor@Sun.COM (void) strncpy(conflsnap, pa.err_ds, MAXNAMELEN); 27946689Smaybee } 27957390SMatthew.Ahrens@Sun.COM 27967390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.shared_snaps, B_TRUE); 27977390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.clone_snaps, B_FALSE); 27987390SMatthew.Ahrens@Sun.COM snaplist_destroy(&pa.origin_snaps, B_FALSE); 27997390SMatthew.Ahrens@Sun.COM if (pa.origin_origin) 28007390SMatthew.Ahrens@Sun.COM dsl_dataset_disown(pa.origin_origin, FTAG); 28016689Smaybee dsl_dataset_rele(ds, FTAG); 28022082Seschrock return (err); 28032082Seschrock } 28043912Slling 28055367Sahrens struct cloneswaparg { 28065367Sahrens dsl_dataset_t *cds; /* clone dataset */ 28075367Sahrens dsl_dataset_t *ohds; /* origin's head dataset */ 28085367Sahrens boolean_t force; 28095481Sck153898 int64_t unused_refres_delta; /* change in unconsumed refreservation */ 28105367Sahrens }; 28115326Sek110237 28125326Sek110237 /* ARGSUSED */ 28135326Sek110237 static int 28145326Sek110237 dsl_dataset_clone_swap_check(void *arg1, void *arg2, dmu_tx_t *tx) 28155326Sek110237 { 28165367Sahrens struct cloneswaparg *csa = arg1; 28175326Sek110237 28185367Sahrens /* they should both be heads */ 28195367Sahrens if (dsl_dataset_is_snapshot(csa->cds) || 28205367Sahrens dsl_dataset_is_snapshot(csa->ohds)) 28215326Sek110237 return (EINVAL); 28225326Sek110237 28235367Sahrens /* the branch point should be just before them */ 28245367Sahrens if (csa->cds->ds_prev != csa->ohds->ds_prev) 28255326Sek110237 return (EINVAL); 28265326Sek110237 282710272SMatthew.Ahrens@Sun.COM /* cds should be the clone (unless they are unrelated) */ 282810272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev != NULL && 282910272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev != csa->cds->ds_dir->dd_pool->dp_origin_snap && 283010272SMatthew.Ahrens@Sun.COM csa->ohds->ds_object != 283110272SMatthew.Ahrens@Sun.COM csa->cds->ds_prev->ds_phys->ds_next_snap_obj) 28325367Sahrens return (EINVAL); 28335326Sek110237 28345367Sahrens /* the clone should be a child of the origin */ 28355367Sahrens if (csa->cds->ds_dir->dd_parent != csa->ohds->ds_dir) 28365367Sahrens return (EINVAL); 28375326Sek110237 28385367Sahrens /* ohds shouldn't be modified unless 'force' */ 28395367Sahrens if (!csa->force && dsl_dataset_modified_since_lastsnap(csa->ohds)) 28405367Sahrens return (ETXTBSY); 28415481Sck153898 28425481Sck153898 /* adjust amount of any unconsumed refreservation */ 28435481Sck153898 csa->unused_refres_delta = 28445481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28455481Sck153898 csa->ohds->ds_phys->ds_unique_bytes) - 28465481Sck153898 (int64_t)MIN(csa->ohds->ds_reserved, 28475481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 28485481Sck153898 28495481Sck153898 if (csa->unused_refres_delta > 0 && 28505481Sck153898 csa->unused_refres_delta > 28515481Sck153898 dsl_dir_space_available(csa->ohds->ds_dir, NULL, 0, TRUE)) 28525481Sck153898 return (ENOSPC); 28535481Sck153898 285410799SChris.Kirby@sun.com if (csa->ohds->ds_quota != 0 && 285510799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes > csa->ohds->ds_quota) 285610799SChris.Kirby@sun.com return (EDQUOT); 285710799SChris.Kirby@sun.com 28585367Sahrens return (0); 28595326Sek110237 } 28605326Sek110237 28615326Sek110237 /* ARGSUSED */ 28625326Sek110237 static void 28635326Sek110237 dsl_dataset_clone_swap_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 28645326Sek110237 { 28655367Sahrens struct cloneswaparg *csa = arg1; 28665367Sahrens dsl_pool_t *dp = csa->cds->ds_dir->dd_pool; 28675326Sek110237 28685481Sck153898 ASSERT(csa->cds->ds_reserved == 0); 286910799SChris.Kirby@sun.com ASSERT(csa->ohds->ds_quota == 0 || 287010799SChris.Kirby@sun.com csa->cds->ds_phys->ds_unique_bytes <= csa->ohds->ds_quota); 28715481Sck153898 28725367Sahrens dmu_buf_will_dirty(csa->cds->ds_dbuf, tx); 28735367Sahrens dmu_buf_will_dirty(csa->ohds->ds_dbuf, tx); 28745326Sek110237 287510298SMatthew.Ahrens@Sun.COM if (csa->cds->ds_objset != NULL) { 287610298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->cds->ds_objset); 287710298SMatthew.Ahrens@Sun.COM csa->cds->ds_objset = NULL; 28785367Sahrens } 28795326Sek110237 288010298SMatthew.Ahrens@Sun.COM if (csa->ohds->ds_objset != NULL) { 288110298SMatthew.Ahrens@Sun.COM dmu_objset_evict(csa->ohds->ds_objset); 288210298SMatthew.Ahrens@Sun.COM csa->ohds->ds_objset = NULL; 28835367Sahrens } 28845326Sek110237 288510272SMatthew.Ahrens@Sun.COM /* 288610272SMatthew.Ahrens@Sun.COM * Reset origin's unique bytes, if it exists. 288710272SMatthew.Ahrens@Sun.COM */ 288810272SMatthew.Ahrens@Sun.COM if (csa->cds->ds_prev) { 288910272SMatthew.Ahrens@Sun.COM dsl_dataset_t *origin = csa->cds->ds_prev; 289010272SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(origin->ds_dbuf, tx); 289110272SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 289210272SMatthew.Ahrens@Sun.COM origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 289310272SMatthew.Ahrens@Sun.COM &origin->ds_phys->ds_unique_bytes)); 289410272SMatthew.Ahrens@Sun.COM } 28955326Sek110237 28965326Sek110237 /* swap blkptrs */ 28975326Sek110237 { 28985326Sek110237 blkptr_t tmp; 28995367Sahrens tmp = csa->ohds->ds_phys->ds_bp; 29005367Sahrens csa->ohds->ds_phys->ds_bp = csa->cds->ds_phys->ds_bp; 29015367Sahrens csa->cds->ds_phys->ds_bp = tmp; 29025326Sek110237 } 29035326Sek110237 29045326Sek110237 /* set dd_*_bytes */ 29055326Sek110237 { 29065326Sek110237 int64_t dused, dcomp, duncomp; 29075326Sek110237 uint64_t cdl_used, cdl_comp, cdl_uncomp; 29085326Sek110237 uint64_t odl_used, odl_comp, odl_uncomp; 29095326Sek110237 29107390SMatthew.Ahrens@Sun.COM ASSERT3U(csa->cds->ds_dir->dd_phys-> 29117390SMatthew.Ahrens@Sun.COM dd_used_breakdown[DD_USED_SNAP], ==, 0); 29127390SMatthew.Ahrens@Sun.COM 29135367Sahrens VERIFY(0 == bplist_space(&csa->cds->ds_deadlist, &cdl_used, 29145326Sek110237 &cdl_comp, &cdl_uncomp)); 29155367Sahrens VERIFY(0 == bplist_space(&csa->ohds->ds_deadlist, &odl_used, 29165326Sek110237 &odl_comp, &odl_uncomp)); 29177390SMatthew.Ahrens@Sun.COM 29185367Sahrens dused = csa->cds->ds_phys->ds_used_bytes + cdl_used - 29195367Sahrens (csa->ohds->ds_phys->ds_used_bytes + odl_used); 29205367Sahrens dcomp = csa->cds->ds_phys->ds_compressed_bytes + cdl_comp - 29215367Sahrens (csa->ohds->ds_phys->ds_compressed_bytes + odl_comp); 29225367Sahrens duncomp = csa->cds->ds_phys->ds_uncompressed_bytes + 29235367Sahrens cdl_uncomp - 29245367Sahrens (csa->ohds->ds_phys->ds_uncompressed_bytes + odl_uncomp); 29255326Sek110237 29267390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_HEAD, 29275367Sahrens dused, dcomp, duncomp, tx); 29287390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->cds->ds_dir, DD_USED_HEAD, 29295367Sahrens -dused, -dcomp, -duncomp, tx); 29307390SMatthew.Ahrens@Sun.COM 29317390SMatthew.Ahrens@Sun.COM /* 29327390SMatthew.Ahrens@Sun.COM * The difference in the space used by snapshots is the 29337390SMatthew.Ahrens@Sun.COM * difference in snapshot space due to the head's 29347390SMatthew.Ahrens@Sun.COM * deadlist (since that's the only thing that's 29357390SMatthew.Ahrens@Sun.COM * changing that affects the snapused). 29367390SMatthew.Ahrens@Sun.COM */ 29377390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->cds->ds_deadlist, 29387390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &cdl_used)); 29397390SMatthew.Ahrens@Sun.COM VERIFY(0 == bplist_space_birthrange(&csa->ohds->ds_deadlist, 29407390SMatthew.Ahrens@Sun.COM csa->ohds->ds_origin_txg, UINT64_MAX, &odl_used)); 29417390SMatthew.Ahrens@Sun.COM dsl_dir_transfer_space(csa->ohds->ds_dir, cdl_used - odl_used, 29427390SMatthew.Ahrens@Sun.COM DD_USED_HEAD, DD_USED_SNAP, tx); 29435367Sahrens } 29445367Sahrens 29455367Sahrens #define SWITCH64(x, y) \ 29465367Sahrens { \ 29475367Sahrens uint64_t __tmp = (x); \ 29485367Sahrens (x) = (y); \ 29495367Sahrens (y) = __tmp; \ 29505326Sek110237 } 29515326Sek110237 29525326Sek110237 /* swap ds_*_bytes */ 29535367Sahrens SWITCH64(csa->ohds->ds_phys->ds_used_bytes, 29545367Sahrens csa->cds->ds_phys->ds_used_bytes); 29555367Sahrens SWITCH64(csa->ohds->ds_phys->ds_compressed_bytes, 29565367Sahrens csa->cds->ds_phys->ds_compressed_bytes); 29575367Sahrens SWITCH64(csa->ohds->ds_phys->ds_uncompressed_bytes, 29585367Sahrens csa->cds->ds_phys->ds_uncompressed_bytes); 29595481Sck153898 SWITCH64(csa->ohds->ds_phys->ds_unique_bytes, 29605481Sck153898 csa->cds->ds_phys->ds_unique_bytes); 29615481Sck153898 29625481Sck153898 /* apply any parent delta for change in unconsumed refreservation */ 29637390SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(csa->ohds->ds_dir, DD_USED_REFRSRV, 29647390SMatthew.Ahrens@Sun.COM csa->unused_refres_delta, 0, 0, tx); 29655326Sek110237 29665326Sek110237 /* swap deadlists */ 29675367Sahrens bplist_close(&csa->cds->ds_deadlist); 29685367Sahrens bplist_close(&csa->ohds->ds_deadlist); 29695367Sahrens SWITCH64(csa->ohds->ds_phys->ds_deadlist_obj, 29705367Sahrens csa->cds->ds_phys->ds_deadlist_obj); 29715367Sahrens VERIFY(0 == bplist_open(&csa->cds->ds_deadlist, dp->dp_meta_objset, 29725367Sahrens csa->cds->ds_phys->ds_deadlist_obj)); 29735367Sahrens VERIFY(0 == bplist_open(&csa->ohds->ds_deadlist, dp->dp_meta_objset, 29745367Sahrens csa->ohds->ds_phys->ds_deadlist_obj)); 29757837SMatthew.Ahrens@Sun.COM 29767837SMatthew.Ahrens@Sun.COM dsl_pool_ds_clone_swapped(csa->ohds, csa->cds, tx); 29775326Sek110237 } 29785326Sek110237 29795326Sek110237 /* 298010272SMatthew.Ahrens@Sun.COM * Swap 'clone' with its origin head datasets. Used at the end of "zfs 298110272SMatthew.Ahrens@Sun.COM * recv" into an existing fs to swizzle the file system to the new 298210272SMatthew.Ahrens@Sun.COM * version, and by "zfs rollback". Can also be used to swap two 298310272SMatthew.Ahrens@Sun.COM * independent head datasets if neither has any snapshots. 29845326Sek110237 */ 29855326Sek110237 int 29865367Sahrens dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head, 29875367Sahrens boolean_t force) 29885326Sek110237 { 29895367Sahrens struct cloneswaparg csa; 29906689Smaybee int error; 29916689Smaybee 29926689Smaybee ASSERT(clone->ds_owner); 29936689Smaybee ASSERT(origin_head->ds_owner); 29946689Smaybee retry: 29956689Smaybee /* Need exclusive access for the swap */ 29966689Smaybee rw_enter(&clone->ds_rwlock, RW_WRITER); 29976689Smaybee if (!rw_tryenter(&origin_head->ds_rwlock, RW_WRITER)) { 29986689Smaybee rw_exit(&clone->ds_rwlock); 29996689Smaybee rw_enter(&origin_head->ds_rwlock, RW_WRITER); 30006689Smaybee if (!rw_tryenter(&clone->ds_rwlock, RW_WRITER)) { 30016689Smaybee rw_exit(&origin_head->ds_rwlock); 30026689Smaybee goto retry; 30036689Smaybee } 30046689Smaybee } 30055367Sahrens csa.cds = clone; 30065367Sahrens csa.ohds = origin_head; 30075367Sahrens csa.force = force; 30086689Smaybee error = dsl_sync_task_do(clone->ds_dir->dd_pool, 30095326Sek110237 dsl_dataset_clone_swap_check, 30106689Smaybee dsl_dataset_clone_swap_sync, &csa, NULL, 9); 30116689Smaybee return (error); 30125326Sek110237 } 30135326Sek110237 30143912Slling /* 30153912Slling * Given a pool name and a dataset object number in that pool, 30163912Slling * return the name of that dataset. 30173912Slling */ 30183912Slling int 30193912Slling dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 30203912Slling { 30213912Slling spa_t *spa; 30223912Slling dsl_pool_t *dp; 30236689Smaybee dsl_dataset_t *ds; 30243912Slling int error; 30253912Slling 30263912Slling if ((error = spa_open(pname, &spa, FTAG)) != 0) 30273912Slling return (error); 30283912Slling dp = spa_get_dsl(spa); 30293912Slling rw_enter(&dp->dp_config_rwlock, RW_READER); 30306689Smaybee if ((error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds)) == 0) { 30316689Smaybee dsl_dataset_name(ds, buf); 30326689Smaybee dsl_dataset_rele(ds, FTAG); 30333912Slling } 30343912Slling rw_exit(&dp->dp_config_rwlock); 30353912Slling spa_close(spa, FTAG); 30363912Slling 30376689Smaybee return (error); 30383912Slling } 30395378Sck153898 30405378Sck153898 int 30415378Sck153898 dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 30426689Smaybee uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 30435378Sck153898 { 30445378Sck153898 int error = 0; 30455378Sck153898 30465378Sck153898 ASSERT3S(asize, >, 0); 30475378Sck153898 30485831Sck153898 /* 30495831Sck153898 * *ref_rsrv is the portion of asize that will come from any 30505831Sck153898 * unconsumed refreservation space. 30515831Sck153898 */ 30525831Sck153898 *ref_rsrv = 0; 30535831Sck153898 30545378Sck153898 mutex_enter(&ds->ds_lock); 30555378Sck153898 /* 30565378Sck153898 * Make a space adjustment for reserved bytes. 30575378Sck153898 */ 30585378Sck153898 if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 30595378Sck153898 ASSERT3U(*used, >=, 30605378Sck153898 ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30615378Sck153898 *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 30625831Sck153898 *ref_rsrv = 30635831Sck153898 asize - MIN(asize, parent_delta(ds, asize + inflight)); 30645378Sck153898 } 30655378Sck153898 30665378Sck153898 if (!check_quota || ds->ds_quota == 0) { 30675378Sck153898 mutex_exit(&ds->ds_lock); 30685378Sck153898 return (0); 30695378Sck153898 } 30705378Sck153898 /* 30715378Sck153898 * If they are requesting more space, and our current estimate 30725378Sck153898 * is over quota, they get to try again unless the actual 30735378Sck153898 * on-disk is over quota and there are no pending changes (which 30745378Sck153898 * may free up space for us). 30755378Sck153898 */ 30765378Sck153898 if (ds->ds_phys->ds_used_bytes + inflight >= ds->ds_quota) { 30775378Sck153898 if (inflight > 0 || ds->ds_phys->ds_used_bytes < ds->ds_quota) 30785378Sck153898 error = ERESTART; 30795378Sck153898 else 30805378Sck153898 error = EDQUOT; 30815378Sck153898 } 30825378Sck153898 mutex_exit(&ds->ds_lock); 30835378Sck153898 30845378Sck153898 return (error); 30855378Sck153898 } 30865378Sck153898 30875378Sck153898 /* ARGSUSED */ 30885378Sck153898 static int 30895378Sck153898 dsl_dataset_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) 30905378Sck153898 { 30915378Sck153898 dsl_dataset_t *ds = arg1; 309211022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 309311022STom.Erickson@Sun.COM int err; 30945378Sck153898 30955378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_REFQUOTA) 30965378Sck153898 return (ENOTSUP); 30975378Sck153898 309811022STom.Erickson@Sun.COM if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) 309911022STom.Erickson@Sun.COM return (err); 310011022STom.Erickson@Sun.COM 310111022STom.Erickson@Sun.COM if (psa->psa_effective_value == 0) 31025378Sck153898 return (0); 31035378Sck153898 310411022STom.Erickson@Sun.COM if (psa->psa_effective_value < ds->ds_phys->ds_used_bytes || 310511022STom.Erickson@Sun.COM psa->psa_effective_value < ds->ds_reserved) 31065378Sck153898 return (ENOSPC); 31075378Sck153898 31085378Sck153898 return (0); 31095378Sck153898 } 31105378Sck153898 311111022STom.Erickson@Sun.COM extern void dsl_prop_set_sync(void *, void *, cred_t *, dmu_tx_t *); 311211022STom.Erickson@Sun.COM 31135378Sck153898 void 31145378Sck153898 dsl_dataset_set_quota_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 31155378Sck153898 { 31165378Sck153898 dsl_dataset_t *ds = arg1; 311711022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 311811022STom.Erickson@Sun.COM uint64_t effective_value = psa->psa_effective_value; 311911022STom.Erickson@Sun.COM 312011022STom.Erickson@Sun.COM dsl_prop_set_sync(ds, psa, cr, tx); 312111022STom.Erickson@Sun.COM DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa); 312211022STom.Erickson@Sun.COM 312311022STom.Erickson@Sun.COM if (ds->ds_quota != effective_value) { 312411022STom.Erickson@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx); 312511022STom.Erickson@Sun.COM ds->ds_quota = effective_value; 312611022STom.Erickson@Sun.COM 312711022STom.Erickson@Sun.COM spa_history_internal_log(LOG_DS_REFQUOTA, 312811022STom.Erickson@Sun.COM ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu ", 312911022STom.Erickson@Sun.COM (longlong_t)ds->ds_quota, ds->ds_object); 313011022STom.Erickson@Sun.COM } 31315378Sck153898 } 31325378Sck153898 31335378Sck153898 int 313411022STom.Erickson@Sun.COM dsl_dataset_set_quota(const char *dsname, zprop_source_t source, uint64_t quota) 31355378Sck153898 { 31365378Sck153898 dsl_dataset_t *ds; 313711022STom.Erickson@Sun.COM dsl_prop_setarg_t psa; 31385378Sck153898 int err; 31395378Sck153898 314011022STom.Erickson@Sun.COM dsl_prop_setarg_init_uint64(&psa, "refquota", source, "a); 314111022STom.Erickson@Sun.COM 31426689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 31435378Sck153898 if (err) 31445378Sck153898 return (err); 31455378Sck153898 314611022STom.Erickson@Sun.COM /* 314711022STom.Erickson@Sun.COM * If someone removes a file, then tries to set the quota, we 314811022STom.Erickson@Sun.COM * want to make sure the file freeing takes effect. 314911022STom.Erickson@Sun.COM */ 315011022STom.Erickson@Sun.COM txg_wait_open(ds->ds_dir->dd_pool, 0); 315111022STom.Erickson@Sun.COM 315211022STom.Erickson@Sun.COM err = dsl_sync_task_do(ds->ds_dir->dd_pool, 315311022STom.Erickson@Sun.COM dsl_dataset_set_quota_check, dsl_dataset_set_quota_sync, 315411022STom.Erickson@Sun.COM ds, &psa, 0); 315511022STom.Erickson@Sun.COM 31566689Smaybee dsl_dataset_rele(ds, FTAG); 31575378Sck153898 return (err); 31585378Sck153898 } 31595378Sck153898 31605378Sck153898 static int 31615378Sck153898 dsl_dataset_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) 31625378Sck153898 { 31635378Sck153898 dsl_dataset_t *ds = arg1; 316411022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 316511022STom.Erickson@Sun.COM uint64_t effective_value; 31665378Sck153898 uint64_t unique; 316711022STom.Erickson@Sun.COM int err; 31685378Sck153898 31695378Sck153898 if (spa_version(ds->ds_dir->dd_pool->dp_spa) < 31705378Sck153898 SPA_VERSION_REFRESERVATION) 31715378Sck153898 return (ENOTSUP); 31725378Sck153898 31735378Sck153898 if (dsl_dataset_is_snapshot(ds)) 31745378Sck153898 return (EINVAL); 31755378Sck153898 317611022STom.Erickson@Sun.COM if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) 317711022STom.Erickson@Sun.COM return (err); 317811022STom.Erickson@Sun.COM 317911022STom.Erickson@Sun.COM effective_value = psa->psa_effective_value; 318011022STom.Erickson@Sun.COM 31815378Sck153898 /* 31825378Sck153898 * If we are doing the preliminary check in open context, the 31835378Sck153898 * space estimates may be inaccurate. 31845378Sck153898 */ 31855378Sck153898 if (!dmu_tx_is_syncing(tx)) 31865378Sck153898 return (0); 31875378Sck153898 31885378Sck153898 mutex_enter(&ds->ds_lock); 31895378Sck153898 unique = dsl_dataset_unique(ds); 31905378Sck153898 mutex_exit(&ds->ds_lock); 31915378Sck153898 319211022STom.Erickson@Sun.COM if (MAX(unique, effective_value) > MAX(unique, ds->ds_reserved)) { 319311022STom.Erickson@Sun.COM uint64_t delta = MAX(unique, effective_value) - 31948525SEric.Schrock@Sun.COM MAX(unique, ds->ds_reserved); 31958525SEric.Schrock@Sun.COM 31968525SEric.Schrock@Sun.COM if (delta > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 31978525SEric.Schrock@Sun.COM return (ENOSPC); 31988525SEric.Schrock@Sun.COM if (ds->ds_quota > 0 && 319911022STom.Erickson@Sun.COM effective_value > ds->ds_quota) 32008525SEric.Schrock@Sun.COM return (ENOSPC); 32018525SEric.Schrock@Sun.COM } 32025378Sck153898 32035378Sck153898 return (0); 32045378Sck153898 } 32055378Sck153898 32065378Sck153898 /* ARGSUSED */ 32075378Sck153898 static void 32085378Sck153898 dsl_dataset_set_reservation_sync(void *arg1, void *arg2, cred_t *cr, 32095378Sck153898 dmu_tx_t *tx) 32105378Sck153898 { 32115378Sck153898 dsl_dataset_t *ds = arg1; 321211022STom.Erickson@Sun.COM dsl_prop_setarg_t *psa = arg2; 321311022STom.Erickson@Sun.COM uint64_t effective_value = psa->psa_effective_value; 32147595SMatthew.Ahrens@Sun.COM uint64_t unique; 32157595SMatthew.Ahrens@Sun.COM int64_t delta; 32167595SMatthew.Ahrens@Sun.COM 321711022STom.Erickson@Sun.COM dsl_prop_set_sync(ds, psa, cr, tx); 321811022STom.Erickson@Sun.COM DSL_PROP_CHECK_PREDICTION(ds->ds_dir, psa); 321911022STom.Erickson@Sun.COM 32207595SMatthew.Ahrens@Sun.COM dmu_buf_will_dirty(ds->ds_dbuf, tx); 32217595SMatthew.Ahrens@Sun.COM 32227595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_dir->dd_lock); 32237595SMatthew.Ahrens@Sun.COM mutex_enter(&ds->ds_lock); 32247595SMatthew.Ahrens@Sun.COM unique = dsl_dataset_unique(ds); 322511022STom.Erickson@Sun.COM delta = MAX(0, (int64_t)(effective_value - unique)) - 32267595SMatthew.Ahrens@Sun.COM MAX(0, (int64_t)(ds->ds_reserved - unique)); 322711022STom.Erickson@Sun.COM ds->ds_reserved = effective_value; 32287595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_lock); 32297595SMatthew.Ahrens@Sun.COM 32307595SMatthew.Ahrens@Sun.COM dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 32317595SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_dir->dd_lock); 32327595SMatthew.Ahrens@Sun.COM 32337595SMatthew.Ahrens@Sun.COM spa_history_internal_log(LOG_DS_REFRESERV, 32347595SMatthew.Ahrens@Sun.COM ds->ds_dir->dd_pool->dp_spa, tx, cr, "%lld dataset = %llu", 323511022STom.Erickson@Sun.COM (longlong_t)effective_value, ds->ds_object); 32365378Sck153898 } 32375378Sck153898 32385378Sck153898 int 323911022STom.Erickson@Sun.COM dsl_dataset_set_reservation(const char *dsname, zprop_source_t source, 324011022STom.Erickson@Sun.COM uint64_t reservation) 32415378Sck153898 { 32425378Sck153898 dsl_dataset_t *ds; 324311022STom.Erickson@Sun.COM dsl_prop_setarg_t psa; 32445378Sck153898 int err; 32455378Sck153898 324611022STom.Erickson@Sun.COM dsl_prop_setarg_init_uint64(&psa, "refreservation", source, 324711022STom.Erickson@Sun.COM &reservation); 324811022STom.Erickson@Sun.COM 32496689Smaybee err = dsl_dataset_hold(dsname, FTAG, &ds); 32505378Sck153898 if (err) 32515378Sck153898 return (err); 32525378Sck153898 32535378Sck153898 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 32545378Sck153898 dsl_dataset_set_reservation_check, 325511022STom.Erickson@Sun.COM dsl_dataset_set_reservation_sync, ds, &psa, 0); 325611022STom.Erickson@Sun.COM 32576689Smaybee dsl_dataset_rele(ds, FTAG); 32585378Sck153898 return (err); 32595378Sck153898 } 326010242Schris.kirby@sun.com 326110342Schris.kirby@sun.com struct dsl_ds_holdarg { 326210342Schris.kirby@sun.com dsl_sync_task_group_t *dstg; 326310342Schris.kirby@sun.com char *htag; 326410342Schris.kirby@sun.com char *snapname; 326510342Schris.kirby@sun.com boolean_t recursive; 326610342Schris.kirby@sun.com boolean_t gotone; 326710342Schris.kirby@sun.com boolean_t temphold; 326810342Schris.kirby@sun.com char failed[MAXPATHLEN]; 326910342Schris.kirby@sun.com }; 327010342Schris.kirby@sun.com 327110951SChris.Kirby@sun.com /* 327210951SChris.Kirby@sun.com * The max length of a temporary tag prefix is the number of hex digits 327310951SChris.Kirby@sun.com * required to express UINT64_MAX plus one for the hyphen. 327410951SChris.Kirby@sun.com */ 327510951SChris.Kirby@sun.com #define MAX_TAG_PREFIX_LEN 17 327610951SChris.Kirby@sun.com 327710242Schris.kirby@sun.com static int 327810242Schris.kirby@sun.com dsl_dataset_user_hold_check(void *arg1, void *arg2, dmu_tx_t *tx) 327910242Schris.kirby@sun.com { 328010242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 328110342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 328210342Schris.kirby@sun.com char *htag = ha->htag; 328310242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 328410242Schris.kirby@sun.com int error = 0; 328510242Schris.kirby@sun.com 328610242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 328710242Schris.kirby@sun.com return (ENOTSUP); 328810242Schris.kirby@sun.com 328910242Schris.kirby@sun.com if (!dsl_dataset_is_snapshot(ds)) 329010242Schris.kirby@sun.com return (EINVAL); 329110242Schris.kirby@sun.com 329210242Schris.kirby@sun.com /* tags must be unique */ 329310242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 329410242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj) { 329510242Schris.kirby@sun.com error = zap_lookup(mos, ds->ds_phys->ds_userrefs_obj, htag, 329610242Schris.kirby@sun.com 8, 1, tx); 329710242Schris.kirby@sun.com if (error == 0) 329810242Schris.kirby@sun.com error = EEXIST; 329910242Schris.kirby@sun.com else if (error == ENOENT) 330010242Schris.kirby@sun.com error = 0; 330110242Schris.kirby@sun.com } 330210242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 330310242Schris.kirby@sun.com 330410951SChris.Kirby@sun.com if (error == 0 && ha->temphold && 330510951SChris.Kirby@sun.com strlen(htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN) 330610951SChris.Kirby@sun.com error = E2BIG; 330710951SChris.Kirby@sun.com 330810242Schris.kirby@sun.com return (error); 330910242Schris.kirby@sun.com } 331010242Schris.kirby@sun.com 331110242Schris.kirby@sun.com static void 331210242Schris.kirby@sun.com dsl_dataset_user_hold_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 331310242Schris.kirby@sun.com { 331410242Schris.kirby@sun.com dsl_dataset_t *ds = arg1; 331510342Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg2; 331610342Schris.kirby@sun.com char *htag = ha->htag; 331710342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 331810342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 331910951SChris.Kirby@sun.com uint64_t now = gethrestime_sec(); 332010242Schris.kirby@sun.com uint64_t zapobj; 332110242Schris.kirby@sun.com 332210242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 332310242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj == 0) { 332410242Schris.kirby@sun.com /* 332510242Schris.kirby@sun.com * This is the first user hold for this dataset. Create 332610242Schris.kirby@sun.com * the userrefs zap object. 332710242Schris.kirby@sun.com */ 332810242Schris.kirby@sun.com dmu_buf_will_dirty(ds->ds_dbuf, tx); 332910242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj = 333010242Schris.kirby@sun.com zap_create(mos, DMU_OT_USERREFS, DMU_OT_NONE, 0, tx); 333110242Schris.kirby@sun.com } else { 333210242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 333310242Schris.kirby@sun.com } 333410242Schris.kirby@sun.com ds->ds_userrefs++; 333510242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 333610242Schris.kirby@sun.com 333710242Schris.kirby@sun.com VERIFY(0 == zap_add(mos, zapobj, htag, 8, 1, &now, tx)); 333810242Schris.kirby@sun.com 333910342Schris.kirby@sun.com if (ha->temphold) { 334010342Schris.kirby@sun.com VERIFY(0 == dsl_pool_user_hold(dp, ds->ds_object, 334110342Schris.kirby@sun.com htag, &now, tx)); 334210342Schris.kirby@sun.com } 334310342Schris.kirby@sun.com 334410242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_HOLD, 334510342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> temp = %d dataset = %llu", htag, 334610342Schris.kirby@sun.com (int)ha->temphold, ds->ds_object); 334710242Schris.kirby@sun.com } 334810242Schris.kirby@sun.com 334910242Schris.kirby@sun.com static int 335011209SMatthew.Ahrens@Sun.COM dsl_dataset_user_hold_one(const char *dsname, void *arg) 335110242Schris.kirby@sun.com { 335210242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 335310242Schris.kirby@sun.com dsl_dataset_t *ds; 335410242Schris.kirby@sun.com int error; 335510242Schris.kirby@sun.com char *name; 335610242Schris.kirby@sun.com 335710242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname plus terminating NULL */ 335810272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 335910242Schris.kirby@sun.com error = dsl_dataset_hold(name, ha->dstg, &ds); 336010272SMatthew.Ahrens@Sun.COM strfree(name); 336110242Schris.kirby@sun.com if (error == 0) { 336210268Schris.kirby@sun.com ha->gotone = B_TRUE; 336310242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_hold_check, 336410342Schris.kirby@sun.com dsl_dataset_user_hold_sync, ds, ha, 0); 336510242Schris.kirby@sun.com } else if (error == ENOENT && ha->recursive) { 336610242Schris.kirby@sun.com error = 0; 336710242Schris.kirby@sun.com } else { 336811209SMatthew.Ahrens@Sun.COM (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 336910242Schris.kirby@sun.com } 337010242Schris.kirby@sun.com return (error); 337110242Schris.kirby@sun.com } 337210242Schris.kirby@sun.com 337310242Schris.kirby@sun.com int 337410242Schris.kirby@sun.com dsl_dataset_user_hold(char *dsname, char *snapname, char *htag, 337510342Schris.kirby@sun.com boolean_t recursive, boolean_t temphold) 337610242Schris.kirby@sun.com { 337710242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 337810242Schris.kirby@sun.com dsl_sync_task_t *dst; 337910242Schris.kirby@sun.com spa_t *spa; 338010242Schris.kirby@sun.com int error; 338110242Schris.kirby@sun.com 338210242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 338310242Schris.kirby@sun.com 338410242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 338510242Schris.kirby@sun.com 338610242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 338710242Schris.kirby@sun.com if (error) { 338810242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 338910242Schris.kirby@sun.com return (error); 339010242Schris.kirby@sun.com } 339110242Schris.kirby@sun.com 339210242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 339310242Schris.kirby@sun.com ha->htag = htag; 339410242Schris.kirby@sun.com ha->snapname = snapname; 339510242Schris.kirby@sun.com ha->recursive = recursive; 339610342Schris.kirby@sun.com ha->temphold = temphold; 339710242Schris.kirby@sun.com if (recursive) { 339810242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_hold_one, 339910242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 340010242Schris.kirby@sun.com } else { 340110242Schris.kirby@sun.com error = dsl_dataset_user_hold_one(dsname, ha); 340210242Schris.kirby@sun.com } 340310242Schris.kirby@sun.com if (error == 0) 340410242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 340510242Schris.kirby@sun.com 340610242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 340710242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 340810242Schris.kirby@sun.com dsl_dataset_t *ds = dst->dst_arg1; 340910242Schris.kirby@sun.com 341010242Schris.kirby@sun.com if (dst->dst_err) { 341110242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 341210242Schris.kirby@sun.com *strchr(ha->failed, '@') = '\0'; 341310242Schris.kirby@sun.com } 341410242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 341510242Schris.kirby@sun.com } 341610242Schris.kirby@sun.com 341710268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 341810268Schris.kirby@sun.com error = ENOENT; 341910268Schris.kirby@sun.com 342010242Schris.kirby@sun.com if (error) 342111209SMatthew.Ahrens@Sun.COM (void) strlcpy(dsname, ha->failed, sizeof (ha->failed)); 342210242Schris.kirby@sun.com 342310242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 342410242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 342510242Schris.kirby@sun.com spa_close(spa, FTAG); 342610242Schris.kirby@sun.com return (error); 342710242Schris.kirby@sun.com } 342810242Schris.kirby@sun.com 342910242Schris.kirby@sun.com struct dsl_ds_releasearg { 343010242Schris.kirby@sun.com dsl_dataset_t *ds; 343110242Schris.kirby@sun.com const char *htag; 343210242Schris.kirby@sun.com boolean_t own; /* do we own or just hold ds? */ 343310242Schris.kirby@sun.com }; 343410242Schris.kirby@sun.com 343510242Schris.kirby@sun.com static int 343610242Schris.kirby@sun.com dsl_dataset_release_might_destroy(dsl_dataset_t *ds, const char *htag, 343710242Schris.kirby@sun.com boolean_t *might_destroy) 343810242Schris.kirby@sun.com { 343910242Schris.kirby@sun.com objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 344010242Schris.kirby@sun.com uint64_t zapobj; 344110242Schris.kirby@sun.com uint64_t tmp; 344210242Schris.kirby@sun.com int error; 344310242Schris.kirby@sun.com 344410242Schris.kirby@sun.com *might_destroy = B_FALSE; 344510242Schris.kirby@sun.com 344610242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 344710242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 344810242Schris.kirby@sun.com if (zapobj == 0) { 344910242Schris.kirby@sun.com /* The tag can't possibly exist */ 345010242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 345110242Schris.kirby@sun.com return (ESRCH); 345210242Schris.kirby@sun.com } 345310242Schris.kirby@sun.com 345410242Schris.kirby@sun.com /* Make sure the tag exists */ 345510242Schris.kirby@sun.com error = zap_lookup(mos, zapobj, htag, 8, 1, &tmp); 345610242Schris.kirby@sun.com if (error) { 345710242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 345810242Schris.kirby@sun.com if (error == ENOENT) 345910242Schris.kirby@sun.com error = ESRCH; 346010242Schris.kirby@sun.com return (error); 346110242Schris.kirby@sun.com } 346210242Schris.kirby@sun.com 346310242Schris.kirby@sun.com if (ds->ds_userrefs == 1 && ds->ds_phys->ds_num_children == 1 && 346410242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) 346510242Schris.kirby@sun.com *might_destroy = B_TRUE; 346610242Schris.kirby@sun.com 346710242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 346810242Schris.kirby@sun.com return (0); 346910242Schris.kirby@sun.com } 347010242Schris.kirby@sun.com 347110242Schris.kirby@sun.com static int 347210242Schris.kirby@sun.com dsl_dataset_user_release_check(void *arg1, void *tag, dmu_tx_t *tx) 347310242Schris.kirby@sun.com { 347410242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 347510242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 347610242Schris.kirby@sun.com boolean_t might_destroy; 347710242Schris.kirby@sun.com int error; 347810242Schris.kirby@sun.com 347910242Schris.kirby@sun.com if (spa_version(ds->ds_dir->dd_pool->dp_spa) < SPA_VERSION_USERREFS) 348010242Schris.kirby@sun.com return (ENOTSUP); 348110242Schris.kirby@sun.com 348210242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ra->htag, &might_destroy); 348310242Schris.kirby@sun.com if (error) 348410242Schris.kirby@sun.com return (error); 348510242Schris.kirby@sun.com 348610242Schris.kirby@sun.com if (might_destroy) { 348710242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 348810242Schris.kirby@sun.com 348910242Schris.kirby@sun.com if (dmu_tx_is_syncing(tx)) { 349010242Schris.kirby@sun.com /* 349110242Schris.kirby@sun.com * If we're not prepared to remove the snapshot, 349210242Schris.kirby@sun.com * we can't allow the release to happen right now. 349310242Schris.kirby@sun.com */ 349410242Schris.kirby@sun.com if (!ra->own) 349510242Schris.kirby@sun.com return (EBUSY); 349610298SMatthew.Ahrens@Sun.COM if (ds->ds_objset) { 349710298SMatthew.Ahrens@Sun.COM dmu_objset_evict(ds->ds_objset); 349810298SMatthew.Ahrens@Sun.COM ds->ds_objset = NULL; 349910242Schris.kirby@sun.com } 350010242Schris.kirby@sun.com } 350110242Schris.kirby@sun.com dsda.ds = ds; 350210242Schris.kirby@sun.com dsda.releasing = B_TRUE; 350310242Schris.kirby@sun.com return (dsl_dataset_destroy_check(&dsda, tag, tx)); 350410242Schris.kirby@sun.com } 350510242Schris.kirby@sun.com 350610242Schris.kirby@sun.com return (0); 350710242Schris.kirby@sun.com } 350810242Schris.kirby@sun.com 350910242Schris.kirby@sun.com static void 351010242Schris.kirby@sun.com dsl_dataset_user_release_sync(void *arg1, void *tag, cred_t *cr, dmu_tx_t *tx) 351110242Schris.kirby@sun.com { 351210242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = arg1; 351310242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 351410342Schris.kirby@sun.com dsl_pool_t *dp = ds->ds_dir->dd_pool; 351510342Schris.kirby@sun.com objset_t *mos = dp->dp_meta_objset; 351610242Schris.kirby@sun.com uint64_t zapobj; 351710242Schris.kirby@sun.com uint64_t dsobj = ds->ds_object; 351810242Schris.kirby@sun.com uint64_t refs; 351910342Schris.kirby@sun.com int error; 352010242Schris.kirby@sun.com 352110242Schris.kirby@sun.com mutex_enter(&ds->ds_lock); 352210242Schris.kirby@sun.com ds->ds_userrefs--; 352310242Schris.kirby@sun.com refs = ds->ds_userrefs; 352410242Schris.kirby@sun.com mutex_exit(&ds->ds_lock); 352510342Schris.kirby@sun.com error = dsl_pool_user_release(dp, ds->ds_object, ra->htag, tx); 352610342Schris.kirby@sun.com VERIFY(error == 0 || error == ENOENT); 352710242Schris.kirby@sun.com zapobj = ds->ds_phys->ds_userrefs_obj; 352810242Schris.kirby@sun.com VERIFY(0 == zap_remove(mos, zapobj, ra->htag, tx)); 352910242Schris.kirby@sun.com if (ds->ds_userrefs == 0 && ds->ds_phys->ds_num_children == 1 && 353010242Schris.kirby@sun.com DS_IS_DEFER_DESTROY(ds)) { 353110242Schris.kirby@sun.com struct dsl_ds_destroyarg dsda = {0}; 353210242Schris.kirby@sun.com 353310242Schris.kirby@sun.com ASSERT(ra->own); 353410242Schris.kirby@sun.com dsda.ds = ds; 353510242Schris.kirby@sun.com dsda.releasing = B_TRUE; 353610242Schris.kirby@sun.com /* We already did the destroy_check */ 353710242Schris.kirby@sun.com dsl_dataset_destroy_sync(&dsda, tag, cr, tx); 353810242Schris.kirby@sun.com } 353910242Schris.kirby@sun.com 354010242Schris.kirby@sun.com spa_history_internal_log(LOG_DS_USER_RELEASE, 354110342Schris.kirby@sun.com dp->dp_spa, tx, cr, "<%s> %lld dataset = %llu", 354210242Schris.kirby@sun.com ra->htag, (longlong_t)refs, dsobj); 354310242Schris.kirby@sun.com } 354410242Schris.kirby@sun.com 354510242Schris.kirby@sun.com static int 354611209SMatthew.Ahrens@Sun.COM dsl_dataset_user_release_one(const char *dsname, void *arg) 354710242Schris.kirby@sun.com { 354810242Schris.kirby@sun.com struct dsl_ds_holdarg *ha = arg; 354910242Schris.kirby@sun.com struct dsl_ds_releasearg *ra; 355010242Schris.kirby@sun.com dsl_dataset_t *ds; 355110242Schris.kirby@sun.com int error; 355210242Schris.kirby@sun.com void *dtag = ha->dstg; 355310242Schris.kirby@sun.com char *name; 355410242Schris.kirby@sun.com boolean_t own = B_FALSE; 355510242Schris.kirby@sun.com boolean_t might_destroy; 355610242Schris.kirby@sun.com 355710242Schris.kirby@sun.com /* alloc a buffer to hold dsname@snapname, plus the terminating NULL */ 355810272SMatthew.Ahrens@Sun.COM name = kmem_asprintf("%s@%s", dsname, ha->snapname); 355910242Schris.kirby@sun.com error = dsl_dataset_hold(name, dtag, &ds); 356010272SMatthew.Ahrens@Sun.COM strfree(name); 356110242Schris.kirby@sun.com if (error == ENOENT && ha->recursive) 356210242Schris.kirby@sun.com return (0); 356311209SMatthew.Ahrens@Sun.COM (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 356410242Schris.kirby@sun.com if (error) 356510242Schris.kirby@sun.com return (error); 356610242Schris.kirby@sun.com 356710268Schris.kirby@sun.com ha->gotone = B_TRUE; 356810268Schris.kirby@sun.com 356910242Schris.kirby@sun.com ASSERT(dsl_dataset_is_snapshot(ds)); 357010242Schris.kirby@sun.com 357110242Schris.kirby@sun.com error = dsl_dataset_release_might_destroy(ds, ha->htag, &might_destroy); 357210242Schris.kirby@sun.com if (error) { 357310242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 357410242Schris.kirby@sun.com return (error); 357510242Schris.kirby@sun.com } 357610242Schris.kirby@sun.com 357710242Schris.kirby@sun.com if (might_destroy) { 357810242Schris.kirby@sun.com #ifdef _KERNEL 357910242Schris.kirby@sun.com error = zfs_unmount_snap(name, NULL); 358010242Schris.kirby@sun.com if (error) { 358110242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 358210242Schris.kirby@sun.com return (error); 358310242Schris.kirby@sun.com } 358410242Schris.kirby@sun.com #endif 358510298SMatthew.Ahrens@Sun.COM if (!dsl_dataset_tryown(ds, B_TRUE, dtag)) { 358610242Schris.kirby@sun.com dsl_dataset_rele(ds, dtag); 358710242Schris.kirby@sun.com return (EBUSY); 358810242Schris.kirby@sun.com } else { 358910242Schris.kirby@sun.com own = B_TRUE; 359010242Schris.kirby@sun.com dsl_dataset_make_exclusive(ds, dtag); 359110242Schris.kirby@sun.com } 359210242Schris.kirby@sun.com } 359310242Schris.kirby@sun.com 359410242Schris.kirby@sun.com ra = kmem_alloc(sizeof (struct dsl_ds_releasearg), KM_SLEEP); 359510242Schris.kirby@sun.com ra->ds = ds; 359610242Schris.kirby@sun.com ra->htag = ha->htag; 359710242Schris.kirby@sun.com ra->own = own; 359810242Schris.kirby@sun.com dsl_sync_task_create(ha->dstg, dsl_dataset_user_release_check, 359910242Schris.kirby@sun.com dsl_dataset_user_release_sync, ra, dtag, 0); 360010242Schris.kirby@sun.com 360110242Schris.kirby@sun.com return (0); 360210242Schris.kirby@sun.com } 360310242Schris.kirby@sun.com 360410242Schris.kirby@sun.com int 360510242Schris.kirby@sun.com dsl_dataset_user_release(char *dsname, char *snapname, char *htag, 360610242Schris.kirby@sun.com boolean_t recursive) 360710242Schris.kirby@sun.com { 360810242Schris.kirby@sun.com struct dsl_ds_holdarg *ha; 360910242Schris.kirby@sun.com dsl_sync_task_t *dst; 361010242Schris.kirby@sun.com spa_t *spa; 361110242Schris.kirby@sun.com int error; 361210242Schris.kirby@sun.com 361311546SChris.Kirby@sun.com top: 361410242Schris.kirby@sun.com ha = kmem_zalloc(sizeof (struct dsl_ds_holdarg), KM_SLEEP); 361510242Schris.kirby@sun.com 361610242Schris.kirby@sun.com (void) strlcpy(ha->failed, dsname, sizeof (ha->failed)); 361710242Schris.kirby@sun.com 361810242Schris.kirby@sun.com error = spa_open(dsname, &spa, FTAG); 361910242Schris.kirby@sun.com if (error) { 362010242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 362110242Schris.kirby@sun.com return (error); 362210242Schris.kirby@sun.com } 362310242Schris.kirby@sun.com 362410242Schris.kirby@sun.com ha->dstg = dsl_sync_task_group_create(spa_get_dsl(spa)); 362510242Schris.kirby@sun.com ha->htag = htag; 362610242Schris.kirby@sun.com ha->snapname = snapname; 362710242Schris.kirby@sun.com ha->recursive = recursive; 362810242Schris.kirby@sun.com if (recursive) { 362910242Schris.kirby@sun.com error = dmu_objset_find(dsname, dsl_dataset_user_release_one, 363010242Schris.kirby@sun.com ha, DS_FIND_CHILDREN); 363110242Schris.kirby@sun.com } else { 363210242Schris.kirby@sun.com error = dsl_dataset_user_release_one(dsname, ha); 363310242Schris.kirby@sun.com } 363410242Schris.kirby@sun.com if (error == 0) 363510242Schris.kirby@sun.com error = dsl_sync_task_group_wait(ha->dstg); 363610242Schris.kirby@sun.com 363710242Schris.kirby@sun.com for (dst = list_head(&ha->dstg->dstg_tasks); dst; 363810242Schris.kirby@sun.com dst = list_next(&ha->dstg->dstg_tasks, dst)) { 363910242Schris.kirby@sun.com struct dsl_ds_releasearg *ra = dst->dst_arg1; 364010242Schris.kirby@sun.com dsl_dataset_t *ds = ra->ds; 364110242Schris.kirby@sun.com 364210242Schris.kirby@sun.com if (dst->dst_err) 364310242Schris.kirby@sun.com dsl_dataset_name(ds, ha->failed); 364410242Schris.kirby@sun.com 364510242Schris.kirby@sun.com if (ra->own) 364610242Schris.kirby@sun.com dsl_dataset_disown(ds, ha->dstg); 364710242Schris.kirby@sun.com else 364810242Schris.kirby@sun.com dsl_dataset_rele(ds, ha->dstg); 364910242Schris.kirby@sun.com 365010242Schris.kirby@sun.com kmem_free(ra, sizeof (struct dsl_ds_releasearg)); 365110242Schris.kirby@sun.com } 365210242Schris.kirby@sun.com 365310268Schris.kirby@sun.com if (error == 0 && recursive && !ha->gotone) 365410268Schris.kirby@sun.com error = ENOENT; 365510268Schris.kirby@sun.com 365611546SChris.Kirby@sun.com if (error && error != EBUSY) 365711209SMatthew.Ahrens@Sun.COM (void) strlcpy(dsname, ha->failed, sizeof (ha->failed)); 365810242Schris.kirby@sun.com 365910242Schris.kirby@sun.com dsl_sync_task_group_destroy(ha->dstg); 366010242Schris.kirby@sun.com kmem_free(ha, sizeof (struct dsl_ds_holdarg)); 366110242Schris.kirby@sun.com spa_close(spa, FTAG); 366211546SChris.Kirby@sun.com 366311546SChris.Kirby@sun.com /* 366411546SChris.Kirby@sun.com * We can get EBUSY if we were racing with deferred destroy and 366511546SChris.Kirby@sun.com * dsl_dataset_user_release_check() hadn't done the necessary 366611546SChris.Kirby@sun.com * open context setup. We can also get EBUSY if we're racing 366711546SChris.Kirby@sun.com * with destroy and that thread is the ds_owner. Either way 366811546SChris.Kirby@sun.com * the busy condition should be transient, and we should retry 366911546SChris.Kirby@sun.com * the release operation. 367011546SChris.Kirby@sun.com */ 367111546SChris.Kirby@sun.com if (error == EBUSY) 367211546SChris.Kirby@sun.com goto top; 367311546SChris.Kirby@sun.com 367410242Schris.kirby@sun.com return (error); 367510242Schris.kirby@sun.com } 367610242Schris.kirby@sun.com 367710342Schris.kirby@sun.com /* 367810342Schris.kirby@sun.com * Called at spa_load time to release a stale temporary user hold. 367910342Schris.kirby@sun.com */ 368010342Schris.kirby@sun.com int 368110342Schris.kirby@sun.com dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, char *htag) 368210342Schris.kirby@sun.com { 368310342Schris.kirby@sun.com dsl_dataset_t *ds; 368410342Schris.kirby@sun.com char *snap; 368510342Schris.kirby@sun.com char *name; 368610342Schris.kirby@sun.com int namelen; 368710342Schris.kirby@sun.com int error; 368810342Schris.kirby@sun.com 368910342Schris.kirby@sun.com rw_enter(&dp->dp_config_rwlock, RW_READER); 369010342Schris.kirby@sun.com error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds); 369110342Schris.kirby@sun.com rw_exit(&dp->dp_config_rwlock); 369210342Schris.kirby@sun.com if (error) 369310342Schris.kirby@sun.com return (error); 369410342Schris.kirby@sun.com namelen = dsl_dataset_namelen(ds)+1; 369510342Schris.kirby@sun.com name = kmem_alloc(namelen, KM_SLEEP); 369610342Schris.kirby@sun.com dsl_dataset_name(ds, name); 369710342Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 369810342Schris.kirby@sun.com 369910342Schris.kirby@sun.com snap = strchr(name, '@'); 370010342Schris.kirby@sun.com *snap = '\0'; 370110342Schris.kirby@sun.com ++snap; 370210342Schris.kirby@sun.com return (dsl_dataset_user_release(name, snap, htag, B_FALSE)); 370310342Schris.kirby@sun.com } 370410342Schris.kirby@sun.com 370510242Schris.kirby@sun.com int 370610242Schris.kirby@sun.com dsl_dataset_get_holds(const char *dsname, nvlist_t **nvp) 370710242Schris.kirby@sun.com { 370810242Schris.kirby@sun.com dsl_dataset_t *ds; 370910242Schris.kirby@sun.com int err; 371010242Schris.kirby@sun.com 371110242Schris.kirby@sun.com err = dsl_dataset_hold(dsname, FTAG, &ds); 371210242Schris.kirby@sun.com if (err) 371310242Schris.kirby@sun.com return (err); 371410242Schris.kirby@sun.com 371510242Schris.kirby@sun.com VERIFY(0 == nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP)); 371610242Schris.kirby@sun.com if (ds->ds_phys->ds_userrefs_obj != 0) { 371710242Schris.kirby@sun.com zap_attribute_t *za; 371810242Schris.kirby@sun.com zap_cursor_t zc; 371910242Schris.kirby@sun.com 372010242Schris.kirby@sun.com za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 372110242Schris.kirby@sun.com for (zap_cursor_init(&zc, ds->ds_dir->dd_pool->dp_meta_objset, 372210242Schris.kirby@sun.com ds->ds_phys->ds_userrefs_obj); 372310242Schris.kirby@sun.com zap_cursor_retrieve(&zc, za) == 0; 372410242Schris.kirby@sun.com zap_cursor_advance(&zc)) { 372510242Schris.kirby@sun.com VERIFY(0 == nvlist_add_uint64(*nvp, za->za_name, 372610242Schris.kirby@sun.com za->za_first_integer)); 372710242Schris.kirby@sun.com } 372810242Schris.kirby@sun.com zap_cursor_fini(&zc); 372910242Schris.kirby@sun.com kmem_free(za, sizeof (zap_attribute_t)); 373010242Schris.kirby@sun.com } 373110242Schris.kirby@sun.com dsl_dataset_rele(ds, FTAG); 373210242Schris.kirby@sun.com return (0); 373310242Schris.kirby@sun.com } 373410298SMatthew.Ahrens@Sun.COM 373510298SMatthew.Ahrens@Sun.COM /* 373610298SMatthew.Ahrens@Sun.COM * Note, this fuction is used as the callback for dmu_objset_find(). We 373710298SMatthew.Ahrens@Sun.COM * always return 0 so that we will continue to find and process 373810298SMatthew.Ahrens@Sun.COM * inconsistent datasets, even if we encounter an error trying to 373910298SMatthew.Ahrens@Sun.COM * process one of them. 374010298SMatthew.Ahrens@Sun.COM */ 374110298SMatthew.Ahrens@Sun.COM /* ARGSUSED */ 374210298SMatthew.Ahrens@Sun.COM int 374311209SMatthew.Ahrens@Sun.COM dsl_destroy_inconsistent(const char *dsname, void *arg) 374410298SMatthew.Ahrens@Sun.COM { 374510298SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds; 374610298SMatthew.Ahrens@Sun.COM 374710298SMatthew.Ahrens@Sun.COM if (dsl_dataset_own(dsname, B_TRUE, FTAG, &ds) == 0) { 374810298SMatthew.Ahrens@Sun.COM if (DS_IS_INCONSISTENT(ds)) 374910298SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(ds, FTAG, B_FALSE); 375010298SMatthew.Ahrens@Sun.COM else 375110298SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 375210298SMatthew.Ahrens@Sun.COM } 375310298SMatthew.Ahrens@Sun.COM return (0); 375410298SMatthew.Ahrens@Sun.COM } 3755