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); 359b287be1bSWill Andrews if (doi.doi_type != DMU_OT_DSL_DATASET) { 360b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 361be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 362b287be1bSWill 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, 950ca48f36fSKeith M Wesolowski dmu_tx_t *tx, boolean_t recv) 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 976ca48f36fSKeith M Wesolowski /* 977ca48f36fSKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as 978ca48f36fSKeith M Wesolowski * those into which we are currently receiving. However, if we are 979ca48f36fSKeith M Wesolowski * creating this snapshot as part of a receive, this check will be 980ca48f36fSKeith M Wesolowski * executed atomically with respect to the completion of the receive 981ca48f36fSKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 982ca48f36fSKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in 983ca48f36fSKeith M Wesolowski * dmu_recv_end_sync(). 984ca48f36fSKeith M Wesolowski */ 985ca48f36fSKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds)) 986ca48f36fSKeith M Wesolowski return (SET_ERROR(EBUSY)); 987ca48f36fSKeith M Wesolowski 9883b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 9893b2aab18SMatthew Ahrens if (error != 0) 9903b2aab18SMatthew Ahrens return (error); 991842727c2SChris Kirby 9921d452cf5Sahrens return (0); 9931d452cf5Sahrens } 9941d452cf5Sahrens 9953b2aab18SMatthew Ahrens static int 9963b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 997745cd3c5Smaybee { 9983b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 9993b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 10003b2aab18SMatthew Ahrens nvpair_t *pair; 10013b2aab18SMatthew Ahrens int rv = 0; 10023b2aab18SMatthew Ahrens 10033b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 10043b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 10053b2aab18SMatthew Ahrens int error = 0; 10063b2aab18SMatthew Ahrens dsl_dataset_t *ds; 10073b2aab18SMatthew Ahrens char *name, *atp; 10083b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 10093b2aab18SMatthew Ahrens 10103b2aab18SMatthew Ahrens name = nvpair_name(pair); 10113b2aab18SMatthew Ahrens if (strlen(name) >= MAXNAMELEN) 1012be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 10133b2aab18SMatthew Ahrens if (error == 0) { 10143b2aab18SMatthew Ahrens atp = strchr(name, '@'); 10153b2aab18SMatthew Ahrens if (atp == NULL) 1016be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 10173b2aab18SMatthew Ahrens if (error == 0) 10183b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 10193b2aab18SMatthew Ahrens } 10203b2aab18SMatthew Ahrens if (error == 0) 10213b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 10223b2aab18SMatthew Ahrens if (error == 0) { 10233b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 1024ca48f36fSKeith M Wesolowski atp + 1, tx, B_FALSE); 10253b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 10263b2aab18SMatthew Ahrens } 1027745cd3c5Smaybee 10283b2aab18SMatthew Ahrens if (error != 0) { 10293b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 10303b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 10313b2aab18SMatthew Ahrens name, error); 10323b2aab18SMatthew Ahrens } 10333b2aab18SMatthew Ahrens rv = error; 10343b2aab18SMatthew Ahrens } 10353b2aab18SMatthew Ahrens } 10363b2aab18SMatthew Ahrens return (rv); 1037745cd3c5Smaybee } 1038745cd3c5Smaybee 10393b2aab18SMatthew Ahrens void 10403b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 10413b2aab18SMatthew Ahrens dmu_tx_t *tx) 1042745cd3c5Smaybee { 10433b2aab18SMatthew Ahrens static zil_header_t zero_zil; 1044745cd3c5Smaybee 10453b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 10463b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 10473b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 10483b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 10493b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 10503b2aab18SMatthew Ahrens objset_t *os; 1051745cd3c5Smaybee 10523b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1053c33e334fSMatthew Ahrens 1054c33e334fSMatthew Ahrens /* 10553b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 10563b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1057c33e334fSMatthew Ahrens */ 10583b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 10593b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 10603b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 10613b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1062c33e334fSMatthew Ahrens 1063cde58dbcSMatthew Ahrens 1064cde58dbcSMatthew Ahrens /* 10653b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1066088f3894Sahrens */ 1067088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1068088f3894Sahrens crtxg = 1; 1069088f3894Sahrens else 1070088f3894Sahrens crtxg = tx->tx_txg; 1071088f3894Sahrens 10721649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 10731649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 10743b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1075fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1076fa9e4066Sahrens dsphys = dbuf->db_data; 1077745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 10781d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1079fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1080fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1081fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1082fa9e4066Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1083fa9e4066Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1084fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1085fa9e4066Sahrens dsphys->ds_num_children = 1; 1086fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1087088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1088fa9e4066Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1089ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes; 1090fa9e4066Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1091fa9e4066Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 109299653d4eSeschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1093fa9e4066Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 1094ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1095fa9e4066Sahrens 10961d452cf5Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 10971d452cf5Sahrens if (ds->ds_prev) { 1098088f3894Sahrens uint64_t next_clones_obj = 1099088f3894Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 11001d452cf5Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1101fa9e4066Sahrens ds->ds_object || 11021d452cf5Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 11031d452cf5Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 11041d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1105fa9e4066Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 11061d452cf5Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 11071d452cf5Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 1108088f3894Sahrens } else if (next_clones_obj != 0) { 11093b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1110c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 11113b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1112088f3894Sahrens next_clones_obj, dsobj, tx)); 1113fa9e4066Sahrens } 1114fa9e4066Sahrens } 1115fa9e4066Sahrens 1116a9799022Sck /* 1117a9799022Sck * If we have a reference-reservation on this dataset, we will 1118a9799022Sck * need to increase the amount of refreservation being charged 1119a9799022Sck * since our unique space is going to zero. 1120a9799022Sck */ 1121a9799022Sck if (ds->ds_reserved) { 11223f9d6ad7SLin Ling int64_t delta; 11233f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 11243f9d6ad7SLin Ling delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 112574e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 11263f9d6ad7SLin Ling delta, 0, 0, tx); 1127a9799022Sck } 1128a9799022Sck 1129fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1130cde58dbcSMatthew Ahrens ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist, 1131cde58dbcSMatthew Ahrens UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx); 1132cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1133cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj); 1134cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1135cde58dbcSMatthew Ahrens ds->ds_phys->ds_prev_snap_txg, tx); 1136cde58dbcSMatthew Ahrens 1137a4611edeSahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1138fa9e4066Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 1139088f3894Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1140fa9e4066Sahrens ds->ds_phys->ds_unique_bytes = 0; 1141a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1142a9799022Sck ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1143fa9e4066Sahrens 11443b2aab18SMatthew Ahrens VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 11453b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1146fa9e4066Sahrens 1147fa9e4066Sahrens if (ds->ds_prev) 11483b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 11493b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1150745cd3c5Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 1151ecd6cf80Smarks 11523f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1153088f3894Sahrens 115471eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 115571eb0538SChris Kirby 11564445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1157fa9e4066Sahrens } 1158fa9e4066Sahrens 11593b2aab18SMatthew Ahrens static void 11603b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1161fa9e4066Sahrens { 11623b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 11633b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11643b2aab18SMatthew Ahrens nvpair_t *pair; 116591ebeef5Sahrens 11663b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 11673b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 11683b2aab18SMatthew Ahrens dsl_dataset_t *ds; 11693b2aab18SMatthew Ahrens char *name, *atp; 11703b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 11713b2aab18SMatthew Ahrens 11723b2aab18SMatthew Ahrens name = nvpair_name(pair); 11733b2aab18SMatthew Ahrens atp = strchr(name, '@'); 11743b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 11753b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 11763b2aab18SMatthew Ahrens 11773b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 11783b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 11793b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 11803b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 11813b2aab18SMatthew Ahrens } 11823b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 11833b2aab18SMatthew Ahrens } 1184fa9e4066Sahrens } 1185fa9e4066Sahrens 11863b2aab18SMatthew Ahrens /* 11873b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 11883b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 11893b2aab18SMatthew Ahrens */ 11903b2aab18SMatthew Ahrens int 11913b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 119219b94df9SMatthew Ahrens { 11933b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 11943b2aab18SMatthew Ahrens nvpair_t *pair; 11953b2aab18SMatthew Ahrens boolean_t needsuspend; 11963b2aab18SMatthew Ahrens int error; 11973b2aab18SMatthew Ahrens spa_t *spa; 11983b2aab18SMatthew Ahrens char *firstname; 11993b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 120019b94df9SMatthew Ahrens 12013b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 12023b2aab18SMatthew Ahrens if (pair == NULL) 12033b2aab18SMatthew Ahrens return (0); 12043b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 12053b2aab18SMatthew Ahrens 12063b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 12073b2aab18SMatthew Ahrens if (error != 0) 12083b2aab18SMatthew Ahrens return (error); 12093b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 12103b2aab18SMatthew Ahrens spa_close(spa, FTAG); 12113b2aab18SMatthew Ahrens 12123b2aab18SMatthew Ahrens if (needsuspend) { 12133b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 12143b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 12153b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 12163b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 12173b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 12183b2aab18SMatthew Ahrens char *atp; 12193b2aab18SMatthew Ahrens void *cookie; 12203b2aab18SMatthew Ahrens 12213b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 12223b2aab18SMatthew Ahrens if (atp == NULL) { 1223be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 12243b2aab18SMatthew Ahrens break; 12253b2aab18SMatthew Ahrens } 12263b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 12273b2aab18SMatthew Ahrens 12283b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 12293b2aab18SMatthew Ahrens if (error != 0) 12303b2aab18SMatthew Ahrens break; 12313b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 12323b2aab18SMatthew Ahrens (uintptr_t)cookie); 12333b2aab18SMatthew Ahrens } 12343b2aab18SMatthew Ahrens } 12353b2aab18SMatthew Ahrens 12363b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 12373b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 12383b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 12393b2aab18SMatthew Ahrens 12403b2aab18SMatthew Ahrens if (error == 0) { 12413b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 12423b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 12433b2aab18SMatthew Ahrens fnvlist_num_pairs(snaps) * 3); 12443b2aab18SMatthew Ahrens } 12453b2aab18SMatthew Ahrens 12463b2aab18SMatthew Ahrens if (suspended != NULL) { 12473b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 12483b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 12493b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 12503b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 12513b2aab18SMatthew Ahrens } 12523b2aab18SMatthew Ahrens fnvlist_free(suspended); 12533b2aab18SMatthew Ahrens } 12543b2aab18SMatthew Ahrens 12553b2aab18SMatthew Ahrens return (error); 12563b2aab18SMatthew Ahrens } 12573b2aab18SMatthew Ahrens 12583b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 12593b2aab18SMatthew Ahrens const char *ddsta_fsname; 12603b2aab18SMatthew Ahrens const char *ddsta_snapname; 12613b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 12623b2aab18SMatthew Ahrens const char *ddsta_htag; 12633b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 12643b2aab18SMatthew Ahrens 12653b2aab18SMatthew Ahrens static int 12663b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 12673b2aab18SMatthew Ahrens { 12683b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 12693b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 12703b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12713b2aab18SMatthew Ahrens int error; 12723b2aab18SMatthew Ahrens 12733b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 12743b2aab18SMatthew Ahrens if (error != 0) 12753b2aab18SMatthew Ahrens return (error); 12763b2aab18SMatthew Ahrens 1277ca48f36fSKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1278ca48f36fSKeith M Wesolowski tx, B_FALSE); 12793b2aab18SMatthew Ahrens if (error != 0) { 12803b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12813b2aab18SMatthew Ahrens return (error); 12823b2aab18SMatthew Ahrens } 12833b2aab18SMatthew Ahrens 12843b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 12853b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1286be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 12873b2aab18SMatthew Ahrens } 12883b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 12893b2aab18SMatthew Ahrens B_TRUE, tx); 12903b2aab18SMatthew Ahrens if (error != 0) { 12913b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12923b2aab18SMatthew Ahrens return (error); 12933b2aab18SMatthew Ahrens } 12943b2aab18SMatthew Ahrens 12953b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12963b2aab18SMatthew Ahrens return (0); 12973b2aab18SMatthew Ahrens } 12983b2aab18SMatthew Ahrens 12993b2aab18SMatthew Ahrens static void 13003b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 13013b2aab18SMatthew Ahrens { 13023b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 13033b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 13043b2aab18SMatthew Ahrens dsl_dataset_t *ds; 13053b2aab18SMatthew Ahrens 13063b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 13073b2aab18SMatthew Ahrens 13083b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 13093b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 13103b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 13113b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 13123b2aab18SMatthew Ahrens 13133b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 13143b2aab18SMatthew Ahrens } 13153b2aab18SMatthew Ahrens 13163b2aab18SMatthew Ahrens int 13173b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 13183b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 13193b2aab18SMatthew Ahrens { 13203b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 13213b2aab18SMatthew Ahrens int error; 13223b2aab18SMatthew Ahrens spa_t *spa; 13233b2aab18SMatthew Ahrens boolean_t needsuspend; 13243b2aab18SMatthew Ahrens void *cookie; 13253b2aab18SMatthew Ahrens 13263b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 13273b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 13283b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 13293b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 13303b2aab18SMatthew Ahrens 13313b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 13323b2aab18SMatthew Ahrens if (error != 0) 13333b2aab18SMatthew Ahrens return (error); 13343b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 13353b2aab18SMatthew Ahrens spa_close(spa, FTAG); 13363b2aab18SMatthew Ahrens 13373b2aab18SMatthew Ahrens if (needsuspend) { 13383b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 13393b2aab18SMatthew Ahrens if (error != 0) 13403b2aab18SMatthew Ahrens return (error); 13413b2aab18SMatthew Ahrens } 13423b2aab18SMatthew Ahrens 13433b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 13443b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3); 13453b2aab18SMatthew Ahrens 13463b2aab18SMatthew Ahrens if (needsuspend) 13473b2aab18SMatthew Ahrens zil_resume(cookie); 13483b2aab18SMatthew Ahrens return (error); 13493b2aab18SMatthew Ahrens } 13503b2aab18SMatthew Ahrens 13513b2aab18SMatthew Ahrens 13523b2aab18SMatthew Ahrens void 13533b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 13543b2aab18SMatthew Ahrens { 13553b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 13563b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 13573b2aab18SMatthew Ahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 13583b2aab18SMatthew Ahrens 13593b2aab18SMatthew Ahrens /* 13603b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 13613b2aab18SMatthew Ahrens * sync it out now. 13623b2aab18SMatthew Ahrens */ 13633b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13643b2aab18SMatthew Ahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 13653b2aab18SMatthew Ahrens 13663b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 13673b2aab18SMatthew Ahrens } 13683b2aab18SMatthew Ahrens 13693b2aab18SMatthew Ahrens static void 13703b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 13713b2aab18SMatthew Ahrens { 13723b2aab18SMatthew Ahrens uint64_t count = 0; 13733b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 13743b2aab18SMatthew Ahrens zap_cursor_t zc; 13753b2aab18SMatthew Ahrens zap_attribute_t za; 13763b2aab18SMatthew Ahrens nvlist_t *propval = fnvlist_alloc(); 13773b2aab18SMatthew Ahrens nvlist_t *val = fnvlist_alloc(); 13783b2aab18SMatthew Ahrens 13793b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 138019b94df9SMatthew Ahrens 138119b94df9SMatthew Ahrens /* 13823b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 138319b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 138419b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 138519b94df9SMatthew Ahrens */ 138619b94df9SMatthew Ahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 13873b2aab18SMatthew Ahrens ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj, 138819b94df9SMatthew Ahrens &count)); 138919b94df9SMatthew Ahrens } 13903b2aab18SMatthew Ahrens if (count != ds->ds_phys->ds_num_children - 1) 139119b94df9SMatthew Ahrens goto fail; 139219b94df9SMatthew Ahrens for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj); 139319b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 139419b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 139519b94df9SMatthew Ahrens dsl_dataset_t *clone; 139619b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 13973b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 13983b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 139919b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 14003b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 140119b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 140219b94df9SMatthew Ahrens } 140319b94df9SMatthew Ahrens zap_cursor_fini(&zc); 14043b2aab18SMatthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 14053b2aab18SMatthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 140619b94df9SMatthew Ahrens fail: 140719b94df9SMatthew Ahrens nvlist_free(val); 140819b94df9SMatthew Ahrens nvlist_free(propval); 140919b94df9SMatthew Ahrens } 141019b94df9SMatthew Ahrens 1411fa9e4066Sahrens void 1412a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1413fa9e4066Sahrens { 14143b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1415187d6ac0SMatt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio; 1416a9799022Sck 14173b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 14183b2aab18SMatthew Ahrens 14194445fffbSMatthew Ahrens ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 14204445fffbSMatthew Ahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 14214445fffbSMatthew Ahrens ds->ds_phys->ds_compressed_bytes); 14224445fffbSMatthew Ahrens 14234445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 142477372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 142577372cb0SMatthew Ahrens ds->ds_phys->ds_uncompressed_bytes); 14264445fffbSMatthew Ahrens 14274445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 14284445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 14294445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 14304445fffbSMatthew Ahrens ds->ds_phys->ds_unique_bytes); 14314445fffbSMatthew Ahrens get_clones_stat(ds, nv); 14324445fffbSMatthew Ahrens } else { 14334445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 14344445fffbSMatthew Ahrens } 1435fa9e4066Sahrens 1436a9799022Sck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1437a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1438a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1439a9799022Sck 1440a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1441a2eea2e1Sahrens ds->ds_phys->ds_creation_time); 1442a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1443a2eea2e1Sahrens ds->ds_phys->ds_creation_txg); 1444a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1445a9799022Sck ds->ds_quota); 1446a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1447a9799022Sck ds->ds_reserved); 1448c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1449c5904d13Seschrock ds->ds_phys->ds_guid); 14501d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 14513f9d6ad7SLin Ling ds->ds_phys->ds_unique_bytes); 14521d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 14531d713200SEric Schrock ds->ds_object); 145492241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 145592241e0bSTom Erickson ds->ds_userrefs); 1456842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1457842727c2SChris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1458fa9e4066Sahrens 145919b94df9SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 146019b94df9SMatthew Ahrens uint64_t written, comp, uncomp; 146119b94df9SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 146219b94df9SMatthew Ahrens dsl_dataset_t *prev; 146319b94df9SMatthew Ahrens 146419b94df9SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, 146519b94df9SMatthew Ahrens ds->ds_phys->ds_prev_snap_obj, FTAG, &prev); 146619b94df9SMatthew Ahrens if (err == 0) { 146719b94df9SMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written, 146819b94df9SMatthew Ahrens &comp, &uncomp); 146919b94df9SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 147019b94df9SMatthew Ahrens if (err == 0) { 147119b94df9SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 147219b94df9SMatthew Ahrens written); 147319b94df9SMatthew Ahrens } 147419b94df9SMatthew Ahrens } 147519b94df9SMatthew Ahrens } 1476fa9e4066Sahrens } 1477fa9e4066Sahrens 1478a2eea2e1Sahrens void 1479a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1480a2eea2e1Sahrens { 14813b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 14823b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 14833b2aab18SMatthew Ahrens 1484a2eea2e1Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 1485a2eea2e1Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 14863cb34c60Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 14874445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 14884445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 1489a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 1490a2eea2e1Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 1491ebedde84SEric Taylor } else { 1492ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 1493ebedde84SEric Taylor stat->dds_num_clones = 0; 1494a2eea2e1Sahrens 14954445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 14964445fffbSMatthew Ahrens dsl_dataset_t *ods; 1497a2eea2e1Sahrens 14983b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 14994445fffbSMatthew Ahrens ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 15004445fffbSMatthew Ahrens dsl_dataset_name(ods, stat->dds_origin); 15013b2aab18SMatthew Ahrens dsl_dataset_rele(ods, FTAG); 15024445fffbSMatthew Ahrens } 1503a2eea2e1Sahrens } 1504a2eea2e1Sahrens } 1505a2eea2e1Sahrens 1506a2eea2e1Sahrens uint64_t 1507a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1508a2eea2e1Sahrens { 150991ebeef5Sahrens return (ds->ds_fsid_guid); 1510a2eea2e1Sahrens } 1511a2eea2e1Sahrens 1512a2eea2e1Sahrens void 1513a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 1514a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 1515a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1516fa9e4066Sahrens { 1517ad135b5dSChristopher Siden *refdbytesp = ds->ds_phys->ds_referenced_bytes; 1518a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1519a9799022Sck if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 1520a9799022Sck *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 1521a9799022Sck if (ds->ds_quota != 0) { 1522a9799022Sck /* 1523a9799022Sck * Adjust available bytes according to refquota 1524a9799022Sck */ 1525a9799022Sck if (*refdbytesp < ds->ds_quota) 1526a9799022Sck *availbytesp = MIN(*availbytesp, 1527a9799022Sck ds->ds_quota - *refdbytesp); 1528a9799022Sck else 1529a9799022Sck *availbytesp = 0; 1530a9799022Sck } 1531a2eea2e1Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 1532a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1533fa9e4066Sahrens } 1534fa9e4066Sahrens 1535f18faf3fSek boolean_t 153634f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 1537f18faf3fSek { 1538f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 1539f18faf3fSek 15403b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 154134f2f8cfSMatthew Ahrens if (snap == NULL) 1542f18faf3fSek return (B_FALSE); 1543f18faf3fSek if (ds->ds_phys->ds_bp.blk_birth > 154434f2f8cfSMatthew Ahrens snap->ds_phys->ds_creation_txg) { 154534f2f8cfSMatthew Ahrens objset_t *os, *os_snap; 15466e0cbcaaSMatthew Ahrens /* 15476e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 15486e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 15496e0cbcaaSMatthew Ahrens * modified. 15506e0cbcaaSMatthew Ahrens */ 15516e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 15526e0cbcaaSMatthew Ahrens return (B_TRUE); 155334f2f8cfSMatthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0) 15546e0cbcaaSMatthew Ahrens return (B_TRUE); 15556e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 155634f2f8cfSMatthew Ahrens &os_snap->os_phys->os_meta_dnode, 15576e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 15586e0cbcaaSMatthew Ahrens } 1559f18faf3fSek return (B_FALSE); 1560f18faf3fSek } 1561f18faf3fSek 15623b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 15633b2aab18SMatthew Ahrens const char *ddrsa_fsname; 15643b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 15653b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 15663b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 15673b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 15683b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 15693b2aab18SMatthew Ahrens 15701d452cf5Sahrens /* ARGSUSED */ 1571fa9e4066Sahrens static int 15723b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 15733b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1574fa9e4066Sahrens { 15753b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 15763b2aab18SMatthew Ahrens int error; 1577fa9e4066Sahrens uint64_t val; 1578fa9e4066Sahrens 15793b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 15803b2aab18SMatthew Ahrens if (error != 0) { 15813b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 15823b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 15833b2aab18SMatthew Ahrens } 15841d452cf5Sahrens 15853b2aab18SMatthew Ahrens /* new name should not exist */ 15863b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 15873b2aab18SMatthew Ahrens if (error == 0) 1588be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 15893b2aab18SMatthew Ahrens else if (error == ENOENT) 15903b2aab18SMatthew Ahrens error = 0; 1591cdf5b4caSmmusante 1592cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 15933b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 15943b2aab18SMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN) 1595be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 1596cdf5b4caSmmusante 15973b2aab18SMatthew Ahrens return (error); 15981d452cf5Sahrens } 1599fa9e4066Sahrens 16003b2aab18SMatthew Ahrens static int 16013b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 16021d452cf5Sahrens { 16033b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 16043b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16051d452cf5Sahrens dsl_dataset_t *hds; 16063b2aab18SMatthew Ahrens int error; 1607fa9e4066Sahrens 16083b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 16093b2aab18SMatthew Ahrens if (error != 0) 16103b2aab18SMatthew Ahrens return (error); 1611fa9e4066Sahrens 16123b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 16133b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 16143b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 16153b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 16163b2aab18SMatthew Ahrens } else { 16173b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 16183b2aab18SMatthew Ahrens } 1619745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 16203b2aab18SMatthew Ahrens return (error); 1621fa9e4066Sahrens } 1622fa9e4066Sahrens 1623cdf5b4caSmmusante static int 16243b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 16253b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1626cdf5b4caSmmusante { 16273b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 16283b2aab18SMatthew Ahrens dsl_dataset_t *ds; 16293b2aab18SMatthew Ahrens uint64_t val; 16303b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 16313b2aab18SMatthew Ahrens int error; 1632ecd6cf80Smarks 16333b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 16343b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 16353b2aab18SMatthew Ahrens if (error == ENOENT) { 16363b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 16373b2aab18SMatthew Ahrens return (0); 1638ecd6cf80Smarks } 1639ecd6cf80Smarks 16403b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 16413b2aab18SMatthew Ahrens 16423b2aab18SMatthew Ahrens /* log before we change the name */ 16433b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 16443b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 1645cdf5b4caSmmusante 16463b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx)); 16473b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 16483b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 16493b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 16503b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj, 16513b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 1652cdf5b4caSmmusante 16533b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1654cdf5b4caSmmusante return (0); 1655cdf5b4caSmmusante } 1656cdf5b4caSmmusante 16573b2aab18SMatthew Ahrens static void 16583b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 1659cdf5b4caSmmusante { 16603b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 16613b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16623b2aab18SMatthew Ahrens dsl_dataset_t *hds; 1663cdf5b4caSmmusante 16643b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 16653b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 16663b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 16673b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 16683b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 16693b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 16703b2aab18SMatthew Ahrens } else { 16713b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 1672cdf5b4caSmmusante } 16733b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 1674cdf5b4caSmmusante } 1675cdf5b4caSmmusante 16763b2aab18SMatthew Ahrens int 16773b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 16783b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 16793a5a36beSmmusante { 16803b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 16813a5a36beSmmusante 16823b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 16833b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 16843b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 16853b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 16863a5a36beSmmusante 16873b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 16883b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 1)); 16893a5a36beSmmusante } 16903a5a36beSmmusante 169191948b51SKeith M Wesolowski /* 169291948b51SKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is 169391948b51SKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here 169491948b51SKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want 169591948b51SKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away 169691948b51SKeith M Wesolowski * when we release the long hold. 169791948b51SKeith M Wesolowski */ 169891948b51SKeith M Wesolowski static int 169991948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 170091948b51SKeith M Wesolowski { 170191948b51SKeith M Wesolowski boolean_t held; 170291948b51SKeith M Wesolowski 170391948b51SKeith M Wesolowski if (!dmu_tx_is_syncing(tx)) 170491948b51SKeith M Wesolowski return (0); 170591948b51SKeith M Wesolowski 170691948b51SKeith M Wesolowski if (owner != NULL) { 170791948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner); 170891948b51SKeith M Wesolowski dsl_dataset_long_rele(ds, owner); 170991948b51SKeith M Wesolowski } 171091948b51SKeith M Wesolowski 171191948b51SKeith M Wesolowski held = dsl_dataset_long_held(ds); 171291948b51SKeith M Wesolowski 171391948b51SKeith M Wesolowski if (owner != NULL) 171491948b51SKeith M Wesolowski dsl_dataset_long_hold(ds, owner); 171591948b51SKeith M Wesolowski 171691948b51SKeith M Wesolowski if (held) 171791948b51SKeith M Wesolowski return (SET_ERROR(EBUSY)); 171891948b51SKeith M Wesolowski 171991948b51SKeith M Wesolowski return (0); 172091948b51SKeith M Wesolowski } 172191948b51SKeith M Wesolowski 172291948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg { 172391948b51SKeith M Wesolowski const char *ddra_fsname; 172491948b51SKeith M Wesolowski void *ddra_owner; 1725*a7027df1SMatthew Ahrens nvlist_t *ddra_result; 172691948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t; 172791948b51SKeith M Wesolowski 17283b2aab18SMatthew Ahrens static int 17293b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 1730fa9e4066Sahrens { 173191948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 17323b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17331d452cf5Sahrens dsl_dataset_t *ds; 17343b2aab18SMatthew Ahrens int64_t unused_refres_delta; 17353b2aab18SMatthew Ahrens int error; 1736fa9e4066Sahrens 173791948b51SKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 17383b2aab18SMatthew Ahrens if (error != 0) 17393b2aab18SMatthew Ahrens return (error); 1740370c1af0SSanjeev Bagewadi 17413b2aab18SMatthew Ahrens /* must not be a snapshot */ 17423b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 17433b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1744be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 17453b2aab18SMatthew Ahrens } 17463a5a36beSmmusante 17473b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 17483b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 17493b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1750be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 17513b2aab18SMatthew Ahrens } 17523a5a36beSmmusante 175391948b51SKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 175491948b51SKeith M Wesolowski if (error != 0) { 17553b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 175691948b51SKeith M Wesolowski return (error); 17573b2aab18SMatthew Ahrens } 17583b2aab18SMatthew Ahrens 17593b2aab18SMatthew Ahrens /* 17603b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 17613b2aab18SMatthew Ahrens * the refquota. 17623b2aab18SMatthew Ahrens */ 17633b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 17643b2aab18SMatthew Ahrens ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) { 17653b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1766be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 1767fa9e4066Sahrens } 1768370c1af0SSanjeev Bagewadi 17693b2aab18SMatthew Ahrens /* 17703b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 17713b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 17723b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 17733b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 17743b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 17753b2aab18SMatthew Ahrens */ 17763b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 17773b2aab18SMatthew Ahrens ds->ds_phys->ds_unique_bytes); 17783b2aab18SMatthew Ahrens 17793b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 17803b2aab18SMatthew Ahrens unused_refres_delta > 17813b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 17823b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1783be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1784fa9e4066Sahrens } 1785fa9e4066Sahrens 17863b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 17873b2aab18SMatthew Ahrens return (0); 17883b2aab18SMatthew Ahrens } 17891d452cf5Sahrens 17903b2aab18SMatthew Ahrens static void 17913b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 17923b2aab18SMatthew Ahrens { 179391948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 17943b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17953b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 17963b2aab18SMatthew Ahrens uint64_t cloneobj; 1797*a7027df1SMatthew Ahrens char namebuf[ZFS_MAXNAMELEN]; 17981d452cf5Sahrens 179991948b51SKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 18001d452cf5Sahrens 1801*a7027df1SMatthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf); 1802*a7027df1SMatthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf); 1803*a7027df1SMatthew Ahrens 18043b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 18053b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 18061d452cf5Sahrens 18073b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 18081d452cf5Sahrens 18093b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 18103b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 18113b2aab18SMatthew Ahrens 18123b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 18133b2aab18SMatthew Ahrens 18143b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 18153b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 18163b2aab18SMatthew Ahrens } 18173b2aab18SMatthew Ahrens 181891948b51SKeith M Wesolowski /* 1819*a7027df1SMatthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot. 1820*a7027df1SMatthew Ahrens * The name of the most recent snapshot will be returned under key "target" 1821*a7027df1SMatthew Ahrens * in the result nvlist. 182291948b51SKeith M Wesolowski * 1823*a7027df1SMatthew Ahrens * If owner != NULL: 182491948b51SKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry 182591948b51SKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we 182691948b51SKeith M Wesolowski * succeed or not. 182791948b51SKeith M Wesolowski * 182891948b51SKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See 182991948b51SKeith M Wesolowski * notes above zfs_suspend_fs() for further details. 183091948b51SKeith M Wesolowski */ 18313b2aab18SMatthew Ahrens int 1832*a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result) 18333b2aab18SMatthew Ahrens { 183491948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t ddra; 183591948b51SKeith M Wesolowski 183691948b51SKeith M Wesolowski ddra.ddra_fsname = fsname; 183791948b51SKeith M Wesolowski ddra.ddra_owner = owner; 1838*a7027df1SMatthew Ahrens ddra.ddra_result = result; 183991948b51SKeith M Wesolowski 18403b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 1841*a7027df1SMatthew Ahrens dsl_dataset_rollback_sync, &ddra, 1)); 1842fa9e4066Sahrens } 184399653d4eSeschrock 1844088f3894Sahrens struct promotenode { 1845745cd3c5Smaybee list_node_t link; 1846745cd3c5Smaybee dsl_dataset_t *ds; 1847745cd3c5Smaybee }; 1848745cd3c5Smaybee 18493b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg { 18503b2aab18SMatthew Ahrens const char *ddpa_clonename; 18513b2aab18SMatthew Ahrens dsl_dataset_t *ddpa_clone; 185274e7dc98SMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps; 18533b2aab18SMatthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */ 185474e7dc98SMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 1855681d9761SEric Taylor char *err_ds; 18563b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t; 18571d452cf5Sahrens 185874e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 18593b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 18603b2aab18SMatthew Ahrens void *tag); 18613b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 186274e7dc98SMatthew Ahrens 186399653d4eSeschrock static int 18643b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 186599653d4eSeschrock { 18663b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 18673b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 18683b2aab18SMatthew Ahrens dsl_dataset_t *hds; 18693b2aab18SMatthew Ahrens struct promotenode *snap; 18703b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 1871745cd3c5Smaybee int err; 1872cde58dbcSMatthew Ahrens uint64_t unused; 18731d452cf5Sahrens 18743b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 18753b2aab18SMatthew Ahrens if (err != 0) 18763b2aab18SMatthew Ahrens return (err); 187799653d4eSeschrock 18783b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 18791d452cf5Sahrens 18803b2aab18SMatthew Ahrens if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) { 18813b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 1882be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 18833b2aab18SMatthew Ahrens } 18843b2aab18SMatthew Ahrens 18853b2aab18SMatthew Ahrens /* 18863b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 18873b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 18883b2aab18SMatthew Ahrens */ 18893b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 18903b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 18913b2aab18SMatthew Ahrens return (0); 18923b2aab18SMatthew Ahrens } 18933b2aab18SMatthew Ahrens 18943b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 18953b2aab18SMatthew Ahrens origin_ds = snap->ds; 189699653d4eSeschrock 18973cb34c60Sahrens /* compute origin's new unique space */ 18983b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 189974e7dc98SMatthew Ahrens ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 1900cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 1901cde58dbcSMatthew Ahrens origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, 19023b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 190399653d4eSeschrock 1904745cd3c5Smaybee /* 1905745cd3c5Smaybee * Walk the snapshots that we are moving 1906745cd3c5Smaybee * 190774e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 19083b2aab18SMatthew Ahrens * to used by each snapshot: 190974e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 191074e7dc98SMatthew Ahrens * So each snapshot gave birth to: 191174e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 1912745cd3c5Smaybee * So a sequence would look like: 191374e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 1914745cd3c5Smaybee * Which simplifies to: 191574e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 1916745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 191774e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 1918745cd3c5Smaybee */ 19193b2aab18SMatthew Ahrens ddpa->used = origin_ds->ds_phys->ds_referenced_bytes; 19203b2aab18SMatthew Ahrens ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes; 19213b2aab18SMatthew Ahrens ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 19223b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 19233b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 192499653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 1925745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 192699653d4eSeschrock 19273b2aab18SMatthew Ahrens /* 19283b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 19293b2aab18SMatthew Ahrens * the objset. 19303b2aab18SMatthew Ahrens */ 19313b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 1932be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 19333b2aab18SMatthew Ahrens goto out; 19343b2aab18SMatthew Ahrens } 19353b2aab18SMatthew Ahrens 193699653d4eSeschrock /* Check that the snapshot name does not conflict */ 19373b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 1938745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 1939681d9761SEric Taylor if (err == 0) { 19403b2aab18SMatthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 1941be6fd75aSMatthew Ahrens err = SET_ERROR(EEXIST); 1942681d9761SEric Taylor goto out; 1943681d9761SEric Taylor } 1944745cd3c5Smaybee if (err != ENOENT) 1945681d9761SEric Taylor goto out; 194699653d4eSeschrock 1947745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 194874e7dc98SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj == 0) 194974e7dc98SMatthew Ahrens continue; 195074e7dc98SMatthew Ahrens 1951cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 1952cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 19533b2aab18SMatthew Ahrens ddpa->used += dlused; 19543b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 19553b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 195674e7dc98SMatthew Ahrens } 1957745cd3c5Smaybee 1958745cd3c5Smaybee /* 1959745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 1960745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 1961745cd3c5Smaybee */ 19623b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 19633b2aab18SMatthew Ahrens ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes; 19643b2aab18SMatthew Ahrens ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes; 19653b2aab18SMatthew Ahrens ddpa->uncomp -= 19663b2aab18SMatthew Ahrens ddpa->origin_origin->ds_phys->ds_uncompressed_bytes; 196799653d4eSeschrock } 196899653d4eSeschrock 196999653d4eSeschrock /* Check that there is enough space here */ 197074e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 19713b2aab18SMatthew Ahrens ddpa->used); 19723b2aab18SMatthew Ahrens if (err != 0) 19733b2aab18SMatthew Ahrens goto out; 197474e7dc98SMatthew Ahrens 197574e7dc98SMatthew Ahrens /* 197674e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 197774e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 197874e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 197974e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 198074e7dc98SMatthew Ahrens */ 198174e7dc98SMatthew Ahrens if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 198274e7dc98SMatthew Ahrens uint64_t space; 198374e7dc98SMatthew Ahrens 198474e7dc98SMatthew Ahrens /* 198574e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 19863f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 1987cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 198874e7dc98SMatthew Ahrens * calls will be fast because they do not have to 198974e7dc98SMatthew Ahrens * iterate over all bps. 199074e7dc98SMatthew Ahrens */ 19913b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 19923b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 19933b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 19943b2aab18SMatthew Ahrens if (err != 0) 19953b2aab18SMatthew Ahrens goto out; 199674e7dc98SMatthew Ahrens 19973b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 19983f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 19993b2aab18SMatthew Ahrens if (err != 0) 20003b2aab18SMatthew Ahrens goto out; 20013b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 200274e7dc98SMatthew Ahrens } 200374e7dc98SMatthew Ahrens if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 20043b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 20053b2aab18SMatthew Ahrens origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap); 20063b2aab18SMatthew Ahrens if (err != 0) 20073b2aab18SMatthew Ahrens goto out; 2008745cd3c5Smaybee } 20091d452cf5Sahrens 2010681d9761SEric Taylor out: 20113b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2012681d9761SEric Taylor return (err); 20131d452cf5Sahrens } 201499653d4eSeschrock 20151d452cf5Sahrens static void 20163b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 20171d452cf5Sahrens { 20183b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 20193b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 20203b2aab18SMatthew Ahrens dsl_dataset_t *hds; 20213b2aab18SMatthew Ahrens struct promotenode *snap; 20223b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 20233b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 20243b2aab18SMatthew Ahrens dsl_dir_t *dd; 20253cb34c60Sahrens dsl_dir_t *odd = NULL; 2026088f3894Sahrens uint64_t oldnext_obj; 202774e7dc98SMatthew Ahrens int64_t delta; 20281d452cf5Sahrens 20293b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 20303b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 20313b2aab18SMatthew Ahrens 20323b2aab18SMatthew Ahrens ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE); 20331d452cf5Sahrens 20343b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 20353b2aab18SMatthew Ahrens origin_ds = snap->ds; 20363b2aab18SMatthew Ahrens dd = hds->ds_dir; 20373b2aab18SMatthew Ahrens 20383b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 203974e7dc98SMatthew Ahrens origin_head = snap->ds; 204074e7dc98SMatthew Ahrens 20410b69c2f0Sahrens /* 20423cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 20430b69c2f0Sahrens * changing. 20440b69c2f0Sahrens */ 20453b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 20463cb34c60Sahrens NULL, FTAG, &odd)); 204799653d4eSeschrock 2048745cd3c5Smaybee /* change origin's next snap */ 2049745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2050088f3894Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 20513b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 205274e7dc98SMatthew Ahrens ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 205374e7dc98SMatthew Ahrens origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 2054745cd3c5Smaybee 2055088f3894Sahrens /* change the origin's next clone */ 2056088f3894Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 20573b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 20583b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 20593b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2060088f3894Sahrens origin_ds->ds_phys->ds_next_clones_obj, 2061088f3894Sahrens oldnext_obj, tx)); 2062088f3894Sahrens } 2063088f3894Sahrens 2064745cd3c5Smaybee /* change origin */ 2065745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2066745cd3c5Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 2067745cd3c5Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 20683f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2069745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2070745cd3c5Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 20713f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 20723f9d6ad7SLin Ling origin_ds->ds_phys->ds_creation_txg; 2073745cd3c5Smaybee 2074cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2075cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 20763b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2077cde58dbcSMatthew Ahrens odd->dd_phys->dd_clones, hds->ds_object, tx)); 20783b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 20793b2aab18SMatthew Ahrens ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2080cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2081cde58dbcSMatthew Ahrens 20823b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 20833b2aab18SMatthew Ahrens ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2084cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2085cde58dbcSMatthew Ahrens if (dd->dd_phys->dd_clones == 0) { 2086cde58dbcSMatthew Ahrens dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset, 2087cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 2088cde58dbcSMatthew Ahrens } 20893b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2090cde58dbcSMatthew Ahrens dd->dd_phys->dd_clones, origin_head->ds_object, tx)); 2091cde58dbcSMatthew Ahrens } 2092cde58dbcSMatthew Ahrens 209399653d4eSeschrock /* move snapshots to this dir */ 20943b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 20953b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2096745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 209799653d4eSeschrock 20983b2aab18SMatthew Ahrens /* 20993b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 21003b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 21013b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 21023b2aab18SMatthew Ahrens */ 2103503ad85cSMatthew Ahrens if (ds->ds_objset) { 2104503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2105503ad85cSMatthew Ahrens ds->ds_objset = NULL; 21063baa08fcSek } 21073b2aab18SMatthew Ahrens 210899653d4eSeschrock /* move snap name entry */ 21093b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 21103b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2111745cd3c5Smaybee ds->ds_snapname, tx)); 21123b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 211399653d4eSeschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 211499653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2115cde58dbcSMatthew Ahrens 211699653d4eSeschrock /* change containing dsl_dir */ 211799653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 21183cb34c60Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 211999653d4eSeschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 21203cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 21213b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 21223b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 212399653d4eSeschrock NULL, ds, &ds->ds_dir)); 212499653d4eSeschrock 2125cde58dbcSMatthew Ahrens /* move any clone references */ 2126cde58dbcSMatthew Ahrens if (ds->ds_phys->ds_next_clones_obj && 2127cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2128cde58dbcSMatthew Ahrens zap_cursor_t zc; 2129cde58dbcSMatthew Ahrens zap_attribute_t za; 2130cde58dbcSMatthew Ahrens 21313b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 21323b2aab18SMatthew Ahrens ds->ds_phys->ds_next_clones_obj); 21333b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 21343b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 21353b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 21363b2aab18SMatthew Ahrens uint64_t o; 2137a9799022Sck 21383b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 21393b2aab18SMatthew Ahrens /* 21403b2aab18SMatthew Ahrens * We've already moved the 21413b2aab18SMatthew Ahrens * origin's reference. 21423b2aab18SMatthew Ahrens */ 21433b2aab18SMatthew Ahrens continue; 21443b2aab18SMatthew Ahrens } 2145a9799022Sck 21463b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 21473b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 21483b2aab18SMatthew Ahrens o = cnds->ds_dir->dd_phys->dd_head_dataset_obj; 2149a9799022Sck 21503b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 21513b2aab18SMatthew Ahrens odd->dd_phys->dd_clones, o, tx)); 21523b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 21533b2aab18SMatthew Ahrens dd->dd_phys->dd_clones, o, tx)); 21543b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 21553b2aab18SMatthew Ahrens } 21563b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 21573b2aab18SMatthew Ahrens } 21589082849eSck 21593b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2160a9799022Sck } 2161a9799022Sck 2162a9799022Sck /* 21633b2aab18SMatthew Ahrens * Change space accounting. 21643b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 21653b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 21663b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2167a9799022Sck */ 2168a9799022Sck 21693b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 21703b2aab18SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 21713b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 21723b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 21733b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 21743b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 21753b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 21763b2aab18SMatthew Ahrens 21773b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 21783b2aab18SMatthew Ahrens odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 21793b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 21803b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 21813b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 21823b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 21833b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 21843b2aab18SMatthew Ahrens 21853b2aab18SMatthew Ahrens origin_ds->ds_phys->ds_unique_bytes = ddpa->unique; 21863b2aab18SMatthew Ahrens 21873b2aab18SMatthew Ahrens /* log history record */ 21883b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 21893b2aab18SMatthew Ahrens 21903b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 21913b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2192a9799022Sck } 2193a9799022Sck 21943b2aab18SMatthew Ahrens /* 21953b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 21963b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 21973b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 21983b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 21993b2aab18SMatthew Ahrens */ 2200a9799022Sck static int 22013b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 22023b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2203a9799022Sck { 22043b2aab18SMatthew Ahrens uint64_t obj = last_obj; 2205a9799022Sck 22063b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 22073b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 2208a9799022Sck 22093b2aab18SMatthew Ahrens while (obj != first_obj) { 22103b2aab18SMatthew Ahrens dsl_dataset_t *ds; 22113b2aab18SMatthew Ahrens struct promotenode *snap; 22123b2aab18SMatthew Ahrens int err; 221392241e0bSTom Erickson 22143b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 22153b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 22163b2aab18SMatthew Ahrens if (err != 0) 22173b2aab18SMatthew Ahrens return (err); 2218a9799022Sck 22193b2aab18SMatthew Ahrens if (first_obj == 0) 22203b2aab18SMatthew Ahrens first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 22213b2aab18SMatthew Ahrens 22223b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 22233b2aab18SMatthew Ahrens snap->ds = ds; 22243b2aab18SMatthew Ahrens list_insert_tail(l, snap); 22253b2aab18SMatthew Ahrens obj = ds->ds_phys->ds_prev_snap_obj; 22263b2aab18SMatthew Ahrens } 2227a9799022Sck 2228a9799022Sck return (0); 2229a9799022Sck } 2230a9799022Sck 22313b2aab18SMatthew Ahrens static int 22323b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2233a9799022Sck { 22343b2aab18SMatthew Ahrens struct promotenode *snap; 2235a9799022Sck 22363b2aab18SMatthew Ahrens *spacep = 0; 22373b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 22383b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 22393b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 22403b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 22413b2aab18SMatthew Ahrens *spacep += used; 224292241e0bSTom Erickson } 22433b2aab18SMatthew Ahrens return (0); 2244a9799022Sck } 2245a9799022Sck 22463b2aab18SMatthew Ahrens static void 22473b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 2248a9799022Sck { 22493b2aab18SMatthew Ahrens struct promotenode *snap; 225092241e0bSTom Erickson 22513b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 22523b2aab18SMatthew Ahrens return; 2253a9799022Sck 22543b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 22553b2aab18SMatthew Ahrens list_remove(l, snap); 22563b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 22573b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 22583b2aab18SMatthew Ahrens } 22593b2aab18SMatthew Ahrens list_destroy(l); 2260a9799022Sck } 2261a9799022Sck 2262a9799022Sck static int 22633b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2264a9799022Sck { 22653b2aab18SMatthew Ahrens int error; 22663b2aab18SMatthew Ahrens dsl_dir_t *dd; 22673b2aab18SMatthew Ahrens struct promotenode *snap; 2268a9799022Sck 22693b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 22703b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 22713b2aab18SMatthew Ahrens if (error != 0) 22723b2aab18SMatthew Ahrens return (error); 22733b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 2274a9799022Sck 22753b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) || 22763b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 22773b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 2278be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 22793b2aab18SMatthew Ahrens } 2280a9799022Sck 22813b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj, 22823b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 22833b2aab18SMatthew Ahrens if (error != 0) 22843b2aab18SMatthew Ahrens goto out; 2285a9799022Sck 22863b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 22873b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 22883b2aab18SMatthew Ahrens if (error != 0) 22893b2aab18SMatthew Ahrens goto out; 2290a9799022Sck 22913b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 22923b2aab18SMatthew Ahrens ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 22933b2aab18SMatthew Ahrens error = snaplist_make(dp, dd->dd_phys->dd_origin_obj, 22943b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, 22953b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 22963b2aab18SMatthew Ahrens if (error != 0) 22973b2aab18SMatthew Ahrens goto out; 2298379c004dSEric Schrock 22993b2aab18SMatthew Ahrens if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) { 23003b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 23013b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_phys->dd_origin_obj, 23023b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 23033b2aab18SMatthew Ahrens if (error != 0) 23043b2aab18SMatthew Ahrens goto out; 2305379c004dSEric Schrock } 23063b2aab18SMatthew Ahrens out: 23073b2aab18SMatthew Ahrens if (error != 0) 23083b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 23093b2aab18SMatthew Ahrens return (error); 2310a9799022Sck } 2311a9799022Sck 2312a9799022Sck static void 23133b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2314a9799022Sck { 23153b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 23163b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 23173b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 23183b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 23193b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 23203b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 23213b2aab18SMatthew Ahrens } 232202c8f3f0SMatthew Ahrens 23233b2aab18SMatthew Ahrens /* 23243b2aab18SMatthew Ahrens * Promote a clone. 23253b2aab18SMatthew Ahrens * 23263b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 23273b2aab18SMatthew Ahrens * in with the name. (It must be at least MAXNAMELEN bytes long.) 23283b2aab18SMatthew Ahrens */ 23293b2aab18SMatthew Ahrens int 23303b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 23313b2aab18SMatthew Ahrens { 23323b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 23333b2aab18SMatthew Ahrens uint64_t numsnaps; 23343b2aab18SMatthew Ahrens int error; 23353b2aab18SMatthew Ahrens objset_t *os; 233692241e0bSTom Erickson 23373b2aab18SMatthew Ahrens /* 23383b2aab18SMatthew Ahrens * We will modify space proportional to the number of 23393b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 23403b2aab18SMatthew Ahrens */ 23413b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 23423b2aab18SMatthew Ahrens if (error != 0) 23433b2aab18SMatthew Ahrens return (error); 23443b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 23453b2aab18SMatthew Ahrens dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps); 23463b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 23473b2aab18SMatthew Ahrens if (error != 0) 23483b2aab18SMatthew Ahrens return (error); 234902c8f3f0SMatthew Ahrens 23503b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 23513b2aab18SMatthew Ahrens ddpa.err_ds = conflsnap; 235202c8f3f0SMatthew Ahrens 23533b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check, 23543b2aab18SMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 2 + numsnaps)); 2355a9799022Sck } 2356a9799022Sck 2357a9799022Sck int 23583b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 235991948b51SKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 2360a9799022Sck { 23613b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2362a9799022Sck 23633b2aab18SMatthew Ahrens /* they should both be heads */ 23643b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(clone) || 23653b2aab18SMatthew Ahrens dsl_dataset_is_snapshot(origin_head)) 2366be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 236792241e0bSTom Erickson 236834f2f8cfSMatthew Ahrens /* if we are not forcing, the branch point should be just before them */ 236934f2f8cfSMatthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev) 2370be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2371a9799022Sck 23723b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 23733b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 23743b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 237534f2f8cfSMatthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir) 2376be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 237792241e0bSTom Erickson 23783b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 23793b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2380be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2381842727c2SChris Kirby 23823b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 238334f2f8cfSMatthew Ahrens if (!force && 238434f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 2385be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 2386c99e4bdcSChris Kirby 23873b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 238891948b51SKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx)) 2389be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 23903b2aab18SMatthew Ahrens 23913b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 23923b2aab18SMatthew Ahrens unused_refres_delta = 23933b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23943b2aab18SMatthew Ahrens origin_head->ds_phys->ds_unique_bytes) - 23953b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23963b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 23973b2aab18SMatthew Ahrens 23983b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 23993b2aab18SMatthew Ahrens unused_refres_delta > 24003b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2401be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 24023b2aab18SMatthew Ahrens 24033b2aab18SMatthew Ahrens /* clone can't be over the head's refquota */ 24043b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 24053b2aab18SMatthew Ahrens clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota) 2406be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2407c99e4bdcSChris Kirby 24083b2aab18SMatthew Ahrens return (0); 2409c99e4bdcSChris Kirby } 2410c99e4bdcSChris Kirby 2411a7f53a56SChris Kirby void 24123b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 24133b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 2414a7f53a56SChris Kirby { 24153b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 24163b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2417a7f53a56SChris Kirby 24183b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 24193b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 24203b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota); 242134f2f8cfSMatthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 2422842727c2SChris Kirby 24233b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 24243b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2425842727c2SChris Kirby 24263b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 24273b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 24283b2aab18SMatthew Ahrens clone->ds_objset = NULL; 24293b2aab18SMatthew Ahrens } 2430842727c2SChris Kirby 24313b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 24323b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 24333b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 2434842727c2SChris Kirby } 2435842727c2SChris Kirby 24363b2aab18SMatthew Ahrens unused_refres_delta = 24373b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 24383b2aab18SMatthew Ahrens origin_head->ds_phys->ds_unique_bytes) - 24393b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 24403b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 24413b2aab18SMatthew Ahrens 24423b2aab18SMatthew Ahrens /* 24433b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 24443b2aab18SMatthew Ahrens */ 24453b2aab18SMatthew Ahrens if (clone->ds_prev) { 24463b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 24473b2aab18SMatthew Ahrens uint64_t comp, uncomp; 24483b2aab18SMatthew Ahrens 24493b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 24503b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 24513b2aab18SMatthew Ahrens origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 24523b2aab18SMatthew Ahrens &origin->ds_phys->ds_unique_bytes, &comp, &uncomp); 24533b2aab18SMatthew Ahrens } 24543b2aab18SMatthew Ahrens 24553b2aab18SMatthew Ahrens /* swap blkptrs */ 24563b2aab18SMatthew Ahrens { 24573b2aab18SMatthew Ahrens blkptr_t tmp; 24583b2aab18SMatthew Ahrens tmp = origin_head->ds_phys->ds_bp; 24593b2aab18SMatthew Ahrens origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp; 24603b2aab18SMatthew Ahrens clone->ds_phys->ds_bp = tmp; 24613b2aab18SMatthew Ahrens } 24623b2aab18SMatthew Ahrens 24633b2aab18SMatthew Ahrens /* set dd_*_bytes */ 24643b2aab18SMatthew Ahrens { 24653b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 24663b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 24673b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 24683b2aab18SMatthew Ahrens 24693b2aab18SMatthew Ahrens ASSERT3U(clone->ds_dir->dd_phys-> 24703b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 24713b2aab18SMatthew Ahrens 24723b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 24733b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 24743b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 24753b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 247615508ac0SChris Kirby 24773b2aab18SMatthew Ahrens dused = clone->ds_phys->ds_referenced_bytes + cdl_used - 24783b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_referenced_bytes + odl_used); 24793b2aab18SMatthew Ahrens dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp - 24803b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_compressed_bytes + odl_comp); 24813b2aab18SMatthew Ahrens duncomp = clone->ds_phys->ds_uncompressed_bytes + 24823b2aab18SMatthew Ahrens cdl_uncomp - 24833b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp); 2484842727c2SChris Kirby 24853b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 24863b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 24873b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 24883b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 2489842727c2SChris Kirby 2490842727c2SChris Kirby /* 24913b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 24923b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 24933b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 24943b2aab18SMatthew Ahrens * changing that affects the snapused). 2495842727c2SChris Kirby */ 24963b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 24973b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 24983b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 24993b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 25003b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 25013b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 25023b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 25033b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 2504842727c2SChris Kirby } 2505842727c2SChris Kirby 25063b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 25073b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_referenced_bytes, 25083b2aab18SMatthew Ahrens clone->ds_phys->ds_referenced_bytes); 25093b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_compressed_bytes, 25103b2aab18SMatthew Ahrens clone->ds_phys->ds_compressed_bytes); 25113b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes, 25123b2aab18SMatthew Ahrens clone->ds_phys->ds_uncompressed_bytes); 25133b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_unique_bytes, 25143b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 2515842727c2SChris Kirby 25163b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 25173b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 25183b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 2519ca45db41SChris Kirby 25203b2aab18SMatthew Ahrens /* 25213b2aab18SMatthew Ahrens * Swap deadlists. 25223b2aab18SMatthew Ahrens */ 25233b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 25243b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 25253b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_deadlist_obj, 25263b2aab18SMatthew Ahrens clone->ds_phys->ds_deadlist_obj); 25273b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 25283b2aab18SMatthew Ahrens clone->ds_phys->ds_deadlist_obj); 25293b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 25303b2aab18SMatthew Ahrens origin_head->ds_phys->ds_deadlist_obj); 2531842727c2SChris Kirby 25323b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 2533842727c2SChris Kirby 25343b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 25353b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 2536842727c2SChris Kirby } 2537842727c2SChris Kirby 25383b2aab18SMatthew Ahrens /* 25393b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 25403b2aab18SMatthew Ahrens * return the name of that dataset. 25413b2aab18SMatthew Ahrens */ 2542a7f53a56SChris Kirby int 25433b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2544a7f53a56SChris Kirby { 25453b2aab18SMatthew Ahrens dsl_pool_t *dp; 25463b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2547a7f53a56SChris Kirby int error; 2548a7f53a56SChris Kirby 25493b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 25503b2aab18SMatthew Ahrens if (error != 0) 25513b2aab18SMatthew Ahrens return (error); 25523b2aab18SMatthew Ahrens 25533b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 25543b2aab18SMatthew Ahrens if (error == 0) { 25553b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 25563b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 25573b2aab18SMatthew Ahrens } 25583b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 2559a7f53a56SChris Kirby 2560a7f53a56SChris Kirby return (error); 2561a7f53a56SChris Kirby } 2562a7f53a56SChris Kirby 2563842727c2SChris Kirby int 25643b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 25653b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 2566842727c2SChris Kirby { 25673b2aab18SMatthew Ahrens int error = 0; 2568842727c2SChris Kirby 25693b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 2570842727c2SChris Kirby 25713b2aab18SMatthew Ahrens /* 25723b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 25733b2aab18SMatthew Ahrens * unconsumed refreservation space. 25743b2aab18SMatthew Ahrens */ 25753b2aab18SMatthew Ahrens *ref_rsrv = 0; 2576842727c2SChris Kirby 25773b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 25783b2aab18SMatthew Ahrens /* 25793b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 25803b2aab18SMatthew Ahrens */ 25813b2aab18SMatthew Ahrens if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 25823b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 25833b2aab18SMatthew Ahrens ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 25843b2aab18SMatthew Ahrens *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 25853b2aab18SMatthew Ahrens *ref_rsrv = 25863b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 2587842727c2SChris Kirby } 2588842727c2SChris Kirby 25893b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 25903b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 25913b2aab18SMatthew Ahrens return (0); 2592842727c2SChris Kirby } 25933b2aab18SMatthew Ahrens /* 25943b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 25953b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 25963b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 25973b2aab18SMatthew Ahrens * may free up space for us). 25983b2aab18SMatthew Ahrens */ 25993b2aab18SMatthew Ahrens if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) { 26003b2aab18SMatthew Ahrens if (inflight > 0 || 26013b2aab18SMatthew Ahrens ds->ds_phys->ds_referenced_bytes < ds->ds_quota) 2602be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 26033b2aab18SMatthew Ahrens else 2604be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 2605842727c2SChris Kirby } 26063b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2607842727c2SChris Kirby 2608842727c2SChris Kirby return (error); 2609842727c2SChris Kirby } 2610842727c2SChris Kirby 26113b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 26123b2aab18SMatthew Ahrens const char *ddsqra_name; 26133b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 26143b2aab18SMatthew Ahrens uint64_t ddsqra_value; 26153b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 2616842727c2SChris Kirby 26173b2aab18SMatthew Ahrens 26183b2aab18SMatthew Ahrens /* ARGSUSED */ 2619842727c2SChris Kirby static int 26203b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 2621842727c2SChris Kirby { 26223b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 26233b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 26243b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2625842727c2SChris Kirby int error; 26263b2aab18SMatthew Ahrens uint64_t newval; 2627842727c2SChris Kirby 26283b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 2629be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2630842727c2SChris Kirby 26313b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 26323b2aab18SMatthew Ahrens if (error != 0) 26333b2aab18SMatthew Ahrens return (error); 26343b2aab18SMatthew Ahrens 26353b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 26363b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2637be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2638842727c2SChris Kirby } 2639842727c2SChris Kirby 26403b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 26413b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 26423b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 26433b2aab18SMatthew Ahrens if (error != 0) { 26443b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2645842727c2SChris Kirby return (error); 2646842727c2SChris Kirby } 2647842727c2SChris Kirby 26483b2aab18SMatthew Ahrens if (newval == 0) { 26493b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 26503b2aab18SMatthew Ahrens return (0); 26513b2aab18SMatthew Ahrens } 2652842727c2SChris Kirby 26533b2aab18SMatthew Ahrens if (newval < ds->ds_phys->ds_referenced_bytes || 26543b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 26553b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2656be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 26573b2aab18SMatthew Ahrens } 26583b2aab18SMatthew Ahrens 26593b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2660842727c2SChris Kirby return (0); 2661842727c2SChris Kirby } 2662842727c2SChris Kirby 26633b2aab18SMatthew Ahrens static void 26643b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 2665842727c2SChris Kirby { 26663b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 26673b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 26683b2aab18SMatthew Ahrens dsl_dataset_t *ds; 26693b2aab18SMatthew Ahrens uint64_t newval; 2670842727c2SChris Kirby 26713b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 2672842727c2SChris Kirby 26733b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 26743b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 26753b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 26763b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 2677842727c2SChris Kirby 26783b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 26793b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 2680842727c2SChris Kirby 26813b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 26823b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 26833b2aab18SMatthew Ahrens ds->ds_quota = newval; 2684842727c2SChris Kirby } 26853b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2686842727c2SChris Kirby } 2687842727c2SChris Kirby 26883b2aab18SMatthew Ahrens int 26893b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 26903b2aab18SMatthew Ahrens uint64_t refquota) 2691842727c2SChris Kirby { 26923b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 2693842727c2SChris Kirby 26943b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 26953b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 26963b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 26973b2aab18SMatthew Ahrens 26983b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 26993b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0)); 2700842727c2SChris Kirby } 2701842727c2SChris Kirby 2702842727c2SChris Kirby static int 27033b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 2704842727c2SChris Kirby { 27053b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 27063b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 2707842727c2SChris Kirby dsl_dataset_t *ds; 2708842727c2SChris Kirby int error; 27093b2aab18SMatthew Ahrens uint64_t newval, unique; 2710d7747cbcSChris Kirby 27113b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 2712be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2713842727c2SChris Kirby 27143b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 27153b2aab18SMatthew Ahrens if (error != 0) 2716842727c2SChris Kirby return (error); 2717842727c2SChris Kirby 27183b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 27193b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2720be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2721842727c2SChris Kirby } 2722842727c2SChris Kirby 27233b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 27243b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 27253b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 27263b2aab18SMatthew Ahrens if (error != 0) { 27273b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2728842727c2SChris Kirby return (error); 2729842727c2SChris Kirby } 2730842727c2SChris Kirby 27313b2aab18SMatthew Ahrens /* 27323b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 27333b2aab18SMatthew Ahrens * space estimates may be inaccurate. 27343b2aab18SMatthew Ahrens */ 27353b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 27363b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 27373b2aab18SMatthew Ahrens return (0); 2738842727c2SChris Kirby } 2739842727c2SChris Kirby 27403b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 27413b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 27423b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 27433b2aab18SMatthew Ahrens unique = ds->ds_phys->ds_unique_bytes; 27443b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2745842727c2SChris Kirby 27463b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 27473b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 27483b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 2749842727c2SChris Kirby 27503b2aab18SMatthew Ahrens if (delta > 27513b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 27523b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 27533b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2754be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 27553b2aab18SMatthew Ahrens } 2756842727c2SChris Kirby } 2757842727c2SChris Kirby 27583b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 27593b2aab18SMatthew Ahrens return (0); 2760842727c2SChris Kirby } 2761842727c2SChris Kirby 27623b2aab18SMatthew Ahrens void 27633b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 27643b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 2765ca45db41SChris Kirby { 27663b2aab18SMatthew Ahrens uint64_t newval; 27673b2aab18SMatthew Ahrens uint64_t unique; 27683b2aab18SMatthew Ahrens int64_t delta; 2769ca45db41SChris Kirby 27703b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 27713b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 2772ca45db41SChris Kirby 27733b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 27743b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 2775a7f53a56SChris Kirby 27763b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 27773b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 27783b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 27793b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 27803b2aab18SMatthew Ahrens unique = ds->ds_phys->ds_unique_bytes; 27813b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 27823b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 27833b2aab18SMatthew Ahrens ds->ds_reserved = newval; 27843b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2785a7f53a56SChris Kirby 27863b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 27873b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 2788ca45db41SChris Kirby } 2789ca45db41SChris Kirby 27903b2aab18SMatthew Ahrens static void 27913b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 2792842727c2SChris Kirby { 27933b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 27943b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 2795842727c2SChris Kirby dsl_dataset_t *ds; 2796842727c2SChris Kirby 27973b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 27983b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 27993b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 2800842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 2801842727c2SChris Kirby } 2802503ad85cSMatthew Ahrens 2803503ad85cSMatthew Ahrens int 28043b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 28053b2aab18SMatthew Ahrens uint64_t refreservation) 2806503ad85cSMatthew Ahrens { 28073b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 2808503ad85cSMatthew Ahrens 28093b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 28103b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 28113b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 28123b2aab18SMatthew Ahrens 28133b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 28143b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 0)); 2815503ad85cSMatthew Ahrens } 281619b94df9SMatthew Ahrens 281719b94df9SMatthew Ahrens /* 281819b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 281919b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 282019b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 282119b94df9SMatthew Ahrens * fail and return EINVAL. 282219b94df9SMatthew Ahrens * 282319b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 282419b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 282519b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 282619b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 282719b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 282819b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 282919b94df9SMatthew Ahrens * 283019b94df9SMatthew Ahrens * space freed [---------------------] 283119b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 283219b94df9SMatthew Ahrens * oldsnap new 283319b94df9SMatthew Ahrens */ 283419b94df9SMatthew Ahrens int 283519b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 283619b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 283719b94df9SMatthew Ahrens { 283819b94df9SMatthew Ahrens int err = 0; 283919b94df9SMatthew Ahrens uint64_t snapobj; 284019b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 284119b94df9SMatthew Ahrens 28423b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 28433b2aab18SMatthew Ahrens 284419b94df9SMatthew Ahrens *usedp = 0; 2845ad135b5dSChristopher Siden *usedp += new->ds_phys->ds_referenced_bytes; 2846ad135b5dSChristopher Siden *usedp -= oldsnap->ds_phys->ds_referenced_bytes; 284719b94df9SMatthew Ahrens 284819b94df9SMatthew Ahrens *compp = 0; 284919b94df9SMatthew Ahrens *compp += new->ds_phys->ds_compressed_bytes; 285019b94df9SMatthew Ahrens *compp -= oldsnap->ds_phys->ds_compressed_bytes; 285119b94df9SMatthew Ahrens 285219b94df9SMatthew Ahrens *uncompp = 0; 285319b94df9SMatthew Ahrens *uncompp += new->ds_phys->ds_uncompressed_bytes; 285419b94df9SMatthew Ahrens *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes; 285519b94df9SMatthew Ahrens 285619b94df9SMatthew Ahrens snapobj = new->ds_object; 285719b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 285819b94df9SMatthew Ahrens dsl_dataset_t *snap; 285919b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 286019b94df9SMatthew Ahrens 2861ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 2862ad135b5dSChristopher Siden snap = new; 2863ad135b5dSChristopher Siden } else { 2864ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 2865ad135b5dSChristopher Siden if (err != 0) 2866ad135b5dSChristopher Siden break; 2867ad135b5dSChristopher Siden } 286819b94df9SMatthew Ahrens 286919b94df9SMatthew Ahrens if (snap->ds_phys->ds_prev_snap_txg == 287019b94df9SMatthew Ahrens oldsnap->ds_phys->ds_creation_txg) { 287119b94df9SMatthew Ahrens /* 287219b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 287319b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 287419b94df9SMatthew Ahrens * which is more efficient (especially for old-format 287519b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 287619b94df9SMatthew Ahrens * doesn't have enough information to make this 287719b94df9SMatthew Ahrens * optimization itself. 287819b94df9SMatthew Ahrens */ 287919b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 288019b94df9SMatthew Ahrens &used, &comp, &uncomp); 288119b94df9SMatthew Ahrens } else { 288219b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 288319b94df9SMatthew Ahrens 0, oldsnap->ds_phys->ds_creation_txg, 288419b94df9SMatthew Ahrens &used, &comp, &uncomp); 288519b94df9SMatthew Ahrens } 288619b94df9SMatthew Ahrens *usedp += used; 288719b94df9SMatthew Ahrens *compp += comp; 288819b94df9SMatthew Ahrens *uncompp += uncomp; 288919b94df9SMatthew Ahrens 289019b94df9SMatthew Ahrens /* 289119b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 289219b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 289319b94df9SMatthew Ahrens * was not a snapshot of/before new. 289419b94df9SMatthew Ahrens */ 289519b94df9SMatthew Ahrens snapobj = snap->ds_phys->ds_prev_snap_obj; 2896ad135b5dSChristopher Siden if (snap != new) 2897ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 289819b94df9SMatthew Ahrens if (snapobj == 0) { 2899be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 290019b94df9SMatthew Ahrens break; 290119b94df9SMatthew Ahrens } 290219b94df9SMatthew Ahrens 290319b94df9SMatthew Ahrens } 290419b94df9SMatthew Ahrens return (err); 290519b94df9SMatthew Ahrens } 290619b94df9SMatthew Ahrens 290719b94df9SMatthew Ahrens /* 290819b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 290919b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 291019b94df9SMatthew Ahrens * 291119b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 291219b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 291319b94df9SMatthew Ahrens * firstsnap lastsnap 291419b94df9SMatthew Ahrens * 291519b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 291619b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 291719b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 291819b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 291919b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 292019b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 292119b94df9SMatthew Ahrens */ 292219b94df9SMatthew Ahrens int 292319b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 292419b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 292519b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 292619b94df9SMatthew Ahrens { 292719b94df9SMatthew Ahrens int err = 0; 292819b94df9SMatthew Ahrens uint64_t snapobj; 292919b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 293019b94df9SMatthew Ahrens 293119b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(firstsnap)); 293219b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(lastsnap)); 293319b94df9SMatthew Ahrens 293419b94df9SMatthew Ahrens /* 293519b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 293619b94df9SMatthew Ahrens * is before lastsnap. 293719b94df9SMatthew Ahrens */ 293819b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 293919b94df9SMatthew Ahrens firstsnap->ds_phys->ds_creation_txg > 294019b94df9SMatthew Ahrens lastsnap->ds_phys->ds_creation_txg) 2941be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 294219b94df9SMatthew Ahrens 294319b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 294419b94df9SMatthew Ahrens 294519b94df9SMatthew Ahrens snapobj = lastsnap->ds_phys->ds_next_snap_obj; 294619b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 294719b94df9SMatthew Ahrens dsl_dataset_t *ds; 294819b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 294919b94df9SMatthew Ahrens 295019b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 295119b94df9SMatthew Ahrens if (err != 0) 295219b94df9SMatthew Ahrens break; 295319b94df9SMatthew Ahrens 295419b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 295519b94df9SMatthew Ahrens firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX, 295619b94df9SMatthew Ahrens &used, &comp, &uncomp); 295719b94df9SMatthew Ahrens *usedp += used; 295819b94df9SMatthew Ahrens *compp += comp; 295919b94df9SMatthew Ahrens *uncompp += uncomp; 296019b94df9SMatthew Ahrens 296119b94df9SMatthew Ahrens snapobj = ds->ds_phys->ds_prev_snap_obj; 296219b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 296319b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 296419b94df9SMatthew Ahrens } 296519b94df9SMatthew Ahrens return (err); 296619b94df9SMatthew Ahrens } 29673b2aab18SMatthew Ahrens 29683b2aab18SMatthew Ahrens /* 29693b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 29703b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 29713b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 29723b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 29733b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 29743b2aab18SMatthew Ahrens */ 29753b2aab18SMatthew Ahrens boolean_t 29763b2aab18SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier) 29773b2aab18SMatthew Ahrens { 29783b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 29793b2aab18SMatthew Ahrens int error; 29803b2aab18SMatthew Ahrens boolean_t ret; 29813b2aab18SMatthew Ahrens 29823b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 29833b2aab18SMatthew Ahrens 29843b2aab18SMatthew Ahrens if (earlier->ds_phys->ds_creation_txg >= 29853b2aab18SMatthew Ahrens later->ds_phys->ds_creation_txg) 29863b2aab18SMatthew Ahrens return (B_FALSE); 29873b2aab18SMatthew Ahrens 29883b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 29893b2aab18SMatthew Ahrens return (B_TRUE); 29903b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 29913b2aab18SMatthew Ahrens return (B_FALSE); 29923b2aab18SMatthew Ahrens 29933b2aab18SMatthew Ahrens if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object) 29943b2aab18SMatthew Ahrens return (B_TRUE); 29953b2aab18SMatthew Ahrens dsl_dataset_t *origin; 29963b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 29973b2aab18SMatthew Ahrens later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin); 29983b2aab18SMatthew Ahrens if (error != 0) 29993b2aab18SMatthew Ahrens return (B_FALSE); 30003b2aab18SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier); 30013b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 30023b2aab18SMatthew Ahrens return (ret); 30033b2aab18SMatthew Ahrens } 3004