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> 50*78f17100SMatthew Ahrens #include <sys/dsl_bookmark.h> 51e1930233Sbonwick 52cde58dbcSMatthew Ahrens #define SWITCH64(x, y) \ 53cde58dbcSMatthew Ahrens { \ 54cde58dbcSMatthew Ahrens uint64_t __tmp = (x); \ 55cde58dbcSMatthew Ahrens (x) = (y); \ 56cde58dbcSMatthew Ahrens (y) = __tmp; \ 57cde58dbcSMatthew Ahrens } 58cde58dbcSMatthew Ahrens 5955434c77Sek #define DS_REF_MAX (1ULL << 62) 60fa9e4066Sahrens 61fa9e4066Sahrens #define DSL_DEADLIST_BLOCKSIZE SPA_MAXBLOCKSIZE 62fa9e4066Sahrens 63a9799022Sck /* 64a9799022Sck * Figure out how much of this delta should be propogated to the dsl_dir 65a9799022Sck * layer. If there's a refreservation, that space has already been 66a9799022Sck * partially accounted for in our ancestors. 67a9799022Sck */ 68a9799022Sck static int64_t 69a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta) 70a9799022Sck { 71a9799022Sck uint64_t old_bytes, new_bytes; 72a9799022Sck 73a9799022Sck if (ds->ds_reserved == 0) 74a9799022Sck return (delta); 75a9799022Sck 76a9799022Sck old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 77a9799022Sck new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 78a9799022Sck 79a9799022Sck ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 80a9799022Sck return (new_bytes - old_bytes); 81a9799022Sck } 82fa9e4066Sahrens 83fa9e4066Sahrens void 84b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 85fa9e4066Sahrens { 86b24ab676SJeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 87fa9e4066Sahrens int compressed = BP_GET_PSIZE(bp); 88fa9e4066Sahrens int uncompressed = BP_GET_UCSIZE(bp); 89a9799022Sck int64_t delta; 90fa9e4066Sahrens 913f9d6ad7SLin Ling dprintf_bp(bp, "ds=%p", ds); 92fa9e4066Sahrens 93fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 94fa9e4066Sahrens /* It could have been compressed away to nothing */ 95fa9e4066Sahrens if (BP_IS_HOLE(bp)) 96fa9e4066Sahrens return; 97fa9e4066Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 98ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 99fa9e4066Sahrens if (ds == NULL) { 100ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 101ce636f8bSMatthew Ahrens used, compressed, uncompressed); 102fa9e4066Sahrens return; 103fa9e4066Sahrens } 1043f9d6ad7SLin Ling 105b62969f8SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 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); 117fa9e4066Sahrens } 118fa9e4066Sahrens 119cdb0ab79Smaybee int 120b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 121b24ab676SJeff Bonwick boolean_t async) 122fa9e4066Sahrens { 12343466aaeSMax Grossman int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 12443466aaeSMax Grossman int compressed = BP_GET_PSIZE(bp); 12543466aaeSMax Grossman int uncompressed = BP_GET_UCSIZE(bp); 12643466aaeSMax Grossman 127fa9e4066Sahrens if (BP_IS_HOLE(bp)) 128cdb0ab79Smaybee return (0); 129fa9e4066Sahrens 130b24ab676SJeff Bonwick ASSERT(dmu_tx_is_syncing(tx)); 131b24ab676SJeff Bonwick ASSERT(bp->blk_birth <= tx->tx_txg); 132b24ab676SJeff Bonwick 133fa9e4066Sahrens if (ds == NULL) { 134b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 135ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 136ce636f8bSMatthew Ahrens -used, -compressed, -uncompressed); 137cdb0ab79Smaybee return (used); 138fa9e4066Sahrens } 139fa9e4066Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 140fa9e4066Sahrens 14174e7dc98SMatthew Ahrens ASSERT(!dsl_dataset_is_snapshot(ds)); 142fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 143fa9e4066Sahrens 144fa9e4066Sahrens if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) { 145a9799022Sck int64_t delta; 146c717a561Smaybee 1473f9d6ad7SLin Ling dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 148b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 149fa9e4066Sahrens 150fa9e4066Sahrens mutex_enter(&ds->ds_lock); 151a9799022Sck ASSERT(ds->ds_phys->ds_unique_bytes >= used || 152a9799022Sck !DS_UNIQUE_IS_ACCURATE(ds)); 153a9799022Sck delta = parent_delta(ds, -used); 154fa9e4066Sahrens ds->ds_phys->ds_unique_bytes -= used; 155fa9e4066Sahrens mutex_exit(&ds->ds_lock); 15674e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 157a9799022Sck delta, -compressed, -uncompressed, tx); 15874e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, -used - delta, 15974e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 160fa9e4066Sahrens } else { 161fa9e4066Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 162b24ab676SJeff Bonwick if (async) { 163b24ab676SJeff Bonwick /* 164b24ab676SJeff Bonwick * We are here as part of zio's write done callback, 165b24ab676SJeff Bonwick * which means we're a zio interrupt thread. We can't 166cde58dbcSMatthew Ahrens * call dsl_deadlist_insert() now because it may block 167b24ab676SJeff Bonwick * waiting for I/O. Instead, put bp on the deferred 168b24ab676SJeff Bonwick * queue and let dsl_pool_sync() finish the job. 169b24ab676SJeff Bonwick */ 170cde58dbcSMatthew Ahrens bplist_append(&ds->ds_pending_deadlist, bp); 171b24ab676SJeff Bonwick } else { 172cde58dbcSMatthew Ahrens dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 173b24ab676SJeff Bonwick } 174a4611edeSahrens ASSERT3U(ds->ds_prev->ds_object, ==, 175a4611edeSahrens ds->ds_phys->ds_prev_snap_obj); 176a4611edeSahrens ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0); 177fa9e4066Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 178a4611edeSahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == 179a4611edeSahrens ds->ds_object && bp->blk_birth > 180a4611edeSahrens ds->ds_prev->ds_phys->ds_prev_snap_txg) { 181a4611edeSahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 182a4611edeSahrens mutex_enter(&ds->ds_prev->ds_lock); 183a4611edeSahrens ds->ds_prev->ds_phys->ds_unique_bytes += used; 184a4611edeSahrens mutex_exit(&ds->ds_prev->ds_lock); 185fa9e4066Sahrens } 1863f9d6ad7SLin Ling if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 18774e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used, 18874e7dc98SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 18974e7dc98SMatthew Ahrens } 190fa9e4066Sahrens } 191fa9e4066Sahrens mutex_enter(&ds->ds_lock); 192ad135b5dSChristopher Siden ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used); 193ad135b5dSChristopher Siden ds->ds_phys->ds_referenced_bytes -= used; 194fa9e4066Sahrens ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed); 195fa9e4066Sahrens ds->ds_phys->ds_compressed_bytes -= compressed; 196fa9e4066Sahrens ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed); 197fa9e4066Sahrens ds->ds_phys->ds_uncompressed_bytes -= uncompressed; 198fa9e4066Sahrens mutex_exit(&ds->ds_lock); 199cdb0ab79Smaybee 200cdb0ab79Smaybee return (used); 201fa9e4066Sahrens } 202fa9e4066Sahrens 203ea8dc4b6Seschrock uint64_t 204ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 205fa9e4066Sahrens { 206a2eea2e1Sahrens uint64_t trysnap = 0; 207a2eea2e1Sahrens 208fa9e4066Sahrens if (ds == NULL) 209ea8dc4b6Seschrock return (0); 210fa9e4066Sahrens /* 211fa9e4066Sahrens * The snapshot creation could fail, but that would cause an 212fa9e4066Sahrens * incorrect FALSE return, which would only result in an 213fa9e4066Sahrens * overestimation of the amount of space that an operation would 214fa9e4066Sahrens * consume, which is OK. 215fa9e4066Sahrens * 216fa9e4066Sahrens * There's also a small window where we could miss a pending 217fa9e4066Sahrens * snapshot, because we could set the sync task in the quiescing 218fa9e4066Sahrens * phase. So this should only be used as a guess. 219fa9e4066Sahrens */ 220a2eea2e1Sahrens if (ds->ds_trysnap_txg > 221a2eea2e1Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 222a2eea2e1Sahrens trysnap = ds->ds_trysnap_txg; 223a2eea2e1Sahrens return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap)); 224ea8dc4b6Seschrock } 225ea8dc4b6Seschrock 2263d692628SSanjeev Bagewadi boolean_t 227c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp, 228c7cd2421SGeorge Wilson uint64_t blk_birth) 229ea8dc4b6Seschrock { 23043466aaeSMax Grossman if (blk_birth <= dsl_dataset_prev_snap_txg(ds) || 23143466aaeSMax Grossman (bp != NULL && BP_IS_HOLE(bp))) 232c7cd2421SGeorge Wilson return (B_FALSE); 233c7cd2421SGeorge Wilson 234837b568bSGeorge Wilson ddt_prefetch(dsl_dataset_get_spa(ds), bp); 235c7cd2421SGeorge Wilson 236c7cd2421SGeorge Wilson return (B_TRUE); 237fa9e4066Sahrens } 238fa9e4066Sahrens 239fa9e4066Sahrens /* ARGSUSED */ 240fa9e4066Sahrens static void 241fa9e4066Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 242fa9e4066Sahrens { 243fa9e4066Sahrens dsl_dataset_t *ds = dsv; 244fa9e4066Sahrens 2453b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == NULL); 246fa9e4066Sahrens 24791ebeef5Sahrens unique_remove(ds->ds_fsid_guid); 248fa9e4066Sahrens 249503ad85cSMatthew Ahrens if (ds->ds_objset != NULL) 250503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 251fa9e4066Sahrens 252fa9e4066Sahrens if (ds->ds_prev) { 2533b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 254fa9e4066Sahrens ds->ds_prev = NULL; 255fa9e4066Sahrens } 256fa9e4066Sahrens 257cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 2583b2aab18SMatthew Ahrens if (ds->ds_phys->ds_deadlist_obj != 0) 259cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 260745cd3c5Smaybee if (ds->ds_dir) 2613b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 262fa9e4066Sahrens 26391ebeef5Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 264fa9e4066Sahrens 2655ad82045Snd mutex_destroy(&ds->ds_lock); 26691ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 2673b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 2685ad82045Snd 269fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 270fa9e4066Sahrens } 271fa9e4066Sahrens 2723b2aab18SMatthew Ahrens int 273fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 274fa9e4066Sahrens { 275fa9e4066Sahrens dsl_dataset_phys_t *headphys; 276fa9e4066Sahrens int err; 277fa9e4066Sahrens dmu_buf_t *headdbuf; 278fa9e4066Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 279fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 280fa9e4066Sahrens 281fa9e4066Sahrens if (ds->ds_snapname[0]) 282ea8dc4b6Seschrock return (0); 283fa9e4066Sahrens if (ds->ds_phys->ds_next_snap_obj == 0) 284ea8dc4b6Seschrock return (0); 285fa9e4066Sahrens 286ea8dc4b6Seschrock err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj, 287ea8dc4b6Seschrock FTAG, &headdbuf); 2883b2aab18SMatthew Ahrens if (err != 0) 289ea8dc4b6Seschrock return (err); 290fa9e4066Sahrens headphys = headdbuf->db_data; 291fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 292e7437265Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 293ea8dc4b6Seschrock dmu_buf_rele(headdbuf, FTAG); 294ea8dc4b6Seschrock return (err); 295fa9e4066Sahrens } 296fa9e4066Sahrens 2973b2aab18SMatthew Ahrens int 298745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 299ab04eb8eStimh { 300745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 301745cd3c5Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 302ab04eb8eStimh matchtype_t mt; 303ab04eb8eStimh int err; 304ab04eb8eStimh 305745cd3c5Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 306ab04eb8eStimh mt = MT_FIRST; 307ab04eb8eStimh else 308ab04eb8eStimh mt = MT_EXACT; 309ab04eb8eStimh 310745cd3c5Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 311ab04eb8eStimh value, mt, NULL, 0, NULL); 312ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 313745cd3c5Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 314ab04eb8eStimh return (err); 315ab04eb8eStimh } 316ab04eb8eStimh 3173b2aab18SMatthew Ahrens int 3183b2aab18SMatthew Ahrens dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx) 319ab04eb8eStimh { 320745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 321745cd3c5Smaybee uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj; 322ab04eb8eStimh matchtype_t mt; 323ab04eb8eStimh int err; 324ab04eb8eStimh 32571eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 32671eb0538SChris Kirby 327745cd3c5Smaybee if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 328ab04eb8eStimh mt = MT_FIRST; 329ab04eb8eStimh else 330ab04eb8eStimh mt = MT_EXACT; 331ab04eb8eStimh 332745cd3c5Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 333ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 334745cd3c5Smaybee err = zap_remove(mos, snapobj, name, tx); 335ab04eb8eStimh return (err); 336ab04eb8eStimh } 337ab04eb8eStimh 3383b2aab18SMatthew Ahrens int 3393b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 340745cd3c5Smaybee dsl_dataset_t **dsp) 341fa9e4066Sahrens { 342fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 343fa9e4066Sahrens dmu_buf_t *dbuf; 344fa9e4066Sahrens dsl_dataset_t *ds; 345ea8dc4b6Seschrock int err; 346a7f53a56SChris Kirby dmu_object_info_t doi; 347fa9e4066Sahrens 3483b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 349fa9e4066Sahrens 350ea8dc4b6Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3513b2aab18SMatthew Ahrens if (err != 0) 352ea8dc4b6Seschrock return (err); 353a7f53a56SChris Kirby 354a7f53a56SChris Kirby /* Make sure dsobj has the correct object type. */ 355a7f53a56SChris Kirby dmu_object_info_from_db(dbuf, &doi); 3562acef22dSMatthew Ahrens if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 357b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 358be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 359b287be1bSWill Andrews } 360a7f53a56SChris Kirby 361fa9e4066Sahrens ds = dmu_buf_get_user(dbuf); 362fa9e4066Sahrens if (ds == NULL) { 363d5285caeSGeorge Wilson dsl_dataset_t *winner = NULL; 364fa9e4066Sahrens 365fa9e4066Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 366fa9e4066Sahrens ds->ds_dbuf = dbuf; 367fa9e4066Sahrens ds->ds_object = dsobj; 368fa9e4066Sahrens ds->ds_phys = dbuf->db_data; 369fa9e4066Sahrens 3705ad82045Snd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 37191ebeef5Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3724e3c9f44SBill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 3733b2aab18SMatthew Ahrens refcount_create(&ds->ds_longholds); 3745ad82045Snd 375cde58dbcSMatthew Ahrens bplist_create(&ds->ds_pending_deadlist); 376cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, 377fa9e4066Sahrens mos, ds->ds_phys->ds_deadlist_obj); 378cde58dbcSMatthew Ahrens 3794e3c9f44SBill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 3804e3c9f44SBill Pijewski offsetof(dmu_sendarg_t, dsa_link)); 3814e3c9f44SBill Pijewski 382ea8dc4b6Seschrock if (err == 0) { 3833b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, 384ea8dc4b6Seschrock ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir); 385ea8dc4b6Seschrock } 3863b2aab18SMatthew Ahrens if (err != 0) { 3875ad82045Snd mutex_destroy(&ds->ds_lock); 38891ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 3893b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 390cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 391cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 392ea8dc4b6Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 393ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 394ea8dc4b6Seschrock return (err); 395ea8dc4b6Seschrock } 396fa9e4066Sahrens 39774e7dc98SMatthew Ahrens if (!dsl_dataset_is_snapshot(ds)) { 398fa9e4066Sahrens ds->ds_snapname[0] = '\0'; 3993b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 4003b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 401745cd3c5Smaybee ds->ds_phys->ds_prev_snap_obj, 402745cd3c5Smaybee ds, &ds->ds_prev); 403fa9e4066Sahrens } 404*78f17100SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 405*78f17100SMatthew Ahrens int zaperr = zap_lookup(mos, ds->ds_object, 406*78f17100SMatthew Ahrens DS_FIELD_BOOKMARK_NAMES, 407*78f17100SMatthew Ahrens sizeof (ds->ds_bookmarks), 1, 408*78f17100SMatthew Ahrens &ds->ds_bookmarks); 409*78f17100SMatthew Ahrens if (zaperr != ENOENT) 410*78f17100SMatthew Ahrens VERIFY0(zaperr); 411*78f17100SMatthew Ahrens } 412842727c2SChris Kirby } else { 413842727c2SChris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 414842727c2SChris Kirby err = dsl_dataset_get_snapname(ds); 415842727c2SChris Kirby if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) { 416842727c2SChris Kirby err = zap_count( 417842727c2SChris Kirby ds->ds_dir->dd_pool->dp_meta_objset, 418842727c2SChris Kirby ds->ds_phys->ds_userrefs_obj, 419842727c2SChris Kirby &ds->ds_userrefs); 420842727c2SChris Kirby } 421fa9e4066Sahrens } 422fa9e4066Sahrens 42374e7dc98SMatthew Ahrens if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 4243b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4253b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 4263b2aab18SMatthew Ahrens &ds->ds_reserved); 427cb625fb5Sck if (err == 0) { 4283b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4293b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 4303b2aab18SMatthew Ahrens &ds->ds_quota); 431cb625fb5Sck } 432cb625fb5Sck } else { 433cb625fb5Sck ds->ds_reserved = ds->ds_quota = 0; 434cb625fb5Sck } 435cb625fb5Sck 436d5285caeSGeorge Wilson if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds, 437d5285caeSGeorge Wilson &ds->ds_phys, dsl_dataset_evict)) != NULL) { 438cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 439cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 440745cd3c5Smaybee if (ds->ds_prev) 4413b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 4423b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 4435ad82045Snd mutex_destroy(&ds->ds_lock); 44491ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 4453b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 446fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4473b2aab18SMatthew Ahrens if (err != 0) { 448ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 449ea8dc4b6Seschrock return (err); 450ea8dc4b6Seschrock } 451fa9e4066Sahrens ds = winner; 452fa9e4066Sahrens } else { 45391ebeef5Sahrens ds->ds_fsid_guid = 454fa9e4066Sahrens unique_insert(ds->ds_phys->ds_fsid_guid); 455fa9e4066Sahrens } 456fa9e4066Sahrens } 457fa9e4066Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 458fa9e4066Sahrens ASSERT3P(ds->ds_phys, ==, dbuf->db_data); 459088f3894Sahrens ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 || 460afc6333aSahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 46184db2a68Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 462ea8dc4b6Seschrock *dsp = ds; 463ea8dc4b6Seschrock return (0); 464fa9e4066Sahrens } 465fa9e4066Sahrens 466745cd3c5Smaybee int 4673b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name, 468503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 469fa9e4066Sahrens { 470fa9e4066Sahrens dsl_dir_t *dd; 471745cd3c5Smaybee const char *snapname; 472fa9e4066Sahrens uint64_t obj; 473fa9e4066Sahrens int err = 0; 474fa9e4066Sahrens 4753b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 4763b2aab18SMatthew Ahrens if (err != 0) 477ea8dc4b6Seschrock return (err); 478fa9e4066Sahrens 4793b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 480fa9e4066Sahrens obj = dd->dd_phys->dd_head_dataset_obj; 4813b2aab18SMatthew Ahrens if (obj != 0) 4823b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, dsp); 483745cd3c5Smaybee else 484be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 485fa9e4066Sahrens 486745cd3c5Smaybee /* we may be looking for a snapshot */ 487745cd3c5Smaybee if (err == 0 && snapname != NULL) { 4883b2aab18SMatthew Ahrens dsl_dataset_t *ds; 489fa9e4066Sahrens 490745cd3c5Smaybee if (*snapname++ != '@') { 491745cd3c5Smaybee dsl_dataset_rele(*dsp, tag); 4923b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 493be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 494fa9e4066Sahrens } 495fa9e4066Sahrens 496745cd3c5Smaybee dprintf("looking for snapshot '%s'\n", snapname); 497745cd3c5Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 498745cd3c5Smaybee if (err == 0) 4993b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 500745cd3c5Smaybee dsl_dataset_rele(*dsp, tag); 501745cd3c5Smaybee 5023b2aab18SMatthew Ahrens if (err == 0) { 503745cd3c5Smaybee mutex_enter(&ds->ds_lock); 504745cd3c5Smaybee if (ds->ds_snapname[0] == 0) 505745cd3c5Smaybee (void) strlcpy(ds->ds_snapname, snapname, 506745cd3c5Smaybee sizeof (ds->ds_snapname)); 507745cd3c5Smaybee mutex_exit(&ds->ds_lock); 5083b2aab18SMatthew Ahrens *dsp = ds; 509fa9e4066Sahrens } 510fa9e4066Sahrens } 5113b2aab18SMatthew Ahrens 5123b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 513fa9e4066Sahrens return (err); 514fa9e4066Sahrens } 515fa9e4066Sahrens 516fa9e4066Sahrens int 5173b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 5183b2aab18SMatthew Ahrens void *tag, dsl_dataset_t **dsp) 5193b2aab18SMatthew Ahrens { 5203b2aab18SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5213b2aab18SMatthew Ahrens if (err != 0) 5223b2aab18SMatthew Ahrens return (err); 5233b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 5243b2aab18SMatthew Ahrens dsl_dataset_rele(*dsp, tag); 5253b2aab18SMatthew Ahrens *dsp = NULL; 526be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 5273b2aab18SMatthew Ahrens } 5283b2aab18SMatthew Ahrens return (0); 5293b2aab18SMatthew Ahrens } 5303b2aab18SMatthew Ahrens 5313b2aab18SMatthew Ahrens int 5323b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name, 533503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 534fa9e4066Sahrens { 5353b2aab18SMatthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp); 5363b2aab18SMatthew Ahrens if (err != 0) 537745cd3c5Smaybee return (err); 5383b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 539503ad85cSMatthew Ahrens dsl_dataset_rele(*dsp, tag); 540be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 541745cd3c5Smaybee } 542745cd3c5Smaybee return (0); 543fa9e4066Sahrens } 544fa9e4066Sahrens 5453b2aab18SMatthew Ahrens /* 5463b2aab18SMatthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long 5473b2aab18SMatthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold 5483b2aab18SMatthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()). 5493b2aab18SMatthew Ahrens * 5503b2aab18SMatthew Ahrens * The dataset and pool must be held when this function is called. After it 5513b2aab18SMatthew Ahrens * is called, the pool hold may be released while the dataset is still held 5523b2aab18SMatthew Ahrens * and accessed. 5533b2aab18SMatthew Ahrens */ 5543b2aab18SMatthew Ahrens void 5553b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 5563b2aab18SMatthew Ahrens { 5573b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 5583b2aab18SMatthew Ahrens (void) refcount_add(&ds->ds_longholds, tag); 5593b2aab18SMatthew Ahrens } 5603b2aab18SMatthew Ahrens 5613b2aab18SMatthew Ahrens void 5623b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 5633b2aab18SMatthew Ahrens { 5643b2aab18SMatthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag); 5653b2aab18SMatthew Ahrens } 5663b2aab18SMatthew Ahrens 5673b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */ 5683b2aab18SMatthew Ahrens boolean_t 5693b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds) 5703b2aab18SMatthew Ahrens { 5713b2aab18SMatthew Ahrens return (!refcount_is_zero(&ds->ds_longholds)); 5723b2aab18SMatthew Ahrens } 5733b2aab18SMatthew Ahrens 574fa9e4066Sahrens void 575fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 576fa9e4066Sahrens { 577fa9e4066Sahrens if (ds == NULL) { 578fa9e4066Sahrens (void) strcpy(name, "mos"); 579fa9e4066Sahrens } else { 580fa9e4066Sahrens dsl_dir_name(ds->ds_dir, name); 5813b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 582fa9e4066Sahrens if (ds->ds_snapname[0]) { 583fa9e4066Sahrens (void) strcat(name, "@"); 584745cd3c5Smaybee /* 585745cd3c5Smaybee * We use a "recursive" mutex so that we 586745cd3c5Smaybee * can call dprintf_ds() with ds_lock held. 587745cd3c5Smaybee */ 588fa9e4066Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 589fa9e4066Sahrens mutex_enter(&ds->ds_lock); 590fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 591fa9e4066Sahrens mutex_exit(&ds->ds_lock); 592fa9e4066Sahrens } else { 593fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 594fa9e4066Sahrens } 595fa9e4066Sahrens } 596fa9e4066Sahrens } 597fa9e4066Sahrens } 598fa9e4066Sahrens 5993cb34c60Sahrens void 600745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6013cb34c60Sahrens { 6023b2aab18SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag); 603745cd3c5Smaybee } 604745cd3c5Smaybee 605745cd3c5Smaybee void 606503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 607745cd3c5Smaybee { 6083b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL); 609745cd3c5Smaybee 6103cb34c60Sahrens mutex_enter(&ds->ds_lock); 611745cd3c5Smaybee ds->ds_owner = NULL; 6123cb34c60Sahrens mutex_exit(&ds->ds_lock); 6133b2aab18SMatthew Ahrens dsl_dataset_long_rele(ds, tag); 6143b2aab18SMatthew Ahrens if (ds->ds_dbuf != NULL) 6153b2aab18SMatthew Ahrens dsl_dataset_rele(ds, tag); 616745cd3c5Smaybee else 617cde58dbcSMatthew Ahrens dsl_dataset_evict(NULL, ds); 6183cb34c60Sahrens } 6193cb34c60Sahrens 6203cb34c60Sahrens boolean_t 6213b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 6223cb34c60Sahrens { 623745cd3c5Smaybee boolean_t gotit = FALSE; 624745cd3c5Smaybee 6253cb34c60Sahrens mutex_enter(&ds->ds_lock); 6263b2aab18SMatthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 627503ad85cSMatthew Ahrens ds->ds_owner = tag; 6283b2aab18SMatthew Ahrens dsl_dataset_long_hold(ds, tag); 629745cd3c5Smaybee gotit = TRUE; 6303cb34c60Sahrens } 6313cb34c60Sahrens mutex_exit(&ds->ds_lock); 632745cd3c5Smaybee return (gotit); 633745cd3c5Smaybee } 634745cd3c5Smaybee 6351d452cf5Sahrens uint64_t 636088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 637ab04eb8eStimh uint64_t flags, dmu_tx_t *tx) 638fa9e4066Sahrens { 6393cb34c60Sahrens dsl_pool_t *dp = dd->dd_pool; 640fa9e4066Sahrens dmu_buf_t *dbuf; 641fa9e4066Sahrens dsl_dataset_phys_t *dsphys; 6423cb34c60Sahrens uint64_t dsobj; 643fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 644fa9e4066Sahrens 645088f3894Sahrens if (origin == NULL) 646088f3894Sahrens origin = dp->dp_origin_snap; 647088f3894Sahrens 6483cb34c60Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 6493cb34c60Sahrens ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0); 650fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 6513cb34c60Sahrens ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); 652fa9e4066Sahrens 6531649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 6541649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 6553b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 656fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 657fa9e4066Sahrens dsphys = dbuf->db_data; 658745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 659fa9e4066Sahrens dsphys->ds_dir_obj = dd->dd_object; 660ab04eb8eStimh dsphys->ds_flags = flags; 661fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 662fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 663fa9e4066Sahrens sizeof (dsphys->ds_guid)); 664fa9e4066Sahrens dsphys->ds_snapnames_zapobj = 665ab04eb8eStimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 666ab04eb8eStimh DMU_OT_NONE, 0, tx); 667fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 668088f3894Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 669a9799022Sck 670cde58dbcSMatthew Ahrens if (origin == NULL) { 671cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 672cde58dbcSMatthew Ahrens } else { 6733b2aab18SMatthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */ 674cde58dbcSMatthew Ahrens 6753cb34c60Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 676fa9e4066Sahrens dsphys->ds_prev_snap_txg = 6773cb34c60Sahrens origin->ds_phys->ds_creation_txg; 678ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = 679ad135b5dSChristopher Siden origin->ds_phys->ds_referenced_bytes; 680fa9e4066Sahrens dsphys->ds_compressed_bytes = 6813cb34c60Sahrens origin->ds_phys->ds_compressed_bytes; 682fa9e4066Sahrens dsphys->ds_uncompressed_bytes = 6833cb34c60Sahrens origin->ds_phys->ds_uncompressed_bytes; 6843cb34c60Sahrens dsphys->ds_bp = origin->ds_phys->ds_bp; 685579ae4d5Stimh dsphys->ds_flags |= origin->ds_phys->ds_flags; 686fa9e4066Sahrens 6873cb34c60Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 6883cb34c60Sahrens origin->ds_phys->ds_num_children++; 689fa9e4066Sahrens 6903b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 691cde58dbcSMatthew Ahrens origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds)); 692cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 693cde58dbcSMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 694cde58dbcSMatthew Ahrens dsl_dataset_rele(ohds, FTAG); 695cde58dbcSMatthew Ahrens 696088f3894Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 697088f3894Sahrens if (origin->ds_phys->ds_next_clones_obj == 0) { 698088f3894Sahrens origin->ds_phys->ds_next_clones_obj = 699088f3894Sahrens zap_create(mos, 700088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 701088f3894Sahrens } 7023b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 7033b2aab18SMatthew Ahrens origin->ds_phys->ds_next_clones_obj, dsobj, tx)); 704088f3894Sahrens } 705088f3894Sahrens 706fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 7073cb34c60Sahrens dd->dd_phys->dd_origin_obj = origin->ds_object; 708cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 709cde58dbcSMatthew Ahrens if (origin->ds_dir->dd_phys->dd_clones == 0) { 710cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 711cde58dbcSMatthew Ahrens origin->ds_dir->dd_phys->dd_clones = 712cde58dbcSMatthew Ahrens zap_create(mos, 713cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 714cde58dbcSMatthew Ahrens } 7153b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 716cde58dbcSMatthew Ahrens origin->ds_dir->dd_phys->dd_clones, dsobj, tx)); 717cde58dbcSMatthew Ahrens } 718fa9e4066Sahrens } 719ab04eb8eStimh 720ab04eb8eStimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 721ab04eb8eStimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 722ab04eb8eStimh 723ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 724fa9e4066Sahrens 725fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 726fa9e4066Sahrens dd->dd_phys->dd_head_dataset_obj = dsobj; 7273cb34c60Sahrens 7283cb34c60Sahrens return (dsobj); 7293cb34c60Sahrens } 7303cb34c60Sahrens 7313b2aab18SMatthew Ahrens static void 7323b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 7333b2aab18SMatthew Ahrens { 7343b2aab18SMatthew Ahrens objset_t *os; 7353b2aab18SMatthew Ahrens 7363b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 7373b2aab18SMatthew Ahrens bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 7383b2aab18SMatthew Ahrens dsl_dataset_dirty(ds, tx); 7393b2aab18SMatthew Ahrens } 7403b2aab18SMatthew Ahrens 7413cb34c60Sahrens uint64_t 742ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 743ab04eb8eStimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 7443cb34c60Sahrens { 7453cb34c60Sahrens dsl_pool_t *dp = pdd->dd_pool; 7463cb34c60Sahrens uint64_t dsobj, ddobj; 7473cb34c60Sahrens dsl_dir_t *dd; 7483cb34c60Sahrens 7493b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 7503cb34c60Sahrens ASSERT(lastname[0] != '@'); 7513cb34c60Sahrens 752088f3894Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 7533b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 7543cb34c60Sahrens 7553b2aab18SMatthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, 7563b2aab18SMatthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx); 7573cb34c60Sahrens 7583cb34c60Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 7593cb34c60Sahrens 7603b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 761fa9e4066Sahrens 762feaa74e4SMark Maybee /* 763feaa74e4SMark Maybee * If we are creating a clone, make sure we zero out any stale 764feaa74e4SMark Maybee * data from the origin snapshots zil header. 765feaa74e4SMark Maybee */ 7663b2aab18SMatthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 767feaa74e4SMark Maybee dsl_dataset_t *ds; 768feaa74e4SMark Maybee 7693b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 7703b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 771feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 772feaa74e4SMark Maybee } 773feaa74e4SMark Maybee 7741d452cf5Sahrens return (dsobj); 775fa9e4066Sahrens } 776fa9e4066Sahrens 7771d452cf5Sahrens /* 7783b2aab18SMatthew Ahrens * The unique space in the head dataset can be calculated by subtracting 7793b2aab18SMatthew Ahrens * the space used in the most recent snapshot, that is still being used 7803b2aab18SMatthew Ahrens * in this file system, from the space currently in use. To figure out 7813b2aab18SMatthew Ahrens * the space in the most recent snapshot still in use, we need to take 7823b2aab18SMatthew Ahrens * the total space used in the snapshot and subtract out the space that 7833b2aab18SMatthew Ahrens * has been freed up since the snapshot was taken. 7841d452cf5Sahrens */ 7853b2aab18SMatthew Ahrens void 7863b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 7871d452cf5Sahrens { 7883b2aab18SMatthew Ahrens uint64_t mrs_used; 7893b2aab18SMatthew Ahrens uint64_t dlused, dlcomp, dluncomp; 7901d452cf5Sahrens 7913b2aab18SMatthew Ahrens ASSERT(!dsl_dataset_is_snapshot(ds)); 7921d452cf5Sahrens 7933b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj != 0) 7943b2aab18SMatthew Ahrens mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes; 7953b2aab18SMatthew Ahrens else 7963b2aab18SMatthew Ahrens mrs_used = 0; 797842727c2SChris Kirby 7983b2aab18SMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 799fa9e4066Sahrens 8003b2aab18SMatthew Ahrens ASSERT3U(dlused, <=, mrs_used); 8013b2aab18SMatthew Ahrens ds->ds_phys->ds_unique_bytes = 8023b2aab18SMatthew Ahrens ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused); 80319b94df9SMatthew Ahrens 8043b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 8053b2aab18SMatthew Ahrens SPA_VERSION_UNIQUE_ACCURATE) 8063b2aab18SMatthew Ahrens ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 807fa9e4066Sahrens } 808fa9e4066Sahrens 8093b2aab18SMatthew Ahrens void 8103b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 8113b2aab18SMatthew Ahrens dmu_tx_t *tx) 812842727c2SChris Kirby { 8133b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 8143b2aab18SMatthew Ahrens uint64_t count; 8153b2aab18SMatthew Ahrens int err; 8163b2aab18SMatthew Ahrens 8173b2aab18SMatthew Ahrens ASSERT(ds->ds_phys->ds_num_children >= 2); 8183b2aab18SMatthew Ahrens err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx); 8193b2aab18SMatthew Ahrens /* 8203b2aab18SMatthew Ahrens * The err should not be ENOENT, but a bug in a previous version 8213b2aab18SMatthew Ahrens * of the code could cause upgrade_clones_cb() to not set 8223b2aab18SMatthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry. 8233b2aab18SMatthew Ahrens * If we knew that the pool was created after 8243b2aab18SMatthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 8253b2aab18SMatthew Ahrens * ENOENT. However, at least we can check that we don't have 8263b2aab18SMatthew Ahrens * too many entries in the next_clones_obj even after failing to 8273b2aab18SMatthew Ahrens * remove this one. 8283b2aab18SMatthew Ahrens */ 8293b2aab18SMatthew Ahrens if (err != ENOENT) 8303b2aab18SMatthew Ahrens VERIFY0(err); 8313b2aab18SMatthew Ahrens ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj, 8323b2aab18SMatthew Ahrens &count)); 8333b2aab18SMatthew Ahrens ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2); 8343b2aab18SMatthew Ahrens } 835842727c2SChris Kirby 836842727c2SChris Kirby 8373b2aab18SMatthew Ahrens blkptr_t * 8383b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds) 8393b2aab18SMatthew Ahrens { 8403b2aab18SMatthew Ahrens return (&ds->ds_phys->ds_bp); 841842727c2SChris Kirby } 842842727c2SChris Kirby 8433b2aab18SMatthew Ahrens void 8443b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 845842727c2SChris Kirby { 8463b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 8473b2aab18SMatthew Ahrens /* If it's the meta-objset, set dp_meta_rootbp */ 8483b2aab18SMatthew Ahrens if (ds == NULL) { 8493b2aab18SMatthew Ahrens tx->tx_pool->dp_meta_rootbp = *bp; 8503b2aab18SMatthew Ahrens } else { 8513b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 8523b2aab18SMatthew Ahrens ds->ds_phys->ds_bp = *bp; 853842727c2SChris Kirby } 8543b2aab18SMatthew Ahrens } 855842727c2SChris Kirby 8563b2aab18SMatthew Ahrens spa_t * 8573b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 8583b2aab18SMatthew Ahrens { 8593b2aab18SMatthew Ahrens return (ds->ds_dir->dd_pool->dp_spa); 860842727c2SChris Kirby } 861842727c2SChris Kirby 8623b2aab18SMatthew Ahrens void 8633b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 864fa9e4066Sahrens { 8653b2aab18SMatthew Ahrens dsl_pool_t *dp; 866842727c2SChris Kirby 8673b2aab18SMatthew Ahrens if (ds == NULL) /* this is the meta-objset */ 8683b2aab18SMatthew Ahrens return; 8691d452cf5Sahrens 8703b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 871fa9e4066Sahrens 8723b2aab18SMatthew Ahrens if (ds->ds_phys->ds_next_snap_obj != 0) 8733b2aab18SMatthew Ahrens panic("dirtying snapshot!"); 874fa9e4066Sahrens 8753b2aab18SMatthew Ahrens dp = ds->ds_dir->dd_pool; 876ce636f8bSMatthew Ahrens 8773b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 8783b2aab18SMatthew Ahrens /* up the hold count until we can be written out */ 8793b2aab18SMatthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 8803b2aab18SMatthew Ahrens } 8813b2aab18SMatthew Ahrens } 882fa9e4066Sahrens 8832e2c1355SMatthew Ahrens boolean_t 8842e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds) 8852e2c1355SMatthew Ahrens { 8862e2c1355SMatthew Ahrens for (int t = 0; t < TXG_SIZE; t++) { 8872e2c1355SMatthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 8882e2c1355SMatthew Ahrens ds, t)) 8892e2c1355SMatthew Ahrens return (B_TRUE); 8902e2c1355SMatthew Ahrens } 8912e2c1355SMatthew Ahrens return (B_FALSE); 8922e2c1355SMatthew Ahrens } 8932e2c1355SMatthew Ahrens 894fa9e4066Sahrens static int 8953b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 896fa9e4066Sahrens { 8973b2aab18SMatthew Ahrens uint64_t asize; 898fa9e4066Sahrens 8993b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 90088b7b0f2SMatthew Ahrens return (0); 901fa9e4066Sahrens 902e1930233Sbonwick /* 9033b2aab18SMatthew Ahrens * If there's an fs-only reservation, any blocks that might become 9043b2aab18SMatthew Ahrens * owned by the snapshot dataset must be accommodated by space 9053b2aab18SMatthew Ahrens * outside of the reservation. 906e1930233Sbonwick */ 9073b2aab18SMatthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 9083b2aab18SMatthew Ahrens asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 9093b2aab18SMatthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 910be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 911e1930233Sbonwick 9123cb34c60Sahrens /* 9133b2aab18SMatthew Ahrens * Propagate any reserved space for this snapshot to other 9143b2aab18SMatthew Ahrens * snapshot checks in this sync group. 9153cb34c60Sahrens */ 9163b2aab18SMatthew Ahrens if (asize > 0) 9173b2aab18SMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx); 9183cb34c60Sahrens 919e1930233Sbonwick return (0); 920e1930233Sbonwick } 921e1930233Sbonwick 9223b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg { 9233b2aab18SMatthew Ahrens nvlist_t *ddsa_snaps; 9243b2aab18SMatthew Ahrens nvlist_t *ddsa_props; 9253b2aab18SMatthew Ahrens nvlist_t *ddsa_errors; 9263b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t; 927842727c2SChris Kirby 9283cb34c60Sahrens int 9293b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 930ca48f36fSKeith M Wesolowski dmu_tx_t *tx, boolean_t recv) 9311d452cf5Sahrens { 9323b2aab18SMatthew Ahrens int error; 9333b2aab18SMatthew Ahrens uint64_t value; 934fa9e4066Sahrens 9353b2aab18SMatthew Ahrens ds->ds_trysnap_txg = tx->tx_txg; 936745cd3c5Smaybee 9373b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 938842727c2SChris Kirby return (0); 939fa9e4066Sahrens 940fa9e4066Sahrens /* 9413b2aab18SMatthew Ahrens * We don't allow multiple snapshots of the same txg. If there 9423b2aab18SMatthew Ahrens * is already one, try again. 943fa9e4066Sahrens */ 9443b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg) 945be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 946fa9e4066Sahrens 947fa9e4066Sahrens /* 9483b2aab18SMatthew Ahrens * Check for conflicting snapshot name. 949fa9e4066Sahrens */ 9503b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value); 9513b2aab18SMatthew Ahrens if (error == 0) 952be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 9533b2aab18SMatthew Ahrens if (error != ENOENT) 9543b2aab18SMatthew Ahrens return (error); 955842727c2SChris Kirby 956ca48f36fSKeith M Wesolowski /* 957ca48f36fSKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as 958ca48f36fSKeith M Wesolowski * those into which we are currently receiving. However, if we are 959ca48f36fSKeith M Wesolowski * creating this snapshot as part of a receive, this check will be 960ca48f36fSKeith M Wesolowski * executed atomically with respect to the completion of the receive 961ca48f36fSKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 962ca48f36fSKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in 963ca48f36fSKeith M Wesolowski * dmu_recv_end_sync(). 964ca48f36fSKeith M Wesolowski */ 965ca48f36fSKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds)) 966ca48f36fSKeith M Wesolowski return (SET_ERROR(EBUSY)); 967ca48f36fSKeith M Wesolowski 9683b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 9693b2aab18SMatthew Ahrens if (error != 0) 9703b2aab18SMatthew Ahrens return (error); 971842727c2SChris Kirby 9721d452cf5Sahrens return (0); 9731d452cf5Sahrens } 9741d452cf5Sahrens 9753b2aab18SMatthew Ahrens static int 9763b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 977745cd3c5Smaybee { 9783b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 9793b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 9803b2aab18SMatthew Ahrens nvpair_t *pair; 9813b2aab18SMatthew Ahrens int rv = 0; 9823b2aab18SMatthew Ahrens 9833b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 9843b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 9853b2aab18SMatthew Ahrens int error = 0; 9863b2aab18SMatthew Ahrens dsl_dataset_t *ds; 9873b2aab18SMatthew Ahrens char *name, *atp; 9883b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 9893b2aab18SMatthew Ahrens 9903b2aab18SMatthew Ahrens name = nvpair_name(pair); 9913b2aab18SMatthew Ahrens if (strlen(name) >= MAXNAMELEN) 992be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 9933b2aab18SMatthew Ahrens if (error == 0) { 9943b2aab18SMatthew Ahrens atp = strchr(name, '@'); 9953b2aab18SMatthew Ahrens if (atp == NULL) 996be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 9973b2aab18SMatthew Ahrens if (error == 0) 9983b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 9993b2aab18SMatthew Ahrens } 10003b2aab18SMatthew Ahrens if (error == 0) 10013b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 10023b2aab18SMatthew Ahrens if (error == 0) { 10033b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 1004ca48f36fSKeith M Wesolowski atp + 1, tx, B_FALSE); 10053b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 10063b2aab18SMatthew Ahrens } 1007745cd3c5Smaybee 10083b2aab18SMatthew Ahrens if (error != 0) { 10093b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 10103b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 10113b2aab18SMatthew Ahrens name, error); 10123b2aab18SMatthew Ahrens } 10133b2aab18SMatthew Ahrens rv = error; 10143b2aab18SMatthew Ahrens } 10153b2aab18SMatthew Ahrens } 10163b2aab18SMatthew Ahrens return (rv); 1017745cd3c5Smaybee } 1018745cd3c5Smaybee 10193b2aab18SMatthew Ahrens void 10203b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 10213b2aab18SMatthew Ahrens dmu_tx_t *tx) 1022745cd3c5Smaybee { 10233b2aab18SMatthew Ahrens static zil_header_t zero_zil; 1024745cd3c5Smaybee 10253b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 10263b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 10273b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 10283b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 10293b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 10303b2aab18SMatthew Ahrens objset_t *os; 1031745cd3c5Smaybee 10323b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1033c33e334fSMatthew Ahrens 1034c33e334fSMatthew Ahrens /* 10353b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 10363b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1037c33e334fSMatthew Ahrens */ 10383b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 10393b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 10403b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 10413b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1042c33e334fSMatthew Ahrens 1043cde58dbcSMatthew Ahrens 1044cde58dbcSMatthew Ahrens /* 10453b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1046088f3894Sahrens */ 1047088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1048088f3894Sahrens crtxg = 1; 1049088f3894Sahrens else 1050088f3894Sahrens crtxg = tx->tx_txg; 1051088f3894Sahrens 10521649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 10531649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 10543b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1055fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1056fa9e4066Sahrens dsphys = dbuf->db_data; 1057745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 10581d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1059fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1060fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1061fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1062fa9e4066Sahrens dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj; 1063fa9e4066Sahrens dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg; 1064fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1065fa9e4066Sahrens dsphys->ds_num_children = 1; 1066fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1067088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1068fa9e4066Sahrens dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj; 1069ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes; 1070fa9e4066Sahrens dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes; 1071fa9e4066Sahrens dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes; 107299653d4eSeschrock dsphys->ds_flags = ds->ds_phys->ds_flags; 1073fa9e4066Sahrens dsphys->ds_bp = ds->ds_phys->ds_bp; 1074ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1075fa9e4066Sahrens 10761d452cf5Sahrens ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0); 10771d452cf5Sahrens if (ds->ds_prev) { 1078088f3894Sahrens uint64_t next_clones_obj = 1079088f3894Sahrens ds->ds_prev->ds_phys->ds_next_clones_obj; 10801d452cf5Sahrens ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj == 1081fa9e4066Sahrens ds->ds_object || 10821d452cf5Sahrens ds->ds_prev->ds_phys->ds_num_children > 1); 10831d452cf5Sahrens if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) { 10841d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1085fa9e4066Sahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==, 10861d452cf5Sahrens ds->ds_prev->ds_phys->ds_creation_txg); 10871d452cf5Sahrens ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj; 1088088f3894Sahrens } else if (next_clones_obj != 0) { 10893b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1090c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 10913b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1092088f3894Sahrens next_clones_obj, dsobj, tx)); 1093fa9e4066Sahrens } 1094fa9e4066Sahrens } 1095fa9e4066Sahrens 1096a9799022Sck /* 1097a9799022Sck * If we have a reference-reservation on this dataset, we will 1098a9799022Sck * need to increase the amount of refreservation being charged 1099a9799022Sck * since our unique space is going to zero. 1100a9799022Sck */ 1101a9799022Sck if (ds->ds_reserved) { 11023f9d6ad7SLin Ling int64_t delta; 11033f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 11043f9d6ad7SLin Ling delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved); 110574e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 11063f9d6ad7SLin Ling delta, 0, 0, tx); 1107a9799022Sck } 1108a9799022Sck 1109fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1110cde58dbcSMatthew Ahrens ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist, 1111cde58dbcSMatthew Ahrens UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx); 1112cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1113cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj); 1114cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1115cde58dbcSMatthew Ahrens ds->ds_phys->ds_prev_snap_txg, tx); 1116cde58dbcSMatthew Ahrens 1117a4611edeSahrens ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg); 1118fa9e4066Sahrens ds->ds_phys->ds_prev_snap_obj = dsobj; 1119088f3894Sahrens ds->ds_phys->ds_prev_snap_txg = crtxg; 1120fa9e4066Sahrens ds->ds_phys->ds_unique_bytes = 0; 1121a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1122a9799022Sck ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1123fa9e4066Sahrens 11243b2aab18SMatthew Ahrens VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj, 11253b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1126fa9e4066Sahrens 1127fa9e4066Sahrens if (ds->ds_prev) 11283b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 11293b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1130745cd3c5Smaybee ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev)); 1131ecd6cf80Smarks 11323f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1133088f3894Sahrens 113471eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 113571eb0538SChris Kirby 11364445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1137fa9e4066Sahrens } 1138fa9e4066Sahrens 11393b2aab18SMatthew Ahrens static void 11403b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1141fa9e4066Sahrens { 11423b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 11433b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11443b2aab18SMatthew Ahrens nvpair_t *pair; 114591ebeef5Sahrens 11463b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 11473b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 11483b2aab18SMatthew Ahrens dsl_dataset_t *ds; 11493b2aab18SMatthew Ahrens char *name, *atp; 11503b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 11513b2aab18SMatthew Ahrens 11523b2aab18SMatthew Ahrens name = nvpair_name(pair); 11533b2aab18SMatthew Ahrens atp = strchr(name, '@'); 11543b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 11553b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 11563b2aab18SMatthew Ahrens 11573b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 11583b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 11593b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 11603b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 11613b2aab18SMatthew Ahrens } 11623b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 11633b2aab18SMatthew Ahrens } 1164fa9e4066Sahrens } 1165fa9e4066Sahrens 11663b2aab18SMatthew Ahrens /* 11673b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 11683b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 11693b2aab18SMatthew Ahrens */ 11703b2aab18SMatthew Ahrens int 11713b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 117219b94df9SMatthew Ahrens { 11733b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 11743b2aab18SMatthew Ahrens nvpair_t *pair; 11753b2aab18SMatthew Ahrens boolean_t needsuspend; 11763b2aab18SMatthew Ahrens int error; 11773b2aab18SMatthew Ahrens spa_t *spa; 11783b2aab18SMatthew Ahrens char *firstname; 11793b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 118019b94df9SMatthew Ahrens 11813b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 11823b2aab18SMatthew Ahrens if (pair == NULL) 11833b2aab18SMatthew Ahrens return (0); 11843b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 11853b2aab18SMatthew Ahrens 11863b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 11873b2aab18SMatthew Ahrens if (error != 0) 11883b2aab18SMatthew Ahrens return (error); 11893b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 11903b2aab18SMatthew Ahrens spa_close(spa, FTAG); 11913b2aab18SMatthew Ahrens 11923b2aab18SMatthew Ahrens if (needsuspend) { 11933b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 11943b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 11953b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 11963b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 11973b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 11983b2aab18SMatthew Ahrens char *atp; 11993b2aab18SMatthew Ahrens void *cookie; 12003b2aab18SMatthew Ahrens 12013b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 12023b2aab18SMatthew Ahrens if (atp == NULL) { 1203be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 12043b2aab18SMatthew Ahrens break; 12053b2aab18SMatthew Ahrens } 12063b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 12073b2aab18SMatthew Ahrens 12083b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 12093b2aab18SMatthew Ahrens if (error != 0) 12103b2aab18SMatthew Ahrens break; 12113b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 12123b2aab18SMatthew Ahrens (uintptr_t)cookie); 12133b2aab18SMatthew Ahrens } 12143b2aab18SMatthew Ahrens } 12153b2aab18SMatthew Ahrens 12163b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 12173b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 12183b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 12193b2aab18SMatthew Ahrens 12203b2aab18SMatthew Ahrens if (error == 0) { 12213b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 12223b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 12233b2aab18SMatthew Ahrens fnvlist_num_pairs(snaps) * 3); 12243b2aab18SMatthew Ahrens } 12253b2aab18SMatthew Ahrens 12263b2aab18SMatthew Ahrens if (suspended != NULL) { 12273b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 12283b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 12293b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 12303b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 12313b2aab18SMatthew Ahrens } 12323b2aab18SMatthew Ahrens fnvlist_free(suspended); 12333b2aab18SMatthew Ahrens } 12343b2aab18SMatthew Ahrens 12353b2aab18SMatthew Ahrens return (error); 12363b2aab18SMatthew Ahrens } 12373b2aab18SMatthew Ahrens 12383b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 12393b2aab18SMatthew Ahrens const char *ddsta_fsname; 12403b2aab18SMatthew Ahrens const char *ddsta_snapname; 12413b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 12423b2aab18SMatthew Ahrens const char *ddsta_htag; 12433b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 12443b2aab18SMatthew Ahrens 12453b2aab18SMatthew Ahrens static int 12463b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 12473b2aab18SMatthew Ahrens { 12483b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 12493b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 12503b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12513b2aab18SMatthew Ahrens int error; 12523b2aab18SMatthew Ahrens 12533b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 12543b2aab18SMatthew Ahrens if (error != 0) 12553b2aab18SMatthew Ahrens return (error); 12563b2aab18SMatthew Ahrens 1257ca48f36fSKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1258ca48f36fSKeith M Wesolowski tx, B_FALSE); 12593b2aab18SMatthew Ahrens if (error != 0) { 12603b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12613b2aab18SMatthew Ahrens return (error); 12623b2aab18SMatthew Ahrens } 12633b2aab18SMatthew Ahrens 12643b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 12653b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1266be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 12673b2aab18SMatthew Ahrens } 12683b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 12693b2aab18SMatthew Ahrens B_TRUE, tx); 12703b2aab18SMatthew Ahrens if (error != 0) { 12713b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12723b2aab18SMatthew Ahrens return (error); 12733b2aab18SMatthew Ahrens } 12743b2aab18SMatthew Ahrens 12753b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12763b2aab18SMatthew Ahrens return (0); 12773b2aab18SMatthew Ahrens } 12783b2aab18SMatthew Ahrens 12793b2aab18SMatthew Ahrens static void 12803b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 12813b2aab18SMatthew Ahrens { 12823b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 12833b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 12843b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12853b2aab18SMatthew Ahrens 12863b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 12873b2aab18SMatthew Ahrens 12883b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 12893b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 12903b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 12913b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 12923b2aab18SMatthew Ahrens 12933b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12943b2aab18SMatthew Ahrens } 12953b2aab18SMatthew Ahrens 12963b2aab18SMatthew Ahrens int 12973b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 12983b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 12993b2aab18SMatthew Ahrens { 13003b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 13013b2aab18SMatthew Ahrens int error; 13023b2aab18SMatthew Ahrens spa_t *spa; 13033b2aab18SMatthew Ahrens boolean_t needsuspend; 13043b2aab18SMatthew Ahrens void *cookie; 13053b2aab18SMatthew Ahrens 13063b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 13073b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 13083b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 13093b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 13103b2aab18SMatthew Ahrens 13113b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 13123b2aab18SMatthew Ahrens if (error != 0) 13133b2aab18SMatthew Ahrens return (error); 13143b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 13153b2aab18SMatthew Ahrens spa_close(spa, FTAG); 13163b2aab18SMatthew Ahrens 13173b2aab18SMatthew Ahrens if (needsuspend) { 13183b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 13193b2aab18SMatthew Ahrens if (error != 0) 13203b2aab18SMatthew Ahrens return (error); 13213b2aab18SMatthew Ahrens } 13223b2aab18SMatthew Ahrens 13233b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 13243b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3); 13253b2aab18SMatthew Ahrens 13263b2aab18SMatthew Ahrens if (needsuspend) 13273b2aab18SMatthew Ahrens zil_resume(cookie); 13283b2aab18SMatthew Ahrens return (error); 13293b2aab18SMatthew Ahrens } 13303b2aab18SMatthew Ahrens 13313b2aab18SMatthew Ahrens 13323b2aab18SMatthew Ahrens void 13333b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 13343b2aab18SMatthew Ahrens { 13353b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 13363b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 13373b2aab18SMatthew Ahrens ASSERT(ds->ds_phys->ds_next_snap_obj == 0); 13383b2aab18SMatthew Ahrens 13393b2aab18SMatthew Ahrens /* 13403b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 13413b2aab18SMatthew Ahrens * sync it out now. 13423b2aab18SMatthew Ahrens */ 13433b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 13443b2aab18SMatthew Ahrens ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid; 13453b2aab18SMatthew Ahrens 13463b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 13473b2aab18SMatthew Ahrens } 13483b2aab18SMatthew Ahrens 13493b2aab18SMatthew Ahrens static void 13503b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 13513b2aab18SMatthew Ahrens { 13523b2aab18SMatthew Ahrens uint64_t count = 0; 13533b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 13543b2aab18SMatthew Ahrens zap_cursor_t zc; 13553b2aab18SMatthew Ahrens zap_attribute_t za; 13563b2aab18SMatthew Ahrens nvlist_t *propval = fnvlist_alloc(); 13573b2aab18SMatthew Ahrens nvlist_t *val = fnvlist_alloc(); 13583b2aab18SMatthew Ahrens 13593b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 136019b94df9SMatthew Ahrens 136119b94df9SMatthew Ahrens /* 13623b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 136319b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 136419b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 136519b94df9SMatthew Ahrens */ 136619b94df9SMatthew Ahrens if (ds->ds_phys->ds_next_clones_obj != 0) { 13673b2aab18SMatthew Ahrens ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj, 136819b94df9SMatthew Ahrens &count)); 136919b94df9SMatthew Ahrens } 13703b2aab18SMatthew Ahrens if (count != ds->ds_phys->ds_num_children - 1) 137119b94df9SMatthew Ahrens goto fail; 137219b94df9SMatthew Ahrens for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj); 137319b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 137419b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 137519b94df9SMatthew Ahrens dsl_dataset_t *clone; 137619b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 13773b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 13783b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 137919b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 13803b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 138119b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 138219b94df9SMatthew Ahrens } 138319b94df9SMatthew Ahrens zap_cursor_fini(&zc); 13843b2aab18SMatthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 13853b2aab18SMatthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 138619b94df9SMatthew Ahrens fail: 138719b94df9SMatthew Ahrens nvlist_free(val); 138819b94df9SMatthew Ahrens nvlist_free(propval); 138919b94df9SMatthew Ahrens } 139019b94df9SMatthew Ahrens 1391fa9e4066Sahrens void 1392a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1393fa9e4066Sahrens { 13943b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1395187d6ac0SMatt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio; 1396a9799022Sck 13973b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 13983b2aab18SMatthew Ahrens 13994445fffbSMatthew Ahrens ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 : 14004445fffbSMatthew Ahrens (ds->ds_phys->ds_uncompressed_bytes * 100 / 14014445fffbSMatthew Ahrens ds->ds_phys->ds_compressed_bytes); 14024445fffbSMatthew Ahrens 14034445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 140477372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 140577372cb0SMatthew Ahrens ds->ds_phys->ds_uncompressed_bytes); 14064445fffbSMatthew Ahrens 14074445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 14084445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 14094445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 14104445fffbSMatthew Ahrens ds->ds_phys->ds_unique_bytes); 14114445fffbSMatthew Ahrens get_clones_stat(ds, nv); 14124445fffbSMatthew Ahrens } else { 14134445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 14144445fffbSMatthew Ahrens } 1415fa9e4066Sahrens 1416a9799022Sck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1417a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1418a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1419a9799022Sck 1420a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1421a2eea2e1Sahrens ds->ds_phys->ds_creation_time); 1422a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1423a2eea2e1Sahrens ds->ds_phys->ds_creation_txg); 1424a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1425a9799022Sck ds->ds_quota); 1426a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1427a9799022Sck ds->ds_reserved); 1428c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1429c5904d13Seschrock ds->ds_phys->ds_guid); 14301d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 14313f9d6ad7SLin Ling ds->ds_phys->ds_unique_bytes); 14321d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 14331d713200SEric Schrock ds->ds_object); 143492241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 143592241e0bSTom Erickson ds->ds_userrefs); 1436842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1437842727c2SChris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1438fa9e4066Sahrens 143919b94df9SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj != 0) { 144019b94df9SMatthew Ahrens uint64_t written, comp, uncomp; 144119b94df9SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 144219b94df9SMatthew Ahrens dsl_dataset_t *prev; 144319b94df9SMatthew Ahrens 144419b94df9SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, 144519b94df9SMatthew Ahrens ds->ds_phys->ds_prev_snap_obj, FTAG, &prev); 144619b94df9SMatthew Ahrens if (err == 0) { 144719b94df9SMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written, 144819b94df9SMatthew Ahrens &comp, &uncomp); 144919b94df9SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 145019b94df9SMatthew Ahrens if (err == 0) { 145119b94df9SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 145219b94df9SMatthew Ahrens written); 145319b94df9SMatthew Ahrens } 145419b94df9SMatthew Ahrens } 145519b94df9SMatthew Ahrens } 1456fa9e4066Sahrens } 1457fa9e4066Sahrens 1458a2eea2e1Sahrens void 1459a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1460a2eea2e1Sahrens { 14613b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 14623b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 14633b2aab18SMatthew Ahrens 1464a2eea2e1Sahrens stat->dds_creation_txg = ds->ds_phys->ds_creation_txg; 1465a2eea2e1Sahrens stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT; 14663cb34c60Sahrens stat->dds_guid = ds->ds_phys->ds_guid; 14674445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 14684445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 1469a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 1470a2eea2e1Sahrens stat->dds_num_clones = ds->ds_phys->ds_num_children - 1; 1471ebedde84SEric Taylor } else { 1472ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 1473ebedde84SEric Taylor stat->dds_num_clones = 0; 1474a2eea2e1Sahrens 14754445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 14764445fffbSMatthew Ahrens dsl_dataset_t *ods; 1477a2eea2e1Sahrens 14783b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 14794445fffbSMatthew Ahrens ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods)); 14804445fffbSMatthew Ahrens dsl_dataset_name(ods, stat->dds_origin); 14813b2aab18SMatthew Ahrens dsl_dataset_rele(ods, FTAG); 14824445fffbSMatthew Ahrens } 1483a2eea2e1Sahrens } 1484a2eea2e1Sahrens } 1485a2eea2e1Sahrens 1486a2eea2e1Sahrens uint64_t 1487a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1488a2eea2e1Sahrens { 148991ebeef5Sahrens return (ds->ds_fsid_guid); 1490a2eea2e1Sahrens } 1491a2eea2e1Sahrens 1492a2eea2e1Sahrens void 1493a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 1494a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 1495a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1496fa9e4066Sahrens { 1497ad135b5dSChristopher Siden *refdbytesp = ds->ds_phys->ds_referenced_bytes; 1498a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1499a9799022Sck if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) 1500a9799022Sck *availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes; 1501a9799022Sck if (ds->ds_quota != 0) { 1502a9799022Sck /* 1503a9799022Sck * Adjust available bytes according to refquota 1504a9799022Sck */ 1505a9799022Sck if (*refdbytesp < ds->ds_quota) 1506a9799022Sck *availbytesp = MIN(*availbytesp, 1507a9799022Sck ds->ds_quota - *refdbytesp); 1508a9799022Sck else 1509a9799022Sck *availbytesp = 0; 1510a9799022Sck } 1511a2eea2e1Sahrens *usedobjsp = ds->ds_phys->ds_bp.blk_fill; 1512a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1513fa9e4066Sahrens } 1514fa9e4066Sahrens 1515f18faf3fSek boolean_t 151634f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 1517f18faf3fSek { 1518f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 1519f18faf3fSek 15203b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 152134f2f8cfSMatthew Ahrens if (snap == NULL) 1522f18faf3fSek return (B_FALSE); 1523f18faf3fSek if (ds->ds_phys->ds_bp.blk_birth > 152434f2f8cfSMatthew Ahrens snap->ds_phys->ds_creation_txg) { 152534f2f8cfSMatthew Ahrens objset_t *os, *os_snap; 15266e0cbcaaSMatthew Ahrens /* 15276e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 15286e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 15296e0cbcaaSMatthew Ahrens * modified. 15306e0cbcaaSMatthew Ahrens */ 15316e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 15326e0cbcaaSMatthew Ahrens return (B_TRUE); 153334f2f8cfSMatthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0) 15346e0cbcaaSMatthew Ahrens return (B_TRUE); 15356e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 153634f2f8cfSMatthew Ahrens &os_snap->os_phys->os_meta_dnode, 15376e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 15386e0cbcaaSMatthew Ahrens } 1539f18faf3fSek return (B_FALSE); 1540f18faf3fSek } 1541f18faf3fSek 15423b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 15433b2aab18SMatthew Ahrens const char *ddrsa_fsname; 15443b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 15453b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 15463b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 15473b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 15483b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 15493b2aab18SMatthew Ahrens 15501d452cf5Sahrens /* ARGSUSED */ 1551fa9e4066Sahrens static int 15523b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 15533b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1554fa9e4066Sahrens { 15553b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 15563b2aab18SMatthew Ahrens int error; 1557fa9e4066Sahrens uint64_t val; 1558fa9e4066Sahrens 15593b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 15603b2aab18SMatthew Ahrens if (error != 0) { 15613b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 15623b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 15633b2aab18SMatthew Ahrens } 15641d452cf5Sahrens 15653b2aab18SMatthew Ahrens /* new name should not exist */ 15663b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 15673b2aab18SMatthew Ahrens if (error == 0) 1568be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 15693b2aab18SMatthew Ahrens else if (error == ENOENT) 15703b2aab18SMatthew Ahrens error = 0; 1571cdf5b4caSmmusante 1572cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 15733b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 15743b2aab18SMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN) 1575be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 1576cdf5b4caSmmusante 15773b2aab18SMatthew Ahrens return (error); 15781d452cf5Sahrens } 1579fa9e4066Sahrens 15803b2aab18SMatthew Ahrens static int 15813b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 15821d452cf5Sahrens { 15833b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 15843b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15851d452cf5Sahrens dsl_dataset_t *hds; 15863b2aab18SMatthew Ahrens int error; 1587fa9e4066Sahrens 15883b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 15893b2aab18SMatthew Ahrens if (error != 0) 15903b2aab18SMatthew Ahrens return (error); 1591fa9e4066Sahrens 15923b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 15933b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 15943b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 15953b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 15963b2aab18SMatthew Ahrens } else { 15973b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 15983b2aab18SMatthew Ahrens } 1599745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 16003b2aab18SMatthew Ahrens return (error); 1601fa9e4066Sahrens } 1602fa9e4066Sahrens 1603cdf5b4caSmmusante static int 16043b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 16053b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1606cdf5b4caSmmusante { 16073b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 16083b2aab18SMatthew Ahrens dsl_dataset_t *ds; 16093b2aab18SMatthew Ahrens uint64_t val; 16103b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 16113b2aab18SMatthew Ahrens int error; 1612ecd6cf80Smarks 16133b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 16143b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 16153b2aab18SMatthew Ahrens if (error == ENOENT) { 16163b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 16173b2aab18SMatthew Ahrens return (0); 1618ecd6cf80Smarks } 1619ecd6cf80Smarks 16203b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 16213b2aab18SMatthew Ahrens 16223b2aab18SMatthew Ahrens /* log before we change the name */ 16233b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 16243b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 1625cdf5b4caSmmusante 16263b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx)); 16273b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 16283b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 16293b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 16303b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj, 16313b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 1632cdf5b4caSmmusante 16333b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1634cdf5b4caSmmusante return (0); 1635cdf5b4caSmmusante } 1636cdf5b4caSmmusante 16373b2aab18SMatthew Ahrens static void 16383b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 1639cdf5b4caSmmusante { 16403b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 16413b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 16423b2aab18SMatthew Ahrens dsl_dataset_t *hds; 1643cdf5b4caSmmusante 16443b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 16453b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 16463b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 16473b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 16483b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 16493b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 16503b2aab18SMatthew Ahrens } else { 16513b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 1652cdf5b4caSmmusante } 16533b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 1654cdf5b4caSmmusante } 1655cdf5b4caSmmusante 16563b2aab18SMatthew Ahrens int 16573b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 16583b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 16593a5a36beSmmusante { 16603b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 16613a5a36beSmmusante 16623b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 16633b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 16643b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 16653b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 16663a5a36beSmmusante 16673b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 16683b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 1)); 16693a5a36beSmmusante } 16703a5a36beSmmusante 167191948b51SKeith M Wesolowski /* 167291948b51SKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is 167391948b51SKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here 167491948b51SKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want 167591948b51SKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away 167691948b51SKeith M Wesolowski * when we release the long hold. 167791948b51SKeith M Wesolowski */ 167891948b51SKeith M Wesolowski static int 167991948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 168091948b51SKeith M Wesolowski { 168191948b51SKeith M Wesolowski boolean_t held; 168291948b51SKeith M Wesolowski 168391948b51SKeith M Wesolowski if (!dmu_tx_is_syncing(tx)) 168491948b51SKeith M Wesolowski return (0); 168591948b51SKeith M Wesolowski 168691948b51SKeith M Wesolowski if (owner != NULL) { 168791948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner); 168891948b51SKeith M Wesolowski dsl_dataset_long_rele(ds, owner); 168991948b51SKeith M Wesolowski } 169091948b51SKeith M Wesolowski 169191948b51SKeith M Wesolowski held = dsl_dataset_long_held(ds); 169291948b51SKeith M Wesolowski 169391948b51SKeith M Wesolowski if (owner != NULL) 169491948b51SKeith M Wesolowski dsl_dataset_long_hold(ds, owner); 169591948b51SKeith M Wesolowski 169691948b51SKeith M Wesolowski if (held) 169791948b51SKeith M Wesolowski return (SET_ERROR(EBUSY)); 169891948b51SKeith M Wesolowski 169991948b51SKeith M Wesolowski return (0); 170091948b51SKeith M Wesolowski } 170191948b51SKeith M Wesolowski 170291948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg { 170391948b51SKeith M Wesolowski const char *ddra_fsname; 170491948b51SKeith M Wesolowski void *ddra_owner; 1705a7027df1SMatthew Ahrens nvlist_t *ddra_result; 170691948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t; 170791948b51SKeith M Wesolowski 17083b2aab18SMatthew Ahrens static int 17093b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 1710fa9e4066Sahrens { 171191948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 17123b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17131d452cf5Sahrens dsl_dataset_t *ds; 17143b2aab18SMatthew Ahrens int64_t unused_refres_delta; 17153b2aab18SMatthew Ahrens int error; 1716fa9e4066Sahrens 171791948b51SKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 17183b2aab18SMatthew Ahrens if (error != 0) 17193b2aab18SMatthew Ahrens return (error); 1720370c1af0SSanjeev Bagewadi 17213b2aab18SMatthew Ahrens /* must not be a snapshot */ 17223b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 17233b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1724be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 17253b2aab18SMatthew Ahrens } 17263a5a36beSmmusante 17273b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 17283b2aab18SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 17293b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1730be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 17313b2aab18SMatthew Ahrens } 17323a5a36beSmmusante 1733*78f17100SMatthew Ahrens /* must not have any bookmarks after the most recent snapshot */ 1734*78f17100SMatthew Ahrens nvlist_t *proprequest = fnvlist_alloc(); 1735*78f17100SMatthew Ahrens fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 1736*78f17100SMatthew Ahrens nvlist_t *bookmarks = fnvlist_alloc(); 1737*78f17100SMatthew Ahrens error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks); 1738*78f17100SMatthew Ahrens fnvlist_free(proprequest); 1739*78f17100SMatthew Ahrens if (error != 0) 1740*78f17100SMatthew Ahrens return (error); 1741*78f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL); 1742*78f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) { 1743*78f17100SMatthew Ahrens nvlist_t *valuenv = 1744*78f17100SMatthew Ahrens fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair), 1745*78f17100SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CREATETXG)); 1746*78f17100SMatthew Ahrens uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value"); 1747*78f17100SMatthew Ahrens if (createtxg > ds->ds_phys->ds_prev_snap_txg) { 1748*78f17100SMatthew Ahrens fnvlist_free(bookmarks); 1749*78f17100SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1750*78f17100SMatthew Ahrens return (SET_ERROR(EEXIST)); 1751*78f17100SMatthew Ahrens } 1752*78f17100SMatthew Ahrens } 1753*78f17100SMatthew Ahrens fnvlist_free(bookmarks); 1754*78f17100SMatthew Ahrens 175591948b51SKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 175691948b51SKeith M Wesolowski if (error != 0) { 17573b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 175891948b51SKeith M Wesolowski return (error); 17593b2aab18SMatthew Ahrens } 17603b2aab18SMatthew Ahrens 17613b2aab18SMatthew Ahrens /* 17623b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 17633b2aab18SMatthew Ahrens * the refquota. 17643b2aab18SMatthew Ahrens */ 17653b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 17663b2aab18SMatthew Ahrens ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) { 17673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1768be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 1769fa9e4066Sahrens } 1770370c1af0SSanjeev Bagewadi 17713b2aab18SMatthew Ahrens /* 17723b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 17733b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 17743b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 17753b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 17763b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 17773b2aab18SMatthew Ahrens */ 17783b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 17793b2aab18SMatthew Ahrens ds->ds_phys->ds_unique_bytes); 17803b2aab18SMatthew Ahrens 17813b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 17823b2aab18SMatthew Ahrens unused_refres_delta > 17833b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 17843b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1785be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1786fa9e4066Sahrens } 1787fa9e4066Sahrens 17883b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 17893b2aab18SMatthew Ahrens return (0); 17903b2aab18SMatthew Ahrens } 17911d452cf5Sahrens 17923b2aab18SMatthew Ahrens static void 17933b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 17943b2aab18SMatthew Ahrens { 179591948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 17963b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17973b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 17983b2aab18SMatthew Ahrens uint64_t cloneobj; 1799a7027df1SMatthew Ahrens char namebuf[ZFS_MAXNAMELEN]; 18001d452cf5Sahrens 180191948b51SKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 18021d452cf5Sahrens 1803a7027df1SMatthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf); 1804a7027df1SMatthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf); 1805a7027df1SMatthew Ahrens 18063b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 18073b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 18081d452cf5Sahrens 18093b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 18101d452cf5Sahrens 18113b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 18123b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 18133b2aab18SMatthew Ahrens 18143b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 18153b2aab18SMatthew Ahrens 18163b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 18173b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 18183b2aab18SMatthew Ahrens } 18193b2aab18SMatthew Ahrens 182091948b51SKeith M Wesolowski /* 1821a7027df1SMatthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot. 1822a7027df1SMatthew Ahrens * The name of the most recent snapshot will be returned under key "target" 1823a7027df1SMatthew Ahrens * in the result nvlist. 182491948b51SKeith M Wesolowski * 1825a7027df1SMatthew Ahrens * If owner != NULL: 182691948b51SKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry 182791948b51SKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we 182891948b51SKeith M Wesolowski * succeed or not. 182991948b51SKeith M Wesolowski * 183091948b51SKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See 183191948b51SKeith M Wesolowski * notes above zfs_suspend_fs() for further details. 183291948b51SKeith M Wesolowski */ 18333b2aab18SMatthew Ahrens int 1834a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result) 18353b2aab18SMatthew Ahrens { 183691948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t ddra; 183791948b51SKeith M Wesolowski 183891948b51SKeith M Wesolowski ddra.ddra_fsname = fsname; 183991948b51SKeith M Wesolowski ddra.ddra_owner = owner; 1840a7027df1SMatthew Ahrens ddra.ddra_result = result; 184191948b51SKeith M Wesolowski 18423b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 1843a7027df1SMatthew Ahrens dsl_dataset_rollback_sync, &ddra, 1)); 1844fa9e4066Sahrens } 184599653d4eSeschrock 1846088f3894Sahrens struct promotenode { 1847745cd3c5Smaybee list_node_t link; 1848745cd3c5Smaybee dsl_dataset_t *ds; 1849745cd3c5Smaybee }; 1850745cd3c5Smaybee 18513b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg { 18523b2aab18SMatthew Ahrens const char *ddpa_clonename; 18533b2aab18SMatthew Ahrens dsl_dataset_t *ddpa_clone; 185474e7dc98SMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps; 18553b2aab18SMatthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */ 185674e7dc98SMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 1857681d9761SEric Taylor char *err_ds; 18583b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t; 18591d452cf5Sahrens 186074e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 18613b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 18623b2aab18SMatthew Ahrens void *tag); 18633b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 186474e7dc98SMatthew Ahrens 186599653d4eSeschrock static int 18663b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 186799653d4eSeschrock { 18683b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 18693b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 18703b2aab18SMatthew Ahrens dsl_dataset_t *hds; 18713b2aab18SMatthew Ahrens struct promotenode *snap; 18723b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 1873745cd3c5Smaybee int err; 1874cde58dbcSMatthew Ahrens uint64_t unused; 18751d452cf5Sahrens 18763b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 18773b2aab18SMatthew Ahrens if (err != 0) 18783b2aab18SMatthew Ahrens return (err); 187999653d4eSeschrock 18803b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 18811d452cf5Sahrens 18823b2aab18SMatthew Ahrens if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) { 18833b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 1884be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 18853b2aab18SMatthew Ahrens } 18863b2aab18SMatthew Ahrens 18873b2aab18SMatthew Ahrens /* 18883b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 18893b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 18903b2aab18SMatthew Ahrens */ 18913b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 18923b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 18933b2aab18SMatthew Ahrens return (0); 18943b2aab18SMatthew Ahrens } 18953b2aab18SMatthew Ahrens 18963b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 18973b2aab18SMatthew Ahrens origin_ds = snap->ds; 189899653d4eSeschrock 18993cb34c60Sahrens /* compute origin's new unique space */ 19003b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 190174e7dc98SMatthew Ahrens ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 1902cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 1903cde58dbcSMatthew Ahrens origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX, 19043b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 190599653d4eSeschrock 1906745cd3c5Smaybee /* 1907745cd3c5Smaybee * Walk the snapshots that we are moving 1908745cd3c5Smaybee * 190974e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 19103b2aab18SMatthew Ahrens * to used by each snapshot: 191174e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 191274e7dc98SMatthew Ahrens * So each snapshot gave birth to: 191374e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 1914745cd3c5Smaybee * So a sequence would look like: 191574e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 1916745cd3c5Smaybee * Which simplifies to: 191774e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 1918745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 191974e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 1920745cd3c5Smaybee */ 19213b2aab18SMatthew Ahrens ddpa->used = origin_ds->ds_phys->ds_referenced_bytes; 19223b2aab18SMatthew Ahrens ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes; 19233b2aab18SMatthew Ahrens ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes; 19243b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 19253b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 192699653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 1927745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 192899653d4eSeschrock 19293b2aab18SMatthew Ahrens /* 19303b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 19313b2aab18SMatthew Ahrens * the objset. 19323b2aab18SMatthew Ahrens */ 19333b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 1934be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 19353b2aab18SMatthew Ahrens goto out; 19363b2aab18SMatthew Ahrens } 19373b2aab18SMatthew Ahrens 193899653d4eSeschrock /* Check that the snapshot name does not conflict */ 19393b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 1940745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 1941681d9761SEric Taylor if (err == 0) { 19423b2aab18SMatthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 1943be6fd75aSMatthew Ahrens err = SET_ERROR(EEXIST); 1944681d9761SEric Taylor goto out; 1945681d9761SEric Taylor } 1946745cd3c5Smaybee if (err != ENOENT) 1947681d9761SEric Taylor goto out; 194899653d4eSeschrock 1949745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 195074e7dc98SMatthew Ahrens if (ds->ds_phys->ds_prev_snap_obj == 0) 195174e7dc98SMatthew Ahrens continue; 195274e7dc98SMatthew Ahrens 1953cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 1954cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 19553b2aab18SMatthew Ahrens ddpa->used += dlused; 19563b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 19573b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 195874e7dc98SMatthew Ahrens } 1959745cd3c5Smaybee 1960745cd3c5Smaybee /* 1961745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 1962745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 1963745cd3c5Smaybee */ 19643b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 19653b2aab18SMatthew Ahrens ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes; 19663b2aab18SMatthew Ahrens ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes; 19673b2aab18SMatthew Ahrens ddpa->uncomp -= 19683b2aab18SMatthew Ahrens ddpa->origin_origin->ds_phys->ds_uncompressed_bytes; 196999653d4eSeschrock } 197099653d4eSeschrock 197199653d4eSeschrock /* Check that there is enough space here */ 197274e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 19733b2aab18SMatthew Ahrens ddpa->used); 19743b2aab18SMatthew Ahrens if (err != 0) 19753b2aab18SMatthew Ahrens goto out; 197674e7dc98SMatthew Ahrens 197774e7dc98SMatthew Ahrens /* 197874e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 197974e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 198074e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 198174e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 198274e7dc98SMatthew Ahrens */ 198374e7dc98SMatthew Ahrens if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 198474e7dc98SMatthew Ahrens uint64_t space; 198574e7dc98SMatthew Ahrens 198674e7dc98SMatthew Ahrens /* 198774e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 19883f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 1989cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 199074e7dc98SMatthew Ahrens * calls will be fast because they do not have to 199174e7dc98SMatthew Ahrens * iterate over all bps. 199274e7dc98SMatthew Ahrens */ 19933b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 19943b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 19953b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 19963b2aab18SMatthew Ahrens if (err != 0) 19973b2aab18SMatthew Ahrens goto out; 199874e7dc98SMatthew Ahrens 19993b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 20003f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 20013b2aab18SMatthew Ahrens if (err != 0) 20023b2aab18SMatthew Ahrens goto out; 20033b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 200474e7dc98SMatthew Ahrens } 200574e7dc98SMatthew Ahrens if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { 20063b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 20073b2aab18SMatthew Ahrens origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap); 20083b2aab18SMatthew Ahrens if (err != 0) 20093b2aab18SMatthew Ahrens goto out; 2010745cd3c5Smaybee } 20111d452cf5Sahrens 2012681d9761SEric Taylor out: 20133b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2014681d9761SEric Taylor return (err); 20151d452cf5Sahrens } 201699653d4eSeschrock 20171d452cf5Sahrens static void 20183b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 20191d452cf5Sahrens { 20203b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 20213b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 20223b2aab18SMatthew Ahrens dsl_dataset_t *hds; 20233b2aab18SMatthew Ahrens struct promotenode *snap; 20243b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 20253b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 20263b2aab18SMatthew Ahrens dsl_dir_t *dd; 20273cb34c60Sahrens dsl_dir_t *odd = NULL; 2028088f3894Sahrens uint64_t oldnext_obj; 202974e7dc98SMatthew Ahrens int64_t delta; 20301d452cf5Sahrens 20313b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 20323b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 20333b2aab18SMatthew Ahrens 20343b2aab18SMatthew Ahrens ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE); 20351d452cf5Sahrens 20363b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 20373b2aab18SMatthew Ahrens origin_ds = snap->ds; 20383b2aab18SMatthew Ahrens dd = hds->ds_dir; 20393b2aab18SMatthew Ahrens 20403b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 204174e7dc98SMatthew Ahrens origin_head = snap->ds; 204274e7dc98SMatthew Ahrens 20430b69c2f0Sahrens /* 20443cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 20450b69c2f0Sahrens * changing. 20460b69c2f0Sahrens */ 20473b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 20483cb34c60Sahrens NULL, FTAG, &odd)); 204999653d4eSeschrock 2050745cd3c5Smaybee /* change origin's next snap */ 2051745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2052088f3894Sahrens oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj; 20533b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 205474e7dc98SMatthew Ahrens ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object); 205574e7dc98SMatthew Ahrens origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object; 2056745cd3c5Smaybee 2057088f3894Sahrens /* change the origin's next clone */ 2058088f3894Sahrens if (origin_ds->ds_phys->ds_next_clones_obj) { 20593b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 20603b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 20613b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2062088f3894Sahrens origin_ds->ds_phys->ds_next_clones_obj, 2063088f3894Sahrens oldnext_obj, tx)); 2064088f3894Sahrens } 2065088f3894Sahrens 2066745cd3c5Smaybee /* change origin */ 2067745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2068745cd3c5Smaybee ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object); 2069745cd3c5Smaybee dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj; 20703f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2071745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2072745cd3c5Smaybee odd->dd_phys->dd_origin_obj = origin_ds->ds_object; 20733f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 20743f9d6ad7SLin Ling origin_ds->ds_phys->ds_creation_txg; 2075745cd3c5Smaybee 2076cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2077cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 20783b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2079cde58dbcSMatthew Ahrens odd->dd_phys->dd_clones, hds->ds_object, tx)); 20803b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 20813b2aab18SMatthew Ahrens ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2082cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2083cde58dbcSMatthew Ahrens 20843b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 20853b2aab18SMatthew Ahrens ddpa->origin_origin->ds_dir->dd_phys->dd_clones, 2086cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2087cde58dbcSMatthew Ahrens if (dd->dd_phys->dd_clones == 0) { 2088cde58dbcSMatthew Ahrens dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset, 2089cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 2090cde58dbcSMatthew Ahrens } 20913b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2092cde58dbcSMatthew Ahrens dd->dd_phys->dd_clones, origin_head->ds_object, tx)); 2093cde58dbcSMatthew Ahrens } 2094cde58dbcSMatthew Ahrens 209599653d4eSeschrock /* move snapshots to this dir */ 20963b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 20973b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2098745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 209999653d4eSeschrock 21003b2aab18SMatthew Ahrens /* 21013b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 21023b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 21033b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 21043b2aab18SMatthew Ahrens */ 2105503ad85cSMatthew Ahrens if (ds->ds_objset) { 2106503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2107503ad85cSMatthew Ahrens ds->ds_objset = NULL; 21083baa08fcSek } 21093b2aab18SMatthew Ahrens 211099653d4eSeschrock /* move snap name entry */ 21113b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 21123b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2113745cd3c5Smaybee ds->ds_snapname, tx)); 21143b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 211599653d4eSeschrock hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname, 211699653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2117cde58dbcSMatthew Ahrens 211899653d4eSeschrock /* change containing dsl_dir */ 211999653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 21203cb34c60Sahrens ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object); 212199653d4eSeschrock ds->ds_phys->ds_dir_obj = dd->dd_object; 21223cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 21233b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 21243b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 212599653d4eSeschrock NULL, ds, &ds->ds_dir)); 212699653d4eSeschrock 2127cde58dbcSMatthew Ahrens /* move any clone references */ 2128cde58dbcSMatthew Ahrens if (ds->ds_phys->ds_next_clones_obj && 2129cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2130cde58dbcSMatthew Ahrens zap_cursor_t zc; 2131cde58dbcSMatthew Ahrens zap_attribute_t za; 2132cde58dbcSMatthew Ahrens 21333b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 21343b2aab18SMatthew Ahrens ds->ds_phys->ds_next_clones_obj); 21353b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 21363b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 21373b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 21383b2aab18SMatthew Ahrens uint64_t o; 2139a9799022Sck 21403b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 21413b2aab18SMatthew Ahrens /* 21423b2aab18SMatthew Ahrens * We've already moved the 21433b2aab18SMatthew Ahrens * origin's reference. 21443b2aab18SMatthew Ahrens */ 21453b2aab18SMatthew Ahrens continue; 21463b2aab18SMatthew Ahrens } 2147a9799022Sck 21483b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 21493b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 21503b2aab18SMatthew Ahrens o = cnds->ds_dir->dd_phys->dd_head_dataset_obj; 2151a9799022Sck 21523b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 21533b2aab18SMatthew Ahrens odd->dd_phys->dd_clones, o, tx)); 21543b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 21553b2aab18SMatthew Ahrens dd->dd_phys->dd_clones, o, tx)); 21563b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 21573b2aab18SMatthew Ahrens } 21583b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 21593b2aab18SMatthew Ahrens } 21609082849eSck 21613b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2162a9799022Sck } 2163a9799022Sck 2164a9799022Sck /* 21653b2aab18SMatthew Ahrens * Change space accounting. 21663b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 21673b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 21683b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2169a9799022Sck */ 2170a9799022Sck 21713b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 21723b2aab18SMatthew Ahrens dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 21733b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 21743b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 21753b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 21763b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 21773b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 21783b2aab18SMatthew Ahrens 21793b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 21803b2aab18SMatthew Ahrens odd->dd_phys->dd_used_breakdown[DD_USED_SNAP]; 21813b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 21823b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 21833b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 21843b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 21853b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 21863b2aab18SMatthew Ahrens 21873b2aab18SMatthew Ahrens origin_ds->ds_phys->ds_unique_bytes = ddpa->unique; 21883b2aab18SMatthew Ahrens 21893b2aab18SMatthew Ahrens /* log history record */ 21903b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 21913b2aab18SMatthew Ahrens 21923b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 21933b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2194a9799022Sck } 2195a9799022Sck 21963b2aab18SMatthew Ahrens /* 21973b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 21983b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 21993b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 22003b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 22013b2aab18SMatthew Ahrens */ 2202a9799022Sck static int 22033b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 22043b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2205a9799022Sck { 22063b2aab18SMatthew Ahrens uint64_t obj = last_obj; 2207a9799022Sck 22083b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 22093b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 2210a9799022Sck 22113b2aab18SMatthew Ahrens while (obj != first_obj) { 22123b2aab18SMatthew Ahrens dsl_dataset_t *ds; 22133b2aab18SMatthew Ahrens struct promotenode *snap; 22143b2aab18SMatthew Ahrens int err; 221592241e0bSTom Erickson 22163b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 22173b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 22183b2aab18SMatthew Ahrens if (err != 0) 22193b2aab18SMatthew Ahrens return (err); 2220a9799022Sck 22213b2aab18SMatthew Ahrens if (first_obj == 0) 22223b2aab18SMatthew Ahrens first_obj = ds->ds_dir->dd_phys->dd_origin_obj; 22233b2aab18SMatthew Ahrens 22243b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 22253b2aab18SMatthew Ahrens snap->ds = ds; 22263b2aab18SMatthew Ahrens list_insert_tail(l, snap); 22273b2aab18SMatthew Ahrens obj = ds->ds_phys->ds_prev_snap_obj; 22283b2aab18SMatthew Ahrens } 2229a9799022Sck 2230a9799022Sck return (0); 2231a9799022Sck } 2232a9799022Sck 22333b2aab18SMatthew Ahrens static int 22343b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2235a9799022Sck { 22363b2aab18SMatthew Ahrens struct promotenode *snap; 2237a9799022Sck 22383b2aab18SMatthew Ahrens *spacep = 0; 22393b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 22403b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 22413b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 22423b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 22433b2aab18SMatthew Ahrens *spacep += used; 224492241e0bSTom Erickson } 22453b2aab18SMatthew Ahrens return (0); 2246a9799022Sck } 2247a9799022Sck 22483b2aab18SMatthew Ahrens static void 22493b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 2250a9799022Sck { 22513b2aab18SMatthew Ahrens struct promotenode *snap; 225292241e0bSTom Erickson 22533b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 22543b2aab18SMatthew Ahrens return; 2255a9799022Sck 22563b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 22573b2aab18SMatthew Ahrens list_remove(l, snap); 22583b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 22593b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 22603b2aab18SMatthew Ahrens } 22613b2aab18SMatthew Ahrens list_destroy(l); 2262a9799022Sck } 2263a9799022Sck 2264a9799022Sck static int 22653b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2266a9799022Sck { 22673b2aab18SMatthew Ahrens int error; 22683b2aab18SMatthew Ahrens dsl_dir_t *dd; 22693b2aab18SMatthew Ahrens struct promotenode *snap; 2270a9799022Sck 22713b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 22723b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 22733b2aab18SMatthew Ahrens if (error != 0) 22743b2aab18SMatthew Ahrens return (error); 22753b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 2276a9799022Sck 22773b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) || 22783b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 22793b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 2280be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 22813b2aab18SMatthew Ahrens } 2282a9799022Sck 22833b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj, 22843b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 22853b2aab18SMatthew Ahrens if (error != 0) 22863b2aab18SMatthew Ahrens goto out; 2287a9799022Sck 22883b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 22893b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 22903b2aab18SMatthew Ahrens if (error != 0) 22913b2aab18SMatthew Ahrens goto out; 2292a9799022Sck 22933b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 22943b2aab18SMatthew Ahrens ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj); 22953b2aab18SMatthew Ahrens error = snaplist_make(dp, dd->dd_phys->dd_origin_obj, 22963b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_phys->dd_head_dataset_obj, 22973b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 22983b2aab18SMatthew Ahrens if (error != 0) 22993b2aab18SMatthew Ahrens goto out; 2300379c004dSEric Schrock 23013b2aab18SMatthew Ahrens if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) { 23023b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 23033b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_phys->dd_origin_obj, 23043b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 23053b2aab18SMatthew Ahrens if (error != 0) 23063b2aab18SMatthew Ahrens goto out; 2307379c004dSEric Schrock } 23083b2aab18SMatthew Ahrens out: 23093b2aab18SMatthew Ahrens if (error != 0) 23103b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 23113b2aab18SMatthew Ahrens return (error); 2312a9799022Sck } 2313a9799022Sck 2314a9799022Sck static void 23153b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2316a9799022Sck { 23173b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 23183b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 23193b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 23203b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 23213b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 23223b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 23233b2aab18SMatthew Ahrens } 232402c8f3f0SMatthew Ahrens 23253b2aab18SMatthew Ahrens /* 23263b2aab18SMatthew Ahrens * Promote a clone. 23273b2aab18SMatthew Ahrens * 23283b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 23293b2aab18SMatthew Ahrens * in with the name. (It must be at least MAXNAMELEN bytes long.) 23303b2aab18SMatthew Ahrens */ 23313b2aab18SMatthew Ahrens int 23323b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 23333b2aab18SMatthew Ahrens { 23343b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 23353b2aab18SMatthew Ahrens uint64_t numsnaps; 23363b2aab18SMatthew Ahrens int error; 23373b2aab18SMatthew Ahrens objset_t *os; 233892241e0bSTom Erickson 23393b2aab18SMatthew Ahrens /* 23403b2aab18SMatthew Ahrens * We will modify space proportional to the number of 23413b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 23423b2aab18SMatthew Ahrens */ 23433b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 23443b2aab18SMatthew Ahrens if (error != 0) 23453b2aab18SMatthew Ahrens return (error); 23463b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 23473b2aab18SMatthew Ahrens dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps); 23483b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 23493b2aab18SMatthew Ahrens if (error != 0) 23503b2aab18SMatthew Ahrens return (error); 235102c8f3f0SMatthew Ahrens 23523b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 23533b2aab18SMatthew Ahrens ddpa.err_ds = conflsnap; 235402c8f3f0SMatthew Ahrens 23553b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check, 23563b2aab18SMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 2 + numsnaps)); 2357a9799022Sck } 2358a9799022Sck 2359a9799022Sck int 23603b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 236191948b51SKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 2362a9799022Sck { 23633b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2364a9799022Sck 23653b2aab18SMatthew Ahrens /* they should both be heads */ 23663b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(clone) || 23673b2aab18SMatthew Ahrens dsl_dataset_is_snapshot(origin_head)) 2368be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 236992241e0bSTom Erickson 237034f2f8cfSMatthew Ahrens /* if we are not forcing, the branch point should be just before them */ 237134f2f8cfSMatthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev) 2372be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2373a9799022Sck 23743b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 23753b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 23763b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 237734f2f8cfSMatthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir) 2378be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 237992241e0bSTom Erickson 23803b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 23813b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2382be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2383842727c2SChris Kirby 23843b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 238534f2f8cfSMatthew Ahrens if (!force && 238634f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 2387be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 2388c99e4bdcSChris Kirby 23893b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 239091948b51SKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx)) 2391be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 23923b2aab18SMatthew Ahrens 23933b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 23943b2aab18SMatthew Ahrens unused_refres_delta = 23953b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23963b2aab18SMatthew Ahrens origin_head->ds_phys->ds_unique_bytes) - 23973b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 23983b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 23993b2aab18SMatthew Ahrens 24003b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 24013b2aab18SMatthew Ahrens unused_refres_delta > 24023b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2403be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 24043b2aab18SMatthew Ahrens 24053b2aab18SMatthew Ahrens /* clone can't be over the head's refquota */ 24063b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 24073b2aab18SMatthew Ahrens clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota) 2408be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2409c99e4bdcSChris Kirby 24103b2aab18SMatthew Ahrens return (0); 2411c99e4bdcSChris Kirby } 2412c99e4bdcSChris Kirby 2413a7f53a56SChris Kirby void 24143b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 24153b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 2416a7f53a56SChris Kirby { 24173b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 24183b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2419a7f53a56SChris Kirby 24203b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 24213b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 24223b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota); 242334f2f8cfSMatthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 2424842727c2SChris Kirby 24253b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 24263b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2427842727c2SChris Kirby 24283b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 24293b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 24303b2aab18SMatthew Ahrens clone->ds_objset = NULL; 24313b2aab18SMatthew Ahrens } 2432842727c2SChris Kirby 24333b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 24343b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 24353b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 2436842727c2SChris Kirby } 2437842727c2SChris Kirby 24383b2aab18SMatthew Ahrens unused_refres_delta = 24393b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 24403b2aab18SMatthew Ahrens origin_head->ds_phys->ds_unique_bytes) - 24413b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 24423b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 24433b2aab18SMatthew Ahrens 24443b2aab18SMatthew Ahrens /* 24453b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 24463b2aab18SMatthew Ahrens */ 24473b2aab18SMatthew Ahrens if (clone->ds_prev) { 24483b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 24493b2aab18SMatthew Ahrens uint64_t comp, uncomp; 24503b2aab18SMatthew Ahrens 24513b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 24523b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 24533b2aab18SMatthew Ahrens origin->ds_phys->ds_prev_snap_txg, UINT64_MAX, 24543b2aab18SMatthew Ahrens &origin->ds_phys->ds_unique_bytes, &comp, &uncomp); 24553b2aab18SMatthew Ahrens } 24563b2aab18SMatthew Ahrens 24573b2aab18SMatthew Ahrens /* swap blkptrs */ 24583b2aab18SMatthew Ahrens { 24593b2aab18SMatthew Ahrens blkptr_t tmp; 24603b2aab18SMatthew Ahrens tmp = origin_head->ds_phys->ds_bp; 24613b2aab18SMatthew Ahrens origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp; 24623b2aab18SMatthew Ahrens clone->ds_phys->ds_bp = tmp; 24633b2aab18SMatthew Ahrens } 24643b2aab18SMatthew Ahrens 24653b2aab18SMatthew Ahrens /* set dd_*_bytes */ 24663b2aab18SMatthew Ahrens { 24673b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 24683b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 24693b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 24703b2aab18SMatthew Ahrens 24713b2aab18SMatthew Ahrens ASSERT3U(clone->ds_dir->dd_phys-> 24723b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 24733b2aab18SMatthew Ahrens 24743b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 24753b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 24763b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 24773b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 247815508ac0SChris Kirby 24793b2aab18SMatthew Ahrens dused = clone->ds_phys->ds_referenced_bytes + cdl_used - 24803b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_referenced_bytes + odl_used); 24813b2aab18SMatthew Ahrens dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp - 24823b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_compressed_bytes + odl_comp); 24833b2aab18SMatthew Ahrens duncomp = clone->ds_phys->ds_uncompressed_bytes + 24843b2aab18SMatthew Ahrens cdl_uncomp - 24853b2aab18SMatthew Ahrens (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp); 2486842727c2SChris Kirby 24873b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 24883b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 24893b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 24903b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 2491842727c2SChris Kirby 2492842727c2SChris Kirby /* 24933b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 24943b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 24953b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 24963b2aab18SMatthew Ahrens * changing that affects the snapused). 2497842727c2SChris Kirby */ 24983b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 24993b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 25003b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 25013b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 25023b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 25033b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 25043b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 25053b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 2506842727c2SChris Kirby } 2507842727c2SChris Kirby 25083b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 25093b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_referenced_bytes, 25103b2aab18SMatthew Ahrens clone->ds_phys->ds_referenced_bytes); 25113b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_compressed_bytes, 25123b2aab18SMatthew Ahrens clone->ds_phys->ds_compressed_bytes); 25133b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes, 25143b2aab18SMatthew Ahrens clone->ds_phys->ds_uncompressed_bytes); 25153b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_unique_bytes, 25163b2aab18SMatthew Ahrens clone->ds_phys->ds_unique_bytes); 2517842727c2SChris Kirby 25183b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 25193b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 25203b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 2521ca45db41SChris Kirby 25223b2aab18SMatthew Ahrens /* 25233b2aab18SMatthew Ahrens * Swap deadlists. 25243b2aab18SMatthew Ahrens */ 25253b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 25263b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 25273b2aab18SMatthew Ahrens SWITCH64(origin_head->ds_phys->ds_deadlist_obj, 25283b2aab18SMatthew Ahrens clone->ds_phys->ds_deadlist_obj); 25293b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 25303b2aab18SMatthew Ahrens clone->ds_phys->ds_deadlist_obj); 25313b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 25323b2aab18SMatthew Ahrens origin_head->ds_phys->ds_deadlist_obj); 2533842727c2SChris Kirby 25343b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 2535842727c2SChris Kirby 25363b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 25373b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 2538842727c2SChris Kirby } 2539842727c2SChris Kirby 25403b2aab18SMatthew Ahrens /* 25413b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 25423b2aab18SMatthew Ahrens * return the name of that dataset. 25433b2aab18SMatthew Ahrens */ 2544a7f53a56SChris Kirby int 25453b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2546a7f53a56SChris Kirby { 25473b2aab18SMatthew Ahrens dsl_pool_t *dp; 25483b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2549a7f53a56SChris Kirby int error; 2550a7f53a56SChris Kirby 25513b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 25523b2aab18SMatthew Ahrens if (error != 0) 25533b2aab18SMatthew Ahrens return (error); 25543b2aab18SMatthew Ahrens 25553b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 25563b2aab18SMatthew Ahrens if (error == 0) { 25573b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 25583b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 25593b2aab18SMatthew Ahrens } 25603b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 2561a7f53a56SChris Kirby 2562a7f53a56SChris Kirby return (error); 2563a7f53a56SChris Kirby } 2564a7f53a56SChris Kirby 2565842727c2SChris Kirby int 25663b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 25673b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 2568842727c2SChris Kirby { 25693b2aab18SMatthew Ahrens int error = 0; 2570842727c2SChris Kirby 25713b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 2572842727c2SChris Kirby 25733b2aab18SMatthew Ahrens /* 25743b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 25753b2aab18SMatthew Ahrens * unconsumed refreservation space. 25763b2aab18SMatthew Ahrens */ 25773b2aab18SMatthew Ahrens *ref_rsrv = 0; 2578842727c2SChris Kirby 25793b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 25803b2aab18SMatthew Ahrens /* 25813b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 25823b2aab18SMatthew Ahrens */ 25833b2aab18SMatthew Ahrens if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) { 25843b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 25853b2aab18SMatthew Ahrens ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 25863b2aab18SMatthew Ahrens *used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes); 25873b2aab18SMatthew Ahrens *ref_rsrv = 25883b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 2589842727c2SChris Kirby } 2590842727c2SChris Kirby 25913b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 25923b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 25933b2aab18SMatthew Ahrens return (0); 2594842727c2SChris Kirby } 25953b2aab18SMatthew Ahrens /* 25963b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 25973b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 25983b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 25993b2aab18SMatthew Ahrens * may free up space for us). 26003b2aab18SMatthew Ahrens */ 26013b2aab18SMatthew Ahrens if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) { 26023b2aab18SMatthew Ahrens if (inflight > 0 || 26033b2aab18SMatthew Ahrens ds->ds_phys->ds_referenced_bytes < ds->ds_quota) 2604be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 26053b2aab18SMatthew Ahrens else 2606be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 2607842727c2SChris Kirby } 26083b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2609842727c2SChris Kirby 2610842727c2SChris Kirby return (error); 2611842727c2SChris Kirby } 2612842727c2SChris Kirby 26133b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 26143b2aab18SMatthew Ahrens const char *ddsqra_name; 26153b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 26163b2aab18SMatthew Ahrens uint64_t ddsqra_value; 26173b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 2618842727c2SChris Kirby 26193b2aab18SMatthew Ahrens 26203b2aab18SMatthew Ahrens /* ARGSUSED */ 2621842727c2SChris Kirby static int 26223b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 2623842727c2SChris Kirby { 26243b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 26253b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 26263b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2627842727c2SChris Kirby int error; 26283b2aab18SMatthew Ahrens uint64_t newval; 2629842727c2SChris Kirby 26303b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 2631be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2632842727c2SChris Kirby 26333b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 26343b2aab18SMatthew Ahrens if (error != 0) 26353b2aab18SMatthew Ahrens return (error); 26363b2aab18SMatthew Ahrens 26373b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 26383b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2639be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2640842727c2SChris Kirby } 2641842727c2SChris Kirby 26423b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 26433b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 26443b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 26453b2aab18SMatthew Ahrens if (error != 0) { 26463b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2647842727c2SChris Kirby return (error); 2648842727c2SChris Kirby } 2649842727c2SChris Kirby 26503b2aab18SMatthew Ahrens if (newval == 0) { 26513b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 26523b2aab18SMatthew Ahrens return (0); 26533b2aab18SMatthew Ahrens } 2654842727c2SChris Kirby 26553b2aab18SMatthew Ahrens if (newval < ds->ds_phys->ds_referenced_bytes || 26563b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 26573b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2658be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 26593b2aab18SMatthew Ahrens } 26603b2aab18SMatthew Ahrens 26613b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2662842727c2SChris Kirby return (0); 2663842727c2SChris Kirby } 2664842727c2SChris Kirby 26653b2aab18SMatthew Ahrens static void 26663b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 2667842727c2SChris Kirby { 26683b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 26693b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 26703b2aab18SMatthew Ahrens dsl_dataset_t *ds; 26713b2aab18SMatthew Ahrens uint64_t newval; 2672842727c2SChris Kirby 26733b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 2674842727c2SChris Kirby 26753b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 26763b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 26773b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 26783b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 2679842727c2SChris Kirby 26803b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 26813b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 2682842727c2SChris Kirby 26833b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 26843b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 26853b2aab18SMatthew Ahrens ds->ds_quota = newval; 2686842727c2SChris Kirby } 26873b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2688842727c2SChris Kirby } 2689842727c2SChris Kirby 26903b2aab18SMatthew Ahrens int 26913b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 26923b2aab18SMatthew Ahrens uint64_t refquota) 2693842727c2SChris Kirby { 26943b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 2695842727c2SChris Kirby 26963b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 26973b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 26983b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 26993b2aab18SMatthew Ahrens 27003b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 27013b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0)); 2702842727c2SChris Kirby } 2703842727c2SChris Kirby 2704842727c2SChris Kirby static int 27053b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 2706842727c2SChris Kirby { 27073b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 27083b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 2709842727c2SChris Kirby dsl_dataset_t *ds; 2710842727c2SChris Kirby int error; 27113b2aab18SMatthew Ahrens uint64_t newval, unique; 2712d7747cbcSChris Kirby 27133b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 2714be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2715842727c2SChris Kirby 27163b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 27173b2aab18SMatthew Ahrens if (error != 0) 2718842727c2SChris Kirby return (error); 2719842727c2SChris Kirby 27203b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 27213b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2722be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2723842727c2SChris Kirby } 2724842727c2SChris Kirby 27253b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 27263b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 27273b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 27283b2aab18SMatthew Ahrens if (error != 0) { 27293b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2730842727c2SChris Kirby return (error); 2731842727c2SChris Kirby } 2732842727c2SChris Kirby 27333b2aab18SMatthew Ahrens /* 27343b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 27353b2aab18SMatthew Ahrens * space estimates may be inaccurate. 27363b2aab18SMatthew Ahrens */ 27373b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 27383b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 27393b2aab18SMatthew Ahrens return (0); 2740842727c2SChris Kirby } 2741842727c2SChris Kirby 27423b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 27433b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 27443b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 27453b2aab18SMatthew Ahrens unique = ds->ds_phys->ds_unique_bytes; 27463b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2747842727c2SChris Kirby 27483b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 27493b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 27503b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 2751842727c2SChris Kirby 27523b2aab18SMatthew Ahrens if (delta > 27533b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 27543b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 27553b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2756be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 27573b2aab18SMatthew Ahrens } 2758842727c2SChris Kirby } 2759842727c2SChris Kirby 27603b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 27613b2aab18SMatthew Ahrens return (0); 2762842727c2SChris Kirby } 2763842727c2SChris Kirby 27643b2aab18SMatthew Ahrens void 27653b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 27663b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 2767ca45db41SChris Kirby { 27683b2aab18SMatthew Ahrens uint64_t newval; 27693b2aab18SMatthew Ahrens uint64_t unique; 27703b2aab18SMatthew Ahrens int64_t delta; 2771ca45db41SChris Kirby 27723b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 27733b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 2774ca45db41SChris Kirby 27753b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 27763b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 2777a7f53a56SChris Kirby 27783b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 27793b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 27803b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 27813b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 27823b2aab18SMatthew Ahrens unique = ds->ds_phys->ds_unique_bytes; 27833b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 27843b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 27853b2aab18SMatthew Ahrens ds->ds_reserved = newval; 27863b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2787a7f53a56SChris Kirby 27883b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 27893b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 2790ca45db41SChris Kirby } 2791ca45db41SChris Kirby 27923b2aab18SMatthew Ahrens static void 27933b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 2794842727c2SChris Kirby { 27953b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 27963b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 2797842727c2SChris Kirby dsl_dataset_t *ds; 2798842727c2SChris Kirby 27993b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 28003b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 28013b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 2802842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 2803842727c2SChris Kirby } 2804503ad85cSMatthew Ahrens 2805503ad85cSMatthew Ahrens int 28063b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 28073b2aab18SMatthew Ahrens uint64_t refreservation) 2808503ad85cSMatthew Ahrens { 28093b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 2810503ad85cSMatthew Ahrens 28113b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 28123b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 28133b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 28143b2aab18SMatthew Ahrens 28153b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 28163b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 0)); 2817503ad85cSMatthew Ahrens } 281819b94df9SMatthew Ahrens 281919b94df9SMatthew Ahrens /* 282019b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 282119b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 282219b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 282319b94df9SMatthew Ahrens * fail and return EINVAL. 282419b94df9SMatthew Ahrens * 282519b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 282619b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 282719b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 282819b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 282919b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 283019b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 283119b94df9SMatthew Ahrens * 283219b94df9SMatthew Ahrens * space freed [---------------------] 283319b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 283419b94df9SMatthew Ahrens * oldsnap new 283519b94df9SMatthew Ahrens */ 283619b94df9SMatthew Ahrens int 283719b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 283819b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 283919b94df9SMatthew Ahrens { 284019b94df9SMatthew Ahrens int err = 0; 284119b94df9SMatthew Ahrens uint64_t snapobj; 284219b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 284319b94df9SMatthew Ahrens 28443b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 28453b2aab18SMatthew Ahrens 284619b94df9SMatthew Ahrens *usedp = 0; 2847ad135b5dSChristopher Siden *usedp += new->ds_phys->ds_referenced_bytes; 2848ad135b5dSChristopher Siden *usedp -= oldsnap->ds_phys->ds_referenced_bytes; 284919b94df9SMatthew Ahrens 285019b94df9SMatthew Ahrens *compp = 0; 285119b94df9SMatthew Ahrens *compp += new->ds_phys->ds_compressed_bytes; 285219b94df9SMatthew Ahrens *compp -= oldsnap->ds_phys->ds_compressed_bytes; 285319b94df9SMatthew Ahrens 285419b94df9SMatthew Ahrens *uncompp = 0; 285519b94df9SMatthew Ahrens *uncompp += new->ds_phys->ds_uncompressed_bytes; 285619b94df9SMatthew Ahrens *uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes; 285719b94df9SMatthew Ahrens 285819b94df9SMatthew Ahrens snapobj = new->ds_object; 285919b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 286019b94df9SMatthew Ahrens dsl_dataset_t *snap; 286119b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 286219b94df9SMatthew Ahrens 2863ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 2864ad135b5dSChristopher Siden snap = new; 2865ad135b5dSChristopher Siden } else { 2866ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 2867ad135b5dSChristopher Siden if (err != 0) 2868ad135b5dSChristopher Siden break; 2869ad135b5dSChristopher Siden } 287019b94df9SMatthew Ahrens 287119b94df9SMatthew Ahrens if (snap->ds_phys->ds_prev_snap_txg == 287219b94df9SMatthew Ahrens oldsnap->ds_phys->ds_creation_txg) { 287319b94df9SMatthew Ahrens /* 287419b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 287519b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 287619b94df9SMatthew Ahrens * which is more efficient (especially for old-format 287719b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 287819b94df9SMatthew Ahrens * doesn't have enough information to make this 287919b94df9SMatthew Ahrens * optimization itself. 288019b94df9SMatthew Ahrens */ 288119b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 288219b94df9SMatthew Ahrens &used, &comp, &uncomp); 288319b94df9SMatthew Ahrens } else { 288419b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 288519b94df9SMatthew Ahrens 0, oldsnap->ds_phys->ds_creation_txg, 288619b94df9SMatthew Ahrens &used, &comp, &uncomp); 288719b94df9SMatthew Ahrens } 288819b94df9SMatthew Ahrens *usedp += used; 288919b94df9SMatthew Ahrens *compp += comp; 289019b94df9SMatthew Ahrens *uncompp += uncomp; 289119b94df9SMatthew Ahrens 289219b94df9SMatthew Ahrens /* 289319b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 289419b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 289519b94df9SMatthew Ahrens * was not a snapshot of/before new. 289619b94df9SMatthew Ahrens */ 289719b94df9SMatthew Ahrens snapobj = snap->ds_phys->ds_prev_snap_obj; 2898ad135b5dSChristopher Siden if (snap != new) 2899ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 290019b94df9SMatthew Ahrens if (snapobj == 0) { 2901be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 290219b94df9SMatthew Ahrens break; 290319b94df9SMatthew Ahrens } 290419b94df9SMatthew Ahrens 290519b94df9SMatthew Ahrens } 290619b94df9SMatthew Ahrens return (err); 290719b94df9SMatthew Ahrens } 290819b94df9SMatthew Ahrens 290919b94df9SMatthew Ahrens /* 291019b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 291119b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 291219b94df9SMatthew Ahrens * 291319b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 291419b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 291519b94df9SMatthew Ahrens * firstsnap lastsnap 291619b94df9SMatthew Ahrens * 291719b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 291819b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 291919b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 292019b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 292119b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 292219b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 292319b94df9SMatthew Ahrens */ 292419b94df9SMatthew Ahrens int 292519b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 292619b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 292719b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 292819b94df9SMatthew Ahrens { 292919b94df9SMatthew Ahrens int err = 0; 293019b94df9SMatthew Ahrens uint64_t snapobj; 293119b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 293219b94df9SMatthew Ahrens 293319b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(firstsnap)); 293419b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(lastsnap)); 293519b94df9SMatthew Ahrens 293619b94df9SMatthew Ahrens /* 293719b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 293819b94df9SMatthew Ahrens * is before lastsnap. 293919b94df9SMatthew Ahrens */ 294019b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 294119b94df9SMatthew Ahrens firstsnap->ds_phys->ds_creation_txg > 294219b94df9SMatthew Ahrens lastsnap->ds_phys->ds_creation_txg) 2943be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 294419b94df9SMatthew Ahrens 294519b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 294619b94df9SMatthew Ahrens 294719b94df9SMatthew Ahrens snapobj = lastsnap->ds_phys->ds_next_snap_obj; 294819b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 294919b94df9SMatthew Ahrens dsl_dataset_t *ds; 295019b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 295119b94df9SMatthew Ahrens 295219b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 295319b94df9SMatthew Ahrens if (err != 0) 295419b94df9SMatthew Ahrens break; 295519b94df9SMatthew Ahrens 295619b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 295719b94df9SMatthew Ahrens firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX, 295819b94df9SMatthew Ahrens &used, &comp, &uncomp); 295919b94df9SMatthew Ahrens *usedp += used; 296019b94df9SMatthew Ahrens *compp += comp; 296119b94df9SMatthew Ahrens *uncompp += uncomp; 296219b94df9SMatthew Ahrens 296319b94df9SMatthew Ahrens snapobj = ds->ds_phys->ds_prev_snap_obj; 296419b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 296519b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 296619b94df9SMatthew Ahrens } 296719b94df9SMatthew Ahrens return (err); 296819b94df9SMatthew Ahrens } 29693b2aab18SMatthew Ahrens 29703b2aab18SMatthew Ahrens /* 29713b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 29723b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 29733b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 29743b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 29753b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 2976*78f17100SMatthew Ahrens * 2977*78f17100SMatthew Ahrens * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 29783b2aab18SMatthew Ahrens */ 29793b2aab18SMatthew Ahrens boolean_t 2980*78f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 2981*78f17100SMatthew Ahrens uint64_t earlier_txg) 29823b2aab18SMatthew Ahrens { 29833b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 29843b2aab18SMatthew Ahrens int error; 29853b2aab18SMatthew Ahrens boolean_t ret; 29863b2aab18SMatthew Ahrens 29873b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 2988*78f17100SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(earlier) || earlier_txg != 0); 2989*78f17100SMatthew Ahrens 2990*78f17100SMatthew Ahrens if (earlier_txg == 0) 2991*78f17100SMatthew Ahrens earlier_txg = earlier->ds_phys->ds_creation_txg; 29923b2aab18SMatthew Ahrens 2993*78f17100SMatthew Ahrens if (dsl_dataset_is_snapshot(later) && 2994*78f17100SMatthew Ahrens earlier_txg >= later->ds_phys->ds_creation_txg) 29953b2aab18SMatthew Ahrens return (B_FALSE); 29963b2aab18SMatthew Ahrens 29973b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 29983b2aab18SMatthew Ahrens return (B_TRUE); 29993b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 30003b2aab18SMatthew Ahrens return (B_FALSE); 30013b2aab18SMatthew Ahrens 30023b2aab18SMatthew Ahrens if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object) 30033b2aab18SMatthew Ahrens return (B_TRUE); 30043b2aab18SMatthew Ahrens dsl_dataset_t *origin; 30053b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 30063b2aab18SMatthew Ahrens later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin); 30073b2aab18SMatthew Ahrens if (error != 0) 30083b2aab18SMatthew Ahrens return (B_FALSE); 3009*78f17100SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 30103b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 30113b2aab18SMatthew Ahrens return (ret); 30123b2aab18SMatthew Ahrens } 30132acef22dSMatthew Ahrens 30142acef22dSMatthew Ahrens 30152acef22dSMatthew Ahrens void 30162acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 30172acef22dSMatthew Ahrens { 30182acef22dSMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 30192acef22dSMatthew Ahrens dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 30202acef22dSMatthew Ahrens } 3021