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. 2342fcb65eSMatthew Ahrens * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 24a2afb611SJerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved. 2503d1795fSAlexander Stetsenko * Copyright (c) 2014 RackTop Systems. 26fa9e4066Sahrens */ 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/dmu_objset.h> 29fa9e4066Sahrens #include <sys/dsl_dataset.h> 30fa9e4066Sahrens #include <sys/dsl_dir.h> 3199653d4eSeschrock #include <sys/dsl_prop.h> 321d452cf5Sahrens #include <sys/dsl_synctask.h> 33fa9e4066Sahrens #include <sys/dmu_traverse.h> 344e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 35fa9e4066Sahrens #include <sys/dmu_tx.h> 36fa9e4066Sahrens #include <sys/arc.h> 37fa9e4066Sahrens #include <sys/zio.h> 38fa9e4066Sahrens #include <sys/zap.h> 39ad135b5dSChristopher Siden #include <sys/zfeature.h> 40fa9e4066Sahrens #include <sys/unique.h> 41fa9e4066Sahrens #include <sys/zfs_context.h> 42cdf5b4caSmmusante #include <sys/zfs_ioctl.h> 43ecd6cf80Smarks #include <sys/spa.h> 44088f3894Sahrens #include <sys/zfs_znode.h> 45c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 46842727c2SChris Kirby #include <sys/zvol.h> 473f9d6ad7SLin Ling #include <sys/dsl_scan.h> 48cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h> 493b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 503b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 5178f17100SMatthew Ahrens #include <sys/dsl_bookmark.h> 52e1930233Sbonwick 53b5152584SMatthew Ahrens /* 54b5152584SMatthew Ahrens * The SPA supports block sizes up to 16MB. However, very large blocks 55b5152584SMatthew Ahrens * can have an impact on i/o latency (e.g. tying up a spinning disk for 56b5152584SMatthew Ahrens * ~300ms), and also potentially on the memory allocator. Therefore, 57b5152584SMatthew Ahrens * we do not allow the recordsize to be set larger than zfs_max_recordsize 58b5152584SMatthew Ahrens * (default 1MB). Larger blocks can be created by changing this tunable, 59b5152584SMatthew Ahrens * and pools with larger blocks can always be imported and used, regardless 60b5152584SMatthew Ahrens * of this setting. 61b5152584SMatthew Ahrens */ 62b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024; 63b5152584SMatthew Ahrens 64cde58dbcSMatthew Ahrens #define SWITCH64(x, y) \ 65cde58dbcSMatthew Ahrens { \ 66cde58dbcSMatthew Ahrens uint64_t __tmp = (x); \ 67cde58dbcSMatthew Ahrens (x) = (y); \ 68cde58dbcSMatthew Ahrens (y) = __tmp; \ 69cde58dbcSMatthew Ahrens } 70cde58dbcSMatthew Ahrens 7155434c77Sek #define DS_REF_MAX (1ULL << 62) 72fa9e4066Sahrens 73*c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds); 74*c1379625SJustin T. Gibbs extern inline boolean_t dsl_dataset_is_snapshot(dsl_dataset_t *ds); 75*c1379625SJustin T. Gibbs 76a9799022Sck /* 77a9799022Sck * Figure out how much of this delta should be propogated to the dsl_dir 78a9799022Sck * layer. If there's a refreservation, that space has already been 79a9799022Sck * partially accounted for in our ancestors. 80a9799022Sck */ 81a9799022Sck static int64_t 82a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta) 83a9799022Sck { 84*c1379625SJustin T. Gibbs dsl_dataset_phys_t *ds_phys; 85a9799022Sck uint64_t old_bytes, new_bytes; 86a9799022Sck 87a9799022Sck if (ds->ds_reserved == 0) 88a9799022Sck return (delta); 89a9799022Sck 90*c1379625SJustin T. Gibbs ds_phys = dsl_dataset_phys(ds); 91*c1379625SJustin T. Gibbs old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved); 92*c1379625SJustin T. Gibbs new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 93a9799022Sck 94a9799022Sck ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 95a9799022Sck return (new_bytes - old_bytes); 96a9799022Sck } 97fa9e4066Sahrens 98fa9e4066Sahrens void 99b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 100fa9e4066Sahrens { 101b24ab676SJeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 102fa9e4066Sahrens int compressed = BP_GET_PSIZE(bp); 103fa9e4066Sahrens int uncompressed = BP_GET_UCSIZE(bp); 104a9799022Sck int64_t delta; 105fa9e4066Sahrens 1063f9d6ad7SLin Ling dprintf_bp(bp, "ds=%p", ds); 107fa9e4066Sahrens 108fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 109fa9e4066Sahrens /* It could have been compressed away to nothing */ 110fa9e4066Sahrens if (BP_IS_HOLE(bp)) 111fa9e4066Sahrens return; 112fa9e4066Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 113ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 114fa9e4066Sahrens if (ds == NULL) { 115ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 116ce636f8bSMatthew Ahrens used, compressed, uncompressed); 117fa9e4066Sahrens return; 118fa9e4066Sahrens } 1193f9d6ad7SLin Ling 120b62969f8SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 121fa9e4066Sahrens mutex_enter(&ds->ds_lock); 122a9799022Sck delta = parent_delta(ds, used); 123*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes += used; 124*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes += compressed; 125*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed; 126*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes += used; 127b5152584SMatthew Ahrens if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) 128b5152584SMatthew Ahrens ds->ds_need_large_blocks = B_TRUE; 129fa9e4066Sahrens mutex_exit(&ds->ds_lock); 13074e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 13174e7dc98SMatthew Ahrens compressed, uncompressed, tx); 13274e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used - delta, 13374e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 134fa9e4066Sahrens } 135fa9e4066Sahrens 136cdb0ab79Smaybee int 137b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 138b24ab676SJeff Bonwick boolean_t async) 139fa9e4066Sahrens { 14043466aaeSMax Grossman int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 14143466aaeSMax Grossman int compressed = BP_GET_PSIZE(bp); 14243466aaeSMax Grossman int uncompressed = BP_GET_UCSIZE(bp); 14343466aaeSMax Grossman 144fa9e4066Sahrens if (BP_IS_HOLE(bp)) 145cdb0ab79Smaybee return (0); 146fa9e4066Sahrens 147b24ab676SJeff Bonwick ASSERT(dmu_tx_is_syncing(tx)); 148b24ab676SJeff Bonwick ASSERT(bp->blk_birth <= tx->tx_txg); 149b24ab676SJeff Bonwick 150fa9e4066Sahrens if (ds == NULL) { 151b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 152ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 153ce636f8bSMatthew Ahrens -used, -compressed, -uncompressed); 154cdb0ab79Smaybee return (used); 155fa9e4066Sahrens } 156fa9e4066Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 157fa9e4066Sahrens 15874e7dc98SMatthew Ahrens ASSERT(!dsl_dataset_is_snapshot(ds)); 159fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 160fa9e4066Sahrens 161*c1379625SJustin T. Gibbs if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 162a9799022Sck int64_t delta; 163c717a561Smaybee 1643f9d6ad7SLin Ling dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 165b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 166fa9e4066Sahrens 167fa9e4066Sahrens mutex_enter(&ds->ds_lock); 168*c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used || 169a9799022Sck !DS_UNIQUE_IS_ACCURATE(ds)); 170a9799022Sck delta = parent_delta(ds, -used); 171*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes -= used; 172fa9e4066Sahrens mutex_exit(&ds->ds_lock); 17374e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 174a9799022Sck delta, -compressed, -uncompressed, tx); 17574e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, -used - delta, 17674e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 177fa9e4066Sahrens } else { 178fa9e4066Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 179b24ab676SJeff Bonwick if (async) { 180b24ab676SJeff Bonwick /* 181b24ab676SJeff Bonwick * We are here as part of zio's write done callback, 182b24ab676SJeff Bonwick * which means we're a zio interrupt thread. We can't 183cde58dbcSMatthew Ahrens * call dsl_deadlist_insert() now because it may block 184b24ab676SJeff Bonwick * waiting for I/O. Instead, put bp on the deferred 185b24ab676SJeff Bonwick * queue and let dsl_pool_sync() finish the job. 186b24ab676SJeff Bonwick */ 187cde58dbcSMatthew Ahrens bplist_append(&ds->ds_pending_deadlist, bp); 188b24ab676SJeff Bonwick } else { 189cde58dbcSMatthew Ahrens dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 190b24ab676SJeff Bonwick } 191a4611edeSahrens ASSERT3U(ds->ds_prev->ds_object, ==, 192*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj); 193*c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0); 194fa9e4066Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 195*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 196a4611edeSahrens ds->ds_object && bp->blk_birth > 197*c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) { 198a4611edeSahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 199a4611edeSahrens mutex_enter(&ds->ds_prev->ds_lock); 200*c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used; 201a4611edeSahrens mutex_exit(&ds->ds_prev->ds_lock); 202fa9e4066Sahrens } 2033f9d6ad7SLin Ling if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 20474e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used, 20574e7dc98SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 20674e7dc98SMatthew Ahrens } 207fa9e4066Sahrens } 208fa9e4066Sahrens mutex_enter(&ds->ds_lock); 209*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used); 210*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes -= used; 211*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed); 212*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed; 213*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed); 214*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed; 215fa9e4066Sahrens mutex_exit(&ds->ds_lock); 216cdb0ab79Smaybee 217cdb0ab79Smaybee return (used); 218fa9e4066Sahrens } 219fa9e4066Sahrens 220ea8dc4b6Seschrock uint64_t 221ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 222fa9e4066Sahrens { 223a2eea2e1Sahrens uint64_t trysnap = 0; 224a2eea2e1Sahrens 225fa9e4066Sahrens if (ds == NULL) 226ea8dc4b6Seschrock return (0); 227fa9e4066Sahrens /* 228fa9e4066Sahrens * The snapshot creation could fail, but that would cause an 229fa9e4066Sahrens * incorrect FALSE return, which would only result in an 230fa9e4066Sahrens * overestimation of the amount of space that an operation would 231fa9e4066Sahrens * consume, which is OK. 232fa9e4066Sahrens * 233fa9e4066Sahrens * There's also a small window where we could miss a pending 234fa9e4066Sahrens * snapshot, because we could set the sync task in the quiescing 235fa9e4066Sahrens * phase. So this should only be used as a guess. 236fa9e4066Sahrens */ 237a2eea2e1Sahrens if (ds->ds_trysnap_txg > 238a2eea2e1Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 239a2eea2e1Sahrens trysnap = ds->ds_trysnap_txg; 240*c1379625SJustin T. Gibbs return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap)); 241ea8dc4b6Seschrock } 242ea8dc4b6Seschrock 2433d692628SSanjeev Bagewadi boolean_t 244c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp, 245c7cd2421SGeorge Wilson uint64_t blk_birth) 246ea8dc4b6Seschrock { 24743466aaeSMax Grossman if (blk_birth <= dsl_dataset_prev_snap_txg(ds) || 24843466aaeSMax Grossman (bp != NULL && BP_IS_HOLE(bp))) 249c7cd2421SGeorge Wilson return (B_FALSE); 250c7cd2421SGeorge Wilson 251837b568bSGeorge Wilson ddt_prefetch(dsl_dataset_get_spa(ds), bp); 252c7cd2421SGeorge Wilson 253c7cd2421SGeorge Wilson return (B_TRUE); 254fa9e4066Sahrens } 255fa9e4066Sahrens 256fa9e4066Sahrens /* ARGSUSED */ 257fa9e4066Sahrens static void 258fa9e4066Sahrens dsl_dataset_evict(dmu_buf_t *db, void *dsv) 259fa9e4066Sahrens { 260fa9e4066Sahrens dsl_dataset_t *ds = dsv; 261fa9e4066Sahrens 2623b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == NULL); 263fa9e4066Sahrens 26491ebeef5Sahrens unique_remove(ds->ds_fsid_guid); 265fa9e4066Sahrens 266503ad85cSMatthew Ahrens if (ds->ds_objset != NULL) 267503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 268fa9e4066Sahrens 269fa9e4066Sahrens if (ds->ds_prev) { 2703b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 271fa9e4066Sahrens ds->ds_prev = NULL; 272fa9e4066Sahrens } 273fa9e4066Sahrens 274cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 275*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_deadlist_obj != 0) 276cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 277745cd3c5Smaybee if (ds->ds_dir) 2783b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 279fa9e4066Sahrens 28091ebeef5Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 281fa9e4066Sahrens 2825ad82045Snd mutex_destroy(&ds->ds_lock); 28391ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 284d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 2853b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 2865ad82045Snd 287fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 288fa9e4066Sahrens } 289fa9e4066Sahrens 2903b2aab18SMatthew Ahrens int 291fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 292fa9e4066Sahrens { 293fa9e4066Sahrens dsl_dataset_phys_t *headphys; 294fa9e4066Sahrens int err; 295fa9e4066Sahrens dmu_buf_t *headdbuf; 296fa9e4066Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 297fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 298fa9e4066Sahrens 299fa9e4066Sahrens if (ds->ds_snapname[0]) 300ea8dc4b6Seschrock return (0); 301*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) 302ea8dc4b6Seschrock return (0); 303fa9e4066Sahrens 304*c1379625SJustin T. Gibbs err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, 305ea8dc4b6Seschrock FTAG, &headdbuf); 3063b2aab18SMatthew Ahrens if (err != 0) 307ea8dc4b6Seschrock return (err); 308fa9e4066Sahrens headphys = headdbuf->db_data; 309fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 310e7437265Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 311ea8dc4b6Seschrock dmu_buf_rele(headdbuf, FTAG); 312ea8dc4b6Seschrock return (err); 313fa9e4066Sahrens } 314fa9e4066Sahrens 3153b2aab18SMatthew Ahrens int 316745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 317ab04eb8eStimh { 318745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 319*c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 320ab04eb8eStimh matchtype_t mt; 321ab04eb8eStimh int err; 322ab04eb8eStimh 323*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 324ab04eb8eStimh mt = MT_FIRST; 325ab04eb8eStimh else 326ab04eb8eStimh mt = MT_EXACT; 327ab04eb8eStimh 328745cd3c5Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 329ab04eb8eStimh value, mt, NULL, 0, NULL); 330ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 331745cd3c5Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 332ab04eb8eStimh return (err); 333ab04eb8eStimh } 334ab04eb8eStimh 3353b2aab18SMatthew Ahrens int 336a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx, 337a2afb611SJerry Jelinek boolean_t adj_cnt) 338ab04eb8eStimh { 339745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 340*c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 341ab04eb8eStimh matchtype_t mt; 342ab04eb8eStimh int err; 343ab04eb8eStimh 34471eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 34571eb0538SChris Kirby 346*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 347ab04eb8eStimh mt = MT_FIRST; 348ab04eb8eStimh else 349ab04eb8eStimh mt = MT_EXACT; 350ab04eb8eStimh 351745cd3c5Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 352ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 353745cd3c5Smaybee err = zap_remove(mos, snapobj, name, tx); 354a2afb611SJerry Jelinek 355a2afb611SJerry Jelinek if (err == 0 && adj_cnt) 356a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, -1, 357a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 358a2afb611SJerry Jelinek 359ab04eb8eStimh return (err); 360ab04eb8eStimh } 361ab04eb8eStimh 3623b2aab18SMatthew Ahrens int 3633b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 364745cd3c5Smaybee dsl_dataset_t **dsp) 365fa9e4066Sahrens { 366fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 367fa9e4066Sahrens dmu_buf_t *dbuf; 368fa9e4066Sahrens dsl_dataset_t *ds; 369ea8dc4b6Seschrock int err; 370a7f53a56SChris Kirby dmu_object_info_t doi; 371fa9e4066Sahrens 3723b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 373fa9e4066Sahrens 374ea8dc4b6Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 3753b2aab18SMatthew Ahrens if (err != 0) 376ea8dc4b6Seschrock return (err); 377a7f53a56SChris Kirby 378a7f53a56SChris Kirby /* Make sure dsobj has the correct object type. */ 379a7f53a56SChris Kirby dmu_object_info_from_db(dbuf, &doi); 3802acef22dSMatthew Ahrens if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 381b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 382be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 383b287be1bSWill Andrews } 384a7f53a56SChris Kirby 385fa9e4066Sahrens ds = dmu_buf_get_user(dbuf); 386fa9e4066Sahrens if (ds == NULL) { 387d5285caeSGeorge Wilson dsl_dataset_t *winner = NULL; 388fa9e4066Sahrens 389fa9e4066Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 390fa9e4066Sahrens ds->ds_dbuf = dbuf; 391fa9e4066Sahrens ds->ds_object = dsobj; 392fa9e4066Sahrens 3935ad82045Snd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 39491ebeef5Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 3954e3c9f44SBill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 3963b2aab18SMatthew Ahrens refcount_create(&ds->ds_longholds); 3975ad82045Snd 398cde58dbcSMatthew Ahrens bplist_create(&ds->ds_pending_deadlist); 399cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, 400*c1379625SJustin T. Gibbs mos, dsl_dataset_phys(ds)->ds_deadlist_obj); 401cde58dbcSMatthew Ahrens 4024e3c9f44SBill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 4034e3c9f44SBill Pijewski offsetof(dmu_sendarg_t, dsa_link)); 4044e3c9f44SBill Pijewski 405b5152584SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 406b5152584SMatthew Ahrens err = zap_contains(mos, dsobj, DS_FIELD_LARGE_BLOCKS); 407b5152584SMatthew Ahrens if (err == 0) 408b5152584SMatthew Ahrens ds->ds_large_blocks = B_TRUE; 409b5152584SMatthew Ahrens else 410b5152584SMatthew Ahrens ASSERT3U(err, ==, ENOENT); 411b5152584SMatthew Ahrens } 412b5152584SMatthew Ahrens 413ea8dc4b6Seschrock if (err == 0) { 4143b2aab18SMatthew Ahrens err = dsl_dir_hold_obj(dp, 415*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, 416*c1379625SJustin T. Gibbs &ds->ds_dir); 417ea8dc4b6Seschrock } 4183b2aab18SMatthew Ahrens if (err != 0) { 4195ad82045Snd mutex_destroy(&ds->ds_lock); 42091ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 421d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 4223b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 423cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 424cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 425ea8dc4b6Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 426ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 427ea8dc4b6Seschrock return (err); 428ea8dc4b6Seschrock } 429fa9e4066Sahrens 43074e7dc98SMatthew Ahrens if (!dsl_dataset_is_snapshot(ds)) { 431fa9e4066Sahrens ds->ds_snapname[0] = '\0'; 432*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 4333b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 434*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, 435745cd3c5Smaybee ds, &ds->ds_prev); 436fa9e4066Sahrens } 43778f17100SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 43878f17100SMatthew Ahrens int zaperr = zap_lookup(mos, ds->ds_object, 43978f17100SMatthew Ahrens DS_FIELD_BOOKMARK_NAMES, 44078f17100SMatthew Ahrens sizeof (ds->ds_bookmarks), 1, 44178f17100SMatthew Ahrens &ds->ds_bookmarks); 44278f17100SMatthew Ahrens if (zaperr != ENOENT) 44378f17100SMatthew Ahrens VERIFY0(zaperr); 44478f17100SMatthew Ahrens } 445842727c2SChris Kirby } else { 446842727c2SChris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 447842727c2SChris Kirby err = dsl_dataset_get_snapname(ds); 448*c1379625SJustin T. Gibbs if (err == 0 && 449*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj != 0) { 450842727c2SChris Kirby err = zap_count( 451842727c2SChris Kirby ds->ds_dir->dd_pool->dp_meta_objset, 452*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj, 453842727c2SChris Kirby &ds->ds_userrefs); 454842727c2SChris Kirby } 455fa9e4066Sahrens } 456fa9e4066Sahrens 45774e7dc98SMatthew Ahrens if (err == 0 && !dsl_dataset_is_snapshot(ds)) { 4583b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4593b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 4603b2aab18SMatthew Ahrens &ds->ds_reserved); 461cb625fb5Sck if (err == 0) { 4623b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 4633b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 4643b2aab18SMatthew Ahrens &ds->ds_quota); 465cb625fb5Sck } 466cb625fb5Sck } else { 467cb625fb5Sck ds->ds_reserved = ds->ds_quota = 0; 468cb625fb5Sck } 469cb625fb5Sck 470d5285caeSGeorge Wilson if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds, 471*c1379625SJustin T. Gibbs dsl_dataset_evict)) != NULL) { 472cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 473cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 474745cd3c5Smaybee if (ds->ds_prev) 4753b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 4763b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 4775ad82045Snd mutex_destroy(&ds->ds_lock); 47891ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 479d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 4803b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 481fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 4823b2aab18SMatthew Ahrens if (err != 0) { 483ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 484ea8dc4b6Seschrock return (err); 485ea8dc4b6Seschrock } 486fa9e4066Sahrens ds = winner; 487fa9e4066Sahrens } else { 48891ebeef5Sahrens ds->ds_fsid_guid = 489*c1379625SJustin T. Gibbs unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid); 490fa9e4066Sahrens } 491fa9e4066Sahrens } 492fa9e4066Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 493*c1379625SJustin T. Gibbs ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data); 494*c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 || 495afc6333aSahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 49684db2a68Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 497ea8dc4b6Seschrock *dsp = ds; 498ea8dc4b6Seschrock return (0); 499fa9e4066Sahrens } 500fa9e4066Sahrens 501745cd3c5Smaybee int 5023b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name, 503503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 504fa9e4066Sahrens { 505fa9e4066Sahrens dsl_dir_t *dd; 506745cd3c5Smaybee const char *snapname; 507fa9e4066Sahrens uint64_t obj; 508fa9e4066Sahrens int err = 0; 509fa9e4066Sahrens 5103b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 5113b2aab18SMatthew Ahrens if (err != 0) 512ea8dc4b6Seschrock return (err); 513fa9e4066Sahrens 5143b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 515*c1379625SJustin T. Gibbs obj = dsl_dir_phys(dd)->dd_head_dataset_obj; 5163b2aab18SMatthew Ahrens if (obj != 0) 5173b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, dsp); 518745cd3c5Smaybee else 519be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 520fa9e4066Sahrens 521745cd3c5Smaybee /* we may be looking for a snapshot */ 522745cd3c5Smaybee if (err == 0 && snapname != NULL) { 5233b2aab18SMatthew Ahrens dsl_dataset_t *ds; 524fa9e4066Sahrens 525745cd3c5Smaybee if (*snapname++ != '@') { 526745cd3c5Smaybee dsl_dataset_rele(*dsp, tag); 5273b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 528be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 529fa9e4066Sahrens } 530fa9e4066Sahrens 531745cd3c5Smaybee dprintf("looking for snapshot '%s'\n", snapname); 532745cd3c5Smaybee err = dsl_dataset_snap_lookup(*dsp, snapname, &obj); 533745cd3c5Smaybee if (err == 0) 5343b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 535745cd3c5Smaybee dsl_dataset_rele(*dsp, tag); 536745cd3c5Smaybee 5373b2aab18SMatthew Ahrens if (err == 0) { 538745cd3c5Smaybee mutex_enter(&ds->ds_lock); 539745cd3c5Smaybee if (ds->ds_snapname[0] == 0) 540745cd3c5Smaybee (void) strlcpy(ds->ds_snapname, snapname, 541745cd3c5Smaybee sizeof (ds->ds_snapname)); 542745cd3c5Smaybee mutex_exit(&ds->ds_lock); 5433b2aab18SMatthew Ahrens *dsp = ds; 544fa9e4066Sahrens } 545fa9e4066Sahrens } 5463b2aab18SMatthew Ahrens 5473b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 548fa9e4066Sahrens return (err); 549fa9e4066Sahrens } 550fa9e4066Sahrens 551fa9e4066Sahrens int 5523b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 5533b2aab18SMatthew Ahrens void *tag, dsl_dataset_t **dsp) 5543b2aab18SMatthew Ahrens { 5553b2aab18SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 5563b2aab18SMatthew Ahrens if (err != 0) 5573b2aab18SMatthew Ahrens return (err); 5583b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 5593b2aab18SMatthew Ahrens dsl_dataset_rele(*dsp, tag); 5603b2aab18SMatthew Ahrens *dsp = NULL; 561be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 5623b2aab18SMatthew Ahrens } 5633b2aab18SMatthew Ahrens return (0); 5643b2aab18SMatthew Ahrens } 5653b2aab18SMatthew Ahrens 5663b2aab18SMatthew Ahrens int 5673b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name, 568503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 569fa9e4066Sahrens { 5703b2aab18SMatthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp); 5713b2aab18SMatthew Ahrens if (err != 0) 572745cd3c5Smaybee return (err); 5733b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 574503ad85cSMatthew Ahrens dsl_dataset_rele(*dsp, tag); 575be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 576745cd3c5Smaybee } 577745cd3c5Smaybee return (0); 578fa9e4066Sahrens } 579fa9e4066Sahrens 5803b2aab18SMatthew Ahrens /* 5813b2aab18SMatthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long 5823b2aab18SMatthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold 5833b2aab18SMatthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()). 5843b2aab18SMatthew Ahrens * 5853b2aab18SMatthew Ahrens * The dataset and pool must be held when this function is called. After it 5863b2aab18SMatthew Ahrens * is called, the pool hold may be released while the dataset is still held 5873b2aab18SMatthew Ahrens * and accessed. 5883b2aab18SMatthew Ahrens */ 5893b2aab18SMatthew Ahrens void 5903b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 5913b2aab18SMatthew Ahrens { 5923b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 5933b2aab18SMatthew Ahrens (void) refcount_add(&ds->ds_longholds, tag); 5943b2aab18SMatthew Ahrens } 5953b2aab18SMatthew Ahrens 5963b2aab18SMatthew Ahrens void 5973b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 5983b2aab18SMatthew Ahrens { 5993b2aab18SMatthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag); 6003b2aab18SMatthew Ahrens } 6013b2aab18SMatthew Ahrens 6023b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */ 6033b2aab18SMatthew Ahrens boolean_t 6043b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds) 6053b2aab18SMatthew Ahrens { 6063b2aab18SMatthew Ahrens return (!refcount_is_zero(&ds->ds_longholds)); 6073b2aab18SMatthew Ahrens } 6083b2aab18SMatthew Ahrens 609fa9e4066Sahrens void 610fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 611fa9e4066Sahrens { 612fa9e4066Sahrens if (ds == NULL) { 613fa9e4066Sahrens (void) strcpy(name, "mos"); 614fa9e4066Sahrens } else { 615fa9e4066Sahrens dsl_dir_name(ds->ds_dir, name); 6163b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 617fa9e4066Sahrens if (ds->ds_snapname[0]) { 618fa9e4066Sahrens (void) strcat(name, "@"); 619745cd3c5Smaybee /* 620745cd3c5Smaybee * We use a "recursive" mutex so that we 621745cd3c5Smaybee * can call dprintf_ds() with ds_lock held. 622745cd3c5Smaybee */ 623fa9e4066Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 624fa9e4066Sahrens mutex_enter(&ds->ds_lock); 625fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 626fa9e4066Sahrens mutex_exit(&ds->ds_lock); 627fa9e4066Sahrens } else { 628fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 629fa9e4066Sahrens } 630fa9e4066Sahrens } 631fa9e4066Sahrens } 632fa9e4066Sahrens } 633fa9e4066Sahrens 6343cb34c60Sahrens void 635745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6363cb34c60Sahrens { 6373b2aab18SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag); 638745cd3c5Smaybee } 639745cd3c5Smaybee 640745cd3c5Smaybee void 641503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 642745cd3c5Smaybee { 643d808a4fcSJustin T. Gibbs ASSERT3P(ds->ds_owner, ==, tag); 644d808a4fcSJustin T. Gibbs ASSERT(ds->ds_dbuf != NULL); 645745cd3c5Smaybee 6463cb34c60Sahrens mutex_enter(&ds->ds_lock); 647745cd3c5Smaybee ds->ds_owner = NULL; 6483cb34c60Sahrens mutex_exit(&ds->ds_lock); 6493b2aab18SMatthew Ahrens dsl_dataset_long_rele(ds, tag); 650d808a4fcSJustin T. Gibbs dsl_dataset_rele(ds, tag); 6513cb34c60Sahrens } 6523cb34c60Sahrens 6533cb34c60Sahrens boolean_t 6543b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 6553cb34c60Sahrens { 656745cd3c5Smaybee boolean_t gotit = FALSE; 657745cd3c5Smaybee 6583cb34c60Sahrens mutex_enter(&ds->ds_lock); 6593b2aab18SMatthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 660503ad85cSMatthew Ahrens ds->ds_owner = tag; 6613b2aab18SMatthew Ahrens dsl_dataset_long_hold(ds, tag); 662745cd3c5Smaybee gotit = TRUE; 6633cb34c60Sahrens } 6643cb34c60Sahrens mutex_exit(&ds->ds_lock); 665745cd3c5Smaybee return (gotit); 666745cd3c5Smaybee } 667745cd3c5Smaybee 6681d452cf5Sahrens uint64_t 669088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 670ab04eb8eStimh uint64_t flags, dmu_tx_t *tx) 671fa9e4066Sahrens { 6723cb34c60Sahrens dsl_pool_t *dp = dd->dd_pool; 673fa9e4066Sahrens dmu_buf_t *dbuf; 674fa9e4066Sahrens dsl_dataset_phys_t *dsphys; 6753cb34c60Sahrens uint64_t dsobj; 676fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 677fa9e4066Sahrens 678088f3894Sahrens if (origin == NULL) 679088f3894Sahrens origin = dp->dp_origin_snap; 680088f3894Sahrens 6813cb34c60Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 682*c1379625SJustin T. Gibbs ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0); 683fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 684*c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 685fa9e4066Sahrens 6861649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 6871649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 6883b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 689fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 690fa9e4066Sahrens dsphys = dbuf->db_data; 691745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 692fa9e4066Sahrens dsphys->ds_dir_obj = dd->dd_object; 693ab04eb8eStimh dsphys->ds_flags = flags; 694fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 695fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 696fa9e4066Sahrens sizeof (dsphys->ds_guid)); 697fa9e4066Sahrens dsphys->ds_snapnames_zapobj = 698ab04eb8eStimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 699ab04eb8eStimh DMU_OT_NONE, 0, tx); 700fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 701088f3894Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 702a9799022Sck 703cde58dbcSMatthew Ahrens if (origin == NULL) { 704cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 705cde58dbcSMatthew Ahrens } else { 7063b2aab18SMatthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */ 707cde58dbcSMatthew Ahrens 7083cb34c60Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 709fa9e4066Sahrens dsphys->ds_prev_snap_txg = 710*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_creation_txg; 711ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = 712*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_referenced_bytes; 713fa9e4066Sahrens dsphys->ds_compressed_bytes = 714*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_compressed_bytes; 715fa9e4066Sahrens dsphys->ds_uncompressed_bytes = 716*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_uncompressed_bytes; 717*c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp; 71842fcb65eSMatthew Ahrens 71942fcb65eSMatthew Ahrens /* 72042fcb65eSMatthew Ahrens * Inherit flags that describe the dataset's contents 72142fcb65eSMatthew Ahrens * (INCONSISTENT) or properties (Case Insensitive). 72242fcb65eSMatthew Ahrens */ 723*c1379625SJustin T. Gibbs dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags & 72442fcb65eSMatthew Ahrens (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET); 725fa9e4066Sahrens 726b5152584SMatthew Ahrens if (origin->ds_large_blocks) 727b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx); 728b5152584SMatthew Ahrens 7293cb34c60Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 730*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_num_children++; 731fa9e4066Sahrens 7323b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 733*c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj, 734*c1379625SJustin T. Gibbs FTAG, &ohds)); 735cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 736cde58dbcSMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 737cde58dbcSMatthew Ahrens dsl_dataset_rele(ohds, FTAG); 738cde58dbcSMatthew Ahrens 739088f3894Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 740*c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) { 741*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj = 742088f3894Sahrens zap_create(mos, 743088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 744088f3894Sahrens } 7453b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 746*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj, 747*c1379625SJustin T. Gibbs dsobj, tx)); 748088f3894Sahrens } 749088f3894Sahrens 750fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 751*c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object; 752cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 753*c1379625SJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 754cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 755*c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones = 756cde58dbcSMatthew Ahrens zap_create(mos, 757cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 758cde58dbcSMatthew Ahrens } 7593b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 760*c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones, 761*c1379625SJustin T. Gibbs dsobj, tx)); 762cde58dbcSMatthew Ahrens } 763fa9e4066Sahrens } 764ab04eb8eStimh 765ab04eb8eStimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 766ab04eb8eStimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 767ab04eb8eStimh 768ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 769fa9e4066Sahrens 770fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 771*c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj; 7723cb34c60Sahrens 7733cb34c60Sahrens return (dsobj); 7743cb34c60Sahrens } 7753cb34c60Sahrens 7763b2aab18SMatthew Ahrens static void 7773b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 7783b2aab18SMatthew Ahrens { 7793b2aab18SMatthew Ahrens objset_t *os; 7803b2aab18SMatthew Ahrens 7813b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 7823b2aab18SMatthew Ahrens bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 7833b2aab18SMatthew Ahrens dsl_dataset_dirty(ds, tx); 7843b2aab18SMatthew Ahrens } 7853b2aab18SMatthew Ahrens 7863cb34c60Sahrens uint64_t 787ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 788ab04eb8eStimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 7893cb34c60Sahrens { 7903cb34c60Sahrens dsl_pool_t *dp = pdd->dd_pool; 7913cb34c60Sahrens uint64_t dsobj, ddobj; 7923cb34c60Sahrens dsl_dir_t *dd; 7933cb34c60Sahrens 7943b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 7953cb34c60Sahrens ASSERT(lastname[0] != '@'); 7963cb34c60Sahrens 797088f3894Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 7983b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 7993cb34c60Sahrens 8003b2aab18SMatthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, 8013b2aab18SMatthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx); 8023cb34c60Sahrens 8033cb34c60Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8043cb34c60Sahrens 805a2afb611SJerry Jelinek /* 806a2afb611SJerry Jelinek * Since we're creating a new node we know it's a leaf, so we can 807a2afb611SJerry Jelinek * initialize the counts if the limit feature is active. 808a2afb611SJerry Jelinek */ 809a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 810a2afb611SJerry Jelinek uint64_t cnt = 0; 811a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 812a2afb611SJerry Jelinek 813a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx); 814a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 815a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 816a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 817a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 818a2afb611SJerry Jelinek } 819a2afb611SJerry Jelinek 8203b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 821fa9e4066Sahrens 822feaa74e4SMark Maybee /* 823feaa74e4SMark Maybee * If we are creating a clone, make sure we zero out any stale 824feaa74e4SMark Maybee * data from the origin snapshots zil header. 825feaa74e4SMark Maybee */ 8263b2aab18SMatthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 827feaa74e4SMark Maybee dsl_dataset_t *ds; 828feaa74e4SMark Maybee 8293b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 8303b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 831feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 832feaa74e4SMark Maybee } 833feaa74e4SMark Maybee 8341d452cf5Sahrens return (dsobj); 835fa9e4066Sahrens } 836fa9e4066Sahrens 8371d452cf5Sahrens /* 8383b2aab18SMatthew Ahrens * The unique space in the head dataset can be calculated by subtracting 8393b2aab18SMatthew Ahrens * the space used in the most recent snapshot, that is still being used 8403b2aab18SMatthew Ahrens * in this file system, from the space currently in use. To figure out 8413b2aab18SMatthew Ahrens * the space in the most recent snapshot still in use, we need to take 8423b2aab18SMatthew Ahrens * the total space used in the snapshot and subtract out the space that 8433b2aab18SMatthew Ahrens * has been freed up since the snapshot was taken. 8441d452cf5Sahrens */ 8453b2aab18SMatthew Ahrens void 8463b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 8471d452cf5Sahrens { 8483b2aab18SMatthew Ahrens uint64_t mrs_used; 8493b2aab18SMatthew Ahrens uint64_t dlused, dlcomp, dluncomp; 8501d452cf5Sahrens 8513b2aab18SMatthew Ahrens ASSERT(!dsl_dataset_is_snapshot(ds)); 8521d452cf5Sahrens 853*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) 854*c1379625SJustin T. Gibbs mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes; 8553b2aab18SMatthew Ahrens else 8563b2aab18SMatthew Ahrens mrs_used = 0; 857842727c2SChris Kirby 8583b2aab18SMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 859fa9e4066Sahrens 8603b2aab18SMatthew Ahrens ASSERT3U(dlused, <=, mrs_used); 861*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 862*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused); 86319b94df9SMatthew Ahrens 8643b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 8653b2aab18SMatthew Ahrens SPA_VERSION_UNIQUE_ACCURATE) 866*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 867fa9e4066Sahrens } 868fa9e4066Sahrens 8693b2aab18SMatthew Ahrens void 8703b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 8713b2aab18SMatthew Ahrens dmu_tx_t *tx) 872842727c2SChris Kirby { 8733b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 8743b2aab18SMatthew Ahrens uint64_t count; 8753b2aab18SMatthew Ahrens int err; 8763b2aab18SMatthew Ahrens 877*c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2); 878*c1379625SJustin T. Gibbs err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 879*c1379625SJustin T. Gibbs obj, tx); 8803b2aab18SMatthew Ahrens /* 8813b2aab18SMatthew Ahrens * The err should not be ENOENT, but a bug in a previous version 8823b2aab18SMatthew Ahrens * of the code could cause upgrade_clones_cb() to not set 8833b2aab18SMatthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry. 8843b2aab18SMatthew Ahrens * If we knew that the pool was created after 8853b2aab18SMatthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 8863b2aab18SMatthew Ahrens * ENOENT. However, at least we can check that we don't have 8873b2aab18SMatthew Ahrens * too many entries in the next_clones_obj even after failing to 8883b2aab18SMatthew Ahrens * remove this one. 8893b2aab18SMatthew Ahrens */ 8903b2aab18SMatthew Ahrens if (err != ENOENT) 8913b2aab18SMatthew Ahrens VERIFY0(err); 892*c1379625SJustin T. Gibbs ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 8933b2aab18SMatthew Ahrens &count)); 894*c1379625SJustin T. Gibbs ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2); 8953b2aab18SMatthew Ahrens } 896842727c2SChris Kirby 897842727c2SChris Kirby 8983b2aab18SMatthew Ahrens blkptr_t * 8993b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds) 9003b2aab18SMatthew Ahrens { 901*c1379625SJustin T. Gibbs return (&dsl_dataset_phys(ds)->ds_bp); 902842727c2SChris Kirby } 903842727c2SChris Kirby 9043b2aab18SMatthew Ahrens void 9053b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 906842727c2SChris Kirby { 9073b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 9083b2aab18SMatthew Ahrens /* If it's the meta-objset, set dp_meta_rootbp */ 9093b2aab18SMatthew Ahrens if (ds == NULL) { 9103b2aab18SMatthew Ahrens tx->tx_pool->dp_meta_rootbp = *bp; 9113b2aab18SMatthew Ahrens } else { 9123b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 913*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_bp = *bp; 914842727c2SChris Kirby } 9153b2aab18SMatthew Ahrens } 916842727c2SChris Kirby 9173b2aab18SMatthew Ahrens spa_t * 9183b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 9193b2aab18SMatthew Ahrens { 9203b2aab18SMatthew Ahrens return (ds->ds_dir->dd_pool->dp_spa); 921842727c2SChris Kirby } 922842727c2SChris Kirby 9233b2aab18SMatthew Ahrens void 9243b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 925fa9e4066Sahrens { 9263b2aab18SMatthew Ahrens dsl_pool_t *dp; 927842727c2SChris Kirby 9283b2aab18SMatthew Ahrens if (ds == NULL) /* this is the meta-objset */ 9293b2aab18SMatthew Ahrens return; 9301d452cf5Sahrens 9313b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 932fa9e4066Sahrens 933*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) 9343b2aab18SMatthew Ahrens panic("dirtying snapshot!"); 935fa9e4066Sahrens 9363b2aab18SMatthew Ahrens dp = ds->ds_dir->dd_pool; 937ce636f8bSMatthew Ahrens 9383b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 9393b2aab18SMatthew Ahrens /* up the hold count until we can be written out */ 9403b2aab18SMatthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 9413b2aab18SMatthew Ahrens } 9423b2aab18SMatthew Ahrens } 943fa9e4066Sahrens 9442e2c1355SMatthew Ahrens boolean_t 9452e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds) 9462e2c1355SMatthew Ahrens { 9472e2c1355SMatthew Ahrens for (int t = 0; t < TXG_SIZE; t++) { 9482e2c1355SMatthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 9492e2c1355SMatthew Ahrens ds, t)) 9502e2c1355SMatthew Ahrens return (B_TRUE); 9512e2c1355SMatthew Ahrens } 9522e2c1355SMatthew Ahrens return (B_FALSE); 9532e2c1355SMatthew Ahrens } 9542e2c1355SMatthew Ahrens 955fa9e4066Sahrens static int 9563b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 957fa9e4066Sahrens { 9583b2aab18SMatthew Ahrens uint64_t asize; 959fa9e4066Sahrens 9603b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 96188b7b0f2SMatthew Ahrens return (0); 962fa9e4066Sahrens 963e1930233Sbonwick /* 9643b2aab18SMatthew Ahrens * If there's an fs-only reservation, any blocks that might become 9653b2aab18SMatthew Ahrens * owned by the snapshot dataset must be accommodated by space 9663b2aab18SMatthew Ahrens * outside of the reservation. 967e1930233Sbonwick */ 9683b2aab18SMatthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 969*c1379625SJustin T. Gibbs asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved); 9703b2aab18SMatthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 971be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 972e1930233Sbonwick 9733cb34c60Sahrens /* 9743b2aab18SMatthew Ahrens * Propagate any reserved space for this snapshot to other 9753b2aab18SMatthew Ahrens * snapshot checks in this sync group. 9763cb34c60Sahrens */ 9773b2aab18SMatthew Ahrens if (asize > 0) 9783b2aab18SMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx); 9793cb34c60Sahrens 980e1930233Sbonwick return (0); 981e1930233Sbonwick } 982e1930233Sbonwick 9833b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg { 9843b2aab18SMatthew Ahrens nvlist_t *ddsa_snaps; 9853b2aab18SMatthew Ahrens nvlist_t *ddsa_props; 9863b2aab18SMatthew Ahrens nvlist_t *ddsa_errors; 987a2afb611SJerry Jelinek cred_t *ddsa_cr; 9883b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t; 989842727c2SChris Kirby 9903cb34c60Sahrens int 9913b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 992a2afb611SJerry Jelinek dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr) 9931d452cf5Sahrens { 9943b2aab18SMatthew Ahrens int error; 9953b2aab18SMatthew Ahrens uint64_t value; 996fa9e4066Sahrens 9973b2aab18SMatthew Ahrens ds->ds_trysnap_txg = tx->tx_txg; 998745cd3c5Smaybee 9993b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 1000842727c2SChris Kirby return (0); 1001fa9e4066Sahrens 1002fa9e4066Sahrens /* 10033b2aab18SMatthew Ahrens * We don't allow multiple snapshots of the same txg. If there 10043b2aab18SMatthew Ahrens * is already one, try again. 1005fa9e4066Sahrens */ 1006*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) 1007be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 1008fa9e4066Sahrens 1009fa9e4066Sahrens /* 10103b2aab18SMatthew Ahrens * Check for conflicting snapshot name. 1011fa9e4066Sahrens */ 10123b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value); 10133b2aab18SMatthew Ahrens if (error == 0) 1014be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 10153b2aab18SMatthew Ahrens if (error != ENOENT) 10163b2aab18SMatthew Ahrens return (error); 1017842727c2SChris Kirby 1018ca48f36fSKeith M Wesolowski /* 1019ca48f36fSKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as 1020ca48f36fSKeith M Wesolowski * those into which we are currently receiving. However, if we are 1021ca48f36fSKeith M Wesolowski * creating this snapshot as part of a receive, this check will be 1022ca48f36fSKeith M Wesolowski * executed atomically with respect to the completion of the receive 1023ca48f36fSKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 1024ca48f36fSKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in 1025ca48f36fSKeith M Wesolowski * dmu_recv_end_sync(). 1026ca48f36fSKeith M Wesolowski */ 1027ca48f36fSKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds)) 1028ca48f36fSKeith M Wesolowski return (SET_ERROR(EBUSY)); 1029ca48f36fSKeith M Wesolowski 1030a2afb611SJerry Jelinek /* 1031a2afb611SJerry Jelinek * Skip the check for temporary snapshots or if we have already checked 1032a2afb611SJerry Jelinek * the counts in dsl_dataset_snapshot_check. This means we really only 1033a2afb611SJerry Jelinek * check the count here when we're receiving a stream. 1034a2afb611SJerry Jelinek */ 1035a2afb611SJerry Jelinek if (cnt != 0 && cr != NULL) { 1036a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1037a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr); 1038a2afb611SJerry Jelinek if (error != 0) 1039a2afb611SJerry Jelinek return (error); 1040a2afb611SJerry Jelinek } 1041a2afb611SJerry Jelinek 10423b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 10433b2aab18SMatthew Ahrens if (error != 0) 10443b2aab18SMatthew Ahrens return (error); 1045842727c2SChris Kirby 10461d452cf5Sahrens return (0); 10471d452cf5Sahrens } 10481d452cf5Sahrens 10493b2aab18SMatthew Ahrens static int 10503b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 1051745cd3c5Smaybee { 10523b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 10533b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 10543b2aab18SMatthew Ahrens nvpair_t *pair; 10553b2aab18SMatthew Ahrens int rv = 0; 10563b2aab18SMatthew Ahrens 1057a2afb611SJerry Jelinek /* 1058a2afb611SJerry Jelinek * Pre-compute how many total new snapshots will be created for each 1059a2afb611SJerry Jelinek * level in the tree and below. This is needed for validating the 1060a2afb611SJerry Jelinek * snapshot limit when either taking a recursive snapshot or when 1061a2afb611SJerry Jelinek * taking multiple snapshots. 1062a2afb611SJerry Jelinek * 1063a2afb611SJerry Jelinek * The problem is that the counts are not actually adjusted when 1064a2afb611SJerry Jelinek * we are checking, only when we finally sync. For a single snapshot, 1065a2afb611SJerry Jelinek * this is easy, the count will increase by 1 at each node up the tree, 1066a2afb611SJerry Jelinek * but its more complicated for the recursive/multiple snapshot case. 1067a2afb611SJerry Jelinek * 1068a2afb611SJerry Jelinek * The dsl_fs_ss_limit_check function does recursively check the count 1069a2afb611SJerry Jelinek * at each level up the tree but since it is validating each snapshot 1070a2afb611SJerry Jelinek * independently we need to be sure that we are validating the complete 1071a2afb611SJerry Jelinek * count for the entire set of snapshots. We do this by rolling up the 1072a2afb611SJerry Jelinek * counts for each component of the name into an nvlist and then 1073a2afb611SJerry Jelinek * checking each of those cases with the aggregated count. 1074a2afb611SJerry Jelinek * 1075a2afb611SJerry Jelinek * This approach properly handles not only the recursive snapshot 1076a2afb611SJerry Jelinek * case (where we get all of those on the ddsa_snaps list) but also 1077a2afb611SJerry Jelinek * the sibling case (e.g. snapshot a/b and a/c so that we will also 1078a2afb611SJerry Jelinek * validate the limit on 'a' using a count of 2). 1079a2afb611SJerry Jelinek * 1080a2afb611SJerry Jelinek * We validate the snapshot names in the third loop and only report 1081a2afb611SJerry Jelinek * name errors once. 1082a2afb611SJerry Jelinek */ 1083a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) { 1084a2afb611SJerry Jelinek nvlist_t *cnt_track = NULL; 1085a2afb611SJerry Jelinek cnt_track = fnvlist_alloc(); 1086a2afb611SJerry Jelinek 1087a2afb611SJerry Jelinek /* Rollup aggregated counts into the cnt_track list */ 1088a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1089a2afb611SJerry Jelinek pair != NULL; 1090a2afb611SJerry Jelinek pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1091a2afb611SJerry Jelinek char *pdelim; 1092a2afb611SJerry Jelinek uint64_t val; 1093a2afb611SJerry Jelinek char nm[MAXPATHLEN]; 1094a2afb611SJerry Jelinek 1095a2afb611SJerry Jelinek (void) strlcpy(nm, nvpair_name(pair), sizeof (nm)); 1096a2afb611SJerry Jelinek pdelim = strchr(nm, '@'); 1097a2afb611SJerry Jelinek if (pdelim == NULL) 1098a2afb611SJerry Jelinek continue; 1099a2afb611SJerry Jelinek *pdelim = '\0'; 1100a2afb611SJerry Jelinek 1101a2afb611SJerry Jelinek do { 1102a2afb611SJerry Jelinek if (nvlist_lookup_uint64(cnt_track, nm, 1103a2afb611SJerry Jelinek &val) == 0) { 1104a2afb611SJerry Jelinek /* update existing entry */ 1105a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1106a2afb611SJerry Jelinek val + 1); 1107a2afb611SJerry Jelinek } else { 1108a2afb611SJerry Jelinek /* add to list */ 1109a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1); 1110a2afb611SJerry Jelinek } 1111a2afb611SJerry Jelinek 1112a2afb611SJerry Jelinek pdelim = strrchr(nm, '/'); 1113a2afb611SJerry Jelinek if (pdelim != NULL) 1114a2afb611SJerry Jelinek *pdelim = '\0'; 1115a2afb611SJerry Jelinek } while (pdelim != NULL); 1116a2afb611SJerry Jelinek } 1117a2afb611SJerry Jelinek 1118a2afb611SJerry Jelinek /* Check aggregated counts at each level */ 1119a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(cnt_track, NULL); 1120a2afb611SJerry Jelinek pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) { 1121a2afb611SJerry Jelinek int error = 0; 1122a2afb611SJerry Jelinek char *name; 1123a2afb611SJerry Jelinek uint64_t cnt = 0; 1124a2afb611SJerry Jelinek dsl_dataset_t *ds; 1125a2afb611SJerry Jelinek 1126a2afb611SJerry Jelinek name = nvpair_name(pair); 1127a2afb611SJerry Jelinek cnt = fnvpair_value_uint64(pair); 1128a2afb611SJerry Jelinek ASSERT(cnt > 0); 1129a2afb611SJerry Jelinek 1130a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, name, FTAG, &ds); 1131a2afb611SJerry Jelinek if (error == 0) { 1132a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1133a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, 1134a2afb611SJerry Jelinek ddsa->ddsa_cr); 1135a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 1136a2afb611SJerry Jelinek } 1137a2afb611SJerry Jelinek 1138a2afb611SJerry Jelinek if (error != 0) { 1139a2afb611SJerry Jelinek if (ddsa->ddsa_errors != NULL) 1140a2afb611SJerry Jelinek fnvlist_add_int32(ddsa->ddsa_errors, 1141a2afb611SJerry Jelinek name, error); 1142a2afb611SJerry Jelinek rv = error; 1143a2afb611SJerry Jelinek /* only report one error for this check */ 1144a2afb611SJerry Jelinek break; 1145a2afb611SJerry Jelinek } 1146a2afb611SJerry Jelinek } 1147a2afb611SJerry Jelinek nvlist_free(cnt_track); 1148a2afb611SJerry Jelinek } 1149a2afb611SJerry Jelinek 11503b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 11513b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 11523b2aab18SMatthew Ahrens int error = 0; 11533b2aab18SMatthew Ahrens dsl_dataset_t *ds; 11543b2aab18SMatthew Ahrens char *name, *atp; 11553b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 11563b2aab18SMatthew Ahrens 11573b2aab18SMatthew Ahrens name = nvpair_name(pair); 11583b2aab18SMatthew Ahrens if (strlen(name) >= MAXNAMELEN) 1159be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 11603b2aab18SMatthew Ahrens if (error == 0) { 11613b2aab18SMatthew Ahrens atp = strchr(name, '@'); 11623b2aab18SMatthew Ahrens if (atp == NULL) 1163be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 11643b2aab18SMatthew Ahrens if (error == 0) 11653b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 11663b2aab18SMatthew Ahrens } 11673b2aab18SMatthew Ahrens if (error == 0) 11683b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 11693b2aab18SMatthew Ahrens if (error == 0) { 1170a2afb611SJerry Jelinek /* passing 0/NULL skips dsl_fs_ss_limit_check */ 11713b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 1172a2afb611SJerry Jelinek atp + 1, tx, B_FALSE, 0, NULL); 11733b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 11743b2aab18SMatthew Ahrens } 1175745cd3c5Smaybee 11763b2aab18SMatthew Ahrens if (error != 0) { 11773b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 11783b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 11793b2aab18SMatthew Ahrens name, error); 11803b2aab18SMatthew Ahrens } 11813b2aab18SMatthew Ahrens rv = error; 11823b2aab18SMatthew Ahrens } 11833b2aab18SMatthew Ahrens } 1184a2afb611SJerry Jelinek 11853b2aab18SMatthew Ahrens return (rv); 1186745cd3c5Smaybee } 1187745cd3c5Smaybee 11883b2aab18SMatthew Ahrens void 11893b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 11903b2aab18SMatthew Ahrens dmu_tx_t *tx) 1191745cd3c5Smaybee { 11923b2aab18SMatthew Ahrens static zil_header_t zero_zil; 1193745cd3c5Smaybee 11943b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 11953b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 11963b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 11973b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 11983b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 11993b2aab18SMatthew Ahrens objset_t *os; 1200745cd3c5Smaybee 12013b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1202c33e334fSMatthew Ahrens 1203c33e334fSMatthew Ahrens /* 12043b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 12053b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1206c33e334fSMatthew Ahrens */ 12073b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 12083b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 12093b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 12103b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1211c33e334fSMatthew Ahrens 1212a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx); 1213cde58dbcSMatthew Ahrens 1214cde58dbcSMatthew Ahrens /* 12153b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1216088f3894Sahrens */ 1217088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1218088f3894Sahrens crtxg = 1; 1219088f3894Sahrens else 1220088f3894Sahrens crtxg = tx->tx_txg; 1221088f3894Sahrens 12221649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 12231649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 12243b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1225fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1226fa9e4066Sahrens dsphys = dbuf->db_data; 1227745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 12281d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1229fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1230fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1231fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1232*c1379625SJustin T. Gibbs dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 1233*c1379625SJustin T. Gibbs dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 1234fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1235fa9e4066Sahrens dsphys->ds_num_children = 1; 1236fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1237088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1238*c1379625SJustin T. Gibbs dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 1239*c1379625SJustin T. Gibbs dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes; 1240*c1379625SJustin T. Gibbs dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes; 1241*c1379625SJustin T. Gibbs dsphys->ds_uncompressed_bytes = 1242*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes; 1243*c1379625SJustin T. Gibbs dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags; 1244*c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp; 1245ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1246fa9e4066Sahrens 1247b5152584SMatthew Ahrens if (ds->ds_large_blocks) 1248b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx); 1249b5152584SMatthew Ahrens 1250*c1379625SJustin T. Gibbs ASSERT3U(ds->ds_prev != 0, ==, 1251*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 12521d452cf5Sahrens if (ds->ds_prev) { 1253088f3894Sahrens uint64_t next_clones_obj = 1254*c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj; 1255*c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1256fa9e4066Sahrens ds->ds_object || 1257*c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1); 1258*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1259*c1379625SJustin T. Gibbs ds->ds_object) { 12601d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1261*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 1262*c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_creation_txg); 1263*c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj; 1264088f3894Sahrens } else if (next_clones_obj != 0) { 12653b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1266c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 12673b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1268088f3894Sahrens next_clones_obj, dsobj, tx)); 1269fa9e4066Sahrens } 1270fa9e4066Sahrens } 1271fa9e4066Sahrens 1272a9799022Sck /* 1273a9799022Sck * If we have a reference-reservation on this dataset, we will 1274a9799022Sck * need to increase the amount of refreservation being charged 1275a9799022Sck * since our unique space is going to zero. 1276a9799022Sck */ 1277a9799022Sck if (ds->ds_reserved) { 12783f9d6ad7SLin Ling int64_t delta; 12793f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 1280*c1379625SJustin T. Gibbs delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, 1281*c1379625SJustin T. Gibbs ds->ds_reserved); 128274e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 12833f9d6ad7SLin Ling delta, 0, 0, tx); 1284a9799022Sck } 1285a9799022Sck 1286fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1287*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 1288*c1379625SJustin T. Gibbs dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX, 1289*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, tx); 1290cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1291*c1379625SJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos, 1292*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj); 1293cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1294*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 1295cde58dbcSMatthew Ahrens 1296*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg); 1297*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj; 1298*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg; 1299*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 0; 1300a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1301*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1302fa9e4066Sahrens 1303*c1379625SJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj, 13043b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1305fa9e4066Sahrens 1306fa9e4066Sahrens if (ds->ds_prev) 13073b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 13083b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1309*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev)); 1310ecd6cf80Smarks 13113f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1312088f3894Sahrens 131371eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 131471eb0538SChris Kirby 13154445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1316fa9e4066Sahrens } 1317fa9e4066Sahrens 13183b2aab18SMatthew Ahrens static void 13193b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1320fa9e4066Sahrens { 13213b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 13223b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 13233b2aab18SMatthew Ahrens nvpair_t *pair; 132491ebeef5Sahrens 13253b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 13263b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 13273b2aab18SMatthew Ahrens dsl_dataset_t *ds; 13283b2aab18SMatthew Ahrens char *name, *atp; 13293b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 13303b2aab18SMatthew Ahrens 13313b2aab18SMatthew Ahrens name = nvpair_name(pair); 13323b2aab18SMatthew Ahrens atp = strchr(name, '@'); 13333b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 13343b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 13353b2aab18SMatthew Ahrens 13363b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 13373b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 13383b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 13393b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 13403b2aab18SMatthew Ahrens } 13413b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 13423b2aab18SMatthew Ahrens } 1343fa9e4066Sahrens } 1344fa9e4066Sahrens 13453b2aab18SMatthew Ahrens /* 13463b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 13473b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 13483b2aab18SMatthew Ahrens */ 13493b2aab18SMatthew Ahrens int 13503b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 135119b94df9SMatthew Ahrens { 13523b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 13533b2aab18SMatthew Ahrens nvpair_t *pair; 13543b2aab18SMatthew Ahrens boolean_t needsuspend; 13553b2aab18SMatthew Ahrens int error; 13563b2aab18SMatthew Ahrens spa_t *spa; 13573b2aab18SMatthew Ahrens char *firstname; 13583b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 135919b94df9SMatthew Ahrens 13603b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 13613b2aab18SMatthew Ahrens if (pair == NULL) 13623b2aab18SMatthew Ahrens return (0); 13633b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 13643b2aab18SMatthew Ahrens 13653b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 13663b2aab18SMatthew Ahrens if (error != 0) 13673b2aab18SMatthew Ahrens return (error); 13683b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 13693b2aab18SMatthew Ahrens spa_close(spa, FTAG); 13703b2aab18SMatthew Ahrens 13713b2aab18SMatthew Ahrens if (needsuspend) { 13723b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 13733b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 13743b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 13753b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 13763b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 13773b2aab18SMatthew Ahrens char *atp; 13783b2aab18SMatthew Ahrens void *cookie; 13793b2aab18SMatthew Ahrens 13803b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 13813b2aab18SMatthew Ahrens if (atp == NULL) { 1382be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 13833b2aab18SMatthew Ahrens break; 13843b2aab18SMatthew Ahrens } 13853b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 13863b2aab18SMatthew Ahrens 13873b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 13883b2aab18SMatthew Ahrens if (error != 0) 13893b2aab18SMatthew Ahrens break; 13903b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 13913b2aab18SMatthew Ahrens (uintptr_t)cookie); 13923b2aab18SMatthew Ahrens } 13933b2aab18SMatthew Ahrens } 13943b2aab18SMatthew Ahrens 13953b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 13963b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 13973b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 1398a2afb611SJerry Jelinek ddsa.ddsa_cr = CRED(); 13993b2aab18SMatthew Ahrens 14003b2aab18SMatthew Ahrens if (error == 0) { 14013b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 14023b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 14037d46dc6cSMatthew Ahrens fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL); 14043b2aab18SMatthew Ahrens } 14053b2aab18SMatthew Ahrens 14063b2aab18SMatthew Ahrens if (suspended != NULL) { 14073b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 14083b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 14093b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 14103b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 14113b2aab18SMatthew Ahrens } 14123b2aab18SMatthew Ahrens fnvlist_free(suspended); 14133b2aab18SMatthew Ahrens } 14143b2aab18SMatthew Ahrens 14153b2aab18SMatthew Ahrens return (error); 14163b2aab18SMatthew Ahrens } 14173b2aab18SMatthew Ahrens 14183b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 14193b2aab18SMatthew Ahrens const char *ddsta_fsname; 14203b2aab18SMatthew Ahrens const char *ddsta_snapname; 14213b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 14223b2aab18SMatthew Ahrens const char *ddsta_htag; 14233b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 14243b2aab18SMatthew Ahrens 14253b2aab18SMatthew Ahrens static int 14263b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 14273b2aab18SMatthew Ahrens { 14283b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 14293b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14303b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14313b2aab18SMatthew Ahrens int error; 14323b2aab18SMatthew Ahrens 14333b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 14343b2aab18SMatthew Ahrens if (error != 0) 14353b2aab18SMatthew Ahrens return (error); 14363b2aab18SMatthew Ahrens 1437a2afb611SJerry Jelinek /* NULL cred means no limit check for tmp snapshot */ 1438ca48f36fSKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1439a2afb611SJerry Jelinek tx, B_FALSE, 0, NULL); 14403b2aab18SMatthew Ahrens if (error != 0) { 14413b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14423b2aab18SMatthew Ahrens return (error); 14433b2aab18SMatthew Ahrens } 14443b2aab18SMatthew Ahrens 14453b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 14463b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1447be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 14483b2aab18SMatthew Ahrens } 14493b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 14503b2aab18SMatthew Ahrens B_TRUE, tx); 14513b2aab18SMatthew Ahrens if (error != 0) { 14523b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14533b2aab18SMatthew Ahrens return (error); 14543b2aab18SMatthew Ahrens } 14553b2aab18SMatthew Ahrens 14563b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14573b2aab18SMatthew Ahrens return (0); 14583b2aab18SMatthew Ahrens } 14593b2aab18SMatthew Ahrens 14603b2aab18SMatthew Ahrens static void 14613b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 14623b2aab18SMatthew Ahrens { 14633b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 14643b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14653b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14663b2aab18SMatthew Ahrens 14673b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 14683b2aab18SMatthew Ahrens 14693b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 14703b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 14713b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 14723b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 14733b2aab18SMatthew Ahrens 14743b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14753b2aab18SMatthew Ahrens } 14763b2aab18SMatthew Ahrens 14773b2aab18SMatthew Ahrens int 14783b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 14793b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 14803b2aab18SMatthew Ahrens { 14813b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 14823b2aab18SMatthew Ahrens int error; 14833b2aab18SMatthew Ahrens spa_t *spa; 14843b2aab18SMatthew Ahrens boolean_t needsuspend; 14853b2aab18SMatthew Ahrens void *cookie; 14863b2aab18SMatthew Ahrens 14873b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 14883b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 14893b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 14903b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 14913b2aab18SMatthew Ahrens 14923b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 14933b2aab18SMatthew Ahrens if (error != 0) 14943b2aab18SMatthew Ahrens return (error); 14953b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 14963b2aab18SMatthew Ahrens spa_close(spa, FTAG); 14973b2aab18SMatthew Ahrens 14983b2aab18SMatthew Ahrens if (needsuspend) { 14993b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 15003b2aab18SMatthew Ahrens if (error != 0) 15013b2aab18SMatthew Ahrens return (error); 15023b2aab18SMatthew Ahrens } 15033b2aab18SMatthew Ahrens 15043b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 15057d46dc6cSMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED); 15063b2aab18SMatthew Ahrens 15073b2aab18SMatthew Ahrens if (needsuspend) 15083b2aab18SMatthew Ahrens zil_resume(cookie); 15093b2aab18SMatthew Ahrens return (error); 15103b2aab18SMatthew Ahrens } 15113b2aab18SMatthew Ahrens 15123b2aab18SMatthew Ahrens 15133b2aab18SMatthew Ahrens void 15143b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 15153b2aab18SMatthew Ahrens { 15163b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 15173b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1518*c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0); 15193b2aab18SMatthew Ahrens 15203b2aab18SMatthew Ahrens /* 15213b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 15223b2aab18SMatthew Ahrens * sync it out now. 15233b2aab18SMatthew Ahrens */ 15243b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1525*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid; 15263b2aab18SMatthew Ahrens 15273b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 1528b5152584SMatthew Ahrens 1529b5152584SMatthew Ahrens if (ds->ds_need_large_blocks && !ds->ds_large_blocks) { 1530b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx); 1531b5152584SMatthew Ahrens ds->ds_large_blocks = B_TRUE; 1532b5152584SMatthew Ahrens } 15333b2aab18SMatthew Ahrens } 15343b2aab18SMatthew Ahrens 15353b2aab18SMatthew Ahrens static void 15363b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 15373b2aab18SMatthew Ahrens { 15383b2aab18SMatthew Ahrens uint64_t count = 0; 15393b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 15403b2aab18SMatthew Ahrens zap_cursor_t zc; 15413b2aab18SMatthew Ahrens zap_attribute_t za; 15423b2aab18SMatthew Ahrens nvlist_t *propval = fnvlist_alloc(); 15433b2aab18SMatthew Ahrens nvlist_t *val = fnvlist_alloc(); 15443b2aab18SMatthew Ahrens 15453b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 154619b94df9SMatthew Ahrens 154719b94df9SMatthew Ahrens /* 15483b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 154919b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 155019b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 155119b94df9SMatthew Ahrens */ 1552*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 1553*c1379625SJustin T. Gibbs VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 155419b94df9SMatthew Ahrens &count)); 155519b94df9SMatthew Ahrens } 1556*c1379625SJustin T. Gibbs if (count != dsl_dataset_phys(ds)->ds_num_children - 1) 155719b94df9SMatthew Ahrens goto fail; 1558*c1379625SJustin T. Gibbs for (zap_cursor_init(&zc, mos, 1559*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 156019b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 156119b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 156219b94df9SMatthew Ahrens dsl_dataset_t *clone; 156319b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 15643b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 15653b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 156619b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 15673b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 156819b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 156919b94df9SMatthew Ahrens } 157019b94df9SMatthew Ahrens zap_cursor_fini(&zc); 15713b2aab18SMatthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 15723b2aab18SMatthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 157319b94df9SMatthew Ahrens fail: 157419b94df9SMatthew Ahrens nvlist_free(val); 157519b94df9SMatthew Ahrens nvlist_free(propval); 157619b94df9SMatthew Ahrens } 157719b94df9SMatthew Ahrens 1578fa9e4066Sahrens void 1579a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1580fa9e4066Sahrens { 15813b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1582187d6ac0SMatt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio; 1583a9799022Sck 15843b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 15853b2aab18SMatthew Ahrens 1586*c1379625SJustin T. Gibbs ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 : 1587*c1379625SJustin T. Gibbs (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 / 1588*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes); 15894445fffbSMatthew Ahrens 15904445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 159177372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 1592*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes); 15934445fffbSMatthew Ahrens 15944445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 15954445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 15964445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 1597*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 15984445fffbSMatthew Ahrens get_clones_stat(ds, nv); 15994445fffbSMatthew Ahrens } else { 1600b461c746SMatthew Ahrens if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) { 1601b461c746SMatthew Ahrens char buf[MAXNAMELEN]; 1602b461c746SMatthew Ahrens dsl_dataset_name(ds->ds_prev, buf); 1603b461c746SMatthew Ahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf); 1604b461c746SMatthew Ahrens } 1605b461c746SMatthew Ahrens 16064445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 16074445fffbSMatthew Ahrens } 1608fa9e4066Sahrens 1609a9799022Sck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1610a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1611a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1612a9799022Sck 1613a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1614*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_time); 1615a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1616*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg); 1617a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1618a9799022Sck ds->ds_quota); 1619a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1620a9799022Sck ds->ds_reserved); 1621c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1622*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_guid); 16231d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 1624*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 16251d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 16261d713200SEric Schrock ds->ds_object); 162792241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 162892241e0bSTom Erickson ds->ds_userrefs); 1629842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1630842727c2SChris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1631fa9e4066Sahrens 1632*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 163319b94df9SMatthew Ahrens uint64_t written, comp, uncomp; 163419b94df9SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 163519b94df9SMatthew Ahrens dsl_dataset_t *prev; 163619b94df9SMatthew Ahrens 163719b94df9SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, 1638*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 163919b94df9SMatthew Ahrens if (err == 0) { 164019b94df9SMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written, 164119b94df9SMatthew Ahrens &comp, &uncomp); 164219b94df9SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 164319b94df9SMatthew Ahrens if (err == 0) { 164419b94df9SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 164519b94df9SMatthew Ahrens written); 164619b94df9SMatthew Ahrens } 164719b94df9SMatthew Ahrens } 164819b94df9SMatthew Ahrens } 1649fa9e4066Sahrens } 1650fa9e4066Sahrens 1651a2eea2e1Sahrens void 1652a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1653a2eea2e1Sahrens { 16543b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 16553b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 16563b2aab18SMatthew Ahrens 1657*c1379625SJustin T. Gibbs stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg; 1658*c1379625SJustin T. Gibbs stat->dds_inconsistent = 1659*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT; 1660*c1379625SJustin T. Gibbs stat->dds_guid = dsl_dataset_phys(ds)->ds_guid; 16614445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 16624445fffbSMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 1663a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 1664*c1379625SJustin T. Gibbs stat->dds_num_clones = 1665*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children - 1; 1666ebedde84SEric Taylor } else { 1667ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 1668ebedde84SEric Taylor stat->dds_num_clones = 0; 1669a2eea2e1Sahrens 16704445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 16714445fffbSMatthew Ahrens dsl_dataset_t *ods; 1672a2eea2e1Sahrens 16733b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1674*c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj, 1675*c1379625SJustin T. Gibbs FTAG, &ods)); 16764445fffbSMatthew Ahrens dsl_dataset_name(ods, stat->dds_origin); 16773b2aab18SMatthew Ahrens dsl_dataset_rele(ods, FTAG); 16784445fffbSMatthew Ahrens } 1679a2eea2e1Sahrens } 1680a2eea2e1Sahrens } 1681a2eea2e1Sahrens 1682a2eea2e1Sahrens uint64_t 1683a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1684a2eea2e1Sahrens { 168591ebeef5Sahrens return (ds->ds_fsid_guid); 1686a2eea2e1Sahrens } 1687a2eea2e1Sahrens 1688a2eea2e1Sahrens void 1689a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 1690a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 1691a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1692fa9e4066Sahrens { 1693*c1379625SJustin T. Gibbs *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes; 1694a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1695*c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) 1696*c1379625SJustin T. Gibbs *availbytesp += 1697*c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 1698a9799022Sck if (ds->ds_quota != 0) { 1699a9799022Sck /* 1700a9799022Sck * Adjust available bytes according to refquota 1701a9799022Sck */ 1702a9799022Sck if (*refdbytesp < ds->ds_quota) 1703a9799022Sck *availbytesp = MIN(*availbytesp, 1704a9799022Sck ds->ds_quota - *refdbytesp); 1705a9799022Sck else 1706a9799022Sck *availbytesp = 0; 1707a9799022Sck } 1708*c1379625SJustin T. Gibbs *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp); 1709a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1710fa9e4066Sahrens } 1711fa9e4066Sahrens 1712f18faf3fSek boolean_t 171334f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 1714f18faf3fSek { 1715f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 1716f18faf3fSek 17173b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 171834f2f8cfSMatthew Ahrens if (snap == NULL) 1719f18faf3fSek return (B_FALSE); 1720*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_bp.blk_birth > 1721*c1379625SJustin T. Gibbs dsl_dataset_phys(snap)->ds_creation_txg) { 172234f2f8cfSMatthew Ahrens objset_t *os, *os_snap; 17236e0cbcaaSMatthew Ahrens /* 17246e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 17256e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 17266e0cbcaaSMatthew Ahrens * modified. 17276e0cbcaaSMatthew Ahrens */ 17286e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 17296e0cbcaaSMatthew Ahrens return (B_TRUE); 173034f2f8cfSMatthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0) 17316e0cbcaaSMatthew Ahrens return (B_TRUE); 17326e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 173334f2f8cfSMatthew Ahrens &os_snap->os_phys->os_meta_dnode, 17346e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 17356e0cbcaaSMatthew Ahrens } 1736f18faf3fSek return (B_FALSE); 1737f18faf3fSek } 1738f18faf3fSek 17393b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 17403b2aab18SMatthew Ahrens const char *ddrsa_fsname; 17413b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 17423b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 17433b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 17443b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 17453b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 17463b2aab18SMatthew Ahrens 17471d452cf5Sahrens /* ARGSUSED */ 1748fa9e4066Sahrens static int 17493b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 17503b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1751fa9e4066Sahrens { 17523b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 17533b2aab18SMatthew Ahrens int error; 1754fa9e4066Sahrens uint64_t val; 1755fa9e4066Sahrens 17563b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 17573b2aab18SMatthew Ahrens if (error != 0) { 17583b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 17593b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 17603b2aab18SMatthew Ahrens } 17611d452cf5Sahrens 17623b2aab18SMatthew Ahrens /* new name should not exist */ 17633b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 17643b2aab18SMatthew Ahrens if (error == 0) 1765be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 17663b2aab18SMatthew Ahrens else if (error == ENOENT) 17673b2aab18SMatthew Ahrens error = 0; 1768cdf5b4caSmmusante 1769cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 17703b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 17713b2aab18SMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN) 1772be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 1773cdf5b4caSmmusante 17743b2aab18SMatthew Ahrens return (error); 17751d452cf5Sahrens } 1776fa9e4066Sahrens 17773b2aab18SMatthew Ahrens static int 17783b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 17791d452cf5Sahrens { 17803b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 17813b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 17821d452cf5Sahrens dsl_dataset_t *hds; 17833b2aab18SMatthew Ahrens int error; 1784fa9e4066Sahrens 17853b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 17863b2aab18SMatthew Ahrens if (error != 0) 17873b2aab18SMatthew Ahrens return (error); 1788fa9e4066Sahrens 17893b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 17903b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 17913b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 17923b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 17933b2aab18SMatthew Ahrens } else { 17943b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 17953b2aab18SMatthew Ahrens } 1796745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 17973b2aab18SMatthew Ahrens return (error); 1798fa9e4066Sahrens } 1799fa9e4066Sahrens 1800cdf5b4caSmmusante static int 18013b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 18023b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1803cdf5b4caSmmusante { 18043b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 18053b2aab18SMatthew Ahrens dsl_dataset_t *ds; 18063b2aab18SMatthew Ahrens uint64_t val; 18073b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 18083b2aab18SMatthew Ahrens int error; 1809ecd6cf80Smarks 18103b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 18113b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 18123b2aab18SMatthew Ahrens if (error == ENOENT) { 18133b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 18143b2aab18SMatthew Ahrens return (0); 1815ecd6cf80Smarks } 1816ecd6cf80Smarks 18173b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 18183b2aab18SMatthew Ahrens 18193b2aab18SMatthew Ahrens /* log before we change the name */ 18203b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 18213b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 1822cdf5b4caSmmusante 1823a2afb611SJerry Jelinek VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx, 1824a2afb611SJerry Jelinek B_FALSE)); 18253b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 18263b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 18273b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 1828*c1379625SJustin T. Gibbs VERIFY0(zap_add(dp->dp_meta_objset, 1829*c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, 18303b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 1831cdf5b4caSmmusante 18323b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1833cdf5b4caSmmusante return (0); 1834cdf5b4caSmmusante } 1835cdf5b4caSmmusante 18363b2aab18SMatthew Ahrens static void 18373b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 1838cdf5b4caSmmusante { 18393b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 18403b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 18413b2aab18SMatthew Ahrens dsl_dataset_t *hds; 1842cdf5b4caSmmusante 18433b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 18443b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 18453b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 18463b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 18473b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 18483b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 18493b2aab18SMatthew Ahrens } else { 18503b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 1851cdf5b4caSmmusante } 18523b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 1853cdf5b4caSmmusante } 1854cdf5b4caSmmusante 18553b2aab18SMatthew Ahrens int 18563b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 18573b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 18583a5a36beSmmusante { 18593b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 18603a5a36beSmmusante 18613b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 18623b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 18633b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 18643b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 18653a5a36beSmmusante 18663b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 18677d46dc6cSMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 18687d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 18693a5a36beSmmusante } 18703a5a36beSmmusante 187191948b51SKeith M Wesolowski /* 187291948b51SKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is 187391948b51SKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here 187491948b51SKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want 187591948b51SKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away 187691948b51SKeith M Wesolowski * when we release the long hold. 187791948b51SKeith M Wesolowski */ 187891948b51SKeith M Wesolowski static int 187991948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 188091948b51SKeith M Wesolowski { 188191948b51SKeith M Wesolowski boolean_t held; 188291948b51SKeith M Wesolowski 188391948b51SKeith M Wesolowski if (!dmu_tx_is_syncing(tx)) 188491948b51SKeith M Wesolowski return (0); 188591948b51SKeith M Wesolowski 188691948b51SKeith M Wesolowski if (owner != NULL) { 188791948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner); 188891948b51SKeith M Wesolowski dsl_dataset_long_rele(ds, owner); 188991948b51SKeith M Wesolowski } 189091948b51SKeith M Wesolowski 189191948b51SKeith M Wesolowski held = dsl_dataset_long_held(ds); 189291948b51SKeith M Wesolowski 189391948b51SKeith M Wesolowski if (owner != NULL) 189491948b51SKeith M Wesolowski dsl_dataset_long_hold(ds, owner); 189591948b51SKeith M Wesolowski 189691948b51SKeith M Wesolowski if (held) 189791948b51SKeith M Wesolowski return (SET_ERROR(EBUSY)); 189891948b51SKeith M Wesolowski 189991948b51SKeith M Wesolowski return (0); 190091948b51SKeith M Wesolowski } 190191948b51SKeith M Wesolowski 190291948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg { 190391948b51SKeith M Wesolowski const char *ddra_fsname; 190491948b51SKeith M Wesolowski void *ddra_owner; 1905a7027df1SMatthew Ahrens nvlist_t *ddra_result; 190691948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t; 190791948b51SKeith M Wesolowski 19083b2aab18SMatthew Ahrens static int 19093b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 1910fa9e4066Sahrens { 191191948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 19123b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 19131d452cf5Sahrens dsl_dataset_t *ds; 19143b2aab18SMatthew Ahrens int64_t unused_refres_delta; 19153b2aab18SMatthew Ahrens int error; 1916fa9e4066Sahrens 191791948b51SKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 19183b2aab18SMatthew Ahrens if (error != 0) 19193b2aab18SMatthew Ahrens return (error); 1920370c1af0SSanjeev Bagewadi 19213b2aab18SMatthew Ahrens /* must not be a snapshot */ 19223b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 19233b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1924be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 19253b2aab18SMatthew Ahrens } 19263a5a36beSmmusante 19273b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 1928*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) { 19293b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1930be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 19313b2aab18SMatthew Ahrens } 19323a5a36beSmmusante 193378f17100SMatthew Ahrens /* must not have any bookmarks after the most recent snapshot */ 193478f17100SMatthew Ahrens nvlist_t *proprequest = fnvlist_alloc(); 193578f17100SMatthew Ahrens fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 193678f17100SMatthew Ahrens nvlist_t *bookmarks = fnvlist_alloc(); 193778f17100SMatthew Ahrens error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks); 193878f17100SMatthew Ahrens fnvlist_free(proprequest); 193978f17100SMatthew Ahrens if (error != 0) 194078f17100SMatthew Ahrens return (error); 194178f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL); 194278f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) { 194378f17100SMatthew Ahrens nvlist_t *valuenv = 194478f17100SMatthew Ahrens fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair), 194578f17100SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CREATETXG)); 194678f17100SMatthew Ahrens uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value"); 1947*c1379625SJustin T. Gibbs if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 194878f17100SMatthew Ahrens fnvlist_free(bookmarks); 194978f17100SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 195078f17100SMatthew Ahrens return (SET_ERROR(EEXIST)); 195178f17100SMatthew Ahrens } 195278f17100SMatthew Ahrens } 195378f17100SMatthew Ahrens fnvlist_free(bookmarks); 195478f17100SMatthew Ahrens 195591948b51SKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 195691948b51SKeith M Wesolowski if (error != 0) { 19573b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 195891948b51SKeith M Wesolowski return (error); 19593b2aab18SMatthew Ahrens } 19603b2aab18SMatthew Ahrens 19613b2aab18SMatthew Ahrens /* 19623b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 19633b2aab18SMatthew Ahrens * the refquota. 19643b2aab18SMatthew Ahrens */ 19653b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 1966*c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) { 19673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1968be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 1969fa9e4066Sahrens } 1970370c1af0SSanjeev Bagewadi 19713b2aab18SMatthew Ahrens /* 19723b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 19733b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 19743b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 19753b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 19763b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 19773b2aab18SMatthew Ahrens */ 19783b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 1979*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 19803b2aab18SMatthew Ahrens 19813b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 19823b2aab18SMatthew Ahrens unused_refres_delta > 19833b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 19843b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1985be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1986fa9e4066Sahrens } 1987fa9e4066Sahrens 19883b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 19893b2aab18SMatthew Ahrens return (0); 19903b2aab18SMatthew Ahrens } 19911d452cf5Sahrens 19923b2aab18SMatthew Ahrens static void 19933b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 19943b2aab18SMatthew Ahrens { 199591948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 19963b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 19973b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 19983b2aab18SMatthew Ahrens uint64_t cloneobj; 1999a7027df1SMatthew Ahrens char namebuf[ZFS_MAXNAMELEN]; 20001d452cf5Sahrens 200191948b51SKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 20021d452cf5Sahrens 2003a7027df1SMatthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf); 2004a7027df1SMatthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf); 2005a7027df1SMatthew Ahrens 20063b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 20073b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 20081d452cf5Sahrens 20093b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 20101d452cf5Sahrens 20113b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 20123b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 20133b2aab18SMatthew Ahrens 20143b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 20153b2aab18SMatthew Ahrens 20163b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 20173b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 20183b2aab18SMatthew Ahrens } 20193b2aab18SMatthew Ahrens 202091948b51SKeith M Wesolowski /* 2021a7027df1SMatthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot. 2022a7027df1SMatthew Ahrens * The name of the most recent snapshot will be returned under key "target" 2023a7027df1SMatthew Ahrens * in the result nvlist. 202491948b51SKeith M Wesolowski * 2025a7027df1SMatthew Ahrens * If owner != NULL: 202691948b51SKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry 202791948b51SKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we 202891948b51SKeith M Wesolowski * succeed or not. 202991948b51SKeith M Wesolowski * 203091948b51SKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See 203191948b51SKeith M Wesolowski * notes above zfs_suspend_fs() for further details. 203291948b51SKeith M Wesolowski */ 20333b2aab18SMatthew Ahrens int 2034a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result) 20353b2aab18SMatthew Ahrens { 203691948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t ddra; 203791948b51SKeith M Wesolowski 203891948b51SKeith M Wesolowski ddra.ddra_fsname = fsname; 203991948b51SKeith M Wesolowski ddra.ddra_owner = owner; 2040a7027df1SMatthew Ahrens ddra.ddra_result = result; 204191948b51SKeith M Wesolowski 20423b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 20437d46dc6cSMatthew Ahrens dsl_dataset_rollback_sync, &ddra, 20447d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 2045fa9e4066Sahrens } 204699653d4eSeschrock 2047088f3894Sahrens struct promotenode { 2048745cd3c5Smaybee list_node_t link; 2049745cd3c5Smaybee dsl_dataset_t *ds; 2050745cd3c5Smaybee }; 2051745cd3c5Smaybee 20523b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg { 20533b2aab18SMatthew Ahrens const char *ddpa_clonename; 20543b2aab18SMatthew Ahrens dsl_dataset_t *ddpa_clone; 205574e7dc98SMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps; 20563b2aab18SMatthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */ 205774e7dc98SMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 2058681d9761SEric Taylor char *err_ds; 2059a2afb611SJerry Jelinek cred_t *cr; 20603b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t; 20611d452cf5Sahrens 206274e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 20633b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 20643b2aab18SMatthew Ahrens void *tag); 20653b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 206674e7dc98SMatthew Ahrens 206799653d4eSeschrock static int 20683b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 206999653d4eSeschrock { 20703b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 20713b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 20723b2aab18SMatthew Ahrens dsl_dataset_t *hds; 20733b2aab18SMatthew Ahrens struct promotenode *snap; 20743b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 2075745cd3c5Smaybee int err; 2076cde58dbcSMatthew Ahrens uint64_t unused; 2077a2afb611SJerry Jelinek uint64_t ss_mv_cnt; 20781d452cf5Sahrens 20793b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 20803b2aab18SMatthew Ahrens if (err != 0) 20813b2aab18SMatthew Ahrens return (err); 208299653d4eSeschrock 20833b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 20841d452cf5Sahrens 2085*c1379625SJustin T. Gibbs if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) { 20863b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2087be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 20883b2aab18SMatthew Ahrens } 20893b2aab18SMatthew Ahrens 20903b2aab18SMatthew Ahrens /* 20913b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 20923b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 20933b2aab18SMatthew Ahrens */ 20943b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 20953b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 20963b2aab18SMatthew Ahrens return (0); 20973b2aab18SMatthew Ahrens } 20983b2aab18SMatthew Ahrens 20993b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 21003b2aab18SMatthew Ahrens origin_ds = snap->ds; 210199653d4eSeschrock 21023cb34c60Sahrens /* compute origin's new unique space */ 21033b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2104*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2105*c1379625SJustin T. Gibbs origin_ds->ds_object); 2106cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2107*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX, 21083b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 210999653d4eSeschrock 2110745cd3c5Smaybee /* 2111745cd3c5Smaybee * Walk the snapshots that we are moving 2112745cd3c5Smaybee * 211374e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 21143b2aab18SMatthew Ahrens * to used by each snapshot: 211574e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 211674e7dc98SMatthew Ahrens * So each snapshot gave birth to: 211774e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 2118745cd3c5Smaybee * So a sequence would look like: 211974e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2120745cd3c5Smaybee * Which simplifies to: 212174e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 2122745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 212374e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 2124745cd3c5Smaybee */ 2125a2afb611SJerry Jelinek ss_mv_cnt = 0; 2126*c1379625SJustin T. Gibbs ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes; 2127*c1379625SJustin T. Gibbs ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes; 2128*c1379625SJustin T. Gibbs ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes; 21293b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 21303b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 213199653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 2132745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 213399653d4eSeschrock 2134a2afb611SJerry Jelinek ss_mv_cnt++; 2135a2afb611SJerry Jelinek 21363b2aab18SMatthew Ahrens /* 21373b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 21383b2aab18SMatthew Ahrens * the objset. 21393b2aab18SMatthew Ahrens */ 21403b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 2141be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 21423b2aab18SMatthew Ahrens goto out; 21433b2aab18SMatthew Ahrens } 21443b2aab18SMatthew Ahrens 214599653d4eSeschrock /* Check that the snapshot name does not conflict */ 21463b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 2147745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2148681d9761SEric Taylor if (err == 0) { 21493b2aab18SMatthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 2150be6fd75aSMatthew Ahrens err = SET_ERROR(EEXIST); 2151681d9761SEric Taylor goto out; 2152681d9761SEric Taylor } 2153745cd3c5Smaybee if (err != ENOENT) 2154681d9761SEric Taylor goto out; 215599653d4eSeschrock 2156745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 2157*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0) 215874e7dc98SMatthew Ahrens continue; 215974e7dc98SMatthew Ahrens 2160cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 2161cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 21623b2aab18SMatthew Ahrens ddpa->used += dlused; 21633b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 21643b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 216574e7dc98SMatthew Ahrens } 2166745cd3c5Smaybee 2167745cd3c5Smaybee /* 2168745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 2169745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 2170745cd3c5Smaybee */ 21713b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 2172*c1379625SJustin T. Gibbs ddpa->used -= 2173*c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes; 2174*c1379625SJustin T. Gibbs ddpa->comp -= 2175*c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes; 21763b2aab18SMatthew Ahrens ddpa->uncomp -= 2177*c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)-> 2178*c1379625SJustin T. Gibbs ds_uncompressed_bytes; 217999653d4eSeschrock } 218099653d4eSeschrock 2181a2afb611SJerry Jelinek /* Check that there is enough space and limit headroom here */ 218274e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2183a2afb611SJerry Jelinek 0, ss_mv_cnt, ddpa->used, ddpa->cr); 21843b2aab18SMatthew Ahrens if (err != 0) 21853b2aab18SMatthew Ahrens goto out; 218674e7dc98SMatthew Ahrens 218774e7dc98SMatthew Ahrens /* 218874e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 218974e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 219074e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 219174e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 219274e7dc98SMatthew Ahrens */ 2193*c1379625SJustin T. Gibbs if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 219474e7dc98SMatthew Ahrens uint64_t space; 219574e7dc98SMatthew Ahrens 219674e7dc98SMatthew Ahrens /* 219774e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 21983f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 2199cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 220074e7dc98SMatthew Ahrens * calls will be fast because they do not have to 220174e7dc98SMatthew Ahrens * iterate over all bps. 220274e7dc98SMatthew Ahrens */ 22033b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 22043b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 22053b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 22063b2aab18SMatthew Ahrens if (err != 0) 22073b2aab18SMatthew Ahrens goto out; 220874e7dc98SMatthew Ahrens 22093b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 22103f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 22113b2aab18SMatthew Ahrens if (err != 0) 22123b2aab18SMatthew Ahrens goto out; 22133b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 221474e7dc98SMatthew Ahrens } 2215*c1379625SJustin T. Gibbs if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags & 2216*c1379625SJustin T. Gibbs DD_FLAG_USED_BREAKDOWN) { 22173b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 2218*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg, 2219*c1379625SJustin T. Gibbs &ddpa->originusedsnap); 22203b2aab18SMatthew Ahrens if (err != 0) 22213b2aab18SMatthew Ahrens goto out; 2222745cd3c5Smaybee } 22231d452cf5Sahrens 2224681d9761SEric Taylor out: 22253b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2226681d9761SEric Taylor return (err); 22271d452cf5Sahrens } 222899653d4eSeschrock 22291d452cf5Sahrens static void 22303b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 22311d452cf5Sahrens { 22323b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 22333b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 22343b2aab18SMatthew Ahrens dsl_dataset_t *hds; 22353b2aab18SMatthew Ahrens struct promotenode *snap; 22363b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 22373b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 22383b2aab18SMatthew Ahrens dsl_dir_t *dd; 22393cb34c60Sahrens dsl_dir_t *odd = NULL; 2240088f3894Sahrens uint64_t oldnext_obj; 224174e7dc98SMatthew Ahrens int64_t delta; 22421d452cf5Sahrens 22433b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 22443b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 22453b2aab18SMatthew Ahrens 2246*c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE); 22471d452cf5Sahrens 22483b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 22493b2aab18SMatthew Ahrens origin_ds = snap->ds; 22503b2aab18SMatthew Ahrens dd = hds->ds_dir; 22513b2aab18SMatthew Ahrens 22523b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 225374e7dc98SMatthew Ahrens origin_head = snap->ds; 225474e7dc98SMatthew Ahrens 22550b69c2f0Sahrens /* 22563cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 22570b69c2f0Sahrens * changing. 22580b69c2f0Sahrens */ 22593b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 22603cb34c60Sahrens NULL, FTAG, &odd)); 226199653d4eSeschrock 2262745cd3c5Smaybee /* change origin's next snap */ 2263745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2264*c1379625SJustin T. Gibbs oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj; 22653b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2266*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2267*c1379625SJustin T. Gibbs origin_ds->ds_object); 2268*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object; 2269745cd3c5Smaybee 2270088f3894Sahrens /* change the origin's next clone */ 2271*c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) { 22723b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 22733b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 22743b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2275*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_clones_obj, 2276088f3894Sahrens oldnext_obj, tx)); 2277088f3894Sahrens } 2278088f3894Sahrens 2279745cd3c5Smaybee /* change origin */ 2280745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2281*c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object); 2282*c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj; 22833f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2284745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2285*c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object; 22863f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 2287*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg; 2288745cd3c5Smaybee 2289cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2290cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 22913b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2292*c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx)); 22933b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2294*c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2295cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2296cde58dbcSMatthew Ahrens 22973b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2298*c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2299cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2300*c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_clones == 0) { 2301*c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones = 2302*c1379625SJustin T. Gibbs zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES, 2303*c1379625SJustin T. Gibbs DMU_OT_NONE, 0, tx); 2304cde58dbcSMatthew Ahrens } 23053b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2306*c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx)); 2307cde58dbcSMatthew Ahrens } 2308cde58dbcSMatthew Ahrens 230999653d4eSeschrock /* move snapshots to this dir */ 23103b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 23113b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2312745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 231399653d4eSeschrock 23143b2aab18SMatthew Ahrens /* 23153b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 23163b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 23173b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 23183b2aab18SMatthew Ahrens */ 2319503ad85cSMatthew Ahrens if (ds->ds_objset) { 2320503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2321503ad85cSMatthew Ahrens ds->ds_objset = NULL; 23223baa08fcSek } 23233b2aab18SMatthew Ahrens 232499653d4eSeschrock /* move snap name entry */ 23253b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 23263b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2327a2afb611SJerry Jelinek ds->ds_snapname, tx, B_TRUE)); 23283b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 2329*c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname, 233099653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2331a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(hds->ds_dir, 1, 2332a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 2333cde58dbcSMatthew Ahrens 233499653d4eSeschrock /* change containing dsl_dir */ 233599653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 2336*c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object); 2337*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object; 23383cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 23393b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 23403b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 234199653d4eSeschrock NULL, ds, &ds->ds_dir)); 234299653d4eSeschrock 2343cde58dbcSMatthew Ahrens /* move any clone references */ 2344*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj && 2345cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2346cde58dbcSMatthew Ahrens zap_cursor_t zc; 2347cde58dbcSMatthew Ahrens zap_attribute_t za; 2348cde58dbcSMatthew Ahrens 23493b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2350*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 23513b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 23523b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 23533b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 23543b2aab18SMatthew Ahrens uint64_t o; 2355a9799022Sck 23563b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 23573b2aab18SMatthew Ahrens /* 23583b2aab18SMatthew Ahrens * We've already moved the 23593b2aab18SMatthew Ahrens * origin's reference. 23603b2aab18SMatthew Ahrens */ 23613b2aab18SMatthew Ahrens continue; 23623b2aab18SMatthew Ahrens } 2363a9799022Sck 23643b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 23653b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 2366*c1379625SJustin T. Gibbs o = dsl_dir_phys(cnds->ds_dir)-> 2367*c1379625SJustin T. Gibbs dd_head_dataset_obj; 2368a9799022Sck 23693b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2370*c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, o, tx)); 23713b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2372*c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, o, tx)); 23733b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 23743b2aab18SMatthew Ahrens } 23753b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 23763b2aab18SMatthew Ahrens } 23779082849eSck 23783b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2379a9799022Sck } 2380a9799022Sck 2381a9799022Sck /* 23823b2aab18SMatthew Ahrens * Change space accounting. 23833b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 23843b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 23853b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2386a9799022Sck */ 2387a9799022Sck 23883b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 2389*c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]; 23903b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 23913b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 23923b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 23933b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 23943b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 23953b2aab18SMatthew Ahrens 23963b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 2397*c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP]; 23983b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 23993b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 24003b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 24013b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 24023b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 24033b2aab18SMatthew Ahrens 2404*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique; 24053b2aab18SMatthew Ahrens 24063b2aab18SMatthew Ahrens /* log history record */ 24073b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 24083b2aab18SMatthew Ahrens 24093b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 24103b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2411a9799022Sck } 2412a9799022Sck 24133b2aab18SMatthew Ahrens /* 24143b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 24153b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 24163b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 24173b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 24183b2aab18SMatthew Ahrens */ 2419a9799022Sck static int 24203b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 24213b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2422a9799022Sck { 24233b2aab18SMatthew Ahrens uint64_t obj = last_obj; 2424a9799022Sck 24253b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 24263b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 2427a9799022Sck 24283b2aab18SMatthew Ahrens while (obj != first_obj) { 24293b2aab18SMatthew Ahrens dsl_dataset_t *ds; 24303b2aab18SMatthew Ahrens struct promotenode *snap; 24313b2aab18SMatthew Ahrens int err; 243292241e0bSTom Erickson 24333b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 24343b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 24353b2aab18SMatthew Ahrens if (err != 0) 24363b2aab18SMatthew Ahrens return (err); 2437a9799022Sck 24383b2aab18SMatthew Ahrens if (first_obj == 0) 2439*c1379625SJustin T. Gibbs first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj; 24403b2aab18SMatthew Ahrens 24413b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 24423b2aab18SMatthew Ahrens snap->ds = ds; 24433b2aab18SMatthew Ahrens list_insert_tail(l, snap); 2444*c1379625SJustin T. Gibbs obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 24453b2aab18SMatthew Ahrens } 2446a9799022Sck 2447a9799022Sck return (0); 2448a9799022Sck } 2449a9799022Sck 24503b2aab18SMatthew Ahrens static int 24513b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2452a9799022Sck { 24533b2aab18SMatthew Ahrens struct promotenode *snap; 2454a9799022Sck 24553b2aab18SMatthew Ahrens *spacep = 0; 24563b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 24573b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 24583b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 24593b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 24603b2aab18SMatthew Ahrens *spacep += used; 246192241e0bSTom Erickson } 24623b2aab18SMatthew Ahrens return (0); 2463a9799022Sck } 2464a9799022Sck 24653b2aab18SMatthew Ahrens static void 24663b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 2467a9799022Sck { 24683b2aab18SMatthew Ahrens struct promotenode *snap; 246992241e0bSTom Erickson 24703b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 24713b2aab18SMatthew Ahrens return; 2472a9799022Sck 24733b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 24743b2aab18SMatthew Ahrens list_remove(l, snap); 24753b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 24763b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 24773b2aab18SMatthew Ahrens } 24783b2aab18SMatthew Ahrens list_destroy(l); 2479a9799022Sck } 2480a9799022Sck 2481a9799022Sck static int 24823b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2483a9799022Sck { 24843b2aab18SMatthew Ahrens int error; 24853b2aab18SMatthew Ahrens dsl_dir_t *dd; 24863b2aab18SMatthew Ahrens struct promotenode *snap; 2487a9799022Sck 24883b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 24893b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 24903b2aab18SMatthew Ahrens if (error != 0) 24913b2aab18SMatthew Ahrens return (error); 24923b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 2493a9799022Sck 24943b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) || 24953b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 24963b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 2497be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 24983b2aab18SMatthew Ahrens } 2499a9799022Sck 2500*c1379625SJustin T. Gibbs error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj, 25013b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 25023b2aab18SMatthew Ahrens if (error != 0) 25033b2aab18SMatthew Ahrens goto out; 2504a9799022Sck 25053b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 25063b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 25073b2aab18SMatthew Ahrens if (error != 0) 25083b2aab18SMatthew Ahrens goto out; 2509a9799022Sck 25103b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 2511*c1379625SJustin T. Gibbs ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj); 2512*c1379625SJustin T. Gibbs error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj, 2513*c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj, 25143b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 25153b2aab18SMatthew Ahrens if (error != 0) 25163b2aab18SMatthew Ahrens goto out; 2517379c004dSEric Schrock 2518*c1379625SJustin T. Gibbs if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) { 25193b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 2520*c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj, 25213b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 25223b2aab18SMatthew Ahrens if (error != 0) 25233b2aab18SMatthew Ahrens goto out; 2524379c004dSEric Schrock } 25253b2aab18SMatthew Ahrens out: 25263b2aab18SMatthew Ahrens if (error != 0) 25273b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 25283b2aab18SMatthew Ahrens return (error); 2529a9799022Sck } 2530a9799022Sck 2531a9799022Sck static void 25323b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2533a9799022Sck { 25343b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 25353b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 25363b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 25373b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 25383b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 25393b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 25403b2aab18SMatthew Ahrens } 254102c8f3f0SMatthew Ahrens 25423b2aab18SMatthew Ahrens /* 25433b2aab18SMatthew Ahrens * Promote a clone. 25443b2aab18SMatthew Ahrens * 25453b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 25463b2aab18SMatthew Ahrens * in with the name. (It must be at least MAXNAMELEN bytes long.) 25473b2aab18SMatthew Ahrens */ 25483b2aab18SMatthew Ahrens int 25493b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 25503b2aab18SMatthew Ahrens { 25513b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 25523b2aab18SMatthew Ahrens uint64_t numsnaps; 25533b2aab18SMatthew Ahrens int error; 25543b2aab18SMatthew Ahrens objset_t *os; 255592241e0bSTom Erickson 25563b2aab18SMatthew Ahrens /* 25573b2aab18SMatthew Ahrens * We will modify space proportional to the number of 25583b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 25593b2aab18SMatthew Ahrens */ 25603b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 25613b2aab18SMatthew Ahrens if (error != 0) 25623b2aab18SMatthew Ahrens return (error); 25633b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 2564*c1379625SJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj, 2565*c1379625SJustin T. Gibbs &numsnaps); 25663b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 25673b2aab18SMatthew Ahrens if (error != 0) 25683b2aab18SMatthew Ahrens return (error); 256902c8f3f0SMatthew Ahrens 25703b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 25713b2aab18SMatthew Ahrens ddpa.err_ds = conflsnap; 2572a2afb611SJerry Jelinek ddpa.cr = CRED(); 257302c8f3f0SMatthew Ahrens 25743b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check, 25757d46dc6cSMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 25767d46dc6cSMatthew Ahrens 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED)); 2577a9799022Sck } 2578a9799022Sck 2579a9799022Sck int 25803b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 258191948b51SKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 2582a9799022Sck { 25833b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2584a9799022Sck 25853b2aab18SMatthew Ahrens /* they should both be heads */ 25863b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(clone) || 25873b2aab18SMatthew Ahrens dsl_dataset_is_snapshot(origin_head)) 2588be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 258992241e0bSTom Erickson 259034f2f8cfSMatthew Ahrens /* if we are not forcing, the branch point should be just before them */ 259134f2f8cfSMatthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev) 2592be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2593a9799022Sck 25943b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 25953b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 25963b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 259734f2f8cfSMatthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir) 2598be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 259992241e0bSTom Erickson 26003b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 26013b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2602be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2603842727c2SChris Kirby 26043b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 260534f2f8cfSMatthew Ahrens if (!force && 260634f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 2607be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 2608c99e4bdcSChris Kirby 26093b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 261091948b51SKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx)) 2611be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 26123b2aab18SMatthew Ahrens 26133b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 26143b2aab18SMatthew Ahrens unused_refres_delta = 26153b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2616*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 26173b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2618*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 26193b2aab18SMatthew Ahrens 26203b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 26213b2aab18SMatthew Ahrens unused_refres_delta > 26223b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2623be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 26243b2aab18SMatthew Ahrens 26253b2aab18SMatthew Ahrens /* clone can't be over the head's refquota */ 26263b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 2627*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes > 2628*c1379625SJustin T. Gibbs origin_head->ds_quota) 2629be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2630c99e4bdcSChris Kirby 26313b2aab18SMatthew Ahrens return (0); 2632c99e4bdcSChris Kirby } 2633c99e4bdcSChris Kirby 2634a7f53a56SChris Kirby void 26353b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 26363b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 2637a7f53a56SChris Kirby { 26383b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 26393b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2640a7f53a56SChris Kirby 26413b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 26423b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 2643*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota); 264434f2f8cfSMatthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 2645842727c2SChris Kirby 26463b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 26473b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2648842727c2SChris Kirby 26493b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 26503b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 26513b2aab18SMatthew Ahrens clone->ds_objset = NULL; 26523b2aab18SMatthew Ahrens } 2653842727c2SChris Kirby 26543b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 26553b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 26563b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 2657842727c2SChris Kirby } 2658842727c2SChris Kirby 26593b2aab18SMatthew Ahrens unused_refres_delta = 26603b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2661*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 26623b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2663*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 26643b2aab18SMatthew Ahrens 26653b2aab18SMatthew Ahrens /* 26663b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 26673b2aab18SMatthew Ahrens */ 26683b2aab18SMatthew Ahrens if (clone->ds_prev) { 26693b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 26703b2aab18SMatthew Ahrens uint64_t comp, uncomp; 26713b2aab18SMatthew Ahrens 26723b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 26733b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 2674*c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX, 2675*c1379625SJustin T. Gibbs &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp); 26763b2aab18SMatthew Ahrens } 26773b2aab18SMatthew Ahrens 26783b2aab18SMatthew Ahrens /* swap blkptrs */ 26793b2aab18SMatthew Ahrens { 26803b2aab18SMatthew Ahrens blkptr_t tmp; 2681*c1379625SJustin T. Gibbs tmp = dsl_dataset_phys(origin_head)->ds_bp; 2682*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_bp = 2683*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp; 2684*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp = tmp; 26853b2aab18SMatthew Ahrens } 26863b2aab18SMatthew Ahrens 26873b2aab18SMatthew Ahrens /* set dd_*_bytes */ 26883b2aab18SMatthew Ahrens { 26893b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 26903b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 26913b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 26923b2aab18SMatthew Ahrens 2693*c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(clone->ds_dir)-> 26943b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 26953b2aab18SMatthew Ahrens 26963b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 26973b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 26983b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 26993b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 270015508ac0SChris Kirby 2701*c1379625SJustin T. Gibbs dused = dsl_dataset_phys(clone)->ds_referenced_bytes + 2702*c1379625SJustin T. Gibbs cdl_used - 2703*c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_referenced_bytes + 2704*c1379625SJustin T. Gibbs odl_used); 2705*c1379625SJustin T. Gibbs dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes + 2706*c1379625SJustin T. Gibbs cdl_comp - 2707*c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_compressed_bytes + 2708*c1379625SJustin T. Gibbs odl_comp); 2709*c1379625SJustin T. Gibbs duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes + 27103b2aab18SMatthew Ahrens cdl_uncomp - 2711*c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes + 2712*c1379625SJustin T. Gibbs odl_uncomp); 2713842727c2SChris Kirby 27143b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 27153b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 27163b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 27173b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 2718842727c2SChris Kirby 2719842727c2SChris Kirby /* 27203b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 27213b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 27223b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 27233b2aab18SMatthew Ahrens * changing that affects the snapused). 2724842727c2SChris Kirby */ 27253b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 27263b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 27273b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 27283b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 27293b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 27303b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 27313b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 27323b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 2733842727c2SChris Kirby } 2734842727c2SChris Kirby 27353b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 2736*c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes, 2737*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes); 2738*c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes, 2739*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_compressed_bytes); 2740*c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes, 2741*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_uncompressed_bytes); 2742*c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes, 2743*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 2744842727c2SChris Kirby 27453b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 27463b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 27473b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 2748ca45db41SChris Kirby 27493b2aab18SMatthew Ahrens /* 27503b2aab18SMatthew Ahrens * Swap deadlists. 27513b2aab18SMatthew Ahrens */ 27523b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 27533b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 2754*c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj, 2755*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 27563b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 2757*c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 27583b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 2759*c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_deadlist_obj); 2760842727c2SChris Kirby 27613b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 2762842727c2SChris Kirby 27633b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 27643b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 2765842727c2SChris Kirby } 2766842727c2SChris Kirby 27673b2aab18SMatthew Ahrens /* 27683b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 27693b2aab18SMatthew Ahrens * return the name of that dataset. 27703b2aab18SMatthew Ahrens */ 2771a7f53a56SChris Kirby int 27723b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 2773a7f53a56SChris Kirby { 27743b2aab18SMatthew Ahrens dsl_pool_t *dp; 27753b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2776a7f53a56SChris Kirby int error; 2777a7f53a56SChris Kirby 27783b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 27793b2aab18SMatthew Ahrens if (error != 0) 27803b2aab18SMatthew Ahrens return (error); 27813b2aab18SMatthew Ahrens 27823b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 27833b2aab18SMatthew Ahrens if (error == 0) { 27843b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 27853b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 27863b2aab18SMatthew Ahrens } 27873b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 2788a7f53a56SChris Kirby 2789a7f53a56SChris Kirby return (error); 2790a7f53a56SChris Kirby } 2791a7f53a56SChris Kirby 2792842727c2SChris Kirby int 27933b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 27943b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 2795842727c2SChris Kirby { 27963b2aab18SMatthew Ahrens int error = 0; 2797842727c2SChris Kirby 27983b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 2799842727c2SChris Kirby 28003b2aab18SMatthew Ahrens /* 28013b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 28023b2aab18SMatthew Ahrens * unconsumed refreservation space. 28033b2aab18SMatthew Ahrens */ 28043b2aab18SMatthew Ahrens *ref_rsrv = 0; 2805842727c2SChris Kirby 28063b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 28073b2aab18SMatthew Ahrens /* 28083b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 28093b2aab18SMatthew Ahrens */ 2810*c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 28113b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 2812*c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 2813*c1379625SJustin T. Gibbs *used -= 2814*c1379625SJustin T. Gibbs (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 28153b2aab18SMatthew Ahrens *ref_rsrv = 28163b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 2817842727c2SChris Kirby } 2818842727c2SChris Kirby 28193b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 28203b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 28213b2aab18SMatthew Ahrens return (0); 2822842727c2SChris Kirby } 28233b2aab18SMatthew Ahrens /* 28243b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 28253b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 28263b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 28273b2aab18SMatthew Ahrens * may free up space for us). 28283b2aab18SMatthew Ahrens */ 2829*c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >= 2830*c1379625SJustin T. Gibbs ds->ds_quota) { 28313b2aab18SMatthew Ahrens if (inflight > 0 || 2832*c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota) 2833be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 28343b2aab18SMatthew Ahrens else 2835be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 2836842727c2SChris Kirby } 28373b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2838842727c2SChris Kirby 2839842727c2SChris Kirby return (error); 2840842727c2SChris Kirby } 2841842727c2SChris Kirby 28423b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 28433b2aab18SMatthew Ahrens const char *ddsqra_name; 28443b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 28453b2aab18SMatthew Ahrens uint64_t ddsqra_value; 28463b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 2847842727c2SChris Kirby 28483b2aab18SMatthew Ahrens 28493b2aab18SMatthew Ahrens /* ARGSUSED */ 2850842727c2SChris Kirby static int 28513b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 2852842727c2SChris Kirby { 28533b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 28543b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 28553b2aab18SMatthew Ahrens dsl_dataset_t *ds; 2856842727c2SChris Kirby int error; 28573b2aab18SMatthew Ahrens uint64_t newval; 2858842727c2SChris Kirby 28593b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 2860be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2861842727c2SChris Kirby 28623b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 28633b2aab18SMatthew Ahrens if (error != 0) 28643b2aab18SMatthew Ahrens return (error); 28653b2aab18SMatthew Ahrens 28663b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 28673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2868be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2869842727c2SChris Kirby } 2870842727c2SChris Kirby 28713b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 28723b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 28733b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 28743b2aab18SMatthew Ahrens if (error != 0) { 28753b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2876842727c2SChris Kirby return (error); 2877842727c2SChris Kirby } 2878842727c2SChris Kirby 28793b2aab18SMatthew Ahrens if (newval == 0) { 28803b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 28813b2aab18SMatthew Ahrens return (0); 28823b2aab18SMatthew Ahrens } 2883842727c2SChris Kirby 2884*c1379625SJustin T. Gibbs if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes || 28853b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 28863b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2887be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 28883b2aab18SMatthew Ahrens } 28893b2aab18SMatthew Ahrens 28903b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2891842727c2SChris Kirby return (0); 2892842727c2SChris Kirby } 2893842727c2SChris Kirby 28943b2aab18SMatthew Ahrens static void 28953b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 2896842727c2SChris Kirby { 28973b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 28983b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 28993b2aab18SMatthew Ahrens dsl_dataset_t *ds; 29003b2aab18SMatthew Ahrens uint64_t newval; 2901842727c2SChris Kirby 29023b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 2903842727c2SChris Kirby 29043b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 29053b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 29063b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 29073b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 2908842727c2SChris Kirby 29093b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 29103b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 2911842727c2SChris Kirby 29123b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 29133b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 29143b2aab18SMatthew Ahrens ds->ds_quota = newval; 2915842727c2SChris Kirby } 29163b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2917842727c2SChris Kirby } 2918842727c2SChris Kirby 29193b2aab18SMatthew Ahrens int 29203b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 29213b2aab18SMatthew Ahrens uint64_t refquota) 2922842727c2SChris Kirby { 29233b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 2924842727c2SChris Kirby 29253b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 29263b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 29273b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 29283b2aab18SMatthew Ahrens 29293b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 29307d46dc6cSMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 2931842727c2SChris Kirby } 2932842727c2SChris Kirby 2933842727c2SChris Kirby static int 29343b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 2935842727c2SChris Kirby { 29363b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 29373b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 2938842727c2SChris Kirby dsl_dataset_t *ds; 2939842727c2SChris Kirby int error; 29403b2aab18SMatthew Ahrens uint64_t newval, unique; 2941d7747cbcSChris Kirby 29423b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 2943be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 2944842727c2SChris Kirby 29453b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 29463b2aab18SMatthew Ahrens if (error != 0) 2947842727c2SChris Kirby return (error); 2948842727c2SChris Kirby 29493b2aab18SMatthew Ahrens if (dsl_dataset_is_snapshot(ds)) { 29503b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2951be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2952842727c2SChris Kirby } 2953842727c2SChris Kirby 29543b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 29553b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 29563b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 29573b2aab18SMatthew Ahrens if (error != 0) { 29583b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2959842727c2SChris Kirby return (error); 2960842727c2SChris Kirby } 2961842727c2SChris Kirby 29623b2aab18SMatthew Ahrens /* 29633b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 29643b2aab18SMatthew Ahrens * space estimates may be inaccurate. 29653b2aab18SMatthew Ahrens */ 29663b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 29673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 29683b2aab18SMatthew Ahrens return (0); 2969842727c2SChris Kirby } 2970842727c2SChris Kirby 29713b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 29723b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 29733b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 2974*c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 29753b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2976842727c2SChris Kirby 29773b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 29783b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 29793b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 2980842727c2SChris Kirby 29813b2aab18SMatthew Ahrens if (delta > 29823b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 29833b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 29843b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2985be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 29863b2aab18SMatthew Ahrens } 2987842727c2SChris Kirby } 2988842727c2SChris Kirby 29893b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 29903b2aab18SMatthew Ahrens return (0); 2991842727c2SChris Kirby } 2992842727c2SChris Kirby 29933b2aab18SMatthew Ahrens void 29943b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 29953b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 2996ca45db41SChris Kirby { 29973b2aab18SMatthew Ahrens uint64_t newval; 29983b2aab18SMatthew Ahrens uint64_t unique; 29993b2aab18SMatthew Ahrens int64_t delta; 3000ca45db41SChris Kirby 30013b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 30023b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 3003ca45db41SChris Kirby 30043b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 30053b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 3006a7f53a56SChris Kirby 30073b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 30083b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 30093b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 30103b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 3011*c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 30123b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 30133b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 30143b2aab18SMatthew Ahrens ds->ds_reserved = newval; 30153b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3016a7f53a56SChris Kirby 30173b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 30183b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 3019ca45db41SChris Kirby } 3020ca45db41SChris Kirby 30213b2aab18SMatthew Ahrens static void 30223b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 3023842727c2SChris Kirby { 30243b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 30253b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3026842727c2SChris Kirby dsl_dataset_t *ds; 3027842727c2SChris Kirby 30283b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 30293b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 30303b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 3031842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 3032842727c2SChris Kirby } 3033503ad85cSMatthew Ahrens 3034503ad85cSMatthew Ahrens int 30353b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 30363b2aab18SMatthew Ahrens uint64_t refreservation) 3037503ad85cSMatthew Ahrens { 30383b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3039503ad85cSMatthew Ahrens 30403b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 30413b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 30423b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 30433b2aab18SMatthew Ahrens 30443b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 30457d46dc6cSMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 30467d46dc6cSMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE)); 3047503ad85cSMatthew Ahrens } 304819b94df9SMatthew Ahrens 304919b94df9SMatthew Ahrens /* 305019b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 305119b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 305219b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 305319b94df9SMatthew Ahrens * fail and return EINVAL. 305419b94df9SMatthew Ahrens * 305519b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 305619b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 305719b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 305819b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 305919b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 306019b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 306119b94df9SMatthew Ahrens * 306219b94df9SMatthew Ahrens * space freed [---------------------] 306319b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 306419b94df9SMatthew Ahrens * oldsnap new 306519b94df9SMatthew Ahrens */ 306619b94df9SMatthew Ahrens int 306719b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 306819b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 306919b94df9SMatthew Ahrens { 307019b94df9SMatthew Ahrens int err = 0; 307119b94df9SMatthew Ahrens uint64_t snapobj; 307219b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 307319b94df9SMatthew Ahrens 30743b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 30753b2aab18SMatthew Ahrens 307619b94df9SMatthew Ahrens *usedp = 0; 3077*c1379625SJustin T. Gibbs *usedp += dsl_dataset_phys(new)->ds_referenced_bytes; 3078*c1379625SJustin T. Gibbs *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes; 307919b94df9SMatthew Ahrens 308019b94df9SMatthew Ahrens *compp = 0; 3081*c1379625SJustin T. Gibbs *compp += dsl_dataset_phys(new)->ds_compressed_bytes; 3082*c1379625SJustin T. Gibbs *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes; 308319b94df9SMatthew Ahrens 308419b94df9SMatthew Ahrens *uncompp = 0; 3085*c1379625SJustin T. Gibbs *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes; 3086*c1379625SJustin T. Gibbs *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes; 308719b94df9SMatthew Ahrens 308819b94df9SMatthew Ahrens snapobj = new->ds_object; 308919b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 309019b94df9SMatthew Ahrens dsl_dataset_t *snap; 309119b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 309219b94df9SMatthew Ahrens 3093ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 3094ad135b5dSChristopher Siden snap = new; 3095ad135b5dSChristopher Siden } else { 3096ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 3097ad135b5dSChristopher Siden if (err != 0) 3098ad135b5dSChristopher Siden break; 3099ad135b5dSChristopher Siden } 310019b94df9SMatthew Ahrens 3101*c1379625SJustin T. Gibbs if (dsl_dataset_phys(snap)->ds_prev_snap_txg == 3102*c1379625SJustin T. Gibbs dsl_dataset_phys(oldsnap)->ds_creation_txg) { 310319b94df9SMatthew Ahrens /* 310419b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 310519b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 310619b94df9SMatthew Ahrens * which is more efficient (especially for old-format 310719b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 310819b94df9SMatthew Ahrens * doesn't have enough information to make this 310919b94df9SMatthew Ahrens * optimization itself. 311019b94df9SMatthew Ahrens */ 311119b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 311219b94df9SMatthew Ahrens &used, &comp, &uncomp); 311319b94df9SMatthew Ahrens } else { 311419b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 3115*c1379625SJustin T. Gibbs 0, dsl_dataset_phys(oldsnap)->ds_creation_txg, 311619b94df9SMatthew Ahrens &used, &comp, &uncomp); 311719b94df9SMatthew Ahrens } 311819b94df9SMatthew Ahrens *usedp += used; 311919b94df9SMatthew Ahrens *compp += comp; 312019b94df9SMatthew Ahrens *uncompp += uncomp; 312119b94df9SMatthew Ahrens 312219b94df9SMatthew Ahrens /* 312319b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 312419b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 312519b94df9SMatthew Ahrens * was not a snapshot of/before new. 312619b94df9SMatthew Ahrens */ 3127*c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 3128ad135b5dSChristopher Siden if (snap != new) 3129ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 313019b94df9SMatthew Ahrens if (snapobj == 0) { 3131be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 313219b94df9SMatthew Ahrens break; 313319b94df9SMatthew Ahrens } 313419b94df9SMatthew Ahrens 313519b94df9SMatthew Ahrens } 313619b94df9SMatthew Ahrens return (err); 313719b94df9SMatthew Ahrens } 313819b94df9SMatthew Ahrens 313919b94df9SMatthew Ahrens /* 314019b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 314119b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 314219b94df9SMatthew Ahrens * 314319b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 314419b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 314519b94df9SMatthew Ahrens * firstsnap lastsnap 314619b94df9SMatthew Ahrens * 314719b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 314819b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 314919b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 315019b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 315119b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 315219b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 315319b94df9SMatthew Ahrens */ 315419b94df9SMatthew Ahrens int 315519b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 315619b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 315719b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 315819b94df9SMatthew Ahrens { 315919b94df9SMatthew Ahrens int err = 0; 316019b94df9SMatthew Ahrens uint64_t snapobj; 316119b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 316219b94df9SMatthew Ahrens 316319b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(firstsnap)); 316419b94df9SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(lastsnap)); 316519b94df9SMatthew Ahrens 316619b94df9SMatthew Ahrens /* 316719b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 316819b94df9SMatthew Ahrens * is before lastsnap. 316919b94df9SMatthew Ahrens */ 317019b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 3171*c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_creation_txg > 3172*c1379625SJustin T. Gibbs dsl_dataset_phys(lastsnap)->ds_creation_txg) 3173be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 317419b94df9SMatthew Ahrens 317519b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 317619b94df9SMatthew Ahrens 3177*c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj; 317819b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 317919b94df9SMatthew Ahrens dsl_dataset_t *ds; 318019b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 318119b94df9SMatthew Ahrens 318219b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 318319b94df9SMatthew Ahrens if (err != 0) 318419b94df9SMatthew Ahrens break; 318519b94df9SMatthew Ahrens 318619b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 3187*c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX, 318819b94df9SMatthew Ahrens &used, &comp, &uncomp); 318919b94df9SMatthew Ahrens *usedp += used; 319019b94df9SMatthew Ahrens *compp += comp; 319119b94df9SMatthew Ahrens *uncompp += uncomp; 319219b94df9SMatthew Ahrens 3193*c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 319419b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 319519b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 319619b94df9SMatthew Ahrens } 319719b94df9SMatthew Ahrens return (err); 319819b94df9SMatthew Ahrens } 31993b2aab18SMatthew Ahrens 3200b5152584SMatthew Ahrens static int 3201b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_check(void *arg, dmu_tx_t *tx) 3202b5152584SMatthew Ahrens { 3203b5152584SMatthew Ahrens const char *dsname = arg; 3204b5152584SMatthew Ahrens dsl_dataset_t *ds; 3205b5152584SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3206b5152584SMatthew Ahrens int error = 0; 3207b5152584SMatthew Ahrens 3208b5152584SMatthew Ahrens if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS)) 3209b5152584SMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3210b5152584SMatthew Ahrens 3211b5152584SMatthew Ahrens ASSERT(spa_feature_is_enabled(dp->dp_spa, 3212b5152584SMatthew Ahrens SPA_FEATURE_EXTENSIBLE_DATASET)); 3213b5152584SMatthew Ahrens 3214b5152584SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 3215b5152584SMatthew Ahrens if (error != 0) 3216b5152584SMatthew Ahrens return (error); 3217b5152584SMatthew Ahrens 3218b5152584SMatthew Ahrens if (ds->ds_large_blocks) 3219b5152584SMatthew Ahrens error = EALREADY; 3220b5152584SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3221b5152584SMatthew Ahrens 3222b5152584SMatthew Ahrens return (error); 3223b5152584SMatthew Ahrens } 3224b5152584SMatthew Ahrens 3225b5152584SMatthew Ahrens void 3226b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync_impl(uint64_t dsobj, dmu_tx_t *tx) 3227b5152584SMatthew Ahrens { 3228b5152584SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 3229b5152584SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 3230b5152584SMatthew Ahrens uint64_t zero = 0; 3231b5152584SMatthew Ahrens 3232b5152584SMatthew Ahrens spa_feature_incr(spa, SPA_FEATURE_LARGE_BLOCKS, tx); 3233b5152584SMatthew Ahrens dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 3234b5152584SMatthew Ahrens 3235b5152584SMatthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_LARGE_BLOCKS, 3236b5152584SMatthew Ahrens sizeof (zero), 1, &zero, tx)); 3237b5152584SMatthew Ahrens } 3238b5152584SMatthew Ahrens 3239b5152584SMatthew Ahrens static void 3240b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync(void *arg, dmu_tx_t *tx) 3241b5152584SMatthew Ahrens { 3242b5152584SMatthew Ahrens const char *dsname = arg; 3243b5152584SMatthew Ahrens dsl_dataset_t *ds; 3244b5152584SMatthew Ahrens 3245b5152584SMatthew Ahrens VERIFY0(dsl_dataset_hold(dmu_tx_pool(tx), dsname, FTAG, &ds)); 3246b5152584SMatthew Ahrens 3247b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync_impl(ds->ds_object, tx); 3248b5152584SMatthew Ahrens ASSERT(!ds->ds_large_blocks); 3249b5152584SMatthew Ahrens ds->ds_large_blocks = B_TRUE; 3250b5152584SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3251b5152584SMatthew Ahrens } 3252b5152584SMatthew Ahrens 3253b5152584SMatthew Ahrens int 3254b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks(const char *dsname) 3255b5152584SMatthew Ahrens { 3256b5152584SMatthew Ahrens int error; 3257b5152584SMatthew Ahrens 3258b5152584SMatthew Ahrens error = dsl_sync_task(dsname, 3259b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_check, 3260b5152584SMatthew Ahrens dsl_dataset_activate_large_blocks_sync, (void *)dsname, 3261b5152584SMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED); 3262b5152584SMatthew Ahrens 3263b5152584SMatthew Ahrens /* 3264b5152584SMatthew Ahrens * EALREADY indicates that this dataset already supports large blocks. 3265b5152584SMatthew Ahrens */ 3266b5152584SMatthew Ahrens if (error == EALREADY) 3267b5152584SMatthew Ahrens error = 0; 3268b5152584SMatthew Ahrens return (error); 3269b5152584SMatthew Ahrens } 3270b5152584SMatthew Ahrens 32713b2aab18SMatthew Ahrens /* 32723b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 32733b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 32743b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 32753b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 32763b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 327778f17100SMatthew Ahrens * 327878f17100SMatthew Ahrens * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 32793b2aab18SMatthew Ahrens */ 32803b2aab18SMatthew Ahrens boolean_t 328178f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 328278f17100SMatthew Ahrens uint64_t earlier_txg) 32833b2aab18SMatthew Ahrens { 32843b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 32853b2aab18SMatthew Ahrens int error; 32863b2aab18SMatthew Ahrens boolean_t ret; 32873b2aab18SMatthew Ahrens 32883b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 328978f17100SMatthew Ahrens ASSERT(dsl_dataset_is_snapshot(earlier) || earlier_txg != 0); 329078f17100SMatthew Ahrens 329178f17100SMatthew Ahrens if (earlier_txg == 0) 3292*c1379625SJustin T. Gibbs earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg; 32933b2aab18SMatthew Ahrens 329478f17100SMatthew Ahrens if (dsl_dataset_is_snapshot(later) && 3295*c1379625SJustin T. Gibbs earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg) 32963b2aab18SMatthew Ahrens return (B_FALSE); 32973b2aab18SMatthew Ahrens 32983b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 32993b2aab18SMatthew Ahrens return (B_TRUE); 33003b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 33013b2aab18SMatthew Ahrens return (B_FALSE); 33023b2aab18SMatthew Ahrens 3303*c1379625SJustin T. Gibbs if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object) 33043b2aab18SMatthew Ahrens return (B_TRUE); 33053b2aab18SMatthew Ahrens dsl_dataset_t *origin; 33063b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 3307*c1379625SJustin T. Gibbs dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin); 33083b2aab18SMatthew Ahrens if (error != 0) 33093b2aab18SMatthew Ahrens return (B_FALSE); 331078f17100SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 33113b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 33123b2aab18SMatthew Ahrens return (ret); 33133b2aab18SMatthew Ahrens } 33142acef22dSMatthew Ahrens 33152acef22dSMatthew Ahrens 33162acef22dSMatthew Ahrens void 33172acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 33182acef22dSMatthew Ahrens { 33192acef22dSMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 33202acef22dSMatthew Ahrens dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 33212acef22dSMatthew Ahrens } 3322