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. 23ca0cc391SMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 24a2afb611SJerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved. 2503d1795fSAlexander Stetsenko * Copyright (c) 2014 RackTop Systems. 26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <sys/dmu_objset.h> 30fa9e4066Sahrens #include <sys/dsl_dataset.h> 31fa9e4066Sahrens #include <sys/dsl_dir.h> 3299653d4eSeschrock #include <sys/dsl_prop.h> 331d452cf5Sahrens #include <sys/dsl_synctask.h> 34fa9e4066Sahrens #include <sys/dmu_traverse.h> 354e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 36fa9e4066Sahrens #include <sys/dmu_tx.h> 37fa9e4066Sahrens #include <sys/arc.h> 38fa9e4066Sahrens #include <sys/zio.h> 39fa9e4066Sahrens #include <sys/zap.h> 40ad135b5dSChristopher Siden #include <sys/zfeature.h> 41fa9e4066Sahrens #include <sys/unique.h> 42fa9e4066Sahrens #include <sys/zfs_context.h> 43cdf5b4caSmmusante #include <sys/zfs_ioctl.h> 44ecd6cf80Smarks #include <sys/spa.h> 45088f3894Sahrens #include <sys/zfs_znode.h> 46c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 47842727c2SChris Kirby #include <sys/zvol.h> 483f9d6ad7SLin Ling #include <sys/dsl_scan.h> 49cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h> 503b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 513b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 5278f17100SMatthew Ahrens #include <sys/dsl_bookmark.h> 5345818ee1SMatthew Ahrens #include <sys/dmu_send.h> 5445818ee1SMatthew Ahrens #include <sys/zio_checksum.h> 55e1930233Sbonwick 56b5152584SMatthew Ahrens /* 57b5152584SMatthew Ahrens * The SPA supports block sizes up to 16MB. However, very large blocks 58b5152584SMatthew Ahrens * can have an impact on i/o latency (e.g. tying up a spinning disk for 59b5152584SMatthew Ahrens * ~300ms), and also potentially on the memory allocator. Therefore, 60b5152584SMatthew Ahrens * we do not allow the recordsize to be set larger than zfs_max_recordsize 61b5152584SMatthew Ahrens * (default 1MB). Larger blocks can be created by changing this tunable, 62b5152584SMatthew Ahrens * and pools with larger blocks can always be imported and used, regardless 63b5152584SMatthew Ahrens * of this setting. 64b5152584SMatthew Ahrens */ 65b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024; 66b5152584SMatthew Ahrens 67cde58dbcSMatthew Ahrens #define SWITCH64(x, y) \ 68cde58dbcSMatthew Ahrens { \ 69cde58dbcSMatthew Ahrens uint64_t __tmp = (x); \ 70cde58dbcSMatthew Ahrens (x) = (y); \ 71cde58dbcSMatthew Ahrens (y) = __tmp; \ 72cde58dbcSMatthew Ahrens } 73cde58dbcSMatthew Ahrens 7455434c77Sek #define DS_REF_MAX (1ULL << 62) 75fa9e4066Sahrens 76c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds); 77c1379625SJustin T. Gibbs 78a9799022Sck /* 79a9799022Sck * Figure out how much of this delta should be propogated to the dsl_dir 80a9799022Sck * layer. If there's a refreservation, that space has already been 81a9799022Sck * partially accounted for in our ancestors. 82a9799022Sck */ 83a9799022Sck static int64_t 84a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta) 85a9799022Sck { 86c1379625SJustin T. Gibbs dsl_dataset_phys_t *ds_phys; 87a9799022Sck uint64_t old_bytes, new_bytes; 88a9799022Sck 89a9799022Sck if (ds->ds_reserved == 0) 90a9799022Sck return (delta); 91a9799022Sck 92c1379625SJustin T. Gibbs ds_phys = dsl_dataset_phys(ds); 93c1379625SJustin T. Gibbs old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved); 94c1379625SJustin T. Gibbs new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 95a9799022Sck 96a9799022Sck ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 97a9799022Sck return (new_bytes - old_bytes); 98a9799022Sck } 99fa9e4066Sahrens 100fa9e4066Sahrens void 101b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 102fa9e4066Sahrens { 103b24ab676SJeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 104fa9e4066Sahrens int compressed = BP_GET_PSIZE(bp); 105fa9e4066Sahrens int uncompressed = BP_GET_UCSIZE(bp); 106a9799022Sck int64_t delta; 107fa9e4066Sahrens 1083f9d6ad7SLin Ling dprintf_bp(bp, "ds=%p", ds); 109fa9e4066Sahrens 110fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 111fa9e4066Sahrens /* It could have been compressed away to nothing */ 112fa9e4066Sahrens if (BP_IS_HOLE(bp)) 113fa9e4066Sahrens return; 114fa9e4066Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 115ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 116fa9e4066Sahrens if (ds == NULL) { 117ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 118ce636f8bSMatthew Ahrens used, compressed, uncompressed); 119fa9e4066Sahrens return; 120fa9e4066Sahrens } 1213f9d6ad7SLin Ling 122b62969f8SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 123fa9e4066Sahrens mutex_enter(&ds->ds_lock); 124a9799022Sck delta = parent_delta(ds, used); 125c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes += used; 126c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes += compressed; 127c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed; 128c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes += used; 12945818ee1SMatthew Ahrens 130ca0cc391SMatthew Ahrens if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) { 131ca0cc391SMatthew Ahrens ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] = 132ca0cc391SMatthew Ahrens B_TRUE; 133ca0cc391SMatthew Ahrens } 13445818ee1SMatthew Ahrens 13545818ee1SMatthew Ahrens spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp)); 13645818ee1SMatthew Ahrens if (f != SPA_FEATURE_NONE) 13745818ee1SMatthew Ahrens ds->ds_feature_activation_needed[f] = B_TRUE; 13845818ee1SMatthew Ahrens 139fa9e4066Sahrens mutex_exit(&ds->ds_lock); 14074e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 14174e7dc98SMatthew Ahrens compressed, uncompressed, tx); 14274e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used - delta, 14374e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 144fa9e4066Sahrens } 145fa9e4066Sahrens 146cdb0ab79Smaybee int 147b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 148b24ab676SJeff Bonwick boolean_t async) 149fa9e4066Sahrens { 15043466aaeSMax Grossman int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 15143466aaeSMax Grossman int compressed = BP_GET_PSIZE(bp); 15243466aaeSMax Grossman int uncompressed = BP_GET_UCSIZE(bp); 15343466aaeSMax Grossman 154fa9e4066Sahrens if (BP_IS_HOLE(bp)) 155cdb0ab79Smaybee return (0); 156fa9e4066Sahrens 157b24ab676SJeff Bonwick ASSERT(dmu_tx_is_syncing(tx)); 158b24ab676SJeff Bonwick ASSERT(bp->blk_birth <= tx->tx_txg); 159b24ab676SJeff Bonwick 160fa9e4066Sahrens if (ds == NULL) { 161b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 162ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 163ce636f8bSMatthew Ahrens -used, -compressed, -uncompressed); 164cdb0ab79Smaybee return (used); 165fa9e4066Sahrens } 166fa9e4066Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 167fa9e4066Sahrens 168bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 169fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 170fa9e4066Sahrens 171c1379625SJustin T. Gibbs if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 172a9799022Sck int64_t delta; 173c717a561Smaybee 1743f9d6ad7SLin Ling dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 175b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 176fa9e4066Sahrens 177fa9e4066Sahrens mutex_enter(&ds->ds_lock); 178c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used || 179a9799022Sck !DS_UNIQUE_IS_ACCURATE(ds)); 180a9799022Sck delta = parent_delta(ds, -used); 181c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes -= used; 182fa9e4066Sahrens mutex_exit(&ds->ds_lock); 18374e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 184a9799022Sck delta, -compressed, -uncompressed, tx); 18574e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, -used - delta, 18674e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 187fa9e4066Sahrens } else { 188fa9e4066Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 189b24ab676SJeff Bonwick if (async) { 190b24ab676SJeff Bonwick /* 191b24ab676SJeff Bonwick * We are here as part of zio's write done callback, 192b24ab676SJeff Bonwick * which means we're a zio interrupt thread. We can't 193cde58dbcSMatthew Ahrens * call dsl_deadlist_insert() now because it may block 194b24ab676SJeff Bonwick * waiting for I/O. Instead, put bp on the deferred 195b24ab676SJeff Bonwick * queue and let dsl_pool_sync() finish the job. 196b24ab676SJeff Bonwick */ 197cde58dbcSMatthew Ahrens bplist_append(&ds->ds_pending_deadlist, bp); 198b24ab676SJeff Bonwick } else { 199cde58dbcSMatthew Ahrens dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 200b24ab676SJeff Bonwick } 201a4611edeSahrens ASSERT3U(ds->ds_prev->ds_object, ==, 202c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj); 203c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0); 204fa9e4066Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 205c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 206a4611edeSahrens ds->ds_object && bp->blk_birth > 207c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) { 208a4611edeSahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 209a4611edeSahrens mutex_enter(&ds->ds_prev->ds_lock); 210c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used; 211a4611edeSahrens mutex_exit(&ds->ds_prev->ds_lock); 212fa9e4066Sahrens } 2133f9d6ad7SLin Ling if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 21474e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used, 21574e7dc98SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 21674e7dc98SMatthew Ahrens } 217fa9e4066Sahrens } 218fa9e4066Sahrens mutex_enter(&ds->ds_lock); 219c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used); 220c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes -= used; 221c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed); 222c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed; 223c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed); 224c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed; 225fa9e4066Sahrens mutex_exit(&ds->ds_lock); 226cdb0ab79Smaybee 227cdb0ab79Smaybee return (used); 228fa9e4066Sahrens } 229fa9e4066Sahrens 230ea8dc4b6Seschrock uint64_t 231ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 232fa9e4066Sahrens { 233a2eea2e1Sahrens uint64_t trysnap = 0; 234a2eea2e1Sahrens 235fa9e4066Sahrens if (ds == NULL) 236ea8dc4b6Seschrock return (0); 237fa9e4066Sahrens /* 238fa9e4066Sahrens * The snapshot creation could fail, but that would cause an 239fa9e4066Sahrens * incorrect FALSE return, which would only result in an 240fa9e4066Sahrens * overestimation of the amount of space that an operation would 241fa9e4066Sahrens * consume, which is OK. 242fa9e4066Sahrens * 243fa9e4066Sahrens * There's also a small window where we could miss a pending 244fa9e4066Sahrens * snapshot, because we could set the sync task in the quiescing 245fa9e4066Sahrens * phase. So this should only be used as a guess. 246fa9e4066Sahrens */ 247a2eea2e1Sahrens if (ds->ds_trysnap_txg > 248a2eea2e1Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 249a2eea2e1Sahrens trysnap = ds->ds_trysnap_txg; 250c1379625SJustin T. Gibbs return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap)); 251ea8dc4b6Seschrock } 252ea8dc4b6Seschrock 2533d692628SSanjeev Bagewadi boolean_t 254c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp, 255c7cd2421SGeorge Wilson uint64_t blk_birth) 256ea8dc4b6Seschrock { 25743466aaeSMax Grossman if (blk_birth <= dsl_dataset_prev_snap_txg(ds) || 25843466aaeSMax Grossman (bp != NULL && BP_IS_HOLE(bp))) 259c7cd2421SGeorge Wilson return (B_FALSE); 260c7cd2421SGeorge Wilson 261837b568bSGeorge Wilson ddt_prefetch(dsl_dataset_get_spa(ds), bp); 262c7cd2421SGeorge Wilson 263c7cd2421SGeorge Wilson return (B_TRUE); 264fa9e4066Sahrens } 265fa9e4066Sahrens 266fa9e4066Sahrens static void 267bc9014e6SJustin Gibbs dsl_dataset_evict(void *dbu) 268fa9e4066Sahrens { 269bc9014e6SJustin Gibbs dsl_dataset_t *ds = dbu; 270fa9e4066Sahrens 2713b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == NULL); 272fa9e4066Sahrens 273bc9014e6SJustin Gibbs ds->ds_dbuf = NULL; 274bc9014e6SJustin Gibbs 27591ebeef5Sahrens unique_remove(ds->ds_fsid_guid); 276fa9e4066Sahrens 277503ad85cSMatthew Ahrens if (ds->ds_objset != NULL) 278503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 279fa9e4066Sahrens 280fa9e4066Sahrens if (ds->ds_prev) { 2813b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 282fa9e4066Sahrens ds->ds_prev = NULL; 283fa9e4066Sahrens } 284fa9e4066Sahrens 285cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 286bc9014e6SJustin Gibbs if (ds->ds_deadlist.dl_os != NULL) 287cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 288745cd3c5Smaybee if (ds->ds_dir) 289bc9014e6SJustin Gibbs dsl_dir_async_rele(ds->ds_dir, ds); 290fa9e4066Sahrens 29191ebeef5Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 292fa9e4066Sahrens 293*03bad06fSJustin Gibbs list_destroy(&ds->ds_prop_cbs); 2945ad82045Snd mutex_destroy(&ds->ds_lock); 29591ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 296d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 2973b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 2985ad82045Snd 299fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 300fa9e4066Sahrens } 301fa9e4066Sahrens 3023b2aab18SMatthew Ahrens int 303fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 304fa9e4066Sahrens { 305fa9e4066Sahrens dsl_dataset_phys_t *headphys; 306fa9e4066Sahrens int err; 307fa9e4066Sahrens dmu_buf_t *headdbuf; 308fa9e4066Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 309fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 310fa9e4066Sahrens 311fa9e4066Sahrens if (ds->ds_snapname[0]) 312ea8dc4b6Seschrock return (0); 313c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) 314ea8dc4b6Seschrock return (0); 315fa9e4066Sahrens 316c1379625SJustin T. Gibbs err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, 317ea8dc4b6Seschrock FTAG, &headdbuf); 3183b2aab18SMatthew Ahrens if (err != 0) 319ea8dc4b6Seschrock return (err); 320fa9e4066Sahrens headphys = headdbuf->db_data; 321fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 322e7437265Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 323ea8dc4b6Seschrock dmu_buf_rele(headdbuf, FTAG); 324ea8dc4b6Seschrock return (err); 325fa9e4066Sahrens } 326fa9e4066Sahrens 3273b2aab18SMatthew Ahrens int 328745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 329ab04eb8eStimh { 330745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 331c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 332ab04eb8eStimh matchtype_t mt; 333ab04eb8eStimh int err; 334ab04eb8eStimh 335c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 336ab04eb8eStimh mt = MT_FIRST; 337ab04eb8eStimh else 338ab04eb8eStimh mt = MT_EXACT; 339ab04eb8eStimh 340745cd3c5Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 341ab04eb8eStimh value, mt, NULL, 0, NULL); 342ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 343745cd3c5Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 344ab04eb8eStimh return (err); 345ab04eb8eStimh } 346ab04eb8eStimh 3473b2aab18SMatthew Ahrens int 348a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx, 349a2afb611SJerry Jelinek boolean_t adj_cnt) 350ab04eb8eStimh { 351745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 352c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 353ab04eb8eStimh matchtype_t mt; 354ab04eb8eStimh int err; 355ab04eb8eStimh 35671eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 35771eb0538SChris Kirby 358c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 359ab04eb8eStimh mt = MT_FIRST; 360ab04eb8eStimh else 361ab04eb8eStimh mt = MT_EXACT; 362ab04eb8eStimh 363745cd3c5Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 364ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 365745cd3c5Smaybee err = zap_remove(mos, snapobj, name, tx); 366a2afb611SJerry Jelinek 367a2afb611SJerry Jelinek if (err == 0 && adj_cnt) 368a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, -1, 369a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 370a2afb611SJerry Jelinek 371ab04eb8eStimh return (err); 372ab04eb8eStimh } 373ab04eb8eStimh 374e57a022bSJustin T. Gibbs boolean_t 375e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag) 376e57a022bSJustin T. Gibbs { 3779d47dec0SJustin T. Gibbs dmu_buf_t *dbuf = ds->ds_dbuf; 3789d47dec0SJustin T. Gibbs boolean_t result = B_FALSE; 3799d47dec0SJustin T. Gibbs 3809d47dec0SJustin T. Gibbs if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset, 3819d47dec0SJustin T. Gibbs ds->ds_object, DMU_BONUS_BLKID, tag)) { 3829d47dec0SJustin T. Gibbs 3839d47dec0SJustin T. Gibbs if (ds == dmu_buf_get_user(dbuf)) 3849d47dec0SJustin T. Gibbs result = B_TRUE; 3859d47dec0SJustin T. Gibbs else 3869d47dec0SJustin T. Gibbs dmu_buf_rele(dbuf, tag); 3879d47dec0SJustin T. Gibbs } 3889d47dec0SJustin T. Gibbs 3899d47dec0SJustin T. Gibbs return (result); 390e57a022bSJustin T. Gibbs } 391e57a022bSJustin T. Gibbs 3923b2aab18SMatthew Ahrens int 3933b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 394745cd3c5Smaybee dsl_dataset_t **dsp) 395fa9e4066Sahrens { 396fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 397fa9e4066Sahrens dmu_buf_t *dbuf; 398fa9e4066Sahrens dsl_dataset_t *ds; 399ea8dc4b6Seschrock int err; 400a7f53a56SChris Kirby dmu_object_info_t doi; 401fa9e4066Sahrens 4023b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 403fa9e4066Sahrens 404ea8dc4b6Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 4053b2aab18SMatthew Ahrens if (err != 0) 406ea8dc4b6Seschrock return (err); 407a7f53a56SChris Kirby 408a7f53a56SChris Kirby /* Make sure dsobj has the correct object type. */ 409a7f53a56SChris Kirby dmu_object_info_from_db(dbuf, &doi); 4102acef22dSMatthew Ahrens if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 411b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 412be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 413b287be1bSWill Andrews } 414a7f53a56SChris Kirby 415fa9e4066Sahrens ds = dmu_buf_get_user(dbuf); 416fa9e4066Sahrens if (ds == NULL) { 417d5285caeSGeorge Wilson dsl_dataset_t *winner = NULL; 418fa9e4066Sahrens 419fa9e4066Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 420fa9e4066Sahrens ds->ds_dbuf = dbuf; 421fa9e4066Sahrens ds->ds_object = dsobj; 422bc9014e6SJustin Gibbs ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0; 423fa9e4066Sahrens 4245ad82045Snd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 42591ebeef5Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 4264e3c9f44SBill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 4273b2aab18SMatthew Ahrens refcount_create(&ds->ds_longholds); 4285ad82045Snd 429cde58dbcSMatthew Ahrens bplist_create(&ds->ds_pending_deadlist); 430cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, 431c1379625SJustin T. Gibbs mos, dsl_dataset_phys(ds)->ds_deadlist_obj); 432cde58dbcSMatthew Ahrens 4334e3c9f44SBill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 4344e3c9f44SBill Pijewski offsetof(dmu_sendarg_t, dsa_link)); 4354e3c9f44SBill Pijewski 436*03bad06fSJustin Gibbs list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t), 437*03bad06fSJustin Gibbs offsetof(dsl_prop_cb_record_t, cbr_ds_node)); 438*03bad06fSJustin Gibbs 439b5152584SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 440ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 441ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 442ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) 443ca0cc391SMatthew Ahrens continue; 444ca0cc391SMatthew Ahrens err = zap_contains(mos, dsobj, 445ca0cc391SMatthew Ahrens spa_feature_table[f].fi_guid); 446ca0cc391SMatthew Ahrens if (err == 0) { 447ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 448ca0cc391SMatthew Ahrens } else { 449ca0cc391SMatthew Ahrens ASSERT3U(err, ==, ENOENT); 450ca0cc391SMatthew Ahrens err = 0; 451ca0cc391SMatthew Ahrens } 452e1f3c208SJustin T. Gibbs } 453b5152584SMatthew Ahrens } 454b5152584SMatthew Ahrens 455ca0cc391SMatthew Ahrens err = dsl_dir_hold_obj(dp, 456ca0cc391SMatthew Ahrens dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir); 4573b2aab18SMatthew Ahrens if (err != 0) { 4585ad82045Snd mutex_destroy(&ds->ds_lock); 45991ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 460d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 4613b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 462cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 463cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 464ea8dc4b6Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 465ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 466ea8dc4b6Seschrock return (err); 467ea8dc4b6Seschrock } 468fa9e4066Sahrens 469bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) { 470fa9e4066Sahrens ds->ds_snapname[0] = '\0'; 471c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 4723b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 473c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, 474745cd3c5Smaybee ds, &ds->ds_prev); 475fa9e4066Sahrens } 47678f17100SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 47778f17100SMatthew Ahrens int zaperr = zap_lookup(mos, ds->ds_object, 47878f17100SMatthew Ahrens DS_FIELD_BOOKMARK_NAMES, 47978f17100SMatthew Ahrens sizeof (ds->ds_bookmarks), 1, 48078f17100SMatthew Ahrens &ds->ds_bookmarks); 48178f17100SMatthew Ahrens if (zaperr != ENOENT) 48278f17100SMatthew Ahrens VERIFY0(zaperr); 48378f17100SMatthew Ahrens } 484842727c2SChris Kirby } else { 485842727c2SChris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 486842727c2SChris Kirby err = dsl_dataset_get_snapname(ds); 487c1379625SJustin T. Gibbs if (err == 0 && 488c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj != 0) { 489842727c2SChris Kirby err = zap_count( 490842727c2SChris Kirby ds->ds_dir->dd_pool->dp_meta_objset, 491c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj, 492842727c2SChris Kirby &ds->ds_userrefs); 493842727c2SChris Kirby } 494fa9e4066Sahrens } 495fa9e4066Sahrens 496bc9014e6SJustin Gibbs if (err == 0 && !ds->ds_is_snapshot) { 4973b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4983b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 4993b2aab18SMatthew Ahrens &ds->ds_reserved); 500cb625fb5Sck if (err == 0) { 5013b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 5023b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 5033b2aab18SMatthew Ahrens &ds->ds_quota); 504cb625fb5Sck } 505cb625fb5Sck } else { 506cb625fb5Sck ds->ds_reserved = ds->ds_quota = 0; 507cb625fb5Sck } 508cb625fb5Sck 509bc9014e6SJustin Gibbs dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict, &ds->ds_dbuf); 510bc9014e6SJustin Gibbs if (err == 0) 511bc9014e6SJustin Gibbs winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu); 512bc9014e6SJustin Gibbs 513bc9014e6SJustin Gibbs if (err != 0 || winner != NULL) { 514cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 515cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 516745cd3c5Smaybee if (ds->ds_prev) 5173b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 5183b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 5195ad82045Snd mutex_destroy(&ds->ds_lock); 52091ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 521d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 5223b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 523fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 5243b2aab18SMatthew Ahrens if (err != 0) { 525ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 526ea8dc4b6Seschrock return (err); 527ea8dc4b6Seschrock } 528fa9e4066Sahrens ds = winner; 529fa9e4066Sahrens } else { 53091ebeef5Sahrens ds->ds_fsid_guid = 531c1379625SJustin T. Gibbs unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid); 532fa9e4066Sahrens } 533fa9e4066Sahrens } 534fa9e4066Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 535c1379625SJustin T. Gibbs ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data); 536c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 || 537afc6333aSahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 53884db2a68Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 539ea8dc4b6Seschrock *dsp = ds; 540ea8dc4b6Seschrock return (0); 541fa9e4066Sahrens } 542fa9e4066Sahrens 543745cd3c5Smaybee int 5443b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name, 545503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 546fa9e4066Sahrens { 547fa9e4066Sahrens dsl_dir_t *dd; 548745cd3c5Smaybee const char *snapname; 549fa9e4066Sahrens uint64_t obj; 550fa9e4066Sahrens int err = 0; 551a2cdcdd2SPaul Dagnelie dsl_dataset_t *ds; 552fa9e4066Sahrens 5533b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 5543b2aab18SMatthew Ahrens if (err != 0) 555ea8dc4b6Seschrock return (err); 556fa9e4066Sahrens 5573b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 558c1379625SJustin T. Gibbs obj = dsl_dir_phys(dd)->dd_head_dataset_obj; 5593b2aab18SMatthew Ahrens if (obj != 0) 560a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 561745cd3c5Smaybee else 562be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 563fa9e4066Sahrens 564745cd3c5Smaybee /* we may be looking for a snapshot */ 565745cd3c5Smaybee if (err == 0 && snapname != NULL) { 566a2cdcdd2SPaul Dagnelie dsl_dataset_t *snap_ds; 567fa9e4066Sahrens 568745cd3c5Smaybee if (*snapname++ != '@') { 569a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 5703b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 571be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 572fa9e4066Sahrens } 573fa9e4066Sahrens 574745cd3c5Smaybee dprintf("looking for snapshot '%s'\n", snapname); 575a2cdcdd2SPaul Dagnelie err = dsl_dataset_snap_lookup(ds, snapname, &obj); 576745cd3c5Smaybee if (err == 0) 577a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds); 578a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 579745cd3c5Smaybee 5803b2aab18SMatthew Ahrens if (err == 0) { 581a2cdcdd2SPaul Dagnelie mutex_enter(&snap_ds->ds_lock); 582a2cdcdd2SPaul Dagnelie if (snap_ds->ds_snapname[0] == 0) 583a2cdcdd2SPaul Dagnelie (void) strlcpy(snap_ds->ds_snapname, snapname, 584a2cdcdd2SPaul Dagnelie sizeof (snap_ds->ds_snapname)); 585a2cdcdd2SPaul Dagnelie mutex_exit(&snap_ds->ds_lock); 586a2cdcdd2SPaul Dagnelie ds = snap_ds; 587fa9e4066Sahrens } 588fa9e4066Sahrens } 589a2cdcdd2SPaul Dagnelie if (err == 0) 590a2cdcdd2SPaul Dagnelie *dsp = ds; 5913b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 592fa9e4066Sahrens return (err); 593fa9e4066Sahrens } 594fa9e4066Sahrens 595fa9e4066Sahrens int 5963b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 5973b2aab18SMatthew Ahrens void *tag, dsl_dataset_t **dsp) 5983b2aab18SMatthew Ahrens { 5993b2aab18SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 6003b2aab18SMatthew Ahrens if (err != 0) 6013b2aab18SMatthew Ahrens return (err); 6023b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 6033b2aab18SMatthew Ahrens dsl_dataset_rele(*dsp, tag); 6043b2aab18SMatthew Ahrens *dsp = NULL; 605be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 6063b2aab18SMatthew Ahrens } 6073b2aab18SMatthew Ahrens return (0); 6083b2aab18SMatthew Ahrens } 6093b2aab18SMatthew Ahrens 6103b2aab18SMatthew Ahrens int 6113b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name, 612503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 613fa9e4066Sahrens { 6143b2aab18SMatthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp); 6153b2aab18SMatthew Ahrens if (err != 0) 616745cd3c5Smaybee return (err); 6173b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 618503ad85cSMatthew Ahrens dsl_dataset_rele(*dsp, tag); 619be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 620745cd3c5Smaybee } 621745cd3c5Smaybee return (0); 622fa9e4066Sahrens } 623fa9e4066Sahrens 6243b2aab18SMatthew Ahrens /* 6253b2aab18SMatthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long 6263b2aab18SMatthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold 6273b2aab18SMatthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()). 6283b2aab18SMatthew Ahrens * 6293b2aab18SMatthew Ahrens * The dataset and pool must be held when this function is called. After it 6303b2aab18SMatthew Ahrens * is called, the pool hold may be released while the dataset is still held 6313b2aab18SMatthew Ahrens * and accessed. 6323b2aab18SMatthew Ahrens */ 6333b2aab18SMatthew Ahrens void 6343b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 6353b2aab18SMatthew Ahrens { 6363b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 6373b2aab18SMatthew Ahrens (void) refcount_add(&ds->ds_longholds, tag); 6383b2aab18SMatthew Ahrens } 6393b2aab18SMatthew Ahrens 6403b2aab18SMatthew Ahrens void 6413b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 6423b2aab18SMatthew Ahrens { 6433b2aab18SMatthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag); 6443b2aab18SMatthew Ahrens } 6453b2aab18SMatthew Ahrens 6463b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */ 6473b2aab18SMatthew Ahrens boolean_t 6483b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds) 6493b2aab18SMatthew Ahrens { 6503b2aab18SMatthew Ahrens return (!refcount_is_zero(&ds->ds_longholds)); 6513b2aab18SMatthew Ahrens } 6523b2aab18SMatthew Ahrens 653fa9e4066Sahrens void 654fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 655fa9e4066Sahrens { 656fa9e4066Sahrens if (ds == NULL) { 657fa9e4066Sahrens (void) strcpy(name, "mos"); 658fa9e4066Sahrens } else { 659fa9e4066Sahrens dsl_dir_name(ds->ds_dir, name); 6603b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 661fa9e4066Sahrens if (ds->ds_snapname[0]) { 662fa9e4066Sahrens (void) strcat(name, "@"); 663745cd3c5Smaybee /* 664745cd3c5Smaybee * We use a "recursive" mutex so that we 665745cd3c5Smaybee * can call dprintf_ds() with ds_lock held. 666745cd3c5Smaybee */ 667fa9e4066Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 668fa9e4066Sahrens mutex_enter(&ds->ds_lock); 669fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 670fa9e4066Sahrens mutex_exit(&ds->ds_lock); 671fa9e4066Sahrens } else { 672fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 673fa9e4066Sahrens } 674fa9e4066Sahrens } 675fa9e4066Sahrens } 676fa9e4066Sahrens } 677fa9e4066Sahrens 6783cb34c60Sahrens void 679745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6803cb34c60Sahrens { 6813b2aab18SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag); 682745cd3c5Smaybee } 683745cd3c5Smaybee 684745cd3c5Smaybee void 685503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 686745cd3c5Smaybee { 687d808a4fcSJustin T. Gibbs ASSERT3P(ds->ds_owner, ==, tag); 688d808a4fcSJustin T. Gibbs ASSERT(ds->ds_dbuf != NULL); 689745cd3c5Smaybee 6903cb34c60Sahrens mutex_enter(&ds->ds_lock); 691745cd3c5Smaybee ds->ds_owner = NULL; 6923cb34c60Sahrens mutex_exit(&ds->ds_lock); 6933b2aab18SMatthew Ahrens dsl_dataset_long_rele(ds, tag); 694d808a4fcSJustin T. Gibbs dsl_dataset_rele(ds, tag); 6953cb34c60Sahrens } 6963cb34c60Sahrens 6973cb34c60Sahrens boolean_t 6983b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 6993cb34c60Sahrens { 700745cd3c5Smaybee boolean_t gotit = FALSE; 701745cd3c5Smaybee 7023cb34c60Sahrens mutex_enter(&ds->ds_lock); 7033b2aab18SMatthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 704503ad85cSMatthew Ahrens ds->ds_owner = tag; 7053b2aab18SMatthew Ahrens dsl_dataset_long_hold(ds, tag); 706745cd3c5Smaybee gotit = TRUE; 7073cb34c60Sahrens } 7083cb34c60Sahrens mutex_exit(&ds->ds_lock); 709745cd3c5Smaybee return (gotit); 710745cd3c5Smaybee } 711745cd3c5Smaybee 712ca0cc391SMatthew Ahrens static void 713ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 714ca0cc391SMatthew Ahrens { 715ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 716ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 717ca0cc391SMatthew Ahrens uint64_t zero = 0; 718ca0cc391SMatthew Ahrens 719ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 720ca0cc391SMatthew Ahrens 721ca0cc391SMatthew Ahrens spa_feature_incr(spa, f, tx); 722ca0cc391SMatthew Ahrens dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 723ca0cc391SMatthew Ahrens 724ca0cc391SMatthew Ahrens VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid, 725ca0cc391SMatthew Ahrens sizeof (zero), 1, &zero, tx)); 726ca0cc391SMatthew Ahrens } 727ca0cc391SMatthew Ahrens 728ca0cc391SMatthew Ahrens void 729ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 730ca0cc391SMatthew Ahrens { 731ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 732ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 733ca0cc391SMatthew Ahrens 734ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 735ca0cc391SMatthew Ahrens 736ca0cc391SMatthew Ahrens VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx)); 737ca0cc391SMatthew Ahrens spa_feature_decr(spa, f, tx); 738ca0cc391SMatthew Ahrens } 739ca0cc391SMatthew Ahrens 7401d452cf5Sahrens uint64_t 741088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 742ab04eb8eStimh uint64_t flags, dmu_tx_t *tx) 743fa9e4066Sahrens { 7443cb34c60Sahrens dsl_pool_t *dp = dd->dd_pool; 745fa9e4066Sahrens dmu_buf_t *dbuf; 746fa9e4066Sahrens dsl_dataset_phys_t *dsphys; 7473cb34c60Sahrens uint64_t dsobj; 748fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 749fa9e4066Sahrens 750088f3894Sahrens if (origin == NULL) 751088f3894Sahrens origin = dp->dp_origin_snap; 752088f3894Sahrens 7533cb34c60Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 754c1379625SJustin T. Gibbs ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0); 755fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 756c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 757fa9e4066Sahrens 7581649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 7591649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7603b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 761fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 762fa9e4066Sahrens dsphys = dbuf->db_data; 763745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 764fa9e4066Sahrens dsphys->ds_dir_obj = dd->dd_object; 765ab04eb8eStimh dsphys->ds_flags = flags; 766fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 767fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 768fa9e4066Sahrens sizeof (dsphys->ds_guid)); 769fa9e4066Sahrens dsphys->ds_snapnames_zapobj = 770ab04eb8eStimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 771ab04eb8eStimh DMU_OT_NONE, 0, tx); 772fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 773088f3894Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 774a9799022Sck 775cde58dbcSMatthew Ahrens if (origin == NULL) { 776cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 777cde58dbcSMatthew Ahrens } else { 7783b2aab18SMatthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */ 779cde58dbcSMatthew Ahrens 7803cb34c60Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 781fa9e4066Sahrens dsphys->ds_prev_snap_txg = 782c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_creation_txg; 783ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = 784c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_referenced_bytes; 785fa9e4066Sahrens dsphys->ds_compressed_bytes = 786c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_compressed_bytes; 787fa9e4066Sahrens dsphys->ds_uncompressed_bytes = 788c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_uncompressed_bytes; 789c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp; 79042fcb65eSMatthew Ahrens 79142fcb65eSMatthew Ahrens /* 79242fcb65eSMatthew Ahrens * Inherit flags that describe the dataset's contents 79342fcb65eSMatthew Ahrens * (INCONSISTENT) or properties (Case Insensitive). 79442fcb65eSMatthew Ahrens */ 795c1379625SJustin T. Gibbs dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags & 79642fcb65eSMatthew Ahrens (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET); 797fa9e4066Sahrens 798ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 799ca0cc391SMatthew Ahrens if (origin->ds_feature_inuse[f]) 800ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 801ca0cc391SMatthew Ahrens } 802b5152584SMatthew Ahrens 8033cb34c60Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 804c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_num_children++; 805fa9e4066Sahrens 8063b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 807c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj, 808c1379625SJustin T. Gibbs FTAG, &ohds)); 809cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 810cde58dbcSMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 811cde58dbcSMatthew Ahrens dsl_dataset_rele(ohds, FTAG); 812cde58dbcSMatthew Ahrens 813088f3894Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 814c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) { 815c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj = 816088f3894Sahrens zap_create(mos, 817088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 818088f3894Sahrens } 8193b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 820c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj, 821c1379625SJustin T. Gibbs dsobj, tx)); 822088f3894Sahrens } 823088f3894Sahrens 824fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 825c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object; 826cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 827c1379625SJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 828cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 829c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones = 830cde58dbcSMatthew Ahrens zap_create(mos, 831cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 832cde58dbcSMatthew Ahrens } 8333b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 834c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones, 835c1379625SJustin T. Gibbs dsobj, tx)); 836cde58dbcSMatthew Ahrens } 837fa9e4066Sahrens } 838ab04eb8eStimh 839ab04eb8eStimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 840ab04eb8eStimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 841ab04eb8eStimh 842ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 843fa9e4066Sahrens 844fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 845c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj; 8463cb34c60Sahrens 8473cb34c60Sahrens return (dsobj); 8483cb34c60Sahrens } 8493cb34c60Sahrens 8503b2aab18SMatthew Ahrens static void 8513b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 8523b2aab18SMatthew Ahrens { 8533b2aab18SMatthew Ahrens objset_t *os; 8543b2aab18SMatthew Ahrens 8553b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 8563b2aab18SMatthew Ahrens bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 8573b2aab18SMatthew Ahrens dsl_dataset_dirty(ds, tx); 8583b2aab18SMatthew Ahrens } 8593b2aab18SMatthew Ahrens 8603cb34c60Sahrens uint64_t 861ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 862ab04eb8eStimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8633cb34c60Sahrens { 8643cb34c60Sahrens dsl_pool_t *dp = pdd->dd_pool; 8653cb34c60Sahrens uint64_t dsobj, ddobj; 8663cb34c60Sahrens dsl_dir_t *dd; 8673cb34c60Sahrens 8683b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 8693cb34c60Sahrens ASSERT(lastname[0] != '@'); 8703cb34c60Sahrens 871088f3894Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8723b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 8733cb34c60Sahrens 8743b2aab18SMatthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, 8753b2aab18SMatthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx); 8763cb34c60Sahrens 8773cb34c60Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8783cb34c60Sahrens 879a2afb611SJerry Jelinek /* 880a2afb611SJerry Jelinek * Since we're creating a new node we know it's a leaf, so we can 881a2afb611SJerry Jelinek * initialize the counts if the limit feature is active. 882a2afb611SJerry Jelinek */ 883a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 884a2afb611SJerry Jelinek uint64_t cnt = 0; 885a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 886a2afb611SJerry Jelinek 887a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx); 888a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 889a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 890a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 891a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 892a2afb611SJerry Jelinek } 893a2afb611SJerry Jelinek 8943b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 895fa9e4066Sahrens 896feaa74e4SMark Maybee /* 897feaa74e4SMark Maybee * If we are creating a clone, make sure we zero out any stale 898feaa74e4SMark Maybee * data from the origin snapshots zil header. 899feaa74e4SMark Maybee */ 9003b2aab18SMatthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 901feaa74e4SMark Maybee dsl_dataset_t *ds; 902feaa74e4SMark Maybee 9033b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 9043b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 905feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 906feaa74e4SMark Maybee } 907feaa74e4SMark Maybee 9081d452cf5Sahrens return (dsobj); 909fa9e4066Sahrens } 910fa9e4066Sahrens 9111d452cf5Sahrens /* 9123b2aab18SMatthew Ahrens * The unique space in the head dataset can be calculated by subtracting 9133b2aab18SMatthew Ahrens * the space used in the most recent snapshot, that is still being used 9143b2aab18SMatthew Ahrens * in this file system, from the space currently in use. To figure out 9153b2aab18SMatthew Ahrens * the space in the most recent snapshot still in use, we need to take 9163b2aab18SMatthew Ahrens * the total space used in the snapshot and subtract out the space that 9173b2aab18SMatthew Ahrens * has been freed up since the snapshot was taken. 9181d452cf5Sahrens */ 9193b2aab18SMatthew Ahrens void 9203b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 9211d452cf5Sahrens { 9223b2aab18SMatthew Ahrens uint64_t mrs_used; 9233b2aab18SMatthew Ahrens uint64_t dlused, dlcomp, dluncomp; 9241d452cf5Sahrens 925bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 9261d452cf5Sahrens 927c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) 928c1379625SJustin T. Gibbs mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes; 9293b2aab18SMatthew Ahrens else 9303b2aab18SMatthew Ahrens mrs_used = 0; 931842727c2SChris Kirby 9323b2aab18SMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 933fa9e4066Sahrens 9343b2aab18SMatthew Ahrens ASSERT3U(dlused, <=, mrs_used); 935c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 936c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused); 93719b94df9SMatthew Ahrens 9383b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 9393b2aab18SMatthew Ahrens SPA_VERSION_UNIQUE_ACCURATE) 940c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 941fa9e4066Sahrens } 942fa9e4066Sahrens 9433b2aab18SMatthew Ahrens void 9443b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 9453b2aab18SMatthew Ahrens dmu_tx_t *tx) 946842727c2SChris Kirby { 9473b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 9483b2aab18SMatthew Ahrens uint64_t count; 9493b2aab18SMatthew Ahrens int err; 9503b2aab18SMatthew Ahrens 951c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2); 952c1379625SJustin T. Gibbs err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 953c1379625SJustin T. Gibbs obj, tx); 9543b2aab18SMatthew Ahrens /* 9553b2aab18SMatthew Ahrens * The err should not be ENOENT, but a bug in a previous version 9563b2aab18SMatthew Ahrens * of the code could cause upgrade_clones_cb() to not set 9573b2aab18SMatthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry. 9583b2aab18SMatthew Ahrens * If we knew that the pool was created after 9593b2aab18SMatthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 9603b2aab18SMatthew Ahrens * ENOENT. However, at least we can check that we don't have 9613b2aab18SMatthew Ahrens * too many entries in the next_clones_obj even after failing to 9623b2aab18SMatthew Ahrens * remove this one. 9633b2aab18SMatthew Ahrens */ 9643b2aab18SMatthew Ahrens if (err != ENOENT) 9653b2aab18SMatthew Ahrens VERIFY0(err); 966c1379625SJustin T. Gibbs ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 9673b2aab18SMatthew Ahrens &count)); 968c1379625SJustin T. Gibbs ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2); 9693b2aab18SMatthew Ahrens } 970842727c2SChris Kirby 971842727c2SChris Kirby 9723b2aab18SMatthew Ahrens blkptr_t * 9733b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds) 9743b2aab18SMatthew Ahrens { 975c1379625SJustin T. Gibbs return (&dsl_dataset_phys(ds)->ds_bp); 976842727c2SChris Kirby } 977842727c2SChris Kirby 9783b2aab18SMatthew Ahrens void 9793b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 980842727c2SChris Kirby { 9813b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 9823b2aab18SMatthew Ahrens /* If it's the meta-objset, set dp_meta_rootbp */ 9833b2aab18SMatthew Ahrens if (ds == NULL) { 9843b2aab18SMatthew Ahrens tx->tx_pool->dp_meta_rootbp = *bp; 9853b2aab18SMatthew Ahrens } else { 9863b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 987c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_bp = *bp; 988842727c2SChris Kirby } 9893b2aab18SMatthew Ahrens } 990842727c2SChris Kirby 9913b2aab18SMatthew Ahrens spa_t * 9923b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 9933b2aab18SMatthew Ahrens { 9943b2aab18SMatthew Ahrens return (ds->ds_dir->dd_pool->dp_spa); 995842727c2SChris Kirby } 996842727c2SChris Kirby 9973b2aab18SMatthew Ahrens void 9983b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 999fa9e4066Sahrens { 10003b2aab18SMatthew Ahrens dsl_pool_t *dp; 1001842727c2SChris Kirby 10023b2aab18SMatthew Ahrens if (ds == NULL) /* this is the meta-objset */ 10033b2aab18SMatthew Ahrens return; 10041d452cf5Sahrens 10053b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1006fa9e4066Sahrens 1007c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) 10083b2aab18SMatthew Ahrens panic("dirtying snapshot!"); 1009fa9e4066Sahrens 10103b2aab18SMatthew Ahrens dp = ds->ds_dir->dd_pool; 1011ce636f8bSMatthew Ahrens 10123b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 10133b2aab18SMatthew Ahrens /* up the hold count until we can be written out */ 10143b2aab18SMatthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 10153b2aab18SMatthew Ahrens } 10163b2aab18SMatthew Ahrens } 1017fa9e4066Sahrens 10182e2c1355SMatthew Ahrens boolean_t 10192e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds) 10202e2c1355SMatthew Ahrens { 10212e2c1355SMatthew Ahrens for (int t = 0; t < TXG_SIZE; t++) { 10222e2c1355SMatthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 10232e2c1355SMatthew Ahrens ds, t)) 10242e2c1355SMatthew Ahrens return (B_TRUE); 10252e2c1355SMatthew Ahrens } 10262e2c1355SMatthew Ahrens return (B_FALSE); 10272e2c1355SMatthew Ahrens } 10282e2c1355SMatthew Ahrens 1029fa9e4066Sahrens static int 10303b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1031fa9e4066Sahrens { 10323b2aab18SMatthew Ahrens uint64_t asize; 1033fa9e4066Sahrens 10343b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 103588b7b0f2SMatthew Ahrens return (0); 1036fa9e4066Sahrens 1037e1930233Sbonwick /* 10383b2aab18SMatthew Ahrens * If there's an fs-only reservation, any blocks that might become 10393b2aab18SMatthew Ahrens * owned by the snapshot dataset must be accommodated by space 10403b2aab18SMatthew Ahrens * outside of the reservation. 1041e1930233Sbonwick */ 10423b2aab18SMatthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 1043c1379625SJustin T. Gibbs asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved); 10443b2aab18SMatthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 1045be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1046e1930233Sbonwick 10473cb34c60Sahrens /* 10483b2aab18SMatthew Ahrens * Propagate any reserved space for this snapshot to other 10493b2aab18SMatthew Ahrens * snapshot checks in this sync group. 10503cb34c60Sahrens */ 10513b2aab18SMatthew Ahrens if (asize > 0) 10523b2aab18SMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx); 10533cb34c60Sahrens 1054e1930233Sbonwick return (0); 1055e1930233Sbonwick } 1056e1930233Sbonwick 10573b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg { 10583b2aab18SMatthew Ahrens nvlist_t *ddsa_snaps; 10593b2aab18SMatthew Ahrens nvlist_t *ddsa_props; 10603b2aab18SMatthew Ahrens nvlist_t *ddsa_errors; 1061a2afb611SJerry Jelinek cred_t *ddsa_cr; 10623b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t; 1063842727c2SChris Kirby 10643cb34c60Sahrens int 10653b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 1066a2afb611SJerry Jelinek dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr) 10671d452cf5Sahrens { 10683b2aab18SMatthew Ahrens int error; 10693b2aab18SMatthew Ahrens uint64_t value; 1070fa9e4066Sahrens 10713b2aab18SMatthew Ahrens ds->ds_trysnap_txg = tx->tx_txg; 1072745cd3c5Smaybee 10733b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 1074842727c2SChris Kirby return (0); 1075fa9e4066Sahrens 1076fa9e4066Sahrens /* 10773b2aab18SMatthew Ahrens * We don't allow multiple snapshots of the same txg. If there 10783b2aab18SMatthew Ahrens * is already one, try again. 1079fa9e4066Sahrens */ 1080c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) 1081be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 1082fa9e4066Sahrens 1083fa9e4066Sahrens /* 10843b2aab18SMatthew Ahrens * Check for conflicting snapshot name. 1085fa9e4066Sahrens */ 10863b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value); 10873b2aab18SMatthew Ahrens if (error == 0) 1088be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 10893b2aab18SMatthew Ahrens if (error != ENOENT) 10903b2aab18SMatthew Ahrens return (error); 1091842727c2SChris Kirby 1092ca48f36fSKeith M Wesolowski /* 1093ca48f36fSKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as 1094ca48f36fSKeith M Wesolowski * those into which we are currently receiving. However, if we are 1095ca48f36fSKeith M Wesolowski * creating this snapshot as part of a receive, this check will be 1096ca48f36fSKeith M Wesolowski * executed atomically with respect to the completion of the receive 1097ca48f36fSKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 1098ca48f36fSKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in 1099ca48f36fSKeith M Wesolowski * dmu_recv_end_sync(). 1100ca48f36fSKeith M Wesolowski */ 1101ca48f36fSKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds)) 1102ca48f36fSKeith M Wesolowski return (SET_ERROR(EBUSY)); 1103ca48f36fSKeith M Wesolowski 1104a2afb611SJerry Jelinek /* 1105a2afb611SJerry Jelinek * Skip the check for temporary snapshots or if we have already checked 1106a2afb611SJerry Jelinek * the counts in dsl_dataset_snapshot_check. This means we really only 1107a2afb611SJerry Jelinek * check the count here when we're receiving a stream. 1108a2afb611SJerry Jelinek */ 1109a2afb611SJerry Jelinek if (cnt != 0 && cr != NULL) { 1110a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1111a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr); 1112a2afb611SJerry Jelinek if (error != 0) 1113a2afb611SJerry Jelinek return (error); 1114a2afb611SJerry Jelinek } 1115a2afb611SJerry Jelinek 11163b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 11173b2aab18SMatthew Ahrens if (error != 0) 11183b2aab18SMatthew Ahrens return (error); 1119842727c2SChris Kirby 11201d452cf5Sahrens return (0); 11211d452cf5Sahrens } 11221d452cf5Sahrens 11233b2aab18SMatthew Ahrens static int 11243b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 1125745cd3c5Smaybee { 11263b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 11273b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11283b2aab18SMatthew Ahrens nvpair_t *pair; 11293b2aab18SMatthew Ahrens int rv = 0; 11303b2aab18SMatthew Ahrens 1131a2afb611SJerry Jelinek /* 1132a2afb611SJerry Jelinek * Pre-compute how many total new snapshots will be created for each 1133a2afb611SJerry Jelinek * level in the tree and below. This is needed for validating the 1134a2afb611SJerry Jelinek * snapshot limit when either taking a recursive snapshot or when 1135a2afb611SJerry Jelinek * taking multiple snapshots. 1136a2afb611SJerry Jelinek * 1137a2afb611SJerry Jelinek * The problem is that the counts are not actually adjusted when 1138a2afb611SJerry Jelinek * we are checking, only when we finally sync. For a single snapshot, 1139a2afb611SJerry Jelinek * this is easy, the count will increase by 1 at each node up the tree, 1140a2afb611SJerry Jelinek * but its more complicated for the recursive/multiple snapshot case. 1141a2afb611SJerry Jelinek * 1142a2afb611SJerry Jelinek * The dsl_fs_ss_limit_check function does recursively check the count 1143a2afb611SJerry Jelinek * at each level up the tree but since it is validating each snapshot 1144a2afb611SJerry Jelinek * independently we need to be sure that we are validating the complete 1145a2afb611SJerry Jelinek * count for the entire set of snapshots. We do this by rolling up the 1146a2afb611SJerry Jelinek * counts for each component of the name into an nvlist and then 1147a2afb611SJerry Jelinek * checking each of those cases with the aggregated count. 1148a2afb611SJerry Jelinek * 1149a2afb611SJerry Jelinek * This approach properly handles not only the recursive snapshot 1150a2afb611SJerry Jelinek * case (where we get all of those on the ddsa_snaps list) but also 1151a2afb611SJerry Jelinek * the sibling case (e.g. snapshot a/b and a/c so that we will also 1152a2afb611SJerry Jelinek * validate the limit on 'a' using a count of 2). 1153a2afb611SJerry Jelinek * 1154a2afb611SJerry Jelinek * We validate the snapshot names in the third loop and only report 1155a2afb611SJerry Jelinek * name errors once. 1156a2afb611SJerry Jelinek */ 1157a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) { 1158a2afb611SJerry Jelinek nvlist_t *cnt_track = NULL; 1159a2afb611SJerry Jelinek cnt_track = fnvlist_alloc(); 1160a2afb611SJerry Jelinek 1161a2afb611SJerry Jelinek /* Rollup aggregated counts into the cnt_track list */ 1162a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1163a2afb611SJerry Jelinek pair != NULL; 1164a2afb611SJerry Jelinek pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1165a2afb611SJerry Jelinek char *pdelim; 1166a2afb611SJerry Jelinek uint64_t val; 1167a2afb611SJerry Jelinek char nm[MAXPATHLEN]; 1168a2afb611SJerry Jelinek 1169a2afb611SJerry Jelinek (void) strlcpy(nm, nvpair_name(pair), sizeof (nm)); 1170a2afb611SJerry Jelinek pdelim = strchr(nm, '@'); 1171a2afb611SJerry Jelinek if (pdelim == NULL) 1172a2afb611SJerry Jelinek continue; 1173a2afb611SJerry Jelinek *pdelim = '\0'; 1174a2afb611SJerry Jelinek 1175a2afb611SJerry Jelinek do { 1176a2afb611SJerry Jelinek if (nvlist_lookup_uint64(cnt_track, nm, 1177a2afb611SJerry Jelinek &val) == 0) { 1178a2afb611SJerry Jelinek /* update existing entry */ 1179a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1180a2afb611SJerry Jelinek val + 1); 1181a2afb611SJerry Jelinek } else { 1182a2afb611SJerry Jelinek /* add to list */ 1183a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1); 1184a2afb611SJerry Jelinek } 1185a2afb611SJerry Jelinek 1186a2afb611SJerry Jelinek pdelim = strrchr(nm, '/'); 1187a2afb611SJerry Jelinek if (pdelim != NULL) 1188a2afb611SJerry Jelinek *pdelim = '\0'; 1189a2afb611SJerry Jelinek } while (pdelim != NULL); 1190a2afb611SJerry Jelinek } 1191a2afb611SJerry Jelinek 1192a2afb611SJerry Jelinek /* Check aggregated counts at each level */ 1193a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(cnt_track, NULL); 1194a2afb611SJerry Jelinek pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) { 1195a2afb611SJerry Jelinek int error = 0; 1196a2afb611SJerry Jelinek char *name; 1197a2afb611SJerry Jelinek uint64_t cnt = 0; 1198a2afb611SJerry Jelinek dsl_dataset_t *ds; 1199a2afb611SJerry Jelinek 1200a2afb611SJerry Jelinek name = nvpair_name(pair); 1201a2afb611SJerry Jelinek cnt = fnvpair_value_uint64(pair); 1202a2afb611SJerry Jelinek ASSERT(cnt > 0); 1203a2afb611SJerry Jelinek 1204a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, name, FTAG, &ds); 1205a2afb611SJerry Jelinek if (error == 0) { 1206a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1207a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, 1208a2afb611SJerry Jelinek ddsa->ddsa_cr); 1209a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 1210a2afb611SJerry Jelinek } 1211a2afb611SJerry Jelinek 1212a2afb611SJerry Jelinek if (error != 0) { 1213a2afb611SJerry Jelinek if (ddsa->ddsa_errors != NULL) 1214a2afb611SJerry Jelinek fnvlist_add_int32(ddsa->ddsa_errors, 1215a2afb611SJerry Jelinek name, error); 1216a2afb611SJerry Jelinek rv = error; 1217a2afb611SJerry Jelinek /* only report one error for this check */ 1218a2afb611SJerry Jelinek break; 1219a2afb611SJerry Jelinek } 1220a2afb611SJerry Jelinek } 1221a2afb611SJerry Jelinek nvlist_free(cnt_track); 1222a2afb611SJerry Jelinek } 1223a2afb611SJerry Jelinek 12243b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 12253b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 12263b2aab18SMatthew Ahrens int error = 0; 12273b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12283b2aab18SMatthew Ahrens char *name, *atp; 12293b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 12303b2aab18SMatthew Ahrens 12313b2aab18SMatthew Ahrens name = nvpair_name(pair); 12323b2aab18SMatthew Ahrens if (strlen(name) >= MAXNAMELEN) 1233be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 12343b2aab18SMatthew Ahrens if (error == 0) { 12353b2aab18SMatthew Ahrens atp = strchr(name, '@'); 12363b2aab18SMatthew Ahrens if (atp == NULL) 1237be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 12383b2aab18SMatthew Ahrens if (error == 0) 12393b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 12403b2aab18SMatthew Ahrens } 12413b2aab18SMatthew Ahrens if (error == 0) 12423b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 12433b2aab18SMatthew Ahrens if (error == 0) { 1244a2afb611SJerry Jelinek /* passing 0/NULL skips dsl_fs_ss_limit_check */ 12453b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 1246a2afb611SJerry Jelinek atp + 1, tx, B_FALSE, 0, NULL); 12473b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12483b2aab18SMatthew Ahrens } 1249745cd3c5Smaybee 12503b2aab18SMatthew Ahrens if (error != 0) { 12513b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 12523b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 12533b2aab18SMatthew Ahrens name, error); 12543b2aab18SMatthew Ahrens } 12553b2aab18SMatthew Ahrens rv = error; 12563b2aab18SMatthew Ahrens } 12573b2aab18SMatthew Ahrens } 1258a2afb611SJerry Jelinek 12593b2aab18SMatthew Ahrens return (rv); 1260745cd3c5Smaybee } 1261745cd3c5Smaybee 12623b2aab18SMatthew Ahrens void 12633b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 12643b2aab18SMatthew Ahrens dmu_tx_t *tx) 1265745cd3c5Smaybee { 12663b2aab18SMatthew Ahrens static zil_header_t zero_zil; 1267745cd3c5Smaybee 12683b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 12693b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 12703b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 12713b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 12723b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 12733b2aab18SMatthew Ahrens objset_t *os; 1274745cd3c5Smaybee 12753b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1276c33e334fSMatthew Ahrens 1277c33e334fSMatthew Ahrens /* 12783b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 12793b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1280c33e334fSMatthew Ahrens */ 12813b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 12823b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 12833b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 12843b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1285c33e334fSMatthew Ahrens 1286a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx); 1287cde58dbcSMatthew Ahrens 1288cde58dbcSMatthew Ahrens /* 12893b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1290088f3894Sahrens */ 1291088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1292088f3894Sahrens crtxg = 1; 1293088f3894Sahrens else 1294088f3894Sahrens crtxg = tx->tx_txg; 1295088f3894Sahrens 12961649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 12971649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 12983b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1299fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1300fa9e4066Sahrens dsphys = dbuf->db_data; 1301745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 13021d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1303fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1304fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1305fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1306c1379625SJustin T. Gibbs dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 1307c1379625SJustin T. Gibbs dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 1308fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1309fa9e4066Sahrens dsphys->ds_num_children = 1; 1310fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1311088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1312c1379625SJustin T. Gibbs dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 1313c1379625SJustin T. Gibbs dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes; 1314c1379625SJustin T. Gibbs dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes; 1315c1379625SJustin T. Gibbs dsphys->ds_uncompressed_bytes = 1316c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes; 1317c1379625SJustin T. Gibbs dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags; 1318c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp; 1319ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1320fa9e4066Sahrens 1321ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1322ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1323ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 1324ca0cc391SMatthew Ahrens } 1325b5152584SMatthew Ahrens 1326c1379625SJustin T. Gibbs ASSERT3U(ds->ds_prev != 0, ==, 1327c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 13281d452cf5Sahrens if (ds->ds_prev) { 1329088f3894Sahrens uint64_t next_clones_obj = 1330c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj; 1331c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1332fa9e4066Sahrens ds->ds_object || 1333c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1); 1334c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1335c1379625SJustin T. Gibbs ds->ds_object) { 13361d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1337c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 1338c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_creation_txg); 1339c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj; 1340088f3894Sahrens } else if (next_clones_obj != 0) { 13413b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1342c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 13433b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1344088f3894Sahrens next_clones_obj, dsobj, tx)); 1345fa9e4066Sahrens } 1346fa9e4066Sahrens } 1347fa9e4066Sahrens 1348a9799022Sck /* 1349a9799022Sck * If we have a reference-reservation on this dataset, we will 1350a9799022Sck * need to increase the amount of refreservation being charged 1351a9799022Sck * since our unique space is going to zero. 1352a9799022Sck */ 1353a9799022Sck if (ds->ds_reserved) { 13543f9d6ad7SLin Ling int64_t delta; 13553f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 1356c1379625SJustin T. Gibbs delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, 1357c1379625SJustin T. Gibbs ds->ds_reserved); 135874e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 13593f9d6ad7SLin Ling delta, 0, 0, tx); 1360a9799022Sck } 1361a9799022Sck 1362fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1363c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 1364c1379625SJustin T. Gibbs dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX, 1365c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, tx); 1366cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1367c1379625SJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos, 1368c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj); 1369cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1370c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 1371cde58dbcSMatthew Ahrens 1372c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg); 1373c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj; 1374c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg; 1375c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 0; 1376a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1377c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1378fa9e4066Sahrens 1379c1379625SJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj, 13803b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1381fa9e4066Sahrens 1382fa9e4066Sahrens if (ds->ds_prev) 13833b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 13843b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1385c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev)); 1386ecd6cf80Smarks 13873f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1388088f3894Sahrens 138971eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 139071eb0538SChris Kirby 13914445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1392fa9e4066Sahrens } 1393fa9e4066Sahrens 13943b2aab18SMatthew Ahrens static void 13953b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1396fa9e4066Sahrens { 13973b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 13983b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 13993b2aab18SMatthew Ahrens nvpair_t *pair; 140091ebeef5Sahrens 14013b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 14023b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 14033b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14043b2aab18SMatthew Ahrens char *name, *atp; 14053b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 14063b2aab18SMatthew Ahrens 14073b2aab18SMatthew Ahrens name = nvpair_name(pair); 14083b2aab18SMatthew Ahrens atp = strchr(name, '@'); 14093b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 14103b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 14113b2aab18SMatthew Ahrens 14123b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 14133b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 14143b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 14153b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 14163b2aab18SMatthew Ahrens } 14173b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14183b2aab18SMatthew Ahrens } 1419fa9e4066Sahrens } 1420fa9e4066Sahrens 14213b2aab18SMatthew Ahrens /* 14223b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 14233b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 14243b2aab18SMatthew Ahrens */ 14253b2aab18SMatthew Ahrens int 14263b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 142719b94df9SMatthew Ahrens { 14283b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 14293b2aab18SMatthew Ahrens nvpair_t *pair; 14303b2aab18SMatthew Ahrens boolean_t needsuspend; 14313b2aab18SMatthew Ahrens int error; 14323b2aab18SMatthew Ahrens spa_t *spa; 14333b2aab18SMatthew Ahrens char *firstname; 14343b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 143519b94df9SMatthew Ahrens 14363b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 14373b2aab18SMatthew Ahrens if (pair == NULL) 14383b2aab18SMatthew Ahrens return (0); 14393b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 14403b2aab18SMatthew Ahrens 14413b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 14423b2aab18SMatthew Ahrens if (error != 0) 14433b2aab18SMatthew Ahrens return (error); 14443b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 14453b2aab18SMatthew Ahrens spa_close(spa, FTAG); 14463b2aab18SMatthew Ahrens 14473b2aab18SMatthew Ahrens if (needsuspend) { 14483b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 14493b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 14503b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 14513b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 14523b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 14533b2aab18SMatthew Ahrens char *atp; 14543b2aab18SMatthew Ahrens void *cookie; 14553b2aab18SMatthew Ahrens 14563b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 14573b2aab18SMatthew Ahrens if (atp == NULL) { 1458be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 14593b2aab18SMatthew Ahrens break; 14603b2aab18SMatthew Ahrens } 14613b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 14623b2aab18SMatthew Ahrens 14633b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 14643b2aab18SMatthew Ahrens if (error != 0) 14653b2aab18SMatthew Ahrens break; 14663b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 14673b2aab18SMatthew Ahrens (uintptr_t)cookie); 14683b2aab18SMatthew Ahrens } 14693b2aab18SMatthew Ahrens } 14703b2aab18SMatthew Ahrens 14713b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 14723b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 14733b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 1474a2afb611SJerry Jelinek ddsa.ddsa_cr = CRED(); 14753b2aab18SMatthew Ahrens 14763b2aab18SMatthew Ahrens if (error == 0) { 14773b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 14783b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 14797d46dc6cSMatthew Ahrens fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL); 14803b2aab18SMatthew Ahrens } 14813b2aab18SMatthew Ahrens 14823b2aab18SMatthew Ahrens if (suspended != NULL) { 14833b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 14843b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 14853b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 14863b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 14873b2aab18SMatthew Ahrens } 14883b2aab18SMatthew Ahrens fnvlist_free(suspended); 14893b2aab18SMatthew Ahrens } 14903b2aab18SMatthew Ahrens 14913b2aab18SMatthew Ahrens return (error); 14923b2aab18SMatthew Ahrens } 14933b2aab18SMatthew Ahrens 14943b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 14953b2aab18SMatthew Ahrens const char *ddsta_fsname; 14963b2aab18SMatthew Ahrens const char *ddsta_snapname; 14973b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 14983b2aab18SMatthew Ahrens const char *ddsta_htag; 14993b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 15003b2aab18SMatthew Ahrens 15013b2aab18SMatthew Ahrens static int 15023b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 15033b2aab18SMatthew Ahrens { 15043b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15053b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15063b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15073b2aab18SMatthew Ahrens int error; 15083b2aab18SMatthew Ahrens 15093b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 15103b2aab18SMatthew Ahrens if (error != 0) 15113b2aab18SMatthew Ahrens return (error); 15123b2aab18SMatthew Ahrens 1513a2afb611SJerry Jelinek /* NULL cred means no limit check for tmp snapshot */ 1514ca48f36fSKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1515a2afb611SJerry Jelinek tx, B_FALSE, 0, NULL); 15163b2aab18SMatthew Ahrens if (error != 0) { 15173b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15183b2aab18SMatthew Ahrens return (error); 15193b2aab18SMatthew Ahrens } 15203b2aab18SMatthew Ahrens 15213b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 15223b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1523be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 15243b2aab18SMatthew Ahrens } 15253b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 15263b2aab18SMatthew Ahrens B_TRUE, tx); 15273b2aab18SMatthew Ahrens if (error != 0) { 15283b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15293b2aab18SMatthew Ahrens return (error); 15303b2aab18SMatthew Ahrens } 15313b2aab18SMatthew Ahrens 15323b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15333b2aab18SMatthew Ahrens return (0); 15343b2aab18SMatthew Ahrens } 15353b2aab18SMatthew Ahrens 15363b2aab18SMatthew Ahrens static void 15373b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 15383b2aab18SMatthew Ahrens { 15393b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15403b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15413b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15423b2aab18SMatthew Ahrens 15433b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 15443b2aab18SMatthew Ahrens 15453b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 15463b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 15473b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 15483b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 15493b2aab18SMatthew Ahrens 15503b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15513b2aab18SMatthew Ahrens } 15523b2aab18SMatthew Ahrens 15533b2aab18SMatthew Ahrens int 15543b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 15553b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 15563b2aab18SMatthew Ahrens { 15573b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 15583b2aab18SMatthew Ahrens int error; 15593b2aab18SMatthew Ahrens spa_t *spa; 15603b2aab18SMatthew Ahrens boolean_t needsuspend; 15613b2aab18SMatthew Ahrens void *cookie; 15623b2aab18SMatthew Ahrens 15633b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 15643b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 15653b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 15663b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 15673b2aab18SMatthew Ahrens 15683b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 15693b2aab18SMatthew Ahrens if (error != 0) 15703b2aab18SMatthew Ahrens return (error); 15713b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 15723b2aab18SMatthew Ahrens spa_close(spa, FTAG); 15733b2aab18SMatthew Ahrens 15743b2aab18SMatthew Ahrens if (needsuspend) { 15753b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 15763b2aab18SMatthew Ahrens if (error != 0) 15773b2aab18SMatthew Ahrens return (error); 15783b2aab18SMatthew Ahrens } 15793b2aab18SMatthew Ahrens 15803b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 15817d46dc6cSMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED); 15823b2aab18SMatthew Ahrens 15833b2aab18SMatthew Ahrens if (needsuspend) 15843b2aab18SMatthew Ahrens zil_resume(cookie); 15853b2aab18SMatthew Ahrens return (error); 15863b2aab18SMatthew Ahrens } 15873b2aab18SMatthew Ahrens 15883b2aab18SMatthew Ahrens 15893b2aab18SMatthew Ahrens void 15903b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 15913b2aab18SMatthew Ahrens { 15923b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 15933b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1594c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0); 15953b2aab18SMatthew Ahrens 15963b2aab18SMatthew Ahrens /* 15973b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 15983b2aab18SMatthew Ahrens * sync it out now. 15993b2aab18SMatthew Ahrens */ 16003b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1601c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid; 16023b2aab18SMatthew Ahrens 16033b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 1604b5152584SMatthew Ahrens 1605ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1606ca0cc391SMatthew Ahrens if (ds->ds_feature_activation_needed[f]) { 1607ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1608ca0cc391SMatthew Ahrens continue; 1609ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(ds->ds_object, f, tx); 1610ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 1611ca0cc391SMatthew Ahrens } 1612b5152584SMatthew Ahrens } 16133b2aab18SMatthew Ahrens } 16143b2aab18SMatthew Ahrens 16153b2aab18SMatthew Ahrens static void 16163b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 16173b2aab18SMatthew Ahrens { 16183b2aab18SMatthew Ahrens uint64_t count = 0; 16193b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 16203b2aab18SMatthew Ahrens zap_cursor_t zc; 16213b2aab18SMatthew Ahrens zap_attribute_t za; 16223b2aab18SMatthew Ahrens nvlist_t *propval = fnvlist_alloc(); 16233b2aab18SMatthew Ahrens nvlist_t *val = fnvlist_alloc(); 16243b2aab18SMatthew Ahrens 16253b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 162619b94df9SMatthew Ahrens 162719b94df9SMatthew Ahrens /* 16283b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 162919b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 163019b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 163119b94df9SMatthew Ahrens */ 1632c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 1633c1379625SJustin T. Gibbs VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 163419b94df9SMatthew Ahrens &count)); 163519b94df9SMatthew Ahrens } 1636c1379625SJustin T. Gibbs if (count != dsl_dataset_phys(ds)->ds_num_children - 1) 163719b94df9SMatthew Ahrens goto fail; 1638c1379625SJustin T. Gibbs for (zap_cursor_init(&zc, mos, 1639c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 164019b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 164119b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 164219b94df9SMatthew Ahrens dsl_dataset_t *clone; 164319b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 16443b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 16453b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 164619b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 16473b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 164819b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 164919b94df9SMatthew Ahrens } 165019b94df9SMatthew Ahrens zap_cursor_fini(&zc); 16513b2aab18SMatthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 16523b2aab18SMatthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 165319b94df9SMatthew Ahrens fail: 165419b94df9SMatthew Ahrens nvlist_free(val); 165519b94df9SMatthew Ahrens nvlist_free(propval); 165619b94df9SMatthew Ahrens } 165719b94df9SMatthew Ahrens 1658fa9e4066Sahrens void 1659a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1660fa9e4066Sahrens { 16613b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1662187d6ac0SMatt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio; 1663a9799022Sck 16643b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 16653b2aab18SMatthew Ahrens 1666c1379625SJustin T. Gibbs ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 : 1667c1379625SJustin T. Gibbs (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 / 1668c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes); 16694445fffbSMatthew Ahrens 16704445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 167177372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 1672c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes); 16734445fffbSMatthew Ahrens 1674bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 16754445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 16764445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 1677c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 16784445fffbSMatthew Ahrens get_clones_stat(ds, nv); 16794445fffbSMatthew Ahrens } else { 1680b461c746SMatthew Ahrens if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) { 1681b461c746SMatthew Ahrens char buf[MAXNAMELEN]; 1682b461c746SMatthew Ahrens dsl_dataset_name(ds->ds_prev, buf); 1683b461c746SMatthew Ahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf); 1684b461c746SMatthew Ahrens } 1685b461c746SMatthew Ahrens 16864445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 16874445fffbSMatthew Ahrens } 1688fa9e4066Sahrens 1689a9799022Sck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1690a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1691a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1692a9799022Sck 1693a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1694c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_time); 1695a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1696c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg); 1697a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1698a9799022Sck ds->ds_quota); 1699a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1700a9799022Sck ds->ds_reserved); 1701c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1702c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_guid); 17031d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 1704c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 17051d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 17061d713200SEric Schrock ds->ds_object); 170792241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 170892241e0bSTom Erickson ds->ds_userrefs); 1709842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1710842727c2SChris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1711fa9e4066Sahrens 1712c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 171319b94df9SMatthew Ahrens uint64_t written, comp, uncomp; 171419b94df9SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 171519b94df9SMatthew Ahrens dsl_dataset_t *prev; 171619b94df9SMatthew Ahrens 171719b94df9SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, 1718c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 171919b94df9SMatthew Ahrens if (err == 0) { 172019b94df9SMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written, 172119b94df9SMatthew Ahrens &comp, &uncomp); 172219b94df9SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 172319b94df9SMatthew Ahrens if (err == 0) { 172419b94df9SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 172519b94df9SMatthew Ahrens written); 172619b94df9SMatthew Ahrens } 172719b94df9SMatthew Ahrens } 172819b94df9SMatthew Ahrens } 1729fa9e4066Sahrens } 1730fa9e4066Sahrens 1731a2eea2e1Sahrens void 1732a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1733a2eea2e1Sahrens { 17343b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 17353b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 17363b2aab18SMatthew Ahrens 1737c1379625SJustin T. Gibbs stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg; 1738c1379625SJustin T. Gibbs stat->dds_inconsistent = 1739c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT; 1740c1379625SJustin T. Gibbs stat->dds_guid = dsl_dataset_phys(ds)->ds_guid; 17414445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 1742bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 1743a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 1744c1379625SJustin T. Gibbs stat->dds_num_clones = 1745c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children - 1; 1746ebedde84SEric Taylor } else { 1747ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 1748ebedde84SEric Taylor stat->dds_num_clones = 0; 1749a2eea2e1Sahrens 17504445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 17514445fffbSMatthew Ahrens dsl_dataset_t *ods; 1752a2eea2e1Sahrens 17533b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1754c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj, 1755c1379625SJustin T. Gibbs FTAG, &ods)); 17564445fffbSMatthew Ahrens dsl_dataset_name(ods, stat->dds_origin); 17573b2aab18SMatthew Ahrens dsl_dataset_rele(ods, FTAG); 17584445fffbSMatthew Ahrens } 1759a2eea2e1Sahrens } 1760a2eea2e1Sahrens } 1761a2eea2e1Sahrens 1762a2eea2e1Sahrens uint64_t 1763a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1764a2eea2e1Sahrens { 176591ebeef5Sahrens return (ds->ds_fsid_guid); 1766a2eea2e1Sahrens } 1767a2eea2e1Sahrens 1768a2eea2e1Sahrens void 1769a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 1770a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 1771a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1772fa9e4066Sahrens { 1773c1379625SJustin T. Gibbs *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes; 1774a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1775c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) 1776c1379625SJustin T. Gibbs *availbytesp += 1777c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 1778a9799022Sck if (ds->ds_quota != 0) { 1779a9799022Sck /* 1780a9799022Sck * Adjust available bytes according to refquota 1781a9799022Sck */ 1782a9799022Sck if (*refdbytesp < ds->ds_quota) 1783a9799022Sck *availbytesp = MIN(*availbytesp, 1784a9799022Sck ds->ds_quota - *refdbytesp); 1785a9799022Sck else 1786a9799022Sck *availbytesp = 0; 1787a9799022Sck } 1788c1379625SJustin T. Gibbs *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp); 1789a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1790fa9e4066Sahrens } 1791fa9e4066Sahrens 1792f18faf3fSek boolean_t 179334f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 1794f18faf3fSek { 1795f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 1796f18faf3fSek 17973b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 179834f2f8cfSMatthew Ahrens if (snap == NULL) 1799f18faf3fSek return (B_FALSE); 1800c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_bp.blk_birth > 1801c1379625SJustin T. Gibbs dsl_dataset_phys(snap)->ds_creation_txg) { 180234f2f8cfSMatthew Ahrens objset_t *os, *os_snap; 18036e0cbcaaSMatthew Ahrens /* 18046e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 18056e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 18066e0cbcaaSMatthew Ahrens * modified. 18076e0cbcaaSMatthew Ahrens */ 18086e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 18096e0cbcaaSMatthew Ahrens return (B_TRUE); 181034f2f8cfSMatthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0) 18116e0cbcaaSMatthew Ahrens return (B_TRUE); 18126e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 181334f2f8cfSMatthew Ahrens &os_snap->os_phys->os_meta_dnode, 18146e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 18156e0cbcaaSMatthew Ahrens } 1816f18faf3fSek return (B_FALSE); 1817f18faf3fSek } 1818f18faf3fSek 18193b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 18203b2aab18SMatthew Ahrens const char *ddrsa_fsname; 18213b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 18223b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 18233b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 18243b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 18253b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 18263b2aab18SMatthew Ahrens 18271d452cf5Sahrens /* ARGSUSED */ 1828fa9e4066Sahrens static int 18293b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 18303b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1831fa9e4066Sahrens { 18323b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 18333b2aab18SMatthew Ahrens int error; 1834fa9e4066Sahrens uint64_t val; 1835fa9e4066Sahrens 18363b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 18373b2aab18SMatthew Ahrens if (error != 0) { 18383b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 18393b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 18403b2aab18SMatthew Ahrens } 18411d452cf5Sahrens 18423b2aab18SMatthew Ahrens /* new name should not exist */ 18433b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 18443b2aab18SMatthew Ahrens if (error == 0) 1845be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 18463b2aab18SMatthew Ahrens else if (error == ENOENT) 18473b2aab18SMatthew Ahrens error = 0; 1848cdf5b4caSmmusante 1849cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 18503b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 18513b2aab18SMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN) 1852be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 1853cdf5b4caSmmusante 18543b2aab18SMatthew Ahrens return (error); 18551d452cf5Sahrens } 1856fa9e4066Sahrens 18573b2aab18SMatthew Ahrens static int 18583b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 18591d452cf5Sahrens { 18603b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 18613b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 18621d452cf5Sahrens dsl_dataset_t *hds; 18633b2aab18SMatthew Ahrens int error; 1864fa9e4066Sahrens 18653b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 18663b2aab18SMatthew Ahrens if (error != 0) 18673b2aab18SMatthew Ahrens return (error); 1868fa9e4066Sahrens 18693b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 18703b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 18713b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 18723b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 18733b2aab18SMatthew Ahrens } else { 18743b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 18753b2aab18SMatthew Ahrens } 1876745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 18773b2aab18SMatthew Ahrens return (error); 1878fa9e4066Sahrens } 1879fa9e4066Sahrens 1880cdf5b4caSmmusante static int 18813b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 18823b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1883cdf5b4caSmmusante { 18843b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 18853b2aab18SMatthew Ahrens dsl_dataset_t *ds; 18863b2aab18SMatthew Ahrens uint64_t val; 18873b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 18883b2aab18SMatthew Ahrens int error; 1889ecd6cf80Smarks 18903b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 18913b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 18923b2aab18SMatthew Ahrens if (error == ENOENT) { 18933b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 18943b2aab18SMatthew Ahrens return (0); 1895ecd6cf80Smarks } 1896ecd6cf80Smarks 18973b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 18983b2aab18SMatthew Ahrens 18993b2aab18SMatthew Ahrens /* log before we change the name */ 19003b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 19013b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 1902cdf5b4caSmmusante 1903a2afb611SJerry Jelinek VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx, 1904a2afb611SJerry Jelinek B_FALSE)); 19053b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 19063b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 19073b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 1908c1379625SJustin T. Gibbs VERIFY0(zap_add(dp->dp_meta_objset, 1909c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, 19103b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 1911cdf5b4caSmmusante 19123b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1913cdf5b4caSmmusante return (0); 1914cdf5b4caSmmusante } 1915cdf5b4caSmmusante 19163b2aab18SMatthew Ahrens static void 19173b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 1918cdf5b4caSmmusante { 19193b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 19203b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 19213b2aab18SMatthew Ahrens dsl_dataset_t *hds; 1922cdf5b4caSmmusante 19233b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 19243b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 19253b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 19263b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 19273b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 19283b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 19293b2aab18SMatthew Ahrens } else { 19303b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 1931cdf5b4caSmmusante } 19323b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 1933cdf5b4caSmmusante } 1934cdf5b4caSmmusante 19353b2aab18SMatthew Ahrens int 19363b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 19373b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 19383a5a36beSmmusante { 19393b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 19403a5a36beSmmusante 19413b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 19423b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 19433b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 19443b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 19453a5a36beSmmusante 19463b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 19477d46dc6cSMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 19487d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 19493a5a36beSmmusante } 19503a5a36beSmmusante 195191948b51SKeith M Wesolowski /* 195291948b51SKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is 195391948b51SKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here 195491948b51SKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want 195591948b51SKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away 195691948b51SKeith M Wesolowski * when we release the long hold. 195791948b51SKeith M Wesolowski */ 195891948b51SKeith M Wesolowski static int 195991948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 196091948b51SKeith M Wesolowski { 196191948b51SKeith M Wesolowski boolean_t held; 196291948b51SKeith M Wesolowski 196391948b51SKeith M Wesolowski if (!dmu_tx_is_syncing(tx)) 196491948b51SKeith M Wesolowski return (0); 196591948b51SKeith M Wesolowski 196691948b51SKeith M Wesolowski if (owner != NULL) { 196791948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner); 196891948b51SKeith M Wesolowski dsl_dataset_long_rele(ds, owner); 196991948b51SKeith M Wesolowski } 197091948b51SKeith M Wesolowski 197191948b51SKeith M Wesolowski held = dsl_dataset_long_held(ds); 197291948b51SKeith M Wesolowski 197391948b51SKeith M Wesolowski if (owner != NULL) 197491948b51SKeith M Wesolowski dsl_dataset_long_hold(ds, owner); 197591948b51SKeith M Wesolowski 197691948b51SKeith M Wesolowski if (held) 197791948b51SKeith M Wesolowski return (SET_ERROR(EBUSY)); 197891948b51SKeith M Wesolowski 197991948b51SKeith M Wesolowski return (0); 198091948b51SKeith M Wesolowski } 198191948b51SKeith M Wesolowski 198291948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg { 198391948b51SKeith M Wesolowski const char *ddra_fsname; 198491948b51SKeith M Wesolowski void *ddra_owner; 1985a7027df1SMatthew Ahrens nvlist_t *ddra_result; 198691948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t; 198791948b51SKeith M Wesolowski 19883b2aab18SMatthew Ahrens static int 19893b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 1990fa9e4066Sahrens { 199191948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 19923b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 19931d452cf5Sahrens dsl_dataset_t *ds; 19943b2aab18SMatthew Ahrens int64_t unused_refres_delta; 19953b2aab18SMatthew Ahrens int error; 1996fa9e4066Sahrens 199791948b51SKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 19983b2aab18SMatthew Ahrens if (error != 0) 19993b2aab18SMatthew Ahrens return (error); 2000370c1af0SSanjeev Bagewadi 20013b2aab18SMatthew Ahrens /* must not be a snapshot */ 2002bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 20033b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2004be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 20053b2aab18SMatthew Ahrens } 20063a5a36beSmmusante 20073b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 2008c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) { 20093b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2010be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 20113b2aab18SMatthew Ahrens } 20123a5a36beSmmusante 201378f17100SMatthew Ahrens /* must not have any bookmarks after the most recent snapshot */ 201478f17100SMatthew Ahrens nvlist_t *proprequest = fnvlist_alloc(); 201578f17100SMatthew Ahrens fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 201678f17100SMatthew Ahrens nvlist_t *bookmarks = fnvlist_alloc(); 201778f17100SMatthew Ahrens error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks); 201878f17100SMatthew Ahrens fnvlist_free(proprequest); 201978f17100SMatthew Ahrens if (error != 0) 202078f17100SMatthew Ahrens return (error); 202178f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL); 202278f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) { 202378f17100SMatthew Ahrens nvlist_t *valuenv = 202478f17100SMatthew Ahrens fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair), 202578f17100SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CREATETXG)); 202678f17100SMatthew Ahrens uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value"); 2027c1379625SJustin T. Gibbs if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 202878f17100SMatthew Ahrens fnvlist_free(bookmarks); 202978f17100SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 203078f17100SMatthew Ahrens return (SET_ERROR(EEXIST)); 203178f17100SMatthew Ahrens } 203278f17100SMatthew Ahrens } 203378f17100SMatthew Ahrens fnvlist_free(bookmarks); 203478f17100SMatthew Ahrens 203591948b51SKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 203691948b51SKeith M Wesolowski if (error != 0) { 20373b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 203891948b51SKeith M Wesolowski return (error); 20393b2aab18SMatthew Ahrens } 20403b2aab18SMatthew Ahrens 20413b2aab18SMatthew Ahrens /* 20423b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 20433b2aab18SMatthew Ahrens * the refquota. 20443b2aab18SMatthew Ahrens */ 20453b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 2046c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) { 20473b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2048be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2049fa9e4066Sahrens } 2050370c1af0SSanjeev Bagewadi 20513b2aab18SMatthew Ahrens /* 20523b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 20533b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 20543b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 20553b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 20563b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 20573b2aab18SMatthew Ahrens */ 20583b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 2059c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 20603b2aab18SMatthew Ahrens 20613b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 20623b2aab18SMatthew Ahrens unused_refres_delta > 20633b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 20643b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2065be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 2066fa9e4066Sahrens } 2067fa9e4066Sahrens 20683b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 20693b2aab18SMatthew Ahrens return (0); 20703b2aab18SMatthew Ahrens } 20711d452cf5Sahrens 20723b2aab18SMatthew Ahrens static void 20733b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 20743b2aab18SMatthew Ahrens { 207591948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 20763b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 20773b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 20783b2aab18SMatthew Ahrens uint64_t cloneobj; 2079a7027df1SMatthew Ahrens char namebuf[ZFS_MAXNAMELEN]; 20801d452cf5Sahrens 208191948b51SKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 20821d452cf5Sahrens 2083a7027df1SMatthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf); 2084a7027df1SMatthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf); 2085a7027df1SMatthew Ahrens 20863b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 20873b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 20881d452cf5Sahrens 20893b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 20901d452cf5Sahrens 20913b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 20923b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 20933b2aab18SMatthew Ahrens 20943b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 20953b2aab18SMatthew Ahrens 20963b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 20973b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 20983b2aab18SMatthew Ahrens } 20993b2aab18SMatthew Ahrens 210091948b51SKeith M Wesolowski /* 2101a7027df1SMatthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot. 2102a7027df1SMatthew Ahrens * The name of the most recent snapshot will be returned under key "target" 2103a7027df1SMatthew Ahrens * in the result nvlist. 210491948b51SKeith M Wesolowski * 2105a7027df1SMatthew Ahrens * If owner != NULL: 210691948b51SKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry 210791948b51SKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we 210891948b51SKeith M Wesolowski * succeed or not. 210991948b51SKeith M Wesolowski * 211091948b51SKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See 211191948b51SKeith M Wesolowski * notes above zfs_suspend_fs() for further details. 211291948b51SKeith M Wesolowski */ 21133b2aab18SMatthew Ahrens int 2114a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result) 21153b2aab18SMatthew Ahrens { 211691948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t ddra; 211791948b51SKeith M Wesolowski 211891948b51SKeith M Wesolowski ddra.ddra_fsname = fsname; 211991948b51SKeith M Wesolowski ddra.ddra_owner = owner; 2120a7027df1SMatthew Ahrens ddra.ddra_result = result; 212191948b51SKeith M Wesolowski 21223b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 21237d46dc6cSMatthew Ahrens dsl_dataset_rollback_sync, &ddra, 21247d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 2125fa9e4066Sahrens } 212699653d4eSeschrock 2127088f3894Sahrens struct promotenode { 2128745cd3c5Smaybee list_node_t link; 2129745cd3c5Smaybee dsl_dataset_t *ds; 2130745cd3c5Smaybee }; 2131745cd3c5Smaybee 21323b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg { 21333b2aab18SMatthew Ahrens const char *ddpa_clonename; 21343b2aab18SMatthew Ahrens dsl_dataset_t *ddpa_clone; 213574e7dc98SMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps; 21363b2aab18SMatthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */ 213774e7dc98SMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 2138681d9761SEric Taylor char *err_ds; 2139a2afb611SJerry Jelinek cred_t *cr; 21403b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t; 21411d452cf5Sahrens 214274e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 21433b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 21443b2aab18SMatthew Ahrens void *tag); 21453b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 214674e7dc98SMatthew Ahrens 214799653d4eSeschrock static int 21483b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 214999653d4eSeschrock { 21503b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 21513b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 21523b2aab18SMatthew Ahrens dsl_dataset_t *hds; 21533b2aab18SMatthew Ahrens struct promotenode *snap; 21543b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 2155745cd3c5Smaybee int err; 2156cde58dbcSMatthew Ahrens uint64_t unused; 2157a2afb611SJerry Jelinek uint64_t ss_mv_cnt; 2158cb5842f8SAndriy Gapon size_t max_snap_len; 21591d452cf5Sahrens 21603b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 21613b2aab18SMatthew Ahrens if (err != 0) 21623b2aab18SMatthew Ahrens return (err); 216399653d4eSeschrock 21643b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 2165cb5842f8SAndriy Gapon max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1; 21661d452cf5Sahrens 2167c1379625SJustin T. Gibbs if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) { 21683b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2169be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 21703b2aab18SMatthew Ahrens } 21713b2aab18SMatthew Ahrens 21723b2aab18SMatthew Ahrens /* 21733b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 21743b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 21753b2aab18SMatthew Ahrens */ 21763b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 21773b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 21783b2aab18SMatthew Ahrens return (0); 21793b2aab18SMatthew Ahrens } 21803b2aab18SMatthew Ahrens 21813b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 21823b2aab18SMatthew Ahrens origin_ds = snap->ds; 218399653d4eSeschrock 21843cb34c60Sahrens /* compute origin's new unique space */ 21853b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2186c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2187c1379625SJustin T. Gibbs origin_ds->ds_object); 2188cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2189c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX, 21903b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 219199653d4eSeschrock 2192745cd3c5Smaybee /* 2193745cd3c5Smaybee * Walk the snapshots that we are moving 2194745cd3c5Smaybee * 219574e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 21963b2aab18SMatthew Ahrens * to used by each snapshot: 219774e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 219874e7dc98SMatthew Ahrens * So each snapshot gave birth to: 219974e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 2200745cd3c5Smaybee * So a sequence would look like: 220174e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2202745cd3c5Smaybee * Which simplifies to: 220374e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 2204745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 220574e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 2206745cd3c5Smaybee */ 2207a2afb611SJerry Jelinek ss_mv_cnt = 0; 2208c1379625SJustin T. Gibbs ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes; 2209c1379625SJustin T. Gibbs ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes; 2210c1379625SJustin T. Gibbs ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes; 22113b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 22123b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 221399653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 2214745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 221599653d4eSeschrock 2216a2afb611SJerry Jelinek ss_mv_cnt++; 2217a2afb611SJerry Jelinek 22183b2aab18SMatthew Ahrens /* 22193b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 22203b2aab18SMatthew Ahrens * the objset. 22213b2aab18SMatthew Ahrens */ 22223b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 2223be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 22243b2aab18SMatthew Ahrens goto out; 22253b2aab18SMatthew Ahrens } 22263b2aab18SMatthew Ahrens 222799653d4eSeschrock /* Check that the snapshot name does not conflict */ 22283b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 2229cb5842f8SAndriy Gapon if (strlen(ds->ds_snapname) >= max_snap_len) { 2230cb5842f8SAndriy Gapon err = SET_ERROR(ENAMETOOLONG); 2231cb5842f8SAndriy Gapon goto out; 2232cb5842f8SAndriy Gapon } 2233745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2234681d9761SEric Taylor if (err == 0) { 22353b2aab18SMatthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 2236be6fd75aSMatthew Ahrens err = SET_ERROR(EEXIST); 2237681d9761SEric Taylor goto out; 2238681d9761SEric Taylor } 2239745cd3c5Smaybee if (err != ENOENT) 2240681d9761SEric Taylor goto out; 224199653d4eSeschrock 2242745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 2243c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0) 224474e7dc98SMatthew Ahrens continue; 224574e7dc98SMatthew Ahrens 2246cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 2247cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 22483b2aab18SMatthew Ahrens ddpa->used += dlused; 22493b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 22503b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 225174e7dc98SMatthew Ahrens } 2252745cd3c5Smaybee 2253745cd3c5Smaybee /* 2254745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 2255745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 2256745cd3c5Smaybee */ 22573b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 2258c1379625SJustin T. Gibbs ddpa->used -= 2259c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes; 2260c1379625SJustin T. Gibbs ddpa->comp -= 2261c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes; 22623b2aab18SMatthew Ahrens ddpa->uncomp -= 2263c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)-> 2264c1379625SJustin T. Gibbs ds_uncompressed_bytes; 226599653d4eSeschrock } 226699653d4eSeschrock 2267a2afb611SJerry Jelinek /* Check that there is enough space and limit headroom here */ 226874e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2269a2afb611SJerry Jelinek 0, ss_mv_cnt, ddpa->used, ddpa->cr); 22703b2aab18SMatthew Ahrens if (err != 0) 22713b2aab18SMatthew Ahrens goto out; 227274e7dc98SMatthew Ahrens 227374e7dc98SMatthew Ahrens /* 227474e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 227574e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 227674e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 227774e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 227874e7dc98SMatthew Ahrens */ 2279c1379625SJustin T. Gibbs if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 228074e7dc98SMatthew Ahrens uint64_t space; 228174e7dc98SMatthew Ahrens 228274e7dc98SMatthew Ahrens /* 228374e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 22843f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 2285cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 228674e7dc98SMatthew Ahrens * calls will be fast because they do not have to 228774e7dc98SMatthew Ahrens * iterate over all bps. 228874e7dc98SMatthew Ahrens */ 22893b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 22903b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 22913b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 22923b2aab18SMatthew Ahrens if (err != 0) 22933b2aab18SMatthew Ahrens goto out; 229474e7dc98SMatthew Ahrens 22953b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 22963f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 22973b2aab18SMatthew Ahrens if (err != 0) 22983b2aab18SMatthew Ahrens goto out; 22993b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 230074e7dc98SMatthew Ahrens } 2301c1379625SJustin T. Gibbs if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags & 2302c1379625SJustin T. Gibbs DD_FLAG_USED_BREAKDOWN) { 23033b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 2304c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg, 2305c1379625SJustin T. Gibbs &ddpa->originusedsnap); 23063b2aab18SMatthew Ahrens if (err != 0) 23073b2aab18SMatthew Ahrens goto out; 2308745cd3c5Smaybee } 23091d452cf5Sahrens 2310681d9761SEric Taylor out: 23113b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2312681d9761SEric Taylor return (err); 23131d452cf5Sahrens } 231499653d4eSeschrock 23151d452cf5Sahrens static void 23163b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 23171d452cf5Sahrens { 23183b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 23193b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 23203b2aab18SMatthew Ahrens dsl_dataset_t *hds; 23213b2aab18SMatthew Ahrens struct promotenode *snap; 23223b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 23233b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 23243b2aab18SMatthew Ahrens dsl_dir_t *dd; 23253cb34c60Sahrens dsl_dir_t *odd = NULL; 2326088f3894Sahrens uint64_t oldnext_obj; 232774e7dc98SMatthew Ahrens int64_t delta; 23281d452cf5Sahrens 23293b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 23303b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 23313b2aab18SMatthew Ahrens 2332c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE); 23331d452cf5Sahrens 23343b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 23353b2aab18SMatthew Ahrens origin_ds = snap->ds; 23363b2aab18SMatthew Ahrens dd = hds->ds_dir; 23373b2aab18SMatthew Ahrens 23383b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 233974e7dc98SMatthew Ahrens origin_head = snap->ds; 234074e7dc98SMatthew Ahrens 23410b69c2f0Sahrens /* 23423cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 23430b69c2f0Sahrens * changing. 23440b69c2f0Sahrens */ 23453b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 23463cb34c60Sahrens NULL, FTAG, &odd)); 234799653d4eSeschrock 2348745cd3c5Smaybee /* change origin's next snap */ 2349745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2350c1379625SJustin T. Gibbs oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj; 23513b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2352c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2353c1379625SJustin T. Gibbs origin_ds->ds_object); 2354c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object; 2355745cd3c5Smaybee 2356088f3894Sahrens /* change the origin's next clone */ 2357c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) { 23583b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 23593b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 23603b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2361c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_clones_obj, 2362088f3894Sahrens oldnext_obj, tx)); 2363088f3894Sahrens } 2364088f3894Sahrens 2365745cd3c5Smaybee /* change origin */ 2366745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2367c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object); 2368c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj; 23693f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2370745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2371c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object; 23723f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 2373c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg; 2374745cd3c5Smaybee 2375cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2376cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 23773b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2378c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx)); 23793b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2380c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2381cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2382cde58dbcSMatthew Ahrens 23833b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2384c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2385cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2386c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_clones == 0) { 2387c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones = 2388c1379625SJustin T. Gibbs zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES, 2389c1379625SJustin T. Gibbs DMU_OT_NONE, 0, tx); 2390cde58dbcSMatthew Ahrens } 23913b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2392c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx)); 2393cde58dbcSMatthew Ahrens } 2394cde58dbcSMatthew Ahrens 239599653d4eSeschrock /* move snapshots to this dir */ 23963b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 23973b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2398745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 239999653d4eSeschrock 24003b2aab18SMatthew Ahrens /* 24013b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 24023b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 24033b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 24043b2aab18SMatthew Ahrens */ 2405503ad85cSMatthew Ahrens if (ds->ds_objset) { 2406503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2407503ad85cSMatthew Ahrens ds->ds_objset = NULL; 24083baa08fcSek } 24093b2aab18SMatthew Ahrens 241099653d4eSeschrock /* move snap name entry */ 24113b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 24123b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2413a2afb611SJerry Jelinek ds->ds_snapname, tx, B_TRUE)); 24143b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 2415c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname, 241699653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2417a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(hds->ds_dir, 1, 2418a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 2419cde58dbcSMatthew Ahrens 242099653d4eSeschrock /* change containing dsl_dir */ 242199653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 2422c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object); 2423c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object; 24243cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 24253b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 24263b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 242799653d4eSeschrock NULL, ds, &ds->ds_dir)); 242899653d4eSeschrock 2429cde58dbcSMatthew Ahrens /* move any clone references */ 2430c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj && 2431cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2432cde58dbcSMatthew Ahrens zap_cursor_t zc; 2433cde58dbcSMatthew Ahrens zap_attribute_t za; 2434cde58dbcSMatthew Ahrens 24353b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2436c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 24373b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 24383b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 24393b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 24403b2aab18SMatthew Ahrens uint64_t o; 2441a9799022Sck 24423b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 24433b2aab18SMatthew Ahrens /* 24443b2aab18SMatthew Ahrens * We've already moved the 24453b2aab18SMatthew Ahrens * origin's reference. 24463b2aab18SMatthew Ahrens */ 24473b2aab18SMatthew Ahrens continue; 24483b2aab18SMatthew Ahrens } 2449a9799022Sck 24503b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 24513b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 2452c1379625SJustin T. Gibbs o = dsl_dir_phys(cnds->ds_dir)-> 2453c1379625SJustin T. Gibbs dd_head_dataset_obj; 2454a9799022Sck 24553b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2456c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, o, tx)); 24573b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2458c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, o, tx)); 24593b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 24603b2aab18SMatthew Ahrens } 24613b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 24623b2aab18SMatthew Ahrens } 24639082849eSck 24643b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2465a9799022Sck } 2466a9799022Sck 2467a9799022Sck /* 24683b2aab18SMatthew Ahrens * Change space accounting. 24693b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 24703b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 24713b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2472a9799022Sck */ 2473a9799022Sck 24743b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 2475c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]; 24763b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 24773b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 24783b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 24793b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 24803b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 24813b2aab18SMatthew Ahrens 24823b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 2483c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP]; 24843b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 24853b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 24863b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 24873b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 24883b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 24893b2aab18SMatthew Ahrens 2490c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique; 24913b2aab18SMatthew Ahrens 24923b2aab18SMatthew Ahrens /* log history record */ 24933b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 24943b2aab18SMatthew Ahrens 24953b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 24963b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2497a9799022Sck } 2498a9799022Sck 24993b2aab18SMatthew Ahrens /* 25003b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 25013b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 25023b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 25033b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 25043b2aab18SMatthew Ahrens */ 2505a9799022Sck static int 25063b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 25073b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2508a9799022Sck { 25093b2aab18SMatthew Ahrens uint64_t obj = last_obj; 2510a9799022Sck 25113b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 25123b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 2513a9799022Sck 25143b2aab18SMatthew Ahrens while (obj != first_obj) { 25153b2aab18SMatthew Ahrens dsl_dataset_t *ds; 25163b2aab18SMatthew Ahrens struct promotenode *snap; 25173b2aab18SMatthew Ahrens int err; 251892241e0bSTom Erickson 25193b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 25203b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 25213b2aab18SMatthew Ahrens if (err != 0) 25223b2aab18SMatthew Ahrens return (err); 2523a9799022Sck 25243b2aab18SMatthew Ahrens if (first_obj == 0) 2525c1379625SJustin T. Gibbs first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj; 25263b2aab18SMatthew Ahrens 25273b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 25283b2aab18SMatthew Ahrens snap->ds = ds; 25293b2aab18SMatthew Ahrens list_insert_tail(l, snap); 2530c1379625SJustin T. Gibbs obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 25313b2aab18SMatthew Ahrens } 2532a9799022Sck 2533a9799022Sck return (0); 2534a9799022Sck } 2535a9799022Sck 25363b2aab18SMatthew Ahrens static int 25373b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2538a9799022Sck { 25393b2aab18SMatthew Ahrens struct promotenode *snap; 2540a9799022Sck 25413b2aab18SMatthew Ahrens *spacep = 0; 25423b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 25433b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 25443b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 25453b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 25463b2aab18SMatthew Ahrens *spacep += used; 254792241e0bSTom Erickson } 25483b2aab18SMatthew Ahrens return (0); 2549a9799022Sck } 2550a9799022Sck 25513b2aab18SMatthew Ahrens static void 25523b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 2553a9799022Sck { 25543b2aab18SMatthew Ahrens struct promotenode *snap; 255592241e0bSTom Erickson 25563b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 25573b2aab18SMatthew Ahrens return; 2558a9799022Sck 25593b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 25603b2aab18SMatthew Ahrens list_remove(l, snap); 25613b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 25623b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 25633b2aab18SMatthew Ahrens } 25643b2aab18SMatthew Ahrens list_destroy(l); 2565a9799022Sck } 2566a9799022Sck 2567a9799022Sck static int 25683b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2569a9799022Sck { 25703b2aab18SMatthew Ahrens int error; 25713b2aab18SMatthew Ahrens dsl_dir_t *dd; 25723b2aab18SMatthew Ahrens struct promotenode *snap; 2573a9799022Sck 25743b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 25753b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 25763b2aab18SMatthew Ahrens if (error != 0) 25773b2aab18SMatthew Ahrens return (error); 25783b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 2579a9799022Sck 2580bc9014e6SJustin Gibbs if (ddpa->ddpa_clone->ds_is_snapshot || 25813b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 25823b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 2583be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 25843b2aab18SMatthew Ahrens } 2585a9799022Sck 2586c1379625SJustin T. Gibbs error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj, 25873b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 25883b2aab18SMatthew Ahrens if (error != 0) 25893b2aab18SMatthew Ahrens goto out; 2590a9799022Sck 25913b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 25923b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 25933b2aab18SMatthew Ahrens if (error != 0) 25943b2aab18SMatthew Ahrens goto out; 2595a9799022Sck 25963b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 2597c1379625SJustin T. Gibbs ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj); 2598c1379625SJustin T. Gibbs error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj, 2599c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj, 26003b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 26013b2aab18SMatthew Ahrens if (error != 0) 26023b2aab18SMatthew Ahrens goto out; 2603379c004dSEric Schrock 2604c1379625SJustin T. Gibbs if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) { 26053b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 2606c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj, 26073b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 26083b2aab18SMatthew Ahrens if (error != 0) 26093b2aab18SMatthew Ahrens goto out; 2610379c004dSEric Schrock } 26113b2aab18SMatthew Ahrens out: 26123b2aab18SMatthew Ahrens if (error != 0) 26133b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 26143b2aab18SMatthew Ahrens return (error); 2615a9799022Sck } 2616a9799022Sck 2617a9799022Sck static void 26183b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2619a9799022Sck { 26203b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 26213b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 26223b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 26233b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 26243b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 26253b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 26263b2aab18SMatthew Ahrens } 262702c8f3f0SMatthew Ahrens 26283b2aab18SMatthew Ahrens /* 26293b2aab18SMatthew Ahrens * Promote a clone. 26303b2aab18SMatthew Ahrens * 26313b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 26323b2aab18SMatthew Ahrens * in with the name. (It must be at least MAXNAMELEN bytes long.) 26333b2aab18SMatthew Ahrens */ 26343b2aab18SMatthew Ahrens int 26353b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 26363b2aab18SMatthew Ahrens { 26373b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 26383b2aab18SMatthew Ahrens uint64_t numsnaps; 26393b2aab18SMatthew Ahrens int error; 26403b2aab18SMatthew Ahrens objset_t *os; 264192241e0bSTom Erickson 26423b2aab18SMatthew Ahrens /* 26433b2aab18SMatthew Ahrens * We will modify space proportional to the number of 26443b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 26453b2aab18SMatthew Ahrens */ 26463b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 26473b2aab18SMatthew Ahrens if (error != 0) 26483b2aab18SMatthew Ahrens return (error); 26493b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 2650c1379625SJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj, 2651c1379625SJustin T. Gibbs &numsnaps); 26523b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 26533b2aab18SMatthew Ahrens if (error != 0) 26543b2aab18SMatthew Ahrens return (error); 265502c8f3f0SMatthew Ahrens 26563b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 26573b2aab18SMatthew Ahrens ddpa.err_ds = conflsnap; 2658a2afb611SJerry Jelinek ddpa.cr = CRED(); 265902c8f3f0SMatthew Ahrens 26603b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check, 26617d46dc6cSMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 26627d46dc6cSMatthew Ahrens 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED)); 2663a9799022Sck } 2664a9799022Sck 2665a9799022Sck int 26663b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 266791948b51SKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 2668a9799022Sck { 26693b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2670a9799022Sck 26713b2aab18SMatthew Ahrens /* they should both be heads */ 2672bc9014e6SJustin Gibbs if (clone->ds_is_snapshot || 2673bc9014e6SJustin Gibbs origin_head->ds_is_snapshot) 2674be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 267592241e0bSTom Erickson 267634f2f8cfSMatthew Ahrens /* if we are not forcing, the branch point should be just before them */ 267734f2f8cfSMatthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev) 2678be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2679a9799022Sck 26803b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 26813b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 26823b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 268334f2f8cfSMatthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir) 2684be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 268592241e0bSTom Erickson 26863b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 26873b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2688be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2689842727c2SChris Kirby 26903b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 269134f2f8cfSMatthew Ahrens if (!force && 269234f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 2693be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 2694c99e4bdcSChris Kirby 26953b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 269691948b51SKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx)) 2697be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 26983b2aab18SMatthew Ahrens 26993b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 27003b2aab18SMatthew Ahrens unused_refres_delta = 27013b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2702c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 27033b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2704c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 27053b2aab18SMatthew Ahrens 27063b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 27073b2aab18SMatthew Ahrens unused_refres_delta > 27083b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2709be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 27103b2aab18SMatthew Ahrens 27113b2aab18SMatthew Ahrens /* clone can't be over the head's refquota */ 27123b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 2713c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes > 2714c1379625SJustin T. Gibbs origin_head->ds_quota) 2715be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2716c99e4bdcSChris Kirby 27173b2aab18SMatthew Ahrens return (0); 2718c99e4bdcSChris Kirby } 2719c99e4bdcSChris Kirby 2720a7f53a56SChris Kirby void 27213b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 27223b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 2723a7f53a56SChris Kirby { 27243b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 27253b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2726a7f53a56SChris Kirby 27273b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 27283b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 2729c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota); 273034f2f8cfSMatthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 2731842727c2SChris Kirby 2732ca0cc391SMatthew Ahrens /* 2733ca0cc391SMatthew Ahrens * Swap per-dataset feature flags. 2734ca0cc391SMatthew Ahrens */ 2735ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2736ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 2737ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) { 2738ca0cc391SMatthew Ahrens ASSERT(!clone->ds_feature_inuse[f]); 2739ca0cc391SMatthew Ahrens ASSERT(!origin_head->ds_feature_inuse[f]); 2740ca0cc391SMatthew Ahrens continue; 2741ca0cc391SMatthew Ahrens } 2742ca0cc391SMatthew Ahrens 2743ca0cc391SMatthew Ahrens boolean_t clone_inuse = clone->ds_feature_inuse[f]; 2744ca0cc391SMatthew Ahrens boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f]; 2745ca0cc391SMatthew Ahrens 2746ca0cc391SMatthew Ahrens if (clone_inuse) { 2747ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(clone->ds_object, f, tx); 2748ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_FALSE; 2749ca0cc391SMatthew Ahrens } 2750ca0cc391SMatthew Ahrens if (origin_head_inuse) { 2751ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(origin_head->ds_object, 2752ca0cc391SMatthew Ahrens f, tx); 2753ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_FALSE; 2754ca0cc391SMatthew Ahrens } 2755ca0cc391SMatthew Ahrens if (clone_inuse) { 2756ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(origin_head->ds_object, 2757ca0cc391SMatthew Ahrens f, tx); 2758ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_TRUE; 2759ca0cc391SMatthew Ahrens } 2760ca0cc391SMatthew Ahrens if (origin_head_inuse) { 2761ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(clone->ds_object, f, tx); 2762ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_TRUE; 2763ca0cc391SMatthew Ahrens } 2764ca0cc391SMatthew Ahrens } 2765ca0cc391SMatthew Ahrens 27663b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 27673b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2768842727c2SChris Kirby 27693b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 27703b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 27713b2aab18SMatthew Ahrens clone->ds_objset = NULL; 27723b2aab18SMatthew Ahrens } 2773842727c2SChris Kirby 27743b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 27753b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 27763b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 2777842727c2SChris Kirby } 2778842727c2SChris Kirby 27793b2aab18SMatthew Ahrens unused_refres_delta = 27803b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2781c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 27823b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2783c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 27843b2aab18SMatthew Ahrens 27853b2aab18SMatthew Ahrens /* 27863b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 27873b2aab18SMatthew Ahrens */ 27883b2aab18SMatthew Ahrens if (clone->ds_prev) { 27893b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 27903b2aab18SMatthew Ahrens uint64_t comp, uncomp; 27913b2aab18SMatthew Ahrens 27923b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 27933b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 2794c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX, 2795c1379625SJustin T. Gibbs &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp); 27963b2aab18SMatthew Ahrens } 27973b2aab18SMatthew Ahrens 27983b2aab18SMatthew Ahrens /* swap blkptrs */ 27993b2aab18SMatthew Ahrens { 28003b2aab18SMatthew Ahrens blkptr_t tmp; 2801c1379625SJustin T. Gibbs tmp = dsl_dataset_phys(origin_head)->ds_bp; 2802c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_bp = 2803c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp; 2804c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp = tmp; 28053b2aab18SMatthew Ahrens } 28063b2aab18SMatthew Ahrens 28073b2aab18SMatthew Ahrens /* set dd_*_bytes */ 28083b2aab18SMatthew Ahrens { 28093b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 28103b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 28113b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 28123b2aab18SMatthew Ahrens 2813c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(clone->ds_dir)-> 28143b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 28153b2aab18SMatthew Ahrens 28163b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 28173b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 28183b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 28193b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 282015508ac0SChris Kirby 2821c1379625SJustin T. Gibbs dused = dsl_dataset_phys(clone)->ds_referenced_bytes + 2822c1379625SJustin T. Gibbs cdl_used - 2823c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_referenced_bytes + 2824c1379625SJustin T. Gibbs odl_used); 2825c1379625SJustin T. Gibbs dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes + 2826c1379625SJustin T. Gibbs cdl_comp - 2827c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_compressed_bytes + 2828c1379625SJustin T. Gibbs odl_comp); 2829c1379625SJustin T. Gibbs duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes + 28303b2aab18SMatthew Ahrens cdl_uncomp - 2831c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes + 2832c1379625SJustin T. Gibbs odl_uncomp); 2833842727c2SChris Kirby 28343b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 28353b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 28363b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 28373b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 2838842727c2SChris Kirby 2839842727c2SChris Kirby /* 28403b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 28413b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 28423b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 28433b2aab18SMatthew Ahrens * changing that affects the snapused). 2844842727c2SChris Kirby */ 28453b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 28463b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 28473b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 28483b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 28493b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 28503b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 28513b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 28523b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 2853842727c2SChris Kirby } 2854842727c2SChris Kirby 28553b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 2856c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes, 2857c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes); 2858c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes, 2859c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_compressed_bytes); 2860c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes, 2861c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_uncompressed_bytes); 2862c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes, 2863c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 2864842727c2SChris Kirby 28653b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 28663b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 28673b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 2868ca45db41SChris Kirby 28693b2aab18SMatthew Ahrens /* 28703b2aab18SMatthew Ahrens * Swap deadlists. 28713b2aab18SMatthew Ahrens */ 28723b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 28733b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 2874c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj, 2875c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 28763b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 2877c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 28783b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 2879c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_deadlist_obj); 2880842727c2SChris Kirby 28813b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 2882842727c2SChris Kirby 28833b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 28843b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 2885842727c2SChris Kirby } 2886842727c2SChris Kirby 28873b2aab18SMatthew Ahrens /* 28883b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 28893b2aab18SMatthew Ahrens * return the name of that dataset. 28903b2aab18SMatthew Ahrens */ 2891a7f53a56SChris Kirby int 28923b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2893a7f53a56SChris Kirby { 28943b2aab18SMatthew Ahrens dsl_pool_t *dp; 28953b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2896a7f53a56SChris Kirby int error; 2897a7f53a56SChris Kirby 28983b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 28993b2aab18SMatthew Ahrens if (error != 0) 29003b2aab18SMatthew Ahrens return (error); 29013b2aab18SMatthew Ahrens 29023b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 29033b2aab18SMatthew Ahrens if (error == 0) { 29043b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 29053b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 29063b2aab18SMatthew Ahrens } 29073b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 2908a7f53a56SChris Kirby 2909a7f53a56SChris Kirby return (error); 2910a7f53a56SChris Kirby } 2911a7f53a56SChris Kirby 2912842727c2SChris Kirby int 29133b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 29143b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 2915842727c2SChris Kirby { 29163b2aab18SMatthew Ahrens int error = 0; 2917842727c2SChris Kirby 29183b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 2919842727c2SChris Kirby 29203b2aab18SMatthew Ahrens /* 29213b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 29223b2aab18SMatthew Ahrens * unconsumed refreservation space. 29233b2aab18SMatthew Ahrens */ 29243b2aab18SMatthew Ahrens *ref_rsrv = 0; 2925842727c2SChris Kirby 29263b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 29273b2aab18SMatthew Ahrens /* 29283b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 29293b2aab18SMatthew Ahrens */ 2930c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 29313b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 2932c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 2933c1379625SJustin T. Gibbs *used -= 2934c1379625SJustin T. Gibbs (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 29353b2aab18SMatthew Ahrens *ref_rsrv = 29363b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 2937842727c2SChris Kirby } 2938842727c2SChris Kirby 29393b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 29403b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 29413b2aab18SMatthew Ahrens return (0); 2942842727c2SChris Kirby } 29433b2aab18SMatthew Ahrens /* 29443b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 29453b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 29463b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 29473b2aab18SMatthew Ahrens * may free up space for us). 29483b2aab18SMatthew Ahrens */ 2949c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >= 2950c1379625SJustin T. Gibbs ds->ds_quota) { 29513b2aab18SMatthew Ahrens if (inflight > 0 || 2952c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota) 2953be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 29543b2aab18SMatthew Ahrens else 2955be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 2956842727c2SChris Kirby } 29573b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2958842727c2SChris Kirby 2959842727c2SChris Kirby return (error); 2960842727c2SChris Kirby } 2961842727c2SChris Kirby 29623b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 29633b2aab18SMatthew Ahrens const char *ddsqra_name; 29643b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 29653b2aab18SMatthew Ahrens uint64_t ddsqra_value; 29663b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 2967842727c2SChris Kirby 29683b2aab18SMatthew Ahrens 29693b2aab18SMatthew Ahrens /* ARGSUSED */ 2970842727c2SChris Kirby static int 29713b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 2972842727c2SChris Kirby { 29733b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 29743b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 29753b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2976842727c2SChris Kirby int error; 29773b2aab18SMatthew Ahrens uint64_t newval; 2978842727c2SChris Kirby 29793b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 2980be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2981842727c2SChris Kirby 29823b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 29833b2aab18SMatthew Ahrens if (error != 0) 29843b2aab18SMatthew Ahrens return (error); 29853b2aab18SMatthew Ahrens 2986bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 29873b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2988be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2989842727c2SChris Kirby } 2990842727c2SChris Kirby 29913b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 29923b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 29933b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 29943b2aab18SMatthew Ahrens if (error != 0) { 29953b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2996842727c2SChris Kirby return (error); 2997842727c2SChris Kirby } 2998842727c2SChris Kirby 29993b2aab18SMatthew Ahrens if (newval == 0) { 30003b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 30013b2aab18SMatthew Ahrens return (0); 30023b2aab18SMatthew Ahrens } 3003842727c2SChris Kirby 3004c1379625SJustin T. Gibbs if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes || 30053b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 30063b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3007be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 30083b2aab18SMatthew Ahrens } 30093b2aab18SMatthew Ahrens 30103b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3011842727c2SChris Kirby return (0); 3012842727c2SChris Kirby } 3013842727c2SChris Kirby 30143b2aab18SMatthew Ahrens static void 30153b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 3016842727c2SChris Kirby { 30173b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 30183b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 30193b2aab18SMatthew Ahrens dsl_dataset_t *ds; 30203b2aab18SMatthew Ahrens uint64_t newval; 3021842727c2SChris Kirby 30223b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 3023842727c2SChris Kirby 30243b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 30253b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 30263b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 30273b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 3028842727c2SChris Kirby 30293b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 30303b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 3031842727c2SChris Kirby 30323b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 30333b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 30343b2aab18SMatthew Ahrens ds->ds_quota = newval; 3035842727c2SChris Kirby } 30363b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3037842727c2SChris Kirby } 3038842727c2SChris Kirby 30393b2aab18SMatthew Ahrens int 30403b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 30413b2aab18SMatthew Ahrens uint64_t refquota) 3042842727c2SChris Kirby { 30433b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3044842727c2SChris Kirby 30453b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 30463b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 30473b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 30483b2aab18SMatthew Ahrens 30493b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 30507d46dc6cSMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 3051842727c2SChris Kirby } 3052842727c2SChris Kirby 3053842727c2SChris Kirby static int 30543b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 3055842727c2SChris Kirby { 30563b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 30573b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3058842727c2SChris Kirby dsl_dataset_t *ds; 3059842727c2SChris Kirby int error; 30603b2aab18SMatthew Ahrens uint64_t newval, unique; 3061d7747cbcSChris Kirby 30623b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 3063be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3064842727c2SChris Kirby 30653b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 30663b2aab18SMatthew Ahrens if (error != 0) 3067842727c2SChris Kirby return (error); 3068842727c2SChris Kirby 3069bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 30703b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3071be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3072842727c2SChris Kirby } 3073842727c2SChris Kirby 30743b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 30753b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 30763b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 30773b2aab18SMatthew Ahrens if (error != 0) { 30783b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3079842727c2SChris Kirby return (error); 3080842727c2SChris Kirby } 3081842727c2SChris Kirby 30823b2aab18SMatthew Ahrens /* 30833b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 30843b2aab18SMatthew Ahrens * space estimates may be inaccurate. 30853b2aab18SMatthew Ahrens */ 30863b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 30873b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 30883b2aab18SMatthew Ahrens return (0); 3089842727c2SChris Kirby } 3090842727c2SChris Kirby 30913b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 30923b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 30933b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 3094c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 30953b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3096842727c2SChris Kirby 30973b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 30983b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 30993b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 3100842727c2SChris Kirby 31013b2aab18SMatthew Ahrens if (delta > 31023b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 31033b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 31043b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3105be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 31063b2aab18SMatthew Ahrens } 3107842727c2SChris Kirby } 3108842727c2SChris Kirby 31093b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 31103b2aab18SMatthew Ahrens return (0); 3111842727c2SChris Kirby } 3112842727c2SChris Kirby 31133b2aab18SMatthew Ahrens void 31143b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 31153b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 3116ca45db41SChris Kirby { 31173b2aab18SMatthew Ahrens uint64_t newval; 31183b2aab18SMatthew Ahrens uint64_t unique; 31193b2aab18SMatthew Ahrens int64_t delta; 3120ca45db41SChris Kirby 31213b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 31223b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 3123ca45db41SChris Kirby 31243b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 31253b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 3126a7f53a56SChris Kirby 31273b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 31283b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 31293b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 31303b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 3131c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 31323b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 31333b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 31343b2aab18SMatthew Ahrens ds->ds_reserved = newval; 31353b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3136a7f53a56SChris Kirby 31373b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 31383b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 3139ca45db41SChris Kirby } 3140ca45db41SChris Kirby 31413b2aab18SMatthew Ahrens static void 31423b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 3143842727c2SChris Kirby { 31443b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 31453b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3146842727c2SChris Kirby dsl_dataset_t *ds; 3147842727c2SChris Kirby 31483b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 31493b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 31503b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 3151842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 3152842727c2SChris Kirby } 3153503ad85cSMatthew Ahrens 3154503ad85cSMatthew Ahrens int 31553b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 31563b2aab18SMatthew Ahrens uint64_t refreservation) 3157503ad85cSMatthew Ahrens { 31583b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3159503ad85cSMatthew Ahrens 31603b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 31613b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 31623b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 31633b2aab18SMatthew Ahrens 31643b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 31657d46dc6cSMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 31667d46dc6cSMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE)); 3167503ad85cSMatthew Ahrens } 316819b94df9SMatthew Ahrens 316919b94df9SMatthew Ahrens /* 317019b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 317119b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 317219b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 317319b94df9SMatthew Ahrens * fail and return EINVAL. 317419b94df9SMatthew Ahrens * 317519b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 317619b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 317719b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 317819b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 317919b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 318019b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 318119b94df9SMatthew Ahrens * 318219b94df9SMatthew Ahrens * space freed [---------------------] 318319b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 318419b94df9SMatthew Ahrens * oldsnap new 318519b94df9SMatthew Ahrens */ 318619b94df9SMatthew Ahrens int 318719b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 318819b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 318919b94df9SMatthew Ahrens { 319019b94df9SMatthew Ahrens int err = 0; 319119b94df9SMatthew Ahrens uint64_t snapobj; 319219b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 319319b94df9SMatthew Ahrens 31943b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 31953b2aab18SMatthew Ahrens 319619b94df9SMatthew Ahrens *usedp = 0; 3197c1379625SJustin T. Gibbs *usedp += dsl_dataset_phys(new)->ds_referenced_bytes; 3198c1379625SJustin T. Gibbs *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes; 319919b94df9SMatthew Ahrens 320019b94df9SMatthew Ahrens *compp = 0; 3201c1379625SJustin T. Gibbs *compp += dsl_dataset_phys(new)->ds_compressed_bytes; 3202c1379625SJustin T. Gibbs *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes; 320319b94df9SMatthew Ahrens 320419b94df9SMatthew Ahrens *uncompp = 0; 3205c1379625SJustin T. Gibbs *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes; 3206c1379625SJustin T. Gibbs *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes; 320719b94df9SMatthew Ahrens 320819b94df9SMatthew Ahrens snapobj = new->ds_object; 320919b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 321019b94df9SMatthew Ahrens dsl_dataset_t *snap; 321119b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 321219b94df9SMatthew Ahrens 3213ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 3214ad135b5dSChristopher Siden snap = new; 3215ad135b5dSChristopher Siden } else { 3216ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 3217ad135b5dSChristopher Siden if (err != 0) 3218ad135b5dSChristopher Siden break; 3219ad135b5dSChristopher Siden } 322019b94df9SMatthew Ahrens 3221c1379625SJustin T. Gibbs if (dsl_dataset_phys(snap)->ds_prev_snap_txg == 3222c1379625SJustin T. Gibbs dsl_dataset_phys(oldsnap)->ds_creation_txg) { 322319b94df9SMatthew Ahrens /* 322419b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 322519b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 322619b94df9SMatthew Ahrens * which is more efficient (especially for old-format 322719b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 322819b94df9SMatthew Ahrens * doesn't have enough information to make this 322919b94df9SMatthew Ahrens * optimization itself. 323019b94df9SMatthew Ahrens */ 323119b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 323219b94df9SMatthew Ahrens &used, &comp, &uncomp); 323319b94df9SMatthew Ahrens } else { 323419b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 3235c1379625SJustin T. Gibbs 0, dsl_dataset_phys(oldsnap)->ds_creation_txg, 323619b94df9SMatthew Ahrens &used, &comp, &uncomp); 323719b94df9SMatthew Ahrens } 323819b94df9SMatthew Ahrens *usedp += used; 323919b94df9SMatthew Ahrens *compp += comp; 324019b94df9SMatthew Ahrens *uncompp += uncomp; 324119b94df9SMatthew Ahrens 324219b94df9SMatthew Ahrens /* 324319b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 324419b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 324519b94df9SMatthew Ahrens * was not a snapshot of/before new. 324619b94df9SMatthew Ahrens */ 3247c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 3248ad135b5dSChristopher Siden if (snap != new) 3249ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 325019b94df9SMatthew Ahrens if (snapobj == 0) { 3251be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 325219b94df9SMatthew Ahrens break; 325319b94df9SMatthew Ahrens } 325419b94df9SMatthew Ahrens 325519b94df9SMatthew Ahrens } 325619b94df9SMatthew Ahrens return (err); 325719b94df9SMatthew Ahrens } 325819b94df9SMatthew Ahrens 325919b94df9SMatthew Ahrens /* 326019b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 326119b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 326219b94df9SMatthew Ahrens * 326319b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 326419b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 326519b94df9SMatthew Ahrens * firstsnap lastsnap 326619b94df9SMatthew Ahrens * 326719b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 326819b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 326919b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 327019b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 327119b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 327219b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 327319b94df9SMatthew Ahrens */ 327419b94df9SMatthew Ahrens int 327519b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 327619b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 327719b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 327819b94df9SMatthew Ahrens { 327919b94df9SMatthew Ahrens int err = 0; 328019b94df9SMatthew Ahrens uint64_t snapobj; 328119b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 328219b94df9SMatthew Ahrens 3283bc9014e6SJustin Gibbs ASSERT(firstsnap->ds_is_snapshot); 3284bc9014e6SJustin Gibbs ASSERT(lastsnap->ds_is_snapshot); 328519b94df9SMatthew Ahrens 328619b94df9SMatthew Ahrens /* 328719b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 328819b94df9SMatthew Ahrens * is before lastsnap. 328919b94df9SMatthew Ahrens */ 329019b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 3291c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_creation_txg > 3292c1379625SJustin T. Gibbs dsl_dataset_phys(lastsnap)->ds_creation_txg) 3293be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 329419b94df9SMatthew Ahrens 329519b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 329619b94df9SMatthew Ahrens 3297c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj; 329819b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 329919b94df9SMatthew Ahrens dsl_dataset_t *ds; 330019b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 330119b94df9SMatthew Ahrens 330219b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 330319b94df9SMatthew Ahrens if (err != 0) 330419b94df9SMatthew Ahrens break; 330519b94df9SMatthew Ahrens 330619b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 3307c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX, 330819b94df9SMatthew Ahrens &used, &comp, &uncomp); 330919b94df9SMatthew Ahrens *usedp += used; 331019b94df9SMatthew Ahrens *compp += comp; 331119b94df9SMatthew Ahrens *uncompp += uncomp; 331219b94df9SMatthew Ahrens 3313c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 331419b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 331519b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 331619b94df9SMatthew Ahrens } 331719b94df9SMatthew Ahrens return (err); 331819b94df9SMatthew Ahrens } 33193b2aab18SMatthew Ahrens 33203b2aab18SMatthew Ahrens /* 33213b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 33223b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 33233b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 33243b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 33253b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 332678f17100SMatthew Ahrens * 332778f17100SMatthew Ahrens * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 33283b2aab18SMatthew Ahrens */ 33293b2aab18SMatthew Ahrens boolean_t 333078f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 333178f17100SMatthew Ahrens uint64_t earlier_txg) 33323b2aab18SMatthew Ahrens { 33333b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 33343b2aab18SMatthew Ahrens int error; 33353b2aab18SMatthew Ahrens boolean_t ret; 33363b2aab18SMatthew Ahrens 33373b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 3338bc9014e6SJustin Gibbs ASSERT(earlier->ds_is_snapshot || earlier_txg != 0); 333978f17100SMatthew Ahrens 334078f17100SMatthew Ahrens if (earlier_txg == 0) 3341c1379625SJustin T. Gibbs earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg; 33423b2aab18SMatthew Ahrens 3343bc9014e6SJustin Gibbs if (later->ds_is_snapshot && 3344c1379625SJustin T. Gibbs earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg) 33453b2aab18SMatthew Ahrens return (B_FALSE); 33463b2aab18SMatthew Ahrens 33473b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 33483b2aab18SMatthew Ahrens return (B_TRUE); 33493b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 33503b2aab18SMatthew Ahrens return (B_FALSE); 33513b2aab18SMatthew Ahrens 3352c1379625SJustin T. Gibbs if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object) 33533b2aab18SMatthew Ahrens return (B_TRUE); 33543b2aab18SMatthew Ahrens dsl_dataset_t *origin; 33553b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 3356c1379625SJustin T. Gibbs dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin); 33573b2aab18SMatthew Ahrens if (error != 0) 33583b2aab18SMatthew Ahrens return (B_FALSE); 335978f17100SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 33603b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 33613b2aab18SMatthew Ahrens return (ret); 33623b2aab18SMatthew Ahrens } 33632acef22dSMatthew Ahrens 33642acef22dSMatthew Ahrens void 33652acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 33662acef22dSMatthew Ahrens { 33672acef22dSMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 33682acef22dSMatthew Ahrens dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 33692acef22dSMatthew Ahrens } 3370