1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 225afc78aaSChris Kirby * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23ca0cc391SMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 24a2afb611SJerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved. 2503d1795fSAlexander Stetsenko * Copyright (c) 2014 RackTop Systems. 26bc9014e6SJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 27c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 28*5f7a8e6dSDan McDonald * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved. 29fa9e4066Sahrens */ 30fa9e4066Sahrens 31fa9e4066Sahrens #include <sys/dmu_objset.h> 32fa9e4066Sahrens #include <sys/dsl_dataset.h> 33fa9e4066Sahrens #include <sys/dsl_dir.h> 3499653d4eSeschrock #include <sys/dsl_prop.h> 351d452cf5Sahrens #include <sys/dsl_synctask.h> 36fa9e4066Sahrens #include <sys/dmu_traverse.h> 374e3c9f44SBill Pijewski #include <sys/dmu_impl.h> 38fa9e4066Sahrens #include <sys/dmu_tx.h> 39fa9e4066Sahrens #include <sys/arc.h> 40fa9e4066Sahrens #include <sys/zio.h> 41fa9e4066Sahrens #include <sys/zap.h> 42ad135b5dSChristopher Siden #include <sys/zfeature.h> 43fa9e4066Sahrens #include <sys/unique.h> 44fa9e4066Sahrens #include <sys/zfs_context.h> 45cdf5b4caSmmusante #include <sys/zfs_ioctl.h> 46ecd6cf80Smarks #include <sys/spa.h> 47088f3894Sahrens #include <sys/zfs_znode.h> 48c99e4bdcSChris Kirby #include <sys/zfs_onexit.h> 49842727c2SChris Kirby #include <sys/zvol.h> 503f9d6ad7SLin Ling #include <sys/dsl_scan.h> 51cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h> 523b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h> 533b2aab18SMatthew Ahrens #include <sys/dsl_userhold.h> 5478f17100SMatthew Ahrens #include <sys/dsl_bookmark.h> 5545818ee1SMatthew Ahrens #include <sys/dmu_send.h> 5645818ee1SMatthew Ahrens #include <sys/zio_checksum.h> 579c3fd121SMatthew Ahrens #include <sys/zio_compress.h> 589c3fd121SMatthew Ahrens #include <zfs_fletcher.h> 59e1930233Sbonwick 60b5152584SMatthew Ahrens /* 61b5152584SMatthew Ahrens * The SPA supports block sizes up to 16MB. However, very large blocks 62b5152584SMatthew Ahrens * can have an impact on i/o latency (e.g. tying up a spinning disk for 63b5152584SMatthew Ahrens * ~300ms), and also potentially on the memory allocator. Therefore, 64b5152584SMatthew Ahrens * we do not allow the recordsize to be set larger than zfs_max_recordsize 65b5152584SMatthew Ahrens * (default 1MB). Larger blocks can be created by changing this tunable, 66b5152584SMatthew Ahrens * and pools with larger blocks can always be imported and used, regardless 67b5152584SMatthew Ahrens * of this setting. 68b5152584SMatthew Ahrens */ 69b5152584SMatthew Ahrens int zfs_max_recordsize = 1 * 1024 * 1024; 70b5152584SMatthew Ahrens 71cde58dbcSMatthew Ahrens #define SWITCH64(x, y) \ 72cde58dbcSMatthew Ahrens { \ 73cde58dbcSMatthew Ahrens uint64_t __tmp = (x); \ 74cde58dbcSMatthew Ahrens (x) = (y); \ 75cde58dbcSMatthew Ahrens (y) = __tmp; \ 76cde58dbcSMatthew Ahrens } 77cde58dbcSMatthew Ahrens 7855434c77Sek #define DS_REF_MAX (1ULL << 62) 79fa9e4066Sahrens 80c1379625SJustin T. Gibbs extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds); 81c1379625SJustin T. Gibbs 82*5f7a8e6dSDan McDonald extern int spa_asize_inflation; 83*5f7a8e6dSDan McDonald 84a9799022Sck /* 85a9799022Sck * Figure out how much of this delta should be propogated to the dsl_dir 86a9799022Sck * layer. If there's a refreservation, that space has already been 87a9799022Sck * partially accounted for in our ancestors. 88a9799022Sck */ 89a9799022Sck static int64_t 90a9799022Sck parent_delta(dsl_dataset_t *ds, int64_t delta) 91a9799022Sck { 92c1379625SJustin T. Gibbs dsl_dataset_phys_t *ds_phys; 93a9799022Sck uint64_t old_bytes, new_bytes; 94a9799022Sck 95a9799022Sck if (ds->ds_reserved == 0) 96a9799022Sck return (delta); 97a9799022Sck 98c1379625SJustin T. Gibbs ds_phys = dsl_dataset_phys(ds); 99c1379625SJustin T. Gibbs old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved); 100c1379625SJustin T. Gibbs new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved); 101a9799022Sck 102a9799022Sck ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta)); 103a9799022Sck return (new_bytes - old_bytes); 104a9799022Sck } 105fa9e4066Sahrens 106fa9e4066Sahrens void 107b24ab676SJeff Bonwick dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx) 108fa9e4066Sahrens { 109b24ab676SJeff Bonwick int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 110fa9e4066Sahrens int compressed = BP_GET_PSIZE(bp); 111fa9e4066Sahrens int uncompressed = BP_GET_UCSIZE(bp); 112a9799022Sck int64_t delta; 113fa9e4066Sahrens 1143f9d6ad7SLin Ling dprintf_bp(bp, "ds=%p", ds); 115fa9e4066Sahrens 116fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 117fa9e4066Sahrens /* It could have been compressed away to nothing */ 118fa9e4066Sahrens if (BP_IS_HOLE(bp)) 119fa9e4066Sahrens return; 120fa9e4066Sahrens ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE); 121ad135b5dSChristopher Siden ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp))); 122fa9e4066Sahrens if (ds == NULL) { 123ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 124ce636f8bSMatthew Ahrens used, compressed, uncompressed); 125fa9e4066Sahrens return; 126fa9e4066Sahrens } 1273f9d6ad7SLin Ling 128b62969f8SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 129fa9e4066Sahrens mutex_enter(&ds->ds_lock); 130a9799022Sck delta = parent_delta(ds, used); 131c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes += used; 132c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes += compressed; 133c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes += uncompressed; 134c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes += used; 13545818ee1SMatthew Ahrens 136ca0cc391SMatthew Ahrens if (BP_GET_LSIZE(bp) > SPA_OLD_MAXBLOCKSIZE) { 137ca0cc391SMatthew Ahrens ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_BLOCKS] = 138ca0cc391SMatthew Ahrens B_TRUE; 139ca0cc391SMatthew Ahrens } 14045818ee1SMatthew Ahrens 14145818ee1SMatthew Ahrens spa_feature_t f = zio_checksum_to_feature(BP_GET_CHECKSUM(bp)); 14245818ee1SMatthew Ahrens if (f != SPA_FEATURE_NONE) 14345818ee1SMatthew Ahrens ds->ds_feature_activation_needed[f] = B_TRUE; 14445818ee1SMatthew Ahrens 145fa9e4066Sahrens mutex_exit(&ds->ds_lock); 14674e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta, 14774e7dc98SMatthew Ahrens compressed, uncompressed, tx); 14874e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used - delta, 14974e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 150fa9e4066Sahrens } 151fa9e4066Sahrens 152cdb0ab79Smaybee int 153b24ab676SJeff Bonwick dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx, 154b24ab676SJeff Bonwick boolean_t async) 155fa9e4066Sahrens { 15643466aaeSMax Grossman int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp); 15743466aaeSMax Grossman int compressed = BP_GET_PSIZE(bp); 15843466aaeSMax Grossman int uncompressed = BP_GET_UCSIZE(bp); 15943466aaeSMax Grossman 160fa9e4066Sahrens if (BP_IS_HOLE(bp)) 161cdb0ab79Smaybee return (0); 162fa9e4066Sahrens 163b24ab676SJeff Bonwick ASSERT(dmu_tx_is_syncing(tx)); 164b24ab676SJeff Bonwick ASSERT(bp->blk_birth <= tx->tx_txg); 165b24ab676SJeff Bonwick 166fa9e4066Sahrens if (ds == NULL) { 167b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 168ce636f8bSMatthew Ahrens dsl_pool_mos_diduse_space(tx->tx_pool, 169ce636f8bSMatthew Ahrens -used, -compressed, -uncompressed); 170cdb0ab79Smaybee return (used); 171fa9e4066Sahrens } 172fa9e4066Sahrens ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool); 173fa9e4066Sahrens 174bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 175fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 176fa9e4066Sahrens 177c1379625SJustin T. Gibbs if (bp->blk_birth > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 178a9799022Sck int64_t delta; 179c717a561Smaybee 1803f9d6ad7SLin Ling dprintf_bp(bp, "freeing ds=%llu", ds->ds_object); 181b24ab676SJeff Bonwick dsl_free(tx->tx_pool, tx->tx_txg, bp); 182fa9e4066Sahrens 183fa9e4066Sahrens mutex_enter(&ds->ds_lock); 184c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_unique_bytes >= used || 185a9799022Sck !DS_UNIQUE_IS_ACCURATE(ds)); 186a9799022Sck delta = parent_delta(ds, -used); 187c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes -= used; 188fa9e4066Sahrens mutex_exit(&ds->ds_lock); 18974e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, 190a9799022Sck delta, -compressed, -uncompressed, tx); 19174e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, -used - delta, 19274e7dc98SMatthew Ahrens DD_USED_REFRSRV, DD_USED_HEAD, tx); 193fa9e4066Sahrens } else { 194fa9e4066Sahrens dprintf_bp(bp, "putting on dead list: %s", ""); 195b24ab676SJeff Bonwick if (async) { 196b24ab676SJeff Bonwick /* 197b24ab676SJeff Bonwick * We are here as part of zio's write done callback, 198b24ab676SJeff Bonwick * which means we're a zio interrupt thread. We can't 199cde58dbcSMatthew Ahrens * call dsl_deadlist_insert() now because it may block 200b24ab676SJeff Bonwick * waiting for I/O. Instead, put bp on the deferred 201b24ab676SJeff Bonwick * queue and let dsl_pool_sync() finish the job. 202b24ab676SJeff Bonwick */ 203cde58dbcSMatthew Ahrens bplist_append(&ds->ds_pending_deadlist, bp); 204b24ab676SJeff Bonwick } else { 205cde58dbcSMatthew Ahrens dsl_deadlist_insert(&ds->ds_deadlist, bp, tx); 206b24ab676SJeff Bonwick } 207a4611edeSahrens ASSERT3U(ds->ds_prev->ds_object, ==, 208c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj); 209c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_num_children > 0); 210fa9e4066Sahrens /* if (bp->blk_birth > prev prev snap txg) prev unique += bs */ 211c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 212a4611edeSahrens ds->ds_object && bp->blk_birth > 213c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_prev_snap_txg) { 214a4611edeSahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 215a4611edeSahrens mutex_enter(&ds->ds_prev->ds_lock); 216c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_unique_bytes += used; 217a4611edeSahrens mutex_exit(&ds->ds_prev->ds_lock); 218fa9e4066Sahrens } 2193f9d6ad7SLin Ling if (bp->blk_birth > ds->ds_dir->dd_origin_txg) { 22074e7dc98SMatthew Ahrens dsl_dir_transfer_space(ds->ds_dir, used, 22174e7dc98SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 22274e7dc98SMatthew Ahrens } 223fa9e4066Sahrens } 224fa9e4066Sahrens mutex_enter(&ds->ds_lock); 225c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_referenced_bytes, >=, used); 226c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes -= used; 227c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_compressed_bytes, >=, compressed); 228c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes -= compressed; 229c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_uncompressed_bytes, >=, uncompressed); 230c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes -= uncompressed; 231fa9e4066Sahrens mutex_exit(&ds->ds_lock); 232cdb0ab79Smaybee 233cdb0ab79Smaybee return (used); 234fa9e4066Sahrens } 235fa9e4066Sahrens 236ea8dc4b6Seschrock uint64_t 237ea8dc4b6Seschrock dsl_dataset_prev_snap_txg(dsl_dataset_t *ds) 238fa9e4066Sahrens { 239a2eea2e1Sahrens uint64_t trysnap = 0; 240a2eea2e1Sahrens 241fa9e4066Sahrens if (ds == NULL) 242ea8dc4b6Seschrock return (0); 243fa9e4066Sahrens /* 244fa9e4066Sahrens * The snapshot creation could fail, but that would cause an 245fa9e4066Sahrens * incorrect FALSE return, which would only result in an 246fa9e4066Sahrens * overestimation of the amount of space that an operation would 247fa9e4066Sahrens * consume, which is OK. 248fa9e4066Sahrens * 249fa9e4066Sahrens * There's also a small window where we could miss a pending 250fa9e4066Sahrens * snapshot, because we could set the sync task in the quiescing 251fa9e4066Sahrens * phase. So this should only be used as a guess. 252fa9e4066Sahrens */ 253a2eea2e1Sahrens if (ds->ds_trysnap_txg > 254a2eea2e1Sahrens spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa)) 255a2eea2e1Sahrens trysnap = ds->ds_trysnap_txg; 256c1379625SJustin T. Gibbs return (MAX(dsl_dataset_phys(ds)->ds_prev_snap_txg, trysnap)); 257ea8dc4b6Seschrock } 258ea8dc4b6Seschrock 2593d692628SSanjeev Bagewadi boolean_t 260c7cd2421SGeorge Wilson dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp, 261c7cd2421SGeorge Wilson uint64_t blk_birth) 262ea8dc4b6Seschrock { 26343466aaeSMax Grossman if (blk_birth <= dsl_dataset_prev_snap_txg(ds) || 26443466aaeSMax Grossman (bp != NULL && BP_IS_HOLE(bp))) 265c7cd2421SGeorge Wilson return (B_FALSE); 266c7cd2421SGeorge Wilson 267837b568bSGeorge Wilson ddt_prefetch(dsl_dataset_get_spa(ds), bp); 268c7cd2421SGeorge Wilson 269c7cd2421SGeorge Wilson return (B_TRUE); 270fa9e4066Sahrens } 271fa9e4066Sahrens 272fa9e4066Sahrens static void 273bc9014e6SJustin Gibbs dsl_dataset_evict(void *dbu) 274fa9e4066Sahrens { 275bc9014e6SJustin Gibbs dsl_dataset_t *ds = dbu; 276fa9e4066Sahrens 2773b2aab18SMatthew Ahrens ASSERT(ds->ds_owner == NULL); 278fa9e4066Sahrens 279bc9014e6SJustin Gibbs ds->ds_dbuf = NULL; 280bc9014e6SJustin Gibbs 28191ebeef5Sahrens unique_remove(ds->ds_fsid_guid); 282fa9e4066Sahrens 283503ad85cSMatthew Ahrens if (ds->ds_objset != NULL) 284503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 285fa9e4066Sahrens 286fa9e4066Sahrens if (ds->ds_prev) { 2873b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 288fa9e4066Sahrens ds->ds_prev = NULL; 289fa9e4066Sahrens } 290fa9e4066Sahrens 291cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 292bc9014e6SJustin Gibbs if (ds->ds_deadlist.dl_os != NULL) 293cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 294745cd3c5Smaybee if (ds->ds_dir) 295bc9014e6SJustin Gibbs dsl_dir_async_rele(ds->ds_dir, ds); 296fa9e4066Sahrens 29791ebeef5Sahrens ASSERT(!list_link_active(&ds->ds_synced_link)); 298fa9e4066Sahrens 29903bad06fSJustin Gibbs list_destroy(&ds->ds_prop_cbs); 3005ad82045Snd mutex_destroy(&ds->ds_lock); 30191ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 302d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 3033b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 3045ad82045Snd 305fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 306fa9e4066Sahrens } 307fa9e4066Sahrens 3083b2aab18SMatthew Ahrens int 309fa9e4066Sahrens dsl_dataset_get_snapname(dsl_dataset_t *ds) 310fa9e4066Sahrens { 311fa9e4066Sahrens dsl_dataset_phys_t *headphys; 312fa9e4066Sahrens int err; 313fa9e4066Sahrens dmu_buf_t *headdbuf; 314fa9e4066Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 315fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 316fa9e4066Sahrens 317fa9e4066Sahrens if (ds->ds_snapname[0]) 318ea8dc4b6Seschrock return (0); 319c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj == 0) 320ea8dc4b6Seschrock return (0); 321fa9e4066Sahrens 322c1379625SJustin T. Gibbs err = dmu_bonus_hold(mos, dsl_dir_phys(ds->ds_dir)->dd_head_dataset_obj, 323ea8dc4b6Seschrock FTAG, &headdbuf); 3243b2aab18SMatthew Ahrens if (err != 0) 325ea8dc4b6Seschrock return (err); 326fa9e4066Sahrens headphys = headdbuf->db_data; 327fa9e4066Sahrens err = zap_value_search(dp->dp_meta_objset, 328e7437265Sahrens headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname); 329ea8dc4b6Seschrock dmu_buf_rele(headdbuf, FTAG); 330ea8dc4b6Seschrock return (err); 331fa9e4066Sahrens } 332fa9e4066Sahrens 3333b2aab18SMatthew Ahrens int 334745cd3c5Smaybee dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value) 335ab04eb8eStimh { 336745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 337c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 338ab04eb8eStimh matchtype_t mt; 339ab04eb8eStimh int err; 340ab04eb8eStimh 341c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 342ab04eb8eStimh mt = MT_FIRST; 343ab04eb8eStimh else 344ab04eb8eStimh mt = MT_EXACT; 345ab04eb8eStimh 346745cd3c5Smaybee err = zap_lookup_norm(mos, snapobj, name, 8, 1, 347ab04eb8eStimh value, mt, NULL, 0, NULL); 348ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 349745cd3c5Smaybee err = zap_lookup(mos, snapobj, name, 8, 1, value); 350ab04eb8eStimh return (err); 351ab04eb8eStimh } 352ab04eb8eStimh 3533b2aab18SMatthew Ahrens int 354a2afb611SJerry Jelinek dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx, 355a2afb611SJerry Jelinek boolean_t adj_cnt) 356ab04eb8eStimh { 357745cd3c5Smaybee objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 358c1379625SJustin T. Gibbs uint64_t snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 359ab04eb8eStimh matchtype_t mt; 360ab04eb8eStimh int err; 361ab04eb8eStimh 36271eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 36371eb0538SChris Kirby 364c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 365ab04eb8eStimh mt = MT_FIRST; 366ab04eb8eStimh else 367ab04eb8eStimh mt = MT_EXACT; 368ab04eb8eStimh 369745cd3c5Smaybee err = zap_remove_norm(mos, snapobj, name, mt, tx); 370ab04eb8eStimh if (err == ENOTSUP && mt == MT_FIRST) 371745cd3c5Smaybee err = zap_remove(mos, snapobj, name, tx); 372a2afb611SJerry Jelinek 373a2afb611SJerry Jelinek if (err == 0 && adj_cnt) 374a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, -1, 375a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 376a2afb611SJerry Jelinek 377ab04eb8eStimh return (err); 378ab04eb8eStimh } 379ab04eb8eStimh 380e57a022bSJustin T. Gibbs boolean_t 381e57a022bSJustin T. Gibbs dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag) 382e57a022bSJustin T. Gibbs { 3839d47dec0SJustin T. Gibbs dmu_buf_t *dbuf = ds->ds_dbuf; 3849d47dec0SJustin T. Gibbs boolean_t result = B_FALSE; 3859d47dec0SJustin T. Gibbs 3869d47dec0SJustin T. Gibbs if (dbuf != NULL && dmu_buf_try_add_ref(dbuf, dp->dp_meta_objset, 3879d47dec0SJustin T. Gibbs ds->ds_object, DMU_BONUS_BLKID, tag)) { 3889d47dec0SJustin T. Gibbs 3899d47dec0SJustin T. Gibbs if (ds == dmu_buf_get_user(dbuf)) 3909d47dec0SJustin T. Gibbs result = B_TRUE; 3919d47dec0SJustin T. Gibbs else 3929d47dec0SJustin T. Gibbs dmu_buf_rele(dbuf, tag); 3939d47dec0SJustin T. Gibbs } 3949d47dec0SJustin T. Gibbs 3959d47dec0SJustin T. Gibbs return (result); 396e57a022bSJustin T. Gibbs } 397e57a022bSJustin T. Gibbs 3983b2aab18SMatthew Ahrens int 3993b2aab18SMatthew Ahrens dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag, 400745cd3c5Smaybee dsl_dataset_t **dsp) 401fa9e4066Sahrens { 402fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 403fa9e4066Sahrens dmu_buf_t *dbuf; 404fa9e4066Sahrens dsl_dataset_t *ds; 405ea8dc4b6Seschrock int err; 406a7f53a56SChris Kirby dmu_object_info_t doi; 407fa9e4066Sahrens 4083b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 409fa9e4066Sahrens 410ea8dc4b6Seschrock err = dmu_bonus_hold(mos, dsobj, tag, &dbuf); 4113b2aab18SMatthew Ahrens if (err != 0) 412ea8dc4b6Seschrock return (err); 413a7f53a56SChris Kirby 414a7f53a56SChris Kirby /* Make sure dsobj has the correct object type. */ 415a7f53a56SChris Kirby dmu_object_info_from_db(dbuf, &doi); 4162acef22dSMatthew Ahrens if (doi.doi_bonus_type != DMU_OT_DSL_DATASET) { 417b287be1bSWill Andrews dmu_buf_rele(dbuf, tag); 418be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 419b287be1bSWill Andrews } 420a7f53a56SChris Kirby 421fa9e4066Sahrens ds = dmu_buf_get_user(dbuf); 422fa9e4066Sahrens if (ds == NULL) { 423d5285caeSGeorge Wilson dsl_dataset_t *winner = NULL; 424fa9e4066Sahrens 425fa9e4066Sahrens ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP); 426fa9e4066Sahrens ds->ds_dbuf = dbuf; 427fa9e4066Sahrens ds->ds_object = dsobj; 428bc9014e6SJustin Gibbs ds->ds_is_snapshot = dsl_dataset_phys(ds)->ds_num_children != 0; 429fa9e4066Sahrens 4305ad82045Snd mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL); 43191ebeef5Sahrens mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL); 4324e3c9f44SBill Pijewski mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL); 4333b2aab18SMatthew Ahrens refcount_create(&ds->ds_longholds); 4345ad82045Snd 435cde58dbcSMatthew Ahrens bplist_create(&ds->ds_pending_deadlist); 436cde58dbcSMatthew Ahrens dsl_deadlist_open(&ds->ds_deadlist, 437c1379625SJustin T. Gibbs mos, dsl_dataset_phys(ds)->ds_deadlist_obj); 438cde58dbcSMatthew Ahrens 4394e3c9f44SBill Pijewski list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t), 4404e3c9f44SBill Pijewski offsetof(dmu_sendarg_t, dsa_link)); 4414e3c9f44SBill Pijewski 44203bad06fSJustin Gibbs list_create(&ds->ds_prop_cbs, sizeof (dsl_prop_cb_record_t), 44303bad06fSJustin Gibbs offsetof(dsl_prop_cb_record_t, cbr_ds_node)); 44403bad06fSJustin Gibbs 445b5152584SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 446ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 447ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 448ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) 449ca0cc391SMatthew Ahrens continue; 450ca0cc391SMatthew Ahrens err = zap_contains(mos, dsobj, 451ca0cc391SMatthew Ahrens spa_feature_table[f].fi_guid); 452ca0cc391SMatthew Ahrens if (err == 0) { 453ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 454ca0cc391SMatthew Ahrens } else { 455ca0cc391SMatthew Ahrens ASSERT3U(err, ==, ENOENT); 456ca0cc391SMatthew Ahrens err = 0; 457ca0cc391SMatthew Ahrens } 458e1f3c208SJustin T. Gibbs } 459b5152584SMatthew Ahrens } 460b5152584SMatthew Ahrens 461ca0cc391SMatthew Ahrens err = dsl_dir_hold_obj(dp, 462ca0cc391SMatthew Ahrens dsl_dataset_phys(ds)->ds_dir_obj, NULL, ds, &ds->ds_dir); 4633b2aab18SMatthew Ahrens if (err != 0) { 4645ad82045Snd mutex_destroy(&ds->ds_lock); 46591ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 466d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 4673b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 468cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 469cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 470ea8dc4b6Seschrock kmem_free(ds, sizeof (dsl_dataset_t)); 471ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 472ea8dc4b6Seschrock return (err); 473ea8dc4b6Seschrock } 474fa9e4066Sahrens 475bc9014e6SJustin Gibbs if (!ds->ds_is_snapshot) { 476fa9e4066Sahrens ds->ds_snapname[0] = '\0'; 477c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 4783b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, 479c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, 480745cd3c5Smaybee ds, &ds->ds_prev); 481fa9e4066Sahrens } 48278f17100SMatthew Ahrens if (doi.doi_type == DMU_OTN_ZAP_METADATA) { 48378f17100SMatthew Ahrens int zaperr = zap_lookup(mos, ds->ds_object, 48478f17100SMatthew Ahrens DS_FIELD_BOOKMARK_NAMES, 48578f17100SMatthew Ahrens sizeof (ds->ds_bookmarks), 1, 48678f17100SMatthew Ahrens &ds->ds_bookmarks); 48778f17100SMatthew Ahrens if (zaperr != ENOENT) 48878f17100SMatthew Ahrens VERIFY0(zaperr); 48978f17100SMatthew Ahrens } 490842727c2SChris Kirby } else { 491842727c2SChris Kirby if (zfs_flags & ZFS_DEBUG_SNAPNAMES) 492842727c2SChris Kirby err = dsl_dataset_get_snapname(ds); 493c1379625SJustin T. Gibbs if (err == 0 && 494c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj != 0) { 495842727c2SChris Kirby err = zap_count( 496842727c2SChris Kirby ds->ds_dir->dd_pool->dp_meta_objset, 497c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_userrefs_obj, 498842727c2SChris Kirby &ds->ds_userrefs); 499842727c2SChris Kirby } 500fa9e4066Sahrens } 501fa9e4066Sahrens 502bc9014e6SJustin Gibbs if (err == 0 && !ds->ds_is_snapshot) { 5033b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 5043b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 5053b2aab18SMatthew Ahrens &ds->ds_reserved); 506cb625fb5Sck if (err == 0) { 5073b2aab18SMatthew Ahrens err = dsl_prop_get_int_ds(ds, 5083b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 5093b2aab18SMatthew Ahrens &ds->ds_quota); 510cb625fb5Sck } 511cb625fb5Sck } else { 512cb625fb5Sck ds->ds_reserved = ds->ds_quota = 0; 513cb625fb5Sck } 514cb625fb5Sck 515bc9014e6SJustin Gibbs dmu_buf_init_user(&ds->ds_dbu, dsl_dataset_evict, &ds->ds_dbuf); 516bc9014e6SJustin Gibbs if (err == 0) 517bc9014e6SJustin Gibbs winner = dmu_buf_set_user_ie(dbuf, &ds->ds_dbu); 518bc9014e6SJustin Gibbs 519bc9014e6SJustin Gibbs if (err != 0 || winner != NULL) { 520cde58dbcSMatthew Ahrens bplist_destroy(&ds->ds_pending_deadlist); 521cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 522745cd3c5Smaybee if (ds->ds_prev) 5233b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 5243b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 5255ad82045Snd mutex_destroy(&ds->ds_lock); 52691ebeef5Sahrens mutex_destroy(&ds->ds_opening_lock); 527d2b3cbbdSJorgen Lundman mutex_destroy(&ds->ds_sendstream_lock); 5283b2aab18SMatthew Ahrens refcount_destroy(&ds->ds_longholds); 529fa9e4066Sahrens kmem_free(ds, sizeof (dsl_dataset_t)); 5303b2aab18SMatthew Ahrens if (err != 0) { 531ea8dc4b6Seschrock dmu_buf_rele(dbuf, tag); 532ea8dc4b6Seschrock return (err); 533ea8dc4b6Seschrock } 534fa9e4066Sahrens ds = winner; 535fa9e4066Sahrens } else { 53691ebeef5Sahrens ds->ds_fsid_guid = 537c1379625SJustin T. Gibbs unique_insert(dsl_dataset_phys(ds)->ds_fsid_guid); 538fa9e4066Sahrens } 539fa9e4066Sahrens } 540fa9e4066Sahrens ASSERT3P(ds->ds_dbuf, ==, dbuf); 541c1379625SJustin T. Gibbs ASSERT3P(dsl_dataset_phys(ds), ==, dbuf->db_data); 542c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_prev_snap_obj != 0 || 543afc6333aSahrens spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN || 54484db2a68Sahrens dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap); 545ea8dc4b6Seschrock *dsp = ds; 546ea8dc4b6Seschrock return (0); 547fa9e4066Sahrens } 548fa9e4066Sahrens 549745cd3c5Smaybee int 5503b2aab18SMatthew Ahrens dsl_dataset_hold(dsl_pool_t *dp, const char *name, 551503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 552fa9e4066Sahrens { 553fa9e4066Sahrens dsl_dir_t *dd; 554745cd3c5Smaybee const char *snapname; 555fa9e4066Sahrens uint64_t obj; 556fa9e4066Sahrens int err = 0; 557a2cdcdd2SPaul Dagnelie dsl_dataset_t *ds; 558fa9e4066Sahrens 5593b2aab18SMatthew Ahrens err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname); 5603b2aab18SMatthew Ahrens if (err != 0) 561ea8dc4b6Seschrock return (err); 562fa9e4066Sahrens 5633b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 564c1379625SJustin T. Gibbs obj = dsl_dir_phys(dd)->dd_head_dataset_obj; 5653b2aab18SMatthew Ahrens if (obj != 0) 566a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 567745cd3c5Smaybee else 568be6fd75aSMatthew Ahrens err = SET_ERROR(ENOENT); 569fa9e4066Sahrens 570745cd3c5Smaybee /* we may be looking for a snapshot */ 571745cd3c5Smaybee if (err == 0 && snapname != NULL) { 572a2cdcdd2SPaul Dagnelie dsl_dataset_t *snap_ds; 573fa9e4066Sahrens 574745cd3c5Smaybee if (*snapname++ != '@') { 575a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 5763b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 577be6fd75aSMatthew Ahrens return (SET_ERROR(ENOENT)); 578fa9e4066Sahrens } 579fa9e4066Sahrens 580745cd3c5Smaybee dprintf("looking for snapshot '%s'\n", snapname); 581a2cdcdd2SPaul Dagnelie err = dsl_dataset_snap_lookup(ds, snapname, &obj); 582745cd3c5Smaybee if (err == 0) 583a2cdcdd2SPaul Dagnelie err = dsl_dataset_hold_obj(dp, obj, tag, &snap_ds); 584a2cdcdd2SPaul Dagnelie dsl_dataset_rele(ds, tag); 585745cd3c5Smaybee 5863b2aab18SMatthew Ahrens if (err == 0) { 587a2cdcdd2SPaul Dagnelie mutex_enter(&snap_ds->ds_lock); 588a2cdcdd2SPaul Dagnelie if (snap_ds->ds_snapname[0] == 0) 589a2cdcdd2SPaul Dagnelie (void) strlcpy(snap_ds->ds_snapname, snapname, 590a2cdcdd2SPaul Dagnelie sizeof (snap_ds->ds_snapname)); 591a2cdcdd2SPaul Dagnelie mutex_exit(&snap_ds->ds_lock); 592a2cdcdd2SPaul Dagnelie ds = snap_ds; 593fa9e4066Sahrens } 594fa9e4066Sahrens } 595a2cdcdd2SPaul Dagnelie if (err == 0) 596a2cdcdd2SPaul Dagnelie *dsp = ds; 5973b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 598fa9e4066Sahrens return (err); 599fa9e4066Sahrens } 600fa9e4066Sahrens 601fa9e4066Sahrens int 6023b2aab18SMatthew Ahrens dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj, 6033b2aab18SMatthew Ahrens void *tag, dsl_dataset_t **dsp) 6043b2aab18SMatthew Ahrens { 6053b2aab18SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp); 6063b2aab18SMatthew Ahrens if (err != 0) 6073b2aab18SMatthew Ahrens return (err); 6083b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 6093b2aab18SMatthew Ahrens dsl_dataset_rele(*dsp, tag); 6103b2aab18SMatthew Ahrens *dsp = NULL; 611be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 6123b2aab18SMatthew Ahrens } 6133b2aab18SMatthew Ahrens return (0); 6143b2aab18SMatthew Ahrens } 6153b2aab18SMatthew Ahrens 6163b2aab18SMatthew Ahrens int 6173b2aab18SMatthew Ahrens dsl_dataset_own(dsl_pool_t *dp, const char *name, 618503ad85cSMatthew Ahrens void *tag, dsl_dataset_t **dsp) 619fa9e4066Sahrens { 6203b2aab18SMatthew Ahrens int err = dsl_dataset_hold(dp, name, tag, dsp); 6213b2aab18SMatthew Ahrens if (err != 0) 622745cd3c5Smaybee return (err); 6233b2aab18SMatthew Ahrens if (!dsl_dataset_tryown(*dsp, tag)) { 624503ad85cSMatthew Ahrens dsl_dataset_rele(*dsp, tag); 625be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 626745cd3c5Smaybee } 627745cd3c5Smaybee return (0); 628fa9e4066Sahrens } 629fa9e4066Sahrens 6303b2aab18SMatthew Ahrens /* 6313b2aab18SMatthew Ahrens * See the comment above dsl_pool_hold() for details. In summary, a long 6323b2aab18SMatthew Ahrens * hold is used to prevent destruction of a dataset while the pool hold 6333b2aab18SMatthew Ahrens * is dropped, allowing other concurrent operations (e.g. spa_sync()). 6343b2aab18SMatthew Ahrens * 6353b2aab18SMatthew Ahrens * The dataset and pool must be held when this function is called. After it 6363b2aab18SMatthew Ahrens * is called, the pool hold may be released while the dataset is still held 6373b2aab18SMatthew Ahrens * and accessed. 6383b2aab18SMatthew Ahrens */ 6393b2aab18SMatthew Ahrens void 6403b2aab18SMatthew Ahrens dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag) 6413b2aab18SMatthew Ahrens { 6423b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 6433b2aab18SMatthew Ahrens (void) refcount_add(&ds->ds_longholds, tag); 6443b2aab18SMatthew Ahrens } 6453b2aab18SMatthew Ahrens 6463b2aab18SMatthew Ahrens void 6473b2aab18SMatthew Ahrens dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag) 6483b2aab18SMatthew Ahrens { 6493b2aab18SMatthew Ahrens (void) refcount_remove(&ds->ds_longholds, tag); 6503b2aab18SMatthew Ahrens } 6513b2aab18SMatthew Ahrens 6523b2aab18SMatthew Ahrens /* Return B_TRUE if there are any long holds on this dataset. */ 6533b2aab18SMatthew Ahrens boolean_t 6543b2aab18SMatthew Ahrens dsl_dataset_long_held(dsl_dataset_t *ds) 6553b2aab18SMatthew Ahrens { 6563b2aab18SMatthew Ahrens return (!refcount_is_zero(&ds->ds_longholds)); 6573b2aab18SMatthew Ahrens } 6583b2aab18SMatthew Ahrens 659fa9e4066Sahrens void 660fa9e4066Sahrens dsl_dataset_name(dsl_dataset_t *ds, char *name) 661fa9e4066Sahrens { 662fa9e4066Sahrens if (ds == NULL) { 663fa9e4066Sahrens (void) strcpy(name, "mos"); 664fa9e4066Sahrens } else { 665fa9e4066Sahrens dsl_dir_name(ds->ds_dir, name); 6663b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 667fa9e4066Sahrens if (ds->ds_snapname[0]) { 668fa9e4066Sahrens (void) strcat(name, "@"); 669745cd3c5Smaybee /* 670745cd3c5Smaybee * We use a "recursive" mutex so that we 671745cd3c5Smaybee * can call dprintf_ds() with ds_lock held. 672745cd3c5Smaybee */ 673fa9e4066Sahrens if (!MUTEX_HELD(&ds->ds_lock)) { 674fa9e4066Sahrens mutex_enter(&ds->ds_lock); 675fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 676fa9e4066Sahrens mutex_exit(&ds->ds_lock); 677fa9e4066Sahrens } else { 678fa9e4066Sahrens (void) strcat(name, ds->ds_snapname); 679fa9e4066Sahrens } 680fa9e4066Sahrens } 681fa9e4066Sahrens } 682fa9e4066Sahrens } 683fa9e4066Sahrens 6843cb34c60Sahrens void 685745cd3c5Smaybee dsl_dataset_rele(dsl_dataset_t *ds, void *tag) 6863cb34c60Sahrens { 6873b2aab18SMatthew Ahrens dmu_buf_rele(ds->ds_dbuf, tag); 688745cd3c5Smaybee } 689745cd3c5Smaybee 690745cd3c5Smaybee void 691503ad85cSMatthew Ahrens dsl_dataset_disown(dsl_dataset_t *ds, void *tag) 692745cd3c5Smaybee { 693d808a4fcSJustin T. Gibbs ASSERT3P(ds->ds_owner, ==, tag); 694d808a4fcSJustin T. Gibbs ASSERT(ds->ds_dbuf != NULL); 695745cd3c5Smaybee 6963cb34c60Sahrens mutex_enter(&ds->ds_lock); 697745cd3c5Smaybee ds->ds_owner = NULL; 6983cb34c60Sahrens mutex_exit(&ds->ds_lock); 6993b2aab18SMatthew Ahrens dsl_dataset_long_rele(ds, tag); 700d808a4fcSJustin T. Gibbs dsl_dataset_rele(ds, tag); 7013cb34c60Sahrens } 7023cb34c60Sahrens 7033cb34c60Sahrens boolean_t 7043b2aab18SMatthew Ahrens dsl_dataset_tryown(dsl_dataset_t *ds, void *tag) 7053cb34c60Sahrens { 706745cd3c5Smaybee boolean_t gotit = FALSE; 707745cd3c5Smaybee 7089c3fd121SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 7093cb34c60Sahrens mutex_enter(&ds->ds_lock); 7103b2aab18SMatthew Ahrens if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) { 711503ad85cSMatthew Ahrens ds->ds_owner = tag; 7123b2aab18SMatthew Ahrens dsl_dataset_long_hold(ds, tag); 713745cd3c5Smaybee gotit = TRUE; 7143cb34c60Sahrens } 7153cb34c60Sahrens mutex_exit(&ds->ds_lock); 716745cd3c5Smaybee return (gotit); 717745cd3c5Smaybee } 718745cd3c5Smaybee 7199c3fd121SMatthew Ahrens boolean_t 7209c3fd121SMatthew Ahrens dsl_dataset_has_owner(dsl_dataset_t *ds) 7219c3fd121SMatthew Ahrens { 7229c3fd121SMatthew Ahrens boolean_t rv; 7239c3fd121SMatthew Ahrens mutex_enter(&ds->ds_lock); 7249c3fd121SMatthew Ahrens rv = (ds->ds_owner != NULL); 7259c3fd121SMatthew Ahrens mutex_exit(&ds->ds_lock); 7269c3fd121SMatthew Ahrens return (rv); 7279c3fd121SMatthew Ahrens } 7289c3fd121SMatthew Ahrens 729ca0cc391SMatthew Ahrens static void 730ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 731ca0cc391SMatthew Ahrens { 732ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 733ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 734ca0cc391SMatthew Ahrens uint64_t zero = 0; 735ca0cc391SMatthew Ahrens 736ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 737ca0cc391SMatthew Ahrens 738ca0cc391SMatthew Ahrens spa_feature_incr(spa, f, tx); 739ca0cc391SMatthew Ahrens dmu_object_zapify(mos, dsobj, DMU_OT_DSL_DATASET, tx); 740ca0cc391SMatthew Ahrens 741ca0cc391SMatthew Ahrens VERIFY0(zap_add(mos, dsobj, spa_feature_table[f].fi_guid, 742ca0cc391SMatthew Ahrens sizeof (zero), 1, &zero, tx)); 743ca0cc391SMatthew Ahrens } 744ca0cc391SMatthew Ahrens 745ca0cc391SMatthew Ahrens void 746ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(uint64_t dsobj, spa_feature_t f, dmu_tx_t *tx) 747ca0cc391SMatthew Ahrens { 748ca0cc391SMatthew Ahrens spa_t *spa = dmu_tx_pool(tx)->dp_spa; 749ca0cc391SMatthew Ahrens objset_t *mos = dmu_tx_pool(tx)->dp_meta_objset; 750ca0cc391SMatthew Ahrens 751ca0cc391SMatthew Ahrens VERIFY(spa_feature_table[f].fi_flags & ZFEATURE_FLAG_PER_DATASET); 752ca0cc391SMatthew Ahrens 753ca0cc391SMatthew Ahrens VERIFY0(zap_remove(mos, dsobj, spa_feature_table[f].fi_guid, tx)); 754ca0cc391SMatthew Ahrens spa_feature_decr(spa, f, tx); 755ca0cc391SMatthew Ahrens } 756ca0cc391SMatthew Ahrens 7571d452cf5Sahrens uint64_t 758088f3894Sahrens dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin, 759ab04eb8eStimh uint64_t flags, dmu_tx_t *tx) 760fa9e4066Sahrens { 7613cb34c60Sahrens dsl_pool_t *dp = dd->dd_pool; 762fa9e4066Sahrens dmu_buf_t *dbuf; 763fa9e4066Sahrens dsl_dataset_phys_t *dsphys; 7643cb34c60Sahrens uint64_t dsobj; 765fa9e4066Sahrens objset_t *mos = dp->dp_meta_objset; 766fa9e4066Sahrens 767088f3894Sahrens if (origin == NULL) 768088f3894Sahrens origin = dp->dp_origin_snap; 769088f3894Sahrens 7703cb34c60Sahrens ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp); 771c1379625SJustin T. Gibbs ASSERT(origin == NULL || dsl_dataset_phys(origin)->ds_num_children > 0); 772fa9e4066Sahrens ASSERT(dmu_tx_is_syncing(tx)); 773c1379625SJustin T. Gibbs ASSERT(dsl_dir_phys(dd)->dd_head_dataset_obj == 0); 774fa9e4066Sahrens 7751649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 7761649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 7773b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 778fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 779fa9e4066Sahrens dsphys = dbuf->db_data; 780745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 781fa9e4066Sahrens dsphys->ds_dir_obj = dd->dd_object; 782ab04eb8eStimh dsphys->ds_flags = flags; 783fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 784fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 785fa9e4066Sahrens sizeof (dsphys->ds_guid)); 786fa9e4066Sahrens dsphys->ds_snapnames_zapobj = 787ab04eb8eStimh zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP, 788ab04eb8eStimh DMU_OT_NONE, 0, tx); 789fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 790088f3894Sahrens dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg; 791a9799022Sck 792cde58dbcSMatthew Ahrens if (origin == NULL) { 793cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx); 794cde58dbcSMatthew Ahrens } else { 7953b2aab18SMatthew Ahrens dsl_dataset_t *ohds; /* head of the origin snapshot */ 796cde58dbcSMatthew Ahrens 7973cb34c60Sahrens dsphys->ds_prev_snap_obj = origin->ds_object; 798fa9e4066Sahrens dsphys->ds_prev_snap_txg = 799c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_creation_txg; 800ad135b5dSChristopher Siden dsphys->ds_referenced_bytes = 801c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_referenced_bytes; 802fa9e4066Sahrens dsphys->ds_compressed_bytes = 803c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_compressed_bytes; 804fa9e4066Sahrens dsphys->ds_uncompressed_bytes = 805c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_uncompressed_bytes; 806c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(origin)->ds_bp; 80742fcb65eSMatthew Ahrens 80842fcb65eSMatthew Ahrens /* 80942fcb65eSMatthew Ahrens * Inherit flags that describe the dataset's contents 81042fcb65eSMatthew Ahrens * (INCONSISTENT) or properties (Case Insensitive). 81142fcb65eSMatthew Ahrens */ 812c1379625SJustin T. Gibbs dsphys->ds_flags |= dsl_dataset_phys(origin)->ds_flags & 81342fcb65eSMatthew Ahrens (DS_FLAG_INCONSISTENT | DS_FLAG_CI_DATASET); 814fa9e4066Sahrens 815ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 816ca0cc391SMatthew Ahrens if (origin->ds_feature_inuse[f]) 817ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 818ca0cc391SMatthew Ahrens } 819b5152584SMatthew Ahrens 8203cb34c60Sahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 821c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_num_children++; 822fa9e4066Sahrens 8233b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 824c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_head_dataset_obj, 825c1379625SJustin T. Gibbs FTAG, &ohds)); 826cde58dbcSMatthew Ahrens dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist, 827cde58dbcSMatthew Ahrens dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx); 828cde58dbcSMatthew Ahrens dsl_dataset_rele(ohds, FTAG); 829cde58dbcSMatthew Ahrens 830088f3894Sahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) { 831c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin)->ds_next_clones_obj == 0) { 832c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj = 833088f3894Sahrens zap_create(mos, 834088f3894Sahrens DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx); 835088f3894Sahrens } 8363b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 837c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_next_clones_obj, 838c1379625SJustin T. Gibbs dsobj, tx)); 839088f3894Sahrens } 840088f3894Sahrens 841fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 842c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = origin->ds_object; 843cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 844c1379625SJustin T. Gibbs if (dsl_dir_phys(origin->ds_dir)->dd_clones == 0) { 845cde58dbcSMatthew Ahrens dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx); 846c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones = 847cde58dbcSMatthew Ahrens zap_create(mos, 848cde58dbcSMatthew Ahrens DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx); 849cde58dbcSMatthew Ahrens } 8503b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 851c1379625SJustin T. Gibbs dsl_dir_phys(origin->ds_dir)->dd_clones, 852c1379625SJustin T. Gibbs dsobj, tx)); 853cde58dbcSMatthew Ahrens } 854fa9e4066Sahrens } 855ab04eb8eStimh 856ab04eb8eStimh if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 857ab04eb8eStimh dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 858ab04eb8eStimh 859ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 860fa9e4066Sahrens 861fa9e4066Sahrens dmu_buf_will_dirty(dd->dd_dbuf, tx); 862c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_head_dataset_obj = dsobj; 8633cb34c60Sahrens 8643cb34c60Sahrens return (dsobj); 8653cb34c60Sahrens } 8663cb34c60Sahrens 8673b2aab18SMatthew Ahrens static void 8683b2aab18SMatthew Ahrens dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx) 8693b2aab18SMatthew Ahrens { 8703b2aab18SMatthew Ahrens objset_t *os; 8713b2aab18SMatthew Ahrens 8723b2aab18SMatthew Ahrens VERIFY0(dmu_objset_from_ds(ds, &os)); 8733b2aab18SMatthew Ahrens bzero(&os->os_zil_header, sizeof (os->os_zil_header)); 8743b2aab18SMatthew Ahrens dsl_dataset_dirty(ds, tx); 8753b2aab18SMatthew Ahrens } 8763b2aab18SMatthew Ahrens 8773cb34c60Sahrens uint64_t 878ab04eb8eStimh dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname, 879ab04eb8eStimh dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx) 8803cb34c60Sahrens { 8813cb34c60Sahrens dsl_pool_t *dp = pdd->dd_pool; 8823cb34c60Sahrens uint64_t dsobj, ddobj; 8833cb34c60Sahrens dsl_dir_t *dd; 8843cb34c60Sahrens 8853b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 8863cb34c60Sahrens ASSERT(lastname[0] != '@'); 8873cb34c60Sahrens 888088f3894Sahrens ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx); 8893b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd)); 8903cb34c60Sahrens 8913b2aab18SMatthew Ahrens dsobj = dsl_dataset_create_sync_dd(dd, origin, 8923b2aab18SMatthew Ahrens flags & ~DS_CREATE_FLAG_NODIRTY, tx); 8933cb34c60Sahrens 8943cb34c60Sahrens dsl_deleg_set_create_perms(dd, tx, cr); 8953cb34c60Sahrens 896a2afb611SJerry Jelinek /* 897a2afb611SJerry Jelinek * Since we're creating a new node we know it's a leaf, so we can 898a2afb611SJerry Jelinek * initialize the counts if the limit feature is active. 899a2afb611SJerry Jelinek */ 900a2afb611SJerry Jelinek if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { 901a2afb611SJerry Jelinek uint64_t cnt = 0; 902a2afb611SJerry Jelinek objset_t *os = dd->dd_pool->dp_meta_objset; 903a2afb611SJerry Jelinek 904a2afb611SJerry Jelinek dsl_dir_zapify(dd, tx); 905a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, 906a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 907a2afb611SJerry Jelinek VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, 908a2afb611SJerry Jelinek sizeof (cnt), 1, &cnt, tx)); 909a2afb611SJerry Jelinek } 910a2afb611SJerry Jelinek 9113b2aab18SMatthew Ahrens dsl_dir_rele(dd, FTAG); 912fa9e4066Sahrens 913feaa74e4SMark Maybee /* 914feaa74e4SMark Maybee * If we are creating a clone, make sure we zero out any stale 915feaa74e4SMark Maybee * data from the origin snapshots zil header. 916feaa74e4SMark Maybee */ 9173b2aab18SMatthew Ahrens if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) { 918feaa74e4SMark Maybee dsl_dataset_t *ds; 919feaa74e4SMark Maybee 9203b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds)); 9213b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 922feaa74e4SMark Maybee dsl_dataset_rele(ds, FTAG); 923feaa74e4SMark Maybee } 924feaa74e4SMark Maybee 9251d452cf5Sahrens return (dsobj); 926fa9e4066Sahrens } 927fa9e4066Sahrens 9281d452cf5Sahrens /* 9293b2aab18SMatthew Ahrens * The unique space in the head dataset can be calculated by subtracting 9303b2aab18SMatthew Ahrens * the space used in the most recent snapshot, that is still being used 9313b2aab18SMatthew Ahrens * in this file system, from the space currently in use. To figure out 9323b2aab18SMatthew Ahrens * the space in the most recent snapshot still in use, we need to take 9333b2aab18SMatthew Ahrens * the total space used in the snapshot and subtract out the space that 9343b2aab18SMatthew Ahrens * has been freed up since the snapshot was taken. 9351d452cf5Sahrens */ 9363b2aab18SMatthew Ahrens void 9373b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds) 9381d452cf5Sahrens { 9393b2aab18SMatthew Ahrens uint64_t mrs_used; 9403b2aab18SMatthew Ahrens uint64_t dlused, dlcomp, dluncomp; 9411d452cf5Sahrens 942bc9014e6SJustin Gibbs ASSERT(!ds->ds_is_snapshot); 9431d452cf5Sahrens 944c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) 945c1379625SJustin T. Gibbs mrs_used = dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes; 9463b2aab18SMatthew Ahrens else 9473b2aab18SMatthew Ahrens mrs_used = 0; 948842727c2SChris Kirby 9493b2aab18SMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp); 950fa9e4066Sahrens 9513b2aab18SMatthew Ahrens ASSERT3U(dlused, <=, mrs_used); 952c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 953c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes - (mrs_used - dlused); 95419b94df9SMatthew Ahrens 9553b2aab18SMatthew Ahrens if (spa_version(ds->ds_dir->dd_pool->dp_spa) >= 9563b2aab18SMatthew Ahrens SPA_VERSION_UNIQUE_ACCURATE) 957c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 958fa9e4066Sahrens } 959fa9e4066Sahrens 9603b2aab18SMatthew Ahrens void 9613b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj, 9623b2aab18SMatthew Ahrens dmu_tx_t *tx) 963842727c2SChris Kirby { 9643b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 9653b2aab18SMatthew Ahrens uint64_t count; 9663b2aab18SMatthew Ahrens int err; 9673b2aab18SMatthew Ahrens 968c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_num_children >= 2); 969c1379625SJustin T. Gibbs err = zap_remove_int(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 970c1379625SJustin T. Gibbs obj, tx); 9713b2aab18SMatthew Ahrens /* 9723b2aab18SMatthew Ahrens * The err should not be ENOENT, but a bug in a previous version 9733b2aab18SMatthew Ahrens * of the code could cause upgrade_clones_cb() to not set 9743b2aab18SMatthew Ahrens * ds_next_snap_obj when it should, leading to a missing entry. 9753b2aab18SMatthew Ahrens * If we knew that the pool was created after 9763b2aab18SMatthew Ahrens * SPA_VERSION_NEXT_CLONES, we could assert that it isn't 9773b2aab18SMatthew Ahrens * ENOENT. However, at least we can check that we don't have 9783b2aab18SMatthew Ahrens * too many entries in the next_clones_obj even after failing to 9793b2aab18SMatthew Ahrens * remove this one. 9803b2aab18SMatthew Ahrens */ 9813b2aab18SMatthew Ahrens if (err != ENOENT) 9823b2aab18SMatthew Ahrens VERIFY0(err); 983c1379625SJustin T. Gibbs ASSERT0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 9843b2aab18SMatthew Ahrens &count)); 985c1379625SJustin T. Gibbs ASSERT3U(count, <=, dsl_dataset_phys(ds)->ds_num_children - 2); 9863b2aab18SMatthew Ahrens } 987842727c2SChris Kirby 988842727c2SChris Kirby 9893b2aab18SMatthew Ahrens blkptr_t * 9903b2aab18SMatthew Ahrens dsl_dataset_get_blkptr(dsl_dataset_t *ds) 9913b2aab18SMatthew Ahrens { 992c1379625SJustin T. Gibbs return (&dsl_dataset_phys(ds)->ds_bp); 993842727c2SChris Kirby } 994842727c2SChris Kirby 9953b2aab18SMatthew Ahrens void 9963b2aab18SMatthew Ahrens dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx) 997842727c2SChris Kirby { 9983b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 9993b2aab18SMatthew Ahrens /* If it's the meta-objset, set dp_meta_rootbp */ 10003b2aab18SMatthew Ahrens if (ds == NULL) { 10013b2aab18SMatthew Ahrens tx->tx_pool->dp_meta_rootbp = *bp; 10023b2aab18SMatthew Ahrens } else { 10033b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1004c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_bp = *bp; 1005842727c2SChris Kirby } 10063b2aab18SMatthew Ahrens } 1007842727c2SChris Kirby 10083b2aab18SMatthew Ahrens spa_t * 10093b2aab18SMatthew Ahrens dsl_dataset_get_spa(dsl_dataset_t *ds) 10103b2aab18SMatthew Ahrens { 10113b2aab18SMatthew Ahrens return (ds->ds_dir->dd_pool->dp_spa); 1012842727c2SChris Kirby } 1013842727c2SChris Kirby 10143b2aab18SMatthew Ahrens void 10153b2aab18SMatthew Ahrens dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx) 1016fa9e4066Sahrens { 10173b2aab18SMatthew Ahrens dsl_pool_t *dp; 1018842727c2SChris Kirby 10193b2aab18SMatthew Ahrens if (ds == NULL) /* this is the meta-objset */ 10203b2aab18SMatthew Ahrens return; 10211d452cf5Sahrens 10223b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1023fa9e4066Sahrens 1024c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_snap_obj != 0) 10253b2aab18SMatthew Ahrens panic("dirtying snapshot!"); 1026fa9e4066Sahrens 10273b2aab18SMatthew Ahrens dp = ds->ds_dir->dd_pool; 1028ce636f8bSMatthew Ahrens 10293b2aab18SMatthew Ahrens if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) { 10303b2aab18SMatthew Ahrens /* up the hold count until we can be written out */ 10313b2aab18SMatthew Ahrens dmu_buf_add_ref(ds->ds_dbuf, ds); 10323b2aab18SMatthew Ahrens } 10333b2aab18SMatthew Ahrens } 1034fa9e4066Sahrens 10352e2c1355SMatthew Ahrens boolean_t 10362e2c1355SMatthew Ahrens dsl_dataset_is_dirty(dsl_dataset_t *ds) 10372e2c1355SMatthew Ahrens { 10382e2c1355SMatthew Ahrens for (int t = 0; t < TXG_SIZE; t++) { 10392e2c1355SMatthew Ahrens if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets, 10402e2c1355SMatthew Ahrens ds, t)) 10412e2c1355SMatthew Ahrens return (B_TRUE); 10422e2c1355SMatthew Ahrens } 10432e2c1355SMatthew Ahrens return (B_FALSE); 10442e2c1355SMatthew Ahrens } 10452e2c1355SMatthew Ahrens 1046fa9e4066Sahrens static int 10473b2aab18SMatthew Ahrens dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx) 1048fa9e4066Sahrens { 10493b2aab18SMatthew Ahrens uint64_t asize; 1050fa9e4066Sahrens 10513b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 105288b7b0f2SMatthew Ahrens return (0); 1053fa9e4066Sahrens 1054e1930233Sbonwick /* 10553b2aab18SMatthew Ahrens * If there's an fs-only reservation, any blocks that might become 10563b2aab18SMatthew Ahrens * owned by the snapshot dataset must be accommodated by space 10573b2aab18SMatthew Ahrens * outside of the reservation. 1058e1930233Sbonwick */ 10593b2aab18SMatthew Ahrens ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds)); 1060c1379625SJustin T. Gibbs asize = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, ds->ds_reserved); 10613b2aab18SMatthew Ahrens if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) 1062be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 1063e1930233Sbonwick 10643cb34c60Sahrens /* 10653b2aab18SMatthew Ahrens * Propagate any reserved space for this snapshot to other 10663b2aab18SMatthew Ahrens * snapshot checks in this sync group. 10673cb34c60Sahrens */ 10683b2aab18SMatthew Ahrens if (asize > 0) 10693b2aab18SMatthew Ahrens dsl_dir_willuse_space(ds->ds_dir, asize, tx); 10703cb34c60Sahrens 1071e1930233Sbonwick return (0); 1072e1930233Sbonwick } 1073e1930233Sbonwick 10743b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_arg { 10753b2aab18SMatthew Ahrens nvlist_t *ddsa_snaps; 10763b2aab18SMatthew Ahrens nvlist_t *ddsa_props; 10773b2aab18SMatthew Ahrens nvlist_t *ddsa_errors; 1078a2afb611SJerry Jelinek cred_t *ddsa_cr; 10793b2aab18SMatthew Ahrens } dsl_dataset_snapshot_arg_t; 1080842727c2SChris Kirby 10813cb34c60Sahrens int 10823b2aab18SMatthew Ahrens dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname, 1083a2afb611SJerry Jelinek dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr) 10841d452cf5Sahrens { 10853b2aab18SMatthew Ahrens int error; 10863b2aab18SMatthew Ahrens uint64_t value; 1087fa9e4066Sahrens 10883b2aab18SMatthew Ahrens ds->ds_trysnap_txg = tx->tx_txg; 1089745cd3c5Smaybee 10903b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) 1091842727c2SChris Kirby return (0); 1092fa9e4066Sahrens 1093fa9e4066Sahrens /* 10943b2aab18SMatthew Ahrens * We don't allow multiple snapshots of the same txg. If there 10953b2aab18SMatthew Ahrens * is already one, try again. 1096fa9e4066Sahrens */ 1097c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= tx->tx_txg) 1098be6fd75aSMatthew Ahrens return (SET_ERROR(EAGAIN)); 1099fa9e4066Sahrens 1100fa9e4066Sahrens /* 11013b2aab18SMatthew Ahrens * Check for conflicting snapshot name. 1102fa9e4066Sahrens */ 11033b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(ds, snapname, &value); 11043b2aab18SMatthew Ahrens if (error == 0) 1105be6fd75aSMatthew Ahrens return (SET_ERROR(EEXIST)); 11063b2aab18SMatthew Ahrens if (error != ENOENT) 11073b2aab18SMatthew Ahrens return (error); 1108842727c2SChris Kirby 1109ca48f36fSKeith M Wesolowski /* 1110ca48f36fSKeith M Wesolowski * We don't allow taking snapshots of inconsistent datasets, such as 1111ca48f36fSKeith M Wesolowski * those into which we are currently receiving. However, if we are 1112ca48f36fSKeith M Wesolowski * creating this snapshot as part of a receive, this check will be 1113ca48f36fSKeith M Wesolowski * executed atomically with respect to the completion of the receive 1114ca48f36fSKeith M Wesolowski * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this 1115ca48f36fSKeith M Wesolowski * case we ignore this, knowing it will be fixed up for us shortly in 1116ca48f36fSKeith M Wesolowski * dmu_recv_end_sync(). 1117ca48f36fSKeith M Wesolowski */ 1118ca48f36fSKeith M Wesolowski if (!recv && DS_IS_INCONSISTENT(ds)) 1119ca48f36fSKeith M Wesolowski return (SET_ERROR(EBUSY)); 1120ca48f36fSKeith M Wesolowski 1121a2afb611SJerry Jelinek /* 1122a2afb611SJerry Jelinek * Skip the check for temporary snapshots or if we have already checked 1123a2afb611SJerry Jelinek * the counts in dsl_dataset_snapshot_check. This means we really only 1124a2afb611SJerry Jelinek * check the count here when we're receiving a stream. 1125a2afb611SJerry Jelinek */ 1126a2afb611SJerry Jelinek if (cnt != 0 && cr != NULL) { 1127a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1128a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, cr); 1129a2afb611SJerry Jelinek if (error != 0) 1130a2afb611SJerry Jelinek return (error); 1131a2afb611SJerry Jelinek } 1132a2afb611SJerry Jelinek 11333b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_reserve_space(ds, tx); 11343b2aab18SMatthew Ahrens if (error != 0) 11353b2aab18SMatthew Ahrens return (error); 1136842727c2SChris Kirby 11371d452cf5Sahrens return (0); 11381d452cf5Sahrens } 11391d452cf5Sahrens 11403b2aab18SMatthew Ahrens static int 11413b2aab18SMatthew Ahrens dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx) 1142745cd3c5Smaybee { 11433b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 11443b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 11453b2aab18SMatthew Ahrens nvpair_t *pair; 11463b2aab18SMatthew Ahrens int rv = 0; 11473b2aab18SMatthew Ahrens 1148a2afb611SJerry Jelinek /* 1149a2afb611SJerry Jelinek * Pre-compute how many total new snapshots will be created for each 1150a2afb611SJerry Jelinek * level in the tree and below. This is needed for validating the 1151a2afb611SJerry Jelinek * snapshot limit when either taking a recursive snapshot or when 1152a2afb611SJerry Jelinek * taking multiple snapshots. 1153a2afb611SJerry Jelinek * 1154a2afb611SJerry Jelinek * The problem is that the counts are not actually adjusted when 1155a2afb611SJerry Jelinek * we are checking, only when we finally sync. For a single snapshot, 1156a2afb611SJerry Jelinek * this is easy, the count will increase by 1 at each node up the tree, 1157a2afb611SJerry Jelinek * but its more complicated for the recursive/multiple snapshot case. 1158a2afb611SJerry Jelinek * 1159a2afb611SJerry Jelinek * The dsl_fs_ss_limit_check function does recursively check the count 1160a2afb611SJerry Jelinek * at each level up the tree but since it is validating each snapshot 1161a2afb611SJerry Jelinek * independently we need to be sure that we are validating the complete 1162a2afb611SJerry Jelinek * count for the entire set of snapshots. We do this by rolling up the 1163a2afb611SJerry Jelinek * counts for each component of the name into an nvlist and then 1164a2afb611SJerry Jelinek * checking each of those cases with the aggregated count. 1165a2afb611SJerry Jelinek * 1166a2afb611SJerry Jelinek * This approach properly handles not only the recursive snapshot 1167a2afb611SJerry Jelinek * case (where we get all of those on the ddsa_snaps list) but also 1168a2afb611SJerry Jelinek * the sibling case (e.g. snapshot a/b and a/c so that we will also 1169a2afb611SJerry Jelinek * validate the limit on 'a' using a count of 2). 1170a2afb611SJerry Jelinek * 1171a2afb611SJerry Jelinek * We validate the snapshot names in the third loop and only report 1172a2afb611SJerry Jelinek * name errors once. 1173a2afb611SJerry Jelinek */ 1174a2afb611SJerry Jelinek if (dmu_tx_is_syncing(tx)) { 1175a2afb611SJerry Jelinek nvlist_t *cnt_track = NULL; 1176a2afb611SJerry Jelinek cnt_track = fnvlist_alloc(); 1177a2afb611SJerry Jelinek 1178a2afb611SJerry Jelinek /* Rollup aggregated counts into the cnt_track list */ 1179a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 1180a2afb611SJerry Jelinek pair != NULL; 1181a2afb611SJerry Jelinek pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 1182a2afb611SJerry Jelinek char *pdelim; 1183a2afb611SJerry Jelinek uint64_t val; 1184a2afb611SJerry Jelinek char nm[MAXPATHLEN]; 1185a2afb611SJerry Jelinek 1186a2afb611SJerry Jelinek (void) strlcpy(nm, nvpair_name(pair), sizeof (nm)); 1187a2afb611SJerry Jelinek pdelim = strchr(nm, '@'); 1188a2afb611SJerry Jelinek if (pdelim == NULL) 1189a2afb611SJerry Jelinek continue; 1190a2afb611SJerry Jelinek *pdelim = '\0'; 1191a2afb611SJerry Jelinek 1192a2afb611SJerry Jelinek do { 1193a2afb611SJerry Jelinek if (nvlist_lookup_uint64(cnt_track, nm, 1194a2afb611SJerry Jelinek &val) == 0) { 1195a2afb611SJerry Jelinek /* update existing entry */ 1196a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1197a2afb611SJerry Jelinek val + 1); 1198a2afb611SJerry Jelinek } else { 1199a2afb611SJerry Jelinek /* add to list */ 1200a2afb611SJerry Jelinek fnvlist_add_uint64(cnt_track, nm, 1); 1201a2afb611SJerry Jelinek } 1202a2afb611SJerry Jelinek 1203a2afb611SJerry Jelinek pdelim = strrchr(nm, '/'); 1204a2afb611SJerry Jelinek if (pdelim != NULL) 1205a2afb611SJerry Jelinek *pdelim = '\0'; 1206a2afb611SJerry Jelinek } while (pdelim != NULL); 1207a2afb611SJerry Jelinek } 1208a2afb611SJerry Jelinek 1209a2afb611SJerry Jelinek /* Check aggregated counts at each level */ 1210a2afb611SJerry Jelinek for (pair = nvlist_next_nvpair(cnt_track, NULL); 1211a2afb611SJerry Jelinek pair != NULL; pair = nvlist_next_nvpair(cnt_track, pair)) { 1212a2afb611SJerry Jelinek int error = 0; 1213a2afb611SJerry Jelinek char *name; 1214a2afb611SJerry Jelinek uint64_t cnt = 0; 1215a2afb611SJerry Jelinek dsl_dataset_t *ds; 1216a2afb611SJerry Jelinek 1217a2afb611SJerry Jelinek name = nvpair_name(pair); 1218a2afb611SJerry Jelinek cnt = fnvpair_value_uint64(pair); 1219a2afb611SJerry Jelinek ASSERT(cnt > 0); 1220a2afb611SJerry Jelinek 1221a2afb611SJerry Jelinek error = dsl_dataset_hold(dp, name, FTAG, &ds); 1222a2afb611SJerry Jelinek if (error == 0) { 1223a2afb611SJerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, cnt, 1224a2afb611SJerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, 1225a2afb611SJerry Jelinek ddsa->ddsa_cr); 1226a2afb611SJerry Jelinek dsl_dataset_rele(ds, FTAG); 1227a2afb611SJerry Jelinek } 1228a2afb611SJerry Jelinek 1229a2afb611SJerry Jelinek if (error != 0) { 1230a2afb611SJerry Jelinek if (ddsa->ddsa_errors != NULL) 1231a2afb611SJerry Jelinek fnvlist_add_int32(ddsa->ddsa_errors, 1232a2afb611SJerry Jelinek name, error); 1233a2afb611SJerry Jelinek rv = error; 1234a2afb611SJerry Jelinek /* only report one error for this check */ 1235a2afb611SJerry Jelinek break; 1236a2afb611SJerry Jelinek } 1237a2afb611SJerry Jelinek } 1238a2afb611SJerry Jelinek nvlist_free(cnt_track); 1239a2afb611SJerry Jelinek } 1240a2afb611SJerry Jelinek 12413b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 12423b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 12433b2aab18SMatthew Ahrens int error = 0; 12443b2aab18SMatthew Ahrens dsl_dataset_t *ds; 12453b2aab18SMatthew Ahrens char *name, *atp; 12463b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 12473b2aab18SMatthew Ahrens 12483b2aab18SMatthew Ahrens name = nvpair_name(pair); 12493b2aab18SMatthew Ahrens if (strlen(name) >= MAXNAMELEN) 1250be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 12513b2aab18SMatthew Ahrens if (error == 0) { 12523b2aab18SMatthew Ahrens atp = strchr(name, '@'); 12533b2aab18SMatthew Ahrens if (atp == NULL) 1254be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 12553b2aab18SMatthew Ahrens if (error == 0) 12563b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 12573b2aab18SMatthew Ahrens } 12583b2aab18SMatthew Ahrens if (error == 0) 12593b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, dsname, FTAG, &ds); 12603b2aab18SMatthew Ahrens if (error == 0) { 1261a2afb611SJerry Jelinek /* passing 0/NULL skips dsl_fs_ss_limit_check */ 12623b2aab18SMatthew Ahrens error = dsl_dataset_snapshot_check_impl(ds, 1263a2afb611SJerry Jelinek atp + 1, tx, B_FALSE, 0, NULL); 12643b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 12653b2aab18SMatthew Ahrens } 1266745cd3c5Smaybee 12673b2aab18SMatthew Ahrens if (error != 0) { 12683b2aab18SMatthew Ahrens if (ddsa->ddsa_errors != NULL) { 12693b2aab18SMatthew Ahrens fnvlist_add_int32(ddsa->ddsa_errors, 12703b2aab18SMatthew Ahrens name, error); 12713b2aab18SMatthew Ahrens } 12723b2aab18SMatthew Ahrens rv = error; 12733b2aab18SMatthew Ahrens } 12743b2aab18SMatthew Ahrens } 1275a2afb611SJerry Jelinek 12763b2aab18SMatthew Ahrens return (rv); 1277745cd3c5Smaybee } 1278745cd3c5Smaybee 12793b2aab18SMatthew Ahrens void 12803b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname, 12813b2aab18SMatthew Ahrens dmu_tx_t *tx) 1282745cd3c5Smaybee { 12833b2aab18SMatthew Ahrens static zil_header_t zero_zil; 1284745cd3c5Smaybee 12853b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 12863b2aab18SMatthew Ahrens dmu_buf_t *dbuf; 12873b2aab18SMatthew Ahrens dsl_dataset_phys_t *dsphys; 12883b2aab18SMatthew Ahrens uint64_t dsobj, crtxg; 12893b2aab18SMatthew Ahrens objset_t *mos = dp->dp_meta_objset; 12903b2aab18SMatthew Ahrens objset_t *os; 1291745cd3c5Smaybee 12923b2aab18SMatthew Ahrens ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock)); 1293c33e334fSMatthew Ahrens 1294c33e334fSMatthew Ahrens /* 12953b2aab18SMatthew Ahrens * If we are on an old pool, the zil must not be active, in which 12963b2aab18SMatthew Ahrens * case it will be zeroed. Usually zil_suspend() accomplishes this. 1297c33e334fSMatthew Ahrens */ 12983b2aab18SMatthew Ahrens ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP || 12993b2aab18SMatthew Ahrens dmu_objset_from_ds(ds, &os) != 0 || 13003b2aab18SMatthew Ahrens bcmp(&os->os_phys->os_zil_header, &zero_zil, 13013b2aab18SMatthew Ahrens sizeof (zero_zil)) == 0); 1302c33e334fSMatthew Ahrens 1303a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(ds->ds_dir, 1, DD_FIELD_SNAPSHOT_COUNT, tx); 1304cde58dbcSMatthew Ahrens 1305cde58dbcSMatthew Ahrens /* 13063b2aab18SMatthew Ahrens * The origin's ds_creation_txg has to be < TXG_INITIAL 1307088f3894Sahrens */ 1308088f3894Sahrens if (strcmp(snapname, ORIGIN_DIR_NAME) == 0) 1309088f3894Sahrens crtxg = 1; 1310088f3894Sahrens else 1311088f3894Sahrens crtxg = tx->tx_txg; 1312088f3894Sahrens 13131649cd4bStabriz dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0, 13141649cd4bStabriz DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx); 13153b2aab18SMatthew Ahrens VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf)); 1316fa9e4066Sahrens dmu_buf_will_dirty(dbuf, tx); 1317fa9e4066Sahrens dsphys = dbuf->db_data; 1318745cd3c5Smaybee bzero(dsphys, sizeof (dsl_dataset_phys_t)); 13191d452cf5Sahrens dsphys->ds_dir_obj = ds->ds_dir->dd_object; 1320fa9e4066Sahrens dsphys->ds_fsid_guid = unique_create(); 1321fa9e4066Sahrens (void) random_get_pseudo_bytes((void*)&dsphys->ds_guid, 1322fa9e4066Sahrens sizeof (dsphys->ds_guid)); 1323c1379625SJustin T. Gibbs dsphys->ds_prev_snap_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 1324c1379625SJustin T. Gibbs dsphys->ds_prev_snap_txg = dsl_dataset_phys(ds)->ds_prev_snap_txg; 1325fa9e4066Sahrens dsphys->ds_next_snap_obj = ds->ds_object; 1326fa9e4066Sahrens dsphys->ds_num_children = 1; 1327fa9e4066Sahrens dsphys->ds_creation_time = gethrestime_sec(); 1328088f3894Sahrens dsphys->ds_creation_txg = crtxg; 1329c1379625SJustin T. Gibbs dsphys->ds_deadlist_obj = dsl_dataset_phys(ds)->ds_deadlist_obj; 1330c1379625SJustin T. Gibbs dsphys->ds_referenced_bytes = dsl_dataset_phys(ds)->ds_referenced_bytes; 1331c1379625SJustin T. Gibbs dsphys->ds_compressed_bytes = dsl_dataset_phys(ds)->ds_compressed_bytes; 1332c1379625SJustin T. Gibbs dsphys->ds_uncompressed_bytes = 1333c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes; 1334c1379625SJustin T. Gibbs dsphys->ds_flags = dsl_dataset_phys(ds)->ds_flags; 1335c1379625SJustin T. Gibbs dsphys->ds_bp = dsl_dataset_phys(ds)->ds_bp; 1336ea8dc4b6Seschrock dmu_buf_rele(dbuf, FTAG); 1337fa9e4066Sahrens 1338ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1339ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1340ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(dsobj, f, tx); 1341ca0cc391SMatthew Ahrens } 1342b5152584SMatthew Ahrens 1343c1379625SJustin T. Gibbs ASSERT3U(ds->ds_prev != 0, ==, 1344c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj != 0); 13451d452cf5Sahrens if (ds->ds_prev) { 1346088f3894Sahrens uint64_t next_clones_obj = 1347c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_clones_obj; 1348c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1349fa9e4066Sahrens ds->ds_object || 1350c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_num_children > 1); 1351c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj == 1352c1379625SJustin T. Gibbs ds->ds_object) { 13531d452cf5Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1354c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, ==, 1355c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_creation_txg); 1356c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_next_snap_obj = dsobj; 1357088f3894Sahrens } else if (next_clones_obj != 0) { 13583b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(ds->ds_prev, 1359c33e334fSMatthew Ahrens dsphys->ds_next_snap_obj, tx); 13603b2aab18SMatthew Ahrens VERIFY0(zap_add_int(mos, 1361088f3894Sahrens next_clones_obj, dsobj, tx)); 1362fa9e4066Sahrens } 1363fa9e4066Sahrens } 1364fa9e4066Sahrens 1365a9799022Sck /* 1366a9799022Sck * If we have a reference-reservation on this dataset, we will 1367a9799022Sck * need to increase the amount of refreservation being charged 1368a9799022Sck * since our unique space is going to zero. 1369a9799022Sck */ 1370a9799022Sck if (ds->ds_reserved) { 13713f9d6ad7SLin Ling int64_t delta; 13723f9d6ad7SLin Ling ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 1373c1379625SJustin T. Gibbs delta = MIN(dsl_dataset_phys(ds)->ds_unique_bytes, 1374c1379625SJustin T. Gibbs ds->ds_reserved); 137574e7dc98SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, 13763f9d6ad7SLin Ling delta, 0, 0, tx); 1377a9799022Sck } 1378a9799022Sck 1379fa9e4066Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1380c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj = 1381c1379625SJustin T. Gibbs dsl_deadlist_clone(&ds->ds_deadlist, UINT64_MAX, 1382c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, tx); 1383cde58dbcSMatthew Ahrens dsl_deadlist_close(&ds->ds_deadlist); 1384c1379625SJustin T. Gibbs dsl_deadlist_open(&ds->ds_deadlist, mos, 1385c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_deadlist_obj); 1386cde58dbcSMatthew Ahrens dsl_deadlist_add_key(&ds->ds_deadlist, 1387c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg, tx); 1388cde58dbcSMatthew Ahrens 1389c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_prev_snap_txg, <, tx->tx_txg); 1390c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj = dsobj; 1391c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_txg = crtxg; 1392c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes = 0; 1393a9799022Sck if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE) 1394c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_UNIQUE_ACCURATE; 1395fa9e4066Sahrens 1396c1379625SJustin T. Gibbs VERIFY0(zap_add(mos, dsl_dataset_phys(ds)->ds_snapnames_zapobj, 13973b2aab18SMatthew Ahrens snapname, 8, 1, &dsobj, tx)); 1398fa9e4066Sahrens 1399fa9e4066Sahrens if (ds->ds_prev) 14003b2aab18SMatthew Ahrens dsl_dataset_rele(ds->ds_prev, ds); 14013b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1402c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, ds, &ds->ds_prev)); 1403ecd6cf80Smarks 14043f9d6ad7SLin Ling dsl_scan_ds_snapshotted(ds, tx); 1405088f3894Sahrens 140671eb0538SChris Kirby dsl_dir_snap_cmtime_update(ds->ds_dir); 140771eb0538SChris Kirby 14084445fffbSMatthew Ahrens spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, ""); 1409fa9e4066Sahrens } 1410fa9e4066Sahrens 14113b2aab18SMatthew Ahrens static void 14123b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx) 1413fa9e4066Sahrens { 14143b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t *ddsa = arg; 14153b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 14163b2aab18SMatthew Ahrens nvpair_t *pair; 141791ebeef5Sahrens 14183b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL); 14193b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) { 14203b2aab18SMatthew Ahrens dsl_dataset_t *ds; 14213b2aab18SMatthew Ahrens char *name, *atp; 14223b2aab18SMatthew Ahrens char dsname[MAXNAMELEN]; 14233b2aab18SMatthew Ahrens 14243b2aab18SMatthew Ahrens name = nvpair_name(pair); 14253b2aab18SMatthew Ahrens atp = strchr(name, '@'); 14263b2aab18SMatthew Ahrens (void) strlcpy(dsname, name, atp - name + 1); 14273b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds)); 14283b2aab18SMatthew Ahrens 14293b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx); 14303b2aab18SMatthew Ahrens if (ddsa->ddsa_props != NULL) { 14313b2aab18SMatthew Ahrens dsl_props_set_sync_impl(ds->ds_prev, 14323b2aab18SMatthew Ahrens ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx); 14333b2aab18SMatthew Ahrens } 14343b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 14353b2aab18SMatthew Ahrens } 1436fa9e4066Sahrens } 1437fa9e4066Sahrens 14383b2aab18SMatthew Ahrens /* 14393b2aab18SMatthew Ahrens * The snapshots must all be in the same pool. 14403b2aab18SMatthew Ahrens * All-or-nothing: if there are any failures, nothing will be modified. 14413b2aab18SMatthew Ahrens */ 14423b2aab18SMatthew Ahrens int 14433b2aab18SMatthew Ahrens dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors) 144419b94df9SMatthew Ahrens { 14453b2aab18SMatthew Ahrens dsl_dataset_snapshot_arg_t ddsa; 14463b2aab18SMatthew Ahrens nvpair_t *pair; 14473b2aab18SMatthew Ahrens boolean_t needsuspend; 14483b2aab18SMatthew Ahrens int error; 14493b2aab18SMatthew Ahrens spa_t *spa; 14503b2aab18SMatthew Ahrens char *firstname; 14513b2aab18SMatthew Ahrens nvlist_t *suspended = NULL; 145219b94df9SMatthew Ahrens 14533b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, NULL); 14543b2aab18SMatthew Ahrens if (pair == NULL) 14553b2aab18SMatthew Ahrens return (0); 14563b2aab18SMatthew Ahrens firstname = nvpair_name(pair); 14573b2aab18SMatthew Ahrens 14583b2aab18SMatthew Ahrens error = spa_open(firstname, &spa, FTAG); 14593b2aab18SMatthew Ahrens if (error != 0) 14603b2aab18SMatthew Ahrens return (error); 14613b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 14623b2aab18SMatthew Ahrens spa_close(spa, FTAG); 14633b2aab18SMatthew Ahrens 14643b2aab18SMatthew Ahrens if (needsuspend) { 14653b2aab18SMatthew Ahrens suspended = fnvlist_alloc(); 14663b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; 14673b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(snaps, pair)) { 14683b2aab18SMatthew Ahrens char fsname[MAXNAMELEN]; 14693b2aab18SMatthew Ahrens char *snapname = nvpair_name(pair); 14703b2aab18SMatthew Ahrens char *atp; 14713b2aab18SMatthew Ahrens void *cookie; 14723b2aab18SMatthew Ahrens 14733b2aab18SMatthew Ahrens atp = strchr(snapname, '@'); 14743b2aab18SMatthew Ahrens if (atp == NULL) { 1475be6fd75aSMatthew Ahrens error = SET_ERROR(EINVAL); 14763b2aab18SMatthew Ahrens break; 14773b2aab18SMatthew Ahrens } 14783b2aab18SMatthew Ahrens (void) strlcpy(fsname, snapname, atp - snapname + 1); 14793b2aab18SMatthew Ahrens 14803b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 14813b2aab18SMatthew Ahrens if (error != 0) 14823b2aab18SMatthew Ahrens break; 14833b2aab18SMatthew Ahrens fnvlist_add_uint64(suspended, fsname, 14843b2aab18SMatthew Ahrens (uintptr_t)cookie); 14853b2aab18SMatthew Ahrens } 14863b2aab18SMatthew Ahrens } 14873b2aab18SMatthew Ahrens 14883b2aab18SMatthew Ahrens ddsa.ddsa_snaps = snaps; 14893b2aab18SMatthew Ahrens ddsa.ddsa_props = props; 14903b2aab18SMatthew Ahrens ddsa.ddsa_errors = errors; 1491a2afb611SJerry Jelinek ddsa.ddsa_cr = CRED(); 14923b2aab18SMatthew Ahrens 14933b2aab18SMatthew Ahrens if (error == 0) { 14943b2aab18SMatthew Ahrens error = dsl_sync_task(firstname, dsl_dataset_snapshot_check, 14953b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync, &ddsa, 14967d46dc6cSMatthew Ahrens fnvlist_num_pairs(snaps) * 3, ZFS_SPACE_CHECK_NORMAL); 14973b2aab18SMatthew Ahrens } 14983b2aab18SMatthew Ahrens 14993b2aab18SMatthew Ahrens if (suspended != NULL) { 15003b2aab18SMatthew Ahrens for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL; 15013b2aab18SMatthew Ahrens pair = nvlist_next_nvpair(suspended, pair)) { 15023b2aab18SMatthew Ahrens zil_resume((void *)(uintptr_t) 15033b2aab18SMatthew Ahrens fnvpair_value_uint64(pair)); 15043b2aab18SMatthew Ahrens } 15053b2aab18SMatthew Ahrens fnvlist_free(suspended); 15063b2aab18SMatthew Ahrens } 15073b2aab18SMatthew Ahrens 15083b2aab18SMatthew Ahrens return (error); 15093b2aab18SMatthew Ahrens } 15103b2aab18SMatthew Ahrens 15113b2aab18SMatthew Ahrens typedef struct dsl_dataset_snapshot_tmp_arg { 15123b2aab18SMatthew Ahrens const char *ddsta_fsname; 15133b2aab18SMatthew Ahrens const char *ddsta_snapname; 15143b2aab18SMatthew Ahrens minor_t ddsta_cleanup_minor; 15153b2aab18SMatthew Ahrens const char *ddsta_htag; 15163b2aab18SMatthew Ahrens } dsl_dataset_snapshot_tmp_arg_t; 15173b2aab18SMatthew Ahrens 15183b2aab18SMatthew Ahrens static int 15193b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx) 15203b2aab18SMatthew Ahrens { 15213b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15223b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15233b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15243b2aab18SMatthew Ahrens int error; 15253b2aab18SMatthew Ahrens 15263b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds); 15273b2aab18SMatthew Ahrens if (error != 0) 15283b2aab18SMatthew Ahrens return (error); 15293b2aab18SMatthew Ahrens 1530a2afb611SJerry Jelinek /* NULL cred means no limit check for tmp snapshot */ 1531ca48f36fSKeith M Wesolowski error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname, 1532a2afb611SJerry Jelinek tx, B_FALSE, 0, NULL); 15333b2aab18SMatthew Ahrens if (error != 0) { 15343b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15353b2aab18SMatthew Ahrens return (error); 15363b2aab18SMatthew Ahrens } 15373b2aab18SMatthew Ahrens 15383b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) { 15393b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 1540be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 15413b2aab18SMatthew Ahrens } 15423b2aab18SMatthew Ahrens error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag, 15433b2aab18SMatthew Ahrens B_TRUE, tx); 15443b2aab18SMatthew Ahrens if (error != 0) { 15453b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15463b2aab18SMatthew Ahrens return (error); 15473b2aab18SMatthew Ahrens } 15483b2aab18SMatthew Ahrens 15493b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15503b2aab18SMatthew Ahrens return (0); 15513b2aab18SMatthew Ahrens } 15523b2aab18SMatthew Ahrens 15533b2aab18SMatthew Ahrens static void 15543b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx) 15553b2aab18SMatthew Ahrens { 15563b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t *ddsta = arg; 15573b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 15583b2aab18SMatthew Ahrens dsl_dataset_t *ds; 15593b2aab18SMatthew Ahrens 15603b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds)); 15613b2aab18SMatthew Ahrens 15623b2aab18SMatthew Ahrens dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx); 15633b2aab18SMatthew Ahrens dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag, 15643b2aab18SMatthew Ahrens ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx); 15653b2aab18SMatthew Ahrens dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx); 15663b2aab18SMatthew Ahrens 15673b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 15683b2aab18SMatthew Ahrens } 15693b2aab18SMatthew Ahrens 15703b2aab18SMatthew Ahrens int 15713b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname, 15723b2aab18SMatthew Ahrens minor_t cleanup_minor, const char *htag) 15733b2aab18SMatthew Ahrens { 15743b2aab18SMatthew Ahrens dsl_dataset_snapshot_tmp_arg_t ddsta; 15753b2aab18SMatthew Ahrens int error; 15763b2aab18SMatthew Ahrens spa_t *spa; 15773b2aab18SMatthew Ahrens boolean_t needsuspend; 15783b2aab18SMatthew Ahrens void *cookie; 15793b2aab18SMatthew Ahrens 15803b2aab18SMatthew Ahrens ddsta.ddsta_fsname = fsname; 15813b2aab18SMatthew Ahrens ddsta.ddsta_snapname = snapname; 15823b2aab18SMatthew Ahrens ddsta.ddsta_cleanup_minor = cleanup_minor; 15833b2aab18SMatthew Ahrens ddsta.ddsta_htag = htag; 15843b2aab18SMatthew Ahrens 15853b2aab18SMatthew Ahrens error = spa_open(fsname, &spa, FTAG); 15863b2aab18SMatthew Ahrens if (error != 0) 15873b2aab18SMatthew Ahrens return (error); 15883b2aab18SMatthew Ahrens needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP); 15893b2aab18SMatthew Ahrens spa_close(spa, FTAG); 15903b2aab18SMatthew Ahrens 15913b2aab18SMatthew Ahrens if (needsuspend) { 15923b2aab18SMatthew Ahrens error = zil_suspend(fsname, &cookie); 15933b2aab18SMatthew Ahrens if (error != 0) 15943b2aab18SMatthew Ahrens return (error); 15953b2aab18SMatthew Ahrens } 15963b2aab18SMatthew Ahrens 15973b2aab18SMatthew Ahrens error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check, 15987d46dc6cSMatthew Ahrens dsl_dataset_snapshot_tmp_sync, &ddsta, 3, ZFS_SPACE_CHECK_RESERVED); 15993b2aab18SMatthew Ahrens 16003b2aab18SMatthew Ahrens if (needsuspend) 16013b2aab18SMatthew Ahrens zil_resume(cookie); 16023b2aab18SMatthew Ahrens return (error); 16033b2aab18SMatthew Ahrens } 16043b2aab18SMatthew Ahrens 16053b2aab18SMatthew Ahrens 16063b2aab18SMatthew Ahrens void 16073b2aab18SMatthew Ahrens dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx) 16083b2aab18SMatthew Ahrens { 16093b2aab18SMatthew Ahrens ASSERT(dmu_tx_is_syncing(tx)); 16103b2aab18SMatthew Ahrens ASSERT(ds->ds_objset != NULL); 1611c1379625SJustin T. Gibbs ASSERT(dsl_dataset_phys(ds)->ds_next_snap_obj == 0); 16123b2aab18SMatthew Ahrens 16133b2aab18SMatthew Ahrens /* 16143b2aab18SMatthew Ahrens * in case we had to change ds_fsid_guid when we opened it, 16153b2aab18SMatthew Ahrens * sync it out now. 16163b2aab18SMatthew Ahrens */ 16173b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 1618c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_fsid_guid = ds->ds_fsid_guid; 16193b2aab18SMatthew Ahrens 16209c3fd121SMatthew Ahrens if (ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] != 0) { 16219c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16229c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_OBJECT, 8, 1, 16239c3fd121SMatthew Ahrens &ds->ds_resume_object[tx->tx_txg & TXG_MASK], tx)); 16249c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16259c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_OFFSET, 8, 1, 16269c3fd121SMatthew Ahrens &ds->ds_resume_offset[tx->tx_txg & TXG_MASK], tx)); 16279c3fd121SMatthew Ahrens VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, 16289c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_BYTES, 8, 1, 16299c3fd121SMatthew Ahrens &ds->ds_resume_bytes[tx->tx_txg & TXG_MASK], tx)); 16309c3fd121SMatthew Ahrens ds->ds_resume_object[tx->tx_txg & TXG_MASK] = 0; 16319c3fd121SMatthew Ahrens ds->ds_resume_offset[tx->tx_txg & TXG_MASK] = 0; 16329c3fd121SMatthew Ahrens ds->ds_resume_bytes[tx->tx_txg & TXG_MASK] = 0; 16339c3fd121SMatthew Ahrens } 16349c3fd121SMatthew Ahrens 16353b2aab18SMatthew Ahrens dmu_objset_sync(ds->ds_objset, zio, tx); 1636b5152584SMatthew Ahrens 1637ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 1638ca0cc391SMatthew Ahrens if (ds->ds_feature_activation_needed[f]) { 1639ca0cc391SMatthew Ahrens if (ds->ds_feature_inuse[f]) 1640ca0cc391SMatthew Ahrens continue; 1641ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(ds->ds_object, f, tx); 1642ca0cc391SMatthew Ahrens ds->ds_feature_inuse[f] = B_TRUE; 1643ca0cc391SMatthew Ahrens } 1644b5152584SMatthew Ahrens } 16453b2aab18SMatthew Ahrens } 16463b2aab18SMatthew Ahrens 16473b2aab18SMatthew Ahrens static void 16483b2aab18SMatthew Ahrens get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv) 16493b2aab18SMatthew Ahrens { 16503b2aab18SMatthew Ahrens uint64_t count = 0; 16513b2aab18SMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 16523b2aab18SMatthew Ahrens zap_cursor_t zc; 16533b2aab18SMatthew Ahrens zap_attribute_t za; 16543b2aab18SMatthew Ahrens nvlist_t *propval = fnvlist_alloc(); 16553b2aab18SMatthew Ahrens nvlist_t *val = fnvlist_alloc(); 16563b2aab18SMatthew Ahrens 16573b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); 165819b94df9SMatthew Ahrens 165919b94df9SMatthew Ahrens /* 16603b2aab18SMatthew Ahrens * There may be missing entries in ds_next_clones_obj 166119b94df9SMatthew Ahrens * due to a bug in a previous version of the code. 166219b94df9SMatthew Ahrens * Only trust it if it has the right number of entries. 166319b94df9SMatthew Ahrens */ 1664c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj != 0) { 1665c1379625SJustin T. Gibbs VERIFY0(zap_count(mos, dsl_dataset_phys(ds)->ds_next_clones_obj, 166619b94df9SMatthew Ahrens &count)); 166719b94df9SMatthew Ahrens } 1668c1379625SJustin T. Gibbs if (count != dsl_dataset_phys(ds)->ds_num_children - 1) 166919b94df9SMatthew Ahrens goto fail; 1670c1379625SJustin T. Gibbs for (zap_cursor_init(&zc, mos, 1671c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 167219b94df9SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 167319b94df9SMatthew Ahrens zap_cursor_advance(&zc)) { 167419b94df9SMatthew Ahrens dsl_dataset_t *clone; 167519b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 16763b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 16773b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &clone)); 167819b94df9SMatthew Ahrens dsl_dir_name(clone->ds_dir, buf); 16793b2aab18SMatthew Ahrens fnvlist_add_boolean(val, buf); 168019b94df9SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 168119b94df9SMatthew Ahrens } 168219b94df9SMatthew Ahrens zap_cursor_fini(&zc); 16833b2aab18SMatthew Ahrens fnvlist_add_nvlist(propval, ZPROP_VALUE, val); 16843b2aab18SMatthew Ahrens fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval); 168519b94df9SMatthew Ahrens fail: 168619b94df9SMatthew Ahrens nvlist_free(val); 168719b94df9SMatthew Ahrens nvlist_free(propval); 168819b94df9SMatthew Ahrens } 168919b94df9SMatthew Ahrens 16909c3fd121SMatthew Ahrens static void 16919c3fd121SMatthew Ahrens get_receive_resume_stats(dsl_dataset_t *ds, nvlist_t *nv) 16929c3fd121SMatthew Ahrens { 16939c3fd121SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 16949c3fd121SMatthew Ahrens 16959c3fd121SMatthew Ahrens if (dsl_dataset_has_resume_receive_state(ds)) { 16969c3fd121SMatthew Ahrens char *str; 16979c3fd121SMatthew Ahrens void *packed; 16989c3fd121SMatthew Ahrens uint8_t *compressed; 16999c3fd121SMatthew Ahrens uint64_t val; 17009c3fd121SMatthew Ahrens nvlist_t *token_nv = fnvlist_alloc(); 17019c3fd121SMatthew Ahrens size_t packed_size, compressed_size; 17029c3fd121SMatthew Ahrens 17039c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17049c3fd121SMatthew Ahrens DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val) == 0) { 17059c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "fromguid", val); 17069c3fd121SMatthew Ahrens } 17079c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17089c3fd121SMatthew Ahrens DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val) == 0) { 17099c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "object", val); 17109c3fd121SMatthew Ahrens } 17119c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17129c3fd121SMatthew Ahrens DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val) == 0) { 17139c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "offset", val); 17149c3fd121SMatthew Ahrens } 17159c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17169c3fd121SMatthew Ahrens DS_FIELD_RESUME_BYTES, sizeof (val), 1, &val) == 0) { 17179c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "bytes", val); 17189c3fd121SMatthew Ahrens } 17199c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17209c3fd121SMatthew Ahrens DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val) == 0) { 17219c3fd121SMatthew Ahrens fnvlist_add_uint64(token_nv, "toguid", val); 17229c3fd121SMatthew Ahrens } 17239c3fd121SMatthew Ahrens char buf[256]; 17249c3fd121SMatthew Ahrens if (zap_lookup(dp->dp_meta_objset, ds->ds_object, 17259c3fd121SMatthew Ahrens DS_FIELD_RESUME_TONAME, 1, sizeof (buf), buf) == 0) { 17269c3fd121SMatthew Ahrens fnvlist_add_string(token_nv, "toname", buf); 17279c3fd121SMatthew Ahrens } 17289c3fd121SMatthew Ahrens if (zap_contains(dp->dp_meta_objset, ds->ds_object, 17299c3fd121SMatthew Ahrens DS_FIELD_RESUME_EMBEDOK) == 0) { 17309c3fd121SMatthew Ahrens fnvlist_add_boolean(token_nv, "embedok"); 17319c3fd121SMatthew Ahrens } 17329c3fd121SMatthew Ahrens packed = fnvlist_pack(token_nv, &packed_size); 17339c3fd121SMatthew Ahrens fnvlist_free(token_nv); 17349c3fd121SMatthew Ahrens compressed = kmem_alloc(packed_size, KM_SLEEP); 17359c3fd121SMatthew Ahrens 17369c3fd121SMatthew Ahrens compressed_size = gzip_compress(packed, compressed, 17379c3fd121SMatthew Ahrens packed_size, packed_size, 6); 17389c3fd121SMatthew Ahrens 17399c3fd121SMatthew Ahrens zio_cksum_t cksum; 17409c3fd121SMatthew Ahrens fletcher_4_native(compressed, compressed_size, NULL, &cksum); 17419c3fd121SMatthew Ahrens 17429c3fd121SMatthew Ahrens str = kmem_alloc(compressed_size * 2 + 1, KM_SLEEP); 17439c3fd121SMatthew Ahrens for (int i = 0; i < compressed_size; i++) { 17449c3fd121SMatthew Ahrens (void) sprintf(str + i * 2, "%02x", compressed[i]); 17459c3fd121SMatthew Ahrens } 17469c3fd121SMatthew Ahrens str[compressed_size * 2] = '\0'; 17479c3fd121SMatthew Ahrens char *propval = kmem_asprintf("%u-%llx-%llx-%s", 17489c3fd121SMatthew Ahrens ZFS_SEND_RESUME_TOKEN_VERSION, 17499c3fd121SMatthew Ahrens (longlong_t)cksum.zc_word[0], 17509c3fd121SMatthew Ahrens (longlong_t)packed_size, str); 17519c3fd121SMatthew Ahrens dsl_prop_nvlist_add_string(nv, 17529c3fd121SMatthew Ahrens ZFS_PROP_RECEIVE_RESUME_TOKEN, propval); 17539c3fd121SMatthew Ahrens kmem_free(packed, packed_size); 17549c3fd121SMatthew Ahrens kmem_free(str, compressed_size * 2 + 1); 17559c3fd121SMatthew Ahrens kmem_free(compressed, packed_size); 17569c3fd121SMatthew Ahrens strfree(propval); 17579c3fd121SMatthew Ahrens } 17589c3fd121SMatthew Ahrens } 17599c3fd121SMatthew Ahrens 1760fa9e4066Sahrens void 1761a2eea2e1Sahrens dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv) 1762fa9e4066Sahrens { 17633b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 1764187d6ac0SMatt Ahrens uint64_t refd, avail, uobjs, aobjs, ratio; 1765a9799022Sck 17663b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 17673b2aab18SMatthew Ahrens 1768c1379625SJustin T. Gibbs ratio = dsl_dataset_phys(ds)->ds_compressed_bytes == 0 ? 100 : 1769c1379625SJustin T. Gibbs (dsl_dataset_phys(ds)->ds_uncompressed_bytes * 100 / 1770c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_compressed_bytes); 17714445fffbSMatthew Ahrens 17724445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio); 177377372cb0SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED, 1774c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_uncompressed_bytes); 17754445fffbSMatthew Ahrens 1776bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 17774445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio); 17784445fffbSMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, 1779c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 17804445fffbSMatthew Ahrens get_clones_stat(ds, nv); 17814445fffbSMatthew Ahrens } else { 1782b461c746SMatthew Ahrens if (ds->ds_prev != NULL && ds->ds_prev != dp->dp_origin_snap) { 1783b461c746SMatthew Ahrens char buf[MAXNAMELEN]; 1784b461c746SMatthew Ahrens dsl_dataset_name(ds->ds_prev, buf); 1785b461c746SMatthew Ahrens dsl_prop_nvlist_add_string(nv, ZFS_PROP_PREV_SNAP, buf); 1786b461c746SMatthew Ahrens } 1787b461c746SMatthew Ahrens 17884445fffbSMatthew Ahrens dsl_dir_stats(ds->ds_dir, nv); 17894445fffbSMatthew Ahrens } 1790fa9e4066Sahrens 1791a9799022Sck dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs); 1792a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail); 1793a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd); 1794a9799022Sck 1795a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION, 1796c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_time); 1797a2eea2e1Sahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG, 1798c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_creation_txg); 1799a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA, 1800a9799022Sck ds->ds_quota); 1801a9799022Sck dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION, 1802a9799022Sck ds->ds_reserved); 1803c5904d13Seschrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID, 1804c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_guid); 18051d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE, 1806c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 18071d713200SEric Schrock dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID, 18081d713200SEric Schrock ds->ds_object); 180992241e0bSTom Erickson dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS, 181092241e0bSTom Erickson ds->ds_userrefs); 1811842727c2SChris Kirby dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY, 1812842727c2SChris Kirby DS_IS_DEFER_DESTROY(ds) ? 1 : 0); 1813fa9e4066Sahrens 1814c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj != 0) { 181519b94df9SMatthew Ahrens uint64_t written, comp, uncomp; 181619b94df9SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 181719b94df9SMatthew Ahrens dsl_dataset_t *prev; 181819b94df9SMatthew Ahrens 181919b94df9SMatthew Ahrens int err = dsl_dataset_hold_obj(dp, 1820c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_prev_snap_obj, FTAG, &prev); 182119b94df9SMatthew Ahrens if (err == 0) { 182219b94df9SMatthew Ahrens err = dsl_dataset_space_written(prev, ds, &written, 182319b94df9SMatthew Ahrens &comp, &uncomp); 182419b94df9SMatthew Ahrens dsl_dataset_rele(prev, FTAG); 182519b94df9SMatthew Ahrens if (err == 0) { 182619b94df9SMatthew Ahrens dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN, 182719b94df9SMatthew Ahrens written); 182819b94df9SMatthew Ahrens } 182919b94df9SMatthew Ahrens } 183019b94df9SMatthew Ahrens } 18319c3fd121SMatthew Ahrens 18329c3fd121SMatthew Ahrens if (!dsl_dataset_is_snapshot(ds)) { 18339c3fd121SMatthew Ahrens /* 18349c3fd121SMatthew Ahrens * A failed "newfs" (e.g. full) resumable receive leaves 18359c3fd121SMatthew Ahrens * the stats set on this dataset. Check here for the prop. 18369c3fd121SMatthew Ahrens */ 18379c3fd121SMatthew Ahrens get_receive_resume_stats(ds, nv); 18389c3fd121SMatthew Ahrens 18399c3fd121SMatthew Ahrens /* 18409c3fd121SMatthew Ahrens * A failed incremental resumable receive leaves the 18419c3fd121SMatthew Ahrens * stats set on our child named "%recv". Check the child 18429c3fd121SMatthew Ahrens * for the prop. 18439c3fd121SMatthew Ahrens */ 18449c3fd121SMatthew Ahrens char recvname[ZFS_MAXNAMELEN]; 18459c3fd121SMatthew Ahrens dsl_dataset_t *recv_ds; 18469c3fd121SMatthew Ahrens dsl_dataset_name(ds, recvname); 18479c3fd121SMatthew Ahrens (void) strcat(recvname, "/"); 18489c3fd121SMatthew Ahrens (void) strcat(recvname, recv_clone_name); 18499c3fd121SMatthew Ahrens if (dsl_dataset_hold(dp, recvname, FTAG, &recv_ds) == 0) { 18509c3fd121SMatthew Ahrens get_receive_resume_stats(recv_ds, nv); 18519c3fd121SMatthew Ahrens dsl_dataset_rele(recv_ds, FTAG); 18529c3fd121SMatthew Ahrens } 18539c3fd121SMatthew Ahrens } 1854fa9e4066Sahrens } 1855fa9e4066Sahrens 1856a2eea2e1Sahrens void 1857a2eea2e1Sahrens dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat) 1858a2eea2e1Sahrens { 18593b2aab18SMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool; 18603b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 18613b2aab18SMatthew Ahrens 1862c1379625SJustin T. Gibbs stat->dds_creation_txg = dsl_dataset_phys(ds)->ds_creation_txg; 1863c1379625SJustin T. Gibbs stat->dds_inconsistent = 1864c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT; 1865c1379625SJustin T. Gibbs stat->dds_guid = dsl_dataset_phys(ds)->ds_guid; 18664445fffbSMatthew Ahrens stat->dds_origin[0] = '\0'; 1867bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 1868a2eea2e1Sahrens stat->dds_is_snapshot = B_TRUE; 1869c1379625SJustin T. Gibbs stat->dds_num_clones = 1870c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_num_children - 1; 1871ebedde84SEric Taylor } else { 1872ebedde84SEric Taylor stat->dds_is_snapshot = B_FALSE; 1873ebedde84SEric Taylor stat->dds_num_clones = 0; 1874a2eea2e1Sahrens 18754445fffbSMatthew Ahrens if (dsl_dir_is_clone(ds->ds_dir)) { 18764445fffbSMatthew Ahrens dsl_dataset_t *ods; 1877a2eea2e1Sahrens 18783b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 1879c1379625SJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_origin_obj, 1880c1379625SJustin T. Gibbs FTAG, &ods)); 18814445fffbSMatthew Ahrens dsl_dataset_name(ods, stat->dds_origin); 18823b2aab18SMatthew Ahrens dsl_dataset_rele(ods, FTAG); 18834445fffbSMatthew Ahrens } 1884a2eea2e1Sahrens } 1885a2eea2e1Sahrens } 1886a2eea2e1Sahrens 1887a2eea2e1Sahrens uint64_t 1888a2eea2e1Sahrens dsl_dataset_fsid_guid(dsl_dataset_t *ds) 1889a2eea2e1Sahrens { 189091ebeef5Sahrens return (ds->ds_fsid_guid); 1891a2eea2e1Sahrens } 1892a2eea2e1Sahrens 1893a2eea2e1Sahrens void 1894a2eea2e1Sahrens dsl_dataset_space(dsl_dataset_t *ds, 1895a2eea2e1Sahrens uint64_t *refdbytesp, uint64_t *availbytesp, 1896a2eea2e1Sahrens uint64_t *usedobjsp, uint64_t *availobjsp) 1897fa9e4066Sahrens { 1898c1379625SJustin T. Gibbs *refdbytesp = dsl_dataset_phys(ds)->ds_referenced_bytes; 1899a2eea2e1Sahrens *availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE); 1900c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) 1901c1379625SJustin T. Gibbs *availbytesp += 1902c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes; 1903a9799022Sck if (ds->ds_quota != 0) { 1904a9799022Sck /* 1905a9799022Sck * Adjust available bytes according to refquota 1906a9799022Sck */ 1907a9799022Sck if (*refdbytesp < ds->ds_quota) 1908a9799022Sck *availbytesp = MIN(*availbytesp, 1909a9799022Sck ds->ds_quota - *refdbytesp); 1910a9799022Sck else 1911a9799022Sck *availbytesp = 0; 1912a9799022Sck } 1913c1379625SJustin T. Gibbs *usedobjsp = BP_GET_FILL(&dsl_dataset_phys(ds)->ds_bp); 1914a2eea2e1Sahrens *availobjsp = DN_MAX_OBJECT - *usedobjsp; 1915fa9e4066Sahrens } 1916fa9e4066Sahrens 1917f18faf3fSek boolean_t 191834f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap) 1919f18faf3fSek { 1920f18faf3fSek dsl_pool_t *dp = ds->ds_dir->dd_pool; 1921f18faf3fSek 19223b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 192334f2f8cfSMatthew Ahrens if (snap == NULL) 1924f18faf3fSek return (B_FALSE); 1925c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_bp.blk_birth > 1926c1379625SJustin T. Gibbs dsl_dataset_phys(snap)->ds_creation_txg) { 192734f2f8cfSMatthew Ahrens objset_t *os, *os_snap; 19286e0cbcaaSMatthew Ahrens /* 19296e0cbcaaSMatthew Ahrens * It may be that only the ZIL differs, because it was 19306e0cbcaaSMatthew Ahrens * reset in the head. Don't count that as being 19316e0cbcaaSMatthew Ahrens * modified. 19326e0cbcaaSMatthew Ahrens */ 19336e0cbcaaSMatthew Ahrens if (dmu_objset_from_ds(ds, &os) != 0) 19346e0cbcaaSMatthew Ahrens return (B_TRUE); 193534f2f8cfSMatthew Ahrens if (dmu_objset_from_ds(snap, &os_snap) != 0) 19366e0cbcaaSMatthew Ahrens return (B_TRUE); 19376e0cbcaaSMatthew Ahrens return (bcmp(&os->os_phys->os_meta_dnode, 193834f2f8cfSMatthew Ahrens &os_snap->os_phys->os_meta_dnode, 19396e0cbcaaSMatthew Ahrens sizeof (os->os_phys->os_meta_dnode)) != 0); 19406e0cbcaaSMatthew Ahrens } 1941f18faf3fSek return (B_FALSE); 1942f18faf3fSek } 1943f18faf3fSek 19443b2aab18SMatthew Ahrens typedef struct dsl_dataset_rename_snapshot_arg { 19453b2aab18SMatthew Ahrens const char *ddrsa_fsname; 19463b2aab18SMatthew Ahrens const char *ddrsa_oldsnapname; 19473b2aab18SMatthew Ahrens const char *ddrsa_newsnapname; 19483b2aab18SMatthew Ahrens boolean_t ddrsa_recursive; 19493b2aab18SMatthew Ahrens dmu_tx_t *ddrsa_tx; 19503b2aab18SMatthew Ahrens } dsl_dataset_rename_snapshot_arg_t; 19513b2aab18SMatthew Ahrens 19521d452cf5Sahrens /* ARGSUSED */ 1953fa9e4066Sahrens static int 19543b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp, 19553b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 1956fa9e4066Sahrens { 19573b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 19583b2aab18SMatthew Ahrens int error; 1959fa9e4066Sahrens uint64_t val; 1960fa9e4066Sahrens 19613b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 19623b2aab18SMatthew Ahrens if (error != 0) { 19633b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 19643b2aab18SMatthew Ahrens return (error == ENOENT ? 0 : error); 19653b2aab18SMatthew Ahrens } 19661d452cf5Sahrens 19673b2aab18SMatthew Ahrens /* new name should not exist */ 19683b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val); 19693b2aab18SMatthew Ahrens if (error == 0) 1970be6fd75aSMatthew Ahrens error = SET_ERROR(EEXIST); 19713b2aab18SMatthew Ahrens else if (error == ENOENT) 19723b2aab18SMatthew Ahrens error = 0; 1973cdf5b4caSmmusante 1974cdf5b4caSmmusante /* dataset name + 1 for the "@" + the new snapshot name must fit */ 19753b2aab18SMatthew Ahrens if (dsl_dir_namelen(hds->ds_dir) + 1 + 19763b2aab18SMatthew Ahrens strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN) 1977be6fd75aSMatthew Ahrens error = SET_ERROR(ENAMETOOLONG); 1978cdf5b4caSmmusante 19793b2aab18SMatthew Ahrens return (error); 19801d452cf5Sahrens } 1981fa9e4066Sahrens 19823b2aab18SMatthew Ahrens static int 19833b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx) 19841d452cf5Sahrens { 19853b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 19863b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 19871d452cf5Sahrens dsl_dataset_t *hds; 19883b2aab18SMatthew Ahrens int error; 1989fa9e4066Sahrens 19903b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds); 19913b2aab18SMatthew Ahrens if (error != 0) 19923b2aab18SMatthew Ahrens return (error); 1993fa9e4066Sahrens 19943b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 19953b2aab18SMatthew Ahrens error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 19963b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_check_impl, ddrsa, 19973b2aab18SMatthew Ahrens DS_FIND_CHILDREN); 19983b2aab18SMatthew Ahrens } else { 19993b2aab18SMatthew Ahrens error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa); 20003b2aab18SMatthew Ahrens } 2001745cd3c5Smaybee dsl_dataset_rele(hds, FTAG); 20023b2aab18SMatthew Ahrens return (error); 2003fa9e4066Sahrens } 2004fa9e4066Sahrens 2005cdf5b4caSmmusante static int 20063b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp, 20073b2aab18SMatthew Ahrens dsl_dataset_t *hds, void *arg) 2008cdf5b4caSmmusante { 20093b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 20103b2aab18SMatthew Ahrens dsl_dataset_t *ds; 20113b2aab18SMatthew Ahrens uint64_t val; 20123b2aab18SMatthew Ahrens dmu_tx_t *tx = ddrsa->ddrsa_tx; 20133b2aab18SMatthew Ahrens int error; 2014ecd6cf80Smarks 20153b2aab18SMatthew Ahrens error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val); 20163b2aab18SMatthew Ahrens ASSERT(error == 0 || error == ENOENT); 20173b2aab18SMatthew Ahrens if (error == ENOENT) { 20183b2aab18SMatthew Ahrens /* ignore nonexistent snapshots */ 20193b2aab18SMatthew Ahrens return (0); 2020ecd6cf80Smarks } 2021ecd6cf80Smarks 20223b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds)); 20233b2aab18SMatthew Ahrens 20243b2aab18SMatthew Ahrens /* log before we change the name */ 20253b2aab18SMatthew Ahrens spa_history_log_internal_ds(ds, "rename", tx, 20263b2aab18SMatthew Ahrens "-> @%s", ddrsa->ddrsa_newsnapname); 2027cdf5b4caSmmusante 2028a2afb611SJerry Jelinek VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx, 2029a2afb611SJerry Jelinek B_FALSE)); 20303b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 20313b2aab18SMatthew Ahrens (void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname); 20323b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 2033c1379625SJustin T. Gibbs VERIFY0(zap_add(dp->dp_meta_objset, 2034c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, 20353b2aab18SMatthew Ahrens ds->ds_snapname, 8, 1, &ds->ds_object, tx)); 2036cdf5b4caSmmusante 20373b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2038cdf5b4caSmmusante return (0); 2039cdf5b4caSmmusante } 2040cdf5b4caSmmusante 20413b2aab18SMatthew Ahrens static void 20423b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx) 2043cdf5b4caSmmusante { 20443b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t *ddrsa = arg; 20453b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 20463b2aab18SMatthew Ahrens dsl_dataset_t *hds; 2047cdf5b4caSmmusante 20483b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds)); 20493b2aab18SMatthew Ahrens ddrsa->ddrsa_tx = tx; 20503b2aab18SMatthew Ahrens if (ddrsa->ddrsa_recursive) { 20513b2aab18SMatthew Ahrens VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object, 20523b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_sync_impl, ddrsa, 20533b2aab18SMatthew Ahrens DS_FIND_CHILDREN)); 20543b2aab18SMatthew Ahrens } else { 20553b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa)); 2056cdf5b4caSmmusante } 20573b2aab18SMatthew Ahrens dsl_dataset_rele(hds, FTAG); 2058cdf5b4caSmmusante } 2059cdf5b4caSmmusante 20603b2aab18SMatthew Ahrens int 20613b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot(const char *fsname, 20623b2aab18SMatthew Ahrens const char *oldsnapname, const char *newsnapname, boolean_t recursive) 20633a5a36beSmmusante { 20643b2aab18SMatthew Ahrens dsl_dataset_rename_snapshot_arg_t ddrsa; 20653a5a36beSmmusante 20663b2aab18SMatthew Ahrens ddrsa.ddrsa_fsname = fsname; 20673b2aab18SMatthew Ahrens ddrsa.ddrsa_oldsnapname = oldsnapname; 20683b2aab18SMatthew Ahrens ddrsa.ddrsa_newsnapname = newsnapname; 20693b2aab18SMatthew Ahrens ddrsa.ddrsa_recursive = recursive; 20703a5a36beSmmusante 20713b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check, 20727d46dc6cSMatthew Ahrens dsl_dataset_rename_snapshot_sync, &ddrsa, 20737d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 20743a5a36beSmmusante } 20753a5a36beSmmusante 207691948b51SKeith M Wesolowski /* 207791948b51SKeith M Wesolowski * If we're doing an ownership handoff, we need to make sure that there is 207891948b51SKeith M Wesolowski * only one long hold on the dataset. We're not allowed to change anything here 207991948b51SKeith M Wesolowski * so we don't permanently release the long hold or regular hold here. We want 208091948b51SKeith M Wesolowski * to do this only when syncing to avoid the dataset unexpectedly going away 208191948b51SKeith M Wesolowski * when we release the long hold. 208291948b51SKeith M Wesolowski */ 208391948b51SKeith M Wesolowski static int 208491948b51SKeith M Wesolowski dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) 208591948b51SKeith M Wesolowski { 208691948b51SKeith M Wesolowski boolean_t held; 208791948b51SKeith M Wesolowski 208891948b51SKeith M Wesolowski if (!dmu_tx_is_syncing(tx)) 208991948b51SKeith M Wesolowski return (0); 209091948b51SKeith M Wesolowski 209191948b51SKeith M Wesolowski if (owner != NULL) { 209291948b51SKeith M Wesolowski VERIFY3P(ds->ds_owner, ==, owner); 209391948b51SKeith M Wesolowski dsl_dataset_long_rele(ds, owner); 209491948b51SKeith M Wesolowski } 209591948b51SKeith M Wesolowski 209691948b51SKeith M Wesolowski held = dsl_dataset_long_held(ds); 209791948b51SKeith M Wesolowski 209891948b51SKeith M Wesolowski if (owner != NULL) 209991948b51SKeith M Wesolowski dsl_dataset_long_hold(ds, owner); 210091948b51SKeith M Wesolowski 210191948b51SKeith M Wesolowski if (held) 210291948b51SKeith M Wesolowski return (SET_ERROR(EBUSY)); 210391948b51SKeith M Wesolowski 210491948b51SKeith M Wesolowski return (0); 210591948b51SKeith M Wesolowski } 210691948b51SKeith M Wesolowski 210791948b51SKeith M Wesolowski typedef struct dsl_dataset_rollback_arg { 210891948b51SKeith M Wesolowski const char *ddra_fsname; 210991948b51SKeith M Wesolowski void *ddra_owner; 2110a7027df1SMatthew Ahrens nvlist_t *ddra_result; 211191948b51SKeith M Wesolowski } dsl_dataset_rollback_arg_t; 211291948b51SKeith M Wesolowski 21133b2aab18SMatthew Ahrens static int 21143b2aab18SMatthew Ahrens dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx) 2115fa9e4066Sahrens { 211691948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 21173b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 21181d452cf5Sahrens dsl_dataset_t *ds; 21193b2aab18SMatthew Ahrens int64_t unused_refres_delta; 21203b2aab18SMatthew Ahrens int error; 2121fa9e4066Sahrens 212291948b51SKeith M Wesolowski error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds); 21233b2aab18SMatthew Ahrens if (error != 0) 21243b2aab18SMatthew Ahrens return (error); 2125370c1af0SSanjeev Bagewadi 21263b2aab18SMatthew Ahrens /* must not be a snapshot */ 2127bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 21283b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2129be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21303b2aab18SMatthew Ahrens } 21313a5a36beSmmusante 21323b2aab18SMatthew Ahrens /* must have a most recent snapshot */ 2133c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_txg < TXG_INITIAL) { 21343b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2135be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 21363b2aab18SMatthew Ahrens } 21373a5a36beSmmusante 213878f17100SMatthew Ahrens /* must not have any bookmarks after the most recent snapshot */ 213978f17100SMatthew Ahrens nvlist_t *proprequest = fnvlist_alloc(); 214078f17100SMatthew Ahrens fnvlist_add_boolean(proprequest, zfs_prop_to_name(ZFS_PROP_CREATETXG)); 214178f17100SMatthew Ahrens nvlist_t *bookmarks = fnvlist_alloc(); 214278f17100SMatthew Ahrens error = dsl_get_bookmarks_impl(ds, proprequest, bookmarks); 214378f17100SMatthew Ahrens fnvlist_free(proprequest); 214478f17100SMatthew Ahrens if (error != 0) 214578f17100SMatthew Ahrens return (error); 214678f17100SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(bookmarks, NULL); 214778f17100SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(bookmarks, pair)) { 214878f17100SMatthew Ahrens nvlist_t *valuenv = 214978f17100SMatthew Ahrens fnvlist_lookup_nvlist(fnvpair_value_nvlist(pair), 215078f17100SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CREATETXG)); 215178f17100SMatthew Ahrens uint64_t createtxg = fnvlist_lookup_uint64(valuenv, "value"); 2152c1379625SJustin T. Gibbs if (createtxg > dsl_dataset_phys(ds)->ds_prev_snap_txg) { 215378f17100SMatthew Ahrens fnvlist_free(bookmarks); 215478f17100SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 215578f17100SMatthew Ahrens return (SET_ERROR(EEXIST)); 215678f17100SMatthew Ahrens } 215778f17100SMatthew Ahrens } 215878f17100SMatthew Ahrens fnvlist_free(bookmarks); 215978f17100SMatthew Ahrens 216091948b51SKeith M Wesolowski error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx); 216191948b51SKeith M Wesolowski if (error != 0) { 21623b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 216391948b51SKeith M Wesolowski return (error); 21643b2aab18SMatthew Ahrens } 21653b2aab18SMatthew Ahrens 21663b2aab18SMatthew Ahrens /* 21673b2aab18SMatthew Ahrens * Check if the snap we are rolling back to uses more than 21683b2aab18SMatthew Ahrens * the refquota. 21693b2aab18SMatthew Ahrens */ 21703b2aab18SMatthew Ahrens if (ds->ds_quota != 0 && 2171c1379625SJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_referenced_bytes > ds->ds_quota) { 21723b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2173be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2174fa9e4066Sahrens } 2175370c1af0SSanjeev Bagewadi 21763b2aab18SMatthew Ahrens /* 21773b2aab18SMatthew Ahrens * When we do the clone swap, we will temporarily use more space 21783b2aab18SMatthew Ahrens * due to the refreservation (the head will no longer have any 21793b2aab18SMatthew Ahrens * unique space, so the entire amount of the refreservation will need 21803b2aab18SMatthew Ahrens * to be free). We will immediately destroy the clone, freeing 21813b2aab18SMatthew Ahrens * this space, but the freeing happens over many txg's. 21823b2aab18SMatthew Ahrens */ 21833b2aab18SMatthew Ahrens unused_refres_delta = (int64_t)MIN(ds->ds_reserved, 2184c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_unique_bytes); 21853b2aab18SMatthew Ahrens 21863b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 21873b2aab18SMatthew Ahrens unused_refres_delta > 21883b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) { 21893b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 2190be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 2191fa9e4066Sahrens } 2192fa9e4066Sahrens 21933b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 21943b2aab18SMatthew Ahrens return (0); 21953b2aab18SMatthew Ahrens } 21961d452cf5Sahrens 21973b2aab18SMatthew Ahrens static void 21983b2aab18SMatthew Ahrens dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) 21993b2aab18SMatthew Ahrens { 220091948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t *ddra = arg; 22013b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 22023b2aab18SMatthew Ahrens dsl_dataset_t *ds, *clone; 22033b2aab18SMatthew Ahrens uint64_t cloneobj; 2204a7027df1SMatthew Ahrens char namebuf[ZFS_MAXNAMELEN]; 22051d452cf5Sahrens 220691948b51SKeith M Wesolowski VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); 22071d452cf5Sahrens 2208a7027df1SMatthew Ahrens dsl_dataset_name(ds->ds_prev, namebuf); 2209a7027df1SMatthew Ahrens fnvlist_add_string(ddra->ddra_result, "target", namebuf); 2210a7027df1SMatthew Ahrens 22113b2aab18SMatthew Ahrens cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", 22123b2aab18SMatthew Ahrens ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); 22131d452cf5Sahrens 22143b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone)); 22151d452cf5Sahrens 22163b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(clone, ds, tx); 22173b2aab18SMatthew Ahrens dsl_dataset_zero_zil(ds, tx); 22183b2aab18SMatthew Ahrens 22193b2aab18SMatthew Ahrens dsl_destroy_head_sync_impl(clone, tx); 22203b2aab18SMatthew Ahrens 22213b2aab18SMatthew Ahrens dsl_dataset_rele(clone, FTAG); 22223b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 22233b2aab18SMatthew Ahrens } 22243b2aab18SMatthew Ahrens 222591948b51SKeith M Wesolowski /* 2226a7027df1SMatthew Ahrens * Rolls back the given filesystem or volume to the most recent snapshot. 2227a7027df1SMatthew Ahrens * The name of the most recent snapshot will be returned under key "target" 2228a7027df1SMatthew Ahrens * in the result nvlist. 222991948b51SKeith M Wesolowski * 2230a7027df1SMatthew Ahrens * If owner != NULL: 223191948b51SKeith M Wesolowski * - The existing dataset MUST be owned by the specified owner at entry 223291948b51SKeith M Wesolowski * - Upon return, dataset will still be held by the same owner, whether we 223391948b51SKeith M Wesolowski * succeed or not. 223491948b51SKeith M Wesolowski * 223591948b51SKeith M Wesolowski * This mode is required any time the existing filesystem is mounted. See 223691948b51SKeith M Wesolowski * notes above zfs_suspend_fs() for further details. 223791948b51SKeith M Wesolowski */ 22383b2aab18SMatthew Ahrens int 2239a7027df1SMatthew Ahrens dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result) 22403b2aab18SMatthew Ahrens { 224191948b51SKeith M Wesolowski dsl_dataset_rollback_arg_t ddra; 224291948b51SKeith M Wesolowski 224391948b51SKeith M Wesolowski ddra.ddra_fsname = fsname; 224491948b51SKeith M Wesolowski ddra.ddra_owner = owner; 2245a7027df1SMatthew Ahrens ddra.ddra_result = result; 224691948b51SKeith M Wesolowski 22473b2aab18SMatthew Ahrens return (dsl_sync_task(fsname, dsl_dataset_rollback_check, 22487d46dc6cSMatthew Ahrens dsl_dataset_rollback_sync, &ddra, 22497d46dc6cSMatthew Ahrens 1, ZFS_SPACE_CHECK_RESERVED)); 2250fa9e4066Sahrens } 225199653d4eSeschrock 2252088f3894Sahrens struct promotenode { 2253745cd3c5Smaybee list_node_t link; 2254745cd3c5Smaybee dsl_dataset_t *ds; 2255745cd3c5Smaybee }; 2256745cd3c5Smaybee 22573b2aab18SMatthew Ahrens typedef struct dsl_dataset_promote_arg { 22583b2aab18SMatthew Ahrens const char *ddpa_clonename; 22593b2aab18SMatthew Ahrens dsl_dataset_t *ddpa_clone; 226074e7dc98SMatthew Ahrens list_t shared_snaps, origin_snaps, clone_snaps; 22613b2aab18SMatthew Ahrens dsl_dataset_t *origin_origin; /* origin of the origin */ 226274e7dc98SMatthew Ahrens uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap; 2263681d9761SEric Taylor char *err_ds; 2264a2afb611SJerry Jelinek cred_t *cr; 22653b2aab18SMatthew Ahrens } dsl_dataset_promote_arg_t; 22661d452cf5Sahrens 226774e7dc98SMatthew Ahrens static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep); 22683b2aab18SMatthew Ahrens static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, 22693b2aab18SMatthew Ahrens void *tag); 22703b2aab18SMatthew Ahrens static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag); 227174e7dc98SMatthew Ahrens 227299653d4eSeschrock static int 22733b2aab18SMatthew Ahrens dsl_dataset_promote_check(void *arg, dmu_tx_t *tx) 227499653d4eSeschrock { 22753b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 22763b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 22773b2aab18SMatthew Ahrens dsl_dataset_t *hds; 22783b2aab18SMatthew Ahrens struct promotenode *snap; 22793b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 2280745cd3c5Smaybee int err; 2281cde58dbcSMatthew Ahrens uint64_t unused; 2282a2afb611SJerry Jelinek uint64_t ss_mv_cnt; 2283cb5842f8SAndriy Gapon size_t max_snap_len; 22841d452cf5Sahrens 22853b2aab18SMatthew Ahrens err = promote_hold(ddpa, dp, FTAG); 22863b2aab18SMatthew Ahrens if (err != 0) 22873b2aab18SMatthew Ahrens return (err); 228899653d4eSeschrock 22893b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 2290cb5842f8SAndriy Gapon max_snap_len = MAXNAMELEN - strlen(ddpa->ddpa_clonename) - 1; 22911d452cf5Sahrens 2292c1379625SJustin T. Gibbs if (dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE) { 22933b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2294be6fd75aSMatthew Ahrens return (SET_ERROR(EXDEV)); 22953b2aab18SMatthew Ahrens } 22963b2aab18SMatthew Ahrens 22973b2aab18SMatthew Ahrens /* 22983b2aab18SMatthew Ahrens * Compute and check the amount of space to transfer. Since this is 22993b2aab18SMatthew Ahrens * so expensive, don't do the preliminary check. 23003b2aab18SMatthew Ahrens */ 23013b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 23023b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 23033b2aab18SMatthew Ahrens return (0); 23043b2aab18SMatthew Ahrens } 23053b2aab18SMatthew Ahrens 23063b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 23073b2aab18SMatthew Ahrens origin_ds = snap->ds; 230899653d4eSeschrock 23093cb34c60Sahrens /* compute origin's new unique space */ 23103b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2311c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2312c1379625SJustin T. Gibbs origin_ds->ds_object); 2313cde58dbcSMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 2314c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_prev_snap_txg, UINT64_MAX, 23153b2aab18SMatthew Ahrens &ddpa->unique, &unused, &unused); 231699653d4eSeschrock 2317745cd3c5Smaybee /* 2318745cd3c5Smaybee * Walk the snapshots that we are moving 2319745cd3c5Smaybee * 232074e7dc98SMatthew Ahrens * Compute space to transfer. Consider the incremental changes 23213b2aab18SMatthew Ahrens * to used by each snapshot: 232274e7dc98SMatthew Ahrens * (my used) = (prev's used) + (blocks born) - (blocks killed) 232374e7dc98SMatthew Ahrens * So each snapshot gave birth to: 232474e7dc98SMatthew Ahrens * (blocks born) = (my used) - (prev's used) + (blocks killed) 2325745cd3c5Smaybee * So a sequence would look like: 232674e7dc98SMatthew Ahrens * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0) 2327745cd3c5Smaybee * Which simplifies to: 232874e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + k1 + k0 2329745cd3c5Smaybee * Note however, if we stop before we reach the ORIGIN we get: 233074e7dc98SMatthew Ahrens * uN + kN + kN-1 + ... + kM - uM-1 2331745cd3c5Smaybee */ 2332a2afb611SJerry Jelinek ss_mv_cnt = 0; 2333c1379625SJustin T. Gibbs ddpa->used = dsl_dataset_phys(origin_ds)->ds_referenced_bytes; 2334c1379625SJustin T. Gibbs ddpa->comp = dsl_dataset_phys(origin_ds)->ds_compressed_bytes; 2335c1379625SJustin T. Gibbs ddpa->uncomp = dsl_dataset_phys(origin_ds)->ds_uncompressed_bytes; 23363b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 23373b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 233899653d4eSeschrock uint64_t val, dlused, dlcomp, dluncomp; 2339745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 234099653d4eSeschrock 2341a2afb611SJerry Jelinek ss_mv_cnt++; 2342a2afb611SJerry Jelinek 23433b2aab18SMatthew Ahrens /* 23443b2aab18SMatthew Ahrens * If there are long holds, we won't be able to evict 23453b2aab18SMatthew Ahrens * the objset. 23463b2aab18SMatthew Ahrens */ 23473b2aab18SMatthew Ahrens if (dsl_dataset_long_held(ds)) { 2348be6fd75aSMatthew Ahrens err = SET_ERROR(EBUSY); 23493b2aab18SMatthew Ahrens goto out; 23503b2aab18SMatthew Ahrens } 23513b2aab18SMatthew Ahrens 235299653d4eSeschrock /* Check that the snapshot name does not conflict */ 23533b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 2354cb5842f8SAndriy Gapon if (strlen(ds->ds_snapname) >= max_snap_len) { 2355cb5842f8SAndriy Gapon err = SET_ERROR(ENAMETOOLONG); 2356cb5842f8SAndriy Gapon goto out; 2357cb5842f8SAndriy Gapon } 2358745cd3c5Smaybee err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val); 2359681d9761SEric Taylor if (err == 0) { 23603b2aab18SMatthew Ahrens (void) strcpy(ddpa->err_ds, snap->ds->ds_snapname); 2361be6fd75aSMatthew Ahrens err = SET_ERROR(EEXIST); 2362681d9761SEric Taylor goto out; 2363681d9761SEric Taylor } 2364745cd3c5Smaybee if (err != ENOENT) 2365681d9761SEric Taylor goto out; 236699653d4eSeschrock 2367745cd3c5Smaybee /* The very first snapshot does not have a deadlist */ 2368c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_prev_snap_obj == 0) 236974e7dc98SMatthew Ahrens continue; 237074e7dc98SMatthew Ahrens 2371cde58dbcSMatthew Ahrens dsl_deadlist_space(&ds->ds_deadlist, 2372cde58dbcSMatthew Ahrens &dlused, &dlcomp, &dluncomp); 23733b2aab18SMatthew Ahrens ddpa->used += dlused; 23743b2aab18SMatthew Ahrens ddpa->comp += dlcomp; 23753b2aab18SMatthew Ahrens ddpa->uncomp += dluncomp; 237674e7dc98SMatthew Ahrens } 2377745cd3c5Smaybee 2378745cd3c5Smaybee /* 2379745cd3c5Smaybee * If we are a clone of a clone then we never reached ORIGIN, 2380745cd3c5Smaybee * so we need to subtract out the clone origin's used space. 2381745cd3c5Smaybee */ 23823b2aab18SMatthew Ahrens if (ddpa->origin_origin) { 2383c1379625SJustin T. Gibbs ddpa->used -= 2384c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_referenced_bytes; 2385c1379625SJustin T. Gibbs ddpa->comp -= 2386c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)->ds_compressed_bytes; 23873b2aab18SMatthew Ahrens ddpa->uncomp -= 2388c1379625SJustin T. Gibbs dsl_dataset_phys(ddpa->origin_origin)-> 2389c1379625SJustin T. Gibbs ds_uncompressed_bytes; 239099653d4eSeschrock } 239199653d4eSeschrock 2392a2afb611SJerry Jelinek /* Check that there is enough space and limit headroom here */ 239374e7dc98SMatthew Ahrens err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir, 2394a2afb611SJerry Jelinek 0, ss_mv_cnt, ddpa->used, ddpa->cr); 23953b2aab18SMatthew Ahrens if (err != 0) 23963b2aab18SMatthew Ahrens goto out; 239774e7dc98SMatthew Ahrens 239874e7dc98SMatthew Ahrens /* 239974e7dc98SMatthew Ahrens * Compute the amounts of space that will be used by snapshots 240074e7dc98SMatthew Ahrens * after the promotion (for both origin and clone). For each, 240174e7dc98SMatthew Ahrens * it is the amount of space that will be on all of their 240274e7dc98SMatthew Ahrens * deadlists (that was not born before their new origin). 240374e7dc98SMatthew Ahrens */ 2404c1379625SJustin T. Gibbs if (dsl_dir_phys(hds->ds_dir)->dd_flags & DD_FLAG_USED_BREAKDOWN) { 240574e7dc98SMatthew Ahrens uint64_t space; 240674e7dc98SMatthew Ahrens 240774e7dc98SMatthew Ahrens /* 240874e7dc98SMatthew Ahrens * Note, typically this will not be a clone of a clone, 24093f9d6ad7SLin Ling * so dd_origin_txg will be < TXG_INITIAL, so 2410cde58dbcSMatthew Ahrens * these snaplist_space() -> dsl_deadlist_space_range() 241174e7dc98SMatthew Ahrens * calls will be fast because they do not have to 241274e7dc98SMatthew Ahrens * iterate over all bps. 241374e7dc98SMatthew Ahrens */ 24143b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 24153b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->shared_snaps, 24163b2aab18SMatthew Ahrens snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap); 24173b2aab18SMatthew Ahrens if (err != 0) 24183b2aab18SMatthew Ahrens goto out; 241974e7dc98SMatthew Ahrens 24203b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->clone_snaps, 24213f9d6ad7SLin Ling snap->ds->ds_dir->dd_origin_txg, &space); 24223b2aab18SMatthew Ahrens if (err != 0) 24233b2aab18SMatthew Ahrens goto out; 24243b2aab18SMatthew Ahrens ddpa->cloneusedsnap += space; 242574e7dc98SMatthew Ahrens } 2426c1379625SJustin T. Gibbs if (dsl_dir_phys(origin_ds->ds_dir)->dd_flags & 2427c1379625SJustin T. Gibbs DD_FLAG_USED_BREAKDOWN) { 24283b2aab18SMatthew Ahrens err = snaplist_space(&ddpa->origin_snaps, 2429c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg, 2430c1379625SJustin T. Gibbs &ddpa->originusedsnap); 24313b2aab18SMatthew Ahrens if (err != 0) 24323b2aab18SMatthew Ahrens goto out; 2433745cd3c5Smaybee } 24341d452cf5Sahrens 2435681d9761SEric Taylor out: 24363b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2437681d9761SEric Taylor return (err); 24381d452cf5Sahrens } 243999653d4eSeschrock 24401d452cf5Sahrens static void 24413b2aab18SMatthew Ahrens dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx) 24421d452cf5Sahrens { 24433b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t *ddpa = arg; 24443b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 24453b2aab18SMatthew Ahrens dsl_dataset_t *hds; 24463b2aab18SMatthew Ahrens struct promotenode *snap; 24473b2aab18SMatthew Ahrens dsl_dataset_t *origin_ds; 24483b2aab18SMatthew Ahrens dsl_dataset_t *origin_head; 24493b2aab18SMatthew Ahrens dsl_dir_t *dd; 24503cb34c60Sahrens dsl_dir_t *odd = NULL; 2451088f3894Sahrens uint64_t oldnext_obj; 245274e7dc98SMatthew Ahrens int64_t delta; 24531d452cf5Sahrens 24543b2aab18SMatthew Ahrens VERIFY0(promote_hold(ddpa, dp, FTAG)); 24553b2aab18SMatthew Ahrens hds = ddpa->ddpa_clone; 24563b2aab18SMatthew Ahrens 2457c1379625SJustin T. Gibbs ASSERT0(dsl_dataset_phys(hds)->ds_flags & DS_FLAG_NOPROMOTE); 24581d452cf5Sahrens 24593b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 24603b2aab18SMatthew Ahrens origin_ds = snap->ds; 24613b2aab18SMatthew Ahrens dd = hds->ds_dir; 24623b2aab18SMatthew Ahrens 24633b2aab18SMatthew Ahrens snap = list_head(&ddpa->origin_snaps); 246474e7dc98SMatthew Ahrens origin_head = snap->ds; 246574e7dc98SMatthew Ahrens 24660b69c2f0Sahrens /* 24673cb34c60Sahrens * We need to explicitly open odd, since origin_ds's dd will be 24680b69c2f0Sahrens * changing. 24690b69c2f0Sahrens */ 24703b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object, 24713cb34c60Sahrens NULL, FTAG, &odd)); 247299653d4eSeschrock 2473745cd3c5Smaybee /* change origin's next snap */ 2474745cd3c5Smaybee dmu_buf_will_dirty(origin_ds->ds_dbuf, tx); 2475c1379625SJustin T. Gibbs oldnext_obj = dsl_dataset_phys(origin_ds)->ds_next_snap_obj; 24763b2aab18SMatthew Ahrens snap = list_tail(&ddpa->clone_snaps); 2477c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(snap->ds)->ds_prev_snap_obj, ==, 2478c1379625SJustin T. Gibbs origin_ds->ds_object); 2479c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_snap_obj = snap->ds->ds_object; 2480745cd3c5Smaybee 2481088f3894Sahrens /* change the origin's next clone */ 2482c1379625SJustin T. Gibbs if (dsl_dataset_phys(origin_ds)->ds_next_clones_obj) { 24833b2aab18SMatthew Ahrens dsl_dataset_remove_from_next_clones(origin_ds, 24843b2aab18SMatthew Ahrens snap->ds->ds_object, tx); 24853b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2486c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_next_clones_obj, 2487088f3894Sahrens oldnext_obj, tx)); 2488088f3894Sahrens } 2489088f3894Sahrens 2490745cd3c5Smaybee /* change origin */ 2491745cd3c5Smaybee dmu_buf_will_dirty(dd->dd_dbuf, tx); 2492c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(dd)->dd_origin_obj, ==, origin_ds->ds_object); 2493c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_origin_obj = dsl_dir_phys(odd)->dd_origin_obj; 24943f9d6ad7SLin Ling dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg; 2495745cd3c5Smaybee dmu_buf_will_dirty(odd->dd_dbuf, tx); 2496c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_origin_obj = origin_ds->ds_object; 24973f9d6ad7SLin Ling origin_head->ds_dir->dd_origin_txg = 2498c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_creation_txg; 2499745cd3c5Smaybee 2500cde58dbcSMatthew Ahrens /* change dd_clone entries */ 2501cde58dbcSMatthew Ahrens if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 25023b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2503c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, hds->ds_object, tx)); 25043b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2505c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2506cde58dbcSMatthew Ahrens hds->ds_object, tx)); 2507cde58dbcSMatthew Ahrens 25083b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2509c1379625SJustin T. Gibbs dsl_dir_phys(ddpa->origin_origin->ds_dir)->dd_clones, 2510cde58dbcSMatthew Ahrens origin_head->ds_object, tx)); 2511c1379625SJustin T. Gibbs if (dsl_dir_phys(dd)->dd_clones == 0) { 2512c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones = 2513c1379625SJustin T. Gibbs zap_create(dp->dp_meta_objset, DMU_OT_DSL_CLONES, 2514c1379625SJustin T. Gibbs DMU_OT_NONE, 0, tx); 2515cde58dbcSMatthew Ahrens } 25163b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2517c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, origin_head->ds_object, tx)); 2518cde58dbcSMatthew Ahrens } 2519cde58dbcSMatthew Ahrens 252099653d4eSeschrock /* move snapshots to this dir */ 25213b2aab18SMatthew Ahrens for (snap = list_head(&ddpa->shared_snaps); snap; 25223b2aab18SMatthew Ahrens snap = list_next(&ddpa->shared_snaps, snap)) { 2523745cd3c5Smaybee dsl_dataset_t *ds = snap->ds; 252499653d4eSeschrock 25253b2aab18SMatthew Ahrens /* 25263b2aab18SMatthew Ahrens * Property callbacks are registered to a particular 25273b2aab18SMatthew Ahrens * dsl_dir. Since ours is changing, evict the objset 25283b2aab18SMatthew Ahrens * so that they will be unregistered from the old dsl_dir. 25293b2aab18SMatthew Ahrens */ 2530503ad85cSMatthew Ahrens if (ds->ds_objset) { 2531503ad85cSMatthew Ahrens dmu_objset_evict(ds->ds_objset); 2532503ad85cSMatthew Ahrens ds->ds_objset = NULL; 25333baa08fcSek } 25343b2aab18SMatthew Ahrens 253599653d4eSeschrock /* move snap name entry */ 25363b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_get_snapname(ds)); 25373b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_snap_remove(origin_head, 2538a2afb611SJerry Jelinek ds->ds_snapname, tx, B_TRUE)); 25393b2aab18SMatthew Ahrens VERIFY0(zap_add(dp->dp_meta_objset, 2540c1379625SJustin T. Gibbs dsl_dataset_phys(hds)->ds_snapnames_zapobj, ds->ds_snapname, 254199653d4eSeschrock 8, 1, &ds->ds_object, tx)); 2542a2afb611SJerry Jelinek dsl_fs_ss_count_adjust(hds->ds_dir, 1, 2543a2afb611SJerry Jelinek DD_FIELD_SNAPSHOT_COUNT, tx); 2544cde58dbcSMatthew Ahrens 254599653d4eSeschrock /* change containing dsl_dir */ 254699653d4eSeschrock dmu_buf_will_dirty(ds->ds_dbuf, tx); 2547c1379625SJustin T. Gibbs ASSERT3U(dsl_dataset_phys(ds)->ds_dir_obj, ==, odd->dd_object); 2548c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_dir_obj = dd->dd_object; 25493cb34c60Sahrens ASSERT3P(ds->ds_dir, ==, odd); 25503b2aab18SMatthew Ahrens dsl_dir_rele(ds->ds_dir, ds); 25513b2aab18SMatthew Ahrens VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object, 255299653d4eSeschrock NULL, ds, &ds->ds_dir)); 255399653d4eSeschrock 2554cde58dbcSMatthew Ahrens /* move any clone references */ 2555c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_next_clones_obj && 2556cde58dbcSMatthew Ahrens spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) { 2557cde58dbcSMatthew Ahrens zap_cursor_t zc; 2558cde58dbcSMatthew Ahrens zap_attribute_t za; 2559cde58dbcSMatthew Ahrens 25603b2aab18SMatthew Ahrens for (zap_cursor_init(&zc, dp->dp_meta_objset, 2561c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_next_clones_obj); 25623b2aab18SMatthew Ahrens zap_cursor_retrieve(&zc, &za) == 0; 25633b2aab18SMatthew Ahrens zap_cursor_advance(&zc)) { 25643b2aab18SMatthew Ahrens dsl_dataset_t *cnds; 25653b2aab18SMatthew Ahrens uint64_t o; 2566a9799022Sck 25673b2aab18SMatthew Ahrens if (za.za_first_integer == oldnext_obj) { 25683b2aab18SMatthew Ahrens /* 25693b2aab18SMatthew Ahrens * We've already moved the 25703b2aab18SMatthew Ahrens * origin's reference. 25713b2aab18SMatthew Ahrens */ 25723b2aab18SMatthew Ahrens continue; 25733b2aab18SMatthew Ahrens } 2574a9799022Sck 25753b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, 25763b2aab18SMatthew Ahrens za.za_first_integer, FTAG, &cnds)); 2577c1379625SJustin T. Gibbs o = dsl_dir_phys(cnds->ds_dir)-> 2578c1379625SJustin T. Gibbs dd_head_dataset_obj; 2579a9799022Sck 25803b2aab18SMatthew Ahrens VERIFY0(zap_remove_int(dp->dp_meta_objset, 2581c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_clones, o, tx)); 25823b2aab18SMatthew Ahrens VERIFY0(zap_add_int(dp->dp_meta_objset, 2583c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_clones, o, tx)); 25843b2aab18SMatthew Ahrens dsl_dataset_rele(cnds, FTAG); 25853b2aab18SMatthew Ahrens } 25863b2aab18SMatthew Ahrens zap_cursor_fini(&zc); 25873b2aab18SMatthew Ahrens } 25889082849eSck 25893b2aab18SMatthew Ahrens ASSERT(!dsl_prop_hascb(ds)); 2590a9799022Sck } 2591a9799022Sck 2592a9799022Sck /* 25933b2aab18SMatthew Ahrens * Change space accounting. 25943b2aab18SMatthew Ahrens * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either 25953b2aab18SMatthew Ahrens * both be valid, or both be 0 (resulting in delta == 0). This 25963b2aab18SMatthew Ahrens * is true for each of {clone,origin} independently. 2597a9799022Sck */ 2598a9799022Sck 25993b2aab18SMatthew Ahrens delta = ddpa->cloneusedsnap - 2600c1379625SJustin T. Gibbs dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]; 26013b2aab18SMatthew Ahrens ASSERT3S(delta, >=, 0); 26023b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, delta); 26033b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx); 26043b2aab18SMatthew Ahrens dsl_dir_diduse_space(dd, DD_USED_HEAD, 26053b2aab18SMatthew Ahrens ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx); 26063b2aab18SMatthew Ahrens 26073b2aab18SMatthew Ahrens delta = ddpa->originusedsnap - 2608c1379625SJustin T. Gibbs dsl_dir_phys(odd)->dd_used_breakdown[DD_USED_SNAP]; 26093b2aab18SMatthew Ahrens ASSERT3S(delta, <=, 0); 26103b2aab18SMatthew Ahrens ASSERT3U(ddpa->used, >=, -delta); 26113b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx); 26123b2aab18SMatthew Ahrens dsl_dir_diduse_space(odd, DD_USED_HEAD, 26133b2aab18SMatthew Ahrens -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx); 26143b2aab18SMatthew Ahrens 2615c1379625SJustin T. Gibbs dsl_dataset_phys(origin_ds)->ds_unique_bytes = ddpa->unique; 26163b2aab18SMatthew Ahrens 26173b2aab18SMatthew Ahrens /* log history record */ 26183b2aab18SMatthew Ahrens spa_history_log_internal_ds(hds, "promote", tx, ""); 26193b2aab18SMatthew Ahrens 26203b2aab18SMatthew Ahrens dsl_dir_rele(odd, FTAG); 26213b2aab18SMatthew Ahrens promote_rele(ddpa, FTAG); 2622a9799022Sck } 2623a9799022Sck 26243b2aab18SMatthew Ahrens /* 26253b2aab18SMatthew Ahrens * Make a list of dsl_dataset_t's for the snapshots between first_obj 26263b2aab18SMatthew Ahrens * (exclusive) and last_obj (inclusive). The list will be in reverse 26273b2aab18SMatthew Ahrens * order (last_obj will be the list_head()). If first_obj == 0, do all 26283b2aab18SMatthew Ahrens * snapshots back to this dataset's origin. 26293b2aab18SMatthew Ahrens */ 2630a9799022Sck static int 26313b2aab18SMatthew Ahrens snaplist_make(dsl_pool_t *dp, 26323b2aab18SMatthew Ahrens uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag) 2633a9799022Sck { 26343b2aab18SMatthew Ahrens uint64_t obj = last_obj; 2635a9799022Sck 26363b2aab18SMatthew Ahrens list_create(l, sizeof (struct promotenode), 26373b2aab18SMatthew Ahrens offsetof(struct promotenode, link)); 2638a9799022Sck 26393b2aab18SMatthew Ahrens while (obj != first_obj) { 26403b2aab18SMatthew Ahrens dsl_dataset_t *ds; 26413b2aab18SMatthew Ahrens struct promotenode *snap; 26423b2aab18SMatthew Ahrens int err; 264392241e0bSTom Erickson 26443b2aab18SMatthew Ahrens err = dsl_dataset_hold_obj(dp, obj, tag, &ds); 26453b2aab18SMatthew Ahrens ASSERT(err != ENOENT); 26463b2aab18SMatthew Ahrens if (err != 0) 26473b2aab18SMatthew Ahrens return (err); 2648a9799022Sck 26493b2aab18SMatthew Ahrens if (first_obj == 0) 2650c1379625SJustin T. Gibbs first_obj = dsl_dir_phys(ds->ds_dir)->dd_origin_obj; 26513b2aab18SMatthew Ahrens 26523b2aab18SMatthew Ahrens snap = kmem_alloc(sizeof (*snap), KM_SLEEP); 26533b2aab18SMatthew Ahrens snap->ds = ds; 26543b2aab18SMatthew Ahrens list_insert_tail(l, snap); 2655c1379625SJustin T. Gibbs obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 26563b2aab18SMatthew Ahrens } 2657a9799022Sck 2658a9799022Sck return (0); 2659a9799022Sck } 2660a9799022Sck 26613b2aab18SMatthew Ahrens static int 26623b2aab18SMatthew Ahrens snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep) 2663a9799022Sck { 26643b2aab18SMatthew Ahrens struct promotenode *snap; 2665a9799022Sck 26663b2aab18SMatthew Ahrens *spacep = 0; 26673b2aab18SMatthew Ahrens for (snap = list_head(l); snap; snap = list_next(l, snap)) { 26683b2aab18SMatthew Ahrens uint64_t used, comp, uncomp; 26693b2aab18SMatthew Ahrens dsl_deadlist_space_range(&snap->ds->ds_deadlist, 26703b2aab18SMatthew Ahrens mintxg, UINT64_MAX, &used, &comp, &uncomp); 26713b2aab18SMatthew Ahrens *spacep += used; 267292241e0bSTom Erickson } 26733b2aab18SMatthew Ahrens return (0); 2674a9799022Sck } 2675a9799022Sck 26763b2aab18SMatthew Ahrens static void 26773b2aab18SMatthew Ahrens snaplist_destroy(list_t *l, void *tag) 2678a9799022Sck { 26793b2aab18SMatthew Ahrens struct promotenode *snap; 268092241e0bSTom Erickson 26813b2aab18SMatthew Ahrens if (l == NULL || !list_link_active(&l->list_head)) 26823b2aab18SMatthew Ahrens return; 2683a9799022Sck 26843b2aab18SMatthew Ahrens while ((snap = list_tail(l)) != NULL) { 26853b2aab18SMatthew Ahrens list_remove(l, snap); 26863b2aab18SMatthew Ahrens dsl_dataset_rele(snap->ds, tag); 26873b2aab18SMatthew Ahrens kmem_free(snap, sizeof (*snap)); 26883b2aab18SMatthew Ahrens } 26893b2aab18SMatthew Ahrens list_destroy(l); 2690a9799022Sck } 2691a9799022Sck 2692a9799022Sck static int 26933b2aab18SMatthew Ahrens promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag) 2694a9799022Sck { 26953b2aab18SMatthew Ahrens int error; 26963b2aab18SMatthew Ahrens dsl_dir_t *dd; 26973b2aab18SMatthew Ahrens struct promotenode *snap; 2698a9799022Sck 26993b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag, 27003b2aab18SMatthew Ahrens &ddpa->ddpa_clone); 27013b2aab18SMatthew Ahrens if (error != 0) 27023b2aab18SMatthew Ahrens return (error); 27033b2aab18SMatthew Ahrens dd = ddpa->ddpa_clone->ds_dir; 2704a9799022Sck 2705bc9014e6SJustin Gibbs if (ddpa->ddpa_clone->ds_is_snapshot || 27063b2aab18SMatthew Ahrens !dsl_dir_is_clone(dd)) { 27073b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 2708be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 27093b2aab18SMatthew Ahrens } 2710a9799022Sck 2711c1379625SJustin T. Gibbs error = snaplist_make(dp, 0, dsl_dir_phys(dd)->dd_origin_obj, 27123b2aab18SMatthew Ahrens &ddpa->shared_snaps, tag); 27133b2aab18SMatthew Ahrens if (error != 0) 27143b2aab18SMatthew Ahrens goto out; 2715a9799022Sck 27163b2aab18SMatthew Ahrens error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object, 27173b2aab18SMatthew Ahrens &ddpa->clone_snaps, tag); 27183b2aab18SMatthew Ahrens if (error != 0) 27193b2aab18SMatthew Ahrens goto out; 2720a9799022Sck 27213b2aab18SMatthew Ahrens snap = list_head(&ddpa->shared_snaps); 2722c1379625SJustin T. Gibbs ASSERT3U(snap->ds->ds_object, ==, dsl_dir_phys(dd)->dd_origin_obj); 2723c1379625SJustin T. Gibbs error = snaplist_make(dp, dsl_dir_phys(dd)->dd_origin_obj, 2724c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_head_dataset_obj, 27253b2aab18SMatthew Ahrens &ddpa->origin_snaps, tag); 27263b2aab18SMatthew Ahrens if (error != 0) 27273b2aab18SMatthew Ahrens goto out; 2728379c004dSEric Schrock 2729c1379625SJustin T. Gibbs if (dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj != 0) { 27303b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 2731c1379625SJustin T. Gibbs dsl_dir_phys(snap->ds->ds_dir)->dd_origin_obj, 27323b2aab18SMatthew Ahrens tag, &ddpa->origin_origin); 27333b2aab18SMatthew Ahrens if (error != 0) 27343b2aab18SMatthew Ahrens goto out; 2735379c004dSEric Schrock } 27363b2aab18SMatthew Ahrens out: 27373b2aab18SMatthew Ahrens if (error != 0) 27383b2aab18SMatthew Ahrens promote_rele(ddpa, tag); 27393b2aab18SMatthew Ahrens return (error); 2740a9799022Sck } 2741a9799022Sck 2742a9799022Sck static void 27433b2aab18SMatthew Ahrens promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag) 2744a9799022Sck { 27453b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->shared_snaps, tag); 27463b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->clone_snaps, tag); 27473b2aab18SMatthew Ahrens snaplist_destroy(&ddpa->origin_snaps, tag); 27483b2aab18SMatthew Ahrens if (ddpa->origin_origin != NULL) 27493b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->origin_origin, tag); 27503b2aab18SMatthew Ahrens dsl_dataset_rele(ddpa->ddpa_clone, tag); 27513b2aab18SMatthew Ahrens } 275202c8f3f0SMatthew Ahrens 27533b2aab18SMatthew Ahrens /* 27543b2aab18SMatthew Ahrens * Promote a clone. 27553b2aab18SMatthew Ahrens * 27563b2aab18SMatthew Ahrens * If it fails due to a conflicting snapshot name, "conflsnap" will be filled 27573b2aab18SMatthew Ahrens * in with the name. (It must be at least MAXNAMELEN bytes long.) 27583b2aab18SMatthew Ahrens */ 27593b2aab18SMatthew Ahrens int 27603b2aab18SMatthew Ahrens dsl_dataset_promote(const char *name, char *conflsnap) 27613b2aab18SMatthew Ahrens { 27623b2aab18SMatthew Ahrens dsl_dataset_promote_arg_t ddpa = { 0 }; 27633b2aab18SMatthew Ahrens uint64_t numsnaps; 27643b2aab18SMatthew Ahrens int error; 27653b2aab18SMatthew Ahrens objset_t *os; 276692241e0bSTom Erickson 27673b2aab18SMatthew Ahrens /* 27683b2aab18SMatthew Ahrens * We will modify space proportional to the number of 27693b2aab18SMatthew Ahrens * snapshots. Compute numsnaps. 27703b2aab18SMatthew Ahrens */ 27713b2aab18SMatthew Ahrens error = dmu_objset_hold(name, FTAG, &os); 27723b2aab18SMatthew Ahrens if (error != 0) 27733b2aab18SMatthew Ahrens return (error); 27743b2aab18SMatthew Ahrens error = zap_count(dmu_objset_pool(os)->dp_meta_objset, 2775c1379625SJustin T. Gibbs dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj, 2776c1379625SJustin T. Gibbs &numsnaps); 27773b2aab18SMatthew Ahrens dmu_objset_rele(os, FTAG); 27783b2aab18SMatthew Ahrens if (error != 0) 27793b2aab18SMatthew Ahrens return (error); 278002c8f3f0SMatthew Ahrens 27813b2aab18SMatthew Ahrens ddpa.ddpa_clonename = name; 27823b2aab18SMatthew Ahrens ddpa.err_ds = conflsnap; 2783a2afb611SJerry Jelinek ddpa.cr = CRED(); 278402c8f3f0SMatthew Ahrens 27853b2aab18SMatthew Ahrens return (dsl_sync_task(name, dsl_dataset_promote_check, 27867d46dc6cSMatthew Ahrens dsl_dataset_promote_sync, &ddpa, 27877d46dc6cSMatthew Ahrens 2 + numsnaps, ZFS_SPACE_CHECK_RESERVED)); 2788a9799022Sck } 2789a9799022Sck 2790a9799022Sck int 27913b2aab18SMatthew Ahrens dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone, 279291948b51SKeith M Wesolowski dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx) 2793a9799022Sck { 2794*5f7a8e6dSDan McDonald /* 2795*5f7a8e6dSDan McDonald * "slack" factor for received datasets with refquota set on them. 2796*5f7a8e6dSDan McDonald * See the bottom of this function for details on its use. 2797*5f7a8e6dSDan McDonald */ 2798*5f7a8e6dSDan McDonald uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation; 27993b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2800a9799022Sck 28013b2aab18SMatthew Ahrens /* they should both be heads */ 2802bc9014e6SJustin Gibbs if (clone->ds_is_snapshot || 2803bc9014e6SJustin Gibbs origin_head->ds_is_snapshot) 2804be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 280592241e0bSTom Erickson 280634f2f8cfSMatthew Ahrens /* if we are not forcing, the branch point should be just before them */ 280734f2f8cfSMatthew Ahrens if (!force && clone->ds_prev != origin_head->ds_prev) 2808be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2809a9799022Sck 28103b2aab18SMatthew Ahrens /* clone should be the clone (unless they are unrelated) */ 28113b2aab18SMatthew Ahrens if (clone->ds_prev != NULL && 28123b2aab18SMatthew Ahrens clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap && 281334f2f8cfSMatthew Ahrens origin_head->ds_dir != clone->ds_prev->ds_dir) 2814be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 281592241e0bSTom Erickson 28163b2aab18SMatthew Ahrens /* the clone should be a child of the origin */ 28173b2aab18SMatthew Ahrens if (clone->ds_dir->dd_parent != origin_head->ds_dir) 2818be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 2819842727c2SChris Kirby 28203b2aab18SMatthew Ahrens /* origin_head shouldn't be modified unless 'force' */ 282134f2f8cfSMatthew Ahrens if (!force && 282234f2f8cfSMatthew Ahrens dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev)) 2823be6fd75aSMatthew Ahrens return (SET_ERROR(ETXTBSY)); 2824c99e4bdcSChris Kirby 28253b2aab18SMatthew Ahrens /* origin_head should have no long holds (e.g. is not mounted) */ 282691948b51SKeith M Wesolowski if (dsl_dataset_handoff_check(origin_head, owner, tx)) 2827be6fd75aSMatthew Ahrens return (SET_ERROR(EBUSY)); 28283b2aab18SMatthew Ahrens 28293b2aab18SMatthew Ahrens /* check amount of any unconsumed refreservation */ 28303b2aab18SMatthew Ahrens unused_refres_delta = 28313b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2832c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 28333b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2834c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 28353b2aab18SMatthew Ahrens 28363b2aab18SMatthew Ahrens if (unused_refres_delta > 0 && 28373b2aab18SMatthew Ahrens unused_refres_delta > 28383b2aab18SMatthew Ahrens dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE)) 2839be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 28403b2aab18SMatthew Ahrens 2841*5f7a8e6dSDan McDonald /* 2842*5f7a8e6dSDan McDonald * The clone can't be too much over the head's refquota. 2843*5f7a8e6dSDan McDonald * 2844*5f7a8e6dSDan McDonald * To ensure that the entire refquota can be used, we allow one 2845*5f7a8e6dSDan McDonald * transaction to exceed the the refquota. Therefore, this check 2846*5f7a8e6dSDan McDonald * needs to also allow for the space referenced to be more than the 2847*5f7a8e6dSDan McDonald * refquota. The maximum amount of space that one transaction can use 2848*5f7a8e6dSDan McDonald * on disk is DMU_MAX_ACCESS * spa_asize_inflation. Allowing this 2849*5f7a8e6dSDan McDonald * overage ensures that we are able to receive a filesystem that 2850*5f7a8e6dSDan McDonald * exceeds the refquota on the source system. 2851*5f7a8e6dSDan McDonald * 2852*5f7a8e6dSDan McDonald * So that overage is the refquota_slack we use below. 2853*5f7a8e6dSDan McDonald */ 28543b2aab18SMatthew Ahrens if (origin_head->ds_quota != 0 && 2855c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes > 2856*5f7a8e6dSDan McDonald origin_head->ds_quota + refquota_slack) 2857be6fd75aSMatthew Ahrens return (SET_ERROR(EDQUOT)); 2858c99e4bdcSChris Kirby 28593b2aab18SMatthew Ahrens return (0); 2860c99e4bdcSChris Kirby } 2861c99e4bdcSChris Kirby 2862a7f53a56SChris Kirby void 28633b2aab18SMatthew Ahrens dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone, 28643b2aab18SMatthew Ahrens dsl_dataset_t *origin_head, dmu_tx_t *tx) 2865a7f53a56SChris Kirby { 28663b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 28673b2aab18SMatthew Ahrens int64_t unused_refres_delta; 2868a7f53a56SChris Kirby 28693b2aab18SMatthew Ahrens ASSERT(clone->ds_reserved == 0); 2870*5f7a8e6dSDan McDonald /* 2871*5f7a8e6dSDan McDonald * NOTE: On DEBUG kernels there could be a race between this and 2872*5f7a8e6dSDan McDonald * the check function if spa_asize_inflation is adjusted... 2873*5f7a8e6dSDan McDonald */ 28743b2aab18SMatthew Ahrens ASSERT(origin_head->ds_quota == 0 || 2875*5f7a8e6dSDan McDonald dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota + 2876*5f7a8e6dSDan McDonald DMU_MAX_ACCESS * spa_asize_inflation); 287734f2f8cfSMatthew Ahrens ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev); 2878842727c2SChris Kirby 2879ca0cc391SMatthew Ahrens /* 2880ca0cc391SMatthew Ahrens * Swap per-dataset feature flags. 2881ca0cc391SMatthew Ahrens */ 2882ca0cc391SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2883ca0cc391SMatthew Ahrens if (!(spa_feature_table[f].fi_flags & 2884ca0cc391SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) { 2885ca0cc391SMatthew Ahrens ASSERT(!clone->ds_feature_inuse[f]); 2886ca0cc391SMatthew Ahrens ASSERT(!origin_head->ds_feature_inuse[f]); 2887ca0cc391SMatthew Ahrens continue; 2888ca0cc391SMatthew Ahrens } 2889ca0cc391SMatthew Ahrens 2890ca0cc391SMatthew Ahrens boolean_t clone_inuse = clone->ds_feature_inuse[f]; 2891ca0cc391SMatthew Ahrens boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f]; 2892ca0cc391SMatthew Ahrens 2893ca0cc391SMatthew Ahrens if (clone_inuse) { 2894ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(clone->ds_object, f, tx); 2895ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_FALSE; 2896ca0cc391SMatthew Ahrens } 2897ca0cc391SMatthew Ahrens if (origin_head_inuse) { 2898ca0cc391SMatthew Ahrens dsl_dataset_deactivate_feature(origin_head->ds_object, 2899ca0cc391SMatthew Ahrens f, tx); 2900ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_FALSE; 2901ca0cc391SMatthew Ahrens } 2902ca0cc391SMatthew Ahrens if (clone_inuse) { 2903ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(origin_head->ds_object, 2904ca0cc391SMatthew Ahrens f, tx); 2905ca0cc391SMatthew Ahrens origin_head->ds_feature_inuse[f] = B_TRUE; 2906ca0cc391SMatthew Ahrens } 2907ca0cc391SMatthew Ahrens if (origin_head_inuse) { 2908ca0cc391SMatthew Ahrens dsl_dataset_activate_feature(clone->ds_object, f, tx); 2909ca0cc391SMatthew Ahrens clone->ds_feature_inuse[f] = B_TRUE; 2910ca0cc391SMatthew Ahrens } 2911ca0cc391SMatthew Ahrens } 2912ca0cc391SMatthew Ahrens 29133b2aab18SMatthew Ahrens dmu_buf_will_dirty(clone->ds_dbuf, tx); 29143b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2915842727c2SChris Kirby 29163b2aab18SMatthew Ahrens if (clone->ds_objset != NULL) { 29173b2aab18SMatthew Ahrens dmu_objset_evict(clone->ds_objset); 29183b2aab18SMatthew Ahrens clone->ds_objset = NULL; 29193b2aab18SMatthew Ahrens } 2920842727c2SChris Kirby 29213b2aab18SMatthew Ahrens if (origin_head->ds_objset != NULL) { 29223b2aab18SMatthew Ahrens dmu_objset_evict(origin_head->ds_objset); 29233b2aab18SMatthew Ahrens origin_head->ds_objset = NULL; 2924842727c2SChris Kirby } 2925842727c2SChris Kirby 29263b2aab18SMatthew Ahrens unused_refres_delta = 29273b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2928c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_unique_bytes) - 29293b2aab18SMatthew Ahrens (int64_t)MIN(origin_head->ds_reserved, 2930c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 29313b2aab18SMatthew Ahrens 29323b2aab18SMatthew Ahrens /* 29333b2aab18SMatthew Ahrens * Reset origin's unique bytes, if it exists. 29343b2aab18SMatthew Ahrens */ 29353b2aab18SMatthew Ahrens if (clone->ds_prev) { 29363b2aab18SMatthew Ahrens dsl_dataset_t *origin = clone->ds_prev; 29373b2aab18SMatthew Ahrens uint64_t comp, uncomp; 29383b2aab18SMatthew Ahrens 29393b2aab18SMatthew Ahrens dmu_buf_will_dirty(origin->ds_dbuf, tx); 29403b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 2941c1379625SJustin T. Gibbs dsl_dataset_phys(origin)->ds_prev_snap_txg, UINT64_MAX, 2942c1379625SJustin T. Gibbs &dsl_dataset_phys(origin)->ds_unique_bytes, &comp, &uncomp); 29433b2aab18SMatthew Ahrens } 29443b2aab18SMatthew Ahrens 29453b2aab18SMatthew Ahrens /* swap blkptrs */ 29463b2aab18SMatthew Ahrens { 29473b2aab18SMatthew Ahrens blkptr_t tmp; 2948c1379625SJustin T. Gibbs tmp = dsl_dataset_phys(origin_head)->ds_bp; 2949c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_bp = 2950c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp; 2951c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_bp = tmp; 29523b2aab18SMatthew Ahrens } 29533b2aab18SMatthew Ahrens 29543b2aab18SMatthew Ahrens /* set dd_*_bytes */ 29553b2aab18SMatthew Ahrens { 29563b2aab18SMatthew Ahrens int64_t dused, dcomp, duncomp; 29573b2aab18SMatthew Ahrens uint64_t cdl_used, cdl_comp, cdl_uncomp; 29583b2aab18SMatthew Ahrens uint64_t odl_used, odl_comp, odl_uncomp; 29593b2aab18SMatthew Ahrens 2960c1379625SJustin T. Gibbs ASSERT3U(dsl_dir_phys(clone->ds_dir)-> 29613b2aab18SMatthew Ahrens dd_used_breakdown[DD_USED_SNAP], ==, 0); 29623b2aab18SMatthew Ahrens 29633b2aab18SMatthew Ahrens dsl_deadlist_space(&clone->ds_deadlist, 29643b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 29653b2aab18SMatthew Ahrens dsl_deadlist_space(&origin_head->ds_deadlist, 29663b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 296715508ac0SChris Kirby 2968c1379625SJustin T. Gibbs dused = dsl_dataset_phys(clone)->ds_referenced_bytes + 2969c1379625SJustin T. Gibbs cdl_used - 2970c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_referenced_bytes + 2971c1379625SJustin T. Gibbs odl_used); 2972c1379625SJustin T. Gibbs dcomp = dsl_dataset_phys(clone)->ds_compressed_bytes + 2973c1379625SJustin T. Gibbs cdl_comp - 2974c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_compressed_bytes + 2975c1379625SJustin T. Gibbs odl_comp); 2976c1379625SJustin T. Gibbs duncomp = dsl_dataset_phys(clone)->ds_uncompressed_bytes + 29773b2aab18SMatthew Ahrens cdl_uncomp - 2978c1379625SJustin T. Gibbs (dsl_dataset_phys(origin_head)->ds_uncompressed_bytes + 2979c1379625SJustin T. Gibbs odl_uncomp); 2980842727c2SChris Kirby 29813b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD, 29823b2aab18SMatthew Ahrens dused, dcomp, duncomp, tx); 29833b2aab18SMatthew Ahrens dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD, 29843b2aab18SMatthew Ahrens -dused, -dcomp, -duncomp, tx); 2985842727c2SChris Kirby 2986842727c2SChris Kirby /* 29873b2aab18SMatthew Ahrens * The difference in the space used by snapshots is the 29883b2aab18SMatthew Ahrens * difference in snapshot space due to the head's 29893b2aab18SMatthew Ahrens * deadlist (since that's the only thing that's 29903b2aab18SMatthew Ahrens * changing that affects the snapused). 2991842727c2SChris Kirby */ 29923b2aab18SMatthew Ahrens dsl_deadlist_space_range(&clone->ds_deadlist, 29933b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 29943b2aab18SMatthew Ahrens &cdl_used, &cdl_comp, &cdl_uncomp); 29953b2aab18SMatthew Ahrens dsl_deadlist_space_range(&origin_head->ds_deadlist, 29963b2aab18SMatthew Ahrens origin_head->ds_dir->dd_origin_txg, UINT64_MAX, 29973b2aab18SMatthew Ahrens &odl_used, &odl_comp, &odl_uncomp); 29983b2aab18SMatthew Ahrens dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used, 29993b2aab18SMatthew Ahrens DD_USED_HEAD, DD_USED_SNAP, tx); 3000842727c2SChris Kirby } 3001842727c2SChris Kirby 30023b2aab18SMatthew Ahrens /* swap ds_*_bytes */ 3003c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_referenced_bytes, 3004c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_referenced_bytes); 3005c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_compressed_bytes, 3006c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_compressed_bytes); 3007c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_uncompressed_bytes, 3008c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_uncompressed_bytes); 3009c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_unique_bytes, 3010c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_unique_bytes); 3011842727c2SChris Kirby 30123b2aab18SMatthew Ahrens /* apply any parent delta for change in unconsumed refreservation */ 30133b2aab18SMatthew Ahrens dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV, 30143b2aab18SMatthew Ahrens unused_refres_delta, 0, 0, tx); 3015ca45db41SChris Kirby 30163b2aab18SMatthew Ahrens /* 30173b2aab18SMatthew Ahrens * Swap deadlists. 30183b2aab18SMatthew Ahrens */ 30193b2aab18SMatthew Ahrens dsl_deadlist_close(&clone->ds_deadlist); 30203b2aab18SMatthew Ahrens dsl_deadlist_close(&origin_head->ds_deadlist); 3021c1379625SJustin T. Gibbs SWITCH64(dsl_dataset_phys(origin_head)->ds_deadlist_obj, 3022c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 30233b2aab18SMatthew Ahrens dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset, 3024c1379625SJustin T. Gibbs dsl_dataset_phys(clone)->ds_deadlist_obj); 30253b2aab18SMatthew Ahrens dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset, 3026c1379625SJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_deadlist_obj); 3027842727c2SChris Kirby 30283b2aab18SMatthew Ahrens dsl_scan_ds_clone_swapped(origin_head, clone, tx); 3029842727c2SChris Kirby 30303b2aab18SMatthew Ahrens spa_history_log_internal_ds(clone, "clone swap", tx, 30313b2aab18SMatthew Ahrens "parent=%s", origin_head->ds_dir->dd_myname); 3032842727c2SChris Kirby } 3033842727c2SChris Kirby 30343b2aab18SMatthew Ahrens /* 30353b2aab18SMatthew Ahrens * Given a pool name and a dataset object number in that pool, 30363b2aab18SMatthew Ahrens * return the name of that dataset. 30373b2aab18SMatthew Ahrens */ 3038a7f53a56SChris Kirby int 30393b2aab18SMatthew Ahrens dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf) 3040a7f53a56SChris Kirby { 30413b2aab18SMatthew Ahrens dsl_pool_t *dp; 30423b2aab18SMatthew Ahrens dsl_dataset_t *ds; 3043a7f53a56SChris Kirby int error; 3044a7f53a56SChris Kirby 30453b2aab18SMatthew Ahrens error = dsl_pool_hold(pname, FTAG, &dp); 30463b2aab18SMatthew Ahrens if (error != 0) 30473b2aab18SMatthew Ahrens return (error); 30483b2aab18SMatthew Ahrens 30493b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds); 30503b2aab18SMatthew Ahrens if (error == 0) { 30513b2aab18SMatthew Ahrens dsl_dataset_name(ds, buf); 30523b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 30533b2aab18SMatthew Ahrens } 30543b2aab18SMatthew Ahrens dsl_pool_rele(dp, FTAG); 3055a7f53a56SChris Kirby 3056a7f53a56SChris Kirby return (error); 3057a7f53a56SChris Kirby } 3058a7f53a56SChris Kirby 3059842727c2SChris Kirby int 30603b2aab18SMatthew Ahrens dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota, 30613b2aab18SMatthew Ahrens uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv) 3062842727c2SChris Kirby { 30633b2aab18SMatthew Ahrens int error = 0; 3064842727c2SChris Kirby 30653b2aab18SMatthew Ahrens ASSERT3S(asize, >, 0); 3066842727c2SChris Kirby 30673b2aab18SMatthew Ahrens /* 30683b2aab18SMatthew Ahrens * *ref_rsrv is the portion of asize that will come from any 30693b2aab18SMatthew Ahrens * unconsumed refreservation space. 30703b2aab18SMatthew Ahrens */ 30713b2aab18SMatthew Ahrens *ref_rsrv = 0; 3072842727c2SChris Kirby 30733b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 30743b2aab18SMatthew Ahrens /* 30753b2aab18SMatthew Ahrens * Make a space adjustment for reserved bytes. 30763b2aab18SMatthew Ahrens */ 3077c1379625SJustin T. Gibbs if (ds->ds_reserved > dsl_dataset_phys(ds)->ds_unique_bytes) { 30783b2aab18SMatthew Ahrens ASSERT3U(*used, >=, 3079c1379625SJustin T. Gibbs ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 3080c1379625SJustin T. Gibbs *used -= 3081c1379625SJustin T. Gibbs (ds->ds_reserved - dsl_dataset_phys(ds)->ds_unique_bytes); 30823b2aab18SMatthew Ahrens *ref_rsrv = 30833b2aab18SMatthew Ahrens asize - MIN(asize, parent_delta(ds, asize + inflight)); 3084842727c2SChris Kirby } 3085842727c2SChris Kirby 30863b2aab18SMatthew Ahrens if (!check_quota || ds->ds_quota == 0) { 30873b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 30883b2aab18SMatthew Ahrens return (0); 3089842727c2SChris Kirby } 30903b2aab18SMatthew Ahrens /* 30913b2aab18SMatthew Ahrens * If they are requesting more space, and our current estimate 30923b2aab18SMatthew Ahrens * is over quota, they get to try again unless the actual 30933b2aab18SMatthew Ahrens * on-disk is over quota and there are no pending changes (which 30943b2aab18SMatthew Ahrens * may free up space for us). 30953b2aab18SMatthew Ahrens */ 3096c1379625SJustin T. Gibbs if (dsl_dataset_phys(ds)->ds_referenced_bytes + inflight >= 3097c1379625SJustin T. Gibbs ds->ds_quota) { 30983b2aab18SMatthew Ahrens if (inflight > 0 || 3099c1379625SJustin T. Gibbs dsl_dataset_phys(ds)->ds_referenced_bytes < ds->ds_quota) 3100be6fd75aSMatthew Ahrens error = SET_ERROR(ERESTART); 31013b2aab18SMatthew Ahrens else 3102be6fd75aSMatthew Ahrens error = SET_ERROR(EDQUOT); 3103842727c2SChris Kirby } 31043b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3105842727c2SChris Kirby 3106842727c2SChris Kirby return (error); 3107842727c2SChris Kirby } 3108842727c2SChris Kirby 31093b2aab18SMatthew Ahrens typedef struct dsl_dataset_set_qr_arg { 31103b2aab18SMatthew Ahrens const char *ddsqra_name; 31113b2aab18SMatthew Ahrens zprop_source_t ddsqra_source; 31123b2aab18SMatthew Ahrens uint64_t ddsqra_value; 31133b2aab18SMatthew Ahrens } dsl_dataset_set_qr_arg_t; 3114842727c2SChris Kirby 31153b2aab18SMatthew Ahrens 31163b2aab18SMatthew Ahrens /* ARGSUSED */ 3117842727c2SChris Kirby static int 31183b2aab18SMatthew Ahrens dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx) 3119842727c2SChris Kirby { 31203b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 31213b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 31223b2aab18SMatthew Ahrens dsl_dataset_t *ds; 3123842727c2SChris Kirby int error; 31243b2aab18SMatthew Ahrens uint64_t newval; 3125842727c2SChris Kirby 31263b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA) 3127be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3128842727c2SChris Kirby 31293b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 31303b2aab18SMatthew Ahrens if (error != 0) 31313b2aab18SMatthew Ahrens return (error); 31323b2aab18SMatthew Ahrens 3133bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 31343b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3135be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3136842727c2SChris Kirby } 3137842727c2SChris Kirby 31383b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 31393b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 31403b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 31413b2aab18SMatthew Ahrens if (error != 0) { 31423b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3143842727c2SChris Kirby return (error); 3144842727c2SChris Kirby } 3145842727c2SChris Kirby 31463b2aab18SMatthew Ahrens if (newval == 0) { 31473b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 31483b2aab18SMatthew Ahrens return (0); 31493b2aab18SMatthew Ahrens } 3150842727c2SChris Kirby 3151c1379625SJustin T. Gibbs if (newval < dsl_dataset_phys(ds)->ds_referenced_bytes || 31523b2aab18SMatthew Ahrens newval < ds->ds_reserved) { 31533b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3154be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 31553b2aab18SMatthew Ahrens } 31563b2aab18SMatthew Ahrens 31573b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3158842727c2SChris Kirby return (0); 3159842727c2SChris Kirby } 3160842727c2SChris Kirby 31613b2aab18SMatthew Ahrens static void 31623b2aab18SMatthew Ahrens dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx) 3163842727c2SChris Kirby { 31643b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 31653b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 31663b2aab18SMatthew Ahrens dsl_dataset_t *ds; 31673b2aab18SMatthew Ahrens uint64_t newval; 3168842727c2SChris Kirby 31693b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 3170842727c2SChris Kirby 31713b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, 31723b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), 31733b2aab18SMatthew Ahrens ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, 31743b2aab18SMatthew Ahrens &ddsqra->ddsqra_value, tx); 3175842727c2SChris Kirby 31763b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 31773b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval)); 3178842727c2SChris Kirby 31793b2aab18SMatthew Ahrens if (ds->ds_quota != newval) { 31803b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 31813b2aab18SMatthew Ahrens ds->ds_quota = newval; 3182842727c2SChris Kirby } 31833b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3184842727c2SChris Kirby } 3185842727c2SChris Kirby 31863b2aab18SMatthew Ahrens int 31873b2aab18SMatthew Ahrens dsl_dataset_set_refquota(const char *dsname, zprop_source_t source, 31883b2aab18SMatthew Ahrens uint64_t refquota) 3189842727c2SChris Kirby { 31903b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3191842727c2SChris Kirby 31923b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 31933b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 31943b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refquota; 31953b2aab18SMatthew Ahrens 31963b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check, 31977d46dc6cSMatthew Ahrens dsl_dataset_set_refquota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); 3198842727c2SChris Kirby } 3199842727c2SChris Kirby 3200842727c2SChris Kirby static int 32013b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx) 3202842727c2SChris Kirby { 32033b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 32043b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3205842727c2SChris Kirby dsl_dataset_t *ds; 3206842727c2SChris Kirby int error; 32073b2aab18SMatthew Ahrens uint64_t newval, unique; 3208d7747cbcSChris Kirby 32093b2aab18SMatthew Ahrens if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION) 3210be6fd75aSMatthew Ahrens return (SET_ERROR(ENOTSUP)); 3211842727c2SChris Kirby 32123b2aab18SMatthew Ahrens error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); 32133b2aab18SMatthew Ahrens if (error != 0) 3214842727c2SChris Kirby return (error); 3215842727c2SChris Kirby 3216bc9014e6SJustin Gibbs if (ds->ds_is_snapshot) { 32173b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3218be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 3219842727c2SChris Kirby } 3220842727c2SChris Kirby 32213b2aab18SMatthew Ahrens error = dsl_prop_predict(ds->ds_dir, 32223b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 32233b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); 32243b2aab18SMatthew Ahrens if (error != 0) { 32253b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3226842727c2SChris Kirby return (error); 3227842727c2SChris Kirby } 3228842727c2SChris Kirby 32293b2aab18SMatthew Ahrens /* 32303b2aab18SMatthew Ahrens * If we are doing the preliminary check in open context, the 32313b2aab18SMatthew Ahrens * space estimates may be inaccurate. 32323b2aab18SMatthew Ahrens */ 32333b2aab18SMatthew Ahrens if (!dmu_tx_is_syncing(tx)) { 32343b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 32353b2aab18SMatthew Ahrens return (0); 3236842727c2SChris Kirby } 3237842727c2SChris Kirby 32383b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 32393b2aab18SMatthew Ahrens if (!DS_UNIQUE_IS_ACCURATE(ds)) 32403b2aab18SMatthew Ahrens dsl_dataset_recalc_head_uniq(ds); 3241c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 32423b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3243842727c2SChris Kirby 32443b2aab18SMatthew Ahrens if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) { 32453b2aab18SMatthew Ahrens uint64_t delta = MAX(unique, newval) - 32463b2aab18SMatthew Ahrens MAX(unique, ds->ds_reserved); 3247842727c2SChris Kirby 32483b2aab18SMatthew Ahrens if (delta > 32493b2aab18SMatthew Ahrens dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) || 32503b2aab18SMatthew Ahrens (ds->ds_quota > 0 && newval > ds->ds_quota)) { 32513b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 3252be6fd75aSMatthew Ahrens return (SET_ERROR(ENOSPC)); 32533b2aab18SMatthew Ahrens } 3254842727c2SChris Kirby } 3255842727c2SChris Kirby 32563b2aab18SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 32573b2aab18SMatthew Ahrens return (0); 3258842727c2SChris Kirby } 3259842727c2SChris Kirby 32603b2aab18SMatthew Ahrens void 32613b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 32623b2aab18SMatthew Ahrens zprop_source_t source, uint64_t value, dmu_tx_t *tx) 3263ca45db41SChris Kirby { 32643b2aab18SMatthew Ahrens uint64_t newval; 32653b2aab18SMatthew Ahrens uint64_t unique; 32663b2aab18SMatthew Ahrens int64_t delta; 3267ca45db41SChris Kirby 32683b2aab18SMatthew Ahrens dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 32693b2aab18SMatthew Ahrens source, sizeof (value), 1, &value, tx); 3270ca45db41SChris Kirby 32713b2aab18SMatthew Ahrens VERIFY0(dsl_prop_get_int_ds(ds, 32723b2aab18SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval)); 3273a7f53a56SChris Kirby 32743b2aab18SMatthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx); 32753b2aab18SMatthew Ahrens mutex_enter(&ds->ds_dir->dd_lock); 32763b2aab18SMatthew Ahrens mutex_enter(&ds->ds_lock); 32773b2aab18SMatthew Ahrens ASSERT(DS_UNIQUE_IS_ACCURATE(ds)); 3278c1379625SJustin T. Gibbs unique = dsl_dataset_phys(ds)->ds_unique_bytes; 32793b2aab18SMatthew Ahrens delta = MAX(0, (int64_t)(newval - unique)) - 32803b2aab18SMatthew Ahrens MAX(0, (int64_t)(ds->ds_reserved - unique)); 32813b2aab18SMatthew Ahrens ds->ds_reserved = newval; 32823b2aab18SMatthew Ahrens mutex_exit(&ds->ds_lock); 3283a7f53a56SChris Kirby 32843b2aab18SMatthew Ahrens dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx); 32853b2aab18SMatthew Ahrens mutex_exit(&ds->ds_dir->dd_lock); 3286ca45db41SChris Kirby } 3287ca45db41SChris Kirby 32883b2aab18SMatthew Ahrens static void 32893b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx) 3290842727c2SChris Kirby { 32913b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t *ddsqra = arg; 32923b2aab18SMatthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx); 3293842727c2SChris Kirby dsl_dataset_t *ds; 3294842727c2SChris Kirby 32953b2aab18SMatthew Ahrens VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); 32963b2aab18SMatthew Ahrens dsl_dataset_set_refreservation_sync_impl(ds, 32973b2aab18SMatthew Ahrens ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx); 3298842727c2SChris Kirby dsl_dataset_rele(ds, FTAG); 3299842727c2SChris Kirby } 3300503ad85cSMatthew Ahrens 3301503ad85cSMatthew Ahrens int 33023b2aab18SMatthew Ahrens dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source, 33033b2aab18SMatthew Ahrens uint64_t refreservation) 3304503ad85cSMatthew Ahrens { 33053b2aab18SMatthew Ahrens dsl_dataset_set_qr_arg_t ddsqra; 3306503ad85cSMatthew Ahrens 33073b2aab18SMatthew Ahrens ddsqra.ddsqra_name = dsname; 33083b2aab18SMatthew Ahrens ddsqra.ddsqra_source = source; 33093b2aab18SMatthew Ahrens ddsqra.ddsqra_value = refreservation; 33103b2aab18SMatthew Ahrens 33113b2aab18SMatthew Ahrens return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check, 33127d46dc6cSMatthew Ahrens dsl_dataset_set_refreservation_sync, &ddsqra, 33137d46dc6cSMatthew Ahrens 0, ZFS_SPACE_CHECK_NONE)); 3314503ad85cSMatthew Ahrens } 331519b94df9SMatthew Ahrens 331619b94df9SMatthew Ahrens /* 331719b94df9SMatthew Ahrens * Return (in *usedp) the amount of space written in new that is not 331819b94df9SMatthew Ahrens * present in oldsnap. New may be a snapshot or the head. Old must be 331919b94df9SMatthew Ahrens * a snapshot before new, in new's filesystem (or its origin). If not then 332019b94df9SMatthew Ahrens * fail and return EINVAL. 332119b94df9SMatthew Ahrens * 332219b94df9SMatthew Ahrens * The written space is calculated by considering two components: First, we 332319b94df9SMatthew Ahrens * ignore any freed space, and calculate the written as new's used space 332419b94df9SMatthew Ahrens * minus old's used space. Next, we add in the amount of space that was freed 332519b94df9SMatthew Ahrens * between the two snapshots, thus reducing new's used space relative to old's. 332619b94df9SMatthew Ahrens * Specifically, this is the space that was born before old->ds_creation_txg, 332719b94df9SMatthew Ahrens * and freed before new (ie. on new's deadlist or a previous deadlist). 332819b94df9SMatthew Ahrens * 332919b94df9SMatthew Ahrens * space freed [---------------------] 333019b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O------ 333119b94df9SMatthew Ahrens * oldsnap new 333219b94df9SMatthew Ahrens */ 333319b94df9SMatthew Ahrens int 333419b94df9SMatthew Ahrens dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new, 333519b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 333619b94df9SMatthew Ahrens { 333719b94df9SMatthew Ahrens int err = 0; 333819b94df9SMatthew Ahrens uint64_t snapobj; 333919b94df9SMatthew Ahrens dsl_pool_t *dp = new->ds_dir->dd_pool; 334019b94df9SMatthew Ahrens 33413b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 33423b2aab18SMatthew Ahrens 334319b94df9SMatthew Ahrens *usedp = 0; 3344c1379625SJustin T. Gibbs *usedp += dsl_dataset_phys(new)->ds_referenced_bytes; 3345c1379625SJustin T. Gibbs *usedp -= dsl_dataset_phys(oldsnap)->ds_referenced_bytes; 334619b94df9SMatthew Ahrens 334719b94df9SMatthew Ahrens *compp = 0; 3348c1379625SJustin T. Gibbs *compp += dsl_dataset_phys(new)->ds_compressed_bytes; 3349c1379625SJustin T. Gibbs *compp -= dsl_dataset_phys(oldsnap)->ds_compressed_bytes; 335019b94df9SMatthew Ahrens 335119b94df9SMatthew Ahrens *uncompp = 0; 3352c1379625SJustin T. Gibbs *uncompp += dsl_dataset_phys(new)->ds_uncompressed_bytes; 3353c1379625SJustin T. Gibbs *uncompp -= dsl_dataset_phys(oldsnap)->ds_uncompressed_bytes; 335419b94df9SMatthew Ahrens 335519b94df9SMatthew Ahrens snapobj = new->ds_object; 335619b94df9SMatthew Ahrens while (snapobj != oldsnap->ds_object) { 335719b94df9SMatthew Ahrens dsl_dataset_t *snap; 335819b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 335919b94df9SMatthew Ahrens 3360ad135b5dSChristopher Siden if (snapobj == new->ds_object) { 3361ad135b5dSChristopher Siden snap = new; 3362ad135b5dSChristopher Siden } else { 3363ad135b5dSChristopher Siden err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap); 3364ad135b5dSChristopher Siden if (err != 0) 3365ad135b5dSChristopher Siden break; 3366ad135b5dSChristopher Siden } 336719b94df9SMatthew Ahrens 3368c1379625SJustin T. Gibbs if (dsl_dataset_phys(snap)->ds_prev_snap_txg == 3369c1379625SJustin T. Gibbs dsl_dataset_phys(oldsnap)->ds_creation_txg) { 337019b94df9SMatthew Ahrens /* 337119b94df9SMatthew Ahrens * The blocks in the deadlist can not be born after 337219b94df9SMatthew Ahrens * ds_prev_snap_txg, so get the whole deadlist space, 337319b94df9SMatthew Ahrens * which is more efficient (especially for old-format 337419b94df9SMatthew Ahrens * deadlists). Unfortunately the deadlist code 337519b94df9SMatthew Ahrens * doesn't have enough information to make this 337619b94df9SMatthew Ahrens * optimization itself. 337719b94df9SMatthew Ahrens */ 337819b94df9SMatthew Ahrens dsl_deadlist_space(&snap->ds_deadlist, 337919b94df9SMatthew Ahrens &used, &comp, &uncomp); 338019b94df9SMatthew Ahrens } else { 338119b94df9SMatthew Ahrens dsl_deadlist_space_range(&snap->ds_deadlist, 3382c1379625SJustin T. Gibbs 0, dsl_dataset_phys(oldsnap)->ds_creation_txg, 338319b94df9SMatthew Ahrens &used, &comp, &uncomp); 338419b94df9SMatthew Ahrens } 338519b94df9SMatthew Ahrens *usedp += used; 338619b94df9SMatthew Ahrens *compp += comp; 338719b94df9SMatthew Ahrens *uncompp += uncomp; 338819b94df9SMatthew Ahrens 338919b94df9SMatthew Ahrens /* 339019b94df9SMatthew Ahrens * If we get to the beginning of the chain of snapshots 339119b94df9SMatthew Ahrens * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap 339219b94df9SMatthew Ahrens * was not a snapshot of/before new. 339319b94df9SMatthew Ahrens */ 3394c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 3395ad135b5dSChristopher Siden if (snap != new) 3396ad135b5dSChristopher Siden dsl_dataset_rele(snap, FTAG); 339719b94df9SMatthew Ahrens if (snapobj == 0) { 3398be6fd75aSMatthew Ahrens err = SET_ERROR(EINVAL); 339919b94df9SMatthew Ahrens break; 340019b94df9SMatthew Ahrens } 340119b94df9SMatthew Ahrens 340219b94df9SMatthew Ahrens } 340319b94df9SMatthew Ahrens return (err); 340419b94df9SMatthew Ahrens } 340519b94df9SMatthew Ahrens 340619b94df9SMatthew Ahrens /* 340719b94df9SMatthew Ahrens * Return (in *usedp) the amount of space that will be reclaimed if firstsnap, 340819b94df9SMatthew Ahrens * lastsnap, and all snapshots in between are deleted. 340919b94df9SMatthew Ahrens * 341019b94df9SMatthew Ahrens * blocks that would be freed [---------------------------] 341119b94df9SMatthew Ahrens * snapshots ---O-------O--------O-------O--------O 341219b94df9SMatthew Ahrens * firstsnap lastsnap 341319b94df9SMatthew Ahrens * 341419b94df9SMatthew Ahrens * This is the set of blocks that were born after the snap before firstsnap, 341519b94df9SMatthew Ahrens * (birth > firstsnap->prev_snap_txg) and died before the snap after the 341619b94df9SMatthew Ahrens * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist). 341719b94df9SMatthew Ahrens * We calculate this by iterating over the relevant deadlists (from the snap 341819b94df9SMatthew Ahrens * after lastsnap, backward to the snap after firstsnap), summing up the 341919b94df9SMatthew Ahrens * space on the deadlist that was born after the snap before firstsnap. 342019b94df9SMatthew Ahrens */ 342119b94df9SMatthew Ahrens int 342219b94df9SMatthew Ahrens dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, 342319b94df9SMatthew Ahrens dsl_dataset_t *lastsnap, 342419b94df9SMatthew Ahrens uint64_t *usedp, uint64_t *compp, uint64_t *uncompp) 342519b94df9SMatthew Ahrens { 342619b94df9SMatthew Ahrens int err = 0; 342719b94df9SMatthew Ahrens uint64_t snapobj; 342819b94df9SMatthew Ahrens dsl_pool_t *dp = firstsnap->ds_dir->dd_pool; 342919b94df9SMatthew Ahrens 3430bc9014e6SJustin Gibbs ASSERT(firstsnap->ds_is_snapshot); 3431bc9014e6SJustin Gibbs ASSERT(lastsnap->ds_is_snapshot); 343219b94df9SMatthew Ahrens 343319b94df9SMatthew Ahrens /* 343419b94df9SMatthew Ahrens * Check that the snapshots are in the same dsl_dir, and firstsnap 343519b94df9SMatthew Ahrens * is before lastsnap. 343619b94df9SMatthew Ahrens */ 343719b94df9SMatthew Ahrens if (firstsnap->ds_dir != lastsnap->ds_dir || 3438c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_creation_txg > 3439c1379625SJustin T. Gibbs dsl_dataset_phys(lastsnap)->ds_creation_txg) 3440be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 344119b94df9SMatthew Ahrens 344219b94df9SMatthew Ahrens *usedp = *compp = *uncompp = 0; 344319b94df9SMatthew Ahrens 3444c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(lastsnap)->ds_next_snap_obj; 344519b94df9SMatthew Ahrens while (snapobj != firstsnap->ds_object) { 344619b94df9SMatthew Ahrens dsl_dataset_t *ds; 344719b94df9SMatthew Ahrens uint64_t used, comp, uncomp; 344819b94df9SMatthew Ahrens 344919b94df9SMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds); 345019b94df9SMatthew Ahrens if (err != 0) 345119b94df9SMatthew Ahrens break; 345219b94df9SMatthew Ahrens 345319b94df9SMatthew Ahrens dsl_deadlist_space_range(&ds->ds_deadlist, 3454c1379625SJustin T. Gibbs dsl_dataset_phys(firstsnap)->ds_prev_snap_txg, UINT64_MAX, 345519b94df9SMatthew Ahrens &used, &comp, &uncomp); 345619b94df9SMatthew Ahrens *usedp += used; 345719b94df9SMatthew Ahrens *compp += comp; 345819b94df9SMatthew Ahrens *uncompp += uncomp; 345919b94df9SMatthew Ahrens 3460c1379625SJustin T. Gibbs snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 346119b94df9SMatthew Ahrens ASSERT3U(snapobj, !=, 0); 346219b94df9SMatthew Ahrens dsl_dataset_rele(ds, FTAG); 346319b94df9SMatthew Ahrens } 346419b94df9SMatthew Ahrens return (err); 346519b94df9SMatthew Ahrens } 34663b2aab18SMatthew Ahrens 34673b2aab18SMatthew Ahrens /* 34683b2aab18SMatthew Ahrens * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline. 34693b2aab18SMatthew Ahrens * For example, they could both be snapshots of the same filesystem, and 34703b2aab18SMatthew Ahrens * 'earlier' is before 'later'. Or 'earlier' could be the origin of 34713b2aab18SMatthew Ahrens * 'later's filesystem. Or 'earlier' could be an older snapshot in the origin's 34723b2aab18SMatthew Ahrens * filesystem. Or 'earlier' could be the origin's origin. 347378f17100SMatthew Ahrens * 347478f17100SMatthew Ahrens * If non-zero, earlier_txg is used instead of earlier's ds_creation_txg. 34753b2aab18SMatthew Ahrens */ 34763b2aab18SMatthew Ahrens boolean_t 347778f17100SMatthew Ahrens dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier, 34789a686fbcSPaul Dagnelie uint64_t earlier_txg) 34793b2aab18SMatthew Ahrens { 34803b2aab18SMatthew Ahrens dsl_pool_t *dp = later->ds_dir->dd_pool; 34813b2aab18SMatthew Ahrens int error; 34823b2aab18SMatthew Ahrens boolean_t ret; 34833b2aab18SMatthew Ahrens 34843b2aab18SMatthew Ahrens ASSERT(dsl_pool_config_held(dp)); 3485bc9014e6SJustin Gibbs ASSERT(earlier->ds_is_snapshot || earlier_txg != 0); 348678f17100SMatthew Ahrens 348778f17100SMatthew Ahrens if (earlier_txg == 0) 3488c1379625SJustin T. Gibbs earlier_txg = dsl_dataset_phys(earlier)->ds_creation_txg; 34893b2aab18SMatthew Ahrens 3490bc9014e6SJustin Gibbs if (later->ds_is_snapshot && 3491c1379625SJustin T. Gibbs earlier_txg >= dsl_dataset_phys(later)->ds_creation_txg) 34923b2aab18SMatthew Ahrens return (B_FALSE); 34933b2aab18SMatthew Ahrens 34943b2aab18SMatthew Ahrens if (later->ds_dir == earlier->ds_dir) 34953b2aab18SMatthew Ahrens return (B_TRUE); 34963b2aab18SMatthew Ahrens if (!dsl_dir_is_clone(later->ds_dir)) 34973b2aab18SMatthew Ahrens return (B_FALSE); 34983b2aab18SMatthew Ahrens 3499c1379625SJustin T. Gibbs if (dsl_dir_phys(later->ds_dir)->dd_origin_obj == earlier->ds_object) 35003b2aab18SMatthew Ahrens return (B_TRUE); 35013b2aab18SMatthew Ahrens dsl_dataset_t *origin; 35023b2aab18SMatthew Ahrens error = dsl_dataset_hold_obj(dp, 3503c1379625SJustin T. Gibbs dsl_dir_phys(later->ds_dir)->dd_origin_obj, FTAG, &origin); 35043b2aab18SMatthew Ahrens if (error != 0) 35053b2aab18SMatthew Ahrens return (B_FALSE); 350678f17100SMatthew Ahrens ret = dsl_dataset_is_before(origin, earlier, earlier_txg); 35073b2aab18SMatthew Ahrens dsl_dataset_rele(origin, FTAG); 35083b2aab18SMatthew Ahrens return (ret); 35093b2aab18SMatthew Ahrens } 35102acef22dSMatthew Ahrens 35112acef22dSMatthew Ahrens void 35122acef22dSMatthew Ahrens dsl_dataset_zapify(dsl_dataset_t *ds, dmu_tx_t *tx) 35132acef22dSMatthew Ahrens { 35142acef22dSMatthew Ahrens objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset; 35152acef22dSMatthew Ahrens dmu_object_zapify(mos, ds->ds_object, DMU_OT_DSL_DATASET, tx); 35162acef22dSMatthew Ahrens } 35179c3fd121SMatthew Ahrens 35189c3fd121SMatthew Ahrens boolean_t 35199c3fd121SMatthew Ahrens dsl_dataset_is_zapified(dsl_dataset_t *ds) 35209c3fd121SMatthew Ahrens { 35219c3fd121SMatthew Ahrens dmu_object_info_t doi; 35229c3fd121SMatthew Ahrens 35239c3fd121SMatthew Ahrens dmu_object_info_from_db(ds->ds_dbuf, &doi); 35249c3fd121SMatthew Ahrens return (doi.doi_type == DMU_OTN_ZAP_METADATA); 35259c3fd121SMatthew Ahrens } 35269c3fd121SMatthew Ahrens 35279c3fd121SMatthew Ahrens boolean_t 35289c3fd121SMatthew Ahrens dsl_dataset_has_resume_receive_state(dsl_dataset_t *ds) 35299c3fd121SMatthew Ahrens { 35309c3fd121SMatthew Ahrens return (dsl_dataset_is_zapified(ds) && 35319c3fd121SMatthew Ahrens zap_contains(ds->ds_dir->dd_pool->dp_meta_objset, 35329c3fd121SMatthew Ahrens ds->ds_object, DS_FIELD_RESUME_TOGUID) == 0); 35339c3fd121SMatthew Ahrens } 3534