1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 225afc78aaSChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 233b2aab18SMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved. 244e3c9f44SBill Pijewski * Copyright (c) 2012, Joyent, Inc. All rights reserved. 25fa9e4066Sahrens */ 26fa9e4066Sahrens 27fa9e4066Sahrens #include <sys/dmu_objset.h> 28fa9e4066Sahrens #include <sys/dsl_dataset.h> 29fa9e4066Sahrens #include <sys/dsl_dir.h> 3099653d4eSeschrock #include <sys/dsl_prop.h> 311d452cf5Sahrens #include <sys/dsl_synctask.h> 32fa9e4066Sahrens #include <sys/dmu_traverse.h> 334e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 34fa9e4066Sahrens #include <sys/dmu_tx.h> 35fa9e4066Sahrens #include <sys/arc.h> 36fa9e4066Sahrens #include <sys/zio.h> 37fa9e4066Sahrens #include <sys/zap.h> 38ad135b5dSChristopher Siden #include <sys/zfeature.h> 39fa9e4066Sahrens #include <sys/unique.h> 40fa9e4066Sahrens #include <sys/zfs_context.h> 41cdf5b4caSmmusante #include <sys/zfs_ioctl.h> 42ecd6cf80Smarks #include <sys/spa.h> 43088f3894Sahrens #include <sys/zfs_znode.h> 44c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 45842727c2SChris Kirby #include <sys/zvol.h> 463f9d6ad7SLin Ling #include <sys/dsl_scan.h> 47cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h> 483b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 493b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 50e1930233Sbonwick 51cde58dbcSMatthew Ahrens #define SWITCH64(x, y) \ 52cde58dbcSMatthew Ahrens { \ 53cde58dbcSMatthew Ahrens uint64_t __tmp = (x); \ 54cde58dbcSMatthew Ahrens (x) = (y); \ 55cde58dbcSMatthew Ahrens (y) = __tmp; \ 56cde58dbcSMatthew Ahrens } 57cde58dbcSMatthew Ahrens 5855434c77Sek #define DS_REF_MAX (1ULL << 62) 59fa9e4066Sahrens 60fa9e4066Sahrens #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 61fa9e4066Sahrens 62a9799022Sck /* 63a9799022Sck * Figure out how much of this delta should be propogated to the dsl_dir 64a9799022Sck * layer. If there's a refreservation, that space has already been 65a9799022Sck * partially accounted for in our ancestors. 66a9799022Sck */ 67a9799022Sck static int64_t 68a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta) 69a9799022Sck { 70a9799022Sck uint64_t old_bytes, new_bytes; 71a9799022Sck 72a9799022Sck if (ds->ds_reserved == 0) 73a9799022Sck return (delta); 74a9799022Sck 75a9799022Sck old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 76a9799022Sck new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 77a9799022Sck 78a9799022Sck ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 79a9799022Sck return (new_bytes - old_bytes); 80a9799022Sck } 81fa9e4066Sahrens 82fa9e4066Sahrens void 83b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 84fa9e4066Sahrens { 85b24ab676SJeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 86fa9e4066Sahrens int compressed = BP_GET_PSIZE(bp); 87fa9e4066Sahrens int uncompressed = BP_GET_UCSIZE(bp); 88a9799022Sck int64_t delta; 89fa9e4066Sahrens 903f9d6ad7SLin Ling dprintf_bp(bp, "ds=%p", ds); 91fa9e4066Sahrens 92fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 93fa9e4066Sahrens /* It could have been compressed away to nothing */ 94fa9e4066Sahrens if (BP_IS_HOLE(bp)) 95fa9e4066Sahrens return; 96fa9e4066Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 97ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 98fa9e4066Sahrens if (ds == NULL) { 99ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 100ce636f8bSMatthew Ahrens used, compressed, uncompressed); 101fa9e4066Sahrens return; 102fa9e4066Sahrens } 103fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1043f9d6ad7SLin Ling 10502c8f3f0SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 106fa9e4066Sahrens mutex_enter(&ds->ds_lock); 107a9799022Sck delta = parent_delta(ds, used); 108ad135b5dSChristopher Siden ds->ds_phys->ds_referenced_bytes += used; 109fa9e4066Sahrens ds->ds_phys->ds_compressed_bytes += compressed; 110fa9e4066Sahrens ds->ds_phys->ds_uncompressed_bytes += uncompressed; 111fa9e4066Sahrens ds->ds_phys->ds_unique_bytes += used; 112fa9e4066Sahrens mutex_exit(&ds->ds_lock); 11374e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 11474e7dc98SMatthew Ahrens compressed, uncompressed, tx); 11574e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used - delta, 11674e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 11702c8f3f0SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 118fa9e4066Sahrens } 119fa9e4066Sahrens 120cdb0ab79Smaybee int 121b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 122b24ab676SJeff Bonwick boolean_t async) 123fa9e4066Sahrens { 124fa9e4066Sahrens if (BP_IS_HOLE(bp)) 125cdb0ab79Smaybee return (0); 126fa9e4066Sahrens 127b24ab676SJeff Bonwick ASSERT(dmu_tx_is_syncing(tx)); 128b24ab676SJeff Bonwick ASSERT(bp->blk_birth <= tx->tx_txg); 129b24ab676SJeff Bonwick 130b24ab676SJeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 131b24ab676SJeff Bonwick int compressed = BP_GET_PSIZE(bp); 132b24ab676SJeff Bonwick int uncompressed = BP_GET_UCSIZE(bp); 133b24ab676SJeff Bonwick 134fa9e4066Sahrens ASSERT(used > 0); 135fa9e4066Sahrens if (ds == NULL) { 136b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 137ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 138ce636f8bSMatthew Ahrens -used, -compressed, -uncompressed); 139cdb0ab79Smaybee return (used); 140fa9e4066Sahrens } 141fa9e4066Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 142fa9e4066Sahrens 14374e7dc98SMatthew Ahrens ASSERT(!dsl_dataset_is_snapshot(ds)); 144fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 145fa9e4066Sahrens 146fa9e4066Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 147a9799022Sck int64_t delta; 148c717a561Smaybee 1493f9d6ad7SLin Ling dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 150b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 151fa9e4066Sahrens 15202c8f3f0SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 153fa9e4066Sahrens mutex_enter(&ds->ds_lock); 154a9799022Sck ASSERT(ds->ds_phys->ds_unique_bytes >= used || 155a9799022Sck !DS_UNIQUE_IS_ACCURATE(ds)); 156a9799022Sck delta = parent_delta(ds, -used); 157fa9e4066Sahrens ds->ds_phys->ds_unique_bytes -= used; 158fa9e4066Sahrens mutex_exit(&ds->ds_lock); 15974e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 160a9799022Sck delta, -compressed, -uncompressed, tx); 16174e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, -used - delta, 16274e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 16302c8f3f0SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 164fa9e4066Sahrens } else { 165fa9e4066Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 166b24ab676SJeff Bonwick if (async) { 167b24ab676SJeff Bonwick /* 168b24ab676SJeff Bonwick * We are here as part of zio's write done callback, 169b24ab676SJeff Bonwick * which means we're a zio interrupt thread. We can't 170cde58dbcSMatthew Ahrens * call dsl_deadlist_insert() now because it may block 171b24ab676SJeff Bonwick * waiting for I/O. Instead, put bp on the deferred 172b24ab676SJeff Bonwick * queue and let dsl_pool_sync() finish the job. 173b24ab676SJeff Bonwick */ 174cde58dbcSMatthew Ahrens bplist_append(&ds->ds_pending_deadlist, bp); 175b24ab676SJeff Bonwick } else { 176cde58dbcSMatthew Ahrens dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 177b24ab676SJeff Bonwick } 178a4611edeSahrens ASSERT3U(ds->ds_prev->ds_object, ==, 179a4611edeSahrens ds->ds_phys->ds_prev_snap_obj); 180a4611edeSahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 181fa9e4066Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 182a4611edeSahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 183a4611edeSahrens ds->ds_object && bp->blk_birth > 184a4611edeSahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 185a4611edeSahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 186a4611edeSahrens mutex_enter(&ds->ds_prev->ds_lock); 187a4611edeSahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 188a4611edeSahrens mutex_exit(&ds->ds_prev->ds_lock); 189fa9e4066Sahrens } 1903f9d6ad7SLin Ling if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 19174e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used, 19274e7dc98SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 19374e7dc98SMatthew Ahrens } 194fa9e4066Sahrens } 195fa9e4066Sahrens mutex_enter(&ds->ds_lock); 196ad135b5dSChristopher Siden ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used); 197ad135b5dSChristopher Siden ds->ds_phys->ds_referenced_bytes -= used; 198fa9e4066Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 199fa9e4066Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 200fa9e4066Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 201fa9e4066Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 202fa9e4066Sahrens mutex_exit(&ds->ds_lock); 203cdb0ab79Smaybee 204cdb0ab79Smaybee return (used); 205fa9e4066Sahrens } 206fa9e4066Sahrens 207ea8dc4b6Seschrock uint64_t 208ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 209fa9e4066Sahrens { 210a2eea2e1Sahrens uint64_t trysnap = 0; 211a2eea2e1Sahrens 212fa9e4066Sahrens if (ds == NULL) 213ea8dc4b6Seschrock return (0); 214fa9e4066Sahrens /* 215fa9e4066Sahrens * The snapshot creation could fail, but that would cause an 216fa9e4066Sahrens * incorrect FALSE return, which would only result in an 217fa9e4066Sahrens * overestimation of the amount of space that an operation would 218fa9e4066Sahrens * consume, which is OK. 219fa9e4066Sahrens * 220fa9e4066Sahrens * There's also a small window where we could miss a pending 221fa9e4066Sahrens * snapshot, because we could set the sync task in the quiescing 222fa9e4066Sahrens * phase. So this should only be used as a guess. 223fa9e4066Sahrens */ 224a2eea2e1Sahrens if (ds->ds_trysnap_txg > 225a2eea2e1Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 226a2eea2e1Sahrens trysnap = ds->ds_trysnap_txg; 227a2eea2e1Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 228ea8dc4b6Seschrock } 229ea8dc4b6Seschrock 2303d692628SSanjeev Bagewadi boolean_t 231c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp, 232c7cd2421SGeorge Wilson uint64_t blk_birth) 233ea8dc4b6Seschrock { 234c7cd2421SGeorge Wilson if (blk_birth <= dsl_dataset_prev_snap_txg(ds)) 235c7cd2421SGeorge Wilson return (B_FALSE); 236c7cd2421SGeorge Wilson 237837b568bSGeorge Wilson ddt_prefetch(dsl_dataset_get_spa(ds), bp); 238c7cd2421SGeorge Wilson 239c7cd2421SGeorge Wilson return (B_TRUE); 240fa9e4066Sahrens } 241fa9e4066Sahrens 242fa9e4066Sahrens /* ARGSUSED */ 243fa9e4066Sahrens static void 244fa9e4066Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 245fa9e4066Sahrens { 246fa9e4066Sahrens dsl_dataset_t *ds = dsv; 247fa9e4066Sahrens 2483b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == NULL); 249fa9e4066Sahrens 25091ebeef5Sahrens unique_remove(ds->ds_fsid_guid); 251fa9e4066Sahrens 252503ad85cSMatthew Ahrens if (ds->ds_objset != NULL) 253503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 254fa9e4066Sahrens 255fa9e4066Sahrens if (ds->ds_prev) { 2563b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 257fa9e4066Sahrens ds->ds_prev = NULL; 258fa9e4066Sahrens } 259fa9e4066Sahrens 260cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 2613b2aab18SMatthew Ahrens if (ds->ds_phys->ds_deadlist_obj != 0) 262cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 263745cd3c5Smaybee if (ds->ds_dir) 2643b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 265fa9e4066Sahrens 26691ebeef5Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 267fa9e4066Sahrens 2685ad82045Snd mutex_destroy(&ds->ds_lock); 26991ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 2703b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 2715ad82045Snd 272fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 273fa9e4066Sahrens } 274fa9e4066Sahrens 2753b2aab18SMatthew Ahrens int 276fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 277fa9e4066Sahrens { 278fa9e4066Sahrens dsl_dataset_phys_t *headphys; 279fa9e4066Sahrens int err; 280fa9e4066Sahrens dmu_buf_t *headdbuf; 281fa9e4066Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 282fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 283fa9e4066Sahrens 284fa9e4066Sahrens if (ds->ds_snapname[0]) 285ea8dc4b6Seschrock return (0); 286fa9e4066Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 287ea8dc4b6Seschrock return (0); 288fa9e4066Sahrens 289ea8dc4b6Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 290ea8dc4b6Seschrock FTAG, &headdbuf); 2913b2aab18SMatthew Ahrens if (err != 0) 292ea8dc4b6Seschrock return (err); 293fa9e4066Sahrens headphys = headdbuf->db_data; 294fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 295e7437265Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 296ea8dc4b6Seschrock dmu_buf_rele(headdbuf, FTAG); 297ea8dc4b6Seschrock return (err); 298fa9e4066Sahrens } 299fa9e4066Sahrens 3003b2aab18SMatthew Ahrens int 301745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 302ab04eb8eStimh { 303745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 304745cd3c5Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 305ab04eb8eStimh matchtype_t mt; 306ab04eb8eStimh int err; 307ab04eb8eStimh 308745cd3c5Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 309ab04eb8eStimh mt = MT_FIRST; 310ab04eb8eStimh else 311ab04eb8eStimh mt = MT_EXACT; 312ab04eb8eStimh 313745cd3c5Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 314ab04eb8eStimh value, mt, NULL, 0, NULL); 315ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 316745cd3c5Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 317ab04eb8eStimh return (err); 318ab04eb8eStimh } 319ab04eb8eStimh 3203b2aab18SMatthew Ahrens int 3213b2aab18SMatthew Ahrens dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx) 322ab04eb8eStimh { 323745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 324745cd3c5Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 325ab04eb8eStimh matchtype_t mt; 326ab04eb8eStimh int err; 327ab04eb8eStimh 32871eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 32971eb0538SChris Kirby 330745cd3c5Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 331ab04eb8eStimh mt = MT_FIRST; 332ab04eb8eStimh else 333ab04eb8eStimh mt = MT_EXACT; 334ab04eb8eStimh 335745cd3c5Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 336ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 337745cd3c5Smaybee err = zap_remove(mos, snapobj, name, tx); 338ab04eb8eStimh return (err); 339ab04eb8eStimh } 340ab04eb8eStimh 3413b2aab18SMatthew Ahrens int 3423b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 343745cd3c5Smaybee dsl_dataset_t **dsp) 344fa9e4066Sahrens { 345fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 346fa9e4066Sahrens dmu_buf_t *dbuf; 347fa9e4066Sahrens dsl_dataset_t *ds; 348ea8dc4b6Seschrock int err; 349a7f53a56SChris Kirby dmu_object_info_t doi; 350fa9e4066Sahrens 3513b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 352fa9e4066Sahrens 353ea8dc4b6Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3543b2aab18SMatthew Ahrens if (err != 0) 355ea8dc4b6Seschrock return (err); 356a7f53a56SChris Kirby 357a7f53a56SChris Kirby /* Make sure dsobj has the correct object type. */ 358a7f53a56SChris Kirby dmu_object_info_from_db(dbuf, &doi); 359*b287be1bSWill Andrews if (doi.doi_type != DMU_OT_DSL_DATASET) { 360*b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 361be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 362*b287be1bSWill Andrews } 363a7f53a56SChris Kirby 364fa9e4066Sahrens ds = dmu_buf_get_user(dbuf); 365fa9e4066Sahrens if (ds == NULL) { 366d5285caeSGeorge Wilson dsl_dataset_t *winner = NULL; 367fa9e4066Sahrens 368fa9e4066Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 369fa9e4066Sahrens ds->ds_dbuf = dbuf; 370fa9e4066Sahrens ds->ds_object = dsobj; 371fa9e4066Sahrens ds->ds_phys = dbuf->db_data; 372fa9e4066Sahrens 3735ad82045Snd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 37491ebeef5Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3754e3c9f44SBill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 3763b2aab18SMatthew Ahrens refcount_create(&ds->ds_longholds); 3775ad82045Snd 378cde58dbcSMatthew Ahrens bplist_create(&ds->ds_pending_deadlist); 379cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, 380fa9e4066Sahrens mos, ds->ds_phys->ds_deadlist_obj); 381cde58dbcSMatthew Ahrens 3824e3c9f44SBill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 3834e3c9f44SBill Pijewski offsetof(dmu_sendarg_t, dsa_link)); 3844e3c9f44SBill Pijewski 385ea8dc4b6Seschrock if (err == 0) { 3863b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, 387ea8dc4b6Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 388ea8dc4b6Seschrock } 3893b2aab18SMatthew Ahrens if (err != 0) { 3905ad82045Snd mutex_destroy(&ds->ds_lock); 39191ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 3923b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 393cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 394cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 395ea8dc4b6Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 396ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 397ea8dc4b6Seschrock return (err); 398ea8dc4b6Seschrock } 399fa9e4066Sahrens 40074e7dc98SMatthew Ahrens if (!dsl_dataset_is_snapshot(ds)) { 401fa9e4066Sahrens ds->ds_snapname[0] = '\0'; 4023b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 4033b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 404745cd3c5Smaybee ds->ds_phys->ds_prev_snap_obj, 405745cd3c5Smaybee ds, &ds->ds_prev); 406fa9e4066Sahrens } 407842727c2SChris Kirby } else { 408842727c2SChris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 409842727c2SChris Kirby err = dsl_dataset_get_snapname(ds); 410842727c2SChris Kirby if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) { 411842727c2SChris Kirby err = zap_count( 412842727c2SChris Kirby ds->ds_dir->dd_pool->dp_meta_objset, 413842727c2SChris Kirby ds->ds_phys->ds_userrefs_obj, 414842727c2SChris Kirby &ds->ds_userrefs); 415842727c2SChris Kirby } 416fa9e4066Sahrens } 417fa9e4066Sahrens 41874e7dc98SMatthew Ahrens if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 4193b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4203b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 4213b2aab18SMatthew Ahrens &ds->ds_reserved); 422cb625fb5Sck if (err == 0) { 4233b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4243b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 4253b2aab18SMatthew Ahrens &ds->ds_quota); 426cb625fb5Sck } 427cb625fb5Sck } else { 428cb625fb5Sck ds->ds_reserved = ds->ds_quota = 0; 429cb625fb5Sck } 430cb625fb5Sck 431d5285caeSGeorge Wilson if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds, 432d5285caeSGeorge Wilson &ds->ds_phys, dsl_dataset_evict)) != NULL) { 433cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 434cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 435745cd3c5Smaybee if (ds->ds_prev) 4363b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 4373b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 4385ad82045Snd mutex_destroy(&ds->ds_lock); 43991ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 4403b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 441fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4423b2aab18SMatthew Ahrens if (err != 0) { 443ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 444ea8dc4b6Seschrock return (err); 445ea8dc4b6Seschrock } 446fa9e4066Sahrens ds = winner; 447fa9e4066Sahrens } else { 44891ebeef5Sahrens ds->ds_fsid_guid = 449fa9e4066Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 450fa9e4066Sahrens } 451fa9e4066Sahrens } 452fa9e4066Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 453fa9e4066Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 454088f3894Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 455afc6333aSahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 45684db2a68Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 457ea8dc4b6Seschrock *dsp = ds; 458ea8dc4b6Seschrock return (0); 459fa9e4066Sahrens } 460fa9e4066Sahrens 461745cd3c5Smaybee int 4623b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name, 463503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 464fa9e4066Sahrens { 465fa9e4066Sahrens dsl_dir_t *dd; 466745cd3c5Smaybee const char *snapname; 467fa9e4066Sahrens uint64_t obj; 468fa9e4066Sahrens int err = 0; 469fa9e4066Sahrens 4703b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 4713b2aab18SMatthew Ahrens if (err != 0) 472ea8dc4b6Seschrock return (err); 473fa9e4066Sahrens 4743b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 475fa9e4066Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 4763b2aab18SMatthew Ahrens if (obj != 0) 4773b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, dsp); 478745cd3c5Smaybee else 479be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 480fa9e4066Sahrens 481745cd3c5Smaybee /* we may be looking for a snapshot */ 482745cd3c5Smaybee if (err == 0 && snapname != NULL) { 4833b2aab18SMatthew Ahrens dsl_dataset_t *ds; 484fa9e4066Sahrens 485745cd3c5Smaybee if (*snapname++ != '@') { 486745cd3c5Smaybee dsl_dataset_rele(*dsp, tag); 4873b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 488be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 489fa9e4066Sahrens } 490fa9e4066Sahrens 491745cd3c5Smaybee dprintf("looking for snapshot '%s'\n", snapname); 492745cd3c5Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 493745cd3c5Smaybee if (err == 0) 4943b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 495745cd3c5Smaybee dsl_dataset_rele(*dsp, tag); 496745cd3c5Smaybee 4973b2aab18SMatthew Ahrens if (err == 0) { 498745cd3c5Smaybee mutex_enter(&ds->ds_lock); 499745cd3c5Smaybee if (ds->ds_snapname[0] == 0) 500745cd3c5Smaybee (void) strlcpy(ds->ds_snapname, snapname, 501745cd3c5Smaybee sizeof (ds->ds_snapname)); 502745cd3c5Smaybee mutex_exit(&ds->ds_lock); 5033b2aab18SMatthew Ahrens *dsp = ds; 504fa9e4066Sahrens } 505fa9e4066Sahrens } 5063b2aab18SMatthew Ahrens 5073b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 508fa9e4066Sahrens return (err); 509fa9e4066Sahrens } 510fa9e4066Sahrens 511fa9e4066Sahrens int 5123b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 5133b2aab18SMatthew Ahrens void *tag, dsl_dataset_t **dsp) 5143b2aab18SMatthew Ahrens { 5153b2aab18SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5163b2aab18SMatthew Ahrens if (err != 0) 5173b2aab18SMatthew Ahrens return (err); 5183b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 5193b2aab18SMatthew Ahrens dsl_dataset_rele(*dsp, tag); 5203b2aab18SMatthew Ahrens *dsp = NULL; 521be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 5223b2aab18SMatthew Ahrens } 5233b2aab18SMatthew Ahrens return (0); 5243b2aab18SMatthew Ahrens } 5253b2aab18SMatthew Ahrens 5263b2aab18SMatthew Ahrens int 5273b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name, 528503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 529fa9e4066Sahrens { 5303b2aab18SMatthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp); 5313b2aab18SMatthew Ahrens if (err != 0) 532745cd3c5Smaybee return (err); 5333b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 534503ad85cSMatthew Ahrens dsl_dataset_rele(*dsp, tag); 535be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 536745cd3c5Smaybee } 537745cd3c5Smaybee return (0); 538fa9e4066Sahrens } 539fa9e4066Sahrens 5403b2aab18SMatthew Ahrens /* 5413b2aab18SMatthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long 5423b2aab18SMatthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold 5433b2aab18SMatthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()). 5443b2aab18SMatthew Ahrens * 5453b2aab18SMatthew Ahrens * The dataset and pool must be held when this function is called. After it 5463b2aab18SMatthew Ahrens * is called, the pool hold may be released while the dataset is still held 5473b2aab18SMatthew Ahrens * and accessed. 5483b2aab18SMatthew Ahrens */ 5493b2aab18SMatthew Ahrens void 5503b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 5513b2aab18SMatthew Ahrens { 5523b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 5533b2aab18SMatthew Ahrens (void) refcount_add(&ds->ds_longholds, tag); 5543b2aab18SMatthew Ahrens } 5553b2aab18SMatthew Ahrens 5563b2aab18SMatthew Ahrens void 5573b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 5583b2aab18SMatthew Ahrens { 5593b2aab18SMatthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag); 5603b2aab18SMatthew Ahrens } 5613b2aab18SMatthew Ahrens 5623b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */ 5633b2aab18SMatthew Ahrens boolean_t 5643b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds) 5653b2aab18SMatthew Ahrens { 5663b2aab18SMatthew Ahrens return (!refcount_is_zero(&ds->ds_longholds)); 5673b2aab18SMatthew Ahrens } 5683b2aab18SMatthew Ahrens 569fa9e4066Sahrens void 570fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 571fa9e4066Sahrens { 572fa9e4066Sahrens if (ds == NULL) { 573fa9e4066Sahrens (void) strcpy(name, "mos"); 574fa9e4066Sahrens } else { 575fa9e4066Sahrens dsl_dir_name(ds->ds_dir, name); 5763b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 577fa9e4066Sahrens if (ds->ds_snapname[0]) { 578fa9e4066Sahrens (void) strcat(name, "@"); 579745cd3c5Smaybee /* 580745cd3c5Smaybee * We use a "recursive" mutex so that we 581745cd3c5Smaybee * can call dprintf_ds() with ds_lock held. 582745cd3c5Smaybee */ 583fa9e4066Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 584fa9e4066Sahrens mutex_enter(&ds->ds_lock); 585fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 586fa9e4066Sahrens mutex_exit(&ds->ds_lock); 587fa9e4066Sahrens } else { 588fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 589fa9e4066Sahrens } 590fa9e4066Sahrens } 591fa9e4066Sahrens } 592fa9e4066Sahrens } 593fa9e4066Sahrens 594b7661cccSmmusante static int 595b7661cccSmmusante dsl_dataset_namelen(dsl_dataset_t *ds) 596b7661cccSmmusante { 597b7661cccSmmusante int result; 598b7661cccSmmusante 599b7661cccSmmusante if (ds == NULL) { 600b7661cccSmmusante result = 3; /* "mos" */ 601b7661cccSmmusante } else { 602b7661cccSmmusante result = dsl_dir_namelen(ds->ds_dir); 6033b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 604b7661cccSmmusante if (ds->ds_snapname[0]) { 605b7661cccSmmusante ++result; /* adding one for the @-sign */ 606b7661cccSmmusante if (!MUTEX_HELD(&ds->ds_lock)) { 607b7661cccSmmusante mutex_enter(&ds->ds_lock); 608b7661cccSmmusante result += strlen(ds->ds_snapname); 609b7661cccSmmusante mutex_exit(&ds->ds_lock); 610b7661cccSmmusante } else { 611b7661cccSmmusante result += strlen(ds->ds_snapname); 612b7661cccSmmusante } 613b7661cccSmmusante } 614b7661cccSmmusante } 615b7661cccSmmusante 616b7661cccSmmusante return (result); 617b7661cccSmmusante } 618b7661cccSmmusante 6193cb34c60Sahrens void 620745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6213cb34c60Sahrens { 6223b2aab18SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag); 623745cd3c5Smaybee } 624745cd3c5Smaybee 625745cd3c5Smaybee void 626503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 627745cd3c5Smaybee { 6283b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL); 629745cd3c5Smaybee 6303cb34c60Sahrens mutex_enter(&ds->ds_lock); 631745cd3c5Smaybee ds->ds_owner = NULL; 6323cb34c60Sahrens mutex_exit(&ds->ds_lock); 6333b2aab18SMatthew Ahrens dsl_dataset_long_rele(ds, tag); 6343b2aab18SMatthew Ahrens if (ds->ds_dbuf != NULL) 6353b2aab18SMatthew Ahrens dsl_dataset_rele(ds, tag); 636745cd3c5Smaybee else 637cde58dbcSMatthew Ahrens dsl_dataset_evict(NULL, ds); 6383cb34c60Sahrens } 6393cb34c60Sahrens 6403cb34c60Sahrens boolean_t 6413b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 6423cb34c60Sahrens { 643745cd3c5Smaybee boolean_t gotit = FALSE; 644745cd3c5Smaybee 6453cb34c60Sahrens mutex_enter(&ds->ds_lock); 6463b2aab18SMatthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 647503ad85cSMatthew Ahrens ds->ds_owner = tag; 6483b2aab18SMatthew Ahrens dsl_dataset_long_hold(ds, tag); 649745cd3c5Smaybee gotit = TRUE; 6503cb34c60Sahrens } 6513cb34c60Sahrens mutex_exit(&ds->ds_lock); 652745cd3c5Smaybee return (gotit); 653745cd3c5Smaybee } 654745cd3c5Smaybee 6551d452cf5Sahrens uint64_t 656088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 657ab04eb8eStimh uint64_t flags, dmu_tx_t *tx) 658fa9e4066Sahrens { 6593cb34c60Sahrens dsl_pool_t *dp = dd->dd_pool; 660fa9e4066Sahrens dmu_buf_t *dbuf; 661fa9e4066Sahrens dsl_dataset_phys_t *dsphys; 6623cb34c60Sahrens uint64_t dsobj; 663fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 664fa9e4066Sahrens 665088f3894Sahrens if (origin == NULL) 666088f3894Sahrens origin = dp->dp_origin_snap; 667088f3894Sahrens 6683cb34c60Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 6693cb34c60Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 670fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 6713cb34c60Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 672fa9e4066Sahrens 6731649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 6741649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 6753b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 676fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 677fa9e4066Sahrens dsphys = dbuf->db_data; 678745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 679fa9e4066Sahrens dsphys->ds_dir_obj = dd->dd_object; 680ab04eb8eStimh dsphys->ds_flags = flags; 681fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 682fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 683fa9e4066Sahrens sizeof (dsphys->ds_guid)); 684fa9e4066Sahrens dsphys->ds_snapnames_zapobj = 685ab04eb8eStimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 686ab04eb8eStimh DMU_OT_NONE, 0, tx); 687fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 688088f3894Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 689a9799022Sck 690cde58dbcSMatthew Ahrens if (origin == NULL) { 691cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 692cde58dbcSMatthew Ahrens } else { 6933b2aab18SMatthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */ 694cde58dbcSMatthew Ahrens 6953cb34c60Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 696fa9e4066Sahrens dsphys->ds_prev_snap_txg = 6973cb34c60Sahrens origin->ds_phys->ds_creation_txg; 698ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = 699ad135b5dSChristopher Siden origin->ds_phys->ds_referenced_bytes; 700fa9e4066Sahrens dsphys->ds_compressed_bytes = 7013cb34c60Sahrens origin->ds_phys->ds_compressed_bytes; 702fa9e4066Sahrens dsphys->ds_uncompressed_bytes = 7033cb34c60Sahrens origin->ds_phys->ds_uncompressed_bytes; 7043cb34c60Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 705579ae4d5Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 706fa9e4066Sahrens 7073cb34c60Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 7083cb34c60Sahrens origin->ds_phys->ds_num_children++; 709fa9e4066Sahrens 7103b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 711cde58dbcSMatthew Ahrens origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds)); 712cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 713cde58dbcSMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 714cde58dbcSMatthew Ahrens dsl_dataset_rele(ohds, FTAG); 715cde58dbcSMatthew Ahrens 716088f3894Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 717088f3894Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 718088f3894Sahrens origin->ds_phys->ds_next_clones_obj = 719088f3894Sahrens zap_create(mos, 720088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 721088f3894Sahrens } 7223b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 7233b2aab18SMatthew Ahrens origin->ds_phys->ds_next_clones_obj, dsobj, tx)); 724088f3894Sahrens } 725088f3894Sahrens 726fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 7273cb34c60Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 728cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 729cde58dbcSMatthew Ahrens if (origin->ds_dir->dd_phys->dd_clones == 0) { 730cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 731cde58dbcSMatthew Ahrens origin->ds_dir->dd_phys->dd_clones = 732cde58dbcSMatthew Ahrens zap_create(mos, 733cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 734cde58dbcSMatthew Ahrens } 7353b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 736cde58dbcSMatthew Ahrens origin->ds_dir->dd_phys->dd_clones, dsobj, tx)); 737cde58dbcSMatthew Ahrens } 738fa9e4066Sahrens } 739ab04eb8eStimh 740ab04eb8eStimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 741ab04eb8eStimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 742ab04eb8eStimh 743ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 744fa9e4066Sahrens 745fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 746fa9e4066Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 7473cb34c60Sahrens 7483cb34c60Sahrens return (dsobj); 7493cb34c60Sahrens } 7503cb34c60Sahrens 7513b2aab18SMatthew Ahrens static void 7523b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 7533b2aab18SMatthew Ahrens { 7543b2aab18SMatthew Ahrens objset_t *os; 7553b2aab18SMatthew Ahrens 7563b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 7573b2aab18SMatthew Ahrens bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 7583b2aab18SMatthew Ahrens dsl_dataset_dirty(ds, tx); 7593b2aab18SMatthew Ahrens } 7603b2aab18SMatthew Ahrens 7613cb34c60Sahrens uint64_t 762ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 763ab04eb8eStimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 7643cb34c60Sahrens { 7653cb34c60Sahrens dsl_pool_t *dp = pdd->dd_pool; 7663cb34c60Sahrens uint64_t dsobj, ddobj; 7673cb34c60Sahrens dsl_dir_t *dd; 7683cb34c60Sahrens 7693b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 7703cb34c60Sahrens ASSERT(lastname[0] != '@'); 7713cb34c60Sahrens 772088f3894Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 7733b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 7743cb34c60Sahrens 7753b2aab18SMatthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, 7763b2aab18SMatthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx); 7773cb34c60Sahrens 7783cb34c60Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 7793cb34c60Sahrens 7803b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 781fa9e4066Sahrens 782feaa74e4SMark Maybee /* 783feaa74e4SMark Maybee * If we are creating a clone, make sure we zero out any stale 784feaa74e4SMark Maybee * data from the origin snapshots zil header. 785feaa74e4SMark Maybee */ 7863b2aab18SMatthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 787feaa74e4SMark Maybee dsl_dataset_t *ds; 788feaa74e4SMark Maybee 7893b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 7903b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 791feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 792feaa74e4SMark Maybee } 793feaa74e4SMark Maybee 7941d452cf5Sahrens return (dsobj); 795fa9e4066Sahrens } 796fa9e4066Sahrens 7971d452cf5Sahrens /* 7983b2aab18SMatthew Ahrens * The unique space in the head dataset can be calculated by subtracting 7993b2aab18SMatthew Ahrens * the space used in the most recent snapshot, that is still being used 8003b2aab18SMatthew Ahrens * in this file system, from the space currently in use. To figure out 8013b2aab18SMatthew Ahrens * the space in the most recent snapshot still in use, we need to take 8023b2aab18SMatthew Ahrens * the total space used in the snapshot and subtract out the space that 8033b2aab18SMatthew Ahrens * has been freed up since the snapshot was taken. 8041d452cf5Sahrens */ 8053b2aab18SMatthew Ahrens void 8063b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 8071d452cf5Sahrens { 8083b2aab18SMatthew Ahrens uint64_t mrs_used; 8093b2aab18SMatthew Ahrens uint64_t dlused, dlcomp, dluncomp; 8101d452cf5Sahrens 8113b2aab18SMatthew Ahrens ASSERT(!dsl_dataset_is_snapshot(ds)); 8121d452cf5Sahrens 8133b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj != 0) 8143b2aab18SMatthew Ahrens mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes; 8153b2aab18SMatthew Ahrens else 8163b2aab18SMatthew Ahrens mrs_used = 0; 817842727c2SChris Kirby 8183b2aab18SMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 819fa9e4066Sahrens 8203b2aab18SMatthew Ahrens ASSERT3U(dlused, <=, mrs_used); 8213b2aab18SMatthew Ahrens ds->ds_phys->ds_unique_bytes = 8223b2aab18SMatthew Ahrens ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused); 82319b94df9SMatthew Ahrens 8243b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 8253b2aab18SMatthew Ahrens SPA_VERSION_UNIQUE_ACCURATE) 8263b2aab18SMatthew Ahrens ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 827fa9e4066Sahrens } 828fa9e4066Sahrens 8293b2aab18SMatthew Ahrens void 8303b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 8313b2aab18SMatthew Ahrens dmu_tx_t *tx) 832842727c2SChris Kirby { 8333b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 8343b2aab18SMatthew Ahrens uint64_t count; 8353b2aab18SMatthew Ahrens int err; 8363b2aab18SMatthew Ahrens 8373b2aab18SMatthew Ahrens ASSERT(ds->ds_phys->ds_num_children >= 2); 8383b2aab18SMatthew Ahrens err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 8393b2aab18SMatthew Ahrens /* 8403b2aab18SMatthew Ahrens * The err should not be ENOENT, but a bug in a previous version 8413b2aab18SMatthew Ahrens * of the code could cause upgrade_clones_cb() to not set 8423b2aab18SMatthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry. 8433b2aab18SMatthew Ahrens * If we knew that the pool was created after 8443b2aab18SMatthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 8453b2aab18SMatthew Ahrens * ENOENT. However, at least we can check that we don't have 8463b2aab18SMatthew Ahrens * too many entries in the next_clones_obj even after failing to 8473b2aab18SMatthew Ahrens * remove this one. 8483b2aab18SMatthew Ahrens */ 8493b2aab18SMatthew Ahrens if (err != ENOENT) 8503b2aab18SMatthew Ahrens VERIFY0(err); 8513b2aab18SMatthew Ahrens ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj, 8523b2aab18SMatthew Ahrens &count)); 8533b2aab18SMatthew Ahrens ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 8543b2aab18SMatthew Ahrens } 855842727c2SChris Kirby 856842727c2SChris Kirby 8573b2aab18SMatthew Ahrens blkptr_t * 8583b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds) 8593b2aab18SMatthew Ahrens { 8603b2aab18SMatthew Ahrens return (&ds->ds_phys->ds_bp); 861842727c2SChris Kirby } 862842727c2SChris Kirby 8633b2aab18SMatthew Ahrens void 8643b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 865842727c2SChris Kirby { 8663b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 8673b2aab18SMatthew Ahrens /* If it's the meta-objset, set dp_meta_rootbp */ 8683b2aab18SMatthew Ahrens if (ds == NULL) { 8693b2aab18SMatthew Ahrens tx->tx_pool->dp_meta_rootbp = *bp; 8703b2aab18SMatthew Ahrens } else { 8713b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 8723b2aab18SMatthew Ahrens ds->ds_phys->ds_bp = *bp; 873842727c2SChris Kirby } 8743b2aab18SMatthew Ahrens } 875842727c2SChris Kirby 8763b2aab18SMatthew Ahrens spa_t * 8773b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 8783b2aab18SMatthew Ahrens { 8793b2aab18SMatthew Ahrens return (ds->ds_dir->dd_pool->dp_spa); 880842727c2SChris Kirby } 881842727c2SChris Kirby 8823b2aab18SMatthew Ahrens void 8833b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 884fa9e4066Sahrens { 8853b2aab18SMatthew Ahrens dsl_pool_t *dp; 886842727c2SChris Kirby 8873b2aab18SMatthew Ahrens if (ds == NULL) /* this is the meta-objset */ 8883b2aab18SMatthew Ahrens return; 8891d452cf5Sahrens 8903b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 891fa9e4066Sahrens 8923b2aab18SMatthew Ahrens if (ds->ds_phys->ds_next_snap_obj != 0) 8933b2aab18SMatthew Ahrens panic("dirtying snapshot!"); 894fa9e4066Sahrens 8953b2aab18SMatthew Ahrens dp = ds->ds_dir->dd_pool; 896ce636f8bSMatthew Ahrens 8973b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 8983b2aab18SMatthew Ahrens /* up the hold count until we can be written out */ 8993b2aab18SMatthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 9003b2aab18SMatthew Ahrens } 9013b2aab18SMatthew Ahrens } 902fa9e4066Sahrens 9032e2c1355SMatthew Ahrens boolean_t 9042e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds) 9052e2c1355SMatthew Ahrens { 9062e2c1355SMatthew Ahrens for (int t = 0; t < TXG_SIZE; t++) { 9072e2c1355SMatthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 9082e2c1355SMatthew Ahrens ds, t)) 9092e2c1355SMatthew Ahrens return (B_TRUE); 9102e2c1355SMatthew Ahrens } 9112e2c1355SMatthew Ahrens return (B_FALSE); 9122e2c1355SMatthew Ahrens } 9132e2c1355SMatthew Ahrens 914fa9e4066Sahrens static int 9153b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 916fa9e4066Sahrens { 9173b2aab18SMatthew Ahrens uint64_t asize; 918fa9e4066Sahrens 9193b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 92088b7b0f2SMatthew Ahrens return (0); 921fa9e4066Sahrens 922e1930233Sbonwick /* 9233b2aab18SMatthew Ahrens * If there's an fs-only reservation, any blocks that might become 9243b2aab18SMatthew Ahrens * owned by the snapshot dataset must be accommodated by space 9253b2aab18SMatthew Ahrens * outside of the reservation. 926e1930233Sbonwick */ 9273b2aab18SMatthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 9283b2aab18SMatthew Ahrens asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 9293b2aab18SMatthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 930be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 931e1930233Sbonwick 9323cb34c60Sahrens /* 9333b2aab18SMatthew Ahrens * Propagate any reserved space for this snapshot to other 9343b2aab18SMatthew Ahrens * snapshot checks in this sync group. 9353cb34c60Sahrens */ 9363b2aab18SMatthew Ahrens if (asize > 0) 9373b2aab18SMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx); 9383cb34c60Sahrens 939e1930233Sbonwick return (0); 940e1930233Sbonwick } 941e1930233Sbonwick 9423b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg { 9433b2aab18SMatthew Ahrens nvlist_t *ddsa_snaps; 9443b2aab18SMatthew Ahrens nvlist_t *ddsa_props; 9453b2aab18SMatthew Ahrens nvlist_t *ddsa_errors; 9463b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t; 947842727c2SChris Kirby 9483cb34c60Sahrens int 9493b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 9503b2aab18SMatthew Ahrens dmu_tx_t *tx) 9511d452cf5Sahrens { 9523b2aab18SMatthew Ahrens int error; 9533b2aab18SMatthew Ahrens uint64_t value; 954fa9e4066Sahrens 9553b2aab18SMatthew Ahrens ds->ds_trysnap_txg = tx->tx_txg; 956745cd3c5Smaybee 9573b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 958842727c2SChris Kirby return (0); 959fa9e4066Sahrens 960fa9e4066Sahrens /* 9613b2aab18SMatthew Ahrens * We don't allow multiple snapshots of the same txg. If there 9623b2aab18SMatthew Ahrens * is already one, try again. 963fa9e4066Sahrens */ 9643b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 965be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 966fa9e4066Sahrens 967fa9e4066Sahrens /* 9683b2aab18SMatthew Ahrens * Check for conflicting snapshot name. 969fa9e4066Sahrens */ 9703b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value); 9713b2aab18SMatthew Ahrens if (error == 0) 972be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 9733b2aab18SMatthew Ahrens if (error != ENOENT) 9743b2aab18SMatthew Ahrens return (error); 975842727c2SChris Kirby 9763b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 9773b2aab18SMatthew Ahrens if (error != 0) 9783b2aab18SMatthew Ahrens return (error); 979842727c2SChris Kirby 9801d452cf5Sahrens return (0); 9811d452cf5Sahrens } 9821d452cf5Sahrens 9833b2aab18SMatthew Ahrens static int 9843b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 985745cd3c5Smaybee { 9863b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 9873b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 9883b2aab18SMatthew Ahrens nvpair_t *pair; 9893b2aab18SMatthew Ahrens int rv = 0; 9903b2aab18SMatthew Ahrens 9913b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 9923b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 9933b2aab18SMatthew Ahrens int error = 0; 9943b2aab18SMatthew Ahrens dsl_dataset_t *ds; 9953b2aab18SMatthew Ahrens char *name, *atp; 9963b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 9973b2aab18SMatthew Ahrens 9983b2aab18SMatthew Ahrens name = nvpair_name(pair); 9993b2aab18SMatthew Ahrens if (strlen(name) >= MAXNAMELEN) 1000be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 10013b2aab18SMatthew Ahrens if (error == 0) { 10023b2aab18SMatthew Ahrens atp = strchr(name, '@'); 10033b2aab18SMatthew Ahrens if (atp == NULL) 1004be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 10053b2aab18SMatthew Ahrens if (error == 0) 10063b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 10073b2aab18SMatthew Ahrens } 10083b2aab18SMatthew Ahrens if (error == 0) 10093b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 10103b2aab18SMatthew Ahrens if (error == 0) { 10113b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 10123b2aab18SMatthew Ahrens atp + 1, tx); 10133b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 10143b2aab18SMatthew Ahrens } 1015745cd3c5Smaybee 10163b2aab18SMatthew Ahrens if (error != 0) { 10173b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 10183b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 10193b2aab18SMatthew Ahrens name, error); 10203b2aab18SMatthew Ahrens } 10213b2aab18SMatthew Ahrens rv = error; 10223b2aab18SMatthew Ahrens } 10233b2aab18SMatthew Ahrens } 10243b2aab18SMatthew Ahrens return (rv); 1025745cd3c5Smaybee } 1026745cd3c5Smaybee 10273b2aab18SMatthew Ahrens void 10283b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 10293b2aab18SMatthew Ahrens dmu_tx_t *tx) 1030745cd3c5Smaybee { 10313b2aab18SMatthew Ahrens static zil_header_t zero_zil; 1032745cd3c5Smaybee 10333b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 10343b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 10353b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 10363b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 10373b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 10383b2aab18SMatthew Ahrens objset_t *os; 1039745cd3c5Smaybee 10403b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1041c33e334fSMatthew Ahrens 1042c33e334fSMatthew Ahrens /* 10433b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 10443b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1045c33e334fSMatthew Ahrens */ 10463b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 10473b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 10483b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 10493b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1050c33e334fSMatthew Ahrens 1051cde58dbcSMatthew Ahrens 1052cde58dbcSMatthew Ahrens /* 10533b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1054088f3894Sahrens */ 1055088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1056088f3894Sahrens crtxg = 1; 1057088f3894Sahrens else 1058088f3894Sahrens crtxg = tx->tx_txg; 1059088f3894Sahrens 10601649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 10611649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 10623b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1063fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1064fa9e4066Sahrens dsphys = dbuf->db_data; 1065745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 10661d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1067fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1068fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1069fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1070fa9e4066Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1071fa9e4066Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1072fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1073fa9e4066Sahrens dsphys->ds_num_children = 1; 1074fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1075088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1076fa9e4066Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1077ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes; 1078fa9e4066Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1079fa9e4066Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 108099653d4eSeschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1081fa9e4066Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 1082ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1083fa9e4066Sahrens 10841d452cf5Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 10851d452cf5Sahrens if (ds->ds_prev) { 1086088f3894Sahrens uint64_t next_clones_obj = 1087088f3894Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 10881d452cf5Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1089fa9e4066Sahrens ds->ds_object || 10901d452cf5Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 10911d452cf5Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 10921d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1093fa9e4066Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 10941d452cf5Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 10951d452cf5Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 1096088f3894Sahrens } else if (next_clones_obj != 0) { 10973b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1098c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 10993b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1100088f3894Sahrens next_clones_obj, dsobj, tx)); 1101fa9e4066Sahrens } 1102fa9e4066Sahrens } 1103fa9e4066Sahrens 1104a9799022Sck /* 1105a9799022Sck * If we have a reference-reservation on this dataset, we will 1106a9799022Sck * need to increase the amount of refreservation being charged 1107a9799022Sck * since our unique space is going to zero. 1108a9799022Sck */ 1109a9799022Sck if (ds->ds_reserved) { 11103f9d6ad7SLin Ling int64_t delta; 11113f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 11123f9d6ad7SLin Ling delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 111374e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 11143f9d6ad7SLin Ling delta, 0, 0, tx); 1115a9799022Sck } 1116a9799022Sck 1117fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1118cde58dbcSMatthew Ahrens ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist, 1119cde58dbcSMatthew Ahrens UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx); 1120cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1121cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj); 1122cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1123cde58dbcSMatthew Ahrens ds->ds_phys->ds_prev_snap_txg, tx); 1124cde58dbcSMatthew Ahrens 1125a4611edeSahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1126fa9e4066Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 1127088f3894Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1128fa9e4066Sahrens ds->ds_phys->ds_unique_bytes = 0; 1129a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1130a9799022Sck ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1131fa9e4066Sahrens 11323b2aab18SMatthew Ahrens VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 11333b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1134fa9e4066Sahrens 1135fa9e4066Sahrens if (ds->ds_prev) 11363b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 11373b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1138745cd3c5Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 1139ecd6cf80Smarks 11403f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1141088f3894Sahrens 114271eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 114371eb0538SChris Kirby 11444445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1145fa9e4066Sahrens } 1146fa9e4066Sahrens 11473b2aab18SMatthew Ahrens static void 11483b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1149fa9e4066Sahrens { 11503b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 11513b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11523b2aab18SMatthew Ahrens nvpair_t *pair; 115391ebeef5Sahrens 11543b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 11553b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 11563b2aab18SMatthew Ahrens dsl_dataset_t *ds; 11573b2aab18SMatthew Ahrens char *name, *atp; 11583b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 11593b2aab18SMatthew Ahrens 11603b2aab18SMatthew Ahrens name = nvpair_name(pair); 11613b2aab18SMatthew Ahrens atp = strchr(name, '@'); 11623b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 11633b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 11643b2aab18SMatthew Ahrens 11653b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 11663b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 11673b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 11683b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 11693b2aab18SMatthew Ahrens } 11703b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 11713b2aab18SMatthew Ahrens } 1172fa9e4066Sahrens } 1173fa9e4066Sahrens 11743b2aab18SMatthew Ahrens /* 11753b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 11763b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 11773b2aab18SMatthew Ahrens */ 11783b2aab18SMatthew Ahrens int 11793b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 118019b94df9SMatthew Ahrens { 11813b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 11823b2aab18SMatthew Ahrens nvpair_t *pair; 11833b2aab18SMatthew Ahrens boolean_t needsuspend; 11843b2aab18SMatthew Ahrens int error; 11853b2aab18SMatthew Ahrens spa_t *spa; 11863b2aab18SMatthew Ahrens char *firstname; 11873b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 118819b94df9SMatthew Ahrens 11893b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 11903b2aab18SMatthew Ahrens if (pair == NULL) 11913b2aab18SMatthew Ahrens return (0); 11923b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 11933b2aab18SMatthew Ahrens 11943b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 11953b2aab18SMatthew Ahrens if (error != 0) 11963b2aab18SMatthew Ahrens return (error); 11973b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 11983b2aab18SMatthew Ahrens spa_close(spa, FTAG); 11993b2aab18SMatthew Ahrens 12003b2aab18SMatthew Ahrens if (needsuspend) { 12013b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 12023b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 12033b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 12043b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 12053b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 12063b2aab18SMatthew Ahrens char *atp; 12073b2aab18SMatthew Ahrens void *cookie; 12083b2aab18SMatthew Ahrens 12093b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 12103b2aab18SMatthew Ahrens if (atp == NULL) { 1211be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 12123b2aab18SMatthew Ahrens break; 12133b2aab18SMatthew Ahrens } 12143b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 12153b2aab18SMatthew Ahrens 12163b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 12173b2aab18SMatthew Ahrens if (error != 0) 12183b2aab18SMatthew Ahrens break; 12193b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 12203b2aab18SMatthew Ahrens (uintptr_t)cookie); 12213b2aab18SMatthew Ahrens } 12223b2aab18SMatthew Ahrens } 12233b2aab18SMatthew Ahrens 12243b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 12253b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 12263b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 12273b2aab18SMatthew Ahrens 12283b2aab18SMatthew Ahrens if (error == 0) { 12293b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 12303b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 12313b2aab18SMatthew Ahrens fnvlist_num_pairs(snaps) * 3); 12323b2aab18SMatthew Ahrens } 12333b2aab18SMatthew Ahrens 12343b2aab18SMatthew Ahrens if (suspended != NULL) { 12353b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 12363b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 12373b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 12383b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 12393b2aab18SMatthew Ahrens } 12403b2aab18SMatthew Ahrens fnvlist_free(suspended); 12413b2aab18SMatthew Ahrens } 12423b2aab18SMatthew Ahrens 12433b2aab18SMatthew Ahrens return (error); 12443b2aab18SMatthew Ahrens } 12453b2aab18SMatthew Ahrens 12463b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 12473b2aab18SMatthew Ahrens const char *ddsta_fsname; 12483b2aab18SMatthew Ahrens const char *ddsta_snapname; 12493b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 12503b2aab18SMatthew Ahrens const char *ddsta_htag; 12513b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 12523b2aab18SMatthew Ahrens 12533b2aab18SMatthew Ahrens static int 12543b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 12553b2aab18SMatthew Ahrens { 12563b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 12573b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 12583b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12593b2aab18SMatthew Ahrens int error; 12603b2aab18SMatthew Ahrens 12613b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 12623b2aab18SMatthew Ahrens if (error != 0) 12633b2aab18SMatthew Ahrens return (error); 12643b2aab18SMatthew Ahrens 12653b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, tx); 12663b2aab18SMatthew Ahrens if (error != 0) { 12673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12683b2aab18SMatthew Ahrens return (error); 12693b2aab18SMatthew Ahrens } 12703b2aab18SMatthew Ahrens 12713b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 12723b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1273be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 12743b2aab18SMatthew Ahrens } 12753b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 12763b2aab18SMatthew Ahrens B_TRUE, tx); 12773b2aab18SMatthew Ahrens if (error != 0) { 12783b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12793b2aab18SMatthew Ahrens return (error); 12803b2aab18SMatthew Ahrens } 12813b2aab18SMatthew Ahrens 12823b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12833b2aab18SMatthew Ahrens return (0); 12843b2aab18SMatthew Ahrens } 12853b2aab18SMatthew Ahrens 12863b2aab18SMatthew Ahrens static void 12873b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 12883b2aab18SMatthew Ahrens { 12893b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 12903b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 12913b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12923b2aab18SMatthew Ahrens 12933b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 12943b2aab18SMatthew Ahrens 12953b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 12963b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 12973b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 12983b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 12993b2aab18SMatthew Ahrens 13003b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 13013b2aab18SMatthew Ahrens } 13023b2aab18SMatthew Ahrens 13033b2aab18SMatthew Ahrens int 13043b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 13053b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 13063b2aab18SMatthew Ahrens { 13073b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 13083b2aab18SMatthew Ahrens int error; 13093b2aab18SMatthew Ahrens spa_t *spa; 13103b2aab18SMatthew Ahrens boolean_t needsuspend; 13113b2aab18SMatthew Ahrens void *cookie; 13123b2aab18SMatthew Ahrens 13133b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 13143b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 13153b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 13163b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 13173b2aab18SMatthew Ahrens 13183b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 13193b2aab18SMatthew Ahrens if (error != 0) 13203b2aab18SMatthew Ahrens return (error); 13213b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 13223b2aab18SMatthew Ahrens spa_close(spa, FTAG); 13233b2aab18SMatthew Ahrens 13243b2aab18SMatthew Ahrens if (needsuspend) { 13253b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 13263b2aab18SMatthew Ahrens if (error != 0) 13273b2aab18SMatthew Ahrens return (error); 13283b2aab18SMatthew Ahrens } 13293b2aab18SMatthew Ahrens 13303b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 13313b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3); 13323b2aab18SMatthew Ahrens 13333b2aab18SMatthew Ahrens if (needsuspend) 13343b2aab18SMatthew Ahrens zil_resume(cookie); 13353b2aab18SMatthew Ahrens return (error); 13363b2aab18SMatthew Ahrens } 13373b2aab18SMatthew Ahrens 13383b2aab18SMatthew Ahrens 13393b2aab18SMatthew Ahrens void 13403b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 13413b2aab18SMatthew Ahrens { 13423b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 13433b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 13443b2aab18SMatthew Ahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 13453b2aab18SMatthew Ahrens 13463b2aab18SMatthew Ahrens /* 13473b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 13483b2aab18SMatthew Ahrens * sync it out now. 13493b2aab18SMatthew Ahrens */ 13503b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13513b2aab18SMatthew Ahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 13523b2aab18SMatthew Ahrens 13533b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 13543b2aab18SMatthew Ahrens } 13553b2aab18SMatthew Ahrens 13563b2aab18SMatthew Ahrens static void 13573b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 13583b2aab18SMatthew Ahrens { 13593b2aab18SMatthew Ahrens uint64_t count = 0; 13603b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 13613b2aab18SMatthew Ahrens zap_cursor_t zc; 13623b2aab18SMatthew Ahrens zap_attribute_t za; 13633b2aab18SMatthew Ahrens nvlist_t *propval = fnvlist_alloc(); 13643b2aab18SMatthew Ahrens nvlist_t *val = fnvlist_alloc(); 13653b2aab18SMatthew Ahrens 13663b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 136719b94df9SMatthew Ahrens 136819b94df9SMatthew Ahrens /* 13693b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 137019b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 137119b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 137219b94df9SMatthew Ahrens */ 137319b94df9SMatthew Ahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 13743b2aab18SMatthew Ahrens ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj, 137519b94df9SMatthew Ahrens &count)); 137619b94df9SMatthew Ahrens } 13773b2aab18SMatthew Ahrens if (count != ds->ds_phys->ds_num_children - 1) 137819b94df9SMatthew Ahrens goto fail; 137919b94df9SMatthew Ahrens for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj); 138019b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 138119b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 138219b94df9SMatthew Ahrens dsl_dataset_t *clone; 138319b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 13843b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 13853b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 138619b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 13873b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 138819b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 138919b94df9SMatthew Ahrens } 139019b94df9SMatthew Ahrens zap_cursor_fini(&zc); 13913b2aab18SMatthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 13923b2aab18SMatthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 139319b94df9SMatthew Ahrens fail: 139419b94df9SMatthew Ahrens nvlist_free(val); 139519b94df9SMatthew Ahrens nvlist_free(propval); 139619b94df9SMatthew Ahrens } 139719b94df9SMatthew Ahrens 1398fa9e4066Sahrens void 1399a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1400fa9e4066Sahrens { 14013b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1402187d6ac0SMatt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio; 1403a9799022Sck 14043b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 14053b2aab18SMatthew Ahrens 14064445fffbSMatthew Ahrens ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 14074445fffbSMatthew Ahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 14084445fffbSMatthew Ahrens ds->ds_phys->ds_compressed_bytes); 14094445fffbSMatthew Ahrens 14104445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 141177372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 141277372cb0SMatthew Ahrens ds->ds_phys->ds_uncompressed_bytes); 14134445fffbSMatthew Ahrens 14144445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 14154445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 14164445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 14174445fffbSMatthew Ahrens ds->ds_phys->ds_unique_bytes); 14184445fffbSMatthew Ahrens get_clones_stat(ds, nv); 14194445fffbSMatthew Ahrens } else { 14204445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 14214445fffbSMatthew Ahrens } 1422fa9e4066Sahrens 1423a9799022Sck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1424a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1425a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1426a9799022Sck 1427a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1428a2eea2e1Sahrens ds->ds_phys->ds_creation_time); 1429a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1430a2eea2e1Sahrens ds->ds_phys->ds_creation_txg); 1431a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1432a9799022Sck ds->ds_quota); 1433a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1434a9799022Sck ds->ds_reserved); 1435c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1436c5904d13Seschrock ds->ds_phys->ds_guid); 14371d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 14383f9d6ad7SLin Ling ds->ds_phys->ds_unique_bytes); 14391d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 14401d713200SEric Schrock ds->ds_object); 144192241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 144292241e0bSTom Erickson ds->ds_userrefs); 1443842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1444842727c2SChris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1445fa9e4066Sahrens 144619b94df9SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 144719b94df9SMatthew Ahrens uint64_t written, comp, uncomp; 144819b94df9SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 144919b94df9SMatthew Ahrens dsl_dataset_t *prev; 145019b94df9SMatthew Ahrens 145119b94df9SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, 145219b94df9SMatthew Ahrens ds->ds_phys->ds_prev_snap_obj, FTAG, &prev); 145319b94df9SMatthew Ahrens if (err == 0) { 145419b94df9SMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written, 145519b94df9SMatthew Ahrens &comp, &uncomp); 145619b94df9SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 145719b94df9SMatthew Ahrens if (err == 0) { 145819b94df9SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 145919b94df9SMatthew Ahrens written); 146019b94df9SMatthew Ahrens } 146119b94df9SMatthew Ahrens } 146219b94df9SMatthew Ahrens } 1463fa9e4066Sahrens } 1464fa9e4066Sahrens 1465a2eea2e1Sahrens void 1466a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1467a2eea2e1Sahrens { 14683b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 14693b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 14703b2aab18SMatthew Ahrens 1471a2eea2e1Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 1472a2eea2e1Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 14733cb34c60Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 14744445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 14754445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 1476a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 1477a2eea2e1Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 1478ebedde84SEric Taylor } else { 1479ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 1480ebedde84SEric Taylor stat->dds_num_clones = 0; 1481a2eea2e1Sahrens 14824445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 14834445fffbSMatthew Ahrens dsl_dataset_t *ods; 1484a2eea2e1Sahrens 14853b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 14864445fffbSMatthew Ahrens ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 14874445fffbSMatthew Ahrens dsl_dataset_name(ods, stat->dds_origin); 14883b2aab18SMatthew Ahrens dsl_dataset_rele(ods, FTAG); 14894445fffbSMatthew Ahrens } 1490a2eea2e1Sahrens } 1491a2eea2e1Sahrens } 1492a2eea2e1Sahrens 1493a2eea2e1Sahrens uint64_t 1494a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1495a2eea2e1Sahrens { 149691ebeef5Sahrens return (ds->ds_fsid_guid); 1497a2eea2e1Sahrens } 1498a2eea2e1Sahrens 1499a2eea2e1Sahrens void 1500a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 1501a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 1502a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1503fa9e4066Sahrens { 1504ad135b5dSChristopher Siden *refdbytesp = ds->ds_phys->ds_referenced_bytes; 1505a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1506a9799022Sck if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 1507a9799022Sck *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 1508a9799022Sck if (ds->ds_quota != 0) { 1509a9799022Sck /* 1510a9799022Sck * Adjust available bytes according to refquota 1511a9799022Sck */ 1512a9799022Sck if (*refdbytesp < ds->ds_quota) 1513a9799022Sck *availbytesp = MIN(*availbytesp, 1514a9799022Sck ds->ds_quota - *refdbytesp); 1515a9799022Sck else 1516a9799022Sck *availbytesp = 0; 1517a9799022Sck } 1518a2eea2e1Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 1519a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1520fa9e4066Sahrens } 1521fa9e4066Sahrens 1522f18faf3fSek boolean_t 1523f18faf3fSek dsl_dataset_modified_since_lastsnap(dsl_dataset_t *ds) 1524f18faf3fSek { 1525f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 1526f18faf3fSek 15273b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 1528f18faf3fSek if (ds->ds_prev == NULL) 1529f18faf3fSek return (B_FALSE); 1530f18faf3fSek if (ds->ds_phys->ds_bp.blk_birth > 15316e0cbcaaSMatthew Ahrens ds->ds_prev->ds_phys->ds_creation_txg) { 15326e0cbcaaSMatthew Ahrens objset_t *os, *os_prev; 15336e0cbcaaSMatthew Ahrens /* 15346e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 15356e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 15366e0cbcaaSMatthew Ahrens * modified. 15376e0cbcaaSMatthew Ahrens */ 15386e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 15396e0cbcaaSMatthew Ahrens return (B_TRUE); 15406e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds->ds_prev, &os_prev) != 0) 15416e0cbcaaSMatthew Ahrens return (B_TRUE); 15426e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 15436e0cbcaaSMatthew Ahrens &os_prev->os_phys->os_meta_dnode, 15446e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 15456e0cbcaaSMatthew Ahrens } 1546f18faf3fSek return (B_FALSE); 1547f18faf3fSek } 1548f18faf3fSek 15493b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 15503b2aab18SMatthew Ahrens const char *ddrsa_fsname; 15513b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 15523b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 15533b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 15543b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 15553b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 15563b2aab18SMatthew Ahrens 15571d452cf5Sahrens /* ARGSUSED */ 1558fa9e4066Sahrens static int 15593b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 15603b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1561fa9e4066Sahrens { 15623b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 15633b2aab18SMatthew Ahrens int error; 1564fa9e4066Sahrens uint64_t val; 1565fa9e4066Sahrens 15663b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 15673b2aab18SMatthew Ahrens if (error != 0) { 15683b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 15693b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 15703b2aab18SMatthew Ahrens } 15711d452cf5Sahrens 15723b2aab18SMatthew Ahrens /* new name should not exist */ 15733b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 15743b2aab18SMatthew Ahrens if (error == 0) 1575be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 15763b2aab18SMatthew Ahrens else if (error == ENOENT) 15773b2aab18SMatthew Ahrens error = 0; 1578cdf5b4caSmmusante 1579cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 15803b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 15813b2aab18SMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN) 1582be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 1583cdf5b4caSmmusante 15843b2aab18SMatthew Ahrens return (error); 15851d452cf5Sahrens } 1586fa9e4066Sahrens 15873b2aab18SMatthew Ahrens static int 15883b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 15891d452cf5Sahrens { 15903b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 15913b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15921d452cf5Sahrens dsl_dataset_t *hds; 15933b2aab18SMatthew Ahrens int error; 1594fa9e4066Sahrens 15953b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 15963b2aab18SMatthew Ahrens if (error != 0) 15973b2aab18SMatthew Ahrens return (error); 1598fa9e4066Sahrens 15993b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 16003b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 16013b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 16023b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 16033b2aab18SMatthew Ahrens } else { 16043b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 16053b2aab18SMatthew Ahrens } 1606745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 16073b2aab18SMatthew Ahrens return (error); 1608fa9e4066Sahrens } 1609fa9e4066Sahrens 1610cdf5b4caSmmusante static int 16113b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 16123b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1613cdf5b4caSmmusante { 16143b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 16153b2aab18SMatthew Ahrens dsl_dataset_t *ds; 16163b2aab18SMatthew Ahrens uint64_t val; 16173b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 16183b2aab18SMatthew Ahrens int error; 1619ecd6cf80Smarks 16203b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 16213b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 16223b2aab18SMatthew Ahrens if (error == ENOENT) { 16233b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 16243b2aab18SMatthew Ahrens return (0); 1625ecd6cf80Smarks } 1626ecd6cf80Smarks 16273b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 16283b2aab18SMatthew Ahrens 16293b2aab18SMatthew Ahrens /* log before we change the name */ 16303b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 16313b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 1632cdf5b4caSmmusante 16333b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx)); 16343b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 16353b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 16363b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 16373b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj, 16383b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 1639cdf5b4caSmmusante 16403b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1641cdf5b4caSmmusante return (0); 1642cdf5b4caSmmusante } 1643cdf5b4caSmmusante 16443b2aab18SMatthew Ahrens static void 16453b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 1646cdf5b4caSmmusante { 16473b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 16483b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16493b2aab18SMatthew Ahrens dsl_dataset_t *hds; 1650cdf5b4caSmmusante 16513b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 16523b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 16533b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 16543b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 16553b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 16563b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 16573b2aab18SMatthew Ahrens } else { 16583b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 1659cdf5b4caSmmusante } 16603b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 1661cdf5b4caSmmusante } 1662cdf5b4caSmmusante 16633b2aab18SMatthew Ahrens int 16643b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 16653b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 16663a5a36beSmmusante { 16673b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 16683a5a36beSmmusante 16693b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 16703b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 16713b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 16723b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 16733a5a36beSmmusante 16743b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 16753b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 1)); 16763a5a36beSmmusante } 16773a5a36beSmmusante 16783b2aab18SMatthew Ahrens static int 16793b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 1680fa9e4066Sahrens { 16813b2aab18SMatthew Ahrens const char *fsname = arg; 16823b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16831d452cf5Sahrens dsl_dataset_t *ds; 16843b2aab18SMatthew Ahrens int64_t unused_refres_delta; 16853b2aab18SMatthew Ahrens int error; 1686fa9e4066Sahrens 16873b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, fsname, FTAG, &ds); 16883b2aab18SMatthew Ahrens if (error != 0) 16893b2aab18SMatthew Ahrens return (error); 1690370c1af0SSanjeev Bagewadi 16913b2aab18SMatthew Ahrens /* must not be a snapshot */ 16923b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 16933b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1694be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 16953b2aab18SMatthew Ahrens } 16963a5a36beSmmusante 16973b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 16983b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 16993b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1700be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 17013b2aab18SMatthew Ahrens } 17023a5a36beSmmusante 17033b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 17043b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1705be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 17063b2aab18SMatthew Ahrens } 17073b2aab18SMatthew Ahrens 17083b2aab18SMatthew Ahrens /* 17093b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 17103b2aab18SMatthew Ahrens * the refquota. 17113b2aab18SMatthew Ahrens */ 17123b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 17133b2aab18SMatthew Ahrens ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) { 17143b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1715be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 1716fa9e4066Sahrens } 1717370c1af0SSanjeev Bagewadi 17183b2aab18SMatthew Ahrens /* 17193b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 17203b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 17213b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 17223b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 17233b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 17243b2aab18SMatthew Ahrens */ 17253b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 17263b2aab18SMatthew Ahrens ds->ds_phys->ds_unique_bytes); 17273b2aab18SMatthew Ahrens 17283b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 17293b2aab18SMatthew Ahrens unused_refres_delta > 17303b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 17313b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1732be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1733fa9e4066Sahrens } 1734fa9e4066Sahrens 17353b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 17363b2aab18SMatthew Ahrens return (0); 17373b2aab18SMatthew Ahrens } 17381d452cf5Sahrens 17393b2aab18SMatthew Ahrens static void 17403b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 17413b2aab18SMatthew Ahrens { 17423b2aab18SMatthew Ahrens const char *fsname = arg; 17433b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17443b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 17453b2aab18SMatthew Ahrens uint64_t cloneobj; 17461d452cf5Sahrens 17473b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, fsname, FTAG, &ds)); 17481d452cf5Sahrens 17493b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 17503b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 17511d452cf5Sahrens 17523b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 17531d452cf5Sahrens 17543b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 17553b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 17563b2aab18SMatthew Ahrens 17573b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 17583b2aab18SMatthew Ahrens 17593b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 17603b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 17613b2aab18SMatthew Ahrens } 17623b2aab18SMatthew Ahrens 17633b2aab18SMatthew Ahrens int 17643b2aab18SMatthew Ahrens dsl_dataset_rollback(const char *fsname) 17653b2aab18SMatthew Ahrens { 17663b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 17673b2aab18SMatthew Ahrens dsl_dataset_rollback_sync, (void *)fsname, 1)); 1768fa9e4066Sahrens } 176999653d4eSeschrock 1770088f3894Sahrens struct promotenode { 1771745cd3c5Smaybee list_node_t link; 1772745cd3c5Smaybee dsl_dataset_t *ds; 1773745cd3c5Smaybee }; 1774745cd3c5Smaybee 17753b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg { 17763b2aab18SMatthew Ahrens const char *ddpa_clonename; 17773b2aab18SMatthew Ahrens dsl_dataset_t *ddpa_clone; 177874e7dc98SMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps; 17793b2aab18SMatthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */ 178074e7dc98SMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 1781681d9761SEric Taylor char *err_ds; 17823b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t; 17831d452cf5Sahrens 178474e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 17853b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 17863b2aab18SMatthew Ahrens void *tag); 17873b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 178874e7dc98SMatthew Ahrens 178999653d4eSeschrock static int 17903b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 179199653d4eSeschrock { 17923b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 17933b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17943b2aab18SMatthew Ahrens dsl_dataset_t *hds; 17953b2aab18SMatthew Ahrens struct promotenode *snap; 17963b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 1797745cd3c5Smaybee int err; 1798cde58dbcSMatthew Ahrens uint64_t unused; 17991d452cf5Sahrens 18003b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 18013b2aab18SMatthew Ahrens if (err != 0) 18023b2aab18SMatthew Ahrens return (err); 180399653d4eSeschrock 18043b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 18051d452cf5Sahrens 18063b2aab18SMatthew Ahrens if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) { 18073b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 1808be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 18093b2aab18SMatthew Ahrens } 18103b2aab18SMatthew Ahrens 18113b2aab18SMatthew Ahrens /* 18123b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 18133b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 18143b2aab18SMatthew Ahrens */ 18153b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 18163b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 18173b2aab18SMatthew Ahrens return (0); 18183b2aab18SMatthew Ahrens } 18193b2aab18SMatthew Ahrens 18203b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 18213b2aab18SMatthew Ahrens origin_ds = snap->ds; 182299653d4eSeschrock 18233cb34c60Sahrens /* compute origin's new unique space */ 18243b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 182574e7dc98SMatthew Ahrens ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 1826cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 1827cde58dbcSMatthew Ahrens origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, 18283b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 182999653d4eSeschrock 1830745cd3c5Smaybee /* 1831745cd3c5Smaybee * Walk the snapshots that we are moving 1832745cd3c5Smaybee * 183374e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 18343b2aab18SMatthew Ahrens * to used by each snapshot: 183574e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 183674e7dc98SMatthew Ahrens * So each snapshot gave birth to: 183774e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 1838745cd3c5Smaybee * So a sequence would look like: 183974e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 1840745cd3c5Smaybee * Which simplifies to: 184174e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 1842745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 184374e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 1844745cd3c5Smaybee */ 18453b2aab18SMatthew Ahrens ddpa->used = origin_ds->ds_phys->ds_referenced_bytes; 18463b2aab18SMatthew Ahrens ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes; 18473b2aab18SMatthew Ahrens ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 18483b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 18493b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 185099653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 1851745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 185299653d4eSeschrock 18533b2aab18SMatthew Ahrens /* 18543b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 18553b2aab18SMatthew Ahrens * the objset. 18563b2aab18SMatthew Ahrens */ 18573b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 1858be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 18593b2aab18SMatthew Ahrens goto out; 18603b2aab18SMatthew Ahrens } 18613b2aab18SMatthew Ahrens 186299653d4eSeschrock /* Check that the snapshot name does not conflict */ 18633b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 1864745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 1865681d9761SEric Taylor if (err == 0) { 18663b2aab18SMatthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 1867be6fd75aSMatthew Ahrens err = SET_ERROR(EEXIST); 1868681d9761SEric Taylor goto out; 1869681d9761SEric Taylor } 1870745cd3c5Smaybee if (err != ENOENT) 1871681d9761SEric Taylor goto out; 187299653d4eSeschrock 1873745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 187474e7dc98SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj == 0) 187574e7dc98SMatthew Ahrens continue; 187674e7dc98SMatthew Ahrens 1877cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 1878cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 18793b2aab18SMatthew Ahrens ddpa->used += dlused; 18803b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 18813b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 188274e7dc98SMatthew Ahrens } 1883745cd3c5Smaybee 1884745cd3c5Smaybee /* 1885745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 1886745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 1887745cd3c5Smaybee */ 18883b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 18893b2aab18SMatthew Ahrens ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes; 18903b2aab18SMatthew Ahrens ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes; 18913b2aab18SMatthew Ahrens ddpa->uncomp -= 18923b2aab18SMatthew Ahrens ddpa->origin_origin->ds_phys->ds_uncompressed_bytes; 189399653d4eSeschrock } 189499653d4eSeschrock 189599653d4eSeschrock /* Check that there is enough space here */ 189674e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 18973b2aab18SMatthew Ahrens ddpa->used); 18983b2aab18SMatthew Ahrens if (err != 0) 18993b2aab18SMatthew Ahrens goto out; 190074e7dc98SMatthew Ahrens 190174e7dc98SMatthew Ahrens /* 190274e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 190374e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 190474e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 190574e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 190674e7dc98SMatthew Ahrens */ 190774e7dc98SMatthew Ahrens if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 190874e7dc98SMatthew Ahrens uint64_t space; 190974e7dc98SMatthew Ahrens 191074e7dc98SMatthew Ahrens /* 191174e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 19123f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 1913cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 191474e7dc98SMatthew Ahrens * calls will be fast because they do not have to 191574e7dc98SMatthew Ahrens * iterate over all bps. 191674e7dc98SMatthew Ahrens */ 19173b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 19183b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 19193b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 19203b2aab18SMatthew Ahrens if (err != 0) 19213b2aab18SMatthew Ahrens goto out; 192274e7dc98SMatthew Ahrens 19233b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 19243f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 19253b2aab18SMatthew Ahrens if (err != 0) 19263b2aab18SMatthew Ahrens goto out; 19273b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 192874e7dc98SMatthew Ahrens } 192974e7dc98SMatthew Ahrens if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 19303b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 19313b2aab18SMatthew Ahrens origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap); 19323b2aab18SMatthew Ahrens if (err != 0) 19333b2aab18SMatthew Ahrens goto out; 1934745cd3c5Smaybee } 19351d452cf5Sahrens 1936681d9761SEric Taylor out: 19373b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 1938681d9761SEric Taylor return (err); 19391d452cf5Sahrens } 194099653d4eSeschrock 19411d452cf5Sahrens static void 19423b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 19431d452cf5Sahrens { 19443b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 19453b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 19463b2aab18SMatthew Ahrens dsl_dataset_t *hds; 19473b2aab18SMatthew Ahrens struct promotenode *snap; 19483b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 19493b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 19503b2aab18SMatthew Ahrens dsl_dir_t *dd; 19513cb34c60Sahrens dsl_dir_t *odd = NULL; 1952088f3894Sahrens uint64_t oldnext_obj; 195374e7dc98SMatthew Ahrens int64_t delta; 19541d452cf5Sahrens 19553b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 19563b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 19573b2aab18SMatthew Ahrens 19583b2aab18SMatthew Ahrens ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE); 19591d452cf5Sahrens 19603b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 19613b2aab18SMatthew Ahrens origin_ds = snap->ds; 19623b2aab18SMatthew Ahrens dd = hds->ds_dir; 19633b2aab18SMatthew Ahrens 19643b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 196574e7dc98SMatthew Ahrens origin_head = snap->ds; 196674e7dc98SMatthew Ahrens 19670b69c2f0Sahrens /* 19683cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 19690b69c2f0Sahrens * changing. 19700b69c2f0Sahrens */ 19713b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 19723cb34c60Sahrens NULL, FTAG, &odd)); 197399653d4eSeschrock 1974745cd3c5Smaybee /* change origin's next snap */ 1975745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 1976088f3894Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 19773b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 197874e7dc98SMatthew Ahrens ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 197974e7dc98SMatthew Ahrens origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 1980745cd3c5Smaybee 1981088f3894Sahrens /* change the origin's next clone */ 1982088f3894Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 19833b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 19843b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 19853b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 1986088f3894Sahrens origin_ds->ds_phys->ds_next_clones_obj, 1987088f3894Sahrens oldnext_obj, tx)); 1988088f3894Sahrens } 1989088f3894Sahrens 1990745cd3c5Smaybee /* change origin */ 1991745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 1992745cd3c5Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 1993745cd3c5Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 19943f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 1995745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 1996745cd3c5Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 19973f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 19983f9d6ad7SLin Ling origin_ds->ds_phys->ds_creation_txg; 1999745cd3c5Smaybee 2000cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2001cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 20023b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2003cde58dbcSMatthew Ahrens odd->dd_phys->dd_clones, hds->ds_object, tx)); 20043b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 20053b2aab18SMatthew Ahrens ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2006cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2007cde58dbcSMatthew Ahrens 20083b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 20093b2aab18SMatthew Ahrens ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2010cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2011cde58dbcSMatthew Ahrens if (dd->dd_phys->dd_clones == 0) { 2012cde58dbcSMatthew Ahrens dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset, 2013cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 2014cde58dbcSMatthew Ahrens } 20153b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2016cde58dbcSMatthew Ahrens dd->dd_phys->dd_clones, origin_head->ds_object, tx)); 2017cde58dbcSMatthew Ahrens } 2018cde58dbcSMatthew Ahrens 201999653d4eSeschrock /* move snapshots to this dir */ 20203b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 20213b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2022745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 202399653d4eSeschrock 20243b2aab18SMatthew Ahrens /* 20253b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 20263b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 20273b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 20283b2aab18SMatthew Ahrens */ 2029503ad85cSMatthew Ahrens if (ds->ds_objset) { 2030503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2031503ad85cSMatthew Ahrens ds->ds_objset = NULL; 20323baa08fcSek } 20333b2aab18SMatthew Ahrens 203499653d4eSeschrock /* move snap name entry */ 20353b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 20363b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2037745cd3c5Smaybee ds->ds_snapname, tx)); 20383b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 203999653d4eSeschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 204099653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2041cde58dbcSMatthew Ahrens 204299653d4eSeschrock /* change containing dsl_dir */ 204399653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 20443cb34c60Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 204599653d4eSeschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 20463cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 20473b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 20483b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 204999653d4eSeschrock NULL, ds, &ds->ds_dir)); 205099653d4eSeschrock 2051cde58dbcSMatthew Ahrens /* move any clone references */ 2052cde58dbcSMatthew Ahrens if (ds->ds_phys->ds_next_clones_obj && 2053cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2054cde58dbcSMatthew Ahrens zap_cursor_t zc; 2055cde58dbcSMatthew Ahrens zap_attribute_t za; 2056cde58dbcSMatthew Ahrens 20573b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 20583b2aab18SMatthew Ahrens ds->ds_phys->ds_next_clones_obj); 20593b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 20603b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 20613b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 20623b2aab18SMatthew Ahrens uint64_t o; 2063a9799022Sck 20643b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 20653b2aab18SMatthew Ahrens /* 20663b2aab18SMatthew Ahrens * We've already moved the 20673b2aab18SMatthew Ahrens * origin's reference. 20683b2aab18SMatthew Ahrens */ 20693b2aab18SMatthew Ahrens continue; 20703b2aab18SMatthew Ahrens } 2071a9799022Sck 20723b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 20733b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 20743b2aab18SMatthew Ahrens o = cnds->ds_dir->dd_phys->dd_head_dataset_obj; 2075a9799022Sck 20763b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 20773b2aab18SMatthew Ahrens odd->dd_phys->dd_clones, o, tx)); 20783b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 20793b2aab18SMatthew Ahrens dd->dd_phys->dd_clones, o, tx)); 20803b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 20813b2aab18SMatthew Ahrens } 20823b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 20833b2aab18SMatthew Ahrens } 20849082849eSck 20853b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2086a9799022Sck } 2087a9799022Sck 2088a9799022Sck /* 20893b2aab18SMatthew Ahrens * Change space accounting. 20903b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 20913b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 20923b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2093a9799022Sck */ 2094a9799022Sck 20953b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 20963b2aab18SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 20973b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 20983b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 20993b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 21003b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 21013b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 21023b2aab18SMatthew Ahrens 21033b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 21043b2aab18SMatthew Ahrens odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 21053b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 21063b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 21073b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 21083b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 21093b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 21103b2aab18SMatthew Ahrens 21113b2aab18SMatthew Ahrens origin_ds->ds_phys->ds_unique_bytes = ddpa->unique; 21123b2aab18SMatthew Ahrens 21133b2aab18SMatthew Ahrens /* log history record */ 21143b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 21153b2aab18SMatthew Ahrens 21163b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 21173b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2118a9799022Sck } 2119a9799022Sck 21203b2aab18SMatthew Ahrens /* 21213b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 21223b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 21233b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 21243b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 21253b2aab18SMatthew Ahrens */ 2126a9799022Sck static int 21273b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 21283b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2129a9799022Sck { 21303b2aab18SMatthew Ahrens uint64_t obj = last_obj; 2131a9799022Sck 21323b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 21333b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 2134a9799022Sck 21353b2aab18SMatthew Ahrens while (obj != first_obj) { 21363b2aab18SMatthew Ahrens dsl_dataset_t *ds; 21373b2aab18SMatthew Ahrens struct promotenode *snap; 21383b2aab18SMatthew Ahrens int err; 213992241e0bSTom Erickson 21403b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 21413b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 21423b2aab18SMatthew Ahrens if (err != 0) 21433b2aab18SMatthew Ahrens return (err); 2144a9799022Sck 21453b2aab18SMatthew Ahrens if (first_obj == 0) 21463b2aab18SMatthew Ahrens first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 21473b2aab18SMatthew Ahrens 21483b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 21493b2aab18SMatthew Ahrens snap->ds = ds; 21503b2aab18SMatthew Ahrens list_insert_tail(l, snap); 21513b2aab18SMatthew Ahrens obj = ds->ds_phys->ds_prev_snap_obj; 21523b2aab18SMatthew Ahrens } 2153a9799022Sck 2154a9799022Sck return (0); 2155a9799022Sck } 2156a9799022Sck 21573b2aab18SMatthew Ahrens static int 21583b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2159a9799022Sck { 21603b2aab18SMatthew Ahrens struct promotenode *snap; 2161a9799022Sck 21623b2aab18SMatthew Ahrens *spacep = 0; 21633b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 21643b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 21653b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 21663b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 21673b2aab18SMatthew Ahrens *spacep += used; 216892241e0bSTom Erickson } 21693b2aab18SMatthew Ahrens return (0); 2170a9799022Sck } 2171a9799022Sck 21723b2aab18SMatthew Ahrens static void 21733b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 2174a9799022Sck { 21753b2aab18SMatthew Ahrens struct promotenode *snap; 217692241e0bSTom Erickson 21773b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 21783b2aab18SMatthew Ahrens return; 2179a9799022Sck 21803b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 21813b2aab18SMatthew Ahrens list_remove(l, snap); 21823b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 21833b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 21843b2aab18SMatthew Ahrens } 21853b2aab18SMatthew Ahrens list_destroy(l); 2186a9799022Sck } 2187a9799022Sck 2188a9799022Sck static int 21893b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2190a9799022Sck { 21913b2aab18SMatthew Ahrens int error; 21923b2aab18SMatthew Ahrens dsl_dir_t *dd; 21933b2aab18SMatthew Ahrens struct promotenode *snap; 2194a9799022Sck 21953b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 21963b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 21973b2aab18SMatthew Ahrens if (error != 0) 21983b2aab18SMatthew Ahrens return (error); 21993b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 2200a9799022Sck 22013b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) || 22023b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 22033b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 2204be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 22053b2aab18SMatthew Ahrens } 2206a9799022Sck 22073b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj, 22083b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 22093b2aab18SMatthew Ahrens if (error != 0) 22103b2aab18SMatthew Ahrens goto out; 2211a9799022Sck 22123b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 22133b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 22143b2aab18SMatthew Ahrens if (error != 0) 22153b2aab18SMatthew Ahrens goto out; 2216a9799022Sck 22173b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 22183b2aab18SMatthew Ahrens ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 22193b2aab18SMatthew Ahrens error = snaplist_make(dp, dd->dd_phys->dd_origin_obj, 22203b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, 22213b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 22223b2aab18SMatthew Ahrens if (error != 0) 22233b2aab18SMatthew Ahrens goto out; 2224379c004dSEric Schrock 22253b2aab18SMatthew Ahrens if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) { 22263b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 22273b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_phys->dd_origin_obj, 22283b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 22293b2aab18SMatthew Ahrens if (error != 0) 22303b2aab18SMatthew Ahrens goto out; 2231379c004dSEric Schrock } 22323b2aab18SMatthew Ahrens out: 22333b2aab18SMatthew Ahrens if (error != 0) 22343b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 22353b2aab18SMatthew Ahrens return (error); 2236a9799022Sck } 2237a9799022Sck 2238a9799022Sck static void 22393b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2240a9799022Sck { 22413b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 22423b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 22433b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 22443b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 22453b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 22463b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 22473b2aab18SMatthew Ahrens } 224802c8f3f0SMatthew Ahrens 22493b2aab18SMatthew Ahrens /* 22503b2aab18SMatthew Ahrens * Promote a clone. 22513b2aab18SMatthew Ahrens * 22523b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 22533b2aab18SMatthew Ahrens * in with the name. (It must be at least MAXNAMELEN bytes long.) 22543b2aab18SMatthew Ahrens */ 22553b2aab18SMatthew Ahrens int 22563b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 22573b2aab18SMatthew Ahrens { 22583b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 22593b2aab18SMatthew Ahrens uint64_t numsnaps; 22603b2aab18SMatthew Ahrens int error; 22613b2aab18SMatthew Ahrens objset_t *os; 226292241e0bSTom Erickson 22633b2aab18SMatthew Ahrens /* 22643b2aab18SMatthew Ahrens * We will modify space proportional to the number of 22653b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 22663b2aab18SMatthew Ahrens */ 22673b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 22683b2aab18SMatthew Ahrens if (error != 0) 22693b2aab18SMatthew Ahrens return (error); 22703b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 22713b2aab18SMatthew Ahrens dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps); 22723b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 22733b2aab18SMatthew Ahrens if (error != 0) 22743b2aab18SMatthew Ahrens return (error); 227502c8f3f0SMatthew Ahrens 22763b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 22773b2aab18SMatthew Ahrens ddpa.err_ds = conflsnap; 227802c8f3f0SMatthew Ahrens 22793b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check, 22803b2aab18SMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 2 + numsnaps)); 2281a9799022Sck } 2282a9799022Sck 2283a9799022Sck int 22843b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 22853b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, boolean_t force) 2286a9799022Sck { 22873b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2288a9799022Sck 22893b2aab18SMatthew Ahrens /* they should both be heads */ 22903b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(clone) || 22913b2aab18SMatthew Ahrens dsl_dataset_is_snapshot(origin_head)) 2292be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 229392241e0bSTom Erickson 22943b2aab18SMatthew Ahrens /* the branch point should be just before them */ 22953b2aab18SMatthew Ahrens if (clone->ds_prev != origin_head->ds_prev) 2296be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2297a9799022Sck 22983b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 22993b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 23003b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 23013b2aab18SMatthew Ahrens origin_head->ds_object != 23023b2aab18SMatthew Ahrens clone->ds_prev->ds_phys->ds_next_snap_obj) 2303be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 230492241e0bSTom Erickson 23053b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 23063b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2307be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2308842727c2SChris Kirby 23093b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 23103b2aab18SMatthew Ahrens if (!force && dsl_dataset_modified_since_lastsnap(origin_head)) 2311be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 2312c99e4bdcSChris Kirby 23133b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 23143b2aab18SMatthew Ahrens if (dsl_dataset_long_held(origin_head)) 2315be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 23163b2aab18SMatthew Ahrens 23173b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 23183b2aab18SMatthew Ahrens unused_refres_delta = 23193b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23203b2aab18SMatthew Ahrens origin_head->ds_phys->ds_unique_bytes) - 23213b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23223b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 23233b2aab18SMatthew Ahrens 23243b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 23253b2aab18SMatthew Ahrens unused_refres_delta > 23263b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2327be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 23283b2aab18SMatthew Ahrens 23293b2aab18SMatthew Ahrens /* clone can't be over the head's refquota */ 23303b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 23313b2aab18SMatthew Ahrens clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota) 2332be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2333c99e4bdcSChris Kirby 23343b2aab18SMatthew Ahrens return (0); 2335c99e4bdcSChris Kirby } 2336c99e4bdcSChris Kirby 2337a7f53a56SChris Kirby void 23383b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 23393b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 2340a7f53a56SChris Kirby { 23413b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 23423b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2343a7f53a56SChris Kirby 23443b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 23453b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 23463b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota); 2347842727c2SChris Kirby 23483b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 23493b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2350842727c2SChris Kirby 23513b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 23523b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 23533b2aab18SMatthew Ahrens clone->ds_objset = NULL; 23543b2aab18SMatthew Ahrens } 2355842727c2SChris Kirby 23563b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 23573b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 23583b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 2359842727c2SChris Kirby } 2360842727c2SChris Kirby 23613b2aab18SMatthew Ahrens unused_refres_delta = 23623b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23633b2aab18SMatthew Ahrens origin_head->ds_phys->ds_unique_bytes) - 23643b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23653b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 23663b2aab18SMatthew Ahrens 23673b2aab18SMatthew Ahrens /* 23683b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 23693b2aab18SMatthew Ahrens */ 23703b2aab18SMatthew Ahrens if (clone->ds_prev) { 23713b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 23723b2aab18SMatthew Ahrens uint64_t comp, uncomp; 23733b2aab18SMatthew Ahrens 23743b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 23753b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 23763b2aab18SMatthew Ahrens origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 23773b2aab18SMatthew Ahrens &origin->ds_phys->ds_unique_bytes, &comp, &uncomp); 23783b2aab18SMatthew Ahrens } 23793b2aab18SMatthew Ahrens 23803b2aab18SMatthew Ahrens /* swap blkptrs */ 23813b2aab18SMatthew Ahrens { 23823b2aab18SMatthew Ahrens blkptr_t tmp; 23833b2aab18SMatthew Ahrens tmp = origin_head->ds_phys->ds_bp; 23843b2aab18SMatthew Ahrens origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp; 23853b2aab18SMatthew Ahrens clone->ds_phys->ds_bp = tmp; 23863b2aab18SMatthew Ahrens } 23873b2aab18SMatthew Ahrens 23883b2aab18SMatthew Ahrens /* set dd_*_bytes */ 23893b2aab18SMatthew Ahrens { 23903b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 23913b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 23923b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 23933b2aab18SMatthew Ahrens 23943b2aab18SMatthew Ahrens ASSERT3U(clone->ds_dir->dd_phys-> 23953b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 23963b2aab18SMatthew Ahrens 23973b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 23983b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 23993b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 24003b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 240115508ac0SChris Kirby 24023b2aab18SMatthew Ahrens dused = clone->ds_phys->ds_referenced_bytes + cdl_used - 24033b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_referenced_bytes + odl_used); 24043b2aab18SMatthew Ahrens dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp - 24053b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_compressed_bytes + odl_comp); 24063b2aab18SMatthew Ahrens duncomp = clone->ds_phys->ds_uncompressed_bytes + 24073b2aab18SMatthew Ahrens cdl_uncomp - 24083b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp); 2409842727c2SChris Kirby 24103b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 24113b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 24123b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 24133b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 2414842727c2SChris Kirby 2415842727c2SChris Kirby /* 24163b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 24173b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 24183b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 24193b2aab18SMatthew Ahrens * changing that affects the snapused). 2420842727c2SChris Kirby */ 24213b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 24223b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 24233b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 24243b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 24253b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 24263b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 24273b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 24283b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 2429842727c2SChris Kirby } 2430842727c2SChris Kirby 24313b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 24323b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_referenced_bytes, 24333b2aab18SMatthew Ahrens clone->ds_phys->ds_referenced_bytes); 24343b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_compressed_bytes, 24353b2aab18SMatthew Ahrens clone->ds_phys->ds_compressed_bytes); 24363b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes, 24373b2aab18SMatthew Ahrens clone->ds_phys->ds_uncompressed_bytes); 24383b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_unique_bytes, 24393b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 2440842727c2SChris Kirby 24413b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 24423b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 24433b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 2444ca45db41SChris Kirby 24453b2aab18SMatthew Ahrens /* 24463b2aab18SMatthew Ahrens * Swap deadlists. 24473b2aab18SMatthew Ahrens */ 24483b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 24493b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 24503b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_deadlist_obj, 24513b2aab18SMatthew Ahrens clone->ds_phys->ds_deadlist_obj); 24523b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 24533b2aab18SMatthew Ahrens clone->ds_phys->ds_deadlist_obj); 24543b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 24553b2aab18SMatthew Ahrens origin_head->ds_phys->ds_deadlist_obj); 2456842727c2SChris Kirby 24573b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 2458842727c2SChris Kirby 24593b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 24603b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 2461842727c2SChris Kirby } 2462842727c2SChris Kirby 24633b2aab18SMatthew Ahrens /* 24643b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 24653b2aab18SMatthew Ahrens * return the name of that dataset. 24663b2aab18SMatthew Ahrens */ 2467a7f53a56SChris Kirby int 24683b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2469a7f53a56SChris Kirby { 24703b2aab18SMatthew Ahrens dsl_pool_t *dp; 24713b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2472a7f53a56SChris Kirby int error; 2473a7f53a56SChris Kirby 24743b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 24753b2aab18SMatthew Ahrens if (error != 0) 24763b2aab18SMatthew Ahrens return (error); 24773b2aab18SMatthew Ahrens 24783b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 24793b2aab18SMatthew Ahrens if (error == 0) { 24803b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 24813b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 24823b2aab18SMatthew Ahrens } 24833b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 2484a7f53a56SChris Kirby 2485a7f53a56SChris Kirby return (error); 2486a7f53a56SChris Kirby } 2487a7f53a56SChris Kirby 2488842727c2SChris Kirby int 24893b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 24903b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 2491842727c2SChris Kirby { 24923b2aab18SMatthew Ahrens int error = 0; 2493842727c2SChris Kirby 24943b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 2495842727c2SChris Kirby 24963b2aab18SMatthew Ahrens /* 24973b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 24983b2aab18SMatthew Ahrens * unconsumed refreservation space. 24993b2aab18SMatthew Ahrens */ 25003b2aab18SMatthew Ahrens *ref_rsrv = 0; 2501842727c2SChris Kirby 25023b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 25033b2aab18SMatthew Ahrens /* 25043b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 25053b2aab18SMatthew Ahrens */ 25063b2aab18SMatthew Ahrens if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 25073b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 25083b2aab18SMatthew Ahrens ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 25093b2aab18SMatthew Ahrens *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 25103b2aab18SMatthew Ahrens *ref_rsrv = 25113b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 2512842727c2SChris Kirby } 2513842727c2SChris Kirby 25143b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 25153b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 25163b2aab18SMatthew Ahrens return (0); 2517842727c2SChris Kirby } 25183b2aab18SMatthew Ahrens /* 25193b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 25203b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 25213b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 25223b2aab18SMatthew Ahrens * may free up space for us). 25233b2aab18SMatthew Ahrens */ 25243b2aab18SMatthew Ahrens if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) { 25253b2aab18SMatthew Ahrens if (inflight > 0 || 25263b2aab18SMatthew Ahrens ds->ds_phys->ds_referenced_bytes < ds->ds_quota) 2527be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 25283b2aab18SMatthew Ahrens else 2529be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 2530842727c2SChris Kirby } 25313b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2532842727c2SChris Kirby 2533842727c2SChris Kirby return (error); 2534842727c2SChris Kirby } 2535842727c2SChris Kirby 25363b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 25373b2aab18SMatthew Ahrens const char *ddsqra_name; 25383b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 25393b2aab18SMatthew Ahrens uint64_t ddsqra_value; 25403b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 2541842727c2SChris Kirby 25423b2aab18SMatthew Ahrens 25433b2aab18SMatthew Ahrens /* ARGSUSED */ 2544842727c2SChris Kirby static int 25453b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 2546842727c2SChris Kirby { 25473b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 25483b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 25493b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2550842727c2SChris Kirby int error; 25513b2aab18SMatthew Ahrens uint64_t newval; 2552842727c2SChris Kirby 25533b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 2554be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2555842727c2SChris Kirby 25563b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 25573b2aab18SMatthew Ahrens if (error != 0) 25583b2aab18SMatthew Ahrens return (error); 25593b2aab18SMatthew Ahrens 25603b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 25613b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2562be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2563842727c2SChris Kirby } 2564842727c2SChris Kirby 25653b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 25663b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 25673b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 25683b2aab18SMatthew Ahrens if (error != 0) { 25693b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2570842727c2SChris Kirby return (error); 2571842727c2SChris Kirby } 2572842727c2SChris Kirby 25733b2aab18SMatthew Ahrens if (newval == 0) { 25743b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 25753b2aab18SMatthew Ahrens return (0); 25763b2aab18SMatthew Ahrens } 2577842727c2SChris Kirby 25783b2aab18SMatthew Ahrens if (newval < ds->ds_phys->ds_referenced_bytes || 25793b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 25803b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2581be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 25823b2aab18SMatthew Ahrens } 25833b2aab18SMatthew Ahrens 25843b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2585842727c2SChris Kirby return (0); 2586842727c2SChris Kirby } 2587842727c2SChris Kirby 25883b2aab18SMatthew Ahrens static void 25893b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 2590842727c2SChris Kirby { 25913b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 25923b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 25933b2aab18SMatthew Ahrens dsl_dataset_t *ds; 25943b2aab18SMatthew Ahrens uint64_t newval; 2595842727c2SChris Kirby 25963b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 2597842727c2SChris Kirby 25983b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 25993b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 26003b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 26013b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 2602842727c2SChris Kirby 26033b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 26043b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 2605842727c2SChris Kirby 26063b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 26073b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 26083b2aab18SMatthew Ahrens ds->ds_quota = newval; 2609842727c2SChris Kirby } 26103b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2611842727c2SChris Kirby } 2612842727c2SChris Kirby 26133b2aab18SMatthew Ahrens int 26143b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 26153b2aab18SMatthew Ahrens uint64_t refquota) 2616842727c2SChris Kirby { 26173b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 2618842727c2SChris Kirby 26193b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 26203b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 26213b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 26223b2aab18SMatthew Ahrens 26233b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 26243b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0)); 2625842727c2SChris Kirby } 2626842727c2SChris Kirby 2627842727c2SChris Kirby static int 26283b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 2629842727c2SChris Kirby { 26303b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 26313b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 2632842727c2SChris Kirby dsl_dataset_t *ds; 2633842727c2SChris Kirby int error; 26343b2aab18SMatthew Ahrens uint64_t newval, unique; 2635d7747cbcSChris Kirby 26363b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 2637be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2638842727c2SChris Kirby 26393b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 26403b2aab18SMatthew Ahrens if (error != 0) 2641842727c2SChris Kirby return (error); 2642842727c2SChris Kirby 26433b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 26443b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2645be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2646842727c2SChris Kirby } 2647842727c2SChris Kirby 26483b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 26493b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 26503b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 26513b2aab18SMatthew Ahrens if (error != 0) { 26523b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2653842727c2SChris Kirby return (error); 2654842727c2SChris Kirby } 2655842727c2SChris Kirby 26563b2aab18SMatthew Ahrens /* 26573b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 26583b2aab18SMatthew Ahrens * space estimates may be inaccurate. 26593b2aab18SMatthew Ahrens */ 26603b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 26613b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 26623b2aab18SMatthew Ahrens return (0); 2663842727c2SChris Kirby } 2664842727c2SChris Kirby 26653b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 26663b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 26673b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 26683b2aab18SMatthew Ahrens unique = ds->ds_phys->ds_unique_bytes; 26693b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2670842727c2SChris Kirby 26713b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 26723b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 26733b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 2674842727c2SChris Kirby 26753b2aab18SMatthew Ahrens if (delta > 26763b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 26773b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 26783b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2679be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 26803b2aab18SMatthew Ahrens } 2681842727c2SChris Kirby } 2682842727c2SChris Kirby 26833b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 26843b2aab18SMatthew Ahrens return (0); 2685842727c2SChris Kirby } 2686842727c2SChris Kirby 26873b2aab18SMatthew Ahrens void 26883b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 26893b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 2690ca45db41SChris Kirby { 26913b2aab18SMatthew Ahrens uint64_t newval; 26923b2aab18SMatthew Ahrens uint64_t unique; 26933b2aab18SMatthew Ahrens int64_t delta; 2694ca45db41SChris Kirby 26953b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 26963b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 2697ca45db41SChris Kirby 26983b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 26993b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 2700a7f53a56SChris Kirby 27013b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 27023b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 27033b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 27043b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 27053b2aab18SMatthew Ahrens unique = ds->ds_phys->ds_unique_bytes; 27063b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 27073b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 27083b2aab18SMatthew Ahrens ds->ds_reserved = newval; 27093b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2710a7f53a56SChris Kirby 27113b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 27123b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 2713ca45db41SChris Kirby } 2714ca45db41SChris Kirby 27153b2aab18SMatthew Ahrens static void 27163b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 2717842727c2SChris Kirby { 27183b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 27193b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 2720842727c2SChris Kirby dsl_dataset_t *ds; 2721842727c2SChris Kirby 27223b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 27233b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 27243b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 2725842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 2726842727c2SChris Kirby } 2727503ad85cSMatthew Ahrens 2728503ad85cSMatthew Ahrens int 27293b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 27303b2aab18SMatthew Ahrens uint64_t refreservation) 2731503ad85cSMatthew Ahrens { 27323b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 2733503ad85cSMatthew Ahrens 27343b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 27353b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 27363b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 27373b2aab18SMatthew Ahrens 27383b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 27393b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 0)); 2740503ad85cSMatthew Ahrens } 274119b94df9SMatthew Ahrens 274219b94df9SMatthew Ahrens /* 274319b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 274419b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 274519b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 274619b94df9SMatthew Ahrens * fail and return EINVAL. 274719b94df9SMatthew Ahrens * 274819b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 274919b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 275019b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 275119b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 275219b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 275319b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 275419b94df9SMatthew Ahrens * 275519b94df9SMatthew Ahrens * space freed [---------------------] 275619b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 275719b94df9SMatthew Ahrens * oldsnap new 275819b94df9SMatthew Ahrens */ 275919b94df9SMatthew Ahrens int 276019b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 276119b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 276219b94df9SMatthew Ahrens { 276319b94df9SMatthew Ahrens int err = 0; 276419b94df9SMatthew Ahrens uint64_t snapobj; 276519b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 276619b94df9SMatthew Ahrens 27673b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 27683b2aab18SMatthew Ahrens 276919b94df9SMatthew Ahrens *usedp = 0; 2770ad135b5dSChristopher Siden *usedp += new->ds_phys->ds_referenced_bytes; 2771ad135b5dSChristopher Siden *usedp -= oldsnap->ds_phys->ds_referenced_bytes; 277219b94df9SMatthew Ahrens 277319b94df9SMatthew Ahrens *compp = 0; 277419b94df9SMatthew Ahrens *compp += new->ds_phys->ds_compressed_bytes; 277519b94df9SMatthew Ahrens *compp -= oldsnap->ds_phys->ds_compressed_bytes; 277619b94df9SMatthew Ahrens 277719b94df9SMatthew Ahrens *uncompp = 0; 277819b94df9SMatthew Ahrens *uncompp += new->ds_phys->ds_uncompressed_bytes; 277919b94df9SMatthew Ahrens *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes; 278019b94df9SMatthew Ahrens 278119b94df9SMatthew Ahrens snapobj = new->ds_object; 278219b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 278319b94df9SMatthew Ahrens dsl_dataset_t *snap; 278419b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 278519b94df9SMatthew Ahrens 2786ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 2787ad135b5dSChristopher Siden snap = new; 2788ad135b5dSChristopher Siden } else { 2789ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 2790ad135b5dSChristopher Siden if (err != 0) 2791ad135b5dSChristopher Siden break; 2792ad135b5dSChristopher Siden } 279319b94df9SMatthew Ahrens 279419b94df9SMatthew Ahrens if (snap->ds_phys->ds_prev_snap_txg == 279519b94df9SMatthew Ahrens oldsnap->ds_phys->ds_creation_txg) { 279619b94df9SMatthew Ahrens /* 279719b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 279819b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 279919b94df9SMatthew Ahrens * which is more efficient (especially for old-format 280019b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 280119b94df9SMatthew Ahrens * doesn't have enough information to make this 280219b94df9SMatthew Ahrens * optimization itself. 280319b94df9SMatthew Ahrens */ 280419b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 280519b94df9SMatthew Ahrens &used, &comp, &uncomp); 280619b94df9SMatthew Ahrens } else { 280719b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 280819b94df9SMatthew Ahrens 0, oldsnap->ds_phys->ds_creation_txg, 280919b94df9SMatthew Ahrens &used, &comp, &uncomp); 281019b94df9SMatthew Ahrens } 281119b94df9SMatthew Ahrens *usedp += used; 281219b94df9SMatthew Ahrens *compp += comp; 281319b94df9SMatthew Ahrens *uncompp += uncomp; 281419b94df9SMatthew Ahrens 281519b94df9SMatthew Ahrens /* 281619b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 281719b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 281819b94df9SMatthew Ahrens * was not a snapshot of/before new. 281919b94df9SMatthew Ahrens */ 282019b94df9SMatthew Ahrens snapobj = snap->ds_phys->ds_prev_snap_obj; 2821ad135b5dSChristopher Siden if (snap != new) 2822ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 282319b94df9SMatthew Ahrens if (snapobj == 0) { 2824be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 282519b94df9SMatthew Ahrens break; 282619b94df9SMatthew Ahrens } 282719b94df9SMatthew Ahrens 282819b94df9SMatthew Ahrens } 282919b94df9SMatthew Ahrens return (err); 283019b94df9SMatthew Ahrens } 283119b94df9SMatthew Ahrens 283219b94df9SMatthew Ahrens /* 283319b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 283419b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 283519b94df9SMatthew Ahrens * 283619b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 283719b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 283819b94df9SMatthew Ahrens * firstsnap lastsnap 283919b94df9SMatthew Ahrens * 284019b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 284119b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 284219b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 284319b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 284419b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 284519b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 284619b94df9SMatthew Ahrens */ 284719b94df9SMatthew Ahrens int 284819b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 284919b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 285019b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 285119b94df9SMatthew Ahrens { 285219b94df9SMatthew Ahrens int err = 0; 285319b94df9SMatthew Ahrens uint64_t snapobj; 285419b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 285519b94df9SMatthew Ahrens 285619b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(firstsnap)); 285719b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(lastsnap)); 285819b94df9SMatthew Ahrens 285919b94df9SMatthew Ahrens /* 286019b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 286119b94df9SMatthew Ahrens * is before lastsnap. 286219b94df9SMatthew Ahrens */ 286319b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 286419b94df9SMatthew Ahrens firstsnap->ds_phys->ds_creation_txg > 286519b94df9SMatthew Ahrens lastsnap->ds_phys->ds_creation_txg) 2866be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 286719b94df9SMatthew Ahrens 286819b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 286919b94df9SMatthew Ahrens 287019b94df9SMatthew Ahrens snapobj = lastsnap->ds_phys->ds_next_snap_obj; 287119b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 287219b94df9SMatthew Ahrens dsl_dataset_t *ds; 287319b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 287419b94df9SMatthew Ahrens 287519b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 287619b94df9SMatthew Ahrens if (err != 0) 287719b94df9SMatthew Ahrens break; 287819b94df9SMatthew Ahrens 287919b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 288019b94df9SMatthew Ahrens firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX, 288119b94df9SMatthew Ahrens &used, &comp, &uncomp); 288219b94df9SMatthew Ahrens *usedp += used; 288319b94df9SMatthew Ahrens *compp += comp; 288419b94df9SMatthew Ahrens *uncompp += uncomp; 288519b94df9SMatthew Ahrens 288619b94df9SMatthew Ahrens snapobj = ds->ds_phys->ds_prev_snap_obj; 288719b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 288819b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 288919b94df9SMatthew Ahrens } 289019b94df9SMatthew Ahrens return (err); 289119b94df9SMatthew Ahrens } 28923b2aab18SMatthew Ahrens 28933b2aab18SMatthew Ahrens /* 28943b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 28953b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 28963b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 28973b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 28983b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 28993b2aab18SMatthew Ahrens */ 29003b2aab18SMatthew Ahrens boolean_t 29013b2aab18SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier) 29023b2aab18SMatthew Ahrens { 29033b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 29043b2aab18SMatthew Ahrens int error; 29053b2aab18SMatthew Ahrens boolean_t ret; 29063b2aab18SMatthew Ahrens 29073b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 29083b2aab18SMatthew Ahrens 29093b2aab18SMatthew Ahrens if (earlier->ds_phys->ds_creation_txg >= 29103b2aab18SMatthew Ahrens later->ds_phys->ds_creation_txg) 29113b2aab18SMatthew Ahrens return (B_FALSE); 29123b2aab18SMatthew Ahrens 29133b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 29143b2aab18SMatthew Ahrens return (B_TRUE); 29153b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 29163b2aab18SMatthew Ahrens return (B_FALSE); 29173b2aab18SMatthew Ahrens 29183b2aab18SMatthew Ahrens if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object) 29193b2aab18SMatthew Ahrens return (B_TRUE); 29203b2aab18SMatthew Ahrens dsl_dataset_t *origin; 29213b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 29223b2aab18SMatthew Ahrens later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin); 29233b2aab18SMatthew Ahrens if (error != 0) 29243b2aab18SMatthew Ahrens return (B_FALSE); 29253b2aab18SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier); 29263b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 29273b2aab18SMatthew Ahrens return (ret); 29283b2aab18SMatthew Ahrens } 2929